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

From: Kaitao Cheng

Date: Wed Oct 28 2026 - 07:37:31 EST


From: Kaitao Cheng <chengkaitao@xxxxxxxxxx>

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")
Closes: https://sashiko.dev/#/bug/linux-860a3b8f-0b07-4cd5-a122-aa2288463bee
Signed-off-by: Kaitao Cheng <chengkaitao@xxxxxxxxxx>
---
mm/hugetlb.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 8da4bab676c9..d564e6935e61 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -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;
--
2.53.0