Exec queue destruction continues after xe_exec_queue_put() returns.
User queues can also outlive the VM handle. Closing the VM before
those queues finish cleanup can remove mappings still in use.

Track user exec queues and user-created bind queues until final queue
destruction. Add each close count under vm->lock so VM close cannot
miss a queue being created. Keep the bind queue's user_vm reference
until final queue destruction.

If tracked queues remain, retain the mappings and defer VM close
until the last queue is freed. Keep the existing scheduling-disable
wait on file close without adding a wait for the close count.

Before deferring, wait for the VM's BOOKKEEP fences as xe_vm_close()
did. VM destroy and file close can then return while queues finish
cleanup, but not while their jobs still wait on input fences.

Let pending user bind jobs drain during file close and queue destroy.
Killing these queues can make pending PT bind jobs clear mappings
while hardware queues still use them.

Exclude the VM-owned bind queue from the close count. Release it
after xe_vm_close() has waited for DMA_RESV_USAGE_BOOKKEEP fences.

Run deferred close on the module-lifetime destroy workqueue and
hold an extra VM reference until the worker has released its runtime-PM
reference and unplug guard.

v3:
- Remove the deadline calculation and use zero wait explicitly on file close.
  (Tejas)
- Document that internal bind queue cleanup can also defer VM teardown.
  (Sashiko)
- Clarify that mappings remain until all tracked exec queues finish cleanup.
  (Sashiko)
- Keep the counter recheck under close.lock to avoid missing the last
  queue's cleanup. (Sashiko)
- Keep VM-owned bind queues alive until pending bind fences complete.
  (Sashiko)
- Track user bind queues when assigning their file reference.
  (Sashiko)
- Let user bind jobs drain during file close instead of forcing pending
  PT updates to clear mappings before hardware queues have stopped.
  (Sashiko)
- Let user bind jobs drain in xe_exec_queue_destroy_ioctl().
- Count user bind queues before releasing vm->lock.
- Use user_vm_close_tracked to balance the count during final free,
  including creation failure paths.
- Make the bind queue helper static and remove the unused wait header.

v4:
- Move exec rejection on closing VMs to patch 2.
- Wait for VM BOOKKEEP fences before deferring close, so a pending
  input fence cannot hold a closed VM after close returns. (Sashiko)
- Skip runtime PM and HW access in deferred VM close. (Sashiko)
- Use atomic_dec_and_lock() when dropping the last close count. (Sashiko)

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]>
Tested-by: Sanjay Yadav <[email protected]>
Signed-off-by: Arvind Yadav <[email protected]>
---
 drivers/gpu/drm/xe/xe_device.c           |  22 ++-
 drivers/gpu/drm/xe/xe_exec_queue.c       |  39 ++++--
 drivers/gpu/drm/xe/xe_exec_queue_types.h |  11 ++
 drivers/gpu/drm/xe/xe_guc_submit.c       |  20 ++-
 drivers/gpu/drm/xe/xe_vm.c               | 166 ++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_vm.h               |   4 +
 drivers/gpu/drm/xe/xe_vm_types.h         |  16 +++
 drivers/gpu/drm/xe/xe_wait_user_fence.c  |   5 +
 8 files changed, 247 insertions(+), 36 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index 965af3934041..4cc8a7569dc1 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -180,6 +180,7 @@ static void xe_file_close(struct drm_device *dev, struct 
drm_file *file)
        struct xe_file *xef = file->driver_priv;
        struct xe_vm *vm;
        struct xe_exec_queue *q;
+       bool close_deferred = false;
        unsigned long idx;
 
        guard(xe_pm_runtime)(xe);
@@ -197,16 +198,31 @@ static void xe_file_close(struct drm_device *dev, struct 
drm_file *file)
        xa_for_each(&xef->exec_queue.xa, idx, q) {
                if (q->vm && q->hwe->hw_engine_group)
                        
xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
-               xe_exec_queue_kill(q);
+               /*
+                * Killing a bind queue can force pending PT jobs to clear
+                * mappings while hardware exec queues are still running.
+                * Let bind jobs drain before their queue is destroyed.
+                */
+               if (!(q->flags & EXEC_QUEUE_FLAG_VM))
+                       xe_exec_queue_kill(q);
        }
 
        xa_for_each(&xef->exec_queue.xa, idx, q) {
-               xe_exec_queue_kill_sync(q);
+               if (!(q->flags & EXEC_QUEUE_FLAG_VM))
+                       xe_exec_queue_kill_sync(q);
                xe_exec_queue_put(q);
        }
 
