Skip to contentSkip to conversation

x86/traps,VMX: implement software based NMI/IRQ dispatch for VMX NMI/IRQ reinjection

PATCH Superseded Xin Li 5 participants 18 messages Nov 10, 2022 – Nov 14, 2022

Subsystems (6)
lore
Share & tools
lore

Xin Li

author
#0 lore raw

cover x86/traps,VMX: implement software based NMI/IRQ dispatch for VMX NMI/IRQ reinjection

Upon receiving an external interrupt, KVM VMX reinjects it through calling the interrupt handler in its IDT descriptor on the current kernel stack, which essentially uses the IDT as an interrupt dispatch table.

However the IDT is one of the lowest level critical data structures between a x86 CPU and the Linux kernel, we should avoid using it *directly* whenever possible, espeically in a software defined manner.

On x86, external interrupts are divided into the following groups
1) system interrupts
2) external device interrupts
With the IDT, system interrupts are dispatched through the IDT directly, while external device interrupts are all routed to the external interrupt dispatch function common_interrupt(), which dispatches external device interrupts through a per-CPU external interrupt dispatch table vector_irq.

Implement software based NMI/IRQ dispatch for VMX NMI/IRQ reinjection to eliminate dispatching external interrupts through the IDT with adding a system interrupt handler table for dispatching a system interrupt to its corresponding handler directly. Thus a software based dispatch function will be:

  void external_interrupt(struct pt_regs *regs, u8 vector)
  {
    if (is_system_interrupt(vector))
      system_interrupt_handler_table[vector_to_sysvec(vector)](regs);
    else /* external device interrupt */
      common_interrupt(regs, vector);
  }

And the software dispatch approach nukes a bunch of assembly.

What's more, with the Intel FRED (Flexible Return and Event Delivery) architecture, IDT, the hardware based event dispatch table, is gone, and the Linux kernel needs to dispatch events to their handlers with vector to handler mappings, the dispatch function external_interrupt() is also needed.

H. Peter Anvin (Intel) (1):
x86/traps: let common_interrupt() handle IRQ_MOVE_CLEANUP_VECTOR

Xin Li (5):
x86/traps: add a system interrupt table for system interrupt dispatch x86/traps: add install_system_interrupt_handler() x86/traps: add external_interrupt() to dispatch external interrupts
KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection x86/traps: remove unused NMI entry exc_nmi_noist()

 arch/x86/include/asm/idtentry.h  |  15 -----
 arch/x86/include/asm/traps.h     |  12 ++++
 arch/x86/kernel/cpu/acrn.c       |   7 +-
 arch/x86/kernel/cpu/mshyperv.c   |  22 ++++---
 arch/x86/kernel/irq.c            |   4 ++
 arch/x86/kernel/kvm.c            |   4 +-
 arch/x86/kernel/nmi.c            |  10 ---
 arch/x86/kernel/traps.c          | 107 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmenter.S       |  33 ----------
 arch/x86/kvm/vmx/vmx.c           |  19 ++----
 drivers/xen/events/events_base.c |   5 +-
 11 files changed, 156 insertions(+), 82 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/6 x86/traps: let common_interrupt() handle IRQ_MOVE_CLEANUP_VECTOR

From: "H. Peter Anvin (Intel)" <[email protected]>

IRQ_MOVE_CLEANUP_VECTOR is the only one of the system IRQ vectors that is *below* FIRST_SYSTEM_VECTOR. It is a slow path, so just push it into common_interrupt() just before the spurios interrupt handling.

---
 arch/x86/kernel/irq.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/irq.c +4 -0
index 766ffe3ba313..7e125fff45ab 100644
@@ -248,6 +248,10 @@ DEFINE_IDTENTRY_IRQ(common_interrupt)
 	desc = __this_cpu_read(vector_irq[vector]);
 	if (likely(!IS_ERR_OR_NULL(desc))) {
 		handle_irq(desc, regs);
+#ifdef CONFIG_SMP
+	} else if (vector == IRQ_MOVE_CLEANUP_VECTOR) {
+		sysvec_irq_move_cleanup(regs);
+#endif
 	} else {
 		ack_APIC_irq();
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/6 x86/traps: add a system interrupt table for system interrupt dispatch

In reply to Xin Li

Upon receiving an external interrupt, KVM VMX reinjects it through calling the interrupt handler in its IDT descriptor on the current kernel stack, which essentially uses the IDT as an interrupt dispatch table.

However the IDT is one of the lowest level critical data structures between a x86 CPU and the Linux kernel, we should avoid using it *directly* whenever possible, espeically in a software defined manner.

On x86, external interrupts are divided into the following groups
1) system interrupts
2) external device interrupts
With the IDT, system interrupts are dispatched through the IDT directly, while external device interrupts are all routed to the external interrupt dispatch function common_interrupt(), which dispatches external device interrupts through a per-CPU external interrupt dispatch table vector_irq.

To eliminate dispatching external interrupts through the IDT, add a system interrupt handler table for dispatching a system interrupt to its corresponding handler directly. Thus a software based dispatch function will be:

  void external_interrupt(struct pt_regs *regs, u8 vector)
  {
    if (is_system_interrupt(vector))
      system_interrupt_handler_table[vector_to_sysvec(vector)](regs);
    else /* external device interrupt */
      common_interrupt(regs, vector);
  }

What's more, with the Intel FRED (Flexible Return and Event Delivery) architecture, IDT, the hardware based event dispatch table, is gone, and the Linux kernel needs to dispatch events to their handlers with vector to handler mappings, the dispatch function external_interrupt() is also needed.

---
 arch/x86/include/asm/traps.h |  8 ++++++
 arch/x86/kernel/traps.c      | 55 ++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
arch/x86/include/asm/traps.h +8 -0
index 47ecfff2c83d..3dc63d753bda 100644
@@ -47,4 +47,12 @@ void __noreturn handle_stack_overflow(struct pt_regs *regs,
 				      struct stack_info *info);
 #endif
 
+/*
+ * How system interrupt handlers are called.
+ */
+#define DECLARE_SYSTEM_INTERRUPT_HANDLER(f)			\
+	void f (struct pt_regs *regs __maybe_unused,		\
+		unsigned long vector __maybe_unused)
+typedef DECLARE_SYSTEM_INTERRUPT_HANDLER((*system_interrupt_handler));
+
 #endif /* _ASM_X86_TRAPS_H */
arch/x86/kernel/traps.c +55 -0

61 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/6 x86/traps: add install_system_interrupt_handler()

In reply to Xin Li

Some kernel components install system interrupt handlers into the IDT, and we need to do the same for system_interrupt_handler_table. A new function install_system_interrupt_handler() is added to install a system interrupt handler into both the IDT and system_interrupt_handler_table.

---
 arch/x86/include/asm/traps.h     |  2 ++
 arch/x86/kernel/cpu/acrn.c       |  7 +++++--
 arch/x86/kernel/cpu/mshyperv.c   | 22 ++++++++++++++--------
 arch/x86/kernel/kvm.c            |  4 +++-
 arch/x86/kernel/traps.c          | 10 +++++++++-
 drivers/xen/events/events_base.c |  5 ++++-
 6 files changed, 37 insertions(+), 13 deletions(-)
arch/x86/include/asm/traps.h +2 -0
index 3dc63d753bda..89c4233e19db 100644
@@ -41,6 +41,8 @@ void math_emulate(struct math_emu_info *);
 
 bool fault_in_kernel_space(unsigned long address);
 
+void install_system_interrupt_handler(unsigned int n, const void *asm_addr, const void *addr);
+
 #ifdef CONFIG_VMAP_STACK
 void __noreturn handle_stack_overflow(struct pt_regs *regs,
 				      unsigned long fault_address,
arch/x86/kernel/cpu/acrn.c +5 -2
index 485441b7f030..9351bf183a9e 100644
@@ -18,6 +18,7 @@
 #include <asm/hypervisor.h>
 #include <asm/idtentry.h>
 #include <asm/irq_regs.h>
+#include <asm/traps.h>
 
 static u32 __init acrn_detect(void)
 {
@@ -26,8 +27,10 @@ static u32 __init acrn_detect(void)
 
 static void __init acrn_init_platform(void)
 {
-	/* Setup the IDT for ACRN hypervisor callback */
-	alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_acrn_hv_callback);
+	/* Install system interrupt handler for ACRN hypervisor callback */
+	install_system_interrupt_handler(HYPERVISOR_CALLBACK_VECTOR,
+					 asm_sysvec_acrn_hv_callback,
+					 sysvec_acrn_hv_callback);
 
 	x86_platform.calibrate_tsc = acrn_get_tsc_khz;
 	x86_platform.calibrate_cpu = acrn_get_tsc_khz;
arch/x86/kernel/cpu/mshyperv.c +14 -8
index 831613959a92..144b4a622188 100644
@@ -29,6 +29,7 @@
 #include <asm/i8259.h>
 #include <asm/apic.h>
 #include <asm/timer.h>
+#include <asm/traps.h>
 #include <asm/reboot.h>
 #include <asm/nmi.h>
 #include <clocksource/hyperv_timer.h>
@@ -415,19 +416,24 @@ static void __init ms_hyperv_init_platform(void)
 	 */
 	x86_platform.apic_post_init = hyperv_init;
 	hyperv_setup_mmu_ops();
-	/* Setup the IDT for hypervisor callback */
-	alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_hyperv_callback);
 
-	/* Setup the IDT for reenlightenment notifications */
+	/* Install system interrupt handler for hypervisor callback */
+	install_system_interrupt_handler(HYPERVISOR_CALLBACK_VECTOR,
+					 asm_sysvec_hyperv_callback,
+					 sysvec_hyperv_callback);
+
+	/* Install system interrupt handler for reenlightenment notifications */
 	if (ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT) {
-		alloc_intr_gate(HYPERV_REENLIGHTENMENT_VECTOR,
-				asm_sysvec_hyperv_reenlightenment);
+		install_system_interrupt_handler(HYPERV_REENLIGHTENMENT_VECTOR,
+						 asm_sysvec_hyperv_reenlightenment,
+						 sysvec_hyperv_reenlightenment);
 	}
 
-	/* Setup the IDT for stimer0 */
+	/* Install system interrupt handler for stimer0 */
 	if (ms_hyperv.misc_features & HV_STIMER_DIRECT_MODE_AVAILABLE) {
-		alloc_intr_gate(HYPERV_STIMER0_VECTOR,
-				asm_sysvec_hyperv_stimer0);
+		install_system_interrupt_handler(HYPERV_STIMER0_VECTOR,
+						 asm_sysvec_hyperv_stimer0,
+						 sysvec_hyperv_stimer0);
 	}
 
 # ifdef CONFIG_SMP
arch/x86/kernel/kvm.c +3 -1
index d4e48b4a438b..b7388ed2a980 100644
@@ -835,7 +835,9 @@ static void __init kvm_guest_init(void)
 
 	if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
 		static_branch_enable(&kvm_async_pf_enabled);
-		alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt);
+		install_system_interrupt_handler(HYPERVISOR_CALLBACK_VECTOR,
+						 asm_sysvec_kvm_asyncpf_interrupt,
+						 sysvec_kvm_asyncpf_interrupt);
 	}
 
 #ifdef CONFIG_SMP
