[PATCH v3 14/15] perf: Identify the 0->1 transition for event::mmap_count

Peter Zijlstra posted 15 patches 1 month, 3 weeks ago
[PATCH v3 14/15] perf: Identify the 0->1 transition for event::mmap_count
Posted by Peter Zijlstra 1 month, 3 weeks ago
Needed because refcount_inc() doesn't allow the 0->1 transition.

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 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;
 }
Re: [PATCH v3 14/15] perf: Identify the 0->1 transition for event::mmap_count
Posted by Peter Zijlstra 1 month, 3 weeks ago
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.

> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  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;
>  }
> 
>
Re: [PATCH v3 14/15] perf: Identify the 0->1 transition for event::mmap_count
Posted by Lorenzo Stoakes 1 month, 3 weeks ago
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@linutronix.de>
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

LGTM so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> > ---
> >  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;
> >  }
> >
> >
[tip: perf/core] perf: Identify the 0->1 transition for event::mmap_count
Posted by tip-bot2 for Peter Zijlstra 1 month, 2 weeks ago
The following commit has been merged into the perf/core branch of tip:

Commit-ID:     59741451b49ce9964a9758c19d6f7df2a1255c75
Gitweb:        https://git.kernel.org/tip/59741451b49ce9964a9758c19d6f7df2a1255c75
Author:        Peter Zijlstra <peterz@infradead.org>
AuthorDate:    Tue, 12 Aug 2025 12:39:12 +02:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 15 Aug 2025 13:13:02 +02:00

perf: Identify the 0->1 transition for event::mmap_count

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.

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250812104019.956479989@infradead.org
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 41941df..f6211ab 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7034,7 +7034,7 @@ static int perf_mmap_rb(struct vm_area_struct *vma, struct perf_event *event,
 	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;
 }