mm/hugetlb: fix subpool minimum reservation rollback
Subsystems (3)
PATCH 1/1 mm/hugetlb: fix subpool minimum reservation rollback
When a reservation request is partially covered by a subpool minimum and the remaining global reservation fails, the error path first calls hugepage_subpool_put_pages() for the subpool-backed portion. It removes the failed global portion from used_hpages only afterwards.
hugepage_subpool_put_pages() uses used_hpages to decide whether rsv_hpages should be restored. Since used_hpages still includes the global portion, it can remain at or above min_hpages and prevent that restoration. It then reports the subpool reservation as releasable, causing hugetlb_acct_memory() to incorrectly decrement h->resv_huge_pages.
This was reproduced with four 2 MB huge pages and a hugetlbfs mount with size=10M,min_size=8M. After a successful three-page reservation, a two-page reservation which needed one subpool page and one global page failed with -ENOMEM. HugePages_Rsvd incorrectly dropped from four to three even though the subpool minimum was still four pages.
Roll back the failed global portion from used_hpages first, so that hugepage_subpool_put_pages() evaluates the minimum reservation against the current usage and returns the correct global adjustment.
--- mm/hugetlb.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
mm/hugetlb.c +9 -9
@@ -6808,15 +6808,6 @@ long hugetlb_reserve_pages(struct inode *inode, out_put_pages: spool_resv = chg - gbl_reserve; -if (spool_resv) { -/* put sub pool's reservation back, chg - gbl_reserve */ - gbl_resv =hugepage_subpool_put_pages (spool, spool_resv); -/* -* subpool's reserved pages can not be put back due to race, -* return to hstate. -*/ -hugetlb_acct_memory (h, -gbl_resv); - }/* Restore used_hpages for pages that failed global reservation */ if (gbl_reserve && spool) {unsigned long flags;
@@ -6826,6 +6817,15 @@ long hugetlb_reserve_pages(struct inode *inode, spool->used_hpages -= gbl_reserve;unlock_or_release_subpool (spool, flags); } +if (spool_resv) { +/* put sub pool's reservation back, chg - gbl_reserve */ + gbl_resv =hugepage_subpool_put_pages (spool, spool_resv); +/* +* subpool's reserved pages can not be put back due to race, +* return to hstate. +*/ +hugetlb_acct_memory (h, -gbl_resv); + } out_uncharge_cgroup:hugetlb_cgroup_uncharge_cgroup_rsvd (hstate_index (h), chg *pages_per_huge_page (h), h_cg);
Signature · 1 line
2.39.5
Andrew Morton
maintainerQuoted 11 lines · On Mon, 7 Sep 2026 21:20:55 +0800 Jinmeng Zhou <[email protected]> wrote:
When a reservation request is partially covered by a subpool minimum and the remaining global reservation fails, the error path first calls
hugepage_subpool_put_pages()for the subpool-backed portion. It removes the failed global portion fromused_hpagesonly afterwards.
hugepage_subpool_put_pages()usesused_hpagesto decide whetherrsv_hpagesshould be restored. Sinceused_hpagesstill includes the global portion, it can remain at or abovemin_hpagesand prevent that restoration. It then reports the subpool reservation as releasable, causinghugetlb_acct_memory()to incorrectly decrementh->resv_huge_pages.
Quoted 5 lines
This was reproduced with four 2 MB huge pages and a hugetlbfs mount with size=10M,min_size=8M. After a successful three-page reservation, a two-page reservation which needed one subpool page and one global page failed with
-ENOMEM.HugePages_Rsvdincorrectly dropped from four to three even though the subpool minimum was still four pages.
Thanks for making a test case. Perhaps this one could be enshrined in a selftests/
Quoted 3 lines
Roll back the failed global portion from
used_hpagesfirst, so thathugepage_subpool_put_pages()evaluates the minimum reservation against the current usage and returns the correct global adjustment.
OK, I'll queue this for testing and review.
AI review might have found a couple of issues in the nearby code:
https://sashiko.dev/#/patchset/[email protected]
Andrew Morton
maintainerQuoted 17 lines · On Mon, 7 Sep 2026 21:20:55 +0800 Jinmeng Zhou <[email protected]> wrote:
When a reservation request is partially covered by a subpool minimum and the remaining global reservation fails, the error path first calls
hugepage_subpool_put_pages()for the subpool-backed portion. It removes the failed global portion fromused_hpagesonly afterwards.
hugepage_subpool_put_pages()usesused_hpagesto decide whetherrsv_hpagesshould be restored. Sinceused_hpagesstill includes the global portion, it can remain at or abovemin_hpagesand prevent that restoration. It then reports the subpool reservation as releasable, causinghugetlb_acct_memory()to incorrectly decrementh->resv_huge_pages.This was reproduced with four 2 MB huge pages and a hugetlbfs mount with size=10M,min_size=8M. After a successful three-page reservation, a two-page reservation which needed one subpool page and one global page failed with
-ENOMEM.HugePages_Rsvdincorrectly dropped from four to three even though the subpool minimum was still four pages.
Quoted 3 lines
Roll back the failed global portion from
used_hpagesfirst, so thathugepage_subpool_put_pages()evaluates the minimum reservation against the current usage and returns the correct global adjustment.
Please cen we get some review of this cc:stable bugfix?
Thanks.
Quoted 46 lines
Fixes a833a693a490 ("mm: hugetlb: fix incorrect fallback for subpool")Signed-off-by Jinmeng Zhou <[email protected]>--- mm/hugetlb.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)mm/hugetlb.c +9
-9@@ -6808,15 +6808,6 @@ long hugetlb_reserve_pages(struct inode *inode, out_put_pages: spool_resv = chg - gbl_reserve; -if (spool_resv) { -/* put sub pool's reservation back, chg - gbl_reserve */ - gbl_resv =hugepage_subpool_put_pages (spool, spool_resv); -/* -* subpool's reserved pages can not be put back due to race, -* return to hstate. -*/ -hugetlb_acct_memory (h, -gbl_resv); - }/* Restore used_hpages for pages that failed global reservation */ if (gbl_reserve && spool) {unsigned long flags;@@ -6826,6 +6817,15 @@ long hugetlb_reserve_pages(struct inode *inode, spool->used_hpages -= gbl_reserve;unlock_or_release_subpool (spool, flags); } +if (spool_resv) { +/* put sub pool's reservation back, chg - gbl_reserve */ + gbl_resv =hugepage_subpool_put_pages (spool, spool_resv); +/* +* subpool's reserved pages can not be put back due to race, +* return to hstate. +*/ +hugetlb_acct_memory (h, -gbl_resv); + } out_uncharge_cgroup:hugetlb_cgroup_uncharge_cgroup_rsvd (hstate_index (h), chg *pages_per_huge_page (h), h_cg);Signature · 1 line
2.39.5
Quoted 21 lines · Jinmeng Zhou
When a reservation request is partially covered by a subpool minimum and the remaining global reservation fails, the error path first calls
hugepage_subpool_put_pages()for the subpool-backed portion. It removes the failed global portion fromused_hpagesonly afterwards.
hugepage_subpool_put_pages()usesused_hpagesto decide whetherrsv_hpagesshould be restored. Sinceused_hpagesstill includes the global portion, it can remain at or abovemin_hpagesand prevent that restoration. It then reports the subpool reservation as releasable, causinghugetlb_acct_memory()to incorrectly decrementh->resv_huge_pages.This was reproduced with four 2 MB huge pages and a hugetlbfs mount with size=10M,min_size=8M. After a successful three-page reservation, a two-page reservation which needed one subpool page and one global page failed with
-ENOMEM.HugePages_Rsvdincorrectly dropped from four to three even though the subpool minimum was still four pages.
Quoted 7 lines
Roll back the failed global portion from
used_hpagesfirst, so thathugepage_subpool_put_pages()evaluates the minimum reservation against the current usage and returns the correct global adjustment.Fixes a833a693a490 ("mm: hugetlb: fix incorrect fallback for subpool")Signed-off-by Jinmeng Zhou <[email protected]>
Thanks.
Quoted 54 lines · Jinmeng Zhou
When a reservation request is partially covered by a subpool minimum and the remaining global reservation fails, the error path first calls
hugepage_subpool_put_pages()for the subpool-backed portion. It removes the failed global portion fromused_hpagesonly afterwards.
hugepage_subpool_put_pages()usesused_hpagesto decide whetherrsv_hpagesshould be restored. Sinceused_hpagesstill includes the global portion, it can remain at or abovemin_hpagesand prevent that restoration. It then reports the subpool reservation as releasable, causinghugetlb_acct_memory()to incorrectly decrementh->resv_huge_pages.This was reproduced with four 2 MB huge pages and a hugetlbfs mount with size=10M,min_size=8M. After a successful three-page reservation, a two-page reservation which needed one subpool page and one global page failed with
-ENOMEM.HugePages_Rsvdincorrectly dropped from four to three even though the subpool minimum was still four pages.Roll back the failed global portion from
used_hpagesfirst, so thathugepage_subpool_put_pages()evaluates the minimum reservation against the current usage and returns the correct global adjustment.Fixes a833a693a490 ("mm: hugetlb: fix incorrect fallback for subpool")Signed-off-by Jinmeng Zhou <[email protected]>--- mm/hugetlb.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)mm/hugetlb.c +2
-9@@ -6808,15 +6808,6 @@ long hugetlb_reserve_pages(struct inode *inode, out_put_pages: spool_resv = chg - gbl_reserve; -if (spool_resv) { -/* put sub pool's reservation back, chg - gbl_reserve */ - gbl_resv =hugepage_subpool_put_pages (spool, spool_resv); -/* -* subpool's reserved pages can not be put back due to race, -* return to hstate. -*/ -hugetlb_acct_memory (h, -gbl_resv); - }/* Restore used_hpages for pages that failed global reservation */ if (gbl_reserve && spool) {unsigned long flags;@@ -6826,6 +6817,15 @@ long hugetlb_reserve_pages(struct inode *inode, spool->used_hpages -= gbl_reserve;unlock_or_release_subpool (spool, flags); } +if (spool_resv) { +/* put sub pool's reservation back, chg - gbl_reserve */
mm/hugetlb.c long hugetlb_reserve_pages(struct inode *inode, · line 6831
Quoted 10 lines
mm/hugetlb.c +7
-0+ gbl_resv =hugepage_subpool_put_pages (spool, spool_resv); +/* +* subpool's reserved pages can not be put back due to race, +* return to hstate. +*/ +hugetlb_acct_memory (h, -gbl_resv); + } out_uncharge_cgroup:hugetlb_cgroup_uncharge_cgroup_rsvd (hstate_index (h), chg *pages_per_huge_page (h), h_cg);
This fixes out_put_pages in hugetlb_reserve_pages() on a mount that specifies both min_size and max_size, and does not introduce any further bugs AFAICT.
Further details at [1].
[1] https://lore.kernel.org/all/CAEvNRgHQLifmcD1NO6cd7EVcCiQb6gJgNUo4HkFhZAvzuwMq0w@mail.gmail.com/