Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: pxa27x_udc: Keep ep->lock while queueing on ep0
@ 2026-12-04 18:51 Praneeth Balasubramanian
  0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:51 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Neill Kapron
  Cc: Daniel Mack, Haojian Zhuang, Robert Jarzmik, Petr Cvek, linux-usb,
	linux-arm-kernel, linux-kernel, Praneeth Balasubramanian, stable

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



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04 18:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:51 [PATCH] usb: gadget: pxa27x_udc: Keep ep->lock while queueing on ep0 Praneeth Balasubramanian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox