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

From: Masahiko, Yamada
Date: Thu Nov 17 2022 - 21:07:57 EST


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@xxxxxxxxxxx>
---
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