Re: [PATCH] usb: gadget: uvc: check endpoint enabled state in uvcg_video_enable()

From: Keisuke Tsukuda

Date: Tue Dec 01 2026 - 12:31:13 EST


On Fri, Nov 20, 2026 at 09:40:11 +0100, Greg Kroah-Hartman wrote:
> > + if (uvc->state == UVC_STATE_STREAMING)
> > + return 0;
> > +
> > + if (uvc->state != UVC_STATE_CONNECTED)
> > + return -ENODEV;
>
> What prevents the state from changing right after you check it?

I tested one such interleaving with the changes in this patch on a
Raspberry Pi CM4 (dwc2, 7.0.0-1024-raspi) and an Avaota A1
(musb-hdrc, 7.3.1-edge-sunxi64). Each test module had a temporary
30-second pause between uvc_function_setup_continue() and the final
uvc->state = UVC_STATE_STREAMING in uvc_v4l2_streamon(). After the host
selected streaming alt 1 and STREAMON reached the pause, a separate
userspace task wrote "disconnect" to the bound UDC's soft_connect
attribute. Both boards logged this order:

UVC_RACE_WINDOW_OPEN state=1 ep_enabled=1
uvc_function_disable()
UVC_RACE_WINDOW_BEFORE_ASSIGN state=0 ep_enabled=0
UVC_RACE_WINDOW_AFTER_ASSIGN state=2 ep_enabled=0

State 0 is DISCONNECTED and state 2 is STREAMING. The final assignment
overwrote the disconnect state while the endpoint stayed disabled.
That assignment predates this patch, but the new early STREAMING return
would report success on a later VIDIOC_STREAMON while the stale state
persists. I have not measured that later ioctl.

The composite disconnect path calls uvc_function_disable() under
cdev->lock, while the ioctl holds video->mutex. The latter does not
serialize the state check and final transition with disconnect.

The pause forced this ordering and is not part of the proposed change.
The state-transition trials did not enter the original NULL ep->desc
path or measure the race's natural frequency. The test modules also
carried a separate isochronous payload bound fix for the 320x180 YUYV
stream; it does not change this state transition.

Josef Bacik's separate [PATCH 08/19] also addresses this Oops:
https://lore.kernel.org/r/20261027-llm-usb-v1-8-b9751ddb9c7c@xxxxxxxxxxxxxxxxxxx

Thanks,
Keisuke