File close starts queue cleanup while the VM still has live mappings. VM destroy can also race work on a queue that holds a VM reference.
Set XE_VM_FLAG_CLOSING under vm->lock before file close starts killing queues and after VM destroy removes the VM handle. Callers of xe_vm_is_closed_or_banned(), including bind and VMA lookup, then reject new work on the VM. Keep vm->size valid so SVM invalidation can still unmap existing mappings. Stop queueing SVM garbage collection once close has started. v3: - Check VM state before replying to cached and chained faults (Tejas). - Hold vm->lock through successful reply submission and invalidate the local cache when the VM lookup or state check fails. (Sashiko) - Report faults rejected by the final VM-state check. (Sashiko) - Preserve the original fault details when service replaces them with the resolved range. (Sashiko) - Account for failed prefetches and invalidate the local cache on failure. (Sashiko) v4: - Drop all the changes from xe_pagefault.c. (Matt) - Move exec rejection on closing VMs here from patch 3. - Reject compute exec queue registration on a closing VM. (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 | 4 ++++ drivers/gpu/drm/xe/xe_exec.c | 9 +++++++ drivers/gpu/drm/xe/xe_svm.c | 3 ++- drivers/gpu/drm/xe/xe_vm.c | 41 ++++++++++++++++++++++++++++++-- drivers/gpu/drm/xe/xe_vm.h | 9 ++++++- drivers/gpu/drm/xe/xe_vm_types.h | 1 + 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c index 8b676fa5c18b..965af3934041 100644 --- a/drivers/gpu/drm/xe/xe_device.c +++ b/drivers/gpu/drm/xe/xe_device.c @@ -184,6 +184,10 @@ static void xe_file_close(struct drm_device *dev, struct drm_file *file) guard(xe_pm_runtime)(xe); + /* Block new VM work before starting asynchronous queue teardown. */ + xa_for_each(&xef->vm.xa, idx, vm) + xe_vm_close_start(vm); + /* * No need for exec_queue.lock here as there is no contention for it * when FD is closing as IOCTLs presumably can't be modifying the diff --git a/drivers/gpu/drm/xe/xe_exec.c b/drivers/gpu/drm/xe/xe_exec.c index 1949fb103fad..1c6f820eb17c 100644 --- a/drivers/gpu/drm/xe/xe_exec.c +++ b/drivers/gpu/drm/xe/xe_exec.c @@ -233,6 +233,15 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file) if (err) goto err_hw_exec_mode; + if (xe_vm_is_closed_or_banned(vm)) { + err = -ECANCELED; + if (write_locked) + up_write(&vm->lock); + else + up_read(&vm->lock); + goto err_hw_exec_mode; + } + if (write_locked) { err = xe_vm_userptr_pin(vm); downgrade_write(&vm->lock); diff --git a/drivers/gpu/drm/xe/xe_svm.c b/drivers/gpu/drm/xe/xe_svm.c index bc6902b5bf57..62ccb2fe6f8f 100644 --- a/drivers/gpu/drm/xe/xe_svm.c +++ b/drivers/gpu/drm/xe/xe_svm.c @@ -219,7 +219,8 @@ xe_svm_range_notifier_event_end(struct xe_vm *vm, struct drm_gpusvm_range *r, drm_gpusvm_unmap_pages(&vm->svm.gpusvm, &(to_xe_range(r)->pages), drm_gpusvm_range_size(r) >> PAGE_SHIFT, &ctx); - if (!xe_vm_is_closed(vm) && mmu_range->event == MMU_NOTIFY_UNMAP) + if (!xe_vm_is_closed(vm) && !xe_vm_is_closing(vm) && + mmu_range->event == MMU_NOTIFY_UNMAP) xe_svm_garbage_collector_add_range(vm, to_xe_range(r), mmu_range); } diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c index 12afb3363e79..5617711d223c 100644 --- a/drivers/gpu/drm/xe/xe_vm.c +++ b/drivers/gpu/drm/xe/xe_vm.c @@ -249,6 +249,12 @@ int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q) xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm)); down_write(&vm->lock); + /* Queue allocation dropped vm->lock before registering this queue. */ + if (xe_vm_is_closed_or_banned(vm)) { + err = -ENOENT; + goto out_up_write; + } + err = xe_validation_exec_lock(&ctx, &vm_exec, &vm->xe->val); if (err) goto out_up_write; @@ -1976,6 +1982,27 @@ static void xe_vm_close(struct xe_vm *vm) drm_dev_exit(idx); } +static void xe_vm_mark_closing(struct xe_vm *vm) +{ + lockdep_assert_held_write(&vm->lock); + + if (xe_vm_in_fault_mode(vm)) + xe_svm_notifier_lock(vm); + + /* Keep size valid so SVM invalidation still performs its full drain. */ + vm->flags |= XE_VM_FLAG_CLOSING; + + if (xe_vm_in_fault_mode(vm)) + xe_svm_notifier_unlock(vm); +} + +void xe_vm_close_start(struct xe_vm *vm) +{ + down_write(&vm->lock); + xe_vm_mark_closing(vm); + up_write(&vm->lock); +} + void xe_vm_close_and_put(struct xe_vm *vm) { LIST_HEAD(contested); @@ -2263,19 +2290,29 @@ int xe_vm_destroy_ioctl(struct drm_device *dev, void *data, XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) return -EINVAL; - mutex_lock(&xef->vm.lock); - vm = xa_load(&xef->vm.xa, args->vm_id); + vm = xe_vm_lookup(xef, args->vm_id); if (XE_IOCTL_DBG(xe, !vm)) + return -ENOENT; + + down_write(&vm->lock); + mutex_lock(&xef->vm.lock); + /* Another destroy may have removed or reused the ID after lookup. */ + if (xa_load(&xef->vm.xa, args->vm_id) != vm) err = -ENOENT; else if (XE_IOCTL_DBG(xe, vm->preempt.num_exec_queues)) err = -EBUSY; else xa_erase(&xef->vm.xa, args->vm_id); mutex_unlock(&xef->vm.lock); + if (!err) + xe_vm_mark_closing(vm); + up_write(&vm->lock); if (!err) xe_vm_close_and_put(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 b1c5f940bcc5..c90c9dc1351c 100644 --- a/drivers/gpu/drm/xe/xe_vm.h +++ b/drivers/gpu/drm/xe/xe_vm.h @@ -59,6 +59,11 @@ static inline bool xe_vm_is_closed(struct xe_vm *vm) return !vm->size; } +static inline bool xe_vm_is_closing(struct xe_vm *vm) +{ + return vm->flags & XE_VM_FLAG_CLOSING; +} + static inline bool xe_vm_is_banned(struct xe_vm *vm) { return vm->flags & XE_VM_FLAG_BANNED; @@ -67,7 +72,8 @@ static inline bool xe_vm_is_banned(struct xe_vm *vm) static inline bool xe_vm_is_closed_or_banned(struct xe_vm *vm) { lockdep_assert_held(&vm->lock); - return xe_vm_is_closed(vm) || xe_vm_is_banned(vm); + return xe_vm_is_closed(vm) || xe_vm_is_banned(vm) || + xe_vm_is_closing(vm); } struct xe_vma * @@ -214,6 +220,7 @@ int xe_vm_get_property_ioctl(struct drm_device *dev, void *data, struct drm_file *file); void xe_vm_close_and_put(struct xe_vm *vm); +void xe_vm_close_start(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 fb9305124679..6ca9ef9d0c0b 100644 --- a/drivers/gpu/drm/xe/xe_vm_types.h +++ b/drivers/gpu/drm/xe/xe_vm_types.h @@ -286,6 +286,7 @@ struct xe_vm { #define XE_VM_FLAG_SET_TILE_ID(tile) FIELD_PREP(GENMASK(7, 6), (tile)->id) #define XE_VM_FLAG_GSC BIT(8) #define XE_VM_FLAG_NO_VM_OVERCOMMIT BIT(9) +#define XE_VM_FLAG_CLOSING BIT(10) unsigned long flags; /** -- 2.43.0
