Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


Once the entries for the lower and upper limits have been updated (if necessary), the kernel increments
the offset for the next search by 1 and uses the offset of the position just found.

If the proposed page is not free, the kernel iterates over the positions until it finds the first one that
is free:

mm/swapfile.c
...
while (++offset <= si->highest_bit) {
if (!si->swap_map[offset]) {
goto checks;
}
}
...
goto lowest;

no_page:
return 0;
}

If this also fails, the kernel jumps to thelowestlabel, thus restarting the search at the lower
limit of the free area. This does not produce an endless loop becausehighest_bitis set to 0 when
the last available page is allocated. As the previous code section shows, this is an abort condition for
the search.

We must now examine what happens if there is no current cluster. In this case, the kernel attempts to
open a new cluster. This presupposes that an empty section consisting of at leastSWAPFILE_CLUSTER
empty slots is present in the swap area. As clusters do not require any particular alignment of their
starting position, the kernel starts searching from the lowest position as of which there are free entries
and which is defined bylowest_bit(the code shown is at the position of the/* Find new cluster */
comment inserted above):

mm/swapfile.c
si->cluster_nr = SWAPFILE_CLUSTER - 1;
if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER)
goto lowest;

offset = si->lowest_bit;
last_in_cluster = offset + SWAPFILE_CLUSTER - 1;

/* Locate the first empty (unaligned) cluster */
for (; last_in_cluster <= si->highest_bit; offset++) {
if (si->swap_map[offset])
last_in_cluster = offset + SWAPFILE_CLUSTER;
else if (offset == last_in_cluster) {
si->cluster_next = offset-SWAPFILE_CLUSTER-1;
goto cluster;
}
}
goto lowest;

When there are not enough free slots to create a new cluster, the kernel branches to thelowestlabel and
starts an entry-by-entry search there.
Free download pdf