下面是对代码的中文注释分析:
static struct page *
get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
const struct alloc_context *ac)
{
struct zoneref *z = ac->preferred_zoneref;
struct zone *zone;
struct pglist_data *last_pgdat_dirty_limit = NULL;
// 扫描备用区域列表中每一个满足条件的区域:区域类型小于等于首选区域类型
for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
ac->nodemask) {
struct page *page;
unsigned long mark;
if (cpusets_enabled() && // 如果编译了cpuset功能
(alloc_flags & ALLOC_CPUSET) && // 如果设置了ALLOC_CPUSET
!__cpuset_zone_allowed(zone, gfp_mask)) // 如果cpu设置了不允许从当前区域分配内存
continue; // 那么不允许从这个区域分配,进入下个循环
if (ac->spread_dirty_pages) {// 如果设置了写标志位,表示要分配写缓存
// 那么要检查内存脏页数量是否超出限制,超过限制就不能从这个区域分配
if (last_pgdat_dirty_limit == zone->zone_pgdat)
continue;
if (!node_dirty_ok(zone->zone_pgdat)) {
last_pgdat_dirty_limit = zone->zone_pgdat;
continue;
}
}
mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];// 检查允许分配水线
// 判断(区域空闲页-申请页数)是否小于水线
if (!zone_watermark_fast(zone, order, mark,
ac_classzone_idx(ac), alloc_flags)) {
int ret;
/* Checked here to keep the fast path fast */
BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
// 如果没有水线要求,直接选择该区域
if (alloc_flags & ALLOC_NO_WATERMARKS)
goto try_this_zone;
// 如果没有开启节点回收功能或者当前节点和首选节点距离大于回收距离
if (node_reclaim_mode == 0 ||
!zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
continue;
// 从节点回收“没有映射到进程虚拟地址空间的内存页”,然后检查水线
ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
switch (ret) {
case NODE_RECLAIM_NOSCAN:
/* did not scan */
continue;
case NODE_RECLAIM_FULL:
/* scanned but unreclaimable */
continue;
default:
/* did we reclaim enough */
if (zone_watermark_ok(zone, order, mark,
ac_classzone_idx(ac), alloc_flags))
goto try_this_zone;
continue;
}
}
try_this_zone:// 满足上面的条件了,开始分配
// 从当前区域分配页
page = rmqueue(ac->preferred_zoneref->zone, zone, order,
gfp_mask, alloc_flags, ac->migratetype);
if (page) {
// 分配成功,初始化页
prep_new_page(page, order, gfp_mask, alloc_flags);
/*
* If this is a high-order atomic allocation then check
* if the pageblock should be reserved for the future
*/
// 如果这是一个高阶的内存并且是ALLOC_HARDER,需要检查以后是否需要保留
if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
reserve_highatomic_pageblock(page, zone, order);
return page;
} else {
#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
/* Try again if zone has deferred pages */
// 如果分配失败,延迟分配
if (static_branch_unlikely(&deferred_pages)) {
if (_deferred_grow_zone(zone, order))
goto try_this_zone;
}
#endif
}
}
return NULL;
}
这段代码实现了从空闲列表中获取页面的函数。以下是对关键部分的注释:
z:指向首选区域引用的指针。for_next_zone_zonelist_nodemask:遍历备用区域列表中满足条件的区域。cpusets_enabled():如果启用了cpuset功能。__cpuset_zone_allowed():如果当前CPU不允许从此区域分配内存,则跳过循环。ac->spread_dirty_pages:如果设置了写标志位,表示要分配写缓存。last_pgdat_dirty_limit:指向上一个检查内存脏页限制的pgdat的指针。zone->watermark:获取区域中允许分配的水线。zone_watermark_fast():快速检查区域是否有足够的可用页面以满足分配请求。alloc_flags:标志位,包括ALLOC_NO_WATERMARKS和ALLOC_HARDER等。node_reclaim():从当前节点回收“没有映射到进程虚拟地址空间的内存页”,然后检查水线是否满足要求。rmqueue():从当前区域分配所需数量的页面。prep_new_page():初始化新分配的页面。_deferred_grow_zone():如果分配失败,延迟进行增长。
该函数返回从空闲列表中获取的页面。如果无法满足分配请求,则返回NULL。
内容由零声教学AI助手提供,问题来源于学员提问




