percpu: add basic double free check
Subsystems (2)
Dennis Zhou
maintainerPATCH 1/1 percpu: add basic double free check
This adds a basic double free check by validating the first bit of the allocation in alloc_map and bound_map are set. If the alloc_map bit is not set, then this means the area is currently unallocated. If the bound_map bit is not set, then we are not freeing from the beginning of the allocation.
This is a respin of [1] adding the requested changes from me and Christoph.
[1] https://lore.kernel.org/linux-mm/[email protected]/
--- mm/percpu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
mm/percpu.c +14 -0
@@ -79,6 +79,7 @@#include <linux/mutex.h> #include <linux/percpu.h> #include <linux/pfn.h> +#include <linux/ratelimit.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/vmalloc.h>
@@ -1285,6 +1286,11 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off) bit_off = off /PCPU_MIN_ALLOC_SIZE ; +/* check double free */ +if (!test_bit (bit_off, chunk->alloc_map) || + !test_bit (bit_off, chunk->bound_map)) +return 0 ; +/* find end index */ end =find_next_bit (chunk->bound_map,pcpu_chunk_map_bits (chunk), bit_off +1 );
@@ -2225,6 +2231,7 @@ static void pcpu_balance_workfn(struct work_struct *work)*/ void free_percpu (void __percpu *ptr) { + static DEFINE_RATELIMIT_STATE(_rs, 60 * HZ, DEFAULT_RATELIMIT_BURST);void *addr;struct pcpu_chunk *chunk;unsigned long flags;
@@ -2242,6 +2249,13 @@ void free_percpu(void __percpu *ptr)spin_lock_irqsave (&pcpu_lock, flags); size =pcpu_free_area (chunk, off); +if (size ==0 ) { +spin_unlock_irqrestore (&pcpu_lock, flags); + +if (__ratelimit (&_rs)) +WARN (1 ,"percpu double free\n" ); +return ; + }pcpu_alloc_tag_free_hook (chunk, off, size);
Signature · 1 line
2.43.0
Quoted 8 lines · Dennis Zhou
This adds a basic double free check by validating the first bit of the allocation in
alloc_mapandbound_mapare set. If thealloc_mapbit is not set, then this means the area is currently unallocated. If thebound_mapbit is not set, then we are not freeing from the beginning of the allocation.This is a respin of [1] adding the requested changes from me and Christoph.
Thank you.
Quoted 2 lines
Sebastian
Quoted 5 lines · On Fri, 19 Dec 2025 16:27:37 -0800 Dennis Zhou <[email protected]> wrote:
This adds a basic double free check by validating the first bit of the allocation in
alloc_mapandbound_mapare set. If thealloc_mapbit is not set, then this means the area is currently unallocated. If thebound_mapbit is not set, then we are not freeing from the beginning of the allocation.
Hi Dennis,
I ran mm-new through the AI review prompts, and this one was flagged:
Quoted 3 lines
mm/percpu.c +0
-0
[ ... ]
mm/percpu.c static int pcpu_free_area(struct pcpu_chunk *chunk, int off) · line 1292
Quoted 8 lines
mm/percpu.c +4
-0@@ -1285,6 +1286,11 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off) bit_off = off /PCPU_MIN_ALLOC_SIZE ; +/* check double free */ +if (!test_bit (bit_off, chunk->alloc_map) || + !test_bit (bit_off, chunk->bound_map)) +return 0 ;
Does this early return corrupt pcpu_stats.nr_cur_alloc and chunk->nr_alloc?
Looking at pcpu_free_area(), earlier in the function (before this new check) there is a call to pcpu_stats_area_dealloc(chunk) which decrements these counters:
static int pcpu_free_area(struct pcpu_chunk *chunk, int off)
{
...
lockdep_assert_held(&pcpu_lock);
pcpu_stats_area_dealloc(chunk); <-- decrements counters here
...
bit_off = off / PCPU_MIN_ALLOC_SIZE;
/* check double free */
if (!test_bit(bit_off, chunk->alloc_map) ||
!test_bit(bit_off, chunk->bound_map))
return 0; <-- returns without actual free
...
}When a double-free is detected:
1. pcpu_stats_area_dealloc() runs, decrementing pcpu_stats.nr_cur_alloc
and chunk->nr_alloc
2. The check detects the double-free and returns 0
3. No actual memory is freed, but the counters are now wrong
Should pcpu_stats_area_dealloc() be moved after the double-free check, or should there be a compensating increment on the early return path?
This could also affect percpu_stats_show() in mm/percpu-stats.c, which uses chunk->nr_alloc via find_max_nr_alloc() to size a buffer:
buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));
If nr_alloc is underreported due to this bug, the buffer may be undersized for the actual number of allocations tracked in alloc_map, which chunk_map_stats() iterates based on the actual bitmap contents.
Andrew Morton
maintainerQuoted 11 lines · On Wed, 14 Jan 2026 04:22:00 -0800 Chris Mason <[email protected]> wrote:
Quoted 5 lines · On Fri, 19 Dec 2025 16:27:37 -0800 Dennis Zhou <[email protected]> wrote:
This adds a basic double free check by validating the first bit of the allocation in
alloc_mapandbound_mapare set. If thealloc_mapbit is not set, then this means the area is currently unallocated. If thebound_mapbit is not set, then we are not freeing from the beginning of the allocation.Hi Dennis,
I ran mm-new through the AI review prompts, and this one was flagged:
So good.
Quoted 3 lines
If
nr_allocis underreported due to this bug, the buffer may be undersized for the actual number of allocations tracked inalloc_map, whichchunk_map_stats()iterates based on the actual bitmap contents.
That's pretty serious, although unlikely to occur. I'll drop this version of the patch, thanks.
Dennis Zhou
maintainerHi Andrew,
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Wed, 14 Jan 2026 04:22:00 -0800 Chris Mason <[email protected]> wrote:
Quoted 5 lines · On Fri, 19 Dec 2025 16:27:37 -0800 Dennis Zhou <[email protected]> wrote:
This adds a basic double free check by validating the first bit of the allocation in
alloc_mapandbound_mapare set. If thealloc_mapbit is not set, then this means the area is currently unallocated. If thebound_mapbit is not set, then we are not freeing from the beginning of the allocation.Hi Dennis,
I ran mm-new through the AI review prompts, and this one was flagged:
Quoted 9 lines
So good.
Quoted 3 lines
If
nr_allocis underreported due to this bug, the buffer may be undersized for the actual number of allocations tracked inalloc_map, whichchunk_map_stats()iterates based on the actual bitmap contents.That's pretty serious, although unlikely to occur. I'll drop this version of the patch, thanks.
It's a good catch by AI, I fixed it in v2 [1].
My thoughts are this is really to serve developers. It's not really a recoverable error because if the pointer is re-allocated before the double free, then you'll only find out in the future when the new owner tries to free memory that it doesn't own. An even more contrived case is the pages get freed out from under it and then the new owner seg faults.
Thanks,
Dennis
Dennis Zhou
maintainerHi Chris,
Quoted 8 lines · Chris Mason
Quoted 5 lines · On Fri, 19 Dec 2025 16:27:37 -0800 Dennis Zhou <[email protected]> wrote:
This adds a basic double free check by validating the first bit of the allocation in
alloc_mapandbound_mapare set. If thealloc_mapbit is not set, then this means the area is currently unallocated. If thebound_mapbit is not set, then we are not freeing from the beginning of the allocation.
Quoted 4 lines
Hi Dennis,
I ran mm-new through the AI review prompts, and this one was flagged:
Wow that's cool, thanks!
Quoted 39 lines
Quoted 3 lines
mm/percpu.c +0
-0[ ... ]
mm/percpu.c static int pcpu_free_area(struct pcpu_chunk *chunk, int off) · line 1292
Quoted 8 lines
mm/percpu.c +4
-0@@ -1285,6 +1286,11 @@ static int pcpu_free_area(struct pcpu_chunk *chunk, int off) bit_off = off /PCPU_MIN_ALLOC_SIZE ; +/* check double free */ +if (!test_bit (bit_off, chunk->alloc_map) || + !test_bit (bit_off, chunk->bound_map)) +return 0 ;Does this early return corrupt pcpu_stats.nr_cur_alloc and
chunk->nr_alloc?Looking at
pcpu_free_area(), earlier in the function (before this new check) there is a call topcpu_stats_area_dealloc(chunk)which decrements these counters:static int pcpu_free_area(struct pcpu_chunk *chunk, int off) { ... lockdep_assert_held(&pcpu_lock); pcpu_stats_area_dealloc(chunk); <-- decrements counters here ... bit_off = off / PCPU_MIN_ALLOC_SIZE; /* check double free */ if (!test_bit(bit_off, chunk->alloc_map) || !test_bit(bit_off, chunk->bound_map)) return 0; <-- returns without actual free ... }When a double-free is detected:
Quoted 7 lines
1.
pcpu_stats_area_dealloc()runs, decrementing pcpu_stats.nr_cur_allocand chunk->nr_alloc2. The check detects the double-free and returns 0
3. No actual memory is freed, but the counters are now wrongShould
pcpu_stats_area_dealloc()be moved after the double-free check, or should there be a compensating increment on the early return path?
I moved pcpu_stats_area_dealloc() to the end of the function in v2.
Quoted 10 lines
This could also affect
percpu_stats_show()inmm/percpu-stats.c, which useschunk->nr_allocviafind_max_nr_alloc()to size a buffer:buffer = vmalloc_array(2 * max_nr_alloc + 1, sizeof(int));If
nr_allocis underreported due to this bug, the buffer may be undersized for the actual number of allocations tracked inalloc_map, whichchunk_map_stats()iterates based on the actual bitmap contents.