Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


bytes at the start of the partition and the signature at the end), but is also partly because the kernel
must be able to handle different page sizes, and this is simpler if different structures are used. Since
information is located at the startandat the end of the first swap page, the space between must be filled
with a suitable number of empty filler elements — at least from the data structure’s viewpoint. However,
access to the swap signature at the bottom end of the page is easier if the fill space required is calculated
by simply deducting the length of the signature (10 characters) from the page size — which is specified by
PAGE_SIZEon all architectures. This yields the requiredposition. When the upper elements are accessed,
it is only necessary to specify the definition of the upper part. From the viewpoint of the data structure,
the data that then follow are of no interest since they merely contain the list of bad blocks whose array
position addresses can be calculated very easily.

Creating the Extent List


setup_swap_extentsis used to create the extent list. The associated code flow diagram is shown in
Figure 18-3.

Create single extent

Yes add_swap_extent

add_swap_extent

bmap inode->i_mapping->a_ops->bmap

bmap

setup_swap_extents

Block device as swap area
No

Query first block number

Discontinuity? after discontinuityStart new extent

Performs automatic clustering
Set swap information

Iterate over all
Iterate over all blocks of the swap areablocks of the page

Figure 18-3: Code flow diagram forsetup_swap_extents.

The task of the procedure is simple when a swap partition rather than a swap file is used. It is then
guaranteed that all sectors are contained in a contiguous list; consequently, only a single entry is required
in the extent list. This entry is created usingadd_swap_extentand includes all blocks in the partition.

If the swap area is a file, the kernel is required to do a little more work since the blocks of the file must be
scanned individually to determine how blocks are assigned to sectors. Thebmapfunction is used for this
purpose. It is part of the virtual filesystem and invokes thebmapfunction in the address space operations
of the specific filesystem. The various filesystem-specific implementations are of no interest to us here
since they all yield the same result — a sector number that specifies which hard disk sector belongs to a
given block number. The logical blocks of a file can be regarded as contiguous by the remainder of the
kernel. However, this is not the case for the associated sectors on the disk, as discussed in Chapter 9.

The algorithm for creating the mapping list is not particularly complicated. As swap areas are rarely
activated, the kernel need not concern itself with speed issues, which means that implementation is very
straightforward. The first step is to determine the sector address of the first block in the swap area by
means ofbmap. This address serves as the starting point for further examination of the area.
Free download pdf