arch/x86/kernel/traps.c +9 -1
index 95dd917ef9ad..9c7826e588bc 100644
@@ -1458,7 +1458,7 @@ DEFINE_IDTENTRY_SW(iret_error)
  * of the arguments provided by the full system_interrupt_handler prototype,
  * the function type cast is safe here.
  */
-const system_interrupt_handler system_interrupt_handler_table[NR_SYSTEM_VECTORS] = {
+system_interrupt_handler system_interrupt_handler_table[NR_SYSTEM_VECTORS] = {
 	[0 ... NR_SYSTEM_VECTORS-1]		= spurious_interrupt,
 #ifdef CONFIG_SMP
 	SYSV(RESCHEDULE_VECTOR,			sysvec_reschedule_ipi),
@@ -1499,6 +1499,14 @@ const system_interrupt_handler system_interrupt_handler_table[NR_SYSTEM_VECTORS]
 
 #undef SYSV
 
+void __init install_system_interrupt_handler(unsigned int n, const void *asm_addr, const void *addr)
+{
+	BUG_ON(n < FIRST_SYSTEM_VECTOR);
+
+	system_interrupt_handler_table[n - FIRST_SYSTEM_VECTOR] = (system_interrupt_handler)addr;
+	alloc_intr_gate(n, asm_addr);
+}
+
 void __init trap_init(void)
 {
 	/* Init cpu_entry_area before IST entries are set up */
drivers/xen/events/events_base.c +4 -1
index c443f04aaad7..1a9eaf417acc 100644
@@ -45,6 +45,7 @@
 #include <asm/irq.h>
 #include <asm/io_apic.h>
 #include <asm/i8259.h>
+#include <asm/traps.h>
 #include <asm/xen/cpuid.h>
 #include <asm/xen/pci.h>
 #endif
@@ -2246,7 +2247,9 @@ static __init void xen_alloc_callback_vector(void)
 		return;
 
 	pr_info("Xen HVM callback vector for event delivery is enabled\n");
-	alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_xen_hvm_callback);
+	install_system_interrupt_handler(HYPERVISOR_CALLBACK_VECTOR,
+					 asm_sysvec_xen_hvm_callback,
+					 sysvec_xen_hvm_callback);
 }
 #else
 void xen_setup_callback_vector(void) {}
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/6 x86/traps: add external_interrupt() to dispatch external interrupts

In reply to Xin Li

Add external_interrupt() to dispatch external interrupts to their handlers. If an external interrupt is a system interrupt, dipatch it through system_interrupt_handler_table, otherwise call into common_interrupt().

---
 arch/x86/kernel/traps.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/x86/kernel/traps.c +21 -0
index 9c7826e588bc..c1eb3bd335ce 100644
@@ -1507,6 +1507,27 @@ void __init install_system_interrupt_handler(unsigned int n, const void *asm_add
 	alloc_intr_gate(n, asm_addr);
 }
 
+/*
+ * External interrupt dispatch function.
+ *
+ * Until/unless common_interrupt() can be taught to deal with the
+ * special system vectors, split the dispatch.
+ *
+ * Note: common_interrupt() already deals with IRQ_MOVE_CLEANUP_VECTOR.
+ */
+__visible noinstr void external_interrupt(struct pt_regs *regs,
+					  unsigned int vector)
+{
+	unsigned int sysvec = vector - FIRST_SYSTEM_VECTOR;
+
+	BUG_ON(vector < FIRST_EXTERNAL_VECTOR);
+
+	if (sysvec < NR_SYSTEM_VECTORS)
+		return system_interrupt_handler_table[sysvec](regs, vector);
+
+	common_interrupt(regs, vector);
+}
+
 void __init trap_init(void)
 {
 	/* Init cpu_entry_area before IST entries are set up */
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 5/6 KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

In reply to Xin Li

To eliminate dispatching NMI/IRQ through the IDT, add kvm_vmx_reinject_nmi_irq(), which calls external_interrupt() for IRQ reinjection.

Lastly replace calling a NMI/IRQ handler in an IDT descriptor with calling kvm_vmx_reinject_nmi_irq().

---
 arch/x86/include/asm/traps.h |  2 ++
 arch/x86/kernel/traps.c      | 23 +++++++++++++++++++++++
 arch/x86/kvm/vmx/vmenter.S   | 33 ---------------------------------
 arch/x86/kvm/vmx/vmx.c       | 19 +++++++------------
 4 files changed, 32 insertions(+), 45 deletions(-)
arch/x86/include/asm/traps.h +2 -0
index 89c4233e19db..4c56a8d31762 100644
@@ -57,4 +57,6 @@ void __noreturn handle_stack_overflow(struct pt_regs *regs,
 		unsigned long vector __maybe_unused)
 typedef DECLARE_SYSTEM_INTERRUPT_HANDLER((*system_interrupt_handler));
 
+void kvm_vmx_reinject_nmi_irq(u32 vector);
+
 #endif /* _ASM_X86_TRAPS_H */
arch/x86/kernel/traps.c +23 -0
index c1eb3bd335ce..9abf91534b13 100644
@@ -1528,6 +1528,29 @@ __visible noinstr void external_interrupt(struct pt_regs *regs,
 	common_interrupt(regs, vector);
 }
 
+#if IS_ENABLED(CONFIG_KVM_INTEL)
+/*
+ * KVM VMX reinjects NMI/IRQ on its current stack, it's a sync
+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers, except cs.RPL and flags.IF, which
+ * are both always 0 in the VMX NMI/IRQ reinjection context. Thus
+ * we simply allocate a zeroed pt_regs structure on current stack
+ * to call external_interrupt().
+ */
+void kvm_vmx_reinject_nmi_irq(u32 vector)
+{
+	struct pt_regs irq_regs;
+
+	memset(&irq_regs, 0, sizeof(irq_regs));
+
+	if (vector == NMI_VECTOR)
+		return exc_nmi(&irq_regs);
+
+	external_interrupt(&irq_regs, vector);
+}
+EXPORT_SYMBOL_GPL(kvm_vmx_reinject_nmi_irq);
+#endif
+
 void __init trap_init(void)
 {
 	/* Init cpu_entry_area before IST entries are set up */
arch/x86/kvm/vmx/vmenter.S +0 -33
index 8477d8bdd69c..0c1608b329cd 100644
@@ -317,36 +317,3 @@ SYM_FUNC_START(vmread_error_trampoline)
 
 	RET
 SYM_FUNC_END(vmread_error_trampoline)
-
-SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff)
-	/*
-	 * Unconditionally create a stack frame, getting the correct RSP on the
-	 * stack (for x86-64) would take two instructions anyways, and RBP can
-	 * be used to restore RSP to make objtool happy (see below).
-	 */
-	push %_ASM_BP
-	mov %_ASM_SP, %_ASM_BP
-
-#ifdef CONFIG_X86_64
-	/*
-	 * Align RSP to a 16-byte boundary (to emulate CPU behavior) before
-	 * creating the synthetic interrupt stack frame for the IRQ/NMI.
-	 */
-	and  $-16, %rsp
-	push $__KERNEL_DS
-	push %rbp
-#endif
-	pushf
-	push $__KERNEL_CS
-	CALL_NOSPEC _ASM_ARG1
-
-	/*
-	 * "Restore" RSP from RBP, even though IRET has already unwound RSP to
-	 * the correct value.  objtool doesn't know the callee will IRET and,
-	 * without the explicit restore, thinks the stack is getting walloped.
-	 * Using an unwind hint is problematic due to x86-64's dynamic alignment.
-	 */
-	mov %_ASM_BP, %_ASM_SP
-	pop %_ASM_BP
-	RET
-SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff)
arch/x86/kvm/vmx/vmx.c +7 -12
index 63247c57c72c..b457e4888468 100644
@@ -46,6 +46,7 @@
 #include <asm/mshyperv.h>
 #include <asm/mwait.h>
 #include <asm/spec-ctrl.h>
+#include <asm/traps.h>
 #include <asm/virtext.h>
 #include <asm/vmx.h>
 
@@ -6758,15 +6759,11 @@ static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
 	memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
 }
 
-void vmx_do_interrupt_nmi_irqoff(unsigned long entry);
-
-static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu,
-					unsigned long entry)
+static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32 vector)
 {
-	bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist;
-
-	kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI : KVM_HANDLING_IRQ);
-	vmx_do_interrupt_nmi_irqoff(entry);
+	kvm_before_interrupt(vcpu, vector == NMI_VECTOR ?
+				   KVM_HANDLING_NMI : KVM_HANDLING_IRQ);
+	kvm_vmx_reinject_nmi_irq(vector);
 	kvm_after_interrupt(vcpu);
 }
 
@@ -6792,7 +6789,6 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu)
 
 static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
 {
-	const unsigned long nmi_entry = (unsigned long)asm_exc_nmi_noist;
 	u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
 
 	/* if exit due to PF check for async PF */
@@ -6806,20 +6802,19 @@ static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
 		kvm_machine_check();
 	/* We need to handle NMIs before interrupts are enabled */
 	else if (is_nmi(intr_info))
-		handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry);
+		handle_interrupt_nmi_irqoff(&vmx->vcpu, NMI_VECTOR);
 }
 
 static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
 {
 	u32 intr_info = vmx_get_intr_info(vcpu);
 	unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK;
-	gate_desc *desc = (gate_desc *)host_idt_base + vector;
 
 	if (KVM_BUG(!is_external_intr(intr_info), vcpu->kvm,
 	    "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
 		return;
 
-	handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc));
+	handle_interrupt_nmi_irqoff(vcpu, vector);
 	vcpu->arch.at_instruction_boundary = true;
 }
 
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

arch/x86/kernel/traps.c __visible noinstr void external_interrupt(struct pt_regs *regs, · line 1533

Quoted 3 lines · Xin Li
arch/x86/kernel/traps.c +3 -0
+#if IS_ENABLED(CONFIG_KVM_INTEL)
+/*
+ * KVM VMX reinjects NMI/IRQ on its current stack, it's a sync

Please use a verb other than "reinject". There is no event injection of any kind, KVM is simply making a function call. KVM already uses "inject" and "reinject" for KVM where KVM is is literally injecting events into the guest.

The "kvm_vmx" part is also weird IMO. The function is in x86's traps/exceptions namespace, not the KVM VMX namespace.

Maybe exc_raise_nmi_or_irq()?

Quoted 2 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Won't this break stack traces to some extent?

arch/x86/kvm/vmx/vmx.c static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) · line 6766

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +4 -4
+static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32 vector)
 {
-	bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist;
-
-	kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI : KVM_HANDLING_IRQ);
-	vmx_do_interrupt_nmi_irqoff(entry);
+	kvm_before_interrupt(vcpu, vector == NMI_VECTOR ?
+				   KVM_HANDLING_NMI : KVM_HANDLING_IRQ);
+	kvm_vmx_reinject_nmi_irq(vector);

This is where I strongly object to kvm_vmx_reinject_nmi_irq(). This looks like KVM is reinjecting the event into the guest, which is all kinds of confusing.

Quoted 2 lines

kvm_after_interrupt(vcpu);
}

author
#7 lore raw

RE: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 10 lines

arch/x86/kernel/traps.c __visible noinstr void external_interrupt(struct pt_regs *regs, · line 1533

Quoted 3 lines
arch/x86/kernel/traps.c +3 -0
+#if IS_ENABLED(CONFIG_KVM_INTEL)
+/*
+ * KVM VMX reinjects NMI/IRQ on its current stack, it's a sync

Please use a verb other than "reinject". There is no event injection of any kind, KVM is simply making a function call. KVM already uses "inject" and "reinject" for KVM where KVM is is literally injecting events into the guest.

The "kvm_vmx" part is also weird IMO. The function is in x86's traps/exceptions namespace, not the KVM VMX namespace.

right, "kvm_vmx" doesn't look good per your explanation.

Quoted 2 lines

Maybe exc_raise_nmi_or_irq()?

It's good for me.

Quoted 6 lines
Quoted 3 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Won't this break stack traces to some extent?

The pt_regs structure, and its IP/CS, is NOT part of the call stack, thus I don't see a problem. No?

Quoted 18 lines

arch/x86/kvm/vmx/vmx.c static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2 -3
+static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32
+vector)
 {
-	bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist;
-
-	kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI :

KVM_HANDLING_IRQ);

arch/x86/kvm/vmx/vmx.c static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)

Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +2 -1
-	vmx_do_interrupt_nmi_irqoff(entry);
+	kvm_before_interrupt(vcpu, vector == NMI_VECTOR ?
+				   KVM_HANDLING_NMI :

KVM_HANDLING_IRQ);

arch/x86/kvm/vmx/vmx.c static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) · line 6766

Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1 -0
+	kvm_vmx_reinject_nmi_irq(vector);

This is where I strongly object to kvm_vmx_reinject_nmi_irq(). This looks like KVM is reinjecting the event into the guest, which is all kinds of confusing.

Quoted 2 lines

kvm_after_interrupt(vcpu);
}

#8 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 19 lines · On Thu, Nov 10, 2022, Li, Xin3 wrote:
Quoted 10 lines

arch/x86/kernel/traps.c __visible noinstr void external_interrupt(struct pt_regs *regs, · line 1533

Quoted 3 lines
arch/x86/kernel/traps.c +3 -0
+#if IS_ENABLED(CONFIG_KVM_INTEL)
+/*
+ * KVM VMX reinjects NMI/IRQ on its current stack, it's a sync

Please use a verb other than "reinject". There is no event injection of any kind, KVM is simply making a function call. KVM already uses "inject" and "reinject" for KVM where KVM is is literally injecting events into the guest.

The "kvm_vmx" part is also weird IMO. The function is in x86's traps/exceptions namespace, not the KVM VMX namespace.

right, "kvm_vmx" doesn't look good per your explanation.

Quoted 2 lines

Maybe exc_raise_nmi_or_irq()?

It's good for me.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Won't this break stack traces to some extent?

The pt_regs structure, and its IP/CS, is NOT part of the call stack, thus I don't see a problem. No?

  bool nmi_cpu_backtrace(struct pt_regs *regs)
  {
  	int cpu = smp_processor_id();
  	unsigned long flags;
  
  	if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
  		/*
  		 * Allow nested NMI backtraces while serializing
  		 * against other CPUs.
  		 */
  		printk_cpu_sync_get_irqsave(flags);
  		if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) {
  			pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
  				cpu, (void *)instruction_pointer(regs));
  		} else {
  			pr_warn("NMI backtrace for cpu %d\n", cpu);
  			if (regs)
  				show_regs(regs); <============================== HERE!!!
  			else
  				dump_stack();
  		}
  		printk_cpu_sync_put_irqrestore(flags);
  		cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
  		return true;
  	}
  
  	return false;
  }
