From: Ackerley Tng When mem_cgroup_charge_hugetlb() fails, alloc_hugetlb_folio() clears the charges and frees the folio. The reservation committed via vma_commit_reservation() was not undone, leaving the reservation map in an inconsistent state, causing resv_huge_pages to leak when the process exited. Fix this by moving the mem_cgroup_charge_hugetlb() call earlier in alloc_hugetlb_folio(), before vma_commit_reservation() is called. If the charge fails now, the reservation is not yet committed. Jump to out_subpool_put, which will then call vma_end_reservation() to abort the reservation and keep the reservation map and resv_huge_pages counter consistent. Fixes: 991135774c0e0 ("memcg/hugetlb: introduce mem_cgroup_charge_hugetlb") Signed-off-by: Ackerley Tng --- mm/hugetlb.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e42049e2c038b..6e4acb1f021a5 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2960,6 +2960,25 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, spin_unlock_irq(&hugetlb_lock); + ret = mem_cgroup_charge_hugetlb(folio, gfp); + /* + * Unconditionally increment NR_HUGETLB here. If it turns out that + * mem_cgroup_charge_hugetlb failed, then immediately free the page and + * decrement NR_HUGETLB. + */ + lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); + + if (ret == -ENOMEM) { + folio_put(folio); + /* + * Charges to hugetlb_cgroup for usage and + * reservations were already committed, so folio_put() + * would have uncharged those. Go straight to undoing + * subpool charges. + */ + goto out_subpool_put; + } + hugetlb_set_folio_subpool(folio, spool); if (map_chg != MAP_CHG_ENFORCED) { @@ -2987,19 +3006,6 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, } } - ret = mem_cgroup_charge_hugetlb(folio, gfp); - /* - * Unconditionally increment NR_HUGETLB here. If it turns out that - * mem_cgroup_charge_hugetlb failed, then immediately free the page and - * decrement NR_HUGETLB. - */ - lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); - - if (ret == -ENOMEM) { - folio_put(folio); - goto err; - } - return folio; out_uncharge_cgroup: @@ -3020,7 +3026,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, out_end_reservation: if (map_chg != MAP_CHG_ENFORCED) vma_end_reservation(h, vma, addr); -err: + /* * Return -ENOSPC when this function fails to allocate or * charge a huge page. If a standard (PAGE_SIZE) page -- 2.55.0.795.g602f6c329a-goog