Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


The number of bits used to address swap space locations in the architecture-independent format of a
swap entry will in general be larger than in the architecture-specific format. Because architectures are not
required to define the number of bits used for a swap offset in a constant visible to the public, the kernel
needs to employ a little trick to find the maximally addressable swap offset:

maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;

swp_entry(0, 0UL)specifies a swap offset with all bits set. The conversion to a page table entry and
then back to an architecture-independent format guarantees that only valid bits survive. The largest
addressable swap page number is obtained by picking the swap offset from the result.

18.4.2 Structure of the Cache


In terms of its data structures, the swap cache is nothing more than a page cache, as described in
Chapter 3. At the heart of its implementation is theswapper_spaceobject, which groups together the
internal functions and list structures associated with the cache:

mm/swap_state.c
struct address_space swapper_space = {
.page_tree = RADIX_TREE_INIT(GFP_ATOMIC|__GFP_NOWARN),
.tree_lock = RW_LOCK_UNLOCKED(swapper_space.tree_lock),
.a_ops = &swap_aops,
.i_mmap_nonlinear = LIST_HEAD_INIT(swapper_space.i_mmap_nonlinear),
.backing_dev_info = &swap_backing_dev_info,
};

Although each system may have several swap areas, there is just one variable via
which the remaining kernel code accesses the swap cache. The pages are not
organized into different areas until the data are actually written back. In the view of
that part of the kernel that determines which pages are to be swapped out, there is
onlyoneswap cache to which the appropriate instructions must be forwarded, and
this cache is represented by theswapper_spaceobject mentioned above.

Since most of the fields are lists, they are initialized to their (empty) basic settings using suitable macros.
The meaning of the entries is discussed in Chapter 4.

The kernel provides a set of swap cache access functions that can be used by any kernel code involved
with memory management. They allow, for example, pages to be added to the swap cache or a search
to be made for pages in the cache. They constitute the interface between the swap cache and the page
replacement logic and are therefore used to issue commands to swap pages in or out without having to
worry about the technical details of how the data are subsequently transferred.

A set of functions is also provided to handle with the address space made available by the swap cache.
As is common with address spaces and therefore with page caches, these functions are grouped into
anaddress_space_operationsinstance that is associated withswapper_spaceby means of theaops
element. The functions constitute the ‘‘downward’’ interface of the swap cache; in other words, to the
data transfer implementation between the system’s swap areas and RAM memory. In contrast to the
function set mentioned earlier, these routines are not concerned with which pages are swapped out or in
or when this is done, but are responsible for the technical aspects of data transfer for the selected pages.
Free download pdf