Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


The kernel must then find and compare the sector addresses ofallblocks in the area to ascertain
whether the blocks are contiguous. Discontinuity isestablished if this is not the case. The kernel first
performs the operation for the number of blocks that constitute a page. If their sector addresses are
consecutive, a linear area equivalent to one page is found on the disk.add_swap_extentinserts this
information in the extent data structure.

This whole operation is then repeated, this time starting at the address of the next file block whose sector
address had not yet been checked. Once the kernel has ascertained that the sectors of the blocks on this
page are also consecutive on the disk,add_swap_extentis again invoked to add this information to the
extent list.

Ifadd_swap_extentwere to add a new list element to the extent list each time it is invoked, it would
not be possible to merge contiguous areas larger than a single page. Consequently,add_swap_extent
automatically attempts to keep the list as compact as possible. When a new entry is added and its start
sector is immediately after the final sector of the last entry (or, expressed differently, when the sum of the
start_blockandnr_pageselements of the lastswap_extentelement is equal to the start sector of the
new entry),onecombined entry is automatically created to merge the data of both elements. This ensures
that the list comprises as few entries as possible.

But what happens when the kernel encounters a discontinuity? Sincesetup_swap_extentsonly checks
areas that are the size of one page, the current area can be completely discarded. It serves no purpose
because the minimum swapping unit is a full page. When a discontinuity is detected in the sectors, the
kernel restarts the search starting at the sector address of the next file block. This is repeated until the next
page is found that is mapped contiguously on the hard disk. If a new entry is then added to the extent
list usingadd_swap_extent, the end address of the old sector and the start address of the new sector no
longer match. This means that the two entries can no longer be merged and the kernel must create a new
list element.

The above procedure is repeated until all blocks in the swap area have been processed. Once this has
been done, the final step is to enter thenumber of usable pages in the relevantswap_infoelement.

18.4 The Swap Cache


Now that I have described the layout of the swap subsystem by reference to its data structures, let us
turn our focus in the following sections on the techniques employed by the kernel to write pages from
memory into a swap area and to read pages from the swap area back into memory.

The kernel makes use of a further cache, known as aswap cache,thatactsasaliaisonbetweentheopera-
tion to select the pages to be swapped out and the mechanism that actually performs swapping. At first
sight, this seems a little peculiar. What’s the use of another swapping cache andwhatexactly needs to be
cached? The answers are given below.

Figure 18-4 shows how the swap cache interacts with the other components of the swap subsystem.

The swap cache is an agent between the page selection policy and the mechanism for transferring data
between memory and swap areas. These two parts interact via the swap cache. Input in one part triggers
corresponding actions in the other. Notice that the policy routines can, nevertheless, directly interact with
the writeback routines for pages that need not be swapped out, but can be synchronized.
Free download pdf