Skip to content

Commit 50011c2

Browse files
committed
KVM: VMX: Refresh available regs and IDT vectoring info before NMI handling
Reset the mask of available "registers" and refresh the IDT vectoring info snapshot in vmx_vcpu_enter_exit(), before KVM potentially handles a an NMI VM-Exit. One of the "registers" that KVM VMX lazily loads is the vmcs.VM_EXIT_INTR_INFO field, which is holds the vector+type on "exception or NMI" VM-Exits, i.e. is needed to identify NMIs. Clearing the available registers bitmask after handling NMIs results in KVM querying info from the last VM-Exit that read vmcs.VM_EXIT_INTR_INFO, and leads to both missed NMIs and spurious NMIs in the host. Opportunistically grab vmcs.IDT_VECTORING_INFO_FIELD early in the VM-Exit path too, e.g. to guard against similar consumption of stale data. The field is read on every "normal" VM-Exit, and there's no point in delaying the inevitable. Reported-by: Like Xu <like.xu.linux@gmail.com> Fixes: 11df586 ("KVM: VMX: Handle NMI VM-Exits in noinstr region") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20230825014532.2846714-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9ca0c1a commit 50011c2

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

arch/x86/kvm/vmx/vmx.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7209,13 +7209,20 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
72097209
flags);
72107210

72117211
vcpu->arch.cr2 = native_read_cr2();
7212+
vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET;
7213+
7214+
vmx->idt_vectoring_info = 0;
72127215

72137216
vmx_enable_fb_clear(vmx);
72147217

7215-
if (unlikely(vmx->fail))
7218+
if (unlikely(vmx->fail)) {
72167219
vmx->exit_reason.full = 0xdead;
7217-
else
7218-
vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON);
7220+
goto out;
7221+
}
7222+
7223+
vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON);
7224+
if (likely(!vmx->exit_reason.failed_vmentry))
7225+
vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
72197226

72207227
if ((u16)vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI &&
72217228
is_nmi(vmx_get_intr_info(vcpu))) {
@@ -7224,6 +7231,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
72247231
kvm_after_interrupt(vcpu);
72257232
}
72267233

7234+
out:
72277235
guest_state_exit_irqoff();
72287236
}
72297237

@@ -7345,8 +7353,6 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
73457353
loadsegment(es, __USER_DS);
73467354
#endif
73477355

7348-
vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET;
7349-
73507356
pt_guest_exit(vmx);
73517357

73527358
kvm_load_host_xsave_state(vcpu);
@@ -7363,17 +7369,12 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
73637369
vmx->nested.nested_run_pending = 0;
73647370
}
73657371

7366-
vmx->idt_vectoring_info = 0;
7367-
73687372
if (unlikely(vmx->fail))
73697373
return EXIT_FASTPATH_NONE;
73707374

73717375
if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY))
73727376
kvm_machine_check();
73737377

7374-
if (likely(!vmx->exit_reason.failed_vmentry))
7375-
vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7376-
73777378
trace_kvm_exit(vcpu, KVM_ISA_VMX);
73787379

73797380
if (unlikely(vmx->exit_reason.failed_vmentry))

0 commit comments

Comments
 (0)