Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


18.2.2 Checking Memory Utilization


Prior to swapping out pages, the kernel checks memory utilization to identify whether capacity is low.
As when synchronizing pages, the kerneluses a combination of two mechanisms:


  1. Aperiodicdaemon(kswapd) runs in the background and repeatedly checks current memory
    utilization in order to initiate page swap-out when a particular threshold value is reached.
    This method ensures that no swap storms occur in which a very large number of pages sud-
    denly need to be swapped out; this would result in long wait times and must be prevented
    on all accounts.

  2. Nevertheless, the kernel must expect acute memory shortage whenever, for example, a large
    memory area is allocated by the buddy system or when buffers are generated. If insufficient
    RAM is available to satisfy the request for memory, the kernel must attempt to free space by
    swapping out pages as quickly as possible. Swap-out in the event of an acute emergency is
    part ofdirect reclaim.


The VM subsystem has only one option if a kernel request for memory cannot be satisfied even after
pages have been swapped out — the targeted termination of a process by means of the OOM (out of
memory) killer. Even if this sometimes entails severe losses, it is still better than a complete system crash,
which would otherwise result.

18.2.3 Selecting Pages to Be Swapped Out


The key question faced by the swapping subsystem is always the same. Which pages can be swapped
out to ensure maximum benefits at minimum cost to the system? The kernel uses a mixture of the ideas
discussed earlier and implements a rough-grained LRU method that makes use of only one hardware
feature — the setting of an accessed bit following a page access — because this function is available on
all supported architectures and can be emulated with little effort.

In contrast to the general algorithms, the LRU implementation of the kernel is based ontwolinked
lists that are referred to as theactiveand theinactivelist (separate lists exist for each memory zone in
the system). As the two names imply, all the pages in active use are on the one list, while all inactive
pages that may be mapped into one or more processes but are not very frequently used are held on
the other. To distribute the pages between the lists, the kernel performs a regular balancing operation
that determines — by means of the above accessed bit — whether a page is regarded as active or inac-
tive, in other words, whether or not it is frequently accessed by the applications in the system. Transfers
between the two lists are possible in both directions. Pages can be transferred from active to inactive and
vice versa. However, transfer doesnottake place after every single page access but at longer intervals.

In the course of time, the least frequently used pages collect at the end of theinactivelist. When there is a
memory shortage, the kernel selects these pages for swap-out. Since these pages have been little used so
far, the LRU principle dictates that this will prove least disruptive to system operation.

18.2.4 Handling Page Faults


All architectures on which Linux runs support the concept of page faults that are triggered when a page in
virtual address space is accessed but is not present in physical memory. The page fault instructs the kernel
to read the missing data from the swap area or from another backing store, possibly by first deleting other
pages to make space for the new data.
Free download pdf