From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796107846; b=qf2GopNi157UoY+LPaXjHOIPiorJuovuB2Ao4OT0Cs7k2A0ya2sY8pVmLFNQXzpKG9dc vG2Xc8Rsj2sERq7bU73cJIYf4DNk8ksvIwChtoQo4DIwkBNkrBh/OyTt9Je8f/ljdQnYs /LD5Po7ckQnKuUIavSXosn1zKqLswe7jRMTF5fc3OzIz8KA0trw0znISC39Sx1T5zOLMw m3cSxOSywoPA47bptyMjXaKLL4aHJw9238vGyIfSNUs92SzpTL3Q2sTAzSG1itBRN9KIG LU4u99Noafw+OlpFdaDHIybDSvRi9PwsKHwp7b9sc1sGvM+tSpXEUV5aCamlv+uyYNw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796107846; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=kACcIxC4mNG7DM0rPv/9/VCC/Uk+1BQ6rO1lizjoq3E=; b=GjE8LCPTzt5B1Yaio+aeV4wA8fIDz2tsKsnwrSSbPxOsQfT4i6VdNBSTjiWYbva/Z+cI HtUDOTxjmuzqZOz6q/7CGs3KGkbti2QSk30IQFFSm0q023d9C4a9nJUkMoheWb0ORjFja Gyxho7cGEAZN278HSyr2ov9JjL6O3RVCxg4OozeFFlLtM8aUsOSou2EbkCCsWbnxVYrQI QXT5u0jyG6hzjvt79W92wyR7CNbr2NAwT0o+hEilctdn48J87mZ077x8A2yPP1zjhGO0n xOh1AYLI1ugn9asAY2nRQrOFABr4pTfmTOxyMIyn0UdmidYi/ymJET0uNvyULFMv1Ew== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Z8PLD6bl; arc=none smtp.remote-ip=198.175.65.19 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03785606A687; Tue, 1 Dec 2026 06:50:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 03785606A687 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Z8PLD6bl DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796107844; x=1827643844; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=kACcIxC4mNG7DM0rPv/9/VCC/Uk+1BQ6rO1lizjoq3E=; b=Z8PLD6blTz1Ys+tQN3CCTmBKdboNpNozWsyhqI6o95u+Taeby5+zZhRU IrIpQNesGLom/UipouPuqYiHIdR+OtB8knzzjFTl/HpWZR6mgNlRG6h9T QWmEAGNJrpeqffAbUamoMb1CY7YGhGl4giSgK8doTtL515sT1xDLvwN2y C3KC6wQhtrRHCgm++EYR0NRrqD4WYe4gnvdbxNnZnU0qu94wGwB12WvR/ Ym9qvtH+ti7ZbbEBieTUg/NkXX6ZktIfYOXXU/csV4OMjrnpn0xh0jkjJ NFYBURcha+Kkx9UHQhtUrjmBAvvdhyQ93Rx6Faq1zV/IjHCMmrFWqlBsc g==; X-CSE-ConnectionGUID: 0V+dxIXXT0Savcl3AoTDSg== X-CSE-MsgGUID: pMT/P+piQhKnSJ4nh3ezwQ== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="5037246" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="5037246" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 22:50:43 -0800 X-CSE-ConnectionGUID: m5xBTZFbR5yngq5GXyASKA== X-CSE-MsgGUID: dM5tr+TaQy2OysSQ+s2UUg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13285669" Received: from unknown (HELO [10.238.2.182]) ([10.238.2.182]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 22:50:40 -0800 Message-ID: <9c4bfdcb-d786-406f-ae23-cdbcf520e9a0@linux.intel.com> Date: Tue, 1 Dec 2026 14:50:37 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode To: Zide Chen , Sean Christopherson , Paolo Bonzini , Peter Zijlstra Cc: kvm@vger.kernel.org, Jim Mattson , Yosry Ahmed , Andi Kleen , Stephane Eranian , linux-kernel@vger.kernel.org, Mingwei Zhang , Das Sandipan , Shukla Manali , Xudong Hao References: <20261124213901.120572-1-zide.chen@intel.com> <20261124213901.120572-16-zide.chen@intel.com> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: <20261124213901.120572-16-zide.chen@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Reviewed-by: Dapeng Mi On 11/25/2026 5:38 AM, Zide Chen wrote: > Add PERF_CAP_RDPMC_METRICS_CLEAR (IA32_PERF_CAPABILITIES bit 19) > support. When set, and IA32_FIXED_CTR_CTRL.METRICS_CLEAR_EN (bit 14) > is also set, RDPMC of PERF_METRICS clears both PERF_METRICS and fixed > counter 3 (SLOTS) after the read. > > Advertise the capability to guests only when the mediated vPMU is > enabled and the host reports it, and allow the guest to set > METRICS_CLEAR_EN for fixed counter 3 when PERF_METRICS, > RDPMC_METRICS_CLEAR, and fixed counter 3 are all exposed to the guest. > > For the mediated vPMU, METRICS_CLEAR_EN takes effect natively via > passthrough. However, RDPMC can still be intercepted for unrelated > reasons, so also emulate the clear-on-read behavior for consistency. > > Signed-off-by: Zide Chen > --- > v10: > - Gate METRICS_CLEAR_EN unmasking on both perf_metrics and FC3. > - Check for valid fixed counter 3 PMC before clearing it in > intel_emulate_rdpmc(). > - Drop check in vmx_set_msr() requiring PERF_CAP_PERF_METRICS. (Jim) > - Split event filter mask fix into a separate patch ("KVM: x86/pmu: Mask > only OS/USR bits when filtering fixed counters"). > v9: new patch. > --- > arch/x86/include/asm/msr-index.h | 1 + > arch/x86/kvm/vmx/pmu_intel.c | 26 ++++++++++++++++++++++++++ > arch/x86/kvm/vmx/vmx.c | 3 ++- > 3 files changed, 29 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h > index 8fad944197fb..5851edcdda05 100644 > --- a/arch/x86/include/asm/msr-index.h > +++ b/arch/x86/include/asm/msr-index.h > @@ -333,6 +333,7 @@ > #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_RDPMC_METRICS_CLEAR BIT_ULL(19) > #define PERF_CAP_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ > PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \ > PERF_CAP_PEBS_TIMING_INFO) > diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c > index da6dae261514..0861c65bee95 100644 > --- a/arch/x86/kvm/vmx/pmu_intel.c > +++ b/arch/x86/kvm/vmx/pmu_intel.c > @@ -86,6 +86,12 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) > } > } > > +static bool intel_pmu_metrics_clear_enabled(struct kvm_pmu *pmu) > +{ > + return fixed_ctrl_field(pmu->fixed_ctr_ctrl, KVM_FIXED_SLOTS_IDX) & > + INTEL_FIXED_3_METRICS_CLEAR; > +} > + > static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, > u64 *data) > { > @@ -140,6 +146,20 @@ static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, > return 1; > > *data = pmu->perf_metrics; > + > + /* > + * Per the SDM, RDPMC of PERF_METRICS clears both PERF_METRICS > + * and the SLOTS fixed counter when Metrics Clear Mode is > + * enabled via INTEL_FIXED_3_METRICS_CLEAR in FIXED_CTR_CTRL. > + */ > + if (intel_pmu_metrics_clear_enabled(pmu)) { > + pmc = kvm_pmc_idx_to_pmc(pmu, INTEL_PMC_IDX_FIXED_SLOTS); > + > + pmu->perf_metrics = 0; > + if (pmc) > + pmc_write_counter(pmc, 0); > + } > + > return 0; > default: > return 1; > @@ -552,6 +572,12 @@ static void intel_pmu_refresh_perf_metrics(struct kvm_vcpu *vcpu) > ~GENMASK_ULL(kvm_pmu_cap.num_topdown_events * > INTEL_TD_METRIC_FIELD_BITS - 1, 0); > } > + > + if (kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_RDPMC_METRICS_CLEAR) { > + pmu->fixed_ctr_ctrl_rsvd &= > + ~intel_fixed_bits_by_idx(KVM_FIXED_SLOTS_IDX, > + INTEL_FIXED_3_METRICS_CLEAR); > + } > } > > static void intel_pmu_refresh(struct kvm_vcpu *vcpu) > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 7441808be049..d977fc3a31d8 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -7944,7 +7944,8 @@ static __init u64 vmx_get_perf_capabilities(void) > */ > if (enable_mediated_pmu && kvm_pmu_cap.num_topdown_events && > kvm_pmu_cap.num_counters_fixed > KVM_FIXED_SLOTS_IDX) > - perf_cap |= kvm_pmu_cap.perf_capabilities & PERF_CAP_PERF_METRICS; > + perf_cap |= kvm_pmu_cap.perf_capabilities & > + (PERF_CAP_PERF_METRICS | PERF_CAP_RDPMC_METRICS_CLEAR); > > return perf_cap; > }