[PATCH] hugetlb: fix subpool release race
From: Yichong Chen
Date: Mon Jul 20 2026 - 04:54:14 EST
unlock_or_release_subpool() drops spool->lock before checking whether the
subpool can be freed. However, subpool_is_free() reads fields that are
updated under spool->lock, including count, used_hpages and rsv_hpages.
Another thread can update those fields before the first thread evaluates
subpool_is_free(), allowing both threads to observe the final freeable
state and release the subpool.
Make the free decision while still holding spool->lock. Keep the actual
hugetlb_acct_memory() and kfree() calls after dropping the lock.
Fixes: 90481622d757 ("hugepages: fix use after free bug in "quota" handling")
Signed-off-by: Yichong Chen <chenyichong@xxxxxxxxxxxxx>
---
mm/hugetlb.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index e319c6a00555..46ab702c0fc0 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -140,12 +140,14 @@ static inline bool subpool_is_free(struct hugepage_subpool *spool)
static inline void unlock_or_release_subpool(struct hugepage_subpool *spool,
unsigned long irq_flags)
{
- spin_unlock_irqrestore(&spool->lock, irq_flags);
+ bool free_subpool = subpool_is_free(spool);
/* If no pages are used, and no other handles to the subpool
* remain, give up any reservations based on minimum size and
* free the subpool */
- if (subpool_is_free(spool)) {
+ spin_unlock_irqrestore(&spool->lock, irq_flags);
+
+ if (free_subpool) {
if (spool->min_hpages != -1)
hugetlb_acct_memory(spool->hstate,
-spool->min_hpages);
--
2.51.0