From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A284B3B27FC; Fri, 29 May 2026 08:02:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041762; cv=none; b=si9xWh+nBCLi4SRBRL7rrFl9QJyGmQPCFweHmAVdRNbykcN19bEsol+pHljWcw8e1xKChe08g6haaTXGCBV1C05dPKcghZHGHMJGWiwOomBeQflzXqjEeF6LBzvbIRlY1HFmMav+nBF4ylSYgwzm6+EnW2Au1y17wo3JCRUBTIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041762; c=relaxed/simple; bh=qUPDEfNcCt2GNEn5iL1z6nRKeA66n8BleMMrGIHFy1E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sKbw2QSp58nndkx4qQIi1PWSpqDF5LL/TBANE7QYG1zryomlyyJQepa77/1ZUKe76ddoZTZTmd/BzikMU0G6xB43R/pq1duDWhCINWaveauVOrPbyrhe1RpfYYfv+Hy12SkQiSJPXEuRsHfFFVJRkfQOd5mO4oFbFv25/GHUfD4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=G/vADQhh; arc=none smtp.client-ip=192.198.163.7 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.b="G/vADQhh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041761; x=1811577761; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qUPDEfNcCt2GNEn5iL1z6nRKeA66n8BleMMrGIHFy1E=; b=G/vADQhhUBqwE6u66uFvepN8X60jgGMbufJVON4CSAkbtOPop6IIw3GX hC1V02et5Mh7Y/3rg+jlhqbbs8OpeWssMQHLysAlxGaT0Z62YHJ9guJi7 OXqhLElSHgUDB77bynn5hwppuyoc7Szk2PNWiGj/ngcU5NqHu7KXkdG6+ a1UPZFzRJwRP0phRVjo+t22RA2m8bXnYunGwkO+u2GFDoxqrc4jZoOOe7 Q96QPBo3LjNNVMcTdfFIY9yoLdlBJtORBMSY6kMkcAcpmBNEMKo22Uwk8 eZG1sJiCK1d7zNYyrC5uciW5IzFJHEiXtCLVpOuVuh+e3hLEY6pPqj+YD g==; X-CSE-ConnectionGUID: GSkrwPsXTXyHngkYv16giA== X-CSE-MsgGUID: g2+JXHpOTBqUuoAy+cKKww== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341843" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341843" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:02:40 -0700 X-CSE-ConnectionGUID: sTNTr5/gQ0O1BrJbzqeCYg== X-CSE-MsgGUID: sLGQfdjFS1y6pTfB2sMFag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246801936" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:02:35 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 01/23] perf/x86/intel: Validate return value of intel_pmu_init_hybrid() Date: Fri, 29 May 2026 15:56:23 +0800 Message-Id: <20260529075645.580362-2-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The memory allocation for the x86_pmu.hybrid_pmu[] array in intel_pmu_init_hybrid() can theoretically fail due to memory shortages. If this occurs, the initialization of the x86 hybrid PMU would fail. Currently, the code does not check the return value of the intel_pmu_init_hybrid() function, which could lead to attempts to access the uninitialized x86_pmu.hybrid_pmu[] array, potentially causing a system panic. So, adds a check for the return value of intel_pmu_init_hybrid() to prevent invalid memory access in such scenarios. Signed-off-by: Dapeng Mi --- V8: New patch. arch/x86/events/intel/core.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 0217e701aeeb..85c329bd52be 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -7870,6 +7870,7 @@ __init int intel_pmu_init(void) int version, i; char *name; struct x86_hybrid_pmu *pmu; + int ret; =20 /* Architectural Perfmon was introduced starting with Core "Yonah" */ if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { @@ -8545,7 +8546,9 @@ __init int intel_pmu_init(void) * * Initialize the common PerfMon capabilities here. */ - intel_pmu_init_hybrid(hybrid_big_small); + ret =3D intel_pmu_init_hybrid(hybrid_big_small); + if (ret < 0) + return ret; =20 x86_pmu.pebs_latency_data =3D grt_latency_data; x86_pmu.get_event_constraints =3D adl_get_event_constraints; @@ -8603,7 +8606,9 @@ __init int intel_pmu_init(void) case INTEL_METEORLAKE: case INTEL_METEORLAKE_L: case INTEL_ARROWLAKE_U: - intel_pmu_init_hybrid(hybrid_big_small); + ret =3D intel_pmu_init_hybrid(hybrid_big_small); + if (ret < 0) + return ret; =20 x86_pmu.pebs_latency_data =3D cmt_latency_data; x86_pmu.get_event_constraints =3D mtl_get_event_constraints; @@ -8634,7 +8639,9 @@ __init int intel_pmu_init(void) pr_cont("Pantherlake Hybrid events, "); name =3D "pantherlake_hybrid"; =20 - intel_pmu_init_hybrid(hybrid_big_small); + ret =3D intel_pmu_init_hybrid(hybrid_big_small); + if (ret < 0) + return ret; =20 /* Initialize big core specific PerfMon capabilities.*/ pmu =3D &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; @@ -8649,7 +8656,9 @@ __init int intel_pmu_init(void) pr_cont("Arrowlake Hybrid events, "); name =3D "arrowlake_hybrid"; =20 - intel_pmu_init_hybrid(hybrid_big_small); + ret =3D intel_pmu_init_hybrid(hybrid_big_small); + if (ret < 0) + return ret; =20 /* Initialize big core specific PerfMon capabilities.*/ pmu =3D &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; @@ -8666,7 +8675,9 @@ __init int intel_pmu_init(void) pr_cont("Lunarlake Hybrid events, "); name =3D "lunarlake_hybrid"; =20 - intel_pmu_init_hybrid(hybrid_big_small); + ret =3D intel_pmu_init_hybrid(hybrid_big_small); + if (ret < 0) + return ret; =20 /* Initialize big core specific PerfMon capabilities.*/ pmu =3D &x86_pmu.hybrid_pmu[X86_HYBRID_PMU_CORE_IDX]; @@ -8691,7 +8702,9 @@ __init int intel_pmu_init(void) break; =20 case INTEL_ARROWLAKE_H: - intel_pmu_init_hybrid(hybrid_big_small_tiny); + ret =3D intel_pmu_init_hybrid(hybrid_big_small_tiny); + if (ret < 0) + return ret; =20 x86_pmu.pebs_latency_data =3D arl_h_latency_data; x86_pmu.get_event_constraints =3D arl_h_get_event_constraints; @@ -8726,7 +8739,9 @@ __init int intel_pmu_init(void) case INTEL_NOVALAKE_L: pr_cont("Novalake Hybrid events, "); name =3D "novalake_hybrid"; - intel_pmu_init_hybrid(hybrid_big_small); + ret =3D intel_pmu_init_hybrid(hybrid_big_small); + if (ret < 0) + return ret; =20 x86_pmu.pebs_latency_data =3D nvl_latency_data; x86_pmu.get_event_constraints =3D mtl_get_event_constraints; --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6953C3B2FCC; Fri, 29 May 2026 08:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041766; cv=none; b=o2RAdDJo3Z78beoFNYRsqmgQMqnbtEa6tSCqM1FFNjjpaN0ukK221I3fy0ADdh25noqNBmAeGyZuZxVItN7Sgn1AXplLxVldA+K0kIu5wapgaaB17EPHIyUK9mL61xdACHywQd2Klk47ch4zho0WMMayqcB9VxjxRhB1lEaws2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041766; c=relaxed/simple; bh=TIF/tDrphO4XesqSFb2wFeEBW6WzJ61eKEnZ1mB2XCM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ie2faJ99TFQY0KMU4R5MeeVCxpzYENoYFfNF5GN7f5GdUTlUyv4AnuYzInWINt/z27gExITmkBOVXT3Ys/IFGOxkVDSHuHz8Fjx1zHHrSKD3m5XRzo4J3134lPoSvxCtnnPDkrvanhKz8etm1rGqgP2Xk831xFc12QBEhAzMd34= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EeVeRgoT; arc=none smtp.client-ip=192.198.163.7 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.b="EeVeRgoT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041765; x=1811577765; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TIF/tDrphO4XesqSFb2wFeEBW6WzJ61eKEnZ1mB2XCM=; b=EeVeRgoTyM1CZuhU+ZnTSPvExExAuQSoGQ3+DjvH4sw4HqAkO01iblEn cwRSpLyQFkzOO4AcW7PKeLW585rovwHODGiHJik9lkbop52eGeddIa8Ed A/0W6LPYyfJFtvYEw31QCnBXEl0nwQ+SLMZsK7eipXZdesTnJodxSwgax CSiZlB94n7sJYssslBu2Gukvl99mgYJYGHepL63Z0bSGAdNv4BLPqvaFu NOtqXcqBLzherW+YWcy+j1ie/85bM3vvQQdk+cUuEigpVAShals+6P24O RgAj09W1k7S5C2/YKWZ6DAVhdyJgGgWc2rfI7vAfQwg8kzvYgS4BFvM1P w==; X-CSE-ConnectionGUID: 74wx8onWRVGWiQL2sc13NA== X-CSE-MsgGUID: ZdIJXXmtTrmvMgaCgYxpkA== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341854" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341854" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:02:45 -0700 X-CSE-ConnectionGUID: XfJvjuLnT4ubnud0IOl41g== X-CSE-MsgGUID: jazoUWIfSsmTBtYY9DBteg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246801953" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:02:40 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 02/23] perf/x86: Move hybrid PMU initialization before x86_pmu_starting_cpu() Date: Fri, 29 May 2026 15:56:24 +0800 Message-Id: <20260529075645.580362-3-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The current approach initializes hybrid PMU structures immediately before registering them. This is risky as it can lead to key fields, such as 'capabilities', being inadvertently overwritten. Although no issues have arisen so far, this method is not ideal. It makes the PMU structure fields susceptible to being overwritten, especially with future changes that might initialize fields like 'capabilities' within init_hybrid_pmu() called by x86_pmu_starting_cpu(). To mitigate this potential problem, move the default hybrid structure initialization before calling x86_pmu_starting_cpu(). Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 4b9e105309c6..17e122e27e0b 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2195,8 +2195,20 @@ static int __init init_hw_perf_events(void) =20 pmu.attr_update =3D x86_pmu.attr_update; =20 - if (!is_hybrid()) + if (!is_hybrid()) { x86_pmu_show_pmu_cap(NULL); + } else { + int i; + + /* + * Init default ops. + * Must be called before registering x86_pmu_starting_cpu(), + * otherwise some key PMU fields, e.g., capabilities + * initialized in x86_pmu_starting_cpu(), would be overwritten. + */ + for (i =3D 0; i < x86_pmu.num_hybrid_pmus; i++) + x86_pmu.hybrid_pmu[i].pmu =3D pmu; + } =20 if (!x86_pmu.read) x86_pmu.read =3D _x86_pmu_read; @@ -2243,7 +2255,6 @@ static int __init init_hw_perf_events(void) for (i =3D 0; i < x86_pmu.num_hybrid_pmus; i++) { hybrid_pmu =3D &x86_pmu.hybrid_pmu[i]; =20 - hybrid_pmu->pmu =3D pmu; hybrid_pmu->pmu.type =3D -1; hybrid_pmu->pmu.attr_update =3D x86_pmu.attr_update; hybrid_pmu->pmu.capabilities |=3D PERF_PMU_CAP_EXTENDED_HW_TYPE; --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 605603B4EBD; Fri, 29 May 2026 08:02:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041774; cv=none; b=fOBXQjMZ3X9m65TDgyJlceLYrsiRCcWuogLOx/+GGueYxgKj31kOZNAM6Hqttat0uLKl+ygU4XYnDdGUFWSNjCLqo25l4SWqck8JVwZNmX5mVxWg0JMNi8jq+x6FaOcaNCMwosGFpkvM2nzwzHJvRxVTsA7CN0U1xdJx5mcOu/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041774; c=relaxed/simple; bh=pbwTklKfyTOR62FXYb0qiVQZjglvINP5HCMkoD1yNlw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=beQbbqM/TYICPo8ZeTpp0fZifuA3BpWTcXULqWM6wZDCdeQTsdRpxLVQ5vH3O46GpQPNjf70z65m69GHy+oGXo+Qs3JFCHUcrDfGl0CpVDacLdglRAYU4b3Kr14XEbATbMEv5Ptau6BnKTHXRXsvfqPLaKWWbu5ZDzP0399eBA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ACZf2N3W; arc=none smtp.client-ip=192.198.163.7 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.b="ACZf2N3W" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041770; x=1811577770; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pbwTklKfyTOR62FXYb0qiVQZjglvINP5HCMkoD1yNlw=; b=ACZf2N3W8neoS1WhYfxII1N2+Dds6J47IZrsDxhUoWwIOOY7lLOz8ivA YmsIRCxNjYwwU+LeLw+vXxkOxSEmTF8/UVnKJAOXzrMotXyPKSPET8fKh cXYi7ZSjNOjuRvh0uvs+6+eYK0eZb7HIaSjlivUltyae2fsnWcqdvUbkT +Vyvf5WBnXmgblY01sNdNXMKgSg76k5o7eNp4p/mQ7jZ5Me3GYd30GYdo UrfN1hB8t1B7KigWnAnreZy8KIH/OZ7Xgi0264qHNvaupdwEM7S1fGJpI /Rm5nSVud/NtS9JMlQwC510MinwhSKQsxD3HGXvcWSN1eIaoYQrszicoS w==; X-CSE-ConnectionGUID: 2zf3IJQkRuGIXTEmAMa6Xg== X-CSE-MsgGUID: GdWOO+DgS2uwnMLoupPwdA== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341863" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341863" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:02:50 -0700 X-CSE-ConnectionGUID: GyKS4eiRQUifg9YemFGWdg== X-CSE-MsgGUID: /pBsLKSMQF6Qh9rX+HMOWg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246801966" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:02:45 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 03/23] perf/x86/intel: Enable large PEBS sampling for XMMs Date: Fri, 29 May 2026 15:56:25 +0800 Message-Id: <20260529075645.580362-4-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Modern PEBS hardware supports directly sampling XMM registers, then large PEBS can be enabled for XMM registers just like other GPRs. Reported-by: Xudong Hao Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 85c329bd52be..92cb9a716e83 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -4702,7 +4702,8 @@ static unsigned long intel_pmu_large_pebs_flags(struc= t perf_event *event) flags &=3D ~PERF_SAMPLE_REGS_USER; if (event->attr.sample_regs_user & ~PEBS_GP_REGS) flags &=3D ~PERF_SAMPLE_REGS_USER; - if (event->attr.sample_regs_intr & ~PEBS_GP_REGS) + if (event->attr.sample_regs_intr & + ~(PEBS_GP_REGS | PERF_REG_EXTENDED_MASK)) flags &=3D ~PERF_SAMPLE_REGS_INTR; return flags; } --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20F5A3B6352; Fri, 29 May 2026 08:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041777; cv=none; b=aDesz3tL5WVkVbL00wGqFIW7FyE9I7oFWmEX+s7Ui45Ruk8DWOcX1i/gG128l1JDAXS+lDqUc0XsjVTMi8SC3FgfnQiY/ybiMsyUSdNW473OL1VbAmnYfOZZd0m3OEHMpwwtPUq4lx5Jgd49bqU3qMsb88KslW/34S/3LeRbhCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041777; c=relaxed/simple; bh=r77CfGsAvEDfFwMDjM8q9sn4kKauAREg1ZMpuQEEFN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FbNZYUbciMCmu1/1P0eQ+DNe9MiIlRzGmNc1FjVGIlw1Ic7N5ya/rOcr0shpXv/iWLalZ2SN73QpDrytZxy4uDGD7OWa9PbysK+s3tXsn2jnUukckFIlzrt5kWV+TcXTyGEmjanyZiLxNeeAWoSaj/gOW0OZrMEFp9oet62tWJE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=a02Xhsjm; arc=none smtp.client-ip=192.198.163.7 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.b="a02Xhsjm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041775; x=1811577775; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r77CfGsAvEDfFwMDjM8q9sn4kKauAREg1ZMpuQEEFN8=; b=a02Xhsjm4RXMbQRKDiBdCF+H2/pSqiSAO/NZWp4sdIc0Jfd50BAmB2KY QN0EUMh1weyscQBogVfLAb7+FCSKdQ5G7YKlNqr/kM5Rs3/q5W9+IhisG EWXmlgLlYlqdhaOt0oF5XH3xXME/ocul+W+QyXxDvQTf3OZUvSlwaBLT6 PjZesIL37EasOWOuDwIM01xfcNp5CueV0Y9bZ3EFP7Z1ddx7fPnHBILsb C98LqrnZUL/a7SVS8TfUgOj+0SXSFAxZljr0HnC7KdvDAo0VJ25DBWCbD PQF8QksWUAQy+h9S5KJmlOxIp73A3dELR4g1REsgQ1UeDG4ZKSlJteB1g w==; X-CSE-ConnectionGUID: Olul0qKUQ8enyrsJ2qyHQA== X-CSE-MsgGUID: JU7HO9A4R4KT8WPFtrQO7g== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341874" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341874" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:02:55 -0700 X-CSE-ConnectionGUID: okP88NSpTteufOWQCTcoxA== X-CSE-MsgGUID: Kx3l2FVoT8CIRwtc3kWTpQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246801984" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:02:50 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 04/23] perf/x86/intel: Convert x86_perf_regs to per-cpu variables Date: Fri, 29 May 2026 15:56:26 +0800 Message-Id: <20260529075645.580362-5-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, the intel_pmu_drain_pebs_icl() and intel_pmu_drain_arch_pebs() helpers define many temporary variables. Upcoming patches will add new fields like *ymm_regs and *zmm_regs to the x86_perf_regs structure to support sampling for these SIMD registers. This would increase the stack size consumed by these helpers, potentially triggering the warning: "the frame size of 1048 bytes is larger than 1024 bytes [-Wframe-larger-than=3D]". To eliminate this warning, convert x86_perf_regs to per-cpu variables. Please note *_drain_pebs() can't be interrupted by other NMIs since either it's already in NMI context or PMU is already disabled. Signed-off-by: Dapeng Mi --- arch/x86/events/intel/ds.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index cb72af9b61ce..a31648d2adb1 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -2933,6 +2933,8 @@ __intel_pmu_pebs_last_event(struct perf_event *event, } } =20 +static DEFINE_PER_CPU(struct x86_perf_regs, x86_pebs_regs); + static __always_inline void __intel_pmu_pebs_events(struct perf_event *event, struct pt_regs *iregs, @@ -2942,8 +2944,8 @@ __intel_pmu_pebs_events(struct perf_event *event, setup_fn setup_sample) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); - struct x86_perf_regs perf_regs; - struct pt_regs *regs =3D &perf_regs.regs; + struct x86_perf_regs *perf_regs =3D this_cpu_ptr(&x86_pebs_regs); + struct pt_regs *regs =3D &perf_regs->regs; void *at =3D get_next_pebs_record_by_bit(base, top, bit); int cnt =3D count; =20 @@ -3191,8 +3193,8 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs *= iregs, struct perf_sample_d void *last[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS]; struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct debug_store *ds =3D cpuc->ds; - struct x86_perf_regs perf_regs; - struct pt_regs *regs =3D &perf_regs.regs; + struct x86_perf_regs *perf_regs =3D this_cpu_ptr(&x86_pebs_regs); + struct pt_regs *regs =3D &perf_regs->regs; struct pebs_basic *basic; void *base, *at, *top; u64 mask; @@ -3242,8 +3244,8 @@ static void intel_pmu_drain_arch_pebs(struct pt_regs = *iregs, void *last[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS]; struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); union arch_pebs_index index; - struct x86_perf_regs perf_regs; - struct pt_regs *regs =3D &perf_regs.regs; + struct x86_perf_regs *perf_regs =3D this_cpu_ptr(&x86_pebs_regs); + struct pt_regs *regs =3D &perf_regs->regs; void *base, *at, *top; u64 mask; =20 --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE8CA3B1EF1; Fri, 29 May 2026 08:02:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041781; cv=none; b=EZFJLvv9mg2oKwT95q/FDsv38bxgJHCyR1jK/tpsq+Pl62gRS0xKOXteAcRltwOqk1RFbSWmmwywXF/ZdTqFua3UqNw5OnXV1mLA4FuuIlEAWi5ilRsleCH0l+Gi3OCstY2ScjjNM0L80pgG9pM8qTHp7yXvvwxLasJfrW15Ww8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041781; c=relaxed/simple; bh=LZOj3b/EyOvYL3VVB7sG2a+8VrV+NgaxM2Ae5JqisuQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z+BAAJq4EUR77OUSe3GmVdWaxOf5FrS3w+2GbukGGmehpKh1ko2I4QQ7bC+arRpUPU+465LR6sPS686qwJ/fy74SPN6qGM5XwEB+PmpFupuAmVannyT8FuCug51aBkWhU0LgYuIVEpJjTRmQrSQo0Gc8vuo/6n7Yqm3bSByG+94= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=dF4GFaU0; arc=none smtp.client-ip=192.198.163.7 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.b="dF4GFaU0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041780; x=1811577780; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LZOj3b/EyOvYL3VVB7sG2a+8VrV+NgaxM2Ae5JqisuQ=; b=dF4GFaU0I/ZapdETI3EPcGNrdGypXX+z7w7B+UDly4dq+YCE+PQO+KZZ 4XXMTkzJcKStStdOJi/JzoaQj64VyJPvbvpcQgu7nbFGOohGvzSNCQ4Of 6MlfvaoBSBuWIzpeJn2/fETxbVUXFnDqko2H2SL0VJ3Ph7sYmiS7rLXcQ xulE7O0xr/7A/wYI/ngJcRzQhU/7FQgE4xsEChmUWvlNcMmQdsImy+L1F /XjsUFT0Jjq7ZAqsXtOZJMrF+blTcUSXQJmMZTH+nV4MAgkvpC1sZw8kc c/AGmXef65bUWPJuV6mvA4BKihOy2p1eZbZ4OST+QH+X/KoZjybb9rHgN g==; X-CSE-ConnectionGUID: NOPfzi9uR5igzwQpjmc09Q== X-CSE-MsgGUID: pQYDiR66RzOqcuKlECEQ3g== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341888" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341888" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:02:59 -0700 X-CSE-ConnectionGUID: TXuNrQw4QR6KO+NapkrYPA== X-CSE-MsgGUID: ActIh8CmRem+VNG2747cTg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246801995" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:02:55 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 05/23] perf: Eliminate duplicate arch-specific functions definations Date: Fri, 29 May 2026 15:56:27 +0800 Message-Id: <20260529075645.580362-6-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define default common __weak functions for perf_reg_value(), perf_reg_validate(), perf_reg_abi() and perf_get_regs_user(). This helps to eliminate the duplicated arch-specific definations. No function changes intended. Signed-off-by: Dapeng Mi --- arch/arm/kernel/perf_regs.c | 6 ------ arch/arm64/kernel/perf_regs.c | 6 ------ arch/csky/kernel/perf_regs.c | 6 ------ arch/loongarch/kernel/perf_regs.c | 6 ------ arch/mips/kernel/perf_regs.c | 6 ------ arch/parisc/kernel/perf_regs.c | 6 ------ arch/riscv/kernel/perf_regs.c | 6 ------ arch/x86/kernel/perf_regs.c | 6 ------ include/linux/perf_regs.h | 32 ++++++------------------------- kernel/events/core.c | 22 +++++++++++++++++++++ 10 files changed, 28 insertions(+), 74 deletions(-) diff --git a/arch/arm/kernel/perf_regs.c b/arch/arm/kernel/perf_regs.c index 0529f90395c9..d575a4c3ca56 100644 --- a/arch/arm/kernel/perf_regs.c +++ b/arch/arm/kernel/perf_regs.c @@ -31,9 +31,3 @@ u64 perf_reg_abi(struct task_struct *task) return PERF_SAMPLE_REGS_ABI_32; } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/arm64/kernel/perf_regs.c b/arch/arm64/kernel/perf_regs.c index b4eece3eb17d..70e2f13f587f 100644 --- a/arch/arm64/kernel/perf_regs.c +++ b/arch/arm64/kernel/perf_regs.c @@ -98,9 +98,3 @@ u64 perf_reg_abi(struct task_struct *task) return PERF_SAMPLE_REGS_ABI_64; } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/csky/kernel/perf_regs.c b/arch/csky/kernel/perf_regs.c index 09b7f88a2d6a..94601f37b596 100644 --- a/arch/csky/kernel/perf_regs.c +++ b/arch/csky/kernel/perf_regs.c @@ -31,9 +31,3 @@ u64 perf_reg_abi(struct task_struct *task) return PERF_SAMPLE_REGS_ABI_32; } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/loongarch/kernel/perf_regs.c b/arch/loongarch/kernel/perf= _regs.c index 263ac4ab5af6..8dd604f01745 100644 --- a/arch/loongarch/kernel/perf_regs.c +++ b/arch/loongarch/kernel/perf_regs.c @@ -45,9 +45,3 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) return regs->regs[idx]; } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/mips/kernel/perf_regs.c b/arch/mips/kernel/perf_regs.c index e686780d1647..7736d3c5ebd2 100644 --- a/arch/mips/kernel/perf_regs.c +++ b/arch/mips/kernel/perf_regs.c @@ -60,9 +60,3 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) return (s64)v; /* Sign extend if 32-bit. */ } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/parisc/kernel/perf_regs.c b/arch/parisc/kernel/perf_regs.c index 10a1a5f06a18..b9fe1f2fcb9b 100644 --- a/arch/parisc/kernel/perf_regs.c +++ b/arch/parisc/kernel/perf_regs.c @@ -53,9 +53,3 @@ u64 perf_reg_abi(struct task_struct *task) return PERF_SAMPLE_REGS_ABI_64; } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/riscv/kernel/perf_regs.c b/arch/riscv/kernel/perf_regs.c index fd304a248de6..3bba8deababb 100644 --- a/arch/riscv/kernel/perf_regs.c +++ b/arch/riscv/kernel/perf_regs.c @@ -35,9 +35,3 @@ u64 perf_reg_abi(struct task_struct *task) #endif } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 624703af80a1..81204cb7f723 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -100,12 +100,6 @@ u64 perf_reg_abi(struct task_struct *task) return PERF_SAMPLE_REGS_ABI_32; } =20 -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} #else /* CONFIG_X86_64 */ #define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \ (1ULL << PERF_REG_X86_ES) | \ diff --git a/include/linux/perf_regs.h b/include/linux/perf_regs.h index f632c5725f16..144bcc3ff19f 100644 --- a/include/linux/perf_regs.h +++ b/include/linux/perf_regs.h @@ -9,6 +9,12 @@ struct perf_regs { struct pt_regs *regs; }; =20 +u64 perf_reg_value(struct pt_regs *regs, int idx); +int perf_reg_validate(u64 mask); +u64 perf_reg_abi(struct task_struct *task); +void perf_get_regs_user(struct perf_regs *regs_user, + struct pt_regs *regs); + #ifdef CONFIG_HAVE_PERF_REGS #include =20 @@ -16,35 +22,9 @@ struct perf_regs { #define PERF_REG_EXTENDED_MASK 0 #endif =20 -u64 perf_reg_value(struct pt_regs *regs, int idx); -int perf_reg_validate(u64 mask); -u64 perf_reg_abi(struct task_struct *task); -void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs); #else =20 #define PERF_REG_EXTENDED_MASK 0 =20 -static inline u64 perf_reg_value(struct pt_regs *regs, int idx) -{ - return 0; -} - -static inline int perf_reg_validate(u64 mask) -{ - return mask ? -ENOSYS : 0; -} - -static inline u64 perf_reg_abi(struct task_struct *task) -{ - return PERF_SAMPLE_REGS_ABI_NONE; -} - -static inline void perf_get_regs_user(struct perf_regs *regs_user, - struct pt_regs *regs) -{ - regs_user->regs =3D task_pt_regs(current); - regs_user->abi =3D perf_reg_abi(current); -} #endif /* CONFIG_HAVE_PERF_REGS */ #endif /* _LINUX_PERF_REGS_H */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 7935d5663944..fedc0cf65f9e 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7769,6 +7769,28 @@ unsigned long perf_instruction_pointer(struct perf_e= vent *event, return perf_arch_instruction_pointer(regs); } =20 +u64 __weak perf_reg_value(struct pt_regs *regs, int idx) +{ + return 0; +} + +int __weak perf_reg_validate(u64 mask) +{ + return mask ? -ENOSYS : 0; +} + +u64 __weak perf_reg_abi(struct task_struct *task) +{ + return PERF_SAMPLE_REGS_ABI_NONE; +} + +void __weak perf_get_regs_user(struct perf_regs *regs_user, + struct pt_regs *regs) +{ + regs_user->regs =3D task_pt_regs(current); + regs_user->abi =3D perf_reg_abi(current); +} + static void perf_output_sample_regs(struct perf_output_handle *handle, struct pt_regs *regs, u64 mask) --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0769D3B6346; Fri, 29 May 2026 08:03:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041786; cv=none; b=ZFmzFo90TuEgykHWvFfOe8bqwAhabztbZiS6DZrStdVmcmKd07T8aj3k6VK/+fPH7NFnm/1PQima1U53ZBfWn9KDZ8olXbiZhVVqyOGxnDs5XwM7MTD7XGfuYvn1whvJM7U66wMZBpxE+NPIjMWP0I6Mso7p1B/Lsi/xhGAe7gU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041786; c=relaxed/simple; bh=pR2poulYHn6VOTSmZqyeJ9hNbKOoQfLf5cKqhyIl+hc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lCLS611Fs851DFxEbPiAJAkkmtNqxhkI3lyO+mTO9GzPxMiun1pfa4t76ElecL/4RNildYlXtLuQYjHWEA3UrNCKffNTi+JX7m6gL3UgNwGqCjUDihTdHRlAWq9HQz32lYz2D5CNPUP0emGzJlmKIr7mnfkpKDDBvkS0gQ8fViQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MAVKP2q8; arc=none smtp.client-ip=192.198.163.7 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.b="MAVKP2q8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041785; x=1811577785; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pR2poulYHn6VOTSmZqyeJ9hNbKOoQfLf5cKqhyIl+hc=; b=MAVKP2q8deLRjf3jHjjVSURqYW5boeziuo3tmEwbN4SxnIcRDlLF+J3g c3INiFE0kMN6Mui0mstJGF6dRp2pYasXclffGBr5rMd3ulXA2ixSgQzTj /ClKqVq7EpPoBuLR8gkYW63QXztHiO7Cw1TcghIT7y/5leygUmVtNqE2f HShY3CsZL2ijZ8Buw+pVsAOvuN1xsySkVIRo1TfErAWBE4bwPwPlgu2vZ Eshu4GLqEocGFFc9002ruvq8uke2qJJVAfikYaRuIuIgVlTHVzxDYFr8V DM8dnDdCaNifAhhUQ96kX3SaNVzbXqPjJsyIvlhRcbMSGrBgnacpytPX0 Q==; X-CSE-ConnectionGUID: ghi6lCx7SR6pZM34hY79Mw== X-CSE-MsgGUID: oBTcOCCCQLGMcbD+Dqxw2Q== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341902" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341902" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:05 -0700 X-CSE-ConnectionGUID: WaalXZmqQt+jdruMlKt3bw== X-CSE-MsgGUID: 2xSxdNiBQoqGr9B44k6bWg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802023" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:00 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 06/23] perf/x86: Use x86_perf_regs in the x86 nmi handlers Date: Fri, 29 May 2026 15:56:28 +0800 Message-Id: <20260529075645.580362-7-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" More and more regs will be supported in the overflow, e.g., more vector registers, SSP, etc. The generic pt_regs struct cannot store all of them. Use a X86 specific x86_perf_regs instead. The struct pt_regs *regs is still passed to x86_pmu_handle_irq(). There is no functional change for the existing code. AMD IBS's NMI handler doesn't utilize the static call x86_pmu_handle_irq(). The x86_perf_regs struct doesn't apply to the AMD IBS. It can be added separately later when AMD IBS supports more regs. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- V8: Use x86_perf_regs for xen_pmu_irq_handler() as well. arch/x86/events/core.c | 5 ++++- arch/x86/xen/pmu.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 17e122e27e0b..17c8f44ee43b 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -1788,9 +1788,11 @@ void perf_put_guest_lvtpc(void) EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc); #endif /* CONFIG_PERF_GUEST_MEDIATED_PMU */ =20 +static DEFINE_PER_CPU(struct x86_perf_regs, x86_intr_regs); static int perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs) { + struct x86_perf_regs *x86_regs =3D this_cpu_ptr(&x86_intr_regs); u64 start_clock; u64 finish_clock; int ret; @@ -1814,7 +1816,8 @@ perf_event_nmi_handler(unsigned int cmd, struct pt_re= gs *regs) return NMI_DONE; =20 start_clock =3D sched_clock(); - ret =3D static_call(x86_pmu_handle_irq)(regs); + x86_regs->regs =3D *regs; + ret =3D static_call(x86_pmu_handle_irq)(&x86_regs->regs); finish_clock =3D sched_clock(); =20 perf_sample_event_took(finish_clock - start_clock); diff --git a/arch/x86/xen/pmu.c b/arch/x86/xen/pmu.c index 8f89ce0b67e3..de3d7d391a5e 100644 --- a/arch/x86/xen/pmu.c +++ b/arch/x86/xen/pmu.c @@ -455,12 +455,14 @@ static void xen_convert_regs(const struct xen_pmu_reg= s *xen_regs, } } =20 +static DEFINE_PER_CPU(struct x86_perf_regs, x86_xen_intr_regs); irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id) { int err, ret =3D IRQ_NONE; struct pt_regs regs =3D {0}; const struct xen_pmu_data *xenpmu_data =3D get_xenpmu_data(); uint8_t xenpmu_flags =3D get_xenpmu_flags(); + struct x86_perf_regs *x86_regs =3D this_cpu_ptr(&x86_xen_intr_regs); =20 if (!xenpmu_data) { pr_warn_once("%s: pmudata not initialized\n", __func__); @@ -471,7 +473,8 @@ irqreturn_t xen_pmu_irq_handler(int irq, void *dev_id) xenpmu_flags | XENPMU_IRQ_PROCESSING; xen_convert_regs(&xenpmu_data->pmu.r.regs, ®s, xenpmu_data->pmu.pmu_flags); - if (x86_pmu.handle_irq(®s)) + x86_regs->regs =3D regs; + if (x86_pmu.handle_irq(&x86_regs->regs)) ret =3D IRQ_HANDLED; =20 /* Write out cached context to HW */ --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1647B3B440F; Fri, 29 May 2026 08:03:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041793; cv=none; b=raesvsG6SmfF9QB5O8nHxeDm2T+jYRQbkb8K06BtrUVqIwnPtzAUd15Me94f1TPRic/eRvuuApnAY6WWsDYlPwtodG3ONaobE66Rq+9+FJaa7rlzE1JOid6YLyXJWsTXc4/dA5fWZQsejPLFkKSUlQ1rMx0Ggg3L3NhA+mEUtfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041793; c=relaxed/simple; bh=J0sm+5qhCvCbmlY2g4qnRCwola1bhH6zLMUqFRzdQQo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sQS0oN655IQw4KdhTJDePtrwkcs1CFrnoetIzcdWZ8GOFkn81IwLPNmlZscsOrtr9PFKBBbzM37VjYtxEvuiot7QBi8ozgKVrfOqsCCbclwdivxN/VU2ndz+jhDaDU+MBfhl5R7c4VSR9MIxFi5bMF2a4GpvvjePbP8/Mcl05jo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PAtT2bEE; arc=none smtp.client-ip=192.198.163.7 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.b="PAtT2bEE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041792; x=1811577792; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=J0sm+5qhCvCbmlY2g4qnRCwola1bhH6zLMUqFRzdQQo=; b=PAtT2bEEWVsYta93Kv3oTQL6VBWp0Oa58JRNxUogyTBXTvRzMhbVPc3h BlMU07KDYWE0e+l6QHKfeWgKyAHWLn3cOV0etyh/tKF8uKnbtBx8qwzd8 EIjITyVzMsarD5qjGlBpEE//wg5U4r77UhtAoknwuwfF+twfC1i1JPwBT s6Vr9+N76ST4Q6gX38ixAQBvP3LQYi0tmpUYcwJQpW7yzYvqWzHc5HpY2 SE9XTrjrvQFYTC/z1A37sfPQfENteI+i/0eyYScY6tHV1iFnQpGnLlpmf BZhwupizIWHki5vVW5hgNdV9In9GIcxVY/pQwjaGli4ior4+EHrluORHY g==; X-CSE-ConnectionGUID: 5pEFT+AUQVO3aIzdp9w7Uw== X-CSE-MsgGUID: M2OJoUpXSziUFEsiw7p02w== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341921" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341921" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:10 -0700 X-CSE-ConnectionGUID: gZI0IgoVRYS0HTq0EWeI6Q== X-CSE-MsgGUID: kBwA1Cb2TE6AzhwuiTWbKg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802054" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:05 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Kan Liang , Dapeng Mi Subject: [Patch v8 07/23] x86/fpu/xstate: Add xsaves_nmi() helper Date: Fri, 29 May 2026 15:56:29 +0800 Message-Id: <20260529075645.580362-8-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kan Liang Add xsaves_nmi() to save supported xsave states in NMI handler. This function is similar to xsaves(), but should only be called within a NMI handler. This function returns the actual register contents at the moment the NMI occurs. Currently the perf subsystem is the sole user of this helper. It uses this function to snapshot SIMD (XMM/YMM/ZMM) and APX eGPRs registers which would be added in subsequent patches. Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/include/asm/fpu/xstate.h | 1 + arch/x86/kernel/fpu/xstate.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/x= state.h index 7a7dc9d56027..38fa8ff26559 100644 --- a/arch/x86/include/asm/fpu/xstate.h +++ b/arch/x86/include/asm/fpu/xstate.h @@ -110,6 +110,7 @@ int xfeature_size(int xfeature_nr); =20 void xsaves(struct xregs_state *xsave, u64 mask); void xrstors(struct xregs_state *xsave, u64 mask); +void xsaves_nmi(struct xregs_state *xsave, u64 mask); =20 int xfd_enable_feature(u64 xfd_err); =20 diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index a7b6524a9dea..4394091c4791 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1474,6 +1474,29 @@ void xrstors(struct xregs_state *xstate, u64 mask) WARN_ON_ONCE(err); } =20 +/** + * xsaves_nmi - Save selected components to a kernel xstate buffer in NMI + * @xstate: Pointer to the buffer + * @mask: Feature mask to select the components to save + * + * This function is similar to xsaves(), but should only be called within + * a NMI handler. This function returns the actual register contents at + * the moment the NMI occurs. + * + * Currently, the perf subsystem is the sole user of this helper. It uses + * the function to snapshot SIMD (XMM/YMM/ZMM) and APX eGPRs registers. + */ +void xsaves_nmi(struct xregs_state *xstate, u64 mask) +{ + int err; + + if (!in_nmi()) + return; + + XSTATE_OP(XSAVES, xstate, (u32)mask, (u32)(mask >> 32), err); + WARN_ON_ONCE(err); +} + #if IS_ENABLED(CONFIG_KVM) void fpstate_clear_xstate_component(struct fpstate *fpstate, unsigned int = xfeature) { --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19B1D3B5E01; Fri, 29 May 2026 08:03:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041796; cv=none; b=LV6nMNawJpk+i2ygJ2Am0UjIE1bldWrryaVYQESR7nUEocrCN9lgo1YfytV3NXyT5r2vVLqq4vrCybu4DPUQW14zILsVxIop8Nn/RaX+uwh1LOGC+rpyrzUZb2acWRUzTPD3CKQfZQcY0lC7u4+iPHRNT38uszAl8MRbiweq98U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041796; c=relaxed/simple; bh=u769g9Miy1Dv9v+Lug92+pVXVhRuV3mC/HCmdG44lYU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E4ETiN//Ta7wN8KtFZ/TJQP/ZPs60qC4QxWNv+Ma7zuf8MPl2MqmahGqhw82zkVCIpn2gxb3G8Whd3Nx59pVhd76roRG1RJR4XyAuA9gOlYtYODd+Gl4NLS676wOb1cJ7Oq2680OubANS5P7GvTzq+ygRsS/4RePeDjCB75lTf0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=NgQhNjLz; arc=none smtp.client-ip=192.198.163.7 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.b="NgQhNjLz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041795; x=1811577795; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=u769g9Miy1Dv9v+Lug92+pVXVhRuV3mC/HCmdG44lYU=; b=NgQhNjLzCXvnNvcusVWa6aX9nRNQV9t0cY0RWKKXrqWhX0yLjZbYO3to bULAG6Q9gtHvfvGdWlydsk+NyFZVO+Ij9DweBHLsv4Pavw379tO/IleEr z49yl26ld0rILvtXAK7vo7Px3Q21CU2G3crJetf2Xh3wLuQMiPZgA0mA/ i5NoO3+OaJlXDhJyRpdso08Gk50T7BVHvMxNk7qrfrHvl5rau0UNHUyv6 YrjX3Lv1WRlnzZvVHqLryk1lzCwqicZcuGsVcFjqF10Hi/pNxxbVRXImn sKvMaH8c2eiHC0oA8avwG0BJoz2R2PKcrh1jNVQWAENv3tf2ZbKlHy9in w==; X-CSE-ConnectionGUID: gauFmsXpSValinchPKwT7w== X-CSE-MsgGUID: a/EPGTYFQdmYd9I5T8u4OA== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341936" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341936" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:15 -0700 X-CSE-ConnectionGUID: 7HsYs+JGQIaUhAxpcg6NLw== X-CSE-MsgGUID: l5mW+HHMQoqIUptTBLSrQw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802110" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:10 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 08/23] x86/fpu: Ensure TIF_NEED_FPU_LOAD is set after saving FPU state Date: Fri, 29 May 2026 15:56:30 +0800 Message-Id: <20260529075645.580362-9-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Following Peter and Dave's suggestion, Ensure that the TIF_NEED_FPU_LOAD flag is always set after saving the FPU state. This guarantees that the user space FPU state has been saved whenever the TIF_NEED_FPU_LOAD flag is set. A subsequent patch will verify if the user space FPU state can be retrieved from the saved task FPU state in the NMI context by checking the TIF_NEED_FPU_LOAD flag. Please check the below link to get more background about the suggestion. Suggested-by: Peter Zijlstra Link: https://lore.kernel.org/all/20251204154721.GB2619703@noisy.programmin= g.kicks-ass.net/ Signed-off-by: Dapeng Mi --- arch/x86/include/asm/fpu/sched.h | 5 +++-- arch/x86/kernel/fpu/core.c | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/fpu/sched.h b/arch/x86/include/asm/fpu/sc= hed.h index 89004f4ca208..dcb2fa5f06d6 100644 --- a/arch/x86/include/asm/fpu/sched.h +++ b/arch/x86/include/asm/fpu/sched.h @@ -10,6 +10,8 @@ #include =20 extern void save_fpregs_to_fpstate(struct fpu *fpu); +extern void update_fpu_state_and_flag(struct fpu *fpu, + struct task_struct *task); extern void fpu__drop(struct task_struct *tsk); extern int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minim= al, unsigned long shstk_addr); @@ -36,8 +38,7 @@ static inline void switch_fpu(struct task_struct *old, in= t cpu) !(old->flags & (PF_KTHREAD | PF_USER_WORKER))) { struct fpu *old_fpu =3D x86_task_fpu(old); =20 - set_tsk_thread_flag(old, TIF_NEED_FPU_LOAD); - save_fpregs_to_fpstate(old_fpu); + update_fpu_state_and_flag(old_fpu, old); /* * The save operation preserved register state, so the * fpu_fpregs_owner_ctx is still @old_fpu. Store the diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 608983806fd7..48d1ab50a961 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -213,6 +213,19 @@ void restore_fpregs_from_fpstate(struct fpstate *fpsta= te, u64 mask) } } =20 +/* + * Save the FPU register state in fpu->fpstate->regs and set + * TIF_NEED_FPU_LOAD subsequently. + * + * Must be called with fpregs_lock() held, ensuring flag + * TIF_NEED_FPU_LOAD is set last. + */ +void update_fpu_state_and_flag(struct fpu *fpu, struct task_struct *task) +{ + save_fpregs_to_fpstate(fpu); + set_tsk_thread_flag(task, TIF_NEED_FPU_LOAD); +} + void fpu_reset_from_exception_fixup(void) { restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_FPSTATE); @@ -379,17 +392,19 @@ int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu,= bool enter_guest) =20 fpregs_lock(); if (!cur_fps->is_confidential && !test_thread_flag(TIF_NEED_FPU_LOAD)) - save_fpregs_to_fpstate(fpu); + update_fpu_state_and_flag(fpu, current); =20 /* Swap fpstate */ if (enter_guest) { - fpu->__task_fpstate =3D cur_fps; + WRITE_ONCE(fpu->__task_fpstate, cur_fps); + barrier(); fpu->fpstate =3D guest_fps; guest_fps->in_use =3D true; } else { guest_fps->in_use =3D false; fpu->fpstate =3D fpu->__task_fpstate; - fpu->__task_fpstate =3D NULL; + barrier(); + WRITE_ONCE(fpu->__task_fpstate, NULL); } =20 cur_fps =3D fpu->fpstate; @@ -481,10 +496,8 @@ void kernel_fpu_begin_mask(unsigned int kfpu_mask) this_cpu_write(kernel_fpu_allowed, false); =20 if (!(current->flags & (PF_KTHREAD | PF_USER_WORKER)) && - !test_thread_flag(TIF_NEED_FPU_LOAD)) { - set_thread_flag(TIF_NEED_FPU_LOAD); - save_fpregs_to_fpstate(x86_task_fpu(current)); - } + !test_thread_flag(TIF_NEED_FPU_LOAD)) + update_fpu_state_and_flag(x86_task_fpu(current), current); __cpu_invalidate_fpregs_state(); =20 /* Put sane initial values into the control registers. */ --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13BFC3B2FDB; Fri, 29 May 2026 08:03:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041801; cv=none; b=XVkmWBPIpR6KmWvu3GWOZq86oVHqeOwco+43rR82dMENfAx3Vjn5o1J9uR6nq3HxJh+0roQekzd+Kij1yG49aYyAip1L68h5+gzjNKs9Hu3KyA1htlM7BEdFAV1aMVVanEtA95jtH3VkNb/KpmBwjPIJFrzSL5PJLBVrVrgL0QU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041801; c=relaxed/simple; bh=QcZxr/t2d5aOuMpifMfhoT20FIpK5G/CW4f704Y+Z2A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tqXAY3fvwE60MB7Z+5DvXvWhhdLBEL3zBD/fxDjnh/hTehFHbYya5vJ0WN1xrX31DOaO9db7htEVTRP93QSGpQt+C54eUjo3YCEp8r5D+zLUUSPrKEJnr4oo57xdmAFZdZ/LdSVXsKjvnaHPPjCdvAu2+diMfgYJEKjsGN7u3ZU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=M/LAjoNs; arc=none smtp.client-ip=192.198.163.7 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.b="M/LAjoNs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041800; x=1811577800; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QcZxr/t2d5aOuMpifMfhoT20FIpK5G/CW4f704Y+Z2A=; b=M/LAjoNshywTo1MX8LfPTAAEJ1q6XjYr6f6wspoE/XMnqDIUcgP0Q8C8 FlLwuIZgpfE8Y0C0x5YGrlfoihhD4vIU4chwVURJIrzuvS/4fXkFvVia8 I4XvEo2MLtqmbCMRVR0+ZwL72ryiCefsmj18Iv6I/jJhb+fBFm4ySK/LJ qj2rnrMBez6G2+v0+zR46zkHS/Xmv5VQ8pJsS1G1RYuP/9saFQPVleC87 1Q1kJ1UfhjugI1VmAU0b3edv4Vro27GaPoSgTVxxtxSb6bvGXl7eLcicB Ilk92jIubVWQ21vn0jXuqyqEHBHyFWi8O5TDhuy5MA/ZjOZnTWJH9iiZG w==; X-CSE-ConnectionGUID: habs1rBCQgWiqcYhPuBqZQ== X-CSE-MsgGUID: PMfDfINmS3KAliahQ3kKBg== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341949" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341949" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:20 -0700 X-CSE-ConnectionGUID: m4yYZEMXS/uaAPBaD1Pz3w== X-CSE-MsgGUID: F4lw46IIQNSnoSoIoEmxVQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802172" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:15 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Kan Liang , Dapeng Mi Subject: [Patch v8 09/23] perf: Move and enhance has_extended_regs() for arch-specific use Date: Fri, 29 May 2026 15:56:31 +0800 Message-Id: <20260529075645.580362-10-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kan Liang Move has_extended_regs() to include/linux/perf_event.h so it can be used by arch-specific code. While moving it, enhance the check logic and rename it to event_has_extended_regs() to match existing perf event helper naming. Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- include/linux/perf_event.h | 10 ++++++++++ kernel/events/core.c | 8 +------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 48d851fbd8ea..fb38affa7352 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1534,6 +1534,16 @@ perf_event__output_id_sample(struct perf_event *even= t, extern void perf_log_lost_samples(struct perf_event *event, u64 lost); =20 +static inline bool event_has_extended_regs(struct perf_event *event) +{ + struct perf_event_attr *attr =3D &event->attr; + + return ((attr->sample_type & PERF_SAMPLE_REGS_USER) && + (attr->sample_regs_user & PERF_REG_EXTENDED_MASK)) || + ((attr->sample_type & PERF_SAMPLE_REGS_INTR) && + (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK)); +} + static inline bool event_has_any_exclude_flag(struct perf_event *event) { struct perf_event_attr *attr =3D &event->attr; diff --git a/kernel/events/core.c b/kernel/events/core.c index fedc0cf65f9e..2ce553db4dcb 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -13056,12 +13056,6 @@ int perf_pmu_unregister(struct pmu *pmu) } EXPORT_SYMBOL_GPL(perf_pmu_unregister); =20 -static inline bool has_extended_regs(struct perf_event *event) -{ - return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) || - (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK); -} - static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) { struct perf_event_context *ctx =3D NULL; @@ -13096,7 +13090,7 @@ static int perf_try_init_event(struct pmu *pmu, str= uct perf_event *event) goto err_pmu; =20 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && - has_extended_regs(event)) { + event_has_extended_regs(event)) { ret =3D -EOPNOTSUPP; goto err_destroy; } --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25CD53ACF1E; Fri, 29 May 2026 08:03:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041809; cv=none; b=PBjFjiteZjRtMW9/ahh1vLJY4/jB/jsZYAg7hcPJuSVmwcKkzVJLhL4AVdbZZkbDb4AdD0+LpYBVFKvRVJdbXvn2HnpFXiRWQjxQ77YXabiKeIm2S2V2ArQSQfi+Gpgxs6rIgZ1P7f9jgjW8DiRFlzRxtczcoq+CFOjN+MKxhbA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041809; c=relaxed/simple; bh=iwKXwAGrV1yvP2eoGd3UgKQ1x/hGBjKKhT57vhLMLRo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YRdI75xoqspKVLTuUozfr3YPMVY3r3eDdQAmhqVUp4+SLkdlx+eep1kbJXXJuTxnTLs6RkhsqVWLHOJH5jF1osxBau2WLLaIppm356Y2FdxhoMzIfht5a3f1s/lp6cI3wJEJkaV0O5rlcNUSJmXubF3IO0WlgHorgcV6AFVnW6s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ByNX6/re; arc=none smtp.client-ip=192.198.163.7 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.b="ByNX6/re" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041808; x=1811577808; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iwKXwAGrV1yvP2eoGd3UgKQ1x/hGBjKKhT57vhLMLRo=; b=ByNX6/re9al9PuP0r1W40nBmgBCyURFfGmfsg2EOoFoTcT+NzzS2kIpA 7qGP6Omlcg2GKddlYRgndV5LVerxVQG6s+ptUeK/Uy2MasqNfvGtzvtQl NTlOzWF4oZ3TJ/REBeoyUtPzHsRn6mDYEkzcjlt9mBWkCrpMp9H5Hvus7 wZY4Qoo+M641FOiFICdZfiQgeBbx9FDJ3+kQOKBhKJ1xCNhZkafWawjyK bx9zJWoVtKvkffnJB+sFqK8ghrvT2xqe2gTA5HybI0xdr21N0F5JJ9g13 LX/2tTxVa2D9VnaAEUi2GyibeN/g9+PS2rrsns6gebgX3HvMJs1yA2v3f g==; X-CSE-ConnectionGUID: +Hhop1LFTGmxH/pwpVWFjw== X-CSE-MsgGUID: CpElpEufTn2OrV880Qq/Ug== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341970" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341970" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:27 -0700 X-CSE-ConnectionGUID: i1JGtnOfRQ2QYWOoW9VQzA== X-CSE-MsgGUID: uSi5ZjqpR1KolpzmSKSe/A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802220" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:20 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 10/23] perf/x86: Enable XMM Register Sampling for Non-PEBS Events Date: Fri, 29 May 2026 15:56:32 +0800 Message-Id: <20260529075645.580362-11-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Previously, XMM register sampling was only available for PEBS events starting from Icelake. Currently the support is now extended to non-PEBS events by utilizing the xsaves instruction, thereby completing the feature set. To implement this, a 64-byte aligned buffer is required. A per-CPU ext_regs_buf is introduced to store SIMD and other registers, with an approximate size of 2K. The buffer is allocated using kzalloc_node(), ensuring natural and 64-byte alignment for all kmalloc() allocations with powers of 2. XMM sampling for non-PEBS events is supported in the REGS_INTR case. Support for REGS_USER will be added in a subsequent patch. For PEBS events, XMM register sampling data is directly retrieved from PEBS records. Future support for additional vector registers (YMM/ZMM/OPMASK) is planned. An `ext_regs_mask` is added to track the supported vector register groups. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 181 ++++++++++++++++++++++++++++-- arch/x86/events/intel/core.c | 36 +++++- arch/x86/events/intel/ds.c | 18 ++- arch/x86/events/perf_event.h | 13 +++ arch/x86/include/asm/fpu/xstate.h | 2 + arch/x86/include/asm/perf_event.h | 5 +- arch/x86/kernel/fpu/xstate.c | 2 +- 7 files changed, 240 insertions(+), 17 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 17c8f44ee43b..c219a563434d 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -410,6 +410,56 @@ set_ext_hw_attr(struct hw_perf_event *hwc, struct perf= _event *event) return x86_pmu_extra_regs(val, event); } =20 +static DEFINE_PER_CPU(struct xregs_state *, ext_regs_buf); + +static void release_ext_regs_buffers(void) +{ + int cpu; + + if (!x86_pmu.ext_regs_mask) + return; + + for_each_possible_cpu(cpu) { + kfree(per_cpu(ext_regs_buf, cpu)); + per_cpu(ext_regs_buf, cpu) =3D NULL; + } +} + +static void reserve_ext_regs_buffers(void) +{ + bool compacted =3D cpu_feature_enabled(X86_FEATURE_XCOMPACTED); + unsigned int size; + int cpu; + + if (!x86_pmu.ext_regs_mask) + return; + + /* +64 bytes for the 64 bytes alignment request of xsave area. */ + size =3D xstate_calculate_size(x86_pmu.ext_regs_mask, compacted) + 64; + + for_each_possible_cpu(cpu) { + per_cpu(ext_regs_buf, cpu) =3D kzalloc_node(size, GFP_KERNEL, + cpu_to_node(cpu)); + if (WARN_ON_ONCE(!per_cpu(ext_regs_buf, cpu))) + goto err; + } + + return; + +err: + release_ext_regs_buffers(); +} + +static inline struct xregs_state *get_ext_regs_buf(int cpu) +{ + void *buf =3D per_cpu(ext_regs_buf, cpu); + struct xregs_state *xsave; + + xsave =3D buf ? PTR_ALIGN(buf, 64) : NULL; + + return xsave; +} + int x86_reserve_hardware(void) { int err =3D 0; @@ -422,6 +472,7 @@ int x86_reserve_hardware(void) } else { reserve_ds_buffers(); reserve_lbr_buffers(); + reserve_ext_regs_buffers(); } } if (!err) @@ -438,6 +489,7 @@ void x86_release_hardware(void) release_pmc_hardware(); release_ds_buffers(); release_lbr_buffers(); + release_ext_regs_buffers(); mutex_unlock(&pmc_reserve_mutex); } } @@ -655,18 +707,26 @@ int x86_pmu_hw_config(struct perf_event *event) return -EINVAL; } =20 - /* sample_regs_user never support XMM registers */ - if (unlikely(event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK)) - return -EINVAL; - /* - * Besides the general purpose registers, XMM registers may - * be collected in PEBS on some platforms, e.g. Icelake - */ - if (unlikely(event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK)) { - if (!(event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS)) - return -EINVAL; + if (event->attr.sample_type & PERF_SAMPLE_REGS_INTR) { + /* + * Besides the general purpose registers, XMM registers may + * be collected as well. + */ + if (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK) { + if (!(event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS)) + return -EINVAL; + if (is_sampling_event(event) && !event->attr.precise_ip && + !this_cpu_has(X86_FEATURE_XSAVES)) + return -EINVAL; + } + } =20 - if (!event->attr.precise_ip) + if (event->attr.sample_type & PERF_SAMPLE_REGS_USER) { + /* + * Currently XMM registers sampling for REGS_USER is not + * supported yet. + */ + if (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) return -EINVAL; } =20 @@ -1705,6 +1765,105 @@ static void x86_pmu_del(struct perf_event *event, i= nt flags) static_call_cond(x86_pmu_del)(event); } =20 +void x86_pmu_clear_perf_regs(struct pt_regs *regs) +{ + struct x86_perf_regs *perf_regs =3D container_of(regs, struct x86_perf_re= gs, regs); + + perf_regs->xmm_regs =3D NULL; +} + +static void update_perf_regs(struct x86_perf_regs *perf_regs, + struct xregs_state *xsave, u64 bitmap) +{ + u64 mask; + + if (!xsave) + return; + + /* Filtered by what XSAVE really gives */ + mask =3D bitmap & xsave->header.xfeatures; + + if (mask & XFEATURE_MASK_SSE) + perf_regs->xmm_space =3D xsave->i387.xmm_space; +} + +/* + * The x86 specific variant of perf_sample_regs_intr(). + * It would be extended to add more SIMD registers sampling support + * in later patches. + */ +static void x86_pmu_update_regs_intr(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + data->regs_intr.regs =3D regs; + data->regs_intr.abi =3D perf_reg_abi(current); + + data->dyn_size +=3D sizeof(u64); + if (data->regs_intr.regs) { + data->dyn_size +=3D hweight64(event->attr.sample_regs_intr) * + sizeof(u64); + } + + /* + * Set PERF_SAMPLE_REGS_INTR to bypass perf_sample_regs_intr() call + * in perf_prepare_sample() function. + */ + data->sample_flags |=3D PERF_SAMPLE_REGS_INTR; +} + +static void x86_pmu_sample_xregs(struct perf_event *event, + struct perf_sample_data *data, + u64 ignore_mask) +{ + struct xregs_state *xsave =3D get_ext_regs_buf(smp_processor_id()); + u64 sample_type =3D event->attr.sample_type; + struct x86_perf_regs *perf_regs; + u64 intr_mask =3D 0; + u64 mask =3D 0; + + if (WARN_ON_ONCE(!xsave) || !in_nmi()) + return; + + if ((sample_type & PERF_SAMPLE_REGS_INTR) && + (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK)) + mask |=3D XFEATURE_MASK_SSE; + + mask &=3D x86_pmu.ext_regs_mask; + + if (sample_type & PERF_SAMPLE_REGS_INTR) + intr_mask =3D mask & ~ignore_mask; + + if (intr_mask) { + perf_regs =3D container_of(data->regs_intr.regs, + struct x86_perf_regs, regs); + xsave->header.xfeatures =3D 0; + xsaves_nmi(xsave, mask); + update_perf_regs(perf_regs, xsave, intr_mask); + } +} + +void x86_pmu_update_perf_regs(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs, + u64 ignore_mask) +{ + u64 sample_type =3D event->attr.sample_type; + + if (!((sample_type & PERF_SAMPLE_REGS_INTR) && + (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK))) + return; + + if (sample_type & PERF_SAMPLE_REGS_INTR) + x86_pmu_update_regs_intr(event, data, regs); + + /* + * ignore_mask indicates the PEBS sampled extended regs + * which are unnecessary to sample again. + */ + x86_pmu_sample_xregs(event, data, ignore_mask); +} + int x86_pmu_handle_irq(struct pt_regs *regs) { struct perf_sample_data data; diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 92cb9a716e83..f5d458e3ba3f 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3928,6 +3928,9 @@ static int handle_pmi_common(struct pt_regs *regs, u6= 4 status) if (has_branch_stack(event)) intel_pmu_lbr_save_brstack(&data, cpuc, event); =20 + x86_pmu_clear_perf_regs(regs); + x86_pmu_update_perf_regs(event, &data, regs, 0); + perf_event_overflow(event, &data, regs); } =20 @@ -6176,8 +6179,37 @@ static inline void __intel_update_large_pebs_flags(s= truct pmu *pmu) } } =20 -#define counter_mask(_gp, _fixed) ((_gp) | ((u64)(_fixed) << INTEL_PMC_IDX= _FIXED)) +static void intel_extended_regs_init(struct pmu *pmu) +{ + struct pmu *dest_pmu =3D pmu ? pmu : x86_get_pmu(smp_processor_id()); + + /* + * Extend the vector registers support to non-PEBS. + * The feature is limited to newer Intel machines with + * PEBS V4+ or archPerfmonExt (0x23) enabled for now. + * In theory, the vector registers can be retrieved as + * long as the CPU supports. The support for the old + * generations may be added later if there is a + * requirement. + * Only support the extension when XSAVES is available. + */ + if (!boot_cpu_has(X86_FEATURE_XSAVES)) + return; =20 + if (!boot_cpu_has(X86_FEATURE_XMM) || + !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL)) + return; + + /* + * On current hybrid platforms, P-cores and E-cores expose the same + * XSAVE feature set. Therefore, using the global x86_pmu.ext_regs_mask + * is sufficient to represent the hardware-supported XSAVE features. + */ + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_SSE; + dest_pmu->capabilities |=3D PERF_PMU_CAP_EXTENDED_REGS; +} + +#define counter_mask(_gp, _fixed) ((_gp) | ((u64)(_fixed) << INTEL_PMC_IDX= _FIXED)) static void update_pmu_cap(struct pmu *pmu) { unsigned int eax, ebx, ecx, edx; @@ -6241,6 +6273,8 @@ static void update_pmu_cap(struct pmu *pmu) /* Perf Metric (Bit 15) and PEBS via PT (Bit 16) are hybrid enumeration = */ rdmsrq(MSR_IA32_PERF_CAPABILITIES, hybrid(pmu, intel_cap).capabilities); } + + intel_extended_regs_init(pmu); } =20 static void intel_pmu_check_hybrid_pmus(struct x86_hybrid_pmu *pmu) diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index a31648d2adb1..4f72ce6a9585 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -2524,6 +2524,7 @@ static void setup_pebs_adaptive_sample_data(struct pe= rf_event *event, struct pebs_meminfo *meminfo =3D NULL; struct pebs_gprs *gprs =3D NULL; struct x86_perf_regs *perf_regs; + u64 ignore_mask =3D 0; u64 format_group; u16 retire; =20 @@ -2531,7 +2532,7 @@ static void setup_pebs_adaptive_sample_data(struct pe= rf_event *event, return; =20 perf_regs =3D container_of(regs, struct x86_perf_regs, regs); - perf_regs->xmm_regs =3D NULL; + x86_pmu_clear_perf_regs(regs); =20 format_group =3D basic->format_group; =20 @@ -2578,6 +2579,7 @@ static void setup_pebs_adaptive_sample_data(struct pe= rf_event *event, if (format_group & PEBS_DATACFG_XMMS) { struct pebs_xmm *xmm =3D next_record; =20 + ignore_mask |=3D XFEATURE_MASK_SSE; next_record =3D xmm + 1; perf_regs->xmm_regs =3D xmm->xmm; } @@ -2616,6 +2618,8 @@ static void setup_pebs_adaptive_sample_data(struct pe= rf_event *event, next_record +=3D nr * sizeof(u64); } =20 + x86_pmu_update_perf_regs(event, data, regs, ignore_mask); + WARN_ONCE(next_record !=3D __pebs + basic->format_size, "PEBS record size %u, expected %llu, config %llx\n", basic->format_size, @@ -2641,6 +2645,7 @@ static void setup_arch_pebs_sample_data(struct perf_e= vent *event, struct arch_pebs_aux *meminfo =3D NULL; struct arch_pebs_gprs *gprs =3D NULL; struct x86_perf_regs *perf_regs; + u64 ignore_mask =3D 0; void *next_record; void *at =3D __pebs; =20 @@ -2648,7 +2653,7 @@ static void setup_arch_pebs_sample_data(struct perf_e= vent *event, return; =20 perf_regs =3D container_of(regs, struct x86_perf_regs, regs); - perf_regs->xmm_regs =3D NULL; + x86_pmu_clear_perf_regs(regs); =20 __setup_perf_sample_data(event, iregs, data); =20 @@ -2703,6 +2708,7 @@ static void setup_arch_pebs_sample_data(struct perf_e= vent *event, =20 next_record +=3D sizeof(struct arch_pebs_xer_header); =20 + ignore_mask |=3D XFEATURE_MASK_SSE; xmm =3D next_record; perf_regs->xmm_regs =3D xmm->xmm; next_record =3D xmm + 1; @@ -2750,6 +2756,8 @@ static void setup_arch_pebs_sample_data(struct perf_e= vent *event, at =3D at + header->size; goto again; } + + x86_pmu_update_perf_regs(event, data, regs, ignore_mask); } =20 static inline void * @@ -3412,7 +3420,11 @@ static void __init intel_ds_pebs_init(void) x86_pmu.flags |=3D PMU_FL_PEBS_ALL; x86_pmu.pebs_capable =3D ~0ULL; pebs_qual =3D "-baseline"; - x86_get_pmu(smp_processor_id())->capabilities |=3D PERF_PMU_CAP_EXTEND= ED_REGS; + if (boot_cpu_has(X86_FEATURE_XSAVES)) { + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_SSE; + x86_get_pmu(smp_processor_id())->capabilities |=3D + PERF_PMU_CAP_EXTENDED_REGS; + } } else { /* Only basic record supported */ x86_pmu.large_pebs_flags &=3D diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index eae24bb35dc1..cff5fbac000b 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1028,6 +1028,12 @@ struct x86_pmu { struct extra_reg *extra_regs; unsigned int flags; =20 + /* + * Extended regs, e.g., vector registers + * Utilize the same format as the XFEATURE_MASK_* + */ + u64 ext_regs_mask; + /* * Intel host/guest support (KVM) */ @@ -1314,6 +1320,13 @@ void x86_pmu_enable_event(struct perf_event *event); =20 int x86_pmu_handle_irq(struct pt_regs *regs); =20 +void x86_pmu_clear_perf_regs(struct pt_regs *regs); + +void x86_pmu_update_perf_regs(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs, + u64 ignore_mask); + void x86_pmu_show_pmu_cap(struct pmu *pmu); =20 static inline int x86_pmu_num_counters(struct pmu *pmu) diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/x= state.h index 38fa8ff26559..19dec5f0b1c7 100644 --- a/arch/x86/include/asm/fpu/xstate.h +++ b/arch/x86/include/asm/fpu/xstate.h @@ -112,6 +112,8 @@ void xsaves(struct xregs_state *xsave, u64 mask); void xrstors(struct xregs_state *xsave, u64 mask); void xsaves_nmi(struct xregs_state *xsave, u64 mask); =20 +unsigned int xstate_calculate_size(u64 xfeatures, bool compacted); + int xfd_enable_feature(u64 xfd_err); =20 #ifdef CONFIG_X86_64 diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index 752cb319d5ea..e47a963a7cf0 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -726,7 +726,10 @@ extern void perf_events_lapic_init(void); struct pt_regs; struct x86_perf_regs { struct pt_regs regs; - u64 *xmm_regs; + union { + u64 *xmm_regs; + u32 *xmm_space; /* for xsaves */ + }; }; =20 extern unsigned long perf_arch_instruction_pointer(struct pt_regs *regs); diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 4394091c4791..4cef802c2e02 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -587,7 +587,7 @@ static bool __init check_xstate_against_struct(int nr) return true; } =20 -static unsigned int xstate_calculate_size(u64 xfeatures, bool compacted) +unsigned int xstate_calculate_size(u64 xfeatures, bool compacted) { unsigned int topmost =3D fls64(xfeatures) - 1; unsigned int offset, i; --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 220163AD507; Fri, 29 May 2026 08:03:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041812; cv=none; b=neKcdMoKKwdIJY7uiyVJSZF+zpmKLzAH26Bm614ljGr91cEDXv29YBUY5HzXN895u+/MxyNikQBcnrsLqEWUfe5kCzgNkKp76UOYkzGMrECgFzMPMgpkUZg1do9yCAsECXImZZGjOl0FQaTHANBUcMulAIYLnDXP6OoPrNJwKsQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041812; c=relaxed/simple; bh=RCv/pZl8i9VQu4VE3AsBhUxG5YGrOk0qfxx6RmMXF/0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Eu2GAIoid/mMYvLWscky1/uANAFVobUL3Jj7kpbZ6CUSkdyGPoS2WMB2Oy6IZwLGJypt3nWPI5Cyde3BpTGV5Kuvk+FrpFyiu0GECAoiKJlPXwVxO3KXGdC/ceIhaBQNJ3QEyQ6fOWXcbQZuz11IeHhqSH3IPhGC1WE3HODSzpY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Nf6TtNa/; arc=none smtp.client-ip=192.198.163.7 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.b="Nf6TtNa/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041811; x=1811577811; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RCv/pZl8i9VQu4VE3AsBhUxG5YGrOk0qfxx6RmMXF/0=; b=Nf6TtNa/adIc0G7gQ0BNRf+rw202wNelevjjUzlvRfqOLmxungHOHyOD zqzqQq48n9QQOqLYOuMKbJxNc69sGKMNyzO+rrc9xBldejEWVC+1/FY7J VDIXcoNcZAr6DyAAjY5rjnputAY6TM9myPqD8LcB5OZApdPENK22E8dNl nEyuNpKeDkln3oyXxZaDFEbsjl2iuK97ykFVzwBMGwFWUSahM5MLKghe2 FBnFv6DRfoaM+lP/3+SEk7RL2jg+Vx2hoTBzG+eMhHxD8vtVMrlEK8++2 2mDeHWp24IsnRAarZcRzvmYJQhp/sdI0YcNsxGHC8OlNYcP73+Vyz4iNe g==; X-CSE-ConnectionGUID: 3NSUI2KYQ/uzaFPgUC2bIg== X-CSE-MsgGUID: 2l07Muo8Qeak8GolzUq54A== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106341979" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106341979" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:31 -0700 X-CSE-ConnectionGUID: hucRXOpwQ02kii4OG1TwjQ== X-CSE-MsgGUID: EJPbeZB0Td+4wmZpArolKA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802256" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:26 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 11/23] perf/x86: Enable XMM register sampling for REGS_USER case Date: Fri, 29 May 2026 15:56:33 +0800 Message-Id: <20260529075645.580362-12-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds support for XMM register sampling in the REGS_USER case. To handle simultaneous sampling of XMM registers for both REGS_INTR and REGS_USER cases, a per-CPU `x86_user_regs` is introduced to store REGS_USER-specific XMM registers. This prevents REGS_USER-specific XMM register data from being overwritten by REGS_INTR-specific data if they share the same `x86_perf_regs` structure. To sample user-space XMM registers, the `x86_pmu_update_user_xregs()` helper function is added. It checks if the `TIF_NEED_FPU_LOAD` flag is set. If so, the user-space XMM register data can be directly retrieved from the cached task FPU state, as the corresponding hardware registers have been cleared or switched to kernel-space data. Otherwise, the data must be read from the hardware registers using the `xsaves` instruction. For PEBS events, `x86_pmu_update_user_xregs()` checks if the PEBS-sampled XMM register data belongs to user-space. If so, no further action is needed. Otherwise, the user-space XMM register data needs to be re-sampled using the same method as for non-PEBS events. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 150 ++++++++++++++++++++++++++++++----- arch/x86/events/intel/core.c | 6 +- arch/x86/events/intel/ds.c | 5 +- 3 files changed, 138 insertions(+), 23 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index c219a563434d..f9e3f349b69a 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -707,12 +707,12 @@ int x86_pmu_hw_config(struct perf_event *event) return -EINVAL; } =20 - if (event->attr.sample_type & PERF_SAMPLE_REGS_INTR) { + if (event->attr.sample_type & (PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_U= SER)) { /* * Besides the general purpose registers, XMM registers may * be collected as well. */ - if (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK) { + if (event_has_extended_regs(event)) { if (!(event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS)) return -EINVAL; if (is_sampling_event(event) && !event->attr.precise_ip && @@ -721,15 +721,6 @@ int x86_pmu_hw_config(struct perf_event *event) } } =20 - if (event->attr.sample_type & PERF_SAMPLE_REGS_USER) { - /* - * Currently XMM registers sampling for REGS_USER is not - * supported yet. - */ - if (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) - return -EINVAL; - } - return x86_setup_perfctr(event); } =20 @@ -1812,33 +1803,155 @@ static void x86_pmu_update_regs_intr(struct perf_e= vent *event, data->sample_flags |=3D PERF_SAMPLE_REGS_INTR; } =20 +/* + * When both PERF_SAMPLE_REGS_INTR and PERF_SAMPLE_REGS_USER are set, + * an additional x86_perf_regs is required to save user-space registers. + * Without this, user-space register data may be overwritten by kernel-spa= ce + * registers. + */ +static DEFINE_PER_CPU(struct x86_perf_regs, x86_user_regs); +static void x86_pmu_get_regs_user(struct perf_sample_data *data, + struct pt_regs *regs) +{ + struct x86_perf_regs *x86_regs_user =3D this_cpu_ptr(&x86_user_regs); + struct perf_regs regs_user; + + x86_pmu_clear_perf_regs(&x86_regs_user->regs); + + perf_get_regs_user(®s_user, regs); + data->regs_user.abi =3D regs_user.abi; + if (regs_user.regs) { + x86_regs_user->regs =3D *regs_user.regs; + data->regs_user.regs =3D &x86_regs_user->regs; + } else + data->regs_user.regs =3D NULL; +} + +/* + * The x86 specific variant of perf_sample_regs_user(). + * Update data->regs_user fields for extended registers (e.g., SIMD). + */ +static void x86_pmu_update_regs_user(struct perf_event *event, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + struct perf_event_attr *attr =3D &event->attr; + + if (user_mode(regs)) { + data->regs_user.abi =3D perf_reg_abi(current); + data->regs_user.regs =3D regs; + } else if (is_user_task(current)) { + /* + * It cannot guarantee that the kernel will never + * touch the registers outside of the pt_regs, + * especially when more and more registers + * (e.g., SIMD, eGPR) are added. The live data + * cannot be used. + */ + x86_pmu_get_regs_user(data, regs); + } else { + data->regs_user.abi =3D PERF_SAMPLE_REGS_ABI_NONE; + data->regs_user.regs =3D NULL; + } + + data->dyn_size +=3D sizeof(u64); + if (data->regs_user.regs) + data->dyn_size +=3D hweight64(attr->sample_regs_user) * sizeof(u64); + + /* + * Set PERF_SAMPLE_REGS_USER to bypass perf_sample_regs_user() call + * in perf_prepare_sample() function. + */ + data->sample_flags |=3D PERF_SAMPLE_REGS_USER; +} + +/* + * This function retrieves cached user-space fpu registers (XMM/YMM/ZMM). + * If TIF_NEED_FPU_LOAD is set, it indicates that the user-space FPU state + * is cached. Otherwise, the data should be read directly from the hardware + * registers. + */ +static inline u64 x86_pmu_update_user_xregs(struct perf_sample_data *data, + struct pt_regs *regs, + u64 mask, u64 ignore_mask) +{ + struct x86_perf_regs *perf_regs; + struct xregs_state *xsave; + struct fpu *fpu; + struct fpstate *fps; + u64 user_mask =3D mask; + + if (data->regs_user.abi =3D=3D PERF_SAMPLE_REGS_ABI_NONE) + return 0; + + /* + * If PEBS hits kernel space, need to re-sample extended + * registers for user space. + */ + if (user_mode(regs)) + user_mask &=3D ~ignore_mask; + + if (user_mask && test_thread_flag(TIF_NEED_FPU_LOAD)) { + perf_regs =3D container_of(data->regs_user.regs, + struct x86_perf_regs, regs); + fpu =3D x86_task_fpu(current); + /* + * If __task_fpstate is set, it holds the right pointer, + * otherwise fpstate will. + */ + fps =3D READ_ONCE(fpu->__task_fpstate); + if (!fps) + fps =3D fpu->fpstate; + xsave =3D &fps->regs.xsave; + + update_perf_regs(perf_regs, xsave, user_mask); + return 0; + } + + return user_mask; +} + static void x86_pmu_sample_xregs(struct perf_event *event, struct perf_sample_data *data, + struct pt_regs *regs, u64 ignore_mask) { struct xregs_state *xsave =3D get_ext_regs_buf(smp_processor_id()); u64 sample_type =3D event->attr.sample_type; struct x86_perf_regs *perf_regs; + u64 user_mask =3D 0; u64 intr_mask =3D 0; u64 mask =3D 0; =20 if (WARN_ON_ONCE(!xsave) || !in_nmi()) return; =20 - if ((sample_type & PERF_SAMPLE_REGS_INTR) && - (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK)) + if (event_has_extended_regs(event)) mask |=3D XFEATURE_MASK_SSE; =20 mask &=3D x86_pmu.ext_regs_mask; + if (sample_type & PERF_SAMPLE_REGS_USER) { + user_mask =3D x86_pmu_update_user_xregs(data, regs, + mask, ignore_mask); + } =20 if (sample_type & PERF_SAMPLE_REGS_INTR) intr_mask =3D mask & ~ignore_mask; =20 + if (user_mask | intr_mask) { + xsave->header.xfeatures =3D 0; + xsaves_nmi(xsave, user_mask | intr_mask); + } + + if (user_mask) { + perf_regs =3D container_of(data->regs_user.regs, + struct x86_perf_regs, regs); + update_perf_regs(perf_regs, xsave, user_mask); + } + if (intr_mask) { perf_regs =3D container_of(data->regs_intr.regs, struct x86_perf_regs, regs); - xsave->header.xfeatures =3D 0; - xsaves_nmi(xsave, mask); update_perf_regs(perf_regs, xsave, intr_mask); } } @@ -1850,18 +1963,19 @@ void x86_pmu_update_perf_regs(struct perf_event *ev= ent, { u64 sample_type =3D event->attr.sample_type; =20 - if (!((sample_type & PERF_SAMPLE_REGS_INTR) && - (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK))) + if (!event_has_extended_regs(event)) return; =20 if (sample_type & PERF_SAMPLE_REGS_INTR) x86_pmu_update_regs_intr(event, data, regs); + if (sample_type & PERF_SAMPLE_REGS_USER) + x86_pmu_update_regs_user(event, data, regs); =20 /* * ignore_mask indicates the PEBS sampled extended regs * which are unnecessary to sample again. */ - x86_pmu_sample_xregs(event, data, ignore_mask); + x86_pmu_sample_xregs(event, data, regs, ignore_mask); } =20 int x86_pmu_handle_irq(struct pt_regs *regs) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index f5d458e3ba3f..6c06558c416f 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -4698,15 +4698,15 @@ static void intel_pebs_aliases_skl(struct perf_even= t *event) static unsigned long intel_pmu_large_pebs_flags(struct perf_event *event) { unsigned long flags =3D x86_pmu.large_pebs_flags; + u64 gprs_mask =3D PEBS_GP_REGS | PERF_REG_EXTENDED_MASK; =20 if (event->attr.use_clockid) flags &=3D ~PERF_SAMPLE_TIME; if (!event->attr.exclude_kernel) flags &=3D ~PERF_SAMPLE_REGS_USER; - if (event->attr.sample_regs_user & ~PEBS_GP_REGS) + if (event->attr.sample_regs_user & ~gprs_mask) flags &=3D ~PERF_SAMPLE_REGS_USER; - if (event->attr.sample_regs_intr & - ~(PEBS_GP_REGS | PERF_REG_EXTENDED_MASK)) + if (event->attr.sample_regs_intr & ~gprs_mask) flags &=3D ~PERF_SAMPLE_REGS_INTR; return flags; } diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index 4f72ce6a9585..bd43bf26e6bf 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1749,8 +1749,7 @@ static u64 pebs_update_adaptive_cfg(struct perf_event= *event) if (gprs || (attr->precise_ip < 2) || tsx_weight) pebs_data_cfg |=3D PEBS_DATACFG_GP; =20 - if ((sample_type & PERF_SAMPLE_REGS_INTR) && - (attr->sample_regs_intr & PERF_REG_EXTENDED_MASK)) + if (event_has_extended_regs(event)) pebs_data_cfg |=3D PEBS_DATACFG_XMMS; =20 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { @@ -2957,6 +2956,8 @@ __intel_pmu_pebs_events(struct perf_event *event, void *at =3D get_next_pebs_record_by_bit(base, top, bit); int cnt =3D count; =20 + x86_pmu_clear_perf_regs(regs); + if (!iregs) iregs =3D &dummy_iregs; =20 --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3086D3B635E; Fri, 29 May 2026 08:03:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041818; cv=none; b=cJkBCsTN+kR6BuPpF7PsKRdIeEB6QxpD6NAmH1ZTR08bjyp6JAtM6ruIN5trZXTszPfz2SvSMeXM4WH2Ff9VaA9F/NPkUprOWSG79xN6uOIjV0rbGLOQ+odBIZSCbF0osWP3s2/geJadiG921y8VeKZ9yhq1iMJm0Ja05WlqPJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041818; c=relaxed/simple; bh=QPNCOTx8OnNmc1NpiGzPJDqH0hxDbVWjUlz6L1nSRgI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MIGLexrLrYeKw6tpyUROjafMmF3kSUy4CfAvERjGao3JMNX6qUl0h0TESEjyC/Ear/k9s1wDFCVenuXsIiPaXENCor3a7p6jfkPPp7FBv5Oll8LiaDDwvNbZg3GXQQjCboM+bhh74gl4wqaD0CI9g/9G3LYD5EZffr51krPW2Tk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EJbNMO1N; arc=none smtp.client-ip=192.198.163.7 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.b="EJbNMO1N" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041816; x=1811577816; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QPNCOTx8OnNmc1NpiGzPJDqH0hxDbVWjUlz6L1nSRgI=; b=EJbNMO1Njex2hxF7uaS57gcZDyA/LXguioP6XbYaVMZodYO8dXVNSt4t oo77JAi1BiDuBd47qAnzp4UJ22SeGTMHJ2UT/x7Y/Ln+vLIederXuHbK2 He9VCz1Bn1WFs1At46VtZBWG1wuoard5x4iKyBehzL7gFvCFoeH/iNAN7 k1ssIDRLKPNljdcxzVisCa6MoUAOS68EwV/Rhfcp3ZW41g3VdIWpCbwyk AhEtjod9NfFXgPZIaOVuiDvAQNDmBR2GaoDHogOBhXH6WzFnLQCAE/Zlg tJKjT2+hD+w1XRqAwcSjd6XLl1/XRRQ9EIr75kmkZsMijnD1eqSQVjxnr A==; X-CSE-ConnectionGUID: h9VILrFqRN6gShCWv/iCuQ== X-CSE-MsgGUID: t11f/CtnQTa/MMHNtcqOtQ== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342003" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342003" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:36 -0700 X-CSE-ConnectionGUID: vL4UT3w3R6GrgOxbb0avvA== X-CSE-MsgGUID: ULunKXxiSvqEsNLbQqgyTg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802280" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:31 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Kan Liang , Dapeng Mi Subject: [Patch v8 12/23] perf: Add sampling support for SIMD registers Date: Fri, 29 May 2026 15:56:34 +0800 Message-Id: <20260529075645.580362-13-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kan Liang Users may be interested in sampling SIMD registers during profiling. The current sample_regs_* structure does not have sufficient space for all SIMD registers. To address this, new attribute fields sample_simd_{pred,vec}_reg_* are added to struct perf_event_attr to represent the SIMD registers that are expected to be sampled. Currently, the perf/x86 code supports XMM registers in sample_regs_*. To unify the configuration of SIMD registers and ensure a consistent method for configuring XMM and other SIMD registers, a new event attribute field, sample_simd_regs_enabled, is introduced. When sample_simd_regs_enabled is set, it indicates that all SIMD registers, including XMM, will be represented by the newly introduced sample_simd_{pred|vec}_reg_* fields. The original XMM space in sample_regs_* is reserved for future uses. Since SIMD registers are wider than 64 bits, a new output format is introduced. The number and width of SIMD registers are dumped first, followed by the register values. The number and width are based on the user's configuration. A new ABI, PERF_SAMPLE_REGS_ABI_SIMD, is added to indicate the new format. The enum perf_sample_regs_abi is now a bitmap. This change should not impact existing tools, as the version and bitmap remain the same for values 1 and 2. Additionally, two new __weak functions are introduced: - perf_simd_reg_value(): Retrieves the value of the requested SIMD register. - perf_simd_reg_validate(): Validates the configuration of the SIMD registers. A new flag, PERF_PMU_CAP_SIMD_REGS, is added to indicate that the PMU supports SIMD register dumping. An error is generated if sample_simd_{pred|vec}_reg_* is mistakenly set for a PMU that does not support this capability. Suggested-by: Peter Zijlstra (Intel) Signed-off-by: Kan Liang Co-developed-by: Dapeng Mi Signed-off-by: Dapeng Mi --- include/linux/perf_event.h | 8 ++ include/linux/perf_regs.h | 6 ++ include/uapi/linux/perf_event.h | 49 +++++++++- kernel/events/core.c | 153 +++++++++++++++++++++++++++++--- 4 files changed, 202 insertions(+), 14 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index fb38affa7352..5f0642ef4fd2 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -306,6 +306,7 @@ struct perf_event_pmu_context; #define PERF_PMU_CAP_AUX_PAUSE 0x0200 #define PERF_PMU_CAP_AUX_PREFER_LARGE 0x0400 #define PERF_PMU_CAP_MEDIATED_VPMU 0x0800 +#define PERF_PMU_CAP_SIMD_REGS 0x1000 =20 /** * pmu::scope @@ -1534,6 +1535,13 @@ perf_event__output_id_sample(struct perf_event *even= t, extern void perf_log_lost_samples(struct perf_event *event, u64 lost); =20 +static inline bool event_has_simd_regs(struct perf_event *event) +{ + struct perf_event_attr *attr =3D &event->attr; + + return attr->sample_simd_regs_enabled !=3D 0; +} + static inline bool event_has_extended_regs(struct perf_event *event) { struct perf_event_attr *attr =3D &event->attr; diff --git a/include/linux/perf_regs.h b/include/linux/perf_regs.h index 144bcc3ff19f..8fa3eeb14953 100644 --- a/include/linux/perf_regs.h +++ b/include/linux/perf_regs.h @@ -14,6 +14,12 @@ int perf_reg_validate(u64 mask); u64 perf_reg_abi(struct task_struct *task); void perf_get_regs_user(struct perf_regs *regs_user, struct pt_regs *regs); +int perf_simd_reg_validate(u16 simd_enabled, u16 vec_qwords, + u64 vec_mask_intr, u64 vec_mask_user, + u16 pred_qwords, u32 pred_mask_intr, + u32 pred_mask_user); +u64 perf_simd_reg_value(struct pt_regs *regs, int idx, + u16 qwords_idx, bool pred); =20 #ifdef CONFIG_HAVE_PERF_REGS #include diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index fd10aa8d697f..c49fc76292f7 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -314,8 +314,9 @@ enum { */ enum perf_sample_regs_abi { PERF_SAMPLE_REGS_ABI_NONE =3D 0, - PERF_SAMPLE_REGS_ABI_32 =3D 1, - PERF_SAMPLE_REGS_ABI_64 =3D 2, + PERF_SAMPLE_REGS_ABI_32 =3D (1 << 0), + PERF_SAMPLE_REGS_ABI_64 =3D (1 << 1), + PERF_SAMPLE_REGS_ABI_SIMD =3D (1 << 2), }; =20 /* @@ -383,6 +384,7 @@ enum perf_event_read_format { #define PERF_ATTR_SIZE_VER7 128 /* Add: sig_data */ #define PERF_ATTR_SIZE_VER8 136 /* Add: config3 */ #define PERF_ATTR_SIZE_VER9 144 /* add: config4 */ +#define PERF_ATTR_SIZE_VER10 176 /* Add: sample_simd_{vec|pred}_reg_* */ =20 /* * 'struct perf_event_attr' contains various attributes that define @@ -547,6 +549,29 @@ struct perf_event_attr { =20 __u64 config3; /* extension of config2 */ __u64 config4; /* extension of config3 */ + + /* + * Defines the sampling SIMD/PRED(predicate) registers bitmap and + * qwords (8 bytes) length. + * + * sample_simd_regs_enabled !=3D 0 indicates there are SIMD/PRED + * registers to be sampled, the SIMD/PRED registers bitmap and + * qwords length are represented in + * sample_simd_{vec|pred}_reg_{intr|user} and + * sample_simd_{vec|pred}_reg_qwords fields separately. + * + * sample_simd_regs_enabled =3D=3D 0 indicates no SIMD/PRED registers + * are sampled. + */ + __u16 sample_simd_regs_enabled; + __u16 sample_simd_pred_reg_qwords; + __u16 sample_simd_vec_reg_qwords; + __u16 __reserved_4; + + __u32 sample_simd_pred_reg_intr; + __u32 sample_simd_pred_reg_user; + __u64 sample_simd_vec_reg_intr; + __u64 sample_simd_vec_reg_user; }; =20 /* @@ -1020,7 +1045,15 @@ enum perf_event_type { * } && PERF_SAMPLE_BRANCH_STACK * * { u64 abi; # enum perf_sample_regs_abi - * u64 regs[weight(mask)]; } && PERF_SAMPLE_REGS_USER + * u64 regs[weight(mask)]; + * struct { + * u64 nr_vectors; # 0 ... weight(sample_simd_vec_reg_user) + * u64 vector_qwords; # 0 ... sample_simd_vec_reg_qwords + * u64 nr_pred; # 0 ... weight(sample_simd_pred_reg_user) + * u64 pred_qwords; # 0 ... sample_simd_pred_reg_qwords + * u64 data[nr_vectors * vector_qwords + nr_pred * pred_qwords]; + * } && (abi & PERF_SAMPLE_REGS_ABI_SIMD) + * } && PERF_SAMPLE_REGS_USER * * { u64 size; * char data[size]; @@ -1047,7 +1080,15 @@ enum perf_event_type { * { u64 data_src; } && PERF_SAMPLE_DATA_SRC * { u64 transaction; } && PERF_SAMPLE_TRANSACTION * { u64 abi; # enum perf_sample_regs_abi - * u64 regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR + * u64 regs[weight(mask)]; + * struct { + * u64 nr_vectors; # 0 ... weight(sample_simd_vec_reg_intr) + * u64 vector_qwords; # 0 ... sample_simd_vec_reg_qwords + * u64 nr_pred; # 0 ... weight(sample_simd_pred_reg_intr) + * u64 pred_qwords; # 0 ... sample_simd_pred_reg_qwords + * u64 data[nr_vectors * vector_qwords + nr_pred * pred_qwords]; + * } && (abi & PERF_SAMPLE_REGS_ABI_SIMD) + * } && PERF_SAMPLE_REGS_INTR * { u64 phys_addr;} && PERF_SAMPLE_PHYS_ADDR * { u64 cgroup;} && PERF_SAMPLE_CGROUP * { u64 data_page_size;} && PERF_SAMPLE_DATA_PAGE_SIZE diff --git a/kernel/events/core.c b/kernel/events/core.c index 2ce553db4dcb..94bb034da9b9 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7791,22 +7791,60 @@ void __weak perf_get_regs_user(struct perf_regs *re= gs_user, regs_user->abi =3D perf_reg_abi(current); } =20 +#define word_for_each_set_bit(bit, val) \ + for (unsigned long long __v =3D (val); \ + __v && ((bit =3D __builtin_ctzll(__v)), 1); \ + __v &=3D __v - 1) + static void perf_output_sample_regs(struct perf_output_handle *handle, struct pt_regs *regs, u64 mask) { int bit; - DECLARE_BITMAP(_mask, 64); - - bitmap_from_u64(_mask, mask); - for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { - u64 val; =20 - val =3D perf_reg_value(regs, bit); + word_for_each_set_bit(bit, mask) { + u64 val =3D perf_reg_value(regs, bit); perf_output_put(handle, val); } } =20 +static void +perf_output_sample_simd_regs(struct perf_output_handle *handle, + struct perf_event *event, + struct pt_regs *regs, + u64 mask, u32 pred_mask) +{ + u64 pred_qwords =3D event->attr.sample_simd_pred_reg_qwords; + u64 vec_qwords =3D event->attr.sample_simd_vec_reg_qwords; + u64 nr_vectors =3D hweight64(mask); + u64 nr_pred =3D hweight32(pred_mask); + int bit; + + perf_output_put(handle, nr_vectors); + perf_output_put(handle, vec_qwords); + perf_output_put(handle, nr_pred); + perf_output_put(handle, pred_qwords); + + if (nr_vectors) { + word_for_each_set_bit(bit, mask) { + for (int i =3D 0; i < vec_qwords; i++) { + u64 val =3D perf_simd_reg_value(regs, bit, + i, false); + perf_output_put(handle, val); + } + } + } + if (nr_pred) { + word_for_each_set_bit(bit, pred_mask) { + for (int i =3D 0; i < pred_qwords; i++) { + u64 val =3D perf_simd_reg_value(regs, bit, + i, true); + perf_output_put(handle, val); + } + } + } +} + static void perf_sample_regs_user(struct perf_regs *regs_user, struct pt_regs *regs) { @@ -7828,6 +7866,22 @@ static void perf_sample_regs_intr(struct perf_regs *= regs_intr, regs_intr->abi =3D perf_reg_abi(current); } =20 +int __weak perf_simd_reg_validate(u16 simd_enabled, u16 vec_qwords, + u64 vec_mask_intr, u64 vec_mask_user, + u16 pred_qwords, u32 pred_mask_intr, + u32 pred_mask_user) +{ + return simd_enabled || + vec_qwords || vec_mask_intr || vec_mask_user || + pred_qwords || pred_mask_intr || pred_mask_user ? + -EINVAL : 0; +} + +u64 __weak perf_simd_reg_value(struct pt_regs *regs, int idx, + u16 qwords_idx, bool pred) +{ + return 0; +} =20 /* * Get remaining task size from user stack pointer. @@ -8358,10 +8412,17 @@ void perf_output_sample(struct perf_output_handle *= handle, perf_output_put(handle, abi); =20 if (abi) { - u64 mask =3D event->attr.sample_regs_user; + struct perf_event_attr *attr =3D &event->attr; + u64 mask =3D attr->sample_regs_user; perf_output_sample_regs(handle, data->regs_user.regs, mask); + if (abi & PERF_SAMPLE_REGS_ABI_SIMD) { + perf_output_sample_simd_regs(handle, event, + data->regs_user.regs, + attr->sample_simd_vec_reg_user, + attr->sample_simd_pred_reg_user); + } } } =20 @@ -8389,11 +8450,18 @@ void perf_output_sample(struct perf_output_handle *= handle, perf_output_put(handle, abi); =20 if (abi) { - u64 mask =3D event->attr.sample_regs_intr; + struct perf_event_attr *attr =3D &event->attr; + u64 mask =3D attr->sample_regs_intr; =20 perf_output_sample_regs(handle, data->regs_intr.regs, mask); + if (abi & PERF_SAMPLE_REGS_ABI_SIMD) { + perf_output_sample_simd_regs(handle, event, + data->regs_intr.regs, + attr->sample_simd_vec_reg_intr, + attr->sample_simd_pred_reg_intr); + } } } =20 @@ -8596,6 +8664,33 @@ static __always_inline u64 __cond_set(u64 flags, u64= s, u64 d) return d * !!(flags & s); } =20 +static u64 perf_update_xregs_size(struct perf_event *event, bool intr) +{ + u16 pred_qwords =3D event->attr.sample_simd_pred_reg_qwords; + u16 vec_qwords =3D event->attr.sample_simd_vec_reg_qwords; + u64 pred_mask; + u64 mask; + int size; + + if (intr) { + mask =3D event->attr.sample_simd_vec_reg_intr; + pred_mask =3D event->attr.sample_simd_pred_reg_intr; + } else { + mask =3D event->attr.sample_simd_vec_reg_user; + pred_mask =3D event->attr.sample_simd_pred_reg_user; + } + + /* nr_vectors, vector_qwords, nr_pred, pred_qwords */ + size =3D sizeof(u64) * 4; + size +=3D (hweight64(mask) * vec_qwords + + hweight64(pred_mask) * pred_qwords) * sizeof(u64); + + /* Warn if exceeding perf_event_header.size (u16). */ + WARN_ON_ONCE(size > U16_MAX); + + return size; +} + void perf_prepare_sample(struct perf_sample_data *data, struct perf_event *event, struct pt_regs *regs) @@ -8661,6 +8756,11 @@ void perf_prepare_sample(struct perf_sample_data *da= ta, size +=3D hweight64(mask) * sizeof(u64); } =20 + if (data->regs_user.abi && event_has_simd_regs(event)) { + size +=3D perf_update_xregs_size(event, false); + data->regs_user.abi |=3D PERF_SAMPLE_REGS_ABI_SIMD; + } + data->dyn_size +=3D size; data->sample_flags |=3D PERF_SAMPLE_REGS_USER; } @@ -8724,6 +8824,11 @@ void perf_prepare_sample(struct perf_sample_data *da= ta, size +=3D hweight64(mask) * sizeof(u64); } =20 + if (data->regs_intr.abi && event_has_simd_regs(event)) { + size +=3D perf_update_xregs_size(event, true); + data->regs_intr.abi |=3D PERF_SAMPLE_REGS_ABI_SIMD; + } + data->dyn_size +=3D size; data->sample_flags |=3D PERF_SAMPLE_REGS_INTR; } @@ -13089,6 +13194,12 @@ static int perf_try_init_event(struct pmu *pmu, st= ruct perf_event *event) if (ret) goto err_pmu; =20 + if (!(pmu->capabilities & PERF_PMU_CAP_SIMD_REGS) && + event_has_simd_regs(event)) { + ret =3D -EOPNOTSUPP; + goto err_destroy; + } + if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && event_has_extended_regs(event)) { ret =3D -EOPNOTSUPP; @@ -13585,7 +13696,8 @@ static int perf_copy_attr(struct perf_event_attr __= user *uattr, =20 attr->size =3D size; =20 - if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) + if (attr->__reserved_1 || attr->__reserved_2 || + attr->__reserved_3 || attr->__reserved_4) return -EINVAL; =20 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) @@ -13634,6 +13746,15 @@ static int perf_copy_attr(struct perf_event_attr _= _user *uattr, ret =3D perf_reg_validate(attr->sample_regs_user); if (ret) return ret; + ret =3D perf_simd_reg_validate(attr->sample_simd_regs_enabled, + attr->sample_simd_vec_reg_qwords, + attr->sample_simd_vec_reg_intr, + attr->sample_simd_vec_reg_user, + attr->sample_simd_pred_reg_qwords, + attr->sample_simd_pred_reg_intr, + attr->sample_simd_pred_reg_user); + if (ret) + return ret; } =20 if (attr->sample_type & PERF_SAMPLE_STACK_USER) { @@ -13654,8 +13775,20 @@ static int perf_copy_attr(struct perf_event_attr _= _user *uattr, if (!attr->sample_max_stack) attr->sample_max_stack =3D sysctl_perf_event_max_stack; =20 - if (attr->sample_type & PERF_SAMPLE_REGS_INTR) + if (attr->sample_type & PERF_SAMPLE_REGS_INTR) { ret =3D perf_reg_validate(attr->sample_regs_intr); + if (ret) + return ret; + ret =3D perf_simd_reg_validate(attr->sample_simd_regs_enabled, + attr->sample_simd_vec_reg_qwords, + attr->sample_simd_vec_reg_intr, + attr->sample_simd_vec_reg_user, + attr->sample_simd_pred_reg_qwords, + attr->sample_simd_pred_reg_intr, + attr->sample_simd_pred_reg_user); + if (ret) + return ret; + } =20 #ifndef CONFIG_CGROUP_PERF if (attr->sample_type & PERF_SAMPLE_CGROUP) --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D44763B47CC; Fri, 29 May 2026 08:03:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041827; cv=none; b=tHLuFJneHwBzQ++AkdOB5HEsBR8dqFvGWE3JbBiuBb+1LyUb3vpgyjgAnSU/GuugAvanLbAiqMLLVt9hsFUvNlFwiXatXnkdV/KbxsWSZS8zKmcp2p4z3icBWEIpsdLIT6znIJVnI7k7Iaj0zi3/p4moHF65XBI8r6kqH9OKzC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041827; c=relaxed/simple; bh=zy8r1RiNCfCqkY9KBrsV+6CjNaBSXhkaNAfGvfzHd4k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qbMtipgxkns71HiikkSWCTqNMt8Sru0clEFOQhyDUqWVa1NZEVzv5VmPz9g/STqg0863j6Ptu+oTkhvqML5M8H9NgaRdzV9kTeb6qu4pnXaniSNPaGRV2n8Y+X620S4SdE23r4q7n4R1Sh5gBx5PeR2//7O0jR3eEdHM8aY694o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MwStkMHv; arc=none smtp.client-ip=192.198.163.7 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.b="MwStkMHv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041826; x=1811577826; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zy8r1RiNCfCqkY9KBrsV+6CjNaBSXhkaNAfGvfzHd4k=; b=MwStkMHvBltyPe50SnEHebbnHVzfuqd1OuO0UIkJQ9OMgiX38WDjLJXJ 7QbzCJ1oM6RwcJZKtv/l5/Q85B+4Qw26NITcodqeNzyNKuQWFaDLhkCNr GgSsrijbfMp/cubBFdCIYI8ty2xyzsJvM8rmESSyBtN4R75PmW2vLt+66 fi38+Hh+E/vDNxDWz+IBgLQ6JXY+lULvAbkZp/66GQy8pvpMCNDAhDLsY AeCqUzaXHrtKPTuhv8L1TrZwgvv6+ctWUMNUoFJ2csg+xU0S75MWMAso8 /GNh0/Qi7C7+PUQULUB0T8O065Dp5P8uJ7lMMsleukPnq6xlNDYhHccoh A==; X-CSE-ConnectionGUID: 6kXyLjPcTNKSN7FAHgs64g== X-CSE-MsgGUID: hNTRgKHgQdyK/ngs7LgGnw== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342033" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342033" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:45 -0700 X-CSE-ConnectionGUID: rQ0vGAviRRea3mS/m3duqA== X-CSE-MsgGUID: fPofIxdCQD+uNfmYfOnbkg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802352" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:36 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 13/23] perf/x86: Support XMM sampling using sample_simd_vec_reg_* fields Date: Fri, 29 May 2026 15:56:35 +0800 Message-Id: <20260529075645.580362-14-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds support for sampling XMM registers using the sample_simd_vec_reg_* fields. When sample_simd_regs_enabled is set, the original XMM space in the sample_regs_* field is treated as reserved. An INVAL error will be reported to user space if any bit is set in the original XMM space while sample_simd_regs_enabled is set. The perf_reg_value function requires ABI information to understand the layout of sample_regs. To accommodate this, a new abi field is introduced in the struct x86_perf_regs to represent ABI information. Additionally, the X86-specific perf_simd_reg_value function is implemented to retrieve the XMM register values. Please note XMM sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 48 +++++++++++++++-- arch/x86/events/intel/ds.c | 2 +- arch/x86/events/perf_event.h | 12 +++++ arch/x86/include/asm/perf_event.h | 1 + arch/x86/include/uapi/asm/perf_regs.h | 13 +++++ arch/x86/kernel/perf_regs.c | 74 ++++++++++++++++++++++++++- include/linux/perf_event.h | 1 + kernel/events/core.c | 2 +- 8 files changed, 145 insertions(+), 8 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index f9e3f349b69a..5a4760a1716b 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -718,6 +718,20 @@ int x86_pmu_hw_config(struct perf_event *event) if (is_sampling_event(event) && !event->attr.precise_ip && !this_cpu_has(X86_FEATURE_XSAVES)) return -EINVAL; + if (event->attr.sample_simd_regs_enabled) + return -EINVAL; + } + + if (event_has_simd_regs(event)) { + if (!(event->pmu->capabilities & PERF_PMU_CAP_SIMD_REGS)) + return -EINVAL; + if (is_sampling_event(event) && !event->attr.precise_ip && + !this_cpu_has(X86_FEATURE_XSAVES)) + return -EINVAL; + /* The vector registers set is not supported */ + if (event_needs_xmm(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_SSE)) + return -EINVAL; } } =20 @@ -1760,6 +1774,7 @@ void x86_pmu_clear_perf_regs(struct pt_regs *regs) { struct x86_perf_regs *perf_regs =3D container_of(regs, struct x86_perf_re= gs, regs); =20 + perf_regs->abi =3D PERF_SAMPLE_REGS_ABI_NONE; perf_regs->xmm_regs =3D NULL; } =20 @@ -1780,13 +1795,14 @@ static void update_perf_regs(struct x86_perf_regs *= perf_regs, =20 /* * The x86 specific variant of perf_sample_regs_intr(). - * It would be extended to add more SIMD registers sampling support - * in later patches. + * Update data->regs_intr fields for extended registers (e.g., SIMD). */ static void x86_pmu_update_regs_intr(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs) { + struct x86_perf_regs *perf_regs; + data->regs_intr.regs =3D regs; data->regs_intr.abi =3D perf_reg_abi(current); =20 @@ -1796,6 +1812,17 @@ static void x86_pmu_update_regs_intr(struct perf_eve= nt *event, sizeof(u64); } =20 + if (data->regs_intr.abi && event_has_simd_regs(event)) { + data->dyn_size +=3D perf_update_xregs_size(event, true); + data->regs_intr.abi |=3D PERF_SAMPLE_REGS_ABI_SIMD; + } + + if (data->regs_intr.abi) { + perf_regs =3D container_of(data->regs_intr.regs, + struct x86_perf_regs, regs); + perf_regs->abi =3D data->regs_intr.abi; + } + /* * Set PERF_SAMPLE_REGS_INTR to bypass perf_sample_regs_intr() call * in perf_prepare_sample() function. @@ -1836,6 +1863,7 @@ static void x86_pmu_update_regs_user(struct perf_even= t *event, struct pt_regs *regs) { struct perf_event_attr *attr =3D &event->attr; + struct x86_perf_regs *perf_regs; =20 if (user_mode(regs)) { data->regs_user.abi =3D perf_reg_abi(current); @@ -1858,6 +1886,17 @@ static void x86_pmu_update_regs_user(struct perf_eve= nt *event, if (data->regs_user.regs) data->dyn_size +=3D hweight64(attr->sample_regs_user) * sizeof(u64); =20 + if (data->regs_user.abi && event_has_simd_regs(event)) { + data->dyn_size +=3D perf_update_xregs_size(event, false); + data->regs_user.abi |=3D PERF_SAMPLE_REGS_ABI_SIMD; + } + + if (data->regs_user.abi) { + perf_regs =3D container_of(data->regs_user.regs, + struct x86_perf_regs, regs); + perf_regs->abi =3D data->regs_user.abi; + } + /* * Set PERF_SAMPLE_REGS_USER to bypass perf_sample_regs_user() call * in perf_prepare_sample() function. @@ -1926,7 +1965,7 @@ static void x86_pmu_sample_xregs(struct perf_event *e= vent, if (WARN_ON_ONCE(!xsave) || !in_nmi()) return; =20 - if (event_has_extended_regs(event)) + if (event_needs_xmm(event)) mask |=3D XFEATURE_MASK_SSE; =20 mask &=3D x86_pmu.ext_regs_mask; @@ -1963,7 +2002,8 @@ void x86_pmu_update_perf_regs(struct perf_event *even= t, { u64 sample_type =3D event->attr.sample_type; =20 - if (!event_has_extended_regs(event)) + if (!event_needs_xmm(event) && + !event_has_simd_regs(event)) return; =20 if (sample_type & PERF_SAMPLE_REGS_INTR) diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index bd43bf26e6bf..609d4a83115d 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1749,7 +1749,7 @@ static u64 pebs_update_adaptive_cfg(struct perf_event= *event) if (gprs || (attr->precise_ip < 2) || tsx_weight) pebs_data_cfg |=3D PEBS_DATACFG_GP; =20 - if (event_has_extended_regs(event)) + if (event_needs_xmm(event)) pebs_data_cfg |=3D PEBS_DATACFG_XMMS; =20 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index cff5fbac000b..b04f5ba3294a 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -147,6 +147,18 @@ static inline bool is_acr_self_reload_event(struct per= f_event *event) return test_bit(hwc->idx, (unsigned long *)&hwc->config1); } =20 +static inline bool event_needs_xmm(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + event->attr.sample_simd_vec_reg_qwords >=3D PERF_X86_XMM_QWORDS) + return true; + + if (!event->attr.sample_simd_regs_enabled && + event_has_extended_regs(event)) + return true; + return false; +} + struct amd_nb { int nb_id; /* NorthBridge id */ int refcnt; /* reference count */ diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index e47a963a7cf0..e54d21c13494 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -726,6 +726,7 @@ extern void perf_events_lapic_init(void); struct pt_regs; struct x86_perf_regs { struct pt_regs regs; + u64 abi; union { u64 *xmm_regs; u32 *xmm_space; /* for xsaves */ diff --git a/arch/x86/include/uapi/asm/perf_regs.h b/arch/x86/include/uapi/= asm/perf_regs.h index 7c9d2bb3833b..5b7d5216f0bd 100644 --- a/arch/x86/include/uapi/asm/perf_regs.h +++ b/arch/x86/include/uapi/asm/perf_regs.h @@ -55,4 +55,17 @@ enum perf_event_x86_regs { =20 #define PERF_REG_EXTENDED_MASK (~((1ULL << PERF_REG_X86_XMM0) - 1)) =20 +enum { + PERF_X86_SIMD_XMM_REGS =3D 16, + PERF_X86_SIMD_VEC_REGS_MAX =3D PERF_X86_SIMD_XMM_REGS, +}; + +#define PERF_X86_SIMD_VEC_MASK __GENMASK_ULL(PERF_X86_SIMD_VEC_REGS_MAX - = 1, 0) + +enum { + /* 1 qword =3D 8 bytes */ + PERF_X86_XMM_QWORDS =3D 2, + PERF_X86_SIMD_QWORDS_MAX =3D PERF_X86_XMM_QWORDS, +}; + #endif /* _ASM_X86_PERF_REGS_H */ diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 81204cb7f723..7b9b38c189de 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -63,6 +63,9 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 if (idx >=3D PERF_REG_X86_XMM0 && idx < PERF_REG_X86_XMM_MAX) { perf_regs =3D container_of(regs, struct x86_perf_regs, regs); + /* SIMD registers are moved to dedicated sample_simd_vec_reg */ + if (perf_regs->abi & PERF_SAMPLE_REGS_ABI_SIMD) + return 0; if (!perf_regs->xmm_regs) return 0; return perf_regs->xmm_regs[idx - PERF_REG_X86_XMM0]; @@ -74,6 +77,71 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) return regs_get_register(regs, pt_regs_offset[idx]); } =20 +u64 perf_simd_reg_value(struct pt_regs *regs, int idx, + u16 qwords_idx, bool pred) +{ + struct x86_perf_regs *perf_regs =3D + container_of(regs, struct x86_perf_regs, regs); + + if (!(perf_regs->abi & PERF_SAMPLE_REGS_ABI_SIMD)) + return 0; + + if (pred) + return 0; + + if (WARN_ON_ONCE(idx >=3D PERF_X86_SIMD_VEC_REGS_MAX || + qwords_idx >=3D PERF_X86_SIMD_QWORDS_MAX)) + return 0; + + if (qwords_idx < PERF_X86_XMM_QWORDS) { + if (!perf_regs->xmm_regs) + return 0; + return perf_regs->xmm_regs[idx * PERF_X86_XMM_QWORDS + + qwords_idx]; + } + + return 0; +} + +int perf_simd_reg_validate(u16 simd_enabled, u16 vec_qwords, + u64 vec_mask_intr, u64 vec_mask_user, + u16 pred_qwords, u32 pred_mask_intr, + u32 pred_mask_user) +{ + u64 size; + + if (!simd_enabled) { + if (vec_qwords || vec_mask_intr || vec_mask_user || + pred_qwords || pred_mask_intr || pred_mask_user) + return -EINVAL; + return 0; + } + + if (!vec_qwords) { + if (vec_mask_intr || vec_mask_user) + return -EINVAL; + } else { + if (vec_qwords !=3D PERF_X86_XMM_QWORDS) + return -EINVAL; + if ((!vec_mask_intr && !vec_mask_user) || + (vec_mask_intr & ~PERF_X86_SIMD_VEC_MASK) || + (vec_mask_user & ~PERF_X86_SIMD_VEC_MASK)) + return -EINVAL; + } + + if (pred_qwords || pred_mask_intr || pred_mask_user) + return -EINVAL; + + size =3D ((vec_qwords * hweight64(vec_mask_intr)) + + (vec_qwords * hweight64(vec_mask_user)) + + (pred_qwords * hweight32(pred_mask_intr)) + + (pred_qwords * hweight32(pred_mask_user))) * sizeof(u64); + if (size > U16_MAX) + return -EINVAL; + + return 0; +} + #define PERF_REG_X86_RESERVED (((1ULL << PERF_REG_X86_XMM0) - 1) & \ ~((1ULL << PERF_REG_X86_MAX) - 1)) =20 @@ -89,7 +157,8 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 int perf_reg_validate(u64 mask) { - if (!mask || (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED))) + /* The mask could be 0 if only the SIMD registers are interested */ + if (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)) return -EINVAL; =20 return 0; @@ -108,7 +177,8 @@ u64 perf_reg_abi(struct task_struct *task) =20 int perf_reg_validate(u64 mask) { - if (!mask || (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED))) + /* The mask could be 0 if only the SIMD registers are interested */ + if (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)) return -EINVAL; =20 return 0; diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 5f0642ef4fd2..baf694203d23 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1485,6 +1485,7 @@ static inline void perf_clear_branch_entry_bitfields(= struct perf_branch_entry *b br->reserved =3D 0; } =20 +extern u64 perf_update_xregs_size(struct perf_event *event, bool intr); extern void perf_output_sample(struct perf_output_handle *handle, struct perf_event_header *header, struct perf_sample_data *data, diff --git a/kernel/events/core.c b/kernel/events/core.c index 94bb034da9b9..afd5b1408231 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8664,7 +8664,7 @@ static __always_inline u64 __cond_set(u64 flags, u64 = s, u64 d) return d * !!(flags & s); } =20 -static u64 perf_update_xregs_size(struct perf_event *event, bool intr) +u64 perf_update_xregs_size(struct perf_event *event, bool intr) { u16 pred_qwords =3D event->attr.sample_simd_pred_reg_qwords; u16 vec_qwords =3D event->attr.sample_simd_vec_reg_qwords; --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DE1D3B4E98; Fri, 29 May 2026 08:03:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041830; cv=none; b=tiD49T8nMNuWBDaF78F1NGHMFjV8l3dTMWz69GLSPu9Ea3Q0CmhY169SKijGaikPsROtLltJRJ9OYZPLxPOtwTbg0IGYLa9ExGV7Ud6ckB/m/WEV0Qpkt3/EDpSrDaKpvkxscSgMcyt9O84gFHqAjHrdWjRjwL7THPH7h4T6Gzs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041830; c=relaxed/simple; bh=CVIk9o8ujVv0PJRRoXST8MXtv56xoAiMpCaH4tgEGnE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LIPGIAj0KpbLIkc3qQAU6oVwrTy0jbUaybLJH2E23pE8vHXJI2a35jah67zumFjBph90NaAynaf6N5hm9SYHNlF5oL8M5/GNCPApvlo8yGAP3QJbcSpZri9ERr9Si+OIQi/js4rB7JM/fzVfSyGu4IA5/b/wI5rKtvefrYBOxiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=eLvYvP/q; arc=none smtp.client-ip=192.198.163.7 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.b="eLvYvP/q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041829; x=1811577829; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CVIk9o8ujVv0PJRRoXST8MXtv56xoAiMpCaH4tgEGnE=; b=eLvYvP/qt98lYQ8rE4lzf1chWOfkrkmQRObzCEmvCXpJY0OjuxrPGBrA ba1N4YQcTCTeFnZVqnB2b4CjZuYBSrwv/4bHc06xT+BB/kQ3vkMzTZ3bd mlaw6Ah9qCSXWQD4OJVGYOMKbtma7Gy+tkKmR8Uh4bQFPhnkPVTo8o0Tu rRLBx3sXxVNVAVf/1t6n1e358xpenu8gfUgnUOhkQeIk60jmazzpw/idK Cw2BgmY3V1s6b9eD+UVmS/g23D20y67cvQwOuOsQLEB5BpEht4zzWYiyg fUCSgWEn6PtPejRJ282hWuG3Wnb2+lQJYxvbyUNVEqZWa8XzBCn7o1SJC Q==; X-CSE-ConnectionGUID: BzcDAweWQSaF9yZsCsrYgg== X-CSE-MsgGUID: qGDvWAqOSziYPlCkqdM22w== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342045" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342045" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:49 -0700 X-CSE-ConnectionGUID: 5TX8Z/CpQK6Dj5D+7loJ5w== X-CSE-MsgGUID: PKXPv8q0Q66hYjxGyBb/4Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802375" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:44 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 14/23] perf/x86: Support YMM sampling using sample_simd_vec_reg_* fields Date: Fri, 29 May 2026 15:56:36 +0800 Message-Id: <20260529075645.580362-15-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch introduces support for sampling YMM registers via the sample_simd_vec_reg_* fields. Each YMM register consists of 4 u64 words, assembled from two halves: XMM (the lower 2 u64 words) and YMMH (the upper 2 u64 words). Although both XMM and YMMH data can be retrieved with a single xsaves instruction, they are stored in separate locations. The perf_simd_reg_value() function is responsible for assembling these halves into a complete YMM register for output to userspace. Additionally, sample_simd_vec_reg_qwords should be set to 4 to indicate YMM sampling. Please note YMM sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 8 ++++++++ arch/x86/events/perf_event.h | 9 +++++++++ arch/x86/include/asm/perf_event.h | 4 ++++ arch/x86/include/uapi/asm/perf_regs.h | 6 ++++-- arch/x86/kernel/perf_regs.c | 10 +++++++++- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 5a4760a1716b..d39710f42ca0 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -732,6 +732,9 @@ int x86_pmu_hw_config(struct perf_event *event) if (event_needs_xmm(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_SSE)) return -EINVAL; + if (event_needs_ymm(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_YMM)) + return -EINVAL; } } =20 @@ -1776,6 +1779,7 @@ void x86_pmu_clear_perf_regs(struct pt_regs *regs) =20 perf_regs->abi =3D PERF_SAMPLE_REGS_ABI_NONE; perf_regs->xmm_regs =3D NULL; + perf_regs->ymmh_regs =3D NULL; } =20 static void update_perf_regs(struct x86_perf_regs *perf_regs, @@ -1791,6 +1795,8 @@ static void update_perf_regs(struct x86_perf_regs *pe= rf_regs, =20 if (mask & XFEATURE_MASK_SSE) perf_regs->xmm_space =3D xsave->i387.xmm_space; + if (mask & XFEATURE_MASK_YMM) + perf_regs->ymmh =3D get_xsave_addr(xsave, XFEATURE_YMM); } =20 /* @@ -1967,6 +1973,8 @@ static void x86_pmu_sample_xregs(struct perf_event *e= vent, =20 if (event_needs_xmm(event)) mask |=3D XFEATURE_MASK_SSE; + if (event_needs_ymm(event)) + mask |=3D XFEATURE_MASK_YMM; =20 mask &=3D x86_pmu.ext_regs_mask; if (sample_type & PERF_SAMPLE_REGS_USER) { diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index b04f5ba3294a..5111eaf8b12a 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -159,6 +159,15 @@ static inline bool event_needs_xmm(struct perf_event *= event) return false; } =20 +static inline bool event_needs_ymm(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + event->attr.sample_simd_vec_reg_qwords >=3D PERF_X86_YMM_QWORDS) + return true; + + return false; +} + struct amd_nb { int nb_id; /* NorthBridge id */ int refcnt; /* reference count */ diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index e54d21c13494..1d03b86be65d 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -731,6 +731,10 @@ struct x86_perf_regs { u64 *xmm_regs; u32 *xmm_space; /* for xsaves */ }; + union { + u64 *ymmh_regs; + struct ymmh_struct *ymmh; + }; }; =20 extern unsigned long perf_arch_instruction_pointer(struct pt_regs *regs); diff --git a/arch/x86/include/uapi/asm/perf_regs.h b/arch/x86/include/uapi/= asm/perf_regs.h index 5b7d5216f0bd..8f513229fbb8 100644 --- a/arch/x86/include/uapi/asm/perf_regs.h +++ b/arch/x86/include/uapi/asm/perf_regs.h @@ -57,7 +57,8 @@ enum perf_event_x86_regs { =20 enum { PERF_X86_SIMD_XMM_REGS =3D 16, - PERF_X86_SIMD_VEC_REGS_MAX =3D PERF_X86_SIMD_XMM_REGS, + PERF_X86_SIMD_YMM_REGS =3D 16, + PERF_X86_SIMD_VEC_REGS_MAX =3D PERF_X86_SIMD_YMM_REGS, }; =20 #define PERF_X86_SIMD_VEC_MASK __GENMASK_ULL(PERF_X86_SIMD_VEC_REGS_MAX - = 1, 0) @@ -65,7 +66,8 @@ enum { enum { /* 1 qword =3D 8 bytes */ PERF_X86_XMM_QWORDS =3D 2, - PERF_X86_SIMD_QWORDS_MAX =3D PERF_X86_XMM_QWORDS, + PERF_X86_YMM_QWORDS =3D 4, + PERF_X86_SIMD_QWORDS_MAX =3D PERF_X86_YMM_QWORDS, }; =20 #endif /* _ASM_X86_PERF_REGS_H */ diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 7b9b38c189de..9792483360c7 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -77,6 +77,8 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) return regs_get_register(regs, pt_regs_offset[idx]); } =20 +#define PERF_X86_YMMH_QWORDS (PERF_X86_YMM_QWORDS / 2) + u64 perf_simd_reg_value(struct pt_regs *regs, int idx, u16 qwords_idx, bool pred) { @@ -98,6 +100,11 @@ u64 perf_simd_reg_value(struct pt_regs *regs, int idx, return 0; return perf_regs->xmm_regs[idx * PERF_X86_XMM_QWORDS + qwords_idx]; + } else if (qwords_idx < PERF_X86_YMM_QWORDS) { + if (!perf_regs->ymmh_regs) + return 0; + return perf_regs->ymmh_regs[idx * PERF_X86_YMMH_QWORDS + + qwords_idx - PERF_X86_XMM_QWORDS]; } =20 return 0; @@ -121,7 +128,8 @@ int perf_simd_reg_validate(u16 simd_enabled, u16 vec_qw= ords, if (vec_mask_intr || vec_mask_user) return -EINVAL; } else { - if (vec_qwords !=3D PERF_X86_XMM_QWORDS) + if (vec_qwords !=3D PERF_X86_XMM_QWORDS && + vec_qwords !=3D PERF_X86_YMM_QWORDS) return -EINVAL; if ((!vec_mask_intr && !vec_mask_user) || (vec_mask_intr & ~PERF_X86_SIMD_VEC_MASK) || --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 687DE3B38AA; Fri, 29 May 2026 08:03:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041835; cv=none; b=IXCN8cPo036kZOFxJ0rI6/rG0+6HKzAQhx6fGNKGQS9GFY3uxsubHsx9Dqzouvqhegki5tOsn39lo/zL+OGJWnyh3Mr0vbH4XD5wSvdp68gnKY52jByTzb1NvL9MeQT9SbENFex/v7jfvKrCOXTFKin5/FeX+XfWOX6zOk6oWfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041835; c=relaxed/simple; bh=sqvdVX88bcOElntzs4UI27D+S9vsqjVJa4hVSgNR1yU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PZJiwqNJHz4QXd0xMZM9X/C2F/KEeR0uM4JbU45jX9yEHF5kXxIbSSpeEUD6GWHOaZzF1TbWEbT6XSVNy//l3TAiuVKovKfFVXZKD+YbWPLtLisumWGqhKL71nf6eGQxvSm8RvQWUFNBB85uOtL/ULymR5wrEdnxbMVjc5wyds8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Wnv54rfW; arc=none smtp.client-ip=192.198.163.7 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.b="Wnv54rfW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041834; x=1811577834; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sqvdVX88bcOElntzs4UI27D+S9vsqjVJa4hVSgNR1yU=; b=Wnv54rfWscrIpSVn7Wgw1OqAWfb2WSdBb0z3F/ZE1RdzUdt6uABLY2tD pR59AZ+Uhc6wL06l6Aj0x54FdBkcMNpMPKjHdEaqFl0IAlabnAkWAMrGa 6ABXzTepvDLdS1IrlwrlgAFpQUGkQ/MGePbbWb31TsZUMzzn3iFl69Z3A B2KLbfD4ARqhia5laOj2mQZPb7/+bPQIIlPEExTs6t4Q34XU8/gR5B9t4 JnY3oZjjaK+Lb9ygCjOiII5/AB1GtTjJvb+iODrPgKL2SkfhcuprQ7pVi abrtwlbNxzPtHEvtrmp/hqaqCeZ3X5Jvv3/xleGymYsVKgOIYW/WUbI/o Q==; X-CSE-ConnectionGUID: b44J/0vVSUOki0D32cfPBw== X-CSE-MsgGUID: MUI9HggGQquruSl+/r0IIw== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342061" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342061" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:54 -0700 X-CSE-ConnectionGUID: 00zmCkjgScqgCGiOAybqyw== X-CSE-MsgGUID: urCE0KETQPKnTCyB40G3Pg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802399" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:49 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 15/23] perf/x86: Support ZMM sampling using sample_simd_vec_reg_* fields Date: Fri, 29 May 2026 15:56:37 +0800 Message-Id: <20260529075645.580362-16-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds support for sampling ZMM registers via the sample_simd_vec_reg_* fields. Each ZMM register consists of 8 u64 words. Current x86 hardware supports up to 32 ZMM registers. For ZMM registers from ZMM0 to ZMM15, they are assembled from three parts: XMM (the lower 2 u64 words), YMMH (the middle 2 u64 words), and ZMMH (the upper 4 u64 words). The perf_simd_reg_value() function is responsible for assembling these three parts into a complete ZMM register for output to userspace. For ZMM registers ZMM16 to ZMM31, each register can be read as a whole and directly outputted to userspace. Additionally, sample_simd_vec_reg_qwords should be set to 8 to indicate ZMM sampling. Please note ZMM sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 16 ++++++++++++++++ arch/x86/events/perf_event.h | 19 +++++++++++++++++++ arch/x86/include/asm/perf_event.h | 8 ++++++++ arch/x86/include/uapi/asm/perf_regs.h | 8 ++++++-- arch/x86/kernel/perf_regs.c | 16 +++++++++++++++- 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index d39710f42ca0..3051a53232c8 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -735,6 +735,12 @@ int x86_pmu_hw_config(struct perf_event *event) if (event_needs_ymm(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_YMM)) return -EINVAL; + if (event_needs_low16_zmm(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_ZMM_Hi256)) + return -EINVAL; + if (event_needs_high16_zmm(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_Hi16_ZMM)) + return -EINVAL; } } =20 @@ -1780,6 +1786,8 @@ void x86_pmu_clear_perf_regs(struct pt_regs *regs) perf_regs->abi =3D PERF_SAMPLE_REGS_ABI_NONE; perf_regs->xmm_regs =3D NULL; perf_regs->ymmh_regs =3D NULL; + perf_regs->zmmh_regs =3D NULL; + perf_regs->h16zmm_regs =3D NULL; } =20 static void update_perf_regs(struct x86_perf_regs *perf_regs, @@ -1797,6 +1805,10 @@ static void update_perf_regs(struct x86_perf_regs *p= erf_regs, perf_regs->xmm_space =3D xsave->i387.xmm_space; if (mask & XFEATURE_MASK_YMM) perf_regs->ymmh =3D get_xsave_addr(xsave, XFEATURE_YMM); + if (mask & XFEATURE_MASK_ZMM_Hi256) + perf_regs->zmmh =3D get_xsave_addr(xsave, XFEATURE_ZMM_Hi256); + if (mask & XFEATURE_MASK_Hi16_ZMM) + perf_regs->h16zmm =3D get_xsave_addr(xsave, XFEATURE_Hi16_ZMM); } =20 /* @@ -1975,6 +1987,10 @@ static void x86_pmu_sample_xregs(struct perf_event *= event, mask |=3D XFEATURE_MASK_SSE; if (event_needs_ymm(event)) mask |=3D XFEATURE_MASK_YMM; + if (event_needs_low16_zmm(event)) + mask |=3D XFEATURE_MASK_ZMM_Hi256; + if (event_needs_high16_zmm(event)) + mask |=3D XFEATURE_MASK_Hi16_ZMM; =20 mask &=3D x86_pmu.ext_regs_mask; if (sample_type & PERF_SAMPLE_REGS_USER) { diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 5111eaf8b12a..53c5802317bb 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -168,6 +168,25 @@ static inline bool event_needs_ymm(struct perf_event *= event) return false; } =20 +static inline bool event_needs_low16_zmm(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + event->attr.sample_simd_vec_reg_qwords >=3D PERF_X86_ZMM_QWORDS) + return true; + + return false; +} + +static inline bool event_needs_high16_zmm(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + (fls64(event->attr.sample_simd_vec_reg_intr) > PERF_X86_H16ZMM_BASE || + fls64(event->attr.sample_simd_vec_reg_user) > PERF_X86_H16ZMM_BASE)) + return true; + + return false; +} + struct amd_nb { int nb_id; /* NorthBridge id */ int refcnt; /* reference count */ diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index 1d03b86be65d..273840bd7b33 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -735,6 +735,14 @@ struct x86_perf_regs { u64 *ymmh_regs; struct ymmh_struct *ymmh; }; + union { + u64 *zmmh_regs; + struct avx_512_zmm_uppers_state *zmmh; + }; + union { + u64 *h16zmm_regs; + struct avx_512_hi16_state *h16zmm; + }; }; =20 extern unsigned long perf_arch_instruction_pointer(struct pt_regs *regs); diff --git a/arch/x86/include/uapi/asm/perf_regs.h b/arch/x86/include/uapi/= asm/perf_regs.h index 8f513229fbb8..3aacdd4e2764 100644 --- a/arch/x86/include/uapi/asm/perf_regs.h +++ b/arch/x86/include/uapi/asm/perf_regs.h @@ -58,16 +58,20 @@ enum perf_event_x86_regs { enum { PERF_X86_SIMD_XMM_REGS =3D 16, PERF_X86_SIMD_YMM_REGS =3D 16, - PERF_X86_SIMD_VEC_REGS_MAX =3D PERF_X86_SIMD_YMM_REGS, + PERF_X86_SIMD_ZMM_REGS =3D 32, + PERF_X86_SIMD_VEC_REGS_MAX =3D PERF_X86_SIMD_ZMM_REGS, }; =20 #define PERF_X86_SIMD_VEC_MASK __GENMASK_ULL(PERF_X86_SIMD_VEC_REGS_MAX - = 1, 0) =20 +#define PERF_X86_H16ZMM_BASE 16 + enum { /* 1 qword =3D 8 bytes */ PERF_X86_XMM_QWORDS =3D 2, PERF_X86_YMM_QWORDS =3D 4, - PERF_X86_SIMD_QWORDS_MAX =3D PERF_X86_YMM_QWORDS, + PERF_X86_ZMM_QWORDS =3D 8, + PERF_X86_SIMD_QWORDS_MAX =3D PERF_X86_ZMM_QWORDS, }; =20 #endif /* _ASM_X86_PERF_REGS_H */ diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 9792483360c7..3c28f28de1e6 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -78,6 +78,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) } =20 #define PERF_X86_YMMH_QWORDS (PERF_X86_YMM_QWORDS / 2) +#define PERF_X86_ZMMH_QWORDS (PERF_X86_ZMM_QWORDS / 2) =20 u64 perf_simd_reg_value(struct pt_regs *regs, int idx, u16 qwords_idx, bool pred) @@ -95,6 +96,13 @@ u64 perf_simd_reg_value(struct pt_regs *regs, int idx, qwords_idx >=3D PERF_X86_SIMD_QWORDS_MAX)) return 0; =20 + if (idx >=3D PERF_X86_H16ZMM_BASE) { + if (!perf_regs->h16zmm_regs) + return 0; + return perf_regs->h16zmm_regs[(idx - PERF_X86_H16ZMM_BASE) * + PERF_X86_ZMM_QWORDS + qwords_idx]; + } + if (qwords_idx < PERF_X86_XMM_QWORDS) { if (!perf_regs->xmm_regs) return 0; @@ -105,6 +113,11 @@ u64 perf_simd_reg_value(struct pt_regs *regs, int idx, return 0; return perf_regs->ymmh_regs[idx * PERF_X86_YMMH_QWORDS + qwords_idx - PERF_X86_XMM_QWORDS]; + } else if (qwords_idx < PERF_X86_ZMM_QWORDS) { + if (!perf_regs->zmmh_regs) + return 0; + return perf_regs->zmmh_regs[idx * PERF_X86_ZMMH_QWORDS + + qwords_idx - PERF_X86_YMM_QWORDS]; } =20 return 0; @@ -129,7 +142,8 @@ int perf_simd_reg_validate(u16 simd_enabled, u16 vec_qw= ords, return -EINVAL; } else { if (vec_qwords !=3D PERF_X86_XMM_QWORDS && - vec_qwords !=3D PERF_X86_YMM_QWORDS) + vec_qwords !=3D PERF_X86_YMM_QWORDS && + vec_qwords !=3D PERF_X86_ZMM_QWORDS) return -EINVAL; if ((!vec_mask_intr && !vec_mask_user) || (vec_mask_intr & ~PERF_X86_SIMD_VEC_MASK) || --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CAEB3B38AA; Fri, 29 May 2026 08:03:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041840; cv=none; b=uqo9xZn2wn4qtaDG2iALViobRelk6pJvKu+jAdr24h4w7m8MgnrAJAbknjGD5hg7IH1APimpUUzN/a5W47nnKLN7hfyCj4b7R52kS4lqpUTKyRqGBwZp/TrDy+JZm/AlB0N7/FpWn1+ryX00ZEkInO5PcsQlBB8/xx/FPD4/IAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041840; c=relaxed/simple; bh=rZBr9qflDrLN/WbZSugLXTx8IysHRIo0zqEmyduN7y0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z6ZuFS4ihV3hjaVhBEED+olTKfm6PDr4eEZI03QSRui7RqSEWvNARTxylBZZ4JUgXurRTQRoPwQhkUkruQU4oIPXFgb5G1UxiK79c58BcL5p0NGSYo9qFw564tmQUR2AerJzks8D2dbAnvjynSb74+TdEib8vHVW0sPt0P8PUus= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Mb2YtgGr; arc=none smtp.client-ip=192.198.163.7 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.b="Mb2YtgGr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041839; x=1811577839; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rZBr9qflDrLN/WbZSugLXTx8IysHRIo0zqEmyduN7y0=; b=Mb2YtgGrbZjNFliCyCG9zOv2z7sRrJvRGVHshcedOfQnz4NK9j6CmtCM saNzWuJ/TzH9cqDTwuheLkRJAeaDajtk92S8doNEKxeoNVBw+PbuE9Cnt +vXTaE63ctq31BP4IUdiTdcHX/sATrLHkfpFBRECsczs6600wyFzDL1i3 8YisPQLJkeRXdF3jr57xY8tGr+D+X7ZFqTPej1GRpQPiLmF/tPxY8ux4X U8q4+I52uk2G7hdGofrgqWzexssxF8yrqrNsF/0qhYx5A7wW1d5IsiIq3 qmDB43hxX+S64Ttd6cc11JHkML2oUA6bxt/lujtKUC6FBUNz4EtlVJfTV Q==; X-CSE-ConnectionGUID: 5I9eQNwGQw+phzBr+7imyg== X-CSE-MsgGUID: W+ioE4C+S/KcPBx3i28P/Q== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342079" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342079" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:03:59 -0700 X-CSE-ConnectionGUID: e+l6Y8/pTEy+rn/H7ojV7Q== X-CSE-MsgGUID: TuvHjGh3RWKRUpdXDMaUWg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802418" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:54 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 16/23] perf/x86: Support OPMASK sampling using sample_simd_pred_reg_* fields Date: Fri, 29 May 2026 15:56:38 +0800 Message-Id: <20260529075645.580362-17-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds support for sampling OPMASK registers via the sample_simd_pred_reg_* fields. Each OPMASK register consists of 1 u64 word. Current x86 hardware supports 8 OPMASK registers. The perf_simd_reg_value() function is responsible for outputting OPMASK value to userspace. Additionally, sample_simd_pred_reg_qwords should be set to 1 to indicate OPMASK sampling. Please note OPMASK sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 8 ++++++++ arch/x86/events/perf_event.h | 10 ++++++++++ arch/x86/include/asm/perf_event.h | 4 ++++ arch/x86/include/uapi/asm/perf_regs.h | 5 +++++ arch/x86/kernel/perf_regs.c | 23 +++++++++++++++++++---- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 3051a53232c8..d4516d3b5d5a 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -741,6 +741,9 @@ int x86_pmu_hw_config(struct perf_event *event) if (event_needs_high16_zmm(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_Hi16_ZMM)) return -EINVAL; + if (event_needs_opmask(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_OPMASK)) + return -EINVAL; } } =20 @@ -1788,6 +1791,7 @@ void x86_pmu_clear_perf_regs(struct pt_regs *regs) perf_regs->ymmh_regs =3D NULL; perf_regs->zmmh_regs =3D NULL; perf_regs->h16zmm_regs =3D NULL; + perf_regs->opmask_regs =3D NULL; } =20 static void update_perf_regs(struct x86_perf_regs *perf_regs, @@ -1809,6 +1813,8 @@ static void update_perf_regs(struct x86_perf_regs *pe= rf_regs, perf_regs->zmmh =3D get_xsave_addr(xsave, XFEATURE_ZMM_Hi256); if (mask & XFEATURE_MASK_Hi16_ZMM) perf_regs->h16zmm =3D get_xsave_addr(xsave, XFEATURE_Hi16_ZMM); + if (mask & XFEATURE_MASK_OPMASK) + perf_regs->opmask =3D get_xsave_addr(xsave, XFEATURE_OPMASK); } =20 /* @@ -1991,6 +1997,8 @@ static void x86_pmu_sample_xregs(struct perf_event *e= vent, mask |=3D XFEATURE_MASK_ZMM_Hi256; if (event_needs_high16_zmm(event)) mask |=3D XFEATURE_MASK_Hi16_ZMM; + if (event_needs_opmask(event)) + mask |=3D XFEATURE_MASK_OPMASK; =20 mask &=3D x86_pmu.ext_regs_mask; if (sample_type & PERF_SAMPLE_REGS_USER) { diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 53c5802317bb..22b846999cfa 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -187,6 +187,16 @@ static inline bool event_needs_high16_zmm(struct perf_= event *event) return false; } =20 +static inline bool event_needs_opmask(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + (event->attr.sample_simd_pred_reg_intr || + event->attr.sample_simd_pred_reg_user)) + return true; + + return false; +} + struct amd_nb { int nb_id; /* NorthBridge id */ int refcnt; /* reference count */ diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index 273840bd7b33..7e8b60bddd5a 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -743,6 +743,10 @@ struct x86_perf_regs { u64 *h16zmm_regs; struct avx_512_hi16_state *h16zmm; }; + union { + u64 *opmask_regs; + struct avx_512_opmask_state *opmask; + }; }; =20 extern unsigned long perf_arch_instruction_pointer(struct pt_regs *regs); diff --git a/arch/x86/include/uapi/asm/perf_regs.h b/arch/x86/include/uapi/= asm/perf_regs.h index 3aacdd4e2764..24c981ba8bae 100644 --- a/arch/x86/include/uapi/asm/perf_regs.h +++ b/arch/x86/include/uapi/asm/perf_regs.h @@ -60,14 +60,19 @@ enum { PERF_X86_SIMD_YMM_REGS =3D 16, PERF_X86_SIMD_ZMM_REGS =3D 32, PERF_X86_SIMD_VEC_REGS_MAX =3D PERF_X86_SIMD_ZMM_REGS, + + PERF_X86_SIMD_OPMASK_REGS =3D 8, + PERF_X86_SIMD_PRED_REGS_MAX =3D PERF_X86_SIMD_OPMASK_REGS, }; =20 +#define PERF_X86_SIMD_PRED_MASK __GENMASK(PERF_X86_SIMD_PRED_REGS_MAX - 1,= 0) #define PERF_X86_SIMD_VEC_MASK __GENMASK_ULL(PERF_X86_SIMD_VEC_REGS_MAX - = 1, 0) =20 #define PERF_X86_H16ZMM_BASE 16 =20 enum { /* 1 qword =3D 8 bytes */ + PERF_X86_OPMASK_QWORDS =3D 1, PERF_X86_XMM_QWORDS =3D 2, PERF_X86_YMM_QWORDS =3D 4, PERF_X86_ZMM_QWORDS =3D 8, diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 3c28f28de1e6..21b282be8ab9 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -89,8 +89,14 @@ u64 perf_simd_reg_value(struct pt_regs *regs, int idx, if (!(perf_regs->abi & PERF_SAMPLE_REGS_ABI_SIMD)) return 0; =20 - if (pred) - return 0; + if (pred) { + if (WARN_ON_ONCE(idx >=3D PERF_X86_SIMD_PRED_REGS_MAX || + qwords_idx >=3D PERF_X86_OPMASK_QWORDS)) + return 0; + if (!perf_regs->opmask_regs) + return 0; + return perf_regs->opmask_regs[idx]; + } =20 if (WARN_ON_ONCE(idx >=3D PERF_X86_SIMD_VEC_REGS_MAX || qwords_idx >=3D PERF_X86_SIMD_QWORDS_MAX)) @@ -151,8 +157,17 @@ int perf_simd_reg_validate(u16 simd_enabled, u16 vec_q= words, return -EINVAL; } =20 - if (pred_qwords || pred_mask_intr || pred_mask_user) - return -EINVAL; + if (!pred_qwords) { + if (pred_mask_intr || pred_mask_user) + return -EINVAL; + } else { + if (pred_qwords !=3D PERF_X86_OPMASK_QWORDS) + return -EINVAL; + if ((!pred_mask_intr && !pred_mask_user) || + (pred_mask_intr & ~PERF_X86_SIMD_PRED_MASK) || + (pred_mask_user & ~PERF_X86_SIMD_PRED_MASK)) + return -EINVAL; + } =20 size =3D ((vec_qwords * hweight64(vec_mask_intr)) + (vec_qwords * hweight64(vec_mask_user)) + --=20 2.34.1 From nobody Mon Jun 8 12:11:55 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F2A63B38AA; Fri, 29 May 2026 08:04:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041845; cv=none; b=tGjNV9RGOdUOe9bnRPYdxeUt5QTXigo8yqBRg2xztajk0CFVlgUubwHfr7qwthBwEP4ApcKbAHbZiTCNdHdVz+s2UORfbNN15aX+BrYJSSBIUOFlQXoeg/3fn6pffB6lTYaqDbDylJNZeth2Rq1KlJgkaxYYUWQE+vR97s2037I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041845; c=relaxed/simple; bh=zji4kzmOkTMEUSi2LnhVML9fZRR/rYsSGCekPnWAnGk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jV58RXL8/DY8p8VcFfIkY58I9ArDw3OyLKu7nbwlOt57iE+QF27F3Q+rIhz8bhGp2rZX5TiUSXjnXUF4mVMIS+G9+KdqQIuwRgim1wGenUdl8Ueol+eHE3NOgmrQJXDNLLSd4fOgXw6oqc599W89y24Td+7PY9oM2gt3E+N0E08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jUGghkDo; arc=none smtp.client-ip=192.198.163.7 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.b="jUGghkDo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041844; x=1811577844; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zji4kzmOkTMEUSi2LnhVML9fZRR/rYsSGCekPnWAnGk=; b=jUGghkDoJg04wclTVCZ0mBVZagX5lR7Cj3z0SwuqO36e4+SBof5X2DIP rpj+RTJOAa08sGAyy5jjqvjTzHnx16bKnyqEjdd6FaUVZohmWMBmHFiZW 8HZKENH4q2Sr0q3wj4IhQOivQ66BuS+l9ud5xDN9FqzoAZ3/nJmdb0yRA 9d46iGKa2TLCjtWd3e1EZPr0RWK+2bf+gzHOUuxLDbc42iZzqXCMPger3 ozODOuLfygDe2dfTFKACYUixZ7Qk+HCKtGfSnOkhqGU+0GsTDphF7ANJA TOaH/fSCxGswj/spCxBswNfQk/8SPmni1mgTJ65eEduSfvHhatNH3E8N8 w==; X-CSE-ConnectionGUID: 1FRgVifpT160ZP3wSt1rVw== X-CSE-MsgGUID: 9g7nIiEqR3a0FZTvxe2SxQ== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342094" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342094" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:04 -0700 X-CSE-ConnectionGUID: GtWtFA1RTvKuaM3rIRY0Ug== X-CSE-MsgGUID: G11wQUw+RHGknPHLD9BJ4A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802440" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:03:59 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 17/23] perf: Enhance perf_reg_validate() with simd_enabled argument Date: Fri, 29 May 2026 15:56:39 +0800 Message-Id: <20260529075645.580362-18-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The upcoming patch will support x86 APX eGPRs sampling by using the reclaimed XMM register space to represent eGPRs in sample_regs_* fields. To differentiate between XMM and eGPRs in sample_regs_* fields, an additional argument, simd_enabled, is introduced to the perf_reg_validate() helper. If simd_enabled is set to 1, it indicates that eGPRs are represented in sample_regs_* fields for the x86 platform; otherwise, XMM registers are represented. Signed-off-by: Dapeng Mi --- arch/arm/kernel/perf_regs.c | 2 +- arch/arm64/kernel/perf_regs.c | 2 +- arch/csky/kernel/perf_regs.c | 2 +- arch/loongarch/kernel/perf_regs.c | 2 +- arch/mips/kernel/perf_regs.c | 2 +- arch/parisc/kernel/perf_regs.c | 2 +- arch/powerpc/perf/perf_regs.c | 2 +- arch/riscv/kernel/perf_regs.c | 2 +- arch/s390/kernel/perf_regs.c | 2 +- arch/x86/kernel/perf_regs.c | 4 ++-- include/linux/perf_regs.h | 2 +- kernel/events/core.c | 8 +++++--- 12 files changed, 17 insertions(+), 15 deletions(-) diff --git a/arch/arm/kernel/perf_regs.c b/arch/arm/kernel/perf_regs.c index d575a4c3ca56..838d701adf4d 100644 --- a/arch/arm/kernel/perf_regs.c +++ b/arch/arm/kernel/perf_regs.c @@ -18,7 +18,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 #define REG_RESERVED (~((1ULL << PERF_REG_ARM_MAX) - 1)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask || mask & REG_RESERVED) return -EINVAL; diff --git a/arch/arm64/kernel/perf_regs.c b/arch/arm64/kernel/perf_regs.c index 70e2f13f587f..71a3e0238de4 100644 --- a/arch/arm64/kernel/perf_regs.c +++ b/arch/arm64/kernel/perf_regs.c @@ -77,7 +77,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 #define REG_RESERVED (~((1ULL << PERF_REG_ARM64_MAX) - 1)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { u64 reserved_mask =3D REG_RESERVED; =20 diff --git a/arch/csky/kernel/perf_regs.c b/arch/csky/kernel/perf_regs.c index 94601f37b596..c932a96afc56 100644 --- a/arch/csky/kernel/perf_regs.c +++ b/arch/csky/kernel/perf_regs.c @@ -18,7 +18,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 #define REG_RESERVED (~((1ULL << PERF_REG_CSKY_MAX) - 1)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask || mask & REG_RESERVED) return -EINVAL; diff --git a/arch/loongarch/kernel/perf_regs.c b/arch/loongarch/kernel/perf= _regs.c index 8dd604f01745..164514f40ae0 100644 --- a/arch/loongarch/kernel/perf_regs.c +++ b/arch/loongarch/kernel/perf_regs.c @@ -25,7 +25,7 @@ u64 perf_reg_abi(struct task_struct *tsk) } #endif /* CONFIG_32BIT */ =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask) return -EINVAL; diff --git a/arch/mips/kernel/perf_regs.c b/arch/mips/kernel/perf_regs.c index 7736d3c5ebd2..00a5201dbd5d 100644 --- a/arch/mips/kernel/perf_regs.c +++ b/arch/mips/kernel/perf_regs.c @@ -28,7 +28,7 @@ u64 perf_reg_abi(struct task_struct *tsk) } #endif /* CONFIG_32BIT */ =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask) return -EINVAL; diff --git a/arch/parisc/kernel/perf_regs.c b/arch/parisc/kernel/perf_regs.c index b9fe1f2fcb9b..4f21aab5405c 100644 --- a/arch/parisc/kernel/perf_regs.c +++ b/arch/parisc/kernel/perf_regs.c @@ -34,7 +34,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 #define REG_RESERVED (~((1ULL << PERF_REG_PARISC_MAX) - 1)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask || mask & REG_RESERVED) return -EINVAL; diff --git a/arch/powerpc/perf/perf_regs.c b/arch/powerpc/perf/perf_regs.c index 350dccb0143c..a01d8a903640 100644 --- a/arch/powerpc/perf/perf_regs.c +++ b/arch/powerpc/perf/perf_regs.c @@ -125,7 +125,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) return regs_get_register(regs, pt_regs_offset[idx]); } =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask || mask & REG_RESERVED) return -EINVAL; diff --git a/arch/riscv/kernel/perf_regs.c b/arch/riscv/kernel/perf_regs.c index 3bba8deababb..1ecc8760b88b 100644 --- a/arch/riscv/kernel/perf_regs.c +++ b/arch/riscv/kernel/perf_regs.c @@ -18,7 +18,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 #define REG_RESERVED (~((1ULL << PERF_REG_RISCV_MAX) - 1)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask || mask & REG_RESERVED) return -EINVAL; diff --git a/arch/s390/kernel/perf_regs.c b/arch/s390/kernel/perf_regs.c index 7b305f1456f8..6496fd23c540 100644 --- a/arch/s390/kernel/perf_regs.c +++ b/arch/s390/kernel/perf_regs.c @@ -34,7 +34,7 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) =20 #define REG_RESERVED (~((1UL << PERF_REG_S390_MAX) - 1)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { if (!mask || mask & REG_RESERVED) return -EINVAL; diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 21b282be8ab9..79803b3b6d6b 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -192,7 +192,7 @@ int perf_simd_reg_validate(u16 simd_enabled, u16 vec_qw= ords, (1ULL << PERF_REG_X86_R14) | \ (1ULL << PERF_REG_X86_R15)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { /* The mask could be 0 if only the SIMD registers are interested */ if (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)) @@ -212,7 +212,7 @@ u64 perf_reg_abi(struct task_struct *task) (1ULL << PERF_REG_X86_FS) | \ (1ULL << PERF_REG_X86_GS)) =20 -int perf_reg_validate(u64 mask) +int perf_reg_validate(u64 mask, bool simd_enabled) { /* The mask could be 0 if only the SIMD registers are interested */ if (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)) diff --git a/include/linux/perf_regs.h b/include/linux/perf_regs.h index 8fa3eeb14953..496080e12141 100644 --- a/include/linux/perf_regs.h +++ b/include/linux/perf_regs.h @@ -10,7 +10,7 @@ struct perf_regs { }; =20 u64 perf_reg_value(struct pt_regs *regs, int idx); -int perf_reg_validate(u64 mask); +int perf_reg_validate(u64 mask, bool simd_enabled); u64 perf_reg_abi(struct task_struct *task); void perf_get_regs_user(struct perf_regs *regs_user, struct pt_regs *regs); diff --git a/kernel/events/core.c b/kernel/events/core.c index afd5b1408231..bf88d9a61f0b 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7774,7 +7774,7 @@ u64 __weak perf_reg_value(struct pt_regs *regs, int i= dx) return 0; } =20 -int __weak perf_reg_validate(u64 mask) +int __weak perf_reg_validate(u64 mask, bool simd_enabled) { return mask ? -ENOSYS : 0; } @@ -13743,7 +13743,8 @@ static int perf_copy_attr(struct perf_event_attr __= user *uattr, } =20 if (attr->sample_type & PERF_SAMPLE_REGS_USER) { - ret =3D perf_reg_validate(attr->sample_regs_user); + ret =3D perf_reg_validate(attr->sample_regs_user, + attr->sample_simd_regs_enabled); if (ret) return ret; ret =3D perf_simd_reg_validate(attr->sample_simd_regs_enabled, @@ -13776,7 +13777,8 @@ static int perf_copy_attr(struct perf_event_attr __= user *uattr, attr->sample_max_stack =3D sysctl_perf_event_max_stack; =20 if (attr->sample_type & PERF_SAMPLE_REGS_INTR) { - ret =3D perf_reg_validate(attr->sample_regs_intr); + ret =3D perf_reg_validate(attr->sample_regs_intr, + attr->sample_simd_regs_enabled); if (ret) return ret; ret =3D perf_simd_reg_validate(attr->sample_simd_regs_enabled, --=20 2.34.1 From nobody Mon Jun 8 12:11:56 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78A573BA222; Fri, 29 May 2026 08:04:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041852; cv=none; b=WEHi+jaBhcOBfbxCyHvPIwP6wiCILp49a/N8+PVXAWT4y680mZr4XxbYmhWqQsJPjSuNK77uA062YjQhr4zInEXrZjIuyneyaAkRdwkdkF2eWOSpPllDJrY7BuM6cLAr8DKOD14Isd3iLA+vcCHoBI36rImDzz5V/4mTi1O2Mj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041852; c=relaxed/simple; bh=lsVhnPtPlS5vmfuMTBFCkTvB3OVqtxoR3bmXqFoTHWU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fnvUB+OWlMbNoEZOLYKOOr4h35EYelqm7MTLZtVM8OELC3AKjPEHy/D0YZt3GY5qU6lUgt0iuJ6z5Mx4Y3INlY0NV6n9ZwP+BeBrWa0j1+vrJQ42zEOSOYXnKIp4yr8pgfP0e7OpTs33bcjLFfXva3AKVOjMc7215dxHhp2USNI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=L0xraY76; arc=none smtp.client-ip=192.198.163.7 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.b="L0xraY76" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041849; x=1811577849; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lsVhnPtPlS5vmfuMTBFCkTvB3OVqtxoR3bmXqFoTHWU=; b=L0xraY765b7xFAHqSdDcRgVJsyz+ttRnebT9arQ/9IhHy8K1u6D5d4JB SiaCUlWg4vKololTNz8BxbWxu7bNaGEpA/AZJRZhmDWobx3K8do+YjvRd Su13azVbmZDjqlYq5uj0Blstb1NBkgFeCKWny4UFZO+vMofkd2swekN9z cI4pjbJMg0M6CikWfwjRbqKVUYfbGATNW16jTYRpKi5qRjLVRbnKZA7xW m8/+Nu0H+bPWS0x0BiCEg30F2LKYqj2D5c1Wyn9tmpDfk8BDUq6zCv38s a4EBVQFkqQzBo2r+ke9tX4BlNr82lA4mfXIUlwN/PHZQpi3WjgTOqEfuA Q==; X-CSE-ConnectionGUID: hiltHg68RgqXPaf9Tdvwcw== X-CSE-MsgGUID: foZQkzbjSMu5D+x7xvOSFg== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342112" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342112" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:09 -0700 X-CSE-ConnectionGUID: kI/NT3ewSx+qDmfIv2XkEw== X-CSE-MsgGUID: QzLGesZiRSGRbRpEh+hxHg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802470" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:04:04 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 18/23] perf/x86: Support eGPRs sampling using sample_regs_* fields Date: Fri, 29 May 2026 15:56:40 +0800 Message-Id: <20260529075645.580362-19-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch supports sampling of APX eGPRs (R16 ~ R31) via the sample_regs_* fields. To sample eGPRs, the sample_simd_regs_enabled field must be set. This allows the spare space (reclaimed from the original XMM space) in the sample_regs_* fields to be used for representing eGPRs. The perf_reg_value() function needs to check if the PERF_SAMPLE_REGS_ABI_SIMD flag is set first, and then determine whether to output eGPRs or legacy XMM registers to userspace. The perf_reg_validate() function first checks the simd_enabled argument to determine if the eGPRs bitmap is represented in sample_regs_* fields. It then validates the eGPRs bitmap accordingly. Currently, eGPRs sampling is only supported on the x86_64 architecture, as APX is only available on x86_64 platforms. Please note eGPRs sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Suggested-by: Peter Zijlstra (Intel) Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 43 ++++++++++++++++++--------- arch/x86/events/intel/core.c | 4 ++- arch/x86/events/perf_event.h | 10 +++++++ arch/x86/include/asm/perf_event.h | 4 +++ arch/x86/include/uapi/asm/perf_regs.h | 26 ++++++++++++++++ arch/x86/kernel/perf_regs.c | 43 ++++++++++++++++----------- 6 files changed, 98 insertions(+), 32 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index d4516d3b5d5a..af874ff3d048 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -708,26 +708,24 @@ int x86_pmu_hw_config(struct perf_event *event) } =20 if (event->attr.sample_type & (PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_U= SER)) { - /* - * Besides the general purpose registers, XMM registers may - * be collected as well. - */ - if (event_has_extended_regs(event)) { - if (!(event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS)) - return -EINVAL; - if (is_sampling_event(event) && !event->attr.precise_ip && - !this_cpu_has(X86_FEATURE_XSAVES)) - return -EINVAL; - if (event->attr.sample_simd_regs_enabled) - return -EINVAL; - } - if (event_has_simd_regs(event)) { + u64 reserved =3D ~GENMASK_ULL(PERF_REG_MISC_MAX - 1, 0); + if (!(event->pmu->capabilities & PERF_PMU_CAP_SIMD_REGS)) return -EINVAL; if (is_sampling_event(event) && !event->attr.precise_ip && !this_cpu_has(X86_FEATURE_XSAVES)) return -EINVAL; + /* + * The XMM space in the perf_event_x86_regs is reclaimed + * for eGPRs and other general registers. + */ + if ((event->attr.sample_regs_user & reserved) || + (event->attr.sample_regs_intr & reserved)) + return -EINVAL; + if (event_needs_egprs(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_APX)) + return -EINVAL; /* The vector registers set is not supported */ if (event_needs_xmm(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_SSE)) @@ -744,6 +742,18 @@ int x86_pmu_hw_config(struct perf_event *event) if (event_needs_opmask(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_OPMASK)) return -EINVAL; + } else { + /* + * Besides the general purpose registers, XMM registers may + * be collected as well. + */ + if (event_has_extended_regs(event)) { + if (!(event->pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS)) + return -EINVAL; + if (is_sampling_event(event) && !event->attr.precise_ip && + !this_cpu_has(X86_FEATURE_XSAVES)) + return -EINVAL; + } } } =20 @@ -1792,6 +1802,7 @@ void x86_pmu_clear_perf_regs(struct pt_regs *regs) perf_regs->zmmh_regs =3D NULL; perf_regs->h16zmm_regs =3D NULL; perf_regs->opmask_regs =3D NULL; + perf_regs->egpr_regs =3D NULL; } =20 static void update_perf_regs(struct x86_perf_regs *perf_regs, @@ -1815,6 +1826,8 @@ static void update_perf_regs(struct x86_perf_regs *pe= rf_regs, perf_regs->h16zmm =3D get_xsave_addr(xsave, XFEATURE_Hi16_ZMM); if (mask & XFEATURE_MASK_OPMASK) perf_regs->opmask =3D get_xsave_addr(xsave, XFEATURE_OPMASK); + if (mask & XFEATURE_MASK_APX) + perf_regs->egpr =3D get_xsave_addr(xsave, XFEATURE_APX); } =20 /* @@ -1999,6 +2012,8 @@ static void x86_pmu_sample_xregs(struct perf_event *e= vent, mask |=3D XFEATURE_MASK_Hi16_ZMM; if (event_needs_opmask(event)) mask |=3D XFEATURE_MASK_OPMASK; + if (event_needs_egprs(event)) + mask |=3D XFEATURE_MASK_APX; =20 mask &=3D x86_pmu.ext_regs_mask; if (sample_type & PERF_SAMPLE_REGS_USER) { diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 6c06558c416f..a2473f962681 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -4698,7 +4698,9 @@ static void intel_pebs_aliases_skl(struct perf_event = *event) static unsigned long intel_pmu_large_pebs_flags(struct perf_event *event) { unsigned long flags =3D x86_pmu.large_pebs_flags; - u64 gprs_mask =3D PEBS_GP_REGS | PERF_REG_EXTENDED_MASK; + u64 gprs_mask =3D event->attr.sample_simd_regs_enabled ? + PEBS_GP_REGS : + PEBS_GP_REGS | PERF_REG_EXTENDED_MASK; =20 if (event->attr.use_clockid) flags &=3D ~PERF_SAMPLE_TIME; diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 22b846999cfa..4cc490aa04fc 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -197,6 +197,16 @@ static inline bool event_needs_opmask(struct perf_even= t *event) return false; } =20 +static inline bool event_needs_egprs(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + (event->attr.sample_regs_user & PERF_X86_EGPRS_MASK || + event->attr.sample_regs_intr & PERF_X86_EGPRS_MASK)) + return true; + + return false; +} + struct amd_nb { int nb_id; /* NorthBridge id */ int refcnt; /* reference count */ diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index 7e8b60bddd5a..a54ea8fa6a04 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -747,6 +747,10 @@ struct x86_perf_regs { u64 *opmask_regs; struct avx_512_opmask_state *opmask; }; + union { + u64 *egpr_regs; + struct apx_state *egpr; + }; }; =20 extern unsigned long perf_arch_instruction_pointer(struct pt_regs *regs); diff --git a/arch/x86/include/uapi/asm/perf_regs.h b/arch/x86/include/uapi/= asm/perf_regs.h index 24c981ba8bae..8774a1290fbe 100644 --- a/arch/x86/include/uapi/asm/perf_regs.h +++ b/arch/x86/include/uapi/asm/perf_regs.h @@ -27,9 +27,34 @@ enum perf_event_x86_regs { PERF_REG_X86_R13, PERF_REG_X86_R14, PERF_REG_X86_R15, + /* + * The eGPRs and XMM have overlaps. Only one can be used + * at a time. The ABI PERF_SAMPLE_REGS_ABI_SIMD is used to + * distinguish which one is used. If PERF_SAMPLE_REGS_ABI_SIMD + * is set, then eGPRs is used, otherwise, XMM is used. + * + * Extended GPRs (eGPRs) + */ + PERF_REG_X86_R16, + PERF_REG_X86_R17, + PERF_REG_X86_R18, + PERF_REG_X86_R19, + PERF_REG_X86_R20, + PERF_REG_X86_R21, + PERF_REG_X86_R22, + PERF_REG_X86_R23, + PERF_REG_X86_R24, + PERF_REG_X86_R25, + PERF_REG_X86_R26, + PERF_REG_X86_R27, + PERF_REG_X86_R28, + PERF_REG_X86_R29, + PERF_REG_X86_R30, + PERF_REG_X86_R31, /* These are the limits for the GPRs. */ PERF_REG_X86_32_MAX =3D PERF_REG_X86_GS + 1, PERF_REG_X86_64_MAX =3D PERF_REG_X86_R15 + 1, + PERF_REG_MISC_MAX =3D PERF_REG_X86_R31 + 1, =20 /* These all need two bits set because they are 128bit */ PERF_REG_X86_XMM0 =3D 32, @@ -54,6 +79,7 @@ enum perf_event_x86_regs { }; =20 #define PERF_REG_EXTENDED_MASK (~((1ULL << PERF_REG_X86_XMM0) - 1)) +#define PERF_X86_EGPRS_MASK __GENMASK_ULL(PERF_REG_X86_R31, PERF_REG_X86_R= 16) =20 enum { PERF_X86_SIMD_XMM_REGS =3D 16, diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 79803b3b6d6b..006883ad443d 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -61,14 +61,24 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) { struct x86_perf_regs *perf_regs; =20 - if (idx >=3D PERF_REG_X86_XMM0 && idx < PERF_REG_X86_XMM_MAX) { + if (idx > PERF_REG_X86_R15) { perf_regs =3D container_of(regs, struct x86_perf_regs, regs); - /* SIMD registers are moved to dedicated sample_simd_vec_reg */ - if (perf_regs->abi & PERF_SAMPLE_REGS_ABI_SIMD) + if (perf_regs->abi =3D=3D PERF_SAMPLE_REGS_ABI_NONE) return 0; - if (!perf_regs->xmm_regs) - return 0; - return perf_regs->xmm_regs[idx - PERF_REG_X86_XMM0]; + + if (perf_regs->abi & PERF_SAMPLE_REGS_ABI_SIMD) { + if (idx <=3D PERF_REG_X86_R31) { + if (!perf_regs->egpr_regs) + return 0; + return perf_regs->egpr_regs[idx - PERF_REG_X86_R16]; + } + } else { + if (idx >=3D PERF_REG_X86_XMM0 && idx < PERF_REG_X86_XMM_MAX) { + if (!perf_regs->xmm_regs) + return 0; + return perf_regs->xmm_regs[idx - PERF_REG_X86_XMM0]; + } + } } =20 if (WARN_ON_ONCE(idx >=3D ARRAY_SIZE(pt_regs_offset))) @@ -179,18 +189,12 @@ int perf_simd_reg_validate(u16 simd_enabled, u16 vec_= qwords, return 0; } =20 -#define PERF_REG_X86_RESERVED (((1ULL << PERF_REG_X86_XMM0) - 1) & \ - ~((1ULL << PERF_REG_X86_MAX) - 1)) +#define PERF_REG_X86_RESERVED (GENMASK_ULL(PERF_REG_X86_XMM0 - 1, PERF_REG= _X86_AX) & \ + ~GENMASK_ULL(PERF_REG_X86_R15, PERF_REG_X86_AX)) +#define PERF_REG_X86_EXT_RESERVED (~GENMASK_ULL(PERF_REG_MISC_MAX - 1, PER= F_REG_X86_AX)) =20 #ifdef CONFIG_X86_32 -#define REG_NOSUPPORT ((1ULL << PERF_REG_X86_R8) | \ - (1ULL << PERF_REG_X86_R9) | \ - (1ULL << PERF_REG_X86_R10) | \ - (1ULL << PERF_REG_X86_R11) | \ - (1ULL << PERF_REG_X86_R12) | \ - (1ULL << PERF_REG_X86_R13) | \ - (1ULL << PERF_REG_X86_R14) | \ - (1ULL << PERF_REG_X86_R15)) +#define REG_NOSUPPORT GENMASK_ULL(PERF_REG_X86_R15, PERF_REG_X86_R8) =20 int perf_reg_validate(u64 mask, bool simd_enabled) { @@ -214,8 +218,13 @@ u64 perf_reg_abi(struct task_struct *task) =20 int perf_reg_validate(u64 mask, bool simd_enabled) { + if (!simd_enabled && + (!mask || (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)))) + return -EINVAL; + /* The mask could be 0 if only the SIMD registers are interested */ - if (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)) + if (simd_enabled && + (mask & (REG_NOSUPPORT | PERF_REG_X86_EXT_RESERVED))) return -EINVAL; =20 return 0; --=20 2.34.1 From nobody Mon Jun 8 12:11:56 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 813773B585F; Fri, 29 May 2026 08:04:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041856; cv=none; b=ZJt2C4tbNTl2vrn8XWqEx+3EhyFPu8quE3Ts3m76iS1w/wzaP1j1v+comLAMmo+SqD1GzQUOTTjKCox+goUsz9HRAdivlLIKinPRi9ULB6948Fbn/83H2awx/EK3N4Jwf+SEUZgJArlJMacN3sb9lKCl6y9RM3Yq6spIRB7pGhg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041856; c=relaxed/simple; bh=CPFIJX+N0i4wombRjgO3CWBDUMt5tp0qbmkDhohEaJA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Zc0xZoUDKqhXeflRnsGT6BxRAeQllA7JjJvDwwGqWKW6Nytq+ggJx+foYDP+pQA0PUCFeSkLi1vQzlyPPsVSkdjmL9QYfcu4gZbUUKIu/zLfqJK8fvLHTautwB7YkOFlKN+GpG5vsr6amcTNKwIclw2FVc0+koCu9WRRvIGFcVo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=A7nRotMl; arc=none smtp.client-ip=192.198.163.7 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.b="A7nRotMl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041854; x=1811577854; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CPFIJX+N0i4wombRjgO3CWBDUMt5tp0qbmkDhohEaJA=; b=A7nRotMl9bM/KQ1dOWK7CU+3HOcIs6q3OavC2xzCDfZPfGpaAOqjUFgD UuYG9AjsVXkLCKP53HVKTrWUbn+XtqoiyBQfz7F5XvWP4x+80lqovHB1/ R04SgFpqYNxBCt8ggFkXh8zpfPuI27B/EijPm+nU8gRGLhs5nqZi4VqC3 cFU+cA5a84S9ePCJhzpg8OhRnDYXVQODq+hCCOgIIcWI6UvIwKmUAuru7 tXW2oxL7BP1eOuHm7Vao1Ln2TZhQr8SfGGSJrYdrqNJ1+g6zWfE99ByKZ LtmDxku8DmX6FMNEfHQYf1kKxMHsQwrpmEKEveEOp+qgBvx+qJYWkE4xf g==; X-CSE-ConnectionGUID: G3P9tl0QQUKKrWk6ikh4fQ== X-CSE-MsgGUID: E1/oXJRtTx+iPxEwfahhUg== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342136" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342136" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:14 -0700 X-CSE-ConnectionGUID: KNuC5qCEQc2oYieT2np7Zg== X-CSE-MsgGUID: /UlGFx/VRrycOCh0dTEnog== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802501" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:04:09 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi , Kan Liang Subject: [Patch v8 19/23] perf/x86: Support SSP sampling using sample_regs_* fields Date: Fri, 29 May 2026 15:56:41 +0800 Message-Id: <20260529075645.580362-20-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch supports sampling of CET SSP register via the sample_regs_* fields. To sample SSP, the sample_simd_regs_enabled field must be set. This allows the spare space (reclaimed from the original XMM space) in the sample_regs_* fields to be used for representing SSP. Similar with eGPRs sampling, the perf_reg_value() function needs to check if the PERF_SAMPLE_REGS_ABI_SIMD flag is set first, and then determine whether to output SSP or legacy XMM registers to userspace. Additionally, arch-PEBS supports sampling SSP, which is placed into the GPRs group. This patch also enables arch-PEBS-based SSP sampling. Currently, SSP sampling is only supported on the x86_64 architecture, as CET is only available on x86_64 platforms. Please note SSP sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Co-developed-by: Kan Liang Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/core.c | 11 +++++++++++ arch/x86/events/intel/ds.c | 15 +++++++++++++-- arch/x86/events/perf_event.h | 10 ++++++++++ arch/x86/include/asm/perf_event.h | 1 + arch/x86/include/uapi/asm/perf_regs.h | 7 ++++--- arch/x86/kernel/perf_regs.c | 5 +++++ 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index af874ff3d048..f990256fb2ff 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -726,6 +726,9 @@ int x86_pmu_hw_config(struct perf_event *event) if (event_needs_egprs(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_APX)) return -EINVAL; + if (event_needs_ssp(event) && + !(x86_pmu.ext_regs_mask & XFEATURE_MASK_CET_USER)) + return -EINVAL; /* The vector registers set is not supported */ if (event_needs_xmm(event) && !(x86_pmu.ext_regs_mask & XFEATURE_MASK_SSE)) @@ -1803,11 +1806,13 @@ void x86_pmu_clear_perf_regs(struct pt_regs *regs) perf_regs->h16zmm_regs =3D NULL; perf_regs->opmask_regs =3D NULL; perf_regs->egpr_regs =3D NULL; + perf_regs->ssp =3D NULL; } =20 static void update_perf_regs(struct x86_perf_regs *perf_regs, struct xregs_state *xsave, u64 bitmap) { + struct cet_user_state *cet; u64 mask; =20 if (!xsave) @@ -1828,6 +1833,10 @@ static void update_perf_regs(struct x86_perf_regs *p= erf_regs, perf_regs->opmask =3D get_xsave_addr(xsave, XFEATURE_OPMASK); if (mask & XFEATURE_MASK_APX) perf_regs->egpr =3D get_xsave_addr(xsave, XFEATURE_APX); + if (mask & XFEATURE_MASK_CET_USER) { + cet =3D get_xsave_addr(xsave, XFEATURE_CET_USER); + perf_regs->ssp =3D cet ? &cet->user_ssp : NULL; + } } =20 /* @@ -2014,6 +2023,8 @@ static void x86_pmu_sample_xregs(struct perf_event *e= vent, mask |=3D XFEATURE_MASK_OPMASK; if (event_needs_egprs(event)) mask |=3D XFEATURE_MASK_APX; + if (event_needs_ssp(event)) + mask |=3D XFEATURE_MASK_CET_USER; =20 mask &=3D x86_pmu.ext_regs_mask; if (sample_type & PERF_SAMPLE_REGS_USER) { diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index 609d4a83115d..fb393be13fcb 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1723,6 +1723,7 @@ static u64 pebs_update_adaptive_cfg(struct perf_event= *event) u64 sample_type =3D attr->sample_type; u64 pebs_data_cfg =3D 0; bool gprs, tsx_weight; + u64 xgprs_mask; =20 if (!(sample_type & ~(PERF_SAMPLE_IP|PERF_SAMPLE_TIME)) && attr->precise_ip > 1) @@ -1737,10 +1738,13 @@ static u64 pebs_update_adaptive_cfg(struct perf_eve= nt *event) * + precise_ip < 2 for the non event IP * + For RTM TSX weight we need GPRs for the abort code. */ + xgprs_mask =3D event->attr.sample_simd_regs_enabled ? + PEBS_GP_REGS | BIT_ULL(PERF_REG_X86_SSP) : + PEBS_GP_REGS; gprs =3D ((sample_type & PERF_SAMPLE_REGS_INTR) && - (attr->sample_regs_intr & PEBS_GP_REGS)) || + (attr->sample_regs_intr & xgprs_mask)) || ((sample_type & PERF_SAMPLE_REGS_USER) && - (attr->sample_regs_user & PEBS_GP_REGS)); + (attr->sample_regs_user & xgprs_mask)); =20 tsx_weight =3D (sample_type & PERF_SAMPLE_WEIGHT_TYPE) && ((attr->config & INTEL_ARCH_EVENT_MASK) =3D=3D @@ -2690,6 +2694,13 @@ static void setup_arch_pebs_sample_data(struct perf_= event *event, __setup_pebs_gpr_group(event, regs, (struct pebs_gprs *)gprs, sample_type); + + /* Currently only user space mode enables SSP. */ + if (user_mode(regs) && (sample_type & + (PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_REGS_USER))) { + perf_regs->ssp =3D &gprs->ssp; + ignore_mask |=3D XFEATURE_MASK_CET_USER; + } } =20 if (header->aux) { diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index 4cc490aa04fc..c521a7fbe9c6 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -207,6 +207,16 @@ static inline bool event_needs_egprs(struct perf_event= *event) return false; } =20 +static inline bool event_needs_ssp(struct perf_event *event) +{ + if (event->attr.sample_simd_regs_enabled && + (event->attr.sample_regs_user & BIT_ULL(PERF_REG_X86_SSP) || + event->attr.sample_regs_intr & BIT_ULL(PERF_REG_X86_SSP))) + return true; + + return false; +} + struct amd_nb { int nb_id; /* NorthBridge id */ int refcnt; /* reference count */ diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index a54ea8fa6a04..2769ec3030e5 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -751,6 +751,7 @@ struct x86_perf_regs { u64 *egpr_regs; struct apx_state *egpr; }; + u64 *ssp; }; =20 extern unsigned long perf_arch_instruction_pointer(struct pt_regs *regs); diff --git a/arch/x86/include/uapi/asm/perf_regs.h b/arch/x86/include/uapi/= asm/perf_regs.h index 8774a1290fbe..31a025cb9dba 100644 --- a/arch/x86/include/uapi/asm/perf_regs.h +++ b/arch/x86/include/uapi/asm/perf_regs.h @@ -28,10 +28,10 @@ enum perf_event_x86_regs { PERF_REG_X86_R14, PERF_REG_X86_R15, /* - * The eGPRs and XMM have overlaps. Only one can be used + * The eGPRs/SSP and XMM have overlaps. Only one can be used * at a time. The ABI PERF_SAMPLE_REGS_ABI_SIMD is used to * distinguish which one is used. If PERF_SAMPLE_REGS_ABI_SIMD - * is set, then eGPRs is used, otherwise, XMM is used. + * is set, then eGPRs/SSP is used, otherwise, XMM is used. * * Extended GPRs (eGPRs) */ @@ -51,10 +51,11 @@ enum perf_event_x86_regs { PERF_REG_X86_R29, PERF_REG_X86_R30, PERF_REG_X86_R31, + PERF_REG_X86_SSP, /* These are the limits for the GPRs. */ PERF_REG_X86_32_MAX =3D PERF_REG_X86_GS + 1, PERF_REG_X86_64_MAX =3D PERF_REG_X86_R15 + 1, - PERF_REG_MISC_MAX =3D PERF_REG_X86_R31 + 1, + PERF_REG_MISC_MAX =3D PERF_REG_X86_SSP + 1, =20 /* These all need two bits set because they are 128bit */ PERF_REG_X86_XMM0 =3D 32, diff --git a/arch/x86/kernel/perf_regs.c b/arch/x86/kernel/perf_regs.c index 006883ad443d..6f0970ed60db 100644 --- a/arch/x86/kernel/perf_regs.c +++ b/arch/x86/kernel/perf_regs.c @@ -72,6 +72,11 @@ u64 perf_reg_value(struct pt_regs *regs, int idx) return 0; return perf_regs->egpr_regs[idx - PERF_REG_X86_R16]; } + if (idx =3D=3D PERF_REG_X86_SSP) { + if (!perf_regs->ssp) + return 0; + return *perf_regs->ssp; + } } else { if (idx >=3D PERF_REG_X86_XMM0 && idx < PERF_REG_X86_XMM_MAX) { if (!perf_regs->xmm_regs) --=20 2.34.1 From nobody Mon Jun 8 12:11:56 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 648383BB68B; Fri, 29 May 2026 08:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041861; cv=none; b=FBUArZpgVijUDzVRAUqn0EfS+33jgu6D51b/03KQK6lZjeJYram42S6mAza+YgvSKAt3tqzOc3Gc/PBBHQGitE/FdMuhxhJjZGGXe9W253JNdFm2vJFNezFJKOb/8p1Qo4W4goAjCOuTs+bCjQVLPTsHtE4fsknoM2TZyFRGRVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041861; c=relaxed/simple; bh=WZ1HBHoN2EzGxfxaKEd2JQqxxNX25CUAJ680lUlOWO0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EAjxe7Ue2YjL3lfmx4UWD6APebbCxLKD48tDhrH9sg+bBkztUaQZT6vyFKFiOd9kUPmp225g5+aYUFfEV5NO+5ThQ33AunXTw661KNdkYdoeJ+tvsc64mLY0k/h4935qgturl4kBVMpdeEAtSy1efLostjwRQXdYi1w7RWPZu9M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=F0Htizk2; arc=none smtp.client-ip=192.198.163.7 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.b="F0Htizk2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041859; x=1811577859; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WZ1HBHoN2EzGxfxaKEd2JQqxxNX25CUAJ680lUlOWO0=; b=F0Htizk2c6q7W2sOvD3dr1XEO2odRzVLXFr41jWJq47roetTa0gntPNV SDJtaUWFCeu33l/2WDvB3X9Wp1zDwDRoRPN7BeIxRS053/b7lRy4kp+d+ NXmxYd+ScK96EFfRdWQB+yOC6BinnFAcNb1unWt/wAQeEDCMieHfo4uGl ITgHqNR31ZbgHqc3IeNPGc+bP8K41Pu34JPYRfkmZmqI4fUqh0aEEFk8S qayWv4hYRsk1dg9DC0y7nblC0ynnWC2dkKQ0vSRxFwdlzJ4xTng9AHKZW LeeJn8IC1Fws92CK9uY0nqhMGRPLbaiYT0bL9krJWpVzrLKbfvltquWRF A==; X-CSE-ConnectionGUID: 2ZeNfhGQRhqerZs3+3Yd+g== X-CSE-MsgGUID: h6WriYtwRyqXBHkjmAzw6A== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342167" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342167" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:19 -0700 X-CSE-ConnectionGUID: xYYbCsoLSo6L+ZVCxU4r4g== X-CSE-MsgGUID: 8tIGK3raTm6NQ4MW3zva7A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802535" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:04:14 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 20/23] perf/x86/intel: Support arch-PEBS based SIMD/eGPRs/SSP sampling Date: Fri, 29 May 2026 15:56:42 +0800 Message-Id: <20260529075645.580362-21-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch supports arch-PEBS based SIMD/eGPRs/SSP registers sampling. Arch-PEBS supports sampling of these registers, with all except SSP placed into the XSAVE-Enabled Registers (XER) group with the layout described below. Field Name Registers Used Size XSTATE_BV XINUSE for groups 8 B Reserved Reserved 8 B SSER XMM0-XMM15 16 regs * 16 B =3D 256 B YMMHIR Upper 128 bits of YMM0-YMM15 16 regs * 16 B =3D 256 B EGPR R16-R31 16 regs * 8 B =3D 128 B OPMASKR K0-K7 8 regs * 8 B =3D 64 B ZMMHIR Upper 256 bits of ZMM0-ZMM15 16 regs * 32 B =3D 512 B Hi16ZMMR ZMM16-ZMM31 16 regs * 64 B =3D 1024 B Memory space in the output buffer is allocated for these sub-groups as long as the corresponding Format.XER[55:49] bits in the PEBS record header are set. However, the arch-PEBS hardware engine does not write the sub-group if it is not used (in INIT state). In such cases, the corresponding bit in the XSTATE_BV bitmap is set to 0. Therefore, the XSTATE_BV field is checked to determine if the register data is actually written for each PEBS record. If not, the register data is not outputted to userspace. The SSP register is sampled and placed into the GPRs group by arch-PEBS. Additionally, the MSRs IA32_PMC_{GPn|FXm}_CFG_C.[55:49] bits are used to manage which types of these registers need to be sampled. Please note arch-PEBS based SIMD/eGPRs/SSP sampling is not enabled yet, it will be enabled in a later patch when PERF_PMU_CAP_SIMD_REGS is set. Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 121 ++++++++++++++++++++++++++++-- arch/x86/events/intel/ds.c | 77 +++++++++++++++++-- arch/x86/include/asm/msr-index.h | 7 ++ arch/x86/include/asm/perf_event.h | 8 +- 4 files changed, 199 insertions(+), 14 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index a2473f962681..679781519f8c 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3473,6 +3473,21 @@ static void intel_pmu_enable_event_ext(struct perf_e= vent *event) if (pebs_data_cfg & PEBS_DATACFG_XMMS) ext |=3D ARCH_PEBS_VECR_XMM & cap.caps; =20 + if (pebs_data_cfg & PEBS_DATACFG_YMMHS) + ext |=3D ARCH_PEBS_VECR_YMMH & cap.caps; + + if (pebs_data_cfg & PEBS_DATACFG_EGPRS) + ext |=3D ARCH_PEBS_VECR_EGPRS & cap.caps; + + if (pebs_data_cfg & PEBS_DATACFG_OPMASKS) + ext |=3D ARCH_PEBS_VECR_OPMASK & cap.caps; + + if (pebs_data_cfg & PEBS_DATACFG_ZMMHS) + ext |=3D ARCH_PEBS_VECR_ZMMH & cap.caps; + + if (pebs_data_cfg & PEBS_DATACFG_H16ZMMS) + ext |=3D ARCH_PEBS_VECR_H16ZMM & cap.caps; + if (pebs_data_cfg & PEBS_DATACFG_LBRS) ext |=3D ARCH_PEBS_LBR & cap.caps; =20 @@ -4695,21 +4710,113 @@ static void intel_pebs_aliases_skl(struct perf_eve= nt *event) return intel_pebs_aliases_precdist(event); } =20 +static inline bool intel_pebs_support_regs(struct perf_event *event, u64 r= egs) +{ + struct arch_pebs_cap cap =3D hybrid(event->pmu, arch_pebs_cap); + int pebs_format =3D x86_pmu.intel_cap.pebs_format; + bool supported =3D true; + + if (regs & PEBS_DATACFG_GP) { + /* Legacy PEBS always supports GPRs sampling. */ + supported &=3D x86_pmu.arch_pebs ? + !!(ARCH_PEBS_GPR & cap.caps) : true; + } + if (regs & PEBS_DATACFG_XMMS) { + supported &=3D x86_pmu.arch_pebs ? + !!(ARCH_PEBS_VECR_XMM & cap.caps) : + pebs_format > 3 && x86_pmu.intel_cap.pebs_baseline; + } + /* Legacy PEBS doesn't support OPMASK/YMM+ and eGPRs sampling. */ + if (regs & PEBS_DATACFG_YMMHS) + supported &=3D x86_pmu.arch_pebs && (ARCH_PEBS_VECR_YMMH & cap.caps); + if (regs & PEBS_DATACFG_EGPRS) + supported &=3D x86_pmu.arch_pebs && (ARCH_PEBS_VECR_EGPRS & cap.caps); + if (regs & PEBS_DATACFG_OPMASKS) + supported &=3D x86_pmu.arch_pebs && (ARCH_PEBS_VECR_OPMASK & cap.caps); + if (regs & PEBS_DATACFG_ZMMHS) + supported &=3D x86_pmu.arch_pebs && (ARCH_PEBS_VECR_ZMMH & cap.caps); + if (regs & PEBS_DATACFG_H16ZMMS) + supported &=3D x86_pmu.arch_pebs && (ARCH_PEBS_VECR_H16ZMM & cap.caps); + + return supported; +} + +static bool __regs_support_large_pebs(struct perf_event *event, bool intr) +{ + u64 regs =3D intr ? event->attr.sample_regs_intr : + event->attr.sample_regs_user; + u64 vec_regs =3D intr ? event->attr.sample_simd_vec_reg_intr : + event->attr.sample_simd_vec_reg_user; + u64 pred_regs =3D intr ? event->attr.sample_simd_pred_reg_intr : + event->attr.sample_simd_pred_reg_user; + u64 xregs_mask =3D PEBS_GP_REGS | PERF_X86_EGPRS_MASK | + BIT_ULL(PERF_REG_X86_SSP); + + if (regs & ~xregs_mask) + return false; + + if ((regs & (PEBS_GP_REGS | BIT_ULL(PERF_REG_X86_SSP))) && + !intel_pebs_support_regs(event, PEBS_DATACFG_GP)) + return false; + + if ((regs & PERF_X86_EGPRS_MASK) && + !intel_pebs_support_regs(event, PEBS_DATACFG_EGPRS)) + return false; + + if (event_needs_opmask(event) && pred_regs && + !intel_pebs_support_regs(event, PEBS_DATACFG_OPMASKS)) + return false; + + if (event_needs_xmm(event) && vec_regs && + !intel_pebs_support_regs(event, PEBS_DATACFG_XMMS)) + return false; + + if (event_needs_ymm(event) && vec_regs && + !intel_pebs_support_regs(event, PEBS_DATACFG_YMMHS)) + return false; + + if (event_needs_low16_zmm(event) && vec_regs && + !intel_pebs_support_regs(event, PEBS_DATACFG_ZMMHS)) + return false; + + if (event_needs_high16_zmm(event) && vec_regs && + !intel_pebs_support_regs(event, PEBS_DATACFG_H16ZMMS)) + return false; + + return true; +} + +static inline bool intr_regs_support_large_pebs(struct perf_event *event) +{ + return __regs_support_large_pebs(event, /*intr=3D*/true); +} + +static inline bool user_regs_support_large_pebs(struct perf_event *event) +{ + return __regs_support_large_pebs(event, /*intr=3D*/false); +} + static unsigned long intel_pmu_large_pebs_flags(struct perf_event *event) { unsigned long flags =3D x86_pmu.large_pebs_flags; - u64 gprs_mask =3D event->attr.sample_simd_regs_enabled ? - PEBS_GP_REGS : - PEBS_GP_REGS | PERF_REG_EXTENDED_MASK; =20 if (event->attr.use_clockid) flags &=3D ~PERF_SAMPLE_TIME; if (!event->attr.exclude_kernel) flags &=3D ~PERF_SAMPLE_REGS_USER; - if (event->attr.sample_regs_user & ~gprs_mask) - flags &=3D ~PERF_SAMPLE_REGS_USER; - if (event->attr.sample_regs_intr & ~gprs_mask) - flags &=3D ~PERF_SAMPLE_REGS_INTR; + if (event->attr.sample_simd_regs_enabled) { + if (!user_regs_support_large_pebs(event)) + flags &=3D ~PERF_SAMPLE_REGS_USER; + if (!intr_regs_support_large_pebs(event)) + flags &=3D ~PERF_SAMPLE_REGS_INTR; + } else { + u64 gprs_mask =3D PEBS_GP_REGS | PERF_REG_EXTENDED_MASK; + + if (event->attr.sample_regs_user & ~gprs_mask) + flags &=3D ~PERF_SAMPLE_REGS_USER; + if (event->attr.sample_regs_intr & ~gprs_mask) + flags &=3D ~PERF_SAMPLE_REGS_INTR; + } return flags; } =20 diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index fb393be13fcb..8a653edce392 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1750,11 +1750,22 @@ static u64 pebs_update_adaptive_cfg(struct perf_eve= nt *event) ((attr->config & INTEL_ARCH_EVENT_MASK) =3D=3D x86_pmu.rtm_abort_event); =20 - if (gprs || (attr->precise_ip < 2) || tsx_weight) + if (gprs || (attr->precise_ip < 2) || + tsx_weight || event_needs_ssp(event)) pebs_data_cfg |=3D PEBS_DATACFG_GP; =20 if (event_needs_xmm(event)) pebs_data_cfg |=3D PEBS_DATACFG_XMMS; + if (x86_pmu.arch_pebs && event_needs_ymm(event)) + pebs_data_cfg |=3D PEBS_DATACFG_YMMHS; + if (x86_pmu.arch_pebs && event_needs_low16_zmm(event)) + pebs_data_cfg |=3D PEBS_DATACFG_ZMMHS; + if (x86_pmu.arch_pebs && event_needs_high16_zmm(event)) + pebs_data_cfg |=3D PEBS_DATACFG_H16ZMMS; + if (x86_pmu.arch_pebs && event_needs_opmask(event)) + pebs_data_cfg |=3D PEBS_DATACFG_OPMASKS; + if (x86_pmu.arch_pebs && event_needs_egprs(event)) + pebs_data_cfg |=3D PEBS_DATACFG_EGPRS; =20 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { /* @@ -2713,15 +2724,69 @@ static void setup_arch_pebs_sample_data(struct perf= _event *event, meminfo->tsx_tuning, ax); } =20 - if (header->xmm) { + if (header->xmm || header->ymmh || header->egpr || + header->opmask || header->zmmh || header->h16zmm) { + struct arch_pebs_xer_header *xer_header =3D next_record; struct pebs_xmm *xmm; + struct ymmh_struct *ymmh; + struct avx_512_zmm_uppers_state *zmmh; + struct avx_512_hi16_state *h16zmm; + struct avx_512_opmask_state *opmask; + struct apx_state *egpr; =20 next_record +=3D sizeof(struct arch_pebs_xer_header); =20 - ignore_mask |=3D XFEATURE_MASK_SSE; - xmm =3D next_record; - perf_regs->xmm_regs =3D xmm->xmm; - next_record =3D xmm + 1; + if (header->xmm) { + ignore_mask |=3D XFEATURE_MASK_SSE; + xmm =3D next_record; + /* + * Only output XMM regs to user space when arch-PEBS + * really writes data into xstate area. + */ + if (xer_header->xstate & XFEATURE_MASK_SSE) + perf_regs->xmm_regs =3D xmm->xmm; + next_record =3D xmm + 1; + } + + if (header->ymmh) { + ignore_mask |=3D XFEATURE_MASK_YMM; + ymmh =3D next_record; + if (xer_header->xstate & XFEATURE_MASK_YMM) + perf_regs->ymmh =3D ymmh; + next_record =3D ymmh + 1; + } + + if (header->egpr) { + ignore_mask |=3D XFEATURE_MASK_APX; + egpr =3D next_record; + if (xer_header->xstate & XFEATURE_MASK_APX) + perf_regs->egpr =3D egpr; + next_record =3D egpr + 1; + } + + if (header->opmask) { + ignore_mask |=3D XFEATURE_MASK_OPMASK; + opmask =3D next_record; + if (xer_header->xstate & XFEATURE_MASK_OPMASK) + perf_regs->opmask =3D opmask; + next_record =3D opmask + 1; + } + + if (header->zmmh) { + ignore_mask |=3D XFEATURE_MASK_ZMM_Hi256; + zmmh =3D next_record; + if (xer_header->xstate & XFEATURE_MASK_ZMM_Hi256) + perf_regs->zmmh =3D zmmh; + next_record =3D zmmh + 1; + } + + if (header->h16zmm) { + ignore_mask |=3D XFEATURE_MASK_Hi16_ZMM; + h16zmm =3D next_record; + if (xer_header->xstate & XFEATURE_MASK_Hi16_ZMM) + perf_regs->h16zmm =3D h16zmm; + next_record =3D h16zmm + 1; + } } =20 if (header->lbr) { diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index a14a0f43e04a..e3b3293aa87f 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -350,6 +350,13 @@ #define ARCH_PEBS_LBR_SHIFT 40 #define ARCH_PEBS_LBR (0x3ull << ARCH_PEBS_LBR_SHIFT) #define ARCH_PEBS_VECR_XMM BIT_ULL(49) +#define ARCH_PEBS_VECR_YMMH BIT_ULL(50) +#define ARCH_PEBS_VECR_EGPRS BIT_ULL(51) +#define ARCH_PEBS_VECR_OPMASK BIT_ULL(53) +#define ARCH_PEBS_VECR_ZMMH BIT_ULL(54) +#define ARCH_PEBS_VECR_H16ZMM BIT_ULL(55) +#define ARCH_PEBS_VECR_EXT_SHIFT 50 +#define ARCH_PEBS_VECR_EXT (0x3full << ARCH_PEBS_VECR_EXT_SHIFT) #define ARCH_PEBS_GPR BIT_ULL(61) #define ARCH_PEBS_AUX BIT_ULL(62) #define ARCH_PEBS_EN BIT_ULL(63) diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index 2769ec3030e5..bbbe0835ac55 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -148,6 +148,11 @@ #define PEBS_DATACFG_LBRS BIT_ULL(3) #define PEBS_DATACFG_CNTR BIT_ULL(4) #define PEBS_DATACFG_METRICS BIT_ULL(5) +#define PEBS_DATACFG_YMMHS BIT_ULL(6) +#define PEBS_DATACFG_OPMASKS BIT_ULL(7) +#define PEBS_DATACFG_ZMMHS BIT_ULL(8) +#define PEBS_DATACFG_H16ZMMS BIT_ULL(9) +#define PEBS_DATACFG_EGPRS BIT_ULL(10) #define PEBS_DATACFG_LBR_SHIFT 24 #define PEBS_DATACFG_CNTR_SHIFT 32 #define PEBS_DATACFG_CNTR_MASK GENMASK_ULL(15, 0) @@ -545,7 +550,8 @@ struct arch_pebs_header { rsvd3:7, xmm:1, ymmh:1, - rsvd4:2, + egpr:1, + rsvd4:1, opmask:1, zmmh:1, h16zmm:1, --=20 2.34.1 From nobody Mon Jun 8 12:11:56 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14E223B8BB6; Fri, 29 May 2026 08:04:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041866; cv=none; b=inihsyy0v3VsSQdDNvyt/kxfE220wTo30T57Ma0iwP/7xkueNsiFwfQAJ9fL6m7ovH1QtmGIJ2tI8yBNIbBvGY8hB9MLfpkE5HhVKg4DQuhH+qRwj+lFJnyB4+Q2xDPSDZkE6hMk8ZKhdnIWcYRiyPKasSRQD8A2phKnyzbJDIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041866; c=relaxed/simple; bh=iw9fsApi5+CdPLB3kDC+sPXSUN/gnfbdEGcLHqHLaN8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rktcszYm0hHR4jTA0QS6ihhdLNSwRwLAluaxbbCd5LPWvezI3p3oAvkxKWBB5w1dZP+zKYrlcooxHfxPO4U6QTsKRB0N0i8Xr8wBPb5wm1/2s+V++Nfuehxj/1wN71cGO2ipUpEm4bQAzY7qEz3YdBRsQP2sDezmu9x8Zo6VJgw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=d+ihK5Gh; arc=none smtp.client-ip=192.198.163.7 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.b="d+ihK5Gh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041865; x=1811577865; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iw9fsApi5+CdPLB3kDC+sPXSUN/gnfbdEGcLHqHLaN8=; b=d+ihK5GhERy5Nrr8JNS7CjR8z5mH5ltC97hMtdiZdoCX2Cn87QrPQnOW zR5KLhoXGFIRdYWNhPdKmGEnhYblSap/7Eu7y6+gCaWwkXV4D/c6gDWH5 pMlugetynLeFBaiTnue0by5wEq3UO9Ve8C5jeMhd6ik1D2HiFxelS9/AT kNQaY/Yblb4EOVhFNegSg7ekOZ+7ri966zRoPXEqqtYSEmt+axCSrR1jX gjt9RLjOJcBOrpHIG30mbBaTTBzqoHcR/qI4w0n0vOK8JArr1cAmf7Mws nDyWyHGtOJ9St9g/JHp94E6zRThRqIdIl3AoN0yyZ0gqnfZtQWhhBDnmH g==; X-CSE-ConnectionGUID: tkxZS4rpRxGuXHrEs+ARow== X-CSE-MsgGUID: MKw8E5thT5qombUlK7Ti5A== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342231" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342231" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:24 -0700 X-CSE-ConnectionGUID: 81VQ2tqtSl6h5SChSmh7KQ== X-CSE-MsgGUID: wk73pliHSp277tk8ghePBA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802579" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:04:19 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Kan Liang , Dapeng Mi Subject: [Patch v8 21/23] perf/x86/intel: Enable PERF_PMU_CAP_SIMD_REGS capability Date: Fri, 29 May 2026 15:56:43 +0800 Message-Id: <20260529075645.580362-22-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kan Liang Enable the PERF_PMU_CAP_SIMD_REGS capability if XSAVES support is available for YMM, ZMM, OPMASK, eGPRs, or SSP. Signed-off-by: Kan Liang Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 679781519f8c..eef5d116aa06 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -6316,6 +6316,26 @@ static void intel_extended_regs_init(struct pmu *pmu) */ x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_SSE; dest_pmu->capabilities |=3D PERF_PMU_CAP_EXTENDED_REGS; + + if (boot_cpu_has(X86_FEATURE_AVX) && + cpu_has_xfeatures(XFEATURE_MASK_YMM, NULL)) + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_YMM; + if (boot_cpu_has(X86_FEATURE_APX) && + cpu_has_xfeatures(XFEATURE_MASK_APX, NULL)) + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_APX; + if (boot_cpu_has(X86_FEATURE_AVX512F)) { + if (cpu_has_xfeatures(XFEATURE_MASK_OPMASK, NULL)) + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_OPMASK; + if (cpu_has_xfeatures(XFEATURE_MASK_ZMM_Hi256, NULL)) + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_ZMM_Hi256; + if (cpu_has_xfeatures(XFEATURE_MASK_Hi16_ZMM, NULL)) + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_Hi16_ZMM; + } + if (cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) + x86_pmu.ext_regs_mask |=3D XFEATURE_MASK_CET_USER; + + if (x86_pmu.ext_regs_mask !=3D XFEATURE_MASK_SSE) + dest_pmu->capabilities |=3D PERF_PMU_CAP_SIMD_REGS; } =20 #define counter_mask(_gp, _fixed) ((_gp) | ((u64)(_fixed) << INTEL_PMC_IDX= _FIXED)) --=20 2.34.1 From nobody Mon Jun 8 12:11:56 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 869103B3C03; Fri, 29 May 2026 08:04:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041871; cv=none; b=nqIQritwfbntAe8Ozw+jydUx48LkSsWVXTvXoo5WYgpYcmHX6eHgSfD57b8Ym9x3Pw9JClyGHOAx63yaAQGZkgqqHRYRUUNxKD2+/HnhJB0oGr5Husn5ULoVj4UXr52yxgSL3uTcPn3gnHyEANx26Kms8s3lvVAIM4K7BBAIrwM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041871; c=relaxed/simple; bh=7oSm4pPQG6wRIGgpGdgeEKhIyxfI1BVG9xwOosyn5q4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LAElZDT2GjDJKQetzau6xXb0mkinQocMYqjFu8wxG0bDsoHmC/TybsGgNKQ20egHqvM5u+vFx4YorzgEypl/0xE8waoQOCWPTv5T87CO9WhMVlQmep68wyiy584HhqDCGjYwJZELtWUj/iLw8dE49tujJL9HTdhHAibYF6gIRSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=XjxmvhxX; arc=none smtp.client-ip=192.198.163.7 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.b="XjxmvhxX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041869; x=1811577869; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7oSm4pPQG6wRIGgpGdgeEKhIyxfI1BVG9xwOosyn5q4=; b=XjxmvhxXC6+cy2GcQpMSyfdkTqsxjyBx9ZaVzX4W9yWAJSvTP5AM4gyu YMHHPk+Slz2dMZIGXMVtigmVe6grdo8GlvNUbS0tDoN4PwehWhNI3FeXk 7eLkIlFkvcgYhVy7RIhD2mV5jHtPhRSE3CDQsc0xSGBR4PbL0pudtrdB8 7jFZprlxnzpBMwJc4FPXxhx4wDrNYaEBOBe68qoYrtIhqXaHqj+b1yf6t 2YxACdxNdE5qtzSeJtf5JXD3L6JK28Yi0gMqxF+VT+ki+/SaHvx/ywi4h cNGqiDN0gTKy2fg5Y8OSHgwnozgMhLvyK48jP39q5+vg3+aUVnvICBuux w==; X-CSE-ConnectionGUID: MZO96pfzSdKWF7Vz3AmDTA== X-CSE-MsgGUID: 4okE6tRrTeuo4KlLtp1D7w== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342246" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342246" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:29 -0700 X-CSE-ConnectionGUID: MSmAlwP/QDeYrfZ472/fHQ== X-CSE-MsgGUID: 4DASNmacS9y3eBPvU0ymCA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802605" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:04:24 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 22/23] perf/x86: Activate back-to-back NMI detection for arch-PEBS induced NMIs Date: Fri, 29 May 2026 15:56:44 +0800 Message-Id: <20260529075645.580362-23-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When two or more identical PEBS events with the same sampling period are programmed on a mix of PDIST and non-PDIST counters, multiple back-to-back NMIs can be triggered. The Linux PMI handler processes the first NMI and clears the GLOBAL_STATUS MSR. If a second NMI is triggered immediately after the first, it is recognized as a "suspicious NMI" because no bits are set in the GLOBAL_STATUS MSR (cleared by the first NMI). This issue does not lead to PEBS data corruption or data loss, but it does result in an annoying warning message. The current NMI handler supports back-to-back NMI detection, but it requires the PMI handler to return the count of actually processed events, which the PEBS handler does not currently do. This patch modifies the PEBS handlers to return the count of actually processed events, thereby activating back-to-back NMI detection and avoiding the "suspicious NMI" warning. Suggested-by: Andi Kleen Signed-off-by: Dapeng Mi --- arch/x86/events/intel/core.c | 29 +++++++++++++++++--------- arch/x86/events/intel/ds.c | 40 ++++++++++++++++++++++++------------ arch/x86/events/perf_event.h | 2 +- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index eef5d116aa06..4546b20429ba 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3763,7 +3763,7 @@ static void intel_pmu_reset(void) * * The contents and other behavior of the guest event do not matter. */ -static void x86_pmu_handle_guest_pebs(struct pt_regs *regs, +static int x86_pmu_handle_guest_pebs(struct pt_regs *regs, struct perf_sample_data *data) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); @@ -3772,11 +3772,11 @@ static void x86_pmu_handle_guest_pebs(struct pt_reg= s *regs, int bit; =20 if (!unlikely(perf_guest_state())) - return; + return 0; =20 if (!x86_pmu.pebs_ept || !x86_pmu.pebs_active || !guest_pebs_idxs) - return; + return 0; =20 for_each_set_bit(bit, (unsigned long *)&guest_pebs_idxs, X86_PMC_IDX_MAX)= { event =3D cpuc->events[bit]; @@ -3786,9 +3786,14 @@ static void x86_pmu_handle_guest_pebs(struct pt_regs= *regs, perf_sample_data_init(data, 0, event->hw.last_period); perf_event_overflow(event, data, regs); =20 - /* Inject one fake event is enough. */ - break; + /* + * Inject one fake event is enough. + * Returning 1 to inform PMI is handled. + */ + return 1; } + + return 0; } =20 static int handle_pmi_common(struct pt_regs *regs, u64 status) @@ -3837,9 +3842,11 @@ static int handle_pmi_common(struct pt_regs *regs, u= 64 status) if (__test_and_clear_bit(GLOBAL_STATUS_BUFFER_OVF_BIT, (unsigned long *)&= status)) { u64 pebs_enabled =3D cpuc->pebs_enabled; =20 - handled++; - x86_pmu_handle_guest_pebs(regs, &data); - static_call(x86_pmu_drain_pebs)(regs, &data); + handled +=3D x86_pmu_handle_guest_pebs(regs, &data); + handled +=3D static_call(x86_pmu_drain_pebs)(regs, &data); + /* Ensure no "suspicious NMI" warning for empty PEBS buffer. */ + if (!handled) + handled++; =20 /* * PMI throttle may be triggered, which stops the PEBS event. @@ -3866,8 +3873,10 @@ static int handle_pmi_common(struct pt_regs *regs, u= 64 status) */ if (__test_and_clear_bit(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT, (unsigned long *)&status)) { - handled++; - static_call(x86_pmu_drain_pebs)(regs, &data); + handled +=3D static_call(x86_pmu_drain_pebs)(regs, &data); + /* Ensure no "suspicious NMI" warning for empty PEBS buffer. */ + if (!handled) + handled++; =20 if (cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS] && is_pebs_counter_event_group(cpuc->events[INTEL_PMC_IDX_FIXED_SLOTS])) diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index 8a653edce392..e0d307627702 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -3047,7 +3047,7 @@ __intel_pmu_pebs_events(struct perf_event *event, __intel_pmu_pebs_last_event(event, iregs, regs, data, at, count, setup_sa= mple); } =20 -static void intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_s= ample_data *data) +static int intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sa= mple_data *data) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct debug_store *ds =3D cpuc->ds; @@ -3056,7 +3056,7 @@ static void intel_pmu_drain_pebs_core(struct pt_regs = *iregs, struct perf_sample_ int n; =20 if (!x86_pmu.pebs_active) - return; + return 0; =20 at =3D (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; top =3D (struct pebs_record_core *)(unsigned long)ds->pebs_index; @@ -3067,22 +3067,24 @@ static void intel_pmu_drain_pebs_core(struct pt_reg= s *iregs, struct perf_sample_ ds->pebs_index =3D ds->pebs_buffer_base; =20 if (!test_bit(0, cpuc->active_mask)) - return; + return 0; =20 WARN_ON_ONCE(!event); =20 if (!event->attr.precise_ip) - return; + return 0; =20 n =3D top - at; if (n <=3D 0) { if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) intel_pmu_save_and_restart_reload(event, 0); - return; + return 0; } =20 __intel_pmu_pebs_events(event, iregs, data, at, top, 0, n, setup_pebs_fixed_sample_data); + + return 1; /* PMC0 only*/ } =20 static void intel_pmu_pebs_event_update_no_drain(struct cpu_hw_events *cpu= c, u64 mask) @@ -3105,7 +3107,7 @@ static void intel_pmu_pebs_event_update_no_drain(stru= ct cpu_hw_events *cpuc, u64 } } =20 -static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sa= mple_data *data) +static int intel_pmu_drain_pebs_nhm(struct pt_regs *iregs, struct perf_sam= ple_data *data) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct debug_store *ds =3D cpuc->ds; @@ -3114,11 +3116,12 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs= *iregs, struct perf_sample_d short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] =3D {}; short error[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] =3D {}; int max_pebs_events =3D intel_pmu_max_num_pebs(NULL); + u64 events_bitmap =3D 0; int bit, i, size; u64 mask; =20 if (!x86_pmu.pebs_active) - return; + return 0; =20 base =3D (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; top =3D (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; @@ -3134,7 +3137,7 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *= iregs, struct perf_sample_d =20 if (unlikely(base >=3D top)) { intel_pmu_pebs_event_update_no_drain(cpuc, mask); - return; + return 0; } =20 for (at =3D base; at < top; at +=3D x86_pmu.pebs_record_size) { @@ -3198,6 +3201,7 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *= iregs, struct perf_sample_d if ((counts[bit] =3D=3D 0) && (error[bit] =3D=3D 0)) continue; =20 + events_bitmap |=3D BIT(bit); event =3D cpuc->events[bit]; if (WARN_ON_ONCE(!event)) continue; @@ -3219,6 +3223,8 @@ static void intel_pmu_drain_pebs_nhm(struct pt_regs *= iregs, struct perf_sample_d setup_pebs_fixed_sample_data); } } + + return hweight64(events_bitmap); } =20 static __always_inline void @@ -3272,7 +3278,7 @@ __intel_pmu_handle_last_pebs_record(struct pt_regs *i= regs, =20 } =20 -static void intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sa= mple_data *data) +static int intel_pmu_drain_pebs_icl(struct pt_regs *iregs, struct perf_sam= ple_data *data) { short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] =3D {}; void *last[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS]; @@ -3282,10 +3288,11 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs= *iregs, struct perf_sample_d struct pt_regs *regs =3D &perf_regs->regs; struct pebs_basic *basic; void *base, *at, *top; + u64 events_bitmap =3D 0; u64 mask; =20 if (!x86_pmu.pebs_active) - return; + return 0; =20 base =3D (struct pebs_basic *)(unsigned long)ds->pebs_buffer_base; top =3D (struct pebs_basic *)(unsigned long)ds->pebs_index; @@ -3298,7 +3305,7 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs *= iregs, struct perf_sample_d =20 if (unlikely(base >=3D top)) { intel_pmu_pebs_event_update_no_drain(cpuc, mask); - return; + return 0; } =20 if (!iregs) @@ -3313,6 +3320,7 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs *= iregs, struct perf_sample_d continue; =20 pebs_status =3D mask & basic->applicable_counters; + events_bitmap |=3D pebs_status; __intel_pmu_handle_pebs_record(iregs, regs, data, at, pebs_status, counts, last, setup_pebs_adaptive_sample_data); @@ -3320,9 +3328,11 @@ static void intel_pmu_drain_pebs_icl(struct pt_regs = *iregs, struct perf_sample_d =20 __intel_pmu_handle_last_pebs_record(iregs, regs, data, mask, counts, last, setup_pebs_adaptive_sample_data); + + return hweight64(events_bitmap); } =20 -static void intel_pmu_drain_arch_pebs(struct pt_regs *iregs, +static int intel_pmu_drain_arch_pebs(struct pt_regs *iregs, struct perf_sample_data *data) { short counts[INTEL_PMC_IDX_FIXED + MAX_FIXED_PEBS_EVENTS] =3D {}; @@ -3332,13 +3342,14 @@ static void intel_pmu_drain_arch_pebs(struct pt_reg= s *iregs, struct x86_perf_regs *perf_regs =3D this_cpu_ptr(&x86_pebs_regs); struct pt_regs *regs =3D &perf_regs->regs; void *base, *at, *top; + u64 events_bitmap =3D 0; u64 mask; =20 rdmsrq(MSR_IA32_PEBS_INDEX, index.whole); =20 if (unlikely(!index.wr)) { intel_pmu_pebs_event_update_no_drain(cpuc, X86_PMC_IDX_MAX); - return; + return 0; } =20 base =3D cpuc->pebs_vaddr; @@ -3377,6 +3388,7 @@ static void intel_pmu_drain_arch_pebs(struct pt_regs = *iregs, =20 basic =3D at + sizeof(struct arch_pebs_header); pebs_status =3D mask & basic->applicable_counters; + events_bitmap |=3D pebs_status; __intel_pmu_handle_pebs_record(iregs, regs, data, at, pebs_status, counts, last, setup_arch_pebs_sample_data); @@ -3396,6 +3408,8 @@ static void intel_pmu_drain_arch_pebs(struct pt_regs = *iregs, __intel_pmu_handle_last_pebs_record(iregs, regs, data, mask, counts, last, setup_arch_pebs_sample_data); + + return hweight64(events_bitmap); } =20 static void __init intel_arch_pebs_init(void) diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index c521a7fbe9c6..77bc42f8a070 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1022,7 +1022,7 @@ struct x86_pmu { int pebs_record_size; int pebs_buffer_size; u64 pebs_events_mask; - void (*drain_pebs)(struct pt_regs *regs, struct perf_sample_data *data); + int (*drain_pebs)(struct pt_regs *regs, struct perf_sample_data *data); struct event_constraint *pebs_constraints; void (*pebs_aliases)(struct perf_event *event); u64 (*pebs_latency_data)(struct perf_event *event, u64 status); --=20 2.34.1 From nobody Mon Jun 8 12:11:56 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 662B03BD64C; Fri, 29 May 2026 08:04:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041876; cv=none; b=ljJxCWAsWmNMwDsnp5wGL/Y/zzc76gLIkeHS/Ix80mu4g5azQC7pSw9babASHrguUCKengW459MDdJd2oFF9F9hZWYxvC6x9iO8+tHaYPj5gmhjRvd6pr41e+kOhFA9qKCGNEhMBNC2pA4GQXbkXzaw48oAkm99MP0bF5zNLiMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780041876; c=relaxed/simple; bh=ejHAjMXmlgnoWHSKQWwwp11JhfD8ttmuEBIljZim/xc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=djTMAsNJ59zz0pABejE679hRhZTVH4zOMfZPUjGEQ6C4zz20mMfP/5Wd9tiwh+gOfrnxjN7ymZzlyHn3wtWOZNf08BZlAMJ9ocMTPfbNy+hfGlu3eysXeIf9GJ13H68yN7jGT+yIcM+AFP1qKbjN6tZXMWJhinL5PTQ00BuiiOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jC+/PK5W; arc=none smtp.client-ip=192.198.163.7 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.b="jC+/PK5W" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1780041874; x=1811577874; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ejHAjMXmlgnoWHSKQWwwp11JhfD8ttmuEBIljZim/xc=; b=jC+/PK5WRgISHkFJzHPP6UDAFdZrkmsDhi77OOjJGnEglyuRMtI6IyeF GFT1M/oKI26DUDu8wXRikAG+1FQvdNxMRXF7b7Czuqw4BNOwhEsZ0JUZy Ix+1C7Vg7Z07uJHZTNvZgWBkj5S4i5H7ic7It8P6jBfAVxcGNO39q5f3A QPuNl6CVEPOPIh3LmNaD4L/6TVDCjtVC3gAwvc6C2U61DEiLbCYS5TmnR e0LKxfdKkPAdyYKgneW8zZov7zZmxW4meI8Gda1ZF/JvaIVezFmJTZYfU lZbKQJ9UNHuO/mE0ZFJ/UD3qcWXFONZacj9CeeWNhidr7W805y86Q4dIv A==; X-CSE-ConnectionGUID: aA96FwAoQdaf74Ha4LEfvQ== X-CSE-MsgGUID: 1zfv8Gq3QdmiVvCKg52XlA== X-IronPort-AV: E=McAfee;i="6800,10657,11800"; a="106342271" X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="106342271" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2026 01:04:34 -0700 X-CSE-ConnectionGUID: hjHTdM+eTf6PL95phwVvpg== X-CSE-MsgGUID: CFjwm1agQeawR70ntITtdA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.24,175,1774335600"; d="scan'208";a="246802627" Received: from spr.sh.intel.com ([10.112.230.239]) by orviesa003.jf.intel.com with ESMTP; 29 May 2026 01:04:29 -0700 From: Dapeng Mi To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Thomas Gleixner , Dave Hansen , Ian Rogers , Adrian Hunter , Jiri Olsa , Alexander Shishkin , Andi Kleen , Eranian Stephane Cc: Mark Rutland , broonie@kernel.org, Ravi Bangoria , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Zide Chen , Falcon Thomas , Dapeng Mi , Xudong Hao , Dapeng Mi Subject: [Patch v8 23/23] perf/x86/intel: Add sanity check for PEBS fragment size Date: Fri, 29 May 2026 15:56:45 +0800 Message-Id: <20260529075645.580362-24-dapeng1.mi@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> References: <20260529075645.580362-1-dapeng1.mi@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Prevent potential infinite loops by adding a sanity check for the corrupted PEBS fragment sizes which could happen in theory. If a corrupted PEBS fragment is detected, the entire PEBS record including the corrupted fragment and all subsequent records will be dropped and a NULL PEBS record is reported to user space. This ensures the integrity of PEBS data and prevents infinite loops in setup_arch_pebs_sample_data() again. Please note software has no way to figure out which events are impacted from the corrupted record, so the last record of each event would be discarded for all events if corrupted record is detected even though it may be a well-formed record for some events. Signed-off-by: Dapeng Mi --- arch/x86/events/intel/ds.c | 68 +++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index e0d307627702..3e100cb206a8 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -2675,6 +2675,9 @@ static void setup_arch_pebs_sample_data(struct perf_e= vent *event, =20 again: header =3D at; + if (!header->size) + return; + next_record =3D at + sizeof(struct arch_pebs_header); if (header->basic) { struct arch_pebs_basic *basic =3D next_record; @@ -2827,7 +2830,7 @@ static void setup_arch_pebs_sample_data(struct perf_e= vent *event, } =20 /* Parse followed fragments if there are. */ - if (arch_pebs_record_continued(header)) { + if (arch_pebs_record_continued(header) && header->size) { at =3D at + header->size; goto again; } @@ -2956,13 +2959,21 @@ __intel_pmu_pebs_last_event(struct perf_event *even= t, struct pt_regs *iregs, struct pt_regs *regs, struct perf_sample_data *data, - void *at, - int count, + void *at, int count, bool corrupted, setup_fn setup_sample) { struct hw_perf_event *hwc =3D &event->hw; =20 - setup_sample(event, iregs, at, data, regs); + /* Skip parsing corrupted PEBS record. */ + if (corrupted) { + /* Clear stale register states in previous records. */ + memset(regs, 0, sizeof(*regs)); + x86_pmu_clear_perf_regs(regs); + perf_sample_data_init(data, 0, event->hw.last_period); + } else { + setup_sample(event, iregs, at, data, regs); + } + if (iregs =3D=3D &dummy_iregs) { /* * The PEBS records may be drained in the non-overflow context, @@ -2980,12 +2991,16 @@ __intel_pmu_pebs_last_event(struct perf_event *even= t, } =20 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { - if ((is_pebs_counter_event_group(event))) { - /* - * The value of each sample has been updated when setup - * the corresponding sample data. - */ - perf_event_update_userpage(event); + if (is_pebs_counter_event_group(event)) { + if (corrupted) { + intel_pmu_save_and_restart_reload(event, 1); + } else { + /* + * The value of each sample has been updated + * when setup the corresponding sample data. + */ + perf_event_update_userpage(event); + } } else { /* * Now, auto-reload is only enabled in fixed period mode. @@ -3009,7 +3024,7 @@ __intel_pmu_pebs_last_event(struct perf_event *event, * counters-snapshotting record, only needs to set the new * period for the counter. */ - if (is_pebs_counter_event_group(event)) + if (is_pebs_counter_event_group(event) && !corrupted) static_call(x86_pmu_set_period)(event); else intel_pmu_save_and_restart(event); @@ -3038,13 +3053,15 @@ __intel_pmu_pebs_events(struct perf_event *event, iregs =3D &dummy_iregs; =20 while (cnt > 1) { - __intel_pmu_pebs_event(event, iregs, regs, data, at, setup_sample); + __intel_pmu_pebs_event(event, iregs, regs, data, + at, setup_sample); at +=3D cpuc->pebs_record_size; at =3D get_next_pebs_record_by_bit(at, top, bit); cnt--; } =20 - __intel_pmu_pebs_last_event(event, iregs, regs, data, at, count, setup_sa= mple); + __intel_pmu_pebs_last_event(event, iregs, regs, data, at, + count, false, setup_sample); } =20 static int intel_pmu_drain_pebs_core(struct pt_regs *iregs, struct perf_sa= mple_data *data) @@ -3259,7 +3276,8 @@ static __always_inline void __intel_pmu_handle_last_pebs_record(struct pt_regs *iregs, struct pt_regs *regs, struct perf_sample_data *data, - u64 mask, short *counts, void **last, + u64 mask, short *counts, + void **last, bool corrupted, setup_fn setup_sample) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); @@ -3273,7 +3291,7 @@ __intel_pmu_handle_last_pebs_record(struct pt_regs *i= regs, event =3D cpuc->events[bit]; =20 __intel_pmu_pebs_last_event(event, iregs, regs, data, last[bit], - counts[bit], setup_sample); + counts[bit], corrupted, setup_sample); } =20 } @@ -3327,7 +3345,7 @@ static int intel_pmu_drain_pebs_icl(struct pt_regs *i= regs, struct perf_sample_da } =20 __intel_pmu_handle_last_pebs_record(iregs, regs, data, mask, counts, last, - setup_pebs_adaptive_sample_data); + false, setup_pebs_adaptive_sample_data); =20 return hweight64(events_bitmap); } @@ -3343,6 +3361,7 @@ static int intel_pmu_drain_arch_pebs(struct pt_regs *= iregs, struct pt_regs *regs =3D &perf_regs->regs; void *base, *at, *top; u64 events_bitmap =3D 0; + bool corrupted =3D false; u64 mask; =20 rdmsrq(MSR_IA32_PEBS_INDEX, index.whole); @@ -3377,8 +3396,10 @@ static int intel_pmu_drain_arch_pebs(struct pt_regs = *iregs, =20 header =3D at; =20 - if (WARN_ON_ONCE(!header->size)) - break; + if (WARN_ON_ONCE(!header->size)) { + corrupted =3D true; + goto done; + } =20 /* 1st fragment or single record must have basic group */ if (!header->basic) { @@ -3398,15 +3419,24 @@ static int intel_pmu_drain_arch_pebs(struct pt_regs= *iregs, if (!header->size) break; at +=3D header->size; + if (WARN_ON_ONCE(at >=3D top)) { + corrupted =3D true; + goto done; + } header =3D at; } =20 /* Skip last fragment or the single record */ at +=3D header->size; + if (WARN_ON_ONCE(at > top)) { + corrupted =3D true; + goto done; + } } =20 +done: __intel_pmu_handle_last_pebs_record(iregs, regs, data, mask, - counts, last, + counts, last, corrupted, setup_arch_pebs_sample_data); =20 return hweight64(events_bitmap); --=20 2.34.1