xe_vm_free() queues vm_destroy_work_func() and returns.
drm_gpuvm_free() then drops its device reference, but the worker
still accesses device state while finishing VM destruction.

Take a device reference before queueing the work and release it
after the worker has finished using the device.

Run the worker on the module-lifetime destroy workqueue. Releasing
the last device reference can call xe_device_destroy(), which drains
xe->destroy_wq. Running on that per-device workqueue would make the
worker wait for itself.

v3:
- Move final VM destruction to the module-lifetime destroy workqueue
  in this patch to avoid self-deadlock on the last device put. (Sashiko)

Fixes: 61e18525023a ("drm/xe: Route deferred xe_vma/xe_vm teardown off 
system_dfl_wq")
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Himal Prasad Ghimiray <[email protected]>
Cc: Rodrigo Vivi <[email protected]>
Cc: Daniel Charles <[email protected]>
Cc: Tejas Upadhyay <[email protected]>
Assisted-by: LLM
Tested-by: Sanjay Yadav <[email protected]>
Signed-off-by: Arvind Yadav <[email protected]>
---
 drivers/gpu/drm/xe/xe_vm.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
index dd855876f571..12afb3363e79 100644
--- a/drivers/gpu/drm/xe/xe_vm.c
+++ b/drivers/gpu/drm/xe/xe_vm.c
@@ -2109,6 +2109,8 @@ static void vm_destroy_work_func(struct work_struct *w)
                xe_file_put(vm->xef);
 
        kfree(vm);
+
+       drm_dev_put(&xe->drm);
 }
 
 static void xe_vm_free(struct drm_gpuvm *gpuvm)
@@ -2116,11 +2118,18 @@ static void xe_vm_free(struct drm_gpuvm *gpuvm)
        struct xe_vm *vm = container_of(gpuvm, struct xe_vm, gpuvm);
 
        /*
-        * To destroy the VM we need to be able to sleep.
-        * drm_gpuvm keeps at least one device reference at this
-        * point so xe->destroy_wq must still be alive.
+        * drm_gpuvm drops its device reference as soon as this callback
+        * returns, but vm_destroy_work_func() still uses device state. Hold a
+        * reference across the deferred work.
+        */
+       drm_dev_get(&vm->xe->drm);
+
+       /*
+        * The worker may release the last device reference. Use the
+        * module-lifetime workqueue so device destruction cannot drain
+        * the workqueue running this worker.
         */
-       queue_work(vm->xe->destroy_wq, &vm->destroy_work);
+       xe_destroy_wq_queue(&vm->destroy_work);
 }
 
 struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
-- 
2.43.0

Reply via email to