add_to_swap(), step by step
- lives at mm/swap_state.c line 163
- Receives two parameters: a struct page, which is the page to be swapped out; and a struct list_head, which is used only for the huge page case
- Has two local variables: a swap_entry_t (which is a bitfield, so all "members" are extracted via macros), and an int to collect the returned error code from add_to_swap_cache().
- Begins by asserting two conditions: that the page has been locked by the caller, and is up to date. It asserts with the BUG_ON macro, which invokes a kernel panic if the condition fails.
- Allocates swap space for the page by calling get_swap_page(), which lives at swapfile.c line 640. Stores the handle to it in the local swap_entry_t.
- handles the huge page case (which is beyond the scope of this article).
- Inserts the swap entry into the kernel's swap management system by calling add_to_swap_cache(), which lives at swap_state.c line 121.
- Marks the page as dirty, so that a kernel pdflush thread will do the actual write to disk, and exits returning 1 if it was successfully added to swap. Otherwise, if add_to_swap_cache returned an error code for an -ENOMEM allocation failure, cleans up by calling swapcache_free and exits returning 0.
Line numbers are current as of December 2013, in Linus' tree.