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=1796085987; b=JU0eSLcogpoYwg5kesmW4doYuGUU+RM0TkYLw9fKMjRxx9huQG4sGfFGvVXlP85fPbef fHBRwdssMqDMNve5ETRsQ2JWU3r8swtgNXOQNexcTapA7Z1fcjHDvEjZKlG/mD77lVASf qmWC56DTT/8gAh1mVshGCQlsAoHlHue5iSZ9jGqIBJH8L9MrExttvddRiFto4UtzxyEmh Qh8XJk9By6inxBdPFOWv0oiKUfzN2K12CG3V5EmUJAjmdAHb//HsTwIUuVulr8h58ehXn p6vNbkYJl5JDO71gZIOpPvzQ/OBHGo0dNWi6hoOBSnWuUNJHyfV0SkoCVaNfDV70LVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796085987; 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=OP0sx7vQcAVnON3tsxQB8M+MfI1aoTxh/l+KTSYYUVw=; b=LxmZKyEFfxdM5KL2edbe/rAkbiET7CLVy9hr5RsSfi7RdZuSiFfrTGUo99pu7pDQR7LW iAN1MCXUKF76vuU7emq4EV26zF6Izm+tWQyh2llrL02X9r1ijS4md3QOQPgvo4X0av/nT 3sVmJM5SGX4wClSz8vJYyd9tahd8KRAg7T/9zavODv952vyiGU1HQsy+vWRMfV9sD05WK RL9eGpWXmLkD729Lh3sPVJJknxyuE0L7cfTphKa9dgEqhyefL4NwJIYfYcNFAsWHjZFRL CJCcXCr6FuumsRDwTHTFOe0d/rqirDzwtA/kx+uiB4fAPZFD2qIW9SgFPADuV3VxPVQ== 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=msX3BGed; 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 A1E8A606A325; Tue, 1 Dec 2026 00:46:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A1E8A606A325 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=msX3BGed DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796085987; x=1827621987; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OP0sx7vQcAVnON3tsxQB8M+MfI1aoTxh/l+KTSYYUVw=; b=msX3BGedeli6OxgtYcnRUabc8eEtwafVQqGKevb0iOuz4Dbj+ceaJ/vv eRvJzfbEp6zPyH/RW/OZVBRnmycDxE0LgkBHfTGSh24lOAspbFDQ52k4t MBx7vkFS0viVg9Bjtpop3ou4RTesGLpNsBpUFkwrWCU5jxXutCHUCe3/7 eCYijOyTLXdaE7vfeJv82DbyuOGDOTsyv/I6NYPMs68r0kXrm8upLEMAj VdYYPqTTEIqBGqBUeHWnf5uZYv86nZobAAvLCPdE0ACfb9b+vl+n6UG7X Dk0otb/PyIMQOf0ZxBMtymK87cisa4TGoCf/DAqEQS1isQd0gUK5QFn9F Q==; X-CSE-ConnectionGUID: dM04WkU0R4mQA/lh5kIh+w== X-CSE-MsgGUID: I/WsQQvyQGyICQnSFPpVgA== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4846029" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4846029" 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:27 -0800 X-CSE-ConnectionGUID: RpKbWHkdSiGXvuI3NAF5kw== X-CSE-MsgGUID: Wv4xUKJQQlCp9doDRqnsFQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="13678831" Received: from spr.sh.intel.com ([10.112.229.196]) by fmviesa010.fm.intel.com with ESMTP; 30 Nov 2026 16:46:20 -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 Subject: [Patch v2 11/16] perf/x86/intel: Fix stale PEBS count without counter-group support Date: Tue, 1 Dec 2026 08:37:03 +0800 Message-Id: <20261201003708.1806748-12-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 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 --- 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