* [PATCH] mempool: fix the race condition in mempool_resize()
@ 2026-03-04 13:12 Vitaly Wool
2026-03-04 13:31 ` Harry Yoo
0 siblings, 1 reply; 4+ messages in thread
From: Vitaly Wool @ 2026-03-04 13:12 UTC (permalink / raw)
To: Andrew Morton, Vlastimil Babka
Cc: Christoph Lameter, David Rientjes, Roman Gushchin, Harry Yoo,
linux-mm, Igor Belousov, Vitaly Wool
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.
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mempool: fix the race condition in mempool_resize()
2026-03-04 13:12 [PATCH] mempool: fix the race condition in mempool_resize() Vitaly Wool
@ 2026-03-04 13:31 ` Harry Yoo
2026-03-04 15:20 ` igor.b
0 siblings, 1 reply; 4+ messages in thread
From: Harry Yoo @ 2026-03-04 13:31 UTC (permalink / raw)
To: Vitaly Wool
Cc: Andrew Morton, Vlastimil Babka, Christoph Lameter,
David Rientjes, Roman Gushchin, linux-mm, Igor Belousov
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
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mempool: fix the race condition in mempool_resize()
2026-03-04 13:31 ` Harry Yoo
@ 2026-03-04 15:20 ` igor.b
2026-03-04 21:20 ` Andrew Morton
0 siblings, 1 reply; 4+ messages in thread
From: igor.b @ 2026-03-04 15:20 UTC (permalink / raw)
To: Harry Yoo
Cc: Vitaly Wool, Andrew Morton, Vlastimil Babka, Christoph Lameter,
David Rientjes, Roman Gushchin, linux-mm
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
>>
>>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mempool: fix the race condition in mempool_resize()
2026-03-04 15:20 ` igor.b
@ 2026-03-04 21:20 ` Andrew Morton
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-03-04 21:20 UTC (permalink / raw)
To: igor.b
Cc: Harry Yoo, Vitaly Wool, Vlastimil Babka, Christoph Lameter,
David Rientjes, Roman Gushchin, linux-mm
On Wed, 04 Mar 2026 19:20:30 +0400 igor.b@beldev.am wrote:
> > 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?
afaict all callers of add_element() and remove_element() hold
pool->lock. mempool_exit() doesn't but presumably there are no
concurrent accessers at this time.
So yes, perhaps a more complete problem description is needed here.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-03-04 21:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-04 13:12 [PATCH] mempool: fix the race condition in mempool_resize() Vitaly Wool
2026-03-04 13:31 ` Harry Yoo
2026-03-04 15:20 ` igor.b
2026-03-04 21:20 ` Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox