Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

11.2 Zone Structure Initialization

The initialization of zone structures is performed by the function build_all_zonelists(), which is defined within the file git/mm/page_alloc.c. Omitting print diagnostics, the function can be simplified as follows:

void __ref build_all_zonelists(pg_data_t *pgdat)
{
	unsigned long vm_total_pages;
	if (system_state == SYSTEM_BOOTING) {
		build_all_zonelists_init();
	} else {
		__build_all_zonelists(pgdat);
	}
	vm_total_pages = nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
	if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
		page_group_by_mobility_disabled = 1;
	else
		page_group_by_mobility_disabled = 0;
}

As inferred from its name, the primary objective of this function is to construct a continuous list of zones. Because the system is currently in its early booting phase, the argument passed to build_all_zonelists() evaluates to a null pointer. This function triggers the initial zone routing map configuration via build_all_zonelists_init(). The build_all_zonelists_init() function is likewise defined within the page_alloc.c file and is implemented as follows:

static noinline void __init build_all_zonelists_init(void)
{
	int cpu;
	__build_all_zonelists(NULL);
	for_each_possible_cpu(cpu)
		setup_pageset(&per_cpu(boot_pageset, cpu), 0);
	mminit_verify_zonelist();
	cpuset_init_current_mems_allowed();
}

The internal function __build_all_zonelists(NULL) leverages the raw data structures provided by each individual pglist_data node to initialize the node_zonelists array embedded inside every parent pg_data_t structure, thereby building the fallback node zone lists. The architectural layout of the node_zonelists array for each individual node is illustrated in Figure 11-2 below:

Figure 11-2 Structural Layout of the node_zonelist Array

The node_zonelists array is composed of zonelist elements. A zonelist is constructed from an array of zoneref structures, each consisting of a pointer to a specific zone along with an integer index (idx) that signifies its matching zone type. The total dimension of the node_zonelists array maps to the total count of online NUMA nodes, while the entry count within a zonelist maps directly to the diverse zone types supported by the operating system.

Stripping out non-essential compilation blocks and inactive operational paths, __build_all_zonelists() can be simplified as follows:

static void __build_all_zonelists(void *data)
{
	int nid;
	int __maybe_unused cpu;
	pg_data_t *self = data;
	static DEFINE_SPINLOCK(lock);
	spin_lock(&lock);
#ifdef CONFIG_NUMA
	memset(node_load, 0, sizeof(node_load));
#endif
	for_each_online_node(nid) {
		pg_data_t *pgdat = NODE_DATA(nid);
		build_zonelists(pgdat);
	}
	spin_unlock(&lock);
}

Its primary operation is to iterate over every online node and leverage the function build_zonelists() to construct the corresponding node_zonelists fallbacks. The NODE_DATA macro is utilized to retrieve the base memory address of node nid from the global node_data tracking table, thereby granting access to its target pglist_data structure. The build_zonelists() routine is implemented as follows:

static void build_zonelists(pg_data_t *pgdat)
{
	static int node_order[MAX_NUMNODES];
	int node, load, nr_nodes = 0;
	nodemask_t used_mask = NODE_MASK_NONE;
	int local_node, prev_node;

	local_node = pgdat->node_id;
	load = nr_online_nodes;
	prev_node = local_node;
	memset(node_order, 0, sizeof(node_order));
	while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
		if (node_distance(local_node, node) != node_distance(local_node, prev_node))
			node_load[node] = load;
		node_order[nr_nodes++] = node;
		prev_node = node;
		load--;
	}
	build_zonelists_in_node_order(pgdat, node_order, nr_nodes);
	build_thisnode_zonelists(pgdat);
}

The function build_zonelists() first sorts the individual memory nodes based on their physical distance relative to the target node along with other affinity weight metrics. This generates an ordered node array node_order[] that isolates remote nodes from the local node context. Following this, the routine invokes build_zonelists_in_node_order() to append all valid zones belonging to the remote nodes sorted within the node_order array directly into the current node's node_zonelists array.

build_zonelists_in_node_order() internally relies on build_zonerefs_node(). The role of build_zonerefs_node() is to construct a discrete zonelist link for an individual node targeted inside the node_order array. Throughout the composition of this fallback link, the routine loops through all valid zone types. If the node from node_order effectively supports the current zone type being evaluated—indicated by a non-zero managed_pages metric—the corresponding zoneref metadata composed of this zone and its matching index type is appended onto the zonelist.

Because build_zonelists_in_node_order() only processes and appends zones belonging to distinct, remote nodes into the target node_zonelists array, build_zonelists() must invoke build_thisnode_zonelists() at the conclusion of its runtime logic path to append the node's local zones onto its primary index slot, saving the finalized tracking links inside node_zonelists[0].

Core memory management procedures require access to the CPU-private variable boot_pageset. Consequently, once the node_zonelists arrays have been constructed for all valid nodes, __build_all_zonelists() must invoke setup_pageset() to initialize the individual fields of the per_cpu_pages structure embedded inside each CPU's boot_pageset allocation. setup_pageset() initializes the internal linked list heads of per_cpu_pages via pageset_init(), while forcing the batch field assignment to evaluate to 1 and the high field threshold to evaluate to 0 via pageset_set_batch().

As outlined in §7.3, system administrators can leverage the cgroup subsystem to govern and coordinate the hardware resources allocated to active tasks, within which the cpuset subsystem explicitly dictates task allocation boundaries across CPU cores and memory nodes. Throughout thread scheduling passes, a task is strictly constrained to execute only on the CPU cores and memory nodes permitted by its assigned cpuset. Through tactical configuration, a CPU core can be explicitly matched to access localized memory nodes situated closest to its physical execution context, thereby drastically minimizing inter-node bus cycles and optimizing memory access latency metrics.

Upon successfully configuring the fallback node_zonelists tracking tables across all active nodes, the build_all_zonelists_init() routine invokes mminit_verify_zonelist() to log and audit the layout of each node zone map, followed by a call to cpuset_init_current_mems_allowed(). Via the internal routine nodes_setall(), the mems_allowed bitmask field inside the root configuration of the initial setup task (init_task) is filled with the comprehensive mask 0xFFFFFFFF,FFFFFFFF, denoting that the bootstrap context possesses unrestricted authorization to allocate from any memory node. At this juncture, the initial initialization tasks for memory nodes and architectural zones are complete.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com