Linux Documentation
 help / color / mirror / Atom feed
From: Richard Cheng <icheng@nvidia.com>
To: "Gregory Price (Meta)" <gourry@gourry.net>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	kernel-team@meta.com,  linux-doc@vger.kernel.org,
	driver-core@lists.linux.dev, linux-serial@vger.kernel.org,
	 linux-pm@vger.kernel.org, damon@lists.linux.dev,
	linux-cxl@vger.kernel.org,  akpm@linux-foundation.org,
	david@kernel.org, liam@infradead.org, vbabka@kernel.org,
	 rppt@kernel.org, surenb@google.com, mhocko@suse.com,
	corbet@lwn.net,  skhan@linuxfoundation.org,
	rdunlap@infradead.org, gregkh@linuxfoundation.org,
	 rafael@kernel.org, dakr@kernel.org, johan@kernel.org,
	jirislaby@kernel.org,  kasong@tencent.com, qi.zheng@linux.dev,
	shakeel.butt@linux.dev, baohua@kernel.org,
	 axelrasmussen@google.com, yuanchu@google.com,
	weixugc@google.com, baoquan.he@linux.dev,
	 baolin.wang@linux.alibaba.com, yury.norov@gmail.com,
	linux@rasmusvillemoes.dk, pavel@kernel.org,  lenb@kernel.org,
	hannes@cmpxchg.org, brendan.jackman@linux.dev, ziy@nvidia.com,
	 sj@kernel.org, muchun.song@linux.dev, osalvador@suse.de,
	nico.pache@linux.dev,  ryan.roberts@arm.com, dev.jain@arm.com,
	lance.yang@linux.dev, usama.arif@linux.dev,  kas@kernel.org,
	xu.xin@linux.dev, chengming.zhou@linux.dev,
	 joshua.hahnjy@gmail.com, rakie.kim@sk.com,
	ying.huang@linux.alibaba.com,  matthew.brost@intel.com,
	byungchul@sk.com, apopple@nvidia.com, rientjes@google.com
Subject: Re: [PATCH 13/22] mm/hugetlb: restrict pool growth to common memory
Date: Wed, 18 Nov 2026 15:32:10 +0800	[thread overview]
Message-ID: <av1PyBvUVJcy_Psd@MWDK4CY14F> (raw)
In-Reply-To: <20261114070959.3490008-14-gourry@gourry.net>

