Swap Area Descriptor

Each active swap area has its own swap_info_struct descriptor in memory. The fields of the descriptor are illustrated in Table 16-1.

Table 16-1. Fields of a swap area descriptor




unsigned int


Swap area flags

kdev t

swap device

Device number of the swap disk partition

spinlock t

sdev lock

Swap area descriptor spin lock

struct dentry *

swap file

Dentry of the file or device file

struct vfsmount *

swap vfsmnt

Mounted filesystem descriptor of the file or device file

unsigned short *

swap map

Pointer to array of counters, one for each swap area page slot

unsigned int

lowest bit

First page slot to be scanned when searching for a free one

unsigned int

highest bit

Last page slot to be scanned when searching for a free one

unsigned int

cluster next

Next page slot to be scanned when searching for a free one

unsigned int

cluster nr

Number of free page slot allocations before restarting from the beginning



Swap area priority



Number of usable page slots

unsigned long


Size of swap area in pages



Pointer to next swap area descriptor

The flags field includes two overlapping subfields:


1 if the swap area is active; 0 if it is nonactive.


This 2-bit field is set to 3 if it is possible to write into the swap area and to 0 otherwise; since the least-significant bit of this field coincides with the bit used to implement swp_used, a swap area can be written only if it is active. The kernel is not allowed to write in a swap area when it is being activated or deactivated.

The swap_map field points to an array of counters, one for each swap area page slot. If the counter is equal to 0, the page slot is free; if it is positive, the page slot is filled with a swapped-out page (the exact meaning of positive values is discussed later in Section 16.3). If the counter has the value swap_map_max (equal to 32, 767), the page stored in the page slot is "permanent" and cannot be removed from the corresponding slot. If the counter has the value swap_map_bad (equal to 32,768), the page slot is considered defective, and thus unusable. HI

[!] "Permanent" page slots protect against overflows of swap_map counters. Without them, valid page slots could become

"defective" if they are referenced too many times, thus leading to data losses. However, no one really expects that a page slot counter could reach the value 32,768. It's just a "belt and suspenders" approach.

The prio field is a signed integer that denotes the order in which the swap subsystem should consider each swap area. Swap areas implemented on faster disks should have a higher priority so they will be used first. Only when they are filled does the swapping algorithm consider lower-priority swap areas. Swap areas that have the same priority are cyclically selected to distribute swapped-out pages among them. As we shall see in Section 16.2.3, the priority is assigned when the swap area is activated.

The sdev_lock field is a spin lock that protects the descriptor against concurrent accesses in SMP systems.

The swap_info array includes max_swapfiles swap area descriptors. Of course, not all of them are necessarily used, only those having the swp_used flag set. Figure 16-1 illustrates the swap_info array, one swap area, and the corresponding array of counters.

Figure 16-1. Swap area data structures

Figure 16-1. Swap area data structures

The nr_swapfiles variable stores the index of the last array element that contains, or that has contained, a used swap area descriptor. Despite its name, the variable does not contain the number of active swap areas.

Descriptors of active swap areas are also inserted into a list sorted by the swap area priority. The list is implemented through the next field of the swap area descriptor, which stores the index of the next descriptor in the swap_info array. This use of the field as an index is different from most fields with the name next, which are usually pointers.

The swap_list variable, of type swap_list_t, includes the following fields:


Index in the swap_info array of the first list element.


Index in the swap_info array of the descriptor of the next swap area to be selected for swapping out pages. This field is used to implement a round-robin algorithm

The swaplock spin lock protects the list against concurrent accesses in multiprocessor systems.

The max field of the swap area descriptor stores the size of the swap area in pages, while the pages field stores the number of usable page slots. These numbers differ because pages does not take the first page slot and the defective page slots into consideration.

Finally, the nr_swap_pages variable contains the number of available (free and nondefective) page slots in all active swap areas, while the total_swap_pages variable contains the total number of nondefective page slots.

Continue reading here: Swapped Out Page Identifier

Was this article helpful?

0 0