linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dennis Zhou <dennis@kernel.org>
To: Chris Mason <clm@meta.com>
Cc: Tejun Heo <tj@kernel.org>, Christoph Lameter <cl@linux.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Subject: Re: [PATCH] percpu: add basic double free check
Date: Thu, 15 Jan 2026 18:38:19 -0800	[thread overview]
Message-ID: <aWmkm3A9qavY9461@snowbird> (raw)
In-Reply-To: <20260114122209.1075584-1-clm@meta.com>

Hi Chris,

On Wed, Jan 14, 2026 at 04:22:00AM -0800, Chris Mason wrote:
> On Fri, 19 Dec 2025 16:27:37 -0800 Dennis Zhou <dennis@kernel.org> wrote:
> 
> > 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.
> 
> Hi Dennis,
> 
> I ran mm-new through the AI review prompts, and this one was flagged:
> 

Wow that's cool, thanks!

> > diff --git a/mm/percpu.c b/mm/percpu.c
> > --- a/mm/percpu.c
> > +++ b/mm/percpu.c
> 
> [ ... ]
> 
> > @@ -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?

I moved pcpu_stats_area_dealloc() to the end of the function in v2.

> 
> 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.
> 


      parent reply	other threads:[~2026-01-16  2:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-20  0:27 Dennis Zhou
2026-01-12 16:03 ` Sebastian Andrzej Siewior
2026-01-14 12:22 ` Chris Mason
2026-01-15 23:13   ` Andrew Morton
2026-01-16  2:56     ` Dennis Zhou
2026-01-16  2:38   ` Dennis Zhou [this message]

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=aWmkm3A9qavY9461@snowbird \
    --to=dennis@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=cl@linux.com \
    --cc=clm@meta.com \
    --cc=linux-kernel@vger.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