media: uvcvideo: Fix deadlock if uvc_status_stop is called from async_ctrl.work
Subsystems (2)
PATCH v2 1/1 media: uvcvideo: Fix deadlock if uvc_status_stop is called from async_ctrl.work
If a UVC camera has an asynchronous control, uvc_status_stop may be called from async_ctrl.work:
uvc_ctrl_status_event_work()
uvc_ctrl_status_event()
uvc_ctrl_clear_handle()
uvc_pm_put()
uvc_status_put()
uvc_status_stop()
cancel_work_sync()This will cause a deadlock, since cancel_work_sync will wait for uvc_ctrl_status_event_work to complete before returning.
Fix this by returning early from uvc_status_stop if we are currently in the work function. flush_status now remains false until uvc_status_start is called again, ensuring that uvc_ctrl_status_event_work won't resubmit the URB.
---
Changes in v2:
- Update comments with review feedback
- Use flush_work instead of cancel_work_sync since the work should never be rescheduled.
drivers/media/usb/uvc/uvc_status.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)
drivers/media/usb/uvc/uvc_status.c +18 -9
@@ -316,6 +316,15 @@ static int uvc_status_start(struct uvc_device *dev, gfp_t flags)if (!dev->int_urb)return 0 ; +/* +* If the work called uvc_status_stop it may still be running. Wait for +* it to finish before we submit the urb. +*/ +flush_work (&dev->async_ctrl.work); + +/* Clear the flush status if we were previously stopped. */ +smp_store_release (&dev->flush_status, false); +return usb_submit_urb (dev->int_urb, flags); }
@@ -336,6 +345,15 @@ static void uvc_status_stop(struct uvc_device *dev)*/ smp_store_release (&dev->flush_status, true); +/* +* If we are called from the event work function, the URB is guaranteed +* to not be in flight as it has completed and has not been resubmitted. +* There's no need to cancel the work (which would deadlock), or to kill +* the URB. +*/ +if (current_work () == &w->work) +return ; +/* * Cancel any pending asynchronous work. If any status event was queued, * process it synchronously.
@@ -354,15 +372,6 @@ static void uvc_status_stop(struct uvc_device *dev)*/ if (cancel_work_sync (&w->work))uvc_ctrl_status_event (w->chain, w->ctrl, w->data); - -/* -* From this point, there are no events on the queue and the status URB -* is dead. No events will be queued until uvc_status_start() is called. -* The barrier is needed to make sure that flush_status is visible to -* uvc_ctrl_status_event_work() when uvc_status_start() will be called -* again. -*/ -smp_store_release (&dev->flush_status, false); }int uvc_status_resume (struct uvc_device *dev)
Signature · 1 line
2.35.1.1320.gc452695387.dirty
Hi Sean
Thanks for the patch
Quoted 15 lines · Sean Anderson
If a UVC camera has an asynchronous control,
uvc_status_stopmay be called from async_ctrl.work:
uvc_ctrl_status_event_work()uvc_ctrl_status_event() uvc_ctrl_clear_handle() uvc_pm_put() uvc_status_put() uvc_status_stop() cancel_work_sync()This will cause a deadlock, since
cancel_work_syncwill wait foruvc_ctrl_status_event_workto complete before returning.
Quoted 8 lines
Fix this by returning early from
uvc_status_stopif we are currently in the work function.flush_statusnow remains false untiluvc_status_startis called again, ensuring thatuvc_ctrl_status_event_workwon't resubmit the URB.Fixes a32d9c41bdb8 ("media: uvcvideo: Make power management granular")Signed-off-by Sean Anderson <[email protected]>
with a camera with no delayed control, plus some device disconnect enumation via authorized.
Quoted 65 lines
---
Changes in v2:
- Update comments with review feedback
- Useflush_workinstead ofcancel_work_syncsince the work should never be rescheduled.drivers/media/usb/uvc/uvc_status.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)drivers/media/usb/uvc/uvc_status.c +18
-10@@ -316,6 +316,15 @@ static int uvc_status_start(struct uvc_device *dev, gfp_t flags)if (!dev->int_urb)return 0 ; +/* +* If the work called uvc_status_stop it may still be running. Wait for +* it to finish before we submit the urb. +*/ +flush_work (&dev->async_ctrl.work); + +/* Clear the flush status if we were previously stopped. */ +smp_store_release (&dev->flush_status, false); +return usb_submit_urb (dev->int_urb, flags); }@@ -336,6 +345,15 @@ static void uvc_status_stop(struct uvc_device *dev)*/ smp_store_release (&dev->flush_status, true); +/* +* If we are called from the event work function, the URB is guaranteed +* to not be in flight as it has completed and has not been resubmitted. +* There's no need to cancel the work (which would deadlock), or to kill +* the URB. +*/ +if (current_work () == &w->work) +return ; +/* * Cancel any pending asynchronous work. If any status event was queued, * process it synchronously. @@ -354,15 +372,6 @@ static void uvc_status_stop(struct uvc_device *dev)*/ if (cancel_work_sync (&w->work))uvc_ctrl_status_event (w->chain, w->ctrl, w->data); - -/* -* From this point, there are no events on the queue and the status URB -* is dead. No events will be queued until uvc_status_start() is called. -* The barrier is needed to make sure that flush_status is visible to -* uvc_ctrl_status_event_work() when uvc_status_start() will be called -* again. -*/ -smp_store_release (&dev->flush_status, false); }int uvc_status_resume (struct uvc_device *dev) --2.35.1.1320.gc452695387.dirty
Signature · 1 line
Ricardo Ribalda
Hi Sean,
Thank you for the patch.
Quoted 37 lines · Sean Anderson
If a UVC camera has an asynchronous control,
uvc_status_stopmay be called from async_ctrl.work:
uvc_ctrl_status_event_work()uvc_ctrl_status_event() uvc_ctrl_clear_handle() uvc_pm_put() uvc_status_put() uvc_status_stop() cancel_work_sync()This will cause a deadlock, since
cancel_work_syncwill wait foruvc_ctrl_status_event_workto complete before returning.Fix this by returning early from
uvc_status_stopif we are currently in the work function.flush_statusnow remains false untiluvc_status_startis called again, ensuring thatuvc_ctrl_status_event_workwon't resubmit the URB.Fixes a32d9c41bdb8 ("media: uvcvideo: Make power management granular")Signed-off-by Sean Anderson <[email protected]>---
Changes in v2:
- Update comments with review feedback
- Useflush_workinstead ofcancel_work_syncsince the work should never be rescheduled.drivers/media/usb/uvc/uvc_status.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)drivers/media/usb/uvc/uvc_status.c +0
-0
drivers/media/usb/uvc/uvc_status.c static int uvc_status_start(struct uvc_device *dev, gfp_t flags) · line 323
Quoted 8 lines
drivers/media/usb/uvc/uvc_status.c +5
-0@@ -316,6 +316,15 @@ static int uvc_status_start(struct uvc_device *dev, gfp_t flags)if (!dev->int_urb)return 0 ; +/* +* If the work called uvc_status_stop it may still be running. Wait for +* it to finish before we submit the urb. +*/ +flush_work (&dev->async_ctrl.work);
I assume the only way this can happen is for uvc_status_start() to run after the uvc_status_put() call in
uvc_ctrl_status_event_work()
uvc_ctrl_status_event()
uvc_ctrl_clear_handle()
uvc_pm_put()
uvc_status_put()returns. Is that right ? If so I'd like to capture that better in the comment, as I have a feeling we'll need to revisit this code at some point.
/*
* If the previous uvc_status_stop() call was from the async work, the
* work may still be running. Wait for it to finish before we submit the
* urb.
*/
And if that's not correct, there's something I don't get and a better comment is even more important :-)
If the above change is fine, I can update the comment when applying, and
This being said, I wonder if we could have a simpler implementation if we always stopped the work and URB when the last userspace user goes away, instead of keeping the device active until we get the asynchronous control notification.
Quoted 39 lines
drivers/media/usb/uvc/uvc_status.c +13
-9+ +/* Clear the flush status if we were previously stopped. */ +smp_store_release (&dev->flush_status, false); +return usb_submit_urb (dev->int_urb, flags); }@@ -336,6 +345,15 @@ static void uvc_status_stop(struct uvc_device *dev)*/ smp_store_release (&dev->flush_status, true); +/* +* If we are called from the event work function, the URB is guaranteed +* to not be in flight as it has completed and has not been resubmitted. +* There's no need to cancel the work (which would deadlock), or to kill +* the URB. +*/ +if (current_work () == &w->work) +return ; +/* * Cancel any pending asynchronous work. If any status event was queued, * process it synchronously. @@ -354,15 +372,6 @@ static void uvc_status_stop(struct uvc_device *dev)*/ if (cancel_work_sync (&w->work))uvc_ctrl_status_event (w->chain, w->ctrl, w->data); - -/* -* From this point, there are no events on the queue and the status URB -* is dead. No events will be queued until uvc_status_start() is called. -* The barrier is needed to make sure that flush_status is visible to -* uvc_ctrl_status_event_work() when uvc_status_start() will be called -* again. -*/ -smp_store_release (&dev->flush_status, false); }int uvc_status_resume (struct uvc_device *dev)
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 70 lines · Laurent Pinchart
Hi Sean,
Thank you for the patch.
Quoted 37 lines · Sean Anderson
If a UVC camera has an asynchronous control,
uvc_status_stopmay be called from async_ctrl.work:
uvc_ctrl_status_event_work()uvc_ctrl_status_event() uvc_ctrl_clear_handle() uvc_pm_put() uvc_status_put() uvc_status_stop() cancel_work_sync()This will cause a deadlock, since
cancel_work_syncwill wait foruvc_ctrl_status_event_workto complete before returning.Fix this by returning early from
uvc_status_stopif we are currently in the work function.flush_statusnow remains false untiluvc_status_startis called again, ensuring thatuvc_ctrl_status_event_workwon't resubmit the URB.Fixes a32d9c41bdb8 ("media: uvcvideo: Make power management granular")Signed-off-by Sean Anderson <[email protected]>---
Changes in v2:
- Update comments with review feedback
- Useflush_workinstead ofcancel_work_syncsince the work should never be rescheduled.drivers/media/usb/uvc/uvc_status.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)drivers/media/usb/uvc/uvc_status.c +0
-0drivers/media/usb/uvc/uvc_status.c static int uvc_status_start(struct uvc_device *dev, gfp_t flags) · line 323
Quoted 8 lines
drivers/media/usb/uvc/uvc_status.c +5
-0@@ -316,6 +316,15 @@ static int uvc_status_start(struct uvc_device *dev, gfp_t flags)if (!dev->int_urb)return 0 ; +/* +* If the work called uvc_status_stop it may still be running. Wait for +* it to finish before we submit the urb. +*/ +flush_work (&dev->async_ctrl.work);I assume the only way this can happen is for
uvc_status_start()to run after theuvc_status_put()call in
uvc_ctrl_status_event_work()uvc_ctrl_status_event() uvc_ctrl_clear_handle() uvc_pm_put() uvc_status_put()returns. Is that right ? If so I'd like to capture that better in the comment, as I have a feeling we'll need to revisit this code at some point.
/*
* If the previousuvc_status_stop()call was from the async work, the
* work may still be running. Wait for it to finish before we submit the
* urb.
*/
Quoted 6 lines
And if that's not correct, there's something I don't get and a better comment is even more important :-)
If the above change is fine, I can update the comment when applying, and
Reviewed-by Laurent Pinchart <[email protected]>
Fine by me
Quoted 45 lines
This being said, I wonder if we could have a simpler implementation if we always stopped the work and URB when the last userspace user goes away, instead of keeping the device active until we get the asynchronous control notification.
Quoted 39 lines
drivers/media/usb/uvc/uvc_status.c +13
-9+ +/* Clear the flush status if we were previously stopped. */ +smp_store_release (&dev->flush_status, false); +return usb_submit_urb (dev->int_urb, flags); }@@ -336,6 +345,15 @@ static void uvc_status_stop(struct uvc_device *dev)*/ smp_store_release (&dev->flush_status, true); +/* +* If we are called from the event work function, the URB is guaranteed +* to not be in flight as it has completed and has not been resubmitted. +* There's no need to cancel the work (which would deadlock), or to kill +* the URB. +*/ +if (current_work () == &w->work) +return ; +/* * Cancel any pending asynchronous work. If any status event was queued, * process it synchronously. @@ -354,15 +372,6 @@ static void uvc_status_stop(struct uvc_device *dev)*/ if (cancel_work_sync (&w->work))uvc_ctrl_status_event (w->chain, w->ctrl, w->data); - -/* -* From this point, there are no events on the queue and the status URB -* is dead. No events will be queued until uvc_status_start() is called. -* The barrier is needed to make sure that flush_status is visible to -* uvc_ctrl_status_event_work() when uvc_status_start() will be called -* again. -*/ -smp_store_release (&dev->flush_status, false); }int uvc_status_resume (struct uvc_device *dev)
Hans de Goede
maintainerHi,
Quoted 71 lines · Sean Anderson
Quoted 70 lines · Laurent Pinchart
Hi Sean,
Thank you for the patch.
Quoted 37 lines · Sean Anderson
If a UVC camera has an asynchronous control,
uvc_status_stopmay be called from async_ctrl.work:
uvc_ctrl_status_event_work()uvc_ctrl_status_event() uvc_ctrl_clear_handle() uvc_pm_put() uvc_status_put() uvc_status_stop() cancel_work_sync()This will cause a deadlock, since
cancel_work_syncwill wait foruvc_ctrl_status_event_workto complete before returning.Fix this by returning early from
uvc_status_stopif we are currently in the work function.flush_statusnow remains false untiluvc_status_startis called again, ensuring thatuvc_ctrl_status_event_workwon't resubmit the URB.Fixes a32d9c41bdb8 ("media: uvcvideo: Make power management granular")Signed-off-by Sean Anderson <[email protected]>---
Changes in v2:
- Update comments with review feedback
- Useflush_workinstead ofcancel_work_syncsince the work should never be rescheduled.drivers/media/usb/uvc/uvc_status.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)drivers/media/usb/uvc/uvc_status.c +0
-0drivers/media/usb/uvc/uvc_status.c static int uvc_status_start(struct uvc_device *dev, gfp_t flags) · line 323
Quoted 8 lines
drivers/media/usb/uvc/uvc_status.c +5
-0@@ -316,6 +316,15 @@ static int uvc_status_start(struct uvc_device *dev, gfp_t flags)if (!dev->int_urb)return 0 ; +/* +* If the work called uvc_status_stop it may still be running. Wait for +* it to finish before we submit the urb. +*/ +flush_work (&dev->async_ctrl.work);I assume the only way this can happen is for
uvc_status_start()to run after theuvc_status_put()call in
uvc_ctrl_status_event_work()uvc_ctrl_status_event() uvc_ctrl_clear_handle() uvc_pm_put() uvc_status_put()returns. Is that right ? If so I'd like to capture that better in the comment, as I have a feeling we'll need to revisit this code at some point.
/*
* If the previousuvc_status_stop()call was from the async work, the
* work may still be running. Wait for it to finish before we submit the
* urb.
*/
Quoted 8 lines
Quoted 6 lines
And if that's not correct, there's something I don't get and a better comment is even more important :-)
If the above change is fine, I can update the comment when applying, and
Reviewed-by Laurent Pinchart <[email protected]>Fine by me
Thank you everyone.
I've merged this in my local uvc/for-next branch with Laurent's updated comment and with Laurent's and Ricardo's Reviewed-by added.
I'll push this out to gitlab for CI later today.
Regards,
Hans
Quoted 47 lines
Quoted 46 lines
This being said, I wonder if we could have a simpler implementation if we always stopped the work and URB when the last userspace user goes away, instead of keeping the device active until we get the asynchronous control notification.
Quoted 39 lines
drivers/media/usb/uvc/uvc_status.c +13
-9+ +/* Clear the flush status if we were previously stopped. */ +smp_store_release (&dev->flush_status, false); +return usb_submit_urb (dev->int_urb, flags); }@@ -336,6 +345,15 @@ static void uvc_status_stop(struct uvc_device *dev)*/ smp_store_release (&dev->flush_status, true); +/* +* If we are called from the event work function, the URB is guaranteed +* to not be in flight as it has completed and has not been resubmitted. +* There's no need to cancel the work (which would deadlock), or to kill +* the URB. +*/ +if (current_work () == &w->work) +return ; +/* * Cancel any pending asynchronous work. If any status event was queued, * process it synchronously. @@ -354,15 +372,6 @@ static void uvc_status_stop(struct uvc_device *dev)*/ if (cancel_work_sync (&w->work))uvc_ctrl_status_event (w->chain, w->ctrl, w->data); - -/* -* From this point, there are no events on the queue and the status URB -* is dead. No events will be queued until uvc_status_start() is called. -* The barrier is needed to make sure that flush_status is visible to -* uvc_ctrl_status_event_work() when uvc_status_start() will be called -* again. -*/ -smp_store_release (&dev->flush_status, false); }int uvc_status_resume (struct uvc_device *dev)