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

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



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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-04 13:12 Vitaly Wool [this message]
2026-03-04 13:31 ` Harry Yoo
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=20260304131214.102588-1-vitaly.wool@konsulko.se \
    --to=vitaly.wool@konsulko.se \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=harry.yoo@oracle.com \
    --cc=igor.b@beldev.am \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@suse.cz \
    /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