KVM: x86: Tracepoint improvements and fixes
v2 cover KVM: x86: Tracepoint improvements and fixes
Various improvements and fixes for the kvm_entry, kvm_exit and kvm_nested_vmexit tracepoints.
1. Capture the guest's RIP during kvm_entry for obvious reasons.
2. Extend kvm_exit to report the same info as kvm_nested_vmexit, and macrofy its definition to reuse it verbatim for nested exits.
3. Stop passing in params to kvm_nested_vmexit, and instead use the same approach (and now code) as kvm_exit where the tracepoint uses a dedicated kvm_x86_ops hook to retrieve the info.
4. Stop reading GUEST_RIP, EXIT_QUAL, INTR_INFO, and ERROR_CODE on every VM-Exit from L2 (some of this comes in #3). This saves ~100 cycles (150+ with retpolines) on VM-Exits from L2 that are handled by L0, e.g. hardware interrupts.
As noted by Vitaly, these changes break trace-cmd[*]. I hereby pinky swear that, if this series is merged, I will send patches to update trace-cmd.
[*] git://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git
v2:
- Fixed some goofs in the changelogs.
- Rebased to kvm/queue, commit e1ba1a15af73 ("KVM: SVM: Enable INVPCID feature on AMD").
Sean Christopherson (7):
KVM: x86: Add RIP to the kvm_entry, i.e. VM-Enter, tracepoint
KVM: x86: Read guest RIP from within the kvm_nested_vmexit tracepoint
KVM: VMX: Add a helper to test for a valid error code given an intr
info
KVM: x86: Add intr/vectoring info and error code to kvm_exit
tracepoint
KVM: x86: Add macro wrapper for defining kvm_exit tracepoint
KVM: x86: Use common definition for kvm_nested_vmexit tracepoint
KVM: nVMX: Read EXIT_QUAL and INTR_INFO only when needed for nested
exit
arch/x86/include/asm/kvm_host.h | 7 ++- arch/x86/kvm/svm/svm.c | 16 ++--- arch/x86/kvm/trace.h | 107 +++++++++++++------------------- arch/x86/kvm/vmx/nested.c | 14 ++--- arch/x86/kvm/vmx/vmcs.h | 7 +++ arch/x86/kvm/vmx/vmx.c | 18 +++++- arch/x86/kvm/x86.c | 2 +- 7 files changed, 86 insertions(+), 85 deletions(-)
Signature · 1 line
2.28.0
PATCH v2 1/7 KVM: x86: Add RIP to the kvm_entry, i.e. VM-Enter, tracepoint
Add RIP to the kvm_entry tracepoint to help debug if the kvm_exit tracepoint is disabled or if VM-Enter fails, in which case the kvm_exit tracepoint won't be hit.
Read RIP from within the tracepoint itself to avoid a potential VMREAD and retpoline if the guest's RIP isn't available.
--- arch/x86/kvm/trace.h | 10 ++++++---- arch/x86/kvm/x86.c | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/trace.h +6 -4
@@ -15,18 +15,20 @@* Tracepoint for guest mode entry. */ TRACE_EVENT (kvm_entry, - TP_PROTO(unsigned int vcpu_id), -TP_ARGS (vcpu_id), + TP_PROTO(struct kvm_vcpu *vcpu), +TP_ARGS (vcpu),TP_STRUCT__entry ( __field( unsigned int, vcpu_id ) + __field( unsigned long, rip ) ),TP_fast_assign ( - __entry->vcpu_id = vcpu_id; + __entry->vcpu_id = vcpu->vcpu_id; + __entry->rip = kvm_rip_read(vcpu); ), -TP_printk ("vcpu %u" , __entry->vcpu_id) +TP_printk ("vcpu %u, rip 0x%lx" , __entry->vcpu_id, __entry->rip) );/*
arch/x86/kvm/x86.c +1 -1
@@ -8546,7 +8546,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) kvm_x86_ops.request_immediate_exit (vcpu); } -trace_kvm_entry (vcpu->vcpu_id); +trace_kvm_entry (vcpu);fpregs_assert_state_consistent ();if (test_thread_flag (TIF_NEED_FPU_LOAD ))
Signature · 1 line
2.28.0
PATCH v2 3/7 KVM: VMX: Add a helper to test for a valid error code given an intr info
In reply to Sean ChristophersonAdd a helper, is_exception_with_error_code(), to provide the simple but difficult to read code of checking for a valid exception with an error code given a vmcs.VM_EXIT_INTR_INFO value. The helper will gain another user, vmx_get_exit_info(), in a future patch.
--- arch/x86/kvm/vmx/nested.c | 4 +--- arch/x86/kvm/vmx/vmcs.h | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -3
@@ -5940,9 +5940,7 @@ bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)* need to be synthesized by querying the in-kernel LAPIC, but external * interrupts are never reflected to L1 so it's a non-issue. */ -if ((exit_intr_info & - (INTR_INFO_VALID_MASK |INTR_INFO_DELIVER_CODE_MASK )) == - (INTR_INFO_VALID_MASK |INTR_INFO_DELIVER_CODE_MASK )) { +if (is_exception_with_error_code (exit_intr_info)) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); vmcs12->vm_exit_intr_error_code =
arch/x86/kvm/vmx/vmcs.h +7 -0
@@ -138,6 +138,13 @@ static inline bool is_external_intr(u32 intr_info)return is_intr_type (intr_info,INTR_TYPE_EXT_INTR ); } +static inline bool is_exception_with_error_code (u32 intr_info) +{ +const u32 mask =INTR_INFO_VALID_MASK |INTR_INFO_DELIVER_CODE_MASK ; + +return (intr_info & mask) == mask; +} +enum vmcs_field_width {VMCS_FIELD_WIDTH_U16 =0 ,VMCS_FIELD_WIDTH_U64 =1 ,
Signature · 1 line
2.28.0
PATCH v2 4/7 KVM: x86: Add intr/vectoring info and error code to kvm_exit tracepoint
In reply to Sean ChristophersonExtend the kvm_exit tracepoint to align it with kvm_nested_vmexit in terms of what information is captured. On SVM, add interrupt info and error code, while on VMX it add IDT ITD vectoring and error code. This sets the stage for macrofying the kvm_exit tracepoint definition so that it can be reused for kvm_nested_vmexit without loss of information.
Opportunistically stuff a zero for VM_EXIT_INTR_INFO if the VM-Enter failed, as the field is guaranteed to be invalid. Note, it'd be possible to further filter the interrupt/exception fields based on the VM-Exit reason, but the helper is intended only for tracepoints, i.e. an extra VMREAD or two is a non-issue, the failed VM-Enter case is just low hanging fruit.
--- arch/x86/include/asm/kvm_host.h | 7 ++++++- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/trace.h | 12 +++++++++--- arch/x86/kvm/vmx/vmx.c | 18 ++++++++++++++++-- 4 files changed, 39 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -1
@@ -1143,7 +1143,12 @@ struct kvm_x86_ops {/* Returns actual tsc_offset set in active VMCS */ u64 (*write_l1_tsc_offset)(struct kvm_vcpu *vcpu,u64 offset); -void (*get_exit_info)(struct kvm_vcpu *vcpu,u64 *info1,u64 *info2); +/* +* Retrieve somewhat arbitrary exit information. Intended to be used +* only from within tracepoints to avoid VMREADs when tracing is off. +*/ +void (*get_exit_info)(struct kvm_vcpu *vcpu,u64 *info1,u64 *info2, +u32 *exit_int_info,u32 *exit_int_info_err_code); int (*check_intercept)(struct kvm_vcpu *vcpu, struct x86_instruction_info *info,
arch/x86/kvm/svm/svm.c +8 -1
@@ -2963,12 +2963,19 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) "excp_to:", save->last_excp_to); } -static void svm_get_exit_info (struct kvm_vcpu *vcpu,u64 *info1,u64 *info2) +static void svm_get_exit_info (struct kvm_vcpu *vcpu,u64 *info1,u64 *info2, +u32 *intr_info,u32 *error_code) {struct vmcb_control_area *control = &to_svm (vcpu)->vmcb->control; *info1 = control->exit_info_1; *info2 = control->exit_info_2; + *intr_info = control->exit_int_info; +if ((*intr_info &SVM_EXITINTINFO_VALID ) && + (*intr_info &SVM_EXITINTINFO_VALID_ERR )) + *error_code = control->exit_int_info_err; +else + *error_code =0 ; }static int handle_exit (struct kvm_vcpu *vcpu,fastpath_t exit_fastpath)
arch/x86/kvm/trace.h +9 -3
@@ -248,6 +248,8 @@ TRACE_EVENT(kvm_exit, __field( u32, isa ) __field( u64, info1 ) __field( u64, info2 ) + __field( u32, intr_info ) + __field( u32, error_code ) __field( unsigned int, vcpu_id ) ),
@@ -257,13 +259,17 @@ TRACE_EVENT(kvm_exit, __entry->isa = isa; __entry->vcpu_id = vcpu->vcpu_id; kvm_x86_ops.get_exit_info (vcpu, &__entry->info1, - &__entry->info2); + &__entry->info2, + &__entry->intr_info, + &__entry->error_code); ), -TP_printk ("vcpu %u reason %s%s%s rip 0x%lx info %llx %llx" , +TP_printk ("vcpu %u reason %s%s%s rip 0x%lx info1 0x%016llx " +"info2 0x%016llx intr_info 0x%08x error_code 0x%08x" , __entry->vcpu_id,kvm_print_exit_reason (__entry->exit_reason, __entry->isa), - __entry->guest_rip, __entry->info1, __entry->info2) + __entry->guest_rip, __entry->info1, __entry->info2, + __entry->intr_info, __entry->error_code) );/*
arch/x86/kvm/vmx/vmx.c +16 -2
@@ -5653,10 +5653,24 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {static const int kvm_vmx_max_exit_handlers =ARRAY_SIZE (kvm_vmx_exit_handlers); -static void vmx_get_exit_info (struct kvm_vcpu *vcpu,u64 *info1,u64 *info2) +static void vmx_get_exit_info (struct kvm_vcpu *vcpu,u64 *info1,u64 *info2, +u32 *intr_info,u32 *error_code) { +struct vcpu_vmx *vmx =to_vmx (vcpu); + *info1 =vmx_get_exit_qual (vcpu); - *info2 =vmx_get_intr_info (vcpu); +if (!(vmx->exit_reason &VMX_EXIT_REASONS_FAILED_VMENTRY )) { + *info2 = vmx->idt_vectoring_info; + *intr_info =vmx_get_intr_info (vcpu); +if (is_exception_with_error_code (*intr_info)) + *error_code =vmcs_read32 (VM_EXIT_INTR_ERROR_CODE ); +else + *error_code =0 ; + }else { + *info2 =0 ; + *intr_info =0 ; + *error_code =0 ; + } }static void vmx_destroy_pml_buffer (struct vcpu_vmx *vmx)
Signature · 1 line
2.28.0
PATCH v2 7/7 KVM: nVMX: Read EXIT_QUAL and INTR_INFO only when needed for nested exit
In reply to Sean ChristophersonRead vmcs.EXIT_QUALIFICATION and vmcs.VM_EXIT_INTR_INFO only if the VM-Exit is being reflected to L1 now that they are no longer passed directly to the kvm_nested_vmexit tracepoint.
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +2 -3
@@ -5918,9 +5918,6 @@ bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) goto reflect_vmexit; } - exit_intr_info =vmx_get_intr_info (vcpu); - exit_qual =vmx_get_exit_qual (vcpu); -trace_kvm_nested_vmexit (exit_reason, vcpu,KVM_ISA_VMX );/* If L0 (KVM) wants the exit, it trumps L1's desires. */
@@ -5937,12 +5934,14 @@ bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)* need to be synthesized by querying the in-kernel LAPIC, but external * interrupts are never reflected to L1 so it's a non-issue. */ + exit_intr_info =vmx_get_intr_info (vcpu);if (is_exception_with_error_code (exit_intr_info)) {struct vmcs12 *vmcs12 =get_vmcs12 (vcpu); vmcs12->vm_exit_intr_error_code =vmcs_read32 (VM_EXIT_INTR_ERROR_CODE ); } + exit_qual =vmx_get_exit_qual (vcpu); reflect_vmexit:nested_vmx_vmexit (vcpu, exit_reason, exit_intr_info, exit_qual);
Signature · 1 line
2.28.0
Paolo Bonzini
maintainerQuoted 5 lines · Sean Christopherson
As noted by Vitaly, these changes break trace-cmd[*]. I hereby pinky swear that, if this series is merged, I will send patches to update trace-cmd.
[*]
git://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git
Actually I think we should just retire the kvm plugin of libtraceevent.
I do use the plugin myself, but really only because it's enabled by
default. There's very little that the plugin does better than the tracepoints and could not be fixed in Linux.
The role in particular is printed better by the plugin, some fields are missing in KVM_MMU_PAGE_PRINTK(). The only real functionality would be the disassembling of emulated instructions.
Paolo
Quoted 17 lines
v2:
- Fixed some goofs in the changelogs.
- Rebased to kvm/queue, commite1ba1a15af73("KVM: SVM: Enable INVPCID feature on AMD").Sean Christopherson (7):
KVM: x86: Add RIP to thekvm_entry, i.e. VM-Enter, tracepoint
KVM: x86: Read guest RIP from within thekvm_nested_vmexittracepoint
KVM: VMX: Add a helper to test for a valid error code given an intr
info
KVM: x86: Add intr/vectoring info and error code tokvm_exit
tracepoint
KVM: x86: Add macro wrapper for definingkvm_exittracepoint
KVM: x86: Use common definition forkvm_nested_vmexittracepoint
KVM: nVMX: ReadEXIT_QUALandINTR_INFOonly when needed for nested
exit
Quoted 9 lines
arch/x86/include/asm/kvm_host.h | 7 ++- arch/x86/kvm/svm/svm.c | 16 ++--- arch/x86/kvm/trace.h | 107 +++++++++++++------------------- arch/x86/kvm/vmx/nested.c | 14 ++--- arch/x86/kvm/vmx/vmcs.h | 7 +++ arch/x86/kvm/vmx/vmx.c | 18 +++++- arch/x86/kvm/x86.c | 2 +- 7 files changed, 86 insertions(+), 85 deletions(-)
Nice. Queued, thanks.
Paolo