Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 4: Virtual Process Memory


mm/mmap.c
unsigned long
get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
unsigned long pgoff, unsigned long flags)

The arguments are self-explanatory. The implementation of the function is of no further interest as the
actual work is delegated to the architecture-specific helper function stored in themm_structinstance of
the current process.^8


Recall from Section 4.2 that different mapping functions are used depending on the layout of the virtual
process address space. Here I consider the standard functionarch_get_unmapped_areathat is employed
on most systems.


arch_get_unmapped_areafirst has to check whether theMAP_FIXEDflag is set, indicating that the map-
ping is to be created at a fixed address. If so, the kernel ensures only that the address satisfies alignment
requirements (page-by-page) and that the interval is fully within the available address space.


If no desired area was specified, the kernel tries to find a suitable section in the virtual memory area of the
process by invokingarch_get_unmapped_area. If a particular preferred (as opposed to a fixed) address
is specified, the kernel checks whether the region overlaps with an existing region. If not, the address can
be returned as the target.


mm/mmap.c
unsigned long
arch_get_unmapped_area(struct file *filp, unsigned long addr,
unsigned long len, unsigned long pgoff, unsigned long flags)
{
struct mm_struct *mm = current->mm;
...

if (addr) {
addr = PAGE_ALIGN(addr);
vma = find_vma(mm, addr);
if (TASK_SIZE - len >= addr &&
(!vma || addr + len <= vma->vm_start))
return addr;
}
...

Otherwise, the kernel must try to find a free area of the right size by iterating over the available regions
of the process. In doing so, it checks whether a cached area from previous scans could be used.


mm/mmap.c
if (len > mm->cached_hole_size) {
start_addr = addr = mm->free_area_cache;
} else {
start_addr = addr = TASK_UNMAPPED_BASE;
mm->cached_hole_size = 0;
}
...

(^8) Files can also be equipped with a special-purpose mapping function. This is, for instance, used by the frame-buffer code to allow
direct manipulation of the video memory when a frame-buffer device file is mapped into memory. However, because the kernel gen-
erally uses the standard implementation, I won’tbother to discuss other more specific routines.

Free download pdf