Refactor alloc_hugetlb_folio() to use the memcg try-commit-cancel protocol. Do this to allow the core of allocating a hugetlb folio and associated memcg charging to be refactored out in a later patch. In addition, checking cgroup memory limits before allocating avoids unnecessary allocation if the limits had already been hit. Update error code propagation in the failure paths so that existing error cases still return -ENOSPC, but if the memory limit is reached, return -ENOMEM as before. Signed-off-by: Ackerley Tng --- mm/hugetlb.c | 53 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 74b5136fdeb54..70e91edc47dc1 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2881,6 +2881,8 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, int ret, idx; struct hugetlb_cgroup *h_cg = NULL; gfp_t gfp = htlb_alloc_mask(h); + bool memory_charged = false; + struct mem_cgroup *memcg; struct mempolicy *mpol; nodemask_t *nodemask; int nid; @@ -2917,8 +2919,10 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, */ if (map_chg) { gbl_chg = hugepage_subpool_get_pages(spool, 1); - if (gbl_chg < 0) + if (gbl_chg < 0) { + ret = -ENOSPC; goto out_end_reservation; + } } else { /* * If we have the vma reservation ready, no need for extra @@ -2934,13 +2938,25 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, if (map_chg) { ret = hugetlb_cgroup_charge_cgroup_rsvd( idx, pages_per_huge_page(h), &h_cg); - if (ret) + if (ret) { + ret = -ENOSPC; goto out_subpool_put; + } } ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg); - if (ret) + if (ret) { + ret = -ENOSPC; goto out_uncharge_cgroup_reservation; + } + + memcg = get_mem_cgroup_from_current(); + ret = mem_cgroup_hugetlb_try_charge(memcg, gfp | __GFP_RETRY_MAYFAIL, + pages_per_huge_page(h)); + if (ret == -ENOMEM) + goto out_put_memcg; + + memory_charged = !ret; spin_lock_irq(&hugetlb_lock); @@ -2961,7 +2977,8 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, folio = alloc_buddy_hugetlb_folio_with_mpol(h, mpol, nid, nodemask); if (!folio) { mpol_cond_put(mpol); - goto out_uncharge_cgroup; + ret = -ENOSPC; + goto out_uncharge_memory; } spin_lock_irq(&hugetlb_lock); list_add(&folio->lru, &h->hugepage_activelist); @@ -2991,6 +3008,12 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, spin_unlock_irq(&hugetlb_lock); + lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); + + if (memory_charged) + mem_cgroup_commit_charge(folio, memcg); + mem_cgroup_put(memcg); + hugetlb_set_folio_subpool(folio, spool); if (map_chg != MAP_CHG_ENFORCED) { @@ -3021,22 +3044,14 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, } } - ret = mem_cgroup_charge_hugetlb(folio, gfp | __GFP_RETRY_MAYFAIL); - /* - * 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) { - free_huge_folio(folio); - return ERR_PTR(-ENOMEM); - } - return folio; -out_uncharge_cgroup: +out_uncharge_memory: + if (memory_charged) + mem_cgroup_cancel_charge(memcg, pages_per_huge_page(h)); +out_put_memcg: + mem_cgroup_put(memcg); + hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg); out_uncharge_cgroup_reservation: if (map_chg) @@ -3056,7 +3071,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); - return ERR_PTR(-ENOSPC); + return ERR_PTR(ret); } static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact) -- 2.53.0.310.g728cabbaf7-goog