Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: shitao <shitao@kylinos.cn>, linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: host: max3421: fix the SPI thread teardown on remove
Date: Thu, 26 Nov 2026 12:26:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-235500@kernel.org> (raw)
In-Reply-To: <20261126121148.1021526-1-shitao@kylinos.cn>

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

  reply	other threads:[~2026-11-26 12:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 12:11 [PATCH] usb: host: max3421: fix the SPI thread teardown on remove shitao
2026-11-26 12:26 ` sashiko-bot [this message]
2026-11-26 12:52 ` Greg KH
2026-11-26 13:52 ` [PATCH v2] " shitao
2026-11-26 14:07   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-235500@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shitao@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox