記事ディレクトリ
[ Linuxカーネルメモリ管理]物理割り当てページ②(__alloc_pages_nodemask関数パラメータ分析| __alloc_pages_nodemask関数割り当て物理ページプロセス)ブログでは、__alloc_pages_nodemask
関数れます。
まず、gfp_t gfp_mask
割り当て「メモリノード」の優先「リージョンタイプ」と「移行タイプ」を取得します。
次に、「高速パス」を実行します。最初の割り当て試行では、低透かし割り当てが使用されます。
上記の「高速パス」割り当てが失敗した場合、「低速パス」割り当てが実行されます。
上記は「ファストパス」と「スローパス」を指します222つの物理ページ割り当て方法。
1. __alloc_pages_nodemask関数のソースコード分析(高速パス|低速パス)
__alloc_pages_nodemask
関数では、最初に関数を呼び出しget_page_from_freelist
、「高速パス」を使用してメモリを割り当てます。メモリ割り当てが失敗した場合は、「低速パス」にジャンプout
してメモリを割り当てます。
/* First allocation attempt */
page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac);
if (likely(page))
goto out;
ソースコードパス: linux-4.12 \ mm \ page_alloc.c #4019
out
ラベルのコードは次のとおりです。以下は「スローパス」割り当てメモリのソースコードです。
out:
if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page &&
unlikely(memcg_kmem_charge(page, gfp_mask, order) != 0)) {
__free_pages(page, order);
page = NULL;
}
if (kmemcheck_enabled && page)
kmemcheck_pagealloc_alloc(page, order, gfp_mask);
trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
return page;
ソースコードパス: linux-4.12 \ mm \ page_alloc.c #4041
__alloc_pages_nodemask
関数の完全なソースコードについては、[Linuxカーネルメモリ管理]物理割り当てページ①(パーティションパートナーアロケータ物理割り当てページコア関数__alloc_pages_nodemask | __alloc_pages_nodemask関数の完全なソースコード)ブログを参照してください。
2.get_page_from_freelist高速パス呼び出し関数の完全なソースコード
高速パス呼び出し関数このget_page_from_freelist
関数は、Linuxカーネルソースコードのlinux-4.12 \ mm \ page_alloc.c #3017の場所で定義されています。
/*
* get_page_from_freelist goes through the zonelist trying to allocate
* a page.
*/
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;
/*
* Scan zonelist, looking for a zone with enough free.
* See also __cpuset_node_allowed() comment in kernel/cpuset.c.
*/
for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
ac->nodemask) {
struct page *page;
unsigned long mark;
if (cpusets_enabled() &&
(alloc_flags & ALLOC_CPUSET) &&
!__cpuset_zone_allowed(zone, gfp_mask))
continue;
/*
* When allocating a page cache page for writing, we
* want to get it from a node that is within its dirty
* limit, such that no single node holds more than its
* proportional share of globally allowed dirty pages.
* The dirty limits take into account the node's
* lowmem reserves and high watermark so that kswapd
* should be able to balance it without having to
* write pages from its LRU list.
*
* XXX: For now, allow allocations to potentially
* exceed the per-node dirty limit in the slowpath
* (spread_dirty_pages unset) before going into reclaim,
* which is important when on a NUMA setup the allowed
* nodes are together not big enough to reach the
* global limit. The proper fix for these situations
* will require awareness of nodes in the
* dirty-throttling and the flusher threads.
*/
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
*/
if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
reserve_highatomic_pageblock(page, zone, order);
return page;
}
}
return NULL;
}
ソースコードパス: linux-4.12 \ mm \ page_alloc.c #3017