Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Starting at the current position, the kernel checks whether all subsequent slots —SWAPFILE_CLUSTERin
number — are free; this check is performed by theforloop within theifquery. If the kernel finds an
allocated entry whoseswap_mapentry is greater than 0, the search for a free cluster is resumed at the
next slot position. This is repeated until a position is finally reached at which there is insufficient space to
create a cluster.

If the kernel is successful in its search for a new cluster, it jumps to thelowestlabel, as above.

Allocating SwapSpace


After the policy routine has decided that a particular page needs to be swapped out,add_to_swapfrom
mm/filemap.ccomes into play. This function accepts astruct pageinstance as parameter and forwards
the swap-out request to the technical part of swapping implementation.

As the code flow diagram in Figure 18-8 shows, this is not a very difficult task and consists basically of
three steps. After theget_swap_pageroutine mentioned above has reserved a page slot in one of the
swap areas, all that needs to be done is move the page into the swap area. This is the responsibility of
the__add_to_swap_cachefunction, which is very similar to the standardadd_to_page_cachefunction
described in Chapter 16. The primary difference is that thePG_swapcacheflag is set and the swap iden-
tifierswp_entry_tis stored in theprivateelement of the page — it will be required to construct an
architecture-dependent page table entry when the page is actually swapped out. Additionally, the global
variabletotal_swapcache_pagesis incremented to update the statistics. Nevertheless, as we would
expect ofadd_to_page_cache, the page is inserted in the radix tree set up byswapper_space.

add_to_swap

get_swap_page

SetPageUptodate

SetPageDirty

_ _add_to_swap_page_cache

Figure 18-8: Code flow diagram for
add_to_swap.

Finally,SetPageUpdateandSetPageDirtymodify the page flags appropriately. Dirtying the page is
essential because the contents of the page are not yet contained in the swap area. Recall from Chapter 17
that pages in the page cache are synchronized with their underlying block device when they are dirty.
For a swap page, the underlying block deviceisthe swap space, and synchronization is therefore (nearly)
equivalent to swapping the page out! What remains to be done after the page has been written to a swap
slot is updating the page table to reflect this.

But, otherwise, that’s it. Nothing more is required of the policy routines when pages are swapped out.
The rest of the work — particularly the transferring of data from memory to the swap area — is per-
formed by the address-space-specific operations associated withswapper_space. The implementation of
the routines is discussed below — as far as the policy is concerned, it is enough to know that the kernel
actually writes the data to the swap area and thus releases a page afteradd_to_swaphas been invoked.
More details follow in the discussion of theshrink_page_listfunction.
Free download pdf