From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: linux-mm@kvack.org
Cc: Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
Christoph Lameter <cl@gentwo.org>,
Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH] mm/percpu: Add a simple double-free check for per-CPU memory
Date: Thu, 4 Sep 2025 16:35:14 +0200 [thread overview]
Message-ID: <20250904143514.Yk6Ap-jy@linutronix.de> (raw)
The free path clears the allocation bits in pcpu_chunk::alloc_map. A
simple double free check would be to check if the bits, which are about
to be cleared, are already cleared.
Check if the bit is already cleared. Issue a warning and abort free in
that case.
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
I managed accidentally a double free recently. This would have noticed
it. It looks low overhead so there might be no need to hide it behind a
debug switch.
mm/percpu.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/mm/percpu.c b/mm/percpu.c
index d9cbaee92b605..a2abddd85294a 100644
--- a/mm/percpu.c
+++ b/mm/percpu.c
@@ -1276,7 +1276,7 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk, int alloc_bits,
static int pcpu_free_area(struct pcpu_chunk *chunk, int off)
{
struct pcpu_block_md *chunk_md = &chunk->chunk_md;
- int bit_off, bits, end, oslot, freed;
+ int bit_off, bits, end, oslot, freed, free_bit;
lockdep_assert_held(&pcpu_lock);
pcpu_stats_area_dealloc(chunk);
@@ -1289,6 +1289,11 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off)
end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk),
bit_off + 1);
bits = end - bit_off;
+
+ free_bit = find_next_bit(chunk->alloc_map, end, bit_off);
+ if (WARN(free_bit != bit_off, "Trying to free already free memory"))
+ return 0;
+
bitmap_clear(chunk->alloc_map, bit_off, bits);
freed = bits * PCPU_MIN_ALLOC_SIZE;
--
2.51.0
next reply other threads:[~2025-09-04 14:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-04 14:35 Sebastian Andrzej Siewior [this message]
2025-09-04 22:51 ` Andrew Morton
2025-09-05 17:14 ` Christoph Lameter (Ampere)
2025-09-04 23:49 ` Dennis Zhou
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=20250904143514.Yk6Ap-jy@linutronix.de \
--to=bigeasy@linutronix.de \
--cc=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=dennis@kernel.org \
--cc=linux-mm@kvack.org \
--cc=tj@kernel.org \
/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