next up previous contents
Next: Reducing the Size of Up: Swapping and Discarding Pages Previous: The Kernel Swap Daemon

Swapping Pages Out

  The swap daemon looks at each process in the system in turn to see if it is a good candidate for swapping. Good candidates are processes that can be swapped (some cannot) and that have one or more pages which can be swapped or discarded from memory. Pages are swapped only if the data in them cannot be retrieved another way. A lot of the contents of an executable image come from the image's file and can easily be re-read from that file. For example, the executable instructions of an image will never be modified by the image and so will never be written to the swap file. These pages can simple be discarded; when they are again referenced by the process, they will be brought back into memory from the executable image.

Once the process to swap has been located, the swap daemon looks through all of its virtual memory regions looking for areas which are not shared or locked.

Linux does not swap out all of the swappable pages of the process that it has selected; instead it removes only a small number of pages. . Pages cannot be swapped or discarded if they:

    The swap algorithm uses page aging. Each page has a counter (held in the mem_map_t  data structure) that gives the Kernel swap daemon some idea whether or not a page is worth swapping. Pages age when they are unused and rejuvinate on access; the swap daemon only swaps out old pages. The default action when a page is first allocated, is to give it an initial age of 3. Each time it is touched (by the memory management subsystem gif) it's age is increased by 3 to a maximum of 20. Each time the Kernel swap daemon runs it ages pages, decrementing their age by 1. These default actions can be changed and for this reason they (and other swap related information) are stored in swap_control.


Figure: Alpha AXP Page Table Entry

If the page is old ( tex2html_wrap11612 ), the swap daemon will process it further. Dirty pages are pages which can be swapped out. Linux uses an architecture specific bit in the PTE to describe pages this way (see Figure gif). However, not all dirty pages are neccessarily written to the swap file. Every virtual memory region of a process may have its own swap method (pointed at by tex2html_wrap11613 ) and, in this case, that method is used. Otherwise, the swap daemon will allocate a page in the swap file and write the page out to that device. If the swap file is used, the page's Page Table Entry is replaced by one which is marked as invalid but which contains information about where the page is in the swap file. Whatever the swap method used, the original physical page is made free by putting it back into the free_area . Clean (or rather not dirty) pages can be discarded and put back into the free_area  for re-use. The PTEs for these pages are made invalid. These pages will be re-read from the executable image running when the process attempts to access them.

As the processes PTEs have been changed, the CPU's Translation Look aside Buffers (TLB) which contain cached internal, CPU readable, copies of PTEs must be updated. This update mechanism is architecture specific. If enough of the swappable processes pages have been swapped out or discarded, the swap daemon will again sleep. The next time it wakes it will consider the next process in the system. In this way, the swap daemon nibbles away at each processes physical pages until the system is again in balance. This is much fairer than swapping out whole processes.

next up previous contents
Next: Reducing the Size of Up: Swapping and Discarding Pages Previous: The Kernel Swap Daemon

David A. Rusling