Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 18: Page Reclaim and Swapping


❑ To identify the swap area as such to the kernel (after all, it could simply be a normal partition
with filesystem data which, of course, may not be inadvertently overwritten if the administrator
uses an invalid swap area), theSWAPSPACE2label is set to the end of the first page.^4
❑ The number of available pages is also stored in the header of the swap area. This figure is calcu-
lated by subtracting the number of defective pages from the total number of available pages. 1
must also be subtracted from this number since the first page is used for state information and
for the list of defective blocks.

Although it may seem very important to deal with defective blocks when a swap
area is created, this activity can simply be skipped. In this case,mkswapdoes not
check the data area for errors and consequently does not write any data into the list
of defective blocks. Since the quality of today’s hardware means that very few
errors occur on block devices, an explicit check is normally not needed.

18.3.3 Activating a Swap Area


Interaction with userspace is required in order to notify the kernel that an area initialized withmkswapis
to be used as RAM expansion. The kernel provides theswaponsystem call for this purpose. As usual, it
is implemented insys_swaponwhose code resides inmm/swapfile.c.

Althoughsys_swaponis one of the kernel’s longer functions, it is not particularly complex. It performs
the following actions.

❑ In a first step, the kernel searches for a free element in theswap_infoarray. Initial values are
then assigned to the entry. If a block device partition provides the swap area, the associated
block_deviceinstance is claimed withbd_claim. Recall from Chapter 6.5.2 that the function
claims a block device for a specific holder (in this case the swap implementation) and signalizes
to other parts of the kernel that the device is already attached to it.
❑ After the swap file (or swap partition) has been opened, the first page containing information on
bad blocks and the area size is read in.
❑ setup_swap_extentsinitializes the extent list. We examine this function in more detail below.
❑ As the last step, the new area is added to the swap list according to its priority. As described
above, the swap list is defined using thenextelements of theswap_info_structentries. Two
global variables are also updated:

❑ nr_swap_pagesspecifies the total number of swap pages currently available; it is incre-
mented by the relevant number of pages provided by the newly activated swap area since
the new pages are still completely unused.
❑ total_swap_pagesyields thetotalnumber of swap pages, regardless of how many are used
and how many are still free. This value is also incremented by the number of swap pages in
the new swap area.

If no explicit priority is specified for the new area when the system call is invoked, the kernel uses the
lowest existing priority minus 1. According to thisscheme, new swap areas are included in descending
priority unless the administrator intervenes manually.

(^4) Earlier versions of the kernel used a different swap area format labeledSWAP-SPACE. This had certain disadvantages — above all,
the maximum size limits of 128 or 512 MiB depending on CPU type — and is now no longer supported by the kernel.

Free download pdf