Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


In the architecture-independent representation, the kernel must store both the identification (also referred
to astype) of the swap partition and an offset within this area in order to be able to uniquely identify a
page. This information is kept in a special data type calledswap_entry_t, which is defined as follows:

<swap.h>
typedef struct {
unsigned long val;
} swp_entry_t;

Only one variable is used, although two different information items must be stored. The components of
the variable can be filtered out by selecting different areas, as illustrated in Figure 18-6.

02732

59 64

Offset Swap Identifier

0

Figure 18-6: Components of an architecture-independent
swap identifier.

Why is just a singleunsigned longvariable used to formally store both items of information? First, all
systems supported so far are happily able to make do with the information provided in this way. And
second, the value held in the variable is also used asa search key for the radix tree that lists all swap
cache pages. Since the swap cache is merely a page cache that useslongs as a key, a swapped-out page
can be uniquely identified in this way.

As this situation may change in the future, theunsigned longvalue is not used directly but is hidden
in a structure. As the contents of aswap_entry_tvalue may only be accessed by special functions, the
internal representation may be modified in future versions without having to rewrite significant parts of
the swap implementation.

To ensure access to both information items inswap_entry_t, the kernel defines two constants for the bit
arrangement shown in Figure 18-6:

<swapops.h>
#define SWP_TYPE_SHIFT(e) (sizeof(e.val) * 8 - MAX_SWAPFILES_SHIFT)
#define SWP_OFFSET_MASK(e) ((1UL << SWP_TYPE_SHIFT(e)) - 1)

MAX_SWAPFILES_SHIFThas the value 5, regardless of platform. The length ofunsigned longon 32-bit
architectures is 4; on 64-bit platforms, it is 8 bytes.

This special arrangement is relatively uninteresting for the rest of the kernel code. Much more important
are the functions that extract the individual components from the structure:

<swapops.h>
static inline unsigned swp_type(swp_entry_t entry)
{
return (entry.val >> SWP_TYPE_SHIFT(entry));
}

static inline pgoff_t swp_offset(swp_entry_t entry)
{
return entry.val & SWP_OFFSET_MASK(entry);
}
Free download pdf