Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Caching SwapPages


In contrast toadd_to_swap,add_to_swap_cacheadds a page to the swap cache, but requires that a page
slot has already been allocated for the page.

If a page already has a swap cache entry, why is it added to the swap cache? This is required when pages
areswapped in. Suppose that a page that was shared among many processes has been swapped out. When
the page is swapped in again, it is necessary to retain the data in the cache after the first page-in untilall
processes have successively reclaimed the page. Only then can the page be deleted from the swap cache
because all user processes are then informed of the page’s new position in memory. The swap cache is
also used in this way when readahead is performed for swap pages; in this case, the pages read in have
not yet been requested because of a page fault but will most likely be shortly.

add_to_swap_cacheis simple as the code flow diagram in Figure 18-9 shows. The basic task is to invoke
__add_to_swap_cache, which adds the pages to the swap cache in the same way asadd_to_swap.How-
ever,swap_duplicatemust be called beforehand to ensure that the page already has a swap entry. The
swap map count is also incremented byswap_duplicate; this signals that the page was swapped out
from more than one place.

add_to_swap_cache

swap_duplicate

_ _add_to_swap_cache

Figure 18-9: Code flow diagram of
add_to_swap_cache.

The main difference betweenadd_to_swapandadd_to_swap_cacheis that the latter sets neither of the
page flagsPG_uptodatenorPG_dirty. Essentially, this means that the kernel does not need to write
the page into the swap area — the contents of both are currently synchronized.^7

18.4.4 Searching for a Page


lookup_swap_cachechecks whether a page is located in the swap cache. Its implementation requires
only a few lines^8 :

mm/swap_state.c
struct page * lookup_swap_cache(swp_entry_t entry)
{
struct page *page;

page = find_get_page(&swapper_space, entry.val);

return page;
}

(^7) Notice that kernel 2.6.25, which was under development when thisbook was written, will reshuffle the function names discussed
here slightly.add_to_swap_cachewill be merged into its only callerread_swap_cache_async, and will not exist anymore.
__add_to_swap_cache, however, will take its place and will be renamed toadd_to_swap_cache. The callers are updated
accordingly.
(^8) Like many other swapping functions described in this chapter, the original function in the kernel sources includes a few short calls
to update the key statistics of the swapping subsystem. I will not include such calls in our discussion because they essentially deal
with simple manipulation of counters, which is not very interesting.

Free download pdf