From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796106171; b=r5QpeaXedWWu1Zgc+P9WOO7z+enoXVQPROWukUR0g7icmCemSQa57b/UkVAy/Dz4PdNV eIOqUc4o16LRpVDL5UvXAYrg4ok7IPFtfF+Ou2dxLYMXIeWZ47JJYNzFeKkzDA3zS+ebp tIFUwYew1L7JWEk6++3B4KYj23+jT44VviOQ57Y/61W+A+7nsU9wJVXzXrnxnBvWjhPgt TxM1qjQC5eZXsispot/fa7NF6odmASplLwLDG/EkTTWJTbsMyBjck7r/iYiKRBFh6df0p sAFLDA1x00zL+Dtp3kjtnZ5AyFXDOctoTZ4cCPWvTWzx3mNIrEP5BoPGJ59oXoz50iA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796106171; 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=q4s5DckNkWeKgf4Ux19L35wmPVEhO9t+BRVoE4MLtjA=; b=SZ63WSPXWWLNSKa008Yb/N+5ay81asHc9CJfsBQAeYzVbi+WNYzd5n3gi5hY7Yu84isU o/6PssiCnQoCOy18hS/vgg9ZVi2fbKoX5/fO9E8DuBH2OAfReD2rvxC4y/b0YiQSOGFaZ M04isJdN6dBWmEtzgz/7u53tkCxejoXqUNHe3G/TC+gfMh5DTHyknSiKPl/sK9qdz6/WH 6pGH2FHpwRCiHt4kRZ+X+bR0/7wFnwE52+/KCUZCsYuRJ25r+/vKIjvBjTipF/44WWqN2 XfiowaZdGbp1fmDcxCpO2jQhgPKkv+ajgNxQdoBdcgmsWX8ZoA0fVoz/Tq1E/2mB+MA== 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=nyxWeAcF; arc=none smtp.remote-ip=198.175.65.9 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65CF0606A31A; Tue, 1 Dec 2026 06:22:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 65CF0606A31A 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, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=nyxWeAcF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796106170; x=1827642170; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=q4s5DckNkWeKgf4Ux19L35wmPVEhO9t+BRVoE4MLtjA=; b=nyxWeAcFtVbn3kt4iG+OE7WMuBvUC7evG7iRYXMEZmfrjEI1E40bcT+C NHP53slPVPM4Yk4R0PRcsciu11xTfdtcDqH2ezQWkiMUPCHUjHUTohrzZ e9+E3St663nJUROhsN3OycjAZtl6sWrTY+8znq1Kr9GwXjf+5pipJAOmG 8EK7wIqrRKF/XUZWcfh+WFT1t2zNAMUr/CXKm8BGl4mAoZRvFbs6vYCv+ clE20sjstaWITi4cpaSfLWvhfKULSegaYPXeSRPzm0AWD8mBHPDAT80Uu 0K89Dp2TphzA5hweD4WL3i7JGHKzVzR5Xl32m2xCCEt6U5jnU3Ne2aovw A==; X-CSE-ConnectionGUID: uXCbJ+xuR1C0PCpHkf0kHA== X-CSE-MsgGUID: ydW/dPwuTvuVqYJtZLnOAQ== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4872949" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="4872949" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 22:22:50 -0800 X-CSE-ConnectionGUID: gaEhcNLRR6S3xC92n23wrA== X-CSE-MsgGUID: 7ncirsgYSb+yErYX0Qd7iw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13738489" 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:22:47 -0800 Message-ID: <29868ae1-210d-4ce3-b88b-006a91e32014@linux.intel.com> Date: Tue, 1 Dec 2026 14:22:44 +0800 X-Mailing-List: kvm@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 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS 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-11-zide.chen@intel.com> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: <20261124213901.120572-11-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: > On hosts supporting PERF_METRICS, if either the PERF_METRICS MSR or > fixed counter 3 is absent from the guest, keep GLOBAL_CTRL and RDPMC > intercepted so the guest cannot set GLOBAL_CTRL[48] or read metrics > via RDPMC without fixed counter 3. > > KVM does not expose PERF_METRICS or fixed counter 3 to guests yet, > so this keeps GLOBAL_CTRL and RDPMC intercepted on hosts that > support PERF_METRICS until both are virtualized. > > Opportunistically, rename kvm_need_any_pmc_intercept() to > kvm_pmu_incomplete_passthrough(). The function does not decide whether > a particular PMC MSR needs interception. It reports whether the guest > PMU differs from the host PMU, and the PERF_METRICS check widens that > further. > > Signed-off-by: Zide Chen > --- > v10: new patch. > --- > arch/x86/include/asm/msr-index.h | 1 + > arch/x86/kvm/pmu.c | 22 +++++++++++++++++----- > arch/x86/kvm/pmu.h | 5 +++++ > 3 files changed, 23 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h > index 3a8e51a0c9e8..8fad944197fb 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/pmu.c b/arch/x86/kvm/pmu.c > index dba651d4e449..4574cac5f148 100644 > --- a/arch/x86/kvm/pmu.c > +++ b/arch/x86/kvm/pmu.c > @@ -778,7 +778,7 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) > return 0; > } > > -static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu) > +static bool kvm_pmu_incomplete_passthrough(struct kvm_vcpu *vcpu) > { > struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); > > @@ -790,13 +790,25 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu) > * KVM's capabilities are constrained based on KVM support, i.e. KVM's > * capabilities themselves may be a subset of hardware capabilities. > */ > - return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || > - pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed; > + if (pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || > + pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed) > + return true; > + > + /* > + * RDPMC passthrough must be blocked if fixed counter 3 is missing > + * to avoid leaking host metrics, as IA32_PERF_METRICS is not > + * context-switched without it. > + * > + * That condition is already caught by the counter count check above, > + * so only check for PERF_METRICS here. > + */ > + return (kvm_host_pmu.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) || > + return kvm_pmu_incomplete_passthrough(vcpu) || > !kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu)); > } > EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept); > @@ -812,7 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) > if (enable_vmware_backdoor) > return true; > > - return kvm_need_any_pmc_intercept(vcpu) || > + return kvm_pmu_incomplete_passthrough(vcpu) || > pmu->counter_bitmask[KVM_PMC_GP] != (BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) || > pmu->counter_bitmask[KVM_PMC_FIXED] != (BIT_ULL(kvm_host_pmu.bit_width_fixed) - 1); > } > diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h > index 2a9c1bbe5c40..ae1e6545df40 100644 > --- a/arch/x86/kvm/pmu.h > +++ b/arch/x86/kvm/pmu.h > @@ -281,6 +281,11 @@ static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu) > return vcpu->arch.perf_capabilities; > } > > +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);