Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Ultimately, the task ofshrink_active_listis to move a specific number of pages in the list of active
pages in a zone back to the list of active or inactive pages in a zone. Three local lists, on whichpage
instances can be buffered, are created to enable the pages to be scanned:


❑ l_activeandl_inactivehold pages that are to be put back on the list of active or inactive
pages of the zone at the end of the function.
❑ l_holdstores pages still to be scanned before it is decided to which list they are returned.

This task is delegated toisolate_lru_pagesdiscussed just above. Recall that the function reads the
LRU list from tail to head, but arranges the pages in opposite order on the temporary local list. This is a
key point when implementing the LRU algorithm for page replacement. The seldom-used pages on the
activelist automatically move to the rear. As a result, the kernel finds it very easy to scan the least-used
pages because they are located at the beginning of thel_holdlist.


The second section ofrefill_inactive_listbegins once the parameters have been calculated. In this
section, the individual pages are distributed to thel_activeandl_inactivelists of the zone. Instead of
using a code flow diagram to show how this is done, let’s reproduce and discuss the relevant code:


mm/vmscan.c
...
while (!list_empty(&l_hold)) {
cond_resched();
page = lru_to_page(&l_hold);
list_del(&page->lru);
if (page_mapped(page)) {
if (!reclaim_mapped ||
(total_swap_pages == 0 && PageAnon(page)) ||
page_referenced(page, 0)) {
list_add(&page->lru, &l_active);
continue;
}
}
list_add(&page->lru, &l_inactive);
}
...

The code becomes more complex since we are getting to the very heart of page reclaim. The basic action is
represented by a loop that iterates over all elements of thel_holdlist, which in the previous section was
filled with pages regarded as active. These pages must now be reclassified and placed on thel_active
andl_inactivelists.


page_mappedfirst checks whether the page is embedded in the pages tables of any process. This is
easy to do using the reverse mapping data structures. Recall from Chapter 4 that the information as
to whether the page is mapped in page tables is held in the_mapcountelement of eachpageinstance. If
the page is mapped by a single process, the counter value is 0; for non-mapped pages, it is−1. Logically,
page_mappedmust therefore check whetherpage->_mappingis greater than orequal to0.


If there is no mapping, the page is immediately placed on the list of inactive pages.


Ifpage_mappedreturns a true value indicating that the page is associated with at least one process,
it is a little more difficult to decide whether the page is important for the system. One of the

Free download pdf