Linux Kernel Architecture

(Jacob Rumans) #1

Chapter3:MemoryManagement


if (free_pages <= min + z->lowmem_reserve[classzone_idx])
return 0;
for (o = 0; o < order; o++) {
/* At the next order, this order’s pages become unavailable */
free_pages -= z->free_area[o].nr_free << o;

/* Require fewer higher order pages to be free */
min >>= 1;

if (free_pages <= min)
return 0;
}
return 1;
}

Recall thatzone_per_stateallows for accessing the per-zone statistics. In this case, the number of free
pages is obtained.

Once theALLOC_HIGHandALLOC_HARDERflags have been interpreted (they reduce the minimum mark by
a half or quarter of the current value, which makes the allocation effectively try hard or even harder), the
function checks whether the number of free pages is less than the desired minimum plus the emergency
reserve specified inlowmem_reserve. If not, the code iterates over all orders less than the current order
and subtracts all pages in the current zone fromfree_pages(theo-fold left shift is necessary because
nr_freestores the free pageblocks). At the same time, the required number of free pages is halved for
each zone. The allocation is freed if the kernel establishes that not enough pages are present after iterating
over all low-memory zones.

get_page_from_freelistis another important helper function used by the buddy system. It refers to the
flags set and the allocation order to decide whether allocation can be made; if so, it initiates actual page
allocation.^20

mm/page_alloc.c
static struct page *
get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
struct zonelist *zonelist, int alloc_flags)
{
struct zone **z;
struct page *page = NULL;
int classzone_idx = zone_idx(zonelist->zones[0]);
struct zone *zone;
...
/*
* Scan zonelist, looking for a zone with enough free.
* See also cpuset_zone_allowed() comment in kernel/cpuset.c.
*/
z = zonelist->zones;

do {
...
zone = *z;

(^20) Notice that NUMA systems use a zone list cache that accelerates scanning through the zones. Although the cache is not active on
UMA systems, it has some influence on the code below that I have removed for the sake of simplicity.

Free download pdf