[Patch v7 21/24] perf/x86/intel: Refresh LBR stack for LBR counter events in PMI handler

From: Dapeng Mi

Date: Tue Nov 17 2026 - 22:25:30 EST


When handling an LBR counter event, the raw LBR counters in the reserved
field of each LBR entry are cleared after the stack is processed and
reported to userspace. This prevents raw counter data from leaking.

However, if multiple events overflow in the same PMI, an LBR counter
event may not be the first one processed. In that case, the raw LBR
counters may already have been cleared by the previous LBR event, so the
later LBR counter event cannot read the correct values.

Refresh the LBR stack before processing an LBR counter event when
multiple events are handled in a single PMI. This restores the correct
raw LBR counter data for the event in this corner case.

This situation is rare, so the extra refresh should have negligible
performance impact.

Fixes: 33744916196b ("perf/x86/intel: Support branch counters logging")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
arch/x86/events/intel/core.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index fc672bc69ae3..b4632b791e26 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3906,8 +3906,9 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
{
struct perf_sample_data data;
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
- int bit;
+ bool lbr_counter_cleared = false;
int handled = 0;
+ int bit;

inc_perf_irq_stat();

@@ -4080,12 +4081,19 @@ 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 (!cpuc->lbr_stack_live) {
+ /*
+ * Re-read the LBR stack if it no longer holds the live state,
+ * or if an earlier LBR event already cleared the raw counters
+ * that this event needs.
+ */
+ if (!cpuc->lbr_stack_live ||
+ (lbr_counter_cleared && is_branch_counters_group(event))) {
intel_pmu_lbr_read(cpu_lbr_stack(cpuc),
cpuc->lbr_counters);
cpuc->lbr_stack_live = true;
}
intel_pmu_lbr_save_brstack(&data, cpuc, event);
+ lbr_counter_cleared = true;
}

x86_pmu_clear_perf_regs(regs);
--
2.34.1