Subject: mm/hugetlb: fix CMA handling and surplus allocation accounting
Subsystems (2)
cover Subject: mm/hugetlb: fix CMA handling and surplus allocation accounting
From: Kaitao Cheng <[email protected]>
This series fixes three independent HugeTLB issues reported by Sashiko, covering CMA parameter parsing, CMA allocation state preservation, and NR_HUGETLB accounting on allocation rollback.
Patch 1 rejects a global hugetlb_cma size following node-specific entries. This prevents malformed input from overwriting valid per-node reservations with an incorrectly parsed global size.
Patch 2 preserves the CMA flag when a folio is returned to the HugeTLB pool after vmemmap restoration fails. This ensures that a subsequent free releases the CMA allocation correctly instead of leaving its bitmap set.
Patch 3 fixes NR_HUGETLB accounting when unused surplus folios are freed during allocation or reservation cleanup.
Kaitao Cheng (3):
mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed
input
mm/hugetlb: preserve CMA flag when returning folios to the pool mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios
mm/hugetlb.c | 13 +++++++++++-- mm/hugetlb_cma.c | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed input
From: Kaitao Cheng <[email protected]>
When hugetlb_cma contains a global size after node-specific entries, cmdline_parse_hugetlb_cma() enters the global branch and reparses the original parameter string. For hugetlb_cma=1:2G,4G, this interprets the initial node ID as a one-byte global size and clears the 2 GiB reservation recorded for node 1.
Global and node-specific formats cannot be mixed. Return -EINVAL when a global entry follows a node-specific entry, before updating any settings. This preserves the valid prefix, so the example retains node 1's 2 GiB reservation and reports the malformed option.
--- mm/hugetlb_cma.c | 3 +++ 1 file changed, 3 insertions(+)
mm/hugetlb_cma.c +3 -0
@@ -167,6 +167,9 @@ static int __init cmdline_parse_hugetlb_cma(char *p) }else {char *next; +if (s != p) +return -EINVAL ; + tmp =memparse (p, &next);if (*next =='%' ) {if (tmp >100 ) {
Signature · 1 line
2.53.0
PATCH 2/3 mm/hugetlb: preserve CMA flag when returning folios to the pool
In reply to Kaitao ChengFrom: Kaitao Cheng <[email protected]>
When vmemmap restoration fails, add_hugetlb_folio() returns the folio to the hugetlb pool. It clears folio->private and restores only HPG_vmemmap_optimized, losing HPG_cma on CMA-allocated folios.
A later successful free then bypasses hugetlb_cma_free_frozen_folio() and returns the pages through free_frozen_pages(). The CMA allocation bitmap remains set, so the range is unavailable for future CMA allocations.
Save the CMA flag before resetting folio->private and restore it along with the vmemmap optimization flag.
--- mm/hugetlb.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/hugetlb.c +4 -1
@@ -1437,6 +1437,7 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio,void add_hugetlb_folio (struct hstate *h,struct folio *folio,bool adjust_surplus) { +bool cma =folio_test_hugetlb_cma (folio);int nid =folio_nid (folio);VM_BUG_ON_FOLIO (!folio_test_hugetlb_vmemmap_optimized (folio), folio);
@@ -1453,9 +1454,11 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, }__folio_set_hugetlb (folio); -/* Clear all folio->private flags except hugetlb_vmemmap_optimized. */ +/* Preserve the vmemmap optimization state and CMA allocation origin. */ folio->private =NULL ;folio_set_hugetlb_vmemmap_optimized (folio); +if (cma) +folio_set_hugetlb_cma (folio);arch_clear_hugetlb_flags (folio);enqueue_hugetlb_folio (h, folio);
Signature · 1 line
2.53.0
PATCH 3/3 mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios
In reply to Kaitao ChengFrom: Kaitao Cheng <[email protected]>
free_huge_folio() unconditionally decrements NR_HUGETLB, but freshly allocated surplus folios can be freed before the counter has been incremented.
This happens when alloc_surplus_hugetlb_folio() rejects a fresh folio after its second surplus limit check. It also happens when gather_surplus_pages() frees folios because a reservation cannot be satisfied, or because concurrent pool changes leave excess folios after the reservation succeeds.
These paths leave unmatched decrements, causing HugeTLB usage to be underreported and potentially underflowing the counter.
Increment NR_HUGETLB before calling free_huge_folio() in both paths to balance its unconditional decrement.
--- mm/hugetlb.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
mm/hugetlb.c +7 -1
@@ -2132,6 +2132,8 @@ static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h,if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {folio_set_hugetlb_temporary (folio);spin_unlock_irq (&hugetlb_lock); +/* Balance the NR_HUGETLB decrement in free_huge_folio(). */ +lruvec_stat_mod_folio (folio,NR_HUGETLB ,pages_per_huge_page (h));free_huge_folio (folio);return NULL ; }
@@ -2376,9 +2378,13 @@ static int gather_surplus_pages(struct hstate *h, long delta)/* * Free unnecessary surplus pages to the buddy allocator. * Pages have no ref count, call free_huge_folio directly. +* These pages were not accounted in NR_HUGETLB, so balance the +* decrement in free_huge_folio(). */ -list_for_each_entry_safe (folio, tmp, &surplus_list, lru) +list_for_each_entry_safe (folio, tmp, &surplus_list, lru) { +lruvec_stat_mod_folio (folio,NR_HUGETLB ,pages_per_huge_page (h));free_huge_folio (folio); + }spin_lock_irq (&hugetlb_lock);return ret;
Signature · 1 line
2.53.0
Re: [PATCH 1/3] mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed input
In reply to Kaitao ChengQuoted 12 lines · Kaitao Cheng
From: Kaitao Cheng <[email protected]>
When
hugetlb_cmacontains a global size after node-specific entries,cmdline_parse_hugetlb_cma()enters the global branch and reparses the original parameter string. For hugetlb_cma=1:2G,4G, this interprets the initial node ID as a one-byte global size and clears the 2 GiB reservation recorded for node 1.
Quoted 8 lines
Global and node-specific formats cannot be mixed. Return
-EINVALwhen a global entry follows a node-specific entry, before updating any settings. This preserves the valid prefix, so the example retains node 1's 2 GiB reservation and reports the malformed option.Fixes 38e719ab2673 ("hugetlb: support node specified when using cma for gigantic hugepages")Signed-off-by Kaitao Cheng <[email protected]>
Thanks
Muchun Song
maintainerRe: [PATCH 2/3] mm/hugetlb: preserve CMA flag when returning folios to the pool
In reply to Kaitao ChengQuoted 10 lines · Kaitao Cheng
From: Kaitao Cheng <[email protected]>
When vmemmap restoration fails,
add_hugetlb_folio()returns the folio to the hugetlb pool. It clearsfolio->privateand restores onlyHPG_vmemmap_optimized, losingHPG_cmaon CMA-allocated folios.
Quoted 7 lines
A later successful free then bypasses
hugetlb_cma_free_frozen_folio()and returns the pages throughfree_frozen_pages(). The CMA allocation bitmap remains set, so the range is unavailable for future CMA allocations.Save the CMA flag before resetting
folio->privateand restore it along with the vmemmap optimization flag.
We already knew this issue. Cc Zi Yan. Seems he hasn't sent a fix.
But we want a different solution. See:
https://lore.kernel.org/linux-mm/[email protected]/
Thanks,
Muchun
Quoted 36 lines
Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")Signed-off-by Kaitao Cheng <[email protected]>---
mm/hugetlb.c| 5 ++++-1 file changed, 4 insertions(+), 1 deletion(-)mm/hugetlb.c +4
-1@@ -1437,6 +1437,7 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio,void add_hugetlb_folio (struct hstate *h,struct folio *folio,bool adjust_surplus) { +bool cma =folio_test_hugetlb_cma (folio);int nid =folio_nid (folio);VM_BUG_ON_FOLIO (!folio_test_hugetlb_vmemmap_optimized (folio), folio);@@ -1453,9 +1454,11 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, }__folio_set_hugetlb (folio); -/* Clear all folio->private flags except hugetlb_vmemmap_optimized. */ +/* Preserve the vmemmap optimization state and CMA allocation origin. */ folio->private =NULL ;folio_set_hugetlb_vmemmap_optimized (folio); +if (cma) +folio_set_hugetlb_cma (folio);arch_clear_hugetlb_flags (folio);enqueue_hugetlb_folio (h, folio);Signature · 1 line
2.53.0
Re: [PATCH 2/3] mm/hugetlb: preserve CMA flag when returning folios to the pool
Quoted 13 lines · Muchun Song
Quoted 13 lines · Kaitao Cheng
From: Kaitao Cheng <[email protected]>
When vmemmap restoration fails,
add_hugetlb_folio()returns the folio to the hugetlb pool. It clearsfolio->privateand restores onlyHPG_vmemmap_optimized, losingHPG_cmaon CMA-allocated folios.A later successful free then bypasses
hugetlb_cma_free_frozen_folio()and returns the pages throughfree_frozen_pages(). The CMA allocation bitmap remains set, so the range is unavailable for future CMA allocations.
Quoted 4 lines
Quoted 2 lines
Save the CMA flag before resetting
folio->privateand restore it along with the vmemmap optimization flag.We already knew this issue. Cc Zi Yan. Seems he hasn't sent a fix.
Thanks. No, I have not formed a proper patch yet.
Quoted 4 lines
But we want a different solution. See:
Feel free to grab my version from the link and add VM_WARN_ON_ONCE_FOLIO()s Muchun suggested. You can add
Thanks.
Quoted 35 lines
Thanks,
MuchunQuoted 31 lines
Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")Signed-off-by Kaitao Cheng <[email protected]>---
mm/hugetlb.c| 5 ++++-1 file changed, 4 insertions(+), 1 deletion(-)mm/hugetlb.c +4
-1@@ -1437,6 +1437,7 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio,void add_hugetlb_folio (struct hstate *h,struct folio *folio,bool adjust_surplus) { +bool cma =folio_test_hugetlb_cma (folio);int nid =folio_nid (folio);VM_BUG_ON_FOLIO (!folio_test_hugetlb_vmemmap_optimized (folio), folio);@@ -1453,9 +1454,11 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, }__folio_set_hugetlb (folio); -/* Clear all folio->private flags except hugetlb_vmemmap_optimized. */ +/* Preserve the vmemmap optimization state and CMA allocation origin. */ folio->private =NULL ;folio_set_hugetlb_vmemmap_optimized (folio); +if (cma) +folio_set_hugetlb_cma (folio);
Quoted 5 lines
Quoted 5 lines
mm/hugetlb.c +0
-0arch_clear_hugetlb_flags (folio);enqueue_hugetlb_folio (h, folio);Signature · 1 line
2.53.0
Best Regards,
Yan, Zi
Re: [PATCH 1/3] mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed input
In reply to Kaitao ChengQuoted 8 lines · Kaitao Cheng
From: Kaitao Cheng <[email protected]>
When
hugetlb_cmacontains a global size after node-specific entries,cmdline_parse_hugetlb_cma()enters the global branch and reparses the original parameter string. For hugetlb_cma=1:2G,4G, this interprets the initial node ID as a one-byte global size and clears the 2 GiB reservation recorded for node 1.
Quoted 8 lines
Global and node-specific formats cannot be mixed. Return
-EINVALwhen a global entry follows a node-specific entry, before updating any settings. This preserves the valid prefix, so the example retains node 1's 2 GiB reservation and reports the malformed option.Fixes 38e719ab2673 ("hugetlb: support node specified when using cma for gigantic hugepages")Signed-off-by Kaitao Cheng <[email protected]>
Quoted 21 lines
--- mm/hugetlb_cma.c | 3 +++ 1 file changed, 3 insertions(+)mm/hugetlb_cma.c +3
-0@@ -167,6 +167,9 @@ static int __init cmdline_parse_hugetlb_cma(char *p) }else {char *next; +if (s != p) +return -EINVAL ; + tmp =memparse (p, &next);if (*next =='%' ) {if (tmp >100 ) {Signature · 1 line
2.53.0
Signature · 2 lines
Oscar Salvador SUSE Labs
Re: [PATCH 3/3] mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios
In reply to Kaitao ChengQuoted 15 lines · Kaitao Cheng
From: Kaitao Cheng <[email protected]>
free_huge_folio()unconditionally decrementsNR_HUGETLB, but freshly allocated surplus folios can be freed before the counter has been incremented.This happens when
alloc_surplus_hugetlb_folio()rejects a fresh folio after its second surplus limit check. It also happens whengather_surplus_pages()frees folios because a reservation cannot be satisfied, or because concurrent pool changes leave excess folios after the reservation succeeds.These paths leave unmatched decrements, causing HugeTLB usage to be underreported and potentially underflowing the counter.
Quoted 6 lines
Increment
NR_HUGETLBbefore callingfree_huge_folio()in both paths to balance its unconditional decrement.Fixes 05d4532b60e3 ("memcg/hugetlb: add hugeTLB counters to memcg")Signed-off-by Kaitao Cheng <[email protected]>
I feel like the whole hugetlb accounting is a bit of a nightmare. I wish we could teach free_huge_folio to ignore accounting-wise these pages but not sure if that would be any prettier/easier.
I will have another look later on.
Signature · 2 lines
Oscar Salvador SUSE Labs
Muchun Song
maintainerRe: [PATCH 3/3] mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios
Quoted 20 lines · Oscar Salvador (SUSE)
Quoted 15 lines · Kaitao Cheng
From: Kaitao Cheng <[email protected]>
free_huge_folio()unconditionally decrementsNR_HUGETLB, but freshly allocated surplus folios can be freed before the counter has been incremented.This happens when
alloc_surplus_hugetlb_folio()rejects a fresh folio after its second surplus limit check. It also happens whengather_surplus_pages()frees folios because a reservation cannot be satisfied, or because concurrent pool changes leave excess folios after the reservation succeeds.These paths leave unmatched decrements, causing HugeTLB usage to be underreported and potentially underflowing the counter.
Quoted 8 lines
Quoted 6 lines
Increment
NR_HUGETLBbefore callingfree_huge_folio()in both paths to balance its unconditional decrement.Fixes 05d4532b60e3 ("memcg/hugetlb: add hugeTLB counters to memcg")Signed-off-by Kaitao Cheng <[email protected]>I feel like the whole hugetlb accounting is a bit of a nightmare.
Can't agree more.
Quoted 2 lines
I wish we could teach
free_huge_folioto ignore accounting-wise these pages but not sure if that would be any prettier/easier.
My thinking is that we should not use lruvec_stat_mod_folio(). Instead, we should use mod_node_page_state() and mod_memcg_lruvec_state() independently. In that case, when a new hugetlb page is allocated, we could account for it directly via mod_node_page_state(). The reason is that lruvec_stat_mod_folio() also accounts per-memcg NR_HUGETLB; only by splitting the two can we minimize the problems introduced by accounting. For callers that need to consider memcg, mod_memcg_lruvec_state() can be used.
I expect that after some cleanup, there will be only one place that needs to handle per-node NR_HUGETLB.
Thoughts?
Thanks,
Muchun
Quoted 7 lines
I will have another look later on.
Signature · 2 lines
Oscar Salvador SUSE Labs
Re: [PATCH 3/3] mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios
Quoted 7 lines · Muchun Song
My thinking is that we should not use
lruvec_stat_mod_folio(). Instead, we should usemod_node_page_state()andmod_memcg_lruvec_state()independently. In that case, when a new hugetlb page is allocated, we could account for it directly viamod_node_page_state(). The reason is thalruvec_stat_mod_folio()also accounts per-memcgNR_HUGETLB; only by splitting the two can we minimize the problems introduced by accounting. For callers that need to consider memcg,mod_memcg_lruvec_state()can be used.
Ok, this sounds pretty compeling, and IMHO makes sense.
I mean I get the ideabehind this patch but it feels like papering over the real problem.
Quoted 17 lines
I expect that after some cleanup, there will be only one place that needs to handle per-node
NR_HUGETLB.Thoughts?
Thanks,
MuchunQuoted 7 lines
I will have another look later on.
Signature · 2 lines
Oscar Salvador SUSE Labs
Signature · 2 lines
Oscar Salvador SUSE Labs