Re: [PATCH v2] mm/hugetlb: fix subpool accounting after cgroup charge failure
From: Oscar Salvador
Date: Tue Apr 28 2026 - 05:22:20 EST
On Tue, Apr 28, 2026 at 11:07:13AM +0800, Zhao Li wrote:
> alloc_hugetlb_folio() calls hugepage_subpool_get_pages() when map_chg
> is set. For subpools with max_hpages, that increments used_hpages.
> If the later hugetlb cgroup charge fails, the unwind must undo that
> charge even when gbl_chg > 0.
I found that last sentence misleading, because we do not really care
about hugetlb cgroup charge/uncharge (besides that being of the reasons
we end up on error path) but rather the fact that we fiddle with
subpool->used_hpages and we need to undo that when we rollback.
> hugepage_subpool_put_pages() can also restore rsv_hpages if concurrent
> frees move used_hpages below min_hpages between the get and put. When
> that happens on the gbl_chg > 0 path, restore the matching global
> reservation as well.
Well, that does not quite explain the problem I think, at least not clear enough?
So the problem at hand (IIUC) is that
1) if we took a global reservation
2) and concurrent hugepage_subpool_put_pages operations made
used_hpages be below min_hpages, and so subpool's reservation
was incremented, but we need to increment the global one as well
otherwise the next time we pull a page from the spool,
global resv_huge_pages will be inbalanced
> Skip the gbl_chg > 0 put when max_hpages is unset. For a min_size-only
> subpool, get_pages() did not change subpool state and put_pages() would
> create a false reservation.
>
> Signed-off-by: Zhao Li <enderaoelyther@xxxxxxxxx>
> ---
> Changes in v2:
> - Handle rsv_hpages restoration when racing frees cross min_hpages.
> - Skip gbl_chg > 0 put_pages() when max_hpages is unset.
>
> mm/hugetlb.c | 21 ++++++++++++++++-----
> 1 file changed, 16 insertions(+), 5 deletions(-)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index f24bf49be047e..4065d66fdcb5c 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -3026,12 +3026,23 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
> h_cg);
> out_subpool_put:
> /*
> - * put page to subpool iff the quota of subpool's rsv_hpages is used
> - * during hugepage_subpool_get_pages.
> + * map_chg means hugepage_subpool_get_pages() succeeded above.
> + * If max_hpages accounting was touched, undo it. If racing frees
> + * moved the subpool below min_hpages, the put path may restore a
> + * subpool reservation. Restore the matching global reservation too.
I would split the comment in two parts and place them within the block
they belong, otherwise it sounds confusing.
And maybe elaborate a little bit more.
Subpools, reservations and hugetlb make a very head-spinning situation, so let
us make our life easier.
--
Oscar Salvador
SUSE Labs