[PATCH] usb: host: max3421: fix the SPI thread teardown on remove
From: shitao
Date: Thu Nov 26 2026 - 07:12:13 EST
max3421_remove() stops the SPI thread with the HCD spinlock held:
spin_lock_irqsave(&max3421_hcd->lock, flags);
kthread_stop(max3421_hcd->spi_thread);
spin_unlock_irqrestore(&max3421_hcd->lock, flags);
kthread_stop() waits for the thread to exit, so it must not be called
under a spinlock. Worse, the thread takes the same lock in its main
loop (in max3421_urb_done() and friends), so it can never finish its
current iteration while remove() holds the lock, and remove() ends up
waiting forever.
The interrupt is freed after the thread is stopped, but its handler
wakes up that very thread, so an interrupt arriving after the thread
has exited calls wake_up_process() on a task_struct that may already
have been freed.
Free the interrupt first and stop the thread without the spinlock.
The thread checks kthread_should_stop() at the top of its loop, so it
exits without re-enabling the freed interrupt.
Fixes: 2d53139f31626 ("Add support for using a MAX3421E chip as a host driver.")
Signed-off-by: shitao <shitao@xxxxxxxxxx>
---
drivers/usb/host/max3421-hcd.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/drivers/usb/host/max3421-hcd.c b/drivers/usb/host/max3421-hcd.c
index 73e76d0e6973..20e458809ac6 100644
--- a/drivers/usb/host/max3421-hcd.c
+++ b/drivers/usb/host/max3421-hcd.c
@@ -1936,21 +1936,16 @@ max3421_remove(struct spi_device *spi)
{
struct max3421_hcd *max3421_hcd;
struct usb_hcd *hcd;
- unsigned long flags;
max3421_hcd = spi_get_drvdata(spi);
hcd = max3421_to_hcd(max3421_hcd);
usb_remove_hcd(hcd);
- spin_lock_irqsave(&max3421_hcd->lock, flags);
+ free_irq(spi->irq, hcd);
kthread_stop(max3421_hcd->spi_thread);
- spin_unlock_irqrestore(&max3421_hcd->lock, flags);
-
- free_irq(spi->irq, hcd);
-
usb_put_hcd(hcd);
}
--
2.25.1