* [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
* Re: [PATCH usb-next v2] usb: gadget: pxa27x_udc: fix locking in req_done() on PREEMPT_RT
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 6:42 UTC (permalink / raw)
To: Xinsheng Zhu, linux-usb; +Cc: linux-rt-devel, linux-kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] usb: gadget: pxa27x_udc: unused pflags pointer kept as bool in
req_done()
- [Low] usb: gadget: pxa27x_udc: commit message omits pxa_ep_dequeue()
lock change
--
commit 5f58fa50b677bda0e909e4f97a6e3ac181b4d2f2
Author: Xinsheng Zhu <xinsheng.zhu.ny@gmail.com>
> usb: gadget: pxa27x_udc: fix locking in req_done() on PREEMPT_RT
>
> 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>
[Severity: Low]
Should the commit message also describe the locking change in
pxa_ep_dequeue()?
The diff keeps ep->lock held across the request lookup and the call to
req_done(), closing a race where an interrupt could complete and free the
request between lookup and removal. That change is currently only noted
under Changes in v2 below the tearline, which git am strips.
> diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
> index 9fd079cb65c6b..2c4e46533999b 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);
> }
[Severity: Low]
Can pflags be replaced now that req_done() never dereferences it?
After switching to spin_unlock() and spin_lock() around the giveback,
pflags is only used as a boolean in req_done() and its wrappers, forcing
locked callers to pass &flags as a dummy sentinel. The conditional
locking also triggers lock-context imbalance warnings in static analyzers.
In addition, ep0_end_out_req() and ep0_end_in_req() still take pflags
even though every call site passes NULL. Splitting req_done() into a
locked helper and an unlocked wrapper (and dropping the parameter from
those two helpers) would make the locking contract explicit.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205063000.75015-1-xinsheng.zhu.ny@gmail.com?part=1
^ permalink raw reply [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 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.