[lkml]   [2021]   [Apr]   [27]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH 3/4] KVM/VMX: Invoke NMI non-IST entry instead of IST entry
From: Lai Jiangshan <>

In VMX, the NMI handler needs to be invoked after NMI VM-Exit.

Before the commit 1a5488ef0dcf6 ("KVM: VMX: Invoke NMI handler via
indirect call instead of INTn"), the work is done by INTn ("int $2").

But INTn microcode is relatively expensive, so the commit reworked
NMI VM-Exit handling to invoke the kernel handler by function call.
And INTn doesn't set the NMI blocked flag required by the linux kernel
NMI entry. So moving away from INTn are very reasonable.

Yet some details were missed. After the said commit applied, the NMI
entry pointer is fetched from the IDT table and called from the kernel
stack. But the NMI entry pointer installed on the IDT table is
asm_exc_nmi() which expects to be invoked on the IST stack by the ISA.
And it relies on the "NMI executing" variable on the IST stack to work
correctly. When it is unexpectedly called from the kernel stack, the
RSP-located "NMI executing" variable is also on the kernel stack and
is "uninitialized" and can cause the NMI entry to run in the wrong way.

So we should not used the NMI entry installed on the IDT table. Rather,
we should use the NMI entry allowed to be used on the kernel stack which
is asm_noist_exc_nmi() which is also used for XENPV and early booting.

Cc: Thomas Gleixner <>
Cc: Paolo Bonzini <>
Cc: Sean Christopherson <>
Cc: Steven Rostedt <>
Cc: Andi Kleen <>
Cc: Andy Lutomirski <>
Cc: Vitaly Kuznetsov <>
Cc: Wanpeng Li <>
Cc: Jim Mattson <>
Cc: Joerg Roedel <>
Cc: Josh Poimboeuf <>
Cc: Uros Bizjak <>
Cc: Maxim Levitsky <>
Signed-off-by: Lai Jiangshan <>
arch/x86/kernel/nmi.c | 3 +++
arch/x86/kvm/vmx/vmx.c | 8 ++++++--
2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
index 2fb1fd59d714..919f0400d931 100644
--- a/arch/x86/kernel/nmi.c
+++ b/arch/x86/kernel/nmi.c
@@ -528,10 +528,13 @@ DEFINE_IDTENTRY_RAW(noist_exc_nmi)
* On Xen PV and early booting stage, NMI doesn't use IST.
+ * And when it is manually called from VMX NMI VM-Exit handler,
+ * it doesn't use IST either.
* The C part is the same as native.

void stop_nmi(void)
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index bcbf0d2139e9..96e59d912637 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -36,6 +36,7 @@
#include <asm/debugreg.h>
#include <asm/desc.h>
#include <asm/fpu/internal.h>
+#include <asm/idtentry.h>
#include <asm/io.h>
#include <asm/irq_remapping.h>
#include <asm/kexec.h>
@@ -6416,8 +6417,11 @@ static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
else if (is_machine_check(intr_info))
/* We need to handle NMIs before interrupts are enabled */
- else if (is_nmi(intr_info))
- handle_interrupt_nmi_irqoff(&vmx->vcpu, intr_info);
+ else if (is_nmi(intr_info)) {
+ kvm_before_interrupt(&vmx->vcpu);
+ vmx_do_interrupt_nmi_irqoff((unsigned long)asm_noist_exc_nmi);
+ kvm_after_interrupt(&vmx->vcpu);
+ }

static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
 \ /
  Last update: 2021-04-27 10:56    [W:0.227 / U:0.172 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site