Re: [PATCH v2] mm/hugetlb: fix subpool accounting after cgroup charge failure
From: Zhao Li
Date: Tue Apr 28 2026 - 08:46:53 EST
On Tue, Apr 28, 2026 at 07:30:59PM +0800, Lance Yang wrote:
> IIUC, there are three cases:
> [...]
> 2) gbl_chg > 0 && spool->max_hpages != -1
> [...]
> If hugepage_subpool_put_pages() returns 0 here, it restored one
> reservation in spool->rsv_hpages, so we also need to increment
> h->resv_huge_pages.
Thanks for working through the three cases - that's a clean
breakdown and matches what v2 was trying to do. We ran into trouble
on case 2 specifically: the h->resv_huge_pages++ on the
put-returned-0 path looked right in isolation but turns out to be
unsafe once you put it next to concurrent hugetlb_unreserve_pages()
or free_huge_folio(). Two reachable orderings break it:
* free_huge_folio() with HPageRestoreReserve set already does
h->resv_huge_pages++ on its own. v2's bump on the gbl_chg > 0
cleanup double-counts against that.
* hugetlb_unreserve_pages() does hugetlb_acct_memory(h, -X) which
subtracts from h->resv_huge_pages and may also return surplus
backing. v2's bump then leaves rsv_hpages backed by no
h->resv_huge_pages - a phantom reservation that the next
subpool_get_pages() consumes without real backing.
v3 ended up going a different way: the gbl_chg > 0 cleanup is now
restricted to (max_hpages != -1, min_hpages == -1). In that
configuration hugepage_subpool_put_pages()'s min-restoration branch
is dead, so a direct used_hpages-- under spool->lock is the exact
inverse of the speculative bump - no put_pages(), no
h->resv_huge_pages++, no concurrent-races to reason about.
Your case 3 (max_hpages == -1) is unchanged: cleanup is a no-op,
because get_pages() didn't touch any subpool field.
Mounts with min_hpages != -1 are left at v1 behaviour for now. That
quadrant has an inherited race that also exists at
hugetlb_reserve_pages()'s out_put_pages cleanup; a coordinated fix
belongs in a separate RFC rather than this stable backport.
v3:
https://lore.kernel.org/linux-mm/20260428113037.88766-2-enderaoelyther@xxxxxxxxx/
Thanks for the review.
--
Zhao Li