irq: detect slow IRQ handlers
Subsystems (2)
v2 cover irq: detect slow IRQ handlers
Hi,
While fuzzing arm64 with Syzkaller (under QEMU+KVM) over a number of releases, I've occasionally seen some ridiculously long stalls (20+ seconds), where it appears that a CPU is stuck in a hard IRQ context. As this gets detected after the CPU returns to the interrupted context, it's difficult to identify where exactly the stall is coming from.
These patches are intended to help tracking this down, with a WARN() if an IRQ handler takes longer than a given timout (1 second by default), logging the specific IRQ and handler function. While it's possible to achieve similar with tracing, it's harder to integrate that into an automated fuzzing setup.
I've been running this for a short while, and haven't yet seen any of the stalls with this applied, but I've tested with smaller timeout periods in the 1 millisecond range by overloading the host, so I'm confident that the check works.
Thanks,
Mark.
Since v1 [1]:
* Minor commit message tweaks
* Add Paul's Acked-by
* Trivial rebase to v5.13-rc4
[1] https://lore.kernel.org/r/[email protected]
Mark Rutland (2):
irq: abstract irqaction handler invocation
irq: detect long-running IRQ handlers
kernel/irq/chip.c | 15 +++---------- kernel/irq/handle.c | 4 +--- kernel/irq/internals.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 15 +++++++++++++ 4 files changed, 76 insertions(+), 15 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/2 irq: abstract irqaction handler invocation
We have a few functions which invoke irqaction handlers, all of which need to call trace_irq_handler_entry() and trace_irq_handler_exit().
In preparation for adding some additional debug logic to each irqaction handler invocation, let's factor out this work to a helper. Where the return value isn't consumed, the unused temporary variable is also removed.
There should be no functional change as a result of this patch.
--- kernel/irq/chip.c | 15 +++------------ kernel/irq/handle.c | 4 +--- kernel/irq/internals.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-)
kernel/irq/chip.c +3 -12
@@ -741,16 +741,13 @@ void handle_fasteoi_nmi(struct irq_desc *desc)struct irq_chip *chip =irq_desc_get_chip (desc);struct irqaction *action = desc->action;unsigned int irq =irq_desc_get_irq (desc); -irqreturn_t res;__kstat_incr_irqs_this_cpu (desc); -trace_irq_handler_entry (irq, action);/* * NMIs cannot be shared, there is only one action. */ - res = action->handler (irq, action->dev_id); -trace_irq_handler_exit (irq, action, res); +handle_irqaction (irq, action);if (chip->irq_eoi) chip->irq_eoi (&desc->irq_data);
@@ -914,7 +911,6 @@ void handle_percpu_devid_irq(struct irq_desc *desc)struct irq_chip *chip =irq_desc_get_chip (desc);struct irqaction *action = desc->action;unsigned int irq =irq_desc_get_irq (desc); -irqreturn_t res;/* * PER CPU interrupts are not serialized. Do not touch
@@ -926,9 +922,7 @@ void handle_percpu_devid_irq(struct irq_desc *desc) chip->irq_ack (&desc->irq_data);if (likely (action)) { -trace_irq_handler_entry (irq, action); - res = action->handler (irq,raw_cpu_ptr (action->percpu_dev_id)); -trace_irq_handler_exit (irq, action, res); +handle_irqaction_percpu_devid (irq, action); }else {unsigned int cpu =smp_processor_id ();bool enabled =cpumask_test_cpu (cpu, desc->percpu_enabled);
@@ -957,13 +951,10 @@ void handle_percpu_devid_fasteoi_nmi(struct irq_desc *desc)struct irq_chip *chip =irq_desc_get_chip (desc);struct irqaction *action = desc->action;unsigned int irq =irq_desc_get_irq (desc); -irqreturn_t res;__kstat_incr_irqs_this_cpu (desc); -trace_irq_handler_entry (irq, action); - res = action->handler (irq,raw_cpu_ptr (action->percpu_dev_id)); -trace_irq_handler_exit (irq, action, res); +handle_irqaction_percpu_devid (irq, action);if (chip->irq_eoi) chip->irq_eoi (&desc->irq_data);
kernel/irq/handle.c +1 -3
@@ -152,9 +152,7 @@ irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags !(action->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT)))lockdep_hardirq_threaded (); -trace_irq_handler_entry (irq, action); - res = action->handler (irq, action->dev_id); -trace_irq_handler_exit (irq, action, res); + res =handle_irqaction (irq, action);if (WARN_ONCE (!irqs_disabled (),"irq %u handler %pS enabled interrupts\n" , irq, action->handler))
kernel/irq/internals.h +28 -0
@@ -11,6 +11,8 @@#include <linux/pm_runtime.h> #include <linux/sched/clock.h> +#include <trace/events/irq.h> +#ifdef CONFIG_SPARSE_IRQ # define IRQ_BITMAP_BITS (NR_IRQS + 8196)#else
@@ -107,6 +109,32 @@ irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flagsirqreturn_t handle_irq_event_percpu (struct irq_desc *desc);irqreturn_t handle_irq_event (struct irq_desc *desc); +static inline irqreturn_t __handle_irqaction (unsigned int irq, +struct irqaction *action, +void *dev_id) +{ +irqreturn_t res; + +trace_irq_handler_entry (irq, action); + res = action->handler (irq, dev_id); +trace_irq_handler_exit (irq, action, res); + +return res; +} + +static inline irqreturn_t handle_irqaction (unsigned int irq, +struct irqaction *action) +{ +return __handle_irqaction (irq, action, action->dev_id); +} + +static inline irqreturn_t handle_irqaction_percpu_devid (unsigned int irq, +struct irqaction *action) +{ +return __handle_irqaction (irq, action, +raw_cpu_ptr (action->percpu_dev_id)); +} +/* Resending of interrupts :*/ int check_irq_resend (struct irq_desc *desc,bool inject);bool irq_wait_for_poll (struct irq_desc *desc);
Signature · 1 line
2.11.0
PATCH v2 2/2 irq: detect long-running IRQ handlers
In reply to Mark RutlandIf a hard IRQ handler takes a long time to handle an IRQ, it may cause a soft lockup or RCU stall, but as this will be detected once the handler has returned it can be difficult to attribute the delay to the specific IRQ handler.
It's possible to trace IRQ handlers to diagnose this, but that's not a great fit for automated testing environments (e.g. fuzzers), where something like the existing lockup/stall detectors works well.
This patch adds a new stall detector for IRQ handlers, which reports when handlers took longer than a given timeout value (defaulting to 1 second). This won't detect hung IRQ handlers (which requires an NMI, and should already be caught by hung task detection on systems with NMIs), but helps on platforms without NMI or where a periodic watchdog is undesireable.
--- kernel/irq/internals.h | 35 ++++++++++++++++++++++++++++++++--- lib/Kconfig.debug | 15 +++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-)
kernel/irq/internals.h +32 -3
@@ -6,6 +6,7 @@* kernel/irq/. Do not even think about using any information outside * of this file for your non core code. */ +#include <linux/bug.h> #include <linux/irqdesc.h> #include <linux/kernel_stat.h> #include <linux/pm_runtime.h>
@@ -122,17 +123,45 @@ static inline irqreturn_t __handle_irqaction(unsigned int irq,return res; } +#ifdef CONFIG_DETECT_SLOW_IRQ_HANDLER +static inline irqreturn_t __handle_check_irqaction (unsigned int irq, +struct irqaction *action, +void *dev_id) +{ +u64 timeout =CONFIG_IRQ_HANDLER_TIMEOUT_NS ; +u64 start, end, duration; +int res; + + start =local_clock (); + res =__handle_irqaction (irq, action, dev_id); + end =local_clock (); + + duration = end - start; +WARN (duration > timeout,"IRQ %d handler %ps took %llu ns\n" , + irq, action->handler, duration); + +return res; +} +#else +static inline irqreturn_t __handle_check_irqaction (unsigned int irq, +struct irqaction *action, +void *dev_id) +{ +return __handle_irqaction (irq, action, dev_id); +} +#endif +static inline irqreturn_t handle_irqaction (unsigned int irq,struct irqaction *action) { -return __handle_irqaction (irq, action, action->dev_id); +return __handle_check_irqaction (irq, action, action->dev_id); }static inline irqreturn_t handle_irqaction_percpu_devid (unsigned int irq,struct irqaction *action) { -return __handle_irqaction (irq, action, -raw_cpu_ptr (action->percpu_dev_id)); +return __handle_check_irqaction (irq, action, +raw_cpu_ptr (action->percpu_dev_id)); }/* Resending of interrupts :*/
lib/Kconfig.debug +15 -0
@@ -1144,6 +1144,21 @@ config WQ_WATCHDOG state. This can be configured through kernel parameter "workqueue.watchdog_thresh" and its sysfs counterpart. +config DETECT_SLOW_IRQ_HANDLER + bool "Detect long-running IRQ handlers" + help + Say Y here to enable detection of long-running IRQ handlers. When a + (hard) IRQ handler returns after a given timeout value (1s by + default) a warning will be printed with the name of the handler. + + This can help to identify specific IRQ handlers which are + contributing to stalls. + +config IRQ_HANDLER_TIMEOUT_NS + int "Timeout for long-running IRQ handlers (in nanoseconds)" + depends on DETECT_SLOW_IRQ_HANDLER + default 1000000000 + config TEST_LOCKUP tristate "Test module to generate lockups" depends on m
Signature · 1 line
2.11.0