Skip to contentSkip to conversation

drm/virtio: Destroy obj_restore_lock on the final drm_dev_put()

PATCH In review Abhin Parekadan Jose 3 participants 7 messages Sep 29 – Oct 18

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/virtio: Destroy obj_restore_lock on the final drm_dev_put()

virtio_gpu_deinit() calls mutex_destroy() on obj_restore_lock, but deinit runs from virtio_gpu_remove() at unbind or PCI remove, and GEM objects can outlive that. An open /dev/fb0 keeps the fbdev buffer alive, and the fbdev DRM client holds a drm_device reference. When the fd is finally closed, the buffer is freed and takes the destroyed lock:

fb_release()

    drm_fbdev_shmem_fb_destroy()
      drm_client_buffer_delete()
        virtio_gpu_free_object()
          virtio_gpu_remove_from_restore_list()
            mutex_lock(&vgdev->obj_restore_lock)

DEBUG_LOCKS_WARN_ON(lock->magic != lock)

  WARNING: kernel/locking/mutex.c:625 at __mutex_lock+0xf2c/0x12ec

virtio_gpu_deinit() should only do hardware teardown in this case related virtio, its queues and others. Software state that GEM callbacks still use belongs in virtio_gpu_release(), which runs on the final drm_dev_put().

There are two equivalent ways to tie the mutex lifetime to the drm_device:

1) Move mutex_destroy() from virtio_gpu_deinit() to the end of virtio_gpu_release(). This is what this patch does. It is the smallest change and keeps the teardown next to the rest of the final-put software cleanup.

2) Initialise the lock with drmm_mutex_init() (checking its return value) and drop mutex_destroy() from virtio_gpu_deinit(), so DRM's managed release destroys it. This also covers the virtio_gpu_init() error path, where release returns early because dev_private is NULL. Skipping mutex_destroy() there is harmless, since it only matters for mutex debugging.

Either fix removes the WARN. Option 1 is sent as the smaller change, and option 2 can be done instead if preferred.

Reproduced on arm64 QEMU (-device virtio-gpu-pci) with DEBUG_MUTEXES and PROVE_LOCKING: open /dev/fb0, write 1 to /sys/bus/pci/devices/0000:00:01.0/remove, then close the fd. With this patch the WARN is gone, and rebinding brings back /dev/fb0 and /dev/dri/card0.

Fixes 54a970048296 ("drm/virtio: Add support for saving and restoring virtio_gpu_objects")
Reported-by [email protected]
Assisted-by LLM
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +2 -1
index 1d4d3bf46a20..0ec755c35050 100644
@@ -354,7 +354,6 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	virtio_reset_device(vgdev->vdev);
 	virtio_gpu_reclaim_vbufs(vgdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
-	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
@@ -370,6 +369,8 @@ void virtio_gpu_release(struct drm_device *dev)
 
 	if (vgdev->has_host_visible)
 		drm_mm_takedown(&vgdev->host_visible_mm);
+
+	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)

base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e

Signature · 1 line
2.51.1
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

virtio_gpu_deinit() calls mutex_destroy() on obj_restore_lock, but deinit runs from virtio_gpu_remove() at unbind or PCI remove, and GEM objects can outlive that. An open /dev/fb0 keeps the fbdev buffer alive, and the fbdev DRM client holds a drm_device reference. When the fd is finally closed, the buffer is freed and takes the destroyed lock:

fb_release()

    drm_fbdev_shmem_fb_destroy()
      drm_client_buffer_delete()
        virtio_gpu_free_object()
          virtio_gpu_remove_from_restore_list()
            mutex_lock(&vgdev->obj_restore_lock)
Quoted 8 lines

DEBUG_LOCKS_WARN_ON(lock->magic != lock)

  WARNING: kernel/locking/mutex.c:625 at __mutex_lock+0xf2c/0x12ec
[ ... ]
Fixes 54a970048296 ("drm/virtio: Add support for saving and restoring virtio_gpu_objects")
Reported-by [email protected]
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Abhin Parekadan Jose

Gentle ping on this fix for the syzbot report.

Dmitry, Gerd, do you have a preference between moving mutex_destroy() to virtio_gpu_release() (this patch) and using drmm_mutex_init()? I can send a v2 with the other option if you prefer it.

Thanks,
Abhin

Quoted 80 lines · Abhin Parekadan Jose

