[Patch v7 17/24] perf/x86/intel: Refresh LBR stack for non-PEBS LBR events when needed
From: Dapeng Mi
Date: Tue Nov 17 2026 - 22:24:43 EST
PEBS and non-PEBS events share the same LBR stack in cpuc->__lbr_stack.
In the PMI handler, the non-PEBS path refreshes the LBR state before the
PEBS buffer is drained.
When PEBS and non-PEBS LBR events overflow in the same PMI, draining the
PEBS buffer can overwrite the shared LBR stack with the PEBS snapshot.
This leaves stale LBR data from the PEBS record in place, so userspace
can see the wrong branch stack instead of the live LBR state at PMI
entry.
Track whether the LBR stack still contains the live state from PMI entry
with a cpuc->lbr_stack_live flag. The flag starts out clear on each pass
of the PMI handler and is set once the live LBR state has been read.
Draining PEBS records with LBR data clears it again, so a non-PEBS LBR
event re-reads the stack only when it is needed. Since the LBR state is
frozen for the duration of PMI handling, it only needs to be read once
for multiple non-PEBS LBR events in the same PMI.
Fixes: c22497f5838c ("perf/x86/intel: Support adaptive PEBS v4")
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
arch/x86/events/intel/core.c | 18 ++++++++++++++----
arch/x86/events/intel/lbr.c | 1 +
arch/x86/events/perf_event.h | 1 +
3 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 5491cd4f4c6f..aa7034c5ea41 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -4054,8 +4054,14 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
perf_sample_data_init(&data, 0, last_period);
- if (has_branch_stack(event))
+ if (has_branch_stack(event)) {
+ if (!cpuc->lbr_stack_live) {
+ intel_pmu_lbr_read(cpu_lbr_stack(cpuc),
+ cpuc->lbr_counters);
+ cpuc->lbr_stack_live = true;
+ }
intel_pmu_lbr_save_brstack(&data, cpuc, event);
+ }
x86_pmu_clear_perf_regs(regs);
x86_pmu_update_perf_regs(event, &data, regs, false);
@@ -4111,12 +4117,16 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
loops = 0;
again:
+ cpuc->lbr_stack_live = false;
/*
- * No need to read the LBR stack when all LBR users are PEBS events,
- * since the PEBS handler updates it.
+ * If PEBS can't record LBRs, draining PEBS can't overwrite the
+ * LBR stack, so read it here at PMI entry. Otherwise, it is read
+ * after PEBS draining when needed.
*/
- if (cpuc->lbr_users != cpuc->lbr_pebs_users)
+ if (cpuc->lbr_users > 0 && !intel_pmu_has_pebs_lbr(cpuc->pmu)) {
intel_pmu_lbr_read(cpu_lbr_stack(cpuc), cpuc->lbr_counters);
+ cpuc->lbr_stack_live = true;
+ }
/*
* Keep the LBR state frozen throughout PMI handling so that the PMI
diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
index 3a282352b0d2..740d5a5ba4c3 100644
--- a/arch/x86/events/intel/lbr.c
+++ b/arch/x86/events/intel/lbr.c
@@ -1317,6 +1317,7 @@ void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr)
intel_pmu_store_lbr(lbr, cpu_lbr_stack(cpuc));
intel_pmu_lbr_filter(cpuc, cpu_lbr_stack(cpuc), cpuc->lbr_counters);
+ cpuc->lbr_stack_live = false;
}
/*
diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
index 8d64bfcdc82d..4e6cf5c1e865 100644
--- a/arch/x86/events/perf_event.h
+++ b/arch/x86/events/perf_event.h
@@ -525,6 +525,7 @@ struct cpu_hw_events {
/*
* Intel LBR bits
*/
+ bool lbr_stack_live; /* If LBR stack holds the live LBR state */
int lbr_users;
int lbr_pebs_users;
u64 __lbr_stack[LBR_STACK_SIZE / sizeof(u64)];
--
2.34.1