Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Sinceswap_entry_tinstances may never be manipulated directly, the kernel must provide a function
that generates aswap_entry_tfrom a given type/offset pair:


<swapops.h>
static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)
{
swp_entry_t ret;

ret.val = (type << SWP_TYPE_SHIFT(ret)) |
(offset & SWP_OFFSET_MASK(ret));
return ret;
}

A few bit operations are used to pack the parameters in anunsigned longvariable that is returned as the
content of a newswap_entry_t.


The kernel requires the ability to switch between the architecture-dependentand architecture-independent
representations, so thepte_to_swp_entryfunction is provided for this purpose:


<swapops.h>
static inline swp_entry_t pte_to_swp_entry(pte_t pte)
{
swp_entry_t arch_entry;

arch_entry = __pte_to_swp_entry(pte);
return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
}

Conversion is performed in two steps. Starting with a page table entry that — as explained in
Chapter 4 — is represented by an instance of data typepte_t, the data it contains are converted to an
architecture-dependentswap_entry_t.


Even if the same data type is used in the processor-specific representation and in the
architecture-independentmemory model, the way in which the bits are distributed
generally differs in the two variants.

__pte_to_swp_entryis an architecture-dependent function that is defined in the CPU-specific include file
<asm-arch/pgtable.h>. It gives the kernel the opportunity to extract the processor-specific information
in the page table. On many architectures, this can be achieved by means of a simple typecast that does
not change the content of the page table entry — justfor a change, even the Sparc processors, which are
otherwise somewhat eccentric in this respect, do not call for anything special here.


In the second step, the information contained in the newly createdswap_entry_tinstance is converted to
the architecture-independent format, where usually a number of bits are devoted to management tasks,
for instance, to mark the identifier as swap entry in contrast to regular page table entries. The kernel is
again reliant on the help of the processor-specific code. All systems must feature the__swp_typeand
__swp_offsetfunctions (note the leading underscores that are absent in the architecture-independent
versions) that extract the type and offset from the machine-specific format and return the information in
the general format, which is then put together byswp_entryto create a newswap_entry_t.

Free download pdf