KVM: x86: Fastpath accounting fixes
Sean Christopherson
maintainercover KVM: x86: Fastpath accounting fixes
Fix two bugs where KVM fails to account/trace exits that are handled in the super fast fastpath. SVM doesn't actually utilize the super fast fastpath, i.e. patch 1 only affects VMX and the bug fixed by patch 2 is benign in the current code base.
Found by inspection, confirmed by hacking together a small selftest and manually verifying the stats via sysfs.
Sean Christopherson (2):
KVM: x86: Account fastpath-only VM-Exits in vCPU stats
KVM: SVM: Invoke trace_kvm_exit() for fastpath VM-Exits
arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/x86.c | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-)
base-commit: 39428f6ea9eace95011681628717062ff7f5eb5f
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Sean Christopherson
maintainerPATCH 1/2 KVM: x86: Account fastpath-only VM-Exits in vCPU stats
Increment vcpu->stat.exits when handling a fastpath VM-Exit without going through any part of the "slow" path. Not bumping the exits stat can result in wildly misleading exit counts, e.g. if the primary reason the guest is exiting is to program the TSC deadline timer.
--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -10754,6 +10754,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) exit_fastpath =EXIT_FASTPATH_EXIT_HANDLED ;break ; } + +/* Note, VM-Exits that go down the "slow" path are accounted below. */ + ++vcpu->stat.exits; }/*
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Sean Christopherson
maintainerPATCH 2/2 KVM: SVM: Invoke trace_kvm_exit() for fastpath VM-Exits
In reply to Sean ChristophersonMove SVM's call to trace_kvm_exit() from the "slow" VM-Exit handler to svm_vcpu_run() so that KVM traces fastpath VM-Exits that re-enter the guest without bouncing through the slow path. This bug is benign in the current code base as KVM doesn't currently support any such exits on SVM.
--- arch/x86/kvm/svm/svm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +2 -2
@@ -3418,8 +3418,6 @@ static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)struct kvm_run *kvm_run = vcpu->run;u32 exit_code = svm->vmcb->control.exit_code; -trace_kvm_exit (vcpu,KVM_ISA_SVM ); -/* SEV-ES guests must use the CR write traps to track CR registers. */ if (!sev_es_guest (vcpu->kvm)) {if (!svm_is_intercept (svm,INTERCEPT_CR0_WRITE ))
@@ -4156,6 +4154,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) SVM_EXIT_EXCP_BASE + MC_VECTOR))svm_handle_mce (vcpu); +trace_kvm_exit (vcpu,KVM_ISA_SVM ); +svm_complete_interrupts (vcpu);if (is_guest_mode (vcpu))
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Quoted 9 lines · Sean Christopherson
Fix two bugs where KVM fails to account/trace exits that are handled in the super fast fastpath. SVM doesn't actually utilize the super fast fastpath, i.e. patch 1 only affects VMX and the bug fixed by patch 2 is benign in the current code base.
Found by inspection, confirmed by hacking together a small selftest and manually verifying the stats via sysfs.
[...]
Applied patch 1 to kvm-x86 fixes (for 6.4), and patch 2 to kvm-x86 svm (for 6.5). Should have posted these separately...
[1/2] KVM: x86: Account fastpath-only VM-Exits in vCPU stats
https://github.com/kvm-x86/linux/commit/8b703a49c9df [2/2] KVM: SVM: Invoke trace_kvm_exit() for fastpath VM-Exits https://github.com/kvm-x86/linux/commit/791a089861fc
--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes
https://github.com/kvm-x86/linux/tree/svm