From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796189283; b=PzUnS2XtNM06C+sAwbA3k25/ZaBgn5sKduxjFVkEFtIN7WmVhggFwWBtDCgDq+tquLX8 caLd5qay2J8voNWmPNGJmW01QUG2MFm39F2oXAB7Cr03rkO6oMZctRCOZ2FpHfrzt+guw y2d9ia5vMXgx1OvK3WGa7TUvBn3ympNsgfo+JRAl0SKio02+g6JzXBA4gbjuH7QG920WY 5+YHhpI2YcCR46GBi09SKtoVLB06BvEKerZKPh5UdKUvIIMxXK46iehLLwzYeYvqJlpvd Ov7NGP7JkZmuut1umgPFFwxyXa6VHzxhxyZwBJPL2lE+IPR35DuDrezBssWu+17Mi6g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796189283; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=qZbM9D1XZXfZhRDyDNN3shDmpULTs4DOhn6bTJj49YY=; b=QwCBYLZVrB3Y7GPoYec1UMdUgnYcHK8lbHFtKcCX7ivEs6KNRf7s1WUMs50oHcJ/yINy U0Li57VQHCDB4sZjNETjHeBCS9w0Wduq9nQnzZ8AjBET8Z/acx5ueo+S8ZN2EiXk50zDA vh7s+0rZZZS4YpMUZmR8ef3X58rn9wRNRnSPvg9tOQkuiGgtD5asktlpy3xxbsuN4KeSy XZP+puZ8kAVI7ymvMWIVMLgp8lBeuP/Pw4DdxmNHnlIV7aZdSnj7hKIIvpAGrXMarlpk4 B4tlai80Pqiize+8XpcJDxRcYEIdc8uILstOdnDgNYWKv7eJl3vKQbyZ5YQEkvRz7TQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gHmlknPa; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90988606A322; Wed, 2 Dec 2026 05:28:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gHmlknPa Received: by smtp.kernel.org (Postfix) with ESMTPSA id E00571F00A15; Wed, 2 Dec 2026 05:28:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796189283; bh=qZbM9D1XZXfZhRDyDNN3shDmpULTs4DOhn6bTJj49YY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=gHmlknPaeQlXMShH6hfMggPfEVLO6pPBHxdFAbVuPIeY7HBugl8Ec2vGXrYzDsiBn 1XrDDS2QTvSmpZ2r9Va0ad94VMEHiXYdKLwqFuFmv7O4av6Q7gd/rzF+okwQOgbTfb eLXxvsn1q8ul0+08B9v427bUcB7ClO7c95zxKDilksb6wa0mZ9enyzGIz55R/Dsqxs IMgBOIapfyr9esLCWzAmVKEcmuVceZH+6mC9Vfm2lMcCZr6UU0bQxuWYtO08XV1DJV D6v9xoJJEG/qqC0BZ/3pOjMuhuXa4liCRfCR9zUVDpNr1S8PkzUnTrniRe2FcUmOVm 4bN/Q1jlDAdOQ== From: Yosry Ahmed To: Sean Christopherson Cc: Paolo Bonzini , Jim Mattson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Subject: [PATCH v2 3/4] KVM: x86/pmu: Always defer PMU reprogramming on SVM nested transitions Date: Wed, 2 Dec 2026 05:27:53 +0000 Message-ID: <20261202052754.3170526-4-yosry@kernel.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: <20261202052754.3170526-1-yosry@kernel.org> References: <20261202052754.3170526-1-yosry@kernel.org> 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 Content-Transfer-Encoding: 8bit Always defer PMU counter reprogramming via KVM_REQ_PMU on SVM nested transitions and EFER.SVME toggles instead of invoking kvm_pmu_handle_event() synchronously. The motivation for synchronous handling was to ensure to-be-emulated PMCs are updated before instruction emulation completes, such that instructions count in the mode they retire in (as per the hardware behavior). However, since kvm_pmu_request_counters_reprogram() now recalculates to-be-emulated PMCs synchronously, and kvm_pmu_recalc_pmc_emulation() -> pmc_is_locally_enabled() already accounts for Host-Only and Guest-Only bits on AMD, VMRUN or WRMSR(MSR_EFER) would correctly be counted in the mode they retire in. Signed-off-by: Yosry Ahmed --- arch/x86/kvm/pmu.c | 1 - arch/x86/kvm/pmu.h | 26 ++++++++------------------ arch/x86/kvm/svm/nested.c | 10 +--------- arch/x86/kvm/svm/svm.h | 18 ++---------------- 4 files changed, 11 insertions(+), 44 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index eef2f792dc9e8..ae781bce7d322 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -698,7 +698,6 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) kvm_for_each_pmc(pmu, pmc, bit, bitmap) kvm_pmu_recalc_pmc_emulation(pmu, pmc); } -EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_pmu_handle_event); int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx) { diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 8aa9db1569167..910a09ed9274e 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -216,11 +216,8 @@ extern struct x86_pmu_capability kvm_pmu_cap; void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops); void kvm_pmu_recalc_pmc_emulation(struct kvm_pmu *pmu, struct kvm_pmc *pmc); -void kvm_pmu_handle_event(struct kvm_vcpu *vcpu); -static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, - u64 counters, - bool defer) +static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu, u64 counters) { DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX); struct kvm_pmc *pmc; @@ -229,22 +226,14 @@ static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, if (!counters) return; + BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters)); atomic64_or(counters, &pmu->__reprogram_pmi); - if (defer) { - BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters)); - bitmap_from_u64(bitmap, counters); - kvm_for_each_pmc(pmu, pmc, bit, bitmap) - kvm_pmu_recalc_pmc_emulation(pmu, pmc); - kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu)); - } else { - kvm_pmu_handle_event(pmu_to_vcpu(pmu)); - } -} + bitmap_from_u64(bitmap, counters); -static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu, - u64 counters) -{ - __kvm_pmu_reprogram_counters(pmu, counters, true); + kvm_for_each_pmc(pmu, pmc, bit, bitmap) + kvm_pmu_recalc_pmc_emulation(pmu, pmc); + + kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu)); } static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc) @@ -278,6 +267,7 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) } void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu); +void kvm_pmu_handle_event(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); bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr); diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 1cecb82702e2c..122be289660c3 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1539,15 +1539,7 @@ void svm_leave_nested(struct kvm_vcpu *vcpu) svm->nested.vmcb12_gpa = INVALID_GPA; leave_guest_mode(vcpu); - - /* - * Force leaving nested is a non-architectural flow so precision - * isn't a priority. Defer updating the PMU until the next vCPU - * run, potentially tolerating some imprecision to avoid poking - * into PMU state from arbitrary contexts (e.g. to avoid using - * stale state). - */ - __svm_pmu_handle_nested_transition(svm, true); + svm_pmu_handle_nested_transition(svm); svm_switch_vmcb(svm, &svm->vmcb01); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 011ab9cd40301..1c7727d47125c 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -914,26 +914,12 @@ bool nested_svm_check_vmcb01_save(struct kvm_vcpu *vcpu, struct vmcb_save_area *save); void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb); - -static inline void __svm_pmu_handle_nested_transition(struct vcpu_svm *svm, - bool defer) +static inline void svm_pmu_handle_nested_transition(struct vcpu_svm *svm) { struct kvm_pmu *pmu = vcpu_to_pmu(&svm->vcpu); u64 counters = *(u64 *)pmu->pmc_has_mode_specific_enables; - __kvm_pmu_reprogram_counters(pmu, counters, defer); -} - -static inline void svm_pmu_handle_nested_transition(struct vcpu_svm *svm) -{ - /* - * Do NOT defer reprogramming the counters by default. Instructions - * causing a state change are counted based on the _new_ CPU state - * (e.g. a successful VMRUN is counted in guest mode). Hence, the - * counters should be reprogrammed with the new state _before_ the - * instruction is potentially counted upon emulation completion. - */ - __svm_pmu_handle_nested_transition(svm, false); + kvm_pmu_request_counters_reprogram(pmu, counters); } extern struct kvm_x86_nested_ops svm_nested_ops; -- 2.56.1.911.g883d8bcb20-goog