#9 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 10 lines · Sean Christopherson
Quoted 8 lines · On Thu, Nov 10, 2022, Li, Xin3 wrote:
Quoted 5 lines
Quoted 2 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Won't this break stack traces to some extent?

The pt_regs structure, and its IP/CS, is NOT part of the call stack, thus I don't see a problem. No?

I'm not sure what Xin3 is trying to say, but NMI/IRQ handers use pt_regs a *LOT*. pt_regs *MUST* be correct.

#10 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Sean Christopherson
Quoted 3 lines · On Thu, Nov 10, 2022, Li, Xin3 wrote:
Quoted 3 lines
Quoted 2 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Won't this break stack traces to some extent?

The pt_regs structure, and its IP/CS, is NOT part of the call stack, thus I don't see a problem. No?

I'm not sure what Xin3 is trying to say, but NMI/IRQ handers use pt_regs a *LOT*. pt_regs *MUST* be correct.

What is "correct" in this context? Could you describe what aspects of the register image you rely on, and what you expect them to be? Currently KVM basically stuff random data into pt_regs; this at least makes it explicitly zero.

#11 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 10 lines · H. Peter Anvin
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Sean Christopherson
Quoted 6 lines · On Thu, Nov 10, 2022, Li, Xin3 wrote:
Quoted 5 lines
Quoted 2 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Won't this break stack traces to some extent?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

The pt_regs structure, and its IP/CS, is NOT part of the call stack, thus I don't see a problem. No?

I'm not sure what Xin3 is trying to say, but NMI/IRQ handers use pt_regs a *LOT*. pt_regs *MUST* be correct.

What is "correct" in this context?

I don't know since I don't really speak virt, but I could image the regset that would match the vmrun (or whatever intel decided to call that again) instruction.

Quoted 2 lines

Could you describe what aspects of
the register image you rely on, and what you expect them to be?

We rely on CS,IP,FLAGS,SS,SP to be coherent and usable at the very least (must be able to start an unwind from it). But things like perf (NMI) access *all* of them and possibly copy them out to userspace. Perf can also try and use the segment registers in order to try and establish a linear address.