On Sat, Nov 14, 2026 at 02:09:50AM +0800, Gregory Price (Meta) wrote:
> Fresh HugeTLB pool folios come from ordinary allocations, which only
> N_MEMORY_COMMON nodes serve.
> 
> Restrict HugeTLB allocations and pool-growth to N_MEMORY_COMMON nodes.
> 
> Keep the original allowed mask for pool shrinking and demotion so
> existing pool folios can still be found on any online-memory node.
> 
> The result of this change is that hugepage allocations will fail on
> nodes without N_MEMORY_COMMON.  This restriction can be loosened later
> when/if it grows a user.
> 
> All memory is common at this point, so behavior is unchanged.
> 
> Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
> ---
>  mm/hugetlb.c | 44 ++++++++++++++++++++------------------------
>  1 file changed, 20 insertions(+), 24 deletions(-)
> 
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index cb2de24affdc4..cc1468f68901b 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -3452,13 +3452,13 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
>  			gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
>  
>  			folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
> -					&node_states[N_MEMORY], NULL);
> +					&node_states[N_MEMORY_COMMON], NULL);
>  			if (!folio && !list_empty(&folio_list) &&
>  			    hugetlb_vmemmap_optimizable(h)) {
>  				prep_and_add_allocated_folios(h, &folio_list);
>  				INIT_LIST_HEAD(&folio_list);
>  				folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
> -						&node_states[N_MEMORY], NULL);
> +						&node_states[N_MEMORY_COMMON], NULL);
>  			}
>  			if (!folio)
>  				break;
> @@ -3528,7 +3528,7 @@ static void __init hugetlb_pages_alloc_boot_node(unsigned long start, unsigned l
>  			prep_and_add_allocated_folios(h, &folio_list);
>  			INIT_LIST_HEAD(&folio_list);
>  		}
> -		folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY],
> +		folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY_COMMON],
>  						&node_alloc_noretry, &next_node);
>  		if (!folio)
>  			break;
> @@ -3672,13 +3672,12 @@ static void __init hugetlb_init_hstates(void)
>  
>  	for_each_hstate(h) {
>  		/*
> -		 * Always reset to first_memory_node here, even if
> -		 * next_nid_to_alloc was set before - we can't
> -		 * reference hugetlb_bootmem_nodes after init, and
> -		 * first_memory_node is right for all further allocations.
> +		 * Always reset the allocation cursor here, even if it was set
> +		 * before: hugetlb_bootmem_nodes cannot be referenced after init,
> +		 * and further pool allocations use common memory.
>  		 */
> -		h->next_nid_to_alloc = first_memory_node;
> -		h->next_nid_to_free = first_memory_node;
> +		h->next_nid_to_alloc = first_node_state(N_MEMORY_COMMON);
> +		h->next_nid_to_free = first_node_state(N_MEMORY_COMMON);
>  
>  		/* oversize hugepages were init'ed in early boot */
>  		if (!hstate_is_gigantic(h))
> @@ -3802,23 +3801,21 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
>  	unsigned long allocated;
>  	struct folio *folio;
>  	LIST_HEAD(page_list);
> -	NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
> +	NODEMASK_ALLOC(struct nodemask_scratch, scratch, GFP_KERNEL);
>  
> -	/*
> -	 * Bit mask controlling how hard we retry per-node allocations.
> -	 * If we can not allocate the bit mask, do not attempt to allocate
> -	 * the requested huge pages.
> -	 */
> -	if (node_alloc_noretry)
> -		nodes_clear(*node_alloc_noretry);
> -	else
> +	/* Scratch masks for allocation eligibility and per-node retries. */
> +	if (!scratch)
>  		return -ENOMEM;
> +	nodes_clear(scratch->mask2);
>  
>  	/*
>  	 * resize_lock mutex prevents concurrent adjustments to number of
>  	 * pages in hstate via the proc/sysfs interfaces.
>  	 */
>  	mutex_lock(&h->resize_lock);
> +
> +	/* Fresh pool pages come from the ordinary allocation domain. */
> +	nodes_and(scratch->mask1, *nodes_allowed, node_states[N_MEMORY_COMMON]);

For a global request the caller passes &node_states[N_MEMORY], so before this patch
the grow loop read the live mask on every alloc_pool_huge_folio().
Now the mask is sampled once, before the loop.

The loop drops hugetlb_lock and can run for a long time. If a node gets its first
memoroy in that window, the old code would start using it. The new code won't, until
the next write to nr_hugepages.

So the pool can end up uneven, or smaller than requested if the other nodes are full.

I think the nodes_and() should be placed inside the loop to keep the old behavior ?

Best regards,
Richard Cheng.


>  	flush_free_hpage_work(h);
>  	spin_lock_irq(&hugetlb_lock);
>  
> @@ -3855,7 +3852,7 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
>  		if (count > persistent_huge_pages(h)) {
>  			spin_unlock_irq(&hugetlb_lock);
>  			mutex_unlock(&h->resize_lock);
> -			NODEMASK_FREE(node_alloc_noretry);
> +			NODEMASK_FREE(scratch);
>  			return -EINVAL;
>  		}
>  		/* Fall through to decrease pool */
> @@ -3873,7 +3870,7 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
>  	 * within all the constraints specified by the sysctls.
>  	 */
>  	while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
> -		if (!adjust_pool_surplus(h, nodes_allowed, -1))
> +		if (!adjust_pool_surplus(h, &scratch->mask1, -1))
>  			break;
>  	}
>  
> @@ -3889,9 +3886,8 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
>  		/* yield cpu to avoid soft lockup */
>  		cond_resched();
>  
> -		folio = alloc_pool_huge_folio(h, nodes_allowed,
> -						node_alloc_noretry,
> -						&h->next_nid_to_alloc);
> +		folio = alloc_pool_huge_folio(h, &scratch->mask1, &scratch->mask2,
> +					      &h->next_nid_to_alloc);
>  		if (!folio) {
>  			prep_and_add_allocated_folios(h, &page_list);
>  			spin_lock_irq(&hugetlb_lock);
> @@ -3978,7 +3974,7 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
>  	spin_unlock_irq(&hugetlb_lock);
>  	mutex_unlock(&h->resize_lock);
>  
> -	NODEMASK_FREE(node_alloc_noretry);
> +	NODEMASK_FREE(scratch);
>  
>  	return 0;
>  }
> -- 
> 2.53.1-Meta
> 
> 

  reply	other threads:[~2026-11-18  7:32 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14  7:09 [PATCH 00/22] mm: introduce N_MEMORY_COMMON Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 01/22] mm: add node-state-filtered zone and node iterators Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 02/22] mm: publish and clear node memory states via helpers Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 03/22] mm: introduce the common-memory node state Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 04/22] mm/page_alloc: build zonelists from common memory Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 05/22] mm/mempolicy: keep interleave and tmpfs mount policies on " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 06/22] mm: base hash distribution on common memory nodes Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 07/22] mm/memory_hotplug: migrate offlined pages to common memory Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 08/22] mm/memory-tiers: restrict demotion topology " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 09/22] mm/ksm: restrict merging " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 10/22] mm/khugepaged: restrict collapse " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 11/22] mm/mempolicy: don't NUMA-hint folios outside " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 12/22] mm/damon: restrict DAMON-native folio operations to " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 13/22] mm/hugetlb: restrict pool growth " Gregory Price (Meta)
