[PATCH v2] hugetlb: evaluate subpool free state while locked

Yichong Chen posted 1 patch 3 days, 21 hours ago
mm/hugetlb.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
[PATCH v2] hugetlb: evaluate subpool free state while locked
Posted by Yichong Chen 3 days, 21 hours ago
unlock_or_release_subpool() drops spool->lock before calling
subpool_is_free().  However, subpool_is_free() reads fields that are
updated under spool->lock, including count, used_hpages and rsv_hpages.

Keep the free-state evaluation under the same lock that protects those
fields.  The reservation accounting and kfree() calls still happen after
dropping spool->lock.

Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
---
v2:
- Reword the changelog based on Joshua's observation.
- Drop the Fixes tag because this is not known to cause a user-visible bug.
- Add Joshua's Reviewed-by.

 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
Re: [PATCH v2] hugetlb: evaluate subpool free state while locked
Posted by Andrew Morton 3 days ago
On Tue, 21 Jul 2026 11:52:07 +0800 Yichong Chen <chenyichong@uniontech.com> wrote:

> unlock_or_release_subpool() drops spool->lock before calling
> subpool_is_free().  However, subpool_is_free() reads fields that are
> updated under spool->lock, including count, used_hpages and rsv_hpages.
> 
> Keep the free-state evaluation under the same lock that protects those
> fields.  The reservation accounting and kfree() calls still happen after
> dropping spool->lock.
> 
> Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
> ---
> v2:
> - Reword the changelog based on Joshua's observation.
> - Drop the Fixes tag because this is not known to cause a user-visible bug.

Retaining the Fixes: would be OK.  It's potentially useful information.

If a patch doesn't fix a user-visible bug then we consider it
inappropriate to backport it (by including Cc:stable).  The -stable
maintainers have been asked not to backport Fixes: patches which lack
the cc:stable.

>
> ...
> --- 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);

OK, better, but the value of `free_subpool' can become out of date as
soon as we drop that lock - some other thread could get in and start
using *spool.  If that subpool is still findable, which it hopefully
isn't.
Re: [PATCH v2] hugetlb: evaluate subpool free state while locked
Posted by jane.chu@oracle.com 3 days, 6 hours ago
On 7/20/2026 8:52 PM, Yichong Chen wrote:
> unlock_or_release_subpool() drops spool->lock before calling 
> subpool_is_free(). However, subpool_is_free() reads fields that are 
> updated under spool->lock, including count, used_hpages and rsv_hpages. 
> Keep the free-state evaluation under
> 
> 
> unlock_or_release_subpool() drops spool->lock before calling
> subpool_is_free().  However, subpool_is_free() reads fields that are
> updated under spool->lock, including count, used_hpages and rsv_hpages.
> 
> Keep the free-state evaluation under the same lock that protects those
> fields.  The reservation accounting and kfree() calls still happen after
> dropping spool->lock.
> 
> Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
> Signed-off-by: Yichong Chen <chenyichong@uniontech.com>
> ---
> v2:
> - Reword the changelog based on Joshua's observation.
> - Drop the Fixes tag because this is not known to cause a user-visible bug.
> - Add Joshua's Reviewed-by.
> 
>   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
> 

Looks good to me.

I also looked at Sashiko's review

  
https://sashiko.dev/#/patchset/20260721035207.1437935-1-chenyichong%40uniontech.com

where it raised a pre-existing issue which, if I'm not mistaken, is not 
an issue?  Because, if spool->max_hpages is not established, 
spool->used_hpages doesn't apply, that is, the subpool tracking is not 
needed for limiting hpage allocation.  To add on that, if 
spool->min_hpages is also not established, subpool is practically not 
needed, and purely for the syntactical reason, when spool->count 
refcount drops to 0, it's okay to free the subpool.

Reviewed-by: Jane Chu <jane.chu@oracle.com>

-jane