本文主要是介绍如何理解内存管理中的alloc_flags?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先需要明确一点alloc_flags和gfp_mask之间的区别,gfp_mask是使用alloc_pages申请内存时所传递的申请标记,而alloc_flags是在内存管理子系统内部使用的另一个标记,二者是不同的,当然alloc_flags也是从gfp_mask经过计算得到的。
关于alloc_flags的定义有如下几个:
/* The ALLOC_WMARK bits are used as an index to zone->watermark */
#define ALLOC_WMARK_MIN WMARK_MIN
#define ALLOC_WMARK_LOW WMARK_LOW
#define ALLOC_WMARK_HIGH WMARK_HIGH
#define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */#define ALLOC_HARDER 0x10 /* try to alloc harder */
#define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
#define ALLOC_CMA 0x80 /* allow allocations from CMA areas */
#define ALLOC_FAIR 0x100 /* fair zone allocation */
那么获取alloc_flags的地方就是在gfp_to_alloc_flags中:
static inline int
gfp_to_alloc_flags(gfp_t gfp_mask)
{int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;const bool atomic = !(gfp_mask & (__GFP_WAIT | __GFP_NO_KSWAPD));/* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);/* * The caller may dip into page reserves a bit more if the caller* cannot run direct reclaim, or if the caller has realtime scheduling* policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will* set both ALLOC_HARDER (atomic == true) and ALLOC_HIGH (__GFP_HIGH).*/alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);if (atomic) {/* * Not worth trying to allocate harder for __GFP_NOMEMALLOC even* if it can't schedule.*/if (!(gfp_mask & __GFP_NOMEMALLOC))alloc_flags |= ALLOC_HARDER;/* * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the* comment for __cpuset_node_allowed_softwall().*/alloc_flags &= ~ALLOC_CPUSET;} else if (unlikely(rt_task(current)) && !in_interrupt())alloc_flags |= ALLOC_HARDER;if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {if (gfp_mask & __GFP_MEMALLOC)alloc_flags |= ALLOC_NO_WATERMARKS;else if (in_serving_softirq() && (current->flags & PF_MEMALLOC))alloc_flags |= ALLOC_NO_WATERMARKS;else if (!in_interrupt() &&((current->flags & PF_MEMALLOC) ||unlikely(test_thread_flag(TIF_MEMDIE))))alloc_flags |= ALLOC_NO_WATERMARKS;}
#ifdef CONFIG_CMAif (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)alloc_flags |= ALLOC_CMA;
#endifreturn alloc_flags;
}
这里我主要讲解ALLOC_HARDER和ALLOC_HIGH,这两个标记都是用来表示在申请内存时判断zone中是否存在合适的内存做判断使用的。
static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark, int classzone_idx, int alloc_flags, long free_pages)
{/* free_pages my go negative - that's OK */long min = mark;long lowmem_reserve = z->lowmem_reserve[classzone_idx];int o;long free_cma = 0;free_pages -= (1 << order) - 1;if (alloc_flags & ALLOC_HIGH)min -= min / 2;if (alloc_flags & ALLOC_HARDER)min -= min / 4;
#ifdef CONFIG_CMA/* If allocation can't use CMA areas don't use free CMA pages */if (!(alloc_flags & ALLOC_CMA))free_cma = zone_page_state(z, NR_FREE_CMA_PAGES);
#endifif (free_pages - free_cma <= min + lowmem_reserve)return false;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 false;}return true;
}
对于ALLOC_HARDER来说它把watermark水位减去了1/4,而对于ALLOC_HIGH来说,它把保留水位值再减少了1/2,这两个标记按照不同程度来减少保留内存数量,从而达到了更容易成功申请内存的目的。
如果单独设置,从这里也可以看出ALLOC_HIGH是比ALLOC_HARDER更加激进的内存申请方式。但是一般我们申请时设置的GFP_ATOMIC实际上会同时设置ALLOC_HARDER 和 ALLOC_HIGH 标记。
这篇关于如何理解内存管理中的alloc_flags?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!