Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping...................................


Naturally, this procedure also has its limits. At some time the point is reached where the caches and
buffers can no longer be shrunk. Furthermore, it does not work for pages whose content is generated
dynamically and that have no backing store.

As in typical systems (with the exception of some embedded or handheld PCs) considerably more
hard disk capacity is generally available than RAM memory space, the kernel — in conjunction with
the capability of the processor to manage virtual address spaces that are larger than the existing RAM
memory — can ‘‘commandeer’’ parts of the disk in order to use them as memory expansions. Since hard
disks are considerably slower than RAM memory, swapping is purely an emergency solution that keeps
the system running but at considerably reduced speed.

The termswappingoriginally referred to the swapping-out of an entire process — with all its data, pro-
gram code, and the like — andnotto the page-by-page, selective exporting of process data to secondary
expanded RAM memory. While this strategy was adopted inveryearly versions ofUnix,whereitwas
perhaps sometimes appropriate, such behavior is now inconceivable. The resultant latency times during
context switching would make interactive working not just sluggish but intolerably slow. However, a
distinction is not made between swapping and paging below. Both stand for the fine-grained swapping-
out of process data. This is now established usage ofthe terms not just amongst experts but also (and
above all) in the kernel sources.

Two questions must be answered when considering how to implement swapping and page reclaim in
the kernel:


  1. According to what scheme should pages be reclaimed; that is, how does the kernel decide
    which pages it should reclaim in order to ensure maximum possible benefit and least possi-
    ble disadvantage?

  2. How are pages that have been swapped out organized in the swap area, and how does the
    kernel write pages to the swap area and read them in again later? How does it synchronize
    pages with their backing device?


The question as to which memory pages are swapped out and which ones remain in RAM is crucial to
system performance. If the kernel selects a frequently used page, a page in memory is then briefly freed
for other purposes. However, because the original data are soon needed again, another page must be
swapped out to create a free page to hold the data that have just been swapped out and are now required
again. This is obviously not very efficient and must therefore be prevented.

18.1.1 Swappable Pages


Only a few kinds of pages may be swapped out to a swap area — all others have an alternative backing
storage on a block device that is used instead:

❑ Pages of theMAP_ANONYMOUScategory that are not associated with a file (or are a mapping of
/dev/zero); for example, a process stack or memory area mapped anonymously usingmmap.
(The reference manual on the GNU C standard library or the customary standard reference
works on system programming provide further information on mappings of this kind.)
❑ Private mappings of a process used to map files in which changes arenotwritten to the under-
lying block device, as would normally be the case. As the file is no longer available as a backing
store in this case, the pages must be swapped out to the swap area if RAM memory becomes
scarce since the contents can no longer be restored from the file. The kernel (and therefore the C
standard library) uses theMAP_PRIVATEflag to create mappings of this type.
Free download pdf