Skip to contentSkip to conversation

kvm: tracing: Fix unmatched kvm_entry and kvm_exit events

PATCH Applied Dario Faggioli 2 participants 4 messages Dec 23, 2020 – Jan 21, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: tracing: Fix unmatched kvm_entry and kvm_exit events

From: Lorenzo Brescia <[email protected]>

On VMX, if we exit and then re-enter immediately without leaving the vmx_vcpu_run() function, the kvm_entry event is not logged. That means we will see one (or more) kvm_exit, without its (their) corresponding kvm_entry, as shown here:

CPU-1979 [002] 89.871187: kvm_entry: vcpu 1 CPU-1979 [002] 89.871218: kvm_exit: reason MSR_WRITE CPU-1979 [002] 89.871259: kvm_exit: reason MSR_WRITE

It also seems possible for a kvm_entry event to be logged, but then we leave vmx_vcpu_run() right away (if vmx->emulation_required is true). In this case, we will have a spurious kvm_entry event in the trace.

Fix these situations by moving trace_kvm_entry() inside vmx_vcpu_run() (where trace_kvm_exit() already is).

A trace obtained with this patch applied looks like this:

CPU-14295 [000] 8388.395387: kvm_entry: vcpu 0 CPU-14295 [000] 8388.395392: kvm_exit: reason MSR_WRITE CPU-14295 [000] 8388.395393: kvm_entry: vcpu 0 CPU-14295 [000] 8388.395503: kvm_exit: reason EXTERNAL_INTERRUPT

Of course, not calling trace_kvm_entry() in common x86 code any longer means that we need to adjust the SVM side of things too.

---
Cc: Paolo Bonzini <[email protected]>
Cc: Sean Christopherson <[email protected]>
Cc: Vitaly Kuznetsov <[email protected]>
Cc: Wanpeng Li <[email protected]>
Cc: Jim Mattson <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: [email protected]
Cc: Lorenzo Brescia <[email protected]>
Cc: Dario Faggioli <[email protected]>

---
 arch/x86/kvm/svm/svm.c |    2 ++
 arch/x86/kvm/vmx/vmx.c |    2 ++
 arch/x86/kvm/x86.c     |    3 +--
 3 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +2 -0
index cce0143a6f80..ed272fcf6495 100644
@@ -3741,6 +3741,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
+	trace_kvm_entry(vcpu);
+
 	svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
 	svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
 	svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
arch/x86/kvm/vmx/vmx.c +2 -0
index 75c9c6a0a3a4..ff20f9e6e5b3 100644
@@ -6653,6 +6653,8 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
 	if (vmx->emulation_required)
 		return EXIT_FASTPATH_NONE;
 
+	trace_kvm_entry(vcpu);
+
 	if (vmx->ple_window_dirty) {
 		vmx->ple_window_dirty = false;
 		vmcs_write32(PLE_WINDOW, vmx->ple_window);
arch/x86/kvm/x86.c +1 -2
index 3f7c1fc7a3ce..a79666204907 100644
@@ -8973,8 +8973,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		kvm_x86_ops.request_immediate_exit(vcpu);
 	}
 
-	trace_kvm_entry(vcpu);
-
 	fpregs_assert_state_consistent();
 	if (test_thread_flag(TIF_NEED_FPU_LOAD))
 		switch_fpu_return();
@@ -11538,6 +11536,7 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
 }
 EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
 
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
#1 lore raw
Quoted 7 lines · Dario Faggioli

From: Lorenzo Brescia <[email protected]>

On VMX, if we exit and then re-enter immediately without leaving the vmx_vcpu_run() function, the kvm_entry event is not logged. That means we will see one (or more) kvm_exit, without its (their) corresponding kvm_entry, as shown here:

Quoted 9 lines

 CPU-1979 [002] 89.871187: kvm_entry: vcpu 1 CPU-1979 [002] 89.871218: kvm_exit:  reason MSR_WRITE CPU-1979 [002] 89.871259: kvm_exit:  reason MSR_WRITE

It also seems possible for a kvm_entry event to be logged, but then we leave vmx_vcpu_run() right away (if vmx->emulation_required is true). In this case, we will have a spurious kvm_entry event in the trace.

Gentle ping... :-)

Thanks and Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)
maintainer
#2 lore raw
In reply to Dario Faggioli
Quoted 89 lines · Dario Faggioli

From: Lorenzo Brescia <[email protected]>

On VMX, if we exit and then re-enter immediately without leaving the vmx_vcpu_run() function, the kvm_entry event is not logged. That means we will see one (or more) kvm_exit, without its (their) corresponding kvm_entry, as shown here:

CPU-1979 [002] 89.871187: kvm_entry: vcpu 1 CPU-1979 [002] 89.871218: kvm_exit: reason MSR_WRITE CPU-1979 [002] 89.871259: kvm_exit: reason MSR_WRITE

It also seems possible for a kvm_entry event to be logged, but then we leave vmx_vcpu_run() right away (if vmx->emulation_required is true). In this case, we will have a spurious kvm_entry event in the trace.

Fix these situations by moving trace_kvm_entry() inside vmx_vcpu_run() (where trace_kvm_exit() already is).

A trace obtained with this patch applied looks like this:

CPU-14295 [000] 8388.395387: kvm_entry: vcpu 0 CPU-14295 [000] 8388.395392: kvm_exit: reason MSR_WRITE CPU-14295 [000] 8388.395393: kvm_entry: vcpu 0 CPU-14295 [000] 8388.395503: kvm_exit: reason EXTERNAL_INTERRUPT

Of course, not calling trace_kvm_entry() in common x86 code any longer means that we need to adjust the SVM side of things too.

---
Cc: Paolo Bonzini <[email protected]>
Cc: Sean Christopherson <[email protected]>
Cc: Vitaly Kuznetsov <[email protected]>
Cc: Wanpeng Li <[email protected]>
Cc: Jim Mattson <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: [email protected]
Cc: Lorenzo Brescia <[email protected]>
Cc: Dario Faggioli <[email protected]>

---
  arch/x86/kvm/svm/svm.c |    2 ++
  arch/x86/kvm/vmx/vmx.c |    2 ++
  arch/x86/kvm/x86.c     |    3 +--
  3 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +2 -0
index cce0143a6f80..ed272fcf6495 100644
@@ -3741,6 +3741,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
  {
  	struct vcpu_svm *svm = to_svm(vcpu);
 
+	trace_kvm_entry(vcpu);
+
  	svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  	svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  	svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
arch/x86/kvm/vmx/vmx.c +2 -0
index 75c9c6a0a3a4..ff20f9e6e5b3 100644
@@ -6653,6 +6653,8 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
  	if (vmx->emulation_required)
  		return EXIT_FASTPATH_NONE;
 
+	trace_kvm_entry(vcpu);
+
  	if (vmx->ple_window_dirty) {
  		vmx->ple_window_dirty = false;
  		vmcs_write32(PLE_WINDOW, vmx->ple_window);
arch/x86/kvm/x86.c +0 -2
index 3f7c1fc7a3ce..a79666204907 100644
@@ -8973,8 +8973,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  		kvm_x86_ops.request_immediate_exit(vcpu);
  	}
 
-	trace_kvm_entry(vcpu);
-
  	fpregs_assert_state_consistent();
  	if (test_thread_flag(TIF_NEED_FPU_LOAD))
  		switch_fpu_return();

arch/x86/kvm/x86.c int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, · line 11542

Quoted 7 lines
arch/x86/kvm/x86.c +1 -0
@@ -11538,6 +11536,7 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
  }
  EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
 
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry);
  EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
  EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);

Qeuued, thanks!

Paolo

maintainer Applied
#3 lore raw
In reply to Dario Faggioli
Quoted 89 lines · Dario Faggioli

From: Lorenzo Brescia <[email protected]>

On VMX, if we exit and then re-enter immediately without leaving the vmx_vcpu_run() function, the kvm_entry event is not logged. That means we will see one (or more) kvm_exit, without its (their) corresponding kvm_entry, as shown here:

CPU-1979 [002] 89.871187: kvm_entry: vcpu 1 CPU-1979 [002] 89.871218: kvm_exit: reason MSR_WRITE CPU-1979 [002] 89.871259: kvm_exit: reason MSR_WRITE

It also seems possible for a kvm_entry event to be logged, but then we leave vmx_vcpu_run() right away (if vmx->emulation_required is true). In this case, we will have a spurious kvm_entry event in the trace.

Fix these situations by moving trace_kvm_entry() inside vmx_vcpu_run() (where trace_kvm_exit() already is).

A trace obtained with this patch applied looks like this:

CPU-14295 [000] 8388.395387: kvm_entry: vcpu 0 CPU-14295 [000] 8388.395392: kvm_exit: reason MSR_WRITE CPU-14295 [000] 8388.395393: kvm_entry: vcpu 0 CPU-14295 [000] 8388.395503: kvm_exit: reason EXTERNAL_INTERRUPT

Of course, not calling trace_kvm_entry() in common x86 code any longer means that we need to adjust the SVM side of things too.

---
Cc: Paolo Bonzini <[email protected]>
Cc: Sean Christopherson <[email protected]>
Cc: Vitaly Kuznetsov <[email protected]>
Cc: Wanpeng Li <[email protected]>
Cc: Jim Mattson <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: [email protected]
Cc: Lorenzo Brescia <[email protected]>
Cc: Dario Faggioli <[email protected]>

---
  arch/x86/kvm/svm/svm.c |    2 ++
  arch/x86/kvm/vmx/vmx.c |    2 ++
  arch/x86/kvm/x86.c     |    3 +--
  3 files changed, 5 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +2 -0
index cce0143a6f80..ed272fcf6495 100644
@@ -3741,6 +3741,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
  {
  	struct vcpu_svm *svm = to_svm(vcpu);
 
+	trace_kvm_entry(vcpu);
+
  	svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  	svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  	svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
arch/x86/kvm/vmx/vmx.c +2 -0
index 75c9c6a0a3a4..ff20f9e6e5b3 100644
@@ -6653,6 +6653,8 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
  	if (vmx->emulation_required)
  		return EXIT_FASTPATH_NONE;
 
+	trace_kvm_entry(vcpu);
+
  	if (vmx->ple_window_dirty) {
  		vmx->ple_window_dirty = false;
  		vmcs_write32(PLE_WINDOW, vmx->ple_window);
arch/x86/kvm/x86.c +0 -2
index 3f7c1fc7a3ce..a79666204907 100644
@@ -8973,8 +8973,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  		kvm_x86_ops.request_immediate_exit(vcpu);
  	}
 
-	trace_kvm_entry(vcpu);
-
  	fpregs_assert_state_consistent();
  	if (test_thread_flag(TIF_NEED_FPU_LOAD))
  		switch_fpu_return();

arch/x86/kvm/x86.c int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, · line 11542

Quoted 7 lines
arch/x86/kvm/x86.c +1 -0
@@ -11538,6 +11536,7 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
  }
  EXPORT_SYMBOL_GPL(kvm_sev_es_string_io);
 
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry);
  EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
  EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);

Queued, thanks.

Paolo