Re: [Patch v7 04/24] perf/x86/intel: Limit branch snapshots to valid entries

From: bot+bpf-ci

Date: Tue Nov 17 2026 - 23:11:04 EST


> perf/x86/intel: Limit branch snapshots to valid entries

TL;DR: Resetting the shared lbr_stack.nr in intel_pmu_lbr_read() may corrupt
an in-flight host adaptive-PEBS branch-stack sample when KVM vLBR is active
and a BPF program calls bpf_get_branch_snapshot(); a later patch 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

[ ... ]

> @@ -1026,14 +1026,19 @@ void intel_pmu_lbr_read(void)
> {
> struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
>
> + if (!cpuc->lbr_users || vlbr_exclude_host()) {
> + /* Reset lbr_stack.nr to prevent stale data leakage. */
> + cpuc->lbr_stack.nr = 0;
> + return;
> + }
> +

Can this reset of cpuc->lbr_stack.nr corrupt a host adaptive-PEBS sample
that is still in flight?

Before this patch the vlbr_exclude_host() case returned without writing
anything. Now the BPF snapshot path writes to the same per-CPU struct
that a PMI-time sample may still point at:

intel_pmu_handle_irq()
handle_pmi_common() -> x86_pmu_drain_pebs
__intel_pmu_pebs_last_event()
setup_pebs_adaptive_sample_data()
intel_pmu_store_pebs_lbrs(lbr) /* cpuc->lbr_stack.nr = N */
intel_pmu_lbr_save_brstack()
perf_sample_save_brstack() /* data->dyn_size += N entries */
/* data->br_stack = &cpuc->lbr_stack */
perf_event_overflow()
__perf_event_overflow()
bpf_overflow_handler()
BPF prog -> bpf_get_branch_snapshot()
__intel_pmu_snapshot_branch_stack()
intel_pmu_lbr_read() /* vlbr_exclude_host(): nr = 0 */
overflow_handler() -> perf_output_sample()
perf_output_put(handle, data->br_stack->nr); /* now 0 */
perf_output_copy(handle, data->br_stack->entries, ...);

The record header was sized for N branch entries, but nr is now 0 and no
entries are written, so the PEBS LBR data for that sample is lost and the
tail of the record is never written.

This looks reachable when KVM's vLBR event is active (it sets
INTEL_PMC_IDX_FIXED_VLBR in intel_ctrl_exclude_host_mask) alongside a host
PEBS event using PERF_SAMPLE_BRANCH_STACK, with a perf_event BPF program
calling bpf_get_branch_snapshot(). Before this patch the same sequence
produced a consistent sample.

The !cpuc->lbr_users half of the condition does not look affected, since
any event whose sample references cpuc->lbr_stack is counted in lbr_users.

The series appears to fix this later in 61909a79de63 ("perf/x86/intel:
Use a dedicated LBR stack for BPF snapshotting"), where
intel_pmu_lbr_read() takes the stack to fill as a parameter. Is it worth
avoiding the shared-stack write in this patch, so that the intermediate
commit does not have this window?

> /*
> * Don't read when all LBRs users are using adaptive PEBS.
> *

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