All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH usb-next v3] usb: gadget: pxa27x_udc: fix locking in req_done() on PREEMPT_RT
@ 2026-12-05 21:47 Xinsheng Zhu
  2026-12-05 21:59 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Xinsheng Zhu @ 2026-12-05 21:47 UTC (permalink / raw)
  To: linux-usb
  Cc: nkapron, gregkh, daniel, haojian.zhuang, robert.jarzmik, bigeasy,
	clrkwllms, rostedt, linux-arm-kernel, linux-rt-devel

req_done() explicitly disables local interrupts around
usb_gadget_giveback_request(). The completion callback may acquire
spinlocks, which become sleeping locks on PREEMPT_RT and cannot be
acquired with interrupts disabled.

Use spin_unlock() and spin_lock() around the giveback, following
the locking pattern introduced in commit 8d63c83d8eb9
("USB: gadget: dummy-hcd: Fix locking bug in RT-enabled kernels")
and commit 09bf21bf5249 ("usbip: Fix locking bug in RT-enabled kernels").
This keeps interrupts disabled during the giveback on non-RT kernels
without explicitly disabling them on PREEMPT_RT.

When the caller does not hold ep->lock, acquire it before
ep_del_request() to protect the request removal and status update.
Release the lock and restore the interrupt state before returning.

pxa_ep_dequeue() drops ep->lock after finding the request but
before removing it from the queue. A concurrent completion may
retire and free the request in this window. Keep ep->lock held
across the request lookup and removal.

Since the caller's saved IRQ flags are no longer needed, replace
pflags with a boolean indicating whether the caller holds ep->lock.
Update the completion helpers and their kernel-doc accordingly.

Fixes: 5e23e90f3388 ("USB: pxa27x_udc: Fix deadlocks on request queueing")
Signed-off-by: Xinsheng Zhu <xinsheng.zhu.ny@gmail.com>
---
Changes in v3:
- Describe the dequeue race in the commit message.
- Replace pflags with bool locked in req_done() and its wrappers.
- Update the corresponding kernel-doc and call sites.

v2: https://lore.kernel.org/r/20261205063000.75015-1-xinsheng.zhu.ny@gmail.com

 drivers/usb/gadget/udc/pxa27x_udc.c | 68 +++++++++++++++--------------
 1 file changed, 36 insertions(+), 32 deletions(-)

diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index 9fd079cb65c6..219833e68165 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -650,17 +650,22 @@ static void ep_del_request(struct pxa_ep *ep, struct pxa27x_request *req)
  * @ep: pxa physical endpoint
  * @req: pxa request
  * @status: usb request status sent to gadget API
- * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
+ * @locked: whether the caller holds ep->lock with spin_lock_irqsave()
  *
- * Context: ep->lock held if flags not NULL, else ep->lock released
+ * Context: ep->lock held if @locked is true, otherwise unlocked.
  *
- * Retire a pxa27x usb request. Endpoint must be locked.
+ * Retire a pxa27x usb request. Acquire the endpoint lock if needed
+ * and temporarily release it around the completion callback.
+ * Return with the original lock state restored.
  */
 static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
-	unsigned long *pflags)
+	bool locked)
 {
 	unsigned long	flags;
 
+	if (!locked)
+		spin_lock_irqsave(&ep->lock, flags);
+
 	ep_del_request(ep, req);
 	if (likely(req->req.status == -EINPROGRESS))
 		req->req.status = status;
@@ -672,37 +677,36 @@ static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
 			&req->req, status,
 			req->req.actual, req->req.length);
 
-	if (pflags)
-		spin_unlock_irqrestore(&ep->lock, *pflags);
-	local_irq_save(flags);
+	spin_unlock(&ep->lock);
 	usb_gadget_giveback_request(&req->udc_usb_ep->usb_ep, &req->req);
-	local_irq_restore(flags);
-	if (pflags)
-		spin_lock_irqsave(&ep->lock, *pflags);
+	spin_lock(&ep->lock);
+
+	if (!locked)
+		spin_unlock_irqrestore(&ep->lock, flags);
 }
 
 /**
  * ep_end_out_req - Ends endpoint OUT request
  * @ep: physical endpoint
  * @req: pxa request
- * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
+ * @locked: whether the caller holds ep->lock with spin_lock_irqsave()
  *
  * Context: ep->lock held or released (see req_done())
  *
  * Ends endpoint OUT request (completes usb request).
  */
 static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
