genirq: Provide interrupt injection mechanism
authorThomas Gleixner <tglx@linutronix.de>
Fri, 6 Mar 2020 13:03:47 +0000 (14:03 +0100)
committerThomas Gleixner <tglx@linutronix.de>
Sun, 8 Mar 2020 10:06:42 +0000 (11:06 +0100)
Error injection mechanisms need a half ways safe way to inject interrupts as
invoking generic_handle_irq() or the actual device interrupt handler
directly from e.g. a debugfs write is not guaranteed to be safe.

On x86 generic_handle_irq() is unsafe due to the hardware trainwreck which
is the base of x86 interrupt delivery and affinity management.

Move the irq debugfs injection code into a separate function which can be
used by error injection code as well.

The implementation prevents at least that state is corrupted, but it cannot
close a very tiny race window on x86 which might result in a stale and not
serviced device interrupt under very unlikely circumstances.

This is explicitly for debugging and testing and not for production use or
abuse in random driver code.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Reviewed-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Acked-by: Marc Zyngier <maz@kernel.org>
Link: https://lkml.kernel.org/r/20200306130623.990928309@linutronix.de
include/linux/interrupt.h
kernel/irq/Kconfig
kernel/irq/chip.c
kernel/irq/debugfs.c
kernel/irq/internals.h
kernel/irq/resend.c

index c5fe60ec6b84e3ae480c57285a8a2aa6e4b63f39..80f637c3a6f38d0c04e60019a20a6666d924c571 100644 (file)
@@ -248,6 +248,8 @@ extern void enable_percpu_nmi(unsigned int irq, unsigned int type);
 extern int prepare_percpu_nmi(unsigned int irq);
 extern void teardown_percpu_nmi(unsigned int irq);
 
+extern int irq_inject_interrupt(unsigned int irq);
+
 /* The following three functions are for the core kernel use only. */
 extern void suspend_device_irqs(void);
 extern void resume_device_irqs(void);
index f92d9a687372051f0ddc579679c6d692b3024419..20d501af4f2e46ac2f8a46a78937ad41310294e6 100644 (file)
@@ -43,6 +43,10 @@ config GENERIC_IRQ_MIGRATION
 config AUTO_IRQ_AFFINITY
        bool
 
+# Interrupt injection mechanism
+config GENERIC_IRQ_INJECTION
+       bool
+
 # Tasklet based software resend for pending interrupts on enable_irq()
 config HARDIRQS_SW_RESEND
        bool
@@ -127,6 +131,7 @@ config SPARSE_IRQ
 config GENERIC_IRQ_DEBUGFS
        bool "Expose irq internals in debugfs"
        depends on DEBUG_FS
+       select GENERIC_IRQ_INJECTION
        default n
        ---help---
 
index b3fa2d87d2f30ae025c9be7c426136fc98e5c549..41e7e37a0928ad25fa61882498f1e44c3a6ca24d 100644 (file)
@@ -278,7 +278,7 @@ int irq_startup(struct irq_desc *desc, bool resend, bool force)
                }
        }
        if (resend)
-               check_irq_resend(desc);
+               check_irq_resend(desc, false);
 
        return ret;
 }
