perf/x86: Check if cpuc->events[*] pointer exists before accessing it
Subsystems (2)
PATCH 1/1 perf/x86: Check if cpuc->events[*] pointer exists before accessing it
The PMI handler could disable some events as the interrupt throttling and clear the corresponding items in cpuc->events[] array.
perf_event_overflow()
-> __perf_event_overflow()
->__perf_event_account_interrupt()
-> perf_event_throttle_group()
-> perf_event_throttle()
-> event->pmu->stop()
-> x86_pmu_stop()Moreover PMI is NMI on x86 platform and it could interrupt other perf code like setup_pebs_adaptive_sample_data(). So once PMI handling finishes and returns into setup_pebs_adaptive_sample_data() and it could find the cpuc->events[*] becomes NULL and accessing this NULL pointer triggers an invalid memory access and leads to kernel crashes eventually.
Thus add NULL check before accessing cpuc->events[*] pointer.
Reported-by kernel test robot <[email protected]>
Fixes 9734e25fbf5a ("perf: Fix the throttle logic for a group")
Signed-off-by Dapeng Mi <[email protected]>
Tested-by kernel test robot <[email protected]>
--- arch/x86/events/core.c | 3 +++ arch/x86/events/intel/core.c | 6 +++++- arch/x86/events/intel/ds.c | 13 ++++++------- 3 files changed, 14 insertions(+), 8 deletions(-)
arch/x86/events/core.c +3 -0
@@ -1711,6 +1711,9 @@ int x86_pmu_handle_irq(struct pt_regs *regs)continue ; event = cpuc->events[idx]; +if (!event) +continue ; + last_period = event->hw.last_period; val =static_call (x86_pmu_update)(event);
arch/x86/events/intel/core.c +5 -1
@@ -2718,6 +2718,8 @@ static void update_saved_topdown_regs(struct perf_event *event, u64 slots,if (!is_topdown_idx (idx))continue ; other = cpuc->events[idx]; +if (!other) +continue ; other->hw.saved_slots = slots; other->hw.saved_metric = metrics; }
@@ -2761,6 +2763,8 @@ static u64 intel_update_topdown_event(struct perf_event *event, int metric_end,if (!is_topdown_idx (idx))continue ; other = cpuc->events[idx]; +if (!other) +continue ;__icl_update_topdown_event (other, slots, metrics, event ? event->hw.saved_slots :0 , event ? event->hw.saved_metric :0 );
@@ -3137,7 +3141,7 @@ static void x86_pmu_handle_guest_pebs(struct pt_regs *regs,for_each_set_bit (bit, (unsigned long *)&guest_pebs_idxs,X86_PMC_IDX_MAX ) { event = cpuc->events[bit]; -if (!event->attr.precise_ip) +if (!event || !event->attr.precise_ip)continue ;perf_sample_data_init (data,0 , event->hw.last_period);
arch/x86/events/intel/ds.c +6 -7
@@ -2480,6 +2480,8 @@ static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpuc, u64*/ for_each_set_bit (bit, (unsigned long *)&pebs_enabled,X86_PMC_IDX_MAX ) { event = cpuc->events[bit]; +if (!event) +continue ;if (event->hw.flags &PERF_X86_EVENT_AUTO_RELOAD )intel_pmu_save_and_restart_reload (event,0 ); }
@@ -2579,10 +2581,7 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sample_dcontinue ; event = cpuc->events[bit]; -if (WARN_ON_ONCE (!event)) -continue ; - -if (WARN_ON_ONCE (!event->attr.precise_ip)) +if (!event ||WARN_ON_ONCE (!event->attr.precise_ip))continue ;/* log dropped samples number */
@@ -2645,9 +2644,7 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_d pebs_status = basic->applicable_counters & cpuc->pebs_enabled & mask;for_each_set_bit (bit, (unsigned long *)&pebs_status,X86_PMC_IDX_MAX ) { event = cpuc->events[bit]; - -if (WARN_ON_ONCE (!event) || -WARN_ON_ONCE (!event->attr.precise_ip)) +if (!event ||WARN_ON_ONCE (!event->attr.precise_ip))continue ;if (counts[bit]++) {
@@ -2663,6 +2660,8 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sample_dcontinue ; event = cpuc->events[bit]; +if (!event) +continue ;__intel_pmu_pebs_last_event (event, iregs, regs, data, last[bit], counts[bit], setup_pebs_adaptive_sample_data);
base-commit: 829f5a6308ce11c3edaa31498a825f8c41b9e9aa
Signature · 1 line
2.34.1