LinuxMM - Chapter 2 Memory Addressing Programmers casually...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 2 Memory Addressing Programmers casually refer to a memory address as the way to access the contents of a memory cell. But when dealing with 80 x 86 microprocessors, we have to distinguish three kinds of addresses: Logical address Included in the machine language instructions to specify the address of an operand or of an instruction. This type of address embodies the well-known 80 x 86 segmented architecture that forces MS-DOS and Windows programmers to divide their programs into segments. Each logical address consists of a segment and an offset (or displacement) that denotes the distance from the start of the segment to the actual address. Linear address (also known as virtual address) A single 32-bit unsigned integer that can be used to address up to 4 GB that is, up to 4,294,967,296 memory cells. Linear addresses are usually represented in hexadecimal notation; their values range from 0x00000000 to 0xffffffff. Physical address Used to address memory cells in memory chips. They correspond to the electrical signals sent along the address pins of the microprocessor to the memory bus. Physical addresses are represented as 32-bit or 36-bit unsigned integers. The Memory Management Unit (MMU) transforms a logical address into a linear address by means of a hardware circuit called a segmentation unit; subsequently, a second hardware circuit called a paging unit transforms the linear address into a physical address (see Figure 2-1). In multiprocessor systems, all CPUs usually share the same memory; this means that RAM chips may be accessed concurrently by independent CPUs. Because read or write operations on a RAM chip must be performed serially, a hardware circuit called a memory arbiter is inserted between the bus and every RAM chip. Its role is to grant access to a CPU if the chip is free and to delay it if the chip is busy servicing a request by another processor. Even uniprocessor systems use memory arbiters, because they include specialized processors called DMA controllers that operate concurrently with the CPU. In the case of multiprocessor systems, the structure of the arbiter is more complex because it has more input ports. The dual Pentium, for instance, maintains a two-port arbiter at each chip entrance and requires that the two CPUs exchange synchronization messages before attempting to use the common bus. From the programming point of view, the arbiter is hidden because it is managed by hardware circuits. 2.2. Segmentation in Hardware Starting with the 80286 model, Intel microprocessors perform address translation in two different ways called real mode and protected mode. We'll focus in the next sections on address translation when protected mode is enabled. Real mode exists mostly to maintain processor compatibility with older models 2.2.1. Segment Selectors and Segmentation Registers A logical address consists of two parts: a segment identifier and an offset that specifies the relative address within the segment. The segment identifier is a 16-bit field called the Segment Selector (see Figure 2-2), while the offset is a 32-bit field. Figure 2-2. Segment Selector format To make it easy to retrieve segment selectors quickly, the processor provides segmentation registers whose only purpose is to hold Segment Selectors; these registers are called cs, ss, ds, es, fs, and gs. Although there are only six of them, a program can reuse the same segmentation register for different purposes by saving its content in memory and then restoring it later. Three of the six segmentation registers have specific purposes: cs The code segment register, which points to a segment containing program instructions ss The stack segment register, which points to a segment containing the current program stack ds The data segment register, which points to a segment containing global and static data The remaining three segmentation registers are general purpose and may refer to arbitrary data segments. The cs register has another important function: it includes a 2-bit field that specifies the Current Privilege Level (CPL) of the CPU. The value 0 denotes the highest privilege level, while the value 3 denotes the lowest one. Linux uses only levels 0 and 3, which are respectively called Kernel Mode and User Mode. 2.2.2. Segment Descriptors Each segment is represented by an 8-byte Segment Descriptor that describes the segment characteristics. Segment Descriptors are stored either in the Global Descriptor Table (GDT) or in the Local Descriptor Table(LDT). Usually only one GDT is defined, while each process is permitted to have its own LDT if it needs to create additional segments besides those stored in the GDT. The address and size of the GDT in main memory are contained in the gdtr control register, while the address and size of the currently used LDT are contained in the ldtr control register. Figure 2-3 illustrates the format of a Segment Descriptor; the meaning of the various fields is explained in Table 2-1. Table 2-1. Segment Descriptor fields Field name Base G Description Contains the linear address of the first byte of the segment. Granularity flag: if it is cleared (equal to 0), the segment size is expressed in bytes; otherwise, it is expressed in multiples of 4096 bytes. Holds the offset of the last memory cell in the segment, thus binding the segment length. When G is set to 0, the size of a segment may vary between 1 byte and 1 MB; otherwise, it may vary between 4 KB and 4 GB. System flag: if it is cleared, the segment is a system segment that stores critical data structures such as the Local Descriptor Table; otherwise, it is a normal code or data segment. Characterizes the segment type and its access rights (see the text that follows this table). Descriptor Privilege Level: used to restrict accesses to the segment. It represents the minimal CPU privilege level requested for accessing the segment. Therefore, a segment with its DPL set to 0 is accessible only when the CPL is 0 that is, in Kernel Mode while a segment with its DPL set to 3 is accessible with every CPL value. Segment-Present flag : is equal to 0 if the segment is not stored currently in main memory. Linux always sets this flag (bit 47) to 1, because it never swaps out whole segments to disk. Called D or B depending on whether the segment contains code or data. Its meaning is slightly different in the two cases, but it is basically set (equal to 1) if the addresses used as segment offsets are 32 bits long, and it is cleared if they are 16 bits long (see the Intel manual for further details). Limit S Type DPL P D or B Table 2-1. Segment Descriptor fields Field name AVL Description May be used by the operating system, but it is ignored by Linux. There are several types of segments, and thus several types of Segment Descriptors. The following list shows the types that are widely used in Linux. Code Segment Descriptor Indicates that the Segment Descriptor refers to a code segment; it may be included either in the GDT or in the LDT. The descriptor has the S flag set (non-system segment). Data Segment Descriptor Indicates that the Segment Descriptor refers to a data segment; it may be included either in the GDT or in the LDT. The descriptor has the S flag set. Stack segments are implemented by means of generic data segments. Task State Segment Descriptor (TSSD) Indicates that the Segment Descriptor refers to a Task State Segment (TSS) that is, a segment used to save the contents of the processor registers (see the section "Task State Segment" in Chapter 3); it can appear only in the GDT. The corresponding Type field has the value 11 or 9, depending on whether the corresponding process is currently executing on a CPU. The S flag of such descriptors is set to 0. Figure 2-3. Segment Descriptor format Local Descriptor Table Descriptor (LDTD) Indicates that the Segment Descriptor refers to a segment containing an LDT; it can appear only in the GDT. The corresponding Type field has the value 2. The S flag of such descriptors is set to 0. The next section shows how 80 x 86 processors are able to decide whether a segment descriptor is stored in the GDT or in the LDT of the process. 2.2.3. Fast Access to Segment Descriptors We recall that logical addresses consist of a 16-bit Segment Selector and a 32-bit Offset, and that segmentation registers store only the Segment Selector. To speed up the translation of logical addresses into linear addresses, the 80 x 86 processor provides an additional nonprogrammable registerthat is, a register that cannot be set by a programmerfor each of the six programmable segmentation registers. Each nonprogrammable register contains the 8-byte Segment Descriptor (described in the previous section) specified by the Segment Selector contained in the corresponding segmentation register. Every time a Segment Selector is loaded in a segmentation register, the corresponding Segment Descriptor is loaded from memory into the matching nonprogrammable CPU register. From then on, translations of logical addresses referring to that segment can be performed without accessing the GDT or LDT stored in main memory; the processor can refer only directly to the CPU register containing the Segment Descriptor. Accesses to the GDT or LDT are necessary only when the contents of the segmentation registers change (see Figure 2-4). Figure 2-4. Segment Selector and Segment Descriptor Any Segment Selector includes three fields that are described in Table 2-2. Table 2-2. Segment Selector fields Field name index Description Identifies the Segment Descriptor entry contained in the GDT or in the LDT (described further in the text following this table). Table Indicator : specifies whether the Segment Descriptor is included in the GDT (TI = 0) or in the LDT (TI = 1). Requestor Privilege Level : specifies the Current Privilege Level of the CPU when the corresponding Segment Selector is loaded into the cs register; it also may be used to selectively weaken the processor privilege level when accessing data segments (see Intel documentation for details). TI RPL Because a Segment Descriptor is 8 bytes long, its relative address inside the GDT or the LDT is obtained by multiplying the 13-bit index field of the Segment Selector by 8. For instance, if the GDT is at 0x00020000 (the value stored in the gdtr register) and the index specified by the Segment Selector is 2, the address of the corresponding Segment Descriptor is 0x00020000 + (2 x 8), or 0x00020010. The first entry of the GDT is always set to 0. This ensures that logical addresses with a null Segment Selector will be considered invalid, thus causing a processor exception. The maximum number of Segment Descriptors that can be stored in the GDT is 8,191 (i.e., 2131). 2.2.4. Segmentation Unit Figure 2-5 shows in detail how a logical address is translated into a corresponding linear address. The segmentation unit performs the following operations: Examines the TI field of the Segment Selector to determine which Descriptor Table stores the Segment Descriptor. This field indicates that the Descriptor is either in the GDT (in which case the segmentation unit gets the base linear address of the GDT from the gdtr register) or in the active LDT (in which case the segmentation unit gets the base linear address of that LDT from the ldtr register). Computes the address of the Segment Descriptor from the index field of the Segment Selector. The index field is multiplied by 8 (the size of a Segment Descriptor), and the result is added to the content of the gdtr or ldtr register. Adds the offset of the logical address to the Base field of the Segment Descriptor, thus obtaining the linear address. Figure 2-5. Translating a logical address Notice that, thanks to the nonprogrammable registers associated with the segmentation registers, the first two operations need to be performed only when a segmentation register has been changed. 2.3. Segmentation in Linux Segmentation has been included in 80 x 86 microprocessors to encourage programmers to split their applications into logically related entities, such as subroutines or global and local data areas. However, Linux uses segmentation in a very limited way. In fact, segmentation and paging are somewhat redundant, because both can be used to separate the physical address spaces of processes: segmentation can assign a different linear address space to each process, while paging can map the same linear address space into different physical address spaces. Linux prefers paging to segmentation for the following reasons: Memory management is simpler when all processes use the same segment register values that is, when they share the same set of linear addresses. One of the design objectives of Linux is portability to a wide range of architectures; RISC architectures in particular have limited support for segmentation. The 2.6 version of Linux uses segmentation only when required by the 80 x 86 architecture. All Linux processes running in User Mode use the same pair of segments to address instructions and data. These segments are called user code segment and user data segment , respectively. Similarly, all Linux processes running in Kernel Mode use the same pair of segments to address instructions and data: they are called kernel code segment and kernel data segment , respectively. Table 2-3 shows the values of the Segment Descriptor fields for these four crucial segments. Table 2-3. Values of the Segment Descriptor fields for the four main Linux segments Segment user code user data kernel code kernel data Base 0x00000000 0x00000000 0x00000000 0x00000000 G 1 1 1 1 Limit 0xfffff 0xfffff 0xfffff 0xfffff S 1 1 1 1 Type 10 2 10 2 DPL 3 3 0 0 D/B 1 1 1 1 P 1 1 1 1 The corresponding Segment Selectors are defined by the macros _ _USER_CS, _ _USER_DS, _ _KERNEL_CS, and _ _KERNEL_DS, respectively. To address the kernel code segment, for instance, the kernel just loads the value yielded by the _ _KERNEL_CS macro into the cs segmentation register. Notice that the linear addresses associated with such segments all start at 0 and reach the addressing limit of 232 -1. This means that all processes, either in User Mode or in Kernel Mode, may use the same logical addresses. Another important consequence of having all segments start at 0x00000000 is that in Linux, logical addresses coincide with linear addresses; that is, the value of the Offset field of a logical address always coincides with the value of the corresponding linear address. As stated earlier, the Current Privilege Level of the CPU indicates whether the processor is in User or Kernel Mode and is specified by the RPL field of the Segment Selector stored in the cs register. Whenever the CPL is changed, some segmentation registers must be correspondingly updated. For instance, when the CPL is equal to 3 (User Mode), the ds register must contain the Segment Selector of the user data segment, but when the CPL is equal to 0, the ds register must contain the Segment Selector of the kernel data segment. A similar situation occurs for the ss register. It must refer to a User Mode stack inside the user data segment when the CPL is 3, and it must refer to a Kernel Mode stack inside the kernel data segment when the CPL is 0. When switching from User Mode to Kernel Mode, Linux always makes sure that the ss register contains the Segment Selector of the kernel data segment. When saving a pointer to an instruction or to a data structure, the kernel does not need to store the Segment Selector component of the logical address, because the ss register contains the current Segment Selector. As an example, when the kernel invokes a function, it executes a call assembly language instruction specifying just the Offset component of its logical address; the Segment Selector is implicitly selected as the one referred to by the cs register. Because there is just one segment of type "executable in Kernel Mode," namely the code segment identified by __KERNEL_CS, it is sufficient to load __KERNEL_CS into cs whenever the CPU switches to Kernel Mode. The same argument goes for pointers to kernel data structures (implicitly using the ds register), as well as for pointers to user data structures (the kernel explicitly uses the es register). Besides the four segments just described, Linux makes use of a few other specialized segments. We'll introduce them in the next section while describing the Linux GDT. 2.3.1. The Linux GDT In uniprocessor systems there is only one GDT, while in multiprocessor systems there is one GDT for every CPU in the system. All GDTs are stored in the cpu_gdt_table array, while the addresses and sizes of the GDTs (used when initializing the gdtr registers) are stored in the cpu_gdt_descr array. If you look in the Source Code Index, you can see that these symbols are defined in the file arch/i386/kernel/head.S . Every macro, function, and other symbol in this book is listed in the Source Code Index, so you can quickly find it in the source code. The layout of the GDTs is shown schematically in Figure 2-6. Each GDT includes 18 segment descriptors and 14 null, unused, or reserved entries. Unused entries are inserted on purpose so that Segment Descriptors usually accessed together are kept in the same 32-byte line of the hardware cache (see the section "Hardware Cache" later in this chapter). The 18 segment descriptors included in each GDT point to the following segments: Four user and kernel code and data segments (see previous section). A Task State Segment (TSS), different for each processor in the system. The linear address space corresponding to a TSS is a small subset of the linear address space corresponding to the kernel data segment. The Task State Segments are sequentially stored in the init_tss array; in particular, the Base field of the TSS descriptor for the nth CPU points to the nth component of the init_tss array. The G (granularity) flag is cleared, while the Limit field is set to 0xeb, because the TSS segment is 236 bytes long. The Type field is set to 9 or 11 (available 32-bit TSS), and the DPL is set to 0, because processes in User Mode are not allowed to access TSS segments. You will find details on how Linux uses TSSs in the section "Task State Segment" in Chapter 3. Figure 2-6. The Global Descriptor Table A segment including the default Local Descriptor Table (LDT), usually shared by all processes (see the next section). Three Thread-Local Storage (TLS) segments: this is a mechanism that allows multithreaded applications to make use of up to three segments containing data local to each thread. The set_thread_area( ) and get_thread_area( ) system calls, respectively, create and release a TLS segment for the executing process. Three segments related to Advanced Power Management (APM ): the BIOS code makes use of segments, so when the Linux APM driver invokes BIOS functions to get or set the status of APM devices, it may use custom code and data segments. Five segments related to Plug and Play (PnP ) BIOS services. As in the previous case, the BIOS code makes use of segments, so when the Linux PnP driver invokes BIOS functions to detect the resources used by PnP devices, it may use custom code and data segments. A special TSS segment used by the kernel to handle "Double fault " exceptions (see "Exceptions" in Chapter 4). As stated earlier, there is a copy of the GDT for each processor in the system. All copies of the GDT store identical entries, except for a few cases. First, each processor has its own TSS segment, thus the corresponding GDT's entries differ. Moreover, a few entries in the GDT may depend on the process that the CPU is executing (LDT and TLS Segment Descriptors). Finally, in some cases a processor may temporarily modify an entry in its copy of the GDT; this happens, for instance, when invoking an APM's BIOS procedure. 2.3.2. The Linux LDTs Most Linux User Mode applications do not make use of a Local Descriptor Table, thus the kernel defines a default LDT to be shared by most processes. The default Local Descriptor Table is stored in the default_ldt array. It includes five entries, but only two of them are effectively used by the kernel: a call gate for iBCS executables, and a call gate for Solaris /x86 executables (see the section "Execution Domains" in Chapter 20). Call gates are a mechanism provided by 80 x 86 microprocessors to change the privilege level of the CPU while invoking a predefined function; as we won't discuss them further, you should consult the Intel documentation for more details. In some cases, however, processes may require to set up their own LDT. This turns out to be useful to applications (such as Wine) that execute segment-oriented Microsoft Windows applications. The modify_ldt( ) system call allows a process to do this. Any custom LDT created by modify_ldt( ) also requires its own segment. When a processor starts executing a process having a custom LDT, the LDT entry in the CPU-specific copy of the GDT is changed accordingly. User Mode applications also may allocate new segments by means of modify_ldt( ); the kernel, however, never makes use of these segments, and it does not have to keep track of the corresponding Segment Descriptors, because they are included in the custom LDT of the process. 2.4. Paging in Hardware The paging unit translates linear addresses into physical ones. One key task in the unit is to check the requested access type against the access rights of the linear address. If the memory access is not valid, it generates a Page Fault exception (see Chapter 4 and Chapter 8). For the sake of efficiency, linear addresses are grouped in fixed-length intervals called pages ; contiguous linear addresses within a page are mapped into contiguous physical addresses. In this way, the kernel can specify the physical address and the access rights of a page instead of those of all the linear addresses included in it. Following the usual convention, we shall use the term "page" to refer both to a set of linear addresses and to the data contained in this group of addresses. The paging unit thinks of all RAM as partitioned into fixed-length page frames (sometimes referred to as physical pages ). Each page frame contains a page that is, the length of a page frame coincides with that of a page. A page frame is a constituent of main memory, and hence it is a storage area. It is important to distinguish a page from a page frame; the former is just a block of data, which may be stored in any page frame or on disk. The data structures that map linear to physical addresses are called page tables ; they are stored in main memory and must be properly initialized by the kernel before enabling the paging unit. Starting with the 80386, all 80 x 86 processors support paging; it is enabled by setting the PG flag of a control register named cr0 . When PG = 0, linear addresses are interpreted as physical addresses. 2.4.1. Regular Paging Starting with the 80386, the paging unit of Intel processors handles 4 KB pages. The 32 bits of a linear address are divided into three fields: Directory The most significant 10 bits Table The intermediate 10 bits Offset The least significant 12 bits The translation of linear addresses is accomplished in two steps, each based on a type of translation table. The first translation table is called the Page Directory, and the second is called the Page Table.[*] [*] In the discussion that follows, the lowercase "page table" term denotes any page storing the mapping between linear and physical addresses, while the capitalized "Page Table" term denotes a page in the last level of page tables. The aim of this two-level scheme is to reduce the amount of RAM required for per-process Page Tables. If a simple one-level Page Table was used, then it would require up to 220 entries (i.e., at 4 bytes per entry, 4 MB of RAM) to represent the Page Table for each process (if the process used a full 4 GB linear address space), even though a process does not use all addresses in that range. The two-level scheme reduces the memory by requiring Page Tables only for those virtual memory regions actually used by a process. Each active process must have a Page Directory assigned to it. However, there is no need to allocate RAM for all Page Tables of a process at once; it is more efficient to allocate RAM for a Page Table only when the process effectively needs it. The physical address of the Page Directory in use is stored in a control register named cr3 . The Directory field within the linear address determines the entry in the Page Directory that points to the proper Page Table. The address's Table field, in turn, determines the entry in the Page Table that contains the physical address of the page frame containing the page. The Offset field determines the relative position within the page frame (see Figure 2-7). Because it is 12 bits long, each page consists of 4096 bytes of data. Figure 2-7. Paging by 80 x 86 processors Both the Directory and the Table fields are 10 bits long, so Page Directories and Page Tables can include up to 1,024 entries. It follows that a Page Directory can address up to 1024 x 1024 x 4096=232 memory cells, as you'd expect in 32-bit addresses. The entries of Page Directories and Page Tables have the same structure. Each entry includes the following fields: Present flag If it is set, the referred-to page (or Page Table) is contained in main memory; if the flag is 0, the page is not contained in main memory and the remaining entry bits may be used by the operating system for its own purposes. If the entry of a Page Table or Page Directory needed to perform an address translation has the Present flag cleared, the paging unit stores the linear address in a control register named cr2 and generates exception 14: the Page Fault exception. (We will see in Chapter 17 how Linux uses this field.) Field containing the 20 most significant bits of a page frame physical address Because each page frame has a 4-KB capacity, its physical address must be a multiple of 4096, so the 12 least significant bits of the physical address are always equal to 0. If the field refers to a Page Directory, the page frame contains a Page Table; if it refers to a Page Table, the page frame contains a page of data. Accessed flag Set each time the paging unit addresses the corresponding page frame. This flag may be used by the operating system when selecting pages to be swapped out. The paging unit never resets this flag; this must be done by the operating system. Dirty flag Applies only to the Page Table entries. It is set each time a write operation is performed on the page frame. As with the Accessed flag, Dirty may be used by the operating system when selecting pages to be swapped out. The paging unit never resets this flag; this must be done by the operating system. Read/Write flag Contains the access right (Read/Write or Read) of the page or of the Page Table (see the section "Hardware Protection Scheme" later in this chapter). User/Supervisor flag Contains the privilege level required to access the page or Page Table (see the later section "Hardware Protection Scheme"). PCD and PWT flags Controls the way the page or Page Table is handled by the hardware cache (see the section "Hardware Cache" later in this chapter). Page Size flag Applies only to Page Directory entries. If it is set, the entry refers to a 2 MB- or 4 MB-long page frame (see the following sections). Global flag Applies only to Page Table entries. This flag was introduced in the Pentium Pro to prevent frequently used pages from being flushed from the TLB cache (see the section "Translation Lookaside Buffers (TLB)" later in this chapter). It works only if the Page Global Enable (PGE) flag of register cr4 is set. 2.4.2. Extended Paging Starting with the Pentium model, 80 x 86 microprocessors introduce extended paging , which allows page frames to be 4 MB instead of 4 KB in size (see Figure 2-8). Extended paging is used to translate large contiguous linear address ranges into corresponding physical ones; in these cases, the kernel can do without intermediate Page Tables and thus save memory and preserve TLB entries (see the section "Translation Lookaside Buffers (TLB)"). Figure 2-8. Extended paging As mentioned in the previous section, extended paging is enabled by setting the Page Size flag of a Page Directory entry. In this case, the paging unit divides the 32 bits of a linear address into two fields: Directory The most significant 10 bits Offset The remaining 22 bits Page Directory entries for extended paging are the same as for normal paging, except that: The Page Size flag must be set. Only the 10 most significant bits of the 20-bit physical address field are significant. This is because each physical address is aligned on a 4-MB boundary, so the 22 least significant bits of the address are 0. Extended paging coexists with regular paging; it is enabled by setting the PSE flag of the cr4 processor register. 2.4.3. Hardware Protection Scheme The paging unit uses a different protection scheme from the segmentation unit. While 80 x 86 processors allow four possible privilege levels to a segment, only two privilege levels are associated with pages and Page Tables, because privileges are controlled by the User/Supervisor flag mentioned in the earlier section "Regular Paging." When this flag is 0, the page can be addressed only when the CPL is less than 3 (this means, for Linux, when the processor is in Kernel Mode). When the flag is 1, the page can always be addressed. Furthermore, instead of the three types of access rights (Read, Write, and Execute) associated with segments, only two types of access rights (Read and Write) are associated with pages. If the Read/Write flag of a Page Directory or Page Table entry is equal to 0, the corresponding Page Table or page can only be read; otherwise it can be read and written.[*] Recent Intel Pentium 4 processors sport an NX (No eXecute) flag in each 64-bit Page Table entry (PAE must be enabled, see the section "The Physical Address Extension (PAE) Paging Mechanism" later in this chapter). Linux 2.6.11 supports this hardware feature. [*] 2.4.4. An Example of Regular Paging A simple example will help in clarifying how regular paging works. Let's assume that the kernel assigns the linear address space between 0x20000000 and 0x2003ffff to a running process.[ ] This space consists of exactly 64 pages. We don't care about the physical addresses of the page frames containing the pages; in fact, some of them might not even be in main memory. We are interested only in the remaining fields of the Page Table entries. [ ] As we shall see in the following chapters, the 3 GB linear address space is an upper limit, but a User Mode process is allowed to reference only a subset of it. Let's start with the 10 most significant bits of the linear addresses assigned to the process, which are interpreted as the Directory field by the paging unit. The addresses start with a 2 followed by zeros, so the 10 bits all have the same value, namely 0x080 or 128 decimal. Thus the Directory field in all the addresses refers to the 129th entry of the process Page Directory. The corresponding entry must contain the physical address of the Page Table assigned to the process (see Figure 2-9). If no other linear addresses are assigned to the process, all the remaining 1,023 entries of the Page Directory are filled with zeros. Figure 2-9. An example of paging The values assumed by the intermediate 10 bits, (that is, the values of the Table field) range from 0 to 0x03f, or from 0 to 63 decimal. Thus, only the first 64 entries of the Page Table are valid. The remaining 960 entries are filled with zeros. Suppose that the process needs to read the byte at linear address 0x20021406. This address is handled by the paging unit as follows: 1. The Directory field 0x80 is used to select entry 0x80 of the Page Directory, which points to the Page Table associated with the process's pages. 2. The Table field 0x21 is used to select entry 0x21 of the Page Table, which points to the page frame containing the desired page. 3. Finally, the Offset field 0x406 is used to select the byte at offset 0x406 in the desired page frame. If the Present flag of the 0x21 entry of the Page Table is cleared, the page is not present in main memory; in this case, the paging unit issues a Page Fault exception while translating the linear address. The same exception is issued whenever the process attempts to access linear addresses outside of the interval delimited by 0x20000000 and 0x2003ffff, because the Page Table entries not assigned to the process are filled with zeros; in particular, their Present flags are all cleared. 2.4.5. The Physical Address Extension (PAE) Paging Mechanism The amount of RAM supported by a processor is limited by the number of address pins connected to the address bus. Older Intel processors from the 80386 to the Pentium used 32-bit physical addresses. In theory, up to 4 GB of RAM could be installed on such systems; in practice, due to the linear address space requirements of User Mode processes, the kernel cannot directly address more than 1 GB of RAM, as we will see in the later section "Paging in Linux." However, big servers that need to run hundreds or thousands of processes at the same time require more than 4 GB of RAM, and in recent years this created a pressure on Intel to expand the amount of RAM supported on the 32-bit 80 x 86 architecture. Intel has satisfied these requests by increasing the number of address pins on its processors from 32 to 36. Starting with the Pentium Pro, all Intel processors are now able to address up to 236 = 64 GB of RAM. However, the increased range of physical addresses can be exploited only by introducing a new paging mechanism that translates 32-bit linear addresses into 36-bit physical ones. With the Pentium Pro processor, Intel introduced a mechanism called Physical Address Extension (PAE). Another mechanism, Page Size Extension (PSE-36), was introduced in the Pentium III processor, but Linux does not use it, and we won't discuss it further in this book. PAE is activated by setting the Physical Address Extension (PAE) flag in the cr4 control register. The Page Size (PS) flag in the page directory entry enables large page sizes (2 MB when PAE is enabled). Intel has changed the paging mechanism in order to support PAE. The 64 GB of RAM are split into 224 distinct page frames, and the physical address field of Page Table entries has been expanded from 20 to 24 bits. Because a PAE Page Table entry must include the 12 flag bits (described in the earlier section "Regular Paging") and the 24 physical address bits, for a grand total of 36, the Page Table entry size has been doubled from 32 bits to 64 bits. As a result, a 4-KB PAE Page Table includes 512 entries instead of 1,024. A new level of Page Table called the Page Directory Pointer Table (PDPT) consisting of four 64-bit entries has been introduced. The cr3 control register contains a 27-bit Page Directory Pointer Table base address field. Because PDPTs are stored in the first 4 GB of RAM and aligned to a multiple of 32 bytes (25), 27 bits are sufficient to represent the base address of such tables. When mapping linear addresses to 4 KB pages (PS flag cleared in Page Directory entry), the 32 bits of a linear address are interpreted in the following way: cr3 Points to a PDPT bits 3130 Point to 1 of 4 possible entries in PDPT bits 2921 Point to 1 of 512 possible entries in Page Directory bits 2012 Point to 1 of 512 possible entries in Page Table bits 11-0 Offset of 4-KB page When mapping linear addresses to 2-MB pages (PS flag set in Page Directory entry), the 32 bits of a linear address are interpreted in the following way: cr3 Points to a PDPT bits 31-30 Point to 1 of 4 possible entries in PDPT bits 2921 Point to 1 of 512 possible entries in Page Directory bits 20-0 Offset of 2-MB page To summarize, once cr3 is set, it is possible to address up to 4 GB of RAM. If we want to address more RAM, we'll have to put a new value in cr3 or change the content of the PDPT. However, the main problem with PAE is that linear addresses are still 32 bits long. This forces kernel programmers to reuse the same linear addresses to map different areas of RAM. We'll sketch how Linux initializes Page Tables when PAE is enabled in the later section, "Final kernel Page Table when RAM size is more than 4096 MB." Clearly, PAE does not enlarge the linear address space of a process, because it deals only with physical addresses. Furthermore, only the kernel can modify the page tables of the processes, thus a process running in User Mode cannot use a physical address space larger than 4 GB. On the other hand, PAE allows the kernel to exploit up to 64 GB of RAM, and thus to increase significantly the number of processes in the system. 2.4.6. Paging for 64-bit Architectures As we have seen in the previous sections, two-level paging is commonly used by 32-bit microprocessors[*]. Two-level paging, however, is not suitable for computers that adopt a 64-bit architecture. Let's use a thought experiment to explain why: [*] The third level of paging present in 80 x 86 processors with PAE enabled has been introduced only to lower from 1024 to 512 the number of entries in the Page Directory and Page Tables. This enlarges the Page Table entries from 32 bits to 64 bits so that they can store the 24 most significant bits of the physical address. Start by assuming a standard page size of 4 KB. Because 1 KB covers a range of 210 addresses, 4 KB covers 212 addresses, so the Offset field is 12 bits. This leaves up to 52 bits of the linear address to be distributed between the Table and the Directory fields. If we now decide to use only 48 of the 64 bits for addressing (this restriction leaves us with a comfortable 256 TB address space!), the remaining 48-12 = 36 bits will have to be split among Table and the Directory fields. If we now decide to reserve 18 bits for each of these two fields, both the Page Directory and the Page Tables of each process should include 218 entries that is, more than 256,000 entries. For that reason, all hardware paging systems for 64-bit processors make use of additional paging levels. The number of levels used depends on the type of processor. Table 2-4 summarizes the main characteristics of the hardware paging systems used by some 64-bit platforms supported by Linux. Please refer to the section "Hardware Dependency" in Chapter 1 for a short description of the hardware associated with the platform name. Table 2-4. Paging levels in some 64-bit architectures Platform name alpha ia64 ppc64 sh64 x86_64 a Page size 8 KB 4 KB 4 KB 4 KB 4 KB a a Number of address bits used 43 39 41 41 48 Number of paging Linear address levels splitting 3 3 3 3 4 10 + 10 + 10 + 13 9 + 9 + 9 + 12 10 + 10 + 9 + 12 10 + 10 + 9 + 12 9 + 9 + 9 + 9 + 12 This architecture supports different page sizes; we select a typical page size adopted by Linux. As we will see in the section "Paging in Linux" later in this chapter, Linux succeeds in providing a common paging model that fits most of the supported hardware paging systems. 2.4.7. Hardware Cache Today's microprocessors have clock rates of several gigahertz, while dynamic RAM (DRAM) chips have access times in the range of hundreds of clock cycles. This means that the CPU may be held back considerably while executing instructions that require fetching operands from RAM and/or storing results into RAM. Hardware cache memories were introduced to reduce the speed mismatch between CPU and RAM. They are based on the well-known locality principle , which holds both for programs and data structures. This states that because of the cyclic structure of programs and the packing of related data into linear arrays, addresses close to the ones most recently used have a high probability of being used in the near future. It therefore makes sense to introduce a smaller and faster memory that contains the most recently used code and data. For this purpose, a new unit called the line was introduced into the 80 x 86 architecture. It consists of a few dozen contiguous bytes that are transferred in burst mode between the slow DRAM and the fast on-chip static RAM (SRAM) used to implement caches. The cache is subdivided into subsets of lines . At one extreme, the cache can be direct mapped , in which case a line in main memory is always stored at the exact same location in the cache. At the other extreme, the cache is fully associative , meaning that any line in memory can be stored at any location in the cache. But most caches are to some degree Nway set associative , where any line of main memory can be stored in any one of N lines of the cache. For instance, a line of memory can be stored in two different lines of a two-way set associative cache. As shown in Figure 2-10, the cache unit is inserted between the paging unit and the main memory. It includes both a hardware cache memory and a cache controller. The cache memory stores the actual lines of memory. The cache controller stores an array of entries, one entry for each line of the cache memory. Each entry includes a tag and a few flags that describe the status of the cache line. The tag consists of some bits that allow the cache controller to recognize the memory location currently mapped by the line. The bits of the memory's physical address are usually split into three groups: the most significant ones correspond to the tag, the middle ones to the cache controller subset index, and the least significant ones to the offset within the line. Figure 2-10. Processor hardware cache When accessing a RAM memory cell, the CPU extracts the subset index from the physical address and compares the tags of all lines in the subset with the high-order bits of the physical address. If a line with the same tag as the high-order bits of the address is found, the CPU has a cache hit; otherwise, it has a cache miss. When a cache hit occurs, the cache controller behaves differently, depending on the access type. For a read operation, the controller selects the data from the cache line and transfers it into a CPU register; the RAM is not accessed and the CPU saves time, which is why the cache system was invented. For a write operation, the controller may implement one of two basic strategies called write-through and write-back . In a write-through, the controller always writes into both RAM and the cache line, effectively switching off the cache for write operations. In a write-back, which offers more immediate efficiency, only the cache line is updated and the contents of the RAM are left unchanged. After a write-back, of course, the RAM must eventually be updated. The cache controller writes the cache line back into RAM only when the CPU executes an instruction requiring a flush of cache entries or when a FLUSH hardware signal occurs (usually after a cache miss). When a cache miss occurs, the cache line is written to memory, if necessary, and the correct line is fetched from RAM into the cache entry. Multiprocessor systems have a separate hardware cache for every processor, and therefore they need additional hardware circuitry to synchronize the cache contents. As shown in Figure 2-11, each CPU has its own local hardware cache. But now updating becomes more time consuming: whenever a CPU modifies its hardware cache, it must check whether the same data is contained in the other hardware cache; if so, it must notify the other CPU to update it with the proper value. This activity is often called cache snooping . Luckily, all this is done at the hardware level and is of no concern to the kernel. Figure 2-11. The caches in a dual processor Cache technology is rapidly evolving. For example, the first Pentium models included a single on-chip cache called the L1-cache. More recent models also include other larger, slower on-chip caches called the L2-cache, L3-cache, etc. The consistency between the cache levels is implemented at the hardware level. Linux ignores these hardware details and assumes there is a single cache. The CD flag of the cr0 processor register is used to enable or disable the cache circuitry. The NW flag, in the same register, specifies whether the write-through or the write-back strategy is used for the caches. Another interesting feature of the Pentium cache is that it lets an operating system associate a different cache management policy with each page frame. For this purpose, each Page Directory and each Page Table entry includes two flags: PCD (Page Cache Disable), which specifies whether the cache must be enabled or disabled while accessing data included in the page frame; and PWT (Page Write-Through), which specifies whether the write-back or the write-through strategy must be applied while writing data into the page frame. Linux clears the PCD and PWT flags of all Page Directory and Page Table entries; as a result, caching is enabled for all page frames, and the write-back strategy is always adopted for writing. 2.4.8. Translation Lookaside Buffers (TLB) Besides general-purpose hardware caches, 80 x 86 processors include another cache called Translation Lookaside Buffers (TLB) to speed up linear address translation. When a linear address is used for the first time, the corresponding physical address is computed through slow accesses to the Page Tables in RAM. The physical address is then stored in a TLB entry so that further references to the same linear address can be quickly translated. In a multiprocessor system, each CPU has its own TLB, called the local TLB of the CPU. Contrary to the hardware cache, the corresponding entries of the TLB need not be synchronized, because processes running on the existing CPUs may associate the same linear address with different physical ones. When the cr3 control register of a CPU is modified, the hardware automatically invalidates all entries of the local TLB, because a new set of page tables is in use and the TLBs are pointing to old data. 2.5. Paging in Linux Linux adopts a common paging model that fits both 32-bit and 64-bit architectures. As explained in the earlier section "Paging for 64-bit Architectures," two paging levels are sufficient for 32-bit architectures, while 64-bit architectures require a higher number of paging levels. Up to version 2.6.10, the Linux paging model consisted of three paging levels. Starting with version 2.6.11, a four-level paging model has been adopted.[*] The four types of page tables illustrated in Figure 2-12 are called: [*] This change has been made to fully support the linear address bit splitting used by the x86_64 platform (see Table 2-4). Page Page Page Page Global Directory Upper Directory Middle Directory Table The Page Global Directory includes the addresses of several Page Upper Directories, which in turn include the addresses of several Page Middle Directories, which in turn include the addresses of several Page Tables. Each Page Table entry points to a page frame. Thus the linear address can be split into up to five parts. Figure 2-12 does not show the bit numbers, because the size of each part depends on the computer architecture. For 32-bit architectures with no Physical Address Extension, two paging levels are sufficient. Linux essentially eliminates the Page Upper Directory and the Page Middle Directory fields by saying that they contain zero bits. However, the positions of the Page Upper Directory and the Page Middle Directory in the sequence of pointers are kept so that the same code can work on 32-bit and 64-bit architectures. The kernel keeps a position for the Page Upper Directory and the Page Middle Directory by setting the number of entries in them to 1 and mapping these two entries into the proper entry of the Page Global Directory. Figure 2-12. The Linux paging model For 32-bit architectures with the Physical Address Extension enabled, three paging levels are used. The Linux's Page Global Directory corresponds to the 80 x 86's Page Directory Pointer Table, the Page Upper Directory is eliminated, the Page Middle Directory corresponds to the 80 x 86's Page Directory, and the Linux's Page Table corresponds to the 80 x 86's Page Table. Finally, for 64-bit architectures three or four levels of paging are used depending on the linear address bit splitting performed by the hardware (see Table 2-2). Linux's handling of processes relies heavily on paging. In fact, the automatic translation of linear addresses into physical ones makes the following design objectives feasible: Assign a different physical address space to each process, ensuring an efficient protection against addressing errors. Distinguish pages (groups of data) from page frames (physical addresses in main memory). This allows the same page to be stored in a page frame, then saved to disk and later reloaded in a different page frame. This is the basic ingredient of the virtual memory mechanism (see Chapter 17). In the remaining part of this chapter, we will refer for the sake of concreteness to the paging circuitry used by the 80 x 86 processors. As we will see in Chapter 9, each process has its own Page Global Directory and its own set of Page Tables. When a process switch occurs (see the section "Process Switch" in Chapter 3), Linux saves the cr3 control register in the descriptor of the process previously in execution and then loads cr3 with the value stored in the descriptor of the process to be executed next. Thus, when the new process resumes its execution on the CPU, the paging unit refers to the correct set of Page Tables. Mapping linear to physical addresses now becomes a mechanical task, although it is still somewhat complex. The next few sections of this chapter are a rather tedious list of functions and macros that retrieve information the kernel needs to find addresses and manage the tables; most of the functions are one or two lines long. You may want to only skim these sections now, but it is useful to know the role of these functions and macros, because you'll see them often in discussions throughout this book. 2.5.1. The Linear Address Fields The following macros simplify Page Table handling: PAGE_SHIFT Specifies the length in bits of the Offset field; when applied to 80 x 86 processors, it yields the value 12. Because all the addresses in a page must fit in the Offset field, the size of a page on 80 x 86 systems is 212 or the familiar 4,096 bytes; the PAGE_SHIFT of 12 can thus be considered the logarithm base 2 of the total page size. This macro is used by PAGE_SIZE to return the size of the page. Finally, the PAGE_MASK macro yields the value 0xfffff000 and is used to mask all the bits of the Offset field. PMD_SHIFT The total length in bits of the Offset and Table fields of a linear address; in other words, the logarithm of the size of the area a Page Middle Directory entry can map. The PMD_SIZE macro computes the size of the area mapped by a single entry of the Page Middle Directory that is, of a Page Table. The PMD_MASK macro is used to mask all the bits of the Offset and Table fields. When PAE is disabled, PMD_SHIFT yields the value 22 (12 from Offset plus 10 from Table), PMD_SIZE yields 222 or 4 MB, and PMD_MASK yields 0xffc00000. Conversely, when PAE is enabled, PMD_SHIFT yields the value 21 (12 from Offset plus 9 from Table), PMD_SIZE yields 221 or 2 MB, and PMD_MASK yields 0xffe00000. Large pages do not make use of the last level of page tables, thus LARGE_PAGE_SIZE, which yields the size of a large page, is equal to PMD_SIZE (2PMD_SHIFT) while LARGE_PAGE_MASK, which is used to mask all the bits of the Offset and Table fields in a large page address, is equal to PMD_MASK. PUD_SHIFT Determines the logarithm of the size of the area a Page Upper Directory entry can map. The PUD_SIZE macro computes the size of the area mapped by a single entry of the Page Global Directory. The PUD_MASK macro is used to mask all the bits of the Offset, Table, Middle Air, and Upper Air fields. On the 80 x 86 processors, PUD_SHIFT is always equal to PMD_SHIFT and PUD_SIZE is equal to 4 MB or 2 MB. PGDIR_SHIFT Determines the logarithm of the size of the area that a Page Global Directory entry can map. The PGDIR_SIZE macro computes the size of the area mapped by a single entry of the Page Global Directory. The PGDIR_MASK macro is used to mask all the bits of the Offset, Table, Middle Air, and Upper Air fields. When PAE is disabled, PGDIR_SHIFT yields the value 22 (the same value yielded by PMD_SHIFT and by PUD_SHIFT), PGDIR_SIZE yields 222 or 4 MB, and PGDIR_MASK yields 0xffc00000. Conversely, when PAE is enabled, PGDIR_SHIFT yields the value 30 (12 from Offset plus 9 from Table plus 9 from Middle Air), PGDIR_SIZE yields 230 or 1 GB, and PGDIR_MASK yields 0xc0000000. PTRS_PER_PTE, PTRS_PER_PMD, PTRS_PER_PUD, and PTRS_PER_PGD Compute the number of entries in the Page Table, Page Middle Directory, Page Upper Directory, and Page Global Directory. They yield the values 1,024, 1, 1, and 1,024, respectively, when PAE is disabled; and the values 512, 512, 1, and 4, respectively, when PAE is enabled. 2.5.2. Page Table Handling pte_t, pmd_t, pud_t, and pgd_t describe the format of, respectively, a Page Table, a Page Middle Directory, a Page Upper Directory, and a Page Global Directory entry. They are 64bit data types when PAE is enabled and 32-bit data types otherwise. pgprot_t is another 64- bit (PAE enabled) or 32-bit (PAE disabled) data type that represents the protection flags associated with a single entry. Five type-conversion macros _ _ pte, _ _ pmd, _ _ pud, _ _ pgd, and _ _ pgprot cast an unsigned integer into the required type. Five other type-conversion macros pte_val, pmd_val, pud_val, pgd_val, and pgprot_val perform the reverse casting from one of the four previously mentioned specialized types into an unsigned integer. The kernel also provides several macros and functions to read or modify page table entries: pte_none, pmd_none, pud_none, and pgd_none yield the value 1 if the corresponding entry has the value 0; otherwise, they yield the value 0. pte_clear, pmd_clear, pud_clear, and pgd_clear clear an entry of the corresponding page table, thus forbidding a process to use the linear addresses mapped by the page table entry. The ptep_get_and_clear( ) function clears a Page Table entry and returns the previous value. set_pte, set_pmd, set_pud, and set_pgd write a given value into a page table entry; set_pte_atomic is identical to set_pte, but when PAE is enabled it also ensures that the 64-bit value is written atomically. pte_same(a,b) returns 1 if two Page Table entries a and b refer to the same page and specify the same access privileges, 0 otherwise. pmd_large(e) returns 1 if the Page Middle Directory entry e refers to a large page (2 MB or 4 MB), 0 otherwise. The pmd_bad macro is used by functions to check Page Middle Directory entries passed as input parameters. It yields the value 1 if the entry points to a bad Page Table that is, if at least one of the following conditions applies: The page is not in main memory (Present flag cleared). The page allows only Read access (Read/Write flag cleared). Either Accessed or Dirty is cleared (Linux always forces these flags to be set for every existing Page Table). The pud_bad and pgd_bad macros always yield 0. No pte_bad macro is defined, because it is legal for a Page Table entry to refer to a page that is not present in main memory, not writable, or not accessible at all. The pte_present macro yields the value 1 if either the Present flag or the Page Size flag of a Page Table entry is equal to 1, the value 0 otherwise. Recall that the Page Size flag in Page Table entries has no meaning for the paging unit of the microprocessor; the kernel, however, marks Present equal to 0 and Page Size equal to 1 for the pages present in main memory but without read, write, or execute privileges. In this way, any access to such pages triggers a Page Fault exception because Present is cleared, and the kernel can detect that the fault is not due to a missing page by checking the value of Page Size. The pmd_present macro yields the value 1 if the Present flag of the corresponding entry is equal to 1 that is, if the corresponding page or Page Table is loaded in main memory. The pud_present and pgd_present macros always yield the value 1. The functions listed in Table 2-5 query the current value of any of the flags included in a Page Table entry; with the exception of pte_file(), these functions work properly only on Page Table entries for which pte_present returns 1. Table 2-5. Page flag reading functions Function name pte_user( ) pte_read( ) pte_write( ) pte_exec( ) pte_dirty( ) pte_young( ) pte_file( ) Description Reads the User/Supervisor flag Reads the User/Supervisor flag (pages on the 80 x 86 processor cannot be protected against reading) Reads the Read/Write flag Reads the User/Supervisor flag (pages on the 80 x 86 processor cannot be protected against code execution) Reads the Dirty flag Reads the Accessed flag Reads the Dirty flag (when the Present flag is cleared and the Dirty flag is set, the page belongs to a non-linear disk file mapping; see Chapter 16) Another group of functions listed in Table 2-6 sets the value of the flags in a Page Table entry. Table 2-6. Page flag setting functions Function name mk_pte_huge( ) pte_wrprotect( ) pte_rdprotect( ) pte_exprotect( ) pte_mkwrite( ) pte_mkread( ) pte_mkexec( ) pte_mkclean( ) pte_mkdirty( ) pte_mkold( ) pte_mkyoung( ) pte_modify(p,v) Description Sets the Page Size and Present flags of a Page Table entry Clears the Read/Write flag Clears the User/Supervisor flag Clears the User/Supervisor flag Sets the Read/Write flag Sets the User/Supervisor flag Sets the User/Supervisor flag Clears the Dirty flag Sets the Dirty flag Clears the Accessed flag (makes the page old) Sets the Accessed flag (makes the page young) Sets all access rights in a Page Table entry p to a specified Table 2-6. Page flag setting functions Function name Description value v ptep_set_wrprotect( ) Like pte_wrprotect( ), but acts on a pointer to a Page Table entry If the Dirty flag is set, sets the page's access rights to a specified value and invokes flush_tlb_page() (see the section "Translation Lookaside Buffers (TLB)" later in this chapter) Like pte_mkdirty( ) but acts on a pointer to a Page Table entry Like pte_mkclean( ) but acts on a pointer to a Page Table entry and returns the old value of the flag Like pte_mkold( ) but acts on a pointer to a Page Table entry and returns the old value of the flag ptep_set_access_flags() ptep_mkdirty() ptep_test_and_clear_dirty( ) ptep_test_and_clear_young( ) Now, let's discuss the macros listed in Table 2-7 that combine a page address and a group of protection flags into a page table entry or perform the reverse operation of extracting the page address from a page table entry. Notice that some of these macros refer to a page through the linear address of its "page descriptor" (see the section "Page Descriptors" in Chapter 8) rather than the linear address of the page itself. Table 2-7. Macros acting on Page Table entries Macro name pgd_index(addr) Description Yields the index (relative position) of the entry in the Page Global Directory that maps the linear address addr. Receives as parameters the address of a memory descriptor cw (see Chapter 9) and a linear address addr. The macro yields the linear address of the entry in a Page Global Directory that corresponds to the address addr; the Page Global Directory is found through a pointer within the memory descriptor. Yields the linear address of the entry in the master kernel Page Global Directory that corresponds to the address addr (see the later section "Kernel Page Tables"). Yields the page descriptor address of the page frame containing the Page Upper Directory referred to by the Page Global Directory entry pgd. In a two- or three-level paging system, this macro is equivalent to pud_page() applied to the folded Page Upper Directory entry. Receives as parameters a pointer pgd to a Page Global Directory pgd_offset(mm, addr) pgd_offset_k(addr) pgd_page(pgd) pud_offset(pgd, addr) Table 2-7. Macros acting on Page Table entries Macro name Description entry and a linear address addr. The macro yields the linear address of the entry in a Page Upper Directory that corresponds to addr. In a two- or three-level paging system, this macro yields pgd, the address of a Page Global Directory entry. Yields the linear address of the Page Middle Directory referred to by the Page Upper Directory entry pud. In a two-level paging system, this macro is equivalent to pmd_page() applied to the folded Page Middle Directory entry. Yields the index (relative position) of the entry in the Page Middle Directory that maps the linear address addr. Receives as parameters a pointer pud to a Page Upper Directory entry and a linear address addr. The macro yields the address of the entry in a Page Middle Directory that corresponds to addr. In a two-level paging system, it yields pud, the address of a Page Global Directory entry. Yields the page descriptor address of the Page Table referred to by the Page Middle Directory entry pmd. In a two-level paging system, pmd is actually an entry of a Page Global Directory. Receives as parameters the address of a page descriptor p and a group of access rights prot, and builds the corresponding Page Table entry. Yields the index (relative position) of the entry in the Page Table that maps the linear address addr. Yields the linear address of the Page Table that corresponds to the linear address addr mapped by the Page Middle Directory dir. Used only on the master kernel page tables (see the later section "Kernel Page Tables"). Receives as parameters a pointer dir to a Page Middle Directory entry and a linear address addr; it yields the linear address of the entry in the Page Table that corresponds to the linear address addr. If the Page Table is kept in high memory, the kernel establishes a temporary kernel mapping (see the section "Kernel Mappings of High-Memory Page Frames" in Chapter 8), to be released by means of pte_unmap. The macros pte_offset_map_nested and pte_unmap_nested are identical, but they use a different temporary kernel mapping. Returns the page descriptor address of the page referenced by the Page Table entry x. Extracts from the content pte of a Page Table entry the file offset corresponding to a page belonging to a non-linear file memory mapping (see the section "Non-Linear Memory Mappings" in Chapter 16). pud_page(pud) pmd_index(addr) pmd_offset(pud, addr) pmd_page(pmd) mk_pte(p,prot) pte_index(addr) pte_offset_kernel(dir, addr) pte_offset_map(dir, addr) pte_page(x) pte_to_pgoff(pte) Table 2-7. Macros acting on Page Table entries Macro name pgoff_to_pte(offset ) Description Sets up the content of a Page Table entry for a page belonging to a non-linear file memory mapping. The last group of functions of this long list was introduced to simplify the creation and deletion of page table entries. When two-level paging is used, creating or deleting a Page Middle Directory entry is trivial. As we explained earlier in this section, the Page Middle Directory contains a single entry that points to the subordinate Page Table. Thus, the Page Middle Directory entry is the entry within the Page Global Directory, too. When dealing with Page Tables, however, creating an entry may be more complex, because the Page Table that is supposed to contain it might not exist. In such cases, it is necessary to allocate a new page frame, fill it with zeros, and add the entry. If PAE is enabled, the kernel uses three-level paging. When the kernel creates a new Page Global Directory, it also allocates the four corresponding Page Middle Directories; these are freed only when the parent Page Global Directory is released. When two or three-level paging is used, the Page Upper Directory entry is always mapped as a single entry within the Page Global Directory. As usual, the description of the functions listed in Table 2-8 refers to the 80 x 86 architecture. Table 2-8. Page allocation functions Function name Description Allocates a new Page Global Directory; if PAE is enabled, it also allocates the three children Page Middle Directories that map the User Mode linear addresses. The argument mm (the address of a memory descriptor) is ignored on the 80 x 86 architecture. Releases the Page Global Directory at address pgd; if PAE is enabled, it also releases the three Page Middle Directories that map the User Mode linear addresses. In a two- or three-level paging system, this function does nothing: it simply returns the linear address of the Page Global Directory entry pgd. In a two- or three-level paging system, this macro does nothing. Defined so generic three-level paging systems can allocate a new Page Middle Directory for the linear address addr. If PAE is not enabled, the function simply returns the input parameter pud that pgd_alloc(mm) pgd_free( pgd) pud_alloc(mm, pgd, addr) pud_free(x) pmd_alloc(mm, pud, addr) Table 2-8. Page allocation functions Function name Description is, the address of the entry in the Page Global Directory. If PAE is enabled, the function returns the linear address of the Page Middle Directory entry that maps the linear address addr. The argument cw is ignored. pmd_free(x) Does nothing, because Page Middle Directories are allocated and deallocated together with their parent Page Global Directory. Receives as parameters the address of a Page Middle Directory entry pmd and a linear address addr, and returns the address of the Page Table entry corresponding to addr. If the Page Middle Directory entry is null, the function allocates a new Page Table by invoking pte_alloc_one( ). If a new Page Table is allocated, the entry corresponding to addr is initialized and the User/Supervisor flag is set. If the Page Table is kept in high memory, the kernel establishes a temporary kernel mapping (see the section "Kernel Mappings of High-Memory Page Frames" in Chapter 8), to be released by pte_unmap. If the Page Middle Directory entry pmd associated with the address addr is null, the function allocates a new Page Table. It then returns the linear address of the Page Table entry associated with addr. Used only for master kernel page tables (see the later section "Kernel Page Tables"). Releases the Page Table associated with the pte page descriptor pointer. Equivalent to pte_free( ), but used for master kernel page tables. Clears the contents of the page tables of a process from linear address start to end by iteratively releasing its Page Tables and clearing the Page Middle Directory entries. pte_alloc_map(mm, pmd, addr) pte_alloc_kernel(mm, pmd, addr) pte_free(pte) pte_free_kernel(pte) clear_page_range(mmu, start,end) 2.5.3. Physical Memory Layout During the initialization phase the kernel must build a physical addresses map that specifies which physical address ranges are usable by the kernel and which are unavailable (either because they map hardware devices' I/O shared memory or because the corresponding page frames contain BIOS data). The kernel considers the following page frames as reserved : Those falling in the unavailable physical address ranges Those containing the kernel's code and initialized data structures A page contained in a reserved page frame can never be dynamically assigned or swapped to disk. As a general rule, the Linux kernel is installed in RAM starting from the physical address 0x00100000 i.e., from the second megabyte. The total number of page frames required depends on how the kernel is configured. A typical configuration yields a kernel that can be loaded in less than 3 MB of RAM. Why isn't the kernel loaded starting with the first available megabyte of RAM? Well, the PC architecture has several peculiarities that must be taken into account. For example: Page frame 0 is used by BIOS to store the system hardware configuration detected during the Power-On Self-Test(POST); the BIOS of many laptops, moreover, writes data on this page frame even after the system is initialized. Physical addresses ranging from 0x000a0000 to 0x000fffff are usually reserved to BIOS routines and to map the internal memory of ISA graphics cards. This area is the well-known hole from 640 KB to 1 MB in all IBM-compatible PCs: the physical addresses exist but they are reserved, and the corresponding page frames cannot be used by the operating system. Additional page frames within the first megabyte may be reserved by specific computer models. For example, the IBM ThinkPad maps the 0xa0 page frame into the 0x9f one. In the early stage of the boot sequence (see Appendix A), the kernel queries the BIOS and learns the size of the physical memory. In recent computers, the kernel also invokes a BIOS procedure to build a list of physical address ranges and their corresponding memory types. Later, the kernel executes the machine_specific_memory_setup( ) function, which builds the physical addresses map (see Table 2-9 for an example). Of course, the kernel builds this table on the basis of the BIOS list, if this is available; otherwise the kernel builds the table following the conservative default setup: all page frames with numbers from 0x9f (LOWMEMSIZE( )) to 0x100 (HIGH_MEMORY) are marked as reserved. Table 2-9. Example of BIOS-provided physical addresses map Start 0x00000000 0x000f0000 0x00100000 0x07ff0000 0x07ff3000 0xffff0000 End 0x0009ffff 0x000fffff 0x07feffff 0x07ff2fff 0x07ffffff 0xffffffff Type Usable Reserved Usable ACPI data ACPI NVS Reserved A typical configuration for a computer having 128 MB of RAM is shown in Table 2-9. The physical address range from 0x07ff0000 to 0x07ff2fff stores information about the hardware devices of the system written by the BIOS in the POST phase; during the initialization phase, the kernel copies such information in a suitable kernel data structure, and then considers these page frames usable. Conversely, the physical address range of 0x07ff3000 to 0x07ffffff is mapped to ROM chips of the hardware devices. The physical address range starting from 0xffff0000 is marked as reserved, because it is mapped by the hardware to the BIOS's ROM chip (see Appendix A). Notice that the BIOS may not provide information for some physical address ranges (in the table, the range is 0x000a0000 to 0x000effff). To be on the safe side, Linux assumes that such ranges are not usable. The kernel might not see all physical memory reported by the BIOS: for instance, the kernel can address only 4 GB of RAM if it has not been compiled with PAE support, even if a larger amount of physical memory is actually available. The setup_memory( ) function is invoked right after machine_specific_memory_setup( ): it analyzes the table of physical memory regions and initializes a few variables that describe the kernel's physical memory layout. These variables are shown in Table 2-10. Table 2-10. Variables describing the kernel's physical memory layout Variable name num_physpages totalram_pages Description Page frame number of the highest usable page frame Total number of usable page frames Page frame number of the first usable page frame after the kernel image in RAM Page frame number of the last usable page frame Page frame number of the last page frame directly mapped by the kernel (low memory) Total number of page frames not directly mapped by the kernel (high memory) Page frame number of the first page frame not directly mapped by the kernel Page frame number of the last page frame not directly mapped by the kernel min_low_pfn max_pfn max_low_pfn totalhigh_pages highstart_pfn highend_pfn To avoid loading the kernel into groups of noncontiguous page frames, Linux prefers to skip the first megabyte of RAM. Clearly, page frames not reserved by the PC architecture will be used by Linux to store dynamically assigned pages. Figure 2-13 shows how the first 3 MB of RAM are filled by Linux. We have assumed that the kernel requires less than 3 MB of RAM. The symbol _text, which corresponds to physical address 0x00100000, denotes the address of the first byte of kernel code. The end of the kernel code is similarly identified by the symbol _etext. Kernel data is divided into two groups: initialized and uninitialized. The initialized data starts right after _etext and ends at _edata. The uninitialized data follows and ends up at _end. The symbols appearing in the figure are not defined in Linux source code; they are produced while compiling the kernel.[*] [*] You can find the linear address of these symbols in the file System.map, which is created right after the kernel is compiled. Figure 2-13. The first 768 page frames (3 MB) in Linux 2.6 2.5.4. Process Page Tables The linear address space of a process is divided into two parts: Linear addresses from 0x00000000 to 0xbfffffff can be addressed when the process runs in either User or Kernel Mode. Linear addresses from 0xc0000000 to 0xffffffff can be addressed only when the process runs in Kernel Mode. When a process runs in User Mode, it issues linear addresses smaller than 0xc0000000; when it runs in Kernel Mode, it is executing kernel code and the linear addresses issued are greater than or equal to 0xc0000000. In some cases, however, the kernel must access the User Mode linear address space to retrieve or store data. The PAGE_OFFSET macro yields the value 0xc0000000; this is the offset in the linear address space of a process where the kernel lives. In this book, we often refer directly to the number 0xc0000000 instead. The content of the first entries of the Page Global Directory that map linear addresses lower than 0xc0000000 (the first 768 entries with PAE disabled, or the first 3 entries with PAE enabled) depends on the specific process. Conversely, the remaining entries should be the same for all processes and equal to the corresponding entries of the master kernel Page Global Directory (see the following section). 2.5.5. Kernel Page Tables The kernel maintains a set of page tables for its own use, rooted at a so-called master kernel Page Global Directory. After system initialization, this set of page tables is never directly used by any process or kernel thread; rather, the highest entries of the master kernel Page Global Directory are the reference model for the corresponding entries of the Page Global Directories of every regular process in the system. We explain how the kernel ensures that changes to the master kernel Page Global Directory are propagated to the Page Global Directories that are actually used by processes in the section "Linear Addresses of Noncontiguous Memory Areas" in Chapter 8. We now describe how the kernel initializes its own page tables. This is a two-phase activity. In fact, right after the kernel image is loaded into memory, the CPU is still running in real mode; thus, paging is not enabled. In the first phase, the kernel creates a limited address space including the kernel's code and data segments, the initial Page Tables, and 128 KB for some dynamic data structures. This minimal address space is just large enough to install the kernel in RAM and to initialize its core data structures. In the second phase, the kernel takes advantage of all of the existing RAM and sets up the page tables properly. Let us examine how this plan is executed. 2.5.5.1. Provisional kernel Page Tables A provisional Page Global Directory is initialized statically during kernel compilation, while the provisional Page Tables are initialized by the startup_32( ) assembly language function defined in arch/i386/kernel/head.S . We won't bother mentioning the Page Upper Directories and Page Middle Directories anymore, because they are equated to Page Global Directory entries. PAE support is not enabled at this stage. The provisional Page Global Directory is contained in the swapper_pg_dir variable. The provisional Page Tables are stored starting from pg0, right after the end of the kernel's uninitialized data segments (symbol _end in Figure 2-13). For the sake of simplicity, let's assume that the kernel's segments, the provisional Page Tables, and the 128 KB memory area fit in the first 8 MB of RAM. In order to map 8 MB of RAM, two Page Tables are required. The objective of this first phase of paging is to allow these 8 MB of RAM to be easily addressed both in real mode and protected mode. Therefore, the kernel must create a mapping from both the linear addresses 0x00000000 through 0x007fffff and the linear addresses 0xc0000000 through 0xc07fffff into the physical addresses 0x00000000 through 0x007fffff. In other words, the kernel during its first phase of initialization can address the first 8 MB of RAM by either linear addresses identical to the physical ones or 8 MB worth of linear addresses, starting from 0xc0000000. The Kernel creates the desired mapping by filling all the swapper_pg_dir entries with zeroes, except for entries 0, 1, 0x300 (decimal 768), and 0x301 (decimal 769); the latter two entries span all linear addresses between 0xc0000000 and 0xc07fffff. The 0, 1, 0x300, and 0x301 enTRies are initialized as follows: The address field of entries 0 and 0x300 is set to the physical address of pg0, while the address field of entries 1 and 0x301 is set to the physical address of the page frame following pg0. The Present, Read/Write, and User/Supervisor flags are set in all four entries. The Accessed, Dirty, PCD, PWD, and Page Size flags are cleared in all four entries. The startup_32( ) assembly language function also enables the paging unit. This is achieved by loading the physical address of swapper_pg_dir into the cr3 control register and by setting the PG flag of the cr0 control register, as shown in the following equivalent code fragment: movl $swapper_pg_dir-0xc0000000,%eax movl %eax,%cr3 /* set the page table pointer.. */ movl %cr0,%eax orl $0x80000000,%eax movl %eax,%cr0 /* ..and set paging (PG) bit */ 2.5.5.2. Final kernel Page Table when RAM size is less than 896 MB The final mapping provided by the kernel page tables must transform linear addresses starting from 0xc0000000 into physical addresses starting from 0. The _ _pa macro is used to convert a linear address starting from PAGE_OFFSET to the corresponding physical address, while the _ _va macro does the reverse. The master kernel Page Global Directory is still stored in swapper_pg_dir. It is initialized by the paging_init( ) function, which does the following: 1. Invokes pagetable_init( ) to set up the Page Table entries properly. 2. Writes the physical address of swapper_pg_dir in the cr3 control register. 3. If the CPU supports PAE and if the kernel is compiled with PAE support, sets the PAE flag in the cr4 control register. 4. Invokes _ _flush_tlb_all( ) to invalidate all TLB entries. The actions performed by pagetable_init( ) depend on both the amount of RAM present and on the CPU model. Let's start with the simplest case. Our computer has less than 896 MB[*] of RAM, 32-bit physical addresses are sufficient to address all the available RAM, and there is no need to activate the PAE mechanism. (See the earlier section "The Physical Address Extension (PAE) Paging Mechanism.") The highest 128 MB of linear addresses are left available for several kinds of mappings (see sections "Fix-Mapped Linear Addresses" later in this chapter and "Linear Addresses of Noncontiguous Memory Areas" in Chapter 8). The kernel address space left for mapping the RAM is thus 1 GB - 128 MB = 896 MB. [*] The swapper_pg_dir Page Global Directory is reinitialized by a cycle equivalent to the following: pgd = swapper_pg_dir + pgd_index(PAGE_OFFSET); /* 768 */ phys_addr = 0x00000000; while (phys_addr < (max_low_pfn * PAGE_SIZE)) { pmd = one_md_table_init(pgd); /* returns pgd itself */ set_pmd(pmd, _ _pmd(phys_addr | pgprot_val(_ _pgprot(0x1e3)))); /* 0x1e3 == Present, Accessed, Dirty, Read/Write, Page Size, Global */ phys_addr += PTRS_PER_PTE * PAGE_SIZE; /* 0x400000 */ ++pgd; } We assume that the CPU is a recent 80 x 86 microprocessor supporting 4 MB pages and "global" TLB entries. Notice that the User/Supervisor flags in all Page Global Directory entries referencing linear addresses above 0xc0000000 are cleared, thus denying processes in User Mode access to the kernel address space. Notice also that the Page Size flag is set so that the kernel can address the RAM by making use of large pages (see the section "Extended Paging" earlier in this chapter). The identity mapping of the first megabytes of physical memory (8 MB in our example) built by the startup_32( ) function is required to complete the initialization phase of the kernel. When this mapping is no longer necessary, the kernel clears the corresponding page table entries by invoking the zap_low_mappings( ) function. Actually, this description does not state the whole truth. As we'll see in the later section "Fix-Mapped Linear Addresses," the kernel also adjusts the entries of Page Tables corresponding to the "fix-mapped linear addresses ." 2.5.5.3. Final kernel Page Table when RAM size is between 896 MB and 4096 MB In this case, the RAM cannot be mapped entirely into the kernel linear address space. The best Linux can do during the initialization phase is to map a RAM window of size 896 MB into the kernel linear address space. If a program needs to address other parts of the existing RAM, some other linear address interval must be mapped to the required RAM. This implies changing the value of some page table entries. We'll discuss how this kind of dynamic remapping is done in Chapter 8. To initialize the Page Global Directory, the kernel uses the same code as in the previous case. 2.5.5.4. Final kernel Page Table when RAM size is more than 4096 MB Let's now consider kernel Page Table initialization for computers with more than 4 GB; more precisely, we deal with cases in which the following happens: The CPU model supports Physical Address Extension (PAE ). The amount of RAM is larger than 4 GB. The kernel is compiled with PAE support. Although PAE handles 36-bit physical addresses, linear addresses are still 32-bit addresses. As in the previous case, Linux maps a 896-MB RAM window into the kernel linear address space; the remaining RAM is left unmapped and handled by dynamic remapping, as described in Chapter 8. The main difference with the previous case is that a three-level paging model is used, so the Page Global Directory is initialized by a cycle equivalent to the following: pgd_idx = pgd_index(PAGE_OFFSET); /* 3 */ for (i=0; i<pgd_idx; i++) set_pgd(swapper_pg_dir + i, _ _pgd(_ _pa(empty_zero_page) + 0x001)); /* 0x001 == Present */ pgd = swapper_pg_dir + pgd_idx; phys_addr = 0x00000000; for (; i<PTRS_PER_PGD; ++i, ++pgd) { pmd = (pmd_t *) alloc_bootmem_low_pages(PAGE_SIZE); set_pgd(pgd, _ _pgd(_ _pa(pmd) | 0x001)); /* 0x001 == Present */ if (phys_addr < max_low_pfn * PAGE_SIZE) for (j=0; j < PTRS_PER_PMD /* 512 */ && phys_addr < max_low_pfn*PAGE_SIZE; ++j) { set_pmd(pmd, _ _pmd(phys_addr | pgprot_val(_ _pgprot(0x1e3)))); /* 0x1e3 == Present, Accessed, Dirty, Read/Write, Page Size, Global */ phys_addr += PTRS_PER_PTE * PAGE_SIZE; /* 0x200000 */ } } swapper_pg_dir[0] = swapper_pg_dir[pgd_idx]; The kernel initializes the first three entries in the Page Global Directory corresponding to the user linear address space with the address of an empty page (empty_zero_page). The fourth entry is initialized with the address of a Page Middle Directory (pmd) allocated by invoking alloc_bootmem_low_pages( ). The first 448 entries in the Page Middle Directory (there are 512 entries, but the last 64 are reserved for noncontiguous memory allocation; see the section "Noncontiguous Memory Area Management" in Chapter 8) are filled with the physical address of the first 896 MB of RAM. Notice that all CPU models that support PAE also support large 2-MB pages and global pages. As in the previous cases, whenever possible, Linux uses large pages to reduce the number of Page Tables. The fourth Page Global Directory entry is then copied into the first entry, so as to mirror the mapping of the low physical memory in the first 896 MB of the linear address space. This mapping is required in order to complete the initialization of SMP systems: when it is no longer necessary, the kernel clears the corresponding page table entries by invoking the zap_low_mappings( ) function, as in the previous cases. 2.5.6. Fix-Mapped Linear Addresses We saw that the initial part of the fourth gigabyte of kernel linear addresses maps the physical memory of the system. However, at least 128 MB of linear addresses are always left available because the kernel uses them to implement noncontiguous memory allocation and fix-mapped linear addresses. Noncontiguous memory allocation is just a special way to dynamically allocate and release pages of memory, and is described in the section "Linear Addresses of Noncontiguous Memory Areas" in Chapter 8. In this section, we focus on fix-mapped linear addresses. Basically, a fix-mapped linear address is a constant linear address like 0xffffc000 whose corresponding physical address does not have to be the linear address minus 0xc000000, but rather a physical address set in an arbitrary way. Thus, each fix-mapped linear address maps one page frame of the physical memory. As we'll see in later chapters, the kernel uses fix-mapped linear addresses instead of pointer variables that never change their value. Fix-mapped linear addresses are conceptually similar to the linear addresses that map the first 896 MB of RAM. However, a fix-mapped linear address can map any physical address, while the mapping established by the linear addresses in the initial portion of the fourth gigabyte is linear (linear address X maps physical address X-PAGE_OFFSET). With respect to variable pointers, fix-mapped linear addresses are more efficient. In fact, dereferencing a variable pointer requires one memory access more than dereferencing an immediate constant address. Moreover, checking the value of a variable pointer before dereferencing it is a good programming practice; conversely, the check is never required for a constant linear address. Each fix-mapped linear address is represented by a small integer index defined in the enum fixed_addresses data structure: enum fixed_addresses { FIX_HOLE, FIX_VSYSCALL, FIX_APIC_BASE, FIX_IO_APIC_BASE_0, [...] _ _end_of_fixed_addresses }; Fix-mapped linear addresses are placed at the end of the fourth gigabyte of linear addresses. The fix_to_virt( ) function computes the constant linear address starting from the index: inline unsigned long fix_to_virt(const unsigned int idx) { if (idx >= _ _end_of_fixed_addresses) _ _this_fixmap_does_not_exist( ); return (0xfffff000UL - (idx << PAGE_SHIFT)); } Let's assume that some kernel function invokes fix_to_virt(FIX_IOAPIC_BASE_0). Because the function is declared as "inline," the C compiler does not generate a call to fix_to_virt( ), but inserts its code in the calling function. Moreover, the check on the index value is never performed at runtime. In fact, FIX_IOAPIC_BASE_0 is a constant equal to 3, so the compiler can cut away the if statement because its condition is false at compile time. Conversely, if the condition is true or the argument of fix_to_virt( ) is not a constant, the compiler issues an error during the linking phase because the symbol _ _this_fixmap_does_not_exist is not defined anywhere. Eventually, the compiler computes 0xfffff000-(3<<PAGE_SHIFT) and replaces the fix_to_virt( ) function call with the constant linear address 0xffffc000. To associate a physical address with a fix-mapped linear address, the kernel uses the set_fixmap(idx,phys) and set_fixmap_nocache(idx,phys) macros. Both of them initialize the Page Table entry corresponding to the fix_to_virt(idx) linear address with the physical address phys; however, the second function also sets the PCD flag of the Page Table entry, thus disabling the hardware cache when accessing the data in the page frame (see the section "Hardware Cache" earlier in this chapter). Conversely, clear_fixmap(idx) removes the linking between a fix-mapped linear address idx and the physical address. 2.5.7. Handling the Hardware Cache and the TLB The last topic of memory addressing deals with how the kernel makes an optimal use of the hardware caches. Hardware caches and Translation Lookaside Buffers play a crucial role in boosting the performance of modern computer architectures. Several techniques are used by kernel developers to reduce the number of cache and TLB misses. 2.5.7.1. Handling the hardware cache As mentioned earlier in this chapter, hardware caches are addressed by cache lines. The L1_CACHE_BYTES macro yields the size of a cache line in bytes. On Intel models earlier than the Pentium 4, the macro yields the value 32; on a Pentium 4, it yields the value 128. To optimize the cache hit rate, the kernel considers the architecture in making the following decisions. The most frequently used fields of a data structure are placed at the low offset within the data structure, so they can be cached in the same line. When allocating a large set of data structures, the kernel tries to store each of them in memory in such a way that all cache lines are used uniformly. Cache synchronization is performed automatically by the 80 x 86 microprocessors, thus the Linux kernel for this kind of processor does not perform any hardware cache flushing. The kernel does provide, however, cache flushing interfaces for processors that do not synchronize caches. 2.5.7.2. Handling the TLB Processors cannot synchronize their own TLB cache automatically because it is the kernel, and not the hardware, that decides when a mapping between a linear and a physical address is no longer valid. Linux 2.6 offers several TLB flush methods that should be applied appropriately, depending on the type of page table change (see Table 2-11). Table 2-11. Architecture-independent TLB-invalidating methods Method name flush_tlb_all Description Typically used when Flushes all TLB entries (including those that Changing the kernel Table 2-11. Architecture-independent TLB-invalidating methods Method name Description refer to global pages, that is, pages whose Global flag is set) Typically used when page table entries Changing a range of kernel page table entries Performing a process switch Forking a new process Releasing a linear address interval of a process Flushes all TLB entries in a given range of flush_tlb_kernel_range linear addresses (including those that refer to global pages) flush_tlb Flushes all TLB entries of the non-global pages owned by the current process Flushes all TLB entries of the non-global pages owned by a given process Flushes the TLB entries corresponding to a linear address interval of a given process flush_tlb_mm flush_tlb_range flush_tlb_pgtables Flushes the TLB entries of a given Releasing some page contiguous subset of page tables of a given tables of a process process Flushes the TLB of a single Page Table entry of a given process Processing a Page Fault flush_tlb_page Despite the rich set of TLB methods offered by the generic Linux kernel, every microprocessor usually offers a far more restricted set of TLB-invalidating assembly language instructions. In this respect, one of the more flexible hardware platforms is Sun's UltraSPARC. In contrast, Intel microprocessors offers only two TLB-invalidating techniques: All Pentium models automatically flush the TLB entries relative to non-global pages when a value is loaded into the cr3 register. In Pentium Pro and later models, the invlpg assembly language instruction invalidates a single TLB entry mapping a given linear address. Table 2-12 lists the Linux macros that exploit such hardware techniques; these macros are the basic ingredients to implement the architecture-independent methods listed in Table 211. Table 2-12. TLB-invalidating macros for the Intel Pentium Pro and later processors Macro name _ _flush_tlb( ) Description Rewrites cr3 register back into itself Disables global pages by Used by flush_tlb, flush_tlb_mm,flush_tlb_range flush_tlb_all,flush_tlb_kernel_range _ _flush_tlb_global( ) Table 2-12. TLB-invalidating macros for the Intel Pentium Pro and later processors Macro name Description clearing the PGE flag of cr4, rewrites cr3 register back into itself, and sets again the PGE flag Executes invlpg assembly _ language instruction with _flush_tlb_single(addr) parameter addr flush_tlb_page Used by Notice that the flush_tlb_pgtables method is missing from Table 2-12: in the 80 x 86 architecture nothing has to be done when a page table is unlinked from its parent table, thus the function implementing this method is empty. The architecture-independent TLB-invalidating methods are extended quite simply to multiprocessor systems. The function running on a CPU sends an Interprocessor Interrupt (see "Interprocessor Interrupt Handling" in Chapter 4) to the other CPUs that forces them to execute the proper TLB-invalidating function. As a general rule, any process switch implies changing the set of active page tables. Local TLB entries relative to the old page tables must be flushed; this is done automatically when the kernel writes the address of the new Page Global Directory into the cr3 control register. The kernel succeeds, however, in avoiding TLB flushes in the following cases: When performing a process switch between two regular processes that use the same set of page tables (see the section "The schedule( ) Function" in Chapter 7). When performing a process switch between a regular process and a kernel thread. In fact, we'll see in the section "Memory Descriptor of Kernel Threads" in Chapter 9, that kernel threads do not have their own set of page tables; rather, they use the set of page tables owned by the regular process that was scheduled last for execution on the CPU. Besides process switches, there are other cases in which the kernel needs to flush some entries in a TLB. For instance, when the kernel assigns a page frame to a User Mode process and stores its physical address into a Page Table entry, it must flush any local TLB entry that refers to the corresponding linear address. On multiprocessor systems, the kernel also must flush the same TLB entry on the CPUs that are using the same set of page tables, if any. To avoid useless TLB flushing in multiprocessor systems, the kernel uses a technique called lazy TLB mode . The basic idea is the following: if several CPUs are using the same page tables and a TLB entry must be flushed on all of them, then TLB flushing may, in some cases, be delayed on CPUs running kernel threads. In fact, each kernel thread does not have its own set of page tables; rather, it makes use of the set of page tables belonging to a regular process. However, there is no need to invalidate a TLB entry that refers to a User Mode linear address, because no kernel thread accesses the User Mode address space.[*] [*] By the way, the flush_tlb_all method does not use the lazy TLB mode mechanism; it is usually invoked whenever the kernel modifies a Page Table entry relative to the Kernel Mode address space. When some CPUs start running a kernel thread, the kernel sets it into lazy TLB mode. When requests are issued to clear some TLB entries, each CPU in lazy TLB mode does not flush the corresponding entries; however, the CPU remembers that its current process is running on a set of page tables whose TLB entries for the User Mode addresses are invalid. As soon as the CPU in lazy TLB mode switches to a regular process with a different set of page tables, the hardware automatically flushes the TLB entries, and the kernel sets the CPU back in non-lazy TLB mode. However, if a CPU in lazy TLB mode switches to a regular process that owns the same set of page tables used by the previously running kernel thread, then any deferred TLB invalidation must be effectively applied by the kernel. This "lazy" invalidation is effectively achieved by flushing all non-global TLB entries of the CPU. Some extra data structures are needed to implement the lazy TLB mode. The cpu_tlbstate variable is a static array of NR_CPUS structures (the default value for this macro is 32; it denotes the maximum number of CPUs in the system) consisting of an active_mm field pointing to the memory descriptor of the current process (see Chapter 9) and a state flag that can assume only two values: TLBSTATE_OK (non-lazy TLB mode) or TLBSTATE_LAZY (lazy TLB mode). Furthermore, each memory descriptor includes a cpu_vm_mask field that stores the indices of the CPUs that should receive Interprocessor Interrupts related to TLB flushing. This field is meaningful only when the memory descriptor belongs to a process currently in execution. When a CPU starts executing a kernel thread, the kernel sets the state field of its cpu_tlbstate element to TLBSTATE_LAZY; moreover, the cpu_vm_mask field of the active memory descriptor stores the indices of all CPUs in the system, including the one that is entering in lazy TLB mode. When another CPU wants to invalidate the TLB entries of all CPUs relative to a given set of page tables, it delivers an Interprocessor Interrupt to all CPUs whose indices are included in the cpu_vm_mask field of the corresponding memory descriptor. When a CPU receives an Interprocessor Interrupt related to TLB flushing and verifies that it affects the set of page tables of its current process, it checks whether the state field of its cpu_tlbstate element is equal to TLBSTATE_LAZY. In this case, the kernel refuses to invalidate the TLB entries and removes the CPU index from the cpu_vm_mask field of the memory descriptor. This has two consequences: As long as the CPU remains in lazy TLB mode, it will not receive other Interprocessor Interrupts related to TLB flushing. If the CPU switches to another process that is using the same set of page tables as the kernel thread that is being replaced, the kernel invokes _ _flush_tlb( ) to invalidate all non-global TLB entries of the CPU. Chapter 8. Memory Management We saw in Chapter 2 how Linux takes advantage of 80 x 86's segmentation and paging circuits to translate logical addresses into physical ones. We also mentioned that some portion of RAM is permanently assigned to the kernel and used to store both the kernel code and the static kernel data structures. The remaining part of the RAM is called dynamic memory . It is a valuable resource, needed not only by the processes but also by the kernel itself. In fact, the performance of the entire system depends on how efficiently dynamic memory is managed. Therefore, all current multitasking operating systems try to optimize the use of dynamic memory, assigning it only when it is needed and freeing it as soon as possible. Figure 8-1 shows schematically the page frames used as dynamic memory; see the section "Physical Memory Layout" in Chapter 2 for details. This chapter, which consists of three main sections, describes how the kernel allocates dynamic memory for its own use. The sections "Page Frame Management" and "Memory Area Management" illustrate two different techniques for handling physically contiguous memory areas, while the section "Noncontiguous Memory Area Management" illustrates a third technique that handles noncontiguous memory areas. In these sections we'll cover topics such as memory zones, kernel mappings, the buddy system, the slab cache, and memory pools. 8.1. Page Frame Management We saw in the section "Paging in Hardware" in Chapter 2 how the Intel Pentium processor can use two different page frame sizes: 4 KB and 4 MB (or 2 MB if PAE is enabledsee the section "The Physical Address Extension (PAE) Paging Mechanism" in Chapter 2). Linux adopts the smaller 4 KB page frame size as the standard memory allocation unit. This makes things simpler for two reasons: The Page Fault exceptions issued by the paging circuitry are easily interpreted. Either the page requested exists but the process is not allowed to address it, or the page does not exist. In the second case, the memory allocator must find a free 4 KB page frame and assign it to the process. Although both 4 KB and 4 MB are multiples of all disk block sizes, transfers of data between main memory and disks are in most cases more efficient when the smaller size is used. Figure 8-1. Dynamic memory 8.1.1. Page Descriptors The kernel must keep track of the current status of each page frame. For instance, it must be able to distinguish the page frames that are used to contain pages that belong to processes from those that contain kernel code or kernel data structures. Similarly, it must be able to determine whether a page frame in dynamic memory is free. A page frame in dynamic memory is free if it does not contain any useful data. It is not free when the page frame contains data of a User Mode process, data of a software cache, dynamically allocated kernel data structures, buffered data of a device driver, code of a kernel module, and so on. State information of a page frame is kept in a page descriptor of type page, whose fields are shown in Table 8-1. All page descriptors are stored in the mem_map array. Because each descriptor is 32 bytes long, the space required by mem_map is slightly less than 1% of the whole RAM. The virt_to_page(addr) macro yields the address of the page descriptor associated with the linear address addr. The pfn_to_page(pfn) macro yields the address of the page descriptor associated with the page frame having number pfn. Table 8-1. The fields of the page descriptor Type unsigned long atomic_t atomic_t Name flags _count _mapcount Description Array of flags (see Table 8-2). Also encodes the zone number to which the page frame belongs. Page frame's reference counter. Number of Page Table entries that refer to the page frame (-1 if none). Available to the kernel component that is using the page (for instance, it is a buffer head pointer in case of buffer page; see "Block Buffers and Buffer Heads" in Chapter 15). If the page is free, this field is used by the buddy system (see later in this chapter). unsigned long private Table 8-1. The fields of the page descriptor Type struct address_space * mapping Name Description Used when the page is inserted into the page cache (see the section "The Page Cache" in Chapter 15), or when it belongs to an anonymous region (see the section "Reverse Mapping for Anonymous Pages" in Chapter 17). Used by several kernel components with different meanings. For instance, it identifies the position of the data stored in the page frame within the page's disk image or within an anonymous region (Chapter 15), or it stores a swapped-out page identifier (Chapter 17). Contains pointers to the least recently used doubly linked list of pages. unsigned long index struct list_head lru You don't have to fully understand the role of all fields in the page descriptor right now. In the following chapters, we often come back to the fields of the page descriptor. Moreover, several fields have different meaning, according to whether the page frame is free or what kernel component is using the page frame. Let's describe in greater detail two of the fields: _count A usage reference counter for the page. If it is set to -1, the corresponding page frame is free and can be assigned to any process or to the kernel itself. If it is set to a value greater than or equal to 0, the page frame is assigned to one or more processes or is used to store some kernel data structures. The page_count( ) function returns the value of the _count field increased by one, that is, the number of users of the page. flags Includes up to 32 flags (see Table 8-2) that describe the status of the page frame. For each PG_xyz flag, the kernel defines some macros that manipulate its value. Usually, the PageXyz macro returns the value of the flag, while the SetPageXyz and ClearPageXyz macro set and clear the corresponding bit, respectively. Table 8-2. Flags describing the status of a page frame Flag name Meaning Table 8-2. Flags describing the status of a page frame Flag name PG_locked PG_error PG_referenced PG_uptodate Meaning The page is locked; for instance, it is involved in a disk I/O operation. An I/O error occurred while transferring the page. The page has been recently accessed. This flag is set after completing a read operation, unless a disk I/O error happened. The page has been modified (see the section "Implementing the PFRA" in Chapter 17). The page is in the active or inactive page list (see the section "The Least Recently Used (LRU) Lists" in Chapter 17). The page is in the active page list (see the section "The Least Recently Used (LRU) Lists" in Chapter 17). The page frame is included in a slab (see the section "Memory Area Management" later in this chapter). The page frame belongs to the ZONE_HIGHMEM zone (see the following section "Non-Uniform Memory Access (NUMA)"). Used by some filesystems such as Ext2 and Ext3 (see Chapter 18). Not used on the 80 x 86 architecture. The page frame is reserved for kernel code or is unusable. The private field of the page descriptor stores meaningful data. The page is being written to disk by means of the writepage method (see Chapter 16) . Used for system suspend/resume. The page frame is handled through the extended paging mechanism (see the section "Extended Paging" in Chapter 2). The page belongs to the swap cache (see the section "The Swap Cache" in Chapter 17). The page has been marked to be written to disk in order to reclaim memory. Used for system suspend/resume. PG_dirty PG_lru PG_active PG_slab PG_highmem PG_checked PG_arch_1 PG_reserved PG_private PG_writeback PG_nosave PG_compound PG_swapcache PG_mappedtodisk All data in the page frame corresponds to blocks allocated on disk. PG_reclaim PG_nosave_free 8.1.2. Non-Uniform Memory Access (NUMA) We are used to thinking of the computer's memory as a homogeneous, shared resource. Disregarding the role of the hardware caches, we expect the time required for a CPU to access a memory location to be essentially the same, regardless of the location's physical address and the CPU. Unfortunately, this assumption is not true in some architectures. For instance, it is not true for some multiprocessor Alpha or MIPS computers. Linux 2.6 supports the Non-Uniform Memory Access (NUMA) model, in which the access times for different memory locations from a given CPU may vary. The physical memory of the system is partitioned in several nodes . The time needed by a given CPU to access pages within a single node is the same. However, this time might not be the same for two different CPUs. For every CPU, the kernel tries to minimize the number of accesses to costly nodes by carefully selecting where the kernel data structures that are most often referenced by the CPU are stored.[*] [*] Furthermore, the Linux kernel makes use of NUMA even for some peculiar uniprocessor systems that have huge "holes" in the physical address space. The kernel handles these architectures by assigning the contiguous subranges of valid physical addresses to different memory nodes . The physical memory inside each node can be split into several zones, as we will see in the next section. Each node has a descriptor of type pg_data_t, whose fields are shown in Table 8-3. All node descriptors are stored in a singly linked list, whose first element is pointed to by the pgdat_list variable. Table 8-3. The fields of the node descriptor Type struct zone [ ] struct zonelist [ ] int struct page * struct bdata bootmem_data * unsigned long unsigned long unsigned long int pg_data_t * node_start_pfn node_present_pages Name node_zones Description Array of zone descriptors of the node Array of zonelist data structures used by the page allocator (see the later section "Memory Zones") Number of zones in the node Array of page descriptors of the node Used in the kernel initialization phase Index of the first page frame in the node Size of the memory node, excluding holes (in page frames) Identifier of the node Next item in the memory node list Wait queue for the kswapd pageout daemon (see the section "Periodic Reclaiming" in Chapter 17) Pointer to the process descriptor of the kswapd kernel thread Logarithmic size of free blocks to be created by kswapd node_zonelists nr_zones node_mem_map node_spanned_pages Size of the node, including holes (in page frames) node_id pgdat_next wait_queue_head_t kswapd_wait struct task_struct * kswapd int kswapd_max_order As usual, we are mostly concerned with the 80 x 86 architecture. IBM-compatible PCs use the Uniform Memory Access model (UMA), thus the NUMA support is not really required. However, even if NUMA support is not compiled in the kernel, Linux makes use of a single node that includes all system physical memory. Thus, the pgdat_list variable points to a list consisting of a single elementthe node 0 descriptorstored in the contig_page_data variable. On the 80 x 86 architecture, grouping the physical memory in a single node might appear useless; however, this approach makes the memory handling code more portable, because the kernel can assume that the physical memory is partitioned in one or more nodes in all architectures.[*] [*] We have another example of this kind of design choice: Linux uses four levels of Page Tables even when the hardware architecture defines just two levels (see the section "Paging in Linux" in Chapter 2). 8.1.3. Memory Zones In an ideal computer architecture, a page frame is a memory storage unit that can be used for anything: storing kernel and user data, buffering disk data, and so on. Every kind of page of data can be stored in a page frame, without limitations. However, real computer architectures have hardware constraints that may limit the way page frames can be used. In particular, the Linux kernel must deal with two hardware constraints of the 80 x 86 architecture: The Direct Memory Access (DMA) processors for old ISA buses have a strong limitation: they are able to address only the first 16 MB of RAM. In modern 32-bit computers with lots of RAM, the CPU cannot directly access all physical memory because the linear address space is too small. To cope with these two limitations, Linux 2.6 partitions the physical memory of every memory node into three zones. In the 80 x 86 UMA architecture the zones are: ZONE_DMA Contains page frames of memory below 16 MB ZONE_NORMAL Contains page frames of memory at and above 16 MB and below 896 MB ZONE_HIGHMEM Contains page frames of memory at and above 896 MB The ZONE_DMA zone includes page frames that can be used by old ISA-based devices by means of the DMA. (The section "Direct Memory Access (DMA)" in Chapter 13 gives further details on DMA.) The ZONE_DMA and ZONE_NORMAL zones include the "normal" page frames that can be directly accessed by the kernel through the linear mapping in the fourth gigabyte of the linear address space (see the section "Kernel Page Tables" in Chapter 2). Conversely, the ZONE_HIGHMEM zone includes page frames that cannot be directly accessed by the kernel through the linear mapping in the fourth gigabyte of linear address space (see the section "Kernel Mappings of High-Memory Page Frames" later in this chapter). The ZONE_HIGHMEM zone is always empty on 64-bit architectures. Each memory zone has its own descriptor of type zone. Its fields are shown in Table 8-4. Table 8-4. The fields of the zone descriptor Type unsigned long Name free_pages Description Number of free pages in the zone. Number of reserved pages of the zone (see the section "The Pool of Reserved Page Frames" later in this chapter). Low watermark for page frame reclaiming; also used by the zone allocator as a threshold value (see the section "The Zone Allocator" later in this chapter). High watermark for page frame reclaiming; also used by the zone allocator as a threshold value. unsigned long pages_min unsigned long pages_low unsigned long pages_high unsigned long Specifies how many page frames in each zone lowmem_reserve must be reserved for handling low-on-memory critical situations. pageset lock struct per_cpu_pageset spinlock_t Data structure used to implement special caches of single page frames (see the section "The Per-CPU Page Frame Cache" later in this chapter). Spin lock protecting the descriptor. Identifies the blocks of free page frames in the zone (see the section "The Buddy System Algorithm" later in this chapter). Spin lock for the active and inactive lists. List of active pages in the zone (see Chapter 17). List of inactive pages in the zone (see Chapter 17). Number of active pages to be scanned when reclaiming memory (see the section "Low On Memory Reclaiming" in Chapter 17). struct free_area spinlock_t struct list head struct list head free_area lru_lock active_list inactive_list unsigned long unsigned long nr_scan_active nr_scan_inactive Number of inactive pages to be scanned when Table 8-4. The fields of the zone descriptor Type Name Description reclaiming memory. unsigned long unsigned long unsigned long nr_active nr_inactive pages_scanned Number of pages in the zone's active list. Number of pages in the zone's inactive list. Counter used when doing page frame reclaiming in the zone. Flag set when the zone is full of unreclaimable pages. Temporary zone's priority (used when doing page frame reclaiming). Zone's priority ranging between 12 and 0 (used by the page frame reclaiming algorithm, see the section "Low On Memory Reclaiming" in Chapter 17). Hash table of wait queues of processes waiting for one of the pages of the zone. Size of the wait queue hash table. Power-of-2 order of the size of the wait queue hash table array. Memory node (see the earlier section "Non-Uniform Memory Access (NUMA)"). Index of the first page frame of the zone. Total size of zone in pages, including holes. Total size of zone in pages, excluding holes. Pointer to the conventional name of the zone: "DMA," "Normal," or "HighMem." int int all_unreclaimable temp_priority int wait_queue_head_t * unsigned long unsigned long struct pglist_data * struct page * unsigned long unsigned long unsigned long char * prev_priority wait_table wait_table_size wait_table_bits zone_pgdat zone_mem_map Pointer to first page descriptor of the zone. zone_start_pfn spanned_pages present_pages name Many fields of the zone structure are used for page frame reclaiming and will be described in Chapter 17. Each page descriptor has links to the memory node and to the zone inside the node that includes the corresponding page frame. To save space, these links are not stored as classical pointers; rather, they are encoded as indices stored in the high bits of the flags field. In fact, the number of flags that characterize a page frame is limited, thus it is always possible to reserve the most significant bits of the flags field to encode the proper memory node and zone number.[*] The page_zone( ) function receives as its parameter the address of a page descriptor; it reads the most significant bits of the flags field in the page descriptor, then it determines the address of the corresponding zone descriptor by looking in the zone_table array. This array is initialized at boot time with the addresses of all zone descriptors of all memory nodes. [*] The number of bits reserved for the indices depends on whether the kernel supports the NUMA model and on the size of the flags field. If NUMA is not supported, the flags field has two bits for the zone index and one bitalways set to zerofor the node index. On NUMA 32-bit architectures, flags has two bits for the zone index and six bits for the node number. Finally, on NUMA 64-bit architectures, the 64-bit flags field has 2 bits for the zone index and 10 bits for the node number. When the kernel invokes a memory allocation function, it must specify the zones that contain the requested page frames. The kernel usually specifies which zones it's willing to use. For instance, if a page frame must be directly mapped in the fourth gigabyte of linear addresses but it is not going to be used for ISA DMA transfers, then the kernel requests a page frame either in ZONE_NORMAL or in ZONE_DMA. Of course, the page frame should be obtained from ZONE_DMA only if ZONE_NORMAL does not have free page frames. To specify the preferred zones in a memory allocation request, the kernel uses the zonelist data structure, which is an array of zone descriptor pointers. 8.1.4. The Pool of Reserved Page Frames Memory allocation requests can be satisfied in two different ways. If enough free memory is available, the request can be satisfied immediately. Otherwise, some memory reclaiming must take place, and the kernel control path that made the request is blocked until additional memory has been freed. However, some kernel control paths cannot be blocked while requesting memorythis happens, for instance, when handling an interrupt or when executing code inside a critical region. In these cases, a kernel control path should issue atomic memory allocation requests (using the GFP_ATOMIC flag; see the later section "The Zoned Page Frame Allocator"). An atomic request never blocks: if there are not enough free pages, the allocation simply fails. Although there is no way to ensure that an atomic memory allocation request never fails, the kernel tries hard to minimize the likelihood of this unfortunate event. In order to do this, the kernel reserves a pool of page frames for atomic memory allocation requests to be used only on low-on-memory conditions. The amount of the reserved memory (in kilobytes) is stored in the min_free_kbytes variable. Its initial value is set during kernel initialization and depends on the amount of physical memory that is directly mapped in the kernel's fourth gigabyte of linear addressesthat is, it depends on the number of page frames included in the ZONE_DMA and ZONE_NORMAL memory zones: However, initially min_free_kbytes cannot be lower than 128 and greater than 65,536.[*] [*] The amount of reserved memory can be changed later by the system administrator either by writing in the /proc/sys/vm/min_free_kbytes file or by issuing a suitable sysctl( ) system call. The ZONE_DMA and ZONE_NORMAL memory zones contribute to the reserved memory with a number of page frames proportional to their relative sizes. For instance, if the ZONE_NORMAL zone is eight times bigger than ZONE_DMA, seven-eighths of the page frames will be taken from ZONE_NORMAL and one-eighth from ZONE_DMA. The pages_min field of the zone descriptor stores the number of reserved page frames inside the zone. As we'll see in Chapter 17, this field plays also a role for the page frame reclaiming algorithm, together with the pages_low and pages_high fields. The pages_low field is always set to 5/4 of the value of pages_min, and pages_high is always set to 3/2 of the value of pages_min. 8.1.5. The Zoned Page Frame Allocator The kernel subsystem that handles the memory allocation requests for groups of contiguous page frames is called the zoned page frame allocator . Its main components are shown in Figure 8-2. The component named "zone allocator " receives the requests for allocation and deallocation of dynamic memory. In the case of allocation requests, the component searches a memory zone that includes a group of contiguous page frames that can satisfy the request (see the later section "The Zone Allocator"). Inside each zone, page frames are handled by a component named "buddy system " (see the later section "The Buddy System Algorithm"). To get better system performance, a small number of page frames are kept in cache to quickly satisfy the allocation requests for single page frames (see the later section "The PerCPU Page Frame Cache"). Figure 8-2. Components of the zoned page frame allocator 8.1.5.1. Requesting and releasing page frames Page frames can be requested by using six slightly different functions and macros. Unless otherwise stated, they return the linear address of the first allocated page or return NULL if the allocation failed. alloc_pages(gfp_mask, order) Macro used to request 2order contiguous page frames. It returns the address of the descriptor of the first allocated page frame or returns NULL if the allocation failed. alloc_page(gfp_mask) Macro used to get a single page frame; it expands to: alloc_pages(gfp_mask, 0) It returns the address of the descriptor of the allocated page frame or returns NULL if the allocation failed. _ _get_free_pages(gfp_mask, order) Function that is similar to alloc_pages( ), but it returns the linear address of the first allocated page. _ _get_free_page(gfp_mask) Macro used to get a single page frame; it expands to: _ _get_free_pages(gfp_mask, 0) get_zeroed_page(gfp_mask) Function used to obtain a page frame filled with zeros; it invokes: alloc_pages(gfp_mask | _ _GFP_ZERO, 0) and returns the linear address of the obtained page frame. _ _get_dma_pages(gfp_mask, order) Macro used to get page frames suitable for DMA; it expands to: _ _get_free_pages(gfp_mask | _ _GFP_DMA, order) The parameter gfp_mask is a group of flags that specify how to look for free page frames. The flags that can be used in gfp_mask are shown in Table 8-5. Table 8-5. Flag used to request page frames Flag _ _GFP_DMA _ _GFP_HIGHMEM _ _GFP_WAIT _ _GFP_HIGH _ _GFP_IO Description The page frame must belong to the ZONE_DMA memory zone. Equivalent to GFP_DMA. The page frame may belong to the ZONE_HIGHMEM memory zone. The kernel is allowed to block the current process waiting for free page frames. The kernel is allowed to access the pool of reserved page frames. The kernel is allowed to perform I/O transfers on low memory pages in order to free page frames. If clear, the kernel is not allowed to perform filesystem-dependent operations. The requested page frames may be "cold" (see the later section "The PerCPU Page Frame Cache"). _ _GFP_FS _ _GFP_COLD _ _GFP_NOWARN A memory allocation failure will not produce a warning message. _ _GFP_REPEAT The kernel keeps retrying the memory allocation until it succeeds. _ _GFP_NOFAIL Same as _ _GFP_REPEAT. _ _GFP_NORETRY _ _GFP_NO_GROW _ _GFP_COMP _ _GFP_ZERO Do not retry a failed memory allocation. The slab allocator does not allow a slab cache to be enlarged (see the later section "The Slab Allocator"). The page frame belongs to an extended page (see the section "Extended Paging" in Chapter 2). The page frame returned, if any, must be filled with zeros. In practice, Linux uses the predefined combinations of flag values shown in Table 8-6; the group name is what you'll encounter as the argument of the six page frame allocation functions. Table 8-6. Groups of flag values used to request page frames Group name GFP_ATOMIC GFP_NOIO GFP_NOFS GFP_KERNEL GFP_USER GFP_HIGHUSER Corresponding flags _ _GFP_HIGH _ _GFP_WAIT _ _GFP_WAIT | _ _GFP_IO _ _GFP_WAIT | _ _GFP_IO | _ _GFP_FS _ _GFP_WAIT | _ _GFP_IO | _ _GFP_FS _ _GFP_WAIT | _ _GFP_IO | _ _GFP_FS | _ _GFP_HIGHMEM The _ _GFP_DMA and _ _GFP_HIGHMEM flags are called zone modifiers ; they specify the zones searched by the kernel while looking for free page frames. The node_zonelists field of the contig_page_data node descriptor is an array of lists of zone descriptors representing the fallback zones: for each setting of the zone modifiers, the corresponding list includes the memory zones that could be used to satisfy the memory allocation request in case the original zone is short on page frames. In the 80 x 86 UMA architecture, the fallback zones are the following: If the _ _GFP_DMA flag is set, page frames can be taken only from the ZONE_DMA memory zone. Otherwise, if the _ _GFP_HIGHMEM flag is not set, page frames can be taken only from the ZONE_NORMAL and the ZONE_DMA memory zones, in order of preference. Otherwise (the _ _GFP_HIGHMEM flag is set), page frames can be taken from ZONE_HIGHMEM, ZONE_NORMAL, and ZONE_DMA memory zones, in order of preference. Page frames can be released through each of the following four functions and macros: _ _free_pages(page, order) This function checks the page descriptor pointed to by page; if the page frame is not reserved (i.e., if the PG_reserved flag is equal to 0), it decreases the count field of the descriptor. If count becomes 0, it assumes that 2order contiguous page frames starting from the one corresponding to page are no longer used. In this case, the function releases the page frames as explained in the later section "The Zone Allocator." free_pages(addr, order) This function is similar to _ _free_pages( ), but it receives as an argument the linear address addr of the first page frame to be released. _ _free_page(page) This macro releases the page frame having the descriptor pointed to by page; it expands to: _ _free_pages(page, 0) free_page(addr) This macro releases the page frame having the linear address addr; it expands to: free_pages(addr, 0) 8.1.6. Kernel Mappings of High-Memory Page Frames The linear address that corresponds to the end of the directly mapped physical memory, and thus to the beginning of the high memory, is stored in the high_memory variable, which is set to 896 MB. Page frames above the 896 MB boundary are not generally mapped in the fourth gigabyte of the kernel linear address spaces, so the kernel is unable to directly access them. This implies that each page allocator function that returns the linear address of the assigned page frame doesn't work for high-memory page frames, that is, for page frames in the ZONE_HIGHMEM memory zone. For instance, suppose that the kernel invoked _ _get_free_pages(GFP_HIGHMEM,0) to allocate a page frame in high memory. If the allocator assigned a page frame in high memory, _ _get_free_pages( ) cannot return its linear address because it doesn't exist; thus, the function returns NULL. In turn, the kernel cannot use the page frame; even worse, the page frame cannot be released because the kernel has lost track of it. This problem does not exist on 64-bit hardware platforms, because the available linear address space is much larger than the amount of RAM that can be installedin short, the ZONE_HIGHMEM zone of these architectures is always empty. On 32-bit platforms such as the 80 x 86 architecture, however, Linux designers had to find some way to allow the kernel to exploit all the available RAM, up to the 64 GB supported by PAE. The approach adopted is the following: The allocation of high-memory page frames is done only through the alloc_pages( ) function and its alloc_page( ) shortcut. These functions do not return the linear address of the first allocated page frame, because if the page frame belongs to the high memory, such linear address simply does not exist. Instead, the functions return the linear address of the page descriptor of the first allocated page frame. These linear addresses always exist, because all page descriptors are allocated in low memory once and forever during the kernel initialization. Page frames in high memory that do not have a linear address cannot be accessed by the kernel. Therefore, part of the last 128 MB of the kernel linear address space is dedicated to mapping high-memory page frames. Of course, this kind of mapping is temporary, otherwise only 128 MB of high memory would be accessible. Instead, by recycling linear addresses the whole high memory can be accessed, although at different times. The kernel uses three different mechanisms to map page frames in high memory; they are called permanent kernel mapping, temporary kernel mapping, and noncontiguous memory allocation. In this section, we'll cover the first two techniques; the third one is discussed in the section "Noncontiguous Memory Area Management" later in this chapter. Establishing a permanent kernel mapping may block the current process; this happens when no free Page Table entries exist that can be used as "windows" on the page frames in high memory. Thus, a permanent kernel mapping cannot be established in interrupt handlers and deferrable functions. Conversely, establishing a temporary kernel mapping never requires blocking the current process; its drawback, however, is that very few temporary kernel mappings can be established at the same time. A kernel control path that uses a temporary kernel mapping must ensure that no other kernel control path is using the same mapping. This implies that the kernel control path can never block, otherwise another kernel control path might use the same window to map some other high memory page. Of course, none of these techniques allow addressing the whole RAM simultaneously. After all, less than 128 MB of linear address space are left for mapping the high memory, while PAE supports systems having up to 64 GB of RAM. 8.1.6.1. Permanent kernel mappings Permanent kernel mappings allow the kernel to establish long-lasting mappings of highmemory page frames into the kernel address space. They use a dedicated Page Table in the master kernel page tables . The pkmap_page_table variable stores the address of this Page Table, while the LAST_PKMAP macro yields the number of entries. As usual, the Page Table includes either 512 or 1,024 entries, according to whether PAE is enabled or disabled (see the section "The Physical Address Extension (PAE) Paging Mechanism" in Chapter 2); thus, the kernel can access at most 2 or 4 MB of high memory at once. The Page Table maps the linear addresses starting from PKMAP_BASE. The pkmap_count array includes LAST_PKMAP counters, one for each entry of the pkmap_page_table Page Table. We distinguish three cases: The counter is 0 The corresponding Page Table entry does not map any high-memory page frame and is usable. The counter is 1 The corresponding Page Table entry does not map any high-memory page frame, but it cannot be used because the corresponding TLB entry has not been flushed since its last usage. The counter is n (greater than 1) The corresponding Page Table entry maps a high-memory page frame, which is used by exactly n - 1 kernel components. To keep track of the association between high memory page frames and linear addresses induced by permanent kernel mappings , the kernel makes use of the page_address_htable hash table. This table contains one page_address_map data structure for each page frame in high memory that is currently mapped. In turn, this data structure contains a pointer to the page descriptor and the linear address assigned to the page frame. The page_address( ) function returns the linear address associated with the page frame, or NULL if the page frame is in high memory and is not mapped. This function, which receives as its parameter a page descriptor pointer page, distinguishes two cases: 1. If the page frame is not in high memory (PG_highmem flag clear), the linear address always exists and is obtained by computing the page frame index, converting it into a physical address, and finally deriving the linear address corresponding to the physical address. This is accomplished by the following code: _ _va((unsigned long)(page mem_map) << 12) 2. If the page frame is in high memory (PG_highmem flag set), the function looks into the page_address_htable hash table. If the page frame is found in the hash table, page_address( ) returns its linear address, otherwise it returns NULL. The kmap( ) function establishes a permanent kernel mapping. It is essentially equivalent to the following code: void * kmap(struct page * page) { if (!PageHighMem(page)) return page_address(page); return kmap_high(page); } The kmap_high( ) function is invoked if the page frame really belongs to high memory. The function is essentially equivalent to the following code: void * kmap_high(struct page * page) { unsigned long vaddr; spin_lock(&kmap_lock); vaddr = (unsigned long) page_address(page); if (!vaddr) vaddr = map_new_virtual(page); pkmap_count[(vaddr-PKMAP_BASE) >> PAGE_SHIFT]++; spin_unlock(&kmap_lock); return (void *) vaddr; } The function gets the kmap_lock spin lock to protect the Page Table against concurrent accesses in multiprocessor systems. Notice that there is no need to disable the interrupts, because kmap( ) cannot be invoked by interrupt handlers and deferrable functions. Next, the kmap_high( ) function checks whether the page frame is already mapped by invoking page_address( ). If not, the function invokes map_new_virtual( ) to insert the page frame physical address into an entry of pkmap_page_table and to add an element to the page_address_htable hash table. Then kmap_high( ) increases the counter corresponding to the linear address of the page frame to take into account the new kernel component that invoked this function. Finally, kmap_high( ) releases the kmap_lock spin lock and returns the linear address that maps the page frame. The map_new_virtual( ) function essentially executes two nested loops: for (;;) { int count; DECLARE_WAITQUEUE(wait, current); for (count = LAST_PKMAP; count > 0; --count) { last_pkmap_nr = (last_pkmap_nr + 1) & (LAST_PKMAP - 1); if (!last_pkmap_nr) { flush_all_zero_pkmaps( ); count = LAST_PKMAP; } if (!pkmap_count[last_pkmap_nr]) { unsigned long vaddr = PKMAP_BASE + (last_pkmap_nr << PAGE_SHIFT); set_pte(&(pkmap_page_table[last_pkmap_nr]), mk_pte(page, _ _pgprot(0x63))); pkmap_count[last_pkmap_nr] = 1; set_page_address(page, (void *) vaddr); return vaddr; } } current->state = TASK_UNINTERRUPTIBLE; add_wait_queue(&pkmap_map_wait, &wait); spin_unlock(&kmap_lock); schedule( ); remove_wait_queue(&pkmap_map_wait, &wait); spin_lock(&kmap_lock); if (page_address(page)) return (unsigned long) page_address(page); } In the inner loop, the function scans all counters in pkmap_count until it finds a null value. The large if block runs when an unused entry is found in pkmap_count. That block determines the linear address corresponding to the entry, creates an entry for it in the pkmap_page_table Page Table, sets the count to 1 because the entry is now used, invokes set_page_address( ) to insert a new element in the page_address_htable hash table, and returns the linear address. The function starts where it left off last time, cycling through the pkmap_count array. It does this by preserving in a variable named last_pkmap_nr the index of the last used entry in the pkmap_page_table Page Table. Thus, the search starts from where it was left in the last invocation of the map_new_virtual( ) function. When the last counter in pkmap_count is reached, the search restarts from the counter at index 0. Before continuing, however, map_new_virtual( ) invokes the flush_all_zero_pkmaps( ) function, which starts another scan of the counters, looking for those that have the value 1. Each counter that has a value of 1 denotes an entry in pkmap_page_table that is free but cannot be used because the corresponding TLB entry has not yet been flushed. flush_all_zero_pkmaps( ) resets their counters to zero, deletes the corresponding elements from the page_address_htable hash table, and issues TLB flushes on all entries of pkmap_page_table. If the inner loop cannot find a null counter in pkmap_count, the map_new_virtual( ) function blocks the current process until some other process releases an entry of the pkmap_page_table Page Table. This is achieved by inserting current in the pkmap_map_wait wait queue, setting the current state to TASK_UNINTERRUPTIBLE, and invoking schedule( ) to relinquish the CPU. Once the process is awakened, the function checks whether another process has mapped the page by invoking page_address( ); if no other process has mapped the page yet, the inner loop is restarted. The kunmap( ) function destroys a permanent kernel mapping established previously by kmap( ). If the page is really in the high memory zone, it invokes the kunmap_high( ) function, which is essentially equivalent to the following code: void kunmap_high(struct page * page) { spin_lock(&kmap_lock); if ((--pkmap_count[((unsigned long)page_address(page) -PKMAP_BASE)>>PAGE_SHIFT]) == 1) if (waitqueue_active(&pkmap_map_wait)) wake_up(&pkmap_map_wait); spin_unlock(&kmap_lock); } The expression within the brackets computes the index into the pkmap_count array from the page's linear address. The counter is decreased and compared to 1. A successful comparison indicates that no process is using the page. The function can finally wake up processes in the wait queue filled by map_new_virtual( ), if any. 8.1.6.2. Temporary kernel mappings Temporary kernel mappings are simpler to implement than permanent kernel mappings; moreover, they can be used inside interrupt handlers and deferrable functions, because requesting a temporary kernel mapping never blocks the current process. Every page frame in high memory can be mapped through a window in the kernel address spacenamely, a Page Table entry that is reserved for this purpose. The number of windows reserved for temporary kernel mappings is quite small. Each CPU has its own set of 13 windows, represented by the enum km_type data structure. Each symbol defined in this data structuresuch as KM_BOUNCE_READ, KM_USER0, or KM_PTE0identifies the linear address of a window. The kernel must ensure that the same window is never used by two kernel control paths at the same time. Thus, each symbol in the km_type structure is dedicated to one kernel component and is named after the component. The last symbol, KM_TYPE_NR, does not represent a linear address by itself, but yields the number of different windows usable by every CPU. Each symbol in km_type, except the last one, is an index of a fix-mapped linear address (see the section "Fix-Mapped Linear Addresses" in Chapter 2). The enum fixed_addresses data structure includes the symbols FIX_KMAP_BEGIN and FIX_KMAP_END; the latter is assigned to the index FIX_KMAP_BEGIN + (KM_TYPE_NR * NR_CPUS) - 1. In this manner, there are KM_TYPE_NR fix-mapped linear addresses for each CPU in the system. Furthermore, the kernel initializes the kmap_pte variable with the address of the Page Table entry corresponding to the fix_to_virt(FIX_KMAP_BEGIN) linear address. To establish a temporary kernel mapping, the kernel invokes the kmap_atomic( ) function, which is essentially equivalent to the following code: void * kmap_atomic(struct page * page, enum km_type type) { enum fixed_addresses idx; unsigned long vaddr; current_thread_info( )->preempt_count++; if (!PageHighMem(page)) return page_address(page); idx = type + KM_TYPE_NR * smp_processor_id( ); vaddr = fix_to_virt(FIX_KMAP_BEGIN + idx); set_pte(kmap_pte-idx, mk_pte(page, 0x063)); _ _flush_tlb_single(vaddr); return (void *) vaddr; } The type argument and the CPU identifier retrieved through smp_processor_id( ) specify what fix-mapped linear address has to be used to map the request page. The function returns the linear address of the page frame if it doesn't belong to high memory; otherwise, it sets up the Page Table entry corresponding to the fix-mapped linear address with the page's physical address and the bits Present, Accessed, Read/Write, and Dirty. Finally, the function flushes the proper TLB entry and returns the linear address. To destroy a temporary kernel mapping, the kernel uses the kunmap_atomic( ) function. In the 80 x 86 architecture, this function decreases the preempt_count of the current process; thus, if the kernel control path was preemptable right before requiring a temporary kernel mapping, it will be preemptable again after it has destroyed the same mapping. Moreover, kunmap_atomic( ) checks whether the TIF_NEED_RESCHED flag of current is set and, if so, invokes schedule( ). 8.1.7. The Buddy System Algorithm The kernel must establish a robust and efficient strategy for allocating groups of contiguous page frames. In doing so, it must deal with a well-known memory management problem called external fragmentation: frequent requests and releases of groups of contiguous page frames of different sizes may lead to a situation in which several small blocks of free page frames are "scattered" inside blocks of allocated page frames. As a result, it may become impossible to allocate a large block of contiguous page frames, even if there are enough free pages to satisfy the request. There are essentially two ways to avoid external fragmentation: Use the paging circuitry to map groups of noncontiguous free page frames into intervals of contiguous linear addresses. Develop a suitable technique to keep track of the existing blocks of free contiguous page frames, avoiding as much as possible the need to split up a large free block to satisfy a request for a smaller one. The second approach is preferred by the kernel for three good reasons: In some cases, contiguous page frames are really necessary, because contiguous linear addresses are not sufficient to satisfy the request. A typical example is a memory request for buffers to be assigned to a DMA processor (see Chapter 13). Because most DMAs ignore the paging circuitry and access the address bus directly while transferring several disk sectors in a single I/O operation, the buffers requested must be located in contiguous page frames. Even if contiguous page frame allocation is not strictly necessary, it offers the big advantage of leaving the kernel paging tables unchanged. What's wrong with modifying the Page Tables? As we know from Chapter 2, frequent Page Table modifications lead to higher average memory access times, because they make the CPU flush the contents of the translation lookaside buffers. Large chunks of contiguous physical memory can be accessed by the kernel through 4 MB pages. This reduces the translation lookaside buffers misses, thus significantly speeding up the average memory access time (see the section "Translation Lookaside Buffers (TLB)" in Chapter 2). The technique adopted by Linux to solve the external fragmentation problem is based on the well-known buddy system algorithm. All free page frames are grouped into 11 lists of blocks that contain groups of 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, and 1024 contiguous page frames, respectively. The largest request of 1024 page frames corresponds to a chunk of 4 MB of contiguous RAM. The physical address of the first page frame of a block is a multiple of the group sizefor example, the initial address of a 16-page-frame block is a multiple of 16 x 212 (212 = 4,096, which is the regular page size). We'll show how the algorithm works through a simple example: Assume there is a request for a group of 256 contiguous page frames (i.e., one megabyte). The algorithm checks first to see whether a free block in the 256-page-frame list exists. If there is no such block, the algorithm looks for the next larger blocka free block in the 512page-frame list. If such a block exists, the kernel allocates 256 of the 512 page frames to satisfy the request and inserts the remaining 256 page frames into the list of free 256page-frame blocks. If there is no free 512-page block, the kernel then looks for the next larger block (i.e., a free 1024-page-frame block). If such a block exists, it allocates 256 of the 1024 page frames to satisfy the request, inserts the first 512 of the remaining 768 page frames into the list of free 512-page-frame blocks, and inserts the last 256 page frames into the list of free 256-page-frame blocks. If the list of 1024-page-frame blocks is empty, the algorithm gives up and signals an error condition. The reverse operation, releasing blocks of page frames, gives rise to the name of this algorithm. The kernel attempts to merge pairs of free buddy blocks of size b together into a single block of size 2b. Two blocks are considered buddies if: Both blocks have the same size, say b. They are located in contiguous physical addresses. The physical address of the first page frame of the first block is a multiple of 2 x b x 212. The algorithm is iterative; if it succeeds in merging released blocks, it doubles b and tries again so as to create even bigger blocks. 8.1.7.1. Data structures Linux 2.6 uses a different buddy system for each zone. Thus, in the 80 x 86 architecture, there are 3 buddy systems: the first handles the page frames suitable for ISA DMA, the second handles the "normal" page frames, and the third handles the high-memory page frames. Each buddy system relies on the following main data structures : The mem_map array introduced previously. Actually, each zone is concerned with a subset of the mem_map elements. The first element in the subset and its number of elements are specified, respectively, by the zone_mem_map and size fields of the zone descriptor. An array consisting of eleven elements of type free_area, one element for each group size. The array is stored in the free_area field of the zone descriptor. Let us consider the kth element of the free_area array in the zone descriptor, which identifies all the free blocks of size 2k. The free_list field of this element is the head of a doubly linked circular list that collects the page descriptors associated with the free blocks of 2k pages. More precisely, this list includes the page descriptors of the starting page frame of every block of 2k free page frames; the pointers to the adjacent elements in the list are stored in the lru field of the page descriptor.[*] [*] As we'll see later, the lru field of the page descriptor can be used with other meanings when the page is not free. Besides the head of the list, the kth element of the free_area array includes also the field nr_free, which specifies the number of free blocks of size 2k pages. Of course, if there are no blocks of 2k free page frames, nr_free is equal to 0 and the free_list list is empty (both pointers of free_list point to the free_list field itself). Finally, the private field of the descriptor of the first page in a block of 2k free pages stores the order of the block, that is, the number k. Thanks to this field, when a block of pages is freed, the kernel can determine whether the buddy of the block is also free and, if so, it can coalesce the two blocks in a single block of 2k+1 pages. It should be noted that up to Linux 2.6.10, the kernel used 10 arrays of flags to encode this information. 8.1.7.2. Allocating a block The _ _rmqueue( ) function is used to find a free block in a zone. The function takes two arguments: the address of the zone descriptor, and order, which denotes the logarithm of the size of the requested block of free pages (0 for a one-page block, 1 for a two-page block, and so forth). If the page frames are successfully allocated, the _ _rmqueue( ) function returns the address of the page descriptor of the first allocated page frame. Otherwise, the function returns NULL. The _ _rmqueue( ) function assumes that the caller has already disabled local interrupts and acquired the zone->lock spin lock, which protects the data structures of the buddy system. It performs a cyclic search through each list for an available block (denoted by an entry that doesn't point to the entry itself), starting with the list for the requested order and continuing if necessary to larger orders: struct free_area *area; unsigned int current_order; for (current_order=order; current_order<11; ++current_order) { area = zone->free_area + current_order; if (!list_empty(&area->free_list)) goto block_found; } return NULL; If the loop terminates, no suitable free block has been found, so _ _rmqueue( ) returns a NULL value. Otherwise, a suitable free block has been found; in this case, the descriptor of its first page frame is removed from the list and the value of free_ pages in the zone descriptor is decreased: block_found: page = list_entry(area->free_list.next, struct page, lru); list_del(&page->lru); ClearPagePrivate(page); page->private = 0; area->nr_free--; zone->free_pages -= 1UL << order; If the block found comes from a list of size curr_order greater than the requested size order, a while cycle is executed. The rationale behind these lines of codes is as follows: when it becomes necessary to use a block of 2k page frames to satisfy a request for 2h page frames (h < k), the program allocates the first 2h page frames and iteratively reassigns the last 2k 2h page frames to the free_area lists that have indexes between h and k: size = 1 << curr_order; while (curr_order > order) { area--; curr_order--; size >>= 1; buddy = page + size; /* insert buddy as first element in the list */ list_add(&buddy->lru, &area->free_list); area->nr_free++; buddy->private = curr_order; SetPagePrivate(buddy); } return page; Because the _ _rmqueue( ) function has found a suitable free block, it returns the address page of the page descriptor associated with the first allocated page frame. 8.1.7.3. Freeing a block The _ _free_pages_bulk( ) function implements the buddy system strategy for freeing page frames. It uses three basic input parameters:[*] For performance reasons, this inline function also uses another parameter; its value, however, can be determined by the three basic parameters shown in the text. [*] page The address of the descriptor of the first page frame included in the block to be released zone The address of the zone descriptor order The logarithmic size of the block The function assumes that the caller has already disabled local interrupts and acquired the zone->lock spin lock, which protects the data structure of the buddy system. _ _free_pages_bulk( ) starts by declaring and initializing a few local variables: struct page * base = zone->zone_mem_map; unsigned long buddy_idx, page_idx = page - base; struct page * buddy, * coalesced; int order_size = 1 << order; The page_idx local variable contains the index of the first page frame in the block with respect to the first page frame of the zone. The order_size local variable is used to increase the counter of free page frames in the zone: zone->free_pages += order_size; The function now performs a cycle executed at most 10- order times, once for each possibility for merging a block with its buddy. The function starts with the smallest-sized block and moves up to the top size: while (order < 10) { buddy_idx = page_idx ^ (1 << order); buddy = base + buddy_idx; if (!page_is_buddy(buddy, order)) break; list_del(&buddy->lru); zone->free_area[order].nr_free--; ClearPagePrivate(buddy); buddy->private = 0; page_idx &= buddy_idx; order++; } In the body of the loop, the function looks for the index buddy_idx of the block, which is buddy to the one having the page descriptor index page_idx. It turns out that this index can be easily computed as: buddy_idx = page_idx ^ (1 << order); In fact, an Exclusive OR (XOR) using the (1<<order) mask switches the value of the order-th bit of page_idx. Therefore, if the bit was previously zero, buddy_idx is equal to page_idx+ order_size; conversely, if the bit was previously one, buddy_idx is equal to page_idx order_size. Once the buddy block index is known, the page descriptor of the buddy block can be easily obtained as: buddy = base + buddy_idx; Now the function invokes page_is_buddy() to check if buddy describes the first page of a block of order_size free page frames. int page_is_buddy(struct page *page, int order) { if (PagePrivate(buddy) && page->private == order && !PageReserved(buddy) && page_count(page) ==0) return 1; return 0; } As you see, the buddy's first page must be free ( _count field equal to -1), it must belong to the dynamic memory (PG_reserved bit clear), its private field must be meaningful (PG_private bit set), and finally the private field must store the order of the block being freed. If all these conditions are met, the buddy block is free and the function removes the buddy block from the list of free blocks of order order, and performs one more iteration looking for buddy blocks twice as big. If at least one of the conditions in page_is_buddy( ) is not met, the function breaks out of the cycle, because the free block obtained cannot be merged further with other free blocks. The function inserts it in the proper list and updates the private field of the first page frame with the order of the block size: coalesced = base + page_idx; coalesced->private = order; SetPagePrivate(coalesced); list_add(&coalesced->lru, &zone->free_area[order].free_list); zone->free_area[order].nr_free++; 8.1.8. The Per-CPU Page Frame Cache As we will see later in this chapter, the kernel often requests and releases single page frames. To boost system performance, each memory zone defines a per-CPU page frame cache. Each per-CPU cache includes some pre-allocated page frames to be used for single memory requests issued by the local CPU. Actually, there are two caches for each memory zone and for each CPU: a hot cache , which stores page frames whose contents are likely to be included in the CPU's hardware cache, and a cold cache . Taking a page frame from the hot cache is beneficial for system performance if either the kernel or a User Mode process will write into the page frame right after the allocation. In fact, every access to a memory cell of the page frame will result in a line of the hardware cache being "stolen" from another page frameunless, of course, the hardware cache already includes a line that maps the cell of the "hot" page frame just accessed. Conversely, taking a page frame from the cold cache is convenient if the page frame is going to be filled with a DMA operation. In this case, the CPU is not involved and no line of the hardware cache will be modified. Taking the page frame from the cold cache preserves the reserve of hot page frames for the other kinds of memory allocation requests. The main data structure implementing the per-CPU page frame cache is an array of per_cpu_pageset data structures stored in the pageset field of the memory zone descriptor. The array includes one element for each CPU; this element, in turn, consists of two per_cpu_pages descriptors, one for the hot cache and the other for the cold cache. The fields of the per_cpu_pages descriptor are listed in Table 8-7. Table 8-7. The fields of the per_cpu_pages descriptor Type int int int int Name Description count low high batch Number of pages frame in the cache Low watermark for cache replenishing High watermark for cache depletion Number of page frames to be added or subtracted from the cache List of descriptors of the page frames included in the cache struct list_head list The kernel monitors the size of the both the hot and cold caches by using two watermarks: if the number of page frames falls below the low watermark, the kernel replenishes the proper cache by allocating batch single page frames from the buddy system; otherwise, if the number of page frames rises above the high watermark, the kernel releases to the buddy system batch page frames in the cache. The values of batch, low, and high essentially depend on the number of page frames included in the memory zone. 8.1.8.1. Allocating page frames through the per-CPU page frame caches The buffered_rmqueue( ) function allocates page frames in a given memory zone. It makes use of the per-CPU page frame caches to handle single page frame requests. The parameters are the address of the memory zone descriptor, the order of the memory allocation request order, and the allocation flags gfp_flags. If the _ _GFP_COLD flag is set in gfp_flags, the page frame should be taken from the cold cache, otherwise it should be taken from the hot cache (this flag is meaningful only for single page frame requests). The function essentially executes the following operations: 1. If order is not equal to 0, the per-CPU page frame cache cannot be used: the function jumps to step 4. 2. Checks whether the memory zone's local per-CPU cache identified by the value of the _ _GFP_COLD flag has to be replenished (the count field of the per_cpu_pages 3. 4. 5. 6. descriptor is lower than or equal to the low field). In this case, it executes the following substeps: a. Allocates batch single page frames from the buddy system by repeatedly invoking the _ _rmqueue( ) function. b. Inserts the descriptors of the allocated page frames in the cache's list. c. Updates the value of count by adding the number of page frames actually allocated. If count is positive, the function gets a page frame from the cache's list, decreases count, and jumps to step 5. (Observe that a per-CPU page frame cache could be empty; this happens when the _ _rmqueue( ) function invoked in step 2a fails to allocate any page frames.) Here, the memory request has not yet been satisfied, either because the request spans several contiguous page frames, or because the selected page frame cache is empty. Invokes the _ _rmqueue( ) function to allocate the requested page frames from the buddy system. If the memory request has been satisfied, the function initializes the page descriptor of the (first) page frame: clears some flags, sets the private field to zero, and sets the page frame reference counter to one. Moreover, if the _ _GPF_ZERO flag in gfp_flags is set, it fills the allocated memory area with zeros. Returns the page descriptor address of the (first) page frame, or NULL if the memory allocation request failed. 8.1.8.2. Releasing page frames to the per-CPU page frame caches In order to release a single page frame to a per-CPU page frame cache, the kernel makes use of the free_hot_page( ) and free_cold_page( ) functions. Both of them are simple wrappers for the free_hot_cold_page( ) function, which receives as its parameters the descriptor address page of the page frame to be released and a cold flag specifying either the hot cache or the cold cache. The free_hot_cold_page( ) function executes the following operations: 1. Gets from the page->flags field the address of the memory zone descriptor including the page frame (see the earlier section "Non-Uniform Memory Access (NUMA)"). 2. Gets the address of the per_cpu_pages descriptor of the zone's cache selected by the cold flag. 3. Checks whether the cache should be depleted: if count is higher than or equal to high, invokes the free_pages_bulk( ) function, passing to it the zone descriptor, the number of page frames to be released (batch field), the address of the cache's list, and the number zero (for 0-order page frames). In turn, the latter function invokes repeatedly the _ _free_pages_bulk( ) function to releases the specified number of page framestaken from the cache's listto the buddy system of the memory zone. 4. Adds the page frame to be released to the cache's list, and increases the count field. It should be noted that in the current version of the Linux 2.6 kernel, no page frame is ever released to the cold cache: the kernel always assumes the freed page frame is hot with respect to the hardware cache. Of course, this does not mean that the cold cache is empty: the cache is replenished by buffered_rmqueue( ) when the low watermark has been reached. 8.1.9. The Zone Allocator The zone allocator is the frontend of the kernel page frame allocator. This component must locate a memory zone that includes a number of free page frames large enough to satisfy the memory request. This task is not as simple as it could appear at a first glance, because the zone allocator must satisfy several goals: It should protect the pool of reserved page frames (see the earlier section "The Pool of Reserved Page Frames"). It should trigger the page frame reclaiming algorithm (see Chapter 17) when memory is scarce and blocking the current process is allowed; once some page frames have been freed, the zone allocator will retry the allocation. It should preserve the small, precious ZONE_DMA memory zone, if possible. For instance, the zone allocator should be somewhat reluctant to assign page frames in the ZONE_DMA memory zone if the request was for ZONE_NORMAL or ZONE_HIGHMEM page frames. We have seen in the earlier section "The Zoned Page Frame Allocator" that every request for a group of contiguous page frames is eventually handled by executing the alloc_pages macro. This macro, in turn, ends up invoking the _ _alloc_pages( ) function, which is the core of the zone allocator. It receives three parameters: gfp_mask The flags specified in the memory allocation request (see earlier Table 8-5) order The logarithmic size of the group of contiguous page frames to be allocated zonelist Pointer to a zonelist data structure describing, in order of preference, the memory zones suitable for the memory allocation The _ _alloc_pages( ) function scans every memory zone included in the zonelist data structure. The code that does this looks like the following: for (i = 0; (z=zonelist->zones[i]) != NULL; i++) { if (zone_watermark_ok(z, order, ...)) { page = buffered_rmqueue(z, order, gfp_mask); if (page) return page; } } For each memory zone, the function compares the number of free page frames with a threshold value that depends on the memory allocation flags, on the type of current process, and on how many times the zone has already been checked by the function. In fact, if free memory is scarce, every memory zone is typically scanned several times, each time with lower threshold on the minimal amount of free memory required for the allocation. The previous block of code is thus replicated several timeswith minor variationsin the body of the _ _alloc_pages( ) function. The buffered_rmqueue( ) function has been described already in the earlier section "The Per-CPU Page Frame Cache:" it returns the page descriptor of the first allocated page frame, or NULL if the memory zone does not include a group of contiguous page frames of the requested size. The zone_watermark_ok( ) auxiliary function receives several parameters, which determine a threshold min on the number of free page frames in the memory zone. In particular, the function returns the value 1 if the following two conditions are met: 1. Besides the page frames to be allocated, there are at least min free page frames in the memory zone, not including the page frames in the low-on-memory reserve (lowmem_reserve field of the zone descriptor). free page frames 2. Besides the page frames to be allocated, there are at least in blocks of order at least k, for each k between 1 and the order of the allocation. Therefore, if order is greater than zero, there must be at least min/2 free page frames in blocks of size at least 2; if order is greater than one, there must be at least min/4 free page frames in blocks of size at least 4; and so on. The value of the threshold min is determined by zone_watermark_ok( ) as follows: The base value is passed as a parameter of the function and can be one of the pages_min, pages_low, and pages_high zone's watermarks (see the section "The Pool of Reserved Page Frames" earlier in this chapter). The base value is divided by two if the gfp_high flag passed as parameter is set. Usually, this flag is equal to one if the _ _GFP_HIGHMEM flag is set in the gfp_mask, that is, if the page frames can be allocated from high memory. The threshold value is further reduced by one-fourth if the can_try_harder flag passed as parameter is set. This flag is usually equal to one if either the _ _GFP_WAIT flag is set in gfp_mask, or if the current process is a real-time process and the memory allocation is done in process context (outside of interrupt handlers and deferrable functions). The _ _alloc_pages( ) function essentially executes the following steps: 1. Performs a first scanning of the memory zones (see the block of code shown earlier). In this first scan, the min threshold value is set to z->pages_low, where z points to the zone descriptor being analyzed (the can_try_harder and gfp_high parameters are set to zero). 2. If the function did not terminate in the previous step, there is not much free memory left: the function awakens the kswapd kernel threads to start reclaiming page frames asynchronously (see Chapter 17). 3. Performs a second scanning of the memory zones, passing as base threshold the value z->pages_min. As explained previously, the actual threshold is determined also by the can_try_harder and gfp_high flags. This step is nearly identical to step 1, except that the function is using a lower threshold. 4. If the function did not terminate in the previous step, the system is definitely low on memory. If the kernel control path that issued the memory allocation request is not an interrupt handler or a deferrable function and it is trying to reclaim page frames (either the PF_MEMALLOC flag or the PF_MEMDIE flag of current is set), the function then performs a third scanning of the memory zones, trying to allocate the page frames ignoring the low-on-memory thresholdsthat is, without invoking zone_watermark_ok( ). This is the only case where the kernel control path is allowed to deplete the low-on-memory reserve of pages specified by the lowmem_reserve field of the zone descriptor. In fact, in this case the kernel control path that issued the memory request is ultimately trying to free page frames, thus it should get what it has requested, if at all possible. If no memory zone includes enough page frames, the function returns NULL to notify the caller of the failure. 5. Here, the invoking kernel control path is not trying to reclaim memory. If the _ _GFP_WAIT flag of gfp_mask is not set, the function returns NULL to notify the kernel control path of the memory allocation failure: in this case, there is no way to satisfy the request without blocking the current process. 6. Here the current process can be blocked: invokes cond_resched() to check whether some other process needs the CPU. 7. Sets the PF_MEMALLOC flag of current, to denote the fact that the process is ready to perform memory reclaiming. 8. Stores in current->reclaim_state a pointer to a reclaim_state structure. This structure includes just one field, reclaimed_slab, initialized to zero (we'll see how this field is used in the section "Interfacing the Slab Allocator with the Zoned Page Frame Allocator" later in this chapter). 9. Invokes TRy_to_free_pages( ) to look for some page frames to be reclaimed (see the section "Low On Memory Reclaiming" in Chapter 17). The latter function may block the current process. Once that function returns, _ _alloc_pages( ) resets the PF_MEMALLOC flag of current and invokes once more cond_resched(). 10. If the previous step has freed some page frames, the function performs yet another scanning of the memory zones equal to the one performed in step 3. If the memory allocation request cannot be satisfied, the function determines whether it should continue scanning the memory zone: if the _ _GFP_NORETRY flag is clear and either the memory allocation request spans up to eight page frames, or one of the _ _GFP_REPEAT and _ _GFP_NOFAIL flags is set, the function invokes blk_congestion_wait( ) to put the process asleep for awhile (see Chapter 14), and it jumps back to step 6. Otherwise, the function returns NULL to notify the caller that the memory allocation failed. 11. If no page frame has been freed in step 9, the kernel is in deep trouble, because free memory is dangerously low and it was not possible to reclaim any page frame. Perhaps the time has come to take a crucial decision. If the kernel control path is allowed to perform the filesystem-dependent operations needed to kill a process (the _ _GFP_FS flag in gfp_mask is set) and the _ _GFP_NORETRY flag is clear, performs the following substeps: a. Scans once again the memory zones with a threshold value equal to z>pages_high. b. Invokes out_of_memory() to start freeing some memory by killing a victim process (see "The Out of Memory Killer" in Chapter 17). c. Jumps back to step 1. Because the watermark used in step 11a is much higher than the watermarks used in the previous scannings, that step is likely to fail. Actually, step 11a succeeds only if another kernel control path is already killing a process to reclaim its memory. Thus, step 11a avoids that two innocent processes are killed instead of one. 8.1.9.1. Releasing a group of page frames The zone allocator also takes care of releasing page frames; thankfully, releasing memory is a lot easier than allocating it. All kernel macros and functions that release page framesdescribed in the earlier section "The Zoned Page Frame Allocator"rely on the _ _free_pages( ) function. It receives as its parameters the address of the page descriptor of the first page frame to be released (page), and the logarithmic size of the group of contiguous page frames to be released (order). The function executes the following steps: 1. Checks that the first page frame really belongs to dynamic memory (its PG_reserved flag is cleared); if not, terminates. 2. Decreases the page->_count usage counter; if it is still greater than or equal to zero, terminates. 3. If order is equal to zero, the function invokes free_hot_page( ) to release the page frame to the per-CPU hot cache of the proper memory zone (see the earlier section "The Per-CPU Page Frame Cache"). 4. If order is greater than zero, it adds the page frames in a local list and invokes the free_pages_bulk( ) function to release them to the buddy system of the proper memory zone (see step 3 in the description of free_hot_cold_page( ) in the earlier section "The Per-CPU Page Frame Cache"). 8.2. Memory Area Management This section deals with memory areas that is, with sequences of memory cells having contiguous physical addresses and an arbitrary length. The buddy system algorithm adopts the page frame as the basic memory area. This is fine for dealing with relatively large memory requests, but how are we going to deal with requests for small memory areas, say a few tens or hundreds of bytes? Clearly, it would be quite wasteful to allocate a full page frame to store a few bytes. A better approach instead consists of introducing new data structures that describe how small memory areas are allocated within the same page frame. In doing so, we introduce a new problem called internal fragmentation. It is caused by a mismatch between the size of the memory request and the size of the memory area allocated to satisfy the request. A classical solution (adopted by early Linux versions) consists of providing memory areas whose sizes are geometrically distributed; in other words, the size depends on a power of 2 rather than on the size of the data to be stored. In this way, no matter what the memory request size is, we can ensure that the internal fragmentation is always smaller than 50 percent. Following this approach, the kernel creates 13 geometrically distributed lists of free memory areas whose sizes range from 32 to 131, 072 bytes. The buddy system is invoked both to obtain additional page frames needed to store new memory areas and, conversely, to release page frames that no longer contain memory areas. A dynamic list is used to keep track of the free memory areas contained in each page frame. 8.2.1. The Slab Allocator Running a memory area allocation algorithm on top of the buddy algorithm is not particularly efficient. A better algorithm is derived from the slab allocator schema that was adopted for the first time in the Sun Microsystems Solaris 2.4 operating system. It is based on the following premises: The type of data to be stored may affect how memory areas are allocated; for instance, when allocating a page frame to a User Mode process, the kernel invokes the get_zeroed_page( ) function, which fills the page with zeros. The concept of a slab allocator expands upon this idea and views the memory areas as objects consisting of both a set of data structures and a couple of functions or methods called the constructor and destructor. The former initializes the memory area while the latter deinitializes it. To avoid initializing objects repeatedly, the slab allocator does not discard the objects that have been allocated and then released but instead saves them in memory. When a new object is then requested, it can be taken from memory without having to be reinitialized. The kernel functions tend to request memory areas of the same type repeatedly. For instance, whenever the kernel creates a new process, it allocates memory areas for some fixed size tables such as the process descriptor, the open file object, and so on (see Chapter 3). When a process terminates, the memory areas used to contain these tables can be reused. Because processes are created and destroyed quite frequently, without the slab allocator, the kernel wastes time allocating and deallocating the page frames containing the same memory areas repeatedly; the slab allocator allows them to be saved in a cache and reused quickly. Requests for memory areas can be classified according to their frequency. Requests of a particular size that are expected to occur frequently can be handled most efficiently by creating a set of special-purpose objects that have the right size, thus avoiding internal fragmentation. Meanwhile, sizes that are rarely encountered can be handled through an allocation scheme based on objects in a series of geometrically distributed sizes (such as the power-of-2 sizes used in early Linux versions), even if this approach leads to internal fragmentation. There is another subtle bonus in introducing objects whose sizes are not geometrically distributed: the initial addresses of the data structures are less prone to be concentrated on physical addresses whose values are a power of 2. This, in turn, leads to better performance by the processor hardware cache. Hardware cache performance creates an additional reason for limiting calls to the buddy system allocator as much as possible. Every call to a buddy system function "dirties" the hardware cache, thus increasing the average memory access time. The impact of a kernel function on the hardware cache is called the function footprint; it is defined as the percentage of cache overwritten by the function when it terminates. Clearly, large footprints lead to a slower execution of the code executed right after the kernel function, because the hardware cache is by now filled with useless information. The slab allocator groups objects into caches . Each cache is a "store" of objects of the same type. For instance, when a file is opened, the memory area needed to store the corresponding "open file" object is taken from a slab allocator cache named filp (for "file pointer"). The area of main memory that contains a cache is divided into slabs ; each slab consists of one or more contiguous page frames that contain both allocated and free objects (see Figure 8-3). Figure 8-3. The slab allocator components As we'll see in Chapter 17, the kernel periodically scans the caches and releases the page frames corresponding to empty slabs. 8.2.2. Cache Descriptor Each cache is described by a structure of type kmem_cache_t (which is equivalent to the type struct kmem_cache_s), whose fields are listed in Table 8-8. We omitted from the table several fields used for collecting statistical information and for debugging. Table 8-8. The fields of the kmem_cache_t descriptor Type struct array_cache * unsigned int array Name Description Per-CPU array of pointers to local caches of free objects (see the section "Local Caches of Free Slab Objects" later in this chapter). Number of objects to be transferred in bulk to or from the local caches. Maximum number of free objects in the local caches. This is tunable. See next table. batchcount unsigned int struct kmem_list3 limit lists Table 8-8. The fields of the kmem_cache_t descriptor Type unsigned int unsigned int Name objsize Description Size of the objects included in the cache. Set of flags that describes permanent properties of the cache. Number of objects packed into a single slab. (All slabs of the cache have the same size.) Upper limit of free objects in the whole slab cache. Cache spin lock. Logarithm of the number of contiguous page frames included in a single slab. Set of flags passed to the buddy system function when allocating page frames. Number of colors for the slabs (see the section "Slab Coloring" later in this chapter). Basic alignment offset in the slabs. flags num free_limit spinlock gfporder gfpflags colour colour_off unsigned int unsigned int spinlock_t unsigned int unsigned int size_t unsigned int unsigned int colour_next Color to use for the next allocated slab. Pointer to the general slab cache containing the slab slabp_cache descriptors (NULL if internal slab descriptors are used; see next section). slab_size dflags ctor dtor name next The size of a single slab. Set of flags that describe dynamic properties of the cache. Pointer to constructor method associated with the cache. Pointer to destructor method associated with the cache. Character array storing the name of the cache. Pointers for the doubly linked list of cache descriptors. kmem_cache_t * unsigned int unsigned int void * void * const char * struct list_head The lists field of the kmem_cache_t descriptor, in turn, is a structure whose fields are listed in Table 8-9. Table 8-9. The fields of the kmem_list3 structure Type struct list_head struct list_head Name slabs_partial slabs_full Description Doubly linked circular list of slab descriptors with both free and nonfree objects Doubly linked circular list of slab descriptors with no free objects Table 8-9. The fields of the kmem_list3 structure Type struct list_head unsigned long int unsigned long struct array_cache * shared Name slabs_free Description Doubly linked circular list of slab descriptors with free objects only free_objects Number of free objects in the cache free_touched Used by the slab allocator's page reclaiming algorithm (see Chapter 17) Used by the slab allocator's page reclaiming algorithm (see Chapter 17) Pointer to a local cache shared by all CPUs (see the later section "Local Caches of Free Slab Objects") next_reap 8.2.3. Slab Descriptor Each slab of a cache has its own descriptor of type slab illustrated in Table 8-10. Table 8-10. The fields of the slab descriptor Type struct list_head unsigned long void * unsigned int unsigned int Name list Description Pointers for one of the three doubly linked list of slab descriptors (either the slabs_full, slabs_partial, or slabs_free list in the kmem_list3 structure of the cache descriptor) later in this chapter) colouroff Offset of the first object in the slab (see the section "Slab Coloring" s_mem inuse Address of first object (either allocated or free) in the slab Number of objects in the slab that are currently used (not free) Index of next free object in the slab, or BUFCTL_END if there are no free objects left (see the section "Object Descriptor" later in this chapter) free Slab descriptors can be stored in two possible places: External slab descriptor Stored outside the slab, in one of the general caches not suitable for ISA DMA pointed to by cache_sizes (see the next section). Internal slab descriptor Stored inside the slab, at the beginning of the first page frame assigned to the slab. The slab allocator chooses the second solution when the size of the objects is smaller than 512MB or when internal fragmentation leaves enough space for the slab descriptor and the object descriptors (as described later)inside the slab. The CFLGS_OFF_SLAB flag in the flags field of the cache descriptor is set to one if the slab descriptor is stored outside the slab; it is set to zero otherwise. Figure 8-4 illustrates the major relationships between cache and slab descriptors. Full slabs, partially full slabs, and free slabs are linked in different lists. 8.2.4. General and Specific Caches Caches are divided into two types: general and specific. General caches are used only by the slab allocator for its own purposes, while specific caches are used by the remaining parts of the kernel. Figure 8-4. Relationship between cache and slab descriptors The general caches are: A first cache called kmem_cache whose objects are the cache descriptors of the remaining caches used by the kernel. The cache_cache variable contains the descriptor of this special cache. Several additional caches contain general purpose memory areas. The range of the memory area sizes typically includes 13 geometrically distributed sizes. A table called malloc_sizes (whose elements are of type cache_sizes) points to 26 cache descriptors associated with memory areas of size 32, 64, 128, 256, 512, 1,024, 2,048, 4,096, 8,192, 16,384, 32,768, 65,536, and 131,072 bytes. For each size, there are two caches: one suitable for ISA DMA allocations and the other for normal allocations. The kmem_cache_init( ) function is invoked during system initialization to set up the general caches. Specific caches are created by the kmem_cache_create( ) function. Depending on the parameters, the function first determines the best way to handle the new cache (for instance, whether to include the slab descriptor inside or outside of the slab). It then allocates a cache descriptor for the new cache from the cache_cache general cache and inserts the descriptor in the cache_chain list of cache descriptors (the insertion is done after having acquired the cache_chain_sem semaphore that protects the list from concurrent accesses). It is also possible to destroy a cache and remove it from the cache_chain list by invoking kmem_cache_destroy( ). This function is mostly useful to modules that create their own caches when loaded and destroy them when unloaded. To avoid wasting memory space, the kernel must destroy all slabs before destroying the cache itself. The kmem_cache_shrink( ) function destroys all the slabs in a cache by invoking slab_destroy( ) iteratively (see the later section "Releasing a Slab from a Cache"). The names of all general and specific caches can be obtained at runtime by reading /proc/slabinfo; this file also specifies the number of free objects and the number of allocated objects in each cache. 8.2.5. Interfacing the Slab Allocator with the Zoned Page Frame Allocator When the slab allocator creates a new slab, it relies on the zoned page frame allocator to obtain a group of free contiguous page frames. For this purpose, it invokes the kmem_getpages( ) function, which is essentially equivalent, on a UMA system, to the following code fragment: void * kmem_getpages(kmem_cache_t *cachep, int flags) { struct page *page; int i; flags |= cachep->gfpflags; page = alloc_pages(flags, cachep->gfporder); if (!page) return NULL; i = (1 << cache->gfporder); if (cachep->flags & SLAB_RECLAIM_ACCOUNT) atomic_add(i, &slab_reclaim_pages); while (i--) SetPageSlab(page++); return page_address(page); } The two parameters have the following meaning: cachep Points to the cache descriptor of the cache that needs additional page frames (the number of required page frames is determined by the order in the cachep->gfporder field). flags Specifies how the page frame is requested (see the section "The Zoned Page Frame Allocator" earlier in this chapter). This set of flags is combined with the specific cache allocation flags stored in the gfpflags field of the cache descriptor. The size of the memory allocation request is specified by the gfporder field of the cache descriptor, which encodes the size of a slab in the cache.[*] If the slab cache has been created with the SLAB_RECLAIM_ACCOUNT flag set, the page frames assigned to the slabs are accounted for as reclaimable pages when the kernel checks whether there is enough memory to satisfy some User Mode requests. The function also sets the PG_slab flag in the page descriptors of the allocated page frames. [*] Notice that it is not possible to allocate page frames from the ZONE_HIGHMEM memory zone, because the kmem_getpages( ) function returns the linear address yielded by the page_address( ) function; as explained in the section "Kernel Mappings of High-Memory Page Frames" earlier in this chapter, this function returns NULL for unmapped high-memory page frames. In the reverse operation, page frames assigned to a slab can be released (see the section "Releasing a Slab from a Cache" later in this chapter) by invoking the kmem_freepages( ) function: void kmem_freepages(kmem_cache_t *cachep, void *addr) { unsigned long i = (1<<cachep->gfporder); struct page *page = virt_to_page(addr); if (current->reclaim_state) current->reclaim_state->reclaimed_slab += i; while (i--) ClearPageSlab(page++); free_pages((unsigned long) addr, cachep->gfporder); if (cachep->flags & SLAB_RECLAIM_ACCOUNT) atomic_sub(1<<cachep->gfporder, &slab_reclaim_pages); } The function releases the page frames, starting from the one having the linear address addr, that had been allocated to the slab of the cache identified by cachep. If the current process is performing memory reclaiming (current->reclaim_state field not NULL), the reclaimed_slab field of the reclaim_state structure is properly increased, so that the pages just freed can be accounted for by the page frame reclaiming algorithm (see the section "Low On Memory Reclaiming" in Chapter 17). Moreover, if the SLAB_RECLAIM_ACCOUNT flag is set (see above), the slab_reclaim_pages variable is properly decreased. 8.2.6. Allocating a Slab to a Cache A newly created cache does not contain a slab and therefore does not contain any free objects. New slabs are assigned to a cache only when both of the following are true: A request has been issued to allocate a new object. The cache does not include a free object. Under these circumstances, the slab allocator assigns a new slab to the cache by invoking cache_grow( ). This function calls kmem_ getpages( ) to obtain from the zoned page frame allocator the group of page frames needed to store a single slab; it then calls alloc_slabmgmt( ) to get a new slab descriptor. If the CFLGS_OFF_SLAB flag of the cache descriptor is set, the slab descriptor is allocated from the general cache pointed to by the slabp_cache field of the cache descriptor; otherwise, the slab descriptor is allocated in the first page frame of the slab. The kernel must be able to determine, given a page frame, whether it is used by the slab allocator and, if so, to derive quickly the addresses of the corresponding cache and slab descriptors. Therefore, cache_ grow( ) scans all page descriptors of the page frames assigned to the new slab, and loads the next and prev subfields of the lru fields in the page descriptors with the addresses of, respectively, the cache descriptor and the slab descriptor. This works correctly because the lru field is used by functions of the buddy system only when the page frame is free, while page frames handled by the slab allocator functions have the PG_slab flag set and are not free as far as the buddy system is concerned.[*] The opposite questiongiven a slab in a cache, which are the page frames that implement it?can be answered by using the s_mem field of the slab descriptor and the gfporder field (the size of a slab) of the cache descriptor. [*] As we'll in Chapter 17, the lru field is also used by the page frame reclaiming algorithm. Next, cache_grow( ) calls cache_init_objs( ), which applies the constructor method (if defined) to all the objects contained in the new slab. Finally, cache_ grow( ) calls list_add_tail( ) to add the newly obtained slab descriptor *slabp at the end of the fully free slab list of the cache descriptor *cachep, and updates the counter of free objects in the cache: list_add_tail(&slabp->list, &cachep->lists->slabs_free); cachep->lists->free_objects += cachep->num; 8.2.7. Releasing a Slab from a Cache Slabs can be destroyed in two cases: There are too many free objects in the slab cache (see the later section "Releasing a Slab from a Cache"). A timer function invoked periodically determines that there are fully unused slabs that can be released (see Chapter 17). In both cases, the slab_destroy( ) function is invoked to destroy a slab and release the corresponding page frames to the zoned page frame allocator: void slab_destroy(kmem_cache_t *cachep, slab_t *slabp) { if (cachep->dtor) { int i; for (i = 0; i < cachep->num; i++) { void* objp = slabp->s_mem+cachep->objsize*i; (cachep->dtor)(objp, cachep, 0); } } kmem_freepages(cachep, slabp->s_mem - slabp->colouroff); if (cachep->flags & CFLGS_OFF_SLAB) kmem_cache_free(cachep->slabp_cache, slabp); } The function checks whether the cache has a destructor method for its objects (the dtor field is not NULL), in which case it applies the destructor to all the objects in the slab; the objp local variable keeps track of the currently examined object. Next, it calls kmem_freepages( ), which returns all the contiguous page frames used by the slab to the buddy system. Finally, if the slab descriptor is stored outside of the slab, the function releases it from the cache of slab descriptors . Actually, the function is slightly more complicated. For example, a slab cache can be created with the SLAB_DESTROY_BY_RCU flag, which means that slabs should be released in a deferred way by registering a callback with the call_rcu( ) function (see the section "Read-Copy Update (RCU)" in Chapter 5). The callback function, in turn, invokes kmem_freepages() and, possibly, the kmem_cache_free(), as in the main case shown above. 8.2.8. Object Descriptor Each object has a short descriptor of type kmem_bufctl_t. Object descriptors are stored in an array placed right after the corresponding slab descriptor. Thus, like the slab descriptors themselves, the object descriptors of a slab can be stored in two possible ways that are illustrated in Figure 8-5. External object descriptors Stored outside the slab, in the general cache pointed to by the slabp_cache field of the cache descriptor. The size of the memory area, and thus the particular general cache used to store object descriptors, depends on the number of objects stored in the slab (num field of the cache descriptor). Internal object descriptors Stored inside the slab, right before the objects they describe. The first object descriptor in the array describes the first object in the slab, and so on. An object descriptor is simply an unsigned short integer, which is meaningful only when the object is free. It contains the index of the next free object in the slab, thus implementing a simple list of free objects inside the slab. The object descriptor of the last element in the free object list is marked by the conventional value BUFCTL_END (0xffff). Figure 8-5. Relationships between slab and object descriptors 8.2.9. Aligning Objects in Memory The objects managed by the slab allocator are aligned in memorythat is, they are stored in memory cells whose initial physical addresses are multiples of a given constant, which is usually a power of 2. This constant is called the alignment factor. The largest alignment factor allowed by the slab allocator is 4,096the page frame size. This means that objects can be aligned by referring to either their physical addresses or their linear addresses. In both cases, only the 12 least significant bits of the address may be altered by the alignment. Usually, microcomputers access memory cells more quickly if their physical addresses are aligned with respect to the word size (that is, to the width of the internal memory bus of the computer). Thus, by default, the kmem_cache_create( ) function aligns objects according to the word size specified by the BYTES_PER_WORD macro. For 80 x 86 processors, the macro yields the value 4 because the word is 32 bits long. When creating a new slab cache, it's possible to specify that the objects included in it be aligned in the first-level hardware cache. To achieve this, the kernel sets the SLAB_HWCACHE_ALIGN cache descriptor flag. The kmem_cache_create( ) function handles the request as follows: If the object's size is greater than half of a cache line, it is aligned in RAM to a multiple of L1_CACHE_BYTESthat is, at the beginning of the line. Otherwise, the object size is rounded up to a submultiple of L1_CACHE_BYTES; this ensures that a small object will never span across two cache lines. Clearly, what the slab allocator is doing here is trading memory space for access time; it gets better cache performance by artificially increasing the object size, thus causing additional internal fragmentation. 8.2.10. Slab Coloring We know from Chapter 2 that the same hardware cache line maps many different blocks of RAM. In this chapter, we have also seen that objects of the same size end up being stored at the same offset within a cache. Objects that have the same offset within different slabs will, with a relatively high probability, end up mapped in the same cache line. The cache hardware might therefore waste memory cycles transferring two objects from the same cache line back and forth to different RAM locations, while other cache lines go underutilized. The slab allocator tries to reduce this unpleasant cache behavior by a policy called slab coloring : different arbitrary values called colors are assigned to the slabs. Before examining slab coloring, we have to look at the layout of objects in the cache. Let's consider a cache whose objects are aligned in RAM. This means that the object address must be a multiple of a given positive value, say aln. Even taking the alignment constraint into account, there are many possible ways to place objects inside the slab. The choices depend on decisions made for the following variables: num Number of objects that can be stored in a slab (its value is in the num field of the cache descriptor). osize Object size, including the alignment bytes. dsize Slab descriptor size plus all object descriptors size, rounded up to the smallest multiple of the hardware cache line size. Its value is equal to 0 if the slab and object descriptors are stored outside of the slab. free Number of unused bytes (bytes not assigned to any object) inside the slab. The total length in bytes of a slab can then be expressed as: slab length = (num x osize) + dsize+ free free is always smaller than osize, because otherwise, it would be possible to place additional objects inside the slab. However, free could be greater than aln. The slab allocator takes advantage of the free unused bytes to color the slab. The term "color" is used simply to subdivide the slabs and allow the memory allocator to spread objects out among different linear addresses. In this way, the kernel obtains the best possible performance from the microprocessor's hardware cache. Slabs having different colors store the first object of the slab in different memory locations, while satisfying the alignment constraint. The number of available colors is free/aln (this value is stored in the colour field of the cache descriptor). Thus, the first color is denoted as 0 and the last one is denoted as (free / aln)-1. (As a particular case, if free is lower than aln, colour is set to 0, nevertheless all slabs use color 0, thus really the number of colors is one.) If a slab is colored with color col, the offset of the first object (with respect to the slab initial address) is equal to colx aln + dsize bytes. Figure 8-6 illustrates how the placement of objects inside the slab depends on the slab color. Coloring essentially leads to moving some of the free area of the slab from the end to the beginning. Figure 8-6. Slab with color col and alignment aln Coloring works only when free is large enough. Clearly, if no alignment is required for the objects or if the number of unused bytes inside the slab is smaller than the required alignment (free < aln), the only possible slab coloring is the one that has the color 0the one that assigns a zero offset to the first object. The various colors are distributed equally among slabs of a given object type by storing the current color in a field of the cache descriptor called colour_next. The cache_ grow( ) function assigns the color specified by colour_next to a new slab and then increases the value of this field. After reaching colour, it wraps around again to 0. In this way, each slab is created with a different color from the previous one, up to the maximum available colors. The cache_grow( ) function, moreover, gets the value aln from the colour_off field of the cache descriptor, computes dsize according to the number of objects inside the slab, and finally stores the value colx aln + dsize in the colouroff field of the slab descriptor. 8.2.11. Local Caches of Free Slab Objects The Linux 2.6 implementation of the slab allocator for multiprocessor systems differs from that of the original Solaris 2.4. To reduce spin lock contention among processors and to make better use of the hardware caches, each cache of the slab allocator includes a perCPU data structure consisting of a small array of pointers to freed objects called the slab local cache . Most allocations and releases of slab objects affect the local cache only; the slab data structures get involved only when the local cache underflows or overflows. This technique is quite similar to the one illustrated in the section "The Per-CPU Page Frame Cache" earlier in this chapter. The array field of the cache descriptor is an array of pointers to array_cache data structures, one element for each CPU in the system. Each array_cache data structure is a descriptor of the local cache of free objects, whose fields are illustrated in Table 8-11. Table 8-11. The fields of the array_cache structure Type unsigned int unsigned int Name avail Description Number of pointers to available objects in the local cache. The field also acts as the index of the first free slot in the cache. Size of the local cachethat is, the maximum number of pointers in the local cache. limit Table 8-11. The fields of the array_cache structure Type unsigned int unsigned int Name Description batchcount Chunk size for local cache refill or emptying. touched Flag set to 1 if the local cache has been recently used. Notice that the local cache descriptor does not include the address of the local cache itself; in fact, the local cache is placed right after the descriptor. Of course, the local cache stores the pointers to the freed objects, not the object themselves, which are always placed inside the slabs of the cache. When creating a new slab cache, the kmem_cache_create( ) function determines the size of the local caches (storing this value in the limit field of the cache descriptor), allocates them, and stores their pointers into the array field of the cache descriptor. When creating a new slab cache, the kmem_cache_create( ) function determines the size of the local caches (storing this value in the limit field of the cache descriptor), allocates them, and stores their pointers into the array field of the cache descriptor. The size depends on the size of the objects stored in the slab cache, and ranges from 1 for very large objects to 120 for small ones. Moreover, the initial value of the batchcount field, which is the number of objects added or removed in a chunk from a local cache, is initially set to half of the local cache size.[*] [*] The system administrator can tunefor each cachethe size of the local caches and the value of the batchcount field by writing into the /proc/slabinfo file. In multiprocessor systems, slab caches for small objects also sport an additional local cache, whose address is stored in the lists.shared field of the cache descriptor. The shared local cache is, as the name suggests, shared among all CPUs, and it makes the task of migrating free objects from a local cache to another easier (see the following section). Its initial size is equal to eight times the value of the batchcount field. 8.2.12. Allocating a Slab Object New objects may be obtained by invoking the kmem_cache_alloc( ) function. The parameter cachep points to the cache descriptor from which the new free object must be obtained, while the parameter flag represents the flags to be passed to the zoned page frame allocator functions, should all slabs of the cache be full. The function is essentially equivalent to the following: void * kmem_cache_alloc(kmem_cache_t *cachep, int flags) { unsigned long save_flags; void *objp; struct array_cache *ac; local_irq_save(save_flags); ac = cache_p->array[smp_processor_id()]; if (ac->avail) { ac->touched = 1; objp = ((void **)(ac+1))[--ac->avail]; } else objp = cache_alloc_refill(cachep, flags); local_irq_restore(save_flags); return objp; } The function tries first to retrieve a free object from the local cache. If there are free objects, the avail field contains the index in the local cache of the entry that points to the last freed object. Because the local cache array is stored right after the ac descriptor, ((void**)(ac+1))[--ac->avail] gets the address of that free object and decreases the value of ac->avail. The cache_alloc_refill( ) function is invoked to repopulate the local cache and get a free object when there are no free objects in the local cache. The cache_alloc_refill( ) function essentially performs the following steps: 1. Stores in the ac local variable the address of the local cache descriptor: ac = cachep->array[smp_processor_id()]; 2. Gets the cachep->spinlock. 3. If the slab cache includes a shared local cache, and if the shared local cache includes some free objects, it refills the CPU's local cache by moving up to ac->batchcount pointers from the shared local cache. Then, it jumps to step 6. 4. Tries to fill the local cache with up to ac->batchcount pointers to free objects included in the slabs of the cache: a. Looks in the slabs_partial and slabs_free lists of the cache descriptor, and gets the address slabp of a slab descriptor whose corresponding slab is either partially filled or empty. If no such descriptor exists, the function goes to step 5. b. For each free object in the slab, the function increases the inuse field of the slab descriptor, inserts the object's address in the local cache, and updates the free field so that it stores the index of the next free object in the slab: c. slabp->inuse++; d. ((void**)(ac+1))[ac->avail++] = e. slabp->s_mem + slabp->free * cachep->obj_size; f. slabp->free = ((kmem_bufctl_t*)(slabp+1))[slabp->free]; g. Inserts, if necessary, the depleted slab in the proper list, either the slab_full or the slab_partial list. 5. At this point, the number of pointers added to the local cache is stored in the ac>avail field: the function decreases the free_objects field of the kmem_list3 structure of the same amount to specify that the objects are no longer free. 6. Releases the cachep->spinlock. 7. If the ac->avail field is now greater than 0 (some cache refilling took place), it sets the ac->touched field to 1 and returns the free object pointer that was last inserted in the local cache: return ((void**)(ac+1))[--ac->avail]; 8. Otherwise, no cache refilling took place: invokes cache_grow() to get a new slab, and thus new free objects. 9. If cache_grow() fails, it returns NULL; otherwise it goes back to step 1 to repeat the procedure. 8.2.13. Freeing a Slab Object The kmem_cache_free( ) function releases an object previously allocated by the slab allocator to some kernel function. Its parameters are cachep, the address of the cache descriptor, and objp, the address of the object to be released: void kmem_cache_free(kmem_cache_t *cachep, void *objp) { unsigned long flags; struct array_cache *ac; local_irq_save(flags); ac = cachep->array[smp_processor_id()]; if (ac->avail == ac->limit) cache_flusharray(cachep, ac); ((void**)(ac+1))[ac->avail++] = objp; local_irq_restore(flags); } The function checks first whether the local cache has room for an additional pointer to a free object. If so, the pointer is added to the local cache and the function returns. Otherwise it first invokes cache_flusharray( ) to deplete the local cache and then adds the pointer to the local cache. The cache_flusharray( ) function performs the following operations: 1. Acquires the cachep->spinlock spin lock. 2. If the slab cache includes a shared local cache, and if the shared local cache is not already full, it refills the shared local cache by moving up to ac->batchcount pointers from the CPU's local cache. Then, it jumps to step 4. 3. Invokes the free_block( ) function to give back to the slab allocator up to ac>batchcount objects currently included in the local cache. For each object at address objp, the function executes the following steps: a. Increases the lists.free_objects field of the cache descriptor. b. Determines the address of the slab descriptor containing the object: c. slabp = (struct slab *)(virt_to_page(objp)->lru.prev); (Remember that the lru.prev field of the descriptor of the slab page points to the corresponding slab descriptor.) d. Removes the slab descriptor from its slab cache list (either cachep>lists.slabs_partial or cachep->lists.slabs_full). e. Computes the index of the object inside the slab: f. objnr = (objp - slabp->s_mem) / cachep->objsize; g. Stores in the object descriptor the current value of the slabp->free, and puts in slabp->free the index of the object (the last released object will be the first object to be allocated again): h. ((kmem_bufctl_t *)(slabp+1))[objnr] = slabp->free; i. slabp->free = objnr; j. Decreases the slabp->inuse field. k. If slabp->inuse is equal to zeroall objects in the slab are freeand the number of free objects in the whole slab cache (cachep->lists.free_objects) is greater than the limit stored in the cachep->free_limit field, then the function releases the slab's page frame(s) to the zoned page frame allocator: l. cachep->lists.free_objects -= cachep->num; m. slab_destroy(cachep, slabp); The value stored in the cachep->free_limit field is usually equal to cachep>num+ (1+N) x cachep->batchcount, where N denotes the number of CPUs of the system. n. Otherwise, if slab->inuse is equal to zero but the number of free objects in the whole slab cache is less than cachep->free_limit, it inserts the slab descriptor in the cachep->lists.slabs_free list. o. Finally, if slab->inuse is greater than zero, the slab is partially filled, so the function inserts the slab descriptor in the cachep->lists.slabs_partial list. 4. Releases the cachep->spinlock spin lock. 5. Updates the avail field of the local cache descriptor by subtracting the number of objects moved to the shared local cache or released to the slab allocator. 6. Moves all valid pointers in the local cache at the beginning of the local cache's array. This step is necessary because the first object pointers have been removed from the local cache, thus the remaining ones must be moved up. 8.2.14. General Purpose Objects As stated earlier in the section "The Buddy System Algorithm," infrequent requests for memory areas are handled through a group of general caches whose objects have geometrically distributed sizes ranging from a minimum of 32 to a maximum of 131,072 bytes. Objects of this type are obtained by invoking the kmalloc( ) function, which is essentially equivalent to the following code fragment: void * kmalloc(size_t size, int flags) { struct cache_sizes *csizep = malloc_sizes; kmem_cache_t * cachep; for (; csizep->cs_size; csizep++) { if (size > csizep->cs_size) continue; if (flags & _ _GFP_DMA) cachep = csizep->cs_dmacachep; else cachep = csizep->cs_cachep; return kmem_cache_alloc(cachep, flags); } return NULL; } The function uses the malloc_sizes table to locate the nearest power-of-2 size to the requested size. It then calls kmem_cache_alloc( ) to allocate the object, passing to it either the cache descriptor for the page frames usable for ISA DMA or the cache descriptor for the "normal" page frames, depending on whether the caller specified the _ _GFP_DMA flag. Objects obtained by invoking kmalloc( ) can be released by calling kfree( ): void kfree(const void *objp) { kmem_cache_t * c; unsigned long flags; if (!objp) return; local_irq_save(flags); c = (kmem_cache_t *)(virt_to_page(objp)->lru.next); kmem_cache_free(c, (void *)objp); local_irq_restore(flags); } The proper cache descriptor is identified by reading the lru.next subfield of the descriptor of the first page frame containing the memory area. The memory area is released by invoking kmem_cache_free( ). 8.2.15. Memory Pools Memory pools are a new feature of Linux 2.6. Basically, a memory pool allows a kernel componentsuch as the block device subsystemto allocate some dynamic memory to be used only in low-on-memory emergencies. Memory pools should not be confused with the reserved page frames described in the earlier section "The Pool of Reserved Page Frames." In fact, those page frames can be used only to satisfy atomic memory allocation requests issued by interrupt handlers or inside critical regions. Instead, a memory pool is a reserve of dynamic memory that can be used only by a specific kernel component, namely the "owner" of the pool. The owner does not normally use the reserve; however, if dynamic memory becomes so scarce that all usual memory allocation requests are doomed to fail, the kernel component can invoke, as a last resort, special memory pool functions that dip in the reserve and get the memory needed. Thus, creating a memory pool is similar to keeping a reserve of canned foods on hand and using a can opener only when no fresh food is available. Often, a memory pool is stacked over the slab allocatorthat is, it is used to keep a reserve of slab objects. Generally speaking, however, a memory pool can be used to allocate every kind of dynamic memory, from whole page frames to small memory areas allocated with kmalloc(). Therefore, we will generically refer to the memory units handled by a memory pool as "memory elements." A memory pool is described by a mempool_t object, whose fields are shown in Table 8-12. Table 8-12. The fields of the mempool_t object Type spinlock_t int int void ** void * mempool_alloc_t * mempool_free_t * wait_queue_head_t Name lock min_nr curr_nr elements pool_data alloc free wait Description Spin lock protecting the object fields Maximum number of elements in the memory pool Current number of elements in the memory pool Pointer to an array of pointers to the reserved elements Private data available to the pool's owner Method to allocate an element Method to free an element Wait queue used when the memory pool is empty The min_nr field stores the initial number of elements in the memory pool. In other words, the value stored in this field represents the number of memory elements that the owner of the memory pool is sure to obtain from the memory allocator. The curr_nr field, which is always lower than or equal to min_nr, stores the number of memory elements currently included in the memory pool. The memory elements themselves are referenced by an array of pointers, whose address is stored in the elements field. The alloc and free methods interface with the underlying memory allocator to get and release a memory element, respectively. Both methods may be custom functions provided by the kernel component that owns the memory pool. When the memory elements are slab objects, the alloc and free methods are commonly implemented by the mempool_alloc_slab( ) and mempool_free_slab( ) functions, which just invoke the kmem_cache_alloc( ) and kmem_cache_free( ) functions, respectively. In this case, the pool_data field of the mempool_t object stores the address of the slab cache descriptor. The mempool_create( ) function creates a new memory pool; it receives the number of memory elements min_nr, the addresses of the functions that implement the alloc and free methods, and an optional value for the pool_data field. The function allocates memory for the mempool_t object and the array of pointers to the memory elements, then repeatedly invokes the alloc method to get the min_nr memory elements. Conversely, the mempool_destroy( ) function releases all memory elements in the pool, then releases the array of elements and the mempool_t object themselves. To allocate an element from a memory pool, the kernel invokes the mempool_alloc( ) function, passing to it the address of the mempool_t object and the memory allocation flags (see Table 8-5 and Table 8-6 earlier in this chapter). Essentially, the function tries to allocate a memory element from the underlying memory allocator by invoking the alloc method, according to the memory allocation flags specified as parameters. If the allocation succeeds, the function returns the memory element obtained, without touching the memory pool. Otherwise, if the allocation fails, the memory element is taken from the memory pool. Of course, too many allocations in a low-on-memory condition can exhaust the memory pool: in this case, if the _ _GFP_WAIT flag is not set, mempool_alloc() blocks the current process until a memory element is released to the memory pool. Conversely, to release an element to a memory pool, the kernel invokes the mempool_free( ) function. If the memory pool is not full (curr_min is smaller than min_nr), the function adds the element to the memory pool. Otherwise, mempool_free( ) invokes the free method to release the element to the underlying memory allocator. 8.3. Noncontiguous Memory Area Management We already know that it is preferable to map memory areas into sets of contiguous page frames, thus making better use of the cache and achieving lower average memory access times. Nevertheless, if the requests for memory areas are infrequent, it makes sense to consider an allocation scheme based on noncontiguous page frames accessed through contiguous linear addresses . The main advantage of this schema is to avoid external fragmentation, while the disadvantage is that it is necessary to fiddle with the kernel Page Tables. Clearly, the size of a noncontiguous memory area must be a multiple of 4,096. Linux uses noncontiguous memory areas in several ways for instance, to allocate data structures for active swap areas (see the section "Activating and Deactivating a Swap Area" in Chapter 17), to allocate space for a module (see Appendix B), or to allocate buffers to some I/O drivers. Furthermore, noncontiguous memory areas provide yet another way to make use of high memory page frames (see the later section "Allocating a Noncontiguous Memory Area"). 8.3.1. Linear Addresses of Noncontiguous Memory Areas To find a free range of linear addresses, we can look in the area starting from PAGE_OFFSET (usually 0xc0000000, the beginning of the fourth gigabyte). Figure 8-7 shows how the fourth gigabyte linear addresses are used: The beginning of the area includes the linear addresses that map the first 896 MB of RAM (see the section "Process Page Tables" in Chapter 2); the linear address that corresponds to the end of the directly mapped physical memory is stored in the high_memory variable. The end of the area contains the fix-mapped linear addresses (see the section "FixMapped Linear Addresses" in Chapter 2). Starting from PKMAP_BASE we find the linear addresses used for the persistent kernel mapping of high-memory page frames (see the section "Kernel Mappings of HighMemory Page Frames" earlier in this chapter). The remaining linear addresses can be used for noncontiguous memory areas. A safety interval of size 8 MB (macro VMALLOC_OFFSET) is inserted between the end of the physical memory mapping and the first memory area; its purpose is to "capture" out-of-bounds memory accesses. For the same reason, additional safety intervals of size 4 KB are inserted to separate noncontiguous memory areas. Figure 8-7. The linear address interval starting from PAGE_OFFSET The VMALLOC_START macro defines the starting address of the linear space reserved for noncontiguous memory areas, while VMALLOC_END defines its ending address. 8.3.2. Descriptors of Noncontiguous Memory Areas Each noncontiguous memory area is associated with a descriptor of type vm_struct, whose fields are listed in Table 8-13. Table 8-13. The fields of the vm_struct descriptor Type void * unsigned long Name addr size Description Linear address of the first memory cell of the area Size of the area plus 4,096 (inter-area safety interval) Table 8-13. The fields of the vm_struct descriptor Type unsigned long Name flags Description Type of memory mapped by the noncontiguous memory area Pointer to array of nr_pages pointers to page descriptors Number of pages filled by the area Set to 0 unless the area has been created to map the I/O shared memory of a hardware device Pointer to next vm_struct structure struct page ** pages unsigned int unsigned long struct vm_struct * nr_pages phys_addr next These descriptors are inserted in a simple list by means of the next field; the address of the first element of the list is stored in the vmlist variable. Accesses to this list are protected by means of the vmlist_lock read/write spin lock. The flags field identifies the type of memory mapped by the area: VM_ALLOC for pages obtained by means of vmalloc( ), VM_MAP for already allocated pages mapped by means of vmap() (see the next section), and VM_IOREMAP for on-board memory of hardware devices mapped by means of ioremap( ) (see Chapter 13). The get_vm_area( ) function looks for a free range of linear addresses between VMALLOC_START and VMALLOC_END. This function acts on two parameters: the size (size) in bytes of the memory region to be created, and a flag (flag) specifying the type of region (see above). The steps performed are the following: 1. Invokes kmalloc( ) to obtain a memory area for the new descriptor of type vm_struct. 2. Gets the vmlist_lock lock for writing and scans the list of descriptors of type vm_struct looking for a free range of linear addresses that includes at least size + 4096 addresses (4096 is the size of the safety interval between the memory areas). 3. If such an interval exists, the function initializes the fields of the descriptor, releases the vmlist_lock lock, and terminates by returning the initial address of the noncontiguous memory area. 4. Otherwise, get_vm_area( ) releases the descriptor obtained previously, releases the vmlist_lock lock, and returns NULL. 8.3.3. Allocating a Noncontiguous Memory Area The vmalloc( ) function allocates a noncontiguous memory area to the kernel. The parameter size denotes the size of the requested area. If the function is able to satisfy the request, it then returns the initial linear address of the new area; otherwise, it returns a NULL pointer: void * vmalloc(unsigned long size) { struct vm_struct *area; struct page **pages; unsigned int array_size, i; size = (size + PAGE_SIZE - 1) & PAGE_MASK; area = get_vm_area(size, VM_ALLOC); if (!area) return NULL; area->nr_pages = size >> PAGE_SHIFT; array_size = (area->nr_pages * sizeof(struct page *)); area->pages = pages = kmalloc(array_size, GFP_KERNEL); if (!area_pages) { remove_vm_area(area->addr); kfree(area); return NULL; } memset(area->pages, 0, array_size); for (i=0; i<area->nr_pages; i++) { area->pages[i] = alloc_page(GFP_KERNEL|_ _GFP_HIGHMEM); if (!area->pages[i]) { area->nr_pages = i; fail: vfree(area->addr); return NULL; } } if (map_vm_area(area, _ _pgprot(0x63), &pages)) goto fail; return area->addr; } The function starts by rounding up the value of the size parameter to a multiple of 4,096 (the page frame size). Then vmalloc( ) invokes get_vm_area( ), which creates a new descriptor and returns the linear addresses assigned to the memory area. The flags field of the descriptor is initialized with the VM_ALLOC flag, which means that the noncontiguous page frames will be mapped into a linear address range by means of the vmalloc( ) function. Then the vmalloc( ) function invokes kmalloc( ) to request a group of contiguous page frames large enough to contain an array of page descriptor pointers. The memset( ) function is invoked to set all these pointers to NULL. Next the alloc_page( ) function is called repeatedly, once for each of the nr_pages of the region, to allocate a page frame and store the address of the corresponding page descriptor in the area->pages array. Observe that using the area->pages array is necessary because the page frames could belong to the ZONE_HIGHMEM memory zone, thus right now they are not necessarily mapped to a linear address. Now comes the tricky part. Up to this point, a fresh interval of contiguous linear addresses has been obtained and a group of noncontiguous page frames has been allocated to map these linear addresses. The last crucial step consists of fiddling with the page table entries used by the kernel to indicate that each page frame allocated to the noncontiguous memory area is now associated with a linear address included in the interval of contiguous linear addresses yielded by vmalloc( ). This is what map_vm_area( ) does. The map_vm_area( ) function uses three parameters: area The pointer to the vm_struct descriptor of the area. prot The protection bits of the allocated page frames. It is always set to 0x63, which corresponds to Present, Accessed, Read/Write, and Dirty. pages The address of a variable pointing to an array of pointers to page descriptors (thus, struct page *** is used as the data type!). The function starts by assigning the linear addresses of the start and end of the area to the address and end local variables, respectively: address = area->addr; end = address + (area->size - PAGE_SIZE); Remember that area->size stores the actual size of the area plus the 4 KB inter-area safety interval. The function then uses the pgd_offset_k macro to derive the entry in the master kernel Page Global Directory related to the initial linear address of the area; it then acquires the kernel Page Table spin lock: pgd = pgd_offset_k(address); spin_lock(&init_mm.page_table_lock); The function then executes the following cycle: int ret = 0; for (i = pgd_index(address); i < pgd_index(end-1); i++) { pud_t *pud = pud_alloc(&init_mm, pgd, address); ret = -ENOMEM; if (!pud) break; next = (address + PGDIR_SIZE) & PGDIR_MASK; if (next < address || next > end) next = end; if (map_area_pud(pud, address, next, prot, pages)) break; address = next; pgd++; ret = 0; } spin_unlock(&init_mm.page_table_lock); flush_cache_vmap((unsigned long)area->addr, end); return ret; In each cycle, it first invokes pud_alloc( ) to create a Page Upper Directory for the new area and writes its physical address in the right entry of the kernel Page Global Directory. It then calls map_area_pud( ) to allocate all the page tables associated with the new Page Upper Directory. It adds the size of the range of linear addresses spanned by a single Page Upper Directorythe constant 230 if PAE is enabled, 222 otherwiseto the current value of address, and it increases the pointer pgd to the Page Global Directory. The cycle is repeated until all Page Table entries referring to the noncontiguous memory area are set up. The map_area_pud( ) function executes a similar cycle for all the page tables that a Page Upper Directory points to: do { pmd_t * pmd = pmd_alloc(&init_mm, pud, address); if (!pmd) return -ENOMEM; if (map_area_pmd(pmd, address, end-address, prot, pages)) return -ENOMEM; address = (address + PUD_SIZE) & PUD_MASK; pud++; } while (address < end); The map_area_pmd( ) function executes a similar cycle for all the Page Tables that a Page Middle Directory points to: do { pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address); if (!pte) return -ENOMEM; if (map_area_pte(pte, address, end-address, prot, pages)) return -ENOMEM; address = (address + PMD_SIZE) & PMD_MASK; pmd++; } while (address < end); The pte_alloc_kernel( ) function (see the section "Page Table Handling" in Chapter 2) allocates a new Page Table and updates the corresponding entry in the Page Middle Directory. Next, map_area_pte( ) allocates all the page frames corresponding to the entries in the Page Table. The value of address is increased by 222the size of the linear address interval spanned by a single Page Tableand the cycle is repeated. The main cycle of map_area_pte( ) is: do { struct page * page = **pages; set_pte(pte, mk_pte(page, prot)); address += PAGE_SIZE; pte++; (*pages)++; } while (address < end); The page descriptor address page of the page frame to be mapped is read from the array's entry pointed to by the variable at address pages. The physical address of the new page frame is written into the Page Table by the set_pte and mk_pte macros. The cycle is repeated after adding the constant 4,096 (the length of a page frame) to address. Notice that the Page Tables of the current process are not touched by map_vm_area( ). Therefore, when a process in Kernel Mode accesses the noncontiguous memory area, a Page Fault occurs, because the entries in the process's Page Tables corresponding to the area are null. However, the Page Fault handler checks the faulty linear address against the master kernel Page Tables (which are init_mm.pgd Page Global Directory and its child page tables; see the section "Kernel Page Tables" in Chapter 2). Once the handler discovers that a master kernel Page Table includes a non-null entry for the address, it copies its value into the corresponding process's Page Table entry and resumes normal execution of the process. This mechanism is described in the section "Page Fault Exception Handler" in Chapter 9. Beside the vmalloc( ) function, a noncontiguous memory area can be allocated by the vmalloc_32( ) function, which is very similar to vmalloc( ) but only allocates page frames from the ZONE_NORMAL and ZONE_DMA memory zones. Linux 2.6 also features a vmap( ) function, which maps page frames already allocated in a noncontiguous memory area: essentially, this function receives as its parameter an array of pointers to page descriptors, invokes get_vm_area( ) to get a new vm_struct descriptor, and then invokes map_vm_area( ) to map the page frames. The function is thus similar to vmalloc( ), but it does not allocate page frames. 8.3.4. Releasing a Noncontiguous Memory Area The vfree( ) function releases noncontiguous memory areas created by vmalloc( ) or vmalloc_32( ), while the vunmap( ) function releases memory areas created by vmap( ). Both functions have one parameterthe address of the initial linear address of the area to be released; they both rely on the _ _vunmap( ) function to do the real work. The _ _vunmap( ) function receives two parameters: the address addr of the initial linear address of the area to be released, and the flag deallocate_pages, which is set if the page frames mapped in the area should be released to the zoned page frame allocator (vfree( )'s invocation), and cleared otherwise (vunmap( )'s invocation). The function performs the following operations: 1. Invokes the remove_vm_area( ) function to get the address area of the vm_struct descriptor and to clear the kernel's page table entries corresponding to the linear address in the noncontiguous memory area. 2. If the deallocate_pages flag is set, it scans the area->pages array of pointers to the page descriptor; for each element of the array, invokes the _ _free_page( ) function to release the page frame to the zoned page frame allocator. Moreover, executes kfree(area->pages) to release the array itself. 3. Invokes kfree(area) to release the vm_struct descriptor. The remove_vm_area( ) function performs the following cycle: write_lock(&vmlist_lock); for (p = &vmlist ; (tmp = *p) ; p = &tmp->next) { if (tmp->addr == addr) { unmap_vm_area(tmp); *p = tmp->next; break; } } write_unlock(&vmlist_lock); return tmp; The area itself is released by invoking unmap_vm_area( ). This function acts on a single parameter, namely a pointer area to the vm_struct descriptor of the area. It executes the following cycle to reverse the actions performed by map_vm_area( ): address = area->addr; end = address + area->size; pgd = pgd_offset_k(address); for (i = pgd_index(address); i <= pgd_index(end-1); i++) { next = (address + PGDIR_SIZE) & PGDIR_MASK; if (next <= address || next > end) next = end; unmap_area_pud(pgd, address, next - address); address = next; pgd++; } In turn, unmap_area_pud( ) reverses the actions of map_area_pud( ) in the cycle: do { unmap_area_pmd(pud, address, end-address); address = (address + PUD_SIZE) & PUD_MASK; pud++; } while (address && (address < end)); The unmap_area_pmd( ) function reverses the actions of map_area_pmd( ) in the cycle: do { unmap_area_pte(pmd, address, end-address); address = (address + PMD_SIZE) & PMD_MASK; pmd++; } while (address < end); Finally, unmap_area_pte( ) reverses the actions of map_area_pte( ) in the cycle: do { pte_t page = ptep_get_and_clear(pte); address += PAGE_SIZE; pte++; if (!pte_none(page) && !pte_present(page)) printk("Whee... Swapped out page in kernel page table\n"); } while (address < end); In every iteration of the cycle, the page table entry pointed to by pte is set to 0 by the ptep_get_and_clear macro. As for vmalloc( ), the kernel modifies the entries of the master kernel Page Global Directory and its child page tables (see the section "Kernel Page Tables" in Chapter 2), but it leaves unchanged the entries of the process page tables mapping the fourth gigabyte. This is fine because the kernel never reclaims Page Upper Directories, Page Middle Directories, and Page Tables rooted at the master kernel Page Global Directory. For instance, suppose that a process in Kernel Mode accessed a noncontiguous memory area that later got released. The process's Page Global Directory entries are equal to the corresponding entries of the master kernel Page Global Directory, thanks to the mechanism explained in the section "Page Fault Exception Handler" in Chapter 9; they point to the same Page Upper Directories, Page Middle Directories, and Page Tables. The unmap_area_pte( ) function clears only the entries of the page tables (without reclaiming the page tables themselves). Further accesses of the process to the released noncontiguous memory area will trigger Page Faults because of the null page table entries. However, the handler will consider such accesses a bug, because the master kernel page tables do not include valid entries. Chapter 9. Process Address Space As seen in the previous chapter, a kernel function gets dynamic memory in a fairly straightforward manner by invoking one of a variety of functions: _ _get_free_pages( ) or alloc_pages( ) to get pages from the zoned page frame allocator, kmem_cache_alloc( ) or kmalloc( ) to use the slab allocator for specialized or general-purpose objects, and vmalloc( ) or vmalloc_32( ) to get a noncontiguous memory area. If the request can be satisfied, each of these functions returns a page descriptor address or a linear address identifying the beginning of the allocated dynamic memory area. These simple approaches work for two reasons: The kernel is the highest-priority component of the operating system. If a kernel function makes a request for dynamic memory, it must have a valid reason to issue that request, and there is no point in trying to defer it. The kernel trusts itself. All kernel functions are assumed to be error-free, so the kernel does not need to insert any protection against programming errors. When allocating memory to User Mode processes, the situation is entirely different: Process requests for dynamic memory are considered non-urgent. When a process's executable file is loaded, for instance, it is unlikely that the process will address all the pages of code in the near future. Similarly, when a process invokes malloc( ) to get additional dynamic memory, it doesn't mean the process will soon access all the additional memory obtained. Thus, as a general rule, the kernel tries to defer allocating dynamic memory to User Mode processes. Because user programs cannot be trusted, the kernel must be prepared to catch all addressing errors caused by processes in User Mode. As this chapter describes, the kernel succeeds in deferring the allocation of dynamic memory to processes by using a new kind of resource. When a User Mode process asks for dynamic memory, it doesn't get additional page frames; instead, it gets the right to use a new range of linear addresses, which become part of its address space. This interval is called a "memory region." In the next section, we discuss how the process views dynamic memory. We then describe the basic components of the process address space in the section "Memory Regions." Next, we examine in detail the role played by the Page Fault exception handler in deferring the allocation of page frames to processes and illustrate how the kernel creates and deletes whole process address spaces. Last, we discuss the APIs and system calls related to address space management. 9.1. The Process's Address Space The address space of a process consists of all linear addresses that the process is allowed to use. Each process sees a different set of linear addresses; the address used by one process bears no relation to the address used by another. As we will see later, the kernel may dynamically modify a process address space by adding or removing intervals of linear addresses. The kernel represents intervals of linear addresses by means of resources called memory regions , which are characterized by an initial linear address, a length, and some access rights. For reasons of efficiency, both the initial address and the length of a memory region must be multiples of 4,096, so that the data identified by each memory region completely fills up the page frames allocated to it. Following are some typical situations in which a process gets new memory regions: When the user types a command at the console, the shell process creates a new process to execute the command. As a result, a fresh address space, and thus a set of memory regions, is assigned to the new process (see the section "Creating and Deleting a Process Address Space" later in this chapter; also, see Chapter 20). A running process may decide to load an entirely different program. In this case, the process ID remains unchanged, but the memory regions used before loading the program are released and a new set of memory regions is assigned to the process (see the section "The exec Functions" in Chapter 20). A running process may perform a "memory mapping" on a file (or on a portion of it). In such cases, the kernel assigns a new memory region to the process to map the file (see the section "Memory Mapping" in Chapter 16). A process may keep adding data on its User Mode stack until all addresses in the memory region that map the stack have been used. In this case, the kernel may decide to expand the size of that memory region (see the section "Page Fault Exception Handler" later in this chapter). A process may create an IPC-shared memory region to share data with other cooperating processes. In this case, the kernel assigns a new memory region to the process to implement this construct (see the section "IPC Shared Memory" in Chapter 19). A process may expand its dynamic area (the heap) through a function such as malloc( ). As a result, the kernel may decide to expand the size of the memory region assigned to the heap (see the section "Managing the Heap" later in this chapter). Table 9-1 illustrates some of the system calls related to the previously mentioned tasks. brk( ) is discussed at the end of this chapter, while the remaining system calls are described in other chapters. Table 9-1. System calls related to memory region creation and deletion System call brk( ) execve( ) _exit( ) fork( ) mmap( ), mmap2( ) mremap( ) Description Changes the heap size of the process Loads a new executable file, thus changing the process address space Terminates the current process and destroys its address space Creates a new process, and thus a new address space Creates a memory mapping for a file, thus enlarging the process address space Expands or shrinks a memory region remap_file_pages( ) Creates a non-linear mapping for a file (see Chapter 16) Table 9-1. System calls related to memory region creation and deletion System call munmap( ) shmat( ) shmdt( ) Description Destroys a memory mapping for a file, thus contracting the process address space Attaches a shared memory region Detaches a shared memory region As we'll see in the later section "Page Fault Exception Handler," it is essential for the kernel to identify the memory regions currently owned by a process (the address space of a process), because that allows the Page Fault exception handler to efficiently distinguish between two types of invalid linear addresses that cause it to be invoked: Those caused by programming errors. Those caused by a missing page; even though the linear address belongs to the process's address space, the page frame corresponding to that address has yet to be allocated. The latter addresses are not invalid from the process's point of view; the induced Page Faults are exploited by the kernel to implement demand paging : the kernel provides the missing page frame and lets the process continue. 9.2. The Memory Descriptor All information related to the process address space is included in an object called the memory descriptor of type mm_struct. This object is referenced by the mm field of the process descriptor. The fields of a memory descriptor are listed in Table 9-2. Table 9-2. The fields of the memory descriptor Type struct vm_area_struct * struct rb_root struct vm_area_struct * mm_rb mmap_cache Field mmap Description Pointer to the head of the list of memory region objects Pointer to the root of the red-black tree of memory region objects Pointer to the last referenced memory region object unsigned long (*)( ) get_unmapped_area Method that searches an available linear address interval in the process address space Table 9-2. The fields of the memory descriptor Type void (*)( ) unsigned long Field unmap_area mmap_base Description Method invoked when releasing a linear address interval Identifies the linear address of the first allocated anonymous memory region or file memory mapping (see the section "Program Segments and Process Memory Regions" in Chapter 20) Address from which the kernel will look for a free interval of linear addresses in the process address space Pointer to the Page Global Directory Secondary usage counter Main usage counter Number of memory regions Memory regions' read/write semaphore unsigned long pgd_t * atomic_t atomic_t int struct rw_semaphore spinlock_t struct list_head unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long unsigned long free_area_cache pgd mm_users mm_count map_count mmap_sem page_table_lock mmlist Memory regions' and Page Tables' spin lock Pointers to adjacent elements in the list of memory descriptors Initial address of executable code Final address of executable code Initial address of initialized data Final address of initialized data Initial address of the heap Current final address of the heap Initial address of User Mode stack Initial address of command-line arguments Final address of command-line arguments Initial address of environment variables Final address of environment variables Number of page frames allocated to the process Number of page frames assigned to anonymous memory mappings Size of the process address space (number of pages) Number of "locked" pages that cannot be swapped out (see Chapter 17) start_code end_code start_data end_data start_brk brk start_stack arg_start arg_end env_start env_end rss unsigned long unsigned long unsigned long anon_rss total_vm locked_vm Table 9-2. The fields of the memory descriptor Type unsigned long unsigned long unsigned long unsigned long unsigned long Field shared_vm exec_vm stack_vm reserved_vm def_flags Description Number of pages in shared file memory mappings Number of pages in executable memory mappings Number of pages in the User Mode stack Number of pages in reserved or special memory regions Default access flags of the memory regions Number of Page Tables of this process Used when starting the execution of an ELF program (see Chapter 20) Flag that specifies whether the process can produce a core dump of the memory Bit mask for lazy TLB switches (see Chapter 2) Pointer to table for architecture-specific information (e.g., LDT's address in 80 86 platforms) When this process will become eligible for having the swap token (see the section "The Swap Token" in Chapter 17) Flag set if a major Page Fault has recently occurred Number of lightweight processes that are dumping the contents of the process address space to a core file (see the section "Deleting a Process Address Space" later in this chapter) Pointer to a completion used when creating a core file (see the section "Completions" in Chapter 5) Completion used when creating a core file Lock used to protect the list of asynchronous I/O contexts (see Chapter 16) List of asynchronous I/O contexts (see Chapter 16) Default asynchronous I/O context (see Chapter 16) Maximum number of page frames ever owned by the process Maximum number of pages ever included in the memory regions of the process unsigned long unsigned long unsigned int cpumask_t mm_context_t nr_ptes saved_auxv dumpable cpu_vm_mask context unsigned long char int swap_token_time recent_pagein core_waiters struct completion * struct completion rwlock_t struct kioctx * struct kioctx unsigned long unsigned long core_startup_done core_done ioctx_list_lock ioctx_list default_kioctx hiwater_rss hiwater_vm All memory descriptors are stored in a doubly linked list. Each descriptor stores the address of the adjacent list items in the mmlist field. The first element of the list is the mmlist field of init_mm, the memory descriptor used by process 0 in the initialization phase. The list is protected against concurrent accesses in multiprocessor systems by the mmlist_lock spin lock. The mm_users field stores the number of lightweight processes that share the mm_struct data structure (see the section "The clone( ), fork( ), and vfork( ) System Calls" in Chapter 3). The mm_count field is the main usage counter of the memory descriptor; all "users" in mm_users count as one unit in mm_count. Every time the mm_count field is decreased, the kernel checks whether it becomes zero; if so, the memory descriptor is deallocated because it is no longer in use. We'll try to explain the difference between the use of mm_users and mm_count with an example. Consider a memory descriptor shared by two lightweight processes. Normally, its mm_users field stores the value 2, while its mm_count field stores the value 1 (both owner processes count as one). If the memory descriptor is temporarily lent to a kernel thread (see the next section), the kernel increases the mm_count field. In this way, even if both lightweight processes die and the mm_users field becomes zero, the memory descriptor is not released until the kernel thread finishes using it because the mm_count field remains greater than zero. If the kernel wants to be sure that the memory descriptor is not released in the middle of a lengthy operation, it might increase the mm_users field instead of mm_count (this is what the try_to_unuse( ) function does; see the section "Activating and Deactivating a Swap Area" in Chapter 17). The final result is the same because the increment of mm_users ensures that mm_count does not become zero even if all lightweight processes that own the memory descriptor die. The mm_alloc( ) function is invoked to get a new memory descriptor. Because these descriptors are stored in a slab allocator cache, mm_alloc( ) calls kmem_cache_alloc( ), initializes the new memory descriptor, and sets the mm_count and mm_users field to 1. Conversely, the mmput( ) function decreases the mm_users field of a memory descriptor. If that field becomes 0, the function releases the Local Descriptor Table, the memory region descriptors (see later in this chapter), and the Page Tables referenced by the memory descriptor, and then invokes mmdrop( ). The latter function decreases mm_count and, if it becomes zero, releases the mm_struct data structure. The mmap, mm_rb, mmlist, and mmap_cache fields are discussed in the next section. 9.2.1. Memory Descriptor of Kernel Threads Kernel threads run only in Kernel Mode, so they never access linear addresses below TASK_SIZE (same as PAGE_OFFSET, usually 0xc0000000). Contrary to regular processes, kernel threads do not use memory regions, therefore most of the fields of a memory descriptor are meaningless for them. Because the Page Table entries that refer to the linear address above TASK_SIZE should always be identical, it does not really matter what set of Page Tables a kernel thread uses. To avoid useless TLB and cache flushes, a kernel thread uses the set of Page Tables of the last previously running regular process. To that end, two kinds of memory descriptor pointers are included in every process descriptor: mm and active_mm. The mm field in the process descriptor points to the memory descriptor owned by the process, while the active_mm field points to the memory descriptor used by the process when it is in execution. For regular processes, the two fields store the same pointer. Kernel threads, however, do not own any memory descriptor, thus their mm field is always NULL. When a kernel thread is selected for execution, its active_mm field is initialized to the value of the active_mm of the previously running process (see the section "The schedule( ) Function" in Chapter 7). There is, however, a small complication. Whenever a process in Kernel Mode modifies a Page Table entry for a "high" linear address (above TASK_SIZE), it should also update the corresponding entry in the sets of Page Tables of all processes in the system. In fact, once set by a process in Kernel Mode, the mapping should be effective for all other processes in Kernel Mode as well. Touching the sets of Page Tables of all processes is a costly operation; therefore, Linux adopts a deferred approach. We already mentioned this deferred approach in the section "Noncontiguous Memory Area Management" in Chapter 8: every time a high linear address has to be remapped (typically by vmalloc( ) or vfree( )), the kernel updates a canonical set of Page Tables rooted at the swapper_pg_dir master kernel Page Global Directory (see the section "Kernel Page Tables" in Chapter 2). This Page Global Directory is pointed to by the pgd field of a master memory descriptor , which is stored in the init_mm variable.[*] [*] We mentioned in the section "Kernel Threads" in Chapter 3 that the swapper process uses init_mm during the initialization phase. However, swapper never uses this memory descriptor once the initialization phase completes. Later, in the section "Handling Noncontiguous Memory Area Accesses," we'll describe how the Page Fault handler takes care of spreading the information stored in the canonical Page Tables when effectively needed. 9.3. Memory Regions Linux implements a memory region by means of an object of type vm_area_struct; its fields are shown in Table 9-3.[*] [*] We omitted describing a few additional fields used in NUMA systems. Table 9-3. The fields of the memory region object Type struct mm_struct * unsigned long unsigned long Field vm_mm vm_start vm_end Description Pointer to the memory descriptor that owns the region. First linear address inside the region. First linear address after the region. Table 9-3. The fields of the memory region object Type struct vm_area_struct * pgprot_t unsigned long struct rb_node union vm_page_prot vm_flags vm_rb shared Field vm_next Description Next region in the process list. Access permissions for the page frames of the region. Flags of the region. Data for the red-black tree (see later in this chapter). Links to the data structures used for reverse mapping (see the section "Reverse Mapping for Mapped Pages" in Chapter 17). Pointers for the list of anonymous memory regions (see the section "Reverse Mapping for Anonymous Pages" in Chapter 17). Pointer to the anon_vma data structure (see the section "Reverse Mapping for Anonymous Pages" in Chapter 17). Pointer to the methods of the memory region. struct list_head anon_vma_node struct anon_vma * anon_vma struct vm_operations_struct* unsigned long vm_ops vm_pgoff Offset in mapped file (see Chapter 16). For anonymous pages, it is either zero or equal to vm_start/PAGE_SIZE (see Chapter 17). Pointer to the file object of the mapped file, if any. Pointer to private data of the memory region. Used when releasing a linear address interval in a non-linear file memory mapping. struct file * void * vm_file vm_private_data unsigned long vm_truncate_count Each memory region descriptor identifies a linear address interval. The vm_start field contains the first linear address of the interval, while the vm_end field contains the first linear address outside of the interval; vm_end-vm_start thus denotes the length of the memory region. The vm_mm field points to the mm_struct memory descriptor of the process that owns the region. We will describe the other fields of vm_area_struct as they come up. Memory regions owned by a process never overlap, and the kernel tries to merge regions when a new one is allocated right next to an existing one. Two adjacent regions can be merged if their access rights match. As shown in Figure 9-1, when a new range of linear addresses is added to the process address space, the kernel checks whether an already existing memory region can be enlarged (case a). If not, a new memory region is created (case b). Similarly, if a range of linear addresses is removed from the process address space, the kernel resizes the affected memory regions (case c). In some cases, the resizing forces a memory region to split into two smaller ones (case d) .[*] [*] Removing a linear address interval may theoretically fail because no free memory is available for a new memory descriptor. Figure 9-1. Adding or removing a linear address interval The vm_ops field points to a vm_operations_struct data structure, which stores the methods of the memory region. Only four methodsillustrated in Table 9-4are applicable to UMA systems. Table 9-4. The methods to act on a memory region Method Description open Invoked when the memory region is added to the set of regions owned by a process. Table 9-4. The methods to act on a memory region Method Description close nopage Invoked when the memory region is removed from the set of regions owned by a process. Invoked by the Page Fault exception handler when a process tries to access a page not present in RAM whose linear address belongs to the memory region (see the later section "Page Fault Exception Handler"). memory region (prefaulting). Mainly used for non-linear file memory mappings. populate Invoked to set the page table entries corresponding to the linear addresses of the 9.3.1. Memory Region Data Structures All the regions owned by a process are linked in a simple list. Regions appear in the list in ascending order by memory address; however, successive regions can be separated by an area of unused memory addresses. The vm_next field of each vm_area_struct element points to the next element in the list. The kernel finds the memory regions through the mmap field of the process memory descriptor, which points to the first memory region descriptor in the list. The map_count field of the memory descriptor contains the number of regions owned by the process. By default, a process may own up to 65,536 different memory regions; however, the system administrator may change this limit by writing in the /proc/sys/vm/max_map_count file. Figure 9-2 illustrates the relationships among the address space of a process, its memory descriptor, and the list of memory regions. Figure 9-2. Descriptors related to the address space of a process A frequent operation performed by the kernel is to search the memory region that includes a specific linear address. Because the list is sorted, the search can terminate as soon as a memory region that ends after the specific linear address is found. However, using the list is convenient only if the process has very few memory regionslet's say less than a few tens of them. Searching, inserting elements, and deleting elements in the list involve a number of operations whose times are linearly proportional to the list length. Although most Linux processes use very few memory regions, there are some large applications, such as object-oriented databases or specialized debuggers for the usage of malloc(), that have many hundreds or even thousands of regions. In such cases, the memory region list management becomes very inefficient, hence the performance of the memory-related system calls degrades to an intolerable point. Therefore, Linux 2.6 stores memory descriptors in data structures called red-black trees . In a red-black tree, each element (or node) usually has two children: a left child and a right child. The elements in the tree are sorted. For each node N, all elements of the subtree rooted at the left child of N precede N, while, conversely, all elements of the subtree rooted at the right child of N follow N (see Figure 9-3(a); the key of the node is written inside the node itself. Moreover, a red-black tree must satisfy four additional rules: 1. 2. 3. 4. Every node must be either red or black. The root of the tree must be black. The children of a red node must be black. Every path from a node to a descendant leaf must contain the same number of black nodes . When counting the number of black nodes, null pointers are counted as black nodes. Figure 9-3. Example of red-black trees These four rules ensure that every red-black tree with n internal nodes has a height of at most 2 x log(n + 1). Searching an element in a red-black tree is thus very efficient, because it requires operations whose execution time is linearly proportional to the logarithm of the tree size. In other words, doubling the number of memory regions adds just one more iteration to the operation. Inserting and deleting an element in a red-black tree is also efficient, because the algorithm can quickly traverse the tree to locate the position at which the element will be inserted or from which it will be removed. Each new node must be inserted as a leaf and colored red. If the operation breaks the rules, a few nodes of the tree must be moved or recolored. For instance, suppose that an element having the value 4 must be inserted in the red-black tree shown in Figure 9-3(a). Its proper position is the right child of the node that has key 3, but once it is inserted, the red node that has the value 3 has a red child, thus breaking rule 3. To satisfy the rule, the color of nodes that have the values 3, 4, and 7 is changed. This operation, however, breaks rule 4, thus the algorithm performs a "rotation" on the subtree rooted at the node that has the key 19, producing the new red-black tree shown in Figure 9-3(b). This looks complicated, but inserting or deleting an element in a red-black tree requires a small number of operationsa number linearly proportional to the logarithm of the tree size. Therefore, to store the memory regions of a process, Linux uses both a linked list and a redblack tree. Both data structures contain pointers to the same memory region descriptors. When inserting or removing a memory region descriptor, the kernel searches the previous and next elements through the red-black tree and uses them to quickly update the list without scanning it. The head of the linked list is referenced by the mmap field of the memory descriptor. Each memory region object stores the pointer to the next element of the list in the vm_next field. The head of the red-black tree is referenced by the mm_rb field of the memory descriptor. Each memory region object stores the color of the node, as well as the pointers to the parent, the left child, and the right child, in the vm_rb field of type rb_node. In general, the red-black tree is used to locate a region including a specific address, while the linked list is mostly useful when scanning the whole set of regions. 9.3.2. Memory Region Access Rights Before moving on, we should clarify the relation between a page and a memory region. As mentioned in Chapter 2, we use the term "page" to refer both to a set of linear addresses and to the data contained in this group of addresses. In particular, we denote the linear address interval ranging between 0 and 4,095 as page 0, the linear address interval ranging between 4,096 and 8,191 as page 1, and so forth. Each memory region therefore consists of a set of pages that have consecutive page numbers. We have already discussed two kinds of flags associated with a page: A few flags such as Read/Write, Present, or User/Supervisor stored in each Page Table entry (see the section "Regular Paging" in Chapter 2). A set of flags stored in the flags field of each page descriptor (see the section "Page Frame Management" in Chapter 8). The first kind of flag is used by the 80 x 86 hardware to check whether the requested kind of addressing can be performed; the second kind is used by Linux for many different purposes (see Table 8-2). We now introduce a third kind of flag: those associated with the pages of a memory region. They are stored in the vm_flags field of the vm_area_struct descriptor (see Table 9-5). Some flags offer the kernel information about all the pages of the memory region, such as what they contain and what rights the process has to access each page. Other flags describe the region itself, such as how it can grow. Table 9-5. The memory region flags Flag name VM_READ VM_WRITE VM_EXEC VM_SHARED VM_MAYREAD VM_MAYWRITE VM_MAYEXEC VM_MAYSHARE VM_GROWSDOWN VM_GROWSUP VM_SHM VM_DENYWRITE Description Pages can be read Pages can be written Pages can be executed Pages can be shared by several processes VM_READ flag may be set VM_WRITE flag may be set VM_EXEC flag may be set VM_SHARE flag may be set The region can expand toward lower addresses The region can expand toward higher addresses The region is used for IPC's shared memory The region maps a file that cannot be opened for writing Pages in the region are locked and cannot be swapped out The region maps the I/O address space of a device The application accesses the pages sequentially The application accesses the pages in a truly random order Do not copy the region when forking a new process The region is special (for instance, it maps the I/O address space of a device), so its pages must not be swapped out Check whether there is enough free memory for the mapping when creating an IPC shared memory region (see Chapter 19) The pages in the region are handled through the extended paging mechanism (see the section "Extended Paging" in Chapter 2) The region implements a non-linear file mapping VM_EXECUTABLE The region maps an executable file VM_LOCKED VM_IO VM_SEQ_READ VM_RAND_READ VM_DONTCOPY VM_DONTEXPAND Forbid region expansion through mremap( ) system call VM_RESERVED VM_ACCOUNT VM_HUGETLB VM_NONLINEAR Page access rights included in a memory region descriptor may be combined arbitrarily. It is possible, for instance, to allow the pages of a region to be read but not executed. To implement this protection scheme efficiently, the Read, Write, and Execute access rights associated with the pages of a memory region must be duplicated in all the corresponding Page Table entries, so that checks can be directly performed by the Paging Unit circuitry. In other words, the page access rights dictate what kinds of access should generate a Page Fault exception. As we'll see shortly, the job of figuring out what caused the Page Fault is delegated by Linux to the Page Fault handler, which implements several page-handling strategies. The initial values of the Page Table flags (which must be the same for all pages in the memory region, as we have seen) are stored in the vm_ page_ prot field of the vm_area_struct descriptor. When adding a page, the kernel sets the flags in the corresponding Page Table entry according to the value of the vm_ page_ prot field. However, translating the memory region's access rights into the page protection bits is not straightforward for the following reasons: In some cases, a page access should generate a Page Fault exception even when its access type is granted by the page access rights specified in the vm_flags field of the corresponding memory region. For instance, as we'll see in the section "Copy On Write" later in this chapter, the kernel may wish to store two identical, writable private pages (whose VM_SHARE flags are cleared) belonging to two different processes in the same page frame; in this case, an exception should be generated when either one of the processes tries to modify the page. As mentioned in Chapter 2, 80 x 86 processors's Page Tables have just two protection bits, namely the Read/Write and User/Supervisor flags. Moreover, the User/Supervisor flag of every page included in a memory region must always be set, because the page must always be accessible by User Mode processes. Recent Intel Pentium 4 microprocessors with PAE enabled sport a NX (No eXecute) flag in each 64-bit Page Table entry. If the kernel has been compiled without support for PAE, Linux adopts the following rules, which overcome the hardware limitation of the 80 x 86 microprocessors: The Read access right always implies the Execute access right, and vice versa. The Write access right always implies the Read access right. Conversely, if the kernel has been compiled with support for PAE and the CPU has the NX flag, Linux adopts different rules: The Execute access right always implies the Read access right. The Write access right always implies the Read access right. Moreover, to correctly defer the allocation of page frames through the "Copy On Write" technique (see later in this chapter), the page frame is write-protected whenever the corresponding page must not be shared by several processes. Therefore, the 16 possible combinations of the Read, Write, Execute, and Share access rights are scaled down according to the following rules: If the page has both Write and Share access rights, the Read/Write bit is set. If the page has the Read or Execute access right but does not have either the Write or the Share access right, the Read/Write bit is cleared. If the NX bit is supported and the page does not have the Execute access right, the NX bit is set. If the page does not have any access rights, the Present bit is cleared so that each access generates a Page Fault exception. However, to distinguish this condition from the real page-not-present case, Linux also sets the Page size bit to 1.[*] [*] You might consider this use of the Page size bit to be a dirty trick, because the bit was meant to indicate the real page size. But Linux can get away with the deception because the 80 x 86 chip checks the Page size bit in Page Directory entries, but not in Page Table entries. The downscaled protection bits corresponding to each combination of access rights are stored in the 16 elements of the protection_map array. 9.3.3. Memory Region Handling Having the basic understanding of data structures and state information that control memory handling , we can look at a group of low-level functions that operate on memory region descriptors. They should be considered auxiliary functions that simplify the implementation of do_mmap( ) and do_munmap( ). Those two functions, which are described in the sections "Allocating a Linear Address Interval" and "Releasing a Linear Address Interval" later in this chapter, enlarge and shrink the address space of a process, respectively. Working at a higher level than the functions we consider here, they do not receive a memory region descriptor as their parameter, but rather the initial address, the length, and the access rights of a linear address interval. 9.3.3.1. Finding the closest region to a given address: find_vma( ) The find_vma( ) function acts on two parameters: the address mm of a process memory descriptor and a linear address addr. It locates the first memory region whose vm_end field is greater than addr and returns the address of its descriptor; if no such region exists, it returns a NULL pointer. Notice that the region selected by find_vma( ) does not necessarily include addr because addr may lie outside of any memory region. Each memory descriptor includes an mmap_cache field that stores the descriptor address of the region that was last referenced by the process. This additional field is introduced to reduce the time spent in looking for the region that contains a given linear address. Locality of address references in programs makes it highly likely that if the last linear address checked belonged to a given region, the next one to be checked belongs to the same region. The function thus starts by checking whether the region identified by mmap_cache includes addr. If so, it returns the region descriptor pointer: vma = mm->mmap_cache; if (vma && vma->vm_end > addr && vma->vm_start <= addr) return vma; Otherwise, the memory regions of the process must be scanned, and the function looks up the memory region in the red-black tree: rb_node = mm->mm_rb.rb_node; vma = NULL; while (rb_node) { vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb); if (vma_tmp->vm_end > addr) { vma = vma_tmp; if (vma_tmp->vm_start <= addr) break; rb_node = rb_node->rb_left; } else rb_node = rb_node->rb_right; } if (vma) mm->mmap_cache = vma; return vma; The function uses the rb_entry macro, which derives from a pointer to a node of the redblack tree the address of the corresponding memory region descriptor. The find_vma_prev( ) function is similar to find_vma( ), except that it writes in an additional pprev parameter a pointer to the descriptor of the memory region that precedes the one selected by the function. Finally, the find_vma_prepare( ) function locates the position of the new leaf in the redblack tree that corresponds to a given linear address and returns the addresses of the preceding memory region and of the parent node of the leaf to be inserted. 9.3.3.2. Finding a region that overlaps a given interval: find_vma_intersection( ) The find_vma_intersection( ) function finds the first memory region that overlaps a given linear address interval; the mm parameter points to the memory descriptor of the process, while the start_addr and end_addr linear addresses specify the interval: vma = find_vma(mm,start_addr); if (vma && end_addr <= vma->vm_start) vma = NULL; return vma; The function returns a NULL pointer if no such region exists. To be exact, if find_vma( ) returns a valid address but the memory region found starts after the end of the linear address interval, vma is set to NULL. 9.3.3.3. Finding a free interval: get_unmapped_area( ) The get_unmapped_area( ) function searches the process address space to find an available linear address interval. The len parameter specifies the interval length, while a non-null addr parameter specifies the address from which the search must be started. If the search is successful, the function returns the initial address of the new interval; otherwise, it returns the error code -ENOMEM. If the addr parameter is not NULL, the function checks that the specified address is in the User Mode address space and that it is aligned to a page boundary. Next, the function invokes either one of two methods, depending on whether the linear address interval should be used for a file memory mapping or for an anonymous memory mapping. In the former case, the function executes the get_unmapped_area file operation; this is discussed in Chapter 16. In the latter case, the function executes the get_unmapped_area method of the memory descriptor. In turn, this method is implemented by either the arch_get_unmapped_area( ) function, or the arch_get_unmapped_area_topdown( ) function, according to the memory region layout of the process. As we'll see in the section "Program Segments and Process Memory Regions" in Chapter 20, every process can have two different layouts for the memory regions allocated through the mmap( ) system call: either they start from the linear address 0x40000000 and grow towards higher addresses, or they start right above the User Mode stack and grow towards lower addresses. Let us discuss the arch_get_unmapped_area( ) function, which is used when the memory regions are allocated moving from lower addresses to higher ones. It is essentially equivalent to the following code fragment: if (len > TASK_SIZE) return -ENOMEM; addr = (addr + 0xfff) & 0xfffff000; if (addr && addr + len <= TASK_SIZE) { vma = find_vma(current->mm, addr); if (!vma || addr + len <= vma->vm_start) return addr; } start_addr = addr = mm->free_area_cache; for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) { if (addr + len > TASK_SIZE) { if (start_addr == (TASK_SIZE/3+0xfff)&0xfffff000) return -ENOMEM; start_addr = addr = (TASK_SIZE/3+0xfff)&0xfffff000; vma = find_vma(current->mm, addr); } if (!vma || addr + len <= vma->vm_start) { mm->free_area_cache = addr + len; return addr; } addr = vma->vm_end; } The function starts by checking to make sure the interval length is within TASK_SIZE, the limit imposed on User Mode linear addresses (usually 3 GB). If addr is different from zero, the function tries to allocate the interval starting from addr. To be on the safe side, the function rounds up the value of addr to a multiple of 4 KB. If addr is 0 or the previous search failed, the arch_get_unmapped_area( ) function scans the User Mode linear address space looking for a range of linear addresses not included in any memory region and large enough to contain the new region. To speed up the search, the search's starting point is usually set to the linear address following the last allocated memory region. The mm->free_area_cache field of the memory descriptor is initialized to onethird of the User Mode linear address spaceusually, 1 GBand then updated as new memory regions are created. If the function fails in finding a suitable range of linear addresses, the search restarts from the beginningthat is, from one-third of the User Mode linear address space: in fact, the first third of the User Mode linear address space is reserved for memory regions having a predefined starting linear address, typically the text, data, and bss segments of an executable file (see Chapter 20). The function invokes find_vma( ) to locate the first memory region ending after the search's starting point, then repeatedly considers all the following memory regions. Three cases may occur: The requested interval is larger than the portion of linear address space yet to be scanned (addr + len > TASK_SIZE): in this case, the function either restarts from one-third of the User Mode address space or, if the second search has already been done, returns -ENOMEM (there are not enough linear addresses to satisfy the request). The hole following the last scanned region is not large enough (vma != NULL && vma>vm_start < addr + len). In this case, the function considers the next region. If neither one of the preceding conditions holds, a large enough hole has been found. In this case, the function returns addr. 9.3.3.4. Inserting a region in the memory descriptor list: insert_vm_struct( ) insert_vm_struct( ) inserts a vm_area_struct structure in the memory region object list and red-black tree of a memory descriptor. It uses two parameters: mm, which specifies the address of a process memory descriptor, and vma, which specifies the address of the vm_area_struct object to be inserted. The vm_start and vm_end fields of the memory region object must have already been initialized. The function invokes the find_vma_prepare( ) function to look up the position in the red-black tree mm->mm_rb where vma should go. Then insert_vm_struct( ) invokes the vma_link( ) function, which in turn: 1. Inserts the memory region in the linked list referenced by mm->mmap. 2. Inserts the memory region in the red-black tree mm->mm_rb. 3. If the memory region is anonymous, inserts the region in the list headed at the corresponding anon_vma data structure (see the section "Reverse Mapping for Anonymous Pages" in Chapter 17). 4. Increases the mm->map_count counter. If the region contains a memory-mapped file, the vma_link( ) function performs additional tasks that are described in Chapter 17. The _ _vma_unlink( ) function receives as its parameters a memory descriptor address mm and two memory region object addresses vma and prev. Both memory regions should belong to mm, and prev should precede vma in the memory region ordering. The function removes vma from the linked list and the red-black tree of the memory descriptor. It also updates mm>mmap_cache, which stores the last referenced memory region, if this field points to the memory region just deleted. 9.3.4. Allocating a Linear Address Interval Now let's discuss how new linear address intervals are allocated. To do this, the do_mmap( ) function creates and initializes a new memory region for the current process. However, after a successful allocation, the memory region could be merged with other memory regions defined for the process. The function uses the following parameters: file and offset File object pointer file and file offset offset are used if the new memory region will map a file into memory. This topic is discussed in Chapter 16. In this section, we assume that no memory mapping is required and that file and offset are both NULL. addr This linear address specifies where the search for a free interval must start. len The length of the linear address interval. prot This parameter specifies the access rights of the pages included in the memory region. Possible flags are PROT_READ, PROT_WRITE, PROT_EXEC, and PROT_NONE. The first three flags mean the same things as the VM_READ, VM_WRITE, and VM_EXEC flags. PROT_NONE indicates that the process has none of those access rights. flag This parameter specifies the remaining memory region flags: MAP_GROWSDOWN, MAP_LOCKED, MAP_DENYWRITE, and MAP_EXECUTABLE Their meanings are identical to those of the flags listed in Table 9-5. MAP_SHARED and MAP_PRIVATE The former flag specifies that the pages in the memory region can be shared among several processes; the latter flag has the opposite effect. Both flags refer to the VM_SHARED flag in the vm_area_struct descriptor. MAP_FIXED The initial linear address of the interval must be exactly the one specified in the addr parameter. MAP_ANONYMOUS No file is associated with the memory region (see Chapter 16). MAP_NORESERVE The function doesn't have to do a preliminary check on the number of free page frames. MAP_POPULATE The function should pre-allocate the page frames required for the mapping established by the memory region. This flag is significant only for memory regions that map files (see Chapter 16) and for IPC shared memory regions (see Chapter 19). MAP_NONBLOCK Significant only when the MAP_POPULATE flag is set: when pre-allocating the page frames, the function must not block. The do_mmap( ) function performs some preliminary checks on the value of offset and then executes the do_mmap_pgoff( ) function. In this chapter we will suppose that the new interval of linear address does not map a file on diskfile memory mapping is discussed in detail in Chapter 16. Here is a description of the do_mmap_pgoff( ) function for anonymous memory regions: 1. Checks whether the parameter values are correct and whether the request can be satisfied. In particular, it checks for the following conditions that prevent it from satisfying the request: o The linear address interval has zero length or includes addresses greater than TASK_SIZE. o The process has already mapped too many memory regionsthat is, the value of the map_count field of its mm memory descriptor exceeds the allowed maximum value. o The flag parameter specifies that the pages of the new linear address interval must be locked in RAM, but the process is not allowed to create locked memory regions, or the number of pages locked by the process exceeds the threshold stored in the signal->rlim[RLIMIT_MEMLOCK].rlim_cur field of the process descriptor. If any of the preceding conditions holds, do_mmap_pgoff( ) terminates by returning a negative value. If the linear address interval has a zero length, the function returns without performing any action. 2. Invokes get_unmapped_area( ) to obtain a linear address interval for the new region (see the previous section "Memory Region Handling"). 3. Computes the flags of the new memory region by combining the values stored in the prot and flags parameters: 4. vm_flags = calc_vm_prot_bits(prot,flags) | 5. calc_vm_flag_bits(prot,flags) | 6. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 7. if (flags & MAP_SHARED) vm_flags |= VM_SHARED | VM_MAYSHARE; The calc_vm_prot_bits( ) function sets the VM_READ, VM_WRITE, and VM_EXEC flags in vm_flags only if the corresponding PROT_READ, PROT_WRITE, and PROT_EXEC flags in prot are set. The calc_vm_flag_bits( ) function sets the VM_GROWSDOWN, VM_DENYWRITE, VM_EXECUTABLE, and VM_LOCKED flags in vm_flags only if the corresponding MAP_GROWSDOWN, MAP_DENYWRITE, MAP_EXECUTABLE, and MAP_LOCKED flags in flags are set. A few other flags are set in vm_flags: VM_MAYREAD, VM_MAYWRITE, VM_MAYEXEC, the default flags for all memory regions in mm->def_flags,[*] and both VM_SHARED and VM_MAYSHARE if the pages of the memory region have to be shared with other processes. [*] Actually, the def_flags field of the memory descriptor is modified only by the mlockall( ) system call, which can be used to set the VM_LOCKED flag, thus locking all future pages of the calling process in RAM. 8. Invokes find_vma_prepare( ) to locate the object of the memory region that shall precede the new interval, as well as the position of the new region in the red-black tree: 9. for (;;) { 10. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent); 11. if (!vma || vma->vm_start >= addr + len) 12. break; 13. if (do_munmap(mm, addr, len)) 14. return -ENOMEM; } The find_vma_prepare( ) function also checks whether a memory region that overlaps the new interval already exists. This occurs when the function returns a non-NULL address pointing to a region that starts before the end of the new interval. In this case, do_mmap_pgoff( ) invokes do_munmap( ) to remove the new interval and then repeats the whole step (see the later section "Releasing a Linear Address Interval"). 15. Checks whether inserting the new memory region causes the size of the process address space (mm->total_vm<<PAGE_SHIFT)+len to exceed the threshold stored in the signal->rlim[RLIMIT_AS].rlim_cur field of the process descriptor. If so, it returns the error code -ENOMEM. Notice that the check is done here and not in step 1 with the other checks, because some memory regions could have been removed in step 4. 16. Returns the error code -ENOMEM if the MAP_NORESERVE flag was not set in the flags parameter, the new memory region contains private writable pages, and there are not enough free page frames; this last check is performed by the security_vm_enough_memory( ) function. 17. If the new interval is private (VM_SHARED not set) and it does not map a file on disk, it invokes vma_merge( ) to check whether the preceding memory region can be expanded in such a way to include the new interval. Of course, the preceding memory region must have exactly the same flags as those memory regions stored in the vm_flags local variable. If the preceding memory region can be expanded, vma_merge( ) also tries to merge it with the following memory region (this occurs when the new interval fills the hole between two memory regions and all three have the same flags). In case it succeeds in expanding the preceding memory region, the function jumps to step 12. 18. Allocates a vm_area_struct data structure for the new memory region by invoking the kmem_cache_alloc( ) slab allocator function. 19. Initializes the new memory region object (pointed to by vma): 20. vma->vm_mm = mm; 21. vma->vm_start = addr; 22. vma->vm_end = addr + len; 23. vma->vm_flags = vm_flags; 24. vma->vm_page_prot = protection_map[vm_flags & 0x0f]; 25. vma->vm_ops = NULL; 26. vma->vm_pgoff = pgoff; 27. vma->vm_file = NULL; 28. vma->vm_private_data = NULL; 29. vma->vm_next = NULL; INIT_LIST_HEAD(&vma->shared); 30. If the MAP_SHARED flag is set (and the new memory region doesn't map a file on disk), the region is a shared anonymous region: invokes shmem_zero_setup( ) to initialize it. Shared anonymous regions are mainly used for interprocess communications; see the section "IPC Shared Memory" in Chapter 19. 31. Invokes vma_link( ) to insert the new region in the memory region list and red-black tree (see the earlier section "Memory Region Handling"). 32. Increases the size of the process address space stored in the total_vm field of the memory descriptor. 33. If the VM_LOCKED flag is set, it invokes make_pages_present( ) to allocate all the pages of the memory region in succession and lock them in RAM: 34. if (vm_flags & VM_LOCKED) { 35. mm->locked_vm += len >> PAGE_SHIFT; 36. make_pages_present(addr, addr + len); 37. } The make_pages_present( ) function, in turn, invokes get_user_pages( ) as follows: write = (vma->vm_flags & VM_WRITE) != 0; get_user_pages(current, current->mm, addr, len, write, 0, NULL, NULL); The get_user_pages( ) function cycles through all starting linear addresses of the pages between addr and addr+len; for each of them, it invokes follow_page( ) to check whether there is a mapping to a physical page in the current's Page Tables. If no such physical page exists, get_user_pages( ) invokes handle_mm_fault( ), which, as we'll see in the section "Handling a Faulty Address Inside the Address Space," allocates one page frame and sets its Page Table entry according to the vm_flags field of the memory region descriptor. 38. Finally, it terminates by returning the linear address of the new memory region. 9.3.5. Releasing a Linear Address Interval When the kernel must delete a linear address interval from the address space of the current process, it uses the do_munmap( ) function. The parameters are: the address mm of the process's memory descriptor, the starting address start of the interval, and its length len. The interval to be deleted does not usually correspond to a memory region; it may be included in one memory region or span two or more regions. 9.3.5.1. The do_munmap( ) function The function goes through two main phases. In the first phase (steps 16), it scans the list of memory regions owned by the process and unlinks all regions included in the linear address interval from the process address space. In the second phase (steps 712), the function updates the process Page Tables and removes the memory regions identified in the first phase. The function makes use of the split_vma( ) and unmap_region( ) functions, which will be described later. do_munmap( ) executes the following steps: 1. Performs some preliminary checks on the parameter values. If the linear address interval includes addresses greater than TASK_SIZE, if start is not a multiple of 4,096, or if the linear address interval has a zero length, the function returns the error code -EINVAL. 2. Locates the first memory region mpnt that ends after the linear address interval to be deleted (mpnt->end > start), if any: mpnt = find_vma_prev(mm, start, &prev); 3. If there is no such memory region, or if the region does not overlap with the linear address interval, nothing has to be done because there is no memory region in the interval: 4. end = start + len; 5. if (!mpnt || mpnt->vm_start >= end) 6. return 0; 7. If the linear address interval starts inside the mpnt memory region, it invokes split_vma( ) (described below) to split the mpnt memory region into two smaller regions: one outside the interval and the other inside the interval: 8. if (start > mpnt->vm_start) { 9. if (split_vma(mm, mpnt, start, 0)) 10. return -ENOMEM; 11. prev = mpnt; } The prev local variable, which previously stored the pointer to the memory region preceding mpnt, is updated so that it points to mpntthat is, to the new memory region lying outside the linear address interval. In this way, prev still points to the memory region preceding the first memory region to be removed. 12. If the linear address interval ends inside a memory region, it invokes split_vma( ) once again to split the last overlapping memory region into two smaller regions: one inside the interval and the other outside the interval:[*] [*] If the linear address interval is properly contained inside a memory region, the region must be replaced by two new smaller regions. When this case occurs, step 4 and step 5 break the memory region in three smaller regions: the middle region is destroyed, while the first and the last ones will be preserved. last = find_vma(mm, end); if (last && end > last->vm_start)){ if (split_vma(mm, last, start, end, 1)) return -ENOMEM; } 13. Updates the value of mpnt so that it points to the first memory region in the linear address interval. If prev is NULLthat is, there is no preceding memory regionthe address of the first memory region is taken from mm->mmap: mpnt = prev ? prev->vm_next : mm->mmap; 14. Invokes detach_vmas_to_be_unmapped( ) to remove the memory regions included in the linear address interval from the process's linear address space. This function essentially executes the following code: 15. vma = mpnt; 16. insertion_point = (prev ? &prev->vm_next : &mm->mmap); 17. do { 18. rb_erase(&vma->vm_rb, &mm->mm_rb); 19. mm->map_count--; 20. tail_vma = vma; 21. vma = vma->next; 22. } while (vma && vma->start < end); 23. *insertion_point = vma; 24. tail_vma->vm_next = NULL; mm->map_cache = NULL; The descriptors of the regions to be removed are stored in an ordered list, whose head is pointed to by the mpnt local variable (actually, this list is just a fragment of the original process's list of memory regions). 25. Gets the mm->page_table_lock spin lock. 26. Invokes unmap_region( ) to clear the Page Table entries covering the linear address interval and to free the corresponding page frames (discussed later): unmap_region(mm, mpnt, prev, start, end); 27. Releases the mm->page_table_lock spin lock. 28. Releases the descriptors of the memory regions collected in the list built in step 7: 29. do { 30. struct vm_area_struct * next = mpnt->vm_next; 31. unmap_vma(mm, mpnt); 32. mpnt = next; } while (mpnt != NULL); The unmap_vma( ) function is invoked on every memory region in the list; it essentially executes the following steps: a. Updates the mm->total_vm and mm->locked_vm fields. b. Executes the mm->unmap_area method of the memory descriptor. This method is implemented either by arch_unmap_area( ) or by arch_unmap_area_topdown( ), according to the memory region layout of the process (see the earlier section "Memory Region Handling"). In both cases, the mm->free_area_cache field is updated, if needed. c. Invokes the close method of the memory region, if defined. d. If the memory region is anonymous, the function removes it from the anonymous memory region list headed at mm->anon_vma. e. Invokes kmem_cache_free( ) to release the memory region descriptor. 33. Returns 0 (success). 9.3.5.2. The split_vma( ) function The purpose of the split_vma( ) function is to split a memory region that intersects a linear address interval into two smaller regions, one outside of the interval and the other inside. The function receives four parameters: a memory descriptor pointer mm, a memory area descriptor pointer vma that identifies the region to be split, an address addr that specifies the intersection point between the interval and the memory region, and a flag new_below that specifies whether the intersection occurs at the beginning or at the end of the interval. The function performs the following basic steps: 1. Invokes kmem_cache_alloc( ) to get an additional vm_area_struct descriptor, and stores its address in the new local variable. If no free memory is available, it returns ENOMEM. 2. Initializes the fields of the new descriptor with the contents of the fields of the vma descriptor. 3. If the new_below flag is 0, the linear address interval starts inside the vma region, so the new region must be placed after the vma region. Thus, the function sets both the new->vm_start and the vma->vm_end fields to addr. 4. Conversely, if the new_below flag is equal to 1, the linear address interval ends inside the vma region, so the new region must be placed before the vma region. Thus, the function sets both the new->vm_end and the vma->vm_start fields to addr. 5. If the open method of the new memory region is defined, the function executes it. 6. Links the new memory region descriptor to the mm->mmap list of memory regions and to the mm->mm_rb red-black tree. Moreover, the function adjusts the red-black tree to take care of the new size of the memory region vma. 7. Returns 0 (success). 9.3.5.3. The unmap_region( ) function The unmap_region( ) function walks through a list of memory regions and releases the page frames belonging to them. It acts on five parameters: a memory descriptor pointer mm, a pointer vma to the descriptor of the first memory region being removed, a pointer prev to the memory region preceding vma in the process's list (see steps 2 and 4 in do_munmap()), and two addresses start and end that delimit the linear address interval being removed. The function essentially executes the following steps: 1. Invokes lru_add_drain( ) (see Chapter 17). 2. Invokes the tlb_gather_mmu( ) function to initialize a per-CPU variable named mmu_gathers. The contents of mmu_gathers are architecture-dependent: generally speaking, the variable should store all information required for a successful updating of the page table entries of a process. In the 80 x 86 architecture, the tlb_gather_mmu( ) function simply saves the value of the mm memory descriptor pointer in the mmu_gathers variable of the local CPU. 3. Stores the address of the mmu_gathers variable in the tlb local variable. 4. Invokes unmap_vmas( ) to scan all Page Table entries belonging to the linear address interval: if only one CPU is available, the function invokes free_swap_and_cache( ) repeatedly to release the corresponding pages (see Chapter 17); otherwise, the function saves the pointers of the corresponding page descriptors in the mmu_gathers local variable. 5. Invokes free_pgtables(tlb,prev,start,end) to try to reclaim the Page Tables of the process that have been emptied in the previous step. 6. Invokes tlb_finish_mmu(tlb,start,end) to finish the work: in turn, this function: a. Invokes flush_tlb_mm( ) to flush the TLB (see the section "Handling the Hardware Cache and the TLB" in Chapter 2). b. In multiprocessor system, invokes free_pages_and_swap_cache( ) to release the page frames whose pointers have been collected in the mmu_gather data structure. This function is described in Chapter 17. 9.4. Page Fault Exception Handler As stated previously, the Linux Page Fault exception handler must distinguish exceptions caused by programming errors from those caused by a reference to a page that legitimately belongs to the process address space but simply hasn't been allocated yet. The memory region descriptors allow the exception handler to perform its job quite efficiently. The do_page_fault( ) function, which is the Page Fault interrupt service routine for the 80 x 86 architecture, compares the linear address that caused the Page Fault against the memory regions of the current process; it can thus determine the proper way to handle the exception according to the scheme that is illustrated in Figure 9-4. Figure 9-4. Overall scheme for the Page Fault handler In practice, things are a lot more complex because the Page Fault handler must recognize several particular subcases that fit awkwardly into the overall scheme, and it must distinguish several kinds of legal access. A detailed flow diagram of the handler is illustrated in Figure 9-5. The identifiers vmalloc_fault, good_area, bad_area, and no_context are labels appearing in do_page_fault( ) that should help you to relate the blocks of the flow diagram to specific lines of code. The do_ page_fault( ) function accepts the following input parameters: The regs address of a pt_regs structure containing the values of the microprocessor registers when the exception occurred. A 3-bit error_code, which is pushed on the stack by the control unit when the exception occurred (see "Hardware Handling of Interrupts and Exceptions" in Chapter 4). The bits have the following meanings: o If bit 0 is clear, the exception was caused by an access to a page that is not present (the Present flag in the Page Table entry is clear); otherwise, if bit 0 is set, the exception was caused by an invalid access right. Figure 9-5. The flow diagram of the Page Fault handler o o If bit 1 is clear, the exception was caused by a read or execute access; if set, the exception was caused by a write access. If bit 2 is clear, the exception occurred while the processor was in Kernel Mode; otherwise, it occurred in User Mode. The first operation of do_ page_fault( ) consists of reading the linear address that caused the Page Fault. When the exception occurs, the CPU control unit stores that value in the cr2 control register: asm("movl %%cr2,%0":"=r" (address)); if (regs->eflags & 0x00020200) local_irq_enable( ); tsk = current; The linear address is saved in the address local variable. The function also ensures that local interrupts are enabled if they were enabled before the fault or the CPU was running in virtual-8086 mode, and saves the pointers to the process descriptor of current in the tsk local variable. As shown at the top of Figure 9-5, do_ page_fault( ) checks whether the faulty linear address belongs to the fourth gigabyte: info.si_code = SEGV_MAPERR; if (address >= TASK_SIZE ) { if (!(error_code & 0x101)) goto vmalloc_fault; goto bad_area_nosemaphore; } If the exception was caused by the kernel trying to access a nonexisting page frame, a jump is made to the code at label vmalloc_fault, which takes care of faults that were likely caused by accessing a noncontiguous memory area in Kernel Mode; we describe this case in the later section "Handling Noncontiguous Memory Area Accesses." Otherwise, a jump is made to the code at the bad_area_nosemaphore label, described in the later section "Handling a Faulty Address Outside the Address Space." Next, the handler checks whether the exception occurred while the kernel was executing some critical routine or running a kernel thread (remember that the mm field of the process descriptor is always NULL for kernel threads ): if (in_atomic( ) || !tsk->mm) goto bad_area_nosemaphore; The in_atomic( ) macro yields the value one if the fault occurred while either one of the following conditions holds: The kernel was executing an interrupt handler or a deferrable function. The kernel was executing a critical region with kernel preemption disabled (see the section "Kernel Preemption" in Chapter 5). If the Page Fault did occur in an interrupt handler, in a deferrable function, in a critical region, or in a kernel thread, do_ page_fault( ) does not try to compare the linear address with the memory regions of current. Kernel threads never use linear addresses below TASK_SIZE. Similarly, interrupt handlers, deferrable functions, and code of critical regions should not use linear addresses below TASK_SIZE because this might block the current process. (See the section "Handling a Faulty Address Outside the Address Space" later in this chapter for information on the info local variable and a description of the code at the bad_area_nosemaphore label.) Let's suppose that the Page Fault did not occur in an interrupt handler, in a deferrable function, in a critical region, or in a kernel thread. Then the function must inspect the memory regions owned by the process to determine whether the faulty linear address is included in the process address space. In order to this, it must acquire the mmap_sem read/write semaphore of the process: if (!down_read_trylock(&tsk->mm>mmap_sem)) { if ((error_code & 4) == 0 && !search_exception_table(regs->eip)) goto bad_area_nosemaphore; down_read(&tsk->mm->mmap_sem); } If kernel bugs and hardware malfunctioning can be ruled out, the current process has not already acquired the mmap_sem semaphore for writing when the Page Fault occurs. However, do_page_fault( ) wants to be sure that this is actually true, because otherwise a deadlock would occur. For that reason, the function makes use of down_read_trylock( ) instead of down_read( ) (see the section "Read/Write Semaphores" in Chapter 5). If the semaphore is closed and the Page Fault occurred in Kernel Mode, do_page_fault( ) determines whether the exception occurred while using some linear address that has been passed to the kernel as a parameter of a system call (see the next section "Handling a Faulty Address Outside the Address Space"). In this case, do_page_fault( ) knows for sure that the semaphore is owned by another processbecause every system call service routine carefully avoids acquiring the mmap_sem semaphore for writing before accessing the User Mode address spaceso the function waits until the semaphore is released. Otherwise, the Page Fault is due to a kernel bug or to a serious hardware problem, so the function jumps to the bad_area_nosemaphore label. Let's assume that the mmap_sem semaphore has been safely acquired for reading. Now do_page_fault( ) looks for a memory region containing the faulty linear address: vma = find_vma(tsk->mm, address); if (!vma) goto bad_area; if (vma->vm_start <= address) goto good_area; If vma is NULL, there is no memory region ending after address, and thus the faulty address is certainly bad. On the other hand, if the first memory region ending after address includes address, the function jumps to the code at label good_area. If none of the two "if" conditions are satisfied, the function has determined that address is not included in any memory region; however, it must perform an additional check, because the faulty address may have been caused by a push or pusha instruction on the User Mode stack of the process. Let's make a short digression to explain how stacks are mapped into memory regions. Each region that contains a stack expands toward lower addresses; its VM_GROWSDOWN flag is set, so the value of its vm_end field remains fixed while the value of its vm_start field may be decreased. The region boundaries include, but do not delimit precisely, the current size of the User Mode stack. The reasons for the fuzz factor are: The region size is a multiple of 4 KB (it must include complete pages) while the stack size is arbitrary. Page frames assigned to a region are never released until the region is deleted; in particular, the value of the vm_start field of a region that includes a stack can only decrease; it can never increase. Even if the process executes a series of pop instructions, the region size remains unchanged. It should now be clear how a process that has filled up the last page frame allocated to its stack may cause a Page Fault exception: the push refers to an address outside of the region (and to a nonexistent page frame). Notice that this kind of exception is not caused by a programming error; thus it must be handled separately by the Page Fault handler. We now return to the description of do_ page_fault( ), which checks for the case described previously: if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; if (error_code & 4 /* User Mode */ && address + 32 < regs->esp) goto bad_area; if (expand_stack(vma, address)) goto bad_area; goto good_area; If the VM_GROWSDOWN flag of the region is set and the exception occurred in User Mode, the function checks whether address is smaller than the regs->esp stack pointer (it should be only a little smaller). Because a few stack-related assembly language instructions (such as pusha) perform a decrement of the esp register only after the memory access, a 32-byte tolerance interval is granted to the process. If the address is high enough (within the tolerance granted), the code invokes the expand_stack( ) function to check whether the process is allowed to extend both its stack and its address space; if everything is OK, it sets the vm_start field of vma to address and returns 0; otherwise, it returns -ENOMEM. Note that the preceding code skips the tolerance check whenever the VM_GROWSDOWN flag of the region is set and the exception did not occur in User Mode. These conditions mean that the kernel is addressing the User Mode stack and that the code should always run expand_stack( ). 9.4.1. Handling a Faulty Address Outside the Address Space If address does not belong to the process address space, do_page_fault( ) proceeds to execute the statements at the label bad_area. If the error occurred in User Mode, it sends a SIGSEGV signal to current (see the section "Generating a Signal" in Chapter 11) and terminates: bad_area: up_read(&tsk->mm->mmap_sem); bad_area_nosemaphore: if (error_code & 4) { /* User Mode */ tsk->thread.cr2 = address; tsk->thread.error_code = error_code | (address >= TASK_SIZE); tsk->thread.trap_no = 14; info.si_signo = SIGSEGV; info.si_errno = 0; info.si_addr = (void *) address; force_sig_info(SIGSEGV, &info, tsk); return; } The force_sig_info( ) function makes sure that the process does not ignore or block the SIGSEGV signal, and sends the signal to the User Mode process while passing some additional information in the info local variable (see the section "Generating a Signal" in Chapter 11). The info.si_code field is already set to SEGV_MAPERR (if the exception was due to a nonexisting page frame) or to SEGV_ACCERR (if the exception was due to an invalid access to an existing page frame). If the exception occurred in Kernel Mode (bit 2 of error_code is clear), there are still two alternatives: The exception occurred while using some linear address that has been passed to the kernel as a parameter of a system call. The exception is due to a real kernel bug. The function distinguishes these two alternatives as follows: no_context: if ((fixup = search_exception_table(regs->eip)) != 0) { regs->eip = fixup; return; } In the first case, it jumps to a "fixup code," which typically sends a SIGSEGV signal to current or terminates a system call handler with a proper error code (see the section "Dynamic Address Checking: The Fix-up Code" in Chapter 10). In the second case, the function prints a complete dump of the CPU registers and of the Kernel Mode stack both on the console and on a system message buffer; it then kills the current process by invoking the do_exit( ) function (see Chapter 20). This is the so-called "Kernel oops" error, named after the message displayed. The dumped values can be used by kernel hackers to reconstruct the conditions that triggered the bug, and thus find and correct it. 9.4.2. Handling a Faulty Address Inside the Address Space If address belongs to the process address space, do_ page_fault( ) proceeds to the statement labeled good_area: good_area: info.si_code = SEGV_ACCERR; write = 0; if (error_code & 2) { /* write access */ if (!(vma->vm_flags & VM_WRITE)) goto bad_area; write++; } else /* read access */ if ((error_code & 1) || !(vma->vm_flags & (VM_READ | VM_EXEC))) goto bad_area; If the exception was caused by a write access, the function checks whether the memory region is writable. If not, it jumps to the bad_area code; if so, it sets the write local variable to 1. If the exception was caused by a read or execute access, the function checks whether the page is already present in RAM. In this case, the exception occurred because the process tried to access a privileged page frame (one whose User/Supervisor flag is clear) in User Mode, so the function jumps to the bad_area code.[*] If the page is not present, the function also checks whether the memory region is readable or executable. [*] However, this case should never happen, because the kernel does not assign privileged page frames to the processes. If the memory region access rights match the access type that caused the exception, the handle_mm_fault( ) function is invoked to allocate a new page frame: survive: ret = handle_mm_fault(tsk->mm, vma, address, write); if (ret == VM_FAULT_MINOR || ret == VM_FAULT_MAJOR) { if (ret == VM_FAULT_MINOR) tsk->min_flt++; else tsk->maj_flt++; up_read(&tsk->mm->mmap_sem); return; } The handle_mm_fault( ) function returns VM_FAULT_MINOR or VM_FAULT_MAJOR if it succeeded in allocating a new page frame for the process. The value VM_FAULT_MINOR indicates that the Page Fault has been handled without blocking the current process; this kind of Page Fault is called minor fault. The value VM_FAULT_MAJOR indicates that the Page Fault forced the current process to sleep (most likely because time was spent while filling the page frame assigned to the process with data read from disk); a Page Fault that blocks the current process is called a major fault. The function can also return VM_FAULT_OOM (for not enough memory) or VM_FAULT_SIGBUS (for every other error). If handle_mm_fault( ) returns the value VM_FAULT_SIGBUS, a SIGBUS signal is sent to the process: if (ret == VM_FAULT_SIGBUS) { do_sigbus: up_read(&tsk->mm->mmap_sem); if (!(error_code & 4)) /* Kernel Mode */ goto no_context; tsk->thread.cr2 = address; tsk->thread.error_code = error_code; tsk->thread.trap_no = 14; info.si_signo = SIGBUS; info.si_errno = 0; info.si_code = BUS_ADRERR; info.si_addr = (void *) address; force_sig_info(SIGBUS, &info, tsk); } If handle_mm_fault( ) cannot allocate the new page frame, it returns the value VM_FAULT_OOM; in this case, the kernel usually kills the current process. However, if current is the init process, it is just put at the end of the run queue and the scheduler is invoked; once init resumes its execution, handle_mm_fault( ) is executed again: if (ret == VM_FAULT_OOM) { out_of_memory: up_read(&tsk->mm->mmap_sem); if (tsk->pid != 1) { if (error_code & 4) /* User Mode */ do_exit(SIGKILL); goto no_context; } yield(); down_read(&tsk->mm->mmap_sem); goto survive; } The handle_mm_fault( ) function acts on four parameters: mm A pointer to the memory descriptor of the process that was running on the CPU when the exception occurred vma A pointer to the descriptor of the memory region, including the linear address that caused the exception address The linear address that caused the exception write_access Set to 1 if tsk attempted to write in address and to 0 if tsk attempted to read or execute it The function starts by checking whether the Page Middle Directory and the Page Table used to map address exist. Even if address belongs to the process address space, the corresponding Page Tables might not have been allocated, so the task of allocating them precedes everything else: pgd = pgd_offset(mm, address); spin_lock(&mm->page_table_lock); pud = pud_alloc(mm, pgd, address); if (pud) { pmd = pmd_alloc(mm, pud, address); if (pmd) { pte = pte_alloc_map(mm, pmd, address); if (pte) return handle_pte_fault(mm, vma, address, write_access, pte, pmd); } } spin_unlock(&mm->page_table_lock); return VM_FAULT_OOM; The pgd local variable contains the Page Global Directory entry that refers to address; pud_alloc( ) and pmd_alloc( ) are invoked to allocate, if needed, a new Page Upper Directory and a new Page Middle Directory, respectively.[*] pte_alloc_map( ) is then invoked to allocate, if needed, a new Page Table. If both operations are successful, the pte local variable points to the Page Table entry that refers to address. The handle_pte_fault( ) function is then invoked to inspect the Page Table entry corresponding to address and to determine how to allocate a new page frame for the process: [*] On 80 x 86 microprocessors, these allocations never occur, because the Page Upper Directories are always included in the Page Global Directory, and the Page Middle Directories are either included in the Page Upper Directory (PAE not enabled) or allocated together with the Page Upper Directory (PAE enabled). If the accessed page is not presentthat is, if it is not already stored in any page framethe kernel allocates a new page frame and initializes it properly; this technique is called demand paging . If the accessed page is present but is marked read-onlyi.e., if it is already stored in a page framethe kernel allocates a new page frame and initializes its contents by copying the old page frame data; this technique is called Copy On Write. 9.4.3. Demand Paging The term demand paging denotes a dynamic memory allocation technique that consists of deferring page frame allocation until the last possible momentuntil the process attempts to address a page that is not present in RAM, thus causing a Page Fault exception. The motivation behind demand paging is that processes do not address all the addresses included in their address space right from the start; in fact, some of these addresses may never be used by the process. Moreover, the program locality principle (see the section "Hardware Cache" in Chapter 2) ensures that, at each stage of program execution, only a small subset of the process pages are really referenced, and therefore the page frames containing the temporarily useless pages can be used by other processes. Demand paging is thus preferable to global allocation (assigning all page frames to the process right from the start and leaving them in memory until program termination), because it increases the average number of free page frames in the system and therefore allows better use of the available free memory. From another viewpoint, it allows the system as a whole to get better throughput with the same amount of RAM. The price to pay for all these good things is system overhead: each Page Fault exception induced by demand paging must be handled by the kernel, thus wasting CPU cycles. Fortunately, the locality principle ensures that once a process starts working with a group of pages, it sticks with them without addressing other pages for quite a while. Thus, Page Fault exceptions may be considered rare events. An addressed page may not be present in main memory either because the page was never accessed by the process, or because the corresponding page frame has been reclaimed by the kernel (see Chapter 17). In both cases, the page fault handler must assign a new page frame to the process. How this page frame is initialized, however, depends on the kind of page and on whether the page was previously accessed by the process. In particular: 1. Either the page was never accessed by the process and it does not map a disk file, or the page maps a disk file. The kernel can recognize these cases because the Page Table entry is filled with zerosi.e., the pte_none macro returns the value 1. 2. The page belongs to a non-linear disk file mapping (see the section "Non-Linear Memory Mappings" in Chapter 16). The kernel can recognize this case, because the Present flag is cleared and the Dirty flag is seti.e., the pte_file macro returns the value 1. 3. The page was already accessed by the process, but its content is temporarily saved on disk. The kernel can recognize this case because the Page Table entry is not filled with zeros, but the Present and Dirty flags are cleared. Thus, the handle_ pte_fault( ) function is able to distinguish the three cases by inspecting the Page Table entry that refers to address: entry = *pte; if (!pte_present(entry)) { if (pte_none(entry)) return do_no_page(mm, vma, address, write_access, pte, pmd); if (pte_file(entry)) return do_file_page(mm, vma, address, write_access, pte, pmd); return do_swap_page(mm, vma, address, pte, pmd, entry, write_access); } We'll examine cases 2 and 3 in Chapter 16 and in Chapter 17, respectively. In case 1, when the page was never accessed or the page linearly maps a disk file, the do_no_page( ) function is invoked. There are two ways to load the missing page, depending on whether the page is mapped to a disk file. The function determines this by checking the nopage method of the vma memory region object, which points to the function that loads the missing page from disk into RAM if the page is mapped to a file. Therefore, the possibilities are: The vma->vm_ops->nopage field is not NULL. In this case, the memory region maps a disk file and the field points to the function that loads the page. This case is covered in the section "Demand Paging for Memory Mapping" in Chapter 16 and in the section "IPC Shared Memory" in Chapter 19. Either the vma->vm_ops field or the vma->vm_ops->nopage field is NULL. In this case, the memory region does not map a file on diski.e., it is an anonymous mapping . Thus, do_no_ page( ) invokes the do_anonymous_page( ) function to get a new page frame: if (!vma->vm_ops || !vma->vm_ops->nopage) return do_anonymous_page(mm, vma, page_table, pmd, write_access, address); The do_anonymous_page( ) function[*] handles write and read requests separately: [*] To simplify the description of this function, we skip the statements that deal with reverse mapping, a topic that will be covered in the section "Reverse Mapping" in Chapter 17. if (write_access) { pte_unmap(page_table); spin_unlock(&mm->page_table_lock); page = alloc_page(GFP_HIGHUSER | _ _GFP_ZERO); spin_lock(&mm->page_table_lock); page_table = pte_offset_map(pmd, addr); mm->rss++; entry = maybe_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)), vma); lru_cache_add_active(page); SetPageReferenced(page); set_pte(page_table, entry); pte_unmap(page_table); spin_unlock(&mm->page_table_lock); return VM_FAULT_MINOR; } The first execution of the pte_unmap macro releases the temporary kernel mapping for the high-memory physical address of the Page Table entry established by pte_offset_map before invoking the handle_pte_fault( ) function (see Table 2-7 in the section "Page Table Handling" in Chapter 2). The following pair or pte_offset_map and pte_unmap macros acquires and releases the same temporary kernel mapping. The temporary kernel mapping has to be released before invoking alloc_page( ), because this function might block the current process. The function increases the rss field of the memory descriptor to keep track of the number of page frames allocated to the process. The Page Table entry is then set to the physical address of the page frame, which is marked as writable[ ] and dirty. The lru_cache_add_active( ) function inserts the new page frame in the swap-related data structures; we discuss it in Chapter 17. [ ] If a debugger attempts to write in a page belonging to a read-only memory region of the traced process, the kernel does not set the Read/Write flag. The maybe_mkwrite( ) function takes care of this special case. Conversely, when handling a read access, the content of the page is irrelevant because the process is addressing it for the first time. It is safer to give a page filled with zeros to the process rather than an old page filled with information written by some other process. Linux goes one step further in the spirit of demand paging. There is no need to assign a new page frame filled with zeros to the process right away, because we might as well give it an existing page called zero page , thus deferring further page frame allocation. The zero page is allocated statically during kernel initialization in the empty_zero_page variable (an array of 4,096 bytes filled with zeros). The Page Table entry is thus set with the physical address of the zero page: entry = pte_wrprotect(mk_pte(virt_to_page(empty_zero_page), vma->vm_page_prot)); set_pte(page_table, entry); spin_unlock(&mm->page_table_lock); return VM_FAULT_MINOR; Because the page is marked as nonwritable, if the process attempts to write in it, the Copy On Write mechanism is activated. Only then does the process get a page of its own to write in. The mechanism is described in the next section. 9.4.4. Copy On Write First-generation Unix systems implemented process creation in a rather clumsy way: when a fork( ) system call was issued, the kernel duplicated the whole parent address space in the literal sense of the word and assigned the copy to the child process. This activity was quite time consuming since it required: Allocating page frames for the Page Tables of the child process Allocating page frames for the pages of the child process Initializing the Page Tables of the child process Copying the pages of the parent process into the corresponding pages of the child process This way of creating an address space involved many memory accesses, used up many CPU cycles, and completely spoiled the cache contents. Last but not least, it was often pointless because many child processes start their execution by loading a new program, thus discarding entirely the inherited address space (see Chapter 20). Modern Unix kernels, including Linux, follow a more efficient approach called Copy On Write (COW ). The idea is quite simple: instead of duplicating page frames, they are shared between the parent and the child process. However, as long as they are shared, they cannot be modified. Whenever the parent or the child process attempts to write into a shared page frame, an exception occurs. At this point, the kernel duplicates the page into a new page frame that it marks as writable. The original page frame remains write-protected: when the other process tries to write into it, the kernel checks whether the writing process is the only owner of the page frame; in such a case, it makes the page frame writable for the process. The _count field of the page descriptor is used to keep track of the number of processes that are sharing the corresponding page frame. Whenever a process releases a page frame or a Copy On Write is executed on it, its _count field is decreased; the page frame is freed only when _count becomes -1 (see the section "Page Descriptors" in Chapter 8). Let's now describe how Linux implements COW. When handle_ pte_fault( ) determines that the Page Fault exception was caused by an access to a page present in memory, it executes the following instructions: if (pte_present(entry)) { if (write_access) { if (!pte_write(entry)) return do_wp_page(mm, vma, address, pte, pmd, entry); entry = pte_mkdirty(entry); } entry = pte_mkyoung(entry); set_pte(pte, entry); flush_tlb_page(vma, address); pte_unmap(pte); spin_unlock(&mm->page_table_lock); return VM_FAULT_MINOR; } The handle_pte_fault( ) function is architecture-independent: it considers each possible violation of the page access rights. However, in the 80 x 86 architecture, if the page is present, the access was for writing and the page frame is write-protected (see the earlier section "Handling a Faulty Address Inside the Address Space"). Thus, the do_wp_page( ) function is always invoked. The do_wp_page( ) function[*] starts by deriving the page descriptor of the page frame referenced by the Page Table entry involved in the Page Fault exception. Next, the function determines whether the page must really be duplicated. If only one process owns the page, Copy On Write does not apply, and the process should be free to write the page. Basically, the function reads the _count field of the page descriptor: if it is equal to 0 (a single owner), COW must not be done. Actually, the check is slightly more complicated, because the _count field is also increased when the page is inserted into the swap cache (see the section "The Swap Cache" in Chapter 17) and when the PG_private flag in the page descriptor is set. However, when COW is not to be done, the page frame is marked as writable, so that it does not cause further Page Fault exceptions when writes are attempted: [*] To simplify the description of this function, we skip the statements that deal with reverse mapping, a topic that will be covered in the section "Reverse Mapping" in Chapter 17. set_pte(page_table, maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),vma)); flush_tlb_page(vma, address); pte_unmap(page_table); spin_unlock(&mm->page_table_lock); return VM_FAULT_MINOR; If the page is shared among several processes by means of COW, the function copies the content of the old page frame (old_page) into the newly allocated one (new_page). To avoid race conditions, get_page( ) is invoked to increase the usage counter of old_page before starting the copy operation: old_page = pte_page(pte); pte_unmap(page_table); get_page(old_page); spin_unlock(&mm->page_table_lock); if (old_page == virt_to_page(empty_zero_page)) new_page = alloc_page(GFP_HIGHUSER | _ _GFP_ZERO); } else { new_page = alloc_page(GFP_HIGHUSER); vfrom = kmap_atomic(old_page, KM_USER0) vto = kmap_atomic(new_page, KM_USER1); copy_page(vto, vfrom); kunmap_atomic(vfrom, KM_USER0); kunmap_atomic(vto, KM_USER0); } If the old page is the zero page, the new frame is efficiently filled with zeros when it is allocated (_ _GFP_ZERO flag). Otherwise, the page frame content is copied using the copy_page( ) macro. Special handling for the zero page is not strictly required, but it improves the system performance, because it preserves the microprocessor hardware cache by making fewer address references. Because the allocation of a page frame can block the process, the function checks whether the Page Table entry has been modified since the beginning of the function (pte and *page_table do not have the same value). In this case, the new page frame is released, the usage counter of old_page is decreased (to undo the increment made previously), and the function terminates. If everything looks OK, the physical address of the new page frame is finally written into the Page Table entry, and the corresponding TLB register is invalidated: spin_lock(&mm->page_table_lock); entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),vma); set_pte(page_table, entry); flush_tlb_page(vma, address); lru_cache_add_active(new_page); pte_unmap(page_table); spin_unlock(&mm->page_table_lock); The lru_cache_add_active( ) function inserts the new page frame in the swap-related data structures; see Chapter 17 for its description. Finally, do_wp_page( ) decreases the usage counter of old_page twice. The first decrement undoes the safety increment made before copying the page frame contents; the second decrement reflects the fact that the current process no longer owns the page frame. 9.4.5. Handling Noncontiguous Memory Area Accesses We have seen in the section "Noncontiguous Memory Area Management" in Chapter 8 that the kernel is quite lazy in updating the Page Table entries corresponding to noncontiguous memory areas. In fact, the vmalloc( ) and vfree( ) functions limit themselves to updating the master kernel Page Tables (i.e., the Page Global Directory init_mm.pgd and its child Page Tables). However, once the kernel initialization phase ends, the master kernel Page Tables are not directly used by any process or kernel thread. Thus, consider the first time that a process in Kernel Mode accesses a noncontiguous memory area. When translating the linear address into a physical address, the CPU's memory management unit encounters a null Page Table entry and raises a Page Fault. However, the handler recognizes this special case because the exception occurred in Kernel Mode, and the faulty linear address is greater than TASK_SIZE. Thus, the do_page_fault( ) handler checks the corresponding master kernel Page Table entry: vmalloc_fault: asm("movl %%cr3 ,%0":"=r" (pgd_paddr)); pgd = pgd_index(address) + (pgd_t *) _ _va(pgd_paddr); pgd_k = init_mm.pgd + pgd_index(address); if (!pgd_present(*pgd_k)) goto no_context; pud = pud_offset(pgd, address); pud_k = pud_offset(pgd_k, address); if (!pud_present(*pud_k)) goto no_context; pmd = pmd_offset(pud, address); pmd_k = pmd_offset(pud_k, address); if (!pmd_present(*pmd_k)) goto no_context; set_pmd(pmd, *pmd_k); pte_k = pte_offset_kernel(pmd_k, address); if (!pte_present(*pte_k)) goto no_context; return; The pgd_paddr local variable is loaded with the physical address of the Page Global Directory of the current process, which is stored in the cr3 register.[*] The pgd local variable is then loaded with the linear address corresponding to pgd_paddr, and the pgd_k local variable is loaded with the linear address of the master kernel Page Global Directory. [*] The kernel doesn't use current->mm->pgd to derive the address because this fault can occur anytime, even during a process switch. If the master kernel Page Global Directory entry corresponding to the faulty linear address is null, the function jumps to the code at the no_context label (see the earlier section "Handling a Faulty Address Outside the Address Space"). Otherwise, the function looks at the master kernel Page Upper Directory entry and at the master kernel Page Middle Directory entry corresponding to the faulty linear address. Again, if either one of these entries is null, a jump is done to the no_context label. Otherwise, the master entry is copied into the corresponding entry of the process's Page Middle Directory.[*] Then the whole operation is repeated with the master Page Table entry. [*] You might remember from the section "Paging in Linux" in Chapter 2 that if PAE is enabled then the Page Upper Directory entry cannot be null; otherwise, if PAE is disabled, setting the Page Middle Directory entry implicitly sets the Page Upper Directory entry, too. 9.5. Creating and Deleting a Process Address Space Of the six typical cases mentioned earlier in the section "The Process's Address Space," in which a process gets new memory regions, the first oneissuing a fork( ) system callrequires the creation of a whole new address space for the child process. Conversely, when a process terminates, the kernel destroys its address space. In this section, we discuss how these two activities are performed by Linux. 9.5.1. Creating a Process Address Space In the section "The clone( ), fork( ), and vfork( ) System Calls" in Chapter 3, we mentioned that the kernel invokes the copy_mm( ) function while creating a new process. This function creates the process address space by setting up all Page Tables and memory descriptors of the new process. Each process usually has its own address space, but lightweight processes can be created by calling clone( ) with the CLONE_VM flag set. These processes share the same address space; that is, they are allowed to address the same set of pages. Following the COW approach described earlier, traditional processes inherit the address space of their parent: pages stay shared as long as they are only read. When one of the processes attempts to write one of them, however, the page is duplicated; after some time, a forked process usually gets its own address space that is different from that of the parent process. Lightweight processes, on the other hand, use the address space of their parent process. Linux implements them simply by not duplicating address space. Lightweight processes can be created considerably faster than normal processes, and the sharing of pages can also be considered a benefit as long as the parent and children coordinate their accesses carefully. If the new process has been created by means of the clone( ) system call and if the CLONE_VM flag of the flag parameter is set, copy_mm( ) gives the clone (tsk) the address space of its parent (current): if (clone_flags & CLONE_VM) { atomic_inc(&current->mm->mm_users); spin_unlock_wait(&current->mm->page_table_lock); tsk->mm = current->mm; tsk->active_mm = current->mm; return 0; } Invoking the spin_unlock_wait( ) function ensures that, if the page table spin lock of the process is held by some other CPU, the page fault handler does not terminate until that lock is released. In fact, beside protecting the page tables, this spin lock must forbid the creation of new lightweight processes sharing the current->mm descriptor. If the CLONE_VM flag is not set, copy_mm( ) must create a new address space (even though no memory is allocated within that address space until the process requests an address). The function allocates a new memory descriptor, stores its address in the mm field of the new process descriptor tsk, and copies the contents of current->mm into tsk->mm. It then changes a few fields of the new descriptor: tsk->mm = kmem_cache_alloc(mm_cachep, SLAB_KERNEL); memcpy(tsk->mm, current->mm, sizeof(*tsk->mm)); atomic_set(&tsk->mm->mm_users, 1); atomic_set(&tsk->mm->mm_count, 1); init_rwsem(&tsk->mm->mmap_sem); tsk->mm->core_waiters = 0; tsk->mm->page_table_lock = SPIN_LOCK_UNLOCKED; tsk->mm->ioctx_list_lock = RW_LOCK_UNLOCKED; tsk->mm->ioctx_list = NULL; tsk->mm->default_kioctx = INIT_KIOCTX(tsk->mm->default_kioctx, *tsk->mm); tsk->mm->free_area_cache = (TASK_SIZE/3+0xfff)&0xfffff000; tsk->mm->pgd = pgd_alloc(tsk->mm); tsk->mm->def_flags = 0; Remember that the pgd_alloc( ) macro allocates a Page Global Directory for the new process. The architecture-dependent init_new_context( ) function is then invoked: when dealing with 80 x 86 processors, this function checks whether the current process owns a customized Local Descriptor Table; if so, init_new_context( ) makes a copy of the Local Descriptor Table of current and adds it to the address space of tsk. Finally, the dup_mmap( ) function is invoked to duplicate both the memory regions and the Page Tables of the parent process. This function inserts the new memory descriptor tsk->mm in the global list of memory descriptors. Then it scans the list of regions owned by the parent process, starting from the one pointed to by current->mm->mmap. It duplicates each vm_area_struct memory region descriptor encountered and inserts the copy in the list of regions and in the red-black tree owned by the child process. Right after inserting a new memory region descriptor, dup_mmap( ) invokes copy_page_range( ) to create, if necessary, the Page Tables needed to map the group of pages included in the memory region and to initialize the new Page Table entries. In particular, each page frame corresponding to a private, writable page (VM_SHARED flag off and VM_MAYWRITE flag on) is marked as read-only for both the parent and the child, so that it will be handled with the Copy On Write mechanism. 9.5.2. Deleting a Process Address Space When a process terminates, the kernel invokes the exit_mm( ) function to release the address space owned by that process: mm_release(tsk, tsk->mm); if (!(mm = tsk->mm)) /* kernel thread ? */ return; down_read(&mm->mmap_sem); The mm_release( ) function essentially wakes up all processes sleeping in the tsk>vfork_done completion (see the section "Completions" in Chapter 5). Typically, the corresponding wait queue is nonempty only if the exiting process was created by means of the vfork( ) system call (see the section "The clone( ), fork( ), and vfork( ) System Calls" in Chapter 3). If the process being terminated is not a kernel thread, the exit_mm( ) function must release the memory descriptor and all related data structures. First of all, it checks whether the mm>core_waiters flag is set: if it does, then the process is dumping the contents of the memory to a core file. To avoid corruption in the core file, the function makes use of the mm>core_done and mm->core_startup_done completions to serialize the execution of the lightweight processes sharing the same memory descriptor mm. Next, the function increases the memory descriptor's main usage counter, resets the mm field of the process descriptor, and puts the processor in lazy TLB mode (see "Handling the Hardware Cache and the TLB" in Chapter 2): atomic_inc(&mm->mm_count); spin_lock(tsk->alloc_lock); tsk->mm = NULL; up_read(&mm->map_sem); enter_lazy_tlb(mm, current); spin_unlock(tsk->alloc_lock); mmput(mm); Finally, the mmput( ) function is invoked to release the Local Descriptor Table, the memory region descriptors, and the Page Tables. The memory descriptor itself, however, is not released, because exit_mm( ) has increased the main usage counter. The descriptor will be released by the finish_task_switch( ) function when the process being terminated will be effectively evicted from the local CPU (see the section "The schedule( ) Function" in Chapter 7). 9.6. Managing the Heap Each Unix process owns a specific memory region called the heap, which is used to satisfy the process's dynamic memory requests. The start_brk and brk fields of the memory descriptor delimit the starting and ending addresses, respectively, of that region. The following APIs can be used by the process to request and release dynamic memory: malloc(size) Requests size bytes of dynamic memory; if the allocation succeeds, it returns the linear address of the first memory location. calloc(n,size) Requests an array consisting of n elements of size size; if the allocation succeeds, it initializes the array components to 0 and returns the linear address of the first element. realloc(ptr,size) Changes the size of a memory area previously allocated by malloc( ) or calloc( ) . free(addr) Releases the memory region allocated by malloc( ) or calloc( ) that has an initial address of addr. brk(addr) Modifies the size of the heap directly; the addr parameter specifies the new value of current->mm->brk, and the return value is the new ending address of the memory region (the process must check whether it coincides with the requested addr value). sbrk(incr) Is similar to brk( ) , except that the incr parameter specifies the increment or decrement of the heap size in bytes. The brk( ) function differs from the other functions listed because it is the only one implemented as a system call. All the other functions are implemented in the C library by using brk( ) and mmap( ).[*] [*] The realloc( ) library function can also make use of the mremap( ) system call. When a process in User Mode invokes the brk( ) system call, the kernel executes the sys_brk(addr) function. This function first verifies whether the addr parameter falls inside the memory region that contains the process code; if so, it returns immediately because the heap cannot overlap with memory region containing the process's code: mm = current->mm; down_write(&mm->mmap_sem); if (addr < mm->end_code) { out: up_write(&mm->mmap_sem); return mm->brk; } Because the brk( ) system call acts on a memory region, it allocates and deallocates whole pages. Therefore, the function aligns the value of addr to a multiple of PAGE_SIZE and compares the result with the value of the brk field of the memory descriptor: newbrk = (addr + 0xfff) & 0xfffff000; oldbrk = (mm->brk + 0xfff) & 0xfffff000; if (oldbrk == newbrk) { mm->brk = addr; goto out; } If the process asked to shrink the heap, sys_brk( ) invokes the do_munmap( ) function to do the job and then returns: if (addr <= mm->brk) { if (!do_munmap(mm, newbrk, oldbrk-newbrk)) mm->brk = addr; goto out; } If the process asked to enlarge the heap, sys_brk( ) first checks whether the process is allowed to do so. If the process is trying to allocate memory outside its limit, the function simply returns the original value of mm->brk without allocating more memory: rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur; if (rlim < RLIM_INFINITY && addr - mm->start_data > rlim) goto out; The function then checks whether the enlarged heap would overlap some other memory region belonging to the process and, if so, returns without doing anything: if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE)) goto out; If everything is OK, the do_brk( ) function is invoked. If it returns the oldbrk value, the allocation was successful and sys_brk( ) returns the value addr; otherwise, it returns the old mm->brk value: if (do_brk(oldbrk, newbrk-oldbrk) == oldbrk) mm->brk = addr; goto out; The do_brk( ) function is actually a simplified version of do_mmap( ) that handles only anonymous memory regions. Its invocation might be considered equivalent to: do_mmap(NULL, oldbrk, newbrk-oldbrk, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE, 0) do_brk( ) is slightly faster than do_mmap( ), because it avoids several checks on the memory region object fields by assuming that the memory region doesn't map a file on disk. Chapter 15. The Page Cache As already mentioned in the section "The Common File Model" in Chapter 12, a disk cache is a software mechanism that allows the system to keep in RAM some data that is normally stored on a disk, so that further accesses to that data can be satisfied quickly without accessing the disk. Disk caches are crucial for system performance, because repeated accesses to the same disk data are quite common. A User Mode process that interacts with a disk is entitled to ask repeatedly to read or write the same disk data. Moreover, different processes may also need to address the same disk data at different times. As an example, you may use the cp command to copy a text file and then invoke your favorite editor to modify it. To satisfy your requests, the command shell will create two different processes that access the same file at different times. We have already encountered other disk caches in Chapter 12: the dentry cache , which stores dentry objects representing filesystem pathnames, and the inode cache , which stores inode objects representing disk inodes. Notice, however, that dentry objects and inode objects are not mere buffers storing the contents of some disk blocks; thus, the dentry cache and the inode cache are rather peculiar as disk caches. This chapter deals with the page cache , which is a disk cache working on whole pages of data. We introduce the page cache in the first section. Then, we discuss in the section "Storing Blocks in the Page Cache" how the page cache can be used to retrieve single blocks of data (for instance, superblocks and inodes); this feature is crucial to speed up the VFS and the disk-based filesystems. Next, we describe in the section "Writing Dirty Pages to Disk" how the dirty pages in the page cache are written back to disk. Finally, we mention in the last section "The sync( ), fsync( ), and fdatasync( ) System Calls" some system calls that allow a user to flush the contents of the page cache so as to update the disk contents. 15.1. The Page Cache The page cache is the main disk cache used by the Linux kernel. In most cases, the kernel refers to the page cache when reading from or writing to disk. New pages are added to the page cache to satisfy User Mode processes's read requests. If the page is not already in the cache, a new entry is added to the cache and filled with the data read from the disk. If there is enough free memory, the page is kept in the cache for an indefinite period of time and can then be reused by other processes without accessing the disk. Similarly, before writing a page of data to a block device, the kernel verifies whether the corresponding page is already included in the cache; if not, a new entry is added to the cache and filled with the data to be written on disk. The I/O data transfer does not start immediately: the disk update is delayed for a few seconds, thus giving a chance to the processes to further modify the data to be written (in other words, the kernel implements deferred write operations). Kernel code and kernel data structures don't need to be read from or written to disk.[*] Thus, the pages included in the page cache can be of the following types: [*] Well, almost never: if you want to resume the whole state of the system after a shutdown, you can perform a "suspend to disk" operation (hibernation ), which saves the content of the whole RAM on a swap partition. We won't further discuss this case. Pages containing data of regular files; in Chapter 16, we describe how the kernel handles read, write, and memory mapping operations on them. Pages containing directories; as we'll see in Chapter 18, Linux handles the directories much like regular files. Pages containing data directly read from block device files (skipping the filesystem layer); as discussed in Chapter 16, the kernel handles them using the same set of functions as for pages containing data of regular files. Pages containing data of User Mode processes that have been swapped out on disk. As we'll see in Chapter 17, the kernel could be forced to keep in the page cache some pages whose contents have been already written on a swap area (either a regular file or a disk partition). Pages belonging to files of special filesystems, such as the shm special filesystem used for Interprocess Communication (IPC) shared memory region (see Chapter 19). As you can see, each page included in the page cache contains data belonging to some file. This fileor more precisely the file's inodeis called the page's owner. (As we will see in Chapter 17, pages containing swapped-out data have the same owner even if they refer to different swap areas.) Practically all read( ) and write( ) file operations rely on the page cache. The only exception occurs when a process opens a file with the O_DIRECT flag set: in this case, the page cache is bypassed and the I/O data transfers make use of buffers in the User Mode address space of the process (see the section "Direct I/O Transfers" in Chapter 16); several database applications make use of the O_DIRECT flag so that they can use their own disk caching algorithm. Kernel designers have implemented the page cache to fulfill two main requirements: Quickly locate a specific page containing data relative to a given owner. To take the maximum advantage from the page cache, searching it should be a very fast operation. Keep track of how every page in the cache should be handled when reading or writing its content. For instance, reading a page from a regular file, a block device file, or a swap area must be performed in different ways, thus the kernel must select the proper operation depending on the page's owner. The unit of information kept in the page cache is, of course, a whole page of data. As we'll see in Chapter 18, a page does not necessarily contain physically adjacent disk blocks, so it cannot be identified by a device number and a block number. Instead, a page in the page cache is identified by an owner and by an index within the owner's datausually, an inode and an offset inside the corresponding file. 15.1.1. The address_space Object The core data structure of the page cache is the address_space object, a data structure embedded in the inode object that owns the page.[*] Many pages in the cache may refer to the same owner, thus they may be linked to the same address_space object. This object also establishes a link between the owner's pages and a set of methods that operate on these pages. [*] An exception occurs for pages that have been swapped out. As we will see in Chapter 17, these pages have a common address_space object not included in any inode. Each page descriptor includes two fields called mapping and index, which link the page to the page cache (see the section "Page Descriptors" in Chapter 8). The first field points to the address_space object of the inode that owns the page. The second field specifies the offset in page-size units within the owner's "address space," that is, the position of the page's data inside the owner's disk image. These two fields are used when looking for a page in the page cache. Quite surprisingly, the page cache may happily contain multiple copies of the same disk data. For instance, the same 4-KB block of data of a regular file can be accessed in the following ways: Reading the file; hence, the data is included in a page owned by the regular file's inode. Reading the block from the device file (disk partition) that hosts the file; hence, the data is included in a page owned by the master inode of the block device file. Thus, the same disk data appears in two different pages referenced by two different address_space objects. The fields of the address_space object are shown in Table 15-1. Table 15-1. The fields of the address_space object Type struct inode * struct radix_tree_root spinlock_t unsigned int struct prio_tree_root struct list_head i_mmap_nonlinear tree_lock i_mmap_writable i_mmap Field host page_tree Description Pointer to the inode hosting this object, if any Root of radix tree identifying the owner's pages Spin lock protecting the radix tree Number of shared memory mappings in the address space Root of the radix priority search tree (see Chapter 17) List of non-linear memory regions in the address space Spin lock protecting the radix priority search tree Sequence counter used when truncating the file spinlock_t i_mmap_lock unsigned int TRuncate_count Table 15-1. The fields of the address_space object Type unsigned long unsigned long struct address_space_operations * Field nrpages writeback_index a_ops Description Total number of owner's pages Page index of the last write-back operation on the owner's pages Methods that operate on the owner's pages Error bits and memory allocator flags Pointer to the backing_dev_info of the block device holding the data of this owner Usually, spin lock used when managing the private_list list unsigned long struct backing_dev_info * spinlock_t struct list head struct address_space * flags backing_dev_info private_lock private_list assoc_mapping Usually, a list of dirty buffers of indirect blocks associated with the inode Usually, pointer to the address_space object of the block device including the indirect blocks If the owner of a page in the page cache is a file, the address_space object is embedded in the i_data field of a VFS inode object. The i_mapping field of the inode always points to the address_space object of the owner of the pages containing the inode's data. The host field of the address_space object points to the inode object in which the descriptor is embedded. Thus, if a page belongs to a file that is stored in an Ext3 filesystem , the owner of the page is the inode of the file and the corresponding address_space object is stored in the i_data field of the VFS inode object. The i_mapping field of the inode points to the i_data field of the same inode, and the host field of the address_space object points to the same inode. Sometimes, however, things are more complicated. If a page contains data read from a block device filethat is, it stores "raw" data of a block devicethe address_space object is embedded in the "master" inode of the file in the bdev special filesystem associated with the block device (this inode is referenced by the bd_inode field of the block device descriptor, see the section "Block Devices" in Chapter 14). Therefore, the i_mapping field of an inode of a block device file points to the address_space object embedded in the master inode; correspondingly, the host field of the address_space object points to the master inode. In this way, all pages containing data read from a block device have the same address_space object, even if they have been accessed by referring to different block device files. The i_mmap, i_mmap_writable, i_mmap_nonlinear, and i_mmap_lock fields refer to memory mapping and reverse mapping. We'll discuss these topics in Chapter 16 and Chapter 17. The backing_dev_info field points the backing_dev_info descriptor associated with the block device storing the data of the owner. As explained in the section "Request Queue Descriptors" in Chapter 14, the backing_dev_info structure is usually embedded in the request queue descriptor of the block device. The private_list field is the head of a generic list that can be freely used by the filesystem for its specific purposes. For example, the Ext2 filesystem makes use of this list to collect the dirty buffers of "indirect" blocks associated with the inode (see the section "Data Blocks Addressing" in Chapter 18). When a flush operation forces the inode to be written to disk, the kernel flushes also all the buffers in this list. Moreover, the Ext2 filesystem stores in the assoc_mapping field a pointer to the address_space object of the block device containing the indirect blocks; it also uses the assoc_mapping->private_lock spin lock to protect the lists of indirect blocks in multiprocessor systems. A crucial field of the address_space object is a_ops, which points to a table of type address_space_operations containing the methods that define how the owner's pages are handled. These methods are shown in Table 15-2. Table 15-2. The methods of the address_space object Method writepage readpage sync_page Description Write operation (from the page to the owner's disk image) Read operation (from the owner's disk image to the page) Start the I/O data transfer of already scheduled operations on owner's pages Write back to disk a given number of dirty owner's pages Read a list of owner's pages from disk Prepare a write operation (used by disk-based filesystems) Complete a write operation (used by disk-based filesystems) Get a logical block number from a file block index Used by journaling filesystems to prepare the release of a page Direct I/O transfer of the owner's pages (bypassing the page cache) writepages readpages prepare_write commit_write bmap set_page_dirty Set an owner's page as dirty invalidatepage Invalidate owner's pages (used when truncating the file) releasepage direct_IO The most important methods are readpage, writepage, prepare_write, and commit_write. We discuss them in Chapter 16. In most cases, the methods link the owner inode objects with the low-level drivers that access the physical devices. For instance, the function that implements the readpage method for an inode of a regular file knows how to locate the positions on the physical disk device of the blocks corresponding to each page of the file. In this chapter, however, we don't have to discuss the address_space methods further. 15.1.2. The Radix Tree In Linux, files can have large sizes, even a few terabytes. When accessing a large file, the page cache may become filled with so many of the file's pages that sequentially scanning all of them would be too time-consuming. In order to perform page cache lookup efficiently, Linux 2.6 makes use of a large set of search trees, one for each address_space object. The page_tree field of an address_space object is the root of a radix tree, which contains pointers to the descriptors of the owner's pages. Given a page index denoting the position of the page inside the owner's disk image, the kernel can perform a very fast lookup operation in order to determine whether the required page is already included in the page cache. When looking up the page, the kernel interprets the index as a path inside the radix tree and quickly reaches the position where the page descriptor isor should bestored. If found, the kernel can retrieve from the radix tree the descriptor of the page; it can also quickly determine whether the page is dirty (i.e., to be flushed to disk) and whether an I/O transfer for its data is currently on-going. Each node of the radix tree can have up to 64 pointers to other nodes or to page descriptors. Nodes at the bottom level store pointers to page descriptors (the leaves), while nodes at higher levels store pointers to other nodes (the children). Each node is represented by the radix_tree_node data structure, which includes three fields: slots is an array of 64 pointers, count is a counter of how many pointers in the node are not NULL, and tags is a twocomponent array of flags that will be discussed in the section "The Tags of the Radix Tree" later in this chapter. The root of the tree is represented by a radix_tree_root data structure, having three fields: height denotes the current tree's height (number of levels excluding the leaves), gfp_mask specifies the flags used when requesting memory for a new node, and rnode points to the radix_tree_node data structure corresponding to the node at level 1 of the tree (if any). Let us consider a simple example. If none of the indices stored in the tree is greater than 63, the tree height is equal to one, because the 64 potential leaves can all be stored in the node at level 1 (see Figure 15-1 (a)). If, however, a new page descriptor corresponding to index 131 must be stored in the page cache, the tree height is increased to two, so that the radix tree can pinpoint indices up to 4095 (see Figure 15-1(b)). Figure 15-1. Two examples of a radix tree Table 15-3 shows the highest page index and the corresponding maximum file size for each given height of the radix tree on a 32-bit architecture. In this case, the maximum height of a radix tree is six, although it is quite unlikely that the page cache of your system will make use of a radix tree that huge. Because the page index is stored in a 32-bit variable, when the tree has height equal to six, the node at the highest level can have at most four children. Table 15-3. Highest index and maximum file size for each radix tree height Radix tree height 0 1 2 Highest index none 26 -1 = 63 2 12 Maximum file size 0 bytes 256 kilobytes 16 megabytes 1 gigabyte 64 gigabytes 4 terabytes 16 terabytes -1 = 4 095 3 4 5 6 218 -1 = 262 143 2 -1 = 16 777 215 230 -1 = 1 073 741 823 2 32 24 -1 = 4 294 967 295 The best way to understand how page lookup is performed is to recall how the paging system makes use of the page tables to translate linear addresses into physical addresses. As discussed in the section "Regular Paging" in Chapter 2, the 20 most significant bits of a linear address are split into two 10-bit long fields: the first field is an offset in the Page Directory, while the second one is an offset in the Page Table pointed to by the proper Page Directory entry. A similar approach is used in the radix tree. The equivalent of the linear address is the page's index. However, the number of fields to be considered in the page's index depends on the height of the radix tree. If the radix tree has height 1, only indices ranging from 0 to 63 can be represented, thus the 6 less significant bits of the page's index are interpreted as the slots array index for the single node at level 1. If the radix tree has height 2, the indices that can be represented range from 0 to 4095; the 12 less significant bits of the page's index are thus split in 2 fields of 6 bits each; the most significant field is used as an array index for the node at level 1, while the less significant field is used as an array index for the node at level 2. The procedure is similar for every other radix tree's height. If the height is equal to 6, the 2 most significant bits of the page's index are the array index for the node at level 1, the following 6 bits are the array index for the node at level 2, and so on up to the 6 less significant bits, which are the array index for the node at level 6. If the highest index of a radix tree is smaller than the index of a page that should be added, then the kernel increases the tree height correspondingly; the intermediate nodes of the radix tree depend on the value of the page index (see Figure 15-1 for an example). 15.1.3. Page Cache Handling Functions The basic high-level functions that use the page cache involve finding, adding, and removing a page. Another function based on the previous ones ensures that the cache includes an up-to-date version of a given page. 15.1.3.1. Finding a page The find_get_page( ) function receives as its parameters a pointer to an address_space object and an offset value. It acquires the address space's spin lock and invokes the radix_tree_lookup( ) function to search for a leaf node of the radix tree having the required offset. This function, in turn, starts from the root node of the tree and goes down according to the bits of the offset value, as explained in the previous section. If a NULL pointer is encountered, the function returns NULL; otherwise, it returns the address of a leaf node, that is, the pointer of the required page descriptor. If the requested page is found, find_get_page( ) increases its usage counter, releases the spin lock, and returns its address; otherwise, the function releases the spin lock and returns NULL. The find_get_pages( ) function is similar to find_get_page( ), but it performs a page cache lookup for a group of pages having contiguous indices. It receives as its parameters a pointer to an address_space object, the offset in the address space from where to start searching, the maximum number of pages to be retrieved, and a pointer to an array of pages descriptors to be filled by the function. To perform the lookup operation, find_get_pages( ) relies on the radix_tree_gang_lookup( ) function, which fills the array of pointers and returns the number of pages found. The returned pages have ascending indices, although there may be holes in the indices because some pages may not be in the page cache. There are several other functions that perform search operations on the page cache. For example, the find_lock_page( ) function is similar to find_get_page( ), but it increases the usage counter of the returned page and invokes lock_page( ) to set the PG_locked flagthus, when the function returns, the page can be accessed exclusively by the caller. The lock_page( ) function, in turn, blocks the current process if the page is already locked. To that end, it invokes the _ _wait_on_bit_lock( ) function on the PG_locked bit. The latter function puts the current process in the TASK_UNINTERRUPTIBLE state, stores the process descriptor in a wait queue, executes the sync_page method of the address_space object to unplug the request queue of the block device containing the file, and finally invokes schedule( ) to suspend the process until the PG_locked flag of the page is cleared. To unlock a page and wake up the processes sleeping in the wait queue, the kernel makes use of the unlock_page( ) function. The find_trylock_page( ) function is similar to find_lock_page( ), except that it never blocks: if the requested page is already locked, the function returns an error code. Finally, the find_or_create_page( ) function executes find_lock_page( ); however, if the page is not found, a new page is allocated and inserted in the page cache. 15.1.3.2. Adding a page The add_to_page_cache( ) function inserts a new page descriptor in the page cache. It receives as its parameters the address page of the page descriptor, the address mapping of an address_space object, the value offset representing the page index inside the address space, and the memory allocation flags gfp_mask to be used when allocating the new nodes of the radix tree. The function performs the following operations: 1. Invokes radix_tree_preload( ), which disables kernel preemption and fills the perCPU variable radix_tree_preloads with a few free radix_tree_node structures. Allocation of radix_tree_node structures is done by means of the radix_tree_node_cachep slab allocator cache. If radix_tree_preload( ) fails in preallocating the radix_tree_node structures, the add_to_page_cache( ) function terminates by returning the error code -ENOMEM. Otherwise, if radix_tree_preload( ) succeeds, add_to_page_cache( ) can be sure that the insertion of the new page descriptor will not fail for lack of free memory, at least for files of size up to 64 GB. 2. Acquires the mapping->tree_lock spin locknotice that kernel preemption has already been disabled by radix_tree_preload( ). 3. Invokes radix_tree_insert( ) to insert the new node in the tree. This function performs the following steps: a. Invokes radix_tree_maxindex( ) to get the maximum index that can be inserted in the radix tree with its current height; if the index of the new page cannot be represented with the current height, it invokes radix_tree_extend( ) to increase the height of the tree by adding the proper number of nodes (for instance, when applied to the radix tree shown in Figure 15-1 (a), radix_tree_extend( ) would add a single node on top of it). New nodes are allocated by executing the radix_tree_node_alloc( ) function, which tries to get a radix_tree_node structure from the slab allocator cache or, if this allocation fails, from the pool of preallocated structures stored in radix_tree_preloads. b. Starting from the root (mapping->page_tree), it traverses the tree according to the offset page's index until the leaf is reached, as described in the previous section. If required, it allocates new intermediate nodes by invoking radix_tree_node_alloc( ). c. Stores the page descriptor address in the proper slot of the last traversed node of the radix tree, and returns 0. 4. Increases the usage counter page->_count of the page descriptor. 5. Because the page is new, its content is invalid: the function sets the PG_locked flag of the page frame to protect the page against concurrent accesses from other kernel control paths. 6. Initializes page->mapping and page->index with the parameters mapping and offset. 7. Increases the counter of cached pages in the address space (mapping->nrpages). 8. Releases the address space's spin lock. 9. Invokes radix_tree_preload_end( ) to reenable kernel preemption. 10. Returns 0 (success). 15.1.3.3. Removing a page The remove_from_page_cache( ) function removes a page descriptor from the page cache. This is achieved in the following way: 1. Acquires the page->mapping->tree_lock spin lock and disables interrupts. 2. Invokes radix_tree_delete( ) to delete the node from the tree. This function receives as its parameters the address of the tree's root (page->mapping->page_tree) and the index of the page to be removed and performs the following steps: a. Starting from the root, it traverses the tree according to the page's index until the leaf is reached, as described in the previous section. While doing so, it builds up an array of radix_tree_path structures that describe the components of the path from the root to the leaf corresponding to the page to be deleted. b. Starts a cycle on the nodes collected in the path array, starting with the last node, which contains the pointer to the page descriptor. For each node, it sets to NULL the element of the slots array pointing to the next node (or to the page descriptor) and decreases the count field. If count becomes zero, it removes the node from the tree and releases the radix_tree_node structure to the slab allocator cache, then continues the cycle with the preceding node in the path array; otherwise, if count does not become zero, it continues with the next step. c. Returns the pointer to the page descriptor that has been removed from the tree. 3. Sets the page->mapping field to NULL. 4. Decreases by one the page->mapping->nrpages counter of cached pages. 5. Releases the page->mapping->tree_lock spin lock, enables the interrupts, and terminates. 15.1.3.4. Updating a page The read_cache_page( ) function ensures that the cache includes an up-to-date version of a given page. Its parameters are a pointer mapping to an address_space object, an offset value index that specifies the requested page, a pointer filler to a function that reads the page's data from disk (usually it is the function that implements the address space's readpage method), and a pointer data that is passed to the filler function (usually, it is NULL). Here is a simplified description of what the function does: 1. Invokes find_get_page( ) to check whether the page is already in the page cache. 2. If the page is not in the page cache, it performs the following substeps: a. Invokes alloc_pages( ) to allocate a new page frame. b. Invokes add_to_page_cache( ) to insert the corresponding page descriptor into the page cache. c. Invokes lru_cache_add( ) to insert the page in the zone's inactive LRU list (see the section "The Least Recently Used (LRU) Lists" in Chapter 17). 3. Here the page is in the page cache. Invokes mark_page_accessed( ) to record the fact that the page has been accessed (see the section "The Least Recently Used (LRU) Lists" in Chapter 17). 4. If the page is not up-to-date (PG_uptodate flag clear), it invokes the filler function to read from disk the page. 5. Returns the address of the page descriptor. 15.1.4. The Tags of the Radix Tree As stated previously, the page cache not only allows the kernel to quickly retrieve a page containing specified data of a block device; the cache also allows the kernel to quickly retrieve pages in the cache that are in a given state. For instance, let us suppose that the kernel must retrieve all pages in the cache that belong to a given owner and that are dirty, that is, the pages whose contents have not yet been written to disk. The PG_dirty flag stored in the page descriptor specifies whether a page is dirty or not; however, traversing the whole radix tree to sequentially access all the leavesthat is, the page descriptorswould be an unduly slow operation if most pages are not dirty. Instead, to allow a quick search of dirty pages, each intermediate node in the radix tree contains a dirty tag for each child node (or leaf); this flag is set if and only if at least one of the dirty tags of the child node is set. The dirty tags of the nodes at the bottom level are usually copies of the PG_dirty flags of the page descriptors. In this way, when the kernel traverses a radix tree looking for dirty pages, it can skip each subtree rooted at an intermediate node whose dirty tag is clear: it knows for sure that all page descriptors stored in the subtree are not dirty. The same idea applies to the PG_writeback flag, which denotes that a page is currently being written back to disk. Thus, each node of the radix tree propagates two flags of the page descriptor: PG_dirty and PG_writeback (see the section "Page Descriptors" in Chapter 8). To store them, each node includes two arrays of 64 bits in the tags field. The tags[0] array (PAGECACHE_TAG_DIRTY) is the dirty tag, while the tags[1] (PAGECACHE_TAG_WRITEBACK) array is the writeback tag. The radix_tree_tag_set( ) function is invoked when setting the PG_dirty or the PG_writeback flag of a cached page; it acts on three parameters: the root of the radix tree, the page's index, and the type of tag to be set (PAGECACHE_TAG_DIRTY or PAGECACHE_TAG_WRITEBACK). The function starts from the root of the tree and goes down to the leaf corresponding to the given index; for each node of the path leading from the root to the leaf, the function sets the tag associated with the pointer to the next node in the path. The function then returns the address of the page descriptor. As a result, each in node in the path that goes down from the root to the leaf is tagged in the appropriate way. The radix_tree_tag_clear( ) function is invoked when clearing the PG_dirty or the PG_writeback flag of a cached page; it acts on the same parameters as radix_tree_tag_set( ). The function starts from the root of the tree and goes down to the leaf, building an array of radix_tree_path structures describing the path. Then, the function proceeds backward from the leaf to the root: it clears the tag of the node at the bottom level, then it checks whether all tags in the node's array are now cleared; if so, the function clears the proper tag in the parent node at the upper level, checks whether all tags in that node are cleared, and so on. The function then returns the address of the page descriptor. When a page descriptor is removed from a radix tree, the proper tags in the nodes belonging to the path from the root to the leaf must be updated. The radix_tree_delete( ) function does this properly (even if we omitted mentioning this fact in the previous section). The radix_tree_insert( ) function, however, doesn't update the tags, because each page descriptor inserted in the radix tree is supposed to have the PG_dirty and PG_writeback flags cleared. If necessary, the kernel may later invoke the radix_tree_tag_set( ) function. The radix_tree_tagged( ) function takes advantage of the arrays of flags included in all nodes of the tree to test whether a radix tree includes at least one page in a given state. The function performs this task quite simply by executing the following code (root is a pointer to the radix_tree_root structure of the radix tree, and tag is the flag to be tested): for (idx = 0; idx < 2; idx++) { if (root->rnode->tags[tag][idx]) return 1; } return 0; Because the tags of all nodes of the radix tree can be assumed to be properly updated, radix_tree_tagged( ) needs only to check the tags of the node at level 1. An example of use of such function occurs when determining whether an inode contains dirty pages to be written to disk. Notice that in each iteration the function tests whether any of the 32 flags stored in an unsigned long is set. The find_get_pages_tag( ) function is similar to find_get_pages( ) except that it returns only pages that are tagged with the tag parameter. As we'll see in the section "Writing Dirty Pages to Disk," this function is crucial to quickly identify all the dirty pages of an inode. 15.2. Storing Blocks in the Page Cache We have seen in the section "Block Devices Handling" in Chapter 14 that the VFS, the mapping layer, and the various filesystems group the disk data in logical units called "blocks." In old versions of the Linux kernel, there were two different main disk caches: the page cache, which stored whole pages of disk data resulting from accesses to the contents of the disk files, and the buffer cache , which was used to keep in memory the contents of the blocks accessed by the VFS to manage the disk-based filesystems. Starting from stable version 2.4.10, the buffer cache does not really exist anymore. In fact, for reasons of efficiency, block buffers are no longer allocated individually; instead, they are stored in dedicated pages called "buffer pages ," which are kept in the page cache. Formally, a buffer page is a page of data associated with additional descriptors called "buffer heads ," whose main purpose is to quickly locate the disk address of each individual block in the page. In fact, the chunks of data stored in a page belonging to the page cache are not necessarily adjacent on disk. 15.2.1. Block Buffers and Buffer Heads Each block buffer has a buffer head descriptor of type buffer_head. This descriptor contains all the information needed by the kernel to know how to handle the block; thus, before operating on each block, the kernel checks its buffer head. The fields of a buffer head are listed in Table 15-4. Table 15-4. The fields of a buffer head Type unsigned long struct buffer_head * struct page * atomic_t u32 sector_t char * struct block_device * bh_end_io_t * void * struct list_head Field b_state b_this_page Description Buffer status flags Pointer to the next element in the buffer page's list Pointer to the descriptor of the buffer page holding this block Block usage counter Block size Block number relative to the block device (logical block number) Position of the block inside the buffer page Pointer to block device descriptor I/O completion method Pointer to data for the I/O completion method inode (see the section "The address_space Object" earlier in this chapter) b_page b_count b_size b_blocknr b_data b_bdev b_end_io b_private b_assoc_buffers Pointers for the list of indirect blocks associated with an Two fields of the buffer head encode the disk address of the block: the b_bdev field identifies the block deviceusually, a disk or a partitionthat contains the block (see the section "Block Devices" in Chapter 14), while the b_blocknr field stores the logical block number, that is, the index of the block inside its disk or partition. The b_data field specifies the position of the block buffer inside the buffer page. Actually, the encoding of this position depends on whether the page is in high memory or not. If the page is in high memory, the b_data field contains the offset of the block buffer with respect to the beginning of the page; otherwise, b_data contains the linear address of the block buffer. The b_state field may store several flags. Some of them are of general use and are listed in Table 15-5. Each filesystem may also define its own private buffer head flags. Table 15-5. The buffer head's general flags Flag BH_Uptodate BH_Dirty Description Set if the buffer contains valid data Set if the buffer is dirtythat is, it contains data that must be written to the block device Set if the buffer is locked, which usually happens when the buffer is involved in a disk transfer Set if data transfer for initializing the buffer has already been requested Set if the buffer is mapped to diskthat is, if the b_bdev and b_blocknr fields of the corresponding buffer head are significant Set if the corresponding block has just been allocated and has never been accessed Set if the buffer is being read asynchronously Set if the buffer is not yet allocated on disk Set if the block to be submitted after this one will not be adjacent to this one Set if there was an I/O error when writing this block Set if the block should be written strictly after the blocks submitted before it (used by journaling filesystems ) Set if the block device driver does not support the operation requested BH_Lock BH_Req BH_Mapped BH_New BH_Async_Read BH_Async_Write Set if the buffer is being written asynchronously BH_Delay BH_Boundary BH_Write_EIO BH_Ordered BH_Eopnotsupp 15.2.2. Managing the Buffer Heads The buffer heads have their own slab allocator cache, whose kmem_cache_s descriptor is stored in the bh_cachep variable. The alloc_buffer_head( ) and free_buffer_head( ) functions are used to get and release a buffer head, respectively. The b_count field of the buffer head is a usage counter for the corresponding block buffer. The counter is increased right before each operation on the block buffer and decreased right after. The block buffers kept in the page cache are examined both periodically and when free memory becomes scarce, and only the block buffers having null usage counters may be reclaimed (see Chapter 17). When a kernel control path wishes to access a block buffer, it should first increase the usage counter. The function that locates a block inside the page cache (_ _getblk( ); see the section "Searching Blocks in the Page Cache" later in this chapter) does this automatically, hence the higher-level functions do not usually increase the block buffer's usage counter. When a kernel control path stops accessing a block buffer, it should invoke either _ _brelse( ) or _ _bforget( ) to decrease the corresponding usage counter. The difference between these two functions is that _ _bforget( ) also removes the block from any list of indirect blocks (b_assoc_buffers buffer head field; see the previous section "Block Buffers and Buffer Heads") and marks the buffer as clean, thus forcing the kernel to forget any change in the buffer that has yet to be written on disk. 15.2.3. Buffer Pages Whenever the kernel must individually address a block, it refers to the buffer page that holds the block buffer and checks the corresponding buffer head. Here are two common cases in which the kernel creates buffer pages: When reading or writing pages of a file that are not stored in contiguous disk blocks. This happens either because the filesystem has allocated noncontiguous blocks to the file, or because the file contains "holes" (see the section "File Holes" in Chapter 18). When accessing a single disk block (for instance, when reading a superblock or an inode block). In the first case, the buffer page's descriptor is inserted in the radix tree of a regular file. The buffer heads are preserved because they store precious information: the block device and the logical block number that specify the position of the data in the disk. We will see how the kernel makes use of this type of buffer page in Chapter 16. In the second case, the buffer page's descriptor is inserted in the radix tree rooted at the address_space object of the inode in the bdev special filesystem associated with the block device (see the section "The address_space Object" earlier in this chapter). This kind of buffer pages must satisfy a strong constraint: all the block buffers must refer to adjacent blocks of the underlying block device. An instance of where this is useful is when the VFS wants to read the 1,024-byte inode block containing the inode of a given file. Instead of allocating a single buffer, the kernel must allocate a whole page storing four buffers; these buffers will contain the data of a group of four adjacent blocks on the block device, including the requested inode block. In this chapter we will focus our attention on the second type of buffer pages, the so-called block device buffer pages (sometimes shortened to blockdev's pages). All the block buffers within a single buffer page must have the same size; hence, on the 80 x 86 architecture, a buffer page can include from one to eight buffers, depending on the block size. When a page acts as a buffer page, all buffer heads associated with its block buffers are collected in a singly linked circular list. The private field of the descriptor of the buffer page points to the buffer head of the first block in the page;[*] every buffer head stores in the b_this_page field a pointer to the next buffer head in the list. Moreover, every buffer head stores the address of the buffer page's descriptor in the b_page field. Figure 15-2 shows a buffer page containing four block buffers and the corresponding buffer heads. [*] Because the private field contains valid data, the PG_private flag of the page is also set; hence, if the page contains disk data and the PG_private flag is set, then the page is a buffer page. Notice, however, that other kernel components not related to the block I/O subsystem use the private and PG_private fields for other purposes. Figure 15-2. A buffer page including four buffers and their buffer heads 15.2.4. Allocating Block Device Buffer Pages The kernel allocates a new block device buffer page when it discovers that the page cache does not include a page containing the buffer for a given block (see the section "Searching Blocks in the Page Cache" later in this chapter). In particular, the lookup operation for the block might fail for the following reasons: 1. The radix tree of the block device does not include a page containing the data of the block: in this case a new page descriptor must be added to the radix tree. 2. The radix tree of the block device includes a page containing the data of the block, but this page is not a buffer page: in this case new buffer heads must be allocated and linked to the page, thus transforming it into a block device buffer page. 3. The radix tree of the block device includes a buffer page containing the data of the block, but the page has been split in blocks of size different from the size of the requested block: in this case the old buffer heads must be released, and a new set of buffer heads must be allocated and linked to the page. In order to add a block device buffer page to the page cache, the kernel invokes the grow_buffers( ) function, which receives three parameters that identify the block: The address bdev of the block_device descriptor The logical block number block the position of the block inside the block device The block size size The function essentially performs the following actions: 1. Computes the offset index of the page of data within the block device that includes the requested block. 2. Invokes grow_dev_page( ) to create a new block device buffer page, if necessary. In turn, this function performs the following substeps: a. Invokes find_or_create_page( ), passing to it the address_space object of the block device (bdev->bd_inode->i_mapping), the page offset index, and the GFP_NOFS flag. As described in the earlier section "Page Cache Handling Functions," find_or_create_page( ) looks for the page in the page cache and, if necessary, inserts a new page in the cache. b. Now the required page is in the page cache, and the function has the address of its descriptor. The function checks its PG_private flag; if it is NULL, the page is not yet a buffer page (it has no associated buffer heads): it jumps to step 2e. c. The page is already a buffer page. Gets from the private field of its descriptor the address bh of the first buffer head, and checks whether the block size bh>size is equal to the size of the requested block; if so, the page found in the page cache is a valid buffer page: it jumps to step 2g. d. The page has blocks of the wrong size: it invokes try_to_free_buffers( ) (see the next section) to release the previous buffer heads of the buffer page. e. Invokes the alloc_page_buffers( ) function to allocate the buffer heads for the blocks of the requested size within the page and insert them into the singly linked circular list implemented by the b_this_page fields. Moreover, the function initializes the b_page fields of the buffer heads with the address of the page descriptor, and the b_data fields with the offset or linear address of the block buffer inside the page. f. Stores the address of the first buffer head in the private field, sets the PG_private field, and increases the usage counter of the page (the block buffers inside the page counts as a page user). g. Invokes the init_page_buffers( ) function to initialize the b_bdev, b_blocknr, and b_bstate fields of the buffer heads linked to the page. All blocks are adjacent on disk, hence the logical block numbers are consecutive and can be easily derived from block. h. Returns the page descriptor address. 3. Unlocks the page (the page was locked by find_or_create_page( )). 4. Decreases the page's usage counter (again, the counter was increased by find_or_create_page( )). 5. Returns 1 (success). 15.2.5. Releasing Block Device Buffer Pages As we will see in Chapter 17, block device buffer pages are released when the kernel tries to get additional free memory. Clearly a buffer page cannot be freed if it contains dirty or locked buffers. To release buffer pages, the kernel invokes the TRy_to_release_page( ) function, which receives the address page of a page descriptor and performs the following actions:[*] [*] The TRy_to_release_page( ) function can also be invoked on buffer pages owned by regular files. 1. If the PG_writeback flag of the page is set, it returns 0 (no release is possible because the page is being written back to disk). 2. If defined, it invokes the releasepage method of the block device's address_space object. (The method is usually not defined for block devices.) 3. Invokes the try_to_free_buffers( ) function, and returns its error code. In turn, the try_to_free_buffers( ) function scans the buffer heads linked to the buffer page; it performs essentially the following actions: 1. Checks the flags of all the buffer heads of buffers included in the page. If some buffer head has the BH_Dirty or BH_Locked flag set, the function terminates by returning 0 (failure): it is not possible to release the buffers. 2. If a buffer head is inserted in a list of indirect buffers (see the section "Block Buffers and Buffer Heads" earlier in this chapter), the function removes it from the list. 3. Clears the PG_private flag of the page descriptor, sets the private field to NULL, and decreases the page's usage counter. 4. Clears the PG_dirty flag of the page. 5. Invokes repeatedly free_buffer_head( ) on the buffer heads of the page to free all of them. 6. Returns 1 (success). 15.2.6. Searching Blocks in the Page Cache When the kernel needs to read or write a single block of a physical device (for instance, a superblock), it must check whether the required block buffer is already included in the page cache. Searching the page cache for a given block bufferspecified by the address bdev of a block device descriptor and by a logical block number nris a three stage process: 1. Get a pointer to the address_space object of the block device containing the block (bdev->bd_inode->i_mapping). 2. Get the block size of the device (bdev->bd_block_size), and compute the index of the page that contains the block. This is always a bit shift operation on the logical block number. For instance, if the block size is 1,024 bytes, each buffer page contains four block buffers, thus the page's index is nr/4. 3. Searches for the buffer page in the radix tree of the block device. After obtaining the page descriptor, the kernel has access to the buffer heads that describe the status of the block buffers inside the page. Details are slightly more complicated than this, however. In order to enhance system performance, the kernel manages a bh_lrus array of small disk caches , one for each CPU, called the Least Recently Used (LRU) block cache. Each disk cache contains eight pointers to buffer heads that have been recently accessed by a given CPU. The elements in each CPU array are sorted so that the pointer to the most recently used buffer head has index 0. The same buffer head might appear on several CPU arrays (but never twice in the same CPU array); for each occurrence of a buffer head in the LRU block cache , the buffer head's b_count usage counter is increased by one. 15.2.6.1. The _ _find_get_block( ) function The _ _find_get_block( ) function receives as its parameters the address bdev of a block_device descriptor, the block number block, and the block size size, and returns the address of the buffer head associated with the block buffer inside the page cache, or NULL if no such block buffer exists. The function performs essentially the following actions: 1. Checks whether the LRU block cache array of the executing CPU includes a buffer head whose b_bdev, b_blocknr, and b_size fields are equal to bdev, block, and size, respectively. 2. If the buffer head is in the LRU block cache, it reshuffles the elements in the array so as to put the pointer to the just discovered buffer head in the first position (index 0), increases its b_count field, and jumps to step 8. 3. Here the buffer head is not in the LRU block cache: it derives from the block number and the block size the page index relative to the block device as: index = block >> (PAGE_SHIFT - bdev->bd_inode->i_blkbits); 4. Invokes find_get_page( ) to locate, in the page cache, the descriptor of the buffer page containing the required block buffer. The function passes as parameters a pointer to the address_space object of the block device (bdev->bd_inode->i_mapping) and the page index to locate in the page cache the descriptor of the buffer page containing the required block buffer. If there is no such page in the cache, returns NULL (failure). 5. At this point, the function has the address of a descriptor for the buffer page: it scans the list of buffer heads linked to the buffer page, looking for the block having logical block number equal to block. 6. Decreases the count field of the page descriptor (it was increased by find_get_page( )). 7. Moves all elements in the LRU block cache one position down, and inserts the pointer to the buffer head of the requested block in the first position. If a buffer head has been dropped out of the LRU block cache, it decreases its b_count usage counter. 8. Invokes mark_page_accessed( ) to move the buffer page in the proper LRU list, if necessary (see the section "The Least Recently Used (LRU) Lists" in Chapter 17). 9. Returns the buffer head pointer. 15.2.6.2. The _ _getblk( ) function The _ _getblk( ) function receives the same parameters as _ _find_get_block( ), namely the address bdev of a block_device descriptor, the block number block, and the block size size, and returns the address of a buffer head associated with the buffer. The function never fails: even if the block does not exist at all, the _ _getblk( ) obligingly allocates a block device buffer page and returns a pointer to the buffer head that should describe the block. Notice that the block buffer returned by _ _getblk( ) does not necessarily contain valid datathe BH_Uptodate flag of the buffer head might be cleared. The _ _getblk( ) function essentially performs the following steps: 1. Invokes _ _find_get_block( ) to check whether the block is already in the page cache. If the block is found, the function returns the address of its buffer head. 2. Otherwise, it invokes grow_buffers( ) to allocate a new buffer page for the requested block (see the section "Allocating Block Device Buffer Pages" earlier in this chapter). 3. If grow_buffers( ) fails in allocating such a page, _ _getblk( ) tries to reclaim some memory by invoking free_more_memory( ) (see Chapter 17). 4. Jumps back to step 1. 15.2.6.3. The _ _bread( ) function The _ _bread( ) function receives the same parameters as _ _getblk( ), namely the address bdev of a block_device descriptor, the block number block, and the block size size, and returns the address of a buffer head associated with the buffer. Contrary to _ _getblk( ), the function reads the block from disk, if necessary, before returning the buffer head. The _ _bread( ) function performs the following steps: 1. Invokes _ _getblk( ) to find in the page cache the buffer page associated with the required block and to get a pointer to the corresponding buffer head. 2. If the block is already in the page cache and the buffer contains valid data (flag BH_Uptodate set), it returns the address of the buffer head. 3. Otherwise, it increases the usage counter of the buffer head. 4. Sets the b_end_io field to the address of end_buffer_read_sync( ) (see the next section). 5. Invokes submit_bh( ) to transmit the buffer head to the generic block layer (see next section). 6. Invokes wait_on_buffer( ) to put the current process in a wait queue until the read I/O operation is completed, that is, until the BH_Lock flag of the buffer head is cleared. 7. Returns the address of the buffer head. 15.2.7. Submitting Buffer Heads to the Generic Block Layer A couple of functions, submit_bh( ) and ll_rw_block( ), allow the kernel to start an I/O data transfer on one or more buffers described by their buffer heads. 15.2.7.1. The submit_bh( ) function To transmit a single buffer head to the generic block layer, and thus to require the transfer of a single block of data, the kernel makes use of the submit_bh( ) function. Its parameters are the direction of data transfer (essentially READ or WRITE) and a pointer bh to the buffer head describing the block buffer. The submit_bh( ) function assumes that the buffer head is fully initialized; in particular, the b_bdev, b_blocknr, and b_size fields must be properly set to identify the block on disk containing the requested data. If the block buffer belongs to a block device buffer page, the initialization of the buffer head is done by _ _find_get_block( ), as described in the previous section. However, as we will see in the next chapter, submit_bh( ) can also be invoked on blocks belonging to buffer pages owned by regular files. The submit_bh( ) function is little else than a glue function that creates a bio request from the contents of the buffer head and then invokes generic_make_request( ) (see the section "Submitting a Request" in Chapter 14). The main steps performed by it are the following: 1. Sets the BH_Req flag of the buffer head to record that the block has been submitted at least one time; moreover, if the direction of the data transfer is WRITE, clears the BH_Write_EIO flag. 2. Invokes bio_alloc( ) to allocate a new bio descriptor (see the section "The Bio Structure" in Chapter 14). 3. Initializes the fields of the bio descriptor according to the contents of the buffer head: a. Sets the bi_sector field to the number of the first sector in the block (bh>b_blocknr * bh->b_size / 512); b. Sets the bi_bdev field with the address of the block device descriptor (bh>b_bdev); c. Sets the bi_size field with the block size (bh->b_size); d. Initializes the first element of the bi_io_vec array so that the segment corresponds to the block buffer: bi_io_vec[0].bv_page is set to bh->b_page, bi_io_vec[0].bv_len is set to bh->b_size, and bi_io_vec[0].bv_offset is set to the offset of the block buffer in the page as specified by bh->b_data; e. Sets bi_vcnt to 1 (just one segment on the bio), and bi_idx to 0 (the current segment to be transferred); f. Sets the bi_end_io field to the address of end_bio_bh_io_sync( ), and sets the bi_private field to the address of the buffer head; the function will be invoked when the data transfer terminates (see below). 4. Increases the reference counter of the bio (it becomes equal to 2). 5. Invokes submit_bio( ), which sets the bi_rw flag with the direction of the data transfer, updates the page_states per-CPU variable to keep track of the number of sectors read and written, and invokes the generic_make_request( ) function on the bio descriptor. 6. Decreases the usage counter of the bio; the bio descriptor is not freed, because it is now inserted in a queue of the I/O scheduler. 7. Returns 0 (success). When the I/O data transfer on the bio terminates, the kernel executes the bi_end_io method, in this particular case the end_bio_bh_io_sync( ) function. The latter function essentially gets the address of the buffer head from the bi_private field of the bio, then invokes the b_end_io method of the buffer headit was properly set before invoking submit_bh( )and finally invokes bio_put( ) to destroy the bio structure. 15.2.7.2. The ll_rw_block( ) function Sometimes the kernel must trigger the data transfer of several data blocks at once, which are not necessarily physically adjacent. The ll_rw_block( ) function receives as its parameters the direction of data transfer (essentially READ or WRITE), the number of blocks to be transferred, and an array of pointers to buffer heads describing the corresponding block buffers. The function iterates over all buffer heads; for each of them, it executes the following actions: 1. Tests and sets the BH_Lock flag of the buffer head; if the buffer was already locked, the data transfer has been activated by another kernel control path, so just skips the buffer by jumping to step 9. 2. Increases by one the usage counter b_count of the buffer head. 3. If the data transfer direction is WRITE, it sets the b_end_io method of the buffer head to point to the address of the end_buffer_write_sync( ) function; otherwise, it sets the b_end_io method to point to the address of the end_buffer_read_sync( ) function. 4. If the data transfer direction is WRITE, it tests and clears the BH_Dirty flag of the buffer head. If the flag was not set, there is no need to write the block on disk, so it jumps to step 7. 5. If the data transfer direction is READ or READA (read-ahead), it checks whether the BH_Uptodate flag of the buffer head is set; if so, there is no need to read the block from disk, so it jumps to step 7. 6. Here the block has to be read or written: it invokes the submit_bh( ) function to pass the buffer head to the generic block layer, then jumps to step 9. 7. Unlocks the buffer head by clearing the BH_Lock flag, and awakens every process that was waiting for the block being unlocked. 8. Decreases the b_count field of the buffer head. 9. If there are other buffer heads in the array to be processed, it selects the next one and jumps back to step 1; otherwise, it terminates. Notice that if the ll_rw_block( ) function passes a buffer head to the generic block layer, it leaves the buffer locked and its reference counter increased, so that the buffer cannot be accessed and cannot be freed until the data transfer completes. The kernel executes the b_end_io completion method of the buffer head when the data transfer for the block terminates. Assuming that there was no I/O error, the end_buffer_write_sync( ) and end_buffer_read_sync( ) functions simply set the BH_Uptodate field of the buffer head, unlock the buffer, and decrease its usage counter. 15.3. Writing Dirty Pages to Disk As we have seen, the kernel keeps filling the page cache with pages containing data of block devices. Whenever a process modifies some data, the corresponding page is marked as dirtythat is, its PG_dirty flag is set. Unix systems allow the deferred writes of dirty pages into block devices, because this noticeably improves system performance. Several write operations on a page in cache could be satisfied by just one slow physical update of the corresponding disk sectors. Moreover, write operations are less critical than read operations, because a process is usually not suspended due to delayed writings, while it is most often suspended because of delayed reads. Thanks to deferred writes, each physical block device will service, on the average, many more read requests than write ones. A dirty page might stay in main memory until the last possible moment that is, until system shutdown. However, pushing the delayed-write strategy to its limits has two major drawbacks: If a hardware or power supply failure occurs, the contents of RAM can no longer be retrieved, so many file updates that were made since the system was booted are lost. The size of the page cache, and hence of the RAM required to contain it, would have to be hugeat least as big as the size of the accessed block devices. Therefore, dirty pages are flushed (written) to disk under the following conditions: The page cache gets too full and more pages are needed, or the number of dirty pages becomes too large. Too much time has elapsed since a page has stayed dirty. A process requests all pending changes of a block device or of a particular file to be flushed; it does this by invoking a sync( ), fsync( ), or fdatasync( ) system call (see the section "The sync( ), fsync( ), and fdatasync( ) System Calls" later in this chapter). Buffer pages introduce a further complication. The buffer heads associated with each buffer page allow the kernel to keep track of the status of each individual block buffer. The PG_dirty flag of the buffer page should be set if at least one of the associated buffer heads has the BH_Dirty flag set. When the kernel selects a dirty buffer page for flushing, it scans the associated buffer heads and effectively writes to disk only the contents of the dirty blocks. As soon as the kernel flushes all dirty blocks in a buffer page to disk, it clears the PG_dirty flag of the page. 15.3.1. The pdflush Kernel Threads Earlier versions of Linux used a kernel thread called bdflush to systematically scan the page cache looking for dirty pages to flush, and they used a second kernel thread called kupdate to ensure that no page remains dirty for too long. Linux 2.6 has replaced both of them with a group of general purpose kernel threads called pdflush. These kernel threads have a flexible structure. They act on two parameters: a pointer to a function to be executed by the thread and a parameter for the function. The number of pdflush kernel threads in the system is dynamically adjusted: new threads are created when they are too few and existing threads are killed when they are too many. Because the functions executed by these kernel threads can block, creating several pdflush kernel threads instead of a single one, leads to better system performance. Births and deaths are governed by the following rules: There must be at least two pdflush kernel threads and at most eight. If there were no idle pdflush during the last second, a new pdflush should be created. If more than one second elapsed since the last pdflush became idle, a pdflush should be removed. Each pdflush kernel thread has a pdflush_work descriptor (see Table 15-6). The descriptors of idle pdflush kernel threads are collected in the pdflush_list list; the pdflush_lock spin lock protects that list from concurrent accesses in multiprocessor systems. The nr_pdflush_threads variable[*] stores the total number of pdflush kernel threads (idle and busy). Finally, the last_empty_jifs variable stores the last time (in jiffies) since the pdflush_list list of pdflush threads became empty. [*] The value of this variable can be read from the /proc/sys/vm/nr_pdflush_threads file. Table 15-6. The fields of the pdflush_work descriptor Type struct task_struct * void(*)(unsigned long) unsigned long struct list head Field who fn arg0 list when_i_went_to_sleep Description Pointer to kernel thread descriptor Callback function to be executed by the kernel thread Argument to callback function Links for the pdflush_list list Time in jiffies when kernel thread became available unsigned long Each pdflush kernel thread executes the _ _pdflush( ) function, which essentially loops in an endless cycle until the kernel thread dies. Let's suppose that the pdflush kernel thread is idle; then, the process is sleeping in TASK_INTERRUPTIBLE state. As soon as the kernel thread is woken up, _ _pdflush( ) accesses its pdflush_work descriptor and executes the callback function stored in the fn field, passing to it the argument stored in the arg0 field. When the callback function terminates, _ _pdflush( ) checks the value of the last_empty_jifs variable: if there was no idle pdflush kernel thread for more than one second and if there are less than eight pdflush kernel threads, _ _pdflush( ) starts another kernel thread. Otherwise, if the last entry in the pdflush_list list is idle for more than one second, and there are more than two pdflush kernel threads, _ _pdflush( ) terminates: as explained in the section "Kernel Threads" in Chapter 3, the corresponding kernel thread executes the _exit( ) system call and it is thus destroyed. Otherwise, _ _pdflush( ) reinserts the pdflush_work descriptor of the kernel thread in the pdflush_list list and puts the kernel thread to sleep. The pdflush_operation( ) function is used to activate an idle pdflush kernel thread. This function acts on two parameters: a pointer fn to the function that must be executed and an argument arg0; it performs the following steps: 1. Extracts from the pdflush_list list a pointer pdf to the pdflush_work descriptor of an idle pdflush kernel thread. If the list is empty, it returns -1. If the list contained just one element, it sets the value of the last_empty_jifs variable to jiffies. 2. Stores in pdf->fn and in pdf->arg0 the parameters fn and arg0. 3. Invokes wake_up_process( ) to wake up the idle pdflush kernel thread, that is, pdf>who. What kinds of jobs are delegated to the pdflush kernel threads? There are a few of them, all related to flushing of dirty data. In particular, pdflush usually executes one of the following callback functions: background_writeout( ): systematically walks the page cache looking for dirty pages to be flushed (see the next section "Looking for Dirty Pages To Be Flushed"). wb_kupdate( ): checks that no page in the page cache remains dirty for too long (see the section "Retrieving Old Dirty Pages" later in this chapter). 15.3.2. Looking for Dirty Pages To Be Flushed Every radix tree could include dirty pages to be flushed. Retrieving all of them thus involves an exhaustive search among all address_space objects associated with inodes having an image on disk. Because the page cache might include a large number of pages, scanning the whole cache in a single run might keep the CPU and the disks busy for a long time. Therefore, Linux adopts a sophisticated mechanism that splits the page cache scanning in several runs of execution. The wakeup_bdflush( ) function receives as argument the number of dirty pages in the page cache that should be flushed; the value zero means that all dirty pages in the cache should be written back to disk. The function invokes pdflush_operation( ) to wake up a pdflush kernel thread (see the previous section) and delegate to it the execution of the background_writeout( ) callback function. The latter function effectively retrieves the specified number of dirty pages from the page cache and writes them back to disk. The wakeup_bdflush( ) function is executed when either memory is scarce or a user makes an explicit request for a flush operation. In particular, the function is invoked when: The User Mode process issues a sync( ) system call (see the section "The sync( ), fsync( ), and fdatasync( ) System Calls" later in this chapter). The grow_buffers( ) function fails to allocate a new buffer page (see the earlier section "Allocating Block Device Buffer Pages"). The page frame reclaiming algorithm invokes free_more_memory( ) or TRy_to_free_pages( ) (see Chapter 17). The mempool_alloc( ) function fails to allocate a new memory pool element (see the section "Memory Pools" in Chapter 8). Moreover, a pdflush kernel thread executing the background_writeout( ) callback function is woken up by every process that modifies the contents of pages in the page cache and causes the fraction of dirty pages to rise above some dirty background threshold. The background threshold is typically set to 10% of all pages in the system, but its value can be adjusted by writing in the /proc/sys/vm/dirty_background_ratio file. The background_writeout( ) function relies on a writeback_control structure, which acts as a two-way communication device: on one hand, it tells an auxiliary function called writeback_inodes( ) what to do; on the other hand, it stores some statistics about the number of pages written to disk. The most important fields of this structure are the following: sync_mode Specifies the synchronization mode: WB_SYNC_ALL means that if a locked inode is encountered, it must be waited upon and not just skipped over; WB_SYNC_HOLD means that locked inodes are put in a list for later consideration; and WB_SYNC_NONE means that locked inodes are simply skipped. bdi If not NULL, it points to a backing_dev_info structure; in this case, only dirty pages belonging to the underlying block device will be flushed. older_than_this If not null, it means that inodes younger than the specified value should be skipped. nr_to_write Number of dirty pages yet to be written in this run of execution. nonblocking If this flag is set, the process cannot be blocked. The background_writeout( ) function acts on a single parameter: nr_pages, the minimum number of pages that should be flushed to disk. It essentially executes the following steps: 1. Reads from the page_state per-CPU variable the number of pages and dirty pages currently stored in the page cache. If the fraction of dirty pages is below a given threshold and at least nr_pages have been flushed to disk, the function terminates. The value of this threshold is typically set to about 40% of the number of pages in the system; it could be adjusted by writing into the /proc/sys/vm/dirty_ratio file. 2. Invokes writeback_inodes( ) to try to write 1, 024 dirty pages (see below). 3. Checks the number of pages effectively written and decreases the number of pages yet to be written. 4. If less than 1,024 pages have been written or if pages have been skipped, probably the request queue of the block device is congested: the function puts the current process to sleep in a special wait queue for 100 milliseconds or until the queue becomes uncongested. 5. Goes back to step 1. The writeback_inodes( ) function acts on a single parameter, namely a pointer wbc to a writeback_control descriptor. The nr_to_write field of this descriptor contains the number of pages to be flushed to disk. When the function returns, the same field contains the number of pages remaining to be flushed; if everything went smoothly, this field will be set to 0. Let us suppose that writeback_inodes( ) is called with the wbc->bdi and wbc>older_than_this pointers set to NULL, the WB_SYNC_NONE synchronization mode, and the wbc>nonblocking flag setthese are the values set by background_writeout( ). The function scans the list of superblocks rooted at the super_blocks variable (see the section "Superblock Objects" in Chapter 12). The scanning ends when either the whole list has been traversed, or the target number of pages to be flushed has been reached. For each superblock sb, the function executes the following steps: 1. Checks whether the sb->s_dirty or sb->s_io lists are empty: the first list collects the dirty inodes of the superblock, while the second list collects the inodes waiting to be transferred to disk (see below). If both lists are empty, the inodes on this filesystem have no dirty pages, so the function considers the next superblock in the list. 2. Here the superblock has dirty inodes. Invokes sync_sb_inodes( ) on the sb superblock. This function: a. Puts all the inodes of sb->s_dirty into the list pointed to by sb->s_io and clears the list of dirty inodes. b. Gets the next inode pointer from sb->s_io. If this list is empty, it returns. c. If the inode was dirtied after sync_sb_inodes( ) started, it skips the inode's dirty pages and returns. Notice that some dirty inodes might remain in the sb->s_io list. d. If the current process is a pdflush kernel thread, it checks whether another pdflush kernel thread running on another CPU is already trying to flush dirty pages for files belonging to this block device. This can be done by an atomic test and set operation on the BDI_pdflush flag of the inode's backing_dev_info. Essentially, it is pointless to have more than one pdflush kernel thread on the same request queue (see the section "The pdflush Kernel Threads" earlier in this chapter). e. Increases by one the inode's usage counter. f. Invokes _ _writeback_single_inode( ) to write back the dirty buffers associated with the selected inode: 1. If the inode is locked, it moves inode into the list of dirty inodes (inode->i_sb->s_dirty) and returns 0. (Since we are assuming that the wbc->sync_mode field is not WB_SYNC_ALL, the function does not block waiting for the inode to unlock.) 2. Uses the writepages method of the inode's address space, or the mpage_writepages( ) function if no such method exists, to write up to wbc->nr_to_write dirty pages. This function uses the find_get_pages_tag( ) function to retrieve quickly all dirty pages in the inode's address space (see the section "The Tags of the Radix Tree" earlier in this chapter). Details will be given in the next chapter. 3. If the inode is dirty, it uses the superblock's write_inode method to write the inode to disk. The functions that implement this method usually rely on submit_bh( ) to transfer a single block of data (see the section "Submitting Buffer Heads to the Generic Block Layer" earlier in this chapter). 4. Checks the status of the inode; accordingly, moves the inode back into the sb->s_dirty list if some page of the inode is still dirty, or in the inode_unused list if the inode's reference counter is zero, or in the inode_in_use list otherwise (see the section "Inode Objects" in Chapter 12). 5. Returns the error code of the function invoked in step 2f2. g. Back into the sync_sb_inodes( ) function. If the current process is the pdflush kernel thread, it clears the BDI_pdflush flag set in step 2d. h. If some pages were skipped in the inode just processed, then the inode includes locked buffers: moves all inodes remaining in the sb->s_io list back into the sb->s_dirty list: they will be reconsidered at a later time. i. Decreases by one the usage counter of the inode. j. If wbc->nr_to_write is greater than 0, goes back to step 2b to look for other dirty inodes of the same superblock. Otherwise, the sync_sb_inodes( ) function terminates. 3. Back into the writeback_inodes( ) function. If wbc->nr_to_write is greater than zero, it jumps to step 1 and continues with the next superblock in the global list. Otherwise, it returns. 15.3.3. Retrieving Old Dirty Pages As stated earlier, the kernel tries to avoid the risk of starvation that occurs when some pages are not flushed for a long period of time. Hence, if a page remains dirty for a predefined amount of time, the kernel explicitly starts an I/O data transfer that writes its contents to disk. The job of retrieving old dirty pages is delegated to a pdflush kernel thread that is periodically woken up. During the kernel initialization, the page_writeback_init( ) function sets up the wb_timer dynamic timer so that it decays after dirty_writeback_centisecs hundreds of a second (usually 500, but this value can be adjusted by writing in the /proc/sys/vm/dirty_writeback_centisecs file). The timer function, which is called wb_timer_fn( ), essentially invokes the pdflush_operation( ) function passing to it the address of the wb_kupdate( ) callback function. The wb_kupdate( ) function walks the page cache looking for "old" dirty inodes; it executes the following steps: 1. Invokes the sync_supers( ) function to write the dirty superblocks to disk (see the next section). Although not strictly related to the flushing of the pages in the page cache, this invocation ensures that no superblock remains dirty for more than, usually, five seconds. 2. Stores in the older_than_this field of a writeback_control descriptor a pointer to a value in jiffies corresponding to the current time minus 30 seconds. Thirty seconds is the longest time for which a page is allowed to remain dirty. 3. Determines from the per-CPU page_state variable the rough number of dirty pages currently in the page cache. 4. Invokes repeatedly writeback_inodes( ) until either the number of pages written to disk reaches the value determined in the previous step, or all pages older than 30 seconds have been written. During this cycle the function might sleep if some request queue becomes congested. 5. Uses mod_timer( ) to restart the wb_timer dynamic timer: it will decay once again dirty_writeback_centisecs hundreds of seconds since the invocation of this function (or one second since now if this execution lasted too long). 15.4. The sync( ), fsync( ), and fdatasync( ) System Calls In this section, we examine briefly the three system calls available to user applications to flush dirty buffers to disk: sync( ) Allows a process to flush all dirty buffers to disk fsync( ) Allows a process to flush all blocks that belong to a specific open file to disk fdatasync( ) Very similar to fsync( ), but doesn't flush the inode block of the file 15.4.1. The sync ( ) System Call The service routine sys_sync( ) of the sync( ) system call invokes a series of auxiliary functions: wakeup_bdflush(0); sync_inodes(0); sync_supers( ); sync_filesystems(0); sync_filesystems(1); sync_inodes(1); As described in the previous section, wakeup_bdflush( ) starts a pdflush kernel thread, which flushes to disk all dirty pages contained in the page cache. The sync_inodes( ) function scans the list of superblocks looking for dirty inodes to be flushed; it acts on a wait parameter that specifies whether it must wait until flushing has been performed or not. The function scans the superblocks of all currently mounted filesystems; for each superblock containing dirty inodes, sync_inodes( ) first invokes sync_sb_inodes( ) to flush the corresponding dirty pages (we described this function earlier in the section "Looking for Dirty Pages To Be Flushed"), then invokes sync_blockdev( ) to explicitly flush the dirty buffer pages owned by the block device that includes the superblock. This is done because the write_inode superblock method of many disk-based filesystems simply marks the block buffer corresponding to the disk inode as dirty; the sync_blockdev( ) function makes sure that the updates made by sync_sb_inodes( ) are effectively written to disk. The sync_supers( ) function writes the dirty superblocks to disk, if necessary, by using the proper write_super superblock operations. Finally, the sync_filesystems( ) executes the sync_fs superblock method for all writable filesystems. This method is simply a hook offered to a filesystem in case it needs to perform some peculiar operation at each sync; this method is only used by journaling filesystems such as Ext3 (see Chapter 18). Notice that sync_inodes( ) and sync_filesystems( ) are invoked twice, once with the wait parameter equal to 0 and the second time with the parameter equal to 1. This is done on purpose: first, they quickly flush to disk the unlocked inodes; next, they wait for each locked inode to become unlocked and finish writing them one by one. 15.4.2. The fsync ( ) and fdatasync ( ) System Calls The fsync( ) system call forces the kernel to write to disk all dirty buffers that belong to the file specified by the fd file descriptor parameter (including the buffer containing its inode, if necessary). The corresponding service routine derives the address of the file object and then invokes the fsync method. Usually, this method ends up invoking the _ _writeback_single_inode( ) function to write back both the dirty pages associated with the selected inode and the inode itself (see the section "Looking for Dirty Pages To Be Flushed" earlier in this chapter). The fdatasync( ) system call is very similar to fsync( ), but writes to disk only the buffers that contain the file's data, not those that contain inode information. Because Linux 2.6 does not have a specific file method for fdatasync( ), this system call uses the fsync method and is thus identical to fsync( ). Chapter 17. Page Frame Reclaiming In previous chapters, we explained how the kernel handles dynamic memory by keeping track of free and busy page frames. We have also discussed how every process in User Mode has its own address space and has its requests for memory satisfied by the kernel one page at a time, so that page frames can be assigned to the process at the very last possible moment. Last but not least, we have shown how the kernel makes use of dynamic memory to implement both memory and disk caches . In this chapter, we complete our description of the virtual memory subsystem by discussing page frame reclaiming. We'll start in the first section, "The Page Frame Reclaiming Algorithm," explaining why the kernel needs to reclaim page frames and what strategy it uses to achieve this. We then make a technical digression in the section "Reverse Mapping" to discuss the data structures used by the kernel to locate quickly all the Page Table entries that point to the same page frame. The section "Implementing the PFRA" is devoted to the page frame reclaiming algorithm used by Linux. The last main section, "Swapping," is almost a chapter by itself: it covers the swap subsystem, a kernel component used to save anonymous (not mapping data of files) pages on disk. 17.1. The Page Frame Reclaiming Algorithm One of the fascinating aspects of Linux is that the checks performed before allocating dynamic memory to User Mode processes or to the kernel are somewhat perfunctory. No rigorous check is made, for instance, on the total amount of RAM assigned to the processes created by a single user (the limits mentioned in the section "Process Resource Limits" in Chapter 3 mostly affect single processes). Similarly, no limit is placed on the size of the many disk caches and memory caches used by the kernel. This lack of controls is a design choice that allows the kernel to use the available RAM in the best possible way. When the system load is low, the RAM is filled mostly by the disk caches and the few running processes can benefit from the information stored in them. However, when the system load increases, the RAM is filled mostly by pages of the processes and the caches are shrunken to make room for additional processes. As we saw in previous chapters, both memory and disk caches grab more and more page frames but never release any of them. This is reasonable because cache systems don't know if and when processes will reuse some of the cached data and are therefore unable to identify the portions of cache that should be released. Moreover, thanks to the demand paging mechanism described in Chapter 9, User Mode processes get page frames as long as they proceed with their execution; however, demand paging has no way to force processes to release the page frames whenever they are no longer used. Thus, sooner or later all the free memory will be assigned to processes and caches. The page frame reclaiming algorithm of the Linux kernel refills the lists of free blocks of the buddy system by "stealing" page frames from both User Mode processes and kernel caches. Actually, page frame reclaiming must be performed before all the free memory has been used up. Otherwise, the kernel might be easily trapped in a deadly chain of memory requests that leads to a system crash. Essentially, to free a page frame the kernel must write its data to disk; however, to accomplish this operation, the kernel requires another page frame (for instance, to allocate the buffer heads for the I/O data transfer). If no free page frame exists, no page frame can be freed. One of the goals of page frame reclaiming is thus to conserve a minimal pool of free page frames so that the kernel may safely recover from "low on memory" conditions. 17.1.1. Selecting a Target Page The objective of the page frame reclaiming algorithm (PFRA ) is to pick up page frames and make them free. Clearly the page frames selected by the PFRA must be non-free , that is, they must not be already included in one of the free_area arrays used by the buddy system (see the section "The Buddy System Algorithm" in Chapter 8). The PFRA handles the page frames in different ways, according to their contents. We can distinguish between unreclaimable pages, swappable pages, syncable pages, and discardable pages. These types are explained in Table 17-1. Table 17-1. The types of pages considered by the PFRA Type of pages Description Free pages (included in buddy system lists) Reserved pages (with PG_reserved flag set) Pages dynamically allocated by the kernel Unreclaimable Pages in the Kernel Mode stacks of the processes Temporarily locked pages (with PG_locked flag set) Reclaim action (No reclaiming allowed or needed) Memory locked pages (in memory regions with VM_LOCKED flag set) Anonymous pages in User Mode address spaces Swappable Mapped pages of tmpfs filesystem (e.g., pages of IPC shared memory) Syncable Mapped pages in User Mode address Save the page contents in a swap area Synchronize the page with its image on disk, if necessary Table 17-1. The types of pages considered by the PFRA Type of pages Description spaces Pages included in the page cache and containing data of disk files Block device buffer pages Pages of some disk caches (e.g., the inode cache ) Unused pages included in memory caches (e.g., slab allocator caches) Unused pages of the dentry cache Reclaim action Discardable Nothing to be done In the above table, a page is said to be mapped if it maps a portion of a file. For instance, all pages in the User Mode address spaces belonging to file memory mappings are mapped, as well as any other page included in the page cache. In almost all cases, mapped pages are syncable: in order to reclaim the page frame, the kernel must check whether the page is dirty and, if necessary, write the page contents in the corresponding disk file. Conversely, a page is said to be anonymous if it belongs to an anonymous memory region of a process (for instance, all pages in the User Mode heap or stack of a process are anonymous). In order to reclaim the page frame, the kernel must save the page contents in a dedicated disk partition or disk file called "swap area" (see the later section "Swapping"); therefore, all anonymous pages are swappable. Usually, the pages of special filesystems are not reclaimable. The only exceptions are the pages of the tmpfs special filesystem, which can be reclaimed by saving them in a swap area. As we'll see in Chapter 19, the tmpfs special filesystem is used by the IPC shared memory mechanism. When the PFRA must reclaim a page frame belonging to the User Mode address space of a process, it must take into consideration whether the page frame is shared or non-shared . A shared page frame belongs to multiple User Mode address spaces, while a non-shared page frame belongs to just one. Notice that a non-shared page frame might belong to several lightweight processes referring to the same memory descriptor. Shared page frames are typically created when a process spawns a child; as explained in the section "Copy On Write" in Chapter 9, the page tables of the child are copied from those of the parent, thus parent and child share the same page frames. Another common case occurs when two or more processes access the same file by means of a shared memory mapping (see the section "Memory Mapping" in Chapter 16).[*] [*] It should be noted, however, that when a single process accesses a file through a shared memory mapping, the corresponding pages are non-shared as far as the PFRA is concerned. Similarly, a page belonging to a private memory mapping may be treated as shared by the PFRA (for instance, because two processes read the same file portion and none of them modified the data in the page). 17.1.2. Design of the PFRA While it is easy to identify the page candidates for memory reclaimingroughly speaking, any page belonging to a disk or memory cache, or to the User Mode address space of a processselecting the proper target pages is perhaps the most sensitive issue in kernel design. As a matter of fact, the hardest job of a developer working on the virtual memory subsystem consists of finding an algorithm that ensures acceptable performance both for desktop machines (on which memory requests are quite limited but system responsiveness is crucial) and for high-level machines such as large database servers (on which memory requests tend to be huge). Unfortunately, finding a good page frame reclaiming algorithm is a rather empirical job, with very little support from theory. The situation is somewhat similar to evaluating the factors that determine the dynamic priority of a process: the main objective is to tune the parameters in such a way to achieve good system performance, without asking too many questions about why it works well. Often, it's just a matter of "let's try this approach and see what happens." An unpleasant side effect of this empirical design is that the code changes quickly. For that reason, we cannot ensure that the memory reclaiming algorithm we are going to describethe one used in Linux 2.6.11will be exactly the same, by the time you'll read this chapter, as the one adopted by the most up-to-date version of the Linux 2.6 kernel. However, the general ideas and the main heuristic rules described here should continue to hold. Looking too close to the trees' leaves might lead us to miss the whole forest. Therefore, let us present a few general rules adopted by the PFRA. These rules are embedded in the functions that will be described later in this chapter. Free the "harmless" pages first Pages included in disk and memory caches not referenced by any process should be reclaimed before pages belonging to the User Mode address spaces of the processes; in the former case, in fact, the page frame reclaiming can be done without modifying any Page Table entry. As we will see in the section "The Least Recently Used (LRU) Lists" later in this chapter, this rule is somewhat mitigated by introducing a "swap tendency factor." Make all pages of a User Mode process reclaimable With the exception of locked pages, the PFRA must be able to steal any page of a User Mode process, including the anonymous pages. In this way, processes that have been sleeping for a long period of time will progressively lose all their page frames. Reclaim a shared page frame by unmapping at once all page table entries that reference it When the PFRA wants to free a page frame shared by several processes, it clears all page table entries that refer to the shared page frame, and then reclaims the page frame. Reclaim "unused" pages only The PFRA uses a simplified Least Recently Used (LRU) replacement algorithm to classify pages as in-use and unused.[*] If a page has not been accessed for a long time, the probability that it will be accessed in the near future is low and it can be considered "unused;" on the other hand, if a page has been accessed recently, the probability that it will continue to be accessed is high and it must be considered as "in-use." The PFRA reclaims only unused pages. This is just another application of the locality principle mentioned in the section "Hardware Cache" in Chapter 2. [*] The PFRA could also be considered as a "used-once" algorithm, which has its roots in the 2Q buffer management replacement algorithm proposed by T. Johnson and D. Shasha in 1994. The main idea behind the LRU algorithm is to associate a counter storing the age of the page with each page in RAMthat is, the interval of time elapsed since the last access to the page. This counter allows the PFRA to reclaim only the oldest page of any process. Some computer platforms provide sophisticated support for LRU algorithms;[ ] unfortunately, 80 x 86 processors do not offer such a hardware feature, thus the Linux kernel cannot rely on a page counter that keeps track of the age of every page. To cope with this restriction, Linux takes advantage of the Accessed bit included in each Page Table entry, which is automatically set by the hardware when the page is accessed; moreover, the age of a page is represented by the position of the page descriptor in one of two different lists (see the section "The Least Recently Used (LRU) Lists" later in this chapter). [ ] For instance, the CPUs of some mainframes automatically update the value of a counter included in each page table entry to specify the age of the corresponding page. Therefore, the page frame reclaiming algorithm is a blend of several heuristics: Careful selection of the order in which caches are examined. Ordering of pages based on aging (least recently used pages should be freed before pages accessed recently). Distinction of pages based on the page state (for example, non-dirty pages are better candidates than dirty pages because they don't have to be written to disk). 17.2. Reverse Mapping As stated in the previous section, one of the objectives of the PFRA is to be able to free a shared page frame. To that end, the Linux 2.6 kernel is able to locate quickly all the Page Table entries that point to the same page frame. This activity is called reverse mapping . A trivial solution for reverse mapping would be to include in each page descriptor additional fields to link together all the Page Table entries that point to the page frame associated with the page descriptor. However, keeping such lists up-to-date would increase significantly the kernel overhead; for that reason, more sophisticated solutions have been devised. The technique used in Linux 2.6 is named object-based reverse mapping. Essentially, for any reclaimable User Mode page, the kernel stores the backward links to all memory regions in the system (the "objects") that include the page itself. Each memory region descriptor stores a pointer to a memory descriptor, which in turn includes a pointer to a Page Global Directory. Therefore, the backward links enable the PFRA to retrieve all Page Table entries referencing a given a page. Because there are fewer memory region descriptors than page descriptors, updating the backward links of a shared page is less time consuming. Let's see how this scheme is worked out. First of all, the PFRA must have a way to determine whether the page to be reclaimed is shared or non-shared, and whether it is mapped or anonymous. In order to do this, the kernel looks at two fields of the page descriptor: _mapcount and mapping. The _mapcount field stores the number of Page Table entries that refer to the page frame. The counter starts from -1: this value means that no Page Table entry references the page frame. Thus, if the counter is zero, the page is non-shared, while if it is greater than zero the page is shared. The page_mapcount( ) function receives the address of a page descriptor and returns the value of its _mapcount plus one (thus, for instance, it returns one for a nonshared page included in the User Mode address space of some process). The mapping field of the page descriptor determines whether the page is mapped or anonymous, as follows: If the mapping field is NULL, the page belongs to the swap cache (see the section "The Swap Cache" later in this chapter). If the mapping field is not NULL and its least significant bit is 1, it means the page is anonymous and the mapping field encodes the pointer to an anon_vma descriptor (see the next section, "Reverse Mapping for Anonymous Pages"). If the mapping field is non-NULL and its least significant bit is 0, the page is mapped; the mapping field points to the address_space object of the corresponding file (see the section "The address_space Object" in Chapter 15). Every address_space object used by Linux is aligned in RAM so that its starting linear address is a multiple of four. Therefore, the least significant bit of the mapping field can be used as a flag denoting whether the field contains a pointer to an address_space object or to an anon_vma descriptor. This is a dirty programming trick, but the kernel uses a lot of page descriptors, thus these data structures should be as small as possible. The PageAnon( ) function receives as its parameter the address of a page descriptor and returns 1 if the least significant bit of the mapping field is set, 0 otherwise. The TRy_to_unmap( ) function, which receives as its parameter a pointer to a page descriptor, tries to clear all the Page Table entries that point to the page frame associated with that page descriptor. The function returns SWAP_SUCCESS (zero) if the function succeeded in removing any reference to the page frame from all Page Table entries, it returns SWAP_AGAIN (one) if some reference could not be removed, and returns SWAP_FAIL (two) in case of errors. The function is quite short: int try_to_unmap(struct page *page) { int ret; if (PageAnon(page)) ret = try_to_unmap_anon(page); else ret = try_to_unmap_file(page); if (!page_mapped(page)) ret = SWAP_SUCCESS; return ret; } The TRy_to_unmap_anon( ) and try_to_unmap_file( ) functions take care of anonymous pages and mapped pages, respectively. These functions will be described in the forthcoming sections. 17.2.1. Reverse Mapping for Anonymous Pages Anonymous pages are often shared among several processes. The most common case occurs when forking a new process: as explained in the section "Copy On Write" in Chapter 9, all page frames owned by the parentincluding the anonymous pagesare assigned also to the child. Another (quite unusual) case occurs when a process creates a memory region specifying both the MAP_ANONYMOUS and MAP_SHARED flag: the pages of such a region will be shared among the future descendants of the process. The strategy to link together all the anonymous pages that refer to the same page frame is simple: the anonymous memory regions that include the page frame are collected in a doubly linked circular list. Be warned that, even if an anonymous memory region includes different pages, there always is just one reverse mapping list for all the page frames in the region. When the kernel assigns the first page frame to an anonymous region, it creates a new anon_vma data structure, which includes just two fields: lock, a spin lock for protecting the list against race conditions, and head, the head of the doubly linked circular list of memory region descriptors. Then, the kernel inserts the vm_area_struct descriptor of the anonymous memory region in the anon_vma's list; to that end, the vm_area_struct data structure includes two fields related to this list: anon_vma_node stores the pointers to the next and previous elements in the list, while anon_vma points to the anon_vma data structure. Finally, the kernel stores the address of the anon_vma data structure in the mapping field of the descriptor of the anonymous page, as described previously. See Figure 17-1. When a page frame already referenced by one process is inserted into a Page Table entry of another process (for instance, as a consequence of a fork( ) system call, see Figure 17-1. Object-based reverse mapping for anonymous pages the section "The clone( ), fork( ), and vfork( ) System Calls" in Chapter 3); the kernel simply inserts the anonymous memory region of the second process in the doubly linked circular list of the anon_vma data structure pointed to by the anon_vma field of the first process's memory region. Therefore, any anon_vma's list typically includes memory regions owned by different processes.[*] [*] An anon_vma's list may also include several adjacent anonymous memory regions owned by the same process. Usually this occurs when an anonymous memory region is split in two or more regions by the mprotect( ) system call. As shown in Figure 17-1, the anon_vma's list allows the kernel to quickly locate all Page Table entries that refer to the same anonymous page frame. In fact, each region descriptor stores in the vm_mm field the address of the memory descriptor, which in turn includes a field pgd containing the address of the Page Global Directory of the process. The Page Table entry can then be determined by considering the starting linear address of the anonymous page, which is easily obtained from the memory region descriptor and the index field of the page descriptor. 17.2.1.1. The try_to_unmap_anon( ) function When reclaiming an anonymous page frame, the PFRA must scan all memory regions in the anon_vma's list and carefully check whether each region actually includes an anonymous page whose underlying page frame is the target page frame. This job is done by the try_to_unmap_anon( ) function, which receives as its parameter the descriptor of the target page frame and performs essentially the following steps: 1. Acquires the lock spin lock of the anon_vma data structure pointed to by the mapping field of the page descriptor. 2. Scans the anon_vma's list of memory region descriptors; for each vma memory region descriptor found in that list, it invokes the try_to_unmap_one( ) function passing as parameters vma and the page descriptor (see below). If for some reason this function returns a SWAP_FAIL value, or if the _mapcount field of the page descriptor indicates that all Page Table entries referencing the page frame have been found, the scanning terminates before reaching the end of the list. 3. Releases the spin lock obtained in step 1. 4. Returns the value computed by the last invocation of TRy_to_unmap_one( ): SWAP_AGAIN (partial success) or SWAP_FAIL (failure). 17.2.1.2. The try_to_unmap_one( ) function The TRy_to_unmap_one( ) function is called repeatedly both from try_to_unmap_anon( ) and from TRy_to_unmap_file( ). It acts on two parameters: a pointer page to a target page descriptor and a pointer vma to a memory region descriptor. The function essentially performs the following actions: 1. Computes the linear address of the page to be reclaimed from the starting linear address of the memory region (vma->vm_start), the offset of the memory region in the mapped file (vma->vm_pgoff), and the offset of the page inside the mapped file (page->index). For anonymous pages, the vma->vm_pgoff field is either zero or equal to vm_start/PAGE_SIZE; correspondingly, the page->index field is either the index of the page inside the region or the linear address of the page divided by PAGE_SIZE. 2. If the target page is anonymous, it checks whether its linear address falls inside the memory region; if not, it terminates by returning SWAP_AGAIN. (As explained when introducing reverse mapping for anonymous pages, the anon_vma's list may include memory regions that do not contain the target page.) 3. Gets the address of the memory descriptor from vma->vm_mm, and acquires the vma>vm_mm->page_table_lock spin lock that protects the page tables. 4. Invokes successively pgd_offset( ), pud_offset( ), pmd_offset( ), and pte_offset_map( ) to get the address of the Page Table entry that corresponds to the linear address of the target page. 5. Performs a few checks to verify that the target page is effectively reclaimable. If any of the following checks fails, the function jumps to step 12 to terminate by returning a proper error number, either SWAP_AGAIN or SWAP_FAIL: a. Checks that the Page Table entry points to the target page; if not, the function returns SWAP_AGAIN. This can happen in the following cases: ! The Page Table entry refers to a page frame assigned with COW , but the anonymous memory region identified by vma still belongs to the anon_vma list of the original page frame. ! The mremap( ) system call may remap memory regions and move the pages into the User Mode address space by directly modifying the page table entries. In this particular case, object-based reverse mapping does not work, because the index field of the page descriptor cannot be used to determine the actual linear address of the page. ! The file memory mapping is non-linear (see the section "Non-Linear Memory Mappings" in Chapter 16). b. Checks that the memory region is not locked (VM_LOCKED) or reserved (VM_RESERVED); if one of these restrictions is in place, the function returns SWAP_FAIL. c. Checks that the Accessed bit inside the Page Table entry is cleared; if not, the function clears the bit and returns SWAP_FAIL. If the Accessed bit is set, the page is considered in-use, thus it should not be reclaimed. d. Checks whether the page belongs to the swap cache (see the section "The Swap Cache" later in this chapter) and it is currently being handled by get_user_pages( ) (see the section "Allocating a Linear Address Interval" in Chapter 9); in this case, to avoid a nasty race condition, the function returns SWAP_FAIL. 6. The page can be reclaimed: if the Dirty bit in the Page Table entry is set, sets the PG_dirty flag of the page. 7. Clears the Page Table entry and flushes the corresponding TLBs. 8. If the page is anonymous, the function inserts a swapped-out page identifier in the Page Table entry so that further accesses to this page will swap in the page (see the section "Swapping" later in this chapter). Moreover, it decreases the counter of anonymous pages stored in the anon_rss field of the memory descriptor. 9. Decreases the counter of page frames allocated to the process stored in the rss field of the memory descriptor. 10. Decreases the _mapcount field of the page descriptor, because a reference to this page frame in the User Mode Page Table entries has been deleted. 11. Decreases the usage counter of the page frame, which is stored in the _count field of the page descriptor. If the counter becomes negative, it removes the page descriptor from the active or inactive list (see the section "The Least Recently Used (LRU) Lists" later in this chapter), and invokes free_hot_page( ) to release the page frame (see the section "The Per-CPU Page Frame Cache" in Chapter 8). 12. Invokes pte_unmap( ) to release the temporary kernel mapping that could have been allocated by pte_offset_map( ) in step 4 (see the section "Kernel Mappings of HighMemory Page Frames" in Chapter 8). 13. Releases the vma->vm_mm->page_table_lock spin lock acquired in step 3. 14. Returns the proper error code (SWAP_AGAIN in case of success). 17.2.2. Reverse Mapping for Mapped Pages As with anonymous pages, object-based reverse mapping for mapped pages is based on a simple idea: it is always possible to retrieve the Page Table entries that refer to a given page frame by accessing the descriptors of the memory regions that include the corresponding mapped pages. Thus, the core of reverse mapping is a clever data structure that collects all memory region descriptors relative to a given page frame. We have seen in the previous section that descriptors for anonymous memory regions are collected in doubly linked circular lists; retrieving all page table entries referencing a given page frame involves a linear scanning of the elements in the list. The number of shared anonymous page frames is never very large, hence this approach works well. Contrary to anonymous pages, mapped pages are frequently shared, because many different processes may share the same pages of code. For instance, consider that nearly all processes in the system share the pages containing the code of the standard C library (see the section "Libraries" in Chapter 20). For this reason, Linux 2.6 relies on special search trees, called "priority search trees ," to quickly locate all the memory regions that refer to the same page frame. There is a priority search tree for every file; its root is stored in the i_mmap field of the address_space object embedded in the file's inode object. It is always possible to quickly retrieve the root of the search tree, because the mapping field in the descriptor of a mapped page points to the address_space object. 17.2.2.1. The priority search tree The priority search tree (PST) used by Linux 2.6 is based on a data structure introduced by Edward McCreight in 1985 to represent a set of overlapping intervals. McCreight's tree is a hybrid of a heap and a balanced search tree, and it is used to perform queries on the set of intervalse.g., "what intervals are contained in a given interval?" and "what intervals intersect a given interval?"in an amount of time directly proportional to the height of the tree and the number of intervals in the answer. Each interval in a PST corresponds to a node of the tree, and it is characterized by two indices: the radix index, which corresponds to the starting point of the interval, and the heap index, which corresponds to the final point. The PST is essentially a search tree on the radix index, with the additional heap-like property that the heap index of a node is never smaller than the heap indices of its children. The Linux priority search tree differs from McCreight's data structure in two important aspects: first, the Linux tree is not always kept balanced (the balancing algorithm is costly both in memory space and in execution time); second, the Linux tree is adapted so as to store memory regions instead of linear intervals. Each memory region can be considered as an interval of file pages identified by the initial position in the file (the radix index) and the final position (the heap index). However, memory regions tend to start from the same pages (typically, from page index 0). Unfortunately, McCreight's original data structure cannot store intervals having the very same starting point. As a partial solution, each node of a PST carries an additional size indexother than the radix and heap indicescorresponding to the size of the memory region in pages minus one. The size index allows the search program to distinguish different memory regions that start at the same file position. The size index, however, increases significantly the number of different nodes that may end up in a PST. In particular, if there are too many nodes having the same radix index but different heap indices, the PST could not contain all of them. To solve this problem, the PST may include overflow subtrees rooted at the leaves of the PST and containing nodes having a common radix tree. Furthermore, different processes may own memory regions that map exactly the same portion of the same file (just consider the example of the standard C library mentioned above). In that case, all nodes corresponding to these memory regions have the same radix, heap, and size indices . When the kernel must insert in a PST a memory region having the same indices as the ones of a node already existing, it inserts the memory region descriptor in a doubly linked circular list rooted at the older PST node. Figure 17-2 shows a simple example of priority search tree. In the left side of the figure, we show seven memory regions covering the first six pages of a file; each interval is labeled with the radix index, size index, and heap index. In the right side of the figure, we draw the corresponding PST. Notice that no child node has a heap index greater than the heap index of the parent. Also observe that the radix index of the left child of any node is never greater than the radix index of the right child; in case of tie between the radix indices, the ordering is given by the size index. Let us suppose that the PFRA must retrieve all memory regions that include the page at index five. The search algorithm starts at the root (0,5,5): because the corresponding interval includes the page, this is the first retrieved memory region. Then the algorithm visits the left child (0,4,4) of the root and compares the heap index (four) with the page index: because the heap index is smaller, the interval does not include the page; moreover, thanks to the heap-like property of the PST, none of the children of this node can include the page. Thus the algorithm directly jumps to the right child (2,3,5) of the root. The corresponding interval includes the page, hence it is retrieved. Then the algorithm visits the children (1,2,3) and (2,0,2), but it discovers that neither of them include the page. Figure 17-2. A simple example of priority search tree We won't be able, for lack of space, to describe in detail the data structures and the functions that implement the Linux PSTs. We'll only mention that a node of a PST is represented by a prio_tree_node data structure, which is embedded in the shared.prio_tree_node field of each memory region descriptor. The shared.vm_set data structure is usedas an alternative to shared.prio_tree_nodeto insert the memory region descriptor in a duplicate list of a PST node. PST nodes can be inserted and removed by executing the vma_prio_tree_insert( ) and vma_prio_tree_remove( ) functions; both of them receive as their parameters the address of a memory region descriptor and the address of a PST root. Queries on the PST can be performed by executing the vma_prio_tree_foreach macro, which implements a loop over all memory region descriptors that includes at least one page in a specified range of linear addresses. 17.2.2.2. The try_to_unmap_file( ) function The try_to_unmap_file( ) function is invoked by TRy_to_unmap( ) to perform the reverse mapping of mapped pages. This function is quite simple to describe when the memory mapping is linear (see the section "Memory Mapping" in Chapter 16). In this case, it performs the following actions: 1. Gets the page->mapping->i_mmap_lock spin lock. 2. Applies the vma_prio_tree_foreach( ) macro to the priority search tree whose root is stored in the page->mapping->i_mmap field. For each vm_area_struct descriptor found by the macro, the function invokes try_to_unmap_one( ) to try to clear the Page Table entry of the memory region that contains the page (see the earlier section "Reverse Mapping for Anonymous Pages"). If for some reason this function returns a SWAP_FAIL value, or if the _mapcount field of the page descriptor indicates that all Page Table entries referencing the page frame have been found, the scanning terminates immediately. 3. Releases the page->mapping->i_mmap_lock spin lock. 4. Returns either SWAP_AGAIN or SWAP_FAIL according to whether all page table entries have been cleared. If the mapping is non-linear (see the section "Non-Linear Memory Mappings" in Chapter 16), the try_to_unmap_one( ) function may fail to clear some Page Table entries, because the index field of the page descriptor, which as usual stores the position of the page in the file, is no longer related to the position of the page in the memory region. Therefore, try_to_unmap_one( ) cannot determine the linear address of the page, hence it cannot get the Page Table entry address. The only solution is an exhaustive search in all the non-linear memory regions of the file. The doubly linked list rooted at the i_mmap_nonlinear field of the page->mapping file's address_space object includes the descriptors of all non-linear memory regions of the file. For each such memory region, try_to_unmap_file( ) invokes the TRy_to_unmap_cluster( ) function, which scans all Page Table entries corresponding to the linear addresses of the memory region and tries to clear them. Because the search might be quite time-consuming, a limited scan is performed and a heuristic rule determines the portion of the memory region to be scanned: the vm_private_data field of the vma_area_struct descriptor holds the current cursor in the current scan. This means that try_to_unmap_file( ) might in some cases end up missing the page to be unmapped. When this occurs, try_to_unmap( ) discovers that the page is still mapped and return SWAP_AGAIN instead of SWAP_SUCCESS. 17.3. Implementing the PFRA The page frame reclaiming algorithm must take care of many kinds of pages owned by User Mode processes, disk caches and memory caches; moreover, it has to obey several heuristic rules. Thus, it is not surprising that the PFRA is composed of a large number of functions. Figure 17-3 shows the main PFRA functions; an arrow denotes a function invocation, thus for instance try_to_free_pages( ) invokes shrink_caches( ), shrink_slab( ), and out_of_memory( ). As you can see, there are several "entry points" for the PFRA. Actually, page frame reclaiming is performed on essentially three occasions: Low on memory reclaiming The kernel detects a "low on memory" condition. Hibernation reclaiming The kernel must free memory because it is entering in the suspend-to-disk state (we don't further discuss this case). Periodic reclaiming A kernel thread is activated periodically to perform memory reclaiming, if necessary. Figure 17-3. The main functions of the PFRA Low on memory reclaiming is activated in the following cases: The grow_buffers( ) function, invoked by _ _getblk( ), fails to allocate a new buffer page (see the section "Searching Blocks in the Page Cache" in Chapter 15). The alloc_page_buffers( ) function, invoked by create_empty_buffers( ), fails to allocate the temporary buffer heads for a page (see the section "Reading and Writing a File" in Chapter 16). The _ _alloc_pages( ) function fails in allocating a group of contiguous page frames in a given list of memory zones (see the section "The Buddy System Algorithm" in Chapter 8). Periodic reclaiming is activated by two different types of kernel threads: The kswapd kernel threads, which check whether the number of free page frames in some memory zone has fallen below the pages_high watermark (see the later section "Periodic Reclaiming"). The events kernel threads, which are the worker threads of the predefined work queue (see the section "Work Queues" in Chapter 4); the PFRA periodically schedules the execution of a task in the predefined work queue to reclaim all free slabs included in the memory caches handled by the slab allocator (see the section "The Slab Allocator" in Chapter 8). We are now going to discuss in detail the various components of the page frame reclaiming algorithm, including all functions shown in Figure 17-3. 17.3.1. The Least Recently Used (LRU) Lists All pages belonging to the User Mode address space of processes or to the page cache are grouped into two lists called the active list and the inactive list ; they are also collectively denoted as LRU lists . The former list tends to include the pages that have been accessed recently, while the latter tends to include the pages that have not been accessed for some time. Clearly, pages should be stolen from the inactive list. The active list and the inactive list of pages are the core data structures of the page frame reclaiming algorithm. The heads of these two doubly linked lists are stored, respectively, in the active_list and inactive_list fields of each zone descriptor (see the section "Memory Zones" in Chapter 8). The nr_active and nr_inactive fields in the same descriptor store the number of pages in the two lists. Finally, the lru_lock field is a spin lock that protects the two lists against concurrent accesses in SMP systems. If a page belongs to an LRU list, its PG_lru flag in the page descriptor is set. Moreover, if the page belongs to the active list, the PG_active flag is set, while if it belongs to the inactive list, the PG_active flag is cleared. The lru field of the page descriptor stores the pointers to the next and previous elements in the LRU list. Several auxiliary functions are available to handle the LRU lists: add_page_to_active_list( ) Adds the page to the head of the zone's active list and increases the nr_active field of the zone descriptor. add_page_to_inactive_list( ) Adds the page to the head of the zone's inactive list and increases the nr_inactive field of the zone descriptor. del_page_from_active_list( ) Removes the page from the zone's active list and decreases the nr_active field of the zone descriptor. del_page_from_inactive_list( ) Removes the page from the zone's inactive list and decreases the nr_inactive field of the zone descriptor. del_page_from_lru( ) Checks the PG_active flag of a page; according to the result, removes the page from the active or inactive list, decreases the nr_active or nr_inactive field of the zone descriptor, and clears, if necessary, the PG_active flag. activate_page( ) Checks the PG_active flag; if it is clear (the page is in the inactive list), it moves the page into the active list: invokes del_page_from_inactive_list( ), then invokes add_page_to_active_list( ), and finally sets the PG_active flag. The zone's lru_lock spin lock is acquired before moving the page. lru_cache_add( ) If the page is not included in an LRU list, it sets the PG_lru flag, acquires the zone's lru_lock spin lock, and invokes add_page_to_inactive_list( ) to insert the page in the zone's inactive list. lru_cache_add_active( ) If the page is not included in an LRU list, it sets the PG_lru and PG_active flags, acquires the zone's lru_lock spin lock, and invokes add_page_to_active_list( ) to insert the page in the zone's active list. Actually, the last two functions, lru_cache_add( ) and lru_cache_add_active( ), are slightly more complicated. In fact, the two functions do not immediately move the page into an LRU; instead, they accumulate the pages in temporary data structures of type pagevec, each of which may contain up to 14 page descriptor pointers. The pages will be effectively moved in an LRU list only when a pagevec structure is completely filled. This mechanism enhances the system performance, because the LRU spin lock is acquired only when the LRU lists are effectively modified. 17.3.1.1. Moving pages across the LRU lists The PFRA collects the pages that were recently accessed in the active list so that it will not scan them when looking for a page frame to reclaim. Conversely, the PFRA collects the pages that have not been accessed for a long time in the inactive list. Of course, pages should move from the inactive list to the active list and back, according to whether they are being accessed. Clearly, two page states ("active" and "inactive") are not sufficient to describe all possible access patterns. For instance, suppose a logger process writes some data in a page once every hour. Although the page is "inactive" for most of the time, the access makes it "active," thus denying the reclaiming of the corresponding page frame, even if it is not going to be accessed for an entire hour. Of course, there is no general solution to this problem, because the PFRA has no way to predict the behavior of User Mode processes; however, it seems reasonable that pages should not change their status on every single access. The PG_referenced flag in the page descriptor is used to double the number of accesses required to move a page from the inactive list to the active list; it is also used to double the number of "missing accesses" required to move a page from the active list to the inactive list (see below). For instance, suppose that a page in the inactive list has the PG_referenced flag set to 0. The first page access sets the value of the flag to 1, but the page remains in the inactive list. The second page access finds the flag set and causes the page to be moved in the active list. If, however, the second access does not occur within a given time interval after the first one, the page frame reclaiming algorithm may reset the PG_referenced flag. As shown in Figure 17-4, the PFRA uses the mark_page_accessed( ), page_referenced( ), and refill_inactive_zone( ) functions to move the pages across the LRU lists. In the figure, the LRU list including the page is specified by the status of the PG_active flag. Figure 17-4. Moving pages across the LRU lists 17.3.1.2. The mark_page_accessed( ) function Whenever the kernel must mark a page as accessed, it invokes the mark_page_accessed( ) function. This happens every time the kernel determines that a page is being referenced by a User Mode process, a filesystem layer, or a device driver. For instance, mark_page_accessed( ) is invoked in the following cases: When loading on demand an anonymous page of a process (performed by the do_anonymous_page( ) function; see the section "Demand Paging" in Chapter 9). When loading on demand a page of a memory mapped file (performed by the filemap_nopage( ) function; see the section "Demand Paging for Memory Mapping" in Chapter 16). When loading on demand a page of an IPC shared memory region (performed by the shmem_nopage( ) function; see the section "IPC Shared Memory" in Chapter 19). When reading a page of data from a file (performed by the do_generic_file_read( ) function; see the section "Reading from a File" in Chapter 16). When swapping in a page (performed by the do_swap_page( ) function; see the section "Swapping in Pages" later in this chapter). When looking up a buffer page in the page cache (see the _ _find_get_block( ) function in the section "Searching Blocks in the Page Cache" in Chapter 15). The mark_page_accessed( ) function executes the following code fragment: if (!PageActive(page) && PageReferenced(page) && PageLRU(page)) { activate_page(page); ClearPageReferenced(page); } else if (!PageReferenced(page)) SetPageReferenced(page); As shown in Figure 17-4, the function moves the page from the inactive list to the active list only if the PG_referenced flag is set before the invocation. 17.3.1.3. The page_referenced( ) function The page_referenced( ) function, which is invoked once for every page scanned by the PFRA, returns 1 if either the PG_referenced flag or some of the Accessed bits in the Page Table entries was set; it returns 0 otherwise. This function first checks the PG_referenced flag of the page descriptor; if the flag is set, it clears it. Next, it makes use of the object-based reverse mapping mechanism to check and clear the Accessed bits in all User Mode Page Table entries that refer to the page frame. To do this, the function makes use of three ancillary functions; page_referenced_anon( ), page_referenced_file( ), and page_referenced_one( ), which are analogous to the try_to_unmap_xxx( ) functions described in the section "Reverse Mapping" earlier in this chapter. The page_referenced( ) function also honors the swap token; see the section "The Swap Token" later in this chapter. The page_referenced( ) function never moves a page from the active list to the inactive list; this job is done by refill_inactive_zone( ). In practice, this function does a lot more than move pages from the active to the inactive list, so we are going to describe it in greater detail. 17.3.1.4. The refill_inactive_zone( ) function As illustrated in Figure 17-3, the refill_inactive_zone( ) function is invoked by shrink_zone( ), which performs the reclaiming of pages in the page cache and in the User Mode address spaces (see the section "Low On Memory Reclaiming" later in this chapter). The function receives two parameters: a pointer zone to a memory zone descriptor, and a pointer sc to a scan_control structure. The latter data structure is widely used by the PFRA and contains information about the ongoing reclaiming operation; its fields are shown in Table 17-2. Table 17-2. The fields of the scan_control descriptor Type unsigned long unsigned long unsigned long unsigned long Field nr_to_scan nr_scanned nr_reclaimed nr_mapped Description Target number of pages to be scanned in the active list. Number of inactive pages scanned in the current iteration. Number of pages reclaimed in the current iteration. Number of pages referenced in the User Mode address spaces. int unsigned int unsigned int int nr_to_reclaim Target number of pages to be reclaimed. priority gfp_mask may_writepage Priority of the scanning, ranging between 12 and 0. Lower priority implies scanning more pages. GFP mask passed from calling function. If set, writing a dirty page to disk is allowed (only for laptop mode). The role of refill_inactive_zone( ) is critical because moving a page from an active list to an inactive list means making the page eligible to fall prey, sooner or later, to the PFRA. If the function is too aggressive, it will move a lot of pages from the active list to the inactive list; as a consequence, the PFRA will reclaim a large number of page frames, and the system performance will be hit. On the other hand, if the function is too lazy, the inactive list will not be replenished with a large enough number of unused pages, and the PFRA will fail in reclaiming memory. Thus, the function implements an adaptive behavior: it starts by scanning, at every invocation, a small number of pages in the active list; however, if the PFRA is having trouble in reclaiming page frames, refill_inactive_zone( ) keeps increasing the number of active pages scanned at every invocation. This behavior is controlled by the value of the priority field in the scan_control data structure (a lower value means a more urgent priority). Another heuristic rule regulates the behavior of the refill_inactive_zone( ) function. The LRU lists include two kinds of pages: those belonging to the User Mode address spaces, and those included in the page cache that do not belong to any User Mode process. As stated earlier, the PFRA should tend to shrink the page cache while leaving in RAM the pages owned by the User Mode processes. However, no fixed "golden rule" may yield good performance in every scenario, thus the refill_inactive_zone( ) function relies on a swap tendency heuristic value: it determines whether the function will move all kinds of pages, or just the pages that do not belong to the User Mode address spaces.[*] The swap tendency value is computed by the function as follows: [*] The name "swap tendency" is a bit misleading, because the pages in User Mode address spaces can be swappable, syncable, or discardable. However, the swap tendency value certainly controls the amount of swapping performed by the PFRA, because almost all swappable pages belong to the User Mode address spaces. swap tendency = mapped ratio / 2 + distress + swappiness The mapped ratio value is the percentage of pages in all memory zones that belong to User Mode address spaces (sc->nr_mapped) with respect to the total number of allocatable page frames. A high value of mapped_ratio means that the dynamic memory is mostly used by User Mode processes, while a low value means that it is mostly used by the page cache. The distress value is a measure of how effectively the PFRA is reclaiming page frames in this zone; it is based on the scanning priority of the zone in the previous run of the PFRA, which is stored in the prev_priority field of the zone descriptor. The distress value depends on the zone's previous priority as follows: Zone prev. priority Distress value 12...7 0 6 1 5 3 4 6 3 12 2 25 1 50 0 100 Finally, the swappiness value is a user-defined constant, which is usually set to 60. The system administrator may tune this value by writing in the /proc/sys/vm/swappiness file or by issuing the proper sysctl( ) system call. Pages will be reclaimed from the address spaces of processes only if the zone's swap tendency is greater than or equal to 100. Thus, if the system administrator sets swappiness to 0, then the PFRA never reclaims pages in the User Mode address spaces unless the zone's previous priority is zero (an unlikely event); if the administrator sets swappiness to 100, then the PFRA reclaims pages in the User Mode address spaces at every invocation. Here is a succinct description of what the refill_inactive_zone( ) function does: 1. Invokes lru_add_drain( ) to move into the active and inactive lists any page still contained in the pagevec data structures. 2. Gets the zone->lru_lock spin lock. 3. Performs a first cycle scanning the pages in zone->active_list, starting from the tail of the list and moving backwards. Continues until the list is empty or until sc>nr_to_scan pages have been scanned. For each page scanned in this cycle, the function increases by one its reference counter, removes the page descriptor from zone->active_list, and puts it in a temporary l_hold local list. However, if the reference counter of the page frame was zero, it puts back the page in the active list. In fact, page frames having a reference counter equal to zero should belong to the zone's Buddy system; however, to free a page frame, first its usage counter is decreased and then the page frame is removed from the LRU lists and inserted in the buddy system's list. Therefore, there is a small time window in which the PFRA may see a free page in an LRU list. 4. Adds to zone->pages_scanned the number of active pages that have been scanned. 5. Subtracts from zone->nr_active the number of pages that have been moved into the l_hold local list. 6. Releases the zone->lru_lock spin lock. 7. Computes the swap tendency value (see above). 8. Performs a second cycle on the pages in the l_hold local list. The objective of this cycle is to split the pages of l_hold into two local sublists called l_active and l_inactive. A page belonging to the User Mode address space of some processthat is, a page whose page->_mapcount is nonnegativeis added to l_active if the swap tendency value is smaller than 100, or if the page is anonymous but no swap area is active, or finally if the page_referenced( ) function applied to the page returns a positive value, which means that the page has been recently accessed. In all other cases, the page is added to the l_inactive list.[*] [*] Notice that a page that does not belong to any User Mode process address space is moved into the inactive list; however, since its PG_referenced flag is not cleared, the first access to the page causes the mark_page_accessed( ) function to move the page back into the active list (see Figure 17-4). 9. Gets the zone->lru_lock spin lock. 10. Performs a third cycle on the pages in the l_inactive local list to move them in the zone->inactive_list list and updates the zone->nr_inactive field. In doing so, it decreases the usage counters of the moved page frames to undo the increments done in step 3. 11. Performs a fourth and last cycle on the pages in the l_active local list to move them into the zone->active_list list and updates the zone->nr_active field. In doing so, it decreases the usage counters of the moved page frames to undo the increments done in step 3. 12. Releases the zone->lru_lock spin lock and returns. It should be noted that refill_inactive_zone( ) checks the PG_referenced flag only for pages that belong to the User Mode address spaces (see step 8); in the opposite case, the pages are in the tail of the active listhence they were accessed some time agoand it is unlikely that they will be accessed in the near future. On the other hand, the function does not evict a page from the active list if it is owned by some User Mode process and has been recently used. 17.3.2. Low On Memory Reclaiming Low on memory reclaiming is activated when a memory allocation fails. As shown in Figure 17-3, the kernel invokes free_more_memory( ) while allocating a VFS buffer or a buffer head, and it invokes try_to_free_pages( ) while allocating one or more page frames from the buddy system. 17.3.2.1. The free_more_memory( ) function The free_more_memory( ) function performs the following actions: 1. Invokes wakeup_bdflush( ) to wake a pdflush kernel thread and trigger write operations for 1024 dirty pages in the page cache (see the section "The pdflush Kernel Threads" in Chapter 15). Writing dirty pages to disk may eventually make freeable the page frames containing buffers, buffers heads, and other VFS data structures. 2. Invokes the service routine of the sched_yield( ) system call to give the pdflush kernel thread a chance to run. 3. Starts a loop over all memory nodes in the system (see the section "Non-Uniform Memory Access (NUMA)" in Chapter 8). For each node, invokes the try_to_free_pages( ) function passing to it a list of the "low" memory zones (in the 80 x 86 architecture, ZONE_DMA and ZONE_NORMAL; see the section "Memory Zones" in Chapter 8). 17.3.2.2. The try_to_free_pages( ) function The TRy_to_free_pages( ) function receives three parameters: zones A list of memory zones in which pages should be reclaimed (see the section "Memory Zones" in Chapter 8) gfp_mask The set of allocation flags that were used by the failed memory allocation (see the section "The Zoned Page Frame Allocator" in Chapter 8) order Not used The goal of the function is to free at least 32 page frames by repeatedly invoking the shrink_caches( ) and shrink_slab( ) functions, each time with a higher priority than the previous invocation. The ancillary functions get the priority levelas well as other parameters of the ongoing scan operationin a descriptor of type scan_control (see Table 17-2 earlier in this chapter). The lowest, initial priority level is 12, while the highest, final priority level is 0. If TRy_to_free_pages( ) does not succeed in reclaiming at least 32 page frames in one of the 13 repeated invocations of shrink_caches( ) and shrink_slab( ), the PFRA is in serious trouble, and it has just one last resort: killing a process to free all its page frames. This operation is performed by the out_of_memory( ) function (see the section "The Out of Memory Killer" later in this chapter). The function performs the following main steps: 1. Allocates and initializes a scan_control descriptor. In particular, stores the gfp_mask allocation mask in the gfp_mask field. 2. For each zone in the zones lists, it sets the temp_priority field of the zone descriptor to the initial priority (12). Moreover, it computes the total number of pages contained in the LRU lists of the zones. 3. Performs a loop of at most 13 iterations, from priority 12 down to 0; in each iteration performs the following substeps: a. Updates some field of the scan_control descriptor. In particular, it stores in the nr_mapped field the total number of pages owned by User Mode processes, and in the priority field the current priority of this iteration. Also, it sets to zero the nr_scanned and nr_reclaimed fields. b. Invokes shrink_caches( ) passing as arguments the zones list and the address of the scan_control descriptor. This function scans the inactive pages of the zones (see below). c. Invokes shrink_slab( ) to reclaim pages from the shrinkable kernel caches (see the section "Reclaiming Pages of Shrinkable Disk Caches" later in this chapter). d. If the current->reclaim_state field is not NULL, it adds to the nr_reclaimed field of the scan_control descriptor the number of pages reclaimed from the slab allocator caches; this number is stored in a small data structure pointed to by the process descriptor field. The _ _alloc_pages( ) function sets up the current->reclaim_state field before invoking the TRy_to_free_pages( ) function, and clears the field right after its termination. (Oddly, the free_more_memory( ) function does not set this field.) e. If the target has been reached (the nr_reclaimed field of the scan_control descriptor is greater than or equal to 32), it breaks the loop and jumps to step 4. f. The target has not yet been reached. If at least 49 pages have been scanned so far, the function invokes wakeup_bdflush( ) to activate a pdflush kernel thread and write some dirty pages in the page cache to disk (see the section "Looking for Dirty Pages To Be Flushed" in Chapter 15). g. If the function has already performed four iterations without reaching the target, it invokes blk_congestion_wait( ) to suspend the current process until any WRITE request queue becomes uncongested or until a 100 ms time-out elapses (see the section "Request Descriptors" in Chapter 14). 4. Sets the prev_priority field of each zone descriptor to the priority level used in the last invocation of shrink_caches( ); it is stored in the temp_priority field of the zone descriptor. 5. Returns 1 if the reclaiming was successful, 0 otherwise. 17.3.2.3. The shrink_caches( ) function The shrink_caches( ) function is invoked by TRy_to_free_pages( ). It acts on two parameters: the zones list of memory zones, and the address sc of a scan_control descriptor. The purpose of this function is simply to invoke the shrink_zone( ) function on each zone in the zones list. However, before invoking shrink_zone( ) on a given zone, shrink_caches( ) updates the temp_priority field of the zone's descriptor by using the value stored in the sc>priority field; this is the current priority level of the scanning operation. Moreover, if the priority value of the previous invocation of the PFRA is higher than the current priority valuethat is, page frame reclaiming in this zone is now harder to doshrink_caches( ) copies the current priority level into the prev_priority field of the zone descriptor. Finally, shrink_caches( ) does not invoke shrink_zone( ) on a given zone if the all_unreclaimable flag in the zone descriptor is set and the current priority level is less than 12that is, shrink_caches( ) is not being invoked in the very first iteration of try_to_free_pages( ). The PFRA sets the all_unreclaimable flag when it decides that a zone is so full of unreclaimable pages that scanning the zone's pages is just a waste of time. 17.3.2.4. The shrink_zone( ) function The shrink_zone( ) function acts on two parameters: zone, a pointer to a struct_zone descriptor, and sc, a pointer to a scan_control descriptor. The goal of this function is to reclaim 32 pages from the zone's inactive list; the function tries to reach this goal by invoking repeatedly an auxiliary function called shrink_cache( ), each time on larger portion of the zone's inactive list. Moreover, shrink_zone( ) replenishes the zone's inactive list by repeatedly invoking the refill_inactive_zone( ) function described in the earlier section "The Least Recently Used (LRU) Lists." The nr_scan_active and nr_scan_inactive fields of the zone descriptor play a special role here. To be efficient, the function works on batches of 32 pages. Thus, if the function is running at a low privilege level (high value of sc->priority) and one of the LRU lists does not contain enough pages, the function skips the scanning on that list. However, the number of active or inactive pages thus skipped is recorded in nr_scan_active or nr_scan_inactive, so that the skipped pages will be considered in the next invocation of the function. Specifically, the shrink_zone( ) function performs the following steps: 1. Increases the zone->nr_scan_active by a fraction of the total number of elements in the active list (zone->nr_active). The actual increment is determined by the current priority level and ranges from zone->nr_active/212 to zone->nr_active/20 (i.e., the whole number of active pages in the zone). 2. Increases the zone->nr_scan_inactive by a fraction of the total number of elements in the active list (zone->nr_inactive). The actual increment is determined by the current priority level and ranges from zone->nr_inactive/212 to zone->nr_inactive. 3. If the zone->nr_scan_active field is greater than or equal to 32, the function copies its value in the nr_active local variable and sets the field to zero; otherwise, it sets nr_active to zero. 4. If the zone->nr_scan_inactive field is greater than or equal to 32, the function copies its value in the nr_inactive local variable and sets the field to zero; otherwise, it sets nr_inactive to zero. 5. Sets the sc->nr_to_reclaim field of the scan_control descriptor to 32. 6. If both nr_active and nr_inactive are 0, there is nothing to be done: the function terminates. This is an unlikely situation where User Mode processes have no page frames allocated to them. 7. If nr_active is positive, it replenishes the zone's inactive list: 8. sc->nr_to_scan = min(nr_active, 32); 9. nr_active -= sc->nr_to_scan; 10. refill_inactive_zone(zone, sc); 11. If nr_inactive is positive, it tries to reclaim at most 32 pages from the inactive list: 12. sc->nr_to_scan = min(nr_inactive, 32); 13. nr_inactive -= sc->nr_to_scan; 14. shrink_cache(zone, sc); 15. If shrink_zone( ) succeeds in reclaiming 32 pages (sc->nr_to_reclaim is now zero or negative), it terminates. Otherwise, it jumps back to step 6. 17.3.2.5. The shrink_cache( ) function The shrink_cache( ) function is yet another auxiliary function whose main purpose is to extract from the zone's inactive list a group of pages, put them in a temporary list, and invoke the shrink_list( ) function to effectively perform page frame reclaiming on every page in that list. The shrink_cache( ) function acts on the same parameters as shrink_zones( ), namely zone and sc, and performs the following main steps: 1. Invokes lru_add_drain( ) to move into the active and inactive lists any page still contained in the pagevec data structures (see the section "The Least Recently Used (LRU) Lists" earlier in this chapter). 2. Gets the zone->lru_lock spin lock. 3. Considers at most 32 pages in the inactive list; for each page, the function increases its usage counter, checks whether the page is not being freed to the buddy system (see the discussion at step 3 of refill_inactive_zone( )), and moves the page from the zone's inactive list to a local list. 4. Decreases the counter zone->nr_inactive by the number of pages removed from the inactive list. 5. Increases the counter zone->pages_scanned by the number of pages effectively examined in the inactive list. 6. Releases the zone->lru_lock spin lock. 7. Invokes the shrink_list( ) function passing to it the (local list of) pages collected in step 3 above. This function is discussed below (as you were no doubt expecting). 8. Decreases the sc->nr_to_reclaim field by the number of pages actually reclaimed by shrink_list( ). 9. Gets again the zone->lru_lock spin lock. 10. Puts back in the inactive or active list all pages of the local list that shrink_list( ) did not succeed in freeing. Notice that shrink_list( ) might mark a page as active by setting its PG_active flag. This operation is performed in a batch of pages using a pagevec data structure (see the section "The Least Recently Used (LRU) Lists" earlier in this chapter). 11. If the function scanned at least sc->nr_to_scan pages, and if it didn't succeed in reclaiming the target number of pages (i.e., sc->nr_to_reclaim is still positive), it jumps back to step 3. 12. Releases the zone->lru_lock spin lock and terminates. 17.3.2.6. The shrink_list( ) function We have now reached the heart of page frame reclaiming. While the purpose of the functions illustrated so far, from try_to_free_pages( ) to shrink_cache( ), was to select the proper set of pages candidates for reclaiming, the shrink_list( ) function effectively tries to reclaim the pages passed as a parameter in the page_list list. The second parameter, namely sc, is the usual pointer to a scan_control descriptor. When shrink_list( ) returns, page_list contains the pages that couldn't be freed. The function performs the following actions: 1. If the need_resched field of the current process is set, it invokes schedule( ). 2. Starts a cycle on every page descriptor included in the page_list list. For each list item, it removes the page descriptor from the list and tries to reclaim the page frame; if for some reason the page frame could not be freed, it inserts the page descriptor in a local list. 3. Now the page_list list is empty: the function moves back the page descriptors from the local list to the page_list list. 4. Increases the sc->nr_reclaimed field by the number of page frames reclaimed in step 2, and returns that number. Of course, what is really interesting in shrink_list( ) is the code that tries to reclaim a page frame. The flow diagram of this code is shown in Figure 17-5. There are only three possible outcomes for each page frame handled by shrink_list( ): The page is released to the zone's buddy system by invoking the free_cold_page( ) function (see the section "The Per-CPU Page Frame Cache" in Chapter 8); hence, the page is effectively reclaimed. The page is not reclaimed, thus it will be reinserted in the page_list list; however, shrink_list( ) assumes that it will be possible to reclaim the page in the near future. Thus, the function leaves the PG_active flag in the page descriptor cleared, so that the page will be put back in the inactive list of the memory zone (see step 9 in the descriptor of shrink_cache( ) above). This event corresponds to the small boxes labeled as "INACTIVE" in Figure 17-5. The page is not reclaimed, thus it will be reinserted in the page_list list; however, either the page is in active use, or shrink_list( ) assumes that it will be impossible to reclaim the page in the foreseeable future. Thus, the function sets the PG_active flag in the page descriptor, so that the page will be put in the active list of the memory zone. This event corresponds to the small boxes labeled as "ACTIVE" in Figure 17-5. The shrink_list( ) function never tries to reclaim a page that is locked (PG_locked flag set) or under writeback (PG_writeback flag set). In order to test whether the page was recently referenced, shrink_list( ) invokes page_referenced( ), which was described in the section "The Least Recently Used (LRU) Lists" earlier in this chapter. Figure 17-5. The page reclaiming logic of the shrink_list( ) function To reclaim an anonymous page, the page must be added to the swap cache, and a new slot in a swap area must be reserved for it; see the section "Swapping" later in this chapter for details. If the page is in the User Mode address space of some process (the _mapcount field in the page descriptor is greater than or equal to zero), shrink_list( ) invokes the try_to_unmap( ) function to locate all User Mode Page Table entries that refer to the page frame (see the section "Reverse Mapping" earlier in this chapter). Of course, reclaiming may proceed only if this function returns SWAP_SUCCESS. If the page is dirty, it cannot be reclaimed unless it is written to disk. To do this, shrink_list( ) relies on the pageout( ) function, which is described next. The reclaiming of the page frame may proceed only if either pageout( ) does not have to issue a write operation, or if the write operation finishes soon. If the page contains VFS buffers, shrink_list( ) invokes TRy_to_release_page( ) to release the associated buffer heads (see the section "Releasing Block Device Buffer Pages" in Chapter 15). Finally, if everything went smoothly, shrink_list( ) checks the reference counter of the page: if it is equal to two, the page has just two owners: the page cache (or the swap cache, in case of anonymous pages), and the PFRA itself (the reference counter was increased in step 3 of shrink_cache( ); see earlier). In this case, the page can be reclaimed, provided it is still not dirty. To do this, first the page is removed from the page cache or the swap cache, according to the value of the PG_swapcache flag of the page descriptor; then, the free_cold_page( ) function is executed. 17.3.2.7. The pageout( ) function The pageout( ) function is invoked by shrink_list( ) when a dirty page must be written to disk. Essentially, the function performs the following operations: 1. Checks that the page is included in the page cache or in the swap cache (see the section "The Swap Cache" later in this chapter). Moreover, checks that the page is owned only by the page cacheor the swap cacheand the PFRA. Returns PAGE_KEEP if a check has failed (it does not make sense to write the page to disk if it is not reclaimable by shrink_list( )). 2. Checks that the writepage method of the address_space object is defined; returns PAGE_ACTIVATE otherwise. 3. Checks that the current process can issue write requests to the request queue of the block device associated with the address_space object. Essentially, the kswapd and pdflush kernel threads may always issue the write request; normal processes can issue the write request only if the request queue is not congested, unless the current->backing_dev_info field points to the backing_dev_info data structure of the block device (see step 3 of the description of the generic_file_aio_write_nolock( ) function in the section "Writing to a File" in Chapter 16). 4. Checks that the page is still dirty; if not, returns PAGE_CLEAN. 5. Sets up a writeback_control descriptor and invokes the writepage method of the address_space object to start a write back operation (see the section "Writing Dirty Pages to Disk" in Chapter 16). 6. If the writepage method returned an error code, the function returns PAGE_ACTIVATE. 7. Returns PAGE_SUCCESS. 17.3.3. Reclaiming Pages of Shrinkable Disk Caches We know from the previous chapters that the kernel uses other disk caches beside the page cache, for instance the dentry cache and the inode cache (see the section "The dentry Cache" in Chapter 12). When the PFRA tries to reclaim page frames, it should also check whether some of these disk caches can be shrunk. Every disk cache that is considered by the PFRA must have a shrinker function registered at initialization time. The shrinker function expects two parameters: the target number of page frames to be reclaimed, and a set of GFP allocation flags; the function does what is required to reclaim the pages from the disk cache, then it returns the number of reclaimable pages remaining in the cache. The set_shrinker( ) function registers a shrinker function with the PFRA. This function allocates a descriptor of type shrinker, stores the address of the shrinker function in the descriptor, and then inserts the descriptor in a global list rooted at the shrinker_list global variable. The set_shrinker( ) function also initializes the seeks field of the shrinker descriptor: informally, it is a parameter that indicates how much it costs to re-create one item of the cache once it is removed. In Linux 2.6.11 there are few disk caches registered with the PFRA: besides the dentry cache and the inode cache, only the disk quota layer, the filesystem meta information block cache (mainly used for filesystems' extended attributes), and the XFS journaling filesystem register shrinker functions . The PFRA's function that reclaims pages from the shrinkable disk caches is called shrink_slab( ) (the name is a bit misleading, because the function has little to do with the slab allocator caches). This function is invoked by TRy_to_free_pages( ), as explained in the earlier section "Low On Memory Reclaiming," and by balance_pgdat( ), which is described in the later section "Periodic Reclaiming." The shrink_slab( ) function tries to balance the cost of reclaiming from the shrinkable disk cache with the cost of reclaiming from the LRU lists (performed by shrink_list( )). Essentially, the function walks the list in the shrinker descriptors to invoke the shrinker functions and get the total number of reclaimable pages in the disk caches. Then, the function scans again the list of the shrinker descriptor; for each shrinkable disk cache, the function heuristically computes a target number of page frames to be reclaimedbased on the number of reclaimable pages in the disk caches, on the relative cost of re-creating a page in the disk cache, and on the number of pages in the LRU listsand invokes the shrinker function to try to reclaim batches of at least 128 pages. For lack of space, we'll limit ourselves to describe briefly the shrinker functions of the dentry cache and of the inode cache. 17.3.3.1. Reclaiming page frames from the dentry cache The shrink_dcache_memory( ) function is the shrinker function for the dentry cache; it searches the cache for unused dentry objectsthat is, objects not referenced by any process, see the section "dentry Objects" in Chapter 12and releases them. Because the dentry cache objects are allocated through the slab allocator, the shrink_dcache_memory( ) function may lead some slabs to become free, causing some page frames to be consequently reclaimed by cache_reap( ) (see the section "Periodic Reclaiming" later in this chapter). Moreover, the dentry cache acts as a controller of the inode cache. Therefore, when a dentry object is released, the pages storing the corresponding inode may become unused, and thus eventually released. The shrink_dcache_memory( ) function receives as its parameters the number of page frames to reclaim and a GFP mask. It starts by checking whether the _ _GFP_FS bit in the GFP mask is clear; if so, the function returns -1, because releasing a dentry may trigger an operation on a disk-based filesystem. Page frame reclaiming is effectively done by invoking prune_dcache( ). This function scans the list of unused dentrieswhose head is stored in the dentry_unused variableuntil it reaches the requested number of freed objects or until the whole list is scanned. On each object that wasn't recently referenced, the function: 1. Removes the dentry object from the dentry hash table, from the list of dentry objects in its parent directory, and from the list of dentry objects of the owner inode. 2. Decreases the usage counter of the dentry's inode by invoking the d_iput dentry method, if defined, or the iput( ) function. 3. Invokes the d_release method of the dentry object, if defined. 4. Invokes the call_rcu( ) function to register a callback function that will remove the dentry object (see the section "Read-Copy Update (RCU)" in Chapter 5). The callback function, in turn, will invoke kmem_cache_free( ) to release the object to the slab allocator (see the section "Freeing a Slab Object" in Chapter 8). 5. Decreases the usage counter of the parent directory. Finally, shrink_dcache_memory( ) returns a value based on the number of unused dentries still contained in the dentry cache. More precisely, the returned value is the number of unused dentries multiplied by 100 and divided by the content of the sysctl_vfs_cache_pressure global variable. By default, this variable is equal to 100, thus the returned value is essentially the number of unused dentries. However, the system administrator may modify the variable by writing in the /proc/sys/vm/vfs_cache_pressure or by issuing a suitable sysctl( ) system call. Setting this variable to a value smaller than 100 causes shrink_slab( ) to reclaim fewer pages from the dentry cache (and the inode cache; see the next section) with respect to the pages reclaimed from the LRU lists; conversely, setting the variable to a value greater than 100 causes shrink_slab( ) to reclaim more pages from the dentry and inode caches with respect to the pages reclaimed from the LRU lists. 17.3.3.2. Reclaiming page frames from the inode cache The shrink_icache_memory( ) function is invoked to remove unused inode objects from the inode cache; here, "unused" means that the inode no longer has a controlling dentry object. The function is similar to the shrink_dcache_memory( ) described previously. It checks the _ _GFP_FS bit in the gfp_mask parameter, then it invokes the prune_icache( ) function, and finally it returns a value based both on the number of unused inodes still included in the inode cache and the value of the sysctl_vfs_cache_pressure variable, as previously. The prune_icache( ) function, in turn, scans the inode_unused list (see the section "Inode Objects" in Chapter 12); to free an inode, the function releases any private buffer associated with the inode, it invalidates the clean page frames in the page cache that refer to the inode and are not longer in use, and then it makes use of the clear_inode( ) and destroy_inode( ) functions to destroy the inode object. 17.3.4. Periodic Reclaiming The PFRA performs periodic reclaiming by using two different mechanisms: the kswapd kernel threads, which invoke shrink_zone( ) and shrink_slab( ) to reclaim pages from the LRU lists, and the cache_reap function, which is invoked periodically to reclaim unused slabs from the slab allocator. 17.3.4.1. The kswapd kernel threads The kswapd kernel threads are another kernel mechanism that activates page frame reclaiming. Why is it necessary? Is it not sufficient to invoke TRy_to_free_pages( ) when free memory becomes really scarce and another memory allocation request is issued? Unfortunately, this is not the case. Some memory allocation requests are performed by interrupt and exception handlers, which cannot block the current process waiting for a page frame to be freed; moreover, some memory allocation requests are done by kernel control paths that have already acquired exclusive access to critical resources and that, therefore, cannot activate I/O data transfers. In the infrequent case in which all memory allocation requests are done by such sorts of kernel control paths, the kernel is never able to free memory. The kswapd kernel threads also have a beneficial effect on system performance by keeping memory free in what would otherwise be idle time for the machine; processes can thus get their pages much faster. There is a different kswapd kernel thread for each memory node (see the section "NonUniform Memory Access (NUMA)" in Chapter 8). Each such thread is usually sleeping in the wait queue headed at the kswapd_wait field of the node descriptor. However, if _ _alloc_pages( ) discovers that all memory zones suitable for a memory allocation have a number of free page frames below a "warning" thresholdessentially, a value based on the pages_low and protection fields of the memory zone descriptorthen the function wakes up the kswapd kernel threads of the corresponding memory nodes (see the section "The Zone Allocator" in Chapter 8.) Essentially, the kernel starts to reclaim some page frames in order to avoid much more dramatic "low on memory" conditions. As explained in the section "The Pool of Reserved Page Frames" in Chapter 8, every zone descriptor also includes a pages_min fielda threshold that specifies the minimum number of free page frames that should always be preservedand a pages_high fielda threshold that specifies the "safe" number of free page frames above which page frame reclaiming should be stopped. The kswapd kernel thread executes the kswapd( ) function. It initializes the kernel thread by binding the kernel thread to the CPUs that may access the memory node, by storing in the current->reclaim_state field of the process descriptor the address of a reclaim_state descriptor (see step 3d in the description of TRy_to_free_pages( ) earlier in this chapter), and by setting the PF_MEMALLOC and PF_KSWAP flags in the current->flags fieldthese flags indicate that the process is reclaiming memory and that it is allowed to use all the free memory available when doing its job. Every time the kswapd kernel thread is awakened, the kswapd( ) function performs essentially the following steps: 1. Invokes finish_wait( ) to remove the kernel thread from the node's kswapd_wait wait queue (see the section "How Processes Are Organized" in Chapter 3). 2. Invokes balance_pgdat( ) to perform the memory reclaiming on the kswapd's memory node (see below). 3. Invokes prepare_to_wait( ) to set the process in the TASK_INTERRUPTIBLE state and to put it to sleep in the node's kswapd_wait wait queue. 4. Invokes schedule( ) to yield the CPU to some other runnable process. The balance_pgdat( ) function performs, in turn, the following basic steps: 1. Sets up a scan_control descriptor (see Table 17-2 earlier in this chapter). 2. Sets the temp_priority field of each zone descriptor in the memory node to 12 (lowest priority). 3. Performs a loop of at most 13 iterations, from priority 12 down to 0; in each iteration performs the following substeps: a. Scans the memory zones to find the highest zone (from ZONE_DMA to ZONE_HIGHMEM) having an insufficient number of free page frames. Each test is done by executing the zone_watermark_ok( ) function described in the section "The Zone Allocator" in Chapter 8. If all zones have a large number of free page frames, it jumps to step 4. b. Scans again the memory zones proceeding from ZONE_DMA to the zone found in step 3a. For each zone, it updates, if necessary, the prev_priority field of the zone descriptor with the current priority level, and invokes successively shrink_zone( ) to reclaim pages from the zone (see the earlier section "Low On Memory Reclaiming"). Next, it invokes shrink_slab( ) to reclaim pages from the shrinkable disk caches (see the earlier section "Reclaiming Pages of Shrinkable Disk Caches"). c. If at least 32 pages have been reclaimed, it breaks the loop and jumps to step 4. 4. Updates the prev_priority field of each zone descriptor with the value stored in the corresponding temp_priority field. 5. If some "low on memory" zone still exists, it invokes schedule( ) if the need_resched field of the process is set; when in execution again, it jumps back to step 1. 6. Returns the number of pages reclaimed. 17.3.4.2. The cache_reap( ) function The PFRA must also reclaim the pages owned by the slab allocator caches (see the section "Memory Area Management " in Chapter 8). To do this, it relies on the cache_reap( ) function, which is periodically scheduledapproximately once every two secondsin the predefined events work queue (see the section "Work Queues" in Chapter 4). The address of the cache_reap( ) function is stored in the func field of the reap_work per-CPU variable of type work_struct. The cache_reap( ) function essentially performs the following steps: 1. Tries to acquire the cache_chain_sem semaphore, which protects the list of slab cache descriptors; if the semaphore is already taken, it invokes schedule_delayed_work( ) to schedule the next invocation of the function, and terminates. 2. Otherwise, scans the kmem_cache_t descriptors collected in the cache_chain list. For each cache descriptor found, the function performs the following steps: a. If the SLAB_NO_REAP flag in the cache descriptor is set, page frame reclaiming has been disabled, hence it continues with the next cache in the list. b. Drains the slab local cache (see the section "Local Caches of Free Slab Objects" in Chapter 8); this operation could cause new slabs to become free. c. Each cache has a "reap time" stored in the next_reap field of the kmem_list3 structure inside the cache descriptor (see the section "Cache Descriptor" in Chapter 8); if jiffies is still smaller than next_reap, it continues with the next cache in the list. d. Sets the next "reap time" in the next_reap field to a value four seconds from the current time. e. In multiprocessor systems, the function drains the slab shared cache (see the section "Local Caches of Free Slab Objects" in Chapter 8); this operation could cause new slabs to become free. f. If a new slab has been recently added to the cachethat is, if the free_touched flag of the kmem_list3 structure inside the cache descriptor is setit skips this cache and continues with the next cache in the list. g. Computes according to a heuristic formula the number of slabs to be freed. Basically, this number depends on the upper limit of free objects in the cache and on the number of objects packed into a single slab. h. Repeatedly invokes slab_destroy( ) on the slabs included in the list of free slabs of the cache, until the list is empty or the target number of free slab has been reached. i. Invokes cond_resched( ) to check the TIF_NEED_RESCHED flag of the current process and to invoke schedule( ), if the flag is set. 3. Releases the cache_chain_sem semaphore. 4. Invokes schedule_delayed_work( ) to schedule the next invocation of the function, and terminates. 17.3.5. The Out of Memory Killer Despite the PFRA effort to keep a reserve of free page frames, it is possible for the pressure on the virtual memory subsystem to become so high that all available memory becomes exhausted. This situation could quickly induce a freeze of every activity in the system: the kernel keeps trying to free memory in order to satisfy some urgent request, but it does not succeed because the swap areas are full and all disk caches have already been shrunken. As a consequence, no process can proceed with its execution, thus no process will eventually free up the page frames that it owns. To cope with this dramatic situation, the PFRA makes use of a so-called out of memory (OOM) killer, which selects a process in the system and abruptly kills it to free its page frames. The OOM killer is like a surgeon that amputates the limb of a man to save his life: losing a limb is not a nice thing, but sometimes there is nothing better to do. The out_of_memory( ) function is invoked by _ _alloc_pages( ) when the free memory is very low and the PFRA has not succeeded in reclaiming any page frames (see the section "The Zone Allocator" in Chapter 8). The function invokes select_bad_process( ) to select a victim among the existing processes, then invokes oom_kill_process( ) to perform the sacrifice. Of course, select_bad_process( ) does not simply pick a process at random. The selected process should satisfy several requisites: The victim should own a large number of page frames, so that the amount of memory that can be freed is significant. (As a countermeasure against the "forkbomb" processes, the function considers the amount of memory eaten by all children owned by the parent, too.) Killing the victim should lose a small amount of workit is not a good idea to kill a batch process that has been working for hours or days. The victim should be a low static priority processthe users tend to assign lower priorities to less important processes. The victim should not be a process with root privilegesthey usually perform important tasks. The victim should not directly access hardware devices (such as the X Window server), because the hardware could be left in an unpredictable state. The victim cannot be swapper (process 0), init (process 1), or any other kernel thread. The select_bad_process( ) function scans every process in the system, uses an empirical formula to compute from the above rules a value that denotes how good selecting that process is, and returns the process descriptor address of the "best" candidate for eviction. Then, the out_of_memory( ) function invokes oom_kill_process( ) to send a deadly signalusually SIGKILL; see Chapter 11either to a child of that process or, if this is not possible, to the process itself. The oom_kill_process( ) function also kills all clones that share the same memory descriptor with the selected victim. 17.3.6. The Swap Token As you might have realized while reading this chapter, the Linux VM subsystemand particularly the PFRAis so complex a piece of code that is quite hard to predict its behavior with an arbitrary workload. There are cases, moreover, in which the VM subsystem exhibits pathological behaviors. An example is the so-called swap thrashing phenomenon: essentially, when the system is short of free memory, the PFRA tries hard to free memory by writing pages to disk and stealing the underlying page frames from some processes; at the same time, however, these processes want to proceed with their executions, hence they try hard to access their pages. As a consequence, the kernel assigns to the processes the page frames just freed by the PFRA and reads their contents from disk. The net result is that pages are continuously written to and read back from the disk; most of the time is spent accessing the disk, hence no process makes substantial progress towards its termination. To mitigate the likelihood of swap thrashing, a technique proposed by Jiang and Zhang in 2004 has been implemented in the kernel version 2.6.9: essentially, a so-called swap token is assigned to a single process in the system; the token exempts the process from the page frame reclaiming, so the process can make substantial progress and, hopefully, terminate even when memory is scarce. The swap token is implemented as a swap_token_mm memory descriptor pointer. When a process owns the swap token, swap_token_mm is set to the address of the process's memory descriptor. Immunity from page frame reclaiming is granted in an elegant and simple way. As we have seen in the section "The Least Recently Used (LRU) Lists," a page is moved from the active to the inactive list only if it was not recently referenced. The check is done by the page_referenced( ) function, which honors the swap token and returns 1 (referenced) if the page belongs to a memory region of the process that owns the swap token. Actually, in a couple of cases the swap token is not considered: when the PFRA is executing on behalf of the process that owns the swap token, and when the PFRA has reached the hardest priority level in page frame reclaiming (level 0). The grab_swap_token( ) function determines whether the swap token should be assigned to the current process. It is invoked on each major page fault, namely on just two occasions: When the filemap_nopage( ) function discovers that the required page is not in the page cache (see the section "Demand Paging for Memory Mapping" in Chapter 16). When the do_swap_page( ) function has read a new page from a swap area (see the section "Swapping in Pages" later in this chapter). The grab_swap_token( ) function makes some checks before assigning the token. In particular, the token is granted if all of the following conditions hold: At least two seconds have elapsed since the last invocation of grab_swap_token( ). The current token-holding process has not raised a major page fault since the last execution of grab_swap_token( ), or has been holding the token since at least swap_token_default_timeout ticks. The swap token has not been recently assigned to the current process. The token holding time should ideally be rather long, even in the order of minutes, because the goal is to allow a process to finish its execution. In Linux 2.6.11 the token holding time is set by default to a very low value, namely one tick. However, the system administrator can tune the value of the swap_token_default_timeout variable by writing in the /proc/sys/vm/swap_token_default_timeout file or by issuing a proper sysctl( ) system call. When a process is killed, the kernel checks whether that process was holding the swap token and, if so, releases it; this is done by the mmput( ) function (see the section "The Memory Descriptor" in Chapter 9). 17.4. Swapping Swapping has been introduced to offer a backup on disk for unmapped pages. We know from the previous discussion that there are three kinds of pages that must be handled by the swapping subsystem: Pages that belong to an anonymous memory region of a process (User Mode stack or heap) Dirty pages that belong to a private memory mapping of a process Pages that belong to an IPC shared memory region (see the section "IPC Shared Memory" in Chapter 19) Like demand paging, swapping must be transparent to programs. In other words, no special instruction related to swapping needs to be inserted into the code. To understand how this can be done, recall from the section "Regular Paging" in Chapter 2 that each Page Table entry includes a Present flag. The kernel exploits this flag to signal that a page belonging to a process address space has been swapped out. Besides that flag, Linux also takes advantage of the remaining bits of the Page Table entry to store into them a "swapped-out page identifier" that encodes the location of the swapped-out page on disk. When a Page Fault exception occurs, the corresponding exception handler can detect that the page is not present in RAM and invoke the function that swaps in the missing page from disk. The main features of the swapping subsystem can be summarized as follows: Set up "swap areas" on disk to store pages that do not have a disk image. Manage the space on swap areas allocating and freeing "page slots" as the need occurs. Provide functions both to "swap out" pages from RAM into a swap area and to "swap in" pages from a swap area into RAM. Make use of "swapped-out page identifiers" in the Page Table entries of pages that are currently swapped out to keep track of the positions of data in the swap areas. To sum up, swapping is the crowning feature of page frame reclaiming. If we want to be sure that all the page frames obtained by a process, and not only those containing pages that have an image on disk, can be reclaimed at will by the PFRA, then swapping has to be used. Of course, you might turn off swapping by using the swapoff command; in this case, however, disk thrashing is likely to occur sooner when the system load increases. We should also mention that swapping can be used to expand the memory address space that is effectively usable by the User Mode processes. In fact, large swap areas allow the kernel to launch several demanding applications whose total memory requests exceed the amount of physical RAM installed in the system. However, simulation of RAM is not like RAM in terms of performance. Every access by a process to a page that is currently swapped out is of several orders of magnitude longer than an access to a page in RAM. In short, if performance is of great importance, swapping should be used only as a last resort; adding RAM chips still remains the best solution to cope with increasing computing needs. 17.4.1. Swap Area The pages swapped out from memory are stored in a swap area, which may be implemented either as a disk partition of its own or as a file included in a larger partition. Several different swap areas may be defined, up to a maximum number specified by the MAX_SWAPFILES macro (usually set to 32). Having multiple swap areas allows a system administrator to spread a lot of swap space among several disks so that the hardware can act on them concurrently; it also lets swap space be increased at runtime without rebooting the system. Each swap area consists of a sequence of page slots : 4,096-byte blocks used to contain a swapped-out page. The first page slot of a swap area is used to persistently store some information about the swap area; its format is described by the swap_header union composed of two structures, info and magic. The magic structure provides a string that marks part of the disk unambiguously as a swap area; it consists of just one field, magic.magic, which contains a 10-character "magic" string. The magic structure essentially allows the kernel to unambiguously identify a file or a partition as a swap area; the text of the string, namely "SWAPSPACE2," is always located at the end of the first page slot. The info structure includes the following fields: bootbits Not used by the swapping algorithm; this field corresponds to the first 1,024 bytes of the swap area, which may store partition data, disk labels, and so on. version Swapping algorithm version. last_page Last page slot that is effectively usable. nr_badpages Number of defective page slots. padding[125] Padding bytes. badpages[1] Up to 637 numbers specifying the location of defective page slots. 17.4.1.1. Creating and activating a swap area The data stored in a swap area is meaningful as long as the system is on. When the system is switched off, all processes are killed, so the data stored by processes in swap areas is discarded. For this reason, swap areas contain very little control information: essentially, the swap area type and the list of defective page slots. This control information easily fits in a single 4 KB page. Usually, the system administrator creates a swap partition when creating the other partitions on the Linux system, and then uses the mkswap command to set up the disk area as a new swap area. That command initializes the fields just described within the first page slot. Because the disk may include some bad blocks, the program also examines all other page slots to locate the defective ones. But executing the mkswap command leaves the swap area in an inactive state. Each swap area can be activated in a script file at system boot or dynamically after the system is running. Each swap area consists of one or more swap extents , each of which is represented by a swap_extent descriptor. Each extent corresponds to a group of pagesor more accurately, page slotsthat are physically adjacent on disk. Hence, the swap_extent descriptor includes the index of the first page of the extent in the swap area, the length in pages of the extent, and the starting disk sector number of the extent. An ordered list of the extents that compose a swap area is created when activating the swap area itself. A swap area stored in a disk partition is composed of just one extent; conversely, a swap area stored in a regular file can be composed of several extents, because the filesystem may not have allocated the whole file in contiguous blocks on disk. 17.4.1.2. How to distribute pages in the swap areas When swapping out, the kernel tries to store pages in contiguous page slots to minimize disk seek time when accessing the swap area; this is an important element of an efficient swapping algorithm. However, if more than one swap area is used, things become more complicated. Faster swap areasswap areas stored in faster disksget a higher priority. When looking for a free slot, the search starts in the swap area that has the highest priority. If there are several of them, swap areas of the same priority are cyclically selected to avoid overloading one of them. If no free slot is found in the swap areas that have the highest priority, the search continues in the swap areas that have a priority next to the highest one, and so on. 17.4.2. 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 17-3. Table 17-3. Fields of a swap area descriptor Type unsigned int spinlock_t Field flags sdev_lock Description Swap area flags Spin lock protecting the swap area Table 17-3. Fields of a swap area descriptor Type struct file * struct bdev block_device * struct list head extent_list Field swap_file Description Pointer to the file object of the regular file or device file that stores the swap area Descriptor of the block device containing the swap area Head of the list of extents that compose the swap area Number of extents composing the swap area int struct nr_extents curr_swap_extent Pointer to the most recently used extent descriptor swap_extent * unsigned int old_block_size Natural block size of the partition containing the swap area Pointer to an array of counters, one for each swap area page slot First page slot to be scanned when searching for a free one Last page slot to be scanned when searching for a free one Next page slot to be scanned when searching for a free one Number of free page slot allocations before restarting from the beginning Swap area priority Number of usable page slots Size of swap area in pages Number of used page slots in the swap area Pointer to next swap area descriptor unsigned short swap_map * unsigned int lowest_bit unsigned int highest_bit unsigned int cluster_next unsigned int int int unsigned long cluster_nr prio pages max unsigned long int inuse_pages next The flags field includes three overlapping subfields: SWP_USED 1 if the swap area is active; 0 if it is inactive. SWP_WRITEOK 1 if it is possible to write into the swap area; 0 if the swap area is read-only (it is being activated or inactivated). SWP_ACTIVE This 2-bit field is actually the combination of SWP_USED and SWP_WRITEOK; the flag is set when both the previous flags are set. 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. Essentially, the page slot counter denotes the number of processes that share the swapped-out page. 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.[*] [*] "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. The sdev_lock field is a spin lock that protects the swap area's data structureschiefly, the swap descriptoragainst concurrent accesses in SMP systems. The swap_info array includes MAX_SWAPFILES swap area descriptors. Only the areas whose SWP_USED flags are set are used, because they are the activated areas. Figure 17-6 illustrates the swap_info array, one swap area, and the corresponding array of counters. Figure 17-6. 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: head Index in the swap_info array of the first list element. next 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 among maximum-priority swap areas with free slots. 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. 17.4.3. Swapped-Out Page Identifier A swapped-out page is uniquely identified quite simply by specifying the index of the swap area in the swap_info array and the page slot index inside the swap area. Because the first page (with index 0) of the swap area is reserved for the swap_header union discussed earlier, the first useful page slot has index 1. The format of a swapped-out page identifier is illustrated in Figure 17-7. Figure 17-7. Swapped-out page identifier The swp_entry(type,offset) function constructs a swapped-out page identifier from the swap area index type and the page slot index offset. Conversely, the swp_type and swp_offset functions extract from a swapped-out page identifier the swap area index and the page slot index, respectively. When a page is swapped out, its identifier is inserted as the page's entry into the Page Table so the page can be found again when needed. Notice that the least-significant bit of such an identifier, which corresponds to the Present flag, is always cleared to denote the fact that the page is not currently in RAM. However, at least one of the remaining 31 bits has to be set because no page is ever stored in slot 0 of swap area 0. It is therefore possible to identify three different cases from the value of a Page Table entry: Null entry The page does not belong to the process address space, or the underlying page frame has not yet been assigned to the process (demand paging ). First 31 most-significant bits not all equal to 0, last bit equal to 0 The page is currently swapped out. Least-significant bit equal to 1 The page is contained in RAM. The maximum size of a swap area is determined by the number of bits available to identify a slot. On the 80 x 86 architecture, the 24 bits available limit the size of a swap area to 224 slots (that is, to 64 GB). Because a page may belong to the address spaces of several processes (see the earlier section "Reverse Mapping"), it may be swapped out from the address space of one process and still remain in main memory; therefore, it is possible to swap out the same page several times. A page is physically swapped out and stored just once, of course, but each subsequent attempt to swap it out increases the swap_map counter. The swap_duplicate( ) function is usually invoked while trying to swap out an already swapped-out page. It simply verifies that the swapped-out page identifier passed as its parameter is valid and increases the corresponding swap_map counter. More precisely, it performs the following actions: 1. Uses the swp_type and swp_offset functions to extract the swap area number and the page slot index from the parameter. 2. Checks whether the swap area number identified is active; if not, it returns 0 (invalid identifier). 3. Checks whether the page slot is valid and not free (its swap_map counter is greater than 0 and less than SWAP_MAP_BAD); if not, it returns 0 (invalid identifier). 4. Otherwise, the swapped-out page identifier locates a valid page. Increases the swap_map counter of the page slot if it has not already reached the value SWAP_MAP_MAX. 5. Returns 1 (valid identifier). 17.4.4. Activating and Deactivating a Swap Area Once a swap area is initialized, the superuser (or, more precisely, every user having the CAP_SYS_ADMIN capability, as described in the section "Process Credentials and Capabilities" in Chapter 20) may use the swapon and swapoff programs to activate and deactivate the swap area, respectively. These programs use the swapon( ) and swapoff( ) system calls; we'll briefly sketch out the corresponding service routines. 17.4.4.1. The sys_swapon( ) service routine The sys_swapon( ) service routine receives the following as its parameters: specialfile This parameter points to the pathname (in the User Mode address space) of the device file (partition) or plain file used to implement the swap area. swap_flags This parameter consists of a single SWAP_FLAG_PREFER bit plus 31 bits of priority of the swap area (these bits are significant only if the SWAP_FLAG_PREFER bit is on). The function checks the fields of the swap_header union that was put in the first slot when the swap area was created. The function performs these main steps: 1. Checks that the current process has the CAP_SYS_ADMIN capability. 2. Looks in the first nr_swapfiles components of the swap_info array of swap area descriptors for the first descriptor having the SWP_USED flag cleared, meaning that the corresponding swap area is inactive. If an inactive swap area is found, it goes to step 4. 3. The new swap area array index is equal to nr_swapfiles: it checks that the number of bits reserved for the swap area index is sufficiently large to encode the new index; if not, returns an error code; otherwise, it increases by one the value of nr_swapfiles. 4. An index of an unused swap area has been found: it initializes the descriptor's fields; in particular, it sets flags to SWP_USED, and sets lowest_bit and highest_bit to 0. 5. If the swap_flags parameter specifies a priority for the new swap area, the function sets the prio field of the descriptor. Otherwise, it initializes the field to one less than the lowest priority among all active swap areas (thus assuming that the last activated swap area is on the slowest block device). If no other swap areas are already active, the function assigns the value -1. 6. Copies the string pointed to by the specialfile parameter from the User Mode address space. 7. Invokes filp_open( ) to open the file specified by the specialfile parameter (see the section "The open( ) System Call" in Chapter 12). 8. Stores the addresses of the file object returned by filp_open( ) in the swap_file field of the swap area descriptor. 9. Makes sure that the swap area is not already activated by looking at the other active swap areas in swap_info. This is done by checking the addresses of the address_space objects stored in the swap_file->f_mapping field of the swap area descriptors. If the swap area is already active, it returns an error code. 10. If the specialfile parameter identifies a block device file, it performs the following substeps: a. Invokes bd_claim( ) to set the swapping subsystem as the holder of the block device (see the section "Block Devices" in Chapter 14). If the block device already has a holder, it returns an error code. b. Stores the address of the block_device descriptor in the bdev field of the swap area descriptor. c. Stores the current block size of the device in the old_block_size field of the swap area descriptor, then sets the block size of the device to 4,096 bytes (the page size). 11. If the specialfile parameter identifies a regular file, it performs the following substeps: a. Checks the S_SWAPFILE field of the i_flags field of the file's inode. If this flag is set, it returns an error code because the file is already being used as a swap area. b. Stores the descriptor address of the block device containing the file in the bdev field of the swap area descriptor. 12. Reads the swap_header descriptor stored in slot 0 of the swap area. To that end, it invokes read_cache_page( ) passing as parameters the address_space object pointed to by swap_file->f_mapping, the page index 0, the address of the file's readpage method (stored in swap_file->f_mapping->a_ops->readpage), and the pointer to the file object swap_file. Waits until the page has been read into memory. 13. Checks that the magic string in the last 10 characters of the first page is equal to "SWAPSPACE2." If not, it returns an error code. 14. Initializes the lowest_bit and highest_bit fields of the swap area descriptor according to the size of the swap area stored in the info.last_page field of the swap_header union. 15. Invokes vmalloc( ) to create the array of counters associated with the new swap area and stores its address in the swap_map field of the swap descriptor. Initializes the elements of the array to 0 or to SWAP_MAP_BAD, according to the list of defective page slots stored in the info.bad_pages field of the swap_header union. 16. Computes the number of useful page slots by accessing the info.last_page and info.nr_badpages fields in the first page slot, and stores it in the pages field of the swap area descriptor. Also sets the max field with the total number of pages in the swap area. 17. Builds the extent_list list of swap extents for the new swap area (only one if the swap area is a disk partition), and sets properly the nr_extents and curr_swap_extent fields in the swap area descriptor. 18. Sets the flags field of the swap area descriptor to SWP_ACTIVE. 19. Updates the nr_good_pages, nr_swap_pages, and total_swap_pages global variables. 20. Inserts the swap area descriptor in the list to which the swap_list variable points. 21. Returns 0 (success). 17.4.4.2. The sys_swapoff( ) service routine The sys_swapoff( ) service routine deactivates a swap area identified by the parameter specialfile. It is much more complex and time-consuming than sys_swapon( ), since the partition to be deactivated might still contain pages that belong to several processes. The function is thus forced to scan the swap area and to swap in all existing pages. Because each swap-in requires a new page frame, it might fail if there are no free page frames left. In this case, the function returns an error code. All this is achieved by performing the following major steps: 1. Checks that the current process has the CAP_SYS_ADMIN capability. 2. Copies the string pointed to by the specialfile parameter in kernel space. 3. Invokes filp_open( ) to open the file referenced by the specialfile parameter; as usual, this function returns the address of a file object. 4. Scans the swap_list list of the swap area descriptor, and compares the address of the file object returned by filp_open( ) with the addresses stored in the swap_file fields of the active swap area descriptors. If no match is found, an invalid parameter was passed to the function, so it returns an error code. 5. Invokes cap_vm_enough_memory( ) to check whether there are enough free page frames to swap in all pages stored in the swap area. If not, the swap area cannot be deactivated; it releases the file object and returns an error code. This is only a rough check, but it could save the kernel from a lot of useless disk activity. While performing this check, cap_vm_enough_memory( ) takes into account the page frames allocated through slab caches having the SLAB_RECLAIM_ACCOUNT flag set (see the section "Interfacing the Slab Allocator with the Zoned Page Frame Allocator" in Chapter 8). The number of such pages, which are considered as reclaimable, is stored in the slab_reclaim_pages variable. 6. Removes the swap area descriptor from the swap_list list. 7. Updates the nr_swap_pages and total_swap_pages variables by subtracting the value in the pages field of the swap area descriptor. 8. Clears the SWP_WRITEOK flag in the flags field of the swap area descriptor; this forbids the PFRA from swapping out more pages in the swap area. 9. Invokes try_to_unuse( ) (see below) to successively force all pages left in the swap area into RAM and to correspondingly update the Page Tables of the processes that use these pages. While executing this function, the current process, which is executing the swapoff command, has the PF_SWAPOFF flag set. Setting this flag has just one consequence: in case of a dramatic shortage of page frames, the select_bad_process( ) function will be forced to select and kill this process! (See the section "The Out of Memory Killer" earlier in this chapter.) 10. Waits until the block device driver that contains the swap area is unplugged (see the section "Activating the Block Device Driver" in Chapter 14). In this way, the reading requests submitted by TRy_to_unuse( ) will be handled by the driver before the swap area is deactivated. 11. If TRy_to_unuse( ) fails in allocating all requested page frames, the swap area cannot be deactivated. Therefore, the function executes the following substeps: a. Reinserts the swap area descriptor in the swap_list list and sets its flags field to SWP_WRITEOK. b. Restores the original contents of the nr_swap_pages and total_swap_pages variables by adding the value in the pages field of the swap area descriptor. c. Invokes filp_close( ) to close the file opened in step 3 (see the section "The close( ) System Call" in Chapter 12), and returns an error code. 12. Otherwise, all used page slots have been successfully transferred to RAM. Therefore, the function executes the following substeps: a. Releases the memory areas used to store the swap_map array and the extent descriptors. b. If the swap area is stored in a disk partition, it restores the block size to its original value, which is stored in the old_block_size field of the swap area descriptor; moreover, it invokes the bd_release( ) function so that the swap subsystem no longer holds the block device (see step 10a in the description of sys_swapon( )). c. If the swap area is stored in a regular file, it clears the S_SWAPFILE flag of the file's inode. d. Invokes filp_close( ) twice, the first time on the swap_file file object, the second time on the object returned by filp_open( ) in step 3. e. Returns 0 (success). 17.4.4.3. The try_to_unuse( ) function The TRy_to_unuse( ) function acts on an index parameter that identifies the swap area to be emptied; it swaps in pages and updates all the Page Tables of processes that have swapped out pages in this swap area. To that end, the function visits the address spaces of all kernel threads and processes, starting with the init_mm memory descriptor that is used as a marker. It is a time-consuming function that runs mostly with the interrupts enabled. Synchronization with other processes is therefore critical. The TRy_to_unuse( ) function scans the swap_map array of the swap area. When the function finds a in-use page slot, it first swaps in the page, and then starts looking for the processes that reference the page. The ordering of these two operations is crucial to avoid race conditions. While the I/O data transfer is ongoing, the page is locked, so no process can access it. Once the I/O data transfer completes, the page is locked again by try_to_unuse( ), so it cannot be swapped out again by another kernel control path. Race conditions are also avoided because each process looks up the page cache before starting a swap-in or swap-out operation (see the later section "The Swap Cache"). Finally, the swap area considered by try_to_unuse( ) is marked as nonwritable (SWP_WRITEOK flag is not set), so no process can perform a swap-out on a page slot of this area. However, try_to_unuse( ) might be forced to scan the swap_map array of usage counters of the swap area several times. This is because memory regions that contain references to swapped-out pages might disappear during one scan and later reappear in the process lists. For instance, recall the description of the do_munmap( ) function (in the section "Releasing a Linear Address Interval" in Chapter 9): whenever a process releases an interval of linear addresses, do_munmap( ) removes from the process list all memory regions that include the affected linear addresses; later, the function reinserts the memory regions that have been only partially unmapped in the process list. do_munmap( ) takes care of freeing the swappedout pages that belong to the interval of released linear addresses. It commendably doesn't free the swapped-out pages that belong to the memory regions that have to be reinserted in the process list. Hence, TRy_to_unuse( ) might fail in finding a process that references a given page slot because the corresponding memory region is temporarily not included in the process list. To cope with this fact, try_to_unuse( ) keeps scanning the swap_map array until all reference counters are null. Eventually, the ghost memory regions referencing the swapped-out pages will reappear in the process lists, so TRy_to_unuse( ) will succeed in freeing all page slots. Let's describe now the major operations executed by TRy_to_unuse( ). It executes a continuous loop on the reference counters in the swap_map array of the swap area passed as its parameter. This loop is interrupted and the function returns an error code if the current process receives a signal. For each reference counter, the function performs the following steps: 1. If the counter is equal to 0 (no page is stored there) or to SWAP_MAP_BAD, it continues with the next page slot. 2. Otherwise, it invokes the read_swap_cache_async( ) function (see the section "Swapping in Pages" later in this chapter) to swap in the page. This consists of allocating, if necessary, a new page frame, filling it with the data stored in the page slot, and putting the page in the swap cache. 3. Waits until the new page has been properly updated from disk and locks it. 4. While the function was executing the previous step, the process could have been suspended. Therefore, it checks again whether the reference counter of the page slot is null; if so, this swap page has been freed by another kernel control path, so the function continues with the next page slot. 5. Invokes unuse_process( ) on every memory descriptor in the doubly linked list whose head is init_mm (see the section "The Memory Descriptor" in Chapter 9). This time-consuming function scans all Page Table entries of the process that owns the memory descriptor, and replaces each occurrence of the swapped-out page identifier with the physical address of the page frame. To reflect this move, the function also decreases the page slot counter in the swap_map array (unless it is equal to SWAP_MAP_MAX) and increases the usage counter of the page frame. 6. Invokes shmem_unuse( ) to check whether the swapped-out page is used as an IPC shared memory resource and to properly handle that case (see the section "IPC Shared Memory" in Chapter 19). 7. Checks the value of the reference counter of the page. If it is equal to SWAP_MAP_MAX, the page slot is "permanent." To free it, it forces the value 1 into the reference counter. 8. The swap cache might own the page as well (it contributes to the value of the reference counter). If the page belongs to the swap cache, it invokes the swap_writepage( ) function to flush its contents to disk (if the page is dirty) and invokes delete_from_swap_cache( ) to remove the page from the swap cache and to decrease its reference counter. 9. Sets the PG_dirty flag of the page descriptor, unlocks the page frame, and decreases its reference counter (to undo the increment done in step 5). 10. Checks the need_resched field of the current process; if it is set, it invokes schedule( ) to relinquish the CPU. Deactivating a swap area is a long job, and the kernel must ensure that the other processes in the system still continue to execute. The try_to_unuse( ) function continues from this step whenever the process is selected again by the scheduler. 11. Proceeds with the next page slot, starting at step 1. The function continues until every reference counter in the swap_map array is null. Recall that even if the function starts examining the next page slot, the reference counter of the previous page slot could still be positive. In fact, a "ghost" process could still reference the page, typically because some memory regions have been temporarily removed from the process list scanned in step 5. Eventually, try_to_unuse( ) catches every reference. In the meantime, however, the page is no longer in the swap cache, it is unlocked, and a copy is still included in the page slot of the swap area being deactivated. One might expect that this situation could lead to data loss. For instance, suppose that some "ghost" process accesses the page slot and starts swapping the page in. Because the page is no longer in the swap cache, the process fills a new page frame with the data read from disk. However, this page frame would be different from the page frames owned by the processes that are supposed to share the page with the "ghost" process. This problem does not arise when deactivating a swap area, because interference from a ghost process could happen only if a swapped-out page belongs to a private anonymous memory mapping.[*] In this case, the page frame is handled by means of the Copy On Write mechanism described in Chapter 9, so it is perfectly legal to assign different page frames to the processes that reference the page. However, the try_to_unuse( ) function marks the page as "dirty" (step 9); otherwise, the shrink_list( ) function might later drop the page from the Page Table of some process without saving it in an another swap area (see the later section "Swapping Out Pages"). [*] Actually, the page might also belong to an IPC shared memory region; Chapter 19 has a discussion of this case. 17.4.5. Allocating and Releasing a Page Slot As we will see later, when freeing memory, the kernel swaps out many pages in a short period of time. It is therefore important to try to store these pages in contiguous slots to minimize disk seek time when accessing the swap area. A first approach to an algorithm that searches for a free slot could choose one of two simplistic, rather extreme strategies: Always start from the beginning of the swap area. This approach may increase the average seek time during swap-out operations, because free page slots may be scattered far away from one another. Always start from the last allocated page slot. This approach increases the average seek time during swap-in operations if the swap area is mostly free (as is usually the case), because the handful of occupied page slots may be scattered far away from one another. Linux adopts a hybrid approach. It always starts from the last allocated page slot unless one of these conditions occurs: The end of the swap area is reached. SWAPFILE_CLUSTER (usually 256) free page slots were allocated after the last restart from the beginning of the swap area. The cluster_nr field in the swap_info_struct descriptor stores the number of free page slots allocated. This field is reset to 0 when the function restarts allocation from the beginning of the swap area. The cluster_next field stores the index of the first page slot to be examined in the next allocation.[*] [*] As you may have noticed, the names of Linux data structures are not always appropriate. In this case, the kernel does not really "cluster" page slots of a swap area. To speed up the search for free page slots, the kernel keeps the lowest_bit and highest_bit fields of each swap area descriptor up-to-date. These fields specify the first and the last page slots that could be free; in other words, every page slot below lowest_bit and above highest_bit is known to be occupied. 17.4.5.1. The scan_swap_map( ) function The scan_swap_map( ) function is used to find a free page slot in a given swap area. It acts on a single parameter, which points to a swap area descriptor and returns the index of a free page slot. It returns 0 if the swap area does not contain any free slots. The function performs the following steps: 1. It tries first to use the current cluster. If the cluster_nr field of the swap area descriptor is positive, it scans the swap_map array of counters starting from the element at index cluster_next and looks for a null entry. If a null entry is found, it decreases the cluster_nr field and goes to step 4. 2. If this point is reached, either the cluster_nr field is null or the search starting from cluster_next didn't find a null entry in the swap_map array. It is time to try the second stage of the hybrid search. The function reinitializes cluster_nr to SWAPFILE_CLUSTER and restarts scanning the array from the lowest_bit index trying to find a group of SWAPFILE_CLUSTER free page slots. If such a group is found, it goes to step 4. 3. No group of SWAPFILE_CLUSTER free page slots exists. The function restarts scanning the array from the lowest_bit index trying to find a single free page slot. If no null entry is found, it sets the lowest_bit field to the maximum index in the array, the highest_bit field to 0, and returns 0 (the swap area is full). 4. A null entry is found. Puts the value 1 in the entry, decreases nr_swap_pages, updates the lowest_bit and highest_bit fields if necessary, increases the inuse_pages field by one, and sets the cluster_next field to the index of the page slot just allocated plus 1. 5. Returns the index of the allocated page slot. 17.4.5.2. The get_swap_page( ) function The get_swap_page( ) function is used to find a free page slot by searching all the active swap areas. The function, which returns the swapped-out page identifier of a newly allocated page slot or 0 if all swap areas are filled, takes into consideration the different priorities of the active swap areas. Two passes are done in order to minimize runtime when it's easy to find a page slot. The first pass is partial and applies only to areas that have a single priority; the function searches such areas in a Round Robin fashion for a free slot. If no free page slot is found, a second pass is made starting from the beginning of the swap area list; during this second pass, all swap areas are examined. More precisely, the function performs the following steps: 1. If nr_swap_pages is null or if there are no active swap areas, it returns 0. 2. Starts by considering the swap area pointed to by swap_list.next (recall that the swap area list is sorted by decreasing priorities). 3. If the swap area is active, it invokes scan_swap_map( ) to allocate a free page slot. If scan_swap_map( ) returns a page slot index, the function's job is essentially done, but it must prepare for its next invocation. Thus, it updates swap_list.next to point to the next swap area in the swap area list, if the latter has the same priority (thus continuing the round-robin use of these swap areas). If the next swap area does not have the same priority as the current one, the function sets swap_list.next to the first swap area in the list (so that the next search will start with the swap areas that have the highest priority). The function finishes by returning the swapped-out page identifier corresponding to the page slot just allocated. 4. Either the swap area is not writable, or it does not have free page slots. If the next swap area in the swap area list has the same priority as the current one, the function makes it the current one and goes to step 3. 5. At this point, the next swap area in the swap area list has a lower priority than the previous one. The next step depends on which of the two passes the function is performing. a. If this is the first (partial) pass, it considers the first swap area in the list and goes to step 3, thus starting the second pass. b. Otherwise, it checks if there is a next element in the list; if so, it considers it and goes to step 3. 6. At this point the list is completely scanned by the second pass and no free page slot has been found; it returns 0. 17.4.5.3. The swap_free( ) function The swap_free( ) function is invoked when swapping in a page to decrease the corresponding swap_map counter (see Table 17-3). When the counter reaches 0, the page slot becomes free since its identifier is no longer included in any Page Table entry. We'll see in the later section "The Swap Cache," however, that the swap cache counts as an owner of the page slot. The function acts on a single entry parameter that specifies a swapped-out page identifier and performs the following steps: 1. Derives the swap area index and the offset page slot index from the entry parameter and gets the address of the swap area descriptor. 2. Checks whether the swap area is active and returns right away if it is not. 3. If the swap_map counter corresponding to the page slot being freed is smaller than SWAP_MAP_MAX, the function decreases it. Recall that entries that have the SWAP_MAP_MAX value are considered persistent (undeletable). 4. If the swap_map counter becomes 0, the function increases the value of nr_swap_pages, decreases the inuse_pages field, and updates, if necessary, the lowest_bit and highest_bit fields of the swap area descriptor. 17.4.6. The Swap Cache Transferring pages to and from a swap area is an activity that can induce many race conditions. In particular, the swapping subsystem must handle carefully the following cases: Multiple swap-ins Two processes may concurrently try to swap in the same shared anonymous page. Concurrent swap-ins and swap-outs A process may swap-in a page that is being swapped out by the PFRA. The swap cache has been introduced to solve these kinds of synchronization problems. The key rule is that nobody can start a swap-in or swap-out without checking whether the swap cache already includes the affected page. Thanks to the swap cache, concurrent swap operations affecting the same page always act on the same page frame; therefore, the kernel may safely rely on the PG_locked flag of the page descriptor to avoid any race condition. For example, consider two processes that share the same swapped-out page. When the first process tries to access the page, the kernel starts the swap-in operation. The very first step consists of checking whether the page frame is already included in the swap cache. Let's suppose it isn't: then, the kernel allocates a new page frame and inserts it into the swap cache; next, it starts the I/O operation to read the page's contents from the swap area. Meanwhile, the second process accesses the shared anonymous page. As above, the kernel starts a swap-in operation and checks whether the affected page frame is already included in the swap cache. Now, it is included, thus the kernel simply accesses the page frame descriptor and puts the current process to sleep until the PG_locked flag is cleared, that is, until the I/O data transfer completes. The swap cache plays a crucial role also when concurrent swap-in and swap-out operations mix up. As explained in the section "Low On Memory Reclaiming" earlier in this chapter, the shrink_list( ) function starts swapping out an anonymous page only if TRy_to_unmap( ) succeeds in removing the page frame from the User Mode Page Tables of all processes that own the page. However, one of these processes may access the page and cause a swap-in while the swap-out write operation is still in progress. Before being written to disk, each page to be swapped out is stored in the swap cache by shrink_list( ). Consider a page P that is shared among two processes, A and B. Initially, the Page Table entries of both processes contain a reference to the page frame, and the page has two owners; this case is illustrated in Figure 17-8(a). When the PFRA selects the page for reclaiming, shrink_list( ) inserts the page frame in the swap cache. As illustrated in Figure 17-8(b), now the page frame has three owners, while the page slot in the swap area is referenced only by the swap cache. Next, the PFRA invokes try_to_unmap( ) to remove the references to the page frame from the Page Table of the processes; once this function terminates, the page frame is referenced only by the swap cache, while the page slot is referenced by the two processes and the swap cache, as illustrated in Figure 17-8(c). Let's suppose that, while the page's contents are being written to disk, process B accesses the pagethat is, it tries to access a memory cell using a linear address inside the page. Then, the page fault handler finds the page frame in the swap cache and puts back its physical address in the Page Table entry of process B, as illustrated in Figure 17-8(d). Conversely, if the swap-out operation terminates without concurrent swap-in operations, the shrink_list( ) function removes the page frame from the swap cache and releases the page frame to the Buddy system, as illustrated in Figure 17-8(e). Figure 17-8. The role of the swap cache You might consider the swap cache as a transit area containing the page descriptors of anonymous pages that are being currently swapped-in or swapped out. When the swap-in or swap-out terminates (in the case of shared anonymous pages, the swap-in or swap-out must have been performed on all the processes that share the page), the page descriptor of the anonymous page may be removed from the swap cache.[*] [*] In some cases, the swap cache improves also the system performance: consider a server daemon that services requests by creating child processes. Under heavy system load, a page can get swapped out from the parent process, and it will never be paged in for the parent process. Without the swap cache, every child process that gets forked off needs to fault that page in from the swap area. 17.4.6.1. Swap cache implementation The swap cache is implemented by the page cache data structures and procedures, which are described in the section "The Page Cache" in Chapter 15. Recall that the core of the page cache is a set of radix trees that allows the algorithm to quickly derive the address of a page descriptor from the address of an address_space object identifying the owner of the page as well as from an offset value. Pages in the swap cache are stored as every other page in the page cache, with the following special treatment: The mapping field of the page descriptor is set to NULL. The PG_swapcache flag of the page descriptor is set. The private field stores the swapped-out page identifier associated with the page. Moreover, when the page is put in the swap cache, both the count field of the page descriptor and the page slot usage counters are increased, because the swap cache uses both the page frame and the page slot. Finally, a single swapper_space address space is used for all pages in the swap cache, so a single radix tree pointed to by swapper_space.page_tree addresses the pages in the swap cache. The nrpages field of the swapper_space address space stores the number of pages contained in the swap cache. 17.4.6.2. Swap cache helper functions The kernel uses several functions to handle the swap cache; they are based mainly on those discussed in the section "The Page Cache" in Chapter 15. We show later how these relatively low-level functions are invoked by higher-level functions to swap pages in and out as needed. The main functions that handle the swap cache are: lookup_swap_cache( ) Finds a page in the swap cache through its swapped-out page identifier passed as a parameter and returns the page descriptor address. It returns 0 if the page is not present in the cache. To find the required page, it invokes radix_tree_lookup( ), passing as parameters a pointer to swapper_space.page_treethe radix tree used for pages in the swap cacheand the swapped-out page identifier. add_to_swap_cache( ) Inserts a page into the swap cache. It essentially invokes swap_duplicate( ) to check whether the page slot passed as a parameter is valid and to increase the page slot usage counter; then, it invokes radix_tree_insert( ) to insert the page into the cache; finally, it increases the page's reference counter and sets the PG_swapcache and PG_locked flags. _ _add_to_swap_cache( ) Similar to add_to_swap_cache( ), except that the function does not invoke swap_duplicate( ) before inserting the page frame in the swap cache. delete_from_swap_cache( ) Removes a page from the swap cache by invoking radix_tree_delete( ), decreases the corresponding usage counter in swap_map, and decreases the page reference counter. free_page_and_swap_cache( ) Removes a page from the swap cache if no User Mode process besides current is referencing the corresponding page slot, and decreases the page's usage counter. free_pages_and_swap_cache( ) Analogous to free_page_and_swap_cache( ), but operates on a set of pages. free_swap_and_cache( ) Frees a swap entry, and checks whether the page referenced by the entry is in the swap cache. If either no User Mode process, besides current, is referencing the page or more than 50% of the swap entries are busy, the function removes the page from the swap cache. 17.4.7. Swapping Out Pages We have seen in the section "Low On Memory Reclaiming" earlier in this chapter how the PFRA determines whether a given anonymous page should be swapped out. In this section we show how the kernel performs a swap-out. 17.4.7.1. Inserting the page frame in the swap cache The first step of a swap-out operation consists of preparing the swap cache. If the shrink_list( ) function determines that a page is anonymous (the PageAnon( ) function returns 1) and that the swap cache does not include the corresponding page frame (the PG_swapcache flag in the page descriptor is clear), the kernel invokes the add_to_swap( ) function. The add_to_swap( ) function allocates a new page slot in a swap area and inserts a page framewhose page descriptor address is passed as its parameterin the swap cache. Essentially, the function performs the following steps: 1. Invokes get_swap_page( ) to allocate a new page slot; see the section "Allocating and Releasing a Page Slot" earlier in this chapter. Returns 0 in case of failure (for example, no free page slot found). 2. Invokes _ _add_to_page_cache( ), passing to it the page slot index, the page descriptor address, and some allocation flags. 3. Sets the PG_uptodate and PG_dirty flags in the page descriptor, so that the shrink_list( ) function will be forced to write the page to disk (see the next section). 4. Returns 1 (success). 17.4.7.2. Updating the Page Table entries Once add_to_swap( ) terminates, shrink_list( ) invokes try_to_unmap( ), which determines the address of every User Mode page table entry referring to the anonymous page and writes into it a swapped-out page identifier; this is described in the section "Reverse Mapping for Anonymous Pages" earlier in this chapter. 17.4.7.3. Writing the page into the swap area The next action to be performed to complete the swap-out consists of writing the page's contents into the swap area. This I/O transfer is activated by the shrink_list( ) function, which checks whether the PG_dirty flag of the page frame is set and consequently executes the pageout( ) function (see Figure 17-5 earlier in this chapter). As explained in the section "Low On Memory Reclaiming" earlier in this chapter, the pageout( ) function sets up a writeback_control descriptor and invokes the writepage method of the page's address_space object. The writepage method of the swapper_state object is implemented by the swap_writepage( ) function. The swap_writepage( ) function, in turn, performs essentially the following steps: 1. Checks whether at least one User Mode process is referencing the page. If not, it removes the page from the swap cache and returns 0. This check is necessary because a process might race with the PRFA and release a page after the check performed by shrink_list( ). 2. Invokes get_swap_bio( ) to allocate and initialize a bio descriptor (see the section "The Bio Structure" in Chapter 14). The function derives the address of the swap area descriptor from the swapped-out page identifier; then, it walks the swap extent lists to determine the initial disk sector of the page slot. The bio descriptor will include a request for a single page of data (the page slot); the completion method is set to the end_swap_bio_write( ) function. 3. Sets the PG_writeback flag in the page descriptor and the writeback tags in the swap cache's radix tree (see the section "The Tags of the Radix Tree" in Chapter 15). Moreover, the function resets the PG_locked flag. 4. Invokes submit_bio( ), passing to it the WRITE command and the bio descriptor address. 5. Returns 0. Once the I/O data transfer terminates, the end_swap_bio_write( ) function is executed. Essentially, this function wakes up any process waiting until the PG_writeback flag of the page is cleared, clears the PG_writeback flag and the corresponding tags in the radix tree, and releases the bio descriptor used for the I/O transfer. 17.4.7.4. Removing the page frame from the swap cache The last step of the swap-out operation is performed once more by shrink_list( ): if it verifies that no process has tried to access the page frame while doing the I/O data transfer, it essentially invokes delete_from_swap_cache( ) to remove the page frame from the swap cache. Because the swap cache was the only owner of the page, the page frame is released to the buddy system. 17.4.8. Swapping in Pages Swap-in takes place when a process attempts to address a page that has been swapped out to disk. The Page Fault exception handler triggers a swap-in operation when the following conditions occur (see the section "Handling a Faulty Address Inside the Address Space" in Chapter 9): The page including the address that caused the exception is a valid onethat is, it belongs to a memory region of the current process. The page is not present in memorythat is, the Present flag in the Page Table entry is cleared. The Page Table entry associated with the page is not null, but the Dirty bit is clear; this means that the entry contains a swapped-out page identifier (see the section "Demand Paging" in Chapter 9). If all the above conditions are satisfied, handle_pte_fault( ) invokes a quite handy do_swap_page( ) function to swap in the page required. 17.4.8.1. The do_swap_page( ) function The do_swap_page( ) function acts on the following parameters: mm Memory descriptor address of the process that caused the Page Fault exception vma Memory region descriptor address of the region that includes address address Linear address that causes the exception page_table Address of the Page Table entry that maps address pmd Address of the Page Middle Directory that maps address orig_pte Content of the Page Table entry that maps address write_access Flag denoting whether the attempted access was a read or a write Contrary to other functions, do_swap_page( ) never returns 0. It returns 1 if the page is already in the swap cache (minor fault), 2 if the page was read from the swap area (major fault), and -1 if an error occurred while performing the swap-in. It essentially executes the following steps: 1. Gets the swapped-out page identifier from orig_pte. 2. Invokes pte_unmap( ) to release any temporary kernel mapping for the Page Table created by the handle_mm_fault( ) function (see the section "Handling a Faulty Address Inside the Address Space" in Chapter 9). As explained in the section "Kernel Mappings of High-Memory Page Frames" in Chapter 8, a kernel mapping is required to access a page table in high memory. 3. Releases the page_table_lock spin lock of the memory descriptor (it was acquired by the caller function handle_pte_fault( )). 4. Invokes lookup_swap_cache( ) to check whether the swap cache already contains a page corresponding to the swapped-out page identifier; if the page is already in the swap cache, it jumps to step 6. 5. Invokes the swapin_readahead( ) function to read from the swap area a group of at most 2n pages, including the requested one. The value n is stored in the page_cluster variable, and is usually equal to 3.[*] Each page is read by invoking the read_swap_cache_async( ) function (see below). [*] The system administrator may tune this value by writing into the /proc/sys/vm/page-cluster file. Swap-in read-ahead can be disabled by setting page_cluster to 0. 6. Invokes read_swap_cache_async( ) once more to swap in precisely the page accessed by the process that caused the Page Fault. This step might appear redundant, but it isn't really. The swapin_readahead( ) function might fail in reading the requested pagefor instance, because page_cluster is set to 0 or the function tried to read a group of pages including a free page slot or a defective page slot (SWAP_MAP_BAD). On the other hand, if swapin_readahead( ) succeeded, this invocation of read_swap_cache_async( ) terminates quickly because it finds the page in the swap cache. 7. If, despite all efforts, the requested page was not added to the swap cache, another kernel control path might have already swapped in the requested page on behalf of a clone of this process. This case is checked by temporarily acquiring the page_table_lock spin lock and comparing the entry to which page_table points with orig_pte. If they differ, the page has already been swapped in by some other kernel control path, so the function returns 1 (minor fault); otherwise, it returns -1 (failure). 8. At this point, we know that the page is in the swap cache. If the page has been effectively swapped in (major fault), the function invokes grab_swap_token( ) to try to grab the swap token (see the section "The Swap Token" earlier in this chapter). 9. Invokes mark_page_accessed( ) (see the earlier section "The Least Recently Used (LRU) Lists") and locks the page. 10. Acquires the page_table_lock spin lock. 11. Checks whether another kernel control path has swapped in the requested page on behalf of a clone of this process. In this case, it releases the page_table_lock spin lock, unlocks the page, and returns 1 (minor fault). 12. Invokes swap_free( ) to decrease the usage counter of the page slot corresponding to enTRy. 13. Checks whether the swap cache is at least 50 percent full (nr_swap_pages is smaller than half of total_swap_pages). If so, it checks whether the page is owned only by the process that caused the fault (or one of its clones); if this is the case, removes the page from the swap cache. 14. Increases the rss field of the process's memory descriptor. 15. Updates the Page Table entry so the process can find the page. The function accomplishes this by writing the physical address of the requested page and the protection bits found in the vm_page_prot field of the memory region into the Page Table entry addressed by page_table. Moreover, if the access that caused the fault was a write and the faulting process is the unique owner of the page, the function also sets the Dirty flag and the Read/Write flag to prevent a useless Copy On Write fault. 16. Unlocks the page. 17. Invokes page_add_anon_rmap( ) to insert the anonymous page in the object-based reverse mapping data structures (see the section "Reverse Mapping for Anonymous Pages" earlier in this chapter.) 18. If the write_access parameter is equal to 1, the function invokes do_wp_page( ) to make a copy of the page frame (see the section "Copy On Write" in Chapter 9). 19. Releases the mm->page_table_lock spin lock and returns the ret return code: 1 (minor fault) or 2 (major fault). 17.4.8.2. The read_swap_cache_async( ) function The read_swap_cache_async( ) function is invoked whenever the kernel must swap in a page. It acts on three parameters: entry A swapped-out page identifier vma A pointer to the memory region that should contain the page addr The linear address of the page As we know, before accessing the swap partition, the function must check whether the swap cache already includes the desired page frame. Therefore, the function essentially executes the following operations: 1. Invokes radix_tree_lookup( ) to locate in the radix tree of the swapper_space object a page frame at the position given by the swapped-out page identifier enTRy. If the page is found, it increases its reference counter and returns the address of its descriptor. 2. The page is not included in the swap cache. Invokes alloc_pages( ) to allocate a new page frame. If no free page frame is available, it returns 0 (indicating the system is out of memory). 3. Invokes add_to_swap_cache( ) to insert the page descriptor of the new page frame into the swap cache. As mentioned in the earlier section "Swap cache helper functions," this function also locks the page. 4. The previous step might fail if add_to_swap_cache( ) finds a duplicate of the page in the swap cache. For instance, the process could block in step 2, thus allowing another process to start a swap-in operation on the same page slot. In this case, it releases the page frame allocated in step 2 and restarts from step 1. 5. Invokes lru_cache_add_active( ) to insert the page in the LRU active list (see the section "The Least Recently Used (LRU) Lists" earlier in this chapter). 6. The page descriptor of the new page frame is now in the swap cache. Invokes swap_readpage( ) to read the page's contents from the swap area. This function is quite similar to swap_writepage( ) described in the earlier section "Swapping Out Pages:" it clears the PG_uptodate flag of the page descriptor, invokes get_swap_bio( ) to allocate and initialize a bio descriptor for the I/O transfer, and invokes submit_bio( ) to submit the I/O request to the block subsystem layer. 7. Returns the address of the page descriptor. ...
View Full Document

This note was uploaded on 03/01/2012 for the course CMP 426 taught by Professor Gwangs.jung during the Spring '12 term at CUNY Lehman.

Ask a homework question - tutors are online