index 0c607798f519cf12231588245b680fc82c889831..4f9f844074db1510fe33c4d58a93f91407597fc6 100644 (file)
@@ -190,39 +190,7 @@ static ssize_t irq_debug_write(struct file *file, const char __user *user_buf,
                return -EFAULT;
 
        if (!strncmp(buf, "trigger", size)) {
-               unsigned long flags;
-               int err;
-
-               /* Try the HW interface first */
-               err = irq_set_irqchip_state(irq_desc_get_irq(desc),
-                                           IRQCHIP_STATE_PENDING, true);
-               if (!err)
-                       return count;
-
-               /*
-                * Otherwise, try to inject via the resend interface,
-                * which may or may not succeed.
-                */
-               chip_bus_lock(desc);
-               raw_spin_lock_irqsave(&desc->lock, flags);
-
-               /*
-                * Don't allow injection when the interrupt is:
-                *  - Level or NMI type
-                *  - not activated
-                *  - replaying already
-                */
-               if (irq_settings_is_level(desc) ||
-                   !irqd_is_activated(&desc->irq_data) ||
-                   (desc->istate & (IRQS_NMI | IRQS_REPLAY))) {
-                       err = -EINVAL;
-               } else {
-                       desc->istate |= IRQS_PENDING;
-                       err = check_irq_resend(desc);
-               }
-
-               raw_spin_unlock_irqrestore(&desc->lock, flags);
-               chip_bus_sync_unlock(desc);
+               int err = irq_inject_interrupt(irq_desc_get_irq(desc));
 
                return err ? err : count;
        }
index 8980859bdf1eef919e2b25153de1fec4bd8a9edd..7db284b10ac9ca0360c91668e0e69da76ec5b03f 100644 (file)
@@ -108,7 +108,7 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc);
 irqreturn_t handle_irq_event(struct irq_desc *desc);
 
 /* Resending of interrupts :*/
-int check_irq_resend(struct irq_desc *desc);
+int check_irq_resend(struct irq_desc *desc, bool inject);
 bool irq_wait_for_poll(struct irq_desc *desc);
 void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action);
 
index bef72dcfb79b5313ddf9f34b3e3b9b122b0bbe97..27634f4022d034f42f25cef8cc4b0c36dc240a05 100644 (file)
@@ -91,7 +91,7 @@ static int irq_sw_resend(struct irq_desc *desc)
  *
  * Is called with interrupts disabled and desc->lock held.
  */
-int check_irq_resend(struct irq_desc *desc)
+int check_irq_resend(struct irq_desc *desc, bool inject)
 {
        int err = 0;
 
@@ -108,7 +108,7 @@ int check_irq_resend(struct irq_desc *desc)
        if (desc->istate & IRQS_REPLAY)
                return -EBUSY;
 
-       if (!(desc->istate & IRQS_PENDING))
+       if (!(desc->istate & IRQS_PENDING) && !inject)
                return 0;
 
        desc->istate &= ~IRQS_PENDING;
@@ -122,3 +122,52 @@ int check_irq_resend(struct irq_desc *desc)
                desc->istate |= IRQS_REPLAY;
        return err;
 }
+
+#ifdef CONFIG_GENERIC_IRQ_INJECTION
+/**
+ * irq_inject_interrupt - Inject an interrupt for testing/error injection
+ * @irq:       The interrupt number
+ *
+ * This function must only be used for debug and testing purposes!
+ *
+ * Especially on x86 this can cause a premature completion of an interrupt
+ * affinity change causing the interrupt line to become stale. Very
+ * unlikely, but possible.
+ *
+ * The injection can fail for various reasons:
+ * - Interrupt is not activated
+ * - Interrupt is NMI type or currently replaying
+ * - Interrupt is level type
+ * - Interrupt does not support hardware retrigger and software resend is
+ *   either not enabled or not possible for the interrupt.
+ */
+int irq_inject_interrupt(unsigned int irq)
+{
+       struct irq_desc *desc;
+       unsigned long flags;
+       int err;
+
+       /* Try the state injection hardware interface first */
+       if (!irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, true))
+               return 0;
+
+       /* That failed, try via the resend mechanism */
+       desc = irq_get_desc_buslock(irq, &flags, 0);
+       if (!desc)
+               return -EINVAL;
+
+       /*
+        * Only try to inject when the interrupt is:
+        *  - not NMI type
+        *  - activated
+        */
+       if ((desc->istate & IRQS_NMI) || !irqd_is_activated(&desc->irq_data))
+               err = -EINVAL;
+       else
+               err = check_irq_resend(desc, true);
+
+       irq_put_desc_busunlock(desc, flags);
+       return err;
+}
+EXPORT_SYMBOL_GPL(irq_inject_interrupt);
+#endif