* [PATCH] usb: gadget: lpc32xx_udc: Fix a race in lpc32xx_get_frame()
@ 2026-09-22 6:53 Ginger Li
2026-11-20 14:20 ` Greg KH
0 siblings, 1 reply; 2+ messages in thread
From: Ginger Li @ 2026-09-22 6:53 UTC (permalink / raw)
To: vz, piotr.wojtaszczyk; +Cc: linux-usb, linux-arm-kernel, linux-kernel
lpc32xx_get_frame() tests udc->clocked before it takes udc->lock, but
udc->clocked is cleared by udc_clk_set() with the lock held, for example from
lpc32xx_vbus_session() when the session is disconnected, which also gates the
UDC clock.
When the test passes on a stale value, udc_get_current_frame() then reads UDC
registers whose clock has already been turned off. Check udc->clocked inside
the critical section so that the state can not change between the check and
the register access.
Fixes: 24a28e428351 ("USB: gadget driver for LPC32xx")
Signed-off-by: Ginger Li <ginger.jzllee@gmail.com>
---
drivers/usb/gadget/udc/lpc32xx_udc.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c
--- a/drivers/usb/gadget/udc/lpc32xx_udc.c
+++ b/drivers/usb/gadget/udc/lpc32xx_udc.c
@@ -2403,10 +2403,12 @@ static int lpc32xx_get_frame(struct usb_gadget *gadget
unsigned long flags;
struct lpc32xx_udc *udc = to_udc(gadget);
- if (!udc->clocked)
- return -EINVAL;
+ spin_lock_irqsave(&udc->lock, flags);
- spin_lock_irqsave(&udc->lock, flags);
+ if (!udc->clocked) {
+ spin_unlock_irqrestore(&udc->lock, flags);
+ return -EINVAL;
+ }
frame = (int) udc_get_current_frame(udc);
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] usb: gadget: lpc32xx_udc: Fix a race in lpc32xx_get_frame()
2026-09-22 6:53 [PATCH] usb: gadget: lpc32xx_udc: Fix a race in lpc32xx_get_frame() Ginger Li
@ 2026-11-20 14:20 ` Greg KH
0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2026-11-20 14:20 UTC (permalink / raw)
To: Ginger Li
Cc: vz, piotr.wojtaszczyk, linux-usb, linux-arm-kernel, linux-kernel
On Tue, Sep 22, 2026 at 02:53:57PM +0800, Ginger Li wrote:
> lpc32xx_get_frame() tests udc->clocked before it takes udc->lock, but
> udc->clocked is cleared by udc_clk_set() with the lock held, for example from
> lpc32xx_vbus_session() when the session is disconnected, which also gates the
> UDC clock.
>
> When the test passes on a stale value, udc_get_current_frame() then reads UDC
> registers whose clock has already been turned off. Check udc->clocked inside
> the critical section so that the state can not change between the check and
> the register access.
>
> Fixes: 24a28e428351 ("USB: gadget driver for LPC32xx")
> Signed-off-by: Ginger Li <ginger.jzllee@gmail.com>
> ---
> drivers/usb/gadget/udc/lpc32xx_udc.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
How was this found and tested?
>
> diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c
> --- a/drivers/usb/gadget/udc/lpc32xx_udc.c
> +++ b/drivers/usb/gadget/udc/lpc32xx_udc.c
> @@ -2403,10 +2403,12 @@ static int lpc32xx_get_frame(struct usb_gadget *gadget
> unsigned long flags;
> struct lpc32xx_udc *udc = to_udc(gadget);
>
> - if (!udc->clocked)
> - return -EINVAL;
> + spin_lock_irqsave(&udc->lock, flags);
guard()?
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 14:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-22 6:53 [PATCH] usb: gadget: lpc32xx_udc: Fix a race in lpc32xx_get_frame() Ginger Li
2026-11-20 14:20 ` Greg KH
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox