* [PATCH] usb: host: max3421: fix the SPI thread teardown on remove
@ 2026-11-26 12:11 shitao
2026-11-26 12:26 ` sashiko-bot
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: shitao @ 2026-11-26 12:11 UTC (permalink / raw)
To: gregkh, linux-usb; +Cc: davidm, eeodqql09, linux-kernel, shitao
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@kylinos.cn>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] usb: host: max3421: fix the SPI thread teardown on remove
2026-11-26 12:11 [PATCH] usb: host: max3421: fix the SPI thread teardown on remove shitao
@ 2026-11-26 12:26 ` sashiko-bot
2026-11-26 12:52 ` Greg KH
2026-11-26 13:52 ` [PATCH v2] " shitao
2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-26 12:26 UTC (permalink / raw)
To: shitao, linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: host: max3421: free_irq() races with enable_irq() in
max3421_remove()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] usb: host: max3421-hcd: missing usb_remove_hcd() in
max3421_probe() [1]
--
commit 360a9e2d1723c072cf5873c19d8972ab271a893c
Author: shitao <shitao@kylinos.cn>
usb: host: max3421: fix the SPI thread teardown on remove
This commit drops max3421_hcd->lock around kthread_stop() in
max3421_remove() and moves free_irq() ahead of stopping the SPI thread.
> diff --git a/drivers/usb/host/max3421-hcd.c b/drivers/usb/host/max3421-hcd.c
> index 11c0d0e1f48d3..e6a447b0992de 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);
> }
[Severity: Medium]
Can max3421_spi_thread() still call enable_irq() during or after
free_irq()?
kthread_should_stop() does not become true until kthread_stop() runs after
free_irq(), and max3421_spi_thread() sleeps in spi_wr8() after checking
kthread_should_stop() before testing ENABLE_IRQ:
drivers/usb/host/max3421-hcd.c:max3421_spi_thread() {
while (!kthread_should_stop()) {
if (!i_worked) {
...
spi_wr8(hcd, MAX3421_REG_HIEN, max3421_hcd->hien);
set_current_state(TASK_INTERRUPTIBLE);
if (test_and_clear_bit(ENABLE_IRQ, &max3421_hcd->todo))
enable_irq(spi->irq);
schedule();
}
If an interrupt fires shortly before free_irq(), max3421_irq_handler()
disables spi->irq via disable_irq_nosync() and sets ENABLE_IRQ:
CPU0 max3421_spi_thread
<hardirq>
max3421_irq_handler()
set_bit(ENABLE_IRQ, ...)
disable_irq_nosync(spi->irq)
while (!kthread_should_stop())
spi_wr8(...)
max3421_remove()
free_irq(spi->irq, hcd)
kthread_stop(...)
test_and_clear_bit(ENABLE_IRQ, ...)
enable_irq(spi->irq) <-- already freed
This either calls enable_irq() on an already-freed interrupt or, if the
thread exits the loop while ENABLE_IRQ is still pending, leaves the
disable depth from disable_irq_nosync() unbalanced.
[Severity: Critical]
This problem wasn't introduced by this patch, but in max3421_probe() right
above [1], doesn't a request_irq() failure skip usb_remove_hcd()
after usb_add_hcd() has already succeeded?
drivers/usb/host/max3421-hcd.c:max3421_probe() {
...
retval = usb_add_hcd(hcd, 0, 0);
if (retval) {
dev_err(&spi->dev, "failed to add HCD\n");
goto error;
}
retval = request_irq(spi->irq, max3421_irq_handler,
IRQF_TRIGGER_LOW, "max3421", hcd);
if (retval < 0) {
dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
goto error;
}
return 0;
error:
...
if (hcd) {
kfree(max3421_hcd->tx);
kfree(max3421_hcd->rx);
if (!IS_ERR_OR_NULL(max3421_hcd->spi_thread))
kthread_stop(max3421_hcd->spi_thread);
usb_put_hcd(hcd);
}
return retval;
}
When request_irq() fails, the error path frees the transfer buffers, stops
spi_thread, and drops the reference with usb_put_hcd() while the HCD
remains registered with the USB core, leading to a use-after-free.
[1] https://sashiko.dev/#/bug/linux-234c6b25-0fde-46c3-828e-2b7e28c04092
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126121148.1021526-1-shitao@kylinos.cn?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] usb: host: max3421: fix the SPI thread teardown on remove
2026-11-26 12:11 [PATCH] usb: host: max3421: fix the SPI thread teardown on remove shitao
2026-11-26 12:26 ` sashiko-bot
@ 2026-11-26 12:52 ` Greg KH
2026-11-26 13:52 ` [PATCH v2] " shitao
2 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2026-11-26 12:52 UTC (permalink / raw)
To: shitao; +Cc: linux-usb, davidm, eeodqql09, linux-kernel
On Thu, Nov 26, 2026 at 08:11:48PM +0800, shitao wrote:
> 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@kylinos.cn>
Real / full name please.
And how did you test this?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2] usb: host: max3421: fix the SPI thread teardown on remove
2026-11-26 12:11 [PATCH] usb: host: max3421: fix the SPI thread teardown on remove shitao
2026-11-26 12:26 ` sashiko-bot
2026-11-26 12:52 ` Greg KH
@ 2026-11-26 13:52 ` shitao
2026-11-26 14:07 ` sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: shitao @ 2026-11-26 13:52 UTC (permalink / raw)
To: gregkh, linux-usb; +Cc: davidm, eeodqql09, linux-kernel, Shi Tao
From: Shi Tao <shitao@kylinos.cn>
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: Shi Tao <shitao@kylinos.cn>
---
Changes in v2:
- Full name in the author, From: and sign-off, as requested.
My name is Shi Tao (surname Shi).
- To answer "how did you test this": compile-tested only.
No MAX3421E hardware was available, so the unbind path
was not exercised at runtime.
Thanks!
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] usb: host: max3421: fix the SPI thread teardown on remove
2026-11-26 13:52 ` [PATCH v2] " shitao
@ 2026-11-26 14:07 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-26 14:07 UTC (permalink / raw)
To: shitao, linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: host: max3421: free_irq() races with enable_irq() in
max3421_remove()
--
commit 58f589c27256d9aa92eb5a12720e9958ee5eba27
Author: Shi Tao <shitao@kylinos.cn>
usb: host: max3421: fix the SPI thread teardown on remove
This commit updates max3421_remove() to free the interrupt before stopping
the SPI thread and drops the HCD spinlock around kthread_stop().
> diff --git a/drivers/usb/host/max3421-hcd.c b/drivers/usb/host/max3421-hcd.c
> index 11c0d0e1f48d3..e6a447b0992de 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);
> }
[Severity: Medium]
Can max3421_spi_thread() call enable_irq(spi->irq) after free_irq() has
already deactivated the IRQ, or exit leaving disable_irq_nosync(spi->irq)
unbalanced?
max3421_irq_handler() sets ENABLE_IRQ in max3421_hcd->todo and calls
disable_irq_nosync(spi->irq), delegating the matching enable_irq(spi->irq)
to max3421_spi_thread(). Because kthread_stop() is only called after
free_irq(), kthread_should_stop() is still false while free_irq() runs,
and max3421_spi_thread() also sleeps in spi_wr8() after checking
kthread_should_stop():
CPU0 CPU1 (max3421_spi_thread)
max3421_irq_handler()
test_and_set_bit(ENABLE_IRQ, ...)
disable_irq_nosync(spi->irq)
while (!kthread_should_stop())
if (!i_worked)
spi_wr8(..., MAX3421_REG_HIEN, ...)
max3421_remove()
free_irq(spi->irq, hcd)
if (test_and_clear_bit(ENABLE_IRQ, ...))
enable_irq(spi->irq) <-- on freed IRQ
kthread_stop(max3421_hcd->spi_thread)
Conversely, if max3421_irq_handler() sets ENABLE_IRQ and calls
disable_irq_nosync(spi->irq) just before free_irq(), and
max3421_spi_thread() then sees kthread_should_stop() at the top of its
loop and exits, the disable depth on spi->irq is never balanced by
enable_irq().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126135222.1428623-1-shitao@kylinos.cn?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-26 14:07 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 12:11 [PATCH] usb: host: max3421: fix the SPI thread teardown on remove shitao
2026-11-26 12:26 ` sashiko-bot
2026-11-26 12:52 ` Greg KH
2026-11-26 13:52 ` [PATCH v2] " shitao
2026-11-26 14:07 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox