hugetlb: fix subpool release race
Subsystems (2)
PATCH 1/1 hugetlb: fix subpool release race
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.
--- mm/hugetlb.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/hugetlb.c +4 -2
@@ -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);
Signature · 1 line
2.51.0
Hello Yichong, thank you for the patch!
Quoted 7 lines
unlock_or_release_subpool()dropsspool->lockbefore checking whether the subpool can be freed. However,subpool_is_free()reads fields that are updated underspool->lock, including count,used_hpagesandrsv_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.
I don't think that the race you are mentioning can be hit in practice (i.e. there is no user-visible change for this) since two threads cannot actually observe the freeable state at the same time.
subpool_is_free() does an early exit on spool->count so it will never reach the inside if block and have two threads try to decrement the spool->hstate and free the spool twice. I'm wondering if we need the Fixes tag in this case.
I do think that this is not obvious though, so having the subpool_is_free check inside the locked section makes sense to me too.
Have a great day!
Joshua
Quoted 32 lines
Make the free decision while still holding
spool->lock. Keep the actualhugetlb_acct_memory()andkfree()calls after dropping the lock.Fixes 90481622d757 ("hugepages: fix use after free bug in "quota" handling")Signed-off-by Yichong Chen <[email protected]>--- mm/hugetlb.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/hugetlb.c +4
-2@@ -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);Signature · 1 line
2.51.0
Andrew Morton
maintainerQuoted 19 lines · On Mon, 20 Jul 2026 07:31:29 -0700 Joshua Hahn <[email protected]> wrote:
Hello Yichong, thank you for the patch!
Quoted 7 lines
unlock_or_release_subpool()dropsspool->lockbefore checking whether the subpool can be freed. However,subpool_is_free()reads fields that are updated underspool->lock, including count,used_hpagesandrsv_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.I don't think that the race you are mentioning can be hit in practice (i.e. there is no user-visible change for this) since two threads cannot actually observe the freeable state at the same time.
subpool_is_free()does an early exit onspool->countso it will never reach the inside if block and have two threads try to decrement thespool->hstateand free the spool twice. I'm wondering if we need the Fixes tag in this case.
Quoted 4 lines
I do think that this is not obvious though, so having the
subpool_is_freecheck inside the locked section makes sense to me too.Reviewed-by Joshua Hahn <[email protected]>
Thanks. Yichong, can you please consider Joshua's observations and perhaps resend with a more accurate changelog?