[PATCH v3 4/6] mm: hugetlb: Use error variable in alloc_hugetlb_folio

From: Ackerley Tng via B4 Relay

Date: Mon May 18 2026 - 20:20:15 EST


From: Ackerley Tng <ackerleytng@xxxxxxxxxx>

Refactor alloc_hugetlb_folio to use a local variable for returning error
codes. Instead of returning ERR_PTR(-ENOSPC) at the end of the error
path, assign -ENOSPC to a return variable at each failure point and
return that variable at the end.

This allows the cleanup goto targets to be used with other errors in a
later patch.

No functional change intended.

Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
mm/hugetlb.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 9807bbe0d70df..ad07e72d6fac3 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2903,8 +2903,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
@@ -2920,13 +2922,17 @@ 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;
+ }

/* Takes reference on mpol. */
nid = huge_node(vma, addr, gfp, &mpol, &nodemask);
@@ -2954,6 +2960,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
mpol_cond_put(mpol);
if (!folio) {
mpol_cond_put(mpol);
+ ret = -ENOSPC;
goto out_uncharge_cgroup;
}
spin_lock_irq(&hugetlb_lock);
@@ -3046,7 +3053,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.54.0.563.g4f69b47b94-goog