[CI 2/7] drm/xe: Rework xe_vm_rebind()

Thomas Hellström thomas.hellstrom at linux.intel.com
Tue Mar 12 15:34:54 UTC 2024


Rework xe_vm_rebind() to attach its resulting fence to the vm directly,
and have exec- and rebind worker only deal with the vm's rebind fence.

Also ensure that any new rebind fence completes *after* the previous
one so we don't lose dependencies. Use dma_fence_chain when
necessary.

Signed-off-by: Thomas Hellström <thomas.hellstrom at linux.intel.com>
---
 drivers/gpu/drm/xe/xe_exec.c | 17 ++-------
 drivers/gpu/drm/xe/xe_vm.c   | 73 +++++++++++++++++++++++++++---------
 drivers/gpu/drm/xe/xe_vm.h   |  2 +-
 3 files changed, 61 insertions(+), 31 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_exec.c b/drivers/gpu/drm/xe/xe_exec.c
index 952496c6260d..5124663a9694 100644
--- a/drivers/gpu/drm/xe/xe_exec.c
+++ b/drivers/gpu/drm/xe/xe_exec.c
@@ -152,7 +152,6 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	struct drm_exec *exec = &vm_exec.exec;
 	u32 i, num_syncs = 0, num_ufence = 0;
 	struct xe_sched_job *job;
-	struct dma_fence *rebind_fence;
 	struct xe_vm *vm;
 	bool write_locked, skip_retry = false;
 	ktime_t end = 0;
@@ -289,20 +288,12 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	 * Rebind any invalidated userptr or evicted BOs in the VM, non-compute
 	 * VM mode only.
 	 */
-	rebind_fence = xe_vm_rebind(vm, false);
-	if (IS_ERR(rebind_fence)) {
-		err = PTR_ERR(rebind_fence);
-		goto err_put_job;
+	if (!xe_vm_in_lr_mode(vm)) {
+		err = xe_vm_rebind(vm, false);
+		if (err)
+			goto err_put_job;
 	}
 
-	/*
-	 * We store the rebind_fence in the VM so subsequent execs don't get
-	 * scheduled before the rebinds of userptrs / evicted BOs is complete.
-	 */
-	if (rebind_fence) {
-		dma_fence_put(vm->rebind_fence);
-		vm->rebind_fence = rebind_fence;
-	}
 	if (vm->rebind_fence) {
 		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
 			     &vm->rebind_fence->flags)) {
diff --git a/drivers/gpu/drm/xe/xe_vm.c b/drivers/gpu/drm/xe/xe_vm.c
index 643b3701a738..18a8e1137016 100644
--- a/drivers/gpu/drm/xe/xe_vm.c
+++ b/drivers/gpu/drm/xe/xe_vm.c
@@ -15,6 +15,7 @@
 #include <drm/xe_drm.h>
 #include <linux/ascii85.h>
 #include <linux/delay.h>
+#include <linux/dma-fence-chain.h>
 #include <linux/kthread.h>
 #include <linux/mm.h>
 #include <linux/swap.h>
@@ -522,7 +523,6 @@ static void preempt_rebind_work_func(struct work_struct *w)
 {
 	struct xe_vm *vm = container_of(w, struct xe_vm, preempt.rebind_work);
 	struct drm_exec exec;
-	struct dma_fence *rebind_fence;
 	unsigned int fence_count = 0;
 	LIST_HEAD(preempt_fences);
 	ktime_t end = 0;
@@ -568,15 +568,14 @@ static void preempt_rebind_work_func(struct work_struct *w)
 	if (err)
 		goto out_unlock;
 
-	rebind_fence = xe_vm_rebind(vm, true);
-	if (IS_ERR(rebind_fence)) {
-		err = PTR_ERR(rebind_fence);
+	err = xe_vm_rebind(vm, true);
+	if (err)
 		goto out_unlock;
-	}
 
-	if (rebind_fence) {
-		dma_fence_wait(rebind_fence, false);
-		dma_fence_put(rebind_fence);
+	if (vm->rebind_fence) {
+		dma_fence_wait(vm->rebind_fence, false);
+		dma_fence_put(vm->rebind_fence);
+		vm->rebind_fence = NULL;
 	}
 
 	/* Wait on munmap style VM unbinds */
@@ -756,32 +755,72 @@ xe_vm_bind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
 	       struct xe_sync_entry *syncs, u32 num_syncs,
 	       bool first_op, bool last_op);
 
-struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
+int xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
 {
 	struct dma_fence *fence = NULL;
 	struct xe_vma *vma, *next;
+	struct dma_fence_chain *chain = NULL;
 
 	lockdep_assert_held(&vm->lock);
-	if (xe_vm_in_lr_mode(vm) && !rebind_worker)
-		return NULL;
-
 	xe_vm_assert_held(vm);
+
 	list_for_each_entry_safe(vma, next, &vm->rebind_list,
 				 combined_links.rebind) {
+		struct dma_fence *old = vm->rebind_fence;
+		bool old_signaled = !old || dma_fence_is_signaled(old);
+
 		xe_assert(vm->xe, vma->tile_present);
 
-		list_del_init(&vma->combined_links.rebind);
-		dma_fence_put(fence);
 		if (rebind_worker)
 			trace_xe_vma_rebind_worker(vma);
 		else
 			trace_xe_vma_rebind_exec(vma);
+
+		if (!old_signaled && !chain) {
+			chain = kzalloc(sizeof(*chain), GFP_NOWAIT | __GFP_ACCOUNT);
+			if (!chain) {
+				int err = dma_fence_wait(old, true);
+
+				if (err)
+					return err;
+				old_signaled = true;
+			}
+		}
+
+		if (old_signaled) {
+			dma_fence_put(old);
+			old = NULL;
+			vm->rebind_fence = NULL;
+		}
+
 		fence = xe_vm_bind_vma(vma, NULL, NULL, 0, false, false);
-		if (IS_ERR(fence))
-			return fence;
+		if (IS_ERR(fence)) {
+			kfree(chain);
+			return PTR_ERR(fence);
+		}
+
+		list_del_init(&vma->combined_links.rebind);
+		if (fence && !dma_fence_is_signaled(fence)) {
+			if (!old_signaled) {
+				if (fence->context == old->context) {
+					dma_fence_put(old);
+					vm->rebind_fence = fence;
+				} else {
+					dma_fence_chain_init(chain, old, fence, old->seqno + 1);
+					vm->rebind_fence = &chain->base;
+					chain = NULL;
+				}
+				fence = NULL;
+			} else {
+				vm->rebind_fence = fence;
+				fence = NULL;
+			}
+		}
+		dma_fence_put(fence);
 	}
+	kfree(chain);
 
-	return fence;
+	return 0;
 }
 
 static void xe_vma_free(struct xe_vma *vma)
diff --git a/drivers/gpu/drm/xe/xe_vm.h b/drivers/gpu/drm/xe/xe_vm.h
index 6df1f1c7f85d..4853354336f2 100644
--- a/drivers/gpu/drm/xe/xe_vm.h
+++ b/drivers/gpu/drm/xe/xe_vm.h
@@ -207,7 +207,7 @@ int __xe_vm_userptr_needs_repin(struct xe_vm *vm);
 
 int xe_vm_userptr_check_repin(struct xe_vm *vm);
 
-struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker);
+int xe_vm_rebind(struct xe_vm *vm, bool rebind_worker);
 
 int xe_vm_invalidate_vma(struct xe_vma *vma);
 
-- 
2.44.0



More information about the Intel-xe mailing list