From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 819A346D2CE; Mon, 17 Aug 2026 18:41:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992089; cv=none; b=FJg9dR2zvEhIgJWicj+TIPmVJ+wtd6Unk60vdTnPRFt3C4HEGuXMu+hTPE4cA3hnVBVHkdiraKZpnKjT4MnUn4TucC1kQeJiPJyTysRF7CnVz/IZkc4fYGIXsuOLmL+v7iBAR1mnlvFWivCi/oFqDexubrzyRpoa/+So73OYpys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992089; c=relaxed/simple; bh=1JE+m3tZntaa+yghR75WxF2AkhAkb2NTIHjVAS1ARUU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lp/ENX1w6FTj+0rh++Hrc8vDyQPI9NNlDOotLIRCSesUoWe1ylWyet+XDrQDAioa9mFtqlP8TSz/MR4abYlzsooQbHfGBJPEMcDTcbi2blJoRyXB50BW8ZItHf/EOgT1qtq//2f9NYGVGOLuEKcsZPuMC0nRsM8XQ8ux8BTOL00= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=GK4cwrwY; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="GK4cwrwY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992088; x=1818528088; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1JE+m3tZntaa+yghR75WxF2AkhAkb2NTIHjVAS1ARUU=; b=GK4cwrwYi2cpDkHxiiUbQJA38keoWxYb5926B8oKBVcvUzt9eOG4G9c8 ZqBorGSFHpyRtx/zFFYT7SpVJ/c/cVijos57cdiH3NW+YoaFOIomfETy2 L8ezKdEwqa56nLjdyJwN3UwB0X/GErD3IPGVsRGmbiuGyMWueAyWnfPaT /TineIL7+819JHObi2wSXd8sfdnqqgsHnLXn8uNb+pkfXSK6U4ePWcaL/ wxV1/wp1uZHOZYD4l+7iasRQkQpJzDCItCPTR03WyzO98rBnVt3lDV/Ix U47F2J8lkMe5jkX/nzDCT2XzIw0M23zXX5lfb4ciA7Ez7hl6sNORHkfe+ A==; X-CSE-ConnectionGUID: mZrII20pThCapwYMQhRjBw== X-CSE-MsgGUID: 57YWSZA1RquHY30MZ9wnUg== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066077" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066077" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: SkGiWfhgSni/osSoDKExmQ== X-CSE-MsgGUID: NLM3B1ZMSza2spvCCQmr/Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656957" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 1/8] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events Date: Mon, 17 Aug 2026 11:31:35 -0700 Message-ID: <20260817183143.226156-2-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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" Only fixed counters 0..2 have matching generic cross-platform hardware perf events (INSTRUCTIONS, CPU_CYCLES, REF_CPU_CYCLES). Therefore, perf_get_hw_event_config() is only applicable to these counters. KVM does not intend to emulate fixed counters >=3D 3 on legacy (non-mediated) vPMU, while for mediated vPMU, KVM does not care what the fixed counter event mappings are. Therefore, return 0 for their eventsel. The two BUILD_BUG_ON() checks are no longer needed, so drop them along with __always_inline. Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi Reviewed-by: Jim Mattson --- v6: - Re-arrange the code for early return. Clearer. v2: - Replace 3 in "if (index < 3)" with ARRAY_SIZE(fixed_pmc_perf_ids). --- arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index bfa8612fb450..df51a17b6e17 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -465,11 +465,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, st= ruct msr_data *msr_info) * different perf_event is already utilizing the requested counter, but th= e end * result is the same (ignoring the fact that using a general purpose coun= ter * will likely exacerbate counter contention). - * - * Forcibly inlined to allow asserting on @index at build time, and there = should - * never be more than one user. */ -static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index) +static u64 intel_get_fixed_pmc_eventsel(unsigned int index) { const enum perf_hw_id fixed_pmc_perf_ids[] =3D { [0] =3D PERF_COUNT_HW_INSTRUCTIONS, @@ -478,8 +475,13 @@ static __always_inline u64 intel_get_fixed_pmc_eventse= l(unsigned int index) }; u64 eventsel; =20 - BUILD_BUG_ON(ARRAY_SIZE(fixed_pmc_perf_ids) !=3D KVM_MAX_NR_INTEL_FIXED_C= OUNTERS); - BUILD_BUG_ON(index >=3D KVM_MAX_NR_INTEL_FIXED_COUNTERS); + /* + * Fixed counters 3 and above don't have a corresponding generic + * hardware perf event, and KVM does not intend to emulate them on + * non-mediated vPMU. + */ + if (index >=3D ARRAY_SIZE(fixed_pmc_perf_ids)) + return 0; =20 /* * Yell if perf reports support for a fixed counter but perf doesn't --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 35423470110; Mon, 17 Aug 2026 18:41:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992090; cv=none; b=J/4+AzPPREUnFyTz1fQD2uj7dXbILD1mTHXPNKrUMjJQ2C3bfRKpPiryciEqzj7ScnHOeJXv+3RHC7OWhUgFzvjP+QQH/af/7uIQYE0BfR4Hy8iYd6zPoaNux/gQI98hJ6mipTZkGn2wd8LzYyKufHvMlAyCtCP7CxvexZKtbcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992090; c=relaxed/simple; bh=7enoeOr+5bHlwtkPhEcyQTkoqg1DbtY8tEDKcB319iE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JRErLed52afxvFrPfEdoYuZjGVAeIoOJiRRJbOSeyd/WTcILV3HbkEQ/YjB4OkO40sp7hcAblT1vNUepPLt343zikdLrfHfeXt1c06kSHxWinQrW/s4us73ZiZAd/VEjMd1oMjcAr8lK6aT4BxrfLQ4qNgkI8ZE0dQMHpSnnWXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=aEX/Np5K; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="aEX/Np5K" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992089; x=1818528089; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7enoeOr+5bHlwtkPhEcyQTkoqg1DbtY8tEDKcB319iE=; b=aEX/Np5KujO3l/4VNRodIZfywDX19156JW+EhNWOmCGBOGtLKzsIIxos yQ7t1OvbNk6Qg9/RLRhi0bJTVCcXrBwPPFraX7wGIwYv9uKViROR9yzxn 5QXGJZ0AF3XNkJblmnvxEzzs4NAUg9L8RtmeumGNd785IIDrKFHBTwV1T Vtx4rPrfieyPzbHxET5ZETXrPt9DYmpNd1B5Cf/DqyE08M3DAsiCL2BdJ JkTXfahSGu+76vY2I2Iq2eQmuD7bJMAdatmbV9B5i8lm17y7yEEGTFOyw SbGyiUoK1g7fXZAFr9i+aSbivKaXviUMn9jGsRPz2CnlnZOVV4cH+44mz w==; X-CSE-ConnectionGUID: Se1UkMjsRDSc7K4Fsogziw== X-CSE-MsgGUID: K1+pvi5lSP2hBQWDACNT2A== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066084" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066084" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: NytYkXPSRg+GGvz9nguNUA== X-CSE-MsgGUID: dbs722HgTJaosJhrxE2y4g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656959" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 2/8] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU Date: Mon, 17 Aug 2026 11:31:36 -0700 Message-ID: <20260817183143.226156-3-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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: Dapeng Mi Starting with Ice Lake, Intel introduced fixed counter 3, which counts TOPDOWN.SLOTS - the number of available slots for an unhalted logical processor. It serves as the denominator for top-level metrics in the Top-down Microarchitecture Analysis method. Emulating this counter on legacy vPMU would require introducing a new generic perf encoding for the Intel-specific TOPDOWN.SLOTS event in order to call perf_get_hw_event_config(). This is undesirable as it would pollute the generic perf event encoding. Moreover, KVM does not intend to emulate IA32_PERF_METRICS in the legacy vPMU model, and without IA32_PERF_METRICS, emulating this counter has little practical value. Therefore, expose fixed counter 3 to guests only when mediated vPMU is enabled. Signed-off-by: Dapeng Mi Co-developed-by: Zide Chen Signed-off-by: Zide Chen Reviewed-by: Jim Mattson --- v6: - Update comments to replace 2 with KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1. v3: - Move the non-contiguous counter filter code to pmu.c v2: - Don't advertise fixed counter 3 to userspace if the host doesn't support it. --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/msrs.c | 4 ++-- arch/x86/kvm/pmu.c | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 283847619ff8..8a4e542218a7 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -562,7 +562,7 @@ struct kvm_pmc { #define KVM_MAX_NR_GP_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \ KVM_MAX_NR_AMD_GP_COUNTERS) =20 -#define KVM_MAX_NR_INTEL_FIXED_COUNTERS 3 +#define KVM_MAX_NR_INTEL_FIXED_COUNTERS 4 #define KVM_MAX_NR_AMD_FIXED_COUNTERS 0 #define KVM_MAX_NR_FIXED_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS,= \ KVM_MAX_NR_AMD_FIXED_COUNTERS) diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c index 66fa7140d65d..1fbd8f08a7f0 100644 --- a/arch/x86/kvm/msrs.c +++ b/arch/x86/kvm/msrs.c @@ -217,7 +217,7 @@ static const u32 msrs_to_save_base[] =3D { =20 static const u32 msrs_to_save_pmu[] =3D { MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, - MSR_ARCH_PERFMON_FIXED_CTR0 + 2, + MSR_ARCH_PERFMON_FIXED_CTR2, MSR_ARCH_PERFMON_FIXED_CTR3, MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, MSR_CORE_PERF_GLOBAL_CTRL, MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG, @@ -2671,7 +2671,7 @@ void kvm_init_msr_lists(void) { unsigned i; =20 - BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS !=3D 3, + BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS !=3D 4, "Please update the fixed PMCs in msrs_to_save_pmu[]"); =20 num_msrs_to_save =3D 0; diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index a7d60c8785cd..913a0ecbf260 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "x86.h" #include "cpuid.h" #include "lapic.h" @@ -99,7 +100,8 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] =3D { * all perf counters (both gp and fixed). The mapping relationship * between pmc and perf counters is as the following: * * Intel: [0 .. KVM_MAX_NR_INTEL_GP_COUNTERS-1] <=3D> gp counters - * [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + 2] = <=3D> fixed + * [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + + * KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1] <=3D> fixed * * AMD: [0 .. AMD64_NUM_COUNTERS-1] and, for families 15H * and later, [0 .. AMD64_NUM_COUNTERS_CORE-1] <=3D> gp counters */ @@ -134,6 +136,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_op= s) { bool is_intel =3D boot_cpu_data.x86_vendor =3D=3D X86_VENDOR_INTEL; int min_nr_gp_ctrs =3D pmu_ops->MIN_NR_GP_COUNTERS; + union cpuid10_edx edx; + u32 eax, ebx, ecx; =20 /* * Hybrid PMUs don't play nice with virtualization without careful @@ -181,6 +185,19 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_o= ps) kvm_pmu_cap.num_counters_fixed =3D min(kvm_pmu_cap.num_counters_fixed, KVM_MAX_NR_FIXED_COUNTERS); =20 + /* + * Currently, KVM doesn't support non-contiguous fixed counters; make + * sure only contiguous ones are retained in kvm_pmu_cap. + */ + if (kvm_host_pmu.version >=3D 5) { + cpuid(0xa, &eax, &ebx, &ecx, &edx.full); + if (kvm_pmu_cap.num_counters_fixed > edx.split.num_counters_fixed) + kvm_pmu_cap.num_counters_fixed =3D edx.split.num_counters_fixed; + } + + if (!enable_mediated_pmu && kvm_pmu_cap.num_counters_fixed > 3) + kvm_pmu_cap.num_counters_fixed =3D 3; + kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =3D perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS); kvm_pmu_eventsel.BRANCH_INSTRUCTIONS_RETIRED =3D --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 DEAD837DE99; Mon, 17 Aug 2026 18:41:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992089; cv=none; b=IbYagtBQSOEZ2so1+15wAYhuF114Z1AldBipSzSVBbecUOT7IqXufKNW+OcvHLMkoPGhmcS65ovRI6maac4VfAsCwdx+NI7BOFo6RfTWOsYzdY3NiZ+QUrbxEmi5IKdVmw/gEcl1tmDW6kwBg3ExD//chMuOVQKgDI0xFXoG4D8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992089; c=relaxed/simple; bh=brncUiGnalGElaj9h67hLSOtPMjkT92NThj25A70XkU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G+h/cpN3xuvWxIKA6NNCxZPe7O/JXLzkPzLU8JfSwyoldP8+A1J27qdCs+F0us13ddvU2YKBZUMub3QXH3DzfVrMoJTTR/XPLXF0TI9kg+clPog2724pCNXrFnA++0Dhgexdx5hxKQUEdZED5AUwgx1syf9axxvGQ/W6Y6KUS3w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Y1Vz6gaR; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Y1Vz6gaR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992088; x=1818528088; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=brncUiGnalGElaj9h67hLSOtPMjkT92NThj25A70XkU=; b=Y1Vz6gaRQOLR9J9LraYBRF9AqGpCqWgLA8YNP4Wrw8iKSdnnaKcrLxQR bXjfhRH3dlZ086Yc1nzmF3U+R7LlvRt0pt4snb8rNkz3EGvl1Ei7rWiao W57U0YLHkJatGkYcYHLWmlFJbkd9pU8y54wxwMuEwixZTyNmGObZPdha/ jKIHsixP9d6TNOrqf/pnJM1OQ6e1zaoF96Lbi341zSWe4BZsV6OT20N4m 9YtJKg0p5cJqoVR2oe2jKohdztr0wCURuivcyGAjDTXE9YC1+tqOg1kvX Z/anldPC04sVSskCsCLDt/2U61Xy1md1/rA1jnV2T4akJVH2jdKI+GlIB w==; X-CSE-ConnectionGUID: Pda1ncQSRd218QZCNiiTeA== X-CSE-MsgGUID: WQWfelcER/iOypK4hILqGA== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066081" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066081" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: IA6c4iE8QVKk2lCkifwxcA== X-CSE-MsgGUID: 4SCalm5ESNupnBi2QEH7Pg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656963" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 3/8] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h Date: Mon, 17 Aug 2026 11:31:37 -0700 Message-ID: <20260817183143.226156-4-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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 is in preparation for it to be called from common x86 code, for example kvm_need_rdpmc_intercept(), to check the guest's PERF_METRICS capability. Rename it to kvm_vcpu_get_perf_caps() to indicate that it's part of the common API, and shorten _capabilities to _caps. No functional change intended. Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi Reviewed-by: Jim Mattson --- v8: - remove include "cpuid.h" from pmu_intel.h. v5: new patch. --- arch/x86/kvm/pmu.h | 8 ++++++++ arch/x86/kvm/vmx/pmu_intel.c | 6 +++--- arch/x86/kvm/vmx/pmu_intel.h | 12 +----------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 090c9bbb74f4..82f955e5a450 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -271,6 +271,14 @@ static inline bool kvm_pmu_is_fastpath_emulation_allow= ed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } =20 +static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu) +{ + if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM)) + return 0; + + return vcpu->arch.perf_capabilities; +} + void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx); diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index df51a17b6e17..100c072b7fb3 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -190,13 +190,13 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu,= u32 msr) case MSR_CORE_PERF_FIXED_CTR_CTRL: return kvm_pmu_has_perf_global_ctrl(pmu); case MSR_IA32_PEBS_ENABLE: - ret =3D vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT; + ret =3D kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PEBS_FORMAT; break; case MSR_IA32_DS_AREA: ret =3D guest_cpu_cap_has(vcpu, X86_FEATURE_DS); break; case MSR_PEBS_DATA_CFG: - perf_capabilities =3D vcpu_get_perf_capabilities(vcpu); + perf_capabilities =3D kvm_vcpu_get_perf_caps(vcpu); ret =3D (perf_capabilities & PERF_CAP_PEBS_BASELINE) && ((perf_capabilities & PERF_CAP_PEBS_FORMAT) > 3); break; @@ -551,7 +551,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->raw_event_mask |=3D (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED); } =20 - perf_capabilities =3D vcpu_get_perf_capabilities(vcpu); + perf_capabilities =3D kvm_vcpu_get_perf_caps(vcpu); if (intel_pmu_lbr_is_compatible(vcpu) && (perf_capabilities & PERF_CAP_LBR_FMT)) memcpy(&lbr_desc->records, &vmx_lbr_caps, sizeof(vmx_lbr_caps)); diff --git a/arch/x86/kvm/vmx/pmu_intel.h b/arch/x86/kvm/vmx/pmu_intel.h index 5d9357640aa1..dd447d8b6fdd 100644 --- a/arch/x86/kvm/vmx/pmu_intel.h +++ b/arch/x86/kvm/vmx/pmu_intel.h @@ -4,19 +4,9 @@ =20 #include =20 -#include "cpuid.h" - -static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu) -{ - if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM)) - return 0; - - return vcpu->arch.perf_capabilities; -} - static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu) { - return (vcpu_get_perf_capabilities(vcpu) & PERF_CAP_FW_WRITES) !=3D 0; + return (kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_FW_WRITES) !=3D 0; } =20 bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu); --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 892AB470125; Mon, 17 Aug 2026 18:41:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992091; cv=none; b=IVKTl2wJBQ/AqfayH/ujOMHCdeKK9nE/5jsMDWiat1jSrfAg8xh+eqSqIdzef2ffuFzZdZthJY+2VFQVqr8H+KIuyejtLkZSZJnMeDz4CsPdQtF1mkiyUED7gtmg1h5PJExE7e+NVFiaihrcNAt2V2DU5c68vVhnLCCDZxGk4u0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992091; c=relaxed/simple; bh=s7qmEIh6BTSzvB8kHFd3pxa7drzucQTpbxqF5pglosw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PbNmQJ6eTCY45m5g7faJzQCHtCCe3yPJL8KR5rBo9hSP7vcjJ+d2BD6oIv7xx9/U/M8fWCuZoaiU6GZZRf2TdvTXeY0Ynuj6veBuxW6wfEbsmBS1Ob3ZOv9LC1PEf823KUrvc61TaTsqM5IGcxSsW8PAaxPNIT3nsL3wKdn13u0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Ox4N41SE; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Ox4N41SE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992090; x=1818528090; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s7qmEIh6BTSzvB8kHFd3pxa7drzucQTpbxqF5pglosw=; b=Ox4N41SElCYVFOctHe7BDNgZzTpa41nw+WuOjaCXfoR0dR7QCxTs6FWT qW/K3tHPTqAonau20h7zgGLJl6iRKERpPtGMTUbHDHr0yh28wHcNvu2fl Y8OglUZyPWt/hG3PKpU+/g6fdneS5mrKkune4BTLzd9AJfl+Mwq+MWZ2w Qxqd+H0sdX4O8D8xyoakuaDvk+Pf+zZxprwSoKDJ1O5u6eWku/M4PjPCD +mpsj88m0VQw3xGzcmwR8XPURCoskiWNay5Z7KeoVkPWDKj/9VhqgS5fV c0epNOnQIDaTXDd1Su2chRWZOfS5D8/yUqTeBzuHwOScuwWLmen5qDYis A==; X-CSE-ConnectionGUID: XuUDIcCZSxOw1B7Is2SqOQ== X-CSE-MsgGUID: az1Z3xhbT1+bdcgXsdoLnQ== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066088" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066088" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: 4BbITAnWTkiPZ6zLRjD1SQ== X-CSE-MsgGUID: gXMATm2JShiDNIGDoiNT1w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656965" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 4/8] KVM: x86/pmu: Snapshot host IA32_PERF_CAPABILITIES in kvm_host Date: Mon, 17 Aug 2026 11:31:38 -0700 Message-ID: <20260817183143.226156-5-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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: Mingwei Zhang Cache the unadulterated snapshot of perf_capabilities so that KVM can compare guest vPMU capabilities against raw hardware capabilities. For example, if the host supports PERF_METRICS but it is not configured for the guest, KVM can use it to determine that RDPMC accesses must be intercepted. Signed-off-by: Mingwei Zhang Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi Reviewed-by: Jim Mattson --- v5: new patch. --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 8 ++------ arch/x86/kvm/x86.c | 4 ++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 8a4e542218a7..d893fa5be838 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -294,6 +294,7 @@ struct kvm_host_values { u64 xss; u64 s_cet; u64 arch_capabilities; + u64 perf_capabilities; }; extern struct kvm_host_values kvm_host; =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e3bfe6aca1a0..6e9d197982b7 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8023,14 +8023,10 @@ void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) static __init u64 vmx_get_perf_capabilities(void) { u64 perf_cap =3D PERF_CAP_FW_WRITES; - u64 host_perf_cap =3D 0; =20 if (!enable_pmu) return 0; =20 - if (boot_cpu_has(X86_FEATURE_PDCM)) - rdmsrq(MSR_IA32_PERF_CAPABILITIES, host_perf_cap); - if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR) && !enable_mediated_pmu) { x86_perf_get_lbr(&vmx_lbr_caps); @@ -8043,11 +8039,11 @@ static __init u64 vmx_get_perf_capabilities(void) if (!vmx_lbr_caps.has_callstack) memset(&vmx_lbr_caps, 0, sizeof(vmx_lbr_caps)); else if (vmx_lbr_caps.nr) - perf_cap |=3D host_perf_cap & PERF_CAP_LBR_FMT; + perf_cap |=3D kvm_host.perf_capabilities & PERF_CAP_LBR_FMT; } =20 if (vmx_pebs_supported()) { - perf_cap |=3D host_perf_cap & PERF_CAP_PEBS_MASK; + perf_cap |=3D kvm_host.perf_capabilities & PERF_CAP_PEBS_MASK; =20 /* * Disallow adaptive PEBS as it is functionally broken, can be diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d94b59140c45..d349224d2734 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7083,6 +7083,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) rdmsrq(MSR_IA32_ARCH_CAPABILITIES, kvm_host.arch_capabilities); =20 + if (boot_cpu_has(X86_FEATURE_PDCM)) + rdmsrq_safe(MSR_IA32_PERF_CAPABILITIES, + &kvm_host.perf_capabilities); + WARN_ON_ONCE(kvm_nr_uret_msrs); =20 r =3D ops->hardware_setup(); --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 BEB99470451; Mon, 17 Aug 2026 18:41:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992092; cv=none; b=gu098InSvh6KRWNSAdWtyB21WWjJ6BWloyXdAZczL+okHjeaPvnusEhhmFrMwi2QIyR7LzBjwhT3KYiE9R4gQQUNGw9KMmSTWNmZS1fW2Vedv5ZmAaHzfgPqcdwp+yJdQhkpzkvGagvGMxugTT6GNIQZXW9DK6myV+tFlwX7WHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992092; c=relaxed/simple; bh=YbFZ5sjoydE5xguc1kIwFm2K3DrAajQkO8whLN67b0c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cFtL8e9S9lEZcpIfK5F7hK/oWkCVORmGhiIbZsRW+6a88AXxfW71sKofFzWS4CjGxDkMf/HytSFvuPc4WIgWIRHWZ6Wd7t0mVow5+8J4F5QW80BMeJ1ltyfOtenz/rvKuTg0Kie4sY4D4rBFah/iUPU9bsfdK87V0Eu5EBSlMvA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=G7OCU5uM; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="G7OCU5uM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992091; x=1818528091; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YbFZ5sjoydE5xguc1kIwFm2K3DrAajQkO8whLN67b0c=; b=G7OCU5uM5RrVxCDkZTsXRQbd73bAU4FmR/jCro/VkwZURKIAEdX7P8qe /WI2q4nBPcHOn6WQuSP4AqUN4HTjOo5NzoXhownQY0AUXHki1kI1fk8BQ gh0qf2DlFRAc1NVoWF9aQIEkJDsry/nUatwD7qeGNRucw3lDYE+F9Vt40 wmSO+T3m18ebSGNtmxTFIYv5SzrtQ6tMC60JqSY37+ZBRv1W0YynjDDf+ Vvs8HLqM7DNeMu68UIi7XzbbHZ6zin/5q0VWdIgyP13clu586z7daqKns c9oSNRSBwmGISBfA+zC31lmL8ZXTROV/RkdsVuNY1Czzxz9I0I2/Z5laW A==; X-CSE-ConnectionGUID: k67R8I8KQUK7PQFMuGlwHw== X-CSE-MsgGUID: 7CV9erhaQzKa/XyHtTCXEQ== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066096" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066096" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: I1iBWZYRQSactsFS7HXLGQ== X-CSE-MsgGUID: VHJke1YORp23ucinCMqX3g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656969" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU Date: Mon, 17 Aug 2026 11:31:39 -0700 Message-ID: <20260817183143.226156-6-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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: Dapeng Mi Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR. Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical. Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses. Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host. If the host supports PERF_METRICS but it is not exposed to the guest, MSR_CORE_PERF_GLOBAL_CTRL must be intercepted so the guest cannot enable PERF_METRICS via GLOBAL_CTRL[48]. Signed-off-by: Dapeng Mi Co-developed-by: Zide Chen Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi --- v8: - Intercept global ctrl MSR if host supports PerfMetrics but not enabled in the guest. v7: - Clear bit 48 in global_ctrl_rsvd instead of explicitly clearing it in global_status_rsvd, allowing it to propagate through the assignment from global_ctrl_rsvd. - Add comments documenting the all-zero PERF_METRICS/FC3 write-order exception. - Add nested_vmx_merge_msr_bitmaps_rw(MSR_PERF_METRICS); v5: - Remove host_initiated check in set/get MSR handlers. v4: - Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko) - Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko) - Remove the redundant !! in vcpu_has_perf_metrics(). v3: - Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng) - Add comments to explain why we don't validate writes on PERF_METRICS. --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/msrs.c | 6 +++++- arch/x86/kvm/pmu.c | 7 +++++++ arch/x86/kvm/pmu.h | 5 +++++ arch/x86/kvm/vmx/nested.c | 2 ++ arch/x86/kvm/vmx/pmu_intel.c | 34 ++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 7 +++++++ 8 files changed, 62 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index d893fa5be838..7166efdf79ce 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -583,6 +583,7 @@ struct kvm_pmu { u64 global_status_rsvd; u64 reserved_bits; u64 raw_event_mask; + u64 perf_metrics; struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS]; struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS]; =20 diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index 18c4be75e927..fdcaeb6c8352 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -331,6 +331,7 @@ #define PERF_CAP_PEBS_FORMAT 0xf00 #define PERF_CAP_FW_WRITES BIT_ULL(13) #define PERF_CAP_PEBS_BASELINE BIT_ULL(14) +#define PERF_CAP_PERF_METRICS BIT_ULL(15) #define PERF_CAP_PEBS_TIMING_INFO BIT_ULL(17) #define PERF_CAP_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \ diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c index 1fbd8f08a7f0..2b2cc6bf3ce7 100644 --- a/arch/x86/kvm/msrs.c +++ b/arch/x86/kvm/msrs.c @@ -219,7 +219,7 @@ static const u32 msrs_to_save_pmu[] =3D { MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, MSR_ARCH_PERFMON_FIXED_CTR2, MSR_ARCH_PERFMON_FIXED_CTR3, MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, - MSR_CORE_PERF_GLOBAL_CTRL, + MSR_CORE_PERF_GLOBAL_CTRL, MSR_PERF_METRICS, MSR_IA32_PEBS_ENABLE, MSR_IA32_DS_AREA, MSR_PEBS_DATA_CFG, =20 /* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */ @@ -2608,6 +2608,10 @@ static void kvm_probe_msr_to_save(u32 msr_index) intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)) return; break; + case MSR_PERF_METRICS: + if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS)) + return; + break; case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1: if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=3D diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 913a0ecbf260..2fddcbc8924d 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -806,9 +806,16 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu= *vcpu) pmu->nr_arch_fixed_counters !=3D kvm_host_pmu.num_counters_fixed; } =20 +static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu) +{ + return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) && + !kvm_vcpu_has_perf_metrics(vcpu); +} + bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu) { return kvm_need_any_pmc_intercept(vcpu) || + kvm_need_perf_metrics_intercept(vcpu) || !kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu)); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept); diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 82f955e5a450..be578013ed83 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -279,6 +279,11 @@ static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vc= pu *vcpu) return vcpu->arch.perf_capabilities; } =20 +static inline bool kvm_vcpu_has_perf_metrics(struct kvm_vcpu *vcpu) +{ + return kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PERF_METRICS; +} + void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data); int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx); diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 7ed79894d11d..e81b8a5423c8 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -717,6 +717,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm= _vcpu *vcpu, nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL); nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS); nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL); + + nested_vmx_merge_msr_bitmaps_rw(MSR_PERF_METRICS); } =20 /* diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 100c072b7fb3..1fd0480baf68 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -189,6 +189,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u= 32 msr) switch (msr) { case MSR_CORE_PERF_FIXED_CTR_CTRL: return kvm_pmu_has_perf_global_ctrl(pmu); + case MSR_PERF_METRICS: + return kvm_vcpu_has_perf_metrics(vcpu); case MSR_IA32_PEBS_ENABLE: ret =3D kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PEBS_FORMAT; break; @@ -346,6 +348,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, str= uct msr_data *msr_info) case MSR_CORE_PERF_FIXED_CTR_CTRL: msr_info->data =3D pmu->fixed_ctr_ctrl; break; + case MSR_PERF_METRICS: + msr_info->data =3D pmu->perf_metrics; + break; case MSR_IA32_PEBS_ENABLE: msr_info->data =3D pmu->pebs_enable; break; @@ -395,6 +400,14 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, st= ruct msr_data *msr_info) if (pmu->fixed_ctr_ctrl !=3D data) reprogram_fixed_counters(pmu, data); break; + case MSR_PERF_METRICS: + /* + * Bits [63:32] are ignored on hardware that supports only + * level-1 metrics, but may be valid on hardware that supports + * level-2 metrics. Preserve guest writes verbatim. + */ + pmu->perf_metrics =3D data; + break; case MSR_IA32_PEBS_ENABLE: if (data & pmu->pebs_enable_rsvd) return 1; @@ -577,6 +590,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) counter_rsvd =3D ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) | ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX)= ); pmu->global_ctrl_rsvd =3D counter_rsvd; + if (perf_capabilities & PERF_CAP_PERF_METRICS) + pmu->global_ctrl_rsvd &=3D ~GLOBAL_CTRL_EN_PERF_METRICS; =20 /* * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET) @@ -633,6 +648,9 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) =20 static void intel_pmu_reset(struct kvm_vcpu *vcpu) { + struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); + + pmu->perf_metrics =3D 0; intel_pmu_release_guest_lbr_event(vcpu); } =20 @@ -803,6 +821,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vc= pu) struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); u64 global_status, toggle; =20 + if (kvm_vcpu_has_perf_metrics(vcpu)) + wrmsrq(MSR_PERF_METRICS, pmu->perf_metrics); + rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, global_status); toggle =3D pmu->global_status ^ global_status; if (global_status & toggle) @@ -831,6 +852,19 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vc= pu) */ if (pmu->fixed_ctr_ctrl_hw) wrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, 0); + + if (kvm_vcpu_has_perf_metrics(vcpu)) { + pmu->perf_metrics =3D rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS); + /* + * The SDM requires restoring fixed counter 3 before + * PERF_METRICS. However, this path writes 0 to PERF_METRICS + * before fixed counter 3. For this all-zero case, the + * resulting hardware state is therefore the same regardless + * of write order. + */ + if (pmu->perf_metrics) + wrmsrq(MSR_PERF_METRICS, 0); + } } =20 struct kvm_pmu_ops intel_pmu_ops __initdata =3D { diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6e9d197982b7..8cb9d642277a 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4277,6 +4277,10 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm= _vcpu *vcpu) MSR_TYPE_RW, intercept); vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_OVF_CTRL, MSR_TYPE_RW, intercept); + + intercept =3D !has_mediated_pmu || !kvm_vcpu_has_perf_metrics(vcpu); + vmx_set_intercept_for_msr(vcpu, MSR_PERF_METRICS, + MSR_TYPE_RW, intercept); } =20 static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu) @@ -8068,6 +8072,9 @@ static __init u64 vmx_get_perf_capabilities(void) perf_cap &=3D ~PERF_CAP_PEBS_BASELINE; } =20 + if (enable_mediated_pmu) + perf_cap |=3D kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS; + return perf_cap; } =20 --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 E4FCE470420; Mon, 17 Aug 2026 18:41:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992091; cv=none; b=ZUv0KnGdzTMaDakiCXJOEdUUcrJ7+sDFNmhfNjvZKKNxsiYudEG3NCLnjbI2GbyEfMlc3fg1Ost2RiomCBsQ/fGX/HN0KbgY1emzz03f/wmCKvg00msDtmguPY2Mnxog792lf9MsoRGc8XAhNiDtpqCLvhIXbEaoJvh0ZYyD6Bs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992091; c=relaxed/simple; bh=fmZo8128F5mya0JvNtH8EI+8fwmC9ki0IKkwopRt7qs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PYitBlcCWsuMRjXL3mHrK16YJVn1uMkHXGlhRO4eNniAheH4Ehfev8Jy6z+qXPVD1Gto977l5AFJWShuCGlmBYhycBChky/ZteCn4c9e8UgnC0cMJbM5l47gYRloAeNmdvkBjpVzVzj0cy8OTpV5h1bS8tuAABMvq7mY2lsoRj8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=RT+CUZ59; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RT+CUZ59" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992090; x=1818528090; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fmZo8128F5mya0JvNtH8EI+8fwmC9ki0IKkwopRt7qs=; b=RT+CUZ59Zztf7PoRLCP/XduqABNKycyZi4H1z1zQX4au1u59vvrL0J6z 7rwUOyOD2djg8L0DpEn9U0TkloFAsBKMbylNsoJP+u83WwfzX7p0XrMqY R2GFwKuFFcVlgKMMO0vNzI6GnO73HR5B7YftLW1mJFgbWqHzOH0s4A2xM Pg1BUvRnAg+o2Eb6ny5IR1fsFLOA0QvshcgyuDrJiB+FzkzUxdfmJxeRh RVaw0D35LxgV0nPpX9nr0WhYFS2c2W4m8hvIDJPcocMjOwGvqmhO8fU+9 dwgakJr15juI7TPNyJ/IeOm3n1sPhdcNNWUvzA4/P5YtI7u9lY0rPxef3 A==; X-CSE-ConnectionGUID: B7+NOr4ARBKEJfceAeszkw== X-CSE-MsgGUID: j2RjBSXTQryRIg5tMptLEA== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066092" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066092" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: 83dk3Tw2QhKoFnS4DoTgEg== X-CSE-MsgGUID: KyGx+FjxTN+X1vqtH+O9Og== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656972" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 6/8] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks Date: Mon, 17 Aug 2026 11:31:40 -0700 Message-ID: <20260817183143.226156-7-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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 RDPMC emulation splits responsibility: rdpmc_ecx_to_pmc() in each vendor returns a kvm_pmc, then common code calls pmc_read_counter(). This design cannot support RDPMC reads that don't map to a counter, such as PERF_METRICS on Intel platforms. Replace rdpmc_ecx_to_pmc() with emulate_rdpmc(), which takes full ownership of the emulation and writes the result directly into @data. Opportunistically drop the redundant bitmask in intel_emulate_rdpmc() since pmc_read_counter() already applies the counter's bit-width mask. No functional change intended. Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi Reviewed-by: Jim Mattson Reviewed-by: Sandipan Das --- v6: new patch. --- arch/x86/include/asm/kvm-x86-pmu-ops.h | 2 +- arch/x86/kvm/pmu.c | 9 +-------- arch/x86/kvm/pmu.h | 4 ++-- arch/x86/kvm/svm/pmu.c | 13 +++++++++---- arch/x86/kvm/vmx/pmu_intel.c | 25 ++++++++++++------------- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/= kvm-x86-pmu-ops.h index 4a223c2793e3..4b50ed058aed 100644 --- a/arch/x86/include/asm/kvm-x86-pmu-ops.h +++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h @@ -13,7 +13,7 @@ * KVM_X86_PMU_OP_OPTIONAL() can be used for those functions that can have * a NULL definition. */ -KVM_X86_PMU_OP(rdpmc_ecx_to_pmc) +KVM_X86_PMU_OP(emulate_rdpmc) KVM_X86_PMU_OP(msr_idx_to_pmc) KVM_X86_PMU_OP_OPTIONAL(check_rdpmc_early) KVM_X86_PMU_OP(is_valid_msr) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 2fddcbc8924d..789624658e8f 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -768,8 +768,6 @@ static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, = unsigned idx, u64 *data) int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) { struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); - struct kvm_pmc *pmc; - u64 mask =3D ~0ull; =20 if (!pmu->version) return 1; @@ -777,17 +775,12 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx= , u64 *data) if (is_vmware_backdoor_pmc(idx)) return kvm_pmu_rdpmc_vmware(vcpu, idx, data); =20 - pmc =3D kvm_pmu_call(rdpmc_ecx_to_pmc)(vcpu, idx, &mask); - if (!pmc) - return 1; - if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_PCE) && (kvm_x86_call(get_cpl)(vcpu) !=3D 0) && kvm_is_cr0_bit_set(vcpu, X86_CR0_PE)) return 1; =20 - *data =3D pmc_read_counter(pmc) & mask; - return 0; + return kvm_pmu_call(emulate_rdpmc)(vcpu, idx, data); } =20 static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index be578013ed83..eb6bbc658f5b 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -24,8 +24,8 @@ #define KVM_FIXED_PMC_BASE_IDX INTEL_PMC_IDX_FIXED =20 struct kvm_pmu_ops { - struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu, - unsigned int idx, u64 *mask); + int (*emulate_rdpmc)(struct kvm_vcpu *vcpu, unsigned int idx, + u64 *data); struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr); int (*check_rdpmc_early)(struct kvm_vcpu *vcpu, unsigned int idx); bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr); diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index c18286545a7a..0517fd4bbcd7 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -84,10 +84,15 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu,= unsigned int idx) } =20 /* idx is the ECX register of RDPMC instruction */ -static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, - unsigned int idx, u64 *mask) +static int amd_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 = *data) { - return amd_pmu_get_pmc(vcpu_to_pmu(vcpu), idx); + struct kvm_pmc *pmc =3D amd_pmu_get_pmc(vcpu_to_pmu(vcpu), idx); + + if (!pmc) + return 1; + + *data =3D pmc_read_counter(pmc); + return 0; } =20 static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr) @@ -302,7 +307,7 @@ static bool amd_pmc_is_disabled_in_current_mode(struct = kvm_pmc *pmc) } =20 struct kvm_pmu_ops amd_pmu_ops __initdata =3D { - .rdpmc_ecx_to_pmc =3D amd_rdpmc_ecx_to_pmc, + .emulate_rdpmc =3D amd_emulate_rdpmc, .msr_idx_to_pmc =3D amd_msr_idx_to_pmc, .check_rdpmc_early =3D amd_check_rdpmc_early, .is_valid_msr =3D amd_is_valid_msr, diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 1fd0480baf68..bbf4afcffafc 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -85,14 +85,13 @@ static void reprogram_fixed_counters(struct kvm_pmu *pm= u, u64 data) } } =20 -static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, - unsigned int idx, u64 *mask) +static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, + u64 *data) { unsigned int type =3D idx & INTEL_RDPMC_TYPE_MASK; struct kvm_pmu *pmu =3D vcpu_to_pmu(vcpu); - struct kvm_pmc *counters; + struct kvm_pmc *counters, *pmc; unsigned int num_counters; - u64 bitmask; =20 /* * The encoding of ECX for RDPMC is different for architectural versus @@ -105,7 +104,9 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kv= m_vcpu *vcpu, * as KVM doesn't support such PMUs. */ if (WARN_ON_ONCE(!pmu->version)) - return NULL; + return 1; + + idx &=3D INTEL_RDPMC_INDEX_MASK; =20 /* * General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs @@ -119,23 +120,21 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct = kvm_vcpu *vcpu, case INTEL_RDPMC_FIXED: counters =3D pmu->fixed_counters; num_counters =3D pmu->nr_arch_fixed_counters; - bitmask =3D pmu->counter_bitmask[KVM_PMC_FIXED]; break; case INTEL_RDPMC_GP: counters =3D pmu->gp_counters; num_counters =3D pmu->nr_arch_gp_counters; - bitmask =3D pmu->counter_bitmask[KVM_PMC_GP]; break; default: - return NULL; + return 1; } =20 - idx &=3D INTEL_RDPMC_INDEX_MASK; if (idx >=3D num_counters) - return NULL; + return 1; =20 - *mask &=3D bitmask; - return &counters[array_index_nospec(idx, num_counters)]; + pmc =3D &counters[array_index_nospec(idx, num_counters)]; + *data =3D pmc_read_counter(pmc); + return 0; } =20 static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr) @@ -868,7 +867,7 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vcp= u) } =20 struct kvm_pmu_ops intel_pmu_ops __initdata =3D { - .rdpmc_ecx_to_pmc =3D intel_rdpmc_ecx_to_pmc, + .emulate_rdpmc =3D intel_emulate_rdpmc, .msr_idx_to_pmc =3D intel_msr_idx_to_pmc, .is_valid_msr =3D intel_is_valid_msr, .get_msr =3D intel_pmu_get_msr, --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 37EFF470E95; Mon, 17 Aug 2026 18:41:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992092; cv=none; b=fAnZq5TXyD6CMOkVOtVx7YO8u6Vca3dy1lAkckQwbFg5gxyfbYiJ+R+2pUvTuzRodXP1Pk3DR6nxRKzUIXuyizzK6Le3PEc4v6yyNHYR/On2ogfWaHDGBNENTW2mDnZk4mSFEslJSwy52Q0Fvgw5m2bay0q94DqXZ6pyHlg1gO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992092; c=relaxed/simple; bh=Jz1x2beAb1qK1p4LmaUmSKWX16jJTmBVaFlBSgFbNsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HYO1n6XM5eES5cCY7J6nra9hYPh+AHJHw7aV4DBktBdEyuF7vtjyD1vHvUDM9sOFy/xXwQcVsCXyflocVJwgDbBxgbSvvh5jS+2j5Vs9ZVGiuhmcpkEdGh+XYGw0dZHU8VjpzOzR7pPSnSdmmxHHDNgbTJ23rE65zWn4FUb6Fhw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=l+82761U; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="l+82761U" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992091; x=1818528091; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Jz1x2beAb1qK1p4LmaUmSKWX16jJTmBVaFlBSgFbNsE=; b=l+82761UjOw8SMIPNbbAsT+kHLvokYp6pGygfe4X1HbjC8nydClqvzXT MgEvzLf693FOAmP0wYabptCNd/3GQOa0jdx1D/wasmEsy5OXzlkrjiOnV leyLrwhpOoyld0tk0jDMez9Tt9XHLaFHalCyR8gG2F/+UPJEtsyqR94ay p8e7ToDIc12XlXr9MlvNZHxAhuirc6U/u16ClDKzsQWF996j1G4CB209v CYLECqxvj1CocSDAtikiViMApCg3tawy95JMFX2o3NrFlLqOSG5UWHOTH DmZyZveuUrT1mqOFtrh8vHqGjn0Ecv6PlqSLicywY2fvxn2rtNu27Xh/U A==; X-CSE-ConnectionGUID: e+uOUCwyTJ2Zfz4+ttm9Ug== X-CSE-MsgGUID: RdWd2vXoSLO3OrHQbKwoTQ== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066100" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066100" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: EFxth9WKTD+kUL3XL1m4MQ== X-CSE-MsgGUID: WGevhLLTRiC2vOC5lVc7Hg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656975" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics Date: Mon, 17 Aug 2026 11:31:41 -0700 Message-ID: <20260817183143.226156-8-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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" If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #GP when the guest attempts a PERF_METRICS RDPMC. If the guest has PERF_METRICS but RDPMC interception is enabled for other reasons, KVM needs to emulate RDPMC with type 0x2000. For simplicity, Metrics Clear Mode is not supported. Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi --- v8: - Move kvm_need_perf_metrics_intercept() to patch 5/9. v6: - Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc(). - Reject non-zero index. v5: - new patch. --- arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index 789624658e8f..dab0bcabaa8f 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -825,6 +825,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) return true; =20 return kvm_need_any_pmc_intercept(vcpu) || + kvm_need_perf_metrics_intercept(vcpu) || pmu->counter_bitmask[KVM_PMC_GP] !=3D (BIT_ULL(kvm_host_pmu.bit_wi= dth_gp) - 1) || pmu->counter_bitmask[KVM_PMC_FIXED] !=3D (BIT_ULL(kvm_host_pmu.bit= _width_fixed) - 1); } diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index bbf4afcffafc..3ca61e4b0ba6 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -31,6 +31,7 @@ */ #define INTEL_RDPMC_GP 0 #define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE +#define INTEL_RDPMC_METRICS INTEL_PMC_FIXED_RDPMC_METRICS =20 #define INTEL_RDPMC_TYPE_MASK GENMASK(31, 16) #define INTEL_RDPMC_INDEX_MASK GENMASK(15, 0) @@ -125,6 +126,19 @@ static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, = unsigned int idx, counters =3D pmu->gp_counters; num_counters =3D pmu->nr_arch_gp_counters; break; + case INTEL_RDPMC_METRICS: + if (!kvm_vcpu_has_perf_metrics(vcpu)) + return 1; + + /* + * The index in ECX[15:0] is implementation specific, but no + * platform currently supports a non-zero index. + */ + if (idx) + return 1; + + *data =3D pmu->perf_metrics; + return 0; default: return 1; } --=20 2.55.0 From nobody Mon Sep 28 20:05:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 E62D646D2CE; Mon, 17 Aug 2026 18:41:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992093; cv=none; b=XUZJgANmbxKrS+6wGPmNbYsWvem3qPtTsP84gsnexK015FlYbBRwu5zkDGLR0UZ4WHbOrmq2J4lEA0yIZM4tetXVocBNbEXHXwLbJgxB2xeoHftd0XgRUNEgiQWl29GNkM7OTtFchYxZMcp17q7XjvXvy+E9oNghzfdA1AfIWZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786992093; c=relaxed/simple; bh=CA2oEWXAGzXMaflBM9URHLErnrCMESPPejAA4r7Xsuw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hY7Ut8D8sEaUoHnlMTcBvFbvhLc1QJ631zx3GWdtpTPZeJEV5ZrHvmmG9vwf9N0pUWn93N/rDvyGV6MdIup1o3BnpM8cXuvHiezoN03Mz2fBRYjKJrzsF8yjh52SweJWpblHIpaN7vRIj77CYvYy0cl3d3RgzSoeWrS5RbJr/e0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=eAhoPCPN; arc=none smtp.client-ip=192.198.163.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="eAhoPCPN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1786992092; x=1818528092; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CA2oEWXAGzXMaflBM9URHLErnrCMESPPejAA4r7Xsuw=; b=eAhoPCPN4+vwGvJfu+A0KqQBv1D+mGwhIu8CvWC8Qa94N7VJvsdsU0KF owcpVEF7+eONkaRBcwRpT8Ow8tS1ObYdusy9Agx9c8vxI8c0p36NxzQ4U bqad8Ak33vltnMPZrLoJIx4BxNgZrhUCvv4Rqc/8/JKlh+vAVO9r8J6Tx SjAZSnDSpptbkQeap5QayKBQuTdAIw98hWo25i3YYnKxuHu+VZgQ+0Xb2 kyxorNvlrVBfxMusBF3XK/ITHHh96HlGZZ6jTEW9gJpr/6u9GiRG3emMN CITAkOdzYSxaAE1RNlH3Ue1GHr7Jjl28LkOuOQg+E66XoRPUVhtSW9gAz A==; X-CSE-ConnectionGUID: 7Cc6tlBETQeXZeDjbaEI5Q== X-CSE-MsgGUID: AVcHZ10PSk2wzsPPE68Z2A== X-IronPort-AV: E=McAfee;i="6800,10657,11878"; a="98066102" X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="98066102" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:25 -0700 X-CSE-ConnectionGUID: kCJeJ5KhQ6WjzomJwDr6cA== X-CSE-MsgGUID: 52ZH8L2RR8C4hw7nvvYYFA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,229,1779174000"; d="scan'208";a="263656978" Received: from 9cc2c43eec6b.jf.intel.com ([10.54.77.29]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Aug 2026 11:41:24 -0700 From: Zide Chen To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, Jim Mattson , Andi Kleen , linux-kernel@vger.kernel.org, Mingwei Zhang , Zide Chen , Das Sandipan , Shukla Manali , Dapeng Mi , Xudong Hao Subject: [PATCH v8 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests Date: Mon, 17 Aug 2026 11:31:42 -0700 Message-ID: <20260817183143.226156-9-zide.chen@intel.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260817183143.226156-1-zide.chen@intel.com> References: <20260817183143.226156-1-zide.chen@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" Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15]. Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile. Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality. Signed-off-by: Zide Chen Reviewed-by: Dapeng Mi --- v7: - Use RDPMC instead of RDMSR in PERF_METRICS sanity test. v6: - Move perf metrics test out of test_arch_events(); it doesn't belong there, and this also avoids redundant runs of perf metrics test. - Correct the +/-3 error margin. v3: - Slightly reword comment to explain the sum of Topdown metrics is close to 100%. - Change abs() with explicit bounds (sum >=3D 0xfd && sum <=3D 0x102) for better readability. v2: - New patch. --- tools/arch/x86/include/asm/msr-index.h | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 3 + .../selftests/kvm/x86/pmu_counters_test.c | 93 ++++++++++++++++++- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/tools/arch/x86/include/asm/msr-index.h b/tools/arch/x86/includ= e/asm/msr-index.h index eff29645719b..e7745e2cd543 100644 --- a/tools/arch/x86/include/asm/msr-index.h +++ b/tools/arch/x86/include/asm/msr-index.h @@ -331,6 +331,7 @@ #define PERF_CAP_PEBS_FORMAT 0xf00 #define PERF_CAP_FW_WRITES BIT_ULL(13) #define PERF_CAP_PEBS_BASELINE BIT_ULL(14) +#define PERF_CAP_PERF_METRICS BIT_ULL(15) #define PERF_CAP_PEBS_TIMING_INFO BIT_ULL(17) #define PERF_CAP_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \ diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/= selftests/kvm/include/x86/pmu.h index 608ed83d7c6a..6c19503e0bb7 100644 --- a/tools/testing/selftests/kvm/include/x86/pmu.h +++ b/tools/testing/selftests/kvm/include/x86/pmu.h @@ -52,6 +52,9 @@ /* Fixed PMC controls, Intel only. */ #define FIXED_PMC_GLOBAL_CTRL_ENABLE(_idx) BIT_ULL((32 + (_idx))) =20 +/* PERF_METRICS enable, Intel only. */ +#define PERF_METRICS_GLOBAL_CTRL_ENABLE BIT_ULL(48) + #define FIXED_PMC_KERNEL BIT_ULL(0) #define FIXED_PMC_USER BIT_ULL(1) #define FIXED_PMC_ANYTHREAD BIT_ULL(2) diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/te= sting/selftests/kvm/x86/pmu_counters_test.c index c3e784e16348..5abcf21d8612 100644 --- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c @@ -3,6 +3,7 @@ * Copyright (C) 2023, Tencent, Inc. */ #include +#include =20 #include "pmu.h" #include "processor.h" @@ -254,17 +255,20 @@ do { \ ); \ } while (0) =20 -#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ +#define GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP) \ do { \ - wrmsr(_pmc_msr, 0); \ - \ if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt %[m]", FEP); \ else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush %[m]", FEP); \ else \ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \ - \ +} while (0) + +#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ +do { \ + wrmsr(_pmc_msr, 0); \ + GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP); \ guest_assert_event_count(_idx, _pmc, _pmc_msr); \ } while (0) =20 @@ -412,7 +416,7 @@ static void test_arch_events(u8 pmu_version, u64 perf_c= apabilities) * other than PMCs in the future. */ #define MAX_NR_GP_COUNTERS 8 -#define MAX_NR_FIXED_COUNTERS 3 +#define MAX_NR_FIXED_COUNTERS 4 =20 #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector) \ __GUEST_ASSERT(expect_gp ? vector =3D=3D GP_VECTOR : !vector, \ @@ -653,8 +657,81 @@ static void test_fixed_counters(u8 pmu_version, u64 pe= rf_capabilities) pmu_vm_free(vm, vcpus); } =20 +static void __guest_test_perf_metrics(void) +{ + int retiring, bad_spec, fe_bound, be_bound, sum; + u64 global_ctrl, metrics; + + if ((guest_get_pmu_version() < 2) || /* Does guest have GLOBAL_CTRL? */ + !this_cpu_has(X86_FEATURE_PDCM) || + !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS)) + return; + + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0); + wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0); + wrmsr(MSR_PERF_METRICS, 0); + + /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */ + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL)); + global_ctrl =3D FIXED_PMC_GLOBAL_CTRL_ENABLE(3) | + PERF_METRICS_GLOBAL_CTRL_ENABLE; + + GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, ""); + + /* Check test results. */ + metrics =3D rdmsr(MSR_PERF_METRICS); + retiring =3D FIELD_GET(GENMASK_ULL(7, 0), metrics); + bad_spec =3D FIELD_GET(GENMASK_ULL(15, 8), metrics); + fe_bound =3D FIELD_GET(GENMASK_ULL(23, 16), metrics); + be_bound =3D FIELD_GET(GENMASK_ULL(31, 24), metrics); + + /* + * Be conservative: the measured payload definitely retires work, so + * Retiring should be non-zero. + */ + GUEST_ASSERT_NE(metrics, 0); + GUEST_ASSERT_NE(retiring, 0); + + /* + * Each level-1 Topdown metric is an integer fraction of 0xff. + * A +/-3 error margin is chosen for a loose sanity check. + */ + sum =3D retiring + bad_spec + fe_bound + be_bound; + GUEST_ASSERT(sum >=3D 0xfc && sum <=3D 0x102); + + /* Sanity check after PERF_METRICS disabled. */ + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS})); + GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics); + wrmsr(MSR_PERF_METRICS, 0xdeaddead); + + guest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0xdeaddead); +} + +static void guest_test_perf_metrics(void) +{ + __guest_test_perf_metrics(); + GUEST_DONE(); +} + +static void test_perf_metrics(u8 pmu_version, u64 perf_capabilities) +{ + struct kvm_vcpu **vcpus; + struct kvm_vm *vm; + + pr_info("Testing Perf Metrics, PMU version %u, perf_caps =3D %lx\n", + pmu_version, perf_capabilities); + + vm =3D pmu_vm_create_with_vcpus(1, guest_test_perf_metrics, + pmu_version, perf_capabilities, &vcpus); + + run_vcpu(vcpus[0]); + + pmu_vm_free(vm, vcpus); +} + static void test_intel_counters(void) { + u64 advertised_perf_caps =3D kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITI= ES); u8 pmu_version =3D kvm_cpu_property(X86_PROPERTY_PMU_VERSION); unsigned int i; u8 v; @@ -662,6 +739,7 @@ static void test_intel_counters(void) const u64 perf_caps[] =3D { 0, PMU_CAP_FW_WRITES, + PERF_CAP_PERF_METRICS, }; =20 /* @@ -699,9 +777,14 @@ static void test_intel_counters(void) if (!kvm_has_perf_caps && perf_caps[i]) continue; =20 + /* Ignore unsupported features. */ + if (perf_caps[i] & ~advertised_perf_caps) + continue; + test_arch_events(v, perf_caps[i]); test_gp_counters(v, perf_caps[i]); test_fixed_counters(v, perf_caps[i]); + test_perf_metrics(v, perf_caps[i]); } } } --=20 2.55.0