-	unsigned long *pflags)
+	bool locked)
 {
 	inc_ep_stats_reqs(ep, !USB_DIR_IN);
-	req_done(ep, req, 0, pflags);
+	req_done(ep, req, 0, locked);
 }
 
 /**
  * ep0_end_out_req - Ends control endpoint OUT request (ends data stage)
  * @ep: physical endpoint
  * @req: pxa request
- * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
+ * @locked: whether the caller holds ep->lock with spin_lock_irqsave()
  *
  * Context: ep->lock held or released (see req_done())
  *
@@ -710,10 +714,10 @@ static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  * control endpoint into idle state
  */
 static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
-	unsigned long *pflags)
+	bool locked)
 {
 	set_ep0state(ep->dev, OUT_STATUS_STAGE);
-	ep_end_out_req(ep, req, pflags);
+	ep_end_out_req(ep, req, locked);
 	ep0_idle(ep->dev);
 }
 
@@ -721,24 +725,24 @@ static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  * ep_end_in_req - Ends endpoint IN request
  * @ep: physical endpoint
  * @req: pxa request
- * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
+ * @locked: whether the caller holds ep->lock with spin_lock_irqsave()
  *
  * Context: ep->lock held or released (see req_done())
  *
  * Ends endpoint IN request (completes usb request).
  */
 static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
-	unsigned long *pflags)
+	bool locked)
 {
 	inc_ep_stats_reqs(ep, USB_DIR_IN);
-	req_done(ep, req, 0, pflags);
+	req_done(ep, req, 0, locked);
 }
 
 /**
  * ep0_end_in_req - Ends control endpoint IN request (ends data stage)
  * @ep: physical endpoint
  * @req: pxa request
- * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
+ * @locked: whether the caller holds ep->lock with spin_lock_irqsave()
  *
  * Context: ep->lock held or released (see req_done())
  *
@@ -746,10 +750,10 @@ static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
  * control endpoint into status state
  */
 static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
-	unsigned long *pflags)
+	bool locked)
 {
 	set_ep0state(ep->dev, IN_STATUS_STAGE);
-	ep_end_in_req(ep, req, pflags);
+	ep_end_in_req(ep, req, locked);
 }
 
 /**
@@ -770,7 +774,7 @@ static void nuke(struct pxa_ep *ep, int status)
 	spin_lock_irqsave(&ep->lock, flags);
 	while (!list_empty(&ep->queue)) {
 		req = list_entry(ep->queue.next, struct pxa27x_request, queue);
-		req_done(ep, req, status, &flags);
+		req_done(ep, req, status, true);
 	}
 	spin_unlock_irqrestore(&ep->lock, flags);
 }
@@ -1108,7 +1112,7 @@ static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
 		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, false);
 			} else {
 				ep_err(ep, "got a request of %d bytes while"
 					"in state WAIT_ACK_SET_CONF_INTERF\n",
@@ -1121,12 +1125,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, false);
 			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, false);
 			break;
 		default:
 			ep_err(ep, "odd state %s to send me a request\n",
@@ -1180,9 +1184,9 @@ static int pxa_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
 		break;
 	}
 
-	spin_unlock_irqrestore(&ep->lock, flags);
 	if (!rc)
-		req_done(ep, req, -ECONNRESET, NULL);
+		req_done(ep, req, -ECONNRESET, true);
+	spin_unlock_irqrestore(&ep->lock, flags);
 	return rc;
 }
 
@@ -1993,13 +1997,13 @@ static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
 		if (req && !ep_is_full(ep))
 			completed = write_ep0_fifo(ep, req);
 		if (completed)
-			ep0_end_in_req(ep, req, NULL);
+			ep0_end_in_req(ep, req, false);
 		break;
 	case OUT_DATA_STAGE:			/* SET_DESCRIPTOR */
 		if (epout_has_pkt(ep) && req)
 			completed = read_ep0_fifo(ep, req);
 		if (completed)
-			ep0_end_out_req(ep, req, NULL);
+			ep0_end_out_req(ep, req, false);
 		break;
 	case STALL:
 		ep_write_UDCCSR(ep, UDCCSR0_FST);
@@ -2074,9 +2078,9 @@ static void handle_ep(struct pxa_ep *ep)
 
 		if (completed) {
 			if (is_in)
-				ep_end_in_req(ep, req, &flags);
+				ep_end_in_req(ep, req, true);
 			else
-				ep_end_out_req(ep, req, &flags);
+				ep_end_out_req(ep, req, true);
 		}
 	} while (completed);
 

base-commit: 9357829ac604c28510b71b154cb6630af81226ff
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 21:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 21:47 [PATCH usb-next v3] usb: gadget: pxa27x_udc: fix locking in req_done() on PREEMPT_RT Xinsheng Zhu
2026-12-05 21:59 ` sashiko-bot

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.