virtio_gpu_deinit() calls mutex_destroy() on obj_restore_lock, but deinit runs from virtio_gpu_remove() at unbind or PCI remove, and GEM objects can outlive that. An open /dev/fb0 keeps the fbdev buffer alive, and the fbdev DRM client holds a drm_device reference. When the fd is finally closed, the buffer is freed and takes the destroyed lock:

fb_release()

    drm_fbdev_shmem_fb_destroy()
      drm_client_buffer_delete()
        virtio_gpu_free_object()
          virtio_gpu_remove_from_restore_list()
            mutex_lock(&vgdev->obj_restore_lock)

DEBUG_LOCKS_WARN_ON(lock->magic != lock)

  WARNING: kernel/locking/mutex.c:625 at __mutex_lock+0xf2c/0x12ec

virtio_gpu_deinit() should only do hardware teardown in this case related virtio, its queues and others. Software state that GEM callbacks still use belongs in virtio_gpu_release(), which runs on the final drm_dev_put().

There are two equivalent ways to tie the mutex lifetime to the drm_device:

1) Move mutex_destroy() from virtio_gpu_deinit() to the end of virtio_gpu_release(). This is what this patch does. It is the smallest change and keeps the teardown next to the rest of the final-put software cleanup.

2) Initialise the lock with drmm_mutex_init() (checking its return value) and drop mutex_destroy() from virtio_gpu_deinit(), so DRM's managed release destroys it. This also covers the virtio_gpu_init() error path, where release returns early because dev_private is NULL. Skipping mutex_destroy() there is harmless, since it only matters for mutex debugging.

Either fix removes the WARN. Option 1 is sent as the smaller change, and option 2 can be done instead if preferred.

Reproduced on arm64 QEMU (-device virtio-gpu-pci) with DEBUG_MUTEXES and PROVE_LOCKING: open /dev/fb0, write 1 to /sys/bus/pci/devices/0000:00:01.0/remove, then close the fd. With this patch the WARN is gone, and rebinding brings back /dev/fb0 and /dev/dri/card0.

Fixes 54a970048296 ("drm/virtio: Add support for saving and restoring virtio_gpu_objects")
Reported-by [email protected]
Assisted-by LLM
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +2 -1
index 1d4d3bf46a20..0ec755c35050 100644
@@ -354,7 +354,6 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	virtio_reset_device(vgdev->vdev);
 	virtio_gpu_reclaim_vbufs(vgdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
-	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
@@ -370,6 +369,8 @@ void virtio_gpu_release(struct drm_device *dev)
 
 	if (vgdev->has_host_visible)
 		drm_mm_takedown(&vgdev->host_visible_mm);
+
+	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)

base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e

Signature · 1 line
2.51.1
maintainer
#3 lore raw
Quoted 78 lines · Abhin Parekadan Jose

Gentle ping on this fix for the syzbot report.

Dmitry, Gerd, do you have a preference between moving mutex_destroy() to virtio_gpu_release() (this patch) and using drmm_mutex_init()? I can send a v2 with the other option if you prefer it.

Thanks,
Abhin

Quoted 68 lines · Abhin Parekadan Jose

virtio_gpu_deinit() calls mutex_destroy() on obj_restore_lock, but deinit runs from virtio_gpu_remove() at unbind or PCI remove, and GEM objects can outlive that. An open /dev/fb0 keeps the fbdev buffer alive, and the fbdev DRM client holds a drm_device reference. When the fd is finally closed, the buffer is freed and takes the destroyed lock:

fb_release()

    drm_fbdev_shmem_fb_destroy()
      drm_client_buffer_delete()
        virtio_gpu_free_object()
          virtio_gpu_remove_from_restore_list()
            mutex_lock(&vgdev->obj_restore_lock)

DEBUG_LOCKS_WARN_ON(lock->magic != lock)

  WARNING: kernel/locking/mutex.c:625 at __mutex_lock+0xf2c/0x12ec

virtio_gpu_deinit() should only do hardware teardown in this case related virtio, its queues and others. Software state that GEM callbacks still use belongs in virtio_gpu_release(), which runs on the final drm_dev_put().

There are two equivalent ways to tie the mutex lifetime to the drm_device:

1) Move mutex_destroy() from virtio_gpu_deinit() to the end of virtio_gpu_release(). This is what this patch does. It is the smallest change and keeps the teardown next to the rest of the final-put software cleanup.

2) Initialise the lock with drmm_mutex_init() (checking its return value) and drop mutex_destroy() from virtio_gpu_deinit(), so DRM's managed release destroys it. This also covers the virtio_gpu_init() error path, where release returns early because dev_private is NULL. Skipping mutex_destroy() there is harmless, since it only matters for mutex debugging.

Either fix removes the WARN. Option 1 is sent as the smaller change, and option 2 can be done instead if preferred.

Reproduced on arm64 QEMU (-device virtio-gpu-pci) with DEBUG_MUTEXES and PROVE_LOCKING: open /dev/fb0, write 1 to /sys/bus/pci/devices/0000:00:01.0/remove, then close the fd. With this patch the WARN is gone, and rebinding brings back /dev/fb0 and /dev/dri/card0.

Fixes 54a970048296 ("drm/virtio: Add support for saving and restoring virtio_gpu_objects")
Reported-by [email protected]
Assisted-by LLM
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -1
index 1d4d3bf46a20..0ec755c35050 100644
@@ -354,7 +354,6 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	virtio_reset_device(vgdev->vdev);
 	virtio_gpu_reclaim_vbufs(vgdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
-	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
@@ -370,6 +369,8 @@ void virtio_gpu_release(struct drm_device *dev)
 
Quoted 7 lines

drivers/gpu/drm/virtio/virtgpu_kms.c void virtio_gpu_release(struct drm_device *dev) · line 376

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +2 -0
 	if (vgdev->has_host_visible)
 		drm_mm_takedown(&vgdev->host_visible_mm);
+
+	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)

Calling virtio_gpu_deinit() from virtio_gpu_release() might be a better option

Signature · 2 lines
Best regards,
Dmitry
#4 lore raw
Quoted 85 lines · Dmitry Osipenko
Quoted 84 lines · Abhin Parekadan Jose

Gentle ping on this fix for the syzbot report.

Dmitry, Gerd, do you have a preference between moving mutex_destroy() to virtio_gpu_release() (this patch) and using drmm_mutex_init()? I can send a v2 with the other option if you prefer it.

Thanks,
Abhin

Quoted 74 lines · Abhin Parekadan Jose

virtio_gpu_deinit() calls mutex_destroy() on obj_restore_lock, but deinit runs from virtio_gpu_remove() at unbind or PCI remove, and GEM objects can outlive that. An open /dev/fb0 keeps the fbdev buffer alive, and the fbdev DRM client holds a drm_device reference. When the fd is finally closed, the buffer is freed and takes the destroyed lock:

fb_release()

    drm_fbdev_shmem_fb_destroy()
      drm_client_buffer_delete()
        virtio_gpu_free_object()
          virtio_gpu_remove_from_restore_list()
            mutex_lock(&vgdev->obj_restore_lock)

DEBUG_LOCKS_WARN_ON(lock->magic != lock)

  WARNING: kernel/locking/mutex.c:625 at __mutex_lock+0xf2c/0x12ec

virtio_gpu_deinit() should only do hardware teardown in this case related virtio, its queues and others. Software state that GEM callbacks still use belongs in virtio_gpu_release(), which runs on the final drm_dev_put().

There are two equivalent ways to tie the mutex lifetime to the drm_device:

1) Move mutex_destroy() from virtio_gpu_deinit() to the end of virtio_gpu_release(). This is what this patch does. It is the smallest change and keeps the teardown next to the rest of the final-put software cleanup.

2) Initialise the lock with drmm_mutex_init() (checking its return value) and drop mutex_destroy() from virtio_gpu_deinit(), so DRM's managed release destroys it. This also covers the virtio_gpu_init() error path, where release returns early because dev_private is NULL. Skipping mutex_destroy() there is harmless, since it only matters for mutex debugging.

Either fix removes the WARN. Option 1 is sent as the smaller change, and option 2 can be done instead if preferred.

Reproduced on arm64 QEMU (-device virtio-gpu-pci) with DEBUG_MUTEXES and PROVE_LOCKING: open /dev/fb0, write 1 to /sys/bus/pci/devices/0000:00:01.0/remove, then close the fd. With this patch the WARN is gone, and rebinding brings back /dev/fb0 and /dev/dri/card0.

