Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


...
if (total_scanned > sc.swap_cluster_max +
sc.swap_cluster_max / 2) {
wakeup_pdflush(laptop_mode? 0 : total_scanned);
sc.may_writepage = 1;
}

/* Take a nap, wait for some writeback to complete */
if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
congestion_wait(WRITE, HZ/10);
}

Depending on the number of pages freed, the kernel wakes the pdflush daemon to enable the periodic
writeback mechanism. Notice that the number of pages to be flushed is usually restricted to the number
of pages that were scanned. In laptop mode, however, the number of pages is unrestricted. As discussed
in Section 17.13, if the hard disk must be spun up from a power-saving state, then it is supposed to do as
much work as possible before it goes into a power-saving state again.congestion_waitis also invoked
to prevent congestion in the block layer by waiting until a few flushing operations have completed suc-
cessfully.

Finally, the priority of the successful pass is stored in theprev_priorityelement of the zone data struc-
ture asrefill_inactive_zoneuses this information to calculate the swap pressure.

18.10 Shrinking Other Caches


In addition to the page cache, the kernel manages other caches that are generally based on the slab (or
sluborslob,butwe’llusethetermslabfor all of them in the following) mechanism discussed in Chapter 3.

Slabs manage frequently required data structures toensure that memory managed page-by-page by the
buddy system is used more efficiently and that instances of the data types can be allocated quickly and
easily as a result of caching.

Kernel subsystems that use their own caches of this type are able to registershrinkerfunctions dynam-
ically with the kernel; these are called when memory is low to free some memory space already in use
(technically, there is no fixed association between slabs and shrinker functions, but currently there are no
other cache types for which shrinkers are used).

In addition to routines for registering and removing shrinker functions, the kernel must also provide
methods to initiate cache shrinking. These are closely examined in the following sections.

18.10.1 Data Structures


The kernel defines its own data structure to describe the characteristics of shrinker functions:

mm/vmscan.c
struct shrinker {
int (*shrink)(int nr_to_scan, gfp_t gfp_mask);
int seeks; /* seeks to recreate an obj */

/* These are for internal use */
struct list_head list;
long nr; /* objs pending delete */
};
Free download pdf