linux-stable/kernel/irq
Thomas Pfaff 61808e4089 genirq: Synchronize interrupt thread startup
commit 8707898e22 upstream.

A kernel hang can be observed when running setserial in a loop on a kernel
with force threaded interrupts. The sequence of events is:

   setserial
     open("/dev/ttyXXX")
       request_irq()
     do_stuff()
      -> serial interrupt
         -> wake(irq_thread)
	      desc->threads_active++;
     close()
       free_irq()
         kthread_stop(irq_thread)
     synchronize_irq() <- hangs because desc->threads_active != 0

The thread is created in request_irq() and woken up, but does not get on a
CPU to reach the actual thread function, which would handle the pending
wake-up. kthread_stop() sets the should stop condition which makes the
thread immediately exit, which in turn leaves the stale threads_active
count around.

This problem was introduced with commit 519cc8652b, which addressed a
interrupt sharing issue in the PCIe code.

Before that commit free_irq() invoked synchronize_irq(), which waits for
the hard interrupt handler and also for associated threads to complete.

To address the PCIe issue synchronize_irq() was replaced with
__synchronize_hardirq(), which only waits for the hard interrupt handler to
complete, but not for threaded handlers.

This was done under the assumption, that the interrupt thread already
reached the thread function and waits for a wake-up, which is guaranteed to
be handled before acting on the stop condition. The problematic case, that
the thread would not reach the thread function, was obviously overlooked.

Make sure that the interrupt thread is really started and reaches
thread_fn() before returning from __setup_irq().

This utilizes the existing wait queue in the interrupt descriptor. The
wait queue is unused for non-shared interrupts. For shared interrupts the
usage might cause a spurious wake-up of a waiter in synchronize_irq() or the
completion of a threaded handler might cause a spurious wake-up of the
waiter for the ready flag. Both are harmless and have no functional impact.

[ tglx: Amended changelog ]

Fixes: 519cc8652b ("genirq: Synchronize only with single thread on free_irq()")
Signed-off-by: Thomas Pfaff <tpfaff@pcs.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Marc Zyngier <maz@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/552fe7b4-9224-b183-bb87-a8f36d335690@pcs.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-05-12 12:30:06 +02:00
..
affinity.c genirq/affinity: Consider that CPUs on nodes can be unbalanced 2022-04-20 09:34:20 +02:00
autoprobe.c genirq: Delay deactivation in free_irq() 2019-07-03 10:12:28 +02:00
chip.c genirq: Provide IRQCHIP_AFFINITY_PRE_STARTUP 2021-08-10 10:59:20 +02:00
cpuhotplug.c genirq/cpuhotplug: Demote debug printk to KERN_DEBUG 2021-08-24 09:16:20 +02:00
debug.h Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/pmladek/printk 2018-02-01 13:36:15 -08:00
debugfs.c Merge branch 'irq/qcom-pdc-wakeup' into irq/irqchip-next 2020-10-06 11:28:03 +01:00
devres.c genirq/devres: Use struct_size() in devm_kzalloc() 2019-04-16 21:54:03 +02:00
dummychip.c irq: Fix typos in comments 2021-03-22 04:23:14 +01:00
generic-chip.c genirq/generic_chip: Use struct_size() in kzalloc() 2021-08-10 15:35:20 +02:00
handle.c genirq: Add a IRQF_NO_DEBUG flag 2021-05-17 20:01:35 +02:00
internals.h genirq: Synchronize interrupt thread startup 2022-05-12 12:30:06 +02:00
ipi.c genirq: Fix kernel-doc warnings in pm.c, msi.c and ipi.c 2021-08-11 14:33:35 +02:00
irq_sim.c The usual updates from the irq departement: 2021-04-26 09:43:16 -07:00
irqdesc.c genirq: Synchronize interrupt thread startup 2022-05-12 12:30:06 +02:00
irqdomain.c irqchip fixes for 5.15, take #1 2021-09-24 14:11:04 +02:00
Kconfig powerpc: Move the use of irq_domain_add_nomap() behind a config option 2021-06-10 13:09:17 +01:00
Makefile genirq/timings: Add selftest for circular array 2019-06-12 10:47:04 +02:00
manage.c genirq: Synchronize interrupt thread startup 2022-05-12 12:30:06 +02:00
matrix.c genirq/matrix: Fix kernel doc warnings for irq_matrix_alloc_managed() 2021-08-10 22:50:07 +02:00
migration.c genirq: Fix typos and misspellings in comments 2021-03-16 15:08:29 +01:00
msi.c PCI/MSI: Move non-mask check back into low level accessors 2021-11-18 19:17:14 +01:00
pm.c genirq: Fix kernel-doc warnings in pm.c, msi.c and ipi.c 2021-08-11 14:33:35 +02:00
proc.c genirq: Improve "hwirq" output in /proc and /sys/ 2021-07-30 23:07:31 +02:00
resend.c genirq: Fix typos and misspellings in comments 2021-03-16 15:08:29 +01:00
settings.h genirq: Add a IRQF_NO_DEBUG flag 2021-05-17 20:01:35 +02:00
spurious.c genirq: Reduce irqdebug cacheline bouncing 2021-04-10 13:35:54 +02:00
timings.c Updates to the interrupt core and driver subsystems: 2021-08-30 14:38:37 -07:00