drm/virtio: abort virtqueue wait on device removal to avoid hung task
Subsystems (4)
PATCH v3 1/1 drm/virtio: abort virtqueue wait on device removal to avoid hung task
virtio_gpu_queue_ctrl_sgs() and virtio_gpu_queue_cursor() use wait_event() without any abort condition when waiting for virtqueue space. If the host device stops processing commands, these waits block indefinitely inside a drm_dev_enter/exit() critical section. Since drm_dev_unplug(), which is called in device removal and system shutdown call path, blocks on synchronize_srcu() until all critical sections complete, device removal and system shutdown also hang.
Add a vqs_released flag to virtio_gpu_device and include it in the wait_event() condition. Set the flag and wake up both queues in a new virtio_gpu_release_vqs() helper, called before drm_dev_unplug() in both virtio_gpu_remove() and virtio_gpu_shutdown(). When the flag is set, the wait returns immediately and the command is aborted, following the same cleanup path as drm_dev_enter() failure.
---
Changes in v3:
- Remove Reported-by and Closes tag from commit msg because they are not
related to this fix.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Update the commit message.
- Replace wait_event_timeout() with wait_event() using a compound condition that includes a new vqs_released flag.
- Add virtio_gpu_release_vqs() helper to set the flag and wake up both queues, called before drm_dev_unplug() in remove and shutdown paths.
- Remove the hardcoded 5-second timeout. Recovery is now driven by the driver flag instead of an arbitrary timeout value.
--- drivers/gpu/drm/virtio/virtgpu_drv.c | 15 +++++++++++++++ drivers/gpu/drm/virtio/virtgpu_drv.h | 1 + drivers/gpu/drm/virtio/virtgpu_vq.c | 23 +++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.c +15 -0
@@ -119,10 +119,24 @@ static int virtio_gpu_probe(struct virtio_device *vdev)return ret; } +/* +* Release pending virtqueue waits so the drm_dev_enter/exit() critical +* sections complete before drm_dev_unplug() blocks on synchronize_srcu(). +*/ +static void virtio_gpu_release_vqs (struct drm_device *dev) +{ +struct virtio_gpu_device *vgdev = dev->dev_private; + + vgdev->vqs_released = true; +wake_up_all (&vgdev->ctrlq.ack_queue); +wake_up_all (&vgdev->cursorq.ack_queue); +} +static void virtio_gpu_remove (struct virtio_device *vdev) {struct drm_device *dev = vdev->priv; +virtio_gpu_release_vqs (dev);drm_dev_unplug (dev);drm_atomic_helper_shutdown (dev);virtio_gpu_deinit (dev);
@@ -133,6 +147,7 @@ static void virtio_gpu_shutdown(struct virtio_device *vdev) {struct drm_device *dev = vdev->priv; +virtio_gpu_release_vqs (dev);/* stop talking to the device */ drm_dev_unplug (dev); }
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
@@ -235,6 +235,7 @@ struct virtio_gpu_device {struct virtio_gpu_queue ctrlq;struct virtio_gpu_queue cursorq; +bool vqs_released;struct kmem_cache *vbufs;atomic_t pending_commands;
drivers/gpu/drm/virtio/virtgpu_vq.c +21 -2
@@ -396,7 +396,19 @@ static int virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev,if (vq->num_free < elemcnt) {spin_unlock (&vgdev->ctrlq.qlock);virtio_gpu_notify (vgdev); -wait_event (vgdev->ctrlq.ack_queue, vq->num_free >= elemcnt); +wait_event (vgdev->ctrlq.ack_queue, + vq->num_free >= elemcnt || vgdev->vqs_released); +/* +* Set by virtio_gpu_release_vqs() to unblock +* synchronize_srcu() wait in drm_dev_unplug(). +*/ +if (vgdev->vqs_released) { +if (fence && vbuf->objs) +virtio_gpu_array_unlock_resv (vbuf->objs); +free_vbuf (vgdev, vbuf); +drm_dev_exit (idx); +return -ENODEV ; + } goto again; }
@@ -566,7 +578,14 @@ static void virtio_gpu_queue_cursor(struct virtio_gpu_device *vgdev, ret =virtqueue_add_sgs (vq, sgs, outcnt,0 , vbuf,GFP_ATOMIC );if (ret == -ENOSPC ) {spin_unlock (&vgdev->cursorq.qlock); -wait_event (vgdev->cursorq.ack_queue, vq->num_free >= outcnt); +wait_event (vgdev->cursorq.ack_queue, + vq->num_free >= outcnt || vgdev->vqs_released); +/* See comment in virtio_gpu_queue_ctrl_sgs(). */ +if (vgdev->vqs_released) { +free_vbuf (vgdev, vbuf); +drm_dev_exit (idx); +return ; + }spin_lock (&vgdev->cursorq.qlock); goto retry; }else {
---
base-commit: e43ffb69e0438cddd72aaa30898b4dc446f664f8
change-id: 20260518-virtio-gpu_wait_event-5aa060754f12
Best regards,
Signature · 1 line
Ryosuke Yasuoka <[email protected]>
Quoted 132 lines · Ryosuke Yasuoka
virtio_gpu_queue_ctrl_sgs()andvirtio_gpu_queue_cursor()usewait_event()without any abort condition when waiting for virtqueue space. If the host device stops processing commands, these waits block indefinitely inside a drm_dev_enter/exit() critical section. Sincedrm_dev_unplug(), which is called in device removal and system shutdown call path, blocks onsynchronize_srcu()until all critical sections complete, device removal and system shutdown also hang.Add a
vqs_releasedflag tovirtio_gpu_deviceand include it in thewait_event()condition. Set the flag and wake up both queues in a newvirtio_gpu_release_vqs()helper, called beforedrm_dev_unplug()in bothvirtio_gpu_remove()andvirtio_gpu_shutdown(). When the flag is set, the wait returns immediately and the command is aborted, following the same cleanup path asdrm_dev_enter()failure.Signed-off-by Ryosuke Yasuoka <[email protected]>---
Changes in v3:
- Remove Reported-by and Closes tag from commit msg because they are not
related to this fix.
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Update the commit message.
- Replacewait_event_timeout()withwait_event()using a compound condition that includes a newvqs_releasedflag.
- Addvirtio_gpu_release_vqs()helper to set the flag and wake up both queues, called beforedrm_dev_unplug()in remove and shutdown paths.
- Remove the hardcoded 5-second timeout. Recovery is now driven by the driver flag instead of an arbitrary timeout value.--- drivers/gpu/drm/virtio/virtgpu_drv.c | 15 +++++++++++++++ drivers/gpu/drm/virtio/virtgpu_drv.h | 1 + drivers/gpu/drm/virtio/virtgpu_vq.c | 23 +++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-)drivers/gpu/drm/virtio/virtgpu_drv.c +15
-0@@ -119,10 +119,24 @@ static int virtio_gpu_probe(struct virtio_device *vdev)return ret; } +/* +* Release pending virtqueue waits so the drm_dev_enter/exit() critical +* sections complete before drm_dev_unplug() blocks on synchronize_srcu(). +*/ +static void virtio_gpu_release_vqs (struct drm_device *dev) +{ +struct virtio_gpu_device *vgdev = dev->dev_private; + + vgdev->vqs_released = true; +wake_up_all (&vgdev->ctrlq.ack_queue); +wake_up_all (&vgdev->cursorq.ack_queue); +} +static void virtio_gpu_remove (struct virtio_device *vdev) {struct drm_device *dev = vdev->priv; +virtio_gpu_release_vqs (dev);drm_dev_unplug (dev);drm_atomic_helper_shutdown (dev);virtio_gpu_deinit (dev);@@ -133,6 +147,7 @@ static void virtio_gpu_shutdown(struct virtio_device *vdev) {struct drm_device *dev = vdev->priv; +virtio_gpu_release_vqs (dev);/* stop talking to the device */ drm_dev_unplug (dev); }drivers/gpu/drm/virtio/virtgpu_drv.h +1
-0@@ -235,6 +235,7 @@ struct virtio_gpu_device {struct virtio_gpu_queue ctrlq;struct virtio_gpu_queue cursorq; +bool vqs_released;struct kmem_cache *vbufs;atomic_t pending_commands;drivers/gpu/drm/virtio/virtgpu_vq.c +21
-3@@ -396,7 +396,19 @@ static int virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev,if (vq->num_free < elemcnt) {spin_unlock (&vgdev->ctrlq.qlock);virtio_gpu_notify (vgdev); -wait_event (vgdev->ctrlq.ack_queue, vq->num_free >= elemcnt); +wait_event (vgdev->ctrlq.ack_queue, + vq->num_free >= elemcnt || vgdev->vqs_released); +/* +* Set by virtio_gpu_release_vqs() to unblock +* synchronize_srcu() wait in drm_dev_unplug(). +*/ +if (vgdev->vqs_released) { +if (fence && vbuf->objs) +virtio_gpu_array_unlock_resv (vbuf->objs); +free_vbuf (vgdev, vbuf); +drm_dev_exit (idx); +return -ENODEV ; + } goto again; }@@ -566,7 +578,14 @@ static void virtio_gpu_queue_cursor(struct virtio_gpu_device *vgdev, ret =virtqueue_add_sgs (vq, sgs, outcnt,0 , vbuf,GFP_ATOMIC );if (ret == -ENOSPC ) {spin_unlock (&vgdev->cursorq.qlock); -wait_event (vgdev->cursorq.ack_queue, vq->num_free >= outcnt); +wait_event (vgdev->cursorq.ack_queue, + vq->num_free >= outcnt || vgdev->vqs_released); +/* See comment in virtio_gpu_queue_ctrl_sgs(). */ +if (vgdev->vqs_released) { +free_vbuf (vgdev, vbuf); +drm_dev_exit (idx); +return ; + }spin_lock (&vgdev->cursorq.qlock); goto retry; }else { ---base-commit:
e43ffb69e0438cddd72aaa30898b4dc446f664f8
change-id: 20260518-virtio-gpu_wait_event-5aa060754f12
Quoted 1 line
Best regards,
Applied to misc-next, thanks!
Signature · 2 lines
Best regards, Dmitry