Mostly just re-indent noise.
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
kernel/events/core.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7143,27 +7143,23 @@ static int perf_mmap(struct file *file,
if (vma_size != PAGE_SIZE * nr_pages)
return -EINVAL;
- mutex_lock(&event->mmap_mutex);
- ret = -EINVAL;
+ scoped_guard (mutex, &event->mmap_mutex) {
+ /*
+ * This relies on __pmu_detach_event() taking mmap_mutex after marking
+ * the event REVOKED. Either we observe the state, or __pmu_detach_event()
+ * will detach the rb created here.
+ */
+ if (event->state <= PERF_EVENT_STATE_REVOKED) {
+ ret = -ENODEV;
+ break;
+ }
- /*
- * This relies on __pmu_detach_event() taking mmap_mutex after marking
- * the event REVOKED. Either we observe the state, or __pmu_detach_event()
- * will detach the rb created here.
- */
- if (event->state <= PERF_EVENT_STATE_REVOKED) {
- ret = -ENODEV;
- goto unlock;
+ if (vma->vm_pgoff == 0)
+ ret = perf_mmap_rb(vma, event, nr_pages);
+ else
+ ret = perf_mmap_aux(vma, event, nr_pages);
}
- if (vma->vm_pgoff == 0)
- ret = perf_mmap_rb(vma, event, nr_pages);
- else
- ret = perf_mmap_aux(vma, event, nr_pages);
-
-unlock:
- mutex_unlock(&event->mmap_mutex);
-
if (ret)
return ret;
On Tue, Aug 12, 2025 at 12:39:11PM +0200, Peter Zijlstra wrote:
> Mostly just re-indent noise.
>
> 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 | 32 ++++++++++++++------------------
> 1 file changed, 14 insertions(+), 18 deletions(-)
>
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -7143,27 +7143,23 @@ static int perf_mmap(struct file *file,
> if (vma_size != PAGE_SIZE * nr_pages)
> return -EINVAL;
>
> - mutex_lock(&event->mmap_mutex);
Yeah getting rid of this lock goto stuff is great... mm is at risk of some
more Lorenzo churn to use this stuff over there... :)
> - ret = -EINVAL;
You see, I'm getting in a pattern here with comment on prior patch and you
reading my mind and fixing it on next ;)
> + scoped_guard (mutex, &event->mmap_mutex) {
> + /*
> + * This relies on __pmu_detach_event() taking mmap_mutex after marking
> + * the event REVOKED. Either we observe the state, or __pmu_detach_event()
> + * will detach the rb created here.
> + */
> + if (event->state <= PERF_EVENT_STATE_REVOKED) {
> + ret = -ENODEV;
> + break;
I don't absolutely love this break-for-what-is-not-obviously-a-for-loop
formulation (I know scoped_guard in practice _is_ a for loop, but obviously
that's hidden by macro), but I guess hey it's C, and we have to do what we
have to do :)
> + }
>
> - /*
> - * This relies on __pmu_detach_event() taking mmap_mutex after marking
> - * the event REVOKED. Either we observe the state, or __pmu_detach_event()
> - * will detach the rb created here.
> - */
> - if (event->state <= PERF_EVENT_STATE_REVOKED) {
> - ret = -ENODEV;
> - goto unlock;
> + if (vma->vm_pgoff == 0)
> + ret = perf_mmap_rb(vma, event, nr_pages);
> + else
> + ret = perf_mmap_aux(vma, event, nr_pages);
> }
>
> - if (vma->vm_pgoff == 0)
> - ret = perf_mmap_rb(vma, event, nr_pages);
> - else
> - ret = perf_mmap_aux(vma, event, nr_pages);
> -
> -unlock:
> - mutex_unlock(&event->mmap_mutex);
> -
> if (ret)
> return ret;
>
>
>
On Wed, Aug 13, 2025 at 07:42:41AM +0100, Lorenzo Stoakes wrote:
> > + scoped_guard (mutex, &event->mmap_mutex) {
> > + /*
> > + * This relies on __pmu_detach_event() taking mmap_mutex after marking
> > + * the event REVOKED. Either we observe the state, or __pmu_detach_event()
> > + * will detach the rb created here.
> > + */
> > + if (event->state <= PERF_EVENT_STATE_REVOKED) {
> > + ret = -ENODEV;
> > + break;
>
> I don't absolutely love this break-for-what-is-not-obviously-a-for-loop
> formulation (I know scoped_guard in practice _is_ a for loop, but obviously
> that's hidden by macro), but I guess hey it's C, and we have to do what we
> have to do :)
Right, don't love it either, but the alternative was a goto and that's
arguably worse, so meh.
> > + }
> >
> > + if (vma->vm_pgoff == 0)
> > + ret = perf_mmap_rb(vma, event, nr_pages);
> > + else
> > + ret = perf_mmap_aux(vma, event, nr_pages);
> > }
> >
> > if (ret)
> > return ret;
> >
> >
> >
On Wed, Aug 13, 2025 at 10:32:44AM +0200, Peter Zijlstra wrote:
> On Wed, Aug 13, 2025 at 07:42:41AM +0100, Lorenzo Stoakes wrote:
>
> > > + scoped_guard (mutex, &event->mmap_mutex) {
> > > + /*
> > > + * This relies on __pmu_detach_event() taking mmap_mutex after marking
> > > + * the event REVOKED. Either we observe the state, or __pmu_detach_event()
> > > + * will detach the rb created here.
> > > + */
> > > + if (event->state <= PERF_EVENT_STATE_REVOKED) {
> > > + ret = -ENODEV;
> > > + break;
> >
> > I don't absolutely love this break-for-what-is-not-obviously-a-for-loop
> > formulation (I know scoped_guard in practice _is_ a for loop, but obviously
> > that's hidden by macro), but I guess hey it's C, and we have to do what we
> > have to do :)
>
> Right, don't love it either, but the alternative was a goto and that's
> arguably worse, so meh.
>
> > > + }
> > >
> > > + if (vma->vm_pgoff == 0)
> > > + ret = perf_mmap_rb(vma, event, nr_pages);
> > > + else
> > > + ret = perf_mmap_aux(vma, event, nr_pages);
> > > }
> > >
> > > if (ret)
> > > return ret;
> > >
Nah, I'm an idiot.. How's this?
---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 20189a3354f2..4b82f8ed6b4e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7151,20 +7151,17 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
* the event REVOKED. Either we observe the state, or __pmu_detach_event()
* will detach the rb created here.
*/
- if (event->state <= PERF_EVENT_STATE_REVOKED) {
- ret = -ENODEV;
- break;
- }
+ if (event->state <= PERF_EVENT_STATE_REVOKED)
+ return -ENODEV;
if (vma->vm_pgoff == 0)
ret = perf_mmap_rb(vma, event, nr_pages);
else
ret = perf_mmap_aux(vma, event, nr_pages);
+ if (ret)
+ return ret;
}
- if (ret)
- return ret;
-
/*
* Since pinned accounting is per vm we cannot allow fork() to copy our
* vma.
On Wed, Aug 13, 2025 at 10:44:48AM +0200, Peter Zijlstra wrote:
> On Wed, Aug 13, 2025 at 10:32:44AM +0200, Peter Zijlstra wrote:
> > On Wed, Aug 13, 2025 at 07:42:41AM +0100, Lorenzo Stoakes wrote:
> >
> > > > + scoped_guard (mutex, &event->mmap_mutex) {
> > > > + /*
> > > > + * This relies on __pmu_detach_event() taking mmap_mutex after marking
> > > > + * the event REVOKED. Either we observe the state, or __pmu_detach_event()
> > > > + * will detach the rb created here.
> > > > + */
> > > > + if (event->state <= PERF_EVENT_STATE_REVOKED) {
> > > > + ret = -ENODEV;
> > > > + break;
> > >
> > > I don't absolutely love this break-for-what-is-not-obviously-a-for-loop
> > > formulation (I know scoped_guard in practice _is_ a for loop, but obviously
> > > that's hidden by macro), but I guess hey it's C, and we have to do what we
> > > have to do :)
> >
> > Right, don't love it either, but the alternative was a goto and that's
> > arguably worse, so meh.
> >
> > > > + }
> > > >
> > > > + if (vma->vm_pgoff == 0)
> > > > + ret = perf_mmap_rb(vma, event, nr_pages);
> > > > + else
> > > > + ret = perf_mmap_aux(vma, event, nr_pages);
> > > > }
> > > >
> > > > if (ret)
> > > > return ret;
> > > >
>
> Nah, I'm an idiot.. How's this?
Right yeah, you could just return instead of break :))
The following commit has been merged into the perf/core branch of tip:
Commit-ID: d23a6dbc0a71741eb7b141fdc04e31360fba46ef
Gitweb: https://git.kernel.org/tip/d23a6dbc0a71741eb7b141fdc04e31360fba46ef
Author: Peter Zijlstra <peterz@infradead.org>
AuthorDate: Tue, 12 Aug 2025 12:39:11 +02:00
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 15 Aug 2025 13:13:01 +02:00
perf: Use scoped_guard() for mmap_mutex in perf_mmap()
Mostly just re-indent noise.
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/r/20250812104019.838047976@infradead.org
---
kernel/events/core.c | 35 ++++++++++++++---------------------
1 file changed, 14 insertions(+), 21 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 3a5fd2b..41941df 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7146,30 +7146,23 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
if (vma_size != PAGE_SIZE * nr_pages)
return -EINVAL;
- mutex_lock(&event->mmap_mutex);
- ret = -EINVAL;
+ scoped_guard (mutex, &event->mmap_mutex) {
+ /*
+ * This relies on __pmu_detach_event() taking mmap_mutex after marking
+ * the event REVOKED. Either we observe the state, or __pmu_detach_event()
+ * will detach the rb created here.
+ */
+ if (event->state <= PERF_EVENT_STATE_REVOKED)
+ return -ENODEV;
- /*
- * This relies on __pmu_detach_event() taking mmap_mutex after marking
- * the event REVOKED. Either we observe the state, or __pmu_detach_event()
- * will detach the rb created here.
- */
- if (event->state <= PERF_EVENT_STATE_REVOKED) {
- ret = -ENODEV;
- goto unlock;
+ if (vma->vm_pgoff == 0)
+ ret = perf_mmap_rb(vma, event, nr_pages);
+ else
+ ret = perf_mmap_aux(vma, event, nr_pages);
+ if (ret)
+ return ret;
}
- if (vma->vm_pgoff == 0)
- ret = perf_mmap_rb(vma, event, nr_pages);
- else
- ret = perf_mmap_aux(vma, event, nr_pages);
-
-unlock:
- mutex_unlock(&event->mmap_mutex);
-
- if (ret)
- return ret;
-
/*
* Since pinned accounting is per vm we cannot allow fork() to copy our
* vma.
© 2016 - 2026 Red Hat, Inc.