Some exceptions (#GP) access whatever is needed to fully decode and emulate the instruction (IOPL,UMIP,etc..) including the segment registers.

Quoted 2 lines

Currently KVM basically stuff random data into pt_regs; this at least makes it explicitly zero.

:-( Both is broken. Once again proving to me that virt is a bunch of duck-tape at best.

maintainer
#12 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 5 lines · Peter Zijlstra
Quoted 1 line

What is "correct" in this context?

I don't know since I don't really speak virt, but I could image the regset that would match the vmrun (or whatever intel decided to call that again) instruction.

Right now it is not exactly that but close. The RIP is somewhere in vmx_do_interrupt_nmi_irqoff; CS/SS are correct (i.e. it's not like they point to guest values!) and other registers including RSP and RFLAGS are consistent with the RIP.

Quoted 5 lines
Quoted 2 lines

Currently KVM basically stuff random data into pt_regs; this at least makes it explicitly zero.

🙁 Both is broken. Once again proving to me that virt is a bunch of duck-tape at best.

Except currently it is not random. At least I'm not alone in sometimes thinking I understand stuff when I actually don't.

Zero is just wrong, I agree. Xin, if you don't want to poke at the IDT you need to build the pt_regs and pass them to the function you add in patch 5. In order to make it like Peter wants it, then:

1) for the flags just use X86_EFLAGS_FIXED

2) for CS/SS segment selectors use __KERNEL_CS and __KERNEL_DS

3) the RIP/RSP can be found respectively in (unsigned long)vmx_vmexit vmx->loaded_vmcs->host_state.rsp

4) the other registers can be taken from vcpu->arch.regs

But I am not sure it's an improvement. It may be okay to zero the registers, but setting CS/RIP/SS/RSP/RFLAGS to the actual processor values in vmx_do_interrupt_nmi_irqoff is safer.

I'm not sure who would set orig_rax, but I haven't looked too closely. Perhaps that's not a problem, but if so it has to be documented in the commit message.

Paolo

#13 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · Peter Zijlstra
Quoted 1 line

What is "correct" in this context?

I don't know since I don't really speak virt, but I could image the regset that would match the vmrun (or whatever intel decided to call that again) instruction.

Right now it is not exactly that but close. The RIP is somewhere in vmx_do_interrupt_nmi_irqoff; CS/SS are correct (i.e. it's not like they point to guest values!) and other registers including RSP and RFLAGS are consistent with the RIP.

*phew*, that sounds a *lot* better than 'random'. And yes, that should do.

Another thing; these patches appear to be about system vectors and everything, but what I understand from Andrew is that VMX is only screwy vs NMI, not regular interrupts/exceptions, so where does that come from?

SVM specifically fixed the NMI wonkyness with their Global Interrupt flag thingy.

maintainer
#14 lore raw

Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 6 lines · Peter Zijlstra

*phew*, that sounds a*lot* better than 'random'. And yes, that should do.

Another thing; these patches appear to be about system vectors and everything, but what I understand from Andrew is that VMX is only screwy vs NMI, not regular interrupts/exceptions, so where does that come from?

Exceptions are fine, for interrupts it's optional in theory but in practice you have to invoke them manually just like NMIs (I had replied on this in the other thread).

Paolo

Quoted 3 lines

SVM specifically fixed the NMI wonkyness with their Global Interrupt flag thingy.

author
#15 lore raw

RE: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

In reply to Paolo Bonzini
Quoted 47 lines

-----Original Message-----
From: Paolo Bonzini <[email protected]>
Sent: Friday, November 11, 2022 3:58 AM
To: Peter Zijlstra <[email protected]>; H. Peter Anvin <[email protected]>
Cc: Christopherson,, Sean <[email protected]>; Li, Xin3 <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tian, Kevin <[email protected]>
Subject: Re: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

Quoted 5 lines · Peter Zijlstra
Quoted 1 line

What is "correct" in this context?

I don't know since I don't really speak virt, but I could image the regset that would match the vmrun (or whatever intel decided to call that again) instruction.

Right now it is not exactly that but close. The RIP is somewhere in vmx_do_interrupt_nmi_irqoff; CS/SS are correct (i.e. it's not like they point to guest values!) and other registers including RSP and RFLAGS are consistent with the RIP.

Quoted 5 lines
Quoted 2 lines

Currently KVM basically stuff random data into pt_regs; this at least makes it explicitly zero.

🙁 Both is broken. Once again proving to me that virt is a bunch of duck-tape at best.

Except currently it is not random. At least I'm not alone in sometimes thinking I understand stuff when I actually don't.

Zero is just wrong, I agree. Xin, if you don't want to poke at the IDT you need to build the pt_regs and pass them to the function you add in patch 5. In order to make it like Peter wants it, then:

1) for the flags just use X86_EFLAGS_FIXED

2) for CS/SS segment selectors use __KERNEL_CS and __KERNEL_DS

