]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: x86: Bump hypercall stat prior to fully completing hypercall
authorSean Christopherson <seanjc@google.com>
Thu, 28 Nov 2024 00:43:42 +0000 (16:43 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sun, 22 Dec 2024 18:00:25 +0000 (13:00 -0500)
Increment the "hypercalls" stat for KVM hypercalls as soon as KVM knows
it will skip the guest instruction, i.e. once KVM is committed to emulating
the hypercall.  Waiting until completion adds no known value, and creates a
discrepancy where the stat will be bumped if KVM exits to userspace as a
result of trying to skip the instruction, but not if the hypercall itself
exits.

Handling the stat in common code will also avoid the need for another
helper to dedup code when TDX comes along (TDX needs a separate completion
path due to GPR usage differences).

Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>
Message-ID: <20241128004344.4072099-5-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/x86.c

index d485a53ac4103de37333eeb906a5e59476475585..754d8166eabfa30233c30d6ce8b6f564963c46b2 100644 (file)
@@ -9979,7 +9979,6 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
        if (!is_64_bit_hypercall(vcpu))
                ret = (u32)ret;
        kvm_rax_write(vcpu, ret);
-       ++vcpu->stat.hypercalls;
        return kvm_skip_emulated_instruction(vcpu);
 }
 
@@ -9990,6 +9989,8 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
 {
        unsigned long ret;
 
+       ++vcpu->stat.hypercalls;
+
        trace_kvm_hypercall(nr, a0, a1, a2, a3);
 
        if (!op_64_bit) {
@@ -10068,7 +10069,6 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
 
                WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ);
                vcpu->arch.complete_userspace_io = complete_hypercall_exit;
-               /* stat is incremented on completion. */
                return 0;
        }
        default:
@@ -10077,7 +10077,6 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
        }
 
 out:
-       ++vcpu->stat.hypercalls;
        return ret;
 }
 EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall);