Skip to content

Commit e2c1273

Browse files
vwaxKAGA-KOKO
authored andcommitted
genirq: Prevent nested thread vs synchronize_hardirq() deadlock
There is a possibility of deadlock if synchronize_hardirq() is called when the nested threaded interrupt is active. The following scenario was observed on a uniprocessor PREEMPT_NONE system: Thread 1 Thread 2 handle_nested_thread() Set INPROGRESS Call ->thread_fn() thread_fn goes to sleep free_irq() __synchronize_hardirq() Busy-loop forever waiting for INPROGRESS to be cleared The INPROGRESS flag is only supposed to be used for hard interrupt handlers. Remove the incorrect usage in the nested threaded interrupt case and instead re-use the threads_active / wait_for_threads mechanism to wait for nested threaded interrupts to complete. Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/r/20230613-genirq-nested-v3-1-ae58221143eb@axis.com
1 parent 5d0c230 commit e2c1273

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

kernel/irq/chip.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,12 @@ void handle_nested_irq(unsigned int irq)
473473
action = desc->action;
474474
if (unlikely(!action || irqd_irq_disabled(&desc->irq_data))) {
475475
desc->istate |= IRQS_PENDING;
476-
goto out_unlock;
476+
raw_spin_unlock_irq(&desc->lock);
477+
return;
477478
}
478479

479480
kstat_incr_irqs_this_cpu(desc);
480-
irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
481+
atomic_inc(&desc->threads_active);
481482
raw_spin_unlock_irq(&desc->lock);
482483

483484
action_ret = IRQ_NONE;
@@ -487,11 +488,7 @@ void handle_nested_irq(unsigned int irq)
487488
if (!irq_settings_no_debug(desc))
488489
note_interrupt(desc, action_ret);
489490

490-
raw_spin_lock_irq(&desc->lock);
491-
irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
492-
493-
out_unlock:
494-
raw_spin_unlock_irq(&desc->lock);
491+
wake_threads_waitq(desc);
495492
}
496493
EXPORT_SYMBOL_GPL(handle_nested_irq);
497494

kernel/irq/internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ void irq_resend_init(struct irq_desc *desc);
121121
bool irq_wait_for_poll(struct irq_desc *desc);
122122
void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action);
123123

124+
void wake_threads_waitq(struct irq_desc *desc);
125+
124126
#ifdef CONFIG_PROC_FS
125127
extern void register_irq_proc(unsigned int irq, struct irq_desc *desc);
126128
extern void unregister_irq_proc(unsigned int irq, struct irq_desc *desc);

kernel/irq/manage.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ bool synchronize_hardirq(unsigned int irq)
108108
}
109109
EXPORT_SYMBOL(synchronize_hardirq);
110110

111+
static void __synchronize_irq(struct irq_desc *desc)
112+
{
113+
__synchronize_hardirq(desc, true);
114+
/*
115+
* We made sure that no hardirq handler is running. Now verify that no
116+
* threaded handlers are active.
117+
*/
118+
wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
119+
}
120+
111121
/**
112122
* synchronize_irq - wait for pending IRQ handlers (on other CPUs)
113123
* @irq: interrupt number to wait for
@@ -127,16 +137,8 @@ void synchronize_irq(unsigned int irq)
127137
{
128138
struct irq_desc *desc = irq_to_desc(irq);
129139

130-
if (desc) {
131-
__synchronize_hardirq(desc, true);
132-
/*
133-
* We made sure that no hardirq handler is
134-
* running. Now verify that no threaded handlers are
135-
* active.
136-
*/
137-
wait_event(desc->wait_for_threads,
138-
!atomic_read(&desc->threads_active));
139-
}
140+
if (desc)
141+
__synchronize_irq(desc);
140142
}
141143
EXPORT_SYMBOL(synchronize_irq);
142144

@@ -1216,7 +1218,7 @@ static irqreturn_t irq_thread_fn(struct irq_desc *desc,
12161218
return ret;
12171219
}
12181220

1219-
static void wake_threads_waitq(struct irq_desc *desc)
1221+
void wake_threads_waitq(struct irq_desc *desc)
12201222
{
12211223
if (atomic_dec_and_test(&desc->threads_active))
12221224
wake_up(&desc->wait_for_threads);
@@ -1944,7 +1946,7 @@ static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
19441946
* supports it also make sure that there is no (not yet serviced)
19451947
* interrupt in flight at the hardware level.
19461948
*/
1947-
__synchronize_hardirq(desc, true);
1949+
__synchronize_irq(desc);
19481950

19491951
#ifdef CONFIG_DEBUG_SHIRQ
19501952
/*

0 commit comments

Comments
 (0)