+       /*
+        * Queue cleanup is asynchronous. Retain VM mappings until tracked
+        * queues complete final destruction.
+        */
        xa_for_each(&xef->vm.xa, idx, vm)
-               xe_vm_close_and_put(vm);
+               close_deferred |= xe_vm_close_and_put_deferred(vm);
+
+       if (close_deferred)
+               drm_dbg(&xe->drm,
+                       "VM teardown deferred until exec queue cleanup 
completes\n");
 
        scoped_guard(mutex, &xef->mmio_gem.lock) {
                if (xef->mmio_gem.pci_barrier) {
diff --git a/drivers/gpu/drm/xe/xe_exec_queue.c 
b/drivers/gpu/drm/xe/xe_exec_queue.c
index 00941a9853b5..4fa48b3df996 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue.c
+++ b/drivers/gpu/drm/xe/xe_exec_queue.c
@@ -156,9 +156,17 @@ static void __xe_exec_queue_free(struct xe_exec_queue *q)
 
        if (q->vm) {
                xe_vm_remove_exec_queue(q->vm, q);
+               if (q->vm->xef)
+                       xe_vm_remove_close_queue(q->vm);
                xe_vm_put(q->vm);
        }
 
+       if (q->user_vm) {
+               if (q->user_vm_close_tracked)
+                       xe_vm_remove_close_queue(q->user_vm);
+               xe_vm_put(q->user_vm);
+       }
+
        if (q->xef)
                xe_file_put(q->xef);
 
@@ -254,8 +262,12 @@ static struct xe_exec_queue *__xe_exec_queue_alloc(struct 
xe_device *xe,
                }
        }
 
-       if (vm)
+       if (vm) {
                q->vm = xe_vm_get(vm);
+               /* vm->xef stays unchanged until final VM destruction. */
+               if (vm->xef)
+                       xe_vm_add_close_queue(vm);
+       }
 
        if (extensions) {
                /*
@@ -692,11 +704,6 @@ void xe_exec_queue_destroy(struct kref *ref)
        for_each_tlb_inval(q, i)
                xe_exec_queue_tlb_inval_last_fence_put_unlocked(q, i);
 
-       if (q->user_vm) {
-               xe_vm_put(q->user_vm);
-               q->user_vm = NULL;
-       }
-
        q->ops->destroy(q);
 }
 
@@ -1430,6 +1437,11 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, 
void *data,
 
                q = xe_exec_queue_create_bind(xe, xe_device_get_root_tile(xe),
                                              vm, flags, args->extensions);
+               if (!IS_ERR(q)) {
+                       /* Serialize close tracking with xe_vm_close_start(). */
+                       q->user_vm_close_tracked = true;
+                       xe_vm_add_close_queue(vm);
+               }
                up_read(&vm->lock);
                xe_vm_put(vm);
                if (IS_ERR(q)) {
@@ -1699,17 +1711,28 @@ int xe_exec_queue_destroy_ioctl(struct drm_device *dev, 
void *data,
 
        mutex_lock(&xef->exec_queue.lock);
        q = xa_erase(&xef->exec_queue.xa, args->exec_queue_id);
-       if (q)
+       if (q) {
+               WRITE_ONCE(q->user_closed, true);
                atomic_inc(&xef->exec_queue.pending_removal);
+       }
        mutex_unlock(&xef->exec_queue.lock);
 
        if (XE_IOCTL_DBG(xe, !q))
                return -ENOENT;
 
+       /* Bind queues drain without kill, so wake their user-fence waiters 
here. */
+       wake_up_all(&xe->ufence_wq);
+
        if (q->vm && q->hwe->hw_engine_group)
                xe_hw_engine_group_del_exec_queue(q->hwe->hw_engine_group, q);
 
-       xe_exec_queue_kill(q);
+       /*
+        * Let bind jobs drain. Killing their queue can turn pending PT binds
+        * into PTE clears while hardware queues still use the VM. A kill
+        * would still leave the scheduler waiting for input fences.
+        */
+       if (!(q->flags & EXEC_QUEUE_FLAG_VM))
+               xe_exec_queue_kill(q);
 
        trace_xe_exec_queue_close(q);
        xe_exec_queue_put(q);
diff --git a/drivers/gpu/drm/xe/xe_exec_queue_types.h 
b/drivers/gpu/drm/xe/xe_exec_queue_types.h
index 611d348145a6..a1401b251436 100644
--- a/drivers/gpu/drm/xe/xe_exec_queue_types.h
+++ b/drivers/gpu/drm/xe/xe_exec_queue_types.h
@@ -92,6 +92,11 @@ struct xe_exec_queue_group {
 struct xe_exec_queue {
        /** @xef: Back pointer to xe file if this is user created exec queue */
        struct xe_file *xef;
+       /**
+        * @user_closed: The queue handle has been destroyed. Allows user-fence
+        * waits to abort without killing pending bind jobs.
+        */
+       bool user_closed;
 
        /** @gt: GT structure this exec queue can submit to */
        struct xe_gt *gt;
@@ -110,6 +115,12 @@ struct xe_exec_queue {
         * only)
         */
        struct xe_vm *user_vm;
+       /**
+        * @user_vm_close_tracked: This user bind queue holds a close count on
+        * @user_vm. Set under user_vm->lock before publishing the queue and
+        * unchanged until final free. VM-owned bind queues are not counted.
+        */
+       bool user_vm_close_tracked;
 
        /** @class: class of this exec queue */
        enum xe_engine_class class;
diff --git a/drivers/gpu/drm/xe/xe_guc_submit.c 
b/drivers/gpu/drm/xe/xe_guc_submit.c
index b6f884efe78c..8413b51dcf3f 100644
--- a/drivers/gpu/drm/xe/xe_guc_submit.c
+++ b/drivers/gpu/drm/xe/xe_guc_submit.c
@@ -1909,20 +1909,28 @@ static void guc_exec_queue_do_destroy(struct 
xe_exec_queue *q)
        struct xe_guc *guc = exec_queue_to_guc(q);
        struct xe_device *xe = guc_to_xe(guc);
        struct drm_device *drm = &xe->drm;
+       bool bound;
+       int idx;
 
        /*
         * guc_exec_queue_fini() drops the queue's drm_device ref.
-        * Keep the device alive until the PM-runtime guard unwinds.
+        * Keep the device alive until the PM and unplug guards unwind.
         */
        drm_dev_get(drm);
 
-       scoped_guard(xe_pm_runtime, xe) {
-               trace_xe_exec_queue_destroy(q);
+       bound = drm_dev_enter(drm, &idx);
+       if (bound)
+               xe_pm_runtime_get(xe);
+
+       trace_xe_exec_queue_destroy(q);
 
-               /* Confirm no work left behind accessing device structures */
-               cancel_delayed_work_sync(&ge->sched.base.work_tdr);
+       /* Confirm no work left behind accessing device structures */
+       cancel_delayed_work_sync(&ge->sched.base.work_tdr);
+       xe_exec_queue_fini(q);
 
-               xe_exec_queue_fini(q);
+       if (bound) {
+               xe_pm_runtime_put(xe);
+               drm_dev_exit(idx);
        }
 
        drm_dev_put(drm);
diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
index 5617711d223c..6a5c397cfcda 100644
--- a/drivers/gpu/drm/xe/xe_vm.c
+++ b/drivers/gpu/drm/xe/xe_vm.c
@@ -1629,7 +1629,9 @@ static const struct xe_pt_ops xelp_pt_ops = {
        .pde_encode_bo = xelp_pde_encode_bo,
 };
 
+static void vm_close_work_func(struct work_struct *w);
 static void vm_destroy_work_func(struct work_struct *w);
+static void xe_vm_put_bind_queue(struct xe_vm *vm);
 
 /**
  * xe_vm_create_scratch() - Setup a scratch memory pagetable tree for the
@@ -1772,6 +1774,9 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 
flags, struct xe_file *xef)
        ttm_lru_bulk_move_init(&vm->lru_bulk_move);
 
        INIT_WORK(&vm->destroy_work, vm_destroy_work_func);
+       atomic_set(&vm->close.num_exec_queues, 0);
+       spin_lock_init(&vm->close.lock);
+       INIT_WORK(&vm->close.work, vm_close_work_func);
 
        INIT_LIST_HEAD(&vm->preempt.exec_queues);
        for (id = 0; id < XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE; ++id)
@@ -1936,6 +1941,12 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 
flags, struct xe_file *xef)
        return ERR_PTR(err);
 }
 
+static void xe_vm_wait_fences(struct xe_vm *vm)
+{
+       dma_resv_wait_timeout(xe_vm_resv(vm), DMA_RESV_USAGE_BOOKKEEP,
+                             false, MAX_SCHEDULE_TIMEOUT);
+}
+
 static void xe_vm_close(struct xe_vm *vm)
 {
        struct xe_device *xe = vm->xe;
@@ -1958,9 +1969,7 @@ static void xe_vm_close(struct xe_vm *vm)
                u8 id;
 
                /* Wait for pending binds */
-               dma_resv_wait_timeout(xe_vm_resv(vm),
-                                     DMA_RESV_USAGE_BOOKKEEP,
-                                     false, MAX_SCHEDULE_TIMEOUT);
+               xe_vm_wait_fences(vm);
 
                if (bound) {
                        for_each_tile(tile, xe, id)
@@ -2024,19 +2033,7 @@ void xe_vm_close_and_put(struct xe_vm *vm)
        if (xe_vm_in_fault_mode(vm))
                xe_svm_close(vm);
 
-       if (vm->q) {
-               int i;
-
-               down_write(&vm->lock);
-               xe_exec_queue_last_fence_put(vm->q, vm);
-               for_each_tlb_inval(vm->q, i)
-                       xe_exec_queue_tlb_inval_last_fence_put(vm->q, vm, i);
-               up_write(&vm->lock);
-
-               xe_exec_queue_kill(vm->q);
-               xe_exec_queue_put(vm->q);
-               vm->q = NULL;
-       }
+       xe_vm_put_bind_queue(vm);
 
        down_write(&vm->lock);
        xe_vm_lock(vm, false);
@@ -2106,6 +2103,131 @@ void xe_vm_close_and_put(struct xe_vm *vm)
        xe_vm_put(vm);
 }
 
+static void vm_close_work_func(struct work_struct *w)
+{
+       struct xe_vm *vm = container_of(w, struct xe_vm, close.work);
+       struct xe_device *xe = vm->xe;
+       bool bound;
+       int idx;
+
+       /* Keep the VM and device alive until the PM and unplug guards unwind. 
*/
+       xe_vm_get(vm);
+
+       bound = drm_dev_enter(&xe->drm, &idx);
+       if (bound)
+               xe_pm_runtime_get(xe);
+
+       /* Release VMAs and queue references even after unplug. */
+       xe_vm_close_and_put(vm);
+
+       if (bound) {
+               xe_pm_runtime_put(xe);
+               drm_dev_exit(idx);
+       }
+
+       xe_vm_put(vm);
+}
+
+/**
+ * xe_vm_put_bind_queue() - Release the VM's default bind queue
+ * @vm: The VM being closed
+ *
+ * xe_vm_close() has waited for the VM's bind jobs. No kill: on a killed
+ * queue, pending PT jobs clear PTEs instead of applying them.
+ */
+static void xe_vm_put_bind_queue(struct xe_vm *vm)
+{
+       struct xe_exec_queue *q;
+       int i;
+
+       if (!vm->q)
+               return;
+
+       down_write(&vm->lock);
+       q = vm->q;
+       vm->q = NULL;
+       xe_exec_queue_last_fence_put(q, vm);
+       for_each_tlb_inval(q, i)
+               xe_exec_queue_tlb_inval_last_fence_put(q, vm, i);
+       up_write(&vm->lock);
+
+       xe_exec_queue_put(q);
+}
+
+/**
+ * xe_vm_add_close_queue() - Track an exec queue using the VM
+ * @vm: The VM used by the exec queue
+ *
+ * The queue is tracked until its asynchronous destruction completes.
+ */
+void xe_vm_add_close_queue(struct xe_vm *vm)
+{
+       atomic_inc(&vm->close.num_exec_queues);
+}
+
+/**
+ * xe_vm_remove_close_queue() - Stop tracking an exec queue
+ * @vm: The VM used by the exec queue
+ *
+ * If this is the last tracked queue and VM close was deferred, schedule
+ * the deferred VM close.
+ */
+void xe_vm_remove_close_queue(struct xe_vm *vm)
+{
+       bool queue_close = false;
+
+       if (!atomic_dec_and_lock(&vm->close.num_exec_queues, &vm->close.lock))
+               return;
+
+       if (vm->close.deferred) {
+               vm->close.deferred = false;
+               queue_close = true;
+       }
+       spin_unlock(&vm->close.lock);
+
+       if (queue_close)
+               xe_destroy_wq_queue(&vm->close.work);
+}
+
+/**
+ * xe_vm_close_and_put_deferred() - Close a VM immediately or defer the close
+ * @vm: The VM reference to consume
+ *
+ * The VM must already be marked closing, and callers must serialize teardown.
+ * Only user exec queues and user-created bind queues are tracked. Keep the
+ * VM-owned bind queue alive until xe_vm_close() has waited for pending bind
+ * fences. xe_vm_close_and_put() releases that queue afterwards.
+ *
+ * The caller's VM reference is consumed on both paths.
+ *
+ * Return: %true if VM close was deferred, or %false if it completed now.
+ */
+bool xe_vm_close_and_put_deferred(struct xe_vm *vm)
+{
+       bool queue_close = false;
+
+       if (!atomic_read(&vm->close.num_exec_queues)) {
+               xe_vm_close_and_put(vm);
+               return false;
+       }
+
+       /* Drain published VM fences before handing close to the last queue. */
+       xe_vm_wait_fences(vm);
+
+       spin_lock(&vm->close.lock);
+       vm->close.deferred = true;
+       if (!atomic_read(&vm->close.num_exec_queues)) {
+               vm->close.deferred = false;
+               queue_close = true;
+       }
+       spin_unlock(&vm->close.lock);
+
+       if (queue_close)
+               xe_destroy_wq_queue(&vm->close.work);
+
+       return true;
+}
+
 static void vm_destroy_work_func(struct work_struct *w)
 {
        struct xe_vm *vm =
@@ -2308,9 +2430,15 @@ int xe_vm_destroy_ioctl(struct drm_device *dev, void 
*data,
                xe_vm_mark_closing(vm);
        up_write(&vm->lock);
 
-       if (!err)
-               xe_vm_close_and_put(vm);
-
+       if (!err) {
+               /*
+                * User exec queues and user-created bind queues can outlive the
+                * VM handle. Retain mappings until those queues finish cleanup.
+                * The VM-owned bind queue is released by xe_vm_close_and_put().
+                * The helper consumes the reference removed from the xarray.
+                */
+               xe_vm_close_and_put_deferred(vm);
+       }
        xe_vm_put(vm);
 
        return err;
diff --git a/drivers/gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h
index c90c9dc1351c..5632f0757b6a 100644
--- a/drivers/gpu/drm/xe/xe_vm.h
+++ b/drivers/gpu/drm/xe/xe_vm.h
@@ -221,6 +221,10 @@ int xe_vm_get_property_ioctl(struct drm_device *dev, void 
*data,
 
 void xe_vm_close_and_put(struct xe_vm *vm);
 void xe_vm_close_start(struct xe_vm *vm);
+bool xe_vm_close_and_put_deferred(struct xe_vm *vm);
+
+void xe_vm_add_close_queue(struct xe_vm *vm);
+void xe_vm_remove_close_queue(struct xe_vm *vm);
 
 static inline bool xe_vm_in_fault_mode(struct xe_vm *vm)
 {
diff --git a/drivers/gpu/drm/xe/xe_vm_types.h b/drivers/gpu/drm/xe/xe_vm_types.h
index 6ca9ef9d0c0b..bd2898c5ffb1 100644
--- a/drivers/gpu/drm/xe/xe_vm_types.h
+++ b/drivers/gpu/drm/xe/xe_vm_types.h
@@ -314,6 +314,22 @@ struct xe_vm {
         */
        struct work_struct destroy_work;
 
+       /** @close: State used to defer VM teardown until exec queues are gone. 
*/
+       struct {
+               /**
+                * @close.num_exec_queues: User exec and user-created bind 
queues
+                * that have not completed final free. Excludes the VM-owned 
bind
+                * queue, which is released by xe_vm_close_and_put().
+                */
+               atomic_t num_exec_queues;
+               /** @close.lock: Protects deferred and work scheduling. */
+               spinlock_t lock;
+               /** @close.deferred: VM close is waiting for queue teardown. */
+               bool deferred;
+               /** @close.work: Completes a deferred VM close. */
+               struct work_struct work;
+       } close;
+
        /**
         * @rftree: range fence tree to track updates to page table structure.
         * Used to implement conflict tracking between independent bind engines.
diff --git a/drivers/gpu/drm/xe/xe_wait_user_fence.c 
b/drivers/gpu/drm/xe/xe_wait_user_fence.c
index e241f3ec3547..f78baa37e292 100644
--- a/drivers/gpu/drm/xe/xe_wait_user_fence.c
+++ b/drivers/gpu/drm/xe/xe_wait_user_fence.c
@@ -146,6 +146,11 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void 
*data,
                }
 
                if (q) {
+                       if (READ_ONCE(q->user_closed)) {
+                               err = -EIO;
+                               break;
+                       }
+
                        if (q->ops->reset_status(q)) {
                                drm_info(&xe->drm, "exec queue reset 
detected\n");
                                err = -EIO;
-- 
2.43.0

Reply via email to