Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jaegyun Lee" <dreams200260@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: f_printer: take dev->lock on copy_from_user() failure
Date: Sun, 29 Nov 2026 14:08:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-239685@kernel.org> (raw)
In-Reply-To: <20261129135824.450263-1-dreams200260@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] usb: gadget: f_printer: interrupt re-entrancy deadlock in
  tx_complete() [1]
--

commit 22f7b45ca310736e9c1b3990fe322c25d2ea2100
Author: Jaegyun Lee <dreams200260@gmail.com>

usb: gadget: f_printer: take dev->lock on copy_from_user() failure

This commit takes dev->lock around list_add() when copy_from_user() fails
in printer_write() to prevent list corruption against tx_complete().

Link: https://lore.kernel.org/all/sashiko-outbox-206117@kernel.org/

> diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
> index 29c8150a7d0ab..e817d8c854afa 100644
> --- a/drivers/usb/gadget/function/f_printer.c
> +++ b/drivers/usb/gadget/function/f_printer.c
> @@ -665,7 +665,9 @@ printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
>  		spin_unlock_irqrestore(&dev->lock, flags);
>  
>  		if (copy_from_user(req->buf, buf, size)) {
> +			spin_lock_irqsave(&dev->lock, flags);
>  			list_add(&req->list, &dev->tx_reqs);
> +			spin_unlock_irqrestore(&dev->lock, flags);
>  			mutex_unlock(&dev->lock_printer_io);
>  			return bytes_copied;
>  		}

[Severity: High]
This problem wasn't introduced by this patch, but can tx_complete()
deadlock on dev->lock [1] when a UDC invokes completion callbacks
with local interrupts enabled?

drivers/usb/gadget/function/f_printer.c:tx_complete() {
	...
	spin_lock(&dev->lock);
	/* Take the request struct off the active list and put it on the
	 * free list.
	 */
	list_del_init(&req->list);
	list_add(&req->list, &dev->tx_reqs);
	...
	spin_unlock(&dev->lock);
}

Because tx_complete() uses plain spin_lock(&dev->lock), a hardware
interrupt on the same CPU can re-enter the driver and spin forever on
dev->lock:

tx_complete()
  spin_lock(&dev->lock)
  <hardirq>
    ...
      spin_lock_irqsave(&dev->lock, flags)  <-- spins forever

Using spin_lock_irqsave() and spin_unlock_irqrestore() in tx_complete()
would avoid this lockup.

[1] https://sashiko.dev/#/bug/linux-5e89bb2a-e067-4f0b-92c6-b7efb6d7b5ea

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129135824.450263-1-dreams200260@gmail.com?part=1

      parent reply	other threads:[~2026-11-29 14:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 13:58 [PATCH] usb: gadget: f_printer: take dev->lock on copy_from_user() failure Jaegyun Lee
2026-11-29 14:06 ` Greg Kroah-Hartman
2026-11-29 14:08 ` sashiko-bot [this message]

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-239685@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dreams200260@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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