Skip to contentSkip to conversation

KVM: x86: Fastpath accounting fixes

PATCH Applied Sean Christopherson 1 participant 4 messages Jun 2, 2023 – Jun 3, 2023

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover 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
author maintainer
#1 lore raw

PATCH 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.

Fixes 404d5d7bff0d ("KVM: X86: Introduce more exit_fastpath_completion enum values")
---
 arch/x86/kvm/x86.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
index ceb7c5e9cf9e..a609e39b2cb8 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/2 KVM: SVM: Invoke trace_kvm_exit() for fastpath VM-Exits

In reply to Sean Christopherson

Move 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.

Fixes a9ab13ff6e84 ("KVM: X86: Improve latency for single target IPI fastpath")
---
 arch/x86/kvm/svm/svm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +2 -2
index ca32389f3c36..6845f4f2dc33 100644
@@ -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
author maintainer Applied
#3 lore raw
In reply to Sean Christopherson
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