Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


❑ get_zeroed_page(mask)allocates a page and returns apageinstance but fills the page with
zeros (with all other functions, page contents are undefined after allocation).
❑ __get_free_pages(mask, order)and__get_free_page(mask)work in the same way as the
above functions but return the virtual address of the reserved memory chunk instead of apage
instance.
❑ get_dma_pages(gfp_mask,order)allows for obtaining pages suitable for DMA.

If allocation fails because insufficient memory is free to satisfy the request, all the above functions
return either a null pointer (alloc_pagesandalloc_page)orthevalue0(get_zeroed_page,
__get_free_pages,and__get_free_page). The kernel must therefore check the result returned after
everyallocation attempt. This practice is not different from any well-designed userland applications, but
neglecting the check in the kernel will lead to much more severe failures.

The kernel provides other memory management functions in addition to the buddy system functions.
They build on layers that are used as a basis by the buddy system but do not belong to the buddy allocator
itself. These functions arevmallocandvmalloc_32, which use page tables to map discontiguous memory
into kernel address space so that it appears to be contiguous. There is also a set of functions of thekmalloc
type to reserve memory areas smaller than a complete page. Their implementation is discussed separately
in later sections of this chapter.

Four slightly different functions are defined to return pages no longer needed in memory to the kernel.

❑ free_page(struct page*)andfree_pages(struct page*, order)return one or 2orderpages
to memory management. The start of the memory area is indicated by means of a pointer to the
firstpageinstance of the area.
❑ __free_page(addr)and__free_pages(addr, order)operate in the same way as the functions
just mentioned but use a virtual memory address instead of apageinstance to select the memory
area to be returned.

AllocationMasks


What is the meaning of themaskparameter that is mandatory for all functions? As we know from
Section 3.2.1, Linux divides memory into zones. The kernel provides what is known aszone modifiers
(defined in the least significant 4 bits of a mask) tospecify the zone from which the pages are to be taken
for purposes of memory allocation.

<gfp.h>
/* Zone modifiers in GFP_ZONEMASK (see linux/mmzone.h - low three bits) */
#define __GFP_DMA ((__force gfp_t)0x01u)
#define __GFP_HIGHMEM ((__force gfp_t)0x02u)
#define __GFP_DMA32 ((__force gfp_t)0x04u)
...
#define __GFP_MOVABLE ((__force gfp_t)0x100000u) /* Page is movable */

These constants are familiar from Section 3.4.1 in which the creation of fallback lists is discussed. The
abbreviationGFPstands forget free pages.__GFP_MOVABLEdoes not represent a physical memory zone, but
instructs the kernel that an allocation should be fulfilled from the special virtual zoneZONE_MOVABLE.
Free download pdf