Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 4: Virtual Process Memory


The presence of a mapping for the fault address does not necessarily mean that access is actually permit-
ted. The kernel must check the access permissions by examining bits 0 and 1 (because 2^0 + 21 =3). The
following situations may apply:


❑ VM_WRITEmust be set in the event of a write access (bit 1 set, cases 3 and 2). Otherwise, access is
invalid, and execution resumes atbad_area.
❑ In the event of a read access to an existing page (Case 1), the fault must be a permission fault
detected by the hardware. Execution then resumes atbad_area.
❑ If a read access is made to a page that doesn’t exist, the kernel must check whetherVM_READor
VM_EXECis set, in which case access is valid. Otherwise, read access is denied, and the kernel
jumps tobad_area.

If the kernel does not explicitly jump tobad_area, it works its way down through thecasestatement and
arrives at thehandle_mm_faultcall that immediately follows; this function is responsible for correcting
the page fault (i.e., reading the required data).


arch/i386/mm/fault.c
...
survive:
/*
* If for any reason at all we couldn’t handle the fault,
* make sure we exit gracefully rather than endlessly redo
* the fault.
*/
fault = handle_mm_fault(mm, vma, address, write);
if (unlikely(fault & VM_FAULT_ERROR)) {
if (fault & VM_FAULT_OOM)
goto out_of_memory;
else if (fault & VM_FAULT_SIGBUS)
goto do_sigbus;
BUG();
}
if (fault & VM_FAULT_MAJOR)
tsk->maj_flt++;
else
tsk->min_flt++;

return;
...
}

handle_mm_faultis an architecture-independentroutine for selecting the appropriate fault correction
method (demand paging, swap-in, etc.) and for applying the method selected (we take a close look at the
implementation and the various options ofhandle_mm_faultin Section 4.11).


If the page is created successfully, the routine returns eitherVM_FAULT_MINOR(the data were already in
memory) orVM_FAULT_MAJOR(the data had to be read from a block device). The kernel then updates the
process statistics.


However, faults may also occur when a page is created. If there is insufficient physical memory to load
the page, the kernel forces termination of the process to at least keep the system running. If a permitted

Free download pdf