Skip to content

Commit c9ec6f1

Browse files
ribaldahverkuil
authored andcommitted
media: uvcvideo: Stop stream during unregister
uvc_unregister_video() can be called asynchronously from uvc_disconnect(). If the device is still streaming when that happens, a plethora of race conditions can occur. Make sure that the device has stopped streaming before exiting this function. If the user still holds handles to the driver's file descriptors, any ioctl will return -ENODEV from the v4l2 core. This change makes uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers. This driver (and many other usb drivers) always had this problem, but it wasn't possible to easily fix this until the vb2_video_unregister_device() helper was added. So the Fixes tag points to the creation of that helper. Reviewed-by: Hans Verkuil <hverkuil@xs4all.nl> Suggested-by: Hans Verkuil <hverkuil@xs4all.nl> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> Reviewed-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Fixes: f729ef5 ("media: videobuf2-v4l2.c: add vb2_video_unregister_device helper function") Cc: stable@vger.kernel.org # 5.10.x [hverkuil: add note regarding Fixes version] Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
1 parent 2269e39 commit c9ec6f1

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

drivers/media/usb/uvc/uvc_driver.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,11 +1935,41 @@ static void uvc_unregister_video(struct uvc_device *dev)
19351935
struct uvc_streaming *stream;
19361936

19371937
list_for_each_entry(stream, &dev->streams, list) {
1938+
/* Nothing to do here, continue. */
19381939
if (!video_is_registered(&stream->vdev))
19391940
continue;
19401941

1942+
/*
1943+
* For stream->vdev we follow the same logic as:
1944+
* vb2_video_unregister_device().
1945+
*/
1946+
1947+
/* 1. Take a reference to vdev */
1948+
get_device(&stream->vdev.dev);
1949+
1950+
/* 2. Ensure that no new ioctls can be called. */
19411951
video_unregister_device(&stream->vdev);
1942-
video_unregister_device(&stream->meta.vdev);
1952+
1953+
/* 3. Wait for old ioctls to finish. */
1954+
mutex_lock(&stream->mutex);
1955+
1956+
/* 4. Stop streaming. */
1957+
uvc_queue_release(&stream->queue);
1958+
1959+
mutex_unlock(&stream->mutex);
1960+
1961+
put_device(&stream->vdev.dev);
1962+
1963+
/*
1964+
* For stream->meta.vdev we can directly call:
1965+
* vb2_video_unregister_device().
1966+
*/
1967+
vb2_video_unregister_device(&stream->meta.vdev);
1968+
1969+
/*
1970+
* Now both vdevs are not streaming and all the ioctls will
1971+
* return -ENODEV.
1972+
*/
19431973

19441974
uvc_debugfs_cleanup_stream(stream);
19451975
}

0 commit comments

Comments
 (0)