Fixes 54a970048296 ("drm/virtio: Add support for saving and restoring virtio_gpu_objects")
Reported-by [email protected]
Assisted-by LLM
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +2 -1
index 1d4d3bf46a20..0ec755c35050 100644
@@ -354,7 +354,6 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	virtio_reset_device(vgdev->vdev);
 	virtio_gpu_reclaim_vbufs(vgdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
-	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
@@ -370,6 +369,8 @@ void virtio_gpu_release(struct drm_device *dev)
 
 	if (vgdev->has_host_visible)
 		drm_mm_takedown(&vgdev->host_visible_mm);
+
+	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
Quoted 3 lines
Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_kms.c void virtio_gpu_release(struct drm_device *dev) · line 376

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -0
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)

Calling virtio_gpu_deinit() from virtio_gpu_release() might be a better option

Wouldn't that end up freeing the virtqueues twice, or using them after they are freed? virtio_gpu_deinit() would then run from both the virtio_gpu_remove() path and the virtio_gpu_release() path. or do you mean removing it from virtio_gpu_remove() and calling it only from virtio_gpu_release()?
This is only from a high-level look so far. I'll look into it more closely and get back to you.

Quoted 4 lines
Signature · 3 lines
-- 
Best regards,
Dmitry
maintainer
#5 lore raw
Quoted 90 lines · Abhin Parekadan Jose
Quoted 85 lines · Dmitry Osipenko
Quoted 84 lines · Abhin Parekadan Jose

Gentle ping on this fix for the syzbot report.

Dmitry, Gerd, do you have a preference between moving mutex_destroy() to virtio_gpu_release() (this patch) and using drmm_mutex_init()? I can send a v2 with the other option if you prefer it.

Thanks,
Abhin

Quoted 74 lines · Abhin Parekadan Jose

virtio_gpu_deinit() calls mutex_destroy() on obj_restore_lock, but deinit runs from virtio_gpu_remove() at unbind or PCI remove, and GEM objects can outlive that. An open /dev/fb0 keeps the fbdev buffer alive, and the fbdev DRM client holds a drm_device reference. When the fd is finally closed, the buffer is freed and takes the destroyed lock:

fb_release()

    drm_fbdev_shmem_fb_destroy()
      drm_client_buffer_delete()
        virtio_gpu_free_object()
          virtio_gpu_remove_from_restore_list()
            mutex_lock(&vgdev->obj_restore_lock)

DEBUG_LOCKS_WARN_ON(lock->magic != lock)

  WARNING: kernel/locking/mutex.c:625 at __mutex_lock+0xf2c/0x12ec

virtio_gpu_deinit() should only do hardware teardown in this case related virtio, its queues and others. Software state that GEM callbacks still use belongs in virtio_gpu_release(), which runs on the final drm_dev_put().

There are two equivalent ways to tie the mutex lifetime to the drm_device:

1) Move mutex_destroy() from virtio_gpu_deinit() to the end of virtio_gpu_release(). This is what this patch does. It is the smallest change and keeps the teardown next to the rest of the final-put software cleanup.

2) Initialise the lock with drmm_mutex_init() (checking its return value) and drop mutex_destroy() from virtio_gpu_deinit(), so DRM's managed release destroys it. This also covers the virtio_gpu_init() error path, where release returns early because dev_private is NULL. Skipping mutex_destroy() there is harmless, since it only matters for mutex debugging.

Either fix removes the WARN. Option 1 is sent as the smaller change, and option 2 can be done instead if preferred.

Reproduced on arm64 QEMU (-device virtio-gpu-pci) with DEBUG_MUTEXES and PROVE_LOCKING: open /dev/fb0, write 1 to /sys/bus/pci/devices/0000:00:01.0/remove, then close the fd. With this patch the WARN is gone, and rebinding brings back /dev/fb0 and /dev/dri/card0.

Fixes 54a970048296 ("drm/virtio: Add support for saving and restoring virtio_gpu_objects")
Reported-by [email protected]
Assisted-by LLM
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +2 -1
index 1d4d3bf46a20..0ec755c35050 100644
@@ -354,7 +354,6 @@ void virtio_gpu_deinit(struct drm_device *dev)
 	virtio_reset_device(vgdev->vdev);
 	virtio_gpu_reclaim_vbufs(vgdev);
 	vgdev->vdev->config->del_vqs(vgdev->vdev);
-	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
 void virtio_gpu_release(struct drm_device *dev)
@@ -370,6 +369,8 @@ void virtio_gpu_release(struct drm_device *dev)
 
 	if (vgdev->has_host_visible)
 		drm_mm_takedown(&vgdev->host_visible_mm);
+
+	mutex_destroy(&vgdev->obj_restore_lock);
 }
 
Quoted 3 lines
Quoted 1 line

drivers/gpu/drm/virtio/virtgpu_kms.c void virtio_gpu_release(struct drm_device *dev) · line 376

Quoted 1 line
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -0
 int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)

Calling virtio_gpu_deinit() from virtio_gpu_release() might be a better option

Quoted 7 lines

Wouldn't that end up freeing the virtqueues twice, or using them after they are freed? virtio_gpu_deinit() would then run from both the virtio_gpu_remove() path and the virtio_gpu_release() path. or do you mean removing it from virtio_gpu_remove() and calling it only from virtio_gpu_release()?
This is only from a high-level look so far. I'll look into it more closely and get back to you.

Remove from virtio_gpu_remove() and call only from virtio_gpu_release().

Signature · 2 lines
Best regards,
Dmitry
#6 lore raw
Quoted 24 lines · Dmitry Osipenko
Quoted 15 lines · Abhin Parekadan Jose
Quoted 7 lines · Dmitry Osipenko
Quoted 6 lines · Abhin Parekadan Jose

Gentle ping on this fix for the syzbot report.

Dmitry, Gerd, do you have a preference between moving mutex_destroy() to virtio_gpu_release() (this patch) and using drmm_mutex_init()? I can send a v2 with the other option if you prefer it.

Quoted 6 lines
Quoted 4 lines

Thanks,
Abhin

On Tue, Sep 29, 2026 at 07:07:14AM +0000, Abhin Parekadan Jose wrote:

Calling virtio_gpu_deinit() from virtio_gpu_release() might be a better option

Quoted 7 lines

Wouldn't that end up freeing the virtqueues twice, or using them after they are freed? virtio_gpu_deinit() would then run from both the virtio_gpu_remove() path and the virtio_gpu_release() path. or do you mean removing it from virtio_gpu_remove() and calling it only from virtio_gpu_release()?
This is only from a high-level look so far. I'll look into it more closely and get back to you.

Quoted 1 line

Remove from virtio_gpu_remove() and call only from virtio_gpu_release().

I tried this out and here are my findings.

That would undo b1df3a2b24a9 ("drm/virtio: add drm_driver.release callback."), which split virtio_gpu_deinit() on purpose: "move the drm shutdown and release to virtio_gpu_release() ... use drm_dev_{enter,exit,unplug} to avoid touching hardware after device removal". That matches what the documentation asks for. drm_dev_unplug() exists so the device can be unplugged "while there are still open users of @dev", and Documentation/gpu/drm-uapi.rst (Device Hot-Unplug) says the DRM instance is torn down only after userspace has closed all descriptors and mmaps. Meanwhile the virtio side has to be gone when ->remove() returns: the example in Documentation/driver-api/virtio/writing_virtio_drivers.rst resets the device and deletes the vqs in remove(), and virtio_dev_remove() warns ("Driver should have reset device") if the driver did not.

So the virtio teardown (reset, flushing the vq workers, del_vqs) has to stay in remove(), and everything that open files and GEM objects can still reach has to wait for release(). I tried moving the deinit call into release() to be sure: the virtio core warns that the device was not reset and that the vq MSI vectors are still allocated at PCI removal, and the deferred reset then runs on a virtio_device that virtio_pci_remove() already freed (KASAN use-after-free in virtio_reset_device()). It only makes things more complicated.

obj_restore_lock is on the wrong side of that split. It is only used from GEM paths (the ->free callbacks via virtio_gpu_remove_from_restore_list(), object creation, and the PM freeze/restore ops), and the free callbacks can run after unplug, like the vbuf cache and host_visible_mm that release() already handles. So the mutex belongs in virtio_gpu_release(), which is what this patch does.

It only looks like the mutex_destroy(...) landed in the wrong function virtio_gpu_deinit() instead of virtio_gpu_release() in 54a970048296.

Is there something I am missing in the framework, or have I understood it wrong?

Regards,
Abhin