Skip to contentSkip to conversation

mm/percpu: Add a simple double-free check for per-CPU memory

PATCH In review Sebastian Andrzej Siewior 4 participants 4 messages Sep 4, 2025 – Sep 5, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/percpu: Add a simple double-free check for per-CPU memory

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.

---

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(-)
mm/percpu.c +6 -1
index d9cbaee92b605..a2abddd85294a 100644
@@ -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;
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 4 lines · On Thu, 4 Sep 2025 16:35:14 +0200 Sebastian Andrzej Siewior…

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.

Nice idea. It's hardly a fast path!

#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Thu, 4 Sep 2025 16:35:14 +0200 Sebastian Andrzej Siewior…

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.

Nice idea. It's hardly a fast path!

And it could be improved by checking the bit instead of finding the next one.

maintainer
#3 lore raw
In reply to Sebastian Andrzej Siewior

Hello,

Quoted 4 lines · Sebastian Andrzej Siewior

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.

I thought about the double free issue in the past. It's a bit imperfect because the same pointer is handed out with no metadata for future allocations. Worse, the whole percpu chunk could go away too should you be so unlucky and then the whole path to get to the chunk goes awry.

Quoted 27 lines

Check if the bit is already cleared. Issue a warning and abort free in that case.

---

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(-)
mm/percpu.c +1 -1
index d9cbaee92b605..a2abddd85294a 100644
@@ -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);

mm/percpu.c static int pcpu_free_area(struct pcpu_chunk *chunk, int off) · line 1296

Quoted 8 lines
mm/percpu.c +5 -0
@@ -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;
+

We might want to test_bit(bit_off, chunk->bound_map) also to make sure we aren't accidentally doing partial frees if the region becomes say the second half of a larger allocation before the double free.

Possibly good to move the WARN to free_percpu() and exit early too for the other hooks rather than do all that with free size of 0.

Quoted 6 lines
mm/percpu.c +0 -0
 	bitmap_clear(chunk->alloc_map, bit_off, bits);
 
 	freed = bits * PCPU_MIN_ALLOC_SIZE;
Signature · 1 line
2.51.0

Thanks,
Dennis