Re: [Patch v7 20/24] perf/x86/intel: Clear raw LBR counters in the LBR stack

From: bot+bpf-ci

Date: Tue Nov 17 2026 - 22:54:58 EST


> perf/x86/intel: Clear raw LBR counters in the LBR stack

TL;DR: Clearing raw LBR counters in the shared stack may zero them before a
later branch-counter event in the same PMI reads them, giving all-zero counter
samples until a later commit fixes it.

> diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
> --- a/arch/x86/events/intel/lbr.c
> +++ b/arch/x86/events/intel/lbr.c

[ ... ]

> @@ -1001,6 +1013,7 @@ void intel_pmu_lbr_save_brstack(struct perf_sample_data *data,
> return;
> }
>
> + intel_pmu_lbr_clear_raw_counters(cpu_lbr_stack(cpuc));
> perf_sample_save_brstack(data, event, cpu_lbr_stack(cpuc), NULL);
> }

Can this clear the counters before a later event in the same PMI has
consumed them?

handle_pmi_common() reads the LBR stack only once per PMI and reuses it for
every overflowing branch 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);
}

Suppose event A is an ordinary LBR event on counter 0 and event B is the
sampling event of a branch counters group on counter 1, and both overflow in
the same PMI. A is handled first, and intel_pmu_lbr_save_brstack() calls
intel_pmu_lbr_clear_raw_counters(), which zeroes entries[i].reserved in the
shared cpu_lbr_stack().

When B is handled, lbr_stack_live is still true so the stack is not re-read,
and intel_pmu_lbr_counters_reorder() reads reserved == 0 for every entry.
B's sample then reports all-zero branch counters.

Before this patch the non-counters path left reserved alone, so B got the
correct counters in this ordering.

The later commit 57fc5222dbd3 ("perf/x86/intel: Refresh LBR stack for LBR
counter events in PMI handler") addresses this case by re-reading the LBR
stack before a branch counters group event once an earlier LBR event has
cleared it. Until that commit, a bisect landing on this patch would see
wrong branch counter samples in this scenario.


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44231525404