Chapter 18: Page Reclaim and Swapping
this point, the kernel must nevertheless consider which area it wants to select for the page and into which
slot it will be inserted. This decision must then be saved in the data structures of the swap cache.
Page fault management
Page Cache Management Reverse Mapping
do_swap_page
Architecture-specific
page fault handler
swapin_readahead
read_swap_cache_async
_ _add_to_swap_cache
add_to_swap
Page Reclaim
Tranfer swap data
to swap space and
handle page tables
shrink_page_list
Zone scanning
try_to_unmap
try_to_unmap_one
swap_writepage
Figure 18-7: Overview of the most important functions that implement swapping and page reclaim. The
figure is not a proper code flow diagram and skips some intermediate functions.
Two kernel methods add pages to the swap cache but serve different purposes:
- add_to_swapis invoked when the kernel wants to swap out a pageactively;thatis,whenthe
policy algorithm has determined that insufficient memory is available. The routine not only
adds the page to the swap cache (where it remains until its data are written to disk), but also
reserves a slot in one of the swap areas. - When a page shared by several processes (this can be determined by referring to the usage
counter in the swap area) is read in from the swap area, the page is retained in both the
swap area and the swap cache until it is either swapped out again or until it is swapped
in by all the processes that share it. The kernel implements this behavior by means of the
add_to_swap_cachefunction, which adds a page to the swap cachewithoutperforming oper-
ations on the swap areas themselves.
Reserving Page Slots
Before dealing with the implementation details of these two functions, we should examine how page
slots are reserved in swap areas. The kernel delegates this task toget_swap_page, which — when called
without parameters — returns the number of the page slot to be used next.
The function must first ensure that the system does, in fact, have swap areas — if so, the global variable
nr_swap_pageshas a value greater than 0.
swap_list.nextalways yields the number of the swap area currently in use (if there is only one swap
area, it obviously always returns the same number). Logically, the kernel begins the search for a free page
slot in this area.scan_swap_mapscans the page bitmap, making use of swap clustering — a technique
examined below.
Ifnofree slot is found in the current swap area, the kernel checks the alternate slots. To do this, it runs
through the list of all swap areas until it finds a free slot. Naturally, searching is performed in line with
the priorities defined for each individual area by means of thenextelement of eachswap_info[]entry.