[Patch v2 16/16] perf/x86/intel: Fix incorrect count calculation for arch-PEBS events

From: Dapeng Mi

Date: Mon Nov 30 2026 - 19:47:02 EST


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@xxxxxxxxx>
Reported-by: Jiebin Sun <jiebin.sun@xxxxxxxxx>
Signed-off-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
---
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