Linux real-time development
 help / color / mirror / Atom feed
* [PATCH usb-next v2] usb: gadget: pxa27x_udc: fix locking in req_done() on PREEMPT_RT
@ 2026-12-05  6:30 Xinsheng Zhu
  2026-12-05  6:42 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Xinsheng Zhu @ 2026-12-05  6:30 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 pflags is NULL, acquire ep->lock before ep_del_request() to
protect the request removal and status update. Release the lock
and restore the interrupt state before returning.

Fixes: 5e23e90f3388 ("USB: pxa27x_udc: Fix deadlocks on request queueing")
Signed-off-by: Xinsheng Zhu <xinsheng.zhu.ny@gmail.com>
---
Changes in v2:
- Acquire ep->lock before ep_del_request().
- Keep ep->lock held across request lookup and removal in
  pxa_ep_dequeue().
- Update the kernel-doc for pflags in req_done() and its wrappers.

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

 drivers/usb/gadget/udc/pxa27x_udc.c | 39 ++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index 9fd079cb65c6..2c4e46533999 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -650,17 +650,23 @@ 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
+ * @pflags: non-NULL if the caller holds ep->lock with spin_lock_irqsave(),
+ *          NULL otherwise
  *
- * Context: ep->lock held if flags not NULL, else ep->lock released
+ * Context: ep->lock held if @pflags is non-NULL, 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)
 {
 	unsigned long	flags;
 
+	if (!pflags)
+		spin_lock_irqsave(&ep->lock, flags);
+
 	ep_del_request(ep, req);
 	if (likely(req->req.status == -EINPROGRESS))
 		req->req.status = status;
@@ -672,20 +678,20 @@ 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 (!pflags)
+		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
+ * @pflags: non-NULL if the caller holds ep->lock with spin_lock_irqsave(),
+ *          NULL otherwise
  *
  * Context: ep->lock held or released (see req_done())
  *
@@ -702,7 +708,8 @@ static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  * 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
+ * @pflags: non-NULL if the caller holds ep->lock with spin_lock_irqsave(),
+ *          NULL otherwise
  *
  * Context: ep->lock held or released (see req_done())
  *
@@ -721,7 +728,8 @@ 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
+ * @pflags: non-NULL if the caller holds ep->lock with spin_lock_irqsave(),
+ *          NULL otherwise
  *
  * Context: ep->lock held or released (see req_done())
  *
@@ -738,7 +746,8 @@ static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
  * 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
+ * @pflags: non-NULL if the caller holds ep->lock with spin_lock_irqsave(),
+ *          NULL otherwise
  *
  * Context: ep->lock held or released (see req_done())
  *
@@ -1180,9 +1189,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, &flags);
+	spin_unlock_irqrestore(&ep->lock, flags);
 	return rc;
 }
 

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  6:42 UTC | newest]

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

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