From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796086019; b=VILK8vwLZEqPlYUHKWaU4GZODkQGUGdcB87iPwmPwvm7pvL3Sf34EhZ6qCO+0KwH5rtB wVaa/Xszn6qyS4U9+CK28VyfSpk3RJkgh+NK7zmjaaRBwa1+dEpEvUr+1dwzaVdmihqv/ SsZXwrFLC7HaILtlUXOFyfVTDAljfOcN4BRVmw0bSCqSPT66ttY72Cpj2Ji9I1ZaWvuxL ctuW/YQKUNK/9vOgzTK8x9LC0iOXneb34ghkIO30nnw0wSedVmV4ztoW6fgQU72bn5vfX pPlHzdj14PPsDbwX2BtyPGf3NYPhA2/JBoiykAuOGjPIy+D46nHsqSb0NsxS9msvwdw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796086019; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=mPRz1wJ6P8pmyd4Yylh3IUMa8k0kUwqi56IxaX+kMsc=; b=bUm0Ug6nEiGfAgYGo5jq29Xk9vfYJP3hu+nFJqpS1xelDe5YkocQmSVLFrJ3KIxvqyVa agr4k/mURLijEaRhkwg87Q/9FkY3abiyiFda6Iou4pjv1WO1lwsEoVkB0As07miCHdmRg WzWzZppWgRgKLdzAbULWkbxWQqqxZFn5VGlIJHqKbfHxtHBb3HKEencssV91nRt+/F4/g k4E8WXFlG9IbAaEqLs4sE7ze9ga7Km/VgtTA07XNsM21HNhZrR+RPzmInzd9GLrZUchk6 YYXNnzwjer/ciIFdWyMWsqQ9RXK3cm/vEVrGYctTTlGtvjmBeDR2FDp9zo6UYdazYAA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=BuL0O8Du; arc=none smtp.remote-ip=198.175.65.9 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2FF2606A325; Tue, 1 Dec 2026 00:46:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C2FF2606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=BuL0O8Du DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796086019; x=1827622019; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mPRz1wJ6P8pmyd4Yylh3IUMa8k0kUwqi56IxaX+kMsc=; b=BuL0O8Dud67cp06DxqowFUWlRz2Tw71lN2xljcakd+1aHAW5aC+P7QaG X1xxboWbDA9adZAwonqzyPPeValwnLTnVriYgs0LEOkqjsAB1U8GBT5Ws HXuFJSXOhObHE9bWqAwBQCsensxAc3irkN8N4zkxfVbOf7FEPXtqPs2Ig VedBHX2WH0u0yx97QgJ3bif0InEjrI0EoeI8/X49FqvNPseQslHTkFWs6 rDg0aOou89pDZFnwCqq+x0pvTshnHTBLfh2MhuqaA1ntlvZ+LDvnDRO9Z QUBXgWpMeujA9mQMY2mzI5Q5yNZ3FQvBuHzO/lu1hOllXPaeGaSZ7r1Js Q==; X-CSE-ConnectionGUID: jdkKCFcuTv+TqeESYT/1Kg== X-CSE-MsgGUID: xsHB+z/mS+uWuCO7JNdPmQ== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4846134" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4846134" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 16:46:59 -0800 X-CSE-ConnectionGUID: Y8+StinYRImy5dwHr+NaEg== X-CSE-MsgGUID: dTbEL0rgRpKcYt2cf4p3yg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="13678915" Received: from spr.sh.intel.com ([10.112.229.196]) by fmviesa010.fm.intel.com with ESMTP; 30 Nov 2026 16:46:51 -0800 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Adrian Hunter , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Dapeng Mi , Zide Chen , Falcon Thomas , Xudong Hao , Dapeng Mi , Badari Pulavarty , Jiebin Sun 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 Message-Id: <20261201003708.1806748-17-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261201003708.1806748-1-dapeng1.mi@linux.intel.com> References: <20261201003708.1806748-1-dapeng1.mi@linux.intel.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Reported-by: Jiebin Sun Signed-off-by: Dapeng Mi --- 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