linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Harry Yoo <harry.yoo@oracle.com>
To: Vitaly Wool <vitaly.wool@konsulko.se>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Vlastimil Babka <vbabka@suse.cz>,
	Christoph Lameter <cl@gentwo.org>,
	David Rientjes <rientjes@google.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	linux-mm@kvack.org, Igor Belousov <igor.b@beldev.am>
Subject: Re: [PATCH] mempool: fix the race condition in mempool_resize()
Date: Wed, 4 Mar 2026 22:31:41 +0900	[thread overview]
Message-ID: <aag0PRXyEN-ansub@hyeyoo> (raw)
In-Reply-To: <20260304131214.102588-1-vitaly.wool@konsulko.se>

On Wed, Mar 04, 2026 at 02:12:14PM +0100, Vitaly Wool wrote:
> From: Igor Belousov <igor.b@beldev.am>
> 
> mempool_resize() at some point has no valid elements array for a pool:
> ...
>     kfree(pool->elements);
>     /* here pool->elements is not valid */
>     pool->elements = new_elements;
> ...
> 
> If e. g. mempool_alloc() tries to access pool->elements after kfree()
> but before the assignment that follows, we end up with an undefined
> behavior. Fix that by changing pool->elements to new_elements first
> and then freeing up the old array.

Hi, is this from code inspection, or a real bug you observed?

I think pool->lock should prevent the bug you described from happening
and I don't think using xchg() is necessary when updating fields
protected by a spinlock.

-- 
Cheers,
Harry / Hyeonggon

> Signed-off-by: Igor Belousov <igor.b@beldev.am>
> Signed-off-by: Vitaly Wool <vitaly.wool@konsulko.se>
> ---
>  mm/mempool.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/mempool.c b/mm/mempool.c
> index db23e0eef652..302d83cbeac1 100644
> --- a/mm/mempool.c
> +++ b/mm/mempool.c
> @@ -384,8 +384,8 @@ int mempool_resize(struct mempool *pool, int new_min_nr)
>  	}
>  	memcpy(new_elements, pool->elements,
>  			pool->curr_nr * sizeof(*new_elements));
> -	kfree(pool->elements);
> -	pool->elements = new_elements;
> +	xchg(pool->elements, new_elements);
> +	kfree(new_elements);
>  	pool->min_nr = new_min_nr;
>  
>  	while (pool->curr_nr < pool->min_nr) {
> -- 
> 2.39.2
> 
> 


  reply	other threads:[~2026-03-04 13:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-04 13:12 Vitaly Wool
2026-03-04 13:31 ` Harry Yoo [this message]
2026-03-04 15:20   ` igor.b

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aag0PRXyEN-ansub@hyeyoo \
    --to=harry.yoo@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=igor.b@beldev.am \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@suse.cz \
    --cc=vitaly.wool@konsulko.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox