From: Dapeng Mi <dapeng1.mi@linux.intel.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Andi Kleen <ak@linux.intel.com>,
Eranian Stephane <eranian@google.com>
Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
Dapeng Mi <dapeng1.mi@intel.com>, Zide Chen <zide.chen@intel.com>,
Falcon Thomas <thomas.falcon@intel.com>,
Xudong Hao <xudong.hao@intel.com>,
Dapeng Mi <dapeng1.mi@linux.intel.com>,
Badari Pulavarty <badari.pulavarty@intel.com>,
Jiebin Sun <jiebin.sun@intel.com>
Subject: [Patch v2 16/16] perf/x86/intel: Fix incorrect count calculation for arch-PEBS events
Date: Tue, 1 Dec 2026 08:37:08 +0800 [thread overview]
Message-ID: <20261201003708.1806748-17-dapeng1.mi@linux.intel.com> (raw)
In-Reply-To: <20261201003708.1806748-1-dapeng1.mi@linux.intel.com>
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
prev parent reply other threads:[~2026-12-01 0:46 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` [Patch v2 03/16] perf/x86/intel: Guard leader sibling walk on nr_siblings Dapeng Mi
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 ` [Patch v2 05/16] perf/x86/intel: Reset active_pebs_data_cfg " 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
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 ` [Patch v2 08/16] perf/x86/intel: Limit PEBS counter iteration to valid array bounds 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
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 ` [Patch v2 11/16] perf/x86/intel: Fix stale PEBS count without counter-group support Dapeng Mi
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 ` [Patch v2 13/16] perf/x86/intel: Initialize arch_pebs_index to 0 before writing MSR 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
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201003708.1806748-17-dapeng1.mi@linux.intel.com \
--to=dapeng1.mi@linux.intel.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=badari.pulavarty@intel.com \
--cc=dapeng1.mi@intel.com \
--cc=eranian@google.com \
--cc=irogers@google.com \
--cc=jiebin.sun@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=thomas.falcon@intel.com \
--cc=xudong.hao@intel.com \
--cc=zide.chen@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox