From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054291; b=zHZj3V09C2GA74EEPwBc6CDmR9YZRHDxJKggS9vtmEpZL3UGhloKqGZ0wh7sgKLz8QTj PD4tPrE7hQ6ldabs3AjSudOSzeJgBJMyXvcr+hxqP0HFuextVrTQihOhL0KDaz4X2nj25 PWmMQSgzo4gOFGkDu9hxxi5QwYaN4jcUjlhox4foc5pxP6uPdK4eL4Xont8rb5SC5HEpA DhdaKSBbPoZpqBQYrlj+q/C6/bwTdnO/klnfAFMkfKewp9fIuGQUasOVd1/Kd1CGb2k5L ru2byd0F1Uxm7kb148AcJW135kxnXmO/Z7vEskVEX8cQZHts3WKXdk8xYSX3xBxKL5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054291; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=zV10GVNVWj7+WVGKzDvQWJ0DLmfwelyXj5ruLX3x/4w=; b=knD9E9hO2QAiCb+CKQMRWSdCN1vZM8OhL84q0898jamag71AGLS66mqOu5pBkhgXMTXE AH2IyWRS5yd8FcISqV9UBLHfyxdqOE32woBSqnlcbnW+CeRrec259Il1+WS6RwLzmx4VD DGSy1lTsDEHv/WSpO/o8pnE319jg+WYdXaL/qHIG4k2zITt5ufCGDQbvgWnUCd9DaB7wT oAxLdDJrbPDoQ1RS5eAnI6hh6tm1bmHRMVv7db40zg0paleMnsVtbepYG2n1ZbzTbBHCW 97zaPIGhYOIJn2O/2QisFMv9VOSMqzgz7OacdBZDDypHl+teKCFyI+8zB7md6O+qa5A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="lUpxEx/+"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 823F4606A320; Mon, 30 Nov 2026 15:58:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lUpxEx/+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id E19431F00A21; Mon, 30 Nov 2026 15:58:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796054291; bh=zV10GVNVWj7+WVGKzDvQWJ0DLmfwelyXj5ruLX3x/4w=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=lUpxEx/+t0ltzEjunSi6VAHFwxVTwxk9UdwTkZOnqHG4unNfqA3lGPkejhresEuh0 A9rnAatM1xkUrGaGPekOmHTyUuxFI9/O1Ilw+ekteQ/aLPMVIqNRvXiIlV3n8HiDKh aZd4vpt8+PhpE4Q2RLj082AumZMMQdUX6Ic3kE24A+nUF72qQZczZktpOxrjFiwM89 VtFQxVWr+n//hmTgo52JfjEJvh+YA+YRi9veg6LxkLRsU8/LPlxjwD1MOa/hD08x7q qOPkehFkkNnEwA3rsEf4uzA1oe/+LLz+59ttiFYWahuzlEOMDDI7TaaRig3UsNW9yM fWCEinAiG+J7g== Message-ID: <951e57b9-b114-4413-a59e-8010f56b6354@kernel.org> Date: Mon, 30 Nov 2026 16:58:08 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] media: uvcvideo: Release streaming interfaces at disconnect, not last close To: johannes.schrimpf@blueye.no, laurent.pinchart@ideasonboard.com Cc: ribalda@chromium.org, hverkuil@kernel.org, linux-kernel@vger.kernel.org, mchehab@kernel.org, linux-media@vger.kernel.org, stable@vger.kernel.org References: <20261022-uvc-release-intfs-v2-1-901bc1c47cfb@blueye.no> From: Hans de Goede Content-Language: en-US, nl In-Reply-To: <20261022-uvc-release-intfs-v2-1-901bc1c47cfb@blueye.no> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, On 22-Oct-26 12:16, Johannes Schrimpf via B4 Relay wrote: > From: Johannes Schrimpf > > If a UVC camera goes through a USB reset while an application holds it > open, the camera is unusable after the reset. > > The old uvc_device releases its streaming interfaces only when its last > open file handle is closed, which unbinds them from the new uvc_device > that has claimed them in the meantime. > > Release them on disconnect and on the probe error path instead, as the > last step of uvc_unregister_video(), which is renamed to > uvc_unregister_device(). > > This also ensures that usb_driver_release_interface() is only called > with the USB device lock held. > > Fixes: c0efd232929c ("V4L/DVB (8145a): USB Video Class driver") > Cc: stable@vger.kernel.org # needs adjustments for <= 6.14: &uvc_driver.driver > Assisted-by: LLM > Reviewed-by: Ricardo Ribalda > Signed-off-by: Johannes Schrimpf Thank you for your patch. I have merged this into: https://gitlab.freedesktop.org/linux-media/users/uvc/-/commits/for-next/ Regards, Hans > --- > Changes in v2: > - Shorten the commit message; the details are now below (Ricardo). > - Move the release into uvc_unregister_video() as its last step and > rename that function to uvc_unregister_device() (Ricardo). The > release still runs after the video nodes are unregistered and before > kref_put(), in uvc_disconnect() and on the probe error path. > - Add Ricardo's Reviewed-by. > - Link to v1: https://patch.msgid.link/20261014-uvc-release-intfs-v1-1-18d6e8dfaad8@blueye.no > --- > Details from the v1 commit message: > > Mechanism: uvcvideo has no pre_reset() and post_reset() handlers, so > usb_reset_device() unbinds it and probes it again on the same struct > usb_interface objects. A file handle from before the reset keeps the > old uvc_device alive. usb_driver_release_interface() only checks that > the interface is bound to uvcvideo, not to which uvc_device, so the > old uvc_delete() unbinds the interfaces from the new device. The URBs > of a running stream then complete with -ESHUTDOWN, > uvc_video_complete() sets UVC_QUEUE_DISCONNECTED, and every following > VIDIOC_QBUF fails with -ENODEV. Nothing is logged, and the stream stays > dead until the next reset or replug. > > Reproduction, with a Sonix camera (0c45:6366) on an i.MX6Q board, > Linux 6.18.24 (linux-fslc-imx): > > 1. open() a video node of the camera and keep the file handle > 2. reset the camera with USBDEVFS_RESET > 3. stream from a video node of the re-probed device > 4. close() the file handle from step 1 > > The stream fails within a few milliseconds of the close() in 4 of 4 > runs, with one KOBJ_UNBIND uevent per VideoStreaming interface; the > VideoControl interface stays bound. A handle on either streaming > interface has the same effect. Without step 1 the stream survives. > > usb-serial splits it the same way: usb_serial_disconnect() releases the > sibling interface, and its kref release, destroy_serial(), releases > none. With no file handle open, uvc_disconnect() drops the last > reference itself, so on that path the interfaces are still released > from within uvc_disconnect(), as before. > > Tested with v1 on the same board and kernel: the reproduction with the > handle on either streaming interface or closed before the stream > starts, a USB reset without a held handle, sysfs unbind/bind of the > VideoControl interface with and without a held handle, and a second > UVC camera (MJPEG and H.264 at 1080p). The stream survives all four > streaming runs, none of the five stale-handle closes unbinds an > interface, and the close() takes 0.13-0.23 ms instead of 38-42 ms. > USB-reset recovery and the second camera work, and sysfs unbind/bind > releases and reclaims the streaming interfaces. > > Tested with v2 on the same board, built into the kernel image: the > reproduction with the handle on either streaming interface, closed > before the stream starts, and with no handle held. The stream survives > all four runs and no interface is unbound; in the two runs that close > the stale handle while streaming, 149 frames arrive in the 5 s after > the close. > > Ricardo's "media: uvcvideo: Remove refcounted cleanup" (Nov 2024) would > have moved all device cleanup into uvc_disconnect(), and was NACKed > because it fixed no concrete bug. This patch keeps the refcounted > cleanup and moves only the release of the streaming interfaces. > > Tool use: this was developed with an LLM coding assistant, Claude Code > running Claude Opus 5.5; v1 was also reviewed by GPT-6 Astra. Starting > from stream failures in our bench logs, the assistant traced the code > path, wrote the reproduction scripts, ran them and the tests above on > the device, surveyed the upstream history and other drivers, built and > checked the patch on media-committers next, and drafted the patch, the > changelog and these notes. > > Test kernel: linux-fslc-imx 6.18.24 with three local uvcvideo patches > (keep MJPEG frames recovered by MJPEG_NO_EOF, nodrop=0 by default, and a > coherent_urbs parameter). None of them touches probe, uvc_disconnect(), > uvc_delete(), the interface claim/release or uvc_video_complete(). > > Not tested: > - an unmodified uvcvideo, or any kernel other than linux-fslc-imx > 6.18.24; on media-committers next, the patch is build-tested only > (v1: gcc and clang, arm, arm64 and x86_64, W=1, sparse, smatch; v2: > gcc, arm, W=1) > - a debug kernel: the test kernel is the board's production config > (SMP, PREEMPT; no lockdep, DEBUG_ATOMIC_SLEEP, DEBUG_MUTEXES, > SLUB_DEBUG, DEBUG_PAGEALLOC or KASAN) > - unplug and replug, sysfs unbind of a VideoStreaming interface, and > the probe error path > - frame delivery after a sysfs rebind: the Sonix camera delivers no > frames after any re-probe that does not follow a USB reset, with and > without this patch, so those runs check only the binding; the > second camera was not rebound > - v4l2-compliance > - any camera other than the two on this board > --- > drivers/media/usb/uvc/uvc_driver.c | 17 ++++++++++++----- > 1 file changed, 12 insertions(+), 5 deletions(-) > > diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c > index cbc1456..bb8f0e3 100644 > --- a/drivers/media/usb/uvc/uvc_driver.c > +++ b/drivers/media/usb/uvc/uvc_driver.c > @@ -1976,7 +1976,6 @@ static void uvc_delete(struct kref *kref) > struct uvc_streaming *streaming; > > streaming = list_entry(p, struct uvc_streaming, list); > - usb_driver_release_interface(&uvc_driver, streaming->intf); > uvc_stream_delete(streaming); > } > > @@ -1992,9 +1991,9 @@ static void uvc_release(struct video_device *vdev) > } > > /* > - * Unregister the video devices. > + * Unregister the device and release the streaming interfaces. > */ > -static void uvc_unregister_video(struct uvc_device *dev) > +static void uvc_unregister_device(struct uvc_device *dev) > { > struct uvc_streaming *stream; > > @@ -2024,6 +2023,14 @@ static void uvc_unregister_video(struct uvc_device *dev) > if (media_devnode_is_registered(dev->mdev.devnode)) > media_device_unregister(&dev->mdev); > #endif > + > + /* > + * Release the streaming interfaces here, not in uvc_delete(): after a > + * USB reset, a new uvc_device may already have claimed the same > + * interfaces. > + */ > + list_for_each_entry(stream, &dev->streams, list) > + usb_driver_release_interface(&uvc_driver, stream->intf); > } > > int uvc_register_video_device(struct uvc_device *dev, > @@ -2355,7 +2362,7 @@ static int uvc_probe(struct usb_interface *intf, > return 0; > > error: > - uvc_unregister_video(dev); > + uvc_unregister_device(dev); > kref_put(&dev->ref, uvc_delete); > return ret; > } > @@ -2374,7 +2381,7 @@ static void uvc_disconnect(struct usb_interface *intf) > UVC_SC_VIDEOSTREAMING) > return; > > - uvc_unregister_video(dev); > + uvc_unregister_device(dev); > kref_put(&dev->ref, uvc_delete); > } > > > --- > base-commit: d7e7bca7169873b46e857c025c10f3336c1258d8 > change-id: 20261014-uvc-release-intfs-ff95e3766b15 > > Best regards, > -- > Johannes Schrimpf