Chapter 4: Virtual Process Memory
find_vma_intersectionis another helper function to establish whether an interval bounded bystart_
addrandend_addris fully within an existing region. It builds onfind_vmaand is easily implemented as
follows:
<mm.h>
static inline
struct vm_area_struct * find_vma_intersection(struct mm_struct * mm,
unsigned long start_addr,
unsigned long end_addr)
{
struct vm_area_struct * vma = find_vma(mm,start_addr);
if (vma && end_addr <= vma->vm_start)
vma = NULL;
return vma;
}
4.5.2 Merging Regions
When a new region is added to the address space of a process, the kernel checks whether it can be merged
with one or more existing regions as shown in Figure 4-10.
vm_mergemerges a new region with the surrounding regions if this is possible. It requires numerous
parameters.
mm/mmap.c
struct vm_area_struct *vma_merge(struct mm_struct *mm,
struct vm_area_struct *prev, unsigned long addr,
unsigned long end, unsigned long vm_flags,
struct anon_vma *anon_vma, struct file *file,
pgoff_t pgoff, struct mempolicy *policy)
{
pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
struct vm_area_struct *area, *next;
...
mmis the address space instance of the relevant process andprevthe region immediatelybeforethe new
region.rb_parentis the parent element of the region in the red-black search tree.
addr,end,andvm_flagsdescribe the start, end, and flags of thenew region as their names suggest. If
the region belongs to a file mapping,filecontains a pointer to thefileinstance that identifies the file.
pgoffspecifies the offset of the mapping within the file data. Sincepolicyis required on NUMA systems
only, I won’t discuss it further.
The technical details of implementation are very straightforward. A check is first made to ascertain
whether the end address of the predecessor region corresponds to the start address of the new region.
If so, the kernel must then check that the flags and the mapped file are identical for both regions, that
the offsets of file mappings are such that a contiguous region results, that both regions do not con-
tain anonymous mappings, and that both regions are mutually compatible.^6 This is done using the
(^6) The regions cannot be merged if two file mappings follow each other without a hole but map non-contiguous sections of the file.