[Patch v7 18/24] perf/x86/intel: Fix draining of un-overflowed PEBS buffer in PMI handler

From: Dapeng Mi

Date: Tue Nov 17 2026 - 22:24:42 EST


When a regular counter which is read by a PEBS counter-event group
overflows, handle_pmi_common() drains the pending PEBS records to
ensure the stale counter snapshots are processed before handling the
counter's overflow. Currently this is done by invoking the
x86_pmu_drain_pebs() static call with a non-NULL regs argument.

This is incorrect. At this point the PEBS buffer has not reached its
interrupt threshold yet (a real PEBS buffer overflow would have
already been handled earlier), and the PMI being serviced belongs to
the regular counter, not to the PEBS events. With a non-NULL regs,
the drained records are processed with perf_event_overflow(), which
charges a PMI to the PEBS events via __perf_event_account_interrupt().
These spurious interrupt counts may falsely trigger the PMI throttling
and unexpectedly disable the PEBS events.

Call intel_pmu_drain_pebs_buffer() instead. It passes a NULL regs to
x86_pmu_drain_pebs(), so the records are emitted with
perf_event_output(), which skips the overflow handling and throttle
accounting.

Fixes: e02e9b0374c3 ("perf/x86/intel: Support PEBS counters snapshotting")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
arch/x86/events/intel/core.c | 2 +-
arch/x86/events/intel/ds.c | 8 ++++++++
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index aa7034c5ea41..67275a873dba 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -4045,7 +4045,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
* The PEBS buffer has to be drained before handling the A-PMI
*/
if (is_pebs_counter_event_group(event))
- static_call(x86_pmu_drain_pebs)(regs, &data);
+ intel_pmu_drain_pebs_buffer();

last_period = event->hw.last_period;

diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index 332754e1da4f..9fe84e4880d9 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -1262,6 +1262,14 @@ int intel_pmu_drain_bts_buffer(void)
return 1;
}

+/*
+ * Drain the pending PEBS buffer for the current CPU.
+ * This helper should only be used to drain a PEBS buffer which
+ * hasn't overflowed yet, e.g. on a context switch with large PEBS,
+ * because the NULL regs argument passed to x86_pmu_drain_pebs()
+ * makes the records be processed with perf_event_output() rather
+ * than perf_event_overflow().
+ */
void intel_pmu_drain_pebs_buffer(void)
{
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
--
2.34.1