Swapping Out Pages

The later section Section 16.7 explains what happens when pages are swapped out. As we indicated at the beginning of this chapter, swapping out pages is a last resort and appears as part of a general strategy to free memory that uses other tactics as well. In this section, we show how the kernel performs a swap out. This is achieved by a series of functions called in cascading fashion. Let’s start with the functions at the higher level.

The swap_out( ) function acts on a single classzone parameter that specifies the memory zone from which pages should be swapped out (see Section 7.1.2). Two other parameters, priority and gfp_mask, are not used.

The swap_out( ) function scans existing memory descriptors and tries to swap out the pages referenced in each process’s Page Tables. It terminates as soon as one of the following conditions occurs:

  • The function succeeds in releasing SWAP_CLUSTER_MAX page frames (by default, 32). A page frame is considered released when it is removed from the Page Tables of all processes that share it.

  • The function scans n memory descriptors, where n is the length of the memory descriptor list when the function starts.[111]

To ensure that all processes are evenly penalized by swap_out( ), the function starts scanning the list from the memory descriptor that was last analyzed in the previous invocation; the address of this memory descriptor is stored in the swap_mm global variable.

For each memory descriptor mm to be considered, the swap_out( ...

Get Understanding the Linux Kernel, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.