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

From: Mi, Dapeng

Date: Wed Nov 18 2026 - 01:48:28 EST



On 11/18/2026 11:54 AM, bot+bpf-ci@xxxxxxxxxx wrote:
>> 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.

Per my understanding, we'd better keep current patch order. Without this
patch, the LBR stack needs to be refreshed only when there are two and more
LBR counter events, so the patch 57fc5222dbd3 needs to refresh LBR stack
only for the cases that there are 2 and more LBR counter events, and we
have to add a new patch to relax the condition, the LBR stack should be
refreshed as long as there are 1 LBR counter event and other LBR events. 

It introduce unnecessary complexity. Thanks.


>
>
> ---
> 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