Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


removed from memory. This issue has noeffect on whether a page is swapped out
or not. Some pages have a permanent backing store into which they can be brought,
while others must be put into the swap area (recall that Section 18.1.1 contains a
more refined characterization of pages that can be swapped out).

Implementation of the swap policy algorithm is one of the more complex parts of the kernel. This is due
not only to the latent question of maximum speed, but also and primarily to the multitude of special
situations that must be addressed. In the examples below, I concentrate on the most frequent situations
that account for the overwhelming share of the swapping subsystem’s work. For the sake of brevity, I
shall not discuss rare phenomena that are due largely to the interplay among the various processors on
SMP systems or to random coincidences on uniprocessor systems. A general overview of the interaction
between the individual components involved in swapping is much more important (and is complicated
enough on its own) than the detailed minutia of every swapping operation.

18.6.1 Overview


The general approach to implementation of the swap policy algorithms has been discussed above. The
following sections focus on the interaction of the swap policy functions and procedures and describe their
implementation in detail. Figure 18-11 shows a code flow diagram listing the most important methods
and illustrating how they are interlinked.

shrink_zone

shrink_inactive_list

shrink_page_list

try_to_free_pages

shrink_zones

Direct page reclaim

balance_pgdat

kswapd

balance_pgdat

kswapd

balance_pgdat

kswapd

Swap Daemons (one instance per NUMA node)

shrink_active_list

Figure 18-11: ‘‘Big picture’’ of the page reclaim implementation. Note that the figure is
not a proper code flow diagram but just displays the most important functions.

The diagram is another refinement of the overview shown in Figure 18-7.Page reclaimis triggered at two
points, as shown in the figure:


  1. try_to_free_pagesis invoked if the kernel detects an acute shortage of memory during an
    operation. It checks all pages in the current memory zone and frees those least frequently
    needed.

  2. A background daemon namedkswapdchecks memory utilization at regular intervals and
    detects impending memory shortage. It can be used to swap out pages as a preventive mea-
    sure before the kernel discovers in the course of another operation that it does not have
    enough memory.

Free download pdf