Chapter 4: Virtual Process Memory
The actual iteration begins either at the address of the last ‘‘hole‘‘ in the virtual address space or at the
global start addressTASK_UNMAPPED_BASE.
mm/mmap.c
full_search:
for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
/* At this point: (!vma || addr < vma->vm_end). */
if (TASK_SIZE - len < addr) {
/*
* Start a new search - just in case we missed
* some holes.
*/
if (start_addr != TASK_UNMAPPED_BASE) {
addr = TASK_UNMAPPED_BASE;
start_addr = addr;
mm->cached_hole_size = 0;
goto full_search;
}
return -ENOMEM;
}
if (!vma || addr + len <= vma->vm_start) {
/*
* Remember the place where we stopped the search:
*/
mm->free_area_cache = addr + len;
return addr;
}
if (addr + mm->cached_hole_size < vma->vm_start)
mm->cached_hole_size = vma->vm_start - addr;
addr = vma->vm_end;
}
}
If the search continues to the end of the user address space (TASK_SIZE) and no suitable area is found, the
kernel returns an-ENOMEMerror that must be forwarded to userspace for processing by the relevant appli-
cation, as it indicates that insufficient virtual address space memory is available to satisfy the request. If
memory is found, its virtual start address is returned.
The version for top-down allocation,arch_get_unmapped_area_topdown, progresses similarly, but the
search direction is, of course, reversed. We neednot bother with the details of implementation here.
4.6 Address Spaces
Memory mappings of files can be regarded as mappings between two different address spaces to simplify
the work of (system) programmers. One address space is the virtual memory address space of the user
process, the other is the address space spanned by the filesystem.
When the kernel creates a mapping, it must create a link between the address spaces to support com-
munication between the two — in the form of read and write requests. Thevm_operations_struct
structure with which we are familiar from Section 4.4.2 is first used to do this. It provides an operation to
read pages not yet in physical memory although their contents have already been mapped there.