Skip to contentSkip to conversation

Subject: mm/hugetlb: fix CMA handling and surplus allocation accounting

PATCH Superseded Kaitao Cheng 4 participants 11 messages Oct 28 – Oct 29

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Fixes 38e719ab2673 ("hugetlb: support node specified when using cma for gigantic hugepages")
---
 mm/hugetlb_cma.c | 3 +++
 1 file changed, 3 insertions(+)
mm/hugetlb_cma.c +3 -0
index 95fd2d190f0d..8e6b20341fc4 100644
@@ -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
#4 lore raw

PATCH 2/3 mm/hugetlb: preserve CMA flag when returning folios to the pool

In reply to Kaitao Cheng

From: 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.

Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
---
 mm/hugetlb.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/hugetlb.c +4 -1
index 4a008afaedf9..8da4bab676c9 100644
@@ -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
#7 lore raw

PATCH 3/3 mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios

In reply to Kaitao Cheng

From: 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.

Fixes 05d4532b60e3 ("memcg/hugetlb: add hugeTLB counters to memcg")
---
 mm/hugetlb.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/hugetlb.c +7 -1
index 8da4bab676c9..d564e6935e61 100644
@@ -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
maintainer Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed input

In reply to Kaitao Cheng
Quoted 12 lines · Kaitao Cheng

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.

Quoted 8 lines

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.

Fixes 38e719ab2673 ("hugetlb: support node specified when using cma for gigantic hugepages")

Thanks

maintainer
#5 lore raw

Re: [PATCH 2/3] mm/hugetlb: preserve CMA flag when returning folios to the pool

In reply to Kaitao Cheng
Quoted 10 lines · Kaitao Cheng

From: 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.

Quoted 7 lines

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.

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")

---
mm/hugetlb.c | 5 ++++-

1 file changed, 4 insertions(+), 1 deletion(-)
mm/hugetlb.c +4 -1
index 4a008afaedf9..8da4bab676c9 100644
@@ -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
#6 lore raw

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 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.

Quoted 4 lines
Quoted 2 lines

Save the CMA flag before resetting folio->private and 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:

https://lore.kernel.org/linux-mm/[email protected]/

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,
Muchun

Quoted 31 lines
Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")

---
mm/hugetlb.c | 5 ++++-

1 file changed, 4 insertions(+), 1 deletion(-)
mm/hugetlb.c +4 -1
index 4a008afaedf9..8da4bab676c9 100644
@@ -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 -0
 	arch_clear_hugetlb_flags(folio);
 	enqueue_hugetlb_folio(h, folio);
Signature · 1 line
2.53.0

Best Regards,
Yan, Zi

Reviewed-by Oscar Salvador on v1 1/3
#3 lore raw

Re: [PATCH 1/3] mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed input

In reply to Kaitao Cheng
Quoted 8 lines · Kaitao Cheng

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.

Quoted 8 lines

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.

Fixes 38e719ab2673 ("hugetlb: support node specified when using cma for gigantic hugepages")
Quoted 21 lines
---
 mm/hugetlb_cma.c | 3 +++
 1 file changed, 3 insertions(+)
mm/hugetlb_cma.c +3 -0
index 95fd2d190f0d..8e6b20341fc4 100644
@@ -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
#8 lore raw

Re: [PATCH 3/3] mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios

In reply to Kaitao Cheng
Quoted 15 lines · Kaitao Cheng

From: 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.

Quoted 6 lines

Increment NR_HUGETLB before calling free_huge_folio() in both paths to balance its unconditional decrement.

Fixes 05d4532b60e3 ("memcg/hugetlb: add hugeTLB counters to memcg")

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
maintainer
#9 lore raw

Re: [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 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.

Quoted 8 lines
Quoted 6 lines

Increment NR_HUGETLB before calling free_huge_folio() in both paths to balance its unconditional decrement.

Fixes 05d4532b60e3 ("memcg/hugetlb: add hugeTLB counters to memcg")

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_folio to 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
#10 lore raw

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 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 tha 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.

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,
Muchun

Quoted 7 lines

I will have another look later on.

Signature · 2 lines
Oscar Salvador
SUSE Labs
Signature · 2 lines
Oscar Salvador
SUSE Labs