Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


the swap space) inshrink_page_listafterward. Therefore the dirty bit is transferred from the
PTE to thepagebitPG_dirty.

Let’s turn our attention back toshrink_page_list. What now follows is a series of queries that, depend-
ing on page state, trigger all the operations needed to reclaim the page.


PageDirtychecks whether the page is dirty and must therefore be synchronized with the underlying
storage medium. This also includes pages that live in the swap address space. If the page is dirty, this
requires a few actions that are represented by Part 2 in Figure 18-17. They are better discussed by looking
at the code itself.


❑ The kernel ensures that the data are written back by invoking thewritepageaddress space rou-
tine (which is called by thepageouthelper function that supplies all the required arguments).
If the data were mapped from a file in the filesystem, a filesystem-specific routine handles the
appropriate synchronization, and swap pages are inserted in their assigned page slot using
swap_writepage.
❑ Depending on the result ofpageout, different actions are required:
mm/vmscan.c
/* Page is dirty, try to write it out here */
switch (pageout(page, mapping, sync_writeback)) {
case PAGE_KEEP:
goto keep_locked;
case PAGE_ACTIVATE:
goto activate_locked;
case PAGE_SUCCESS:
if (PageWriteback(page) || PageDirty(page))
goto keep;
...
case PAGE_CLEAN:
; /* try to free the page below */
}

Thesync_writebackparameter topageoutdenotes the writeback mode in which
shrink_page_listis operating.
The most desirable return code isPAGE_CLEAN: The data are synchronized with the backing store
and the memory can be reclaimed — this happens in Part 3 of the code flow diagram.
If a write request was successfully issued to the block layer, thenPAGE_SUCCESSis returned. In
asynchronous writeback mode, the page will usually still be under writeback whenpageout
returns, and jumping to the labelkeepjust keeps the page on the local page list, which is
returned to theshrink_list— they will be returned to the LRU lists there. Once the write oper-
ation has been performed, the page contents are synchronized with the backing store so that the
page is no longer dirty the next timeshrink_listis invoked and can therefore be swapped out.
If the write operation was already finished whenpageoutreturned, the data have been written
back, and the kernel can continue with Step 3.
If an error has occurred during writeback, the result is eitherPAGE_KEEPorPAGE_KEEP_ACTIVATE.
Both make the function keep the page on the aforementioned return list, butPAGE_KEEP_
ACTIVATEadditionally sets the page state toPG_active(this can, e.g., happen if the page’s
address space does not provide awritebackmethod, which makes trying to synchronize the
page useless).
Free download pdf