Skip to contentSkip to conversation

mm/hugetlb: Fix incorrect error return from hugetlb_reserve_pages()

PATCH Superseded Shameer Kolothum 3 participants 4 messages Oct 24, 2025 – Nov 24, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/hugetlb: Fix incorrect error return from hugetlb_reserve_pages()

The function hugetlb_reserve_pages() returns the number of pages added to the reservation map on success and a negative error code on failure (e.g. -EINVAL, -ENOMEM). However, in some error paths, it may return -1 directly.

For example, a failure at:

if (hugetlb_acct_memory(h, gbl_reserve) < 0)
goto out_put_pages;

results in returning -1 (since add = -1), which may be misinterpreted in userspace as -EPERM.

Fix this by explicitly capturing and propagating the return values from helper functions, and using -EINVAL for all other failure cases.

Fixes 986f5f2b4be3 ("mm/hugetlb: make hugetlb_reserve_pages() return nr of entries updated")

---
Addressed commenst from v1. Thanks!
https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/hugetlb.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
mm/hugetlb.c +18 -7
index 795ee393eac0..b6c0024ff5a5 100644
@@ -7269,6 +7269,7 @@ long hugetlb_reserve_pages(struct inode *inode,
 	struct resv_map *resv_map;
 	struct hugetlb_cgroup *h_cg = NULL;
 	long gbl_reserve, regions_needed = 0;
+	int err;
 
 	/* This should never happen */
 	if (from > to) {
@@ -7308,8 +7309,10 @@ long hugetlb_reserve_pages(struct inode *inode,
 	} else {
 		/* Private mapping. */
 		resv_map = resv_map_alloc();
-		if (!resv_map)
+		if (!resv_map) {
+			err = -EINVAL;
 			goto out_err;
+		}
 
 		chg = to - from;
 
@@ -7317,11 +7320,15 @@ long hugetlb_reserve_pages(struct inode *inode,
 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
 	}
 
-	if (chg < 0)
+	if (chg < 0) {
+		/* region_chg() above can return -ENOMEM */
+		err = (chg == -ENOMEM) ? -ENOMEM : -EINVAL;
 		goto out_err;
+	}
 
-	if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
-				chg * pages_per_huge_page(h), &h_cg) < 0)
+	err = hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
+				chg * pages_per_huge_page(h), &h_cg);
+	if (err < 0)
 		goto out_err;
 
 	if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
@@ -7337,14 +7344,17 @@ long hugetlb_reserve_pages(struct inode *inode,
 	 * reservations already in place (gbl_reserve).
 	 */
 	gbl_reserve = hugepage_subpool_get_pages(spool, chg);
-	if (gbl_reserve < 0)
+	if (gbl_reserve < 0) {
+		err = gbl_reserve;
 		goto out_uncharge_cgroup;
+	}
 
 	/*
 	 * Check enough hugepages are available for the reservation.
 	 * Hand the pages back to the subpool if there are not
 	 */
-	if (hugetlb_acct_memory(h, gbl_reserve) < 0)
+	err = hugetlb_acct_memory(h, gbl_reserve);
+	if (err < 0)
 		goto out_put_pages;
 
 	/*
@@ -7363,6 +7373,7 @@ long hugetlb_reserve_pages(struct inode *inode,
 
 		if (unlikely(add < 0)) {
 			hugetlb_acct_memory(h, -gbl_reserve);
+			err = -EINVAL;
 			goto out_put_pages;
 		} else if (unlikely(chg > add)) {
 			/*
@@ -7423,7 +7434,7 @@ long hugetlb_reserve_pages(struct inode *inode,
 		kref_put(&resv_map->refs, resv_map_release);
 		set_vma_resv_map(vma, NULL);
 	}
-	return chg < 0 ? chg : add < 0 ? add : -EINVAL;
+	return err;
 }
 
 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 16 lines · On Fri, 24 Oct 2025 10:42:40 +0100 Shameer Kolothum <[email protected]>…

The function hugetlb_reserve_pages() returns the number of pages added to the reservation map on success and a negative error code on failure (e.g. -EINVAL, -ENOMEM). However, in some error paths, it may return -1 directly.

For example, a failure at:

if (hugetlb_acct_memory(h, gbl_reserve) < 0)
goto out_put_pages;

results in returning -1 (since add = -1), which may be misinterpreted in userspace as -EPERM.

Fix this by explicitly capturing and propagating the return values from helper functions, and using -EINVAL for all other failure cases.

Quoted 8 lines
Fixes 986f5f2b4be3 ("mm/hugetlb: make hugetlb_reserve_pages() return nr of entries updated")

---
Addressed commenst from v1. Thanks!
https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/hugetlb.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

Hello Shameer, LGTM, thank you for addressing the comments! Feel free to add:

[...snip...]

mm/hugetlb.c long hugetlb_reserve_pages(struct inode *inode, · line 7325

Quoted 2 lines
mm/hugetlb.c +2 -0
+		/* region_chg() above can return -ENOMEM */
+		err = (chg == -ENOMEM) ? -ENOMEM : -EINVAL;

And this looks much more elegant than the version I wrote : -)

#2 lore raw

A gentle ping...It would be great if this can be picked up if there are no further comments.

Thanks,
Shameer

Quoted 21 lines

-----Original Message-----
From: Joshua Hahn <[email protected]>
Sent: 24 October 2025 15:51
To: Shameer Kolothum <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Jason Gunthorpe <[email protected]>; Nicolin Chen <[email protected]>; Nathan Chen <[email protected]>; Matt Ochs <[email protected]>
Subject: Re: [PATCH v2] mm/hugetlb: Fix incorrect error return from hugetlb_reserve_pages()

[...snip...]

mm/hugetlb.c long hugetlb_reserve_pages(struct inode *inode, · line 7325

Quoted 2 lines
mm/hugetlb.c +2 -0
+             /* region_chg() above can return -ENOMEM */
+             err = (chg == -ENOMEM) ? -ENOMEM : -EINVAL;

And this looks much more elegant than the version I wrote : -)

Reviewed-by on v2 1/1
#3 lore raw
In reply to Shameer Kolothum
Quoted 16 lines · Shameer Kolothum

The function hugetlb_reserve_pages() returns the number of pages added to the reservation map on success and a negative error code on failure (e.g. -EINVAL, -ENOMEM). However, in some error paths, it may return -1 directly.

For example, a failure at:

if (hugetlb_acct_memory(h, gbl_reserve) < 0)
goto out_put_pages;

results in returning -1 (since add = -1), which may be misinterpreted in userspace as -EPERM.

Fix this by explicitly capturing and propagating the return values from helper functions, and using -EINVAL for all other failure cases.

Quoted 8 lines
Fixes 986f5f2b4be3 ("mm/hugetlb: make hugetlb_reserve_pages() return nr of entries updated")

---
Addressed commenst from v1. Thanks!
https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/hugetlb.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

Jason