Skip to content

Commit 7e772a9

Browse files
Dapeng MiPeter Zijlstra
authored andcommitted
perf/x86: Fix NULL event access and potential PEBS record loss
When intel_pmu_drain_pebs_icl() is called to drain PEBS records, the perf_event_overflow() could be called to process the last PEBS record. While perf_event_overflow() could trigger the interrupt throttle and stop all events of the group, like what the below call-chain shows. perf_event_overflow() -> __perf_event_overflow() ->__perf_event_account_interrupt() -> perf_event_throttle_group() -> perf_event_throttle() -> event->pmu->stop() -> x86_pmu_stop() The side effect of stopping the events is that all corresponding event pointers in cpuc->events[] array are cleared to NULL. Assume there are two PEBS events (event a and event b) in a group. When intel_pmu_drain_pebs_icl() calls perf_event_overflow() to process the last PEBS record of PEBS event a, interrupt throttle is triggered and all pointers of event a and event b are cleared to NULL. Then intel_pmu_drain_pebs_icl() tries to process the last PEBS record of event b and encounters NULL pointer access. To avoid this issue, move cpuc->events[] clearing from x86_pmu_stop() to x86_pmu_del(). It's safe since cpuc->active_mask or cpuc->pebs_enabled is always checked before access the event pointer from cpuc->events[]. Closes: https://lore.kernel.org/oe-lkp/202507042103.a15d2923-lkp@intel.com Fixes: 9734e25 ("perf: Fix the throttle logic for a group") Reported-by: kernel test robot <oliver.sang@intel.com> Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://patch.msgid.link/20251029102136.61364-3-dapeng1.mi@linux.intel.com
1 parent c7f69dc commit 7e772a9

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

arch/x86/events/core.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ static void x86_pmu_enable(struct pmu *pmu)
13441344
hwc->state |= PERF_HES_ARCH;
13451345

13461346
x86_pmu_stop(event, PERF_EF_UPDATE);
1347+
cpuc->events[hwc->idx] = NULL;
13471348
}
13481349

13491350
/*
@@ -1365,6 +1366,7 @@ static void x86_pmu_enable(struct pmu *pmu)
13651366
* if cpuc->enabled = 0, then no wrmsr as
13661367
* per x86_pmu_enable_event()
13671368
*/
1369+
cpuc->events[hwc->idx] = event;
13681370
x86_pmu_start(event, PERF_EF_RELOAD);
13691371
}
13701372
cpuc->n_added = 0;
@@ -1531,7 +1533,6 @@ static void x86_pmu_start(struct perf_event *event, int flags)
15311533

15321534
event->hw.state = 0;
15331535

1534-
cpuc->events[idx] = event;
15351536
__set_bit(idx, cpuc->active_mask);
15361537
static_call(x86_pmu_enable)(event);
15371538
perf_event_update_userpage(event);
@@ -1610,7 +1611,6 @@ void x86_pmu_stop(struct perf_event *event, int flags)
16101611
if (test_bit(hwc->idx, cpuc->active_mask)) {
16111612
static_call(x86_pmu_disable)(event);
16121613
__clear_bit(hwc->idx, cpuc->active_mask);
1613-
cpuc->events[hwc->idx] = NULL;
16141614
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
16151615
hwc->state |= PERF_HES_STOPPED;
16161616
}
@@ -1648,6 +1648,7 @@ static void x86_pmu_del(struct perf_event *event, int flags)
16481648
* Not a TXN, therefore cleanup properly.
16491649
*/
16501650
x86_pmu_stop(event, PERF_EF_UPDATE);
1651+
cpuc->events[event->hw.idx] = NULL;
16511652

16521653
for (i = 0; i < cpuc->n_events; i++) {
16531654
if (event == cpuc->event_list[i])

0 commit comments

Comments
 (0)