* [Patch v2 01/16] perf/x86/intel: Reject BTS events when BTS is not active
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:36 ` [Patch v2 02/16] perf/x86/intel: Check if BTS is active before handling BTS interrupt Dapeng Mi
` (14 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi, Sashiko
The BTS buffer is allocated by alloc_bts_buffer() during
x86_reserve_hardware(). If both the BTS and PEBS buffer allocations
fail, the DS area is released and cpuc->ds is set to NULL, with
x86_pmu.bts_active staying 0.
Currently bts_event_init() doesn't check bts_active, so a BTS event can
be created successfully even though the BTS buffer is unusable. When
the event is started, bts_event_start() and bts_config_buffer()
unconditionally dereference the NULL cpuc->ds pointer, causing a kernel
oops.
Fix it by rejecting BTS event creation with -ENODEV when BTS is not
active.
Fixes: 8062382c8dbe ("perf/x86/intel/bts: Add BTS PMU driver")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-3e9e2ec1-5f5c-45ca-b48b-117978a3e841
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/bts.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/arch/x86/events/intel/bts.c b/arch/x86/events/intel/bts.c
index 5849392cf26d..d871799e4f79 100644
--- a/arch/x86/events/intel/bts.c
+++ b/arch/x86/events/intel/bts.c
@@ -586,6 +586,11 @@ static int bts_event_init(struct perf_event *event)
return ret;
}
+ if (!x86_pmu.bts_active) {
+ bts_event_destroy(event);
+ return -ENODEV;
+ }
+
event->destroy = bts_event_destroy;
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 02/16] perf/x86/intel: Check if BTS is active before handling BTS interrupt
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
2026-12-01 0:36 ` [Patch v2 01/16] perf/x86/intel: Reject BTS events when BTS is not active Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:36 ` [Patch v2 03/16] perf/x86/intel: Guard leader sibling walk on nr_siblings Dapeng Mi
` (13 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi, Sashiko
alloc_bts_buffer() may fail while the PEBS buffer allocation succeeds.
In that case cpu_hw_events.ds is non-NULL, but the BTS fields in the DS
area, e.g. bts_interrupt_threshold, are left zero-initialized.
With bts_interrupt_threshold being 0, intel_bts_interrupt() wrongly
treats the BTS buffer as ready to drain and returns 1 on every PMI even
though BTS is not in use. The PMI handler thus always reports one more
"handled" event, which defeats the NMI back-to-back detection in
default_do_nmi() and can swallow genuine unknown NMIs.
Fix it by checking bts_active before handling the BTS interrupt.
Fixes: 4d4c47412464 ("perf/x86/intel/bts: Fix BTS PMI detection")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-c2fdfad5-341c-48c0-87ab-af4d31d19aee
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/bts.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/x86/events/intel/bts.c b/arch/x86/events/intel/bts.c
index d871799e4f79..4bc54614978f 100644
--- a/arch/x86/events/intel/bts.c
+++ b/arch/x86/events/intel/bts.c
@@ -464,7 +464,7 @@ int intel_bts_interrupt(void)
s64 old_head;
int err = -ENOSPC, handled = 0;
- if (!bts_ctx)
+ if (!bts_ctx || !x86_pmu.bts_active)
return 0;
bts = this_cpu_ptr(bts_ctx);
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 03/16] perf/x86/intel: Guard leader sibling walk on nr_siblings
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
2026-12-01 0:36 ` [Patch v2 01/16] perf/x86/intel: Reject BTS events when BTS is not active Dapeng Mi
2026-12-01 0:36 ` [Patch v2 02/16] perf/x86/intel: Check if BTS is active before handling BTS interrupt Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:36 ` [Patch v2 04/16] perf/x86/intel: Reset active_fixed_ctrl_val on CPU teardown Dapeng Mi
` (12 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
The leader event's ctx mutex is only held before calling
pmu->event_init() for sibling events, not for a newly-created group
leader.
The branch counters walk in intel_pmu_hw_config() calls
for_each_sibling_event() on the leader itself. At that point,
leader->ctx is not yet initialized, so the lockdep assertion in
for_each_sibling_event() can trigger WARN_ON_ONCE(), and the access to
leader->ctx is unsafe.
This loop is unnecessary when the event is the group leader, because the
leader has no siblings yet. Guard the iteration with leader->nr_siblings
before calling for_each_sibling_event().
Fixes: 33744916196b ("perf/x86/intel: Support branch counters logging")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index e91b5fea5708..9fae7d83a6b4 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -5048,9 +5048,11 @@ static int intel_pmu_hw_config(struct perf_event *event)
return -EINVAL;
leader->hw.flags |= PERF_X86_EVENT_BRANCH_COUNTERS;
- for_each_sibling_event(sibling, leader) {
- if (intel_set_branch_counter_constr(sibling, &num))
- return -EINVAL;
+ if (leader->nr_siblings) {
+ for_each_sibling_event(sibling, leader) {
+ if (intel_set_branch_counter_constr(sibling, &num))
+ return -EINVAL;
+ }
}
/* event isn't installed as a sibling yet. */
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 04/16] perf/x86/intel: Reset active_fixed_ctrl_val on CPU teardown
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (2 preceding siblings ...)
2026-12-01 0:36 ` [Patch v2 03/16] perf/x86/intel: Guard leader sibling walk on nr_siblings Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:36 ` [Patch v2 05/16] perf/x86/intel: Reset active_pebs_data_cfg " Dapeng Mi
` (11 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
cpuc->active_fixed_ctrl_val tracks the value currently programmed into
MSR_CORE_PERF_FIXED_CTR_CTRL, and reprogramming is skipped when the new
value matches the cached one.
After a CPU offline/online cycle, MSR_CORE_PERF_FIXED_CTR_CTRL may return
to its reset state, but active_fixed_ctrl_val may still hold the
pre-offline value. If the next computed fixed_ctrl_val matches that stale
cache entry, the MSR update is incorrectly skipped, leaving
MSR_CORE_PERF_FIXED_CTR_CTRL uninitialized for fixed counters and
resulting in errors in fixed counter counting or sampling.
Clear active_fixed_ctrl_val and MSR_CORE_PERF_FIXED_CTR_CTRL during CPU
teardown so MSR_CORE_PERF_FIXED_CTR_CTRL is always reprogrammed when the
CPU comes back online.
Fixes: fae9ebde9696 ("perf/x86/intel: Optimize FIXED_CTR_CTRL access")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 9fae7d83a6b4..86521677d278 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -6590,8 +6590,25 @@ static void free_excl_cntrs(struct cpu_hw_events *cpuc)
cpuc->constraint_list = NULL;
}
+static void fini_fixed_cntrs_on_cpu(int cpu)
+{
+ struct cpu_hw_events *cpuc;
+
+ if (x86_pmu.version < 2)
+ return;
+
+ /*
+ * Clear active_fixed_ctrl_val so MSR_CORE_PERF_FIXED_CTR_CTRL
+ * can be reprogrammed after CPU online.
+ */
+ cpuc = &per_cpu(cpu_hw_events, cpu);
+ cpuc->active_fixed_ctrl_val = 0;
+ wrmsrq_on_cpu(cpu, MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
+}
+
static void intel_pmu_cpu_dying(int cpu)
{
+ fini_fixed_cntrs_on_cpu(cpu);
fini_debug_store_on_cpu(cpu);
fini_arch_pebs_on_cpu(cpu);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 05/16] perf/x86/intel: Reset active_pebs_data_cfg on CPU teardown
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (3 preceding siblings ...)
2026-12-01 0:36 ` [Patch v2 04/16] perf/x86/intel: Reset active_fixed_ctrl_val on CPU teardown Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:36 ` [Patch v2 06/16] perf/x86/intel: Reset cached acr_cfg_b[] and cfg_c_val[] " Dapeng Mi
` (10 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
cpuc->active_pebs_data_cfg tracks the PEBS data configuration currently
programmed into MSR_PEBS_DATA_CFG, and reprogramming is skipped when the
new value matches the cached one.
After a CPU offline/online cycle, MSR_PEBS_DATA_CFG may return to its
reset state, but active_pebs_data_cfg may still hold the pre-offline
value. If the next computed PEBS data configuration matches that stale
cache entry, the MSR update is incorrectly skipped, leaving
MSR_PEBS_DATA_CFG uninitialized for PEBS sampling and resulting in
incorrect PEBS records.
Clear active_pebs_data_cfg and MSR_PEBS_DATA_CFG during CPU teardown so
MSR_PEBS_DATA_CFG is always reprogrammed when the CPU comes back online.
Fixes: c22497f5838c ("perf/x86/intel: Support adaptive PEBS v4")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 86521677d278..d634cc2ddcf9 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -6606,9 +6606,26 @@ static void fini_fixed_cntrs_on_cpu(int cpu)
wrmsrq_on_cpu(cpu, MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
}
+static void fini_adaptive_pebs_on_cpu(int cpu)
+{
+ struct cpu_hw_events *cpuc;
+
+ if (!x86_pmu.ds_pebs || !x86_pmu.intel_cap.pebs_baseline)
+ return;
+
+ /*
+ * Clear active_pebs_data_cfg so MSR_PEBS_DATA_CFG can
+ * be reprogrammed after CPU online.
+ */
+ cpuc = &per_cpu(cpu_hw_events, cpu);
+ cpuc->active_pebs_data_cfg = 0;
+ wrmsrq_on_cpu(cpu, MSR_PEBS_DATA_CFG, 0);
+}
+
static void intel_pmu_cpu_dying(int cpu)
{
fini_fixed_cntrs_on_cpu(cpu);
+ fini_adaptive_pebs_on_cpu(cpu);
fini_debug_store_on_cpu(cpu);
fini_arch_pebs_on_cpu(cpu);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 06/16] perf/x86/intel: Reset cached acr_cfg_b[] and cfg_c_val[] on CPU teardown
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (4 preceding siblings ...)
2026-12-01 0:36 ` [Patch v2 05/16] perf/x86/intel: Reset active_pebs_data_cfg " Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:36 ` [Patch v2 07/16] perf/x86/intel: Pass correct PEBS counter mask to no-drain update path Dapeng Mi
` (9 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
The acr_cfg_b[] and cfg_c_val[] arrays cache the last values written to
the *_CFG_B and *_CFG_C MSRs, and reprogramming is skipped when a new
value matches the cached one.
After CPU hotplug, those MSRs may return to reset defaults, but the cached
values may still reflect the pre-offline state. This can incorrectly
skip MSR writes when events are enabled again, leaving the hardware with
reset MSR contents.
Clear both caches and corresponding *_CFG_B/*_CFG_C MSRs on CPU teardown,
so *_CFG_B and *_CFG_C are always reprogrammed after the CPU comes back
online.
Fixes: ec980e4facef ("perf/x86/intel: Support auto counter reload")
Fixes: 52448a0a7390 ("perf/x86/intel: Setup PEBS data configuration and enable legacy groups")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 41 ++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index d634cc2ddcf9..ee7ace7be3c6 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -6622,9 +6622,50 @@ static void fini_adaptive_pebs_on_cpu(int cpu)
wrmsrq_on_cpu(cpu, MSR_PEBS_DATA_CFG, 0);
}
+static void clear_pmu_ext_msrs_on_cpu(int cpu, u64 mask,
+ u32 gp_base, u32 fixed_base)
+{
+ u32 idx, msr;
+
+ for_each_set_bit(idx, (unsigned long *)&mask, X86_PMC_IDX_MAX) {
+ if (idx < INTEL_PMC_IDX_FIXED) {
+ msr = gp_base + x86_pmu.addr_offset(idx, false);
+ } else {
+ msr = fixed_base +
+ x86_pmu.addr_offset(idx - INTEL_PMC_IDX_FIXED, false);
+ }
+ wrmsrq_on_cpu(cpu, msr, 0);
+ }
+}
+
+static void fini_pmu_ext_msrs_on_cpu(int cpu)
+{
+ struct cpu_hw_events *cpuc;
+ u64 cfg_b_mask, cfg_c_mask;
+
+ if (x86_pmu.version < 6)
+ return;
+
+ cpuc = &per_cpu(cpu_hw_events, cpu);
+ cfg_b_mask = hybrid(cpuc->pmu, acr_cntr_mask64);
+ cfg_c_mask = cfg_b_mask | hybrid(cpuc->pmu, arch_pebs_cap).counters;
+
+ /*
+ * Clear cached acr_cfg_b[] and cfg_c_val[] so *_CFG_B and
+ * *_CFG_C MSRs can always be reprogrammed after CPU online.
+ */
+ memset(cpuc->acr_cfg_b, 0, sizeof(cpuc->acr_cfg_b));
+ memset(cpuc->cfg_c_val, 0, sizeof(cpuc->cfg_c_val));
+ clear_pmu_ext_msrs_on_cpu(cpu, cfg_b_mask, MSR_IA32_PMC_V6_GP0_CFG_B,
+ MSR_IA32_PMC_V6_FX0_CFG_B);
+ clear_pmu_ext_msrs_on_cpu(cpu, cfg_c_mask, MSR_IA32_PMC_V6_GP0_CFG_C,
+ MSR_IA32_PMC_V6_FX0_CFG_C);
+}
+
static void intel_pmu_cpu_dying(int cpu)
{
fini_fixed_cntrs_on_cpu(cpu);
+ fini_pmu_ext_msrs_on_cpu(cpu);
fini_adaptive_pebs_on_cpu(cpu);
fini_debug_store_on_cpu(cpu);
fini_arch_pebs_on_cpu(cpu);
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 07/16] perf/x86/intel: Pass correct PEBS counter mask to no-drain update path
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (5 preceding siblings ...)
2026-12-01 0:36 ` [Patch v2 06/16] perf/x86/intel: Reset cached acr_cfg_b[] and cfg_c_val[] " Dapeng Mi
@ 2026-12-01 0:36 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 08/16] perf/x86/intel: Limit PEBS counter iteration to valid array bounds Dapeng Mi
` (8 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:36 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
intel_pmu_drain_arch_pebs() calls intel_pmu_pebs_event_update_no_drain()
with X86_PMC_IDX_MAX, but that parameter is a counter bitmask, not a
max index value. As a result, PEBS event state is not updated/restored
correctly on the no-drain path.
Pass the PEBS counter mask to intel_pmu_pebs_event_update_no_drain().
Fixes: d21954c8a0ff ("perf/x86/intel: Process arch-PEBS records or record fragments")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/ds.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index f094c6c7518b..bc9a4ad7ec80 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -3358,8 +3358,9 @@ static int intel_pmu_drain_arch_pebs(struct pt_regs *iregs,
rdmsrq(MSR_IA32_PEBS_INDEX, index.whole);
+ mask = hybrid(cpuc->pmu, arch_pebs_cap).counters & cpuc->pebs_enabled;
if (unlikely(!index.wr)) {
- intel_pmu_pebs_event_update_no_drain(cpuc, X86_PMC_IDX_MAX);
+ intel_pmu_pebs_event_update_no_drain(cpuc, mask);
return 0;
}
@@ -3375,8 +3376,6 @@ static int intel_pmu_drain_arch_pebs(struct pt_regs *iregs,
index.thresh = ARCH_PEBS_THRESH_SINGLE;
wrmsrq(MSR_IA32_PEBS_INDEX, index.whole);
- mask = hybrid(cpuc->pmu, arch_pebs_cap).counters & cpuc->pebs_enabled;
-
if (!iregs)
iregs = &dummy_iregs;
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 08/16] perf/x86/intel: Limit PEBS counter iteration to valid array bounds
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (6 preceding siblings ...)
2026-12-01 0:36 ` [Patch v2 07/16] perf/x86/intel: Pass correct PEBS counter mask to no-drain update path Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 09/16] perf/x86/intel: Avoid calling perf_get_regs_user() for SIMD sampling Dapeng Mi
` (7 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
__intel_pmu_handle_{,last_}pebs_record() iterate counter indexes up to
X86_PMC_IDX_MAX (64), while counts[] and *last[] are sized only for
INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS (48) entries.
If pebs_status were ever to contain bits above the highest valid PEBS
counter index, the loop could access those arrays out of bounds.
Although that should not happen in practice, limit the iteration bound
to INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS to match the array
sizes and keep the code consistent.
Fixes: 722e42e45c2f ("perf/x86: Support counter mask")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/ds.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index bc9a4ad7ec80..aa799a022966 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -3239,7 +3239,8 @@ __intel_pmu_handle_pebs_record(struct pt_regs *iregs,
struct perf_event *event;
int bit;
- for_each_set_bit(bit, (unsigned long *)&pebs_status, X86_PMC_IDX_MAX) {
+ for_each_set_bit(bit, (unsigned long *)&pebs_status,
+ INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS) {
event = cpuc->events[bit];
if (WARN_ON_ONCE(!event) ||
@@ -3268,7 +3269,8 @@ __intel_pmu_handle_last_pebs_record(struct pt_regs *iregs,
bool handled = false;
int bit;
- for_each_set_bit(bit, (unsigned long *)&mask, X86_PMC_IDX_MAX) {
+ for_each_set_bit(bit, (unsigned long *)&mask,
+ INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS) {
if (!counts[bit])
continue;
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 09/16] perf/x86/intel: Avoid calling perf_get_regs_user() for SIMD sampling
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (7 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 08/16] perf/x86/intel: Limit PEBS counter iteration to valid array bounds Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 10/16] perf/x86/intel: Don't inject fake PEBS overflow for non-guest PEBS event Dapeng Mi
` (6 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi, Sashiko
The perf_get_regs_user() function should never be invoked for events
requiring SIMD register sampling. Doing so sets data->regs_user.regs to
either nmi_user_regs or task_pt_regs(current). Neither structure is
embedded within 'struct x86_perf_regs'. As a result, when container_of()
is subsequently invoked in perf_reg_value() and perf_simd_reg_value(), it
dereferences invalid register pointers, triggering out-of-bounds memory
accesses.
While most execution paths already call x86_pmu_update_perf_regs() to
bypass perf_get_regs_user() for SIMD register sampling, a few locations
in the PEBS error handling path fail to do so. Specifically, the
corrupted record case in __intel_pmu_pebs_last_event(), and the early
returns for a record without a basic group or with a zero size in the
PEBS sample setup helpers, do not call it.
Additionally, inside a guest VM configured with Architectural PerfMon v1
on an XSAVES-capable host, PERF_PMU_CAP_EXTENDED_REGS and
PERF_PMU_CAP_SIMD_REGS can be set, but x86_pmu_handle_irq() fails to
call x86_pmu_update_perf_regs().
Fix this by adding x86_pmu_update_perf_regs() to these overlooked paths,
preventing the risk of out-of-bounds memory accesses.
Fixes: edd9aec51213 ("perf/x86: Enable XMM register sampling for REGS_USER case")
Fixes: 68aca309e49c ("perf/x86/intel: Add sanity check for PEBS record/fragment size")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/core.c | 2 ++
arch/x86/events/intel/ds.c | 20 ++++++++++++++------
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index c3d6b1c60bb2..eb51d69396b0 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2195,7 +2195,9 @@ int x86_pmu_handle_irq(struct pt_regs *regs)
if (!static_call(x86_pmu_set_period)(event))
continue;
+ x86_pmu_clear_perf_regs(regs);
perf_sample_data_init(&data, 0, last_period);
+ x86_pmu_update_perf_regs(event, &data, regs, false);
perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL);
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index aa799a022966..8e13a322a922 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -2534,18 +2534,21 @@ static void setup_pebs_adaptive_sample_data(struct perf_event *event,
u64 format_group;
u16 retire;
- if (basic == NULL)
- return;
-
perf_regs = container_of(regs, struct x86_perf_regs, regs);
x86_pmu_clear_perf_regs(regs);
- format_group = basic->format_group;
-
__setup_perf_sample_data(event, iregs, data);
*regs = *iregs;
+ if (basic == NULL) {
+ /* Bypass perf_get_regs_user(). */
+ x86_pmu_update_perf_regs(event, data, regs, true);
+ return;
+ }
+
+ format_group = basic->format_group;
+
/* basic group */
retire = x86_pmu.flags & PMU_FL_RETIRE_LATENCY ?
basic->retire_latency : 0;
@@ -2665,8 +2668,11 @@ static void setup_arch_pebs_sample_data(struct perf_event *event,
again:
header = at;
- if (!header->size)
+ if (!header->size) {
+ /* Bypass perf_get_regs_user(). */
+ x86_pmu_update_perf_regs(event, data, regs, true);
return;
+ }
next_record = at + sizeof(struct arch_pebs_header);
if (header->basic) {
@@ -2952,6 +2958,8 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
memset(regs, 0, sizeof(*regs));
x86_pmu_clear_perf_regs(regs);
perf_sample_data_init(data, 0, event->hw.last_period);
+ /* Bypass perf_get_regs_user(). */
+ x86_pmu_update_perf_regs(event, data, regs, true);
} else {
setup_sample(event, iregs, at, data, regs);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 10/16] perf/x86/intel: Don't inject fake PEBS overflow for non-guest PEBS event
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (8 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 09/16] perf/x86/intel: Avoid calling perf_get_regs_user() for SIMD sampling Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 11/16] perf/x86/intel: Fix stale PEBS count without counter-group support Dapeng Mi
` (5 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi, Sashiko
The x86_pmu_handle_guest_pebs() function injects a fake overflow event
to notify KVM of guest PEBS overflows. However, the existing check to
determine if an event belongs to a KVM guest PEBS counter is insufficient.
If a host PEBS event with 'exclude_guest == 0' is active when a PMI
arrives while perf_guest_state() is active, it can be mistaken for a
guest PEBS event.
If such a host event samples extended or SIMD registers with
PERF_SAMPLE_REGS_USER, perf_prepare_sample() falls back to
perf_get_regs_user(). This points data->regs_user.regs at the per-CPU
nmi_user_regs, which is a bare 'struct pt_regs' and not embedded inside
a 'struct x86_perf_regs'. Subsequent calls to perf_reg_value() and
perf_simd_reg_value() apply container_of() to it, reading past the end
of nmi_user_regs and resulting in an out-of-bounds memory access.
Enhance the guest PEBS check by ensuring that the fake overflow injection
is skipped if the event's overflow handler is the default handler, or if
the event samples registers (via PERF_SAMPLE_REGS_USER or
PERF_SAMPLE_REGS_INTR). A genuine guest PEBS event uses a custom overflow
handler and never configures these register sample types.
Additionally, refine the logic for retrieving the guest PEBS indices and
introduce a NULL pointer check prior to accessing the guest PEBS event.
This excludes non-PEBS indices (such as PEBS_OUTPUT_PT) and guarantees
that only valid guest PEBS events are iterated.
Fixes: edd9aec51213 ("perf/x86: Enable XMM register sampling for REGS_USER case")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-4c1692d7-7f30-48a3-a8d6-481928159d11
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index ee7ace7be3c6..3762f4a7ca2f 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3788,20 +3788,40 @@ static int x86_pmu_handle_guest_pebs(struct pt_regs *regs,
struct perf_sample_data *data)
{
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
- u64 guest_pebs_idxs = cpuc->pebs_enabled & ~cpuc->intel_ctrl_exclude_guest_mask;
struct perf_event *event = NULL;
+ u64 pebs_idxs, guest_pebs_idxs;
int bit;
if (!unlikely(perf_guest_state()))
return 0;
+ pebs_idxs = hybrid(cpuc->pmu, intel_ctrl) &
+ cpuc->pebs_enabled & x86_pmu.pebs_capable;
+ guest_pebs_idxs = pebs_idxs &
+ ~cpuc->intel_ctrl_exclude_guest_mask &
+ cpuc->intel_ctrl_exclude_host_mask;
+
if (!x86_pmu.pebs_ept || !x86_pmu.pebs_active ||
!guest_pebs_idxs)
return 0;
for_each_set_bit(bit, (unsigned long *)&guest_pebs_idxs, X86_PMC_IDX_MAX) {
event = cpuc->events[bit];
- if (!event->attr.precise_ip)
+ if (!event || !event->attr.precise_ip)
+ continue;
+
+ if (is_default_overflow_handler(event))
+ continue;
+
+ /*
+ * Guest PEBS events never sample registers. However, a
+ * host PEBS event may be mistaken for a guest PEBS event
+ * here if its PMI arrives while perf_guest_state() is set.
+ *
+ * Skip the event if it samples registers.
+ */
+ if (event->attr.sample_type &
+ (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_REGS_INTR))
continue;
perf_sample_data_init(data, 0, event->hw.last_period);
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 11/16] perf/x86/intel: Fix stale PEBS count without counter-group support
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (9 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 10/16] perf/x86/intel: Don't inject fake PEBS overflow for non-guest PEBS event Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 12/16] perf/x86/intel: Check period setting return value for PEBS events Dapeng Mi
` (4 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
On platforms without PEBS counter-group support (for example, Sapphire
Rapids), PEBS group read samples can report mismatched counts between the
precise event and its sibling, e.g.,
$ perf record -e \
'{cpu/instructions,period=100000/pu,cpu/instructions/u}:S' -- sleep 1
"perf report -D" then shows invalid event counts,
1st PEBS record:
.... group nr 2
..... id 0000000000006189, value 0000000000000000, lost 0
..... id 0000000000006269, value 00000000000186c1, lost 0
2nd PEBS record:
.... group nr 2
..... id 0000000000006189, value 00000000000186a0, lost 0
..... id 0000000000006269, value 0000000000030d6b, lost 0
In PEBS records, the first event count can lag behind the second event by
about one sample period, even though both counts should be close or equal.
The root cause is ordering in __intel_pmu_pebs_last_event(): PEBS event
counts are updated after perf_event_output()/perf_event_overflow(). As a
result, perf_output_read_group() snapshots stale PEBS counts.
Move the PEBS count update before
perf_event_output()/perf_event_overflow(), matching the non-PEBS ordering
in handle_pmi_common(), so group reads report correct PEBS event counts.
Fixes: 3c00ed344cef ("perf/x86/intel/ds: Factor out functions for PEBS records processing")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/ds.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index 8e13a322a922..98825d333ccb 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -2964,22 +2964,6 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
setup_sample(event, iregs, at, data, regs);
}
- if (iregs == &dummy_iregs) {
- /*
- * The PEBS records may be drained in the non-overflow context,
- * e.g., large PEBS + context switch. Perf should treat the
- * last record the same as other PEBS records, and doesn't
- * invoke the generic overflow handler.
- */
- perf_event_output(event, data, regs);
- } else {
- /*
- * All but the last records are processed.
- * The last one is left to be able to call the overflow handler.
- */
- perf_event_overflow(event, data, regs);
- }
-
if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
if (is_pebs_counter_event_group(event)) {
if (corrupted) {
@@ -3019,6 +3003,22 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
else
intel_pmu_save_and_restart(event);
}
+
+ if (iregs == &dummy_iregs) {
+ /*
+ * The PEBS records may be drained in the non-overflow context,
+ * e.g., large PEBS + context switch. Perf should treat the
+ * last record the same as other PEBS records, and doesn't
+ * invoke the generic overflow handler.
+ */
+ perf_event_output(event, data, regs);
+ } else {
+ /*
+ * All but the last records are processed.
+ * The last one is left to be able to call the overflow handler.
+ */
+ perf_event_overflow(event, data, regs);
+ }
}
static DEFINE_PER_CPU(struct x86_perf_regs, x86_pebs_regs);
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 12/16] perf/x86/intel: Check period setting return value for PEBS events
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (10 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 11/16] perf/x86/intel: Fix stale PEBS count without counter-group support Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 13/16] perf/x86/intel: Initialize arch_pebs_index to 0 before writing MSR Dapeng Mi
` (3 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi, Sashiko
For a non-auto-reload PEBS event (typically used in sample frequency
mode), perf/core may calculate a target sample period that is much larger
than the maximum sample period supported by the PEBS counter.
Consequently, a PEBS counter overflow does not necessarily mean that the
overall sample period calculated by perf/core has fully elapsed.
Fix this by checking the return value of x86_perf_event_set_period() (or
intel_pmu_save_and_restart()). Only invoke perf_event_output() or
perf_event_overflow() when the returned value is 1, which indicates that
the full sample period has indeed elapsed, matching the logic already
used in handle_pmi_common().
Fixes: 3c00ed344cef ("perf/x86/intel/ds: Factor out functions for PEBS records processing")
Fixes: 7da9960b59fb ("perf/x86/intel/ds: Fix counter backwards of non-precise events counters-snapshotting")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/ds.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index 98825d333ccb..bc44f507f872 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -2951,6 +2951,7 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
setup_fn setup_sample)
{
struct hw_perf_event *hwc = &event->hw;
+ int ret = 0;
/* Skip parsing corrupted PEBS record. */
if (corrupted) {
@@ -2984,6 +2985,7 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
*/
intel_pmu_save_and_restart_reload(event, count);
}
+ ret = 1;
} else {
/*
* For a non-precise event, it's possible the
@@ -2997,13 +2999,19 @@ __intel_pmu_pebs_last_event(struct perf_event *event,
* Since the event value has been updated when processing the
* counters-snapshotting record, only needs to set the new
* period for the counter.
+ *
+ * A return value of 1 indicates the full sample period has
+ * indeed elapsed and the overflow handler should be invoked.
*/
if (is_pebs_counter_event_group(event) && !corrupted)
- static_call(x86_pmu_set_period)(event);
+ ret = static_call(x86_pmu_set_period)(event);
else
- intel_pmu_save_and_restart(event);
+ ret = intel_pmu_save_and_restart(event);
}
+ if (!ret)
+ return;
+
if (iregs == &dummy_iregs) {
/*
* The PEBS records may be drained in the non-overflow context,
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 13/16] perf/x86/intel: Initialize arch_pebs_index to 0 before writing MSR
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (11 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 12/16] perf/x86/intel: Check period setting return value for PEBS events Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 14/16] perf/x86/intel: Only program ACR reload value for events with config2 Dapeng Mi
` (2 subsequent siblings)
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
Only the individual fields of the local arch_pebs_index 'new' are set
before it is written to the IA32_PEBS_INDEX MSR, so its reserved bits
are left uninitialized. Writing non-zero reserved bits to the MSR
triggers a #GP.
Initialize 'new' to 0 before filling in its fields.
Fixes: 52448a0a7390 ("perf/x86/intel: Setup PEBS data configuration and enable legacy groups")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 3762f4a7ca2f..6d1038186197 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3528,6 +3528,7 @@ static void intel_pmu_enable_event_ext(struct perf_event *event)
ext |= ARCH_PEBS_CNTR_METRICS & cap.caps;
}
+ new.whole = 0;
if (cpuc->n_pebs == cpuc->n_large_pebs)
new.thresh = ARCH_PEBS_THRESH_MULTI;
else
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 14/16] perf/x86/intel: Only program ACR reload value for events with config2
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (12 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 13/16] perf/x86/intel: Initialize arch_pebs_index to 0 before writing MSR Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 15/16] perf/x86/intel: Reject ACR/arch-PEBS events with sample_period > 2^32 Dapeng Mi
2026-12-01 0:37 ` [Patch v2 16/16] perf/x86/intel: Fix incorrect count calculation for arch-PEBS events Dapeng Mi
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
An ACR (Auto-Counter Reload) event requires its auto-reload value to be
programmed into the matching *_CFG_C MSR. This configuration is supplied
by the user via the event attribute's 'config2' field.
Currently, intel_pmu_enable_event_ext() unconditionally programs the
*_CFG_C MSR reload value for any event residing in an ACR event group,
without validating if 'config2' is actually configured. However, a
counting event with a zero 'config2' field can still reside within an ACR
event group; such an event should not have a reload value written to the
*_CFG_C reload field.
Enhance this check by ensuring that the RELOAD bits are only set
and programmed when the event belongs to an ACR group and 'config2' is
non-zero. This maintains consistency with intel_pmu_enable_acr() and
prevents erroneously setting up the auto-reload configuration for events
that were not explicitly requested to auto-reload.
Fixes: 5c3cdc74af25 ("perf/x86/intel: Consolidate MSR_IA32_PERF_CFG_C tracking")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 6d1038186197..9906eaaef49c 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3476,7 +3476,7 @@ static void intel_pmu_enable_event_ext(struct perf_event *event)
struct hw_perf_event *hwc = &event->hw;
u64 ext = 0;
- if (is_acr_event_group(event))
+ if (is_acr_event_group(event) && event->attr.config2)
ext |= (-hwc->sample_period) & ARCH_PEBS_RELOAD;
if (event->attr.precise_ip) {
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 15/16] perf/x86/intel: Reject ACR/arch-PEBS events with sample_period > 2^32
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (13 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 14/16] perf/x86/intel: Only program ACR reload value for events with config2 Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
2026-12-01 0:37 ` [Patch v2 16/16] perf/x86/intel: Fix incorrect count calculation for arch-PEBS events Dapeng Mi
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi
Currently, the *_CFG_C MSR only features a 32-bit reload value field. If
the sample period of an ACR or auto-reload arch-PEBS event exceeds 2^32,
the intended 48-bit reload value (-hwc->sample_period) is truncated to
32 bits when programmed into *_CFG_C[31:0].
Upon counter overflow, the hardware unconditionally 1-extends these 32
bits to 48 bits. Because the upper 16 bits of a 48-bit two's complement
value for periods > 2^32 are not all 1s (for example, for a period of
2^32 + 1, -P is 0xfffe_ffff_ffff), the hardware's 1-extension corrupts
the reload value, causing the counter to reload with a bogus value
(such as -1).
Fix this by rejecting ACR and auto-reload arch-PEBS events with a sample
period > 2^32 in intel_pmu_hw_config(). Additionally, enforce a similar
period check in intel_pmu_check_period() to prevent invalid configurations.
Opportunistically enforce ACR event sample period validation: an ACR event
with 'config2' set must have a non-zero sample period.
Fixes: ec980e4facef ("perf/x86/intel: Support auto counter reload")
Fixes: 52448a0a7390 ("perf/x86/intel: Setup PEBS data configuration and enable legacy groups")
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/intel/core.c | 31 ++++++++++++++++++++++++++++---
1 file changed, 28 insertions(+), 3 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 9906eaaef49c..519ef122c1a3 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -4995,6 +4995,9 @@ static inline int intel_set_branch_counter_constr(struct perf_event *event,
return 0;
}
+/* The *_CFG_C reload field supports sample periods up to 2^32. */
+#define MAX_CFG_C_RELOAD_PERIOD BIT_ULL(32)
+
static int intel_pmu_hw_config(struct perf_event *event)
{
int ret = x86_pmu_hw_config(event);
@@ -5036,6 +5039,10 @@ static int intel_pmu_hw_config(struct perf_event *event)
pebs_cap.pdists : pebs_cap.counters;
if (cntr_mask != pebs_mask)
event->hw.dyn_constraint &= pebs_mask;
+
+ if ((event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) &&
+ event->attr.sample_period > MAX_CFG_C_RELOAD_PERIOD)
+ return -EINVAL;
}
}
@@ -5139,6 +5146,7 @@ static int intel_pmu_hw_config(struct perf_event *event)
bool has_sw_event = false;
int num = 0, idx = 0;
u64 cause_mask = 0;
+ u64 max_period;
/* Not support perf metrics */
if (is_metric_event(event))
@@ -5152,8 +5160,14 @@ static int intel_pmu_hw_config(struct perf_event *event)
if (event->attr.config2 && event->attr.precise_ip > 2)
return -EINVAL;
- /* The reload value cannot exceeds the max period */
- if (event->attr.sample_period > x86_pmu.max_period)
+ /*
+ * An ACR event must be a sampling event, and its sample period
+ * must fit in the *_CFG_C reload field.
+ */
+ max_period = min(x86_pmu.max_period, MAX_CFG_C_RELOAD_PERIOD);
+ if (event->attr.config2 &&
+ (!event->attr.sample_period ||
+ event->attr.sample_period > max_period))
return -EINVAL;
/*
* The counter-constraints of each event cannot be finalized
@@ -6727,7 +6741,18 @@ static void intel_pmu_sched_task(struct perf_event_pmu_context *pmu_ctx,
static int intel_pmu_check_period(struct perf_event *event, u64 value)
{
- return intel_pmu_has_bts_period(event, value) ? -EINVAL : 0;
+ if (intel_pmu_has_bts_period(event, value))
+ return -EINVAL;
+
+ if ((event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) &&
+ x86_pmu.arch_pebs && value > MAX_CFG_C_RELOAD_PERIOD)
+ return -EINVAL;
+
+ if (is_acr_event_group(event) && event->attr.config2 &&
+ value > MAX_CFG_C_RELOAD_PERIOD)
+ return -EINVAL;
+
+ return 0;
}
static void intel_aux_output_init(void)
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread* [Patch v2 16/16] perf/x86/intel: Fix incorrect count calculation for arch-PEBS events
2026-12-01 0:36 [Patch v2 00/16] perf/x86/intel: Fix multiple sampling and PEBS bugs (Part I) Dapeng Mi
` (14 preceding siblings ...)
2026-12-01 0:37 ` [Patch v2 15/16] perf/x86/intel: Reject ACR/arch-PEBS events with sample_period > 2^32 Dapeng Mi
@ 2026-12-01 0:37 ` Dapeng Mi
15 siblings, 0 replies; 17+ messages in thread
From: Dapeng Mi @ 2026-12-01 0:37 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Ian Rogers, Adrian Hunter, Alexander Shishkin,
Andi Kleen, Eranian Stephane
Cc: linux-kernel, linux-perf-users, Dapeng Mi, Zide Chen,
Falcon Thomas, Xudong Hao, Dapeng Mi, Badari Pulavarty,
Jiebin Sun
When an arch-PEBS counter whose auto-reload value is programmed via a
*_CFG_C MSR overflows, the hardware reloads the counter using the reload
value from *_CFG_C bits [31:0]. However, the hardware 1-extends instead
of sign-extends this 32-bit reload value to the 48-bit counter width.
This works fine for auto-reload arch-PEBS events: since the maximum
allowed sample period is 2^32, "-hwc->sample_period" is always negative,
and the hardware 1-extension yields the correct value.
However, for non-auto-reload arch-PEBS events, the reload value is 0
by default, which is incorrectly extended to 0xffff00000000.
Consequently, x86_perf_event_update() calculates a wrong count for
such non-auto-reload PEBS events. This bogus 0xffff00000000 count
inflates the observed event rate and eventually causes the driver to
derive a much larger sampling period than intended.
Fix this by factoring out two new helpers, _x86_perf_event_update() and
_x86_perf_event_set_period(), which take the effective reload bitwidth
as a parameter. Limit this bitwidth to 32 bits (instead of the default
48 bits) when updating these non-auto-reload events, ensuring that the
1-extended upper bits are masked out of the delta calculation. Apply the
same bitwidth limit to the corresponding count calculation functions,
such as intel_perf_event_update_pmc().
Fixes: 52448a0a7390 ("perf/x86/intel: Setup PEBS data configuration and enable legacy groups")
Reported-by: Badari Pulavarty <badari.pulavarty@intel.com>
Reported-by: Jiebin Sun <jiebin.sun@intel.com>
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
arch/x86/events/core.c | 26 +++++++++++++++++++++-----
arch/x86/events/intel/core.c | 33 ++++++++++++++++++++++++++++++---
arch/x86/events/intel/ds.c | 2 +-
arch/x86/events/perf_event.h | 3 +++
4 files changed, 55 insertions(+), 9 deletions(-)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index eb51d69396b0..413f7b7e3184 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -125,10 +125,10 @@ u64 __read_mostly hw_cache_extra_regs
* Can only be executed on the CPU where the event is active.
* Returns the delta events processed.
*/
-u64 x86_perf_event_update(struct perf_event *event)
+u64 _x86_perf_event_update(struct perf_event *event, int reload_bits)
{
struct hw_perf_event *hwc = &event->hw;
- int shift = 64 - x86_pmu.cntval_bits;
+ int shift = 64 - min(x86_pmu.cntval_bits, reload_bits);
u64 prev_raw_count, new_raw_count;
u64 delta;
@@ -165,6 +165,11 @@ u64 x86_perf_event_update(struct perf_event *event)
return new_raw_count;
}
+u64 x86_perf_event_update(struct perf_event *event)
+{
+ return _x86_perf_event_update(event, x86_pmu.cntval_bits);
+}
+
/*
* Find and validate any extra registers to set up.
*/
@@ -1560,12 +1565,13 @@ DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
* Set the next IRQ period, based on the hwc->period_left value.
* To be called with the event disabled in hw:
*/
-int x86_perf_event_set_period(struct perf_event *event)
+int _x86_perf_event_set_period(struct perf_event *event, int reload_bits)
{
struct hw_perf_event *hwc = &event->hw;
s64 left = local64_read(&hwc->period_left);
s64 period = hwc->sample_period;
int ret = 0, idx = hwc->idx;
+ u64 max_period;
if (unlikely(!hwc->event_base))
return 0;
@@ -1592,8 +1598,13 @@ int x86_perf_event_set_period(struct perf_event *event)
if (unlikely(left < 2))
left = 2;
- if (left > x86_pmu.max_period)
- left = x86_pmu.max_period;
+ /*
+ * Use "BIT_ULL(reload_bits - 1) - 1" instead of BIT_ULL(reload_bits)
+ * to avoid 32-bit wraparound issue.
+ */
+ max_period = min(x86_pmu.max_period, BIT_ULL(reload_bits - 1) - 1);
+ if (left > max_period)
+ left = max_period;
static_call_cond(x86_pmu_limit_period)(event, &left);
@@ -1619,6 +1630,11 @@ int x86_perf_event_set_period(struct perf_event *event)
return ret;
}
+int x86_perf_event_set_period(struct perf_event *event)
+{
+ return _x86_perf_event_set_period(event, x86_pmu.cntval_bits);
+}
+
void x86_pmu_enable_event(struct perf_event *event)
{
if (__this_cpu_read(cpu_hw_events.enabled))
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 519ef122c1a3..bfa0d7dabb56 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3366,7 +3366,7 @@ static void intel_pmu_read_event(struct perf_event *event)
return;
}
- x86_perf_event_update(event);
+ _x86_perf_event_update(event, intel_pmu_get_reload_bits(event));
}
static void intel_pmu_enable_fixed(struct perf_event *event)
@@ -3716,12 +3716,39 @@ int intel_pmu_save_and_restart(struct perf_event *event)
return static_call(x86_pmu_set_period)(event);
}
+int intel_pmu_get_reload_bits(struct perf_event *event)
+{
+ if (!event)
+ return x86_pmu.cntval_bits;
+
+ /*
+ * The *_CFG_C[31:0] MSR holds a 32-bit reload value, which the
+ * hardware 1-extends (instead of sign-extends) to the 48-bit
+ * counter width on overflow.
+ *
+ * This works fine for auto-reload arch-PEBS events: since the
+ * maximum allowed sample period is 2^32, "-hwc->sample_period" is
+ * always negative, and the hardware 1-extension yields the correct
+ * value.
+ *
+ * However, for non-auto-reload arch-PEBS events, the reload value
+ * is 0 by default, which is incorrectly extended to 0xffff00000000.
+ * Limit the effective width to 32 bits to prevent these 1-extended
+ * upper bits from corrupting the delta count calculation.
+ */
+ if (x86_pmu.arch_pebs && event->attr.precise_ip &&
+ !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
+ return min(x86_pmu.cntval_bits, 32);
+
+ return x86_pmu.cntval_bits;
+}
+
static int intel_pmu_set_period(struct perf_event *event)
{
if (unlikely(is_topdown_count(event)))
return static_call(intel_pmu_set_topdown_event_period)(event);
- return x86_perf_event_set_period(event);
+ return _x86_perf_event_set_period(event, intel_pmu_get_reload_bits(event));
}
static u64 intel_pmu_update(struct perf_event *event)
@@ -3729,7 +3756,7 @@ static u64 intel_pmu_update(struct perf_event *event)
if (unlikely(is_topdown_count(event)))
return static_call(intel_pmu_update_topdown_event)(event, NULL);
- return x86_perf_event_update(event);
+ return _x86_perf_event_update(event, intel_pmu_get_reload_bits(event));
}
static void intel_pmu_reset(void)
diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c
index bc44f507f872..2b8ac8ebd190 100644
--- a/arch/x86/events/intel/ds.c
+++ b/arch/x86/events/intel/ds.c
@@ -2346,7 +2346,7 @@ static void adaptive_pebs_save_regs(struct pt_regs *regs,
static void intel_perf_event_update_pmc(struct perf_event *event, u64 pmc)
{
- int shift = 64 - x86_pmu.cntval_bits;
+ int shift = 64 - intel_pmu_get_reload_bits(event);
struct hw_perf_event *hwc;
u64 delta, prev_pmc;
diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h
index e274802ef062..359c19253e77 100644
--- a/arch/x86/events/perf_event.h
+++ b/arch/x86/events/perf_event.h
@@ -1387,6 +1387,7 @@ static inline bool x86_pmu_has_lbr_callstack(void)
DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
DECLARE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
+int _x86_perf_event_set_period(struct perf_event *event, int reload_bits);
int x86_perf_event_set_period(struct perf_event *event);
/*
@@ -1408,6 +1409,7 @@ extern u64 __read_mostly hw_cache_extra_regs
[PERF_COUNT_HW_CACHE_OP_MAX]
[PERF_COUNT_HW_CACHE_RESULT_MAX];
+u64 _x86_perf_event_update(struct perf_event *event, int reload_bits);
u64 x86_perf_event_update(struct perf_event *event);
static inline u64 intel_pmu_topdown_event_update(struct perf_event *event, u64 *val)
@@ -1844,6 +1846,7 @@ static __always_inline void __intel_pmu_lbr_disable(void)
extern int __intel_pmu_quiesce(void);
extern void __intel_pmu_resume(int pmu_enabled);
+int intel_pmu_get_reload_bits(struct perf_event *event);
int intel_pmu_save_and_restart(struct perf_event *event);
struct event_constraint *
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread