Skip to contentSkip to conversation

KVM: X86: Some tracepoint enhancements

PATCH Superseded Peter Xu 2 participants 8 messages Sep 5, 2019 – Sep 6, 2019

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

v3 cover KVM: X86: Some tracepoint enhancements

v3:
- use unsigned int for vcpu id [Sean]
- a new patch to fix ple_window type [Sean]

v2:
- fix commit messages, change format of ple window tracepoint [Sean]
- rebase [Wanpeng]

Each small patch explains itself. I noticed them when I'm tracing some IRQ paths and I found them helpful at least to me.

Please have a look. Thanks,

Peter Xu (4):
KVM: X86: Trace vcpu_id for vmexit
KVM: X86: Remove tailing newline for tracepoints
KVM: X86: Tune PLE Window tracepoint
KVM: VMX: Change ple_window type to unsigned int

 arch/x86/kvm/svm.c     | 16 ++++++++--------
 arch/x86/kvm/trace.h   | 34 ++++++++++++++--------------------
 arch/x86/kvm/vmx/vmx.c | 18 ++++++++++--------
 arch/x86/kvm/vmx/vmx.h |  2 +-
 arch/x86/kvm/x86.c     |  2 +-
 5 files changed, 34 insertions(+), 38 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH v3 1/4 KVM: X86: Trace vcpu_id for vmexit

Tracing the ID helps to pair vmenters and vmexits for guests with multiple vCPUs.

---
 arch/x86/kvm/trace.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/trace.h +4 -1
index b5c831e79094..20d6cac9f157 100644
@@ -232,17 +232,20 @@ TRACE_EVENT(kvm_exit,
 		__field(	u32,	        isa             )
 		__field(	u64,	        info1           )
 		__field(	u64,	        info2           )
+		__field(	unsigned int,	vcpu_id         )
 	),
 
 	TP_fast_assign(
 		__entry->exit_reason	= exit_reason;
 		__entry->guest_rip	= kvm_rip_read(vcpu);
 		__entry->isa            = isa;
+		__entry->vcpu_id        = vcpu->vcpu_id;
 		kvm_x86_ops->get_exit_info(vcpu, &__entry->info1,
 					   &__entry->info2);
 	),
 
-	TP_printk("reason %s rip 0x%lx info %llx %llx",
+	TP_printk("vcpu %u reason %s rip 0x%lx info %llx %llx",
+		  __entry->vcpu_id,
 		 (__entry->isa == KVM_ISA_VMX) ?
 		 __print_symbolic(__entry->exit_reason, VMX_EXIT_REASONS) :
 		 __print_symbolic(__entry->exit_reason, SVM_EXIT_REASONS),
Signature · 1 line
2.21.0
author
#3 lore raw

PATCH v3 2/4 KVM: X86: Remove tailing newline for tracepoints

In reply to Peter Xu

It's done by TP_printk() already.

---
 arch/x86/kvm/trace.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/trace.h +2 -2
index 20d6cac9f157..8a7570f8c943 100644
@@ -1323,7 +1323,7 @@ TRACE_EVENT(kvm_avic_incomplete_ipi,
 		__entry->index = index;
 	),
 
-	TP_printk("vcpu=%u, icrh:icrl=%#010x:%08x, id=%u, index=%u\n",
+	TP_printk("vcpu=%u, icrh:icrl=%#010x:%08x, id=%u, index=%u",
 		  __entry->vcpu, __entry->icrh, __entry->icrl,
 		  __entry->id, __entry->index)
 );
@@ -1348,7 +1348,7 @@ TRACE_EVENT(kvm_avic_unaccelerated_access,
 		__entry->vec = vec;
 	),
 
-	TP_printk("vcpu=%u, offset=%#x(%s), %s, %s, vec=%#x\n",
+	TP_printk("vcpu=%u, offset=%#x(%s), %s, %s, vec=%#x",
 		  __entry->vcpu,
 		  __entry->offset,
 		  __print_symbolic(__entry->offset, kvm_trace_symbol_apic),
Signature · 1 line
2.21.0
author
#4 lore raw

PATCH v3 3/4 KVM: X86: Tune PLE Window tracepoint

In reply to Peter Xu

The PLE window tracepoint triggers even if the window is not changed, and the wording can be a bit confusing too. One example line:

kvm_ple_window: vcpu 0: ple_window 4096 (shrink 4096)

It easily let people think of "the window now is 4096 which is shrinked", but the truth is the value actually didn't change (4096).

Let's only dump this message if the value really changed, and we make the message even simpler like:

kvm_ple_window: vcpu 4 old 4096 new 8192 (growed)

---
 arch/x86/kvm/svm.c     | 16 ++++++++--------
 arch/x86/kvm/trace.h   | 21 ++++++---------------
 arch/x86/kvm/vmx/vmx.c | 14 ++++++++------
 arch/x86/kvm/x86.c     |  2 +-
 4 files changed, 23 insertions(+), 30 deletions(-)
arch/x86/kvm/svm.c +8 -8
index d685491fce4d..d5cb6b5a9254 100644
@@ -1269,11 +1269,11 @@ static void grow_ple_window(struct kvm_vcpu *vcpu)
 							pause_filter_count_grow,
 							pause_filter_count_max);
 
-	if (control->pause_filter_count != old)
+	if (control->pause_filter_count != old) {
 		mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
-
-	trace_kvm_ple_window_grow(vcpu->vcpu_id,
-				  control->pause_filter_count, old);
+		trace_kvm_ple_window_update(vcpu->vcpu_id,
+					    control->pause_filter_count, old);
+	}
 }
 
 static void shrink_ple_window(struct kvm_vcpu *vcpu)
@@ -1287,11 +1287,11 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu)
 						    pause_filter_count,
 						    pause_filter_count_shrink,
 						    pause_filter_count);
-	if (control->pause_filter_count != old)
+	if (control->pause_filter_count != old) {
 		mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
-
-	trace_kvm_ple_window_shrink(vcpu->vcpu_id,
-				    control->pause_filter_count, old);
+		trace_kvm_ple_window_update(vcpu->vcpu_id,
+					    control->pause_filter_count, old);
+	}
 }
 
 static __init int svm_hardware_setup(void)
arch/x86/kvm/trace.h +6 -15
index 8a7570f8c943..f1177e03768f 100644
@@ -890,36 +890,27 @@ TRACE_EVENT(kvm_pml_full,
 	TP_printk("vcpu %d: PML full", __entry->vcpu_id)
 );
 
-TRACE_EVENT(kvm_ple_window,
-	TP_PROTO(bool grow, unsigned int vcpu_id, int new, int old),
-	TP_ARGS(grow, vcpu_id, new, old),
+TRACE_EVENT(kvm_ple_window_update,
+	TP_PROTO(unsigned int vcpu_id, int new, int old),
+	TP_ARGS(vcpu_id, new, old),
 
 	TP_STRUCT__entry(
-		__field(                bool,      grow         )
 		__field(        unsigned int,   vcpu_id         )
 		__field(                 int,       new         )
 		__field(                 int,       old         )
 	),
 
 	TP_fast_assign(
-		__entry->grow           = grow;
 		__entry->vcpu_id        = vcpu_id;
 		__entry->new            = new;
 		__entry->old            = old;
 	),
 
-	TP_printk("vcpu %u: ple_window %d (%s %d)",
-	          __entry->vcpu_id,
-	          __entry->new,
-	          __entry->grow ? "grow" : "shrink",
-	          __entry->old)
+	TP_printk("vcpu %u old %d new %d (%s)",
+	          __entry->vcpu_id, __entry->old, __entry->new,
+		  __entry->old < __entry->new ? "growed" : "shrinked")
 );
 
-#define trace_kvm_ple_window_grow(vcpu_id, new, old) \
-	trace_kvm_ple_window(true, vcpu_id, new, old)
-#define trace_kvm_ple_window_shrink(vcpu_id, new, old) \
-	trace_kvm_ple_window(false, vcpu_id, new, old)
-
 TRACE_EVENT(kvm_pvclock_update,
 	TP_PROTO(unsigned int vcpu_id, struct pvclock_vcpu_time_info *pvclock),
 	TP_ARGS(vcpu_id, pvclock),
arch/x86/kvm/vmx/vmx.c +8 -6
index 42ed3faa6af8..469c4134a4a7 100644
@@ -5233,10 +5233,11 @@ static void grow_ple_window(struct kvm_vcpu *vcpu)
 					    ple_window_grow,
 					    ple_window_max);
 
-	if (vmx->ple_window != old)
+	if (vmx->ple_window != old) {
 		vmx->ple_window_dirty = true;
-
-	trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
+		trace_kvm_ple_window_update(vcpu->vcpu_id,
+					    vmx->ple_window, old);
+	}
 }
 
 static void shrink_ple_window(struct kvm_vcpu *vcpu)
@@ -5248,10 +5249,11 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu)
 					      ple_window_shrink,
 					      ple_window);
 
-	if (vmx->ple_window != old)
+	if (vmx->ple_window != old) {
 		vmx->ple_window_dirty = true;
-
-	trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
+		trace_kvm_ple_window_update(vcpu->vcpu_id,
+					    vmx->ple_window, old);
+	}
 }
 
 /*
arch/x86/kvm/x86.c +1 -1
index 93b0bd45ac73..69ad184edc90 100644
@@ -10082,7 +10082,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
-EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
Signature · 1 line
2.21.0
author
#5 lore raw

PATCH v3 4/4 KVM: VMX: Change ple_window type to unsigned int

In reply to Peter Xu

The VMX ple_window is 32 bits wide, so logically it can overflow with an int. The module parameter is declared as unsigned int which is good, however the dynamic variable is not. Switching all the ple_window references to use unsigned int.

The tracepoint changes will also affect SVM, but SVM is using an even smaller width (16 bits) so it's always fine.

---
 arch/x86/kvm/trace.h   | 8 ++++----
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 arch/x86/kvm/vmx/vmx.h | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/trace.h +4 -4
index f1177e03768f..ae924566c401 100644
@@ -891,13 +891,13 @@ TRACE_EVENT(kvm_pml_full,
 );
 
 TRACE_EVENT(kvm_ple_window_update,
-	TP_PROTO(unsigned int vcpu_id, int new, int old),
+	TP_PROTO(unsigned int vcpu_id, unsigned int new, unsigned int old),
 	TP_ARGS(vcpu_id, new, old),
 
 	TP_STRUCT__entry(
 		__field(        unsigned int,   vcpu_id         )
-		__field(                 int,       new         )
-		__field(                 int,       old         )
+		__field(        unsigned int,       new         )
+		__field(        unsigned int,       old         )
 	),
 
 	TP_fast_assign(
@@ -906,7 +906,7 @@ TRACE_EVENT(kvm_ple_window_update,
 		__entry->old            = old;
 	),
 
-	TP_printk("vcpu %u old %d new %d (%s)",
+	TP_printk("vcpu %u old %u new %u (%s)",
 	          __entry->vcpu_id, __entry->old, __entry->new,
 		  __entry->old < __entry->new ? "growed" : "shrinked")
 );
arch/x86/kvm/vmx/vmx.c +2 -2
index 469c4134a4a7..1dbb63ffdd6d 100644
@@ -5227,7 +5227,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
 static void grow_ple_window(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	int old = vmx->ple_window;
+	unsigned int old = vmx->ple_window;
 
 	vmx->ple_window = __grow_ple_window(old, ple_window,
 					    ple_window_grow,
@@ -5243,7 +5243,7 @@ static void grow_ple_window(struct kvm_vcpu *vcpu)
 static void shrink_ple_window(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	int old = vmx->ple_window;
+	unsigned int old = vmx->ple_window;
 
 	vmx->ple_window = __shrink_ple_window(old, ple_window,
 					      ple_window_shrink,
arch/x86/kvm/vmx/vmx.h +1 -1
index 82d0bc3a4d52..64d5a4890aa9 100644
@@ -253,7 +253,7 @@ struct vcpu_vmx {
 	struct nested_vmx nested;
 
 	/* Dynamic PLE window. */
-	int ple_window;
+	unsigned int ple_window;
 	bool ple_window_dirty;
 
 	bool req_immediate_exit;
Signature · 1 line
2.21.0
#6 lore raw

Re: [PATCH v3 4/4] KVM: VMX: Change ple_window type to unsigned int

Quoted 24 lines · Peter Xu

The VMX ple_window is 32 bits wide, so logically it can overflow with an int. The module parameter is declared as unsigned int which is good, however the dynamic variable is not. Switching all the ple_window references to use unsigned int.

The tracepoint changes will also affect SVM, but SVM is using an even smaller width (16 bits) so it's always fine.

