Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Page in cache? Return page

read_swap_cache_async

find_get_page

alloc_page_vma

add_to_swap_cache

lru_cache_add_active

swap_readpage

Figure 18-20: Code flow diagram forread_swap_cache_async.

If the page is not found,page_alloc_vma(which ultimately reduces toalloc_pageon non-NUMA
systems) must be called to allocate a fresh memory page to hold the data from the swap area. Requests
for memory made withalloc_pagesenjoy high priority. For instance, the kernel attempts to swap out
other pages to provide fresh memory if not enough free space is available. Failure of this function —
indicated by the return of aNULLpointer — isveryserious and results in immediate abortion of
swap-in. In this situation, the higher-level code instructs the OOM killer to close the least-important
process in the system that has a comparatively large number of memory pages in order to obtain
free memory.

If the page is successfully reserved (and this is usually the case because only very few users manage to
inadvertently load the system to such an extent that the OOM killer must be deployed), the kernel adds
thepageinstance to the swap cache usingadd_to_swap_cacheand to the LRU cache (of the active pages)
usinglru_cache_add_active. The page data are then transferred from the swap area to RAM memory
by means ofswap_readpage.

swap_readpageinitiates data transfer from hard disk to RAM memory once the necessary preconditions
have been satisfied. This is done in two short steps.get_swap_biogenerates an appropriate BIO request
to the block layer, andsubmit_biosends the request.

Two things require special attention:

❑ add_page_to_swap_cacheautomatically locks the page.
❑ swap_readpageinstructs the block layer to callend_swap_bio_readwhen the page has been
completely read in. This sets thePG_uptodateflag if everything went well, and additionally
unlocks the page. This is important because the read operation is asynchronous. However, the
kernel can be sure that the page is filled with the required data when it is marked up-to-date and
unlocked.

18.8.3 Swap Readahead


As when reading files, the kernel also uses a readahead mechanism to read data from swap areas.
This ensures that data are read in anticipation so that future page-in requests can be satisfied quickly,
thus improving system performance. In contrast to the rather complicated file readahead method, the
Free download pdf