Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


corresponding mechanism for the swapping subsystem mechanism is relatively simple, as the following
code demonstrates:

mm/memory.c
void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct vma)
{
int i, num;
struct page
new_page;
unsigned long offset;


/*



  • Get the number of handles we should do readahead io to.
    /
    num = valid_swaphandles(entry, &offset);
    for (i = 0; i < num; offset++, i++) {
    /
    Ok, do the async read-ahead now /
    new_page = read_swap_cache_async(swp_entry(swp_type(entry),
    offset), vma, addr);
    if (!new_page)
    break;
    page_cache_release(new_page);
    }
    lru_add_drain(); /
    Push any new pages onto the LRU now */
    }


The kernel invokesvalid_swaphandlesto calculate the number of readahead pages. Typically,
2 page_clusterpages are read, wherepage_clusteris a global variable that is set to 2 for systems
with less than 16 MiB of memory and to 3 for all others. This produces a readahead window of four or
eight pages (/proc/sys/vm/page-clusterallows for tuning the variable from userspace, and to disable
swap-in readahead by setting it to zero). However, the value calculated byvalid_swaphandlesmust be
reduced in the following situations:

❑ If the requested page is near the end of the swap area, the number of readahead pages must be
reduced to prevent reading beyond the area boundary.
❑ If the readahead window includes free or unused pages, the kernel reads only the valid data
beforethese pages.

read_swap_cache_asyncsuccessively submits read requests for the selected pages to the block layer. If
the function returns aNULLpointer because no memory page could be allocated, the kernel aborts swap-
in because clearly no memory is available for further pages and the readahead mechanism is therefore
less important than the memory shortage prevailing in the system.

18.9 Initiating Memory Reclaim


In the implementation overview at the beginning of this chapter, I demonstrated that the page selection
and swap-out routines discussed so far are controlled by a further layer that decideswhenandhow many
pages are reclaimed. This decision is redirected to two places — first to thekswapddaemon that attempts
to maintain optimal memory balance in the system when no too-memory-intensive applications are
running; and second to an emergency mechanism thatkicks in when the kernel thinks it is nearly totally
out of memory.
Free download pdf