Transferring swap pages wouldn't be so complicated if there weren't so many race conditions and other potential hazards to guard against. Here are some of the things that have to be checked regularly:
There may be too many asynchronous swap operations going on; when this occurs, only synchronous I/O operations are started (see Section 16.4.2 later in this chapter).
The process that owns a page may terminate while the page is being swapped in or out.
Another process may be in the middle of swapping in a page that the current one is trying to swap out or vice versa.
We'll follow a bottom-up approach in the following sections. First we describe the synchronization mechanisms that avoid data corruption caused by simultaneous I/O operations on the same page frame or on the same page slot. Then we illustrate a few functions used to perform the data transfer of a swapped page.
Like any other disk access type, I/O data transfers for swap pages are blocking operations. Therefore, the kernel must take care to avoid simultaneous transfers involving the same page frame, the same page slot, or both.
Race conditions can be avoided on the page frame through the mechanisms discussed in Chapter 13. Specifically, before starting an I/O operation on the page frame, the kernel invokes the wait_on_page( ) function to wait until the PG_locked flag is off. When the function returns, the page frame lock has been acquired, and ...