linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: igor.b@beldev.am
To: Harry Yoo <harry.yoo@oracle.com>
Cc: Vitaly Wool <vitaly.wool@konsulko.se>,
	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
Subject: Re: [PATCH] mempool: fix the race condition in mempool_resize()
Date: Wed, 04 Mar 2026 19:20:30 +0400	[thread overview]
Message-ID: <98e1c65fec7c47e1ff77ac33d5604ef6@beldev.am> (raw)
In-Reply-To: <aag0PRXyEN-ansub@hyeyoo>

On 2026-03-04 17:31, Harry Yoo wrote:
> 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?

This is a real problem, not that easy to reproduce though.

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

Can you please explain how pool->lock protects pool->elements in e.g. 
remove_element() function?

Thanks,
Igor

> --
> 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 15:20 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
2026-03-04 15:20   ` igor.b [this message]

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=98e1c65fec7c47e1ff77ac33d5604ef6@beldev.am \
    --to=igor.b@beldev.am \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=harry.yoo@oracle.com \
    --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