Chapter3:MemoryManagement
naming scheme; for example, thePG_lockedconstant defines the bit position inflagsto specify whether
a page is locked or not. The following macros are available to manipulate the bit:
❑ PageLockedqueries whether the bit is set.
❑ SetPageLockedsets thePG_lockedbit, regardless of its previous state.
❑ TestSetPageLockedsets the bit, but also returns its old value.
❑ ClearPageLockeddeletes the bit regardless of its previous state.
❑ TestClearPageLockeddeletes the bit and returns its old value.
There is an identical set of macros to perform the operations shown on the appropriate bit for the other
page flags. The macros are implemented atomically. Although some of them are made up of several state-
ments, special processor commands are used to ensure that they act as if they were a single statement;
that is, they cannot be interrupted as this would result in race conditions. (Chapter 14 describes how race
conditions arise and how they can be prevented.)
Which page flags are available? The following list includes the most important flags (again, their mean-
ings become clear in later chapters):
❑ PG_lockedspecifies whether a page is locked. If the bit is set, other parts of the kernel are not
allowed to access the page. This prevents race conditions in memory management, for example,
when reading data from hard disk into a page frame.
❑ PG_erroris set if an error occurs during an I/O operation involving the page.
❑ PG_referencedandPG_activecontrol how actively a page is used by the system. This infor-
mation is important when the swapping subsystem has to select which page to swap out. The
interaction of the two flags is explained in Chapter 18.
❑ PG_uptodateindicates that the data of a page have beenread without error from a block device.
❑ PG_dirtyis set when the contents of the page have changed as compared to the data on hard
disk. For reasons of performance, pages are notwritten back immediately after each change. The
kernel therefore uses this flag to note which pages have been changed so that they can be flushed
later.
Pages for which this flag has been set are referred to asdirty(generally, this means that the data
in RAM and the data on a secondary storage medium such as a hard disk have not been synchro-
nized).
❑ PG_lruhelps implement page reclaim and swapping. The kernel uses two least recently used
lists^8 to distinguish between active and inactive pages. The bit is set if the page is held on one
of these lists. There is also aPG_activeflag that is set if the page is on the list of active pages.
Chapter 18 discusses this important mechanism in detail.
❑ PG_highmemindicates that a page is in high memory because it cannot be mapped permanently
into kernel memory.
❑ PG_privatemust be set if the value of theprivateelement in thepagestructure is non-NULL.
Pages that are used for I/O use this field to subdivide the page intobuffers(see Chapter 16 for
more information), but other parts of the kernel find different uses to attach private data to a
page.
(^8) Frequently used entries are automatically in the foremost positions on this type of list, whereas inactive entries are always moved
toward the end of the list.