Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


The kernel needs to scan the active and inactive lists to find pages that can be moved between the lists, or
that can be reclaimed from the inactive list. However, the complete lists are not scanned in each pass, but
onlynr_scan_activeelements on the active, andnr_scan_inactiveon the inactive list. Since the kernel
uses an LRU scheme, the number is counted from the tail of the list.pages_scannedremembers how
many pages were scanned in the previous reclaim pass, andvm_statprovides statistical information
about the zone, for instance, the number of currently active and inactive pages. Recall that the statistical
elements can be accessed with the auxiliary functionzone_page_state.

Implementation


After having introduced the required auxiliary data structures, let’s discuss how zone shrinking is ini-
tiated.shrink_zoneexpects an instance ofscan_controlas a parameter. This instance must be filled
with the appropriate values by the caller. Initially, the function is concerned with determining how many
active and inactive pages are to be scanned; it does this by referring to the current state of the processed
zone and to the passedscan_controlinstance:

mm/vmscan.c
static unsigned long shrink_zone(int priority, struct zone *zone,
struct scan_control *sc)
{
unsigned long nr_active;
unsigned long nr_inactive;
unsigned long nr_to_scan;
unsigned long nr_reclaimed = 0;

/*
* Add one to ‘nr_to_scan’ just to make sure that the kernel will
* slowly sift through the active list.
*/
zone->nr_scan_active +=
(zone_page_state(zone, NR_ACTIVE) >> priority) + 1;
nr_active = zone->nr_scan_active;
if (nr_active >= sc->swap_cluster_max)
zone->nr_scan_active = 0;
else
nr_active = 0;

zone->nr_scan_inactive +=
(zone_page_state(zone, NR_INACTIVE) >> priority) + 1;
nr_inactive = zone->nr_scan_inactive;
if (nr_inactive >= sc->swap_cluster_max)
zone->nr_scan_inactive = 0;
else
nr_inactive = 0;

Each timeshrink_zoneis called, the number of active and inactive pages that are to be scanned in this
pass is incremented by the value ofnr_scan_activeornr_scan_inactive, which is scaled with the
current priority by means of a right shift, that is, approximately an integer division by 2priority.1is
always added to ensure that the counter is also incremented even if the bit-shift operation results in 0
over a lengthy period; this can happen with certain load situations. Adding 1 also ensures that, in this
situation too, the inactive zone is filled or the caches are shrunk at some time or other.
Free download pdf