On a hugetlbfs mount with both size= and a positive min_size=, a reservation that fails global accounting while another task releases pages on the same mount can leave the subpool short of its minimum reservations. On unmount the subpool then drops all min_hpages global reservations, more than it still holds, so HugePages_Rsvd underflows or another mapping silently loses its reservation. hugepage_subpool_get_pages() adds the whole request to spool->used_hpages before the caller has obtained global reservations or a folio. A concurrent hugepage_subpool_put_pages() compares this inflated used_hpages with min_hpages, skips refilling spool->rsv_hpages, and drops a global reservation instead. When the request then fails, hugetlb_reserve_pages() only subtracts the failed global portion from used_hpages, so the refill that should have happened is lost. When both max_hpages and a positive min_hpages are set, keep the portion of a request that still needs global backing in a new spool->pending_hpages counter until the caller commits or aborts it, as Karl Mehltretter suggested on the out_put_pages thread [1]. hugepage_subpool_commit_pages() moves it to used_hpages once global accounting or folio allocation succeeds; if a concurrent put refilled rsv_hpages meanwhile, commit trims rsv_hpages to what the minimum still needs and returns the excess for the caller to drop from the global pool. On failure, hugetlb_reserve_pages() drops the pending count without touching rsv_hpages, avoiding any positive global adjustment on rollback. A request that overlaps such a release now fails if the global pool has no free page, as if it had run before the release. Mounts without both max_hpages and a positive min_hpages leave pending_hpages unused and return 0 from hugepage_subpool_commit_pages() without taking spool->lock. hugetlb_fix_reserve_counts() commits its page as well; if hugetlb_acct_memory() fails there but commit trimmed a refilled reservation, one trimmed reservation covers the dangling reserve map entry. The max_hpages check, hugetlbfs_statfs(), and subpool_is_free() count both used_hpages and pending_hpages, while minimum accounting in __hugepage_subpool_put_pages() only considers committed pages. Fixes: 1d3f9bb4c8af ("mm/hugetlb: restore failed global reservations to subpool") Link: https://lore.kernel.org/20260927170103.2381-1-kmehltretter@gmail.com [1] Suggested-by: Karl Mehltretter Signed-off-by: Aveline Noir --- Applies on top of: https://lore.kernel.org/linux-mm/20261125-hugetlb-subpool-release-v1-1-7398c24d09b0@gmail.com/ That patch is a runtime prerequisite (it closes the free_huge_folio() reserve-restoration window that commit trimming can otherwise hit), so please do not backport this to stable without it. Karl's idea in [1] of keeping global requests provisional until commit or abort fits this race well, so I went with that approach here. Karl, would appreciate it if you could take a look :) Note for the overlapping subpool work: for size= + min_size > 0 mounts, in-flight global charges now live in pending_hpages until commit. The pre-existing alloc_hugetlb_folio() failure leak (now in pending_hpages for those mounts) is left to the separate alloc-failure fix. Tests passed in x86_64 QEMU with KASAN, PROVE_LOCKING, DEBUG_VM and DEBUG_ATOMIC_SLEEP. --- fs/hugetlbfs/inode.c | 3 +- include/linux/hugetlb.h | 4 ++- mm/hugetlb.c | 96 ++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 85 insertions(+), 18 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 4a92f31c88e1..a893ba76eab9 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -1138,7 +1138,8 @@ static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf) spin_lock_irq(&sbinfo->spool->lock); buf->f_blocks = sbinfo->spool->max_hpages; free_pages = sbinfo->spool->max_hpages - - sbinfo->spool->used_hpages; + - sbinfo->spool->used_hpages + - sbinfo->spool->pending_hpages; buf->f_bavail = buf->f_bfree = free_pages; spin_unlock_irq(&sbinfo->spool->lock); } diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 0d5414392d87..2fabc19e245a 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -39,8 +39,10 @@ struct hugepage_subpool { spinlock_t lock; long count; long max_hpages; /* Maximum huge pages or -1 if no maximum. */ - long used_hpages; /* Used count against maximum, includes */ + long used_hpages; /* Committed count against maximum, includes */ /* both allocated and reserved pages. */ + long pending_hpages; /* In-flight global requests not yet */ + /* committed to used_hpages. */ struct hstate *hstate; long min_hpages; /* Minimum huge pages or -1 if no minimum. */ long rsv_hpages; /* Pages reserved against global pool to */ diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 05c34ca44a15..a2140a0e7615 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -151,7 +151,7 @@ static inline bool subpool_is_free(struct hugepage_subpool *spool) if (spool->count) return false; if (spool->max_hpages != -1) - return spool->used_hpages == 0; + return !spool->used_hpages && !spool->pending_hpages; if (spool->min_hpages != -1) return spool->rsv_hpages == spool->min_hpages; @@ -219,7 +219,10 @@ void hugepage_put_subpool(struct hugepage_subpool *spool) * request. Otherwise, return the number of pages by which the * global pools must be adjusted (upward). The returned value may * only be different than the passed value (delta) in the case where - * a subpool minimum size must be maintained. + * a subpool minimum size must be maintained. When both max_hpages and a + * positive min_hpages are set, pages counted in a positive return value + * stay in pending_hpages until the caller commits them with + * hugepage_subpool_commit_pages() or drops them on failure. */ static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, long delta) @@ -231,13 +234,11 @@ static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, spin_lock_irq(&spool->lock); - if (spool->max_hpages != -1) { /* maximum size accounting */ - if ((spool->used_hpages + delta) <= spool->max_hpages) - spool->used_hpages += delta; - else { - ret = -ENOMEM; - goto unlock_ret; - } + if (spool->max_hpages != -1 && + (spool->used_hpages + spool->pending_hpages + delta) > + spool->max_hpages) { + ret = -ENOMEM; + goto unlock_ret; } /* minimum size accounting */ @@ -255,11 +256,52 @@ static long hugepage_subpool_get_pages(struct hugepage_subpool *spool, } } + if (spool->max_hpages != -1) { + if (spool->min_hpages > 0) { + spool->used_hpages += delta - ret; + spool->pending_hpages += ret; + } else { + spool->used_hpages += delta; + } + } + unlock_ret: spin_unlock_irq(&spool->lock); return ret; } +/* + * Commit pending pages once the global pool backs them. Return the number of + * subpool reservations that are no longer needed; the caller must drop them + * from the global pool. + */ +static long hugepage_subpool_commit_pages(struct hugepage_subpool *spool, + long delta) +{ + long ret = 0; + unsigned long flags; + + if (!spool || !delta || spool->max_hpages == -1 || + spool->min_hpages <= 0) + return 0; + + spin_lock_irqsave(&spool->lock, flags); + spool->pending_hpages -= delta; + spool->used_hpages += delta; + + if (spool->rsv_hpages) { + long needed = max(0L, spool->min_hpages - spool->used_hpages); + + if (spool->rsv_hpages > needed) { + ret = spool->rsv_hpages - needed; + spool->rsv_hpages = needed; + } + } + spin_unlock_irqrestore(&spool->lock, flags); + + return ret; +} + /* * Subpool accounting for freeing and unreserving pages. * Return the number of global page reservations that must be dropped. @@ -1038,8 +1080,17 @@ void hugetlb_fix_reserve_counts(struct inode *inode) if (rsv_adjust > 0) { struct hstate *h = hstate_inode(inode); - if (!hugetlb_acct_memory(h, 1)) + reserved = !hugetlb_acct_memory(h, 1); + rsv_adjust = hugepage_subpool_commit_pages(spool, 1); + if (!reserved && rsv_adjust) { + /* + * One trimmed subpool reservation covers this entry; + * drop any remaining excess from the global pool. + */ reserved = true; + rsv_adjust--; + } + hugetlb_acct_memory(h, -rsv_adjust); } else if (!rsv_adjust) { reserved = true; } @@ -3253,6 +3304,12 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, goto out_subpool_put; } + if (gbl_chg) { + long rsv_adjust = hugepage_subpool_commit_pages(spool, gbl_chg); + + hugetlb_acct_memory(h, -rsv_adjust); + } + hugetlb_set_folio_subpool(folio, spool); if (map_chg != MAP_CHG_ENFORCED) { @@ -6944,7 +7001,7 @@ long hugetlb_reserve_pages(struct inode *inode, struct vm_area_struct *vma, vma_flags_t vma_flags) { - long chg = -1, add = -1, spool_resv, gbl_resv; + long chg = -1, add = -1, spool_resv, gbl_resv, rsv_adjust; struct hstate *h = hstate_inode(inode); struct hugepage_subpool *spool = subpool_inode(inode); struct resv_map *resv_map; @@ -7058,7 +7115,10 @@ long hugetlb_reserve_pages(struct inode *inode, hugetlb_acct_memory(h, -gbl_reserve); err = add; goto out_put_pages; - } else if (unlikely(chg > add)) { + } + rsv_adjust = hugepage_subpool_commit_pages(spool, gbl_reserve); + hugetlb_acct_memory(h, -rsv_adjust); + if (unlikely(chg > add)) { /* * pages in this range were added to the reserve * map between region_chg and region_add. This @@ -7066,7 +7126,6 @@ long hugetlb_reserve_pages(struct inode *inode, * the subpool and reserve counts modified above * based on the difference. */ - long rsv_adjust; /* * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the @@ -7088,17 +7147,22 @@ long hugetlb_reserve_pages(struct inode *inode, */ hugetlb_cgroup_put_rsvd_cgroup(h_cg); } + } else { + rsv_adjust = hugepage_subpool_commit_pages(spool, gbl_reserve); + hugetlb_acct_memory(h, -rsv_adjust); } return chg; out_put_pages: spool_resv = chg - gbl_reserve; - /* Restore used_hpages for pages that failed global reservation */ - if (gbl_reserve && spool) { + /* Restore subpool accounting for pages that failed global reservation */ + if (gbl_reserve && spool && spool->max_hpages != -1) { unsigned long flags; spin_lock_irqsave(&spool->lock, flags); - if (spool->max_hpages != -1) + if (spool->min_hpages > 0) + spool->pending_hpages -= gbl_reserve; + else spool->used_hpages -= gbl_reserve; unlock_or_release_subpool(spool, flags); } --- base-commit: c8117bff684306f5c1f959d055cc2b24dd67b1eb change-id: 20261126-hugetlb-subpool-pending-290c9ca77fdd prerequisite-change-id: 20261125-hugetlb-subpool-release-ba8e9e7005dd:v1 prerequisite-patch-id: 4e904ec452de2249cd958c99e9801fdd211f2d20