3) the RIP/RSP can be found respectively in (unsigned long)vmx_vmexit vmx->loaded_vmcs->host_state.rsp

4) the other registers can be taken from vcpu->arch.regs

Quoted 7 lines

But I am not sure it's an improvement. It may be okay to zero the registers, but setting CS/RIP/SS/RSP/RFLAGS to the actual processor values in vmx_do_interrupt_nmi_irqoff is safer.

I'm not sure who would set orig_rax, but I haven't looked too closely. Perhaps that's not a problem, but if so it has to be documented in the commit message.

No IRQ handler uses a pt_regs structure as an argument, while NMI handlers do.

For NMI based profiling, what it wants maybe is more of the guest RIP? I might make it over complicated by mixing host/guest profiling.

Quoted 1 line

Paolo

author
#16 lore raw

RE: [PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection

In reply to Sean Christopherson
Quoted 38 lines
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

+ * call thus the values in the pt_regs structure are not used in
+ * executing NMI/IRQ handlers,

Won't this break stack traces to some extent?

The pt_regs structure, and its IP/CS, is NOT part of the call stack, thus I don't see a problem. No?

  bool nmi_cpu_backtrace(struct pt_regs *regs)
  {
  	int cpu = smp_processor_id();
  	unsigned long flags;

if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
/*
* Allow nested NMI backtraces while serializing
* against other CPUs.
*/
printk_cpu_sync_get_irqsave(flags);
if (!READ_ONCE(backtrace_idle) && regs &&
cpu_in_idle(instruction_pointer(regs))) {
pr_warn("NMI backtrace for cpu %d skipped: idling at
%pS\n",
cpu, (void *)instruction_pointer(regs));
} else {
pr_warn("NMI backtrace for cpu %d\n", cpu); if (regs)
show_regs(regs);
<============================== HERE!!!
else
dump_stack();
}
printk_cpu_sync_put_irqrestore(flags); cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); return true;
}

Quoted 2 lines
  	return false;
  }

Right, this is an example in which pt_regs's usage gets broken with my patch.

However a bigger problem emerges, how NMI handlers should get called when VMX is running. If we could address it, we will probably be okay with pt_regs's usage.

author
#17 lore raw

PATCH 6/6 x86/traps: remove unused NMI entry exc_nmi_noist()

In reply to Xin Li

After the introduction of kvm_vmx_reinject_irq(), exc_nmi_noist() is no longer needed, thus remove it.

---
 arch/x86/include/asm/idtentry.h | 15 ---------------
 arch/x86/kernel/nmi.c           | 10 ----------
 2 files changed, 25 deletions(-)
arch/x86/include/asm/idtentry.h +0 -15
index 72184b0b2219..da28ac17c57a 100644
@@ -581,21 +581,6 @@ DECLARE_IDTENTRY_RAW(X86_TRAP_MC,	xenpv_exc_machine_check);
 #endif
 
 /* NMI */
-
-#if defined(CONFIG_X86_64) && IS_ENABLED(CONFIG_KVM_INTEL)
-/*
- * Special NOIST entry point for VMX which invokes this on the kernel
- * stack. asm_exc_nmi() requires an IST to work correctly vs. the NMI
- * 'executing' marker.
- *
- * On 32bit this just uses the regular NMI entry point because 32-bit does
- * not have ISTs.
- */
-DECLARE_IDTENTRY(X86_TRAP_NMI,		exc_nmi_noist);
-#else
-#define asm_exc_nmi_noist		asm_exc_nmi
-#endif
-
 DECLARE_IDTENTRY_NMI(X86_TRAP_NMI,	exc_nmi);
 #ifdef CONFIG_XEN_PV
 DECLARE_IDTENTRY_RAW(X86_TRAP_NMI,	xenpv_exc_nmi);
arch/x86/kernel/nmi.c +0 -10
index cec0bfa3bc04..816bb59a4ba4 100644
@@ -527,16 +527,6 @@ DEFINE_IDTENTRY_RAW(exc_nmi)
 		mds_user_clear_cpu_buffers();
 }
 
-#if defined(CONFIG_X86_64) && IS_ENABLED(CONFIG_KVM_INTEL)
-DEFINE_IDTENTRY_RAW(exc_nmi_noist)
-{
-	exc_nmi(regs);
-}
-#endif
-#if IS_MODULE(CONFIG_KVM_INTEL)
-EXPORT_SYMBOL_GPL(asm_exc_nmi_noist);
-#endif
-
 void stop_nmi(void)
 {
 	ignore_nmis++;
Signature · 1 line
2.34.1