Skip to contentSkip to conversation

uvcvideo: Attempt N to land UVC race conditions fixes

PATCH Superseded Ricardo Ribalda 6 participants 15 messages Mar 27, 2024 – Jun 17, 2024

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover uvcvideo: Attempt N to land UVC race conditions fixes

Back in 2020 Guenter published a set of patches to fix some race conditions in UVC:
https://lore.kernel.org/all/[email protected]/

That kind of race conditions are not only seen in UVC, but are a common seen in almost all the kernel, so this is what it was decided back then that we should try to fix them at higher levels.

After that. A lot of video_is_registered() were added to the core:

```
ribalda@alco:~/work/linux$ git grep is_registered drivers/media/v4l2-core/
drivers/media/v4l2-core/v4l2-compat-ioctl32.c:  if (!video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev)) {
drivers/media/v4l2-core/v4l2-dev.c:             if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (!video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (vdev == NULL || !video_is_registered(vdev)) {
drivers/media/v4l2-core/v4l2-dev.c:             if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (!vdev || !video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-ioctl.c:   if (!video_is_registered(vfd)) {
drivers/media/v4l2-core/v4l2-subdev.c:  if (video_is_registered(vdev)) {
```

And recently Sakari is trying to land:
https://lore.kernel.org/linux-media/[email protected]/

Which will make obsolete a lot off (all?) of the video_is_registered() checks in Guenter's patches.

Besides those checks, there were some other valid races fixed in his patches.

This patchset tries to fix the races still present in our code.

Thanks!

---
Changes in v4: Thanks Sergey and Guenter
- Fix typos
- Move location of mutex_init
- Split patch to make the suspend change explicit
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3: Thanks Hans!
- Stop streaming during uvc_unregister()
- Refactor the uvc_status code
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Actually send the series to the ML an not only to individuals.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Ricardo Ribalda (4):
media: uvcvideo: stop stream during unregister
media: uvcvideo: Refactor the status irq API
media: uvcvideo: Avoid race condition during unregister
media: uvcvideo: Exit early if there is not int_urb

 drivers/media/usb/uvc/uvc_driver.c | 24 ++++++++-------
 drivers/media/usb/uvc/uvc_status.c | 62 +++++++++++++++++++++++++++++++++++---
 drivers/media/usb/uvc/uvc_v4l2.c   | 22 ++++----------
 drivers/media/usb/uvc/uvcvideo.h   | 10 +++---
 4 files changed, 83 insertions(+), 35 deletions(-)

---
base-commit: b14257abe7057def6127f6fb2f14f9adc8acabdb
change-id: 20230309-guenter-mini-89861b084ef1

Best regards,

Signature · 1 line
Ricardo Ribalda <[email protected]>
#1 lore raw

PATCH v4 1/4 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 happen.

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 make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/usb/uvc/uvc_driver.c +11 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
 		if (!video_is_registered(&stream->vdev))
 			continue;
 
+		/*
+		 * Serialize other access to the stream.
+		 */
+		mutex_lock(&stream->mutex);
+		uvc_queue_streamoff(&stream->queue, stream->type);
 		video_unregister_device(&stream->vdev);
 		video_unregister_device(&stream->meta.vdev);
+		mutex_unlock(&stream->mutex);
+
+		/*
+		 * Now the vdev is not streaming and all the ioctls will
+		 * return -ENODEV
+		 */
 
 		uvc_debugfs_cleanup_stream(stream);
 	}
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
#2 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Quoted 25 lines · Ricardo Ribalda

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 happen.

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 make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/usb/uvc/uvc_driver.c +0 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
 		if (!video_is_registered(&stream->vdev))

drivers/media/usb/uvc/uvc_driver.c static void uvc_unregister_video(struct uvc_device *dev) · line 1921

Quoted 10 lines
drivers/media/usb/uvc/uvc_driver.c +6 -0
 			continue;
 
+		/*
+		 * Serialize other access to the stream.
+		 */
+		mutex_lock(&stream->mutex);
+		uvc_queue_streamoff(&stream->queue, stream->type);
 		video_unregister_device(&stream->vdev);
 		video_unregister_device(&stream->meta.vdev);
+		mutex_unlock(&stream->mutex);

This sequence isn't fool proof. You have to follow the same sequence as vb2_video_unregister_device(): take a reference to the video device, then unregister, then take the stream mutex and call vb2_queue_release for each queue. Finally unlock the mutex and call put_device.

