Skip to contentSkip to conversation

x86/irq: trap and interrupt cleanups

PATCH Superseded H. Peter Anvin 2 participants 14 messages May 15, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

H. Peter Anvin

author reviewer
#0 lore raw

v2 cover x86/irq: trap and interrupt cleanups

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

A collection of trap/interrupt-related patches, almost all cleanups. It does remove a modest amount of code (39 lines.) The only patches that should have any possible effect at all are:

7/8 - x86/irq: WARN_ONCE() if irq_move_cleanup is called on a pending interrupt

This condition is believed to be impossible after many improvements to the IRQ vector allocation code since this function was written. Per discussion with tglx, add a WARN_ONCE() if this happens as a first step towards excising this hack.

8/8 - x86/irq: merge and functonalize common code in DECLARE/DEFINE_IDTENTRY_*

This patch reverses kvm_set_cpu_l1tf_flush_l1d() and __irq_enter_raw() in DEFINE_IDTENTRY_SYSVEC_SIMPLE() in order to be able to unify the code with DEFINE_IDTENTRY_SYSVEC().

This replaces a lot of macros with inline functions, which required some amount of adjusting types in various places, they should have no effect.

The reason for unification is mainly to avoid the possibility of inadvertent divergence rather than the rather modest amount of code, but it does remove 25 lines of code.

---
1/8 x86/irq: merge and functionalize common code in DECLARE/DEFINE_IDTENTRY_* 2/8 x86/irq: WARN_ONCE() if irq_move_cleanup is called on a pending interrupt 3/8 x86/kexec: set_[gi]dt() -> native_[gi]dt_invalidate() in machine_kexec_*.c 4/8 x86/desc: add native_[ig]dt_invalidate() to <asm/desc.h>
5/8 x86/idt: remove address argument to idt_invalidate()
6/8 x86/irq: remove unused vectors from <asm/irq_vectors.h>
7/8 x86/irqvector: add NR_EXTERNAL_VECTORS and NR_SYSTEM_VECTORS 8/8 x86/traps: add X86_NR_HW_TRAPS to <asm/trapnr.h>

 arch/x86/entry/common.c                  |   5 +-
 arch/x86/include/asm/desc.h              |  17 ++-
 arch/x86/include/asm/idtentry.h          | 174 +++++++++++++++----------------
 arch/x86/include/asm/irq_stack.h         |  73 +++++--------
 arch/x86/include/asm/irq_vectors.h       |   7 +-
 arch/x86/include/asm/trapnr.h            |   1 +
 arch/x86/kernel/apic/apic.c              |   2 +-
 arch/x86/kernel/apic/vector.c            |   5 +
 arch/x86/kernel/idt.c                    |   5 +-
 arch/x86/kernel/irq.c                    |   1 +
 arch/x86/kernel/machine_kexec_32.c       |  15 +--
 arch/x86/kernel/machine_kexec_64.c       |  33 +-----
 arch/x86/kernel/reboot.c                 |   2 +-
 arch/x86/kernel/sev-es.c                 |   6 +-
 arch/x86/kernel/traps.c                  |   2 +-
 tools/arch/x86/include/asm/irq_vectors.h |   7 +-
 16 files changed, 158 insertions(+), 197 deletions(-)
author reviewer
#1 lore raw

PATCH v2 1/9 x86/traps: add X86_NR_HW_TRAPS to <asm/trapnr.h>

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

The x86 architecture supports up to 32 trap vectors. Add that constant to <asm/trapnr.h>.

---
 arch/x86/include/asm/trapnr.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/trapnr.h +1 -0
index f5d2325aa0b7..f0baf92da20b 100644
@@ -27,6 +27,7 @@
 #define X86_TRAP_VE		20	/* Virtualization Exception */
 #define X86_TRAP_CP		21	/* Control Protection Exception */
 #define X86_TRAP_VC		29	/* VMM Communication Exception */
+#define X86_NR_HW_TRAPS		32	/* Max hardware trap number */
 #define X86_TRAP_IRET		32	/* IRET Exception */
 
 #endif
Signature · 1 line
2.31.1
author reviewer
#2 lore raw