2026-11-18  7:32   ` Richard Cheng [this message]
2026-11-14  7:09 ` [PATCH 14/22] mm/page_alloc: count common memory nodes for kswapd recovery Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 15/22] mm/oom_kill: compare OOM constraints against common memory Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 16/22] tty/sysrq: use common memory for the manual OOM zonelist Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 17/22] mm/vmscan: restrict reclaim to common memory Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 18/22] mm/compaction: restrict compaction " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 19/22] mm/migrate: restrict move_pages() destinations " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 20/22] PM: hibernate: size the image from " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 21/22] mm/khugepaged: base the recommended min_free_kbytes on " Gregory Price (Meta)
2026-11-14  7:09 ` [PATCH 22/22] nodemask: remove the now-unused first_memory_node/next_memory_node Gregory Price (Meta)
2026-11-18  7:01 ` [PATCH 00/22] mm: introduce N_MEMORY_COMMON Richard Cheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=av1PyBvUVJcy_Psd@MWDK4CY14F \
    --to=icheng@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=brendan.jackman@linux.dev \
    --cc=byungchul@sk.com \
    --cc=chengming.zhou@linux.dev \
    --cc=corbet@lwn.net \
    --cc=dakr@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gourry@gourry.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=jirislaby@kernel.org \
    --cc=johan@kernel.org \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kas@kernel.org \
    --cc=kasong@tencent.com \
    --cc=kernel-team@meta.com \
    --cc=lance.yang@linux.dev \
    --cc=lenb@kernel.org \
    --cc=liam@infradead.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=matthew.brost@intel.com \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nico.pache@linux.dev \
    --cc=osalvador@suse.de \
    --cc=pavel@kernel.org \
    --cc=qi.zheng@linux.dev \
    --cc=rafael@kernel.org \
    --cc=rakie.kim@sk.com \
    --cc=rdunlap@infradead.org \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=shakeel.butt@linux.dev \
    --cc=sj@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=xu.xin@linux.dev \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yuanchu@google.com \
    --cc=yury.norov@gmail.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox