Re: [PATCH v3 14/15] perf: Identify the 0->1 transition for event::mmap_count
From: Lorenzo Stoakes
Date: Wed Aug 13 2025 - 02:54:26 EST
On Tue, Aug 12, 2025 at 01:15:22PM +0200, Peter Zijlstra wrote:
>
> ljs noted that this Changelog was a tad terse, and he had a pending
> question here.
>
> On Tue, Aug 12, 2025 at 12:39:12PM +0200, Peter Zijlstra wrote:
> > Needed because refcount_inc() doesn't allow the 0->1 transition.
>
> Specifically, this is the case where we've created the RB, this means
> there was no RB, and as such there could not have been an mmap.
> Additionally we hold mmap_mutex to serialize everything.
>
> This must be the first.
Thanks, this is great!
And really good to separate this out as a really critical step prepping for
refcount_t change.
If we're being nitty it might be worth mentioning that we plan to move to
refcount_t and that considers inc from 0 to be evidence of a UAF, hence the
absolute requirement for this.
However, since we're clearly stating that this is the first instance of
this mmap_count being incremented, it's moot, as it's simply clearer to do
this anyway.
>
> > Suggested-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
LGTM so:
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@xxxxxxxxxx>
> > ---
> > kernel/events/core.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -7033,7 +7033,7 @@ static int perf_mmap_rb(struct vm_area_s
> > perf_event_update_userpage(event);
> >
> > perf_mmap_account(vma, user_extra, extra);
> > - atomic_inc(&event->mmap_count);
> > + atomic_set(&event->mmap_count, 1);
> >
> > return 0;
> > }
> >
> >