Doing the video_unregister_device first ensures no new ioctls can be called on that device node. Taking the mutex ensures that any still running ioctls will finish (since it will sleep until they release the mutex), and then you can release the queue.

This does require that you call get_device before calling video_unregister_device, otherwise everything might be released at that point.

Instead of vb2_queue_release() you might have to call uvc_queue_streamoff, but note that there are two queues here: video and meta. The code above just calls streamoff for the video device.

For the meta device I think you can just use vb2_video_unregister_device() directly, since that sets vdev->queue and vdev->queue.lock to the correct values. That would just leave the video device where you need to do this manually.

Regards,

Hans

Quoted 9 lines
drivers/media/usb/uvc/uvc_driver.c +5 -0
+
+		/*
+		 * Now the vdev is not streaming and all the ioctls will
+		 * return -ENODEV
+		 */
 
 		uvc_debugfs_cleanup_stream(stream);
 	}
#3 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Hi Hans,

Quoted 29 lines · Hans Verkuil
Quoted 27 lines · Ricardo Ribalda

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 happen.

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 make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/usb/uvc/uvc_driver.c +0 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
              if (!video_is_registered(&stream->vdev))
                      continue;
 
Quoted 13 lines

drivers/media/usb/uvc/uvc_driver.c static void uvc_unregister_video(struct uvc_device *dev) · line 1921

Quoted 8 lines
drivers/media/usb/uvc/uvc_driver.c +6 -0
+             /*
+              * Serialize other access to the stream.
+              */
+             mutex_lock(&stream->mutex);
+             uvc_queue_streamoff(&stream->queue, stream->type);
              video_unregister_device(&stream->vdev);
              video_unregister_device(&stream->meta.vdev);
+             mutex_unlock(&stream->mutex);

This sequence isn't fool proof. You have to follow the same sequence as vb2_video_unregister_device(): take a reference to the video device, then unregister, then take the stream mutex and call vb2_queue_release for each queue. Finally unlock the mutex and call put_device.

vb2_queue_release() will run when the userspace releases the file handle that owns the vb2 queue [1], so we shouldn't really need to call it here.

[1] https://elixir.bootlin.com/linux/v6.9.3/source/drivers/media/usb/uvc/uvc_v4l2.c#L655

Quoted 5 lines

Doing the video_unregister_device first ensures no new ioctls can be called on that device node. Taking the mutex ensures that any still running ioctls will finish (since it will sleep until they release the mutex), and then you can release the queue.

Actually isn't the only missing thing in the code basically ensuring that any ioctl currently being executed finishes? Why is the streamoff or queue release needed?

Best regards,
Tomasz

Quoted 27 lines

This does require that you call get_device before calling video_unregister_device, otherwise everything might be released at that point.

Instead of vb2_queue_release() you might have to call uvc_queue_streamoff, but note that there are two queues here: video and meta. The code above just calls streamoff for the video device.

For the meta device I think you can just use vb2_video_unregister_device() directly, since that sets vdev->queue and vdev->queue.lock to the correct values. That would just leave the video device where you need to do this manually.

Regards,

Hans

Quoted 9 lines
drivers/media/usb/uvc/uvc_driver.c +5 -0
+
+             /*
+              * Now the vdev is not streaming and all the ioctls will
+              * return -ENODEV
+              */
 
              uvc_debugfs_cleanup_stream(stream);
      }
#4 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Quoted 53 lines · Tomasz Figa

Hi Hans,

Quoted 29 lines · Hans Verkuil
Quoted 27 lines · Ricardo Ribalda

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 happen.

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 make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/usb/uvc/uvc_driver.c +0 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
              if (!video_is_registered(&stream->vdev))
                      continue;
 
Quoted 13 lines

drivers/media/usb/uvc/uvc_driver.c static void uvc_unregister_video(struct uvc_device *dev) · line 1921

Quoted 8 lines
drivers/media/usb/uvc/uvc_driver.c +6 -0
+             /*
+              * Serialize other access to the stream.
+              */
+             mutex_lock(&stream->mutex);
+             uvc_queue_streamoff(&stream->queue, stream->type);
              video_unregister_device(&stream->vdev);
              video_unregister_device(&stream->meta.vdev);
+             mutex_unlock(&stream->mutex);

This sequence isn't fool proof. You have to follow the same sequence as vb2_video_unregister_device(): take a reference to the video device, then unregister, then take the stream mutex and call vb2_queue_release for each queue. Finally unlock the mutex and call put_device.

vb2_queue_release() will run when the userspace releases the file handle that owns the vb2 queue [1], so we shouldn't really need to call it here.

[1] https://elixir.bootlin.com/linux/v6.9.3/source/drivers/media/usb/uvc/uvc_v4l2.c#L655

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Doing the video_unregister_device first ensures no new ioctls can be called on that device node. Taking the mutex ensures that any still running ioctls will finish (since it will sleep until they release the mutex), and then you can release the queue.

Actually isn't the only missing thing in the code basically ensuring that any ioctl currently being executed finishes? Why is the streamoff or queue release needed?

See below...

Quoted 16 lines

Best regards,
Tomasz

Quoted 12 lines

This does require that you call get_device before calling video_unregister_device, otherwise everything might be released at that point.

Instead of vb2_queue_release() you might have to call uvc_queue_streamoff, but note that there are two queues here: video and meta. The code above just calls streamoff for the video device.

For the meta device I think you can just use vb2_video_unregister_device() directly, since that sets vdev->queue and vdev->queue.lock to the correct values. That would just leave the video device where you need to do this manually.

Ideally uvc should just use centralized locking (i.e. set vdev->queue.lock) for the video node, just like it does for the meta device.

If that was the case, then it can just call vb2_video_unregister_device(). The vb2_video_unregister_device helper was added to ensure that no ioctls are running, and then streaming is stopped and the queue is released.

While it is true that the queue is released automatically when the last user closes the filehandle, it can get complicated if the application has a file handle open when the device is unregistered (usually due to it being unplugged). Without that call to vb2_video_unregister_device() the queue is still active, and especially if you also have subdevices that are still in streaming mode, it is hard to make sure nothing is still using the hardware.

vb2_video_unregister_device() provides a clean way of ensuring that when the device is unregistered all streaming is stopped and the vb2 queue is released.

After that the only file operation that can be used without returning an error is close().

Since uvc uses its own locking for the video device, it has to do this manually. It is probably enough to ensure no ioctls are running since uvc doesn't have subdevices, but I think it makes sense to stop streaming and release the queue when unregistering the device, it is weird to postpone that until the last fh is closed.

Regards,

Hans

Quoted 15 lines
Quoted 15 lines

Regards,

Hans

Quoted 9 lines
drivers/media/usb/uvc/uvc_driver.c +5 -0
+
+             /*
+              * Now the vdev is not streaming and all the ioctls will
+              * return -ENODEV
+              */
 
              uvc_debugfs_cleanup_stream(stream);
      }
#5 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Quoted 83 lines · Hans Verkuil
Quoted 53 lines · Tomasz Figa

Hi Hans,

Quoted 29 lines · Hans Verkuil
Quoted 27 lines · Ricardo Ribalda

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 happen.

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 make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/usb/uvc/uvc_driver.c +0 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
              if (!video_is_registered(&stream->vdev))
                      continue;
 
Quoted 13 lines

drivers/media/usb/uvc/uvc_driver.c static void uvc_unregister_video(struct uvc_device *dev) · line 1921

Quoted 8 lines
drivers/media/usb/uvc/uvc_driver.c +6 -0
+             /*
+              * Serialize other access to the stream.
+              */
+             mutex_lock(&stream->mutex);
+             uvc_queue_streamoff(&stream->queue, stream->type);
              video_unregister_device(&stream->vdev);
              video_unregister_device(&stream->meta.vdev);
+             mutex_unlock(&stream->mutex);

This sequence isn't fool proof. You have to follow the same sequence as vb2_video_unregister_device(): take a reference to the video device, then unregister, then take the stream mutex and call vb2_queue_release for each queue. Finally unlock the mutex and call put_device.

vb2_queue_release() will run when the userspace releases the file handle that owns the vb2 queue [1], so we shouldn't really need to call it here.

[1] https://elixir.bootlin.com/linux/v6.9.3/source/drivers/media/usb/uvc/uvc_v4l2.c#L655

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

Doing the video_unregister_device first ensures no new ioctls can be called on that device node. Taking the mutex ensures that any still running ioctls will finish (since it will sleep until they release the mutex), and then you can release the queue.

Actually isn't the only missing thing in the code basically ensuring that any ioctl currently being executed finishes? Why is the streamoff or queue release needed?

See below...

Quoted 16 lines

Best regards,
Tomasz

Quoted 12 lines

This does require that you call get_device before calling video_unregister_device, otherwise everything might be released at that point.

Instead of vb2_queue_release() you might have to call uvc_queue_streamoff, but note that there are two queues here: video and meta. The code above just calls streamoff for the video device.

For the meta device I think you can just use vb2_video_unregister_device() directly, since that sets vdev->queue and vdev->queue.lock to the correct values. That would just leave the video device where you need to do this manually.

Quoted 6 lines

Ideally uvc should just use centralized locking (i.e. set vdev->queue.lock) for the video node, just like it does for the meta device.

If that was the case, then it can just call vb2_video_unregister_device(). The vb2_video_unregister_device helper was added to ensure that no ioctls are running, and then streaming is stopped and the queue is released.

Yes, it would be as simple as that if it used standard locking, but since it does its own stuff, I'm not very confident that the same logic as in vb2_video_unregister_device() would work fine for it as well.

Quoted 11 lines

While it is true that the queue is released automatically when the last user closes the filehandle, it can get complicated if the application has a file handle open when the device is unregistered (usually due to it being unplugged). Without that call to vb2_video_unregister_device() the queue is still active, and especially if you also have subdevices that are still in streaming mode, it is hard to make sure nothing is still using the hardware.

vb2_video_unregister_device() provides a clean way of ensuring that when the device is unregistered all streaming is stopped and the vb2 queue is released.

Quoted 8 lines

After that the only file operation that can be used without returning an error is close().

Since uvc uses its own locking for the video device, it has to do this manually. It is probably enough to ensure no ioctls are running since uvc doesn't have subdevices, but I think it makes sense to stop streaming and release the queue when unregistering the device, it is weird to postpone that until the last fh is closed.

My comment comes from a concern that releasing objects and changing internal state in the release callback may break some assumptions in the operations that will follow once the lock is released, e.g. close(), leading to some kind of double frees or dereferencing freed objects. Therefore just preventing new opens and setting the video_device as unregistered could be potentially a safer option until the driver is converted to standard locking. (Is there even a plan to do that?)

Best regards,
Tomasz

Quoted 23 lines

Regards,

Hans

Quoted 15 lines
Quoted 15 lines

Regards,

Hans

Quoted 9 lines
drivers/media/usb/uvc/uvc_driver.c +5 -0
+
+             /*
+              * Now the vdev is not streaming and all the ioctls will
+              * return -ENODEV
+              */
 
              uvc_debugfs_cleanup_stream(stream);
      }
#6 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

In reply to Ricardo Ribalda

Hi Ricardo,

Quoted 11 lines · Ricardo Ribalda

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 happen.

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.

Quoted 9 lines

This change make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

First of all, thanks for the patch. I have a question about the problem being fixed here.

Could you point out a specific race condition example that could happen without this change?
From what I see in __video_do_ioctl((), no ioctls would be executed anymore after the video node is unregistered.
Since the device is not present either, what asynchronous code paths could be still triggered?

[1] https://elixir.bootlin.com/linux/latest/source/drivers/media/v4l2-core/v4l2-ioctl.c#L3023

Best regards,
Tomasz

Quoted 28 lines
drivers/media/usb/uvc/uvc_driver.c +11 -1
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
                if (!video_is_registered(&stream->vdev))
                        continue;
 
+               /*
+                * Serialize other access to the stream.
+                */
+               mutex_lock(&stream->mutex);
+               uvc_queue_streamoff(&stream->queue, stream->type);
                video_unregister_device(&stream->vdev);
                video_unregister_device(&stream->meta.vdev);
+               mutex_unlock(&stream->mutex);
+
+               /*
+                * Now the vdev is not streaming and all the ioctls will
+                * return -ENODEV
+                */
 
                uvc_debugfs_cleanup_stream(stream);
        }
 
--

2.44.0.396.g6e790dbe36-goog

maintainer
#7 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Hi Tomasz,

Quoted 24 lines · Tomasz Figa
Quoted 11 lines · Ricardo Ribalda

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 happen.

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.

Quoted 8 lines

This change make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

First of all, thanks for the patch. I have a question about the problem being fixed here.

Quoted 6 lines

Could you point out a specific race condition example that could happen without this change?
From what I see in __video_do_ioctl((), no ioctls would be executed anymore after the video node is unregistered.
Since the device is not present either, what asynchronous code paths could be still triggered?

I believe the issue is that some ioctls can be in progress while the device is unregistered. I'll let Ricardo confirm.

I've tried to explain multiple times before that this should be handled in the V4L2 core, ideally with fixes in the cdev core too, as this issue affects all cdev drivers. I've pointed to related patches that have been posted for the cdev core. They need to be wrapped in V4L2 functions to make them easier to use for drivers. If we don't want to depend on those cdev changes, we can implement the "wrappers" with fixes limited to V4L2 until the cdev changes get merged (assuming someone would resurect them).

Quoted 26 lines

[1] https://elixir.bootlin.com/linux/latest/source/drivers/media/v4l2-core/v4l2-ioctl.c#L3023

Quoted 24 lines
drivers/media/usb/uvc/uvc_driver.c +11 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
                if (!video_is_registered(&stream->vdev))
                        continue;
 
+               /*
+                * Serialize other access to the stream.
+                */
+               mutex_lock(&stream->mutex);
+               uvc_queue_streamoff(&stream->queue, stream->type);
                video_unregister_device(&stream->vdev);
                video_unregister_device(&stream->meta.vdev);
+               mutex_unlock(&stream->mutex);
+
+               /*
+                * Now the vdev is not streaming and all the ioctls will
+                * return -ENODEV
+                */
 
                uvc_debugfs_cleanup_stream(stream);
        }
Signature · 3 lines
Regards,

Laurent Pinchart
#8 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Quoted 37 lines · Laurent Pinchart

Hi Tomasz,

Quoted 24 lines · Tomasz Figa
Quoted 11 lines · Ricardo Ribalda

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 happen.

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.

Quoted 8 lines

This change make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

First of all, thanks for the patch. I have a question about the problem being fixed here.

Quoted 6 lines

Could you point out a specific race condition example that could happen without this change?
From what I see in __video_do_ioctl((), no ioctls would be executed anymore after the video node is unregistered.
Since the device is not present either, what asynchronous code paths could be still triggered?

I believe the issue is that some ioctls can be in progress while the device is unregistered. I'll let Ricardo confirm.

Quoted 8 lines

I've tried to explain multiple times before that this should be handled in the V4L2 core, ideally with fixes in the cdev core too, as this issue affects all cdev drivers. I've pointed to related patches that have been posted for the cdev core. They need to be wrapped in V4L2 functions to make them easier to use for drivers. If we don't want to depend on those cdev changes, we can implement the "wrappers" with fixes limited to V4L2 until the cdev changes get merged (assuming someone would resurect them).

But there is already a V4L2 wrapper for that: vb2_video_unregister_device(). It safely unregisters the video device, ensuring any in-flight ioctls finish first, and it stops any video streaming.

The only reason it can't be used in uvc for the video stream is that that vb2_queue doesn't set the lock field (i.e. uses the core V4L2 serialization mechanism). The metadata stream *does* set that field, so for that stream this function can be used.

While it would be nice to have this fixed in the cdev core part, that will take very long, and we have a perfectly fine V4L2 helper for this already.

Regards,

Hans

Quoted 28 lines
Quoted 27 lines

[1] https://elixir.bootlin.com/linux/latest/source/drivers/media/v4l2-core/v4l2-ioctl.c#L3023

Quoted 24 lines
drivers/media/usb/uvc/uvc_driver.c +11 -0
index bbd90123a4e76..17fc945c8deb6 100644
@@ -1911,8 +1911,19 @@ static void uvc_unregister_video(struct uvc_device *dev)
                if (!video_is_registered(&stream->vdev))
                        continue;
 
+               /*
+                * Serialize other access to the stream.
+                */
+               mutex_lock(&stream->mutex);
+               uvc_queue_streamoff(&stream->queue, stream->type);
                video_unregister_device(&stream->vdev);
                video_unregister_device(&stream->meta.vdev);
+               mutex_unlock(&stream->mutex);
+
+               /*
+                * Now the vdev is not streaming and all the ioctls will
+                * return -ENODEV
+                */
 
                uvc_debugfs_cleanup_stream(stream);
        }
#9 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Hi Hans,

Quoted 51 lines · Hans Verkuil
Quoted 37 lines · Laurent Pinchart

Hi Tomasz,

Quoted 24 lines · Tomasz Figa
Quoted 11 lines · Ricardo Ribalda

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 happen.

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.

Quoted 8 lines

This change make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

First of all, thanks for the patch. I have a question about the problem being fixed here.

Quoted 6 lines

Could you point out a specific race condition example that could happen without this change?
From what I see in __video_do_ioctl((), no ioctls would be executed anymore after the video node is unregistered.
Since the device is not present either, what asynchronous code paths could be still triggered?

I believe the issue is that some ioctls can be in progress while the device is unregistered. I'll let Ricardo confirm.

Quoted 8 lines

I've tried to explain multiple times before that this should be handled in the V4L2 core, ideally with fixes in the cdev core too, as this issue affects all cdev drivers. I've pointed to related patches that have been posted for the cdev core. They need to be wrapped in V4L2 functions to make them easier to use for drivers. If we don't want to depend on those cdev changes, we can implement the "wrappers" with fixes limited to V4L2 until the cdev changes get merged (assuming someone would resurect them).

But there is already a V4L2 wrapper for that: vb2_video_unregister_device(). It safely unregisters the video device, ensuring any in-flight ioctls finish first, and it stops any video streaming.

Quoted 7 lines

The only reason it can't be used in uvc for the video stream is that that vb2_queue doesn't set the lock field (i.e. uses the core V4L2 serialization mechanism). The metadata stream *does* set that field, so for that stream this function can be used.

While it would be nice to have this fixed in the cdev core part, that will take very long, and we have a perfectly fine V4L2 helper for this already.

It might not take *that* long to get there but it won't happen unless
someone does it. Dan Williams posted a patch but his immediate problem was
solved differently so there it remains <URL:https://lore.kernel.org/all/161117153248.2853729.2452425259045172318.stgit@dwillia2-desk3.amr.corp.intel.com/>.

In the meantime vb_video_unregister_device() would seem to be the best choice.

Signature · 3 lines
Regards,

Sakari Ailus
#10 lore raw

Re: [PATCH v4 1/4] media: uvcvideo: stop stream during unregister

Quoted 62 lines · Sakari Ailus

Hi Hans,

Quoted 51 lines · Hans Verkuil
Quoted 37 lines · Laurent Pinchart

Hi Tomasz,

Quoted 24 lines · Tomasz Figa
Quoted 11 lines · Ricardo Ribalda

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 happen.

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.

Quoted 8 lines

This change make uvc more consistent with the rest of the v4l2 drivers using the vb2_fop_* and vb2_ioctl_* helpers.

---
 drivers/media/usb/uvc/uvc_driver.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

First of all, thanks for the patch. I have a question about the problem being fixed here.

Quoted 6 lines

Could you point out a specific race condition example that could happen without this change?
From what I see in __video_do_ioctl((), no ioctls would be executed anymore after the video node is unregistered.
Since the device is not present either, what asynchronous code paths could be still triggered?

I believe the issue is that some ioctls can be in progress while the device is unregistered. I'll let Ricardo confirm.

Quoted 8 lines

I've tried to explain multiple times before that this should be handled in the V4L2 core, ideally with fixes in the cdev core too, as this issue affects all cdev drivers. I've pointed to related patches that have been posted for the cdev core. They need to be wrapped in V4L2 functions to make them easier to use for drivers. If we don't want to depend on those cdev changes, we can implement the "wrappers" with fixes limited to V4L2 until the cdev changes get merged (assuming someone would resurect them).

But there is already a V4L2 wrapper for that: vb2_video_unregister_device(). It safely unregisters the video device, ensuring any in-flight ioctls finish first, and it stops any video streaming.

Quoted 7 lines

The only reason it can't be used in uvc for the video stream is that that vb2_queue doesn't set the lock field (i.e. uses the core V4L2 serialization mechanism). The metadata stream *does* set that field, so for that stream this function can be used.

While it would be nice to have this fixed in the cdev core part, that will take very long, and we have a perfectly fine V4L2 helper for this already.

Quoted 7 lines

It might not take *that* long to get there but it won't happen unless
someone does it. Dan Williams posted a patch but his immediate problem was
solved differently so there it remains <URL:https://lore.kernel.org/all/161117153248.2853729.2452425259045172318.stgit@dwillia2-desk3.amr.corp.intel.com/>.

In the meantime vb_video_unregister_device() would seem to be the best choice.

Also note that even if these cdev improvements ever land, that doesn't remove the need for vb2_video_unregister_device, since that also explicitly stops any streaming that is in progress. Which is something you really want to do when the device is unbound.

Regards,

Hans

#11 lore raw

PATCH v4 2/4 media: uvcvideo: Refactor the status irq API

In reply to Ricardo Ribalda

There are two different use-cases of uvc_status():
- adding/removing a user when the camera is open/closed
- stopping/starting when the camera is suspended/resumed

Make the API reflect these two use-cases and move all the refcounting and locking logic to the uvc_status.c file.

---
 drivers/media/usb/uvc/uvc_driver.c | 13 ++-------
 drivers/media/usb/uvc/uvc_status.c | 55 ++++++++++++++++++++++++++++++++++++--
 drivers/media/usb/uvc/uvc_v4l2.c   | 22 +++++----------
 drivers/media/usb/uvc/uvcvideo.h   | 10 ++++---
 4 files changed, 67 insertions(+), 33 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +2 -11
index 17fc945c8deb6..b579644ac0745 100644
@@ -2116,7 +2116,6 @@ static int uvc_probe(struct usb_interface *intf,
 	INIT_LIST_HEAD(&dev->streams);
 	kref_init(&dev->ref);
 	atomic_set(&dev->nmappings, 0);
-	mutex_init(&dev->lock);
 
 	dev->udev = usb_get_dev(udev);
 	dev->intf = usb_get_intf(intf);
@@ -2282,10 +2281,7 @@ static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
 	/* Controls are cached on the fly so they don't need to be saved. */
 	if (intf->cur_altsetting->desc.bInterfaceSubClass ==
 	    UVC_SC_VIDEOCONTROL) {
-		mutex_lock(&dev->lock);
-		if (dev->users)
-			uvc_status_stop(dev);
-		mutex_unlock(&dev->lock);
+		uvc_status_suspend(dev);
 		return 0;
 	}
 
@@ -2316,12 +2312,7 @@ static int __uvc_resume(struct usb_interface *intf, int reset)
 				return ret;
 		}
 
-		mutex_lock(&dev->lock);
-		if (dev->users)
-			ret = uvc_status_start(dev, GFP_NOIO);
-		mutex_unlock(&dev->lock);
-
-		return ret;
+		return uvc_status_resume(dev);
 	}
 
 	list_for_each_entry(stream, &dev->streams, list) {
drivers/media/usb/uvc/uvc_status.c +53 -2

80 lines not on this page · raw message

drivers/media/usb/uvc/uvc_v4l2.c +6 -16
index f4988f03640ae..97c5407f66032 100644
@@ -628,20 +628,13 @@ static int uvc_v4l2_open(struct file *file)
 		return -ENOMEM;
 	}
 
-	mutex_lock(&stream->dev->lock);
-	if (stream->dev->users == 0) {
-		ret = uvc_status_start(stream->dev, GFP_KERNEL);
-		if (ret < 0) {
-			mutex_unlock(&stream->dev->lock);
-			usb_autopm_put_interface(stream->dev->intf);
-			kfree(handle);
-			return ret;
-		}
+	ret = uvc_status_get(stream->dev);
+	if (ret) {
+		usb_autopm_put_interface(stream->dev->intf);
+		kfree(handle);
+		return ret;
 	}
 
-	stream->dev->users++;
-	mutex_unlock(&stream->dev->lock);
-
 	v4l2_fh_init(&handle->vfh, &stream->vdev);
 	v4l2_fh_add(&handle->vfh);
 	handle->chain = stream->chain;
@@ -670,10 +663,7 @@ static int uvc_v4l2_release(struct file *file)
 	kfree(handle);
 	file->private_data = NULL;
 
-	mutex_lock(&stream->dev->lock);
-	if (--stream->dev->users == 0)
-		uvc_status_stop(stream->dev);
-	mutex_unlock(&stream->dev->lock);
+	uvc_status_put(stream->dev);
 
 	usb_autopm_put_interface(stream->dev->intf);
 	return 0;
drivers/media/usb/uvc/uvcvideo.h +6 -4
index 6fb0a78b1b009..00b600eb058cc 100644
@@ -555,8 +555,6 @@ struct uvc_device {
 
 	const struct uvc_device_info *info;
 
-	struct mutex lock;		/* Protects users */
-	unsigned int users;
 	atomic_t nmappings;
 
 	/* Video control interface */
@@ -578,6 +576,8 @@ struct uvc_device {
 	struct usb_host_endpoint *int_ep;
 	struct urb *int_urb;
 	struct uvc_status *status;
+	struct mutex status_lock; /* Protects status_users */
+	unsigned int status_users;
 	bool flush_status;
 
 	struct input_dev *input;
@@ -744,8 +744,10 @@ int uvc_register_video_device(struct uvc_device *dev,
 int uvc_status_init(struct uvc_device *dev);
 void uvc_status_unregister(struct uvc_device *dev);
 void uvc_status_cleanup(struct uvc_device *dev);
-int uvc_status_start(struct uvc_device *dev, gfp_t flags);
-void uvc_status_stop(struct uvc_device *dev);
+int uvc_status_resume(struct uvc_device *dev);
+void uvc_status_suspend(struct uvc_device *dev);
+int uvc_status_get(struct uvc_device *dev);
+void uvc_status_put(struct uvc_device *dev);
 
 /* Controls */
 extern const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited;
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
#12 lore raw

PATCH v4 3/4 media: uvcvideo: Avoid race condition during unregister

In reply to Ricardo Ribalda

The control events are handled asynchronously by the driver. Once the control event are handled, the urb is re-submitted.

If we simply kill the urb, there is a chance that a control event is waiting to be processed, which will re-submit the urb after the device is disconnected.

uvc_status_suspend() flushes the async controls and stops the urb in a correct manner.

---
 drivers/media/usb/uvc/uvc_status.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/usb/uvc/uvc_status.c +1 -1
index 375a95dd30110..8fd8250110e2f 100644
@@ -294,7 +294,7 @@ int uvc_status_init(struct uvc_device *dev)
 
 void uvc_status_unregister(struct uvc_device *dev)
 {
-	usb_kill_urb(dev->int_urb);
+	uvc_status_suspend(dev);
 	uvc_input_unregister(dev);
 }
 
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
#13 lore raw

PATCH v4 4/4 media: uvcvideo: Exit early if there is not int_urb

In reply to Ricardo Ribalda

If there is no int_urb there is no need to do a clean stop.

Also we avoid calling usb_kill_urb(NULL). It is properly handled by the usb framework, but it is not polite.

Now that we are at it, fix the code style in uvc_status_start() for consistency.

---
 drivers/media/usb/uvc/uvc_status.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/media/usb/uvc/uvc_status.c +4 -1
index 8fd8250110e2f..9108522beea6a 100644
@@ -308,7 +308,7 @@ static int __uvc_status_start(struct uvc_device *dev, gfp_t flags)
 {
 	lockdep_assert_held(&dev->status_lock);
 
-	if (dev->int_urb == NULL)
+	if (!dev->int_urb)
 		return 0;
 
 	return usb_submit_urb(dev->int_urb, flags);
@@ -320,6 +320,9 @@ static void __uvc_status_stop(struct uvc_device *dev)
 
 	lockdep_assert_held(&dev->status_lock);
 
+	if (!dev->int_urb)
+		return;
+
 	/*
 	 * Prevent the asynchronous control handler from requeing the URB. The
 	 * barrier is needed so the flush_status change is visible to other
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
Reviewed-by on the v4 series
#14 lore raw
In reply to Ricardo Ribalda
Quoted 32 lines · Ricardo Ribalda

Back in 2020 Guenter published a set of patches to fix some race conditions in UVC:
https://lore.kernel.org/all/[email protected]/

That kind of race conditions are not only seen in UVC, but are a common seen in almost all the kernel, so this is what it was decided back then that we should try to fix them at higher levels.

After that. A lot of video_is_registered() were added to the core:

```
ribalda@alco:~/work/linux$ git grep is_registered drivers/media/v4l2-core/
drivers/media/v4l2-core/v4l2-compat-ioctl32.c:  if (!video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev)) {
drivers/media/v4l2-core/v4l2-dev.c:             if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (!video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (vdev == NULL || !video_is_registered(vdev)) {
drivers/media/v4l2-core/v4l2-dev.c:             if (video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-dev.c:     if (!vdev || !video_is_registered(vdev))
drivers/media/v4l2-core/v4l2-ioctl.c:   if (!video_is_registered(vfd)) {
drivers/media/v4l2-core/v4l2-subdev.c:  if (video_is_registered(vdev)) {
```

And recently Sakari is trying to land:
https://lore.kernel.org/linux-media/[email protected]/

Which will make obsolete a lot off (all?) of the video_is_registered() checks in Guenter's patches.

Quoted 8 lines

Besides those checks, there were some other valid races fixed in his patches.

This patchset tries to fix the races still present in our code.

Thanks!