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

From: Oscar Salvador (SUSE)

Date: Wed Oct 28 2026 - 11:10:29 EST


On Wed, Oct 28, 2026 at 07:33:19PM +0800, Kaitao Cheng wrote:
> 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>

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.



--
Oscar Salvador
SUSE Labs