Linux process address space and memory region operation

PUBLISHED ON NOV 8, 2015 — LINUX KERNEL

Japanese version

Linux kernel source code reading memo at memory region operation.

Linux version

v4.1.10

Data structure

Kernel manage virtual address space seen from user process as task_struct->mm. Assigned memory regions is soreted in address order by red-black tree mm_struct->mm_rb and manged in the unit of vm_area_struct

vm_area_struct points the start and end addresses.

unsigned long vm_start;         /* Our start address within vm_mm. */
unsigned long vm_end;           /* The first byte after our end address within vm_mm. */

These data structures only manages the allocation and deallocation of memory in the kernel, Page table may be managed another place.

Functions to manipulate memory region directly

Almost all functions start from searching address (range) from mm_rb. and each process have the cache to omit searching. mm->mmap_cache in v2.6 is task_struct->vmacache currently, and it can be stored four vma cache for each process. vmacache_* functions are used for manipulating it.

find_vma

Find minimum vm_area_struct under addr < vm_end. It’s not necessary to contain the addr in the vm_area_struct

  1. Find current->vmacache
  2. Find mm_rb, if tareget vma found, return, else NULL.

find_vma_intersection

Returns the vma that overlaps with argumented address range, start_addr to end_addr. That means to test the return value of find_vma(mm, start_addr) is end_addr <= vma.vm_start, or not.

Another functions

There are functions for searching free range, and adding regions.

Adding page table

The relevant functions with do_mmap has changed from v2.6.x. populate_vma_page_range() is function to add memory to page table, not make_page_present() currently.

Reference

Chapter 9 of Understanding the Linux Kernel.

comments powered by Disqus