All of lore.kernel.org
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Neill Kapron <nkapron@google.com>
Cc: Daniel Mack <daniel@zonque.org>,
	Haojian Zhuang <haojian.zhuang@gmail.com>,
	Robert Jarzmik <robert.jarzmik@free.fr>,
	Petr Cvek <petrcvekcz@gmail.com>,
	linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] usb: gadget: pxa27x_udc: Keep ep->lock while queueing on ep0
Date: Fri,  4 Dec 2026 10:51:28 -0800	[thread overview]
Message-ID: <20261204185128.1688263-1-praneethb2002@gmail.com> (raw)

pxa_ep_queue() adds the request to ep->queue under ep->lock, drops the
lock, and then runs the ep0 data stage: it writes or reads the ep0 FIFO,
completes the request, or removes it again with ep_del_request() on an
unexpected state. ep_del_request() is documented as "Context: ep->lock
held". The ep0 interrupt path, handle_ep0(), works on the same queue
and FIFO. When a gadget queues on ep0 from process context, as FunctionFS
or a UVC gadget does after a delayed setup reply, interrupts are on and
the UDC interrupt can run handle_ep0() in the middle of this sequence.

The ep0 block ran under the lock until commit 5e23e90f3388 ("USB:
pxa27x_udc: Fix deadlocks on request queueing"). That commit released
the lock before it to avoid a deadlock when a completion handler queues
a new request, and noted that ep0 still needed a proper fix. req_done()
can already drop the lock around the completion when it gets the flags,
as nuke() and handle_ep() use it.

Keep ep->lock held for the ep0 block and pass &flags to the completion
helpers, so the lock is still released while the gadget's completion
runs. A 2017 RFC took the lock in the same way for the OUT data stage.

The missing lock was found by a static check that turns the locking rule
in a function's comment into a Clang context-analysis requirement on its
callers.

Link: https://lore.kernel.org/all/7670be97-1199-3c5c-8959-4b982d0fb9cf@tul.cz/
Fixes: 5e23e90f3388 ("USB: pxa27x_udc: Fix deadlocks on request queueing")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by a compiler-based check, part of research at UCSB SecLab.
    The locking rule stated in the kernel-doc comment of the called function
    was turned into a Clang context-analysis requirement (__must_hold) on that
    function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
    caller. An LLM was used only to turn the comment into a machine-readable
    rule; the report itself comes from the compiler. The patch and changelog
    were drafted with Claude Opus 5.5. I reviewed the patch and take
    responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64, clang 23,
    allmodconfig). Not runtime-tested; I do not have this hardware, and I
    have not observed the race myself.

 drivers/usb/gadget/udc/pxa27x_udc.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index df5cca43afbe..d20ccbe332d7 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -1102,13 +1102,12 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
 	_req->actual = 0;
 
 	ep_add_request(ep, req);
-	spin_unlock_irqrestore(&ep->lock, flags);
 
 	if (is_ep0(ep)) {
 		switch (dev->ep0state) {
 		case WAIT_ACK_SET_CONF_INTERF:
 			if (length == 0) {
-				ep_end_in_req(ep, req, NULL);
+				ep_end_in_req(ep, req, &flags);
 			} else {
 				ep_err(ep, "got a request of %d bytes while"
 					"in state WAIT_ACK_SET_CONF_INTERF\n",
@@ -1121,12 +1120,12 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
 		case IN_DATA_STAGE:
 			if (!ep_is_full(ep))
 				if (write_ep0_fifo(ep, req))
-					ep0_end_in_req(ep, req, NULL);
+					ep0_end_in_req(ep, req, &flags);
 			break;
 		case OUT_DATA_STAGE:
 			if ((length == 0) || !epout_has_pkt(ep))
 				if (read_ep0_fifo(ep, req))
-					ep0_end_out_req(ep, req, NULL);
+					ep0_end_out_req(ep, req, &flags);
 			break;
 		default:
 			ep_err(ep, "odd state %s to send me a request\n",
@@ -1135,7 +1134,9 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
 			rc = -EL2HLT;
 			break;
 		}
+		spin_unlock_irqrestore(&ep->lock, flags);
 	} else {
+		spin_unlock_irqrestore(&ep->lock, flags);
 		if (!recursion_detected)
 			handle_ep(ep);
 	}
-- 
2.25.1


             reply	other threads:[~2026-12-04 18:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:51 Praneeth Balasubramanian [this message]
2026-12-04 19:21 ` [PATCH] usb: gadget: pxa27x_udc: Keep ep->lock while queueing on ep0 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=20261204185128.1688263-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=daniel@zonque.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=haojian.zhuang@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=petrcvekcz@gmail.com \
    --cc=robert.jarzmik@free.fr \
    --cc=stable@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.