Skip to contentSkip to conversation

media: usbtv: Remove useless locks in usbtv_video_free()

PATCH Superseded Benjamin Gaignard 3 participants 4 messages Feb 20, 2024 – Feb 28, 2024 Media (V4L/DVB)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: usbtv: Remove useless locks in usbtv_video_free()

Remove locks calls in usbtv_video_free() because are useless and may led to a deadlock as reported here:
https://syzkaller.appspot.com/x/bisect.txt?x=166dc872180000

Fixes c838530d230b ("media: media videobuf2: Be more flexible on the number of queue stored buffers")
---
 drivers/media/usb/usbtv/usbtv-video.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/media/usb/usbtv/usbtv-video.c +0 -6
index 62a583040cd4..96276358d116 100644
@@ -963,15 +963,9 @@ int usbtv_video_init(struct usbtv *usbtv)
 
 void usbtv_video_free(struct usbtv *usbtv)
 {
-	mutex_lock(&usbtv->vb2q_lock);
-	mutex_lock(&usbtv->v4l2_lock);
-
 	usbtv_stop(usbtv);
 	vb2_video_unregister_device(&usbtv->vdev);
 	v4l2_device_disconnect(&usbtv->v4l2_dev);
 
-	mutex_unlock(&usbtv->v4l2_lock);
-	mutex_unlock(&usbtv->vb2q_lock);
-
 	v4l2_device_put(&usbtv->v4l2_dev);
 }
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Benjamin Gaignard

Remove locks calls in usbtv_video_free() because are useless and may led to a deadlock as reported here:
https://syzkaller.appspot.com/x/bisect.txt?x=166dc872180000

Fixes c838530d230b ("media: media videobuf2: Be more flexible on the number of queue stored buffers")
---
 drivers/media/usb/usbtv/usbtv-video.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/media/usb/usbtv/usbtv-video.c +0 -7
index 62a583040cd4..96276358d116 100644
@@ -963,15 +963,9 @@ int usbtv_video_init(struct usbtv *usbtv)
 
 void usbtv_video_free(struct usbtv *usbtv)
 {
-       mutex_lock(&usbtv->vb2q_lock);
-       mutex_lock(&usbtv->v4l2_lock);
-
        usbtv_stop(usbtv);
        vb2_video_unregister_device(&usbtv->vdev);
        v4l2_device_disconnect(&usbtv->v4l2_dev);
 
-       mutex_unlock(&usbtv->v4l2_lock);
-       mutex_unlock(&usbtv->vb2q_lock);
-
        v4l2_device_put(&usbtv->v4l2_dev);
 }
--
Quoted 2 lines

2.40.1

From looking at the code in related vb2 functions and how the uvc driver handles the cleanup, these locks shouldn't be necessary indeed, but it would be good if someone could test this.

Best regards,
Tomasz

#2 lore raw
In reply to Benjamin Gaignard
Quoted 6 lines · Benjamin Gaignard

Remove locks calls in usbtv_video_free() because are useless and may led to a deadlock as reported here:
https://syzkaller.appspot.com/x/bisect.txt?x=166dc872180000

Fixes c838530d230b ("media: media videobuf2: Be more flexible on the number of queue stored buffers")

Hmm, the code was really always wrong, it is just that before this commit it would only fail if you did a disconnect while streaming, and now it also fails if you disconnect when you are not streaming, so it is much more noticeable now.

That should probably be explained better in the commit log. You could add a second Fixes line:

Fixes f3d27f34fdd7 ("[media] usbtv: Add driver for Fushicai USBTV007 video frame grabber")

but I am not sure how useful that is.

Quoted 9 lines
---
 drivers/media/usb/usbtv/usbtv-video.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/media/usb/usbtv/usbtv-video.c +0 -0
index 62a583040cd4..96276358d116 100644

drivers/media/usb/usbtv/usbtv-video.c int usbtv_video_init(struct usbtv *usbtv) · line 966

Quoted 7 lines
drivers/media/usb/usbtv/usbtv-video.c +0 -3
@@ -963,15 +963,9 @@ int usbtv_video_init(struct usbtv *usbtv)
 
 void usbtv_video_free(struct usbtv *usbtv)
 {
-	mutex_lock(&usbtv->vb2q_lock);
-	mutex_lock(&usbtv->v4l2_lock);
-
 	usbtv_stop(usbtv);

This call should also be dropped...

drivers/media/usb/usbtv/usbtv-video.c int usbtv_video_init(struct usbtv *usbtv) · line 967

Quoted 1 line
drivers/media/usb/usbtv/usbtv-video.c +0 -0
 	vb2_video_unregister_device(&usbtv->vdev);

...since this function will call stop_streaming if streaming is in progress, which will call usbtv_stop with all the correct locks held.

Regards,

Hans

Quoted 7 lines
drivers/media/usb/usbtv/usbtv-video.c +0 -3
 	v4l2_device_disconnect(&usbtv->v4l2_dev);
 
-	mutex_unlock(&usbtv->v4l2_lock);
-	mutex_unlock(&usbtv->vb2q_lock);
-
 	v4l2_device_put(&usbtv->v4l2_dev);
 }
Tested-by
#3 lore raw
Quoted 39 lines · Hans Verkuil
Quoted 6 lines · Benjamin Gaignard

Remove locks calls in usbtv_video_free() because are useless and may led to a deadlock as reported here:
https://syzkaller.appspot.com/x/bisect.txt?x=166dc872180000

Fixes c838530d230b ("media: media videobuf2: Be more flexible on the number of queue stored buffers")

Hmm, the code was really always wrong, it is just that before this commit it would only fail if you did a disconnect while streaming, and now it also fails if you disconnect when you are not streaming, so it is much more noticeable now.

That should probably be explained better in the commit log. You could add a second Fixes line:

Fixes f3d27f34fdd7 ("[media] usbtv: Add driver for Fushicai USBTV007 video frame grabber")

but I am not sure how useful that is.

Quoted 9 lines
---
 drivers/media/usb/usbtv/usbtv-video.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/media/usb/usbtv/usbtv-video.c +0 -0
index 62a583040cd4..96276358d116 100644

drivers/media/usb/usbtv/usbtv-video.c int usbtv_video_init(struct usbtv *usbtv) · line 966

Quoted 7 lines
drivers/media/usb/usbtv/usbtv-video.c +0 -3
@@ -963,15 +963,9 @@ int usbtv_video_init(struct usbtv *usbtv)
 
 void usbtv_video_free(struct usbtv *usbtv)
 {
-	mutex_lock(&usbtv->vb2q_lock);
-	mutex_lock(&usbtv->v4l2_lock);
-
 	usbtv_stop(usbtv);

This call should also be dropped...

Quoted 4 lines

drivers/media/usb/usbtv/usbtv-video.c int usbtv_video_init(struct usbtv *usbtv) · line 967

Quoted 1 line
drivers/media/usb/usbtv/usbtv-video.c +0 -0
 	vb2_video_unregister_device(&usbtv->vdev);

...since this function will call stop_streaming if streaming is in progress, which will call usbtv_stop with all the correct locks held.

With that change you can add my:

Regards,

Hans

Quoted 14 lines

Regards,

Hans

Quoted 7 lines
drivers/media/usb/usbtv/usbtv-video.c +0 -3
 	v4l2_device_disconnect(&usbtv->v4l2_dev);
 
-	mutex_unlock(&usbtv->v4l2_lock);
-	mutex_unlock(&usbtv->vb2q_lock);
-
 	v4l2_device_put(&usbtv->v4l2_dev);
 }