Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


array, thenextvariable is defined to create a relative order between the areas despite the fixed
array positions.nextis used as an index forswap_info[]. This enables the kernel to track the
individual entries according to their priority.
But how is it possible to determine which swap area is to be usedfirst? Since this area is not nec-
essarily located at the first array position, the kernel also defines the global variableswap_list
inmm/swapfile.c. It is an instance of theswap_list_tdata type defined specifically for the pur-
pose of finding the first swap area:
<swap.h>
struct swap_list_t {
int head; /* head of priority-ordered swapfile list */
int next; /* swapfile to be used next */
};
headis an index into theswap_info[]array and is used to select the swap area with the highest
priority. The kernel works its way through the list to the swap areas with low priorities using
thenextelements.nextis used to implement a round robin process to uniformly fill multiple
swap areas with pages if the areas have the same priority. I return to this variable below when I
examine how the kernel selects swap pages.
Let us take a close look at the system’s mode of operation by reference to the example above. The
entry point is the first array entry that contains the swap area with the highest priority. The value
ofheadis therefore 0.
nextspecifies which swap area is used next. This need not always be the swap area with the
highest priority. If the latter is already full,nextpoints to another swap area.
❑ In order to reduce search times when the complete swap area is scanned for a free slot, the ker-
nel manages the upper and lower limits of the search zone with the aid of thelowest_bitand
highest_bitelements. There are no free pages above or below these positions so it would be
pointless to search this area.

Although the names of the two variables end with_bit,theyarenotbit fields but
absolutely normal integers that are interpreted as indexes with regard to the linearly
arranged pages of a swap area.

❑ The kernel also provides two elements —cluster_nextandcluster_nr— to implement the
cluster technique mentioned briefly above. The former specifies which slot of an existing cluster
in the swap area is to be used next, andcluster_nrindicates how many pages are still available
for use in the current cluster before it is necessary to start a new cluster, or (if not enough free
pages are available for a new cluster) that recourse is made to fine-grained allocation.

Extents forImplementingNon-ContiguousSwapAreas


The kernel uses theextent_listandcurr_swap_extentelements to implementextents, which create
mappings between the swap slots that are assumed to be contiguous and the disk blocks of the swap file.
This is not necessary if partitions are used as the basis for swap space because the kernel can then rely
on the fact that the blocks on the disk are arrangedlinearly. Mapping between page slots and disk blocks
is therefore very simple. Starting from the position of the first block, it is only necessary to multiply a
constant offset by the required page number in order to obtain the required address, as illustrated in
Figure 18-1. In this case, just oneswap_extentinstance is needed. (Actually, this could also be dispensed
with, but its existence makes things easier for the kernel as it narrows the differences between partition
swap areas and file swap areas.)
Free download pdf