Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


❑ All pages that belong to the process heap and were reserved usingmalloc(and consequently
using thebrksystem call or ultimately an anonymous mapping); see Chapter 3
❑ Pages that are used to implement one of the interprocess communication mechanisms. These
include, for instance, shared memory pages that are used to exchange data between processes.

Memory pages used by the kernel itself areneverswapped out. The reasons are obvious. The complexity
of the kernel code would increase dramatically. Since the kernel does not require very much memory as
compared to other user applications, the potential gain is too low to justify the additional effort.

Naturally, pages used to map peripherals to memory space cannot be swapped out either. This
would make no sense, especially as the pages are used only as a means of communication between the
application and the device and not for actually storing data persistently.

Even though it is not possible to swap out all page types, the kernel’s swapping
page reclaim must still cater to page types that are based on other backing stores.
The most frequent page types of this kind relate to data from files that are mapped
into memory. Ultimately, it is irrelevant which pages fromwhichcategory are
written from RAM memory to backing store because the effect is always the
same — a page frame is freed to make space for more important data that must
reside in the RAM.

18.1.2 Page Thrashing


A further problem that may occur when performing swapping operations ispage thrashing.Astheterm
implies, this involves intensive transfers between swap space and RAM memory; this boils down to
nothing more than the repeated backward and forward swapping of pages. This phenomenon tends to
increase as the number of system processes increases. It occurs when important data are swapped out
and are needed again very soon afterward.

The main problem that the kernel must address to prevent page thrashing is to determine theworking
setof a process (in other words, the pages that are needed most frequently) as accurately as possible so
that the least important pages can be moved to the swap area or some other backing store and the really
important data can be kept in memory.

To do this, the kernel needs an appropriate algorithm to evaluate the importance of pages to the overall
system. On the one hand, pages must be evaluated as fairly as possible so that processes are not unduly
favored or disadvantaged. On the other hand, the algorithm must be implemented simply and efficiently
to ensure that not too much processing time is needed to select the pages to be swapped out.

The many CPU types provide different methods of supporting the kernel in this task that vary in their
level of sophistication. However, Linux is not able to use all methods as they are not always available on
simpler CPUs and may also be difficult to emulate. As usual, the lowest common denominator must be
found upon which the kernel can build its hardware-independent layers.

One particularly simple, but important trick that is completely independent of the processor’s abilities
is to keep aswap tokenin the system that is given to one single process that swaps in pages. The kernel
tries to avoid swapping out pages from this process, thus alleviating its situation by giving it some time
to, hopefully, finish a task. After some time, the swap token is passed to some other process that also
undergoes swapping and requires memory more exigent than the current token holder.
Free download pdf