16.3. The Swap Cache

In Linux, a page frame may be shared among several processes in the following cases:

  • The page frame is associated with a shared[2] memory mapping (see the section Section 15.2 in Chapter 15).

    [2] Page frames for private memory mappings are handled through the Copy On Write mechanism, and thus fall under the next case.

  • The page frame is handled by means of Copy On Write, perhaps because a new process has been forked (see Section 7.4.4 in Chapter 7).

  • The page frame is allocated to an IPC shared memory resource (see Section 18.3.5 in Chapter 18).

As we shall see later in this chapter, page frames used for shared memory mappings are never swapped out. Instead, they are handled by another kernel function that writes their data to the proper files and discards them. However, the other two kinds of shared page frames must be carefully handled by the swapping algorithm.

Because the kernel handles each process separately, a page shared by two processes, A and B, may have been swapped out from the address space of A while it is still in B's address space. To handle this peculiar situation, Linux makes use of a swap cache, which collects all shared page frames that have been copied to swap areas. The swap cache does not exist as a data structure on its own, but the pages in the regular page cache are considered to be in the swap cache if certain fields are set.

The reader might ask at this point why the algorithm does not just swap a shared page out from all the process's ...

Get Understanding the Linux Kernel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.