[Patch v7 02/24] perf/x86/intel: Validate LBR XSAVE state before updating LBR stack

From: Dapeng Mi

Date: Tue Nov 17 2026 - 22:23:33 EST


intel_pmu_arch_lbr_read_xsave() did not verify whether LBR data was
actually present in the XSAVE area. When the LBR state was in the INIT
state, the hardware might not have written valid data to the XSAVE
buffer, allowing stale or incomplete data to be cached.

Add an explicit presence check for the LBR XSAVE state before updating
the cached LBR stack. Also clear the cached LBR stack in
intel_pmu_lbr_reset() to prevent stale entries from being reused across
context switches and similar transitions.

Fixes: c085fb877467 ("perf/x86/intel/lbr: Support XSAVES for arch LBR read")
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
arch/x86/events/intel/lbr.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c
index 0b446d8912e4..3d41385c4924 100644
--- a/arch/x86/events/intel/lbr.c
+++ b/arch/x86/events/intel/lbr.c
@@ -198,6 +198,7 @@ void intel_pmu_lbr_reset(void)

x86_pmu.lbr_reset();

+ cpuc->lbr_stack.nr = 0;
cpuc->last_task_ctx = NULL;
cpuc->last_log_id = 0;
if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) && cpuc->lbr_select)
@@ -1010,9 +1011,15 @@ static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
intel_pmu_store_lbr(cpuc, NULL);
return;
}
+ xsave->header.xfeatures = 0;
xsaves(&xsave->xsave, XFEATURE_MASK_LBR);

- intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
+ if (xsave->header.xfeatures & XFEATURE_MASK_LBR) {
+ intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
+ } else {
+ /* Clear the LBR stack to avoid leaking stale entries. */
+ cpuc->lbr_stack.nr = 0;
+ }
}

void intel_pmu_lbr_read(void)
--
2.34.1