Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


free page frames for upcoming needs and minimization of the time needed to perform swap
operations?
❑ According to which criteria should the pages be selected for swapping? In other words, which
page replacement algorithm should be used?
❑ How are page faults handled as effectively and quickly as possible, and how are pages returned
fromaswapareatosystemRAM?
❑ Which data can be removed from the various system caches (from the inode or dentry cache,
e.g.)withouta backing store because it can be reconstructed indirectly? This question is not, in
fact, directly related to the execution of swapping operations but concerns both the cache and
swap subsystems. However, as cache shrinking is initiated by the swap subsystem, this question
is addressed below in this chapter.

As I have demonstrated, not only are the technical details of paramount importance in achieving an
efficient and powerful implementation of the swap system, but the design of the overall system must
also support the best possible interaction betweenthe various components to ensure that actions are
performed smoothly and harmoniously.

18.2.1 Organization of the Swap Area


Swapped-out pages are held either on a dedicated partition without a filesystem or in a file of fixed size in
an existing filesystem. As every system administrator knows, several such areas can be used in parallel.
It is also possible to assign priorities based on the speed of the various swap areas. These priorities can
then be adopted by the kernel when it uses the swap areas.

Each swap area is subdivided into a number of continuousslots, each of which is precisely the size of one
page frame in the system. On most processors this is still 4 KiB. However, larger pages are commonly
used on newer systems.

Basically, any page in the system can be accommodated in any slot of a swap area. However, the kernel
also uses a structuring method referred to asclusteringto handle accesses to swap areas as quickly as
possible. Consecutive pages in the memory area of a process (or at least pages that are swapped out
consecutively) are written to the hard disk one after the other, with a particular cluster size — normally
256 pages. If no further memory space is available in the swap area for clusters of this size, the kernel
adds the pages at any positions that are currently free.

If several swap areas with the same priority are used, the kernel uses a round robin process
to ensure that they are utilized as uniformly as possible. If the swap areas have different priorities,
the kernel fills the ones with higher priority first before gradually moving on to the ones with
lower priority.

To keep track of which pages are where in which swap partition, the kernel must retain some data struc-
tures that hold this information in memory. The mostimportant structure element is a bitmap that tracks
the used and free state of the slots in a swap area. Other elements yield data to support selection of the
slot to be used next and to help implement clustering.

Two userspace tools are available to create and enable swap areas; these aremkswap(for ‘‘formatting’’ a
swap partition/file) andswapon(for enabling a swap area). As these programs are crucial to a functioning
swap subsystem, they are described (and the system call forswapon)below.
Free download pdf