Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Page Reclaim
(Policy)

Swap Cache MechanismWriteback

Reverse
Mapping

Activation of Page Selection
(periodically or by pressure)

Figure 18-4: Interaction between the swap cache and
the other components of the swap subsystem.

Which data are kept in the swap cache? As the swap cache is simply another page cache built using
the structures discussed in Chapter 3, the answer is simple — memory pages. However, in contrast
to the other page caches in the system, its purpose is not to keep pages in RAM for performance reasons
although the associated data could also always be obtained from a block storage medium (this would run
totally counter to the swapping principle). Instead, the swap cache is used for the following, depending
on the ‘‘direction’’ of the swapping request (read or write):

❑ When pages are swapped out, the selection logic first selects a suitable seldom-used page frame.
This is buffered in the page cache, from where it is transferred to the page cache.
❑ If a page used simultaneously by several processes is swapped out, the kernel must set the page
entry in the directories of the process to point to the relevant position in the swap-out file. When
one of the processes accesses the data on the page, the page is swapped in again, and the page
table entry forthis singleprocess is set to the current memory address at which the page is now
located. However, this causes a problem. The entries of all other processes still point to the entry
in the swap-out file because, although it is possible to determine the number of processes that
share a page, it is not possible to identifywhichprocesses these are.
When shared pages are swapped in, they are therefore retained in the swap cache untilallpro-
cesses have requested the page from the swap area and are all thus aware of the new position of
the page in memory. This situation is illustrated in Figure 18-5.
Without the aid of the swap cache, the kernel is not able to determine whether or not a shared
memory page has already been swapped back in, and this would inevitably result in redundant
reading of data.

The importance of the swap cache is not the same in both directions. It is far more important when pages
are swapped in than when they are swapped out. This asymmetry came about during the development
of 2.5 when thereverse mappingscheme (rmap) described in Chapter 4 was introduced. Recall that the
rmap mechanism finds all processes that share a page.^5

(^5) In earlier versions, shared memory pages could only be swapped out using the swap cache. Once the page had been removed from
thepagetablesofasingleprocess, the kernel had to wait until the page had also been removed from the page tables of all other
processes before it could remove the data from memory; this required the systematic scanning of all system page tables. The pages
were kept in the swap cache in the meantime.

Free download pdf