Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 4: Virtual Process Memory


Once the kernel has granted the desired memory, the following steps are taken:


  1. Allocation and initialization of a newvm_area_structinstance that is inserted in the
    list/tree data structures of the process.

  2. Creation of the mapping with the file-specific functionfile->f_op->mmap. Most filesystems
    usegeneric_file_mmapfor this purpose; all it does is set thevm_opselement of the mapping
    togeneric_file_vm_ops.
    vma->vm_ops = &generic_file_vm_ops;
    The definition ofgeneric_file_vmopsis given in Section 4.5.3. Its key element isfilemap
    fault, which is invoked when an application accesses the mapped area but the correspond-
    ing data are not yet in RAM memory.filemap_faultenlists the help of low-level routines of
    the underlying filesystem to fetch the desired data and — transparently to the application —
    read them into RAM memory. In other words, the mapped data are not read in immediately
    when the mapping is created but only when they are actually needed.
    Chapter 8 takes a closer look at the implementation offilemap_fault.


IfVM_LOCKEDis set — either explicitly with system call flags or implicitly by means of themlockall
mechanism — the kernel invokesmake_pages_presentto successively scan the pages of the mapping
and to trigger a page fault for each so that their data are read in. Of course, this means that the perfor-
mance gain of deferred reading is lost, but the kernel makes sure that the pages arealwaysin memory
after a mapping has been created — after all, theVM_LOCKEDflag prevents them from being swapped out,
so they must be first in.

The start address of the new mapping is then returned to conclude the system call.

do_mmap_pgoffperforms several checks (not described in detail here) at various points in addition to the
actions described above. If one of the checks fails, the operation is terminated, and the system call returns
to userspace with an error code.

❑ Accounting— The kernel keeps statistics on the number of pages a process uses for mappings.
As it is possible to limit process resources, the kernel must always ensure that the permitted
value is not exceeded. There is also a maximum number of mappings per process.
❑ Extensive security and plausibility checksmust be carried out to prevent the applications from
setting invalid parameters or parameters that could threaten system stability. For example, no
mappings may be created that are larger than the virtual address space or extend beyond the
boundaries of virtual address space.

4.7.2 Removing Mappings


Themunmapsystem call, which requires two parameters — the start address and length of the area to be
unmapped, must be used to remove an existing mapping from virtual address space.sys_munmapis the
entry point for the system call; it delegates its work in the usual way to thedo_munmapfunction defined
inmm_mmap.c. (Further implementation information is shown in the associated code flow diagram in
Figure 4-13.)
Free download pdf