Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


for(;;){
struct list_head *lh;

if (se->start_page <= offset &&
offset < (se->start_page + se->nr_pages)) {
return se->start_block + (offset - se->start_page);
}
lh = se->list.next;
if (lh == &sis->extent_list)
lh = lh->next;
se = list_entry(lh, struct swap_extent, list);
sis->curr_swap_extent = se;
}
}

The search does not begin at the start of the list but at the list element last used. This is stored in
curr_swap_extentsince in most cases, access is made to slots that are next to or at least close to each
other. The address can be calculated with the help of the same extent list element.

A page slot is held in a list element when theoffsetnumber — that is, the number of the searched page
slot — is equal to or greater thanse->start_pagebut less thanse->start_page + se->nr_pages.If
this does not apply, the list is searched sequentiallyuntil the matching elementisfound.Asamatching
elementmustexist, the search can be performed in an endless loop that is terminated when the sector
number is returned.

Once thebioinstance has been filled with the appropriate data, thePG_writebackflag must be set for
the page usingSetPageWritebackbefore the write request is forwarded to the block layer by means of
bio_submit.

When the write request has been executed, the block layer invokes theend_swap_bio_writefunction
(which is based on the standard functionend_page_writeback)toremovethePG_writebackflag from
the page structure.

Notice that writing the contents of a page to the page slot in the swap area isnotsufficient to fully swap
out a page! Before a page can be considered to be completely removed from RAM, the page tables need
to be updated. The page table entry needs, on the one hand, to specify that the page is not in memory and
must, on the other hand, point to the location in the swap space. Since the change must be performed for
all current users of the page, this is an involved task discussed in Section 18.6.7.

18.6 Page Reclaim


Now that I have explained the technical details of writeback, let’s turn our attention to the second major
aspect of the swapping subsystem — theswap policyadopted to determine which pages can be swapped
out of RAM memory without seriously degrading thekernel’s performance. Since page frames are freed
by this and new memory is available for urgent needs, the technique is also calledpage reclaim.

In contrast to the previous sections concerning pages in the swap address space, this
section focuses on pages in any address space. The principles of the swap policy
apply for all pages without a backing store, regardless of whether their data are read
from a file or are generated dynamically. The only difference is the location to
which the data are written out when the kernel decides that the pages are to be
Free download pdf