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

From: Ackerley Tng via B4 Relay

Date: Wed May 06 2026 - 11:57:14 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 3395de4d0999a..68c21305fc86a 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2894,8 +2894,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
@@ -2911,13 +2913,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;
+ }

spin_lock_irq(&hugetlb_lock);

@@ -2938,6 +2944,7 @@ 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);
+ ret = -ENOSPC;
goto out_uncharge_cgroup;
}
spin_lock_irq(&hugetlb_lock);
@@ -3030,7 +3037,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.545.g6539524ca2-goog