[1/1] perf: fix reset interface potential failure

Message ID 20221118020016.1571100-2-yamada.masahiko@fujitsu.com
State New
Headers
Series perf: fix reset interface potential failure |

Commit Message

Masahiko Yamada (Fujitsu) Nov. 18, 2022, 2 a.m. UTC
  There is a potential bug where PERF_EVENT_IOC_RESET
does not work when accessing PMU registers directly
from userspace in the perf_event interface.
we have created a patch on the kernel
that fixes a potential perf_event reset failure.

The motivation is to initialize pc->offset.
The perf_mmap__read_self function in tools/lib/perf/mmap.c is set by:.
cnt = READ_ONCE(pc->offset);
The pc->offset value is set in the following process
in the perf_event_update_userpage function:.
userpg->offset -= local64_read(&event->hw.prev_count);
hw->prev_count is set in the armpmu_event_set_period function
in drivers/perf/arm_pmu.c and in the x86_perf_event_set_period function
in arch/x86/events/core.c as follows:.
local64_set(&hwc->prev_count, (u64)-left);

Therefore, this patch was created to initialize hwc->prev_count
during reset processing.

Signed-off-by: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
---
 kernel/events/core.c | 5 +++++
 1 file changed, 5 insertions(+)
  

Comments

Itaru Kitayama Nov. 18, 2022, 5:11 a.m. UTC | #1
On Fri, Nov 18, 2022 at 11:00:16AM +0900, Masahiko wrote:
> There is a potential bug where PERF_EVENT_IOC_RESET
> does not work when accessing PMU registers directly
> from userspace in the perf_event interface.
> we have created a patch on the kernel
> that fixes a potential perf_event reset failure.
> 
> The motivation is to initialize pc->offset.
> The perf_mmap__read_self function in tools/lib/perf/mmap.c is set by:.
> cnt = READ_ONCE(pc->offset);
> The pc->offset value is set in the following process
> in the perf_event_update_userpage function:.
> userpg->offset -= local64_read(&event->hw.prev_count);
> hw->prev_count is set in the armpmu_event_set_period function
> in drivers/perf/arm_pmu.c and in the x86_perf_event_set_period function
> in arch/x86/events/core.c as follows:.
> local64_set(&hwc->prev_count, (u64)-left);
> 
> Therefore, this patch was created to initialize hwc->prev_count
> during reset processing.
> 
> Signed-off-by: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
> ---
>  kernel/events/core.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 4ec3717003d5..296549755a9c 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -5468,8 +5468,13 @@ static __poll_t perf_poll(struct file *file, poll_table *wait)
>  
>  static void _perf_event_reset(struct perf_event *event)
>  {
> +	struct hw_perf_event *hwc = &event->hw;
> +	s64 left;
> +
>  	(void)perf_event_read(event, false);
>  	local64_set(&event->count, 0);
> +	left = local64_read(&hwc->period_left);
> +	local64_set(&hwc->prev_count, (u64)-left);
>  	perf_event_update_userpage(event);
>  }
>  
> -- 
> 2.27.0

Reviewed-by: Itaru Kitayama <itaru.kitayama@fujitsu.com>
  
Masahiko Yamada (Fujitsu) Nov. 18, 2022, 6:59 a.m. UTC | #2
Itaru san

> -----Original Message-----
> From: Itaru Kitayama <itaru.kitayama@fujitsu.com>
> Sent: Friday, November 18, 2022 2:12 PM
> To: Yamada, Masahiko/山田 雅彦 <yamada.masahiko@fujitsu.com>
> Cc: acme@kernel.org; alexander.shishkin@linux.intel.com; jolsa@kernel.org;
> linux-kernel@vger.kernel.org; linux-perf-users@vger.kernel.org;
> mark.rutland@arm.com; mingo@redhat.com; namhyung@kernel.org;
> peterz@infradead.org
> Subject: [PATCH 1/1] perf: fix reset interface potential failure
> 
> On Fri, Nov 18, 2022 at 11:00:16AM +0900, Masahiko wrote:
> > There is a potential bug where PERF_EVENT_IOC_RESET does not work
> when
> > accessing PMU registers directly from userspace in the perf_event
> > interface.
> > we have created a patch on the kernel
> > that fixes a potential perf_event reset failure.
> >
> > The motivation is to initialize pc->offset.
> > The perf_mmap__read_self function in tools/lib/perf/mmap.c is set by:.
> > cnt = READ_ONCE(pc->offset);
> > The pc->offset value is set in the following process in the
> > perf_event_update_userpage function:.
> > userpg->offset -= local64_read(&event->hw.prev_count);
> > hw->prev_count is set in the armpmu_event_set_period function
> > in drivers/perf/arm_pmu.c and in the x86_perf_event_set_period
> > function in arch/x86/events/core.c as follows:.
> > local64_set(&hwc->prev_count, (u64)-left);
> >
> > Therefore, this patch was created to initialize hwc->prev_count during
> > reset processing.
> >
> > Signed-off-by: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
> > ---
> >  kernel/events/core.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/kernel/events/core.c b/kernel/events/core.c index
> > 4ec3717003d5..296549755a9c 100644
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -5468,8 +5468,13 @@ static __poll_t perf_poll(struct file *file,
> > poll_table *wait)
> >
> >  static void _perf_event_reset(struct perf_event *event)  {
> > +	struct hw_perf_event *hwc = &event->hw;
> > +	s64 left;
> > +
> >  	(void)perf_event_read(event, false);
> >  	local64_set(&event->count, 0);
> > +	left = local64_read(&hwc->period_left);
> > +	local64_set(&hwc->prev_count, (u64)-left);
> >  	perf_event_update_userpage(event);
> >  }
> >
> > --
> > 2.27.0
> 
> Reviewed-by: Itaru Kitayama <itaru.kitayama@fujitsu.com>

Thank you for your review.

Masahiko Yamada
  

Patch

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ec3717003d5..296549755a9c 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5468,8 +5468,13 @@  static __poll_t perf_poll(struct file *file, poll_table *wait)
 
 static void _perf_event_reset(struct perf_event *event)
 {
+	struct hw_perf_event *hwc = &event->hw;
+	s64 left;
+
 	(void)perf_event_read(event, false);
 	local64_set(&event->count, 0);
+	left = local64_read(&hwc->period_left);
+	local64_set(&hwc->prev_count, (u64)-left);
 	perf_event_update_userpage(event);
 }