Reserving Page Slots
Before dealing with the implementation details of these two functions, we should examine how page slots are reserved in swap areas. The kernel delegates this task to get_swap_page, which — when called without parameters — returns the number of the page slot to be used next.
The function must first ensure that the system does, in fact, have swap areas — if so, the global variable nr_swap_pages has a value greater than 0.
swap_list.next always yields the number of the swap area currently in use (if there is only one swap area, it obviously always returns the same number). Logically, the kernel begins the search for a free page slot in this area. scan_swap_map scans the page bitmap, making use of swap clustering — a technique examined below.
If no free slot is found in the current swap area, the kernel checks the alternate slots. To do this, it runs through the list of all swap areas until it finds a free slot. Naturally, searching is performed in line with the priorities defined for each individual area by means of the next element of each swap_info entry.
When the area with the lowest priority is reached, the kernel starts searching again from the beginning (i.e., in the area with the highest priority). The search is terminated if no free entry is found after all swap areas in the system have been traversed. The kernel is then not able to swap out the page, and this fact is reported by returning the page number 0 to the calling code.
How are the slot bitmaps of the individual swap areas scanned? Empty entries are recognized because their usage counters equal 0. scan_swap_map therefore scans the swap_map array of the relevant swap partition for such entries, but this is made a little more difficult by swap clustering. A cluster consists of swapfile_cluster contiguous entries into which pages are written sequentially. The kernel first deals with the situation in which there is no free entry in the cluster. Since this is rarely the case, I postpone a discussion of the appropriate code until later.6
Continue reading here: Mmswapfilec
Was this article helpful?