Message ID | 20200529153934.11694-20-pbonzini@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | KVM: nSVM: event fixes and migration support | expand |
On Fri, May 29, 2020 at 11:39:23AM -0400, Paolo Bonzini wrote: > Extract the code that is needed to implement CLGI and STGI, > so that we can run it from VMRUN and vmexit (and in the future, > KVM_SET_NESTED_STATE). Skip the request for KVM_REQ_EVENT unless needed, > subsuming the evaluate_pending_interrupts optimization that is found > in enter_svm_guest_mode. > > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> > --- > arch/x86/kvm/irq.c | 1 + > arch/x86/kvm/svm/nested.c | 22 ++--------------- > arch/x86/kvm/svm/svm.c | 51 ++++++++++++++++++++++++++------------- > arch/x86/kvm/svm/svm.h | 1 + > 4 files changed, 38 insertions(+), 37 deletions(-) [] > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -1981,6 +1981,38 @@ static int vmrun_interception(struct vcpu_svm *svm) > return nested_svm_vmrun(svm); > } > > +void svm_set_gif(struct vcpu_svm *svm, bool value) > +{ > + if (value) { > + /* > + * If VGIF is enabled, the STGI intercept is only added to > + * detect the opening of the SMI/NMI window; remove it now. > + * Likewise, clear the VINTR intercept, we will set it > + * again while processing KVM_REQ_EVENT if needed. > + */ > + if (vgif_enabled(svm)) > + clr_intercept(svm, INTERCEPT_STGI); > + if (is_intercept(svm, SVM_EXIT_VINTR)) A simple qemu-kvm will trigger the warning. (Looks like the patch had already been pulled into the mainline quickly.) # qemu-kvm -name ubuntu-18.04-server-cloudimg -cpu host -smp 2 -m 2G \ -hda ubuntu-18.04-server-cloudimg.qcow2 \ -cdrom ubuntu-18.04-server-cloudimg.iso -nic user,hostfwd=tcp::2222-:22 \ -nographic -device vfio-pci,host=0000:04:0a.0 [ 1362.284812][ T2195] ================================================================================ [ 1362.294789][ T2195] UBSAN: shift-out-of-bounds in arch/x86/kvm/svm/svm.h:316:47 [ 1362.302209][ T2195] shift exponent 100 is too large for 64-bit type 'long long unsigned int' [ 1362.310715][ T2195] CPU: 51 PID: 2195 Comm: qemu-kvm Not tainted 5.7.0-next-20200605 #6 [ 1362.319244][ T2195] Hardware name: HPE ProLiant DL385 Gen10/ProLiant DL385 Gen10, BIOS A40 03/09/2018 [ 1362.328530][ T2195] Call Trace: [ 1362.331710][ T2195] dump_stack+0xa7/0xea [ 1362.335758][ T2195] ubsan_epilogue+0x9/0x45 [ 1362.340070][ T2195] __ubsan_handle_shift_out_of_bounds.cold.13+0x14/0x98 [ 1362.347386][ T2195] ? __kasan_check_write+0x14/0x20 [ 1362.352405][ T2195] ? set_msr_interception+0x1b8/0x300 [kvm_amd] [ 1362.358558][ T2195] svm_set_gif.cold.64+0x16/0xd1 [kvm_amd] [ 1362.364276][ T2195] svm_set_efer+0xbc/0xc0 [kvm_amd] svm_set_efer at arch/x86/kvm/svm/svm.c:281 [ 1362.369866][ T2195] init_vmcb+0x107c/0x1b80 [kvm_amd] [ 1362.375056][ T2195] svm_create_vcpu+0x237/0x360 [kvm_amd] [ 1362.380677][ T2195] kvm_arch_vcpu_create+0x490/0x5f0 [kvm] [ 1362.386381][ T2195] kvm_vm_ioctl+0x10c5/0x17f0 [kvm] [ 1362.391561][ T2195] ? kvm_unregister_device_ops+0xd0/0xd0 [kvm] [ 1362.398118][ T2195] ? validate_chain+0xab/0x1b30 [ 1362.402864][ T2195] ? __kasan_check_read+0x11/0x20 [ 1362.407784][ T2195] ? check_chain_key+0x1df/0x2e0 [ 1362.412615][ T2195] ? __lock_acquire+0x74c/0xc10 [ 1362.417363][ T2195] ? match_held_lock+0x20/0x2f0 [ 1362.422593][ T2195] ? check_chain_key+0x1df/0x2e0 [ 1362.427425][ T2195] ? find_held_lock+0xca/0xf0 [ 1362.431997][ T2195] ? __fget_files+0x172/0x270 [ 1362.436565][ T2195] ? lock_downgrade+0x3e0/0x3e0 [ 1362.441310][ T2195] ? rcu_read_lock_held+0xac/0xc0 [ 1362.446744][ T2195] ? rcu_read_lock_sched_held+0xe0/0xe0 [ 1362.452190][ T2195] ? __fget_files+0x18c/0x270 [ 1362.456759][ T2195] ? __fget_light+0xf2/0x110 [ 1362.461242][ T2195] ksys_ioctl+0x26e/0xc60 [ 1362.465464][ T2195] ? generic_block_fiemap+0x70/0x70 [ 1362.471024][ T2195] ? find_held_lock+0xca/0xf0 [ 1362.475597][ T2195] ? __task_pid_nr_ns+0x145/0x290 [ 1362.480517][ T2195] ? check_flags.part.26+0x86/0x230 [ 1362.485613][ T2195] ? __kasan_check_read+0x11/0x20 [ 1362.490535][ T2195] ? entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 1362.497009][ T2195] ? do_syscall_64+0x23/0x340 [ 1362.501581][ T2195] ? rcu_read_lock_sched_held+0xac/0xe0 [ 1362.507023][ T2195] ? mark_held_locks+0x34/0xb0 [ 1362.511679][ T2195] ? do_syscall_64+0x29/0x340 [ 1362.516254][ T2195] __x64_sys_ioctl+0x43/0x4c [ 1362.521177][ T2195] do_syscall_64+0x64/0x340 [ 1362.525573][ T2195] entry_SYSCALL_64_after_hwframe+0x44/0xa9 [ 1362.531366][ T2195] RIP: 0033:0x7f8c609cb87b [ 1362.535676][ T2195] Code: Bad RIP value. [ 1362.539633][ T2195] RSP: 002b:00007f8c517fb6c8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 [ 1362.548399][ T2195] RAX: ffffffffffffffda RBX: 0000564c201f9110 RCX: 00007f8c609cb87b [ 1362.556287][ T2195] RDX: 0000000000000000 RSI: 000000000000ae41 RDI: 000000000000000a [ 1362.564178][ T2195] RBP: 0000564c201f9110 R08: 0000564c1e47cd50 R09: 0000564c201f9110 [ 1362.572455][ T2195] R10: 0000564c1ebfd680 R11: 0000000000000246 R12: 0000564c201954d0 [ 1362.580344][ T2195] R13: 00007ffe3a592d6f R14: 00007ffe3a592e00 R15: 00007f8c517fb840 [ 1362.588287][ T2195] ================================================================================ > + svm_clear_vintr(svm); > + > + enable_gif(svm); > + if (svm->vcpu.arch.smi_pending || > + svm->vcpu.arch.nmi_pending || > + kvm_cpu_has_injectable_intr(&svm->vcpu)) > + kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); > + } else { > + disable_gif(svm); > + > + /* > + * After a CLGI no interrupts should come. But if vGIF is > + * in use, we still rely on the VINTR intercept (rather than > + * STGI) to detect an open interrupt window. > + */ > + if (!vgif_enabled(svm)) > + svm_clear_vintr(svm); > + } > +} > +
On 05/06/20 22:33, Qian Cai wrote: >> + if (value) { >> + /* >> + * If VGIF is enabled, the STGI intercept is only added to >> + * detect the opening of the SMI/NMI window; remove it now. >> + * Likewise, clear the VINTR intercept, we will set it >> + * again while processing KVM_REQ_EVENT if needed. >> + */ >> + if (vgif_enabled(svm)) >> + clr_intercept(svm, INTERCEPT_STGI); >> + if (is_intercept(svm, SVM_EXIT_VINTR)) > A simple qemu-kvm will trigger the warning. (Looks like the patch had > already been pulled into the mainline quickly.) You're right, that should have been INTERCEPT_VINTR. I'll post a fix. Thanks, Paolo
diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 54f7ea68083b..99d118ffc67d 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -83,6 +83,7 @@ int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v) return kvm_apic_has_interrupt(v) != -1; /* LAPIC */ } +EXPORT_SYMBOL_GPL(kvm_cpu_has_injectable_intr); /* * check if there is pending interrupt without diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 921466eba556..7e4a506828c9 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -333,10 +333,6 @@ static void nested_prepare_vmcb_control(struct vcpu_svm *svm) void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, struct vmcb *nested_vmcb) { - bool evaluate_pending_interrupts = - is_intercept(svm, INTERCEPT_VINTR) || - is_intercept(svm, INTERCEPT_IRET); - svm->nested.vmcb = vmcb_gpa; if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) svm->vcpu.arch.hflags |= HF_HIF_MASK; @@ -347,21 +343,7 @@ void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, nested_prepare_vmcb_save(svm, nested_vmcb); nested_prepare_vmcb_control(svm); - /* - * If L1 had a pending IRQ/NMI before executing VMRUN, - * which wasn't delivered because it was disallowed (e.g. - * interrupts disabled), L0 needs to evaluate if this pending - * event should cause an exit from L2 to L1 or be delivered - * directly to L2. - * - * Usually this would be handled by the processor noticing an - * IRQ/NMI window request. However, VMRUN can unblock interrupts - * by implicitly setting GIF, so force L0 to perform pending event - * evaluation by requesting a KVM_REQ_EVENT. - */ - enable_gif(svm); - if (unlikely(evaluate_pending_interrupts)) - kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); + svm_set_gif(svm, true); } int nested_svm_vmrun(struct vcpu_svm *svm) @@ -505,7 +487,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) svm->vcpu.arch.mp_state = KVM_MP_STATE_RUNNABLE; /* Give the current vmcb to the guest */ - disable_gif(svm); + svm_set_gif(svm, false); nested_vmcb->save.es = vmcb->save.es; nested_vmcb->save.cs = vmcb->save.cs; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7383f821eb3b..e48e4173bc60 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1981,6 +1981,38 @@ static int vmrun_interception(struct vcpu_svm *svm) return nested_svm_vmrun(svm); } +void svm_set_gif(struct vcpu_svm *svm, bool value) +{ + if (value) { + /* + * If VGIF is enabled, the STGI intercept is only added to + * detect the opening of the SMI/NMI window; remove it now. + * Likewise, clear the VINTR intercept, we will set it + * again while processing KVM_REQ_EVENT if needed. + */ + if (vgif_enabled(svm)) + clr_intercept(svm, INTERCEPT_STGI); + if (is_intercept(svm, SVM_EXIT_VINTR)) + svm_clear_vintr(svm); + + enable_gif(svm); + if (svm->vcpu.arch.smi_pending || + svm->vcpu.arch.nmi_pending || + kvm_cpu_has_injectable_intr(&svm->vcpu)) + kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); + } else { + disable_gif(svm); + + /* + * After a CLGI no interrupts should come. But if vGIF is + * in use, we still rely on the VINTR intercept (rather than + * STGI) to detect an open interrupt window. + */ + if (!vgif_enabled(svm)) + svm_clear_vintr(svm); + } +} + static int stgi_interception(struct vcpu_svm *svm) { int ret; @@ -1988,18 +2020,8 @@ static int stgi_interception(struct vcpu_svm *svm) if (nested_svm_check_permissions(svm)) return 1; - /* - * If VGIF is enabled, the STGI intercept is only added to - * detect the opening of the SMI/NMI window; remove it now. - */ - if (vgif_enabled(svm)) - clr_intercept(svm, INTERCEPT_STGI); - ret = kvm_skip_emulated_instruction(&svm->vcpu); - kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); - - enable_gif(svm); - + svm_set_gif(svm, true); return ret; } @@ -2011,12 +2033,7 @@ static int clgi_interception(struct vcpu_svm *svm) return 1; ret = kvm_skip_emulated_instruction(&svm->vcpu); - - disable_gif(svm); - - /* After a CLGI no interrupts should come */ - svm_clear_vintr(svm); - + svm_set_gif(svm, false); return ret; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 7e79f0af1204..10b7b55720a0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -357,6 +357,7 @@ void disable_nmi_singlestep(struct vcpu_svm *svm); bool svm_smi_blocked(struct kvm_vcpu *vcpu); bool svm_nmi_blocked(struct kvm_vcpu *vcpu); bool svm_interrupt_blocked(struct kvm_vcpu *vcpu); +void svm_set_gif(struct vcpu_svm *svm, bool value); /* nested.c */
Extract the code that is needed to implement CLGI and STGI, so that we can run it from VMRUN and vmexit (and in the future, KVM_SET_NESTED_STATE). Skip the request for KVM_REQ_EVENT unless needed, subsuming the evaluate_pending_interrupts optimization that is found in enter_svm_guest_mode. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> --- arch/x86/kvm/irq.c | 1 + arch/x86/kvm/svm/nested.c | 22 ++--------------- arch/x86/kvm/svm/svm.c | 51 ++++++++++++++++++++++++++------------- arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 38 insertions(+), 37 deletions(-)