A running system will eventually use all page frames for purposes like disk buffers, dentries, inode entries or process pages. Linux needs to begin selecting old pages which can be freed and invalidated for new uses before physical memory is exhausted. This section will focus exclusively on how Linux implements it's page replacement policy and how different types of pages are invalidated.
The methods Linux uses to select pages is rather empirical in nature and the theory behind the approach is based on multiple different ideas. It has been shown to work well in practice and adjustments are made based on user feedback and benchmarks.
All pages, except those used by the slab allocator, in use by the system
are initially stored on the page cache via the pagelru
so they can be easily scanned for replacement. The slab pages are not stored
within the page cache as it is considerably more difficult to age a page
based on the objects used by the slab.
Process pages are stored in the page cache but are not easily swappable as there is no way to map page structs to PTE's except to search every page table which is far too expensive. If the page cache has a large number of process pages in it, process page tables will be walked and pages swapped out by swap_out() until enough pages has been freed but this will still have trouble with shared pages. If a page is shared, a swap entry is allocated, the PTE filled with the necessary information to find the page again and the reference count decremented. Only when the count reaches zero will the page be actually swapped out. These type of shared pages are considered to be in the swap cache.