Subject: [PATCH v3 0/4] mm/hugetlb: Fix CMA parsing, flags and usage accounting
Subsystems (4)
v3 cover Subject: [PATCH v3 0/4] mm/hugetlb: Fix CMA parsing, flags and usage accounting
From: Kaitao Cheng <[email protected]>
This series fixes three independent HugeTLB issues reported by Sashiko and includes a follow-up cleanup of NR_HUGETLB accounting.
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 persistent folio flags when a failed vmemmap restore returns a folio to the pool. This retains CMA allocation tracking and raw hardware poison information, while ensuring transient flags are cleared before returning the folio.
Patch 3 retains the minimal NR_HUGETLB accounting fix, as suggested by Muchun Song, to keep it straightforward to backport. It balances the counter before freeing unused surplus folios during reservation gathering or after a failed overcommit recheck.
Patch 4 moves NR_HUGETLB node accounting to the underlying folio state transitions and separates it from memcg statistics. This cleans up the accounting in the allocation and surplus cleanup paths.
Changes in v3:
- Retain patch 3/3 from v1.
- Fix code alignment.
Changes in v2:
- Patch 2: Preserve persistent flags, clear transient flags, and warn about unexpected flags.
- Patch 3: Account at folio state transitions and separate node and memcg statistics.
Link to v2:
https://lore.kernel.org/all/[email protected]/
Link to v1:
https://lore.kernel.org/all/[email protected]/
Kaitao Cheng (4):
mm/hugetlb_cma: fix incorrect hugetlb_cma_size assignment on malformed
input
mm/hugetlb: Preserve persistent flags when returning folios to the
pool
mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios mm/hugetlb: Account NR_HUGETLB at folio state transitions
mm/hugetlb.c | 52 +++++++++++++++++++++++++++++++----------------- mm/hugetlb_cma.c | 3 +++ mm/memcontrol.c | 6 +----- 3 files changed, 38 insertions(+), 23 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/4 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
@@ -165,6 +165,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 v3 2/4 mm/hugetlb: Preserve persistent flags when returning folios to the pool
In reply to Kaitao ChengFrom: Kaitao Cheng <[email protected]>
add_hugetlb_folio() returns a folio to the HugeTLB pool when vmemmap restoration fails. It clears folio->private and restores only HPG_vmemmap_optimized, losing other flags that must survive the rollback.
Clearing HPG_cma causes a later free to bypass the CMA release path, leaving the CMA allocation bitmap set. Clearing HPG_raw_hwp_unreliable loses the indication that the locations of poisoned subpages are unknown, undermining the checks that prevent freeing or unpoisoning such folios.
Preserve the existing flags instead of resetting folio->private. Callers already clear HPG_restore_reserve, HPG_migratable and HPG_freed before returning a folio to the pool. Clear HPG_temporary in free_huge_folio() before attempting to free a temporary folio, so a failed vmemmap restore does not leave that flag set on a folio returned as surplus.
Add a VM_WARN_ON_ONCE_FOLIO() check for flags other than HPG_vmemmap_optimized, HPG_raw_hwp_unreliable and HPG_cma to detect incomplete transient flag cleanup.
--- mm/hugetlb.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
mm/hugetlb.c +7 -3
@@ -1450,6 +1450,12 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio,lockdep_assert_held (&hugetlb_lock); +/* Callers must clear transient flags before returning to the pool. */ +VM_WARN_ON_ONCE_FOLIO ((unsigned long )folio->private & + ~(BIT (HPG_vmemmap_optimized) | +BIT (HPG_raw_hwp_unreliable) | +BIT (HPG_cma)), folio); +INIT_LIST_HEAD (&folio->lru); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++;
@@ -1460,9 +1466,6 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, }__folio_set_hugetlb (folio); -/* Clear all folio->private flags except hugetlb_vmemmap_optimized. */ - folio->private =NULL ; -folio_set_hugetlb_vmemmap_optimized (folio);arch_clear_hugetlb_flags (folio);enqueue_hugetlb_folio (h, folio);
@@ -1750,6 +1753,7 @@ void free_huge_folio(struct folio *folio) h->resv_huge_pages++;if (folio_test_hugetlb_temporary (folio)) { +folio_clear_hugetlb_temporary (folio);remove_hugetlb_folio (h, folio, false);spin_unlock_irqrestore (&hugetlb_lock, flags);update_and_free_hugetlb_folio (h, folio, true);
Signature · 1 line
2.53.0
PATCH v3 3/4 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
@@ -2140,6 +2140,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 ; }
@@ -2384,9 +2386,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
PATCH v3 4/4 mm/hugetlb: Account NR_HUGETLB at folio state transitions
In reply to Kaitao ChengFrom: Kaitao Cheng <[email protected]>
NR_HUGETLB updates are spread across allocation entry points and freeing paths, mixing node accounting with memcg statistics. This makes the accounting harder to follow and requires compensating updates when freeing unused surplus folios.
Consolidate node accounting around folio state transitions. Increment NR_HUGETLB when taking a folio from the free pool or allocating a fresh surplus or migration folio, and decrement it when returning the folio to the pool or removing it from use.
Separate memcg statistics into mod_hugetlb_memcg_state(), updating them after a successful charge and before uncharging. This keeps node accounting independent of memcg charging and removes redundant updates from the allocation entry points and surplus cleanup paths.
--- mm/hugetlb.c | 50 +++++++++++++++++++++++++++---------------------- mm/memcontrol.c | 6 +----- 2 files changed, 29 insertions(+), 27 deletions(-)
mm/hugetlb.c +28 -22
@@ -1288,6 +1288,8 @@ static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,folio_clear_hugetlb_freed (folio); h->free_huge_pages--; h->free_huge_pages_node[nid]--; +mod_node_page_state (folio_pgdat (folio),NR_HUGETLB , +pages_per_huge_page (h));return folio; }
@@ -1423,6 +1425,9 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio,folio_clear_hugetlb_freed (folio); h->free_huge_pages--; h->free_huge_pages_node[nid]--; + }else { +mod_node_page_state (folio_pgdat (folio),NR_HUGETLB , + -(long )pages_per_huge_page (h)); }if (adjust_surplus) { h->surplus_huge_pages--;
@@ -1700,6 +1705,17 @@ struct hstate *size_to_hstate(unsigned long size)return NULL ; } +static void mod_hugetlb_memcg_state (struct folio *folio,int nr_pages) +{ +struct mem_cgroup *memcg; + +guard (rcu)(); + memcg =folio_memcg (folio); +if (memcg) +mod_memcg_lruvec_state (mem_cgroup_lruvec (memcg,folio_pgdat (folio)), +NR_HUGETLB , nr_pages); +} +void free_huge_folio (struct folio *folio) {/*
@@ -1747,7 +1763,7 @@ void free_huge_folio(struct folio *folio) pages_per_huge_page(h), folio);hugetlb_cgroup_uncharge_folio_rsvd (hstate_index (h),pages_per_huge_page (h), folio); -lruvec_stat_mod_folio (folio,NR_HUGETLB , -pages_per_huge_page (h)); +mod_hugetlb_memcg_state (folio, -pages_per_huge_page (h));mem_cgroup_uncharge (folio);if (restore_reserve) h->resv_huge_pages++;
@@ -1763,6 +1779,8 @@ void free_huge_folio(struct folio *folio)spin_unlock_irqrestore (&hugetlb_lock, flags);update_and_free_hugetlb_folio (h, folio, true); }else { +mod_node_page_state (folio_pgdat (folio),NR_HUGETLB , + -(long )pages_per_huge_page (h));arch_clear_hugetlb_flags (folio);enqueue_hugetlb_folio (h, folio);spin_unlock_irqrestore (&hugetlb_lock, flags);
@@ -2130,6 +2148,9 @@ static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h,*/ account_new_hugetlb_folio (h, folio); +/* The overcommit recheck below can free this folio. */ +mod_node_page_state (folio_pgdat (folio),NR_HUGETLB ,pages_per_huge_page (h)); +/* * We could have raced with the pool size change. * Double check that and simply deallocate the new page
@@ -2140,8 +2161,6 @@ 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 ; }
@@ -2169,6 +2188,7 @@ static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_masspin_lock_irq (&hugetlb_lock);account_new_hugetlb_folio (h, folio); +mod_node_page_state (folio_pgdat (folio),NR_HUGETLB ,pages_per_huge_page (h));spin_unlock_irq (&hugetlb_lock);/* fresh huge pages are frozen */
@@ -2227,10 +2247,6 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,spin_unlock_irq (&hugetlb_lock); -if (folio) -lruvec_stat_mod_folio (folio,NR_HUGETLB , -folio_nr_pages (folio)); -return folio; }
@@ -2258,9 +2274,6 @@ struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid, nmask); } -if (folio) -lruvec_stat_mod_folio (folio,NR_HUGETLB ,folio_nr_pages (folio)); -return folio; }
@@ -2378,6 +2391,8 @@ static int gather_surplus_pages(struct hstate *h, long delta)if ((--needed) <0 )break ;/* Add the page to the hugetlb allocator */ +mod_node_page_state (folio_pgdat (folio),NR_HUGETLB , + -(long )pages_per_huge_page (h));enqueue_hugetlb_folio (h, folio); } free:
@@ -2386,13 +2401,9 @@ 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) { -lruvec_stat_mod_folio (folio,NR_HUGETLB ,pages_per_huge_page (h)); +list_for_each_entry_safe (folio, tmp, &surplus_list, lru)free_huge_folio (folio); - }spin_lock_irq (&hugetlb_lock);return ret;
@@ -2957,13 +2968,6 @@ struct folio *hugetlb_alloc_folio(struct hstate *h, ret =mem_cgroup_charge_hugetlb (folio, mm, gfp | __GFP_RETRY_MAYFAIL); -/* -* Unconditionally increment NR_HUGETLB here because if -* mem_cgroup_charge_hugetlb failed, freeing the page will -* decrement NR_HUGETLB. -*/ -lruvec_stat_mod_folio (folio,NR_HUGETLB , nr_pages); -if (ret == -ENOMEM ) {folio_put (folio);/*
@@ -2978,6 +2982,8 @@ struct folio *hugetlb_alloc_folio(struct hstate *h,return ERR_PTR (-ENOSPC ); } +mod_hugetlb_memcg_state (folio, nr_pages); +return folio; err_uncharge_hugetlb_cgroup:
mm/memcontrol.c +1 -5
@@ -5440,11 +5440,7 @@ int mem_cgroup_charge_hugetlb(struct folio *folio, struct mm_struct *mm,struct mem_cgroup *memcg =get_mem_cgroup_from_mm (mm);int ret =0 ; -/* -* Even memcg does not account for hugetlb, we still want to update -* system-level stats via lruvec_stat_mod_folio. Return 0, and skip -* charging the memcg. -*/ +/* Skip charging unless hugetlb accounting is enabled for cgroup v2. */ if (mem_cgroup_disabled () || !memcg_accounts_hugetlb () || !memcg || !cgroup_subsys_on_dfl (memory_cgrp_subsys)) goto out;
Signature · 1 line
2.53.0
Andrew Morton
maintainerQuoted 4 lines · On Fri, 6 Nov 2026 18:05:17 +0800 Kaitao Cheng <[email protected]> wrote:
From: Kaitao Cheng <[email protected]>
This series fixes three independent HugeTLB issues reported by Sashiko and includes a follow-up cleanup of
NR_HUGETLBaccounting.
Thanks, I've updated mm.git's mm-unstable branch to this version.
Quoted 4 lines
Changes in v3:
- Retain patch 3/3 from v1.
- Fix code alignment.
Here's how v3 altered mm.git:
mm/hugetlb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~b +3 -3
@@ -1450,9 +1450,9 @@ void add_hugetlb_folio(struct hstate *h,/* Callers must clear transient flags before returning to the pool. */ VM_WARN_ON_ONCE_FOLIO ((unsigned long )folio->private & - ~(BIT (HPG_vmemmap_optimized) | -BIT (HPG_raw_hwp_unreliable) | -BIT (HPG_cma)), folio); + ~(BIT (HPG_vmemmap_optimized) | +BIT (HPG_raw_hwp_unreliable) | +BIT (HPG_cma)), folio);INIT_LIST_HEAD (&folio->lru); h->nr_huge_pages++;
_
Re: [PATCH v3 3/4] mm/hugetlb: fix NR_HUGETLB accounting for unused surplus folios
In reply to Kaitao ChengQuoted 19 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]>
Thanks.