From 9be579d2265185ec92d75e4540fc6c4e621f1667 Mon Sep 17 00:00:00 2001 From: Yosry Ahmed Date: Wed, 27 May 2026 23:46:58 +0000 Subject: [PATCH] KVM: nSVM: Move VMRUN instruction retirement after entering guest mode A successful VMRUN retires in guest mode and should be counted by the PMU as a guest instruction. Move the call to kvm_pmu_instruction_retired() after potentially entering guest mode, such that VMRUN is counted correctly. The PMU event will be matched against L2's CPL, but otherwise this does not change the behavior in terms of guest vs. host, because KVM does not virtualize Host-Only/Guest-Only PMC controls yet, so all instructions are counted regardless of the vCPU's host/guest state. But this change is needed for the incoming support for Host-Only/Guest-Only controls to count VMRUN correctly. Signed-off-by: Yosry Ahmed Link: https://patch.msgid.link/20260527234711.4175166-5-yosry@kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/nested.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 2ef31efd4c9d..3a90b7f83d8e 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1150,10 +1150,8 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) if (!svm_skip_emulated_instruction(vcpu)) return 0; - kvm_pmu_instruction_retired(vcpu); - if (ret) - return 1; + goto insn_retired; /* * Since vmcb01 is not in use, we can use it to store some of the L1 @@ -1183,6 +1181,12 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) nested_svm_vmexit(svm); } +insn_retired: + /* + * A successful VMRUN is counted by the PMU in guest mode, so only + * retire the instruction after potentially entering guest mode. + */ + kvm_pmu_instruction_retired(vcpu); return 1; }