Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Figure 18-18 shows the code flow diagram for the case that the page isnotdirty. Keep in mind that the
kernel can also reach this path coming from Step 2.

Part 1

shrink_list

remove_mapping

try_to_release_page

_ _delete_from_swap_cache

swap_free

_ _remove_from_page_cache

Page has buffers?

Return pages to buddy system

Page in swap cache?

Yes

No

Figure 18-18: Code flow diagram forshrink_list(Part 3).

❑ try_to_releaseis invoked if the page has private data and buffers are therefore associated with
the page (this is typically the case with pages that contain filesystem metadata). This function
attempts either to release the page using thereleasepageoperation in the address space struc-
ture or, if there is no mapping, to free the data usingtry_to_free_buffers.
❑ The kernel then detaches the page from its address space. The auxiliary function
remove_mappingis provided for this purpose.
If the page is held in the swap cache, it is certain that the data are by now presentbothin the
swap space and in the swap cache. Since the page has been swapped out, the swap cache has
fulfilled its duty, and the page can be removed from there with__delete_from_swap_cache.
The kernel additionally usesswap_freeto decrement the usage counter of the page in the swap
area. This is necessary to reflect the fact that there is no longer a reference to the page in the swap
cache.
❑ If the page isnotin the swap cache, it is removed from the general page cache using
__remove_from_page_cache.

It is now guaranteed that the processed page is not present in the kernel’s data structures. Nevertheless,
the main issue has not been resolved — the RAM memory occupied by the page has not yet been freed.
The kernel does this in chunks using page vectors. The page to be freed is inserted in the localfreed_pvec
page vector usingpagevec_add. When this vector is full, all its elements are released collectively by
means of__pagevec_release_nonlru. As discussed in Section 18.6.2, the function returns the memory
space occupied by the pages to the buddy system. The memory reclaimed in this way can be used for
more important tasks — and this is precisely the purpose of swapping and page reclaim.

A few trivial points need to be cleared up onceshrink_listhas iterated over all the pages passed:

❑ The kernel’s swapping statistics are updated.
❑ The number of freed pages is returned as an integer result.
Free download pdf