PATCH v2 2/9 x86/irqvector: add NR_EXTERNAL_VECTORS and NR_SYSTEM_VECTORS

In reply to H. Peter Anvin

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

Add defines for the number of external vectors and number of system vectors instead of requiring the use of (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR) and (NR_VECTORS - FIRST_SYSTEM_VECTOR) respectively.

---
 arch/x86/include/asm/idtentry.h          | 4 ++--
 arch/x86/include/asm/irq_vectors.h       | 3 +++
 tools/arch/x86/include/asm/irq_vectors.h | 3 +++
 3 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/include/asm/idtentry.h +2 -2
index 73d45b0dfff2..c03a18cac78e 100644
@@ -504,7 +504,7 @@ __visible noinstr void func(struct pt_regs *regs,			\
 	.align 8
 SYM_CODE_START(irq_entries_start)
     vector=FIRST_EXTERNAL_VECTOR
-    .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
+    .rept NR_EXTERNAL_VECTORS
 	UNWIND_HINT_IRET_REGS
 0 :
 	.byte	0x6a, vector
@@ -520,7 +520,7 @@ SYM_CODE_END(irq_entries_start)
 	.align 8
 SYM_CODE_START(spurious_entries_start)
     vector=FIRST_SYSTEM_VECTOR
-    .rept (NR_VECTORS - FIRST_SYSTEM_VECTOR)
+    .rept NR_SYSTEM_VECTORS
 	UNWIND_HINT_IRET_REGS
 0 :
 	.byte	0x6a, vector
arch/x86/include/asm/irq_vectors.h +3 -0
index 889f8b1b5b7f..d2ef35927770 100644
@@ -114,6 +114,9 @@
 #define FIRST_SYSTEM_VECTOR		NR_VECTORS
 #endif
 
+#define NR_EXTERNAL_VECTORS		(FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
+#define NR_SYSTEM_VECTORS		(NR_VECTORS - FIRST_SYSTEM_VECTOR)
+
 /*
  * Size the maximum number of interrupts.
  *
tools/arch/x86/include/asm/irq_vectors.h +3 -0
index 889f8b1b5b7f..d2ef35927770 100644
@@ -114,6 +114,9 @@
 #define FIRST_SYSTEM_VECTOR		NR_VECTORS
 #endif
 
+#define NR_EXTERNAL_VECTORS		(FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
+#define NR_SYSTEM_VECTORS		(NR_VECTORS - FIRST_SYSTEM_VECTOR)
+
 /*
  * Size the maximum number of interrupts.
  *
Signature · 1 line
2.31.1
author reviewer
#3 lore raw

PATCH v2 3/9 x86/irq: remove unused vectors from <asm/irq_vectors.h>

In reply to H. Peter Anvin

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

UV_BAU_MESSAGE is defined but not used anywhere in the kernel. Presumably this is a stale vector number that can be reclaimed.

MCE_VECTOR is not an actual vector: #MC is an exception, not an interrupt vector, and as such is correctly described as X86_TRAP_MC. MCE_VECTOR is not used anywhere is the kernel.

Note that NMI_VECTOR *is* used; specifically it is the vector number programmed into the APIC LVT when an NMI interrupt is configured. At the moment it is always numerically identical to X86_TRAP_NMI, that is not necessarily going to be the case indefinitely.

---
 arch/x86/include/asm/irq_vectors.h       | 4 ++--
 tools/arch/x86/include/asm/irq_vectors.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/irq_vectors.h +2 -2
index d2ef35927770..43dcb9284208 100644
@@ -26,8 +26,8 @@
  * This file enumerates the exact layout of them:
  */
 
+/* This is used as an interrupt vector when programming the APIC. */
 #define NMI_VECTOR			0x02
-#define MCE_VECTOR			0x12
 
 /*
  * IDT vectors usable for external interrupt sources start at 0x20.
@@ -84,7 +84,7 @@
  */
 #define IRQ_WORK_VECTOR			0xf6
 
-#define UV_BAU_MESSAGE			0xf5
+/* 0xf5 - unused, was UV_BAU_MESSAGE */
 #define DEFERRED_ERROR_VECTOR		0xf4
 
 /* Vector on which hypervisor callbacks will be delivered */
tools/arch/x86/include/asm/irq_vectors.h +2 -2
index d2ef35927770..43dcb9284208 100644
@@ -26,8 +26,8 @@
  * This file enumerates the exact layout of them:
  */
 
+/* This is used as an interrupt vector when programming the APIC. */
 #define NMI_VECTOR			0x02
-#define MCE_VECTOR			0x12
 
 /*
  * IDT vectors usable for external interrupt sources start at 0x20.
@@ -84,7 +84,7 @@
  */
 #define IRQ_WORK_VECTOR			0xf6
 
-#define UV_BAU_MESSAGE			0xf5
+/* 0xf5 - unused, was UV_BAU_MESSAGE */
 #define DEFERRED_ERROR_VECTOR		0xf4
 
 /* Vector on which hypervisor callbacks will be delivered */
Signature · 1 line
2.31.1
author reviewer
#4 lore raw

PATCH v2 4/9 x86/idt: remove address argument to idt_invalidate()

In reply to H. Peter Anvin

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

There is no reason to specify any specific address to idt_invalidate(). It looks mostly like an artifact of unifying code done differently by accident. The most "sensible" address to set here is a NULL pointer - virtual address zero, just as a visual marker.

This also makes it possible to mark the struct desc_ptr in idt_invalidate() as static const.

---
 arch/x86/include/asm/desc.h        | 2 +-
 arch/x86/kernel/idt.c              | 5 ++---
 arch/x86/kernel/machine_kexec_32.c | 2 +-
 arch/x86/kernel/reboot.c           | 2 +-
 4 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/include/asm/desc.h +1 -1
index 476082a83d1c..b8429ae50b71 100644
@@ -427,6 +427,6 @@ static inline void idt_setup_early_pf(void) { }
 static inline void idt_setup_ist_traps(void) { }
 #endif
 
-extern void idt_invalidate(void *addr);
+extern void idt_invalidate(void);
 
 #endif /* _ASM_X86_DESC_H */
arch/x86/kernel/idt.c +2 -3
index d552f177eca0..2779f5226dc2 100644
@@ -331,11 +331,10 @@ void __init idt_setup_early_handler(void)
 
 /**
  * idt_invalidate - Invalidate interrupt descriptor table
- * @addr:	The virtual address of the 'invalid' IDT
  */
-void idt_invalidate(void *addr)
+void idt_invalidate(void)
 {
-	struct desc_ptr idt = { .address = (unsigned long) addr, .size = 0 };
+	static const struct desc_ptr idt = { .address = 0, .size = 0 };
 
 	load_idt(&idt);
 }
arch/x86/kernel/machine_kexec_32.c +1 -1
index 64b00b0d7fe8..1e34feebcd5d 100644
@@ -232,7 +232,7 @@ void machine_kexec(struct kimage *image)
 	 * The gdt & idt are now invalid.
 	 * If you want to load them you must set up your own idt & gdt.
 	 */
-	idt_invalidate(phys_to_virt(0));
+	idt_invalidate();
 	set_gdt(phys_to_virt(0), 0);
 
 	/* now call it */
arch/x86/kernel/reboot.c +1 -1
index b29657b76e3f..ebfb91108232 100644
@@ -669,7 +669,7 @@ static void native_machine_emergency_restart(void)
 			break;
 
 		case BOOT_TRIPLE:
-			idt_invalidate(NULL);
+			idt_invalidate();
 			__asm__ __volatile__("int3");
 
 			/* We're probably dead after this, but... */
Signature · 1 line
2.31.1
maintainer Acked-by on v2 4/9
#5 lore raw

Re: [PATCH v2 4/9] x86/idt: remove address argument to idt_invalidate()

Quoted 11 lines · H. Peter Anvin

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

There is no reason to specify any specific address to idt_invalidate(). It looks mostly like an artifact of unifying code done differently by accident. The most "sensible" address to set here is a NULL pointer - virtual address zero, just as a visual marker.

This also makes it possible to mark the struct desc_ptr in idt_invalidate() as static const.

arch/x86/kernel/machine_kexec_32.c void machine_kexec(struct kimage *image) · line 235

Quoted 2 lines
arch/x86/kernel/machine_kexec_32.c +1 -1
-	idt_invalidate(phys_to_virt(0));
+	idt_invalidate();

This hunk is an actual change, but I'm not sure it needs to be mentioned in the changelog.

author reviewer
#6 lore raw

PATCH v2 5/9 x86/desc: add native_[ig]dt_invalidate() to <asm/desc.h>

In reply to H. Peter Anvin

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

In some places, we want the native forms of descriptor table invalidation. Rather than open-coding them, add explicitly native functions to invalidate the GDT and IDT.

---
 arch/x86/include/asm/desc.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/include/asm/desc.h +15 -0
index b8429ae50b71..aa366b2bbc41 100644
@@ -224,6 +224,21 @@ static inline void store_idt(struct desc_ptr *dtr)
 	asm volatile("sidt %0":"=m" (*dtr));
 }
 