---
 arch/x86/kvm/trace.h   | 8 ++++----
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 arch/x86/kvm/vmx/vmx.h | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/trace.h +0 -0
index f1177e03768f..ae924566c401 100644
@@ -891,13 +891,13 @@ TRACE_EVENT(kvm_pml_full,
 );
 
 TRACE_EVENT(kvm_ple_window_update,

arch/x86/kvm/trace.h TRACE_EVENT(kvm_pml_full, · line 900

Quoted 10 lines
arch/x86/kvm/trace.h +3 -3
-	TP_PROTO(unsigned int vcpu_id, int new, int old),
+	TP_PROTO(unsigned int vcpu_id, unsigned int new, unsigned int old),
 	TP_ARGS(vcpu_id, new, old),
 
 	TP_STRUCT__entry(
 		__field(        unsigned int,   vcpu_id         )
-		__field(                 int,       new         )
-		__field(                 int,       old         )
+		__field(        unsigned int,       new         )
+		__field(        unsigned int,       old         )

Changing the trace event storage needs to be done in patch 3/4, otherwise we're knowingly introducing a bug (for one commit). Alternatively, swap the order of the patches.

Quoted 50 lines
arch/x86/kvm/trace.h +1 -1
 	),
 
 	TP_fast_assign(
@@ -906,7 +906,7 @@ TRACE_EVENT(kvm_ple_window_update,
 		__entry->old            = old;
 	),
 
-	TP_printk("vcpu %u old %d new %d (%s)",
+	TP_printk("vcpu %u old %u new %u (%s)",
 	          __entry->vcpu_id, __entry->old, __entry->new,
 		  __entry->old < __entry->new ? "growed" : "shrinked")
 );
arch/x86/kvm/vmx/vmx.c +2 -2
index 469c4134a4a7..1dbb63ffdd6d 100644
@@ -5227,7 +5227,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
 static void grow_ple_window(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	int old = vmx->ple_window;
+	unsigned int old = vmx->ple_window;
 
 	vmx->ple_window = __grow_ple_window(old, ple_window,
 					    ple_window_grow,
@@ -5243,7 +5243,7 @@ static void grow_ple_window(struct kvm_vcpu *vcpu)
 static void shrink_ple_window(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	int old = vmx->ple_window;
+	unsigned int old = vmx->ple_window;
 
 	vmx->ple_window = __shrink_ple_window(old, ple_window,
 					      ple_window_shrink,
arch/x86/kvm/vmx/vmx.h +1 -1
index 82d0bc3a4d52..64d5a4890aa9 100644
@@ -253,7 +253,7 @@ struct vcpu_vmx {
 	struct nested_vmx nested;
 
 	/* Dynamic PLE window. */
-	int ple_window;
+	unsigned int ple_window;
 	bool ple_window_dirty;
 
 	bool req_immediate_exit;
Signature · 1 line
2.21.0
author
#7 lore raw

Re: [PATCH v3 4/4] KVM: VMX: Change ple_window type to unsigned int

Quoted 29 lines · Sean Christopherson
Quoted 28 lines · Peter Xu

The VMX ple_window is 32 bits wide, so logically it can overflow with an int. The module parameter is declared as unsigned int which is good, however the dynamic variable is not. Switching all the ple_window references to use unsigned int.

The tracepoint changes will also affect SVM, but SVM is using an even smaller width (16 bits) so it's always fine.

---
 arch/x86/kvm/trace.h   | 8 ++++----
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 arch/x86/kvm/vmx/vmx.h | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/trace.h +1 -1
index f1177e03768f..ae924566c401 100644
@@ -891,13 +891,13 @@ TRACE_EVENT(kvm_pml_full,
 );
 
 TRACE_EVENT(kvm_ple_window_update,
-	TP_PROTO(unsigned int vcpu_id, int new, int old),
+	TP_PROTO(unsigned int vcpu_id, unsigned int new, unsigned int old),
 	TP_ARGS(vcpu_id, new, old),
 
Quoted 10 lines

arch/x86/kvm/trace.h TRACE_EVENT(kvm_pml_full, · line 900

Quoted 6 lines
arch/x86/kvm/trace.h +2 -2
 	TP_STRUCT__entry(
 		__field(        unsigned int,   vcpu_id         )
-		__field(                 int,       new         )
-		__field(                 int,       old         )
+		__field(        unsigned int,       new         )
+		__field(        unsigned int,       old         )

Changing the trace event storage needs to be done in patch 3/4, otherwise we're knowingly introducing a bug (for one commit). Alternatively, swap the order of the patches.

I'll swap. Thanks,

Signature · 1 line
Peter Xu