+static const struct desc_ptr __invalid_gdt_idt_ptr __maybe_unused = {
+	.address = 0,
+	.size = 0
+};
+
+static inline void native_gdt_invalidate(void)
+{
+	native_load_gdt(&__invalid_gdt_idt_ptr);
+}
+
+static inline void native_idt_invalidate(void)
+{
+	native_load_idt(&__invalid_gdt_idt_ptr);
+}
+
 /*
  * The LTR instruction marks the TSS GDT entry as busy. On 64-bit, the GDT is
  * a read-only remapping. To prevent a page fault, the GDT is switched to the
Signature · 1 line
2.31.1
maintainer
#7 lore raw

Re: [PATCH v2 5/9] x86/desc: add native_[ig]dt_invalidate() to <asm/desc.h>

Quoted 16 lines · H. Peter Anvin

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

In some places, we want the native forms of descriptor table invalidation. Rather than open-coding them, add explicitly native functions to invalidate the GDT and IDT.

---
 arch/x86/include/asm/desc.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/include/asm/desc.h +0 -0
index b8429ae50b71..aa366b2bbc41 100644

arch/x86/include/asm/desc.h static inline void store_idt(struct desc_ptr *dtr) · line 230

Quoted 7 lines
arch/x86/include/asm/desc.h +4 -0
@@ -224,6 +224,21 @@ static inline void store_idt(struct desc_ptr *dtr)
 	asm volatile("sidt %0":"=m" (*dtr));
 }
 
+static const struct desc_ptr __invalid_gdt_idt_ptr __maybe_unused = {
+	.address = 0,
+	.size = 0
+};

I'm not convinced that putting this in a header is really a great idea. How about:

arch/x86/include/asm/desc.h static inline void store_idt(struct desc_ptr *dtr) · line 233

Quoted 3 lines
arch/x86/include/asm/desc.h +3 -0
+
+static inline void native_gdt_invalidate(void)
+{
        const struct desc_ptr ... = ...;

arch/x86/include/asm/desc.h static inline void store_idt(struct desc_ptr *dtr) · line 235

Quoted 2 lines
arch/x86/include/asm/desc.h +2 -0
+	native_load_gdt(&__invalid_gdt_idt_ptr);
+}

That should generate two PUSH instructions and may well result in a smaller binary :)

--Andy

author reviewer
#8 lore raw

PATCH v2 6/9 x86/kexec: set_[gi]dt() -> native_[gi]dt_invalidate() in machine_kexec_*.c

In reply to H. Peter Anvin

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

These files contain private set_gdt() functions which are only used to invalid the gdt; machine_kexec_64.c also contains a set_idt() function to invalidate the idt.

phys_to_virt(0) *really* doesn't make any sense for creating an invalid GDT. A NULL pointer (virtual 0) makes a lot more sense; although neither will allow any actual memory reference, a NULL pointer stands out more.

Replace these calls with native_[gi]dt_invalidate().

---
 arch/x86/kernel/machine_kexec_32.c | 15 ++------------
 arch/x86/kernel/machine_kexec_64.c | 33 ++----------------------------
 2 files changed, 4 insertions(+), 44 deletions(-)
arch/x86/kernel/machine_kexec_32.c +2 -13
index 1e34feebcd5d..1b373d79cedc 100644
@@ -23,17 +23,6 @@
 #include <asm/set_memory.h>
 #include <asm/debugreg.h>
 
-static void set_gdt(void *newgdt, __u16 limit)
-{
-	struct desc_ptr curgdt;
-
-	/* ia32 supports unaligned loads & stores */
-	curgdt.size    = limit;
-	curgdt.address = (unsigned long)newgdt;
-
-	load_gdt(&curgdt);
-}
-
 static void load_segments(void)
 {
 #define __STR(X) #X
@@ -232,8 +221,8 @@ void machine_kexec(struct kimage *image)
 	 * The gdt & idt are now invalid.
 	 * If you want to load them you must set up your own idt & gdt.
 	 */
-	idt_invalidate();
-	set_gdt(phys_to_virt(0), 0);
+	native_idt_invalidate();
+	native_gdt_invalidate();
 
 	/* now call it */
 	image->start = relocate_kernel_ptr((unsigned long)image->head,
arch/x86/kernel/machine_kexec_64.c +2 -31
index c078b0d3ab0e..131f30fdcfbd 100644
@@ -256,35 +256,6 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
 	return init_transition_pgtable(image, level4p);
 }
 
-static void set_idt(void *newidt, u16 limit)
-{
-	struct desc_ptr curidt;
-
-	/* x86-64 supports unaligned loads & stores */
-	curidt.size    = limit;
-	curidt.address = (unsigned long)newidt;
-
-	__asm__ __volatile__ (
-		"lidtq %0\n"
-		: : "m" (curidt)
-		);
-};
-
-
-static void set_gdt(void *newgdt, u16 limit)
-{
-	struct desc_ptr curgdt;
-
-	/* x86-64 supports unaligned loads & stores */
-	curgdt.size    = limit;
-	curgdt.address = (unsigned long)newgdt;
-
-	__asm__ __volatile__ (
-		"lgdtq %0\n"
-		: : "m" (curgdt)
-		);
-};
-
 static void load_segments(void)
 {
 	__asm__ __volatile__ (
@@ -379,8 +350,8 @@ void machine_kexec(struct kimage *image)
 	 * The gdt & idt are now invalid.
 	 * If you want to load them you must set up your own idt & gdt.
 	 */
-	set_gdt(phys_to_virt(0), 0);
-	set_idt(phys_to_virt(0), 0);
+	native_idt_invalidate();
+	native_gdt_invalidate();
 
 	/* now call it */
 	image->start = relocate_kernel((unsigned long)image->head,
Signature · 1 line
2.31.1
maintainer Acked-by on v2 6/9
#9 lore raw

Re: [PATCH v2 6/9] x86/kexec: set_[gi]dt() -> native_[gi]dt_invalidate() in machine_kexec_*.c

Quoted 6 lines · H. Peter Anvin

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

These files contain private set_gdt() functions which are only used to invalid the gdt; machine_kexec_64.c also contains a set_idt() function to invalidate the idt.

Quoted 8 lines

phys_to_virt(0) *really* doesn't make any sense for creating an invalid GDT. A NULL pointer (virtual 0) makes a lot more sense; although neither will allow any actual memory reference, a NULL pointer stands out more.

Replace these calls with native_[gi]dt_invalidate().

author reviewer
#10 lore raw

PATCH v2 7/9 x86/irq: WARN_ONCE() if irq_move_cleanup is called on a pending interrupt

In reply to H. Peter Anvin

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

The current IRQ vector allocation code should be "clean" and never issue a IRQ_MOVE_CLEANUP_VECTOR IPI for an interrupt that could still be pending. This should make it possible to move it to the "normal" system IRQ vector range. This should probably be a three-step process:

1. Introduce this WARN_ONCE() on this event ever occurring.
2. Move the IRQ_MOVE_CLEANUP_VECTOR to the sysvec range.
3. Remove the self-IPI hack.

This implements step 1.

---
 arch/x86/kernel/apic/vector.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kernel/apic/vector.c +5 -0
index 6dbdc7c22bb7..7ba2982a3585 100644
@@ -939,9 +939,14 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_irq_move_cleanup)
 		 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
 		 * priority external vector, so on return from this
 		 * interrupt the device interrupt will happen first.
+		 *
+		 * *** This should never happen with the current IRQ
+		 * cleanup code, so WARN_ONCE() for now, and
+		 * eventually get rid of this hack.
 		 */
 		irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
 		if (irr & (1U << (vector % 32))) {
+			WARN_ONCE(1, "irq_move_cleanup called on still pending interrupt\n");
 			apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
 			continue;
 		}
Signature · 1 line
2.31.1
author reviewer
#11 lore raw

Re: [PATCH v2 7/9] x86/irq: WARN_ONCE() if irq_move_cleanup is called on a pending interrupt

Ugh... I could swear I had fixed the patch description.

Quoted 40 lines · H. Peter Anvin

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

The current IRQ vector allocation code should be "clean" and never issue a IRQ_MOVE_CLEANUP_VECTOR IPI for an interrupt that could still be pending. This should make it possible to move it to the "normal" system IRQ vector range. This should probably be a three-step process:

1. Introduce this WARN_ONCE() on this event ever occurring.
2. Move the IRQ_MOVE_CLEANUP_VECTOR to the sysvec range.
3. Remove the self-IPI hack.

This implements step 1.

---
arch/x86/kernel/apic/vector.c | 5 +++++

1 file changed, 5 insertions(+)
arch/x86/kernel/apic/vector.c +5 -0
index 6dbdc7c22bb7..7ba2982a3585 100644
@@ -939,9 +939,14 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_irq_move_cleanup)
 		 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
 		 * priority external vector, so on return from this
 		 * interrupt the device interrupt will happen first.
+		 *
+		 * *** This should never happen with the current IRQ
+		 * cleanup code, so WARN_ONCE() for now, and
+		 * eventually get rid of this hack.
 		 */
 		irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
 		if (irr & (1U << (vector % 32))) {
+			WARN_ONCE(1, "irq_move_cleanup called on still pending
 interrupt\n");
 			apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
 			continue;
		}
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
author reviewer
#12 lore raw

PATCH v2 7/8 x86/irq: WARN_ONCE() if irq_move_cleanup is called on a pending interrupt

In reply to H. Peter Anvin

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

The current IRQ vector allocation code should be "clean" and never issue a IRQ_MOVE_CLEANUP_VECTOR IPI for an interrupt that could still be pending. This should make it possible to move it to the "normal" system IRQ vector range. This should probably be a three-step process:

1. Introduce this WARN_ONCE() on this event ever occurring.
2. Remove the self-IPI hack.
3. Move the IRQ_MOVE_CLEANUP_VECTOR to the sysvec range.

This implements step 1.

[ Previous versions of this patch had steps 2 and 3 reversed. ]

---
 arch/x86/kernel/apic/vector.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kernel/apic/vector.c +5 -0
index 6dbdc7c22bb7..7ba2982a3585 100644
@@ -939,9 +939,14 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_irq_move_cleanup)
 		 * to this CPU. IRQ_MOVE_CLEANUP_VECTOR is the lowest
 		 * priority external vector, so on return from this
 		 * interrupt the device interrupt will happen first.
+		 *
+		 * *** This should never happen with the current IRQ
+		 * cleanup code, so WARN_ONCE() for now, and
+		 * eventually get rid of this hack.
 		 */
 		irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
 		if (irr & (1U << (vector % 32))) {
+			WARN_ONCE(1, "irq_move_cleanup called on still pending interrupt\n");
 			apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
 			continue;
 		}
Signature · 1 line
2.31.1
author reviewer
#13 lore raw

PATCH v2 8/9 x86/irq: merge and functionalize common code in DECLARE/DEFINE_IDTENTRY_*

In reply to H. Peter Anvin

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

Move as much of the common code in the _IDTENTRY_ and run_*_on_irq_stack() into inline functions instead of macros. A lot of them differ only in types and/or the presence or absence of an additional argument; the differential amount of code is neglible and after bending the types a little bit, they unify nicely.

---
 arch/x86/entry/common.c          |   5 +-
 arch/x86/include/asm/idtentry.h  | 170 +++++++++++++++----------------
 arch/x86/include/asm/irq_stack.h |  73 +++++--------
 arch/x86/kernel/apic/apic.c      |   2 +-
 arch/x86/kernel/irq.c            |   1 +
 arch/x86/kernel/sev-es.c         |   6 +-
 arch/x86/kernel/traps.c          |   2 +-
 7 files changed, 117 insertions(+), 142 deletions(-)
arch/x86/entry/common.c +3 -2
index 7b2542b13ebd..1e46a1a35b20 100644
@@ -253,7 +253,8 @@ static __always_inline bool get_and_clear_inhcall(void) { return false; }
 static __always_inline void restore_inhcall(bool inhcall) { }
 #endif
 
-static void __xen_pv_evtchn_do_upcall(struct pt_regs *regs)
+static void __xen_pv_evtchn_do_upcall(struct pt_regs *regs,
+				      u32 __dummy __always_unused)
 {
 	struct pt_regs *old_regs = set_irq_regs(regs);
 
@@ -269,7 +270,7 @@ __visible noinstr void xen_pv_evtchn_do_upcall(struct pt_regs *regs)
 	irqentry_state_t state = irqentry_enter(regs);
 	bool inhcall;
 
-	run_sysvec_on_irqstack_cond(__xen_pv_evtchn_do_upcall, regs);
+	run_on_irqstack_cond(__xen_pv_evtchn_do_upcall, regs, 0);
 
 	inhcall = get_and_clear_inhcall();
 	if (inhcall && !WARN_ON_ONCE(state.exit_rcu)) {
arch/x86/include/asm/idtentry.h +84 -86

246 lines not on this page · raw message

arch/x86/include/asm/irq_stack.h +24 -49

108 lines not on this page · raw message

arch/x86/kernel/apic/apic.c +1 -1
index 4a39fb429f15..64832869e1a1 100644
@@ -2188,7 +2188,7 @@ static noinline void handle_spurious_interrupt(u8 vector)
  */
 DEFINE_IDTENTRY_IRQ(spurious_interrupt)
 {
-	handle_spurious_interrupt(vector);
+	handle_spurious_interrupt((u8)error_code);
 }
 
 DEFINE_IDTENTRY_SYSVEC(sysvec_spurious_apic_interrupt)
arch/x86/kernel/irq.c +1 -0
index e28f6a5d14f1..27994818d9b1 100644
@@ -239,6 +239,7 @@ static __always_inline void handle_irq(struct irq_desc *desc,
  */
 DEFINE_IDTENTRY_IRQ(common_interrupt)
 {
+	const u8 vector = error_code;
 	struct pt_regs *old_regs = set_irq_regs(regs);
 	struct irq_desc *desc;
 
arch/x86/kernel/sev-es.c +3 -3
index 73873b007838..bdd75b71d0f3 100644
@@ -1335,15 +1335,15 @@ DEFINE_IDTENTRY_VC_SAFE_STACK(exc_vmm_communication)
 		vc_finish_insn(&ctxt);
 		break;
 	case ES_UNSUPPORTED:
-		pr_err_ratelimited("Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
+		pr_err_ratelimited("Unsupported exit-code 0x%02x in early #VC exception (IP: 0x%lx)\n",
 				   error_code, regs->ip);
 		goto fail;
 	case ES_VMM_ERROR:
-		pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
+		pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02x IP: 0x%lx)\n",
 				   error_code, regs->ip);
 		goto fail;
 	case ES_DECODE_FAILED:
-		pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
+		pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02x IP: 0x%lx)\n",
 				   error_code, regs->ip);
 		goto fail;
 	case ES_EXCEPTION:
arch/x86/kernel/traps.c +1 -1
index 853ea7a80806..f5aecbb44bc7 100644
@@ -461,7 +461,7 @@ DEFINE_IDTENTRY_DF(exc_double_fault)
 	}
 #endif
 
-	pr_emerg("PANIC: double fault, error_code: 0x%lx\n", error_code);
+	pr_emerg("PANIC: double fault, error_code: 0x%x\n", error_code);
 	die("double fault", regs, error_code);
 	panic("Machine halted.");
 	instrumentation_end();
Signature · 1 line
2.31.1