Skip to contentSkip to conversation

Refactor VM bind code

PATCH In review Matthew Brost 3 participants 76 messages Mar 8, 2024 – Mar 27, 2024 Page 1 of 2

Subsystems (2)
lore
Share & tools
lore

Matthew Brost

author maintainer
#0 lore raw

v4 cover Refactor VM bind code

Broken into roughly 5 parts:
Part 1: Prep patches, patches 1-14
Part 2: 1 jobs per VM bind IOCTL and error handling, patches 15-23 Part 3: CPU binds, patches 24-27
Part 4: Error injection for testing, patch 28
Part 5: PT exec queues 29-30

For reviewing, let's focus on part 1 for now and see if patches from that part can start to get merged.

Tested with [1] and new error handling appears to be working. Also tested with existing tests at every patch in the series and should be working at every patch in the series.

Latest version includes some of Thomas's patches too [2].

Matt

[1] https://patchwork.freedesktop.org/series/129606/
[2] https://patchwork.freedesktop.org/series/130871/

Matthew Brost (28):
drm/xe: Lock all gpuva ops during VM bind IOCTL
drm/xe: Add ops_execute function which returns a fence drm/xe: Move migrate to prefetch to op_lock function drm/xe: Add struct xe_vma_ops abstraction
drm/xe: Update xe_vm_rebind to use dummy VMA operations drm/xe: Simplify VM bind IOCTL error handling and cleanup drm/xe: Update pagefaults to use dummy VMA operations drm/xe: s/xe_tile_migrate_engine/xe_tile_migrate_exec_queue drm/xe: Add some members to xe_vma_ops
drm/xe: Add vm_bind_ioctl_ops_install_fences helper drm/xe: Move setting last fence to vm_bind_ioctl_ops_install_fences drm/xe: Move ufence check to op_lock
drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences drm/xe: Add xe_gt_tlb_invalidation_range and convert PT layer to use
this
drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops
drm/xe: Use ordered WQ for TLB invalidation fences drm/xe: Delete PT update selftest
drm/xe: Convert multiple bind ops into single job
drm/xe: Remove old functions defs in xe_pt.h
drm/xe: Update PT layer with better error handling drm/xe: Update VM trace events
drm/xe: Update clear / populate arguments
drm/xe: Add __xe_migrate_update_pgtables_cpu helper drm/xe: CPU binds for jobs
drm/xe: Don't use migrate exec queue for page fault binds drm/xe: Add VM bind IOCTL error injection
drm/xe/guc: Assert time'd out jobs are not from a VM exec queue drm/xe: Add PT exec queues

Thomas Hellström (2):
drm/xe: Update xe_vm_rebind to return int drm/xe: Move vma rebinding to the drm_exec locking loop

 drivers/gpu/drm/xe/Makefile                 |    1 +
 drivers/gpu/drm/xe/tests/xe_migrate.c       |   86 --
 drivers/gpu/drm/xe/xe_bo.c                  |    7 +-
 drivers/gpu/drm/xe/xe_bo.h                  |    4 +-
 drivers/gpu/drm/xe/xe_device.c              |   35 +
 drivers/gpu/drm/xe/xe_device.h              |    2 +
 drivers/gpu/drm/xe/xe_device_types.h        |   16 +
 drivers/gpu/drm/xe/xe_exec.c                |   41 +-
 drivers/gpu/drm/xe/xe_exec_queue.c          |  120 +-
 drivers/gpu/drm/xe/xe_exec_queue_types.h    |   20 +-
 drivers/gpu/drm/xe/xe_gt_pagefault.c        |   10 +-
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c |   59 +-
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h |    3 +
 drivers/gpu/drm/xe/xe_guc_submit.c          |   22 +-
 drivers/gpu/drm/xe/xe_migrate.c             |  385 ++----
 drivers/gpu/drm/xe/xe_migrate.h             |   46 +-
 drivers/gpu/drm/xe/xe_pt.c                  | 1236 ++++++++++++-------
 drivers/gpu/drm/xe/xe_pt.h                  |   15 +-
 drivers/gpu/drm/xe/xe_pt_exec_queue.c       |  180 +++
 drivers/gpu/drm/xe/xe_pt_exec_queue.h       |   14 +
 drivers/gpu/drm/xe/xe_pt_types.h            |   53 +
 drivers/gpu/drm/xe/xe_sched_job.c           |   68 +-
 drivers/gpu/drm/xe/xe_sched_job_types.h     |   31 +-
 drivers/gpu/drm/xe/xe_sync.c                |   15 +
 drivers/gpu/drm/xe/xe_sync.h                |    1 +
 drivers/gpu/drm/xe/xe_trace.h               |   21 +-
 drivers/gpu/drm/xe/xe_vm.c                  | 1124 ++++++++---------
 drivers/gpu/drm/xe/xe_vm.h                  |    9 +-
 drivers/gpu/drm/xe/xe_vm_types.h            |  198 +--
 29 files changed, 2118 insertions(+), 1704 deletions(-)
 create mode 100644 drivers/gpu/drm/xe/xe_pt_exec_queue.c
 create mode 100644 drivers/gpu/drm/xe/xe_pt_exec_queue.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v4 1/30 drm/xe: Lock all gpuva ops during VM bind IOCTL

Lock all gpuva ops and validate all BOs in a single step durin the VM bind IOCTL. This help with the transition to making all gpuva ops in a VM bind IOCTL a single atomic job.

---
 drivers/gpu/drm/xe/xe_vm.c | 142 ++++++++++++++++++++++++++-----------
 1 file changed, 101 insertions(+), 41 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +101 -41
index 643b3701a738..3b5dc6de07f7 100644
@@ -413,19 +413,23 @@ int __xe_vm_userptr_needs_repin(struct xe_vm *vm)
 
 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
 
-static void xe_vm_kill(struct xe_vm *vm)
+static void xe_vm_kill(struct xe_vm *vm, bool unlocked)
 {
 	struct xe_exec_queue *q;
 
 	lockdep_assert_held(&vm->lock);
 
-	xe_vm_lock(vm, false);
+	if (unlocked)
+		xe_vm_lock(vm, false);
+
 	vm->flags |= XE_VM_FLAG_BANNED;
 	trace_xe_vm_kill(vm);
 
 	list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
 		q->ops->kill(q);
-	xe_vm_unlock(vm);
+
+	if (unlocked)
+		xe_vm_unlock(vm);
 
 	/* TODO: Inform user the VM is banned */
 }
@@ -621,7 +625,7 @@ static void preempt_rebind_work_func(struct work_struct *w)
 
 	if (err) {
 		drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
-		xe_vm_kill(vm);
+		xe_vm_kill(vm, true);
 	}
 	up_write(&vm->lock);
 
@@ -1831,17 +1835,9 @@ static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue
 		      u32 num_syncs, bool immediate, bool first_op,
 		      bool last_op)
 {
-	int err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
-	if (bo && immediate) {
-		err = xe_bo_validate(bo, vm, true);
-		if (err)
-			return err;
-	}
-
 	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate, first_op,
 			    last_op);
 }
@@ -2488,17 +2484,12 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
-		      struct xe_vma *vma, struct xe_vma_op *op)
+static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		      struct xe_vma_op *op)
 {
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
-
-	err = xe_vm_prepare_vma(exec, vma, 1);
-	if (err)
-		return err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
 
@@ -2579,19 +2570,10 @@ static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
 static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 			       struct xe_vma_op *op)
 {
-	struct drm_exec exec;
 	int err;
 
 retry_userptr:
-	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
-	drm_exec_until_all_locked(&exec) {
-		err = op_execute(&exec, vm, vma, op);
-		drm_exec_retry_on_contention(&exec);
-		if (err)
-			break;
-	}
-	drm_exec_fini(&exec);
-
+	err = op_execute(vm, vma, op);
 	if (err == -EAGAIN) {
 		lockdep_assert_held_write(&vm->lock);
 
@@ -2756,29 +2738,107 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,
 	}
 }
 
+static int vma_lock(struct drm_exec *exec, struct xe_vma *vma, bool validate)
+{
+	struct xe_bo *bo = xe_vma_bo(vma);
+	int err = 0;
+
+	if (bo) {
+		if (!bo->vm)
+			err = drm_exec_prepare_obj(exec, &bo->ttm.base, 1);
+		if (!err && validate)
+			err = xe_bo_validate(bo, xe_vma_vm(vma), true);
+	}
+
+	return err;
+}
+
+static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
+		   struct xe_vma_op *op)
+{
+	int err = 0;
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		err = vma_lock(exec, op->map.vma, !xe_vm_in_fault_mode(vm));
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap->va),
+			       false);
+		if (!err && op->remap.prev)
+			err = vma_lock(exec, op->remap.prev, true);
+		if (!err && op->remap.next)
+			err = vma_lock(exec, op->remap.next, true);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va), true);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
+
+static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
+				  struct xe_vm *vm,
+				  struct list_head *ops_list)
+{
+	struct xe_vma_op *op;
+	int err;
+
+	err = drm_exec_prepare_obj(exec, xe_vm_obj(vm), 1);
+	if (err)
+		return err;
+
+	list_for_each_entry(op, ops_list, link) {
+		err = op_lock(exec, vm, op);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
+	struct drm_exec exec;
 	struct xe_vma_op *op, *next;
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
-		err = xe_vma_op_execute(vm, op);
-		if (err) {
-			drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
-				 op->base.op, err);
-			/*
-			 * FIXME: Killing VM rather than proper error handling
-			 */
-			xe_vm_kill(vm);
-			return -ENOSPC;
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
+		      DRM_EXEC_IGNORE_DUPLICATES, 0);
+	drm_exec_until_all_locked(&exec) {
+		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			goto unlock;
+
+		list_for_each_entry_safe(op, next, ops_list, link) {
+			err = xe_vma_op_execute(vm, op);
+			if (err) {
+				drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
+					 op->base.op, err);
+				/*
+				 * FIXME: Killing VM rather than proper error handling
+				 */
+				xe_vm_kill(vm, false);
+				err = -ENOSPC;
+				goto unlock;
+			}
+			xe_vma_op_cleanup(vm, op);
 		}
-		xe_vma_op_cleanup(vm, op);
 	}
 
-	return 0;
+unlock:
+	drm_exec_fini(&exec);
+	return err;
 }
 
 #define SUPPORTED_FLAGS	(DRM_XE_VM_BIND_FLAG_NULL | \
Signature · 1 line
2.34.1
#2 lore raw

RE: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 11 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Lock all gpuva ops

Can we have a better wording? Better to say locking all Bos used in gpuva ops?

Or maybe lock ops by locking and validating all Bos used in ops.

and validate all BOs in a single step durin the VM

Quoted 2 lines

bind IOCTL. This help with the transition to making all gpuva ops in a VM bind IOCTL a single atomic job.

Can you also explain, why you want bind to be a atomic job?

My guess is, bind ioctl can end up with a series of operations, if some (not all) of those operations fail in the middle, it is hard to revert the successful operations before failure.

Quoted 15 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 142 ++++++++++++++++++++++++++-----------
 1 file changed, 101 insertions(+), 41 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -1
index 643b3701a738..3b5dc6de07f7 100644
@@ -413,19 +413,23 @@ int __xe_vm_userptr_needs_repin(struct xe_vm *vm)
 
 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
 
-static void xe_vm_kill(struct xe_vm *vm)

drivers/gpu/drm/xe/xe_vm.c int __xe_vm_userptr_needs_repin(struct xe_vm *vm) · line 424

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +4 -1
+static void xe_vm_kill(struct xe_vm *vm, bool unlocked)
 {
 	struct xe_exec_queue *q;
 
 	lockdep_assert_held(&vm->lock);
 
-	xe_vm_lock(vm, false);
+	if (unlocked)
+		xe_vm_lock(vm, false);
+

Can you explain why we need xe_vm_lock in the first place here?

My understanding is, xe_vm_lock protect gpu page table update. Below kill function eventually calls into guc_exec_queue_kill where I don't see any page table operation there. So I doubt whether we need the lock in the first place.

Quoted 171 lines
drivers/gpu/drm/xe/xe_vm.c +4 -10
 	vm->flags |= XE_VM_FLAG_BANNED;
 	trace_xe_vm_kill(vm);
 
 	list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
 		q->ops->kill(q);
-	xe_vm_unlock(vm);
+
+	if (unlocked)
+		xe_vm_unlock(vm);
 
 	/* TODO: Inform user the VM is banned */
 }
@@ -621,7 +625,7 @@ static void preempt_rebind_work_func(struct work_struct
 *w)
 
 	if (err) {
 		drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
-		xe_vm_kill(vm);
+		xe_vm_kill(vm, true);
 	}
 	up_write(&vm->lock);
@@ -1831,17 +1835,9 @@ static int xe_vm_bind(struct xe_vm *vm, struct xe_vma
 *vma, struct xe_exec_queue
 		      u32 num_syncs, bool immediate, bool first_op,
 		      bool last_op)
 {
-	int err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
-	if (bo && immediate) {
-		err = xe_bo_validate(bo, vm, true);
-		if (err)
-			return err;
-	}
-
 	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate, first_op,
 			    last_op);

}

drivers/gpu/drm/xe/xe_vm.c +69 -25
@@ -2488,17 +2484,12 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
-		      struct xe_vma *vma, struct xe_vma_op *op)
+static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		      struct xe_vma_op *op)
 {
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
-
-	err = xe_vm_prepare_vma(exec, vma, 1);
-	if (err)
-		return err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -2579,19 +2570,10 @@ static int op_execute(struct drm_exec *exec, struct
 xe_vm *vm,
 static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 			       struct xe_vma_op *op)
 {
-	struct drm_exec exec;
 	int err;
 
 retry_userptr:
-	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
-	drm_exec_until_all_locked(&exec) {
-		err = op_execute(&exec, vm, vma, op);
-		drm_exec_retry_on_contention(&exec);
-		if (err)
-			break;
-	}
-	drm_exec_fini(&exec);
-
+	err = op_execute(vm, vma, op);
 	if (err == -EAGAIN) {
 		lockdep_assert_held_write(&vm->lock);
@@ -2756,29 +2738,107 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm
 *vm,
 	}
 }
 
+static int vma_lock(struct drm_exec *exec, struct xe_vma *vma, bool validate)
+{
+	struct xe_bo *bo = xe_vma_bo(vma);
+	int err = 0;
+
+	if (bo) {
+		if (!bo->vm)
+			err = drm_exec_prepare_obj(exec, &bo->ttm.base, 1);
+		if (!err && validate)
+			err = xe_bo_validate(bo, xe_vma_vm(vma), true);
+	}
+
+	return err;
+}
+
+static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
+		   struct xe_vma_op *op)
+{
+	int err = 0;
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		err = vma_lock(exec, op->map.vma, !xe_vm_in_fault_mode(vm));
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap->va),
+			       false);
+		if (!err && op->remap.prev)
+			err = vma_lock(exec, op->remap.prev, true);
+		if (!err && op->remap.next)
+			err = vma_lock(exec, op->remap.next, true);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va), true);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
+
+static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
+				  struct xe_vm *vm,
+				  struct list_head *ops_list)
+{
+	struct xe_vma_op *op;
+	int err;
+
+	err = drm_exec_prepare_obj(exec, xe_vm_obj(vm), 1);
+	if (err)
+		return err;
+
+	list_for_each_entry(op, ops_list, link) {
+		err = op_lock(exec, vm, op);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
+	struct drm_exec exec;
 	struct xe_vma_op *op, *next;
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
-		err = xe_vma_op_execute(vm, op);
-		if (err) {
-			drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
-				 op->base.op, err);
-			/*
-			 * FIXME: Killing VM rather than proper error handling
-			 */

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm · line 2823

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +8 -2
-			xe_vm_kill(vm);
-			return -ENOSPC;
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
+		      DRM_EXEC_IGNORE_DUPLICATES, 0);
+	drm_exec_until_all_locked(&exec) {
+		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			goto unlock;
+

Do you need below ops inside the drm_exec_until_all_locked loop? After you locked all objects, you can close the drm_exec_until_all_locked loop, then perform below out of drm_exec_until_all_locked loop.

Oak

Quoted 28 lines
drivers/gpu/drm/xe/xe_vm.c +7 -0
+		list_for_each_entry_safe(op, next, ops_list, link) {
+			err = xe_vma_op_execute(vm, op);
+			if (err) {
+				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
+					 op->base.op, err);
+				/*
+				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +9 -3
+				 */
+				xe_vm_kill(vm, false);
+				err = -ENOSPC;
+				goto unlock;
+			}
+			xe_vma_op_cleanup(vm, op);
 		}
-		xe_vma_op_cleanup(vm, op);
 	}
 
-	return 0;
+unlock:
+	drm_exec_fini(&exec);
+	return err;
 }
 
 #define SUPPORTED_FLAGS	(DRM_XE_VM_BIND_FLAG_NULL | \
--

2.34.1

author maintainer
#3 lore raw

Re: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 10 lines · Oak
Quoted 10 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 7 lines
Quoted 1 line

Lock all gpuva ops

Can we have a better wording? Better to say locking all Bos used in gpuva ops?

Or maybe lock ops by locking and validating all Bos used in ops.

and validate all BOs in a single step durin the VM

Lock all BOs used in gpuva ops and validate all BOs in a single step during the VM bind IOCTL?

Quoted 6 lines
Quoted 2 lines

bind IOCTL. This help with the transition to making all gpuva ops in a VM bind IOCTL a single atomic job.

Can you also explain, why you want bind to be a atomic job?

My guess is, bind ioctl can end up with a series of operations, if some (not all) of those operations fail in the middle, it is hard to revert the successful operations before failure.

Yes, exactly. It is ensure if we fail at some point in the bind IOCTL (e.g. memory allocation failure) we can unwind to the initial state. Also another benefit is we get 1 fence per IOCTL which is installed dma-resv slots and returned to the user via out-syncs. Lastly, it logically makes sense that 1 IOCTL translates to 1 job.

The cover letter at some point explained this. I add something in the next rev.

Quoted 21 lines
Quoted 21 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 142 ++++++++++++++++++++++++++-----------
 1 file changed, 101 insertions(+), 41 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -1
index 643b3701a738..3b5dc6de07f7 100644
@@ -413,19 +413,23 @@ int __xe_vm_userptr_needs_repin(struct xe_vm *vm)
 
 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
 
-static void xe_vm_kill(struct xe_vm *vm)
+static void xe_vm_kill(struct xe_vm *vm, bool unlocked)
 {
 	struct xe_exec_queue *q;
 
 	lockdep_assert_held(&vm->lock);
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c int __xe_vm_userptr_needs_repin(struct xe_vm *vm) · line 424

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +3 -1
-	xe_vm_lock(vm, false);
+	if (unlocked)
+		xe_vm_lock(vm, false);
+

Can you explain why we need xe_vm_lock in the first place here?

My understanding is, xe_vm_lock protect gpu page table update. Below kill function eventually calls into guc_exec_queue_kill where I don't see any page table operation there. So I doubt whether we need the lock in the first place.

I think it it protect vm->preempt.exec_queues list but that could probably be reworked. For this series I'd rather leave the locking as is and then in follow up rework the locking a bit and fully document it.

Quoted 175 lines
Quoted 175 lines
drivers/gpu/drm/xe/xe_vm.c +4 -10
 	vm->flags |= XE_VM_FLAG_BANNED;
 	trace_xe_vm_kill(vm);
 
 	list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
 		q->ops->kill(q);
-	xe_vm_unlock(vm);
+
+	if (unlocked)
+		xe_vm_unlock(vm);
 
 	/* TODO: Inform user the VM is banned */
 }
@@ -621,7 +625,7 @@ static void preempt_rebind_work_func(struct work_struct
 *w)
 
 	if (err) {
 		drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
-		xe_vm_kill(vm);
+		xe_vm_kill(vm, true);
 	}
 	up_write(&vm->lock);
@@ -1831,17 +1835,9 @@ static int xe_vm_bind(struct xe_vm *vm, struct xe_vma
 *vma, struct xe_exec_queue
 		      u32 num_syncs, bool immediate, bool first_op,
 		      bool last_op)
 {
-	int err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
-	if (bo && immediate) {
-		err = xe_bo_validate(bo, vm, true);
-		if (err)
-			return err;
-	}
-
 	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate, first_op,
 			    last_op);

}

drivers/gpu/drm/xe/xe_vm.c +71 -27
@@ -2488,17 +2484,12 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
-		      struct xe_vma *vma, struct xe_vma_op *op)
+static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		      struct xe_vma_op *op)
 {
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
-
-	err = xe_vm_prepare_vma(exec, vma, 1);
-	if (err)
-		return err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -2579,19 +2570,10 @@ static int op_execute(struct drm_exec *exec, struct
 xe_vm *vm,
 static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 			       struct xe_vma_op *op)
 {
-	struct drm_exec exec;
 	int err;
 
 retry_userptr:
-	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
-	drm_exec_until_all_locked(&exec) {
-		err = op_execute(&exec, vm, vma, op);
-		drm_exec_retry_on_contention(&exec);
-		if (err)
-			break;
-	}
-	drm_exec_fini(&exec);
-
+	err = op_execute(vm, vma, op);
 	if (err == -EAGAIN) {
 		lockdep_assert_held_write(&vm->lock);
@@ -2756,29 +2738,107 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm
 *vm,
 	}
 }
 
+static int vma_lock(struct drm_exec *exec, struct xe_vma *vma, bool validate)
+{
+	struct xe_bo *bo = xe_vma_bo(vma);
+	int err = 0;
+
+	if (bo) {
+		if (!bo->vm)
+			err = drm_exec_prepare_obj(exec, &bo->ttm.base, 1);
+		if (!err && validate)
+			err = xe_bo_validate(bo, xe_vma_vm(vma), true);
+	}
+
+	return err;
+}
+
+static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
+		   struct xe_vma_op *op)
+{
+	int err = 0;
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		err = vma_lock(exec, op->map.vma, !xe_vm_in_fault_mode(vm));
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap->va),
+			       false);
+		if (!err && op->remap.prev)
+			err = vma_lock(exec, op->remap.prev, true);
+		if (!err && op->remap.next)
+			err = vma_lock(exec, op->remap.next, true);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va), true);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
+
+static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
+				  struct xe_vm *vm,
+				  struct list_head *ops_list)
+{
+	struct xe_vma_op *op;
+	int err;
+
+	err = drm_exec_prepare_obj(exec, xe_vm_obj(vm), 1);
+	if (err)
+		return err;
+
+	list_for_each_entry(op, ops_list, link) {
+		err = op_lock(exec, vm, op);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
+	struct drm_exec exec;
 	struct xe_vma_op *op, *next;
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
-		err = xe_vma_op_execute(vm, op);
-		if (err) {
-			drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
-				 op->base.op, err);
-			/*
-			 * FIXME: Killing VM rather than proper error handling
-			 */
-			xe_vm_kill(vm);
-			return -ENOSPC;
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
+		      DRM_EXEC_IGNORE_DUPLICATES, 0);
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm, · line 2822

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +6 -0
+	drm_exec_until_all_locked(&exec) {
+		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			goto unlock;
+

Do you need below ops inside the drm_exec_until_all_locked loop? After you locked all objects, you can close the drm_exec_until_all_locked loop, then perform below out of drm_exec_until_all_locked loop.

Yes, xe_vma_op_execute can GPU allocate memory. GPU memory allocations can trigger evictions which try to grab more locks. Once the drm_exec_until_all_locked loop is broken we are not allowed try grab more locks. Also eventually on memory allocations I think we will pass in drm_exec state so the looking can hook into that.

Matt

Quoted 31 lines

Oak

Quoted 28 lines
drivers/gpu/drm/xe/xe_vm.c +7 -0
+		list_for_each_entry_safe(op, next, ops_list, link) {
+			err = xe_vma_op_execute(vm, op);
+			if (err) {
+				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
+					 op->base.op, err);
+				/*
+				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +9 -3
+				 */
+				xe_vm_kill(vm, false);
+				err = -ENOSPC;
+				goto unlock;
+			}
+			xe_vma_op_cleanup(vm, op);
 		}
-		xe_vma_op_cleanup(vm, op);
 	}
 
-	return 0;
+unlock:
+	drm_exec_fini(&exec);
+	return err;
 }
 
 #define SUPPORTED_FLAGS	(DRM_XE_VM_BIND_FLAG_NULL | \
--

2.34.1

#4 lore raw

RE: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 25 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Monday, March 11, 2024 3:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 4 lines · Oak
Quoted 4 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 10 lines
Quoted 7 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Lock all gpuva ops

Can we have a better wording? Better to say locking all Bos used in gpuva ops?

Quoted 7 lines
Quoted 3 lines

Or maybe lock ops by locking and validating all Bos used in ops.

and validate all BOs in a single step durin the VM

Lock all BOs used in gpuva ops and validate all BOs in a single step during the VM bind IOCTL?

Yes, that looks better.

Quoted 49 lines
Quoted 7 lines
Quoted 3 lines

bind IOCTL. This help with the transition to making all gpuva ops in a VM bind IOCTL a single atomic job.

Can you also explain, why you want bind to be a atomic job?

My guess is, bind ioctl can end up with a series of operations, if some (not all) of

those operations fail in the middle, it is hard to revert the successful operations before failure.

Yes, exactly. It is ensure if we fail at some point in the bind IOCTL (e.g. memory allocation failure) we can unwind to the initial state. Also another benefit is we get 1 fence per IOCTL which is installed dma-resv slots and returned to the user via out-syncs. Lastly, it logically makes sense that 1 IOCTL translates to 1 job.

The cover letter at some point explained this. I add something in the next rev.

Quoted 4 lines
Quoted 4 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 142 ++++++++++++++++++++++++++---------

--

Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c int __xe_vm_userptr_needs_repin(struct xe_vm

Quoted 7 lines
 1 file changed, 101 insertions(+), 41 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 643b3701a738..3b5dc6de07f7 100644
@@ -413,19 +413,23 @@ int __xe_vm_userptr_needs_repin(struct xe_vm

*vm)

Quoted 17 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
 
-static void xe_vm_kill(struct xe_vm *vm)

drivers/gpu/drm/xe/xe_vm.c int __xe_vm_userptr_needs_repin(struct xe_vm *vm) · line 424

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +4 -1
+static void xe_vm_kill(struct xe_vm *vm, bool unlocked)
 {
 	struct xe_exec_queue *q;
 
 	lockdep_assert_held(&vm->lock);
 
-	xe_vm_lock(vm, false);
+	if (unlocked)
+		xe_vm_lock(vm, false);
+

Can you explain why we need xe_vm_lock in the first place here?

Quoted 8 lines
Quoted 1 line

My understanding is, xe_vm_lock protect gpu page table update. Below kill

function eventually calls into guc_exec_queue_kill where I don't see any page table operation there. So I doubt whether we need the lock in the first place.

Quoted 1 line

I think it it protect vm->preempt.exec_queues list but that could probably be reworked. For this series I'd rather leave the locking as is and then in follow up rework the locking a bit and fully document it.

Isn't that vm->pre-empt.exec_queues is protected by vm::lock (the rw) semaphore? Xe_vm_lock here is vm's dma resv.

I agree we can rework those locks later.

Quoted 194 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +3 -1
 	vm->flags |= XE_VM_FLAG_BANNED;
 	trace_xe_vm_kill(vm);
 
 	list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
 		q->ops->kill(q);
-	xe_vm_unlock(vm);
+
+	if (unlocked)
+		xe_vm_unlock(vm);
 
Quoted 3 lines

drivers/gpu/drm/xe/xe_vm.c static void preempt_rebind_work_func(struct

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	/* TODO: Inform user the VM is banned */
 }
@@ -621,7 +625,7 @@ static void preempt_rebind_work_func(struct

work_struct

Quoted 10 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vm_bind(struct xe_vm *vm, struct

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
 *w)
 
 	if (err) {
 		drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
-		xe_vm_kill(vm);
+		xe_vm_kill(vm, true);
 	}
 	up_write(&vm->lock);
@@ -1831,17 +1835,9 @@ static int xe_vm_bind(struct xe_vm *vm, struct

xe_vma

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -2
 *vma, struct xe_exec_queue
 		      u32 num_syncs, bool immediate, bool first_op,
 		      bool last_op)
 {
-	int err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +0 -6
-	if (bo && immediate) {
-		err = xe_bo_validate(bo, vm, true);
-		if (err)
-			return err;
-	}
-
 	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate,

first_op,

Quoted 3 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_parse(struct xe_vm

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 			    last_op);

}

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2488,17 +2484,12 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm

*vm,

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/xe/xe_vm.c +2 -7
 struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
-		      struct xe_vma *vma, struct xe_vma_op *op)
+static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		      struct xe_vma_op *op)
 {
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
-
-	err = xe_vm_prepare_vma(exec, vma, 1);
-	if (err)
-		return err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
Quoted 1 line

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct drm_exec *exec,

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2579,19 +2570,10 @@ static int op_execute(struct drm_exec *exec,

struct

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/xe/xe_vm.c +1 -10
 xe_vm *vm,
 static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 			       struct xe_vma_op *op)
 {
-	struct drm_exec exec;
 	int err;
 
 retry_userptr:
-	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
-	drm_exec_until_all_locked(&exec) {
-		err = op_execute(&exec, vm, vma, op);
-		drm_exec_retry_on_contention(&exec);
-		if (err)
-			break;
-	}
-	drm_exec_fini(&exec);
-
+	err = op_execute(vm, vma, op);
 	if (err == -EAGAIN) {
 		lockdep_assert_held_write(&vm->lock);
Quoted 1 line

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2756,29 +2738,107 @@ static void vm_bind_ioctl_ops_unwind(struct

xe_vm

Quoted 5 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct · line 2742

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
 *vm,
 	}
 }
 
+static int vma_lock(struct drm_exec *exec, struct xe_vma *vma, bool

validate)

Quoted 27 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_vm.c +12 -0
+{
+	struct xe_bo *bo = xe_vma_bo(vma);
+	int err = 0;
+
+	if (bo) {
+		if (!bo->vm)
+			err = drm_exec_prepare_obj(exec, &bo->ttm.base, 1);
+		if (!err && validate)
+			err = xe_bo_validate(bo, xe_vma_vm(vma), true);
+	}
+
+	return err;

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +10 -0
+}
+
+static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
+		   struct xe_vma_op *op)
+{
+	int err = 0;
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		err = vma_lock(exec, op-

map.vma, !xe_vm_in_fault_mode(vm));

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap-

va),

Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,

Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +11 -0
+			       false);
+		if (!err && op->remap.prev)
+			err = vma_lock(exec, op->remap.prev, true);
+		if (!err && op->remap.next)
+			err = vma_lock(exec, op->remap.next, true);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va),

true);

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/xe/xe_vm.c +31 -10
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
+
+static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
+				  struct xe_vm *vm,
+				  struct list_head *ops_list)
+{
+	struct xe_vma_op *op;
+	int err;
+
+	err = drm_exec_prepare_obj(exec, xe_vm_obj(vm), 1);
+	if (err)
+		return err;
+
+	list_for_each_entry(op, ops_list, link) {
+		err = op_lock(exec, vm, op);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
+	struct drm_exec exec;
 	struct xe_vma_op *op, *next;
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
-		err = xe_vma_op_execute(vm, op);
-		if (err) {
-			drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
-				 op->base.op, err);
-			/*
-			 * FIXME: Killing VM rather than proper error handling
-			 */
-			xe_vm_kill(vm);
-			return -ENOSPC;
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
+		      DRM_EXEC_IGNORE_DUPLICATES, 0);
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm, · line 2822

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +6 -0
+	drm_exec_until_all_locked(&exec) {
+		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			goto unlock;
+

Do you need below ops inside the drm_exec_until_all_locked loop? After you

Quoted 8 lines

locked all objects, you can close the drm_exec_until_all_locked loop, then perform below out of drm_exec_until_all_locked loop.

Quoted 1 line

Yes, xe_vma_op_execute can GPU allocate memory. GPU memory allocations can trigger evictions which try to grab more locks. Once the drm_exec_until_all_locked loop is broken we are not allowed try grab more locks.

I see. So you need xe_vma_op_execute in the drm_exec_until_all_locked loop to grab more locks.

Also eventually on memory allocations I think we will pass

Quoted 1 line

in drm_exec state so the looking can hook into that.

Then I guess you need a exec parameter in xe_vma_op_execute, so eventually you can grab more locks in memory allocation?

Oak

Quoted 34 lines

Matt

Quoted 31 lines

Oak

Quoted 28 lines
drivers/gpu/drm/xe/xe_vm.c +7 -0
+		list_for_each_entry_safe(op, next, ops_list, link) {
+			err = xe_vma_op_execute(vm, op);
+			if (err) {
+				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
+					 op->base.op, err);
+				/*
+				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +9 -3
+				 */
+				xe_vm_kill(vm, false);
+				err = -ENOSPC;
+				goto unlock;
+			}
+			xe_vma_op_cleanup(vm, op);
 		}
-		xe_vma_op_cleanup(vm, op);
 	}
 
-	return 0;
+unlock:
+	drm_exec_fini(&exec);
+	return err;
 }
 
 #define SUPPORTED_FLAGS	(DRM_XE_VM_BIND_FLAG_NULL | \
--

2.34.1

author maintainer
#5 lore raw

Re: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 89 lines · Oak
Quoted 25 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Monday, March 11, 2024 3:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Quoted 4 lines · Oak
Quoted 4 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 10 lines
Quoted 7 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 01/30] drm/xe: Lock all gpuva ops during VM bind IOCTL

Lock all gpuva ops

Can we have a better wording? Better to say locking all Bos used in gpuva ops?

Quoted 7 lines
Quoted 3 lines

Or maybe lock ops by locking and validating all Bos used in ops.

and validate all BOs in a single step durin the VM

Lock all BOs used in gpuva ops and validate all BOs in a single step during the VM bind IOCTL?

Yes, that looks better.

Quoted 54 lines
Quoted 7 lines
Quoted 3 lines

bind IOCTL. This help with the transition to making all gpuva ops in a VM bind IOCTL a single atomic job.

Can you also explain, why you want bind to be a atomic job?

My guess is, bind ioctl can end up with a series of operations, if some (not all) of

those operations fail in the middle, it is hard to revert the successful operations before failure.

Yes, exactly. It is ensure if we fail at some point in the bind IOCTL (e.g. memory allocation failure) we can unwind to the initial state. Also another benefit is we get 1 fence per IOCTL which is installed dma-resv slots and returned to the user via out-syncs. Lastly, it logically makes sense that 1 IOCTL translates to 1 job.

The cover letter at some point explained this. I add something in the next rev.

Quoted 4 lines
Quoted 4 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 142 ++++++++++++++++++++++++++---------

--

Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c int __xe_vm_userptr_needs_repin(struct xe_vm

Quoted 7 lines
 1 file changed, 101 insertions(+), 41 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 643b3701a738..3b5dc6de07f7 100644
@@ -413,19 +413,23 @@ int __xe_vm_userptr_needs_repin(struct xe_vm

*vm)

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
 
-static void xe_vm_kill(struct xe_vm *vm)
+static void xe_vm_kill(struct xe_vm *vm, bool unlocked)
 {
 	struct xe_exec_queue *q;
 
 	lockdep_assert_held(&vm->lock);
 
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c int __xe_vm_userptr_needs_repin(struct xe_vm *vm) · line 424

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +3 -1
-	xe_vm_lock(vm, false);
+	if (unlocked)
+		xe_vm_lock(vm, false);
+

Can you explain why we need xe_vm_lock in the first place here?

My understanding is, xe_vm_lock protect gpu page table update. Below kill

function eventually calls into guc_exec_queue_kill where I don't see any page table operation there. So I doubt whether we need the lock in the first place.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

I think it it protect vm->preempt.exec_queues list but that could probably be reworked. For this series I'd rather leave the locking as is and then in follow up rework the locking a bit and fully document it.

Isn't that vm->pre-empt.exec_queues is protected by vm::lock (the rw) semaphore? Xe_vm_lock here is vm's dma resv.

It isn't really all that clear but since this is in the existing code let's just leave it until we rework / document the locking.

I'll likely add a new patch in next rev at the end of the series which works on the locking a bit...

Quoted 201 lines

I agree we can rework those locks later.

Quoted 198 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +3 -1
 	vm->flags |= XE_VM_FLAG_BANNED;
 	trace_xe_vm_kill(vm);
 
 	list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
 		q->ops->kill(q);
-	xe_vm_unlock(vm);
+
+	if (unlocked)
+		xe_vm_unlock(vm);
 
Quoted 3 lines

drivers/gpu/drm/xe/xe_vm.c static void preempt_rebind_work_func(struct

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	/* TODO: Inform user the VM is banned */
 }
@@ -621,7 +625,7 @@ static void preempt_rebind_work_func(struct

work_struct

Quoted 10 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vm_bind(struct xe_vm *vm, struct

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
 *w)
 
 	if (err) {
 		drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
-		xe_vm_kill(vm);
+		xe_vm_kill(vm, true);
 	}
 	up_write(&vm->lock);
@@ -1831,17 +1835,9 @@ static int xe_vm_bind(struct xe_vm *vm, struct

xe_vma

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -2
 *vma, struct xe_exec_queue
 		      u32 num_syncs, bool immediate, bool first_op,
 		      bool last_op)
 {
-	int err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +0 -6
-	if (bo && immediate) {
-		err = xe_bo_validate(bo, vm, true);
-		if (err)
-			return err;
-	}
-
 	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate,

first_op,

Quoted 3 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_parse(struct xe_vm

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 			    last_op);

}

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2488,17 +2484,12 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm

*vm,

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/xe/xe_vm.c +2 -7
 struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
-		      struct xe_vma *vma, struct xe_vma_op *op)
+static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		      struct xe_vma_op *op)
 {
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
-
-	err = xe_vm_prepare_vma(exec, vma, 1);
-	if (err)
-		return err;
-
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
Quoted 1 line

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct drm_exec *exec,

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2579,19 +2570,10 @@ static int op_execute(struct drm_exec *exec,

struct

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/xe/xe_vm.c +1 -10
 xe_vm *vm,
 static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 			       struct xe_vma_op *op)
 {
-	struct drm_exec exec;
 	int err;
 
 retry_userptr:
-	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
-	drm_exec_until_all_locked(&exec) {
-		err = op_execute(&exec, vm, vma, op);
-		drm_exec_retry_on_contention(&exec);
-		if (err)
-			break;
-	}
-	drm_exec_fini(&exec);
-
+	err = op_execute(vm, vma, op);
 	if (err == -EAGAIN) {
 		lockdep_assert_held_write(&vm->lock);
Quoted 1 line

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2756,29 +2738,107 @@ static void vm_bind_ioctl_ops_unwind(struct

xe_vm

Quoted 5 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct · line 2742

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
 *vm,
 	}
 }
 
+static int vma_lock(struct drm_exec *exec, struct xe_vma *vma, bool

validate)

Quoted 27 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_vm.c +12 -0
+{
+	struct xe_bo *bo = xe_vma_bo(vma);
+	int err = 0;
+
+	if (bo) {
+		if (!bo->vm)
+			err = drm_exec_prepare_obj(exec, &bo->ttm.base, 1);
+		if (!err && validate)
+			err = xe_bo_validate(bo, xe_vma_vm(vma), true);
+	}
+
+	return err;

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +10 -0
+}
+
+static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
+		   struct xe_vma_op *op)
+{
+	int err = 0;
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		err = vma_lock(exec, op-

map.vma, !xe_vm_in_fault_mode(vm));

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap-

va),

Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,

Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +11 -0
+			       false);
+		if (!err && op->remap.prev)
+			err = vma_lock(exec, op->remap.prev, true);
+		if (!err && op->remap.next)
+			err = vma_lock(exec, op->remap.next, true);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va),

true);

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/xe/xe_vm.c +31 -10
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
+
+static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
+				  struct xe_vm *vm,
+				  struct list_head *ops_list)
+{
+	struct xe_vma_op *op;
+	int err;
+
+	err = drm_exec_prepare_obj(exec, xe_vm_obj(vm), 1);
+	if (err)
+		return err;
+
+	list_for_each_entry(op, ops_list, link) {
+		err = op_lock(exec, vm, op);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
+	struct drm_exec exec;
 	struct xe_vma_op *op, *next;
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
-		err = xe_vma_op_execute(vm, op);
-		if (err) {
-			drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
-				 op->base.op, err);
-			/*
-			 * FIXME: Killing VM rather than proper error handling
-			 */
-			xe_vm_kill(vm);
-			return -ENOSPC;
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
+		      DRM_EXEC_IGNORE_DUPLICATES, 0);
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm, · line 2822

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +6 -0
+	drm_exec_until_all_locked(&exec) {
+		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		drm_exec_retry_on_contention(&exec);
+		if (err)
+			goto unlock;
+

Do you need below ops inside the drm_exec_until_all_locked loop? After you

locked all objects, you can close the drm_exec_until_all_locked loop, then perform below out of drm_exec_until_all_locked loop.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Yes, xe_vma_op_execute can GPU allocate memory. GPU memory allocations can trigger evictions which try to grab more locks. Once the drm_exec_until_all_locked loop is broken we are not allowed try grab more locks.

I see. So you need xe_vma_op_execute in the drm_exec_until_all_locked loop to grab more locks.

Yes.

Quoted 6 lines

Also eventually on memory allocations I think we will pass

Quoted 1 line

in drm_exec state so the looking can hook into that.

Then I guess you need a exec parameter in xe_vma_op_execute, so eventually you can grab more locks in memory allocation?

This gets reworkeed later in the series where xe_vma_ops is passed around everywhere (VM, migrate, and PT layers) and we should probably includd the drm_exec as a member of xe_vma_ops.

Matt

Quoted 36 lines

Oak

Quoted 34 lines

Matt

Quoted 31 lines

Oak

Quoted 28 lines
drivers/gpu/drm/xe/xe_vm.c +7 -0
+		list_for_each_entry_safe(op, next, ops_list, link) {
+			err = xe_vma_op_execute(vm, op);
+			if (err) {
+				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
+					 op->base.op, err);
+				/*
+				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +9 -3
+				 */
+				xe_vm_kill(vm, false);
+				err = -ENOSPC;
+				goto unlock;
+			}
+			xe_vma_op_cleanup(vm, op);
 		}
-		xe_vma_op_cleanup(vm, op);
 	}
 
-	return 0;
+unlock:
+	drm_exec_fini(&exec);
+	return err;
 }
 
 #define SUPPORTED_FLAGS	(DRM_XE_VM_BIND_FLAG_NULL | \
--

2.34.1

author maintainer
#6 lore raw

PATCH v4 2/30 drm/xe: Add ops_execute function which returns a fence

In reply to Matthew Brost

Add ops_execute function which returns a fence. This will be helpful to initiate all binds (VM bind IOCTL, rebinds in exec IOCTL, rebinds in preempt rebind worker, and rebinds in pagefaults) via a gpuva ops list. Returning a fence is needed in various paths.

---
 drivers/gpu/drm/xe/xe_vm.c | 211 +++++++++++++++++++------------------
 1 file changed, 111 insertions(+), 100 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +111 -100
index 3b5dc6de07f7..fb73afcab3b7 100644
@@ -1789,16 +1789,17 @@ find_ufence_get(struct xe_sync_entry *syncs, u32 num_syncs)
 	return NULL;
 }
 
-static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool immediate, bool first_op,
-			bool last_op)
+static struct dma_fence *
+xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
+	   struct xe_bo *bo, struct xe_sync_entry *syncs, u32 num_syncs,
+	   bool immediate, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
 	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
+	xe_bo_assert_held(bo);
 
 	ufence = find_ufence_get(syncs, num_syncs);
 	if (vma->ufence && ufence)
@@ -1810,7 +1811,7 @@ static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, first_op,
 				       last_op);
 		if (IS_ERR(fence))
-			return PTR_ERR(fence);
+			return fence;
 	} else {
 		int i;
 
@@ -1825,26 +1826,14 @@ static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
-
-	return 0;
-}
-
-static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
-		      struct xe_bo *bo, struct xe_sync_entry *syncs,
-		      u32 num_syncs, bool immediate, bool first_op,
-		      bool last_op)
-{
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(bo);
 
-	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate, first_op,
-			    last_op);
+	return fence;
 }
 
-static int xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_unbind(struct xe_vm *vm, 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 *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
@@ -1854,14 +1843,13 @@ static int xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 
 	fence = xe_vm_unbind_vma(vma, q, syncs, num_syncs, first_op, last_op);
 	if (IS_ERR(fence))
-		return PTR_ERR(fence);
+		return fence;
 
 	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
 
-	return 0;
+	return fence;
 }
 
 #define ALL_DRM_XE_VM_CREATE_FLAGS (DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \
@@ -2004,10 +1992,11 @@ static const u32 region_to_mem_type[] = {
 	XE_PL_VRAM1,
 };
 
-static int xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-			  struct xe_exec_queue *q, u32 region,
-			  struct xe_sync_entry *syncs, u32 num_syncs,
-			  bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
+	       struct xe_exec_queue *q, u32 region,
+	       struct xe_sync_entry *syncs, u32 num_syncs,
+	       bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
 	int err;
@@ -2017,27 +2006,24 @@ static int xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
 	if (!xe_vma_has_no_bo(vma)) {
 		err = xe_bo_migrate(xe_vma_bo(vma), region_to_mem_type[region]);
 		if (err)
-			return err;
+			return ERR_PTR(err);
 	}
 
 	if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated)) {
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs, num_syncs,
 				  true, first_op, last_op);
 	} else {
+		struct dma_fence *fence =
+			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 		int i;
 
 		/* Nothing to do, signal fences now */
 		if (last_op) {
-			for (i = 0; i < num_syncs; i++) {
-				struct dma_fence *fence =
-					xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-
+			for (i = 0; i < num_syncs; i++)
 				xe_sync_entry_signal(&syncs[i], NULL, fence);
-				dma_fence_put(fence);
-			}
 		}
 
-		return 0;
+		return fence;
 	}
 }
 
@@ -2484,10 +2470,10 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
-		      struct xe_vma_op *op)
+static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
+				    struct xe_vma_op *op)
 {
-	int err;
+	struct dma_fence *fence = NULL;
 
 	lockdep_assert_held_write(&vm->lock);
 	xe_vm_assert_held(vm);
@@ -2495,11 +2481,11 @@ static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		err = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
-				 op->syncs, op->num_syncs,
-				 !xe_vm_in_fault_mode(vm),
-				 op->flags & XE_VMA_OP_FIRST,
-				 op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
+				   op->syncs, op->num_syncs,
+				   !xe_vm_in_fault_mode(vm),
+				   op->flags & XE_VMA_OP_FIRST,
+				   op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_REMAP:
 	{
@@ -2509,37 +2495,39 @@ static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
 		if (!op->remap.unmap_done) {
 			if (prev || next)
 				vma->gpuva.flags |= XE_VMA_FIRST_REBIND;
-			err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-					   op->num_syncs,
-					   op->flags & XE_VMA_OP_FIRST,
-					   op->flags & XE_VMA_OP_LAST &&
-					   !prev && !next);
-			if (err)
+			fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+					     op->num_syncs,
+					     op->flags & XE_VMA_OP_FIRST,
+					     op->flags & XE_VMA_OP_LAST &&
+					     !prev && !next);
+			if (IS_ERR(fence))
 				break;
 			op->remap.unmap_done = true;
 		}
 
 		if (prev) {
 			op->remap.prev->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.prev, op->q,
-					 xe_vma_bo(op->remap.prev), op->syncs,
-					 op->num_syncs, true, false,
-					 op->flags & XE_VMA_OP_LAST && !next);
+			dma_fence_put(fence);
+			fence = xe_vm_bind(vm, op->remap.prev, op->q,
+					   xe_vma_bo(op->remap.prev), op->syncs,
+					   op->num_syncs, true, false,
+					   op->flags & XE_VMA_OP_LAST && !next);
 			op->remap.prev->gpuva.flags &= ~XE_VMA_LAST_REBIND;
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.prev = NULL;
 		}
 
 		if (next) {
 			op->remap.next->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.next, op->q,
-					 xe_vma_bo(op->remap.next),
-					 op->syncs, op->num_syncs,
-					 true, false,
-					 op->flags & XE_VMA_OP_LAST);
+			dma_fence_put(fence);
+			fence = xe_vm_bind(vm, op->remap.next, op->q,
+					   xe_vma_bo(op->remap.next),
+					   op->syncs, op->num_syncs,
+					   true, false,
+					   op->flags & XE_VMA_OP_LAST);
 			op->remap.next->gpuva.flags &= ~XE_VMA_LAST_REBIND;
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.next = NULL;
 		}
@@ -2547,34 +2535,36 @@ static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-				   op->num_syncs, op->flags & XE_VMA_OP_FIRST,
-				   op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+				     op->num_syncs, op->flags & XE_VMA_OP_FIRST,
+				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				     op->syncs, op->num_syncs,
-				     op->flags & XE_VMA_OP_FIRST,
-				     op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
+				       op->syncs, op->num_syncs,
+				       op->flags & XE_VMA_OP_FIRST,
+				       op->flags & XE_VMA_OP_LAST);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	if (err)
+	if (IS_ERR(fence))
 		trace_xe_vma_fail(vma);
 
-	return err;
+	return fence;
 }
 
-static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
-			       struct xe_vma_op *op)
+static struct dma_fence *
+__xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		    struct xe_vma_op *op)
 {
+	struct dma_fence *fence;
 	int err;
 
 retry_userptr:
-	err = op_execute(vm, vma, op);
-	if (err == -EAGAIN) {
+	fence = op_execute(vm, vma, op);
+	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
 		lockdep_assert_held_write(&vm->lock);
 
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
@@ -2591,22 +2581,24 @@ static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 			if (!err)
 				goto retry_userptr;
 
+			fence = ERR_PTR(err);
 			trace_xe_vma_fail(vma);
 		}
 	}
 
-	return err;
+	return fence;
 }
 
-static int xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
+static struct dma_fence *
+xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 {
-	int ret = 0;
+	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
 	lockdep_assert_held_write(&vm->lock);
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		ret = __xe_vma_op_execute(vm, op->map.vma, op);
+		fence = __xe_vma_op_execute(vm, op->map.vma, op);
 		break;
 	case DRM_GPUVA_OP_REMAP:
 	{
@@ -2619,23 +2611,23 @@ static int xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 		else
 			vma = op->remap.next;
 
-		ret = __xe_vma_op_execute(vm, vma, op);
+		fence = __xe_vma_op_execute(vm, vma, op);
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		ret = __xe_vma_op_execute(vm, gpuva_to_vma(op->base.unmap.va),
-					  op);
+		fence = __xe_vma_op_execute(vm, gpuva_to_vma(op->base.unmap.va),
+					    op);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		ret = __xe_vma_op_execute(vm,
-					  gpuva_to_vma(op->base.prefetch.va),
-					  op);
+		fence = __xe_vma_op_execute(vm,
+					    gpuva_to_vma(op->base.prefetch.va),
+					    op);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	return ret;
+	return fence;
 }
 
 static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)
@@ -2803,11 +2795,35 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
 	return 0;
 }
 
+static struct dma_fence *ops_execute(struct xe_vm *vm,
+				     struct list_head *ops_list,
+				     bool cleanup)
+{
+	struct xe_vma_op *op, *next;
+	struct dma_fence *fence = NULL;
+
+	list_for_each_entry_safe(op, next, ops_list, link) {
+		if (!IS_ERR(fence)) {
+			dma_fence_put(fence);
+			fence = xe_vma_op_execute(vm, op);
+		}
+		if (IS_ERR(fence)) {
+			drm_warn(&vm->xe->drm, "VM op(%d) failed with %ld",
+				 op->base.op, PTR_ERR(fence));
+			fence = ERR_PTR(-ENOSPC);
+		}
+		if (cleanup)
+			xe_vma_op_cleanup(vm, op);
+	}
+
+	return fence;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
 	struct drm_exec exec;
-	struct xe_vma_op *op, *next;
+	struct dma_fence *fence;
 	int err;
 
 	lockdep_assert_held_write(&vm->lock);
@@ -2820,19 +2836,14 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 		if (err)
 			goto unlock;
 
-		list_for_each_entry_safe(op, next, ops_list, link) {
-			err = xe_vma_op_execute(vm, op);
-			if (err) {
-				drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
-					 op->base.op, err);
-				/*
-				 * FIXME: Killing VM rather than proper error handling
-				 */
-				xe_vm_kill(vm, false);
-				err = -ENOSPC;
-				goto unlock;
-			}
-			xe_vma_op_cleanup(vm, op);
+		fence = ops_execute(vm, ops_list, true);
+		if (IS_ERR(fence)) {
+			err = PTR_ERR(fence);
+			/* FIXME: Killing VM rather than proper error handling */
+			xe_vm_kill(vm, false);
+			goto unlock;
+		} else {
+			dma_fence_put(fence);
 		}
 	}
 
Signature · 1 line
2.34.1
#7 lore raw

RE: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Quoted 222 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Add ops_execute function which returns a fence. This will be helpful to initiate all binds (VM bind IOCTL, rebinds in exec IOCTL, rebinds in preempt rebind worker, and rebinds in pagefaults) via a gpuva ops list. Returning a fence is needed in various paths.

---
 drivers/gpu/drm/xe/xe_vm.c | 211 +++++++++++++++++++------------------
 1 file changed, 111 insertions(+), 100 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -4
index 3b5dc6de07f7..fb73afcab3b7 100644
@@ -1789,16 +1789,17 @@ find_ufence_get(struct xe_sync_entry *syncs, u32
 num_syncs)
 	return NULL;
 }
 
-static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool immediate, bool first_op,
-			bool last_op)
+static struct dma_fence *
+xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
+	   struct xe_bo *bo, struct xe_sync_entry *syncs, u32 num_syncs,
+	   bool immediate, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
 	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
+	xe_bo_assert_held(bo);
 
drivers/gpu/drm/xe/xe_vm.c +6 -18
 	ufence = find_ufence_get(syncs, num_syncs);
 	if (vma->ufence && ufence)
@@ -1810,7 +1811,7 @@ static int __xe_vm_bind(struct xe_vm *vm, struct
 xe_vma *vma,
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, first_op,
 				       last_op);
 		if (IS_ERR(fence))
-			return PTR_ERR(fence);
+			return fence;
 	} else {
 		int i;
@@ -1825,26 +1826,14 @@ static int __xe_vm_bind(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
-
-	return 0;
-}
-
-static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct
 xe_exec_queue *q,
-		      struct xe_bo *bo, struct xe_sync_entry *syncs,
-		      u32 num_syncs, bool immediate, bool first_op,
-		      bool last_op)
-{
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(bo);
 
-	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate,
 first_op,
-			    last_op);
+	return fence;
 }
 
-static int xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
+	     struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	     u32 num_syncs, bool first_op, bool last_op)

{

drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,

q);

drivers/gpu/drm/xe/xe_vm.c +2 -3
@@ -1854,14 +1843,13 @@ static int xe_vm_unbind(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	fence = xe_vm_unbind_vma(vma, q, syncs, num_syncs, first_op,
 last_op);
 	if (IS_ERR(fence))
-		return PTR_ERR(fence);
+		return fence;
 
 	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
 
-	return 0;
+	return fence;
 }

#define ALL_DRM_XE_VM_CREATE_FLAGS
(DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \

drivers/gpu/drm/xe/xe_vm.c +5 -4
@@ -2004,10 +1992,11 @@ static const u32 region_to_mem_type[] = {
 	XE_PL_VRAM1,
 };
 
-static int xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-			  struct xe_exec_queue *q, u32 region,
-			  struct xe_sync_entry *syncs, u32 num_syncs,
-			  bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
+	       struct xe_exec_queue *q, u32 region,
+	       struct xe_sync_entry *syncs, u32 num_syncs,
+	       bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
drivers/gpu/drm/xe/xe_vm.c +8 -11
 	int err;
@@ -2017,27 +2006,24 @@ static int xe_vm_prefetch(struct xe_vm *vm, struct
 xe_vma *vma,
 	if (!xe_vma_has_no_bo(vma)) {
 		err = xe_bo_migrate(xe_vma_bo(vma),
 region_to_mem_type[region]);
 		if (err)
-			return err;
+			return ERR_PTR(err);
 	}
 
 	if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
 {
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
 				  true, first_op, last_op);
 	} else {
+		struct dma_fence *fence =
+			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 		int i;
 
 		/* Nothing to do, signal fences now */
 		if (last_op) {
-			for (i = 0; i < num_syncs; i++) {
-				struct dma_fence *fence =
-
 	xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-
+			for (i = 0; i < num_syncs; i++)
 				xe_sync_entry_signal(&syncs[i], NULL, fence);
-				dma_fence_put(fence);
-			}
 		}
 
-		return 0;
+		return fence;
 	}
 }
@@ -2484,10 +2470,10 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
-		      struct xe_vma_op *op)
+static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
+				    struct xe_vma_op *op)
 {
-	int err;
+	struct dma_fence *fence = NULL;
 
 	lockdep_assert_held_write(&vm->lock);
drivers/gpu/drm/xe/xe_vm.c +5 -5
 	xe_vm_assert_held(vm);
@@ -2495,11 +2481,11 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		err = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
-				 op->syncs, op->num_syncs,
-				 !xe_vm_in_fault_mode(vm),
-				 op->flags & XE_VMA_OP_FIRST,
-				 op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
+				   op->syncs, op->num_syncs,
+				   !xe_vm_in_fault_mode(vm),
+				   op->flags & XE_VMA_OP_FIRST,
+				   op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_REMAP:
drivers/gpu/drm/xe/xe_vm.c +6 -6
 	{
@@ -2509,37 +2495,39 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 		if (!op->remap.unmap_done) {
 			if (prev || next)
 				vma->gpuva.flags |= XE_VMA_FIRST_REBIND;
-			err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-					   op->num_syncs,
-					   op->flags & XE_VMA_OP_FIRST,
-					   op->flags & XE_VMA_OP_LAST &&
-					   !prev && !next);
-			if (err)
+			fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+					     op->num_syncs,
+					     op->flags & XE_VMA_OP_FIRST,
+					     op->flags & XE_VMA_OP_LAST &&
+					     !prev && !next);
+			if (IS_ERR(fence))
 				break;
 			op->remap.unmap_done = true;

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct xe_vm *vm, struct · line 2512

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +1 -4
 		}
 
 		if (prev) {
 			op->remap.prev->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.prev, op->q,
-					 xe_vma_bo(op->remap.prev), op->syncs,
-					 op->num_syncs, true, false,
-					 op->flags & XE_VMA_OP_LAST
 && !next);
+			dma_fence_put(fence);

So you drop the previous fence. I assume in later operation, we will need to wait dma-fence for the previous operation to complete. Is it safe to only wait for the last fence? Shouldn't we wait all the fences, such as chain the fences in some way?

Quoted 12 lines
drivers/gpu/drm/xe/xe_vm.c +5 -1
+			fence = xe_vm_bind(vm, op->remap.prev, op->q,
+					   xe_vma_bo(op->remap.prev), op-
 >syncs,
+					   op->num_syncs, true, false,
+					   op->flags & XE_VMA_OP_LAST
 && !next);
 			op->remap.prev->gpuva.flags &=
 ~XE_VMA_LAST_REBIND;
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.prev = NULL;

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct xe_vm *vm, struct xe_vma *vma, · line 2523

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +1 -5
 		}
 
 		if (next) {
 			op->remap.next->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.next, op->q,
-					 xe_vma_bo(op->remap.next),
-					 op->syncs, op->num_syncs,
-					 true, false,
-					 op->flags & XE_VMA_OP_LAST);
+			dma_fence_put(fence);

Same comment as above

Quoted 135 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+			fence = xe_vm_bind(vm, op->remap.next, op->q,
+					   xe_vma_bo(op->remap.next),
+					   op->syncs, op->num_syncs,
+					   true, false,
+					   op->flags & XE_VMA_OP_LAST);

op->remap.next->gpuva.flags &=
~XE_VMA_LAST_REBIND;

drivers/gpu/drm/xe/xe_vm.c +16 -14
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.next = NULL;
 		}
@@ -2547,34 +2535,36 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-				   op->num_syncs, op->flags &
 XE_VMA_OP_FIRST,
-				   op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+				     op->num_syncs, op->flags &
 XE_VMA_OP_FIRST,
+				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				     op->syncs, op->num_syncs,
-				     op->flags & XE_VMA_OP_FIRST,
-				     op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
+				       op->syncs, op->num_syncs,
+				       op->flags & XE_VMA_OP_FIRST,
+				       op->flags & XE_VMA_OP_LAST);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	if (err)
+	if (IS_ERR(fence))
 		trace_xe_vma_fail(vma);
 
-	return err;
+	return fence;
 }
 
-static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
-			       struct xe_vma_op *op)
+static struct dma_fence *
+__xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		    struct xe_vma_op *op)
 {
+	struct dma_fence *fence;
 	int err;
 
 retry_userptr:
-	err = op_execute(vm, vma, op);
-	if (err == -EAGAIN) {
+	fence = op_execute(vm, vma, op);
+	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
drivers/gpu/drm/xe/xe_vm.c +6 -4
 		lockdep_assert_held_write(&vm->lock);
 
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
@@ -2591,22 +2581,24 @@ static int __xe_vma_op_execute(struct xe_vm *vm,
 struct xe_vma *vma,
 			if (!err)
 				goto retry_userptr;
 
+			fence = ERR_PTR(err);
 			trace_xe_vma_fail(vma);
 		}
 	}
 
-	return err;
+	return fence;
 }
 
-static int xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
+static struct dma_fence *
+xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 {
-	int ret = 0;
+	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
 	lockdep_assert_held_write(&vm->lock);
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		ret = __xe_vma_op_execute(vm, op->map.vma, op);
+		fence = __xe_vma_op_execute(vm, op->map.vma, op);
 		break;
 	case DRM_GPUVA_OP_REMAP:
drivers/gpu/drm/xe/xe_vm.c +7 -7
 	{
@@ -2619,23 +2611,23 @@ static int xe_vma_op_execute(struct xe_vm *vm,
 struct xe_vma_op *op)
 		else
 			vma = op->remap.next;
 
-		ret = __xe_vma_op_execute(vm, vma, op);
+		fence = __xe_vma_op_execute(vm, vma, op);
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		ret = __xe_vma_op_execute(vm, gpuva_to_vma(op-
 >base.unmap.va),
-					  op);
+		fence = __xe_vma_op_execute(vm, gpuva_to_vma(op-
 >base.unmap.va),
+					    op);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		ret = __xe_vma_op_execute(vm,
-					  gpuva_to_vma(op->base.prefetch.va),
-					  op);
+		fence = __xe_vma_op_execute(vm,
+					    gpuva_to_vma(op->base.prefetch.va),
+					    op);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	return ret;
+	return fence;

}

static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)

drivers/gpu/drm/xe/xe_vm.c +1 -0
@@ -2803,11 +2795,35 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 	return 0;
 }
 
+static struct dma_fence *ops_execute(struct xe_vm *vm,

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct drm_exec · line 2809

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +10 -0
+				     struct list_head *ops_list,
+				     bool cleanup)
+{
+	struct xe_vma_op *op, *next;
+	struct dma_fence *fence = NULL;
+
+	list_for_each_entry_safe(op, next, ops_list, link) {
+		if (!IS_ERR(fence)) {
+			dma_fence_put(fence);
+			fence = xe_vma_op_execute(vm, op);

So you only return the fence of the last operation. In the later on codes, do you use fence to wait for *all* operations to finish?

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct drm_exec *exec, · line 2813

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+		}
+		if (IS_ERR(fence)) {
+			drm_warn(&vm->xe->drm, "VM op(%d) failed with %ld",
+				 op->base.op, PTR_ERR(fence));
+			fence = ERR_PTR(-ENOSPC);

So even if there is error, you don't break the loop. Is it to perform the cleanup below?

Once error happen for one operation, you seem to print the same error message for all the rest operations....because fence = xe_vma_op_execute(vm, op) is not called anymore after the first error

Oak

Quoted 50 lines
drivers/gpu/drm/xe/xe_vm.c +9 -1
+		}
+		if (cleanup)
+			xe_vma_op_cleanup(vm, op);
+	}
+
+	return fence;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
 	struct drm_exec exec;
-	struct xe_vma_op *op, *next;
+	struct dma_fence *fence;
 	int err;
 
drivers/gpu/drm/xe/xe_vm.c +0 -7
 	lockdep_assert_held_write(&vm->lock);
@@ -2820,19 +2836,14 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 		if (err)
 			goto unlock;
 
-		list_for_each_entry_safe(op, next, ops_list, link) {
-			err = xe_vma_op_execute(vm, op);
-			if (err) {
-				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
-					 op->base.op, err);
-				/*
-				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +8 -7
-				 */
-				xe_vm_kill(vm, false);
-				err = -ENOSPC;
-				goto unlock;
-			}
-			xe_vma_op_cleanup(vm, op);
+		fence = ops_execute(vm, ops_list, true);
+		if (IS_ERR(fence)) {
+			err = PTR_ERR(fence);
+			/* FIXME: Killing VM rather than proper error handling */
+			xe_vm_kill(vm, false);
+			goto unlock;
+		} else {
+			dma_fence_put(fence);
 		}
 	}
 
--

2.34.1

author maintainer
#8 lore raw

Re: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Quoted 224 lines · Oak
Quoted 224 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Add ops_execute function which returns a fence. This will be helpful to initiate all binds (VM bind IOCTL, rebinds in exec IOCTL, rebinds in preempt rebind worker, and rebinds in pagefaults) via a gpuva ops list. Returning a fence is needed in various paths.

---
 drivers/gpu/drm/xe/xe_vm.c | 211 +++++++++++++++++++------------------
 1 file changed, 111 insertions(+), 100 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -4
index 3b5dc6de07f7..fb73afcab3b7 100644
@@ -1789,16 +1789,17 @@ find_ufence_get(struct xe_sync_entry *syncs, u32
 num_syncs)
 	return NULL;
 }
 
-static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool immediate, bool first_op,
-			bool last_op)
+static struct dma_fence *
+xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
+	   struct xe_bo *bo, struct xe_sync_entry *syncs, u32 num_syncs,
+	   bool immediate, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
 	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
+	xe_bo_assert_held(bo);
 
drivers/gpu/drm/xe/xe_vm.c +6 -18
 	ufence = find_ufence_get(syncs, num_syncs);
 	if (vma->ufence && ufence)
@@ -1810,7 +1811,7 @@ static int __xe_vm_bind(struct xe_vm *vm, struct
 xe_vma *vma,
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, first_op,
 				       last_op);
 		if (IS_ERR(fence))
-			return PTR_ERR(fence);
+			return fence;
 	} else {
 		int i;
@@ -1825,26 +1826,14 @@ static int __xe_vm_bind(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
-
-	return 0;
-}
-
-static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct
 xe_exec_queue *q,
-		      struct xe_bo *bo, struct xe_sync_entry *syncs,
-		      u32 num_syncs, bool immediate, bool first_op,
-		      bool last_op)
-{
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(bo);
 
-	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate,
 first_op,
-			    last_op);
+	return fence;
 }
 
-static int xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
+	     struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	     u32 num_syncs, bool first_op, bool last_op)

{

drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,

q);

drivers/gpu/drm/xe/xe_vm.c +2 -3
@@ -1854,14 +1843,13 @@ static int xe_vm_unbind(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	fence = xe_vm_unbind_vma(vma, q, syncs, num_syncs, first_op,
 last_op);
 	if (IS_ERR(fence))
-		return PTR_ERR(fence);
+		return fence;
 
 	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
 
-	return 0;
+	return fence;
 }

#define ALL_DRM_XE_VM_CREATE_FLAGS
(DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \

drivers/gpu/drm/xe/xe_vm.c +5 -4
@@ -2004,10 +1992,11 @@ static const u32 region_to_mem_type[] = {
 	XE_PL_VRAM1,
 };
 
-static int xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-			  struct xe_exec_queue *q, u32 region,
-			  struct xe_sync_entry *syncs, u32 num_syncs,
-			  bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
+	       struct xe_exec_queue *q, u32 region,
+	       struct xe_sync_entry *syncs, u32 num_syncs,
+	       bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
drivers/gpu/drm/xe/xe_vm.c +8 -11
 	int err;
@@ -2017,27 +2006,24 @@ static int xe_vm_prefetch(struct xe_vm *vm, struct
 xe_vma *vma,
 	if (!xe_vma_has_no_bo(vma)) {
 		err = xe_bo_migrate(xe_vma_bo(vma),
 region_to_mem_type[region]);
 		if (err)
-			return err;
+			return ERR_PTR(err);
 	}
 
 	if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
 {
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
 				  true, first_op, last_op);
 	} else {
+		struct dma_fence *fence =
+			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 		int i;
 
 		/* Nothing to do, signal fences now */
 		if (last_op) {
-			for (i = 0; i < num_syncs; i++) {
-				struct dma_fence *fence =
-
 	xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-
+			for (i = 0; i < num_syncs; i++)
 				xe_sync_entry_signal(&syncs[i], NULL, fence);
-				dma_fence_put(fence);
-			}
 		}
 
-		return 0;
+		return fence;
 	}
 }
@@ -2484,10 +2470,10 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
-		      struct xe_vma_op *op)
+static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
+				    struct xe_vma_op *op)
 {
-	int err;
+	struct dma_fence *fence = NULL;
 
 	lockdep_assert_held_write(&vm->lock);
drivers/gpu/drm/xe/xe_vm.c +5 -5
 	xe_vm_assert_held(vm);
@@ -2495,11 +2481,11 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		err = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
-				 op->syncs, op->num_syncs,
-				 !xe_vm_in_fault_mode(vm),
-				 op->flags & XE_VMA_OP_FIRST,
-				 op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
+				   op->syncs, op->num_syncs,
+				   !xe_vm_in_fault_mode(vm),
+				   op->flags & XE_VMA_OP_FIRST,
+				   op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_REMAP:
drivers/gpu/drm/xe/xe_vm.c +6 -6
 	{
@@ -2509,37 +2495,39 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 		if (!op->remap.unmap_done) {
 			if (prev || next)
 				vma->gpuva.flags |= XE_VMA_FIRST_REBIND;
-			err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-					   op->num_syncs,
-					   op->flags & XE_VMA_OP_FIRST,
-					   op->flags & XE_VMA_OP_LAST &&
-					   !prev && !next);
-			if (err)
+			fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+					     op->num_syncs,
+					     op->flags & XE_VMA_OP_FIRST,
+					     op->flags & XE_VMA_OP_LAST &&
+					     !prev && !next);
+			if (IS_ERR(fence))
 				break;
 			op->remap.unmap_done = true;
 		}
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct xe_vm *vm, struct xe_vma *vma, · line 2510

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +1 -4
 		if (prev) {
 			op->remap.prev->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.prev, op->q,
-					 xe_vma_bo(op->remap.prev), op->syncs,
-					 op->num_syncs, true, false,
-					 op->flags & XE_VMA_OP_LAST
 && !next);
+			dma_fence_put(fence);

So you drop the previous fence. I assume in later operation, we will need to wait dma-fence for the previous operation to complete. Is it safe to only wait for the last fence? Shouldn't we wait all the fences, such as chain the fences in some way?

All fences on single xe_exec_queue are should ordered (e.g if the last completes all fences prior are also complete).

Techincally this is broken on tip and at this point in the series too as we mix job fences and tlb invalidation fences on single xe_exec_queue. This series gets around to fix this once we convert an IOCTL into single bind job. So this patch while not perfect is regressing anything working toward 100% correctness.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/xe/xe_vm.c +5 -1
+			fence = xe_vm_bind(vm, op->remap.prev, op->q,
+					   xe_vma_bo(op->remap.prev), op-
 >syncs,
+					   op->num_syncs, true, false,
+					   op->flags & XE_VMA_OP_LAST
 && !next);
 			op->remap.prev->gpuva.flags &=
 ~XE_VMA_LAST_REBIND;
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.prev = NULL;
 		}
 
Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct xe_vm *vm, struct xe_vma *vma, · line 2523

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +1 -5
 		if (next) {
 			op->remap.next->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.next, op->q,
-					 xe_vma_bo(op->remap.next),
-					 op->syncs, op->num_syncs,
-					 true, false,
-					 op->flags & XE_VMA_OP_LAST);
+			dma_fence_put(fence);

Same comment as above

Same answer.

Quoted 134 lines
Quoted 134 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+			fence = xe_vm_bind(vm, op->remap.next, op->q,
+					   xe_vma_bo(op->remap.next),
+					   op->syncs, op->num_syncs,
+					   true, false,
+					   op->flags & XE_VMA_OP_LAST);

op->remap.next->gpuva.flags &=
~XE_VMA_LAST_REBIND;

drivers/gpu/drm/xe/xe_vm.c +16 -14
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.next = NULL;
 		}
@@ -2547,34 +2535,36 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-				   op->num_syncs, op->flags &
 XE_VMA_OP_FIRST,
-				   op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+				     op->num_syncs, op->flags &
 XE_VMA_OP_FIRST,
+				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				     op->syncs, op->num_syncs,
-				     op->flags & XE_VMA_OP_FIRST,
-				     op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
+				       op->syncs, op->num_syncs,
+				       op->flags & XE_VMA_OP_FIRST,
+				       op->flags & XE_VMA_OP_LAST);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	if (err)
+	if (IS_ERR(fence))
 		trace_xe_vma_fail(vma);
 
-	return err;
+	return fence;
 }
 
-static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
-			       struct xe_vma_op *op)
+static struct dma_fence *
+__xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		    struct xe_vma_op *op)
 {
+	struct dma_fence *fence;
 	int err;
 
 retry_userptr:
-	err = op_execute(vm, vma, op);
-	if (err == -EAGAIN) {
+	fence = op_execute(vm, vma, op);
+	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
drivers/gpu/drm/xe/xe_vm.c +6 -4
 		lockdep_assert_held_write(&vm->lock);
 
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
@@ -2591,22 +2581,24 @@ static int __xe_vma_op_execute(struct xe_vm *vm,
 struct xe_vma *vma,
 			if (!err)
 				goto retry_userptr;
 
+			fence = ERR_PTR(err);
 			trace_xe_vma_fail(vma);
 		}
 	}
 
-	return err;
+	return fence;
 }
 
-static int xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
+static struct dma_fence *
+xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 {
-	int ret = 0;
+	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
 	lockdep_assert_held_write(&vm->lock);
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		ret = __xe_vma_op_execute(vm, op->map.vma, op);
+		fence = __xe_vma_op_execute(vm, op->map.vma, op);
 		break;
 	case DRM_GPUVA_OP_REMAP:
drivers/gpu/drm/xe/xe_vm.c +7 -7
 	{
@@ -2619,23 +2611,23 @@ static int xe_vma_op_execute(struct xe_vm *vm,
 struct xe_vma_op *op)
 		else
 			vma = op->remap.next;
 
-		ret = __xe_vma_op_execute(vm, vma, op);
+		fence = __xe_vma_op_execute(vm, vma, op);
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		ret = __xe_vma_op_execute(vm, gpuva_to_vma(op-
 >base.unmap.va),
-					  op);
+		fence = __xe_vma_op_execute(vm, gpuva_to_vma(op-
 >base.unmap.va),
+					    op);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		ret = __xe_vma_op_execute(vm,
-					  gpuva_to_vma(op->base.prefetch.va),
-					  op);
+		fence = __xe_vma_op_execute(vm,
+					    gpuva_to_vma(op->base.prefetch.va),
+					    op);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	return ret;
+	return fence;

}

static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2803,11 +2795,35 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 	return 0;
 }
 
Quoted 14 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct drm_exec *exec, · line 2808

Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +11 -0
+static struct dma_fence *ops_execute(struct xe_vm *vm,
+				     struct list_head *ops_list,
+				     bool cleanup)
+{
+	struct xe_vma_op *op, *next;
+	struct dma_fence *fence = NULL;
+
+	list_for_each_entry_safe(op, next, ops_list, link) {
+		if (!IS_ERR(fence)) {
+			dma_fence_put(fence);
+			fence = xe_vma_op_execute(vm, op);

So you only return the fence of the last operation. In the later on codes, do you use fence to wait for *all* operations to finish?

Same answer as above, all fences on exec_queue should be ordered.

Quoted 10 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct drm_exec *exec, · line 2813

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+		}
+		if (IS_ERR(fence)) {
+			drm_warn(&vm->xe->drm, "VM op(%d) failed with %ld",
+				 op->base.op, PTR_ERR(fence));
+			fence = ERR_PTR(-ENOSPC);

So even if there is error, you don't break the loop. Is it to perform the cleanup below?

Once error happen for one operation, you seem to print the same error message for all the rest operations....because fence = xe_vma_op_execute(vm, op) is not called anymore after the first error

Yes.

Matt

Quoted 54 lines

Oak

Quoted 50 lines
drivers/gpu/drm/xe/xe_vm.c +9 -1
+		}
+		if (cleanup)
+			xe_vma_op_cleanup(vm, op);
+	}
+
+	return fence;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
 	struct drm_exec exec;
-	struct xe_vma_op *op, *next;
+	struct dma_fence *fence;
 	int err;
 
drivers/gpu/drm/xe/xe_vm.c +0 -7
 	lockdep_assert_held_write(&vm->lock);
@@ -2820,19 +2836,14 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 		if (err)
 			goto unlock;
 
-		list_for_each_entry_safe(op, next, ops_list, link) {
-			err = xe_vma_op_execute(vm, op);
-			if (err) {
-				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
-					 op->base.op, err);
-				/*
-				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +8 -7
-				 */
-				xe_vm_kill(vm, false);
-				err = -ENOSPC;
-				goto unlock;
-			}
-			xe_vma_op_cleanup(vm, op);
+		fence = ops_execute(vm, ops_list, true);
+		if (IS_ERR(fence)) {
+			err = PTR_ERR(fence);
+			/* FIXME: Killing VM rather than proper error handling */
+			xe_vm_kill(vm, false);
+			goto unlock;
+		} else {
+			dma_fence_put(fence);
 		}
 	}
 
--

2.34.1

#9 lore raw

RE: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Quoted 251 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 22, 2024 1:31 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Quoted 4 lines · Oak
Quoted 4 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 17 lines
Quoted 17 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 02/30] drm/xe: Add ops_execute function which returns a fence

Add ops_execute function which returns a fence. This will be helpful to initiate all binds (VM bind IOCTL, rebinds in exec IOCTL, rebinds in preempt rebind worker, and rebinds in pagefaults) via a gpuva ops list. Returning a fence is needed in various paths.

---
 drivers/gpu/drm/xe/xe_vm.c | 211 +++++++++++++++++++------------------
 1 file changed, 111 insertions(+), 100 deletions(-)
Quoted 5 lines

drivers/gpu/drm/xe/xe_vm.c find_ufence_get(struct xe_sync_entry *syncs,

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 3b5dc6de07f7..fb73afcab3b7 100644
@@ -1789,16 +1789,17 @@ find_ufence_get(struct xe_sync_entry *syncs,

u32

Quoted 10 lines

drivers/gpu/drm/xe/xe_vm.c find_ufence_get(struct xe_sync_entry *syncs, u32 num_syncs)

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +2 -4
 num_syncs)
 	return NULL;
 }
 
-static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool immediate, bool first_op,
-			bool last_op)
+static struct dma_fence *
+xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct

xe_exec_queue *q,

Quoted 83 lines
Quoted 83 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+	   struct xe_bo *bo, struct xe_sync_entry *syncs, u32 num_syncs,
+	   bool immediate, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
 	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
+	xe_bo_assert_held(bo);
 
drivers/gpu/drm/xe/xe_vm.c +6 -18
 	ufence = find_ufence_get(syncs, num_syncs);
 	if (vma->ufence && ufence)
@@ -1810,7 +1811,7 @@ static int __xe_vm_bind(struct xe_vm *vm, struct
 xe_vma *vma,
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, first_op,
 				       last_op);
 		if (IS_ERR(fence))
-			return PTR_ERR(fence);
+			return fence;
 	} else {
 		int i;
@@ -1825,26 +1826,14 @@ static int __xe_vm_bind(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
-
-	return 0;
-}
-
-static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct
 xe_exec_queue *q,
-		      struct xe_bo *bo, struct xe_sync_entry *syncs,
-		      u32 num_syncs, bool immediate, bool first_op,
-		      bool last_op)
-{
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(bo);
 
-	return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate,
 first_op,
-			    last_op);
+	return fence;
 }
 
-static int xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
-			struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-			u32 num_syncs, bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
+	     struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	     u32 num_syncs, bool first_op, bool last_op)

{

drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,

q);

drivers/gpu/drm/xe/xe_vm.c +2 -3
@@ -1854,14 +1843,13 @@ static int xe_vm_unbind(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	fence = xe_vm_unbind_vma(vma, q, syncs, num_syncs, first_op,
 last_op);
 	if (IS_ERR(fence))
-		return PTR_ERR(fence);
+		return fence;
 
 	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-	dma_fence_put(fence);
 
-	return 0;
+	return fence;
 }

#define ALL_DRM_XE_VM_CREATE_FLAGS
(DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2004,10 +1992,11 @@ static const u32 region_to_mem_type[] = {
 	XE_PL_VRAM1,
 };
 
Quoted 14 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vm_prefetch(struct xe_vm *vm,

Quoted 14 lines
drivers/gpu/drm/xe/xe_vm.c +5 -4
-static int xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-			  struct xe_exec_queue *q, u32 region,
-			  struct xe_sync_entry *syncs, u32 num_syncs,
-			  bool first_op, bool last_op)
+static struct dma_fence *
+xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
+	       struct xe_exec_queue *q, u32 region,
+	       struct xe_sync_entry *syncs, u32 num_syncs,
+	       bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	int err;
@@ -2017,27 +2006,24 @@ static int xe_vm_prefetch(struct xe_vm *vm,

struct

Quoted 37 lines
Quoted 37 lines
drivers/gpu/drm/xe/xe_vm.c +5 -8
 xe_vma *vma,
 	if (!xe_vma_has_no_bo(vma)) {
 		err = xe_bo_migrate(xe_vma_bo(vma),
 region_to_mem_type[region]);
 		if (err)
-			return err;
+			return ERR_PTR(err);
 	}
 
 	if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
 {
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
 				  true, first_op, last_op);
 	} else {
+		struct dma_fence *fence =
+			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 		int i;
 
 		/* Nothing to do, signal fences now */
 		if (last_op) {
-			for (i = 0; i < num_syncs; i++) {
-				struct dma_fence *fence =
-
 	xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-
+			for (i = 0; i < num_syncs; i++)
 				xe_sync_entry_signal(&syncs[i], NULL, fence);
-				dma_fence_put(fence);
-			}
 		}
 
-		return 0;
+		return fence;
 	}
 }
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_parse(struct xe_vm · line 2474

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +1 -2
@@ -2484,10 +2470,10 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 	return 0;
 }
 
-static int op_execute(struct xe_vm *vm, struct xe_vma *vma,
-		      struct xe_vma_op *op)
+static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma

*vma,

Quoted 46 lines
Quoted 46 lines
drivers/gpu/drm/xe/xe_vm.c +2 -1
+				    struct xe_vma_op *op)
 {
-	int err;
+	struct dma_fence *fence = NULL;
 
 	lockdep_assert_held_write(&vm->lock);
drivers/gpu/drm/xe/xe_vm.c +5 -5
 	xe_vm_assert_held(vm);
@@ -2495,11 +2481,11 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		err = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
-				 op->syncs, op->num_syncs,
-				 !xe_vm_in_fault_mode(vm),
-				 op->flags & XE_VMA_OP_FIRST,
-				 op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
+				   op->syncs, op->num_syncs,
+				   !xe_vm_in_fault_mode(vm),
+				   op->flags & XE_VMA_OP_FIRST,
+				   op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_REMAP:
drivers/gpu/drm/xe/xe_vm.c +6 -6
 	{
@@ -2509,37 +2495,39 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 		if (!op->remap.unmap_done) {
 			if (prev || next)
 				vma->gpuva.flags |= XE_VMA_FIRST_REBIND;
-			err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-					   op->num_syncs,
-					   op->flags & XE_VMA_OP_FIRST,
-					   op->flags & XE_VMA_OP_LAST &&
-					   !prev && !next);
-			if (err)
+			fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+					     op->num_syncs,
+					     op->flags & XE_VMA_OP_FIRST,
+					     op->flags & XE_VMA_OP_LAST &&
+					     !prev && !next);
+			if (IS_ERR(fence))
 				break;
 			op->remap.unmap_done = true;
 		}
 
Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct xe_vm *vm, struct xe_vma *vma, · line 2510

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +1 -4
 		if (prev) {
 			op->remap.prev->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.prev, op->q,
-					 xe_vma_bo(op->remap.prev), op->syncs,
-					 op->num_syncs, true, false,
-					 op->flags & XE_VMA_OP_LAST
 && !next);
+			dma_fence_put(fence);

So you drop the previous fence. I assume in later operation, we will need to

Quoted 7 lines

wait dma-fence for the previous operation to complete. Is it safe to only wait for the last fence? Shouldn't we wait all the fences, such as chain the fences in some way?

Quoted 1 line

All fences on single xe_exec_queue are should ordered (e.g if the last completes all fences prior are also complete).

Ok, if all fences are ordered, this code make sense to me.

Quoted 196 lines

Techincally this is broken on tip and at this point in the series too as we mix job fences and tlb invalidation fences on single xe_exec_queue. This series gets around to fix this once we convert an IOCTL into single bind job. So this patch while not perfect is regressing anything working toward 100% correctness.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/xe/xe_vm.c +5 -1
+			fence = xe_vm_bind(vm, op->remap.prev, op->q,
+					   xe_vma_bo(op->remap.prev), op-
 >syncs,
+					   op->num_syncs, true, false,
+					   op->flags & XE_VMA_OP_LAST
 && !next);
 			op->remap.prev->gpuva.flags &=
 ~XE_VMA_LAST_REBIND;
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.prev = NULL;
 		}
 
Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static int op_execute(struct xe_vm *vm, struct xe_vma *vma, · line 2523

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +1 -5
 		if (next) {
 			op->remap.next->gpuva.flags |= XE_VMA_LAST_REBIND;
-			err = xe_vm_bind(vm, op->remap.next, op->q,
-					 xe_vma_bo(op->remap.next),
-					 op->syncs, op->num_syncs,
-					 true, false,
-					 op->flags & XE_VMA_OP_LAST);
+			dma_fence_put(fence);

Same comment as above

Same answer.

Quoted 64 lines
Quoted 64 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+			fence = xe_vm_bind(vm, op->remap.next, op->q,
+					   xe_vma_bo(op->remap.next),
+					   op->syncs, op->num_syncs,
+					   true, false,
+					   op->flags & XE_VMA_OP_LAST);

op->remap.next->gpuva.flags &=
~XE_VMA_LAST_REBIND;

drivers/gpu/drm/xe/xe_vm.c +16 -14
-			if (err)
+			if (IS_ERR(fence))
 				break;
 			op->remap.next = NULL;
 		}
@@ -2547,34 +2535,36 @@ static int op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		err = xe_vm_unbind(vm, vma, op->q, op->syncs,
-				   op->num_syncs, op->flags &
 XE_VMA_OP_FIRST,
-				   op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
+				     op->num_syncs, op->flags &
 XE_VMA_OP_FIRST,
+				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				     op->syncs, op->num_syncs,
-				     op->flags & XE_VMA_OP_FIRST,
-				     op->flags & XE_VMA_OP_LAST);
+		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
+				       op->syncs, op->num_syncs,
+				       op->flags & XE_VMA_OP_FIRST,
+				       op->flags & XE_VMA_OP_LAST);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
-	if (err)
+	if (IS_ERR(fence))
 		trace_xe_vma_fail(vma);
 
-	return err;
+	return fence;
 }
 
-static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
-			       struct xe_vma_op *op)
+static struct dma_fence *
+__xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
+		    struct xe_vma_op *op)
 {
+	struct dma_fence *fence;
 	int err;
 
 retry_userptr:
-	err = op_execute(vm, vma, op);
-	if (err == -EAGAIN) {
+	fence = op_execute(vm, vma, op);
+	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {

lockdep_assert_held_write(&vm->lock);

Quoted 2 lines

drivers/gpu/drm/xe/xe_vm.c static int __xe_vma_op_execute(struct xe_vm

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
@@ -2591,22 +2581,24 @@ static int __xe_vma_op_execute(struct xe_vm

*vm,

Quoted 58 lines
Quoted 58 lines
drivers/gpu/drm/xe/xe_vm.c +6 -4
 struct xe_vma *vma,
 			if (!err)
 				goto retry_userptr;
 
+			fence = ERR_PTR(err);
 			trace_xe_vma_fail(vma);
 		}
 	}
 
-	return err;
+	return fence;
 }
 
-static int xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
+static struct dma_fence *
+xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 {
-	int ret = 0;
+	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
 	lockdep_assert_held_write(&vm->lock);
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
-		ret = __xe_vma_op_execute(vm, op->map.vma, op);
+		fence = __xe_vma_op_execute(vm, op->map.vma, op);
 		break;
 	case DRM_GPUVA_OP_REMAP:
drivers/gpu/drm/xe/xe_vm.c +6 -6
 	{
@@ -2619,23 +2611,23 @@ static int xe_vma_op_execute(struct xe_vm *vm,
 struct xe_vma_op *op)
 		else
 			vma = op->remap.next;
 
-		ret = __xe_vma_op_execute(vm, vma, op);
+		fence = __xe_vma_op_execute(vm, vma, op);
 		break;
 	}
 	case DRM_GPUVA_OP_UNMAP:
-		ret = __xe_vma_op_execute(vm, gpuva_to_vma(op-
 >base.unmap.va),
-					  op);
+		fence = __xe_vma_op_execute(vm, gpuva_to_vma(op-
 >base.unmap.va),
+					    op);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		ret = __xe_vma_op_execute(vm,
-					  gpuva_to_vma(op->base.prefetch.va),
-					  op);
+		fence = __xe_vma_op_execute(vm,
+					    gpuva_to_vma(op->base.prefetch.va),
+					    op);
 		break;
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
 
Quoted 6 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
-	return ret;
+	return fence;

}

static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2803,11 +2795,35 @@ static int vm_bind_ioctl_ops_lock(struct

drm_exec

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 *exec,
 	return 0;
 }
 
Quoted 13 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct drm_exec *exec, · line 2808

Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +11 -0
+static struct dma_fence *ops_execute(struct xe_vm *vm,
+				     struct list_head *ops_list,
+				     bool cleanup)
+{
+	struct xe_vma_op *op, *next;
+	struct dma_fence *fence = NULL;
+
+	list_for_each_entry_safe(op, next, ops_list, link) {
+		if (!IS_ERR(fence)) {
+			dma_fence_put(fence);
+			fence = xe_vma_op_execute(vm, op);

So you only return the fence of the last operation. In the later on codes, do you

use fence to wait for *all* operations to finish?

Quoted 1 line

Same answer as above, all fences on exec_queue should be ordered.

Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_lock(struct drm_exec *exec, · line 2813

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+		}
+		if (IS_ERR(fence)) {
+			drm_warn(&vm->xe->drm, "VM op(%d) failed with %ld",
+				 op->base.op, PTR_ERR(fence));
+			fence = ERR_PTR(-ENOSPC);

So even if there is error, you don't break the loop. Is it to perform the cleanup

Quoted 8 lines

below?

Quoted 2 lines

Once error happen for one operation, you seem to print the same error

message for all the rest operations....because fence = xe_vma_op_execute(vm, op) is not called anymore after the first error

Quoted 1 line

Yes.

Is this problematic though? Lets say you have 2 ops in the list and op_execute failed with op1. You will print as below:

VM op1 failed with xxx
VM op1 failed with xxx

Oak

Quoted 58 lines

Matt

Quoted 19 lines

Oak

Quoted 16 lines
drivers/gpu/drm/xe/xe_vm.c +9 -1
+		}
+		if (cleanup)
+			xe_vma_op_cleanup(vm, op);
+	}
+
+	return fence;
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct list_head *ops_list)
 {
 	struct drm_exec exec;
-	struct xe_vma_op *op, *next;
+	struct dma_fence *fence;
 	int err;
 
Quoted 2 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_execute(struct

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	lockdep_assert_held_write(&vm->lock);
@@ -2820,19 +2836,14 @@ static int vm_bind_ioctl_ops_execute(struct

xe_vm

Quoted 33 lines
Quoted 32 lines
drivers/gpu/drm/xe/xe_vm.c +0 -7
 *vm,
 		if (err)
 			goto unlock;
 
-		list_for_each_entry_safe(op, next, ops_list, link) {
-			err = xe_vma_op_execute(vm, op);
-			if (err) {
-				drm_warn(&vm->xe->drm, "VM op(%d) failed
 with %d",
-					 op->base.op, err);
-				/*
-				 * FIXME: Killing VM rather than proper error

handling

drivers/gpu/drm/xe/xe_vm.c +8 -7
-				 */
-				xe_vm_kill(vm, false);
-				err = -ENOSPC;
-				goto unlock;
-			}
-			xe_vma_op_cleanup(vm, op);
+		fence = ops_execute(vm, ops_list, true);
+		if (IS_ERR(fence)) {
+			err = PTR_ERR(fence);
+			/* FIXME: Killing VM rather than proper error handling */
+			xe_vm_kill(vm, false);
+			goto unlock;
+		} else {
+			dma_fence_put(fence);
 		}
 	}
 
--

2.34.1

author maintainer
#10 lore raw

PATCH v4 3/30 drm/xe: Move migrate to prefetch to op_lock function

In reply to Matthew Brost

Migrates need to be done under drm exec to make lockdep happy, move the migrate done for prefetches under the op_lock function.

---
 drivers/gpu/drm/xe/xe_vm.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +13 -15
index fb73afcab3b7..70a5ba621e4e 100644
@@ -1994,20 +1994,10 @@ static const u32 region_to_mem_type[] = {
 
 static struct dma_fence *
 xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-	       struct xe_exec_queue *q, u32 region,
-	       struct xe_sync_entry *syncs, u32 num_syncs,
-	       bool first_op, bool last_op)
+	       struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	       u32 num_syncs, bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
-	int err;
-
-	xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
-
-	if (!xe_vma_has_no_bo(vma)) {
-		err = xe_bo_migrate(xe_vma_bo(vma), region_to_mem_type[region]);
-		if (err)
-			return ERR_PTR(err);
-	}
 
 	if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated)) {
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs, num_syncs,
@@ -2540,8 +2530,7 @@ static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
 				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				       op->syncs, op->num_syncs,
+		fence = xe_vm_prefetch(vm, vma, op->q, op->syncs, op->num_syncs,
 				       op->flags & XE_VMA_OP_FIRST,
 				       op->flags & XE_VMA_OP_LAST);
 		break;
@@ -2766,8 +2755,17 @@ static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va), true);
+	{
+		struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
+		u32 region = op->prefetch.region;
+
+		xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
+
+		err = vma_lock(exec, vma, false);
+		if (!err && !xe_vma_has_no_bo(vma))
+			err = xe_bo_migrate(xe_vma_bo(vma), region);
 		break;
+	}
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
Signature · 1 line
2.34.1
#11 lore raw

RE: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock function

Quoted 11 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock function

Migrates need to be done under drm exec to make lockdep happy,

Can you explain more here? By under drm exec, do you mean drm_exec_until_all_locked? I do see xe_vm_prefetch/xe_bo_migrate is called inside drm_exec_until_all_locked, in patch 1 of this series, in function vm_bind_ioctl_ops_execute

move

Quoted 59 lines

the migrate done for prefetches under the op_lock function.

---
 drivers/gpu/drm/xe/xe_vm.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +2 -12
index fb73afcab3b7..70a5ba621e4e 100644
@@ -1994,20 +1994,10 @@ static const u32 region_to_mem_type[] = {
 
 static struct dma_fence *
 xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-	       struct xe_exec_queue *q, u32 region,
-	       struct xe_sync_entry *syncs, u32 num_syncs,
-	       bool first_op, bool last_op)
+	       struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	       u32 num_syncs, bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	int err;
-
-	xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
-
-	if (!xe_vma_has_no_bo(vma)) {
-		err = xe_bo_migrate(xe_vma_bo(vma),
 region_to_mem_type[region]);
-		if (err)
-			return ERR_PTR(err);
-	}
 

if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
{
return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
num_syncs,

drivers/gpu/drm/xe/xe_vm.c +1 -2
@@ -2540,8 +2530,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				       op->syncs, op->num_syncs,
+		fence = xe_vm_prefetch(vm, vma, op->q, op->syncs, op-
 >num_syncs,
 				       op->flags & XE_VMA_OP_FIRST,
drivers/gpu/drm/xe/xe_vm.c +2 -1
 				       op->flags & XE_VMA_OP_LAST);
 		break;
@@ -2766,8 +2755,17 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va),
 true);
+	{
+		struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);

drivers/gpu/drm/xe/xe_vm.c static int op_lock(struct drm_exec *exec, struct xe_vm · line 2771

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +8 -0
+		u32 region = op->prefetch.region;
+
+		xe_assert(vm->xe, region <=
 ARRAY_SIZE(region_to_mem_type));
+
+		err = vma_lock(exec, vma, false);
+		if (!err && !xe_vma_has_no_bo(vma))
+			err = xe_bo_migrate(xe_vma_bo(vma), region);
 		break;
+	}

Understand you have a reason to do this. It does introduce confusion: the function is called op_lock and now you have a migration operation inside.

Oak

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
--

2.34.1

author maintainer
#12 lore raw

Re: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock function

Quoted 10 lines · Oak
Quoted 10 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock function

Quoted 5 lines
Quoted 1 line

Migrates need to be done under drm exec to make lockdep happy,

Can you explain more here? By under drm exec, do you mean drm_exec_until_all_locked? I do see xe_vm_prefetch/xe_bo_migrate is called inside drm_exec_until_all_locked, in patch 1 of this series, in function vm_bind_ioctl_ops_execute

This is a stale comment, will fix. Something like:

All non-binding operations in VM bind IOCTL should be in the lock and prepare step rather than the execution step. Move prefetch to conform to this pattern.

Quoted 64 lines

move

Quoted 63 lines

the migrate done for prefetches under the op_lock function.

---
 drivers/gpu/drm/xe/xe_vm.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +2 -12
index fb73afcab3b7..70a5ba621e4e 100644
@@ -1994,20 +1994,10 @@ static const u32 region_to_mem_type[] = {
 
 static struct dma_fence *
 xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-	       struct xe_exec_queue *q, u32 region,
-	       struct xe_sync_entry *syncs, u32 num_syncs,
-	       bool first_op, bool last_op)
+	       struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	       u32 num_syncs, bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	int err;
-
-	xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
-
-	if (!xe_vma_has_no_bo(vma)) {
-		err = xe_bo_migrate(xe_vma_bo(vma),
 region_to_mem_type[region]);
-		if (err)
-			return ERR_PTR(err);
-	}
 

if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
{
return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
num_syncs,

drivers/gpu/drm/xe/xe_vm.c +1 -2
@@ -2540,8 +2530,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				       op->syncs, op->num_syncs,
+		fence = xe_vm_prefetch(vm, vma, op->q, op->syncs, op-
 >num_syncs,
 				       op->flags & XE_VMA_OP_FIRST,
drivers/gpu/drm/xe/xe_vm.c +5 -1
 				       op->flags & XE_VMA_OP_LAST);
 		break;
@@ -2766,8 +2755,17 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va),
 true);
+	{
+		struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
+		u32 region = op->prefetch.region;
+
+		xe_assert(vm->xe, region <=
 ARRAY_SIZE(region_to_mem_type));
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c static int op_lock(struct drm_exec *exec, struct xe_vm *vm, · line 2768

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+
+		err = vma_lock(exec, vma, false);
+		if (!err && !xe_vma_has_no_bo(vma))
+			err = xe_bo_migrate(xe_vma_bo(vma), region);
 		break;
+	}

Understand you have a reason to do this. It does introduce confusion: the function is called op_lock and now you have a migration operation inside.

Yes, how about? s/op_lock/op_lock_and_prepare/

Likewise: s/vma_lock/vma_lock/vma_lock_and_validate/

Matt

Quoted 10 lines

Oak

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
--

2.34.1

#13 lore raw

RE: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock function

Quoted 32 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 22, 2024 1:36 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock function

Quoted 4 lines · Oak
Quoted 4 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 5 lines
Quoted 5 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 03/30] drm/xe: Move migrate to prefetch to op_lock

function

Quoted 5 lines
Quoted 2 lines

Migrates need to be done under drm exec to make lockdep happy,

Can you explain more here? By under drm exec, do you mean

drm_exec_until_all_locked? I do see xe_vm_prefetch/xe_bo_migrate is called inside drm_exec_until_all_locked, in patch 1 of this series, in function vm_bind_ioctl_ops_execute

Quoted 1 line
Quoted 5 lines

This is a stale comment, will fix. Something like:

All non-binding operations in VM bind IOCTL should be in the lock and prepare step rather than the execution step. Move prefetch to conform to this pattern.

Have you ever think removing the prefetch API from vm_bind?

One idea is move prefetch to the memory attributes (aka madvise) API we are building for system allocator. From the semantics of prefetch, it is more reasonable to make it part of madvise, vs vm_bind. What do you think?

Quoted 74 lines
Quoted 48 lines

move

Quoted 46 lines

the migrate done for prefetches under the op_lock function.

---
 drivers/gpu/drm/xe/xe_vm.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +2 -12
index fb73afcab3b7..70a5ba621e4e 100644
@@ -1994,20 +1994,10 @@ static const u32 region_to_mem_type[] = {
 
 static struct dma_fence *
 xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-	       struct xe_exec_queue *q, u32 region,
-	       struct xe_sync_entry *syncs, u32 num_syncs,
-	       bool first_op, bool last_op)
+	       struct xe_exec_queue *q, struct xe_sync_entry *syncs,
+	       u32 num_syncs, bool first_op, bool last_op)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	int err;
-
-	xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
-
-	if (!xe_vma_has_no_bo(vma)) {
-		err = xe_bo_migrate(xe_vma_bo(vma),
 region_to_mem_type[region]);
-		if (err)
-			return ERR_PTR(err);
-	}
 

if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
{
return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
num_syncs,

drivers/gpu/drm/xe/xe_vm.c +1 -2
@@ -2540,8 +2530,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 				     op->flags & XE_VMA_OP_LAST);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		fence = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
-				       op->syncs, op->num_syncs,
+		fence = xe_vm_prefetch(vm, vma, op->q, op->syncs, op-
Quoted 5 lines

drivers/gpu/drm/xe/xe_vm.c static int op_lock(struct drm_exec *exec, struct

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 >num_syncs,
 				       op->flags & XE_VMA_OP_FIRST,
drivers/gpu/drm/xe/xe_vm.c +0 -0
 				       op->flags & XE_VMA_OP_LAST);
 		break;
@@ -2766,8 +2755,17 @@ static int op_lock(struct drm_exec *exec, struct

xe_vm

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_vm.c +5 -1
 *vm,
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
-		err = vma_lock(exec, gpuva_to_vma(op->base.prefetch.va),
 true);
+	{
+		struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
+		u32 region = op->prefetch.region;
+
+		xe_assert(vm->xe, region <=
 ARRAY_SIZE(region_to_mem_type));
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c static int op_lock(struct drm_exec *exec, struct xe_vm *vm, · line 2768

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+
+		err = vma_lock(exec, vma, false);
+		if (!err && !xe_vma_has_no_bo(vma))
+			err = xe_bo_migrate(xe_vma_bo(vma), region);
 		break;
+	}

Understand you have a reason to do this. It does introduce confusion: the

Quoted 6 lines

function is called op_lock and now you have a migration operation inside.

Quoted 1 line

Yes, how about? s/op_lock/op_lock_and_prepare/

Likewise: s/vma_lock/vma_lock/vma_lock_and_validate/

Yes, that looks better.

Oak

Quoted 13 lines

Matt

Quoted 10 lines

Oak

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	default:
 		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 	}
--

2.34.1

author maintainer
#14 lore raw

PATCH v4 4/30 drm/xe: Add struct xe_vma_ops abstraction

In reply to Matthew Brost

Having a structure which encapsulates a list of VMA operations will help enable 1 job for the entire list.

---
 drivers/gpu/drm/xe/xe_vm.c       | 37 ++++++++++++++++++--------------
 drivers/gpu/drm/xe/xe_vm_types.h |  7 ++++++
 2 files changed, 28 insertions(+), 16 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +21 -16
index 70a5ba621e4e..e342af6b51b1 100644
@@ -2305,7 +2305,7 @@ static int xe_vma_op_commit(struct xe_vm *vm, struct xe_vma_op *op)
 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 				   struct drm_gpuva_ops *ops,
 				   struct xe_sync_entry *syncs, u32 num_syncs,
-				   struct list_head *ops_list, bool last)
+				   struct xe_vma_ops *vops, bool last)
 {
 	struct xe_device *xe = vm->xe;
 	struct xe_vma_op *last_op = NULL;
@@ -2317,11 +2317,11 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 	drm_gpuva_for_each_op(__op, ops) {
 		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
 		struct xe_vma *vma;
-		bool first = list_empty(ops_list);
+		bool first = list_empty(&vops->list);
 		unsigned int flags = 0;
 
 		INIT_LIST_HEAD(&op->link);
-		list_add_tail(&op->link, ops_list);
+		list_add_tail(&op->link, &vops->list);
 
 		if (first) {
 			op->flags |= XE_VMA_OP_FIRST;
@@ -2445,7 +2445,7 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 	}
 
 	/* FIXME: Unhandled corner case */
-	XE_WARN_ON(!last_op && last && !list_empty(ops_list));
+	XE_WARN_ON(!last_op && last && !list_empty(&vops->list));
 
 	if (!last_op)
 		return 0;
@@ -2775,7 +2775,7 @@ static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 
 static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
 				  struct xe_vm *vm,
-				  struct list_head *ops_list)
+				  struct xe_vma_ops *vops)
 {
 	struct xe_vma_op *op;
 	int err;
@@ -2784,7 +2784,7 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
 	if (err)
 		return err;
 
-	list_for_each_entry(op, ops_list, link) {
+	list_for_each_entry(op, &vops->list, link) {
 		err = op_lock(exec, vm, op);
 		if (err)
 			return err;
@@ -2794,13 +2794,13 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct list_head *ops_list,
+				     struct xe_vma_ops *vops,
 				     bool cleanup)
 {
 	struct xe_vma_op *op, *next;
 	struct dma_fence *fence = NULL;
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
+	list_for_each_entry_safe(op, next, &vops->list, link) {
 		if (!IS_ERR(fence)) {
 			dma_fence_put(fence);
 			fence = xe_vma_op_execute(vm, op);
@@ -2818,7 +2818,7 @@ static struct dma_fence *ops_execute(struct xe_vm *vm,
 }
 
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
-				     struct list_head *ops_list)
+				     struct xe_vma_ops *vops)
 {
 	struct drm_exec exec;
 	struct dma_fence *fence;
@@ -2829,12 +2829,12 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
 		      DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
-		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		err = vm_bind_ioctl_ops_lock(&exec, vm, vops);
 		drm_exec_retry_on_contention(&exec);
 		if (err)
 			goto unlock;
 
-		fence = ops_execute(vm, ops_list, true);
+		fence = ops_execute(vm, vops, true);
 		if (IS_ERR(fence)) {
 			err = PTR_ERR(fence);
 			/* FIXME: Killing VM rather than proper error handling */
@@ -2992,6 +2992,11 @@ static int vm_bind_ioctl_signal_fences(struct xe_vm *vm,
 	return err;
 }
 
+static void xe_vma_ops_init(struct xe_vma_ops *vops)
+{
+	INIT_LIST_HEAD(&vops->list);
+}
+
 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 {
 	struct xe_device *xe = to_xe_device(dev);
@@ -3005,7 +3010,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	u32 num_syncs, num_ufence = 0;
 	struct xe_sync_entry *syncs = NULL;
 	struct drm_xe_vm_bind_op *bind_ops;
-	LIST_HEAD(ops_list);
+	struct xe_vma_ops vops;
 	int err;
 	int i;
 
@@ -3156,6 +3161,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		goto free_syncs;
 	}
 
+	xe_vma_ops_init(&vops);
 	for (i = 0; i < args->num_binds; ++i) {
 		u64 range = bind_ops[i].range;
 		u64 addr = bind_ops[i].addr;
@@ -3175,14 +3181,13 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		}
 
 		err = vm_bind_ioctl_ops_parse(vm, q, ops[i], syncs, num_syncs,
-					      &ops_list,
-					      i == args->num_binds - 1);
+					      &vops, i == args->num_binds - 1);
 		if (err)
 			goto unwind_ops;
 	}
 
 	/* Nothing to do */
-	if (list_empty(&ops_list)) {
+	if (list_empty(&vops.list)) {
 		err = -ENODATA;
 		goto unwind_ops;
 	}
@@ -3191,7 +3196,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	if (q)
 		xe_exec_queue_get(q);
 
-	err = vm_bind_ioctl_ops_execute(vm, &ops_list);
+	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
 	up_write(&vm->lock);
 
drivers/gpu/drm/xe/xe_vm_types.h +7 -0
index 79b5cab57711..cc3dce893f1e 100644
@@ -355,4 +355,11 @@ struct xe_vma_op {
 		struct xe_vma_op_prefetch prefetch;
 	};
 };
+
+/** struct xe_vma_ops - VMA operations */
+struct xe_vma_ops {
+	/** @list: list of VMA operations */
+	struct list_head list;
+};
+
 #endif
Signature · 1 line
2.34.1
#15 lore raw

RE: [PATCH v4 04/30] drm/xe: Add struct xe_vma_ops abstraction

This patch lgtm. Reviewed-by: Oak Zeng <[email protected]>

Quoted 198 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 04/30] drm/xe: Add struct xe_vma_ops abstraction

Having a structure which encapsulates a list of VMA operations will help enable 1 job for the entire list.

---
 drivers/gpu/drm/xe/xe_vm.c       | 37 ++++++++++++++++++--------------
 drivers/gpu/drm/xe/xe_vm_types.h |  7 ++++++
 2 files changed, 28 insertions(+), 16 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -1
index 70a5ba621e4e..e342af6b51b1 100644
@@ -2305,7 +2305,7 @@ static int xe_vma_op_commit(struct xe_vm *vm, struct
 xe_vma_op *op)
 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 				   struct drm_gpuva_ops *ops,
 				   struct xe_sync_entry *syncs, u32 num_syncs,
-				   struct list_head *ops_list, bool last)
+				   struct xe_vma_ops *vops, bool last)
 {
 	struct xe_device *xe = vm->xe;
drivers/gpu/drm/xe/xe_vm.c +2 -2
 	struct xe_vma_op *last_op = NULL;
@@ -2317,11 +2317,11 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 	drm_gpuva_for_each_op(__op, ops) {
 		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
 		struct xe_vma *vma;
-		bool first = list_empty(ops_list);
+		bool first = list_empty(&vops->list);
 		unsigned int flags = 0;
 
 		INIT_LIST_HEAD(&op->link);
-		list_add_tail(&op->link, ops_list);
+		list_add_tail(&op->link, &vops->list);
 
 		if (first) {
drivers/gpu/drm/xe/xe_vm.c +1 -1
 			op->flags |= XE_VMA_OP_FIRST;
@@ -2445,7 +2445,7 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 	}
 
 	/* FIXME: Unhandled corner case */
-	XE_WARN_ON(!last_op && last && !list_empty(ops_list));
+	XE_WARN_ON(!last_op && last && !list_empty(&vops->list));
 
 	if (!last_op)
drivers/gpu/drm/xe/xe_vm.c +1 -1
 		return 0;
@@ -2775,7 +2775,7 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 
 static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
 				  struct xe_vm *vm,
-				  struct list_head *ops_list)
+				  struct xe_vma_ops *vops)
 {
 	struct xe_vma_op *op;
drivers/gpu/drm/xe/xe_vm.c +1 -1
 	int err;
@@ -2784,7 +2784,7 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 	if (err)
 		return err;
 
-	list_for_each_entry(op, ops_list, link) {
+	list_for_each_entry(op, &vops->list, link) {
 		err = op_lock(exec, vm, op);
 		if (err)
drivers/gpu/drm/xe/xe_vm.c +2 -2
 			return err;
@@ -2794,13 +2794,13 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct list_head *ops_list,
+				     struct xe_vma_ops *vops,
 				     bool cleanup)
 {
 	struct xe_vma_op *op, *next;
 	struct dma_fence *fence = NULL;
 
-	list_for_each_entry_safe(op, next, ops_list, link) {
+	list_for_each_entry_safe(op, next, &vops->list, link) {
 		if (!IS_ERR(fence)) {
 			dma_fence_put(fence);
drivers/gpu/drm/xe/xe_vm.c +1 -1
 			fence = xe_vma_op_execute(vm, op);
@@ -2818,7 +2818,7 @@ static struct dma_fence *ops_execute(struct xe_vm
 *vm,
 }
 
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
-				     struct list_head *ops_list)
+				     struct xe_vma_ops *vops)
 {
 	struct drm_exec exec;
drivers/gpu/drm/xe/xe_vm.c +2 -2
 	struct dma_fence *fence;
@@ -2829,12 +2829,12 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
 		      DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
-		err = vm_bind_ioctl_ops_lock(&exec, vm, ops_list);
+		err = vm_bind_ioctl_ops_lock(&exec, vm, vops);
 		drm_exec_retry_on_contention(&exec);
 		if (err)
 			goto unlock;
 
-		fence = ops_execute(vm, ops_list, true);
+		fence = ops_execute(vm, vops, true);
 		if (IS_ERR(fence)) {
 			err = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_vm.c +5 -0
 			/* FIXME: Killing VM rather than proper error handling */
@@ -2992,6 +2992,11 @@ static int vm_bind_ioctl_signal_fences(struct xe_vm
 *vm,
 	return err;
 }
 
+static void xe_vma_ops_init(struct xe_vma_ops *vops)
+{
+	INIT_LIST_HEAD(&vops->list);
+}
+
 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 {
drivers/gpu/drm/xe/xe_vm.c +2 -1
 	struct xe_device *xe = to_xe_device(dev);
@@ -3005,7 +3010,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 	u32 num_syncs, num_ufence = 0;
 	struct xe_sync_entry *syncs = NULL;
 	struct drm_xe_vm_bind_op *bind_ops;
-	LIST_HEAD(ops_list);
+	struct xe_vma_ops vops;
 	int err;
 	int i;
@@ -3156,6 +3161,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto free_syncs;
 	}
 
+	xe_vma_ops_init(&vops);
 	for (i = 0; i < args->num_binds; ++i) {
 		u64 range = bind_ops[i].range;
drivers/gpu/drm/xe/xe_vm.c +2 -3
 		u64 addr = bind_ops[i].addr;
@@ -3175,14 +3181,13 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		}
 
 		err = vm_bind_ioctl_ops_parse(vm, q, ops[i], syncs, num_syncs,
-					      &ops_list,
-					      i == args->num_binds - 1);
+					      &vops, i == args->num_binds - 1);
 		if (err)
 			goto unwind_ops;
 	}
 
 	/* Nothing to do */
-	if (list_empty(&ops_list)) {
+	if (list_empty(&vops.list)) {
 		err = -ENODATA;
 		goto unwind_ops;
drivers/gpu/drm/xe/xe_vm.c +1 -1
 	}
@@ -3191,7 +3196,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 	if (q)
 		xe_exec_queue_get(q);
 
-	err = vm_bind_ioctl_ops_execute(vm, &ops_list);
+	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
 	up_write(&vm->lock);
drivers/gpu/drm/xe/xe_vm_types.h +7 -1
index 79b5cab57711..cc3dce893f1e 100644
@@ -355,4 +355,11 @@ struct xe_vma_op {
 		struct xe_vma_op_prefetch prefetch;
 	};
 };
+
+/** struct xe_vma_ops - VMA operations */
+struct xe_vma_ops {
+	/** @list: list of VMA operations */
+	struct list_head list;
+};
+
 #endif
--

2.34.1

author maintainer
#16 lore raw

PATCH v4 5/30 drm/xe: Update xe_vm_rebind to use dummy VMA operations

In reply to Matthew Brost

All bind interfaces are transitioning to use VMA ops, update xe_vm_rebind to use VMA ops.

---
 drivers/gpu/drm/xe/xe_vm.c       |  47 +++++---
 drivers/gpu/drm/xe/xe_vm_types.h | 189 ++++++++++++++++---------------
 2 files changed, 132 insertions(+), 104 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +33 -14
index e342af6b51b1..0bb807c05d7b 100644
@@ -755,10 +755,22 @@ int xe_vm_userptr_check_repin(struct xe_vm *vm)
 		list_empty_careful(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
 }
 
-static struct dma_fence *
-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);
+static void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma)
+{
+	vm->dummy_ops.op.base.op = DRM_GPUVA_OP_MAP;
+	vm->dummy_ops.op.base.map.va.addr = vma->gpuva.va.addr;
+	vm->dummy_ops.op.base.map.va.range = vma->gpuva.va.range;
+	vm->dummy_ops.op.base.map.gem.obj = vma->gpuva.gem.obj;
+	vm->dummy_ops.op.base.map.gem.offset = vma->gpuva.gem.offset;
+	vm->dummy_ops.op.map.vma = vma;
+	vm->dummy_ops.op.map.immediate = true;
+	vm->dummy_ops.op.map.dumpable = vma->gpuva.flags & XE_VMA_DUMPABLE;
+	vm->dummy_ops.op.map.is_null = xe_vma_is_null(vma);
+}
+
+static struct dma_fence *ops_execute(struct xe_vm *vm,
+				     struct xe_vma_ops *vops,
+				     bool cleanup);
 
 struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
 {
@@ -780,7 +792,9 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
 			trace_xe_vma_rebind_worker(vma);
 		else
 			trace_xe_vma_rebind_exec(vma);
-		fence = xe_vm_bind_vma(vma, NULL, NULL, 0, false, false);
+
+		xe_vm_populate_dummy_rebind(vm, vma);
+		fence = ops_execute(vm, &vm->dummy_ops.vops, false);
 		if (IS_ERR(fence))
 			return fence;
 	}
@@ -1289,6 +1303,11 @@ static void xe_vm_free_scratch(struct xe_vm *vm)
 	}
 }
 
+static void xe_vma_ops_init(struct xe_vma_ops *vops)
+{
+	INIT_LIST_HEAD(&vops->list);
+}
+
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 {
 	struct drm_gem_object *vm_resv_obj;
@@ -1310,6 +1329,10 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 	init_rwsem(&vm->lock);
 	mutex_init(&vm->snap_mutex);
 
+	xe_vma_ops_init(&vm->dummy_ops.vops);
+	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
+	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
+
 	INIT_LIST_HEAD(&vm->rebind_list);
 
 	INIT_LIST_HEAD(&vm->userptr.repin_list);
@@ -2140,6 +2163,7 @@ vm_bind_ioctl_ops_create(struct xe_vm *vm, struct xe_bo *bo,
 		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
 
 		if (__op->op == DRM_GPUVA_OP_MAP) {
+			op->map.immediate = !xe_vm_in_fault_mode(vm);
 			op->map.is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
 			op->map.dumpable = flags & DRM_XE_VM_BIND_FLAG_DUMPABLE;
 			op->map.pat_index = pat_index;
@@ -2465,7 +2489,7 @@ static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
 {
 	struct dma_fence *fence = NULL;
 
-	lockdep_assert_held_write(&vm->lock);
+	lockdep_assert_held(&vm->lock);
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
 
@@ -2473,7 +2497,7 @@ static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
 	case DRM_GPUVA_OP_MAP:
 		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
 				   op->syncs, op->num_syncs,
-				   !xe_vm_in_fault_mode(vm),
+				   op->map.immediate,
 				   op->flags & XE_VMA_OP_FIRST,
 				   op->flags & XE_VMA_OP_LAST);
 		break;
@@ -2554,7 +2578,7 @@ __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
 retry_userptr:
 	fence = op_execute(vm, vma, op);
 	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
-		lockdep_assert_held_write(&vm->lock);
+		lockdep_assert_held(&vm->lock);
 
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
 			if (!op->remap.unmap_done)
@@ -2583,7 +2607,7 @@ xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 {
 	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
-	lockdep_assert_held_write(&vm->lock);
+	lockdep_assert_held(&vm->lock);
 
 	switch (op->base.op) {
 	case DRM_GPUVA_OP_MAP:
@@ -2992,11 +3016,6 @@ static int vm_bind_ioctl_signal_fences(struct xe_vm *vm,
 	return err;
 }
 
-static void xe_vma_ops_init(struct xe_vma_ops *vops)
-{
-	INIT_LIST_HEAD(&vops->list);
-}
-
 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 {
 	struct xe_device *xe = to_xe_device(dev);
drivers/gpu/drm/xe/xe_vm_types.h +99 -90

211 lines not on this page · raw message

Signature · 1 line
2.34.1
#17 lore raw

RE: [PATCH v4 05/30] drm/xe: Update xe_vm_rebind to use dummy VMA operations

Quoted 66 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 05/30] drm/xe: Update xe_vm_rebind to use dummy VMA operations

All bind interfaces are transitioning to use VMA ops, update xe_vm_rebind to use VMA ops.

---
 drivers/gpu/drm/xe/xe_vm.c       |  47 +++++---
 drivers/gpu/drm/xe/xe_vm_types.h | 189 ++++++++++++++++---------------
 2 files changed, 132 insertions(+), 104 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +16 -4
index e342af6b51b1..0bb807c05d7b 100644
@@ -755,10 +755,22 @@ int xe_vm_userptr_check_repin(struct xe_vm *vm)
 		list_empty_careful(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
 }
 
-static struct dma_fence *
-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);
+static void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma
 *vma)
+{
+	vm->dummy_ops.op.base.op = DRM_GPUVA_OP_MAP;
+	vm->dummy_ops.op.base.map.va.addr = vma->gpuva.va.addr;
+	vm->dummy_ops.op.base.map.va.range = vma->gpuva.va.range;
+	vm->dummy_ops.op.base.map.gem.obj = vma->gpuva.gem.obj;
+	vm->dummy_ops.op.base.map.gem.offset = vma->gpuva.gem.offset;
+	vm->dummy_ops.op.map.vma = vma;
+	vm->dummy_ops.op.map.immediate = true;
+	vm->dummy_ops.op.map.dumpable = vma->gpuva.flags &
 XE_VMA_DUMPABLE;
+	vm->dummy_ops.op.map.is_null = xe_vma_is_null(vma);
+}
+
+static struct dma_fence *ops_execute(struct xe_vm *vm,
+				     struct xe_vma_ops *vops,
+				     bool cleanup);
 

struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker) {

drivers/gpu/drm/xe/xe_vm.c +3 -1
@@ -780,7 +792,9 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm,
 bool rebind_worker)
 			trace_xe_vma_rebind_worker(vma);
 		else
 			trace_xe_vma_rebind_exec(vma);
-		fence = xe_vm_bind_vma(vma, NULL, NULL, 0, false, false);
+
+		xe_vm_populate_dummy_rebind(vm, vma);
+		fence = ops_execute(vm, &vm->dummy_ops.vops, false);
 		if (IS_ERR(fence))
 			return fence;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	}

drivers/gpu/drm/xe/xe_vm.c static void xe_vm_free_scratch(struct xe_vm *vm) · line 1309

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
@@ -1289,6 +1303,11 @@ static void xe_vm_free_scratch(struct xe_vm *vm)
 	}
 }
 
+static void xe_vma_ops_init(struct xe_vma_ops *vops)
+{
+	INIT_LIST_HEAD(&vops->list);
+}

this already showed up on patch 4... you just add it in patch5, then moved it to another location on patch 5...

can this be better organized?

Quoted 180 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 {
 	struct drm_gem_object *vm_resv_obj;
@@ -1310,6 +1329,10 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	init_rwsem(&vm->lock);
 	mutex_init(&vm->snap_mutex);
 
+	xe_vma_ops_init(&vm->dummy_ops.vops);
+	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
+	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
+
 	INIT_LIST_HEAD(&vm->rebind_list);
 
drivers/gpu/drm/xe/xe_vm.c +1 -0
 	INIT_LIST_HEAD(&vm->userptr.repin_list);
@@ -2140,6 +2163,7 @@ vm_bind_ioctl_ops_create(struct xe_vm *vm, struct
 xe_bo *bo,
 		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
 
 		if (__op->op == DRM_GPUVA_OP_MAP) {
+			op->map.immediate = !xe_vm_in_fault_mode(vm);
 			op->map.is_null = flags &
 DRM_XE_VM_BIND_FLAG_NULL;

op->map.dumpable = flags &
DRM_XE_VM_BIND_FLAG_DUMPABLE;

drivers/gpu/drm/xe/xe_vm.c +2 -2
 			op->map.pat_index = pat_index;
@@ -2465,7 +2489,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 {
 	struct dma_fence *fence = NULL;
 
-	lockdep_assert_held_write(&vm->lock);
+	lockdep_assert_held(&vm->lock);
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -2473,7 +2497,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 	case DRM_GPUVA_OP_MAP:
 		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
 				   op->syncs, op->num_syncs,
-				   !xe_vm_in_fault_mode(vm),
+				   op->map.immediate,
 				   op->flags & XE_VMA_OP_FIRST,
 				   op->flags & XE_VMA_OP_LAST);
drivers/gpu/drm/xe/xe_vm.c +1 -1
 		break;
@@ -2554,7 +2578,7 @@ __xe_vma_op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 retry_userptr:
 	fence = op_execute(vm, vma, op);
 	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
-		lockdep_assert_held_write(&vm->lock);
+		lockdep_assert_held(&vm->lock);
 
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
drivers/gpu/drm/xe/xe_vm.c +1 -1
 			if (!op->remap.unmap_done)
@@ -2583,7 +2607,7 @@ xe_vma_op_execute(struct xe_vm *vm, struct
 xe_vma_op *op)
 {
 	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
-	lockdep_assert_held_write(&vm->lock);
+	lockdep_assert_held(&vm->lock);
 
 	switch (op->base.op) {
drivers/gpu/drm/xe/xe_vm.c +0 -5
 	case DRM_GPUVA_OP_MAP:
@@ -2992,11 +3016,6 @@ static int vm_bind_ioctl_signal_fences(struct xe_vm
 *vm,
 	return err;
 }
 
-static void xe_vma_ops_init(struct xe_vma_ops *vops)
-{
-	INIT_LIST_HEAD(&vops->list);
-}
-
 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 {
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct xe_device *xe = to_xe_device(dev);
drivers/gpu/drm/xe/xe_vm_types.h +48 -1
index cc3dce893f1e..7ef9e632154a 100644
@@ -18,6 +18,7 @@
 #include "xe_range_fence.h"
 
 struct xe_bo;
+struct xe_device;
 struct xe_sync_entry;
 struct xe_user_fence;
 struct xe_vm;
@@ -124,7 +125,96 @@ struct xe_userptr_vma {
 	struct xe_userptr userptr;
 };
 
-struct xe_device;
+/** struct xe_vma_op_map - VMA map operation */
+struct xe_vma_op_map {
+	/** @vma: VMA to map */
+	struct xe_vma *vma;
+	/** @immediate: Immediate bind */
+	bool immediate;
+	/** @is_null: is NULL binding */
+	bool is_null;
+	/** @dumpable: whether BO is dumped on GPU hang */
+	bool dumpable;
+	/** @pat_index: The pat index to use for this operation. */
+	u16 pat_index;
+};
+
+/** struct xe_vma_op_remap - VMA remap operation */
+struct xe_vma_op_remap {
+	/** @prev: VMA preceding part of a split mapping */
+	struct xe_vma *prev;
+	/** @next: VMA subsequent part of a split mapping */
+	struct xe_vma *next;
+	/** @start: start of the VMA unmap */
+	u64 start;
+	/** @range: range of the VMA unmap */
+	u64 range;
+	/** @skip_prev: skip prev rebind */
+	bool skip_prev;
+	/** @skip_next: skip next rebind */
+	bool skip_next;
+	/** @unmap_done: unmap operation in done */
+	bool unmap_done;
+};
+
+/** struct xe_vma_op_prefetch - VMA prefetch operation */
+struct xe_vma_op_prefetch {
+	/** @region: memory region to prefetch to */
+	u32 region;
+};
+
+/** enum xe_vma_op_flags - flags for VMA operation */
+enum xe_vma_op_flags {
+	/** @XE_VMA_OP_FIRST: first VMA operation for a set of syncs */
+	XE_VMA_OP_FIRST			= BIT(0),
+	/** @XE_VMA_OP_LAST: last VMA operation for a set of syncs */
+	XE_VMA_OP_LAST			= BIT(1),
+	/** @XE_VMA_OP_COMMITTED: VMA operation committed */
+	XE_VMA_OP_COMMITTED		= BIT(2),
+	/** @XE_VMA_OP_PREV_COMMITTED: Previous VMA operation

committed */

drivers/gpu/drm/xe/xe_vm_types.h +2 -0
+	XE_VMA_OP_PREV_COMMITTED	= BIT(3),
+	/** @XE_VMA_OP_NEXT_COMMITTED: Next VMA operation committed

*/

drivers/gpu/drm/xe/xe_vm_types.h +31 -0
+	XE_VMA_OP_NEXT_COMMITTED	= BIT(4),
+};
+
+/** struct xe_vma_op - VMA operation */
+struct xe_vma_op {
+	/** @base: GPUVA base operation */
+	struct drm_gpuva_op base;
+	/**
+	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
+	 * operations is processed
+	 */
+	struct drm_gpuva_ops *ops;
+	/** @q: exec queue for this operation */
+	struct xe_exec_queue *q;
+	/**
+	 * @syncs: syncs for this operation, only used on first and last
+	 * operation
+	 */
+	struct xe_sync_entry *syncs;
+	/** @num_syncs: number of syncs */
+	u32 num_syncs;
+	/** @link: async operation link */
+	struct list_head link;
+	/** @flags: operation flags */
+	enum xe_vma_op_flags flags;
+
+	union {
+		/** @map: VMA map operation specific data */
+		struct xe_vma_op_map map;
+		/** @remap: VMA remap operation specific data */
+		struct xe_vma_op_remap remap;

drivers/gpu/drm/xe/xe_vm_types.h struct xe_userptr_vma { · line 217

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm_types.h +10 -0
+		/** @prefetch: VMA prefetch operation specific data */
+		struct xe_vma_op_prefetch prefetch;
+	};
+};
+
+/** struct xe_vma_ops - VMA operations */
+struct xe_vma_ops {
+	/** @list: list of VMA operations */
+	struct list_head list;
+};

this already showed up on patch 4... you just add it in patch5, then moved it to another location on patch 5...

Quoted 4 lines

struct xe_vm {

drivers/gpu/drm/xe/xe_vm_types.h +0 -0
 	/** @gpuvm: base GPUVM used to track VMAs */

drivers/gpu/drm/xe/xe_vm_types.h struct xe_vm { · line 366

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm_types.h +7 -0
@@ -267,99 +357,18 @@ struct xe_vm {
 		bool capture_once;
 	} error_capture;
 
+	/** @dummy_ops: dummy VMA ops to issue rebinds */
+	struct {
+		/** @dummy_ops.ops: dummy VMA ops */
+		struct xe_vma_ops vops;
+		/** @dummy_ops.op: dummy VMA op */
+		struct xe_vma_op op;
+	} dummy_ops;

If only from this patch, it seems you don't have to introduce this dummy_ops member to xe_vm. For example, it can be a local variable in xe_vm_rebind function. But I will keep looking. Maybe you made it this way for future patches.

Quoted 87 lines
drivers/gpu/drm/xe/xe_vm_types.h +1 -45
+
 	/** @batch_invalidate_tlb: Always invalidate TLB before batch start */
 	bool batch_invalidate_tlb;
 	/** @xef: XE file handle for tracking this VM's drm client */
 	struct xe_file *xef;
 };
 
-/** struct xe_vma_op_map - VMA map operation */
-struct xe_vma_op_map {
-	/** @vma: VMA to map */
-	struct xe_vma *vma;
-	/** @is_null: is NULL binding */
-	bool is_null;
-	/** @dumpable: whether BO is dumped on GPU hang */
-	bool dumpable;
-	/** @pat_index: The pat index to use for this operation. */
-	u16 pat_index;
-};
-
-/** struct xe_vma_op_remap - VMA remap operation */
-struct xe_vma_op_remap {
-	/** @prev: VMA preceding part of a split mapping */
-	struct xe_vma *prev;
-	/** @next: VMA subsequent part of a split mapping */
-	struct xe_vma *next;
-	/** @start: start of the VMA unmap */
-	u64 start;
-	/** @range: range of the VMA unmap */
-	u64 range;
-	/** @skip_prev: skip prev rebind */
-	bool skip_prev;
-	/** @skip_next: skip next rebind */
-	bool skip_next;
-	/** @unmap_done: unmap operation in done */
-	bool unmap_done;
-};
-
-/** struct xe_vma_op_prefetch - VMA prefetch operation */
-struct xe_vma_op_prefetch {
-	/** @region: memory region to prefetch to */
-	u32 region;
-};
-
-/** enum xe_vma_op_flags - flags for VMA operation */
-enum xe_vma_op_flags {
-	/** @XE_VMA_OP_FIRST: first VMA operation for a set of syncs */
-	XE_VMA_OP_FIRST			= BIT(0),
-	/** @XE_VMA_OP_LAST: last VMA operation for a set of syncs */
-	XE_VMA_OP_LAST			= BIT(1),
-	/** @XE_VMA_OP_COMMITTED: VMA operation committed */
-	XE_VMA_OP_COMMITTED		= BIT(2),
-	/** @XE_VMA_OP_PREV_COMMITTED: Previous VMA operation

committed */

drivers/gpu/drm/xe/xe_vm_types.h +0 -2
-	XE_VMA_OP_PREV_COMMITTED	= BIT(3),
-	/** @XE_VMA_OP_NEXT_COMMITTED: Next VMA operation committed

*/

drivers/gpu/drm/xe/xe_vm_types.h +0 -31
-	XE_VMA_OP_NEXT_COMMITTED	= BIT(4),
-};
-
-/** struct xe_vma_op - VMA operation */
-struct xe_vma_op {
-	/** @base: GPUVA base operation */
-	struct drm_gpuva_op base;
-	/**
-	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
-	 * operations is processed
-	 */
-	struct drm_gpuva_ops *ops;
-	/** @q: exec queue for this operation */
-	struct xe_exec_queue *q;
-	/**
-	 * @syncs: syncs for this operation, only used on first and last
-	 * operation
-	 */
-	struct xe_sync_entry *syncs;
-	/** @num_syncs: number of syncs */
-	u32 num_syncs;
-	/** @link: async operation link */
-	struct list_head link;
-	/** @flags: operation flags */
-	enum xe_vma_op_flags flags;
-
-	union {
-		/** @map: VMA map operation specific data */
-		struct xe_vma_op_map map;
-		/** @remap: VMA remap operation specific data */
-		struct xe_vma_op_remap remap;

drivers/gpu/drm/xe/xe_vm_types.h struct xe_vm { · line 363

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm_types.h +0 -10
-		/** @prefetch: VMA prefetch operation specific data */
-		struct xe_vma_op_prefetch prefetch;
-	};
-};
-
-/** struct xe_vma_ops - VMA operations */
-struct xe_vma_ops {
-	/** @list: list of VMA operations */
-	struct list_head list;
-};

It seems you moved a block of codes to another location. It caused more work for code review. Better to avoid this if we can.

Oak

Quoted 4 lines

-
#endif
--
2.34.1

author maintainer
#18 lore raw

Re: [PATCH v4 05/30] drm/xe: Update xe_vm_rebind to use dummy VMA operations

Quoted 69 lines · Oak
Quoted 69 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 05/30] drm/xe: Update xe_vm_rebind to use dummy VMA operations

All bind interfaces are transitioning to use VMA ops, update xe_vm_rebind to use VMA ops.

---
 drivers/gpu/drm/xe/xe_vm.c       |  47 +++++---
 drivers/gpu/drm/xe/xe_vm_types.h | 189 ++++++++++++++++---------------
 2 files changed, 132 insertions(+), 104 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +16 -4
index e342af6b51b1..0bb807c05d7b 100644
@@ -755,10 +755,22 @@ int xe_vm_userptr_check_repin(struct xe_vm *vm)
 		list_empty_careful(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
 }
 
-static struct dma_fence *
-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);
+static void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma
 *vma)
+{
+	vm->dummy_ops.op.base.op = DRM_GPUVA_OP_MAP;
+	vm->dummy_ops.op.base.map.va.addr = vma->gpuva.va.addr;
+	vm->dummy_ops.op.base.map.va.range = vma->gpuva.va.range;
+	vm->dummy_ops.op.base.map.gem.obj = vma->gpuva.gem.obj;
+	vm->dummy_ops.op.base.map.gem.offset = vma->gpuva.gem.offset;
+	vm->dummy_ops.op.map.vma = vma;
+	vm->dummy_ops.op.map.immediate = true;
+	vm->dummy_ops.op.map.dumpable = vma->gpuva.flags &
 XE_VMA_DUMPABLE;
+	vm->dummy_ops.op.map.is_null = xe_vma_is_null(vma);
+}
+
+static struct dma_fence *ops_execute(struct xe_vm *vm,
+				     struct xe_vma_ops *vops,
+				     bool cleanup);
 

struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker) {

drivers/gpu/drm/xe/xe_vm.c +3 -1
@@ -780,7 +792,9 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm,
 bool rebind_worker)
 			trace_xe_vma_rebind_worker(vma);
 		else
 			trace_xe_vma_rebind_exec(vma);
-		fence = xe_vm_bind_vma(vma, NULL, NULL, 0, false, false);
+
+		xe_vm_populate_dummy_rebind(vm, vma);
+		fence = ops_execute(vm, &vm->dummy_ops.vops, false);
 		if (IS_ERR(fence))
 			return fence;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	}
@@ -1289,6 +1303,11 @@ static void xe_vm_free_scratch(struct xe_vm *vm)
 	}
 }
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c static void xe_vm_free_scratch(struct xe_vm *vm) · line 1309

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
+static void xe_vma_ops_init(struct xe_vma_ops *vops)
+{
+	INIT_LIST_HEAD(&vops->list);
+}

this already showed up on patch 4... you just add it in patch5, then moved it to another location on patch 5...

can this be better organized?

Yes.

Quoted 185 lines
Quoted 185 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 {
 	struct drm_gem_object *vm_resv_obj;
@@ -1310,6 +1329,10 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	init_rwsem(&vm->lock);
 	mutex_init(&vm->snap_mutex);
 
+	xe_vma_ops_init(&vm->dummy_ops.vops);
+	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
+	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
+
 	INIT_LIST_HEAD(&vm->rebind_list);
 
drivers/gpu/drm/xe/xe_vm.c +1 -0
 	INIT_LIST_HEAD(&vm->userptr.repin_list);
@@ -2140,6 +2163,7 @@ vm_bind_ioctl_ops_create(struct xe_vm *vm, struct
 xe_bo *bo,
 		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
 
 		if (__op->op == DRM_GPUVA_OP_MAP) {
+			op->map.immediate = !xe_vm_in_fault_mode(vm);
 			op->map.is_null = flags &
 DRM_XE_VM_BIND_FLAG_NULL;

op->map.dumpable = flags &
DRM_XE_VM_BIND_FLAG_DUMPABLE;

drivers/gpu/drm/xe/xe_vm.c +2 -2
 			op->map.pat_index = pat_index;
@@ -2465,7 +2489,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 {
 	struct dma_fence *fence = NULL;
 
-	lockdep_assert_held_write(&vm->lock);
+	lockdep_assert_held(&vm->lock);
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -2473,7 +2497,7 @@ static struct dma_fence *op_execute(struct xe_vm
 *vm, struct xe_vma *vma,
 	case DRM_GPUVA_OP_MAP:
 		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
 				   op->syncs, op->num_syncs,
-				   !xe_vm_in_fault_mode(vm),
+				   op->map.immediate,
 				   op->flags & XE_VMA_OP_FIRST,
 				   op->flags & XE_VMA_OP_LAST);
drivers/gpu/drm/xe/xe_vm.c +1 -1
 		break;
@@ -2554,7 +2578,7 @@ __xe_vma_op_execute(struct xe_vm *vm, struct
 xe_vma *vma,
 retry_userptr:
 	fence = op_execute(vm, vma, op);
 	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
-		lockdep_assert_held_write(&vm->lock);
+		lockdep_assert_held(&vm->lock);
 
 		if (op->base.op == DRM_GPUVA_OP_REMAP) {
drivers/gpu/drm/xe/xe_vm.c +1 -1
 			if (!op->remap.unmap_done)
@@ -2583,7 +2607,7 @@ xe_vma_op_execute(struct xe_vm *vm, struct
 xe_vma_op *op)
 {
 	struct dma_fence *fence = ERR_PTR(-ENOMEM);
 
-	lockdep_assert_held_write(&vm->lock);
+	lockdep_assert_held(&vm->lock);
 
 	switch (op->base.op) {
drivers/gpu/drm/xe/xe_vm.c +0 -5
 	case DRM_GPUVA_OP_MAP:
@@ -2992,11 +3016,6 @@ static int vm_bind_ioctl_signal_fences(struct xe_vm
 *vm,
 	return err;
 }
 
-static void xe_vma_ops_init(struct xe_vma_ops *vops)
-{
-	INIT_LIST_HEAD(&vops->list);
-}
-
 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 {
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct xe_device *xe = to_xe_device(dev);
drivers/gpu/drm/xe/xe_vm_types.h +48 -1
index cc3dce893f1e..7ef9e632154a 100644
@@ -18,6 +18,7 @@
 #include "xe_range_fence.h"
 
 struct xe_bo;
+struct xe_device;
 struct xe_sync_entry;
 struct xe_user_fence;
 struct xe_vm;
@@ -124,7 +125,96 @@ struct xe_userptr_vma {
 	struct xe_userptr userptr;
 };
 
-struct xe_device;
+/** struct xe_vma_op_map - VMA map operation */
+struct xe_vma_op_map {
+	/** @vma: VMA to map */
+	struct xe_vma *vma;
+	/** @immediate: Immediate bind */
+	bool immediate;
+	/** @is_null: is NULL binding */
+	bool is_null;
+	/** @dumpable: whether BO is dumped on GPU hang */
+	bool dumpable;
+	/** @pat_index: The pat index to use for this operation. */
+	u16 pat_index;
+};
+
+/** struct xe_vma_op_remap - VMA remap operation */
+struct xe_vma_op_remap {
+	/** @prev: VMA preceding part of a split mapping */
+	struct xe_vma *prev;
+	/** @next: VMA subsequent part of a split mapping */
+	struct xe_vma *next;
+	/** @start: start of the VMA unmap */
+	u64 start;
+	/** @range: range of the VMA unmap */
+	u64 range;
+	/** @skip_prev: skip prev rebind */
+	bool skip_prev;
+	/** @skip_next: skip next rebind */
+	bool skip_next;
+	/** @unmap_done: unmap operation in done */
+	bool unmap_done;
+};
+
+/** struct xe_vma_op_prefetch - VMA prefetch operation */
+struct xe_vma_op_prefetch {
+	/** @region: memory region to prefetch to */
+	u32 region;
+};
+
+/** enum xe_vma_op_flags - flags for VMA operation */
+enum xe_vma_op_flags {
+	/** @XE_VMA_OP_FIRST: first VMA operation for a set of syncs */
+	XE_VMA_OP_FIRST			= BIT(0),
+	/** @XE_VMA_OP_LAST: last VMA operation for a set of syncs */
+	XE_VMA_OP_LAST			= BIT(1),
+	/** @XE_VMA_OP_COMMITTED: VMA operation committed */
+	XE_VMA_OP_COMMITTED		= BIT(2),
+	/** @XE_VMA_OP_PREV_COMMITTED: Previous VMA operation

committed */

drivers/gpu/drm/xe/xe_vm_types.h +2 -0
+	XE_VMA_OP_PREV_COMMITTED	= BIT(3),
+	/** @XE_VMA_OP_NEXT_COMMITTED: Next VMA operation committed

*/

drivers/gpu/drm/xe/xe_vm_types.h +35 -0
+	XE_VMA_OP_NEXT_COMMITTED	= BIT(4),
+};
+
+/** struct xe_vma_op - VMA operation */
+struct xe_vma_op {
+	/** @base: GPUVA base operation */
+	struct drm_gpuva_op base;
+	/**
+	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
+	 * operations is processed
+	 */
+	struct drm_gpuva_ops *ops;
+	/** @q: exec queue for this operation */
+	struct xe_exec_queue *q;
+	/**
+	 * @syncs: syncs for this operation, only used on first and last
+	 * operation
+	 */
+	struct xe_sync_entry *syncs;
+	/** @num_syncs: number of syncs */
+	u32 num_syncs;
+	/** @link: async operation link */
+	struct list_head link;
+	/** @flags: operation flags */
+	enum xe_vma_op_flags flags;
+
+	union {
+		/** @map: VMA map operation specific data */
+		struct xe_vma_op_map map;
+		/** @remap: VMA remap operation specific data */
+		struct xe_vma_op_remap remap;
+		/** @prefetch: VMA prefetch operation specific data */
+		struct xe_vma_op_prefetch prefetch;
+	};
+};
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm_types.h struct xe_userptr_vma { · line 217

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm_types.h +6 -0
+
+/** struct xe_vma_ops - VMA operations */
+struct xe_vma_ops {
+	/** @list: list of VMA operations */
+	struct list_head list;
+};

this already showed up on patch 4... you just add it in patch5, then moved it to another location on patch 5...

Yes.

Quoted 7 lines
Quoted 7 lines

struct xe_vm {

drivers/gpu/drm/xe/xe_vm_types.h +0 -0
 	/** @gpuvm: base GPUVM used to track VMAs */
@@ -267,99 +357,18 @@ struct xe_vm {
 		bool capture_once;
 	} error_capture;
 
Quoted 11 lines

drivers/gpu/drm/xe/xe_vm_types.h struct xe_vm { · line 366

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm_types.h +7 -0
+	/** @dummy_ops: dummy VMA ops to issue rebinds */
+	struct {
+		/** @dummy_ops.ops: dummy VMA ops */
+		struct xe_vma_ops vops;
+		/** @dummy_ops.op: dummy VMA op */
+		struct xe_vma_op op;
+	} dummy_ops;

If only from this patch, it seems you don't have to introduce this dummy_ops member to xe_vm. For example, it can be a local variable in xe_vm_rebind function. But I will keep looking. Maybe you made it this way for future patches.

I'm going to rewrite or already have rewritten this to use local xe_vm_ops member and execute all rebinds an atomic unit.

You can ignore this patch and also [1] in this rev of the review.

[1] https://patchwork.freedesktop.org/patch/582015/?series=125608&rev=5

Quoted 91 lines
Quoted 91 lines
drivers/gpu/drm/xe/xe_vm_types.h +1 -45
+
 	/** @batch_invalidate_tlb: Always invalidate TLB before batch start */
 	bool batch_invalidate_tlb;
 	/** @xef: XE file handle for tracking this VM's drm client */
 	struct xe_file *xef;
 };
 
-/** struct xe_vma_op_map - VMA map operation */
-struct xe_vma_op_map {
-	/** @vma: VMA to map */
-	struct xe_vma *vma;
-	/** @is_null: is NULL binding */
-	bool is_null;
-	/** @dumpable: whether BO is dumped on GPU hang */
-	bool dumpable;
-	/** @pat_index: The pat index to use for this operation. */
-	u16 pat_index;
-};
-
-/** struct xe_vma_op_remap - VMA remap operation */
-struct xe_vma_op_remap {
-	/** @prev: VMA preceding part of a split mapping */
-	struct xe_vma *prev;
-	/** @next: VMA subsequent part of a split mapping */
-	struct xe_vma *next;
-	/** @start: start of the VMA unmap */
-	u64 start;
-	/** @range: range of the VMA unmap */
-	u64 range;
-	/** @skip_prev: skip prev rebind */
-	bool skip_prev;
-	/** @skip_next: skip next rebind */
-	bool skip_next;
-	/** @unmap_done: unmap operation in done */
-	bool unmap_done;
-};
-
-/** struct xe_vma_op_prefetch - VMA prefetch operation */
-struct xe_vma_op_prefetch {
-	/** @region: memory region to prefetch to */
-	u32 region;
-};
-
-/** enum xe_vma_op_flags - flags for VMA operation */
-enum xe_vma_op_flags {
-	/** @XE_VMA_OP_FIRST: first VMA operation for a set of syncs */
-	XE_VMA_OP_FIRST			= BIT(0),
-	/** @XE_VMA_OP_LAST: last VMA operation for a set of syncs */
-	XE_VMA_OP_LAST			= BIT(1),
-	/** @XE_VMA_OP_COMMITTED: VMA operation committed */
-	XE_VMA_OP_COMMITTED		= BIT(2),
-	/** @XE_VMA_OP_PREV_COMMITTED: Previous VMA operation

committed */

drivers/gpu/drm/xe/xe_vm_types.h +0 -2
-	XE_VMA_OP_PREV_COMMITTED	= BIT(3),
-	/** @XE_VMA_OP_NEXT_COMMITTED: Next VMA operation committed

*/

drivers/gpu/drm/xe/xe_vm_types.h +0 -35
-	XE_VMA_OP_NEXT_COMMITTED	= BIT(4),
-};
-
-/** struct xe_vma_op - VMA operation */
-struct xe_vma_op {
-	/** @base: GPUVA base operation */
-	struct drm_gpuva_op base;
-	/**
-	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
-	 * operations is processed
-	 */
-	struct drm_gpuva_ops *ops;
-	/** @q: exec queue for this operation */
-	struct xe_exec_queue *q;
-	/**
-	 * @syncs: syncs for this operation, only used on first and last
-	 * operation
-	 */
-	struct xe_sync_entry *syncs;
-	/** @num_syncs: number of syncs */
-	u32 num_syncs;
-	/** @link: async operation link */
-	struct list_head link;
-	/** @flags: operation flags */
-	enum xe_vma_op_flags flags;
-
-	union {
-		/** @map: VMA map operation specific data */
-		struct xe_vma_op_map map;
-		/** @remap: VMA remap operation specific data */
-		struct xe_vma_op_remap remap;
-		/** @prefetch: VMA prefetch operation specific data */
-		struct xe_vma_op_prefetch prefetch;
-	};
-};
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm_types.h struct xe_vm { · line 363

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm_types.h +0 -6
-
-/** struct xe_vma_ops - VMA operations */
-struct xe_vma_ops {
-	/** @list: list of VMA operations */
-	struct list_head list;
-};

It seems you moved a block of codes to another location. It caused more work for code review. Better to avoid this if we can.

See above, with my refactor dummy binds and this moving this is not required.

Matt

Quoted 7 lines

Oak

Quoted 4 lines

-
#endif
--
2.34.1

author maintainer
#19 lore raw

PATCH v4 6/30 drm/xe: Simplify VM bind IOCTL error handling and cleanup

In reply to Matthew Brost

Clean up everything in VM bind IOCTL in 1 path for both errors and non-errors. Also move VM bind IOCTL cleanup from ops (also used by non-IOCTL binds) to the VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_vm.c       | 60 +++++---------------------------
 drivers/gpu/drm/xe/xe_vm_types.h |  5 ---
 2 files changed, 9 insertions(+), 56 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +9 -51
index 0bb807c05d7b..dde777c807cf 100644
@@ -769,8 +769,7 @@ static void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma)
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct xe_vma_ops *vops,
-				     bool cleanup);
+				     struct xe_vma_ops *vops);
 
 struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
 {
@@ -794,7 +793,7 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
 			trace_xe_vma_rebind_exec(vma);
 
 		xe_vm_populate_dummy_rebind(vm, vma);
-		fence = ops_execute(vm, &vm->dummy_ops.vops, false);
+		fence = ops_execute(vm, &vm->dummy_ops.vops);
 		if (IS_ERR(fence))
 			return fence;
 	}
@@ -2474,7 +2473,6 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 	if (!last_op)
 		return 0;
 
-	last_op->ops = ops;
 	if (last) {
 		last_op->flags |= XE_VMA_OP_LAST;
 		last_op->num_syncs = num_syncs;
@@ -2643,25 +2641,6 @@ xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
 	return fence;
 }
 
-static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)
-{
-	bool last = op->flags & XE_VMA_OP_LAST;
-
-	if (last) {
-		while (op->num_syncs--)
-			xe_sync_entry_cleanup(&op->syncs[op->num_syncs]);
-		kfree(op->syncs);
-		if (op->q)
-			xe_exec_queue_put(op->q);
-	}
-	if (!list_empty(&op->link))
-		list_del(&op->link);
-	if (op->ops)
-		drm_gpuva_ops_free(&vm->gpuvm, op->ops);
-	if (last)
-		xe_vm_put(vm);
-}
-
 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
 			     bool post_commit, bool prev_post_commit,
 			     bool next_post_commit)
@@ -2738,8 +2717,6 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,
 					 op->flags & XE_VMA_OP_PREV_COMMITTED,
 					 op->flags & XE_VMA_OP_NEXT_COMMITTED);
 		}
-
-		drm_gpuva_ops_free(&vm->gpuvm, __ops);
 	}
 }
 
@@ -2818,8 +2795,7 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec *exec,
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct xe_vma_ops *vops,
-				     bool cleanup)
+				     struct xe_vma_ops *vops)
 {
 	struct xe_vma_op *op, *next;
 	struct dma_fence *fence = NULL;
@@ -2834,8 +2810,6 @@ static struct dma_fence *ops_execute(struct xe_vm *vm,
 				 op->base.op, PTR_ERR(fence));
 			fence = ERR_PTR(-ENOSPC);
 		}
-		if (cleanup)
-			xe_vma_op_cleanup(vm, op);
 	}
 
 	return fence;
@@ -2858,7 +2832,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 		if (err)
 			goto unlock;
 
-		fence = ops_execute(vm, vops, true);
+		fence = ops_execute(vm, vops);
 		if (IS_ERR(fence)) {
 			err = PTR_ERR(fence);
 			/* FIXME: Killing VM rather than proper error handling */
@@ -3211,30 +3185,14 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		goto unwind_ops;
 	}
 
-	xe_vm_get(vm);
-	if (q)
-		xe_exec_queue_get(q);
-
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
-	up_write(&vm->lock);
-
-	if (q)
-		xe_exec_queue_put(q);
-	xe_vm_put(vm);
-
-	for (i = 0; bos && i < args->num_binds; ++i)
-		xe_bo_put(bos[i]);
-
-	kvfree(bos);
-	kvfree(ops);
-	if (args->num_binds > 1)
-		kvfree(bind_ops);
-
-	return err;
-
 unwind_ops:
-	vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	if (err && err != -ENODATA)
+		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	for (i = args->num_binds - 1; i >= 0; --i)
+		if (ops[i])
+			drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
 free_syncs:
 	if (err == -ENODATA)
 		err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
drivers/gpu/drm/xe/xe_vm_types.h +0 -5
index 7ef9e632154a..f097fe318a74 100644
@@ -181,11 +181,6 @@ enum xe_vma_op_flags {
 struct xe_vma_op {
 	/** @base: GPUVA base operation */
 	struct drm_gpuva_op base;
-	/**
-	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
-	 * operations is processed
-	 */
-	struct drm_gpuva_ops *ops;
 	/** @q: exec queue for this operation */
 	struct xe_exec_queue *q;
 	/**
Signature · 1 line
2.34.1
#20 lore raw

RE: [PATCH v4 06/30] drm/xe: Simplify VM bind IOCTL error handling and cleanup

Hi Matt,

This looks like a nice clean up to me. See one comment inline.

Quoted 104 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 06/30] drm/xe: Simplify VM bind IOCTL error handling and cleanup

Clean up everything in VM bind IOCTL in 1 path for both errors and non-errors. Also move VM bind IOCTL cleanup from ops (also used by non-IOCTL binds) to the VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_vm.c       | 60 +++++---------------------------
 drivers/gpu/drm/xe/xe_vm_types.h |  5 ---
 2 files changed, 9 insertions(+), 56 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -2
index 0bb807c05d7b..dde777c807cf 100644
@@ -769,8 +769,7 @@ static void xe_vm_populate_dummy_rebind(struct
 xe_vm *vm, struct xe_vma *vma)
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct xe_vma_ops *vops,
-				     bool cleanup);
+				     struct xe_vma_ops *vops);
 
 struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)

{

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -794,7 +793,7 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm,
 bool rebind_worker)
 			trace_xe_vma_rebind_exec(vma);
 
 		xe_vm_populate_dummy_rebind(vm, vma);
-		fence = ops_execute(vm, &vm->dummy_ops.vops, false);
+		fence = ops_execute(vm, &vm->dummy_ops.vops);
 		if (IS_ERR(fence))
 			return fence;
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	}
@@ -2474,7 +2473,6 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 	if (!last_op)
 		return 0;
 
-	last_op->ops = ops;
 	if (last) {
 		last_op->flags |= XE_VMA_OP_LAST;
drivers/gpu/drm/xe/xe_vm.c +0 -19
 		last_op->num_syncs = num_syncs;
@@ -2643,25 +2641,6 @@ xe_vma_op_execute(struct xe_vm *vm, struct
 xe_vma_op *op)
 	return fence;
 }
 
-static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)
-{
-	bool last = op->flags & XE_VMA_OP_LAST;
-
-	if (last) {
-		while (op->num_syncs--)
-			xe_sync_entry_cleanup(&op->syncs[op->num_syncs]);
-		kfree(op->syncs);
-		if (op->q)
-			xe_exec_queue_put(op->q);
-	}
-	if (!list_empty(&op->link))
-		list_del(&op->link);
-	if (op->ops)
-		drm_gpuva_ops_free(&vm->gpuvm, op->ops);
-	if (last)
-		xe_vm_put(vm);
-}
-
 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
 			     bool post_commit, bool prev_post_commit,
drivers/gpu/drm/xe/xe_vm.c +0 -2
 			     bool next_post_commit)
@@ -2738,8 +2717,6 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm
 *vm,
 					 op->flags &
 XE_VMA_OP_PREV_COMMITTED,
 					 op->flags &
 XE_VMA_OP_NEXT_COMMITTED);
 		}
-
-		drm_gpuva_ops_free(&vm->gpuvm, __ops);
drivers/gpu/drm/xe/xe_vm.c +1 -2
 	}
 }
@@ -2818,8 +2795,7 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct xe_vma_ops *vops,
-				     bool cleanup)
+				     struct xe_vma_ops *vops)
 {
 	struct xe_vma_op *op, *next;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct dma_fence *fence = NULL;

drivers/gpu/drm/xe/xe_vm.c static struct dma_fence *ops_execute(struct xe_vm · line 2839

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +0 -2
@@ -2834,8 +2810,6 @@ static struct dma_fence *ops_execute(struct xe_vm
 *vm,
 				 op->base.op, PTR_ERR(fence));
 			fence = ERR_PTR(-ENOSPC);
 		}
-		if (cleanup)
-			xe_vma_op_cleanup(vm, op);

Now with this cleanup code removed, do you still want to loop all the ops in the list when xe_vma_op_execute failed with error? Should we break and return earlier in this case?

Oak

Quoted 69 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	}
 
drivers/gpu/drm/xe/xe_vm.c +1 -1
 	return fence;
@@ -2858,7 +2832,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 		if (err)
 			goto unlock;
 
-		fence = ops_execute(vm, vops, true);
+		fence = ops_execute(vm, vops);
 		if (IS_ERR(fence)) {
 			err = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_vm.c +5 -21
 			/* FIXME: Killing VM rather than proper error handling */
@@ -3211,30 +3185,14 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto unwind_ops;
 	}
 
-	xe_vm_get(vm);
-	if (q)
-		xe_exec_queue_get(q);
-
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
-	up_write(&vm->lock);
-
-	if (q)
-		xe_exec_queue_put(q);
-	xe_vm_put(vm);
-
-	for (i = 0; bos && i < args->num_binds; ++i)
-		xe_bo_put(bos[i]);
-
-	kvfree(bos);
-	kvfree(ops);
-	if (args->num_binds > 1)
-		kvfree(bind_ops);
-
-	return err;
-
 unwind_ops:
-	vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	if (err && err != -ENODATA)
+		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	for (i = args->num_binds - 1; i >= 0; --i)
+		if (ops[i])
+			drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
 free_syncs:
 	if (err == -ENODATA)
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
drivers/gpu/drm/xe/xe_vm_types.h +0 -6
index 7ef9e632154a..f097fe318a74 100644
@@ -181,11 +181,6 @@ enum xe_vma_op_flags {
 struct xe_vma_op {
 	/** @base: GPUVA base operation */
 	struct drm_gpuva_op base;
-	/**
-	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
-	 * operations is processed
-	 */
-	struct drm_gpuva_ops *ops;
 	/** @q: exec queue for this operation */
 	struct xe_exec_queue *q;
 	/**
--

2.34.1

author maintainer
#21 lore raw

Re: [PATCH v4 06/30] drm/xe: Simplify VM bind IOCTL error handling and cleanup

Quoted 109 lines · Oak

Hi Matt,

This looks like a nice clean up to me. See one comment inline.

Quoted 105 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 06/30] drm/xe: Simplify VM bind IOCTL error handling and cleanup

Clean up everything in VM bind IOCTL in 1 path for both errors and non-errors. Also move VM bind IOCTL cleanup from ops (also used by non-IOCTL binds) to the VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_vm.c       | 60 +++++---------------------------
 drivers/gpu/drm/xe/xe_vm_types.h |  5 ---
 2 files changed, 9 insertions(+), 56 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -2
index 0bb807c05d7b..dde777c807cf 100644
@@ -769,8 +769,7 @@ static void xe_vm_populate_dummy_rebind(struct
 xe_vm *vm, struct xe_vma *vma)
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct xe_vma_ops *vops,
-				     bool cleanup);
+				     struct xe_vma_ops *vops);
 
 struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)

{

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -794,7 +793,7 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm,
 bool rebind_worker)
 			trace_xe_vma_rebind_exec(vma);
 
 		xe_vm_populate_dummy_rebind(vm, vma);
-		fence = ops_execute(vm, &vm->dummy_ops.vops, false);
+		fence = ops_execute(vm, &vm->dummy_ops.vops);
 		if (IS_ERR(fence))
 			return fence;
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	}
@@ -2474,7 +2473,6 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 	if (!last_op)
 		return 0;
 
-	last_op->ops = ops;
 	if (last) {
 		last_op->flags |= XE_VMA_OP_LAST;
drivers/gpu/drm/xe/xe_vm.c +0 -19
 		last_op->num_syncs = num_syncs;
@@ -2643,25 +2641,6 @@ xe_vma_op_execute(struct xe_vm *vm, struct
 xe_vma_op *op)
 	return fence;
 }
 
-static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)
-{
-	bool last = op->flags & XE_VMA_OP_LAST;
-
-	if (last) {
-		while (op->num_syncs--)
-			xe_sync_entry_cleanup(&op->syncs[op->num_syncs]);
-		kfree(op->syncs);
-		if (op->q)
-			xe_exec_queue_put(op->q);
-	}
-	if (!list_empty(&op->link))
-		list_del(&op->link);
-	if (op->ops)
-		drm_gpuva_ops_free(&vm->gpuvm, op->ops);
-	if (last)
-		xe_vm_put(vm);
-}
-
 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
 			     bool post_commit, bool prev_post_commit,
drivers/gpu/drm/xe/xe_vm.c +0 -2
 			     bool next_post_commit)
@@ -2738,8 +2717,6 @@ static void vm_bind_ioctl_ops_unwind(struct xe_vm
 *vm,
 					 op->flags &
 XE_VMA_OP_PREV_COMMITTED,
 					 op->flags &
 XE_VMA_OP_NEXT_COMMITTED);
 		}
-
-		drm_gpuva_ops_free(&vm->gpuvm, __ops);
drivers/gpu/drm/xe/xe_vm.c +1 -2
 	}
 }
@@ -2818,8 +2795,7 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 }
 
 static struct dma_fence *ops_execute(struct xe_vm *vm,
-				     struct xe_vma_ops *vops,
-				     bool cleanup)
+				     struct xe_vma_ops *vops)
 {
 	struct xe_vma_op *op, *next;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	struct dma_fence *fence = NULL;
@@ -2834,8 +2810,6 @@ static struct dma_fence *ops_execute(struct xe_vm
 *vm,
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c static struct dma_fence *ops_execute(struct xe_vm *vm, · line 2838

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -2
 				 op->base.op, PTR_ERR(fence));
 			fence = ERR_PTR(-ENOSPC);
 		}
-		if (cleanup)
-			xe_vma_op_cleanup(vm, op);

Now with this cleanup code removed, do you still want to loop all the ops in the list when xe_vma_op_execute failed with error? Should we break and return earlier in this case?

Yes, this loop gets rewritten in patch #18 but for this patch to be correct, lets break the loop on error.

Matt

Quoted 72 lines

Oak

Quoted 69 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	}
 
drivers/gpu/drm/xe/xe_vm.c +1 -1
 	return fence;
@@ -2858,7 +2832,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 		if (err)
 			goto unlock;
 
-		fence = ops_execute(vm, vops, true);
+		fence = ops_execute(vm, vops);
 		if (IS_ERR(fence)) {
 			err = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_vm.c +5 -21
 			/* FIXME: Killing VM rather than proper error handling */
@@ -3211,30 +3185,14 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto unwind_ops;
 	}
 
-	xe_vm_get(vm);
-	if (q)
-		xe_exec_queue_get(q);
-
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
-	up_write(&vm->lock);
-
-	if (q)
-		xe_exec_queue_put(q);
-	xe_vm_put(vm);
-
-	for (i = 0; bos && i < args->num_binds; ++i)
-		xe_bo_put(bos[i]);
-
-	kvfree(bos);
-	kvfree(ops);
-	if (args->num_binds > 1)
-		kvfree(bind_ops);
-
-	return err;
-
 unwind_ops:
-	vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	if (err && err != -ENODATA)
+		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	for (i = args->num_binds - 1; i >= 0; --i)
+		if (ops[i])
+			drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
 free_syncs:
 	if (err == -ENODATA)
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
drivers/gpu/drm/xe/xe_vm_types.h +0 -6
index 7ef9e632154a..f097fe318a74 100644
@@ -181,11 +181,6 @@ enum xe_vma_op_flags {
 struct xe_vma_op {
 	/** @base: GPUVA base operation */
 	struct drm_gpuva_op base;
-	/**
-	 * @ops: GPUVA ops, when set call drm_gpuva_ops_free after this
-	 * operations is processed
-	 */
-	struct drm_gpuva_ops *ops;
 	/** @q: exec queue for this operation */
 	struct xe_exec_queue *q;
 	/**
--

2.34.1

author maintainer
#22 lore raw

PATCH v4 7/30 drm/xe: Update pagefaults to use dummy VMA operations

In reply to Matthew Brost

All bind interfaces are transitioning to use VMA ops, update pagefaults to use VMA ops.

---
 drivers/gpu/drm/xe/xe_gt_pagefault.c |  7 ++--
 drivers/gpu/drm/xe/xe_vm.c           | 54 +++++++++++++++++++---------
 drivers/gpu/drm/xe/xe_vm.h           |  3 ++
 drivers/gpu/drm/xe/xe_vm_types.h     |  2 ++
 4 files changed, 47 insertions(+), 19 deletions(-)
drivers/gpu/drm/xe/xe_gt_pagefault.c +4 -3
index 73c535193a98..3c7d6e4e3917 100644
@@ -19,7 +19,6 @@
 #include "xe_guc.h"
 #include "xe_guc_ct.h"
 #include "xe_migrate.h"
-#include "xe_pt.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
@@ -209,8 +208,10 @@ static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
 
 	/* Bind VMA only to the GT that has faulted */
 	trace_xe_vma_pf_bind(vma);
-	fence = __xe_pt_bind_vma(tile, vma, xe_tile_migrate_engine(tile), NULL, 0,
-				 vma->tile_present & BIT(tile->id));
+	xe_vm_populate_dummy_rebind(vm, vma);
+	vm->dummy_ops.op.tile_mask = BIT(tile->id);
+	vm->dummy_ops.op.q = xe_tile_migrate_engine(tile);
+	fence = xe_vm_ops_execute(vm, &vm->dummy_ops.vops);
 	if (IS_ERR(fence)) {
 		ret = PTR_ERR(fence);
 		goto unlock_dma_resv;
drivers/gpu/drm/xe/xe_vm.c +38 -16

157 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.h +3 -0
index 6df1f1c7f85d..b40160b1be01 100644
@@ -262,6 +262,9 @@ static inline struct dma_resv *xe_vm_resv(struct xe_vm *vm)
  */
 #define xe_vm_assert_held(vm) dma_resv_assert_held(xe_vm_resv(vm))
 
+void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma);
+struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops);
+
 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
 #define vm_dbg drm_dbg
 #else
drivers/gpu/drm/xe/xe_vm_types.h +2 -0
index f097fe318a74..0bba5543ac28 100644
@@ -194,6 +194,8 @@ struct xe_vma_op {
 	struct list_head link;
 	/** @flags: operation flags */
 	enum xe_vma_op_flags flags;
+	/** @tile_mask: Tile mask for operation */
+	u8 tile_mask;
 
 	union {
 		/** @map: VMA map operation specific data */
Signature · 1 line
2.34.1
author maintainer
#23 lore raw

PATCH v4 8/30 drm/xe: s/xe_tile_migrate_engine/xe_tile_migrate_exec_queue

In reply to Matthew Brost

xe_engine is now xe_exec_queue, adjust this function's name to reflect.

---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 2 +-
 drivers/gpu/drm/xe/xe_migrate.c      | 8 ++++----
 drivers/gpu/drm/xe/xe_migrate.h      | 3 ++-
 3 files changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -1
index 3c7d6e4e3917..878e234166aa 100644
@@ -210,7 +210,7 @@ static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
 	trace_xe_vma_pf_bind(vma);
 	xe_vm_populate_dummy_rebind(vm, vma);
 	vm->dummy_ops.op.tile_mask = BIT(tile->id);
-	vm->dummy_ops.op.q = xe_tile_migrate_engine(tile);
+	vm->dummy_ops.op.q = xe_tile_migrate_exec_queue(tile);
 	fence = xe_vm_ops_execute(vm, &vm->dummy_ops.vops);
 	if (IS_ERR(fence)) {
 		ret = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_migrate.c +4 -4
index ee1bb938c493..3fd76f912fda 100644
@@ -84,15 +84,15 @@ struct xe_migrate {
 #define MAX_PTE_PER_SDI 0x1FE
 
 /**
- * xe_tile_migrate_engine() - Get this tile's migrate engine.
+ * xe_tile_migrate_exec_queue() - Get this tile's migrate exec queue.
  * @tile: The tile.
  *
- * Returns the default migrate engine of this tile.
+ * Returns the default migrate exec queue of this tile.
  * TODO: Perhaps this function is slightly misplaced, and even unneeded?
  *
- * Return: The default migrate engine
+ * Return: The default migrate exec queue
  */
-struct xe_exec_queue *xe_tile_migrate_engine(struct xe_tile *tile)
+struct xe_exec_queue *xe_tile_migrate_exec_queue(struct xe_tile *tile)
 {
 	return tile->migrate->q;
 }
drivers/gpu/drm/xe/xe_migrate.h +2 -1
index 951f19318ea4..9935ce336bae 100644
@@ -106,5 +106,6 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 
 void xe_migrate_wait(struct xe_migrate *m);
 
-struct xe_exec_queue *xe_tile_migrate_engine(struct xe_tile *tile);
+struct xe_exec_queue *xe_tile_migrate_exec_queue(struct xe_tile *tile);
+
 #endif
Signature · 1 line
2.34.1
#24 lore raw

RE: [PATCH v4 08/30] drm/xe: s/xe_tile_migrate_engine/xe_tile_migrate_exec_queue

This patch lgtm. Reviewed-by: Oak Zeng <[email protected]>

Oak

Quoted 73 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 08/30] drm/xe:
s/xe_tile_migrate_engine/xe_tile_migrate_exec_queue

xe_engine is now xe_exec_queue, adjust this function's name to reflect.

---
 drivers/gpu/drm/xe/xe_gt_pagefault.c | 2 +-
 drivers/gpu/drm/xe/xe_migrate.c      | 8 ++++----
 drivers/gpu/drm/xe/xe_migrate.h      | 3 ++-
 3 files changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/xe/xe_gt_pagefault.c +1 -1
index 3c7d6e4e3917..878e234166aa 100644
@@ -210,7 +210,7 @@ static int handle_pagefault(struct xe_gt *gt, struct
 pagefault *pf)
 	trace_xe_vma_pf_bind(vma);
 	xe_vm_populate_dummy_rebind(vm, vma);
 	vm->dummy_ops.op.tile_mask = BIT(tile->id);
-	vm->dummy_ops.op.q = xe_tile_migrate_engine(tile);
+	vm->dummy_ops.op.q = xe_tile_migrate_exec_queue(tile);
 	fence = xe_vm_ops_execute(vm, &vm->dummy_ops.vops);
 	if (IS_ERR(fence)) {
drivers/gpu/drm/xe/xe_gt_pagefault.c +0 -0
 		ret = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_migrate.c +4 -4
index ee1bb938c493..3fd76f912fda 100644
@@ -84,15 +84,15 @@ struct xe_migrate {
 #define MAX_PTE_PER_SDI 0x1FE
 
 /**
- * xe_tile_migrate_engine() - Get this tile's migrate engine.
+ * xe_tile_migrate_exec_queue() - Get this tile's migrate exec queue.
  * @tile: The tile.
  *
- * Returns the default migrate engine of this tile.
+ * Returns the default migrate exec queue of this tile.
  * TODO: Perhaps this function is slightly misplaced, and even unneeded?
  *
- * Return: The default migrate engine
+ * Return: The default migrate exec queue
  */
-struct xe_exec_queue *xe_tile_migrate_engine(struct xe_tile *tile)
+struct xe_exec_queue *xe_tile_migrate_exec_queue(struct xe_tile *tile)
 {
 	return tile->migrate->q;
 }
drivers/gpu/drm/xe/xe_migrate.h +2 -2
index 951f19318ea4..9935ce336bae 100644
@@ -106,5 +106,6 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 
 void xe_migrate_wait(struct xe_migrate *m);
 
-struct xe_exec_queue *xe_tile_migrate_engine(struct xe_tile *tile);
+struct xe_exec_queue *xe_tile_migrate_exec_queue(struct xe_tile *tile);
+
 #endif
--

2.34.1

author maintainer
#25 lore raw

PATCH v4 9/30 drm/xe: Add some members to xe_vma_ops

In reply to Matthew Brost

This will help with moving to single jobs for many bind operations.

---
 drivers/gpu/drm/xe/xe_vm.c       | 13 ++++++++++---
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +10 -3
index a6bae2f5e296..f8b27746e5a7 100644
@@ -1307,9 +1307,16 @@ static void xe_vm_free_scratch(struct xe_vm *vm)
 	}
 }
 
-static void xe_vma_ops_init(struct xe_vma_ops *vops)
+static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
+			    struct xe_exec_queue *q,
+			    struct xe_sync_entry *syncs, u32 num_syncs)
 {
+	memset(vops, 0, sizeof(*vops));
 	INIT_LIST_HEAD(&vops->list);
+	vops->vm = vm;
+	vops->q = q;
+	vops->syncs = syncs;
+	vops->num_syncs = num_syncs;
 }
 
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
@@ -1333,7 +1340,7 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
 	init_rwsem(&vm->lock);
 	mutex_init(&vm->snap_mutex);
 
-	xe_vma_ops_init(&vm->dummy_ops.vops);
+	xe_vma_ops_init(&vm->dummy_ops.vops, vm, NULL, NULL, 0);
 	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
 	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
 
@@ -3176,7 +3183,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		goto free_syncs;
 	}
 
-	xe_vma_ops_init(&vops);
+	xe_vma_ops_init(&vops, vm, q, syncs, num_syncs);
 	for (i = 0; i < args->num_binds; ++i) {
 		u64 range = bind_ops[i].range;
 		u64 addr = bind_ops[i].addr;
drivers/gpu/drm/xe/xe_vm_types.h +8 -0
index 0bba5543ac28..f6ea4df64e79 100644
@@ -211,6 +211,14 @@ struct xe_vma_op {
 struct xe_vma_ops {
 	/** @list: list of VMA operations */
 	struct list_head list;
+	/** @vm: VM */
+	struct xe_vm *vm;
+	/** @q: exec queue these operations */
+	struct xe_exec_queue *q;
+	/** @syncs: syncs these operation */
+	struct xe_sync_entry *syncs;
+	/** @num_syncs: number of syncs */
+	u32 num_syncs;
 };
 
 struct xe_vm {
Signature · 1 line
2.34.1
#26 lore raw

RE: [PATCH v4 09/30] drm/xe: Add some members to xe_vma_ops

Quoted 41 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 09/30] drm/xe: Add some members to xe_vma_ops

This will help with moving to single jobs for many bind operations.

---
 drivers/gpu/drm/xe/xe_vm.c       | 13 ++++++++++---
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +8 -1
index a6bae2f5e296..f8b27746e5a7 100644
@@ -1307,9 +1307,16 @@ static void xe_vm_free_scratch(struct xe_vm *vm)
 	}
 }
 
-static void xe_vma_ops_init(struct xe_vma_ops *vops)
+static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
+			    struct xe_exec_queue *q,
+			    struct xe_sync_entry *syncs, u32 num_syncs)
 {
+	memset(vops, 0, sizeof(*vops));
 	INIT_LIST_HEAD(&vops->list);
+	vops->vm = vm;
+	vops->q = q;
+	vops->syncs = syncs;
+	vops->num_syncs = num_syncs;
 }
 
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)

drivers/gpu/drm/xe/xe_vm.c struct xe_vm *xe_vm_create(struct xe_device *xe, u32 · line 1344

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -1333,7 +1340,7 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32
 flags)
 	init_rwsem(&vm->lock);
 	mutex_init(&vm->snap_mutex);
 
-	xe_vma_ops_init(&vm->dummy_ops.vops);
+	xe_vma_ops_init(&vm->dummy_ops.vops, vm, NULL, NULL, 0);

You mentioned in previous review, that vm->dummy_ops will be dropped... I guess this line will also be dropped.

Otherwise, this patch looks good to me.

Oak

Quoted 35 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
 	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
 	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
@@ -3176,7 +3183,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto free_syncs;
 	}
 
-	xe_vma_ops_init(&vops);
+	xe_vma_ops_init(&vops, vm, q, syncs, num_syncs);
 	for (i = 0; i < args->num_binds; ++i) {
 		u64 range = bind_ops[i].range;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		u64 addr = bind_ops[i].addr;
drivers/gpu/drm/xe/xe_vm_types.h +8 -1
index 0bba5543ac28..f6ea4df64e79 100644
@@ -211,6 +211,14 @@ struct xe_vma_op {
 struct xe_vma_ops {
 	/** @list: list of VMA operations */
 	struct list_head list;
+	/** @vm: VM */
+	struct xe_vm *vm;
+	/** @q: exec queue these operations */
+	struct xe_exec_queue *q;
+	/** @syncs: syncs these operation */
+	struct xe_sync_entry *syncs;
+	/** @num_syncs: number of syncs */
+	u32 num_syncs;
 };
 
 struct xe_vm {
--

2.34.1

author maintainer
#27 lore raw

Re: [PATCH v4 09/30] drm/xe: Add some members to xe_vma_ops

Quoted 45 lines · Oak
Quoted 45 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 09/30] drm/xe: Add some members to xe_vma_ops

This will help with moving to single jobs for many bind operations.

---
 drivers/gpu/drm/xe/xe_vm.c       | 13 ++++++++++---
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +8 -1
index a6bae2f5e296..f8b27746e5a7 100644
@@ -1307,9 +1307,16 @@ static void xe_vm_free_scratch(struct xe_vm *vm)
 	}
 }
 
-static void xe_vma_ops_init(struct xe_vma_ops *vops)
+static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
+			    struct xe_exec_queue *q,
+			    struct xe_sync_entry *syncs, u32 num_syncs)
 {
+	memset(vops, 0, sizeof(*vops));
 	INIT_LIST_HEAD(&vops->list);
+	vops->vm = vm;
+	vops->q = q;
+	vops->syncs = syncs;
+	vops->num_syncs = num_syncs;
 }
 
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
@@ -1333,7 +1340,7 @@ struct xe_vm *xe_vm_create(struct xe_device *xe, u32
 flags)
 	init_rwsem(&vm->lock);
 	mutex_init(&vm->snap_mutex);
 
Quoted 6 lines

drivers/gpu/drm/xe/xe_vm.c struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags) · line 1343

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
-	xe_vma_ops_init(&vm->dummy_ops.vops);
+	xe_vma_ops_init(&vm->dummy_ops.vops, vm, NULL, NULL, 0);

You mentioned in previous review, that vm->dummy_ops will be dropped... I guess this line will also be dropped.

Yes, will drop the dummy ops.

Matt

Quoted 40 lines

Otherwise, this patch looks good to me.

Oak

Quoted 35 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
 	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
 	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
@@ -3176,7 +3183,7 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto free_syncs;
 	}
 
-	xe_vma_ops_init(&vops);
+	xe_vma_ops_init(&vops, vm, q, syncs, num_syncs);
 	for (i = 0; i < args->num_binds; ++i) {
 		u64 range = bind_ops[i].range;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		u64 addr = bind_ops[i].addr;
drivers/gpu/drm/xe/xe_vm_types.h +8 -1
index 0bba5543ac28..f6ea4df64e79 100644
@@ -211,6 +211,14 @@ struct xe_vma_op {
 struct xe_vma_ops {
 	/** @list: list of VMA operations */
 	struct list_head list;
+	/** @vm: VM */
+	struct xe_vm *vm;
+	/** @q: exec queue these operations */
+	struct xe_exec_queue *q;
+	/** @syncs: syncs these operation */
+	struct xe_sync_entry *syncs;
+	/** @num_syncs: number of syncs */
+	u32 num_syncs;
 };
 
 struct xe_vm {
--

2.34.1

author maintainer
#28 lore raw

PATCH v4 10/30 drm/xe: Add vm_bind_ioctl_ops_install_fences helper

In reply to Matthew Brost

Simplify VM bind code by signaling out-fences / destroying VMAs in a single location. Will help with transition single job for many bind ops.

---
 drivers/gpu/drm/xe/xe_vm.c | 55 ++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 32 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +23 -32
index f8b27746e5a7..8c96c98cba37 100644
@@ -1658,7 +1658,7 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
 	struct dma_fence *fence = NULL;
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_present);
 	int err;
 	u8 id;
@@ -1716,10 +1716,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
 
 	fence = cf ? &cf->base : !fence ?
 		xe_exec_queue_last_fence_get(wait_exec_queue, vm) : fence;
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL, fence);
-	}
 
 	return fence;
 
@@ -1743,7 +1739,7 @@ xe_vm_bind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
 	struct xe_vm *vm = xe_vma_vm(vma);
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_mask);
 	int err;
 	u8 id;
@@ -1790,12 +1786,6 @@ xe_vm_bind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
 		}
 	}
 
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL,
-					     cf ? &cf->base : fence);
-	}
-
 	return cf ? &cf->base : fence;
 
 err_fences:
@@ -1847,15 +1837,8 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
 		if (IS_ERR(fence))
 			return fence;
 	} else {
-		int i;
-
 		xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
-
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
 	}
 
 	if (last_op)
@@ -1879,7 +1862,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	if (IS_ERR(fence))
 		return fence;
 
-	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
 
@@ -2037,17 +2019,7 @@ xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs, num_syncs,
 				  vma->tile_mask, true, first_op, last_op);
 	} else {
-		struct dma_fence *fence =
-			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		int i;
-
-		/* Nothing to do, signal fences now */
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
-
-		return fence;
+		return xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}
 }
 
@@ -2844,6 +2816,25 @@ struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
+static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
+					     struct xe_vma_ops *vops,
+					     struct dma_fence *fence)
+{
+	struct xe_vma_op *op;
+	int i;
+
+	list_for_each_entry(op, &vops->list, link) {
+		if (op->base.op == DRM_GPUVA_OP_UNMAP)
+			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va), fence);
+		else if (op->base.op == DRM_GPUVA_OP_REMAP)
+			xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va),
+				       fence);
+	}
+	for (i = 0; i < vops->num_syncs; i++)
+		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
+	dma_fence_put(fence);
+}
+
 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 				     struct xe_vma_ops *vops)
 {
@@ -2868,7 +2859,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
 			xe_vm_kill(vm, false);
 			goto unlock;
 		} else {
-			dma_fence_put(fence);
+			vm_bind_ioctl_ops_install_fences(vm, vops, fence);
 		}
 	}
 
Signature · 1 line
2.34.1
#29 lore raw

RE: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences helper

Quoted 115 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences helper

Simplify VM bind code by signaling out-fences / destroying VMAs in a single location. Will help with transition single job for many bind ops.

---
 drivers/gpu/drm/xe/xe_vm.c | 55 ++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 32 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -1
index f8b27746e5a7..8c96c98cba37 100644
@@ -1658,7 +1658,7 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 	struct dma_fence *fence = NULL;
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_present);
 	int err;
drivers/gpu/drm/xe/xe_vm.c +1 -5
 	u8 id;
@@ -1716,10 +1716,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 
 	fence = cf ? &cf->base : !fence ?
 		xe_exec_queue_last_fence_get(wait_exec_queue, vm) : fence;
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL, fence);
-	}
 
 	return fence;
@@ -1743,7 +1739,7 @@ xe_vm_bind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
 	struct xe_vm *vm = xe_vma_vm(vma);
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_mask);
 	int err;
drivers/gpu/drm/xe/xe_vm.c +0 -6
 	u8 id;
@@ -1790,12 +1786,6 @@ xe_vm_bind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 		}
 	}
 
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL,
-					     cf ? &cf->base : fence);
-	}
-
 	return cf ? &cf->base : fence;
 

err_fences:

drivers/gpu/drm/xe/xe_vm.c +0 -7
@@ -1847,15 +1837,8 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 struct xe_exec_queue *q,
 		if (IS_ERR(fence))
 			return fence;
 	} else {
-		int i;
-
 		xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
-
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
 	}
 
drivers/gpu/drm/xe/xe_vm.c +1 -12
 	if (last_op)
@@ -1879,7 +1862,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	if (IS_ERR(fence))
 		return fence;
 
-	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
 
@@ -2037,17 +2019,7 @@ xe_vm_prefetch(struct xe_vm *vm, struct xe_vma
 *vma,
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
 				  vma->tile_mask, true, first_op, last_op);
 	} else {
-		struct dma_fence *fence =
-			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		int i;
-
-		/* Nothing to do, signal fences now */
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
-
-		return fence;
+		return xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}

}

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm · line 2822

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
@@ -2844,6 +2816,25 @@ struct dma_fence *xe_vm_ops_execute(struct xe_vm
 *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
+static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
+					     struct xe_vma_ops *vops,
+					     struct dma_fence *fence)

Is this a correct function name? from the codes below, you destroyed the temporary vmas during vm_ioctl, then signaled all the sync entries, then you destroyed the fence generated from the last operation.... it is more like a cleanup stage of the vm_bind.... But I don't quite understand the code, see below questions...

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
+{
+	struct xe_vma_op *op;
+	int i;
+
+	list_for_each_entry(op, &vops->list, link) {

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2834

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +8 -0
+		if (op->base.op == DRM_GPUVA_OP_UNMAP)
+			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va),
 fence);
+		else if (op->base.op == DRM_GPUVA_OP_REMAP)
+			xe_vma_destroy(gpuva_to_vma(op-
 >base.remap.unmap->va),
+				       fence);
+	}
+	for (i = 0; i < vops->num_syncs; i++)
+		xe_sync_entry_signal(vops->syncs + i, NULL, fence);

This signals the out-fence of vm_bind ioctl. Isn't this be done *after* fence is signaled (aka means the last vm bind operation is done)?

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2835

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +1 -0
+	dma_fence_put(fence);

I know this is also in the original code below... but I also don't understand why we can destroy fence here. As I understand it, this fence is generated during vm_bind operations. This is the last fence. Shouldn't we wait this fence somewhere so we know all the vm bind operations have been complete? I need your help to understand the picture here.

Oak

Quoted 17 lines
drivers/gpu/drm/xe/xe_vm.c +2 -0
+}
+

static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,

drivers/gpu/drm/xe/xe_vm.c +1 -2
 				     struct xe_vma_ops *vops)
 {
@@ -2868,7 +2859,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 			xe_vm_kill(vm, false);
 			goto unlock;
 		} else {
-			dma_fence_put(fence);
+			vm_bind_ioctl_ops_install_fences(vm, vops, fence);
 		}
 	}
 
--

2.34.1

author maintainer
#30 lore raw

Re: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences helper

Quoted 119 lines · Oak
Quoted 119 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences helper

Simplify VM bind code by signaling out-fences / destroying VMAs in a single location. Will help with transition single job for many bind ops.

---
 drivers/gpu/drm/xe/xe_vm.c | 55 ++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 32 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -1
index f8b27746e5a7..8c96c98cba37 100644
@@ -1658,7 +1658,7 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 	struct dma_fence *fence = NULL;
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_present);
 	int err;
drivers/gpu/drm/xe/xe_vm.c +1 -5
 	u8 id;
@@ -1716,10 +1716,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 
 	fence = cf ? &cf->base : !fence ?
 		xe_exec_queue_last_fence_get(wait_exec_queue, vm) : fence;
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL, fence);
-	}
 
 	return fence;
@@ -1743,7 +1739,7 @@ xe_vm_bind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
 	struct xe_vm *vm = xe_vma_vm(vma);
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_mask);
 	int err;
drivers/gpu/drm/xe/xe_vm.c +0 -6
 	u8 id;
@@ -1790,12 +1786,6 @@ xe_vm_bind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 		}
 	}
 
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL,
-					     cf ? &cf->base : fence);
-	}
-
 	return cf ? &cf->base : fence;
 

err_fences:

drivers/gpu/drm/xe/xe_vm.c +0 -7
@@ -1847,15 +1837,8 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 struct xe_exec_queue *q,
 		if (IS_ERR(fence))
 			return fence;
 	} else {
-		int i;
-
 		xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
-
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
 	}
 
drivers/gpu/drm/xe/xe_vm.c +1 -12
 	if (last_op)
@@ -1879,7 +1862,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	if (IS_ERR(fence))
 		return fence;
 
-	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
 
@@ -2037,17 +2019,7 @@ xe_vm_prefetch(struct xe_vm *vm, struct xe_vma
 *vma,
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
 				  vma->tile_mask, true, first_op, last_op);
 	} else {
-		struct dma_fence *fence =
-			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		int i;
-
-		/* Nothing to do, signal fences now */
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
-
-		return fence;
+		return xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}

}

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2844,6 +2816,25 @@ struct dma_fence *xe_vm_ops_execute(struct xe_vm
 *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
Quoted 6 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2821

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
+					     struct xe_vma_ops *vops,
+					     struct dma_fence *fence)

Is this a correct function name? from the codes below, you destroyed the temporary vmas during vm_ioctl, then signaled all the sync entries, then you destroyed the fence generated from the last operation.... it is more like a cleanup stage of the vm_bind.... But I don't quite understand the code, see below questions...

Yes, let me rename. How about vm_bind_ioctl_ops_execute_fini?

'destroyed the temporary vmas during vm_ioctl' - This is destroying unmapped VMAs when the fence signals.

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +9 -0
+{
+	struct xe_vma_op *op;
+	int i;
+
+	list_for_each_entry(op, &vops->list, link) {
+		if (op->base.op == DRM_GPUVA_OP_UNMAP)
+			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va),
 fence);
+		else if (op->base.op == DRM_GPUVA_OP_REMAP)
+			xe_vma_destroy(gpuva_to_vma(op-
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2834

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
 >base.remap.unmap->va),
+				       fence);
+	}
+	for (i = 0; i < vops->num_syncs; i++)
+		xe_sync_entry_signal(vops->syncs + i, NULL, fence);

This signals the out-fence of vm_bind ioctl. Isn't this be done *after* fence is signaled (aka means the last vm bind operation is done)?

This, xe_sync_entry_signal, is a bad name. It really should be xe_sync_entry_install_fence or something like that. It is really is installing the fence in all out-syncs, the fence signals, then the out-fence signals.

Quoted 5 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2835

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +1 -0
+	dma_fence_put(fence);

I know this is also in the original code below... but I also don't understand why we can destroy fence here. As I understand it, this fence is generated during vm_bind operations. This is the last fence. Shouldn't we wait this fence somewhere so we know all the vm bind operations have been complete? I need your help to understand the picture here.

This isn't destroying the fence - it dropping a reference. This is reference return from xe_vm_ops_execute, we install the fence anywhere needed (this might take more refs) then drop the ref from xe_vm_ops_execute.

Matt

Quoted 20 lines

Oak

Quoted 17 lines
drivers/gpu/drm/xe/xe_vm.c +2 -0
+}
+

static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,

drivers/gpu/drm/xe/xe_vm.c +1 -2
 				     struct xe_vma_ops *vops)
 {
@@ -2868,7 +2859,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 			xe_vm_kill(vm, false);
 			goto unlock;
 		} else {
-			dma_fence_put(fence);
+			vm_bind_ioctl_ops_install_fences(vm, vops, fence);
 		}
 	}
 
--

2.34.1

#31 lore raw

RE: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences helper

Quoted 140 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Monday, March 25, 2024 3:35 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences helper

Quoted 4 lines · Oak
Quoted 4 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 5 lines
Quoted 5 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 10/30] drm/xe: Add vm_bind_ioctl_ops_install_fences

helper

Quoted 58 lines
Quoted 58 lines

Simplify VM bind code by signaling out-fences / destroying VMAs in a single location. Will help with transition single job for many bind ops.

---
 drivers/gpu/drm/xe/xe_vm.c | 55 ++++++++++++++++----------------------
 1 file changed, 23 insertions(+), 32 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -1
index f8b27746e5a7..8c96c98cba37 100644
@@ -1658,7 +1658,7 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 	struct dma_fence *fence = NULL;
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_present);
 	int err;
drivers/gpu/drm/xe/xe_vm.c +1 -5
 	u8 id;
@@ -1716,10 +1716,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 
 	fence = cf ? &cf->base : !fence ?
 		xe_exec_queue_last_fence_get(wait_exec_queue, vm) : fence;
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL, fence);
-	}
 
 	return fence;
@@ -1743,7 +1739,7 @@ xe_vm_bind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 	struct dma_fence **fences = NULL;
 	struct dma_fence_array *cf = NULL;
 	struct xe_vm *vm = xe_vma_vm(vma);
-	int cur_fence = 0, i;
+	int cur_fence = 0;
 	int number_tiles = hweight8(vma->tile_mask);
 	int err;
drivers/gpu/drm/xe/xe_vm.c +0 -6
 	u8 id;
@@ -1790,12 +1786,6 @@ xe_vm_bind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 		}
 	}
 
-	if (last_op) {
-		for (i = 0; i < num_syncs; i++)
-			xe_sync_entry_signal(&syncs[i], NULL,
-					     cf ? &cf->base : fence);
-	}
-
 	return cf ? &cf->base : fence;
 
Quoted 2 lines

drivers/gpu/drm/xe/xe_vm.c xe_vm_bind(struct xe_vm *vm, struct xe_vma

Quoted 2 lines

err_fences:

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1847,15 +1837,8 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma

*vma,

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_vm.c +0 -7
 struct xe_exec_queue *q,
 		if (IS_ERR(fence))
 			return fence;
 	} else {
-		int i;
-
 		xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
-
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
 	}
 
Quoted 2 lines

drivers/gpu/drm/xe/xe_vm.c xe_vm_unbind(struct xe_vm *vm, struct xe_vma

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	if (last_op)
@@ -1879,7 +1862,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma

*vma,

Quoted 28 lines
Quoted 28 lines
drivers/gpu/drm/xe/xe_vm.c +1 -12
 	if (IS_ERR(fence))
 		return fence;
 
-	xe_vma_destroy(vma, fence);
 	if (last_op)
 		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
 
@@ -2037,17 +2019,7 @@ xe_vm_prefetch(struct xe_vm *vm, struct xe_vma
 *vma,
 		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
 				  vma->tile_mask, true, first_op, last_op);
 	} else {
-		struct dma_fence *fence =
-			xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-		int i;
-
-		/* Nothing to do, signal fences now */
-		if (last_op) {
-			for (i = 0; i < num_syncs; i++)
-				xe_sync_entry_signal(&syncs[i], NULL, fence);
-		}
-
-		return fence;
+		return xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}

}

Quoted 1 line

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2844,6 +2816,25 @@ struct dma_fence *xe_vm_ops_execute(struct

xe_vm

Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2821

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
 *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
+static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
+					     struct xe_vma_ops *vops,
+					     struct dma_fence *fence)

Is this a correct function name? from the codes below, you destroyed the

Quoted 7 lines

temporary vmas during vm_ioctl, then signaled all the sync entries, then you destroyed the fence generated from the last operation.... it is more like a cleanup stage of the vm_bind.... But I don't quite understand the code, see below questions...

Quoted 1 line

Yes, let me rename. How about vm_bind_ioctl_ops_execute_fini?

Yes that sounds better.

Quoted 3 lines

'destroyed the temporary vmas during vm_ioctl' - This is destroying unmapped VMAs when the fence signals.

I see.

Quoted 19 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_vm.c +9 -0
+{
+	struct xe_vma_op *op;
+	int i;
+
+	list_for_each_entry(op, &vops->list, link) {
+		if (op->base.op == DRM_GPUVA_OP_UNMAP)
+			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va),
 fence);
+		else if (op->base.op == DRM_GPUVA_OP_REMAP)
+			xe_vma_destroy(gpuva_to_vma(op-
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2834

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
 >base.remap.unmap->va),
+				       fence);
+	}
+	for (i = 0; i < vops->num_syncs; i++)
+		xe_sync_entry_signal(vops->syncs + i, NULL, fence);

This signals the out-fence of vm_bind ioctl. Isn't this be done *after* fence is

Quoted 8 lines

signaled (aka means the last vm bind operation is done)?

Quoted 2 lines

This, xe_sync_entry_signal, is a bad name. It really should be xe_sync_entry_install_fence or something like that. It is really is installing the fence in all out-syncs, the fence signals, then the out-fence signals.

Ah! That make sense. Now it is more clear to me.

Quoted 11 lines
Quoted 5 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2835

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
+	dma_fence_put(fence);

I know this is also in the original code below... but I also don't understand why

we can destroy fence here. As I understand it, this fence is generated during vm_bind operations. This is the last fence. Shouldn't we wait this fence somewhere so we know all the vm bind operations have been complete? I need your help to understand the picture here.

Quoted 1 line
Quoted 4 lines

This isn't destroying the fence - it dropping a reference. This is reference return from xe_vm_ops_execute, we install the fence anywhere needed (this might take more refs) then drop the ref from xe_vm_ops_execute.

Ok, that make sense.

Oak

Quoted 23 lines

Matt

Quoted 20 lines

Oak

Quoted 17 lines
drivers/gpu/drm/xe/xe_vm.c +2 -0
+}
+

static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,

drivers/gpu/drm/xe/xe_vm.c +1 -2
 				     struct xe_vma_ops *vops)
 {
@@ -2868,7 +2859,7 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 			xe_vm_kill(vm, false);
 			goto unlock;
 		} else {
-			dma_fence_put(fence);
+			vm_bind_ioctl_ops_install_fences(vm, vops, fence);
 		}
 	}
 
--

2.34.1

author maintainer
#32 lore raw

PATCH v4 11/30 drm/xe: Move setting last fence to vm_bind_ioctl_ops_install_fences

In reply to Matthew Brost

This moves setting of the last fence to a single location.

---
 drivers/gpu/drm/xe/xe_vm.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +2 -7
index 8c96c98cba37..4432531f39fe 100644
@@ -1841,9 +1841,6 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}
 
-	if (last_op)
-		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-
 	return fence;
 }
 
@@ -1853,7 +1850,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	     u32 num_syncs, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
-	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
 
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -1862,9 +1858,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	if (IS_ERR(fence))
 		return fence;
 
-	if (last_op)
-		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-
 	return fence;
 }
 
@@ -2820,6 +2813,7 @@ static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
 					     struct xe_vma_ops *vops,
 					     struct dma_fence *fence)
 {
+	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, vops->q);
 	struct xe_vma_op *op;
 	int i;
 
@@ -2832,6 +2826,7 @@ static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
 	}
 	for (i = 0; i < vops->num_syncs; i++)
 		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
+	xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
 	dma_fence_put(fence);
 }
 
Signature · 1 line
2.34.1
#33 lore raw

RE: [PATCH v4 11/30] drm/xe: Move setting last fence to vm_bind_ioctl_ops_install_fences

Quoted 63 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 11/30] drm/xe: Move setting last fence to vm_bind_ioctl_ops_install_fences

This moves setting of the last fence to a single location.

---
 drivers/gpu/drm/xe/xe_vm.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -4
index 8c96c98cba37..4432531f39fe 100644
@@ -1841,9 +1841,6 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 struct xe_exec_queue *q,
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}
 
-	if (last_op)
-		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-
 	return fence;
 }
@@ -1853,7 +1850,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	     u32 num_syncs, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
-	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
 
 	xe_vm_assert_held(vm);
drivers/gpu/drm/xe/xe_vm.c +1 -3
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -1862,9 +1858,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	if (IS_ERR(fence))
 		return fence;
 
-	if (last_op)
-		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-
 	return fence;
 }
 
@@ -2820,6 +2813,7 @@ static void vm_bind_ioctl_ops_install_fences(struct
 xe_vm *vm,
 					     struct xe_vma_ops *vops,
 					     struct dma_fence *fence)
 {
+	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 vops->q);
 	struct xe_vma_op *op;
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	int i;

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_install_fences(struct · line 2830

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
@@ -2832,6 +2826,7 @@ static void vm_bind_ioctl_ops_install_fences(struct
 xe_vm *vm,
 	}
 	for (i = 0; i < vops->num_syncs; i++)
 		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
+	xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);

Oh, With this patch, I can understand the dma-fence-put below won't destroy the last fence, because this fence is set to exec queue's last fence.

I also understand now, how the last fence will be used to signal the out-fences of vm_bind ioctl... things make sense to me now...

IMO, maybe you can squash this patch to the last patch, so people don't have the confusion as I had.

Oak

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	dma_fence_put(fence);
 }
 
--

2.34.1

author maintainer
#34 lore raw

Re: [PATCH v4 11/30] drm/xe: Move setting last fence to vm_bind_ioctl_ops_install_fences

Quoted 62 lines · Oak
Quoted 62 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 11/30] drm/xe: Move setting last fence to vm_bind_ioctl_ops_install_fences

This moves setting of the last fence to a single location.

---
 drivers/gpu/drm/xe/xe_vm.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -4
index 8c96c98cba37..4432531f39fe 100644
@@ -1841,9 +1841,6 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 struct xe_exec_queue *q,
 		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
 	}
 
-	if (last_op)
-		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-
 	return fence;
 }
@@ -1853,7 +1850,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	     u32 num_syncs, bool first_op, bool last_op)
 {
 	struct dma_fence *fence;
-	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
 
 	xe_vm_assert_held(vm);
drivers/gpu/drm/xe/xe_vm.c +1 -3
 	xe_bo_assert_held(xe_vma_bo(vma));
@@ -1862,9 +1858,6 @@ xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
 	if (IS_ERR(fence))
 		return fence;
 
-	if (last_op)
-		xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
-
 	return fence;
 }
 
@@ -2820,6 +2813,7 @@ static void vm_bind_ioctl_ops_install_fences(struct
 xe_vm *vm,
 					     struct xe_vma_ops *vops,
 					     struct dma_fence *fence)
 {
+	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 vops->q);
 	struct xe_vma_op *op;

int i;

Quoted 10 lines

drivers/gpu/drm/xe/xe_vm.c static void vm_bind_ioctl_ops_install_fences(struct · line 2830

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
@@ -2832,6 +2826,7 @@ static void vm_bind_ioctl_ops_install_fences(struct
 xe_vm *vm,
 	}
 	for (i = 0; i < vops->num_syncs; i++)
 		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
+	xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);

Oh, With this patch, I can understand the dma-fence-put below won't destroy the last fence, because this fence is set to exec queue's last fence.

Yes, xe_exec_queue_last_fence_set in this case takes a ref.

Quoted 4 lines

I also understand now, how the last fence will be used to signal the out-fences of vm_bind ioctl... things make sense to me now...

IMO, maybe you can squash this patch to the last patch, so people don't have the confusion as I had.

Will squash.

Matt

Quoted 7 lines

Oak

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 	dma_fence_put(fence);
 }
 
--

2.34.1

author maintainer
#35 lore raw

PATCH v4 12/30 drm/xe: Move ufence check to op_lock

In reply to Matthew Brost

Rather than checking for an unsignaled ufence ay unbind time, check for this during the op_lock function. This will help with the transition to job 1 per VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_vm.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +23 -10
index 4432531f39fe..5767955529dd 100644
@@ -1665,16 +1665,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
 
 	trace_xe_vma_unbind(vma);
 
-	if (vma->ufence) {
-		struct xe_user_fence * const f = vma->ufence;
-
-		if (!xe_sync_ufence_get_status(f))
-			return ERR_PTR(-EBUSY);
-
-		vma->ufence = NULL;
-		xe_sync_ufence_put(f);
-	}
-
 	if (number_tiles > 1) {
 		fences = kmalloc_array(number_tiles, sizeof(*fences),
 				       GFP_KERNEL);
@@ -2721,6 +2711,21 @@ static int vma_lock(struct drm_exec *exec, struct xe_vma *vma, bool validate)
 	return err;
 }
 
+static int check_ufence(struct xe_vma *vma)
+{
+	if (vma->ufence) {
+		struct xe_user_fence * const f = vma->ufence;
+
+		if (!xe_sync_ufence_get_status(f))
+			return -EBUSY;
+
+		vma->ufence = NULL;
+		xe_sync_ufence_put(f);
+	}
+
+	return 0;
+}
+
 static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 		   struct xe_vma_op *op)
 {
@@ -2731,6 +2736,10 @@ static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 		err = vma_lock(exec, op->map.vma, !xe_vm_in_fault_mode(vm));
 		break;
 	case DRM_GPUVA_OP_REMAP:
+		err = check_ufence(gpuva_to_vma(op->base.remap.unmap->va));
+		if (err)
+			break;
+
 		err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap->va),
 			       false);
 		if (!err && op->remap.prev)
@@ -2739,6 +2748,10 @@ static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 			err = vma_lock(exec, op->remap.next, true);
 		break;
 	case DRM_GPUVA_OP_UNMAP:
+		err = check_ufence(gpuva_to_vma(op->base.unmap.va));
+		if (err)
+			break;
+
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;
 	case DRM_GPUVA_OP_PREFETCH:
Signature · 1 line
2.34.1
#36 lore raw

RE: [PATCH v4 12/30] drm/xe: Move ufence check to op_lock

Patch makes sense to me. See one comment inline

Quoted 63 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 12/30] drm/xe: Move ufence check to op_lock

Rather than checking for an unsignaled ufence ay unbind time, check for this during the op_lock function. This will help with the transition to job 1 per VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_vm.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -10
index 4432531f39fe..5767955529dd 100644
@@ -1665,16 +1665,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 
 	trace_xe_vma_unbind(vma);
 
-	if (vma->ufence) {
-		struct xe_user_fence * const f = vma->ufence;
-
-		if (!xe_sync_ufence_get_status(f))
-			return ERR_PTR(-EBUSY);
-
-		vma->ufence = NULL;
-		xe_sync_ufence_put(f);
-	}
-
 	if (number_tiles > 1) {
 		fences = kmalloc_array(number_tiles, sizeof(*fences),
drivers/gpu/drm/xe/xe_vm.c +15 -0
 				       GFP_KERNEL);
@@ -2721,6 +2711,21 @@ static int vma_lock(struct drm_exec *exec, struct
 xe_vma *vma, bool validate)
 	return err;
 }
 
+static int check_ufence(struct xe_vma *vma)
+{
+	if (vma->ufence) {
+		struct xe_user_fence * const f = vma->ufence;
+
+		if (!xe_sync_ufence_get_status(f))
+			return -EBUSY;
+
+		vma->ufence = NULL;
+		xe_sync_ufence_put(f);
+	}
+
+	return 0;
+}
+
 static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 		   struct xe_vma_op *op)

{

drivers/gpu/drm/xe/xe_vm.c static int op_lock(struct drm_exec *exec, struct xe_vm · line 2742

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
@@ -2731,6 +2736,10 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 		err = vma_lock(exec, op-
 >map.vma, !xe_vm_in_fault_mode(vm));
 		break;
 	case DRM_GPUVA_OP_REMAP:
+		err = check_ufence(gpuva_to_vma(op->base.remap.unmap-
 >va));

This is another reason we need to rename op_lock...

Oak

Quoted 21 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+		if (err)
+			break;
+

err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap-

Quoted 1 line

va),

drivers/gpu/drm/xe/xe_vm.c +4 -0
 			       false);
 		if (!err && op->remap.prev)
@@ -2739,6 +2748,10 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 			err = vma_lock(exec, op->remap.next, true);
 		break;
 	case DRM_GPUVA_OP_UNMAP:
+		err = check_ufence(gpuva_to_vma(op->base.unmap.va));
+		if (err)
+			break;
+
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;

case DRM_GPUVA_OP_PREFETCH:
--
2.34.1

author maintainer
#37 lore raw

Re: [PATCH v4 12/30] drm/xe: Move ufence check to op_lock

Quoted 67 lines · Oak

Patch makes sense to me. See one comment inline

Quoted 65 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 12/30] drm/xe: Move ufence check to op_lock

Rather than checking for an unsignaled ufence ay unbind time, check for this during the op_lock function. This will help with the transition to job 1 per VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_vm.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -10
index 4432531f39fe..5767955529dd 100644
@@ -1665,16 +1665,6 @@ xe_vm_unbind_vma(struct xe_vma *vma, struct
 xe_exec_queue *q,
 
 	trace_xe_vma_unbind(vma);
 
-	if (vma->ufence) {
-		struct xe_user_fence * const f = vma->ufence;
-
-		if (!xe_sync_ufence_get_status(f))
-			return ERR_PTR(-EBUSY);
-
-		vma->ufence = NULL;
-		xe_sync_ufence_put(f);
-	}
-
 	if (number_tiles > 1) {
 		fences = kmalloc_array(number_tiles, sizeof(*fences),
drivers/gpu/drm/xe/xe_vm.c +15 -0
 				       GFP_KERNEL);
@@ -2721,6 +2711,21 @@ static int vma_lock(struct drm_exec *exec, struct
 xe_vma *vma, bool validate)
 	return err;
 }
 
+static int check_ufence(struct xe_vma *vma)
+{
+	if (vma->ufence) {
+		struct xe_user_fence * const f = vma->ufence;
+
+		if (!xe_sync_ufence_get_status(f))
+			return -EBUSY;
+
+		vma->ufence = NULL;
+		xe_sync_ufence_put(f);
+	}
+
+	return 0;
+}
+
 static int op_lock(struct drm_exec *exec, struct xe_vm *vm,
 		   struct xe_vma_op *op)

{

drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -2731,6 +2736,10 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 		err = vma_lock(exec, op-
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c static int op_lock(struct drm_exec *exec, struct xe_vm · line 2742

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
 >map.vma, !xe_vm_in_fault_mode(vm));
 		break;
 	case DRM_GPUVA_OP_REMAP:
+		err = check_ufence(gpuva_to_vma(op->base.remap.unmap-
 >va));

This is another reason we need to rename op_lock...

Agree.

Matt

Quoted 24 lines

Oak

Quoted 21 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+		if (err)
+			break;
+

err = vma_lock(exec, gpuva_to_vma(op->base.remap.unmap-

Quoted 1 line

va),

drivers/gpu/drm/xe/xe_vm.c +4 -0
 			       false);
 		if (!err && op->remap.prev)
@@ -2739,6 +2748,10 @@ static int op_lock(struct drm_exec *exec, struct xe_vm
 *vm,
 			err = vma_lock(exec, op->remap.next, true);
 		break;
 	case DRM_GPUVA_OP_UNMAP:
+		err = check_ufence(gpuva_to_vma(op->base.unmap.va));
+		if (err)
+			break;
+
 		err = vma_lock(exec, gpuva_to_vma(op->base.unmap.va), false);
 		break;

case DRM_GPUVA_OP_PREFETCH:
--
2.34.1

author maintainer
#38 lore raw

PATCH v4 13/30 drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

In reply to Matthew Brost

Rather than adding a ufence to a VMA in the bind function, add the ufence to all VMAs in the IOCTL that require binds in vm_bind_ioctl_ops_install_fences. This will help with the transition to job 1 per VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_sync.c | 15 ++++++++++++
 drivers/gpu/drm/xe/xe_sync.h |  1 +
 drivers/gpu/drm/xe/xe_vm.c   | 44 ++++++++++++++++++++++++++++++------
 3 files changed, 53 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_sync.c +15 -0
index 02c9577fe418..07aa65d9bcab 100644
@@ -343,6 +343,21 @@ xe_sync_in_fence_get(struct xe_sync_entry *sync, int num_sync,
 	return ERR_PTR(-ENOMEM);
 }
 
+/**
+ * __xe_sync_ufence_get() - Get user fence from user fence
+ * @ufence: input user fence
+ *
+ * Get a user fence reference from user fence
+ *
+ * Return: xe_user_fence pointer with reference
+ */
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence)
+{
+	user_fence_get(ufence);
+
+	return ufence;
+}
+
 /**
  * xe_sync_ufence_get() - Get user fence from sync
  * @sync: input sync
drivers/gpu/drm/xe/xe_sync.h +1 -0
index 0fd0d51208e6..26e9ec9de1a8 100644
@@ -38,6 +38,7 @@ static inline bool xe_sync_is_ufence(struct xe_sync_entry *sync)
 	return !!sync->ufence;
 }
 
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence);
 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync);
 void xe_sync_ufence_put(struct xe_user_fence *ufence);
 int xe_sync_ufence_get_status(struct xe_user_fence *ufence);
drivers/gpu/drm/xe/xe_vm.c +37 -7

75 lines not on this page · raw message

Signature · 1 line
2.34.1
#39 lore raw

RE: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

This patch makes sense to me. See two nit-pick inline

Quoted 34 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

Rather than adding a ufence to a VMA in the bind function, add the ufence to all VMAs in the IOCTL that require binds in vm_bind_ioctl_ops_install_fences. This will help with the transition to job 1 per VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_sync.c | 15 ++++++++++++
 drivers/gpu/drm/xe/xe_sync.h |  1 +
 drivers/gpu/drm/xe/xe_vm.c   | 44 ++++++++++++++++++++++++++++++------
 3 files changed, 53 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_sync.c +4 -0
index 02c9577fe418..07aa65d9bcab 100644
@@ -343,6 +343,21 @@ xe_sync_in_fence_get(struct xe_sync_entry *sync, int
 num_sync,
 	return ERR_PTR(-ENOMEM);
 }
 
+/**
+ * __xe_sync_ufence_get() - Get user fence from user fence
+ * @ufence: input user fence
+ *

drivers/gpu/drm/xe/xe_sync.c xe_sync_in_fence_get(struct xe_sync_entry *sync, int · line 360

Quoted 10 lines
drivers/gpu/drm/xe/xe_sync.c +10 -0
+ * Get a user fence reference from user fence
+ *
+ * Return: xe_user_fence pointer with reference
+ */
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence)
+{
+	user_fence_get(ufence);
+
+	return ufence;
+}

I wonder why this is made part of xe_sync. Isn't just a ufence get function? Can we drop _sync_ from the function name?

Quoted 42 lines
drivers/gpu/drm/xe/xe_sync.c +1 -0
+
 /**
  * xe_sync_ufence_get() - Get user fence from sync

* @sync: input sync

drivers/gpu/drm/xe/xe_sync.h +1 -0
index 0fd0d51208e6..26e9ec9de1a8 100644
@@ -38,6 +38,7 @@ static inline bool xe_sync_is_ufence(struct xe_sync_entry
 *sync)
 	return !!sync->ufence;
 }
 
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence);
 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync);
 void xe_sync_ufence_put(struct xe_user_fence *ufence);

int xe_sync_ufence_get_status(struct xe_user_fence *ufence);

drivers/gpu/drm/xe/xe_vm.c +0 -7
index 5767955529dd..5b93c71fc5e9 100644
@@ -1810,17 +1810,10 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 struct xe_exec_queue *q,
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
-	ufence = find_ufence_get(syncs, num_syncs);
-	if (vma->ufence && ufence)
-		xe_sync_ufence_put(vma->ufence);
-
-	vma->ufence = ufence ?: vma->ufence;
-
 	if (immediate) {
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, tile_mask,
 				       first_op, last_op);

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct · line 2823

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
@@ -2822,21 +2815,58 @@ struct dma_fence *xe_vm_ops_execute(struct
 xe_vm *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
+static void vma_add_ufence(struct xe_vma *vma, struct xe_user_fence
 *ufence)
+{
+	if (vma->ufence)
+		xe_sync_ufence_put(vma->ufence);

Not sure where/when we introduced xe_sync_ufence_put, for me this can be renamed to xe_ufence_put

Oak

Quoted 57 lines
drivers/gpu/drm/xe/xe_vm.c +31 -0
+	vma->ufence = __xe_sync_ufence_get(ufence);
+}
+
+static void op_add_ufence(struct xe_vm *vm, struct xe_vma_op *op,
+			  struct xe_user_fence *ufence)
+{
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		vma_add_ufence(op->map.vma, ufence);
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		if (op->remap.prev)
+			vma_add_ufence(op->remap.prev, ufence);
+		if (op->remap.next)
+			vma_add_ufence(op->remap.next, ufence);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		vma_add_ufence(gpuva_to_vma(op->base.prefetch.va),
 ufence);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+}
+
 static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
 					     struct xe_vma_ops *vops,
 					     struct dma_fence *fence)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 vops->q);
+	struct xe_user_fence *ufence;
 	struct xe_vma_op *op;
 	int i;
 
+	ufence = find_ufence_get(vops->syncs, vops->num_syncs);
 	list_for_each_entry(op, &vops->list, link) {
+		if (ufence)
+			op_add_ufence(vm, op, ufence);
+
 		if (op->base.op == DRM_GPUVA_OP_UNMAP)
 			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va),
 fence);
 		else if (op->base.op == DRM_GPUVA_OP_REMAP)
 			xe_vma_destroy(gpuva_to_vma(op-
Quoted 1 line

base.remap.unmap->va),

drivers/gpu/drm/xe/xe_vm.c +2 -1
 				       fence);
 	}
+	if (ufence)
+		xe_sync_ufence_put(ufence);
 	for (i = 0; i < vops->num_syncs; i++)
 		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
 	xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
--

2.34.1

author maintainer
#40 lore raw

Re: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

Quoted 32 lines · Oak

This patch makes sense to me. See two nit-pick inline

Quoted 30 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

Rather than adding a ufence to a VMA in the bind function, add the ufence to all VMAs in the IOCTL that require binds in vm_bind_ioctl_ops_install_fences. This will help with the transition to job 1 per VM bind IOCTL.

---
 drivers/gpu/drm/xe/xe_sync.c | 15 ++++++++++++
 drivers/gpu/drm/xe/xe_sync.h |  1 +
 drivers/gpu/drm/xe/xe_vm.c   | 44 ++++++++++++++++++++++++++++++------
 3 files changed, 53 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_sync.c +0 -0
index 02c9577fe418..07aa65d9bcab 100644
@@ -343,6 +343,21 @@ xe_sync_in_fence_get(struct xe_sync_entry *sync, int
 num_sync,
 	return ERR_PTR(-ENOMEM);
 }
 
Quoted 18 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_sync.c +4 -0
+/**
+ * __xe_sync_ufence_get() - Get user fence from user fence
+ * @ufence: input user fence
+ *

drivers/gpu/drm/xe/xe_sync.c xe_sync_in_fence_get(struct xe_sync_entry *sync, int num_sync, · line 359

Quoted 10 lines
drivers/gpu/drm/xe/xe_sync.c +10 -0
+ * Get a user fence reference from user fence
+ *
+ * Return: xe_user_fence pointer with reference
+ */
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence)
+{
+	user_fence_get(ufence);
+
+	return ufence;
+}

I wonder why this is made part of xe_sync. Isn't just a ufence get function? Can we drop _sync_ from the function name?

Typically exported functions should have a prefix matching the header file name.

e.g.

xe_sync.h -> all functions should start with xe_sync_*

In this case struct xe_user_fence is private date member to xe_sync.c (only define in that C file) and just an opaque pointer to the rest of the driver.

Quoted 46 lines
Quoted 46 lines
drivers/gpu/drm/xe/xe_sync.c +1 -0
+
 /**
  * xe_sync_ufence_get() - Get user fence from sync

* @sync: input sync

drivers/gpu/drm/xe/xe_sync.h +1 -0
index 0fd0d51208e6..26e9ec9de1a8 100644
@@ -38,6 +38,7 @@ static inline bool xe_sync_is_ufence(struct xe_sync_entry
 *sync)
 	return !!sync->ufence;
 }
 
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence *ufence);
 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync);
 void xe_sync_ufence_put(struct xe_user_fence *ufence);

int xe_sync_ufence_get_status(struct xe_user_fence *ufence);

drivers/gpu/drm/xe/xe_vm.c +0 -7
index 5767955529dd..5b93c71fc5e9 100644
@@ -1810,17 +1810,10 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
 struct xe_exec_queue *q,
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
-	ufence = find_ufence_get(syncs, num_syncs);
-	if (vma->ufence && ufence)
-		xe_sync_ufence_put(vma->ufence);
-
-	vma->ufence = ufence ?: vma->ufence;
-
 	if (immediate) {
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, tile_mask,
 				       first_op, last_op);
@@ -2822,21 +2815,58 @@ struct dma_fence *xe_vm_ops_execute(struct
 xe_vm *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2821

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
+static void vma_add_ufence(struct xe_vma *vma, struct xe_user_fence
 *ufence)
+{
+	if (vma->ufence)
+		xe_sync_ufence_put(vma->ufence);

Not sure where/when we introduced xe_sync_ufence_put, for me this can be renamed to xe_ufence_put

See above, I think the naming is correct. All of this is a matter of opinion, we don't have any offical style guidelines for Xe but we might want to think about writing some up / fixing Xe to conform while the driver is still relatively small.

Matt

Quoted 60 lines

Oak

Quoted 57 lines
drivers/gpu/drm/xe/xe_vm.c +31 -0
+	vma->ufence = __xe_sync_ufence_get(ufence);
+}
+
+static void op_add_ufence(struct xe_vm *vm, struct xe_vma_op *op,
+			  struct xe_user_fence *ufence)
+{
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		vma_add_ufence(op->map.vma, ufence);
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		if (op->remap.prev)
+			vma_add_ufence(op->remap.prev, ufence);
+		if (op->remap.next)
+			vma_add_ufence(op->remap.next, ufence);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		vma_add_ufence(gpuva_to_vma(op->base.prefetch.va),
 ufence);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+}
+
 static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
 					     struct xe_vma_ops *vops,
 					     struct dma_fence *fence)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 vops->q);
+	struct xe_user_fence *ufence;
 	struct xe_vma_op *op;
 	int i;
 
+	ufence = find_ufence_get(vops->syncs, vops->num_syncs);
 	list_for_each_entry(op, &vops->list, link) {
+		if (ufence)
+			op_add_ufence(vm, op, ufence);
+
 		if (op->base.op == DRM_GPUVA_OP_UNMAP)
 			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va),
 fence);
 		else if (op->base.op == DRM_GPUVA_OP_REMAP)
 			xe_vma_destroy(gpuva_to_vma(op-
Quoted 1 line

base.remap.unmap->va),

drivers/gpu/drm/xe/xe_vm.c +2 -1
 				       fence);
 	}
+	if (ufence)
+		xe_sync_ufence_put(ufence);
 	for (i = 0; i < vops->num_syncs; i++)
 		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
 	xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
--

2.34.1

#41 lore raw

RE: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

Quoted 70 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Tuesday, March 26, 2024 2:54 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

Quoted 4 lines · Oak

This patch makes sense to me. See two nit-pick inline

Quoted 2 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 12 lines
Quoted 12 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 13/30] drm/xe: Move ufence add to vm_bind_ioctl_ops_install_fences

Rather than adding a ufence to a VMA in the bind function, add the ufence to all VMAs in the IOCTL that require binds in vm_bind_ioctl_ops_install_fences. This will help with the transition to job 1 per VM bind IOCTL.

Quoted 5 lines
Quoted 5 lines
---
 drivers/gpu/drm/xe/xe_sync.c | 15 ++++++++++++
 drivers/gpu/drm/xe/xe_sync.h |  1 +
 drivers/gpu/drm/xe/xe_vm.c   | 44 ++++++++++++++++++++++++++++++--

----

Quoted 7 lines

drivers/gpu/drm/xe/xe_sync.c xe_sync_in_fence_get(struct xe_sync_entry *sync,

Quoted 7 lines
 3 files changed, 53 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_sync.c +0 -0
index 02c9577fe418..07aa65d9bcab 100644
@@ -343,6 +343,21 @@ xe_sync_in_fence_get(struct xe_sync_entry *sync,

int

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_sync.c +0 -0
 num_sync,
 	return ERR_PTR(-ENOMEM);
 }
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_sync.c xe_sync_in_fence_get(struct xe_sync_entry *sync, int num_sync,

Quoted 9 lines
drivers/gpu/drm/xe/xe_sync.c +9 -0
+/**
+ * __xe_sync_ufence_get() - Get user fence from user fence
+ * @ufence: input user fence
+ *
+ * Get a user fence reference from user fence
+ *
+ * Return: xe_user_fence pointer with reference
+ */
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence

*ufence)

Quoted 7 lines

drivers/gpu/drm/xe/xe_sync.c xe_sync_in_fence_get(struct xe_sync_entry *sync, int num_sync, · line 359

Quoted 5 lines
drivers/gpu/drm/xe/xe_sync.c +5 -0
+{
+	user_fence_get(ufence);
+
+	return ufence;
+}

I wonder why this is made part of xe_sync. Isn't just a ufence get function? Can

we drop _sync_ from the function name?

Quoted 2 lines

Typically exported functions should have a prefix matching the header file name.

Quoted 7 lines

e.g.

xe_sync.h -> all functions should start with xe_sync_*

In this case struct xe_user_fence is private date member to xe_sync.c (only define in that C file) and just an opaque pointer to the rest of the driver.

That makes sense. Thanks for explaining.

Oak

Quoted 127 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_sync.h static inline bool xe_sync_is_ufence(struct

Quoted 10 lines
drivers/gpu/drm/xe/xe_sync.c +1 -0
+
 /**
  * xe_sync_ufence_get() - Get user fence from sync

* @sync: input sync

drivers/gpu/drm/xe/xe_sync.h +0 -0
index 0fd0d51208e6..26e9ec9de1a8 100644
@@ -38,6 +38,7 @@ static inline bool xe_sync_is_ufence(struct

xe_sync_entry

Quoted 5 lines

drivers/gpu/drm/xe/xe_sync.h static inline bool xe_sync_is_ufence(struct xe_sync_entry *sync)

Quoted 5 lines
drivers/gpu/drm/xe/xe_sync.h +1 -0
 *sync)
 	return !!sync->ufence;
 }
 
+struct xe_user_fence *__xe_sync_ufence_get(struct xe_user_fence

*ufence);

Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c xe_vm_bind(struct xe_vm *vm, struct xe_vma

Quoted 8 lines
drivers/gpu/drm/xe/xe_sync.h +0 -0
 struct xe_user_fence *xe_sync_ufence_get(struct xe_sync_entry *sync);
 void xe_sync_ufence_put(struct xe_user_fence *ufence);

int xe_sync_ufence_get_status(struct xe_user_fence *ufence);

drivers/gpu/drm/xe/xe_vm.c +0 -0
index 5767955529dd..5b93c71fc5e9 100644
@@ -1810,17 +1810,10 @@ xe_vm_bind(struct xe_vm *vm, struct xe_vma

*vma,

Quoted 24 lines
Quoted 24 lines
drivers/gpu/drm/xe/xe_vm.c +0 -7
 struct xe_exec_queue *q,
 {
 	struct dma_fence *fence;
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	struct xe_user_fence *ufence;
 
 	xe_vm_assert_held(vm);
 	xe_bo_assert_held(bo);
 
-	ufence = find_ufence_get(syncs, num_syncs);
-	if (vma->ufence && ufence)
-		xe_sync_ufence_put(vma->ufence);
-
-	vma->ufence = ufence ?: vma->ufence;
-
 	if (immediate) {
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, tile_mask,
 				       first_op, last_op);
@@ -2822,21 +2815,58 @@ struct dma_fence *xe_vm_ops_execute(struct
 xe_vm *vm, struct xe_vma_ops *vops)
 	return fence;
 }
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_vm.c struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops) · line 2821

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +4 -0
+static void vma_add_ufence(struct xe_vma *vma, struct xe_user_fence
 *ufence)
+{
+	if (vma->ufence)
+		xe_sync_ufence_put(vma->ufence);

Not sure where/when we introduced xe_sync_ufence_put, for me this can be

renamed to xe_ufence_put

Quoted 1 line

See above, I think the naming is correct. All of this is a matter of opinion, we don't have any offical style guidelines for Xe but we might want to think about writing some up / fixing Xe to conform while the driver is still relatively small.

Matt

Quoted 60 lines

Oak

Quoted 57 lines
drivers/gpu/drm/xe/xe_vm.c +31 -0
+	vma->ufence = __xe_sync_ufence_get(ufence);
+}
+
+static void op_add_ufence(struct xe_vm *vm, struct xe_vma_op *op,
+			  struct xe_user_fence *ufence)
+{
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		vma_add_ufence(op->map.vma, ufence);
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		if (op->remap.prev)
+			vma_add_ufence(op->remap.prev, ufence);
+		if (op->remap.next)
+			vma_add_ufence(op->remap.next, ufence);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		vma_add_ufence(gpuva_to_vma(op->base.prefetch.va),
 ufence);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+}
+
 static void vm_bind_ioctl_ops_install_fences(struct xe_vm *vm,
 					     struct xe_vma_ops *vops,
 					     struct dma_fence *fence)
 {
 	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 vops->q);
+	struct xe_user_fence *ufence;
 	struct xe_vma_op *op;
 	int i;
 
+	ufence = find_ufence_get(vops->syncs, vops->num_syncs);
 	list_for_each_entry(op, &vops->list, link) {
+		if (ufence)
+			op_add_ufence(vm, op, ufence);
+
 		if (op->base.op == DRM_GPUVA_OP_UNMAP)
 			xe_vma_destroy(gpuva_to_vma(op->base.unmap.va),
 fence);
 		else if (op->base.op == DRM_GPUVA_OP_REMAP)
 			xe_vma_destroy(gpuva_to_vma(op-
Quoted 1 line

base.remap.unmap->va),

drivers/gpu/drm/xe/xe_vm.c +2 -1
 				       fence);
 	}
+	if (ufence)
+		xe_sync_ufence_put(ufence);
 	for (i = 0; i < vops->num_syncs; i++)
 		xe_sync_entry_signal(vops->syncs + i, NULL, fence);
 	xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
--

2.34.1

author maintainer
#42 lore raw

PATCH v4 14/30 drm/xe: Add xe_gt_tlb_invalidation_range and convert PT layer to use this

In reply to Matthew Brost

xe_gt_tlb_invalidation_range accepts a start and end address rather than a VMA. This will enable multiple VMAs to be invalidated in a single invalidation. Update the PT layer to use this new function.

---
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c | 59 +++++++++++++++------
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h |  3 ++
 drivers/gpu/drm/xe/xe_pt.c                  | 25 ++++++---
 3 files changed, 65 insertions(+), 22 deletions(-)
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c +44 -15
index a3c4ffba679d..ac2bf86de39a 100644
@@ -264,11 +264,15 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
 }
 
 /**
- * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
+ * xe_gt_tlb_invalidation_range - Issue a TLB invalidation on this GT for an
+ * address range
+ *
  * @gt: graphics tile
  * @fence: invalidation fence which will be signal on TLB invalidation
  * completion, can be NULL
- * @vma: VMA to invalidate
+ * @start: start address
+ * @end: end address
+ * @asid: address space id
  *
  * Issue a range based TLB invalidation if supported, if not fallback to a full
  * TLB invalidation. Completion of TLB is asynchronous and caller can either use
@@ -278,17 +282,15 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
  * Return: Seqno which can be passed to xe_gt_tlb_invalidation_wait on success,
  * negative error code on error.
  */
-int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
-			       struct xe_gt_tlb_invalidation_fence *fence,
-			       struct xe_vma *vma)
+int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
+				 struct xe_gt_tlb_invalidation_fence *fence,
+				 u64 start, u64 end, u32 asid)
 {
 	struct xe_device *xe = gt_to_xe(gt);
 #define MAX_TLB_INVALIDATION_LEN	7
 	u32 action[MAX_TLB_INVALIDATION_LEN];
 	int len = 0;
 
-	xe_gt_assert(gt, vma);
-
 	/* Execlists not supported */
 	if (gt_to_xe(gt)->info.force_execlist) {
 		if (fence)
@@ -302,8 +304,8 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	if (!xe->info.has_range_tlb_invalidation) {
 		action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_FULL);
 	} else {
-		u64 start = xe_vma_start(vma);
-		u64 length = xe_vma_size(vma);
+		u64 orig_start = start;
+		u64 length = end - start;
 		u64 align, end;
 
 		if (length < SZ_4K)
@@ -316,12 +318,12 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 * address mask covering the required range.
 		 */
 		align = roundup_pow_of_two(length);
-		start = ALIGN_DOWN(xe_vma_start(vma), align);
-		end = ALIGN(xe_vma_end(vma), align);
+		start = ALIGN_DOWN(start, align);
+		end = ALIGN(end, align);
 		length = align;
 		while (start + length < end) {
 			length <<= 1;
-			start = ALIGN_DOWN(xe_vma_start(vma), length);
+			start = ALIGN_DOWN(orig_start, length);
 		}
 
 		/*
@@ -330,16 +332,17 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 */
 		if (length >= SZ_2M) {
 			length = max_t(u64, SZ_16M, length);
-			start = ALIGN_DOWN(xe_vma_start(vma), length);
+			start = ALIGN_DOWN(orig_start, length);
 		}
 
 		xe_gt_assert(gt, length >= SZ_4K);
 		xe_gt_assert(gt, is_power_of_2(length));
-		xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1, ilog2(SZ_2M) + 1)));
+		xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1,
+						    ilog2(SZ_2M) + 1)));
 		xe_gt_assert(gt, IS_ALIGNED(start, length));
 
 		action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_PAGE_SELECTIVE);
-		action[len++] = xe_vma_vm(vma)->usm.asid;
+		action[len++] = asid;
 		action[len++] = lower_32_bits(start);
 		action[len++] = upper_32_bits(start);
 		action[len++] = ilog2(length) - ilog2(SZ_4K);
@@ -350,6 +353,32 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	return send_tlb_invalidation(&gt->uc.guc, fence, action, len);
 }
 
+/**
+ * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
+ * @gt: graphics tile
+ * @fence: invalidation fence which will be signal on TLB invalidation
+ * completion, can be NULL
+ * @vma: VMA to invalidate
+ *
+ * Issue a range based TLB invalidation if supported, if not fallback to a full
+ * TLB invalidation. Completion of TLB is asynchronous and caller can either use
+ * the invalidation fence or seqno + xe_gt_tlb_invalidation_wait to wait for
+ * completion.
+ *
+ * Return: Seqno which can be passed to xe_gt_tlb_invalidation_wait on success,
+ * negative error code on error.
+ */
+int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
+			       struct xe_gt_tlb_invalidation_fence *fence,
+			       struct xe_vma *vma)
+{
+	xe_gt_assert(gt, vma);
+
+	return xe_gt_tlb_invalidation_range(gt, fence, xe_vma_start(vma),
+					    xe_vma_end(vma),
+					    xe_vma_vm(vma)->usm.asid);
+}
+
 /**
  * xe_gt_tlb_invalidation_wait - Wait for TLB to complete
  * @gt: graphics tile
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h +3 -0
index fbb743d80d2c..bf3bebd9f985 100644
@@ -20,6 +20,9 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt);
 int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 			       struct xe_gt_tlb_invalidation_fence *fence,
 			       struct xe_vma *vma);
+int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
+				 struct xe_gt_tlb_invalidation_fence *fence,
+				 u64 start, u64 end, u32 asid);
 int xe_gt_tlb_invalidation_wait(struct xe_gt *gt, int seqno);
 int xe_guc_tlb_invalidation_done_handler(struct xe_guc *guc, u32 *msg, u32 len);
 
drivers/gpu/drm/xe/xe_pt.c +18 -7
index 7f54bc3e389d..110d6917089b 100644
@@ -1074,10 +1074,12 @@ static const struct xe_migrate_pt_update_ops userptr_bind_ops = {
 struct invalidation_fence {
 	struct xe_gt_tlb_invalidation_fence base;
 	struct xe_gt *gt;
-	struct xe_vma *vma;
 	struct dma_fence *fence;
 	struct dma_fence_cb cb;
 	struct work_struct work;
+	u64 start;
+	u64 end;
+	u32 asid;
 };
 
 static const char *
@@ -1120,13 +1122,14 @@ static void invalidation_fence_work_func(struct work_struct *w)
 		container_of(w, struct invalidation_fence, work);
 
 	trace_xe_gt_tlb_invalidation_fence_work_func(&ifence->base);
-	xe_gt_tlb_invalidation_vma(ifence->gt, &ifence->base, ifence->vma);
+	xe_gt_tlb_invalidation_range(ifence->gt, &ifence->base, ifence->start,
+				     ifence->end, ifence->asid);
 }
 
 static int invalidation_fence_init(struct xe_gt *gt,
 				   struct invalidation_fence *ifence,
 				   struct dma_fence *fence,
-				   struct xe_vma *vma)
+				   u64 start, u64 end, u32 asid)
 {
 	int ret;
 
@@ -1144,7 +1147,9 @@ static int invalidation_fence_init(struct xe_gt *gt,
 	dma_fence_get(&ifence->base.base);	/* Ref for caller */
 	ifence->fence = fence;
 	ifence->gt = gt;
-	ifence->vma = vma;
+	ifence->start = start;
+	ifence->end = end;
+	ifence->asid = asid;
 
 	INIT_WORK(&ifence->work, invalidation_fence_work_func);
 	ret = dma_fence_add_callback(fence, &ifence->cb, invalidation_fence_cb);
@@ -1286,8 +1291,11 @@ __xe_pt_bind_vma(struct xe_tile *tile, struct xe_vma *vma, struct xe_exec_queue
 
 		/* TLB invalidation must be done before signaling rebind */
 		if (ifence) {
-			int err = invalidation_fence_init(tile->primary_gt, ifence, fence,
-							  vma);
+			int err = invalidation_fence_init(tile->primary_gt,
+							  ifence, fence,
+							  xe_vma_start(vma),
+							  xe_vma_end(vma),
+							  xe_vma_vm(vma)->usm.asid);
 			if (err) {
 				dma_fence_put(fence);
 				kfree(ifence);
@@ -1625,7 +1633,10 @@ __xe_pt_unbind_vma(struct xe_tile *tile, struct xe_vma *vma, struct xe_exec_queu
 			dma_fence_wait(fence, false);
 
 		/* TLB invalidation must be done before signaling unbind */
-		err = invalidation_fence_init(tile->primary_gt, ifence, fence, vma);
+		err = invalidation_fence_init(tile->primary_gt, ifence, fence,
+					      xe_vma_start(vma),
+					      xe_vma_end(vma),
+					      xe_vma_vm(vma)->usm.asid);
 		if (err) {
 			dma_fence_put(fence);
 			kfree(ifence);
Signature · 1 line
2.34.1
#43 lore raw

RE: [PATCH v4 14/30] drm/xe: Add xe_gt_tlb_invalidation_range and convert PT layer to use this

Hi Matt,

Patch looks good to me. See one question inline

Quoted 169 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 14/30] drm/xe: Add xe_gt_tlb_invalidation_range and convert PT layer to use this

xe_gt_tlb_invalidation_range accepts a start and end address rather than a VMA. This will enable multiple VMAs to be invalidated in a single invalidation. Update the PT layer to use this new function.

---
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c | 59 +++++++++++++++------
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h |  3 ++
 drivers/gpu/drm/xe/xe_pt.c                  | 25 ++++++---
 3 files changed, 65 insertions(+), 22 deletions(-)
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c +18 -15
index a3c4ffba679d..ac2bf86de39a 100644
@@ -264,11 +264,15 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
 }
 
 /**
- * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
+ * xe_gt_tlb_invalidation_range - Issue a TLB invalidation on this GT for an
+ * address range
+ *
  * @gt: graphics tile
  * @fence: invalidation fence which will be signal on TLB invalidation
  * completion, can be NULL
- * @vma: VMA to invalidate
+ * @start: start address
+ * @end: end address
+ * @asid: address space id
  *
  * Issue a range based TLB invalidation if supported, if not fallback to a full
  * TLB invalidation. Completion of TLB is asynchronous and caller can either use
@@ -278,17 +282,15 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
  * Return: Seqno which can be passed to xe_gt_tlb_invalidation_wait on success,
  * negative error code on error.
  */
-int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
-			       struct xe_gt_tlb_invalidation_fence *fence,
-			       struct xe_vma *vma)
+int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
+				 struct xe_gt_tlb_invalidation_fence *fence,
+				 u64 start, u64 end, u32 asid)
 {
 	struct xe_device *xe = gt_to_xe(gt);
 #define MAX_TLB_INVALIDATION_LEN	7
 	u32 action[MAX_TLB_INVALIDATION_LEN];
 	int len = 0;
 
-	xe_gt_assert(gt, vma);
-
 	/* Execlists not supported */
 	if (gt_to_xe(gt)->info.force_execlist) {
 		if (fence)
@@ -302,8 +304,8 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	if (!xe->info.has_range_tlb_invalidation) {
 		action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_FULL);
 	} else {
-		u64 start = xe_vma_start(vma);
-		u64 length = xe_vma_size(vma);
+		u64 orig_start = start;
+		u64 length = end - start;
 		u64 align, end;
 
 		if (length < SZ_4K)
@@ -316,12 +318,12 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 * address mask covering the required range.
 		 */
 		align = roundup_pow_of_two(length);
-		start = ALIGN_DOWN(xe_vma_start(vma), align);
-		end = ALIGN(xe_vma_end(vma), align);
+		start = ALIGN_DOWN(start, align);
+		end = ALIGN(end, align);
 		length = align;
 		while (start + length < end) {
 			length <<= 1;
-			start = ALIGN_DOWN(xe_vma_start(vma), length);
+			start = ALIGN_DOWN(orig_start, length);
 		}
 
 		/*
@@ -330,16 +332,17 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 */
 		if (length >= SZ_2M) {
 			length = max_t(u64, SZ_16M, length);
-			start = ALIGN_DOWN(xe_vma_start(vma), length);
+			start = ALIGN_DOWN(orig_start, length);
 		}
 
 		xe_gt_assert(gt, length >= SZ_4K);
 		xe_gt_assert(gt, is_power_of_2(length));
-		xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1,
 ilog2(SZ_2M) + 1)));
+		xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1,
+						    ilog2(SZ_2M) + 1)));
 		xe_gt_assert(gt, IS_ALIGNED(start, length));
 
 		action[len++] =
 MAKE_INVAL_OP(XE_GUC_TLB_INVAL_PAGE_SELECTIVE);
-		action[len++] = xe_vma_vm(vma)->usm.asid;
+		action[len++] = asid;
 		action[len++] = lower_32_bits(start);
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c +26 -0
 		action[len++] = upper_32_bits(start);
 		action[len++] = ilog2(length) - ilog2(SZ_4K);
@@ -350,6 +353,32 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	return send_tlb_invalidation(&gt->uc.guc, fence, action, len);
 }
 
+/**
+ * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
+ * @gt: graphics tile
+ * @fence: invalidation fence which will be signal on TLB invalidation
+ * completion, can be NULL
+ * @vma: VMA to invalidate
+ *
+ * Issue a range based TLB invalidation if supported, if not fallback to a full
+ * TLB invalidation. Completion of TLB is asynchronous and caller can either use
+ * the invalidation fence or seqno + xe_gt_tlb_invalidation_wait to wait for
+ * completion.
+ *
+ * Return: Seqno which can be passed to xe_gt_tlb_invalidation_wait on success,
+ * negative error code on error.
+ */
+int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
+			       struct xe_gt_tlb_invalidation_fence *fence,
+			       struct xe_vma *vma)
+{
+	xe_gt_assert(gt, vma);
+
+	return xe_gt_tlb_invalidation_range(gt, fence, xe_vma_start(vma),
+					    xe_vma_end(vma),
+					    xe_vma_vm(vma)->usm.asid);
+}
+
 /**
  * xe_gt_tlb_invalidation_wait - Wait for TLB to complete
  * @gt: graphics tile
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h +3 -0
index fbb743d80d2c..bf3bebd9f985 100644
@@ -20,6 +20,9 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt);
 int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 			       struct xe_gt_tlb_invalidation_fence *fence,
 			       struct xe_vma *vma);
+int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
+				 struct xe_gt_tlb_invalidation_fence *fence,
+				 u64 start, u64 end, u32 asid);
 int xe_gt_tlb_invalidation_wait(struct xe_gt *gt, int seqno);
 int xe_guc_tlb_invalidation_done_handler(struct xe_guc *guc, u32 *msg, u32
 len);
drivers/gpu/drm/xe/xe_pt.c +0 -0
index 7f54bc3e389d..110d6917089b 100644
@@ -1074,10 +1074,12 @@ static const struct xe_migrate_pt_update_ops
 userptr_bind_ops = {

drivers/gpu/drm/xe/xe_pt.c static const struct xe_migrate_pt_update_ops · line 1083

Quoted 10 lines
drivers/gpu/drm/xe/xe_pt.c +3 -1
 struct invalidation_fence {
 	struct xe_gt_tlb_invalidation_fence base;
 	struct xe_gt *gt;
-	struct xe_vma *vma;
 	struct dma_fence *fence;
 	struct dma_fence_cb cb;
 	struct work_struct work;
+	u64 start;
+	u64 end;
+	u32 asid;

I didn't see start/end/asid is used anywhere else, except in the below fence_init function... do we really need those members?

Oak

Quoted 66 lines
drivers/gpu/drm/xe/xe_pt.c +0 -0
 };
 

static const char *

drivers/gpu/drm/xe/xe_pt.c +6 -3
@@ -1120,13 +1122,14 @@ static void invalidation_fence_work_func(struct
 work_struct *w)
 		container_of(w, struct invalidation_fence, work);
 
 	trace_xe_gt_tlb_invalidation_fence_work_func(&ifence->base);
-	xe_gt_tlb_invalidation_vma(ifence->gt, &ifence->base, ifence->vma);
+	xe_gt_tlb_invalidation_range(ifence->gt, &ifence->base, ifence->start,
+				     ifence->end, ifence->asid);
 }
 
 static int invalidation_fence_init(struct xe_gt *gt,
 				   struct invalidation_fence *ifence,
 				   struct dma_fence *fence,
-				   struct xe_vma *vma)
+				   u64 start, u64 end, u32 asid)
 {
 	int ret;
@@ -1144,7 +1147,9 @@ static int invalidation_fence_init(struct xe_gt *gt,
 	dma_fence_get(&ifence->base.base);	/* Ref for caller */
 	ifence->fence = fence;
 	ifence->gt = gt;
-	ifence->vma = vma;
+	ifence->start = start;
+	ifence->end = end;
+	ifence->asid = asid;
 
 	INIT_WORK(&ifence->work, invalidation_fence_work_func);
 	ret = dma_fence_add_callback(fence, &ifence->cb,

invalidation_fence_cb);

drivers/gpu/drm/xe/xe_pt.c +5 -2
@@ -1286,8 +1291,11 @@ __xe_pt_bind_vma(struct xe_tile *tile, struct xe_vma
 *vma, struct xe_exec_queue
 
 		/* TLB invalidation must be done before signaling rebind */
 		if (ifence) {
-			int err = invalidation_fence_init(tile->primary_gt, ifence,
 fence,
-							  vma);
+			int err = invalidation_fence_init(tile->primary_gt,
+							  ifence, fence,
+							  xe_vma_start(vma),
+							  xe_vma_end(vma),
+							  xe_vma_vm(vma)-
 >usm.asid);
drivers/gpu/drm/xe/xe_pt.c +0 -0
 			if (err) {
 				dma_fence_put(fence);
 				kfree(ifence);
@@ -1625,7 +1633,10 @@ __xe_pt_unbind_vma(struct xe_tile *tile, struct

xe_vma *vma, struct xe_exec_queu

drivers/gpu/drm/xe/xe_pt.c +0 -1
 			dma_fence_wait(fence, false);
 
 		/* TLB invalidation must be done before signaling unbind */
-		err = invalidation_fence_init(tile->primary_gt, ifence, fence,

vma);

drivers/gpu/drm/xe/xe_pt.c +4 -1
+		err = invalidation_fence_init(tile->primary_gt, ifence, fence,
+					      xe_vma_start(vma),
+					      xe_vma_end(vma),
+					      xe_vma_vm(vma)->usm.asid);
 		if (err) {
 			dma_fence_put(fence);
 			kfree(ifence);
--

2.34.1

author maintainer
#44 lore raw

Re: [PATCH v4 14/30] drm/xe: Add xe_gt_tlb_invalidation_range and convert PT layer to use this

Quoted 178 lines · Oak

Hi Matt,

Patch looks good to me. See one question inline

Quoted 174 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 14/30] drm/xe: Add xe_gt_tlb_invalidation_range and convert PT layer to use this

xe_gt_tlb_invalidation_range accepts a start and end address rather than a VMA. This will enable multiple VMAs to be invalidated in a single invalidation. Update the PT layer to use this new function.

---
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c | 59 +++++++++++++++------
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h |  3 ++
 drivers/gpu/drm/xe/xe_pt.c                  | 25 ++++++---
 3 files changed, 65 insertions(+), 22 deletions(-)
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c +18 -15
index a3c4ffba679d..ac2bf86de39a 100644
@@ -264,11 +264,15 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
 }
 
 /**
- * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
+ * xe_gt_tlb_invalidation_range - Issue a TLB invalidation on this GT for an
+ * address range
+ *
  * @gt: graphics tile
  * @fence: invalidation fence which will be signal on TLB invalidation
  * completion, can be NULL
- * @vma: VMA to invalidate
+ * @start: start address
+ * @end: end address
+ * @asid: address space id
  *
  * Issue a range based TLB invalidation if supported, if not fallback to a full
  * TLB invalidation. Completion of TLB is asynchronous and caller can either use
@@ -278,17 +282,15 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
  * Return: Seqno which can be passed to xe_gt_tlb_invalidation_wait on success,
  * negative error code on error.
  */
-int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
-			       struct xe_gt_tlb_invalidation_fence *fence,
-			       struct xe_vma *vma)
+int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
+				 struct xe_gt_tlb_invalidation_fence *fence,
+				 u64 start, u64 end, u32 asid)
 {
 	struct xe_device *xe = gt_to_xe(gt);
 #define MAX_TLB_INVALIDATION_LEN	7
 	u32 action[MAX_TLB_INVALIDATION_LEN];
 	int len = 0;
 
-	xe_gt_assert(gt, vma);
-
 	/* Execlists not supported */
 	if (gt_to_xe(gt)->info.force_execlist) {
 		if (fence)
@@ -302,8 +304,8 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	if (!xe->info.has_range_tlb_invalidation) {
 		action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_FULL);
 	} else {
-		u64 start = xe_vma_start(vma);
-		u64 length = xe_vma_size(vma);
+		u64 orig_start = start;
+		u64 length = end - start;
 		u64 align, end;
 
 		if (length < SZ_4K)
@@ -316,12 +318,12 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 * address mask covering the required range.
 		 */
 		align = roundup_pow_of_two(length);
-		start = ALIGN_DOWN(xe_vma_start(vma), align);
-		end = ALIGN(xe_vma_end(vma), align);
+		start = ALIGN_DOWN(start, align);
+		end = ALIGN(end, align);
 		length = align;
 		while (start + length < end) {
 			length <<= 1;
-			start = ALIGN_DOWN(xe_vma_start(vma), length);
+			start = ALIGN_DOWN(orig_start, length);
 		}
 
 		/*
@@ -330,16 +332,17 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 */
 		if (length >= SZ_2M) {
 			length = max_t(u64, SZ_16M, length);
-			start = ALIGN_DOWN(xe_vma_start(vma), length);
+			start = ALIGN_DOWN(orig_start, length);
 		}
 
 		xe_gt_assert(gt, length >= SZ_4K);
 		xe_gt_assert(gt, is_power_of_2(length));
-		xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1,
 ilog2(SZ_2M) + 1)));
+		xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1,
+						    ilog2(SZ_2M) + 1)));
 		xe_gt_assert(gt, IS_ALIGNED(start, length));
 
 		action[len++] =
 MAKE_INVAL_OP(XE_GUC_TLB_INVAL_PAGE_SELECTIVE);
-		action[len++] = xe_vma_vm(vma)->usm.asid;
+		action[len++] = asid;
 		action[len++] = lower_32_bits(start);
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c +26 -0
 		action[len++] = upper_32_bits(start);
 		action[len++] = ilog2(length) - ilog2(SZ_4K);
@@ -350,6 +353,32 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	return send_tlb_invalidation(&gt->uc.guc, fence, action, len);
 }
 
+/**
+ * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
+ * @gt: graphics tile
+ * @fence: invalidation fence which will be signal on TLB invalidation
+ * completion, can be NULL
+ * @vma: VMA to invalidate
+ *
+ * Issue a range based TLB invalidation if supported, if not fallback to a full
+ * TLB invalidation. Completion of TLB is asynchronous and caller can either use
+ * the invalidation fence or seqno + xe_gt_tlb_invalidation_wait to wait for
+ * completion.
+ *
+ * Return: Seqno which can be passed to xe_gt_tlb_invalidation_wait on success,
+ * negative error code on error.
+ */
+int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
+			       struct xe_gt_tlb_invalidation_fence *fence,
+			       struct xe_vma *vma)
+{
+	xe_gt_assert(gt, vma);
+
+	return xe_gt_tlb_invalidation_range(gt, fence, xe_vma_start(vma),
+					    xe_vma_end(vma),
+					    xe_vma_vm(vma)->usm.asid);
+}
+
 /**
  * xe_gt_tlb_invalidation_wait - Wait for TLB to complete
  * @gt: graphics tile
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.h +3 -0
index fbb743d80d2c..bf3bebd9f985 100644
@@ -20,6 +20,9 @@ int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt);
 int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 			       struct xe_gt_tlb_invalidation_fence *fence,
 			       struct xe_vma *vma);
+int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
+				 struct xe_gt_tlb_invalidation_fence *fence,
+				 u64 start, u64 end, u32 asid);
 int xe_gt_tlb_invalidation_wait(struct xe_gt *gt, int seqno);
 int xe_guc_tlb_invalidation_done_handler(struct xe_guc *guc, u32 *msg, u32
 len);
drivers/gpu/drm/xe/xe_pt.c +0 -1
index 7f54bc3e389d..110d6917089b 100644
@@ -1074,10 +1074,12 @@ static const struct xe_migrate_pt_update_ops
 userptr_bind_ops = {
 struct invalidation_fence {
 	struct xe_gt_tlb_invalidation_fence base;
 	struct xe_gt *gt;
-	struct xe_vma *vma;
 	struct dma_fence *fence;
Quoted 9 lines

drivers/gpu/drm/xe/xe_pt.c static const struct xe_migrate_pt_update_ops userptr_bind_ops = { · line 1082

Quoted 5 lines
drivers/gpu/drm/xe/xe_pt.c +3 -0
 	struct dma_fence_cb cb;
 	struct work_struct work;
+	u64 start;
+	u64 end;
+	u32 asid;

I didn't see start/end/asid is used anywhere else, except in the below fence_init function... do we really need those members?

Yes, see below.

Quoted 9 lines

Oak

Quoted 7 lines
drivers/gpu/drm/xe/xe_pt.c +0 -0
 };
 

static const char *

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -1120,13 +1122,14 @@ static void invalidation_fence_work_func(struct
 work_struct *w)
 		container_of(w, struct invalidation_fence, work);
 
Quoted 4 lines

drivers/gpu/drm/xe/xe_pt.c static void invalidation_fence_work_func(struct work_struct *w) · line 1126

Quoted 4 lines
drivers/gpu/drm/xe/xe_pt.c +2 -1
 	trace_xe_gt_tlb_invalidation_fence_work_func(&ifence->base);
-	xe_gt_tlb_invalidation_vma(ifence->gt, &ifence->base, ifence->vma);
+	xe_gt_tlb_invalidation_range(ifence->gt, &ifence->base, ifence->start,
+				     ifence->end, ifence->asid);

They are used right here ^^^

Matt

Quoted 56 lines
Quoted 55 lines
drivers/gpu/drm/xe/xe_pt.c +4 -2
 }
 
 static int invalidation_fence_init(struct xe_gt *gt,
 				   struct invalidation_fence *ifence,
 				   struct dma_fence *fence,
-				   struct xe_vma *vma)
+				   u64 start, u64 end, u32 asid)
 {
 	int ret;
@@ -1144,7 +1147,9 @@ static int invalidation_fence_init(struct xe_gt *gt,
 	dma_fence_get(&ifence->base.base);	/* Ref for caller */
 	ifence->fence = fence;
 	ifence->gt = gt;
-	ifence->vma = vma;
+	ifence->start = start;
+	ifence->end = end;
+	ifence->asid = asid;
 
 	INIT_WORK(&ifence->work, invalidation_fence_work_func);
 	ret = dma_fence_add_callback(fence, &ifence->cb,

invalidation_fence_cb);

drivers/gpu/drm/xe/xe_pt.c +5 -2
@@ -1286,8 +1291,11 @@ __xe_pt_bind_vma(struct xe_tile *tile, struct xe_vma
 *vma, struct xe_exec_queue
 
 		/* TLB invalidation must be done before signaling rebind */
 		if (ifence) {
-			int err = invalidation_fence_init(tile->primary_gt, ifence,
 fence,
-							  vma);
+			int err = invalidation_fence_init(tile->primary_gt,
+							  ifence, fence,
+							  xe_vma_start(vma),
+							  xe_vma_end(vma),
+							  xe_vma_vm(vma)-
 >usm.asid);
drivers/gpu/drm/xe/xe_pt.c +0 -0
 			if (err) {
 				dma_fence_put(fence);
 				kfree(ifence);
@@ -1625,7 +1633,10 @@ __xe_pt_unbind_vma(struct xe_tile *tile, struct

xe_vma *vma, struct xe_exec_queu

drivers/gpu/drm/xe/xe_pt.c +0 -1
 			dma_fence_wait(fence, false);
 
 		/* TLB invalidation must be done before signaling unbind */
-		err = invalidation_fence_init(tile->primary_gt, ifence, fence,

vma);

drivers/gpu/drm/xe/xe_pt.c +4 -1
+		err = invalidation_fence_init(tile->primary_gt, ifence, fence,
+					      xe_vma_start(vma),
+					      xe_vma_end(vma),
+					      xe_vma_vm(vma)->usm.asid);
 		if (err) {
 			dma_fence_put(fence);
 			kfree(ifence);
--

2.34.1

author maintainer
#45 lore raw

PATCH v4 15/30 drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

In reply to Matthew Brost

Will help with the converstion to 1 job per VM bind IOCTL. Allocation only implemented in this patch.

---
 drivers/gpu/drm/xe/xe_pt_types.h | 12 ++++++
 drivers/gpu/drm/xe/xe_vm.c       | 66 +++++++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++
 3 files changed, 81 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_pt_types.h +12 -0
index cee70cb0f014..2093150f461e 100644
@@ -74,4 +74,16 @@ struct xe_vm_pgtable_update {
 	u32 flags;
 };
 
+/** struct xe_vm_pgtable_update_op - Page table update operation */
+struct xe_vm_pgtable_update_op {
+	/** @entries: entries to update for this operation */
+	struct xe_vm_pgtable_update entries[XE_VM_MAX_LEVEL * 2 + 1];
+	/** @num_entries: number of entries for this update operation */
+	u32 num_entries;
+	/** @bind: is a bind */
+	bool bind;
+	/** @rebind: is a rebind */
+	bool rebind;
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +61 -5

134 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm_types.h +8 -0
index f6ea4df64e79..f5d740dcbba3 100644
@@ -22,6 +22,7 @@ struct xe_device;
 struct xe_sync_entry;
 struct xe_user_fence;
 struct xe_vm;
+struct xe_vm_pgtable_update_op;
 
 #define XE_VMA_READ_ONLY	DRM_GPUVA_USERBITS
 #define XE_VMA_DESTROYED	(DRM_GPUVA_USERBITS << 1)
@@ -219,6 +220,13 @@ struct xe_vma_ops {
 	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
+	/** @pt_update_ops: page table update operations */
+	struct {
+		/** @ops: operations */
+		struct xe_vm_pgtable_update_op *ops;
+		/** @num_ops: number of operations */
+		u32 num_ops;
+	} pt_update_ops[XE_MAX_TILES_PER_DEVICE];
 };
 
 struct xe_vm {
Signature · 1 line
2.34.1
#46 lore raw

RE: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 11 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 2 lines

Will help with the converstion to 1 job per VM bind IOCTL. Allocation only implemented in this patch.

Can you explain why you need to introduce pt_update_op? this patch seems only allocate some slot for pt_update_op but they are not really used for page table update. I will keep looking the rest of this series.

I also don't get why this helps 1job/bind ioctl.

Oak

Quoted 220 lines
---
 drivers/gpu/drm/xe/xe_pt_types.h | 12 ++++++
 drivers/gpu/drm/xe/xe_vm.c       | 66 +++++++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++
 3 files changed, 81 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_pt_types.h +12 -0
index cee70cb0f014..2093150f461e 100644
@@ -74,4 +74,16 @@ struct xe_vm_pgtable_update {
 	u32 flags;
 };
 
+/** struct xe_vm_pgtable_update_op - Page table update operation */
+struct xe_vm_pgtable_update_op {
+	/** @entries: entries to update for this operation */
+	struct xe_vm_pgtable_update entries[XE_VM_MAX_LEVEL * 2 + 1];
+	/** @num_entries: number of entries for this update operation */
+	u32 num_entries;
+	/** @bind: is a bind */
+	bool bind;
+	/** @rebind: is a rebind */
+	bool rebind;
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +36 -0
index 5b93c71fc5e9..72e9bdab79d5 100644
@@ -1319,6 +1319,42 @@ static void xe_vma_ops_init(struct xe_vma_ops *vops,
 struct xe_vm *vm,
 	vops->num_syncs = num_syncs;
 }
 
+static int xe_vma_ops_alloc(struct xe_vma_ops *vops)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
+		if (!vops->pt_update_ops[i].num_ops)
+			continue;
+
+		vops->pt_update_ops[i].ops =
+			kmalloc_array(vops->pt_update_ops[i].num_ops,
+				      sizeof(*vops->pt_update_ops[i].ops),
+				      GFP_KERNEL);
+		if (!vops->pt_update_ops[i].ops)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static void xe_vma_ops_fini(struct xe_vma_ops *vops)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
+		kfree(vops->pt_update_ops[i].ops);
+}
+
+static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8
 tile_mask)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
+		if (BIT(i) & tile_mask)
+			++vops->pt_update_ops[i].num_ops;
+}
+
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)

{

drivers/gpu/drm/xe/xe_vm.c +11 -4
 	struct drm_gem_object *vm_resv_obj;
@@ -1343,6 +1379,11 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	xe_vma_ops_init(&vm->dummy_ops.vops, vm, NULL, NULL, 0);
 	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
 	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
+	for (id = 0; id < XE_MAX_TILES_PER_DEVICE; ++id)
+		vm->dummy_ops.vops.pt_update_ops[id].num_ops = 1;
+	err = xe_vma_ops_alloc(&vm->dummy_ops.vops);
+	if (err)
+		goto err_free;
 
 	INIT_LIST_HEAD(&vm->rebind_list);
@@ -1468,12 +1509,14 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	return ERR_PTR(err);
 
 err_no_resv:
-	mutex_destroy(&vm->snap_mutex);
+	if (!(flags & XE_VM_FLAG_MIGRATION))
+		xe_device_mem_access_put(xe);
 	for_each_tile(tile, xe, id)
 		xe_range_fence_tree_fini(&vm->rftree[id]);
+err_free:
+	mutex_destroy(&vm->snap_mutex);
+	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);
-	if (!(flags & XE_VM_FLAG_MIGRATION))
-		xe_device_mem_access_put(xe);
 	return ERR_PTR(err);
 }
@@ -1611,6 +1654,7 @@ static void vm_destroy_work_func(struct work_struct
 *w)
 
 	trace_xe_vm_free(vm);
 	dma_fence_put(vm->rebind_fence);
+	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);
 }
@@ -2284,7 +2328,6 @@ static int xe_vma_op_commit(struct xe_vm *vm, struct
 xe_vma_op *op)
 	return err;
 }
 
-
 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 				   struct drm_gpuva_ops *ops,
drivers/gpu/drm/xe/xe_vm.c +3 -0
 				   struct xe_sync_entry *syncs, u32 num_syncs,
@@ -2334,6 +2377,9 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 				return PTR_ERR(vma);
 
 			op->map.vma = vma;
+			if (op->map.immediate || !xe_vm_in_fault_mode(vm))
+				xe_vma_ops_incr_pt_update_ops(vops,
+							      op->tile_mask);
 			break;
 		}
drivers/gpu/drm/xe/xe_vm.c +2 -0
 		case DRM_GPUVA_OP_REMAP:
@@ -2378,6 +2424,8 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 					vm_dbg(&xe->drm, "REMAP:SKIP_PREV:
 addr=0x%016llx, range=0x%016llx",
 					       (ULL)op->remap.start,
 					       (ULL)op->remap.range);
+				} else {
+					xe_vma_ops_incr_pt_update_ops(vops,
 op->tile_mask);
drivers/gpu/drm/xe/xe_vm.c +4 -1
 				}
 			}
@@ -2414,13 +2462,16 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 					vm_dbg(&xe->drm, "REMAP:SKIP_NEXT:
 addr=0x%016llx, range=0x%016llx",
 					       (ULL)op->remap.start,
 					       (ULL)op->remap.range);
+				} else {
+					xe_vma_ops_incr_pt_update_ops(vops,
 op->tile_mask);
 				}
 			}
+			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
 			break;
 		}
 		case DRM_GPUVA_OP_UNMAP:
 		case DRM_GPUVA_OP_PREFETCH:
-			/* Nothing to do */
+			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
drivers/gpu/drm/xe/xe_vm.c +5 -0
 			break;
 		default:
 			drm_warn(&vm->xe->drm, "NOT POSSIBLE");
@@ -3243,11 +3294,16 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto unwind_ops;
 	}
 
+	err = xe_vma_ops_alloc(&vops);
+	if (err)
+		goto unwind_ops;
+
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
 unwind_ops:
 	if (err && err != -ENODATA)
 		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	xe_vma_ops_fini(&vops);
 	for (i = args->num_binds - 1; i >= 0; --i)
 		if (ops[i])
drivers/gpu/drm/xe/xe_vm.c +0 -0
 			drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
drivers/gpu/drm/xe/xe_vm_types.h +8 -1
index f6ea4df64e79..f5d740dcbba3 100644
@@ -22,6 +22,7 @@ struct xe_device;
 struct xe_sync_entry;
 struct xe_user_fence;
 struct xe_vm;
+struct xe_vm_pgtable_update_op;
 
 #define XE_VMA_READ_ONLY	DRM_GPUVA_USERBITS
 #define XE_VMA_DESTROYED	(DRM_GPUVA_USERBITS << 1)
@@ -219,6 +220,13 @@ struct xe_vma_ops {
 	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
+	/** @pt_update_ops: page table update operations */
+	struct {
+		/** @ops: operations */
+		struct xe_vm_pgtable_update_op *ops;
+		/** @num_ops: number of operations */
+		u32 num_ops;
+	} pt_update_ops[XE_MAX_TILES_PER_DEVICE];
 };
 
 struct xe_vm {
--

2.34.1

author maintainer
#47 lore raw

Re: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 11 lines · Oak
Quoted 11 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 8 lines
Quoted 2 lines

Will help with the converstion to 1 job per VM bind IOCTL. Allocation only implemented in this patch.

Can you explain why you need to introduce pt_update_op? this patch seems only allocate some slot for pt_update_op but they are not really used for page table update. I will keep looking the rest of this series.

I also don't get why this helps 1job/bind ioctl.

Each VMA operations is converted into 0-3 PT per ops tile. i.e. a MAP VMA operations with immediate clear generates 0 PT ops, a REMAP VMA with both prev & next generates 3 PT ops. PT ops work on the internal / GPU PT state compared to VMA ops which operate on the internal GPUVM state.

The flow roughly is:

generate VMA ops from IOCTL input
convert all VMA ops to PT ops, prep and stage PT ops run PT ops as an atomic unit (1 job)
commit PT ops
commit VMA ops

If at any point a failure occurs before the commit step the PT ops can be unwound.

Likewise when a failure occurs the VMA ops are unwound too, this is just layer above the PT ops.

In the end if an errors at point in the IOCTL the original GPUVM and PT state is restored. This compared to serial execution on tip where is an error occurs we just kill the VM.

Does this make sense?

Matt

Quoted 224 lines

Oak

Quoted 220 lines
---
 drivers/gpu/drm/xe/xe_pt_types.h | 12 ++++++
 drivers/gpu/drm/xe/xe_vm.c       | 66 +++++++++++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++
 3 files changed, 81 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_pt_types.h +12 -0
index cee70cb0f014..2093150f461e 100644
@@ -74,4 +74,16 @@ struct xe_vm_pgtable_update {
 	u32 flags;
 };
 
+/** struct xe_vm_pgtable_update_op - Page table update operation */
+struct xe_vm_pgtable_update_op {
+	/** @entries: entries to update for this operation */
+	struct xe_vm_pgtable_update entries[XE_VM_MAX_LEVEL * 2 + 1];
+	/** @num_entries: number of entries for this update operation */
+	u32 num_entries;
+	/** @bind: is a bind */
+	bool bind;
+	/** @rebind: is a rebind */
+	bool rebind;
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +36 -0
index 5b93c71fc5e9..72e9bdab79d5 100644
@@ -1319,6 +1319,42 @@ static void xe_vma_ops_init(struct xe_vma_ops *vops,
 struct xe_vm *vm,
 	vops->num_syncs = num_syncs;
 }
 
+static int xe_vma_ops_alloc(struct xe_vma_ops *vops)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
+		if (!vops->pt_update_ops[i].num_ops)
+			continue;
+
+		vops->pt_update_ops[i].ops =
+			kmalloc_array(vops->pt_update_ops[i].num_ops,
+				      sizeof(*vops->pt_update_ops[i].ops),
+				      GFP_KERNEL);
+		if (!vops->pt_update_ops[i].ops)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static void xe_vma_ops_fini(struct xe_vma_ops *vops)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
+		kfree(vops->pt_update_ops[i].ops);
+}
+
+static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8
 tile_mask)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
+		if (BIT(i) & tile_mask)
+			++vops->pt_update_ops[i].num_ops;
+}
+
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)

{

drivers/gpu/drm/xe/xe_vm.c +11 -4
 	struct drm_gem_object *vm_resv_obj;
@@ -1343,6 +1379,11 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	xe_vma_ops_init(&vm->dummy_ops.vops, vm, NULL, NULL, 0);
 	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
 	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
+	for (id = 0; id < XE_MAX_TILES_PER_DEVICE; ++id)
+		vm->dummy_ops.vops.pt_update_ops[id].num_ops = 1;
+	err = xe_vma_ops_alloc(&vm->dummy_ops.vops);
+	if (err)
+		goto err_free;
 
 	INIT_LIST_HEAD(&vm->rebind_list);
@@ -1468,12 +1509,14 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	return ERR_PTR(err);
 
 err_no_resv:
-	mutex_destroy(&vm->snap_mutex);
+	if (!(flags & XE_VM_FLAG_MIGRATION))
+		xe_device_mem_access_put(xe);
 	for_each_tile(tile, xe, id)
 		xe_range_fence_tree_fini(&vm->rftree[id]);
+err_free:
+	mutex_destroy(&vm->snap_mutex);
+	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);
-	if (!(flags & XE_VM_FLAG_MIGRATION))
-		xe_device_mem_access_put(xe);
 	return ERR_PTR(err);
 }
@@ -1611,6 +1654,7 @@ static void vm_destroy_work_func(struct work_struct
 *w)
 
 	trace_xe_vm_free(vm);
 	dma_fence_put(vm->rebind_fence);
+	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);
 }
@@ -2284,7 +2328,6 @@ static int xe_vma_op_commit(struct xe_vm *vm, struct
 xe_vma_op *op)
 	return err;
 }
 
-
 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
 				   struct drm_gpuva_ops *ops,
drivers/gpu/drm/xe/xe_vm.c +3 -0
 				   struct xe_sync_entry *syncs, u32 num_syncs,
@@ -2334,6 +2377,9 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 				return PTR_ERR(vma);
 
 			op->map.vma = vma;
+			if (op->map.immediate || !xe_vm_in_fault_mode(vm))
+				xe_vma_ops_incr_pt_update_ops(vops,
+							      op->tile_mask);
 			break;
 		}
drivers/gpu/drm/xe/xe_vm.c +2 -0
 		case DRM_GPUVA_OP_REMAP:
@@ -2378,6 +2424,8 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 					vm_dbg(&xe->drm, "REMAP:SKIP_PREV:
 addr=0x%016llx, range=0x%016llx",
 					       (ULL)op->remap.start,
 					       (ULL)op->remap.range);
+				} else {
+					xe_vma_ops_incr_pt_update_ops(vops,
 op->tile_mask);
drivers/gpu/drm/xe/xe_vm.c +4 -1
 				}
 			}
@@ -2414,13 +2462,16 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 					vm_dbg(&xe->drm, "REMAP:SKIP_NEXT:
 addr=0x%016llx, range=0x%016llx",
 					       (ULL)op->remap.start,
 					       (ULL)op->remap.range);
+				} else {
+					xe_vma_ops_incr_pt_update_ops(vops,
 op->tile_mask);
 				}
 			}
+			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
 			break;
 		}
 		case DRM_GPUVA_OP_UNMAP:
 		case DRM_GPUVA_OP_PREFETCH:
-			/* Nothing to do */
+			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
drivers/gpu/drm/xe/xe_vm.c +5 -0
 			break;
 		default:
 			drm_warn(&vm->xe->drm, "NOT POSSIBLE");
@@ -3243,11 +3294,16 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void
 *data, struct drm_file *file)
 		goto unwind_ops;
 	}
 
+	err = xe_vma_ops_alloc(&vops);
+	if (err)
+		goto unwind_ops;
+
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
 unwind_ops:
 	if (err && err != -ENODATA)
 		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	xe_vma_ops_fini(&vops);
 	for (i = args->num_binds - 1; i >= 0; --i)
 		if (ops[i])
drivers/gpu/drm/xe/xe_vm.c +0 -0
 			drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
drivers/gpu/drm/xe/xe_vm_types.h +8 -1
index f6ea4df64e79..f5d740dcbba3 100644
@@ -22,6 +22,7 @@ struct xe_device;
 struct xe_sync_entry;
 struct xe_user_fence;
 struct xe_vm;
+struct xe_vm_pgtable_update_op;
 
 #define XE_VMA_READ_ONLY	DRM_GPUVA_USERBITS
 #define XE_VMA_DESTROYED	(DRM_GPUVA_USERBITS << 1)
@@ -219,6 +220,13 @@ struct xe_vma_ops {
 	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
+	/** @pt_update_ops: page table update operations */
+	struct {
+		/** @ops: operations */
+		struct xe_vm_pgtable_update_op *ops;
+		/** @num_ops: number of operations */
+		u32 num_ops;
+	} pt_update_ops[XE_MAX_TILES_PER_DEVICE];
 };
 
 struct xe_vm {
--

2.34.1

#48 lore raw

RE: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 50 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Tuesday, March 26, 2024 3:06 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 4 lines · Oak
Quoted 4 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of

Matthew

Quoted 7 lines
Quoted 7 lines

Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 15/30] drm/xe: Add xe_vm_pgtable_update_op to xe_vma_ops

Quoted 5 lines
Quoted 2 lines

Will help with the converstion to 1 job per VM bind IOCTL. Allocation only implemented in this patch.

Can you explain why you need to introduce pt_update_op? this patch seems

only allocate some slot for pt_update_op but they are not really used for page table update. I will keep looking the rest of this series.

Quoted 3 lines

I also don't get why this helps 1job/bind ioctl.

Each VMA operations is converted into 0-3 PT per ops tile. i.e. a MAP VMA operations with immediate clear generates 0 PT ops, a REMAP VMA with both prev & next generates 3 PT ops. PT ops work on the internal / GPU PT state compared to VMA ops which operate on the internal GPUVM state.

The flow roughly is:

generate VMA ops from IOCTL input
convert all VMA ops to PT ops, prep and stage PT ops run PT ops as an atomic unit (1 job)
commit PT ops
commit VMA ops

If at any point a failure occurs before the commit step the PT ops can be unwound.

Quoted 8 lines

Likewise when a failure occurs the VMA ops are unwound too, this is just layer above the PT ops.

In the end if an errors at point in the IOCTL the original GPUVM and PT state is restored. This compared to serial execution on tip where is an error occurs we just kill the VM.

Does this make sense?

Hi Matt,

Thanks for explain it. Should we also document such implementation details in the xe_vm_doc.h?

Oak

Quoted 236 lines

Matt

Quoted 8 lines

Oak

Quoted 5 lines
---
 drivers/gpu/drm/xe/xe_pt_types.h | 12 ++++++
 drivers/gpu/drm/xe/xe_vm.c       | 66 +++++++++++++++++++++++++++++--

-

Quoted 22 lines
Quoted 22 lines
 drivers/gpu/drm/xe/xe_vm_types.h |  8 ++++
 3 files changed, 81 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_pt_types.h +10 -0
index cee70cb0f014..2093150f461e 100644
@@ -74,4 +74,16 @@ struct xe_vm_pgtable_update {
 	u32 flags;
 };
 
+/** struct xe_vm_pgtable_update_op - Page table update operation */
+struct xe_vm_pgtable_update_op {
+	/** @entries: entries to update for this operation */
+	struct xe_vm_pgtable_update entries[XE_VM_MAX_LEVEL * 2 + 1];
+	/** @num_entries: number of entries for this update operation */
+	u32 num_entries;
+	/** @bind: is a bind */
+	bool bind;
+	/** @rebind: is a rebind */
+	bool rebind;
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c static void xe_vma_ops_init(struct xe_vma_ops

Quoted 8 lines
drivers/gpu/drm/xe/xe_pt_types.h +2 -0
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 5b93c71fc5e9..72e9bdab79d5 100644
@@ -1319,6 +1319,42 @@ static void xe_vma_ops_init(struct xe_vma_ops

*vops,

Quoted 55 lines
Quoted 55 lines
drivers/gpu/drm/xe/xe_vm.c +36 -0
 struct xe_vm *vm,
 	vops->num_syncs = num_syncs;
 }
 
+static int xe_vma_ops_alloc(struct xe_vma_ops *vops)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
+		if (!vops->pt_update_ops[i].num_ops)
+			continue;
+
+		vops->pt_update_ops[i].ops =
+			kmalloc_array(vops->pt_update_ops[i].num_ops,
+				      sizeof(*vops->pt_update_ops[i].ops),
+				      GFP_KERNEL);
+		if (!vops->pt_update_ops[i].ops)
+			return -ENOMEM;
+	}
+
+	return 0;
+}
+
+static void xe_vma_ops_fini(struct xe_vma_ops *vops)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
+		kfree(vops->pt_update_ops[i].ops);
+}
+
+static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8
 tile_mask)
+{
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
+		if (BIT(i) & tile_mask)
+			++vops->pt_update_ops[i].num_ops;
+}
+
 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)

{

drivers/gpu/drm/xe/xe_vm.c +5 -0
 	struct drm_gem_object *vm_resv_obj;
@@ -1343,6 +1379,11 @@ struct xe_vm *xe_vm_create(struct xe_device *xe,
 u32 flags)
 	xe_vma_ops_init(&vm->dummy_ops.vops, vm, NULL, NULL, 0);
 	INIT_LIST_HEAD(&vm->dummy_ops.op.link);
 	list_add(&vm->dummy_ops.op.link, &vm->dummy_ops.vops.list);
+	for (id = 0; id < XE_MAX_TILES_PER_DEVICE; ++id)
+		vm->dummy_ops.vops.pt_update_ops[id].num_ops = 1;
+	err = xe_vma_ops_alloc(&vm->dummy_ops.vops);
+	if (err)
+		goto err_free;
 
Quoted 3 lines

drivers/gpu/drm/xe/xe_vm.c struct xe_vm *xe_vm_create(struct xe_device

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	INIT_LIST_HEAD(&vm->rebind_list);
@@ -1468,12 +1509,14 @@ struct xe_vm *xe_vm_create(struct xe_device

*xe,

Quoted 18 lines
Quoted 18 lines
drivers/gpu/drm/xe/xe_vm.c +5 -3
 u32 flags)
 	return ERR_PTR(err);
 
 err_no_resv:
-	mutex_destroy(&vm->snap_mutex);
+	if (!(flags & XE_VM_FLAG_MIGRATION))
+		xe_device_mem_access_put(xe);
 	for_each_tile(tile, xe, id)
 		xe_range_fence_tree_fini(&vm->rftree[id]);
+err_free:
+	mutex_destroy(&vm->snap_mutex);
+	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);
-	if (!(flags & XE_VM_FLAG_MIGRATION))
-		xe_device_mem_access_put(xe);
 	return ERR_PTR(err);
 }
Quoted 1 line

drivers/gpu/drm/xe/xe_vm.c static void vm_destroy_work_func(struct

Quoted 1 line
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -1611,6 +1654,7 @@ static void vm_destroy_work_func(struct

work_struct

Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vma_op_commit(struct xe_vm *vm,

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +1 -0
 *w)
 
 	trace_xe_vm_free(vm);
 	dma_fence_put(vm->rebind_fence);
+	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);
 }
@@ -2284,7 +2328,6 @@ static int xe_vma_op_commit(struct xe_vm *vm,

struct

Quoted 6 lines

drivers/gpu/drm/xe/xe_vm.c static int xe_vma_op_commit(struct xe_vm *vm, struct xe_vma_op *op)

Quoted 6 lines
drivers/gpu/drm/xe/xe_vm.c +0 -1
 xe_vma_op *op)
 	return err;
 }
 
-
 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct

xe_exec_queue *q,

Quoted 3 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_parse(struct xe_vm

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 				   struct drm_gpuva_ops *ops,
drivers/gpu/drm/xe/xe_vm.c +0 -0
 				   struct xe_sync_entry *syncs, u32 num_syncs,
@@ -2334,6 +2377,9 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm

*vm,

Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl_ops_parse(struct xe_vm

Quoted 11 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
 struct xe_exec_queue *q,
 				return PTR_ERR(vma);
 
 			op->map.vma = vma;
+			if (op->map.immediate || !xe_vm_in_fault_mode(vm))
+				xe_vma_ops_incr_pt_update_ops(vops,
+							      op->tile_mask);
 			break;
 		}
drivers/gpu/drm/xe/xe_vm.c +0 -0
 		case DRM_GPUVA_OP_REMAP:
@@ -2378,6 +2424,8 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm

*vm,

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/xe/xe_vm.c +2 -0
 struct xe_exec_queue *q,
 					vm_dbg(&xe->drm, "REMAP:SKIP_PREV:
 addr=0x%016llx, range=0x%016llx",
 					       (ULL)op->remap.start,
 					       (ULL)op->remap.range);
+				} else {
+					xe_vma_ops_incr_pt_update_ops(vops,
 op->tile_mask);
drivers/gpu/drm/xe/xe_vm.c +3 -0
 				}
 			}
@@ -2414,13 +2462,16 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 					vm_dbg(&xe->drm, "REMAP:SKIP_NEXT:
 addr=0x%016llx, range=0x%016llx",
 					       (ULL)op->remap.start,
 					       (ULL)op->remap.range);
+				} else {
+					xe_vma_ops_incr_pt_update_ops(vops,
 op->tile_mask);
 				}
 			}
+			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
 			break;
 		}
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vm_bind_ioctl(struct drm_device *dev,

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +1 -1
 		case DRM_GPUVA_OP_UNMAP:
 		case DRM_GPUVA_OP_PREFETCH:
-			/* Nothing to do */
+			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
drivers/gpu/drm/xe/xe_vm.c +0 -0
 			break;
 		default:
 			drm_warn(&vm->xe->drm, "NOT POSSIBLE");
@@ -3243,11 +3294,16 @@ int xe_vm_bind_ioctl(struct drm_device *dev,

void

Quoted 47 lines
Quoted 46 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
 *data, struct drm_file *file)
 		goto unwind_ops;
 	}
 
+	err = xe_vma_ops_alloc(&vops);
+	if (err)
+		goto unwind_ops;
+
 	err = vm_bind_ioctl_ops_execute(vm, &vops);
 
 unwind_ops:
 	if (err && err != -ENODATA)
 		vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
+	xe_vma_ops_fini(&vops);
 	for (i = args->num_binds - 1; i >= 0; --i)
 		if (ops[i])
drivers/gpu/drm/xe/xe_vm.c +0 -0
 			drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
drivers/gpu/drm/xe/xe_vm_types.h +8 -1
index f6ea4df64e79..f5d740dcbba3 100644
@@ -22,6 +22,7 @@ struct xe_device;
 struct xe_sync_entry;
 struct xe_user_fence;
 struct xe_vm;
+struct xe_vm_pgtable_update_op;
 
 #define XE_VMA_READ_ONLY	DRM_GPUVA_USERBITS
 #define XE_VMA_DESTROYED	(DRM_GPUVA_USERBITS << 1)
@@ -219,6 +220,13 @@ struct xe_vma_ops {
 	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
+	/** @pt_update_ops: page table update operations */
+	struct {
+		/** @ops: operations */
+		struct xe_vm_pgtable_update_op *ops;
+		/** @num_ops: number of operations */
+		u32 num_ops;
+	} pt_update_ops[XE_MAX_TILES_PER_DEVICE];
 };
 
 struct xe_vm {
--

2.34.1

author maintainer
#49 lore raw

PATCH v4 16/30 drm/xe: Use ordered WQ for TLB invalidation fences

In reply to Matthew Brost

TLB invalidation fences need to be ordered within an exec queue and if an unordered WQ is used TLB invalidation fences could be reordered. Use an ordered WQ to fix this.

---
 drivers/gpu/drm/xe/xe_pt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 110d6917089b..a878e2217c7f 100644
@@ -1107,7 +1107,7 @@ static void invalidation_fence_cb(struct dma_fence *fence,
 
 	trace_xe_gt_tlb_invalidation_fence_cb(&ifence->base);
 	if (!ifence->fence->error) {
-		queue_work(system_wq, &ifence->work);
+		queue_work(ifence->gt->ordered_wq, &ifence->work);
 	} else {
 		ifence->base.base.error = ifence->fence->error;
 		dma_fence_signal(&ifence->base.base);
Signature · 1 line
2.34.1
#50 lore raw

RE: [PATCH v4 16/30] drm/xe: Use ordered WQ for TLB invalidation fences

Quoted 10 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 16/30] drm/xe: Use ordered WQ for TLB invalidation fences

Quoted 3 lines

TLB invalidation fences need to be ordered within an exec queue and if an unordered WQ is used TLB invalidation fences could be reordered. Use an ordered WQ to fix this.

The code change below makes sense to me. But I think we should improve the description. I think we need to explain what need to be in order and why. As I understand it, we need things to be in below order (just an example):
1)Gpu page table update (such as invalidate certain page table)
2)Invalidate tlb entries for above page table entries
3)Notify/call back to user fence to let user know above vm_unbind is complete.

Obviously 1) and 2) can't be re-ordered, because 2) should be triggered by 1). But 2) and 3) can be reordered if we use unordered WQ - why we want to use ordered wq here.

Simply saying TLB invalidation fences need to be ordered is too simple, at least it is hard for me to understand. Better to improve it.

Thanks,
Oak

Quoted 22 lines
---
 drivers/gpu/drm/xe/xe_pt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 110d6917089b..a878e2217c7f 100644
@@ -1107,7 +1107,7 @@ static void invalidation_fence_cb(struct dma_fence
 *fence,
 
 	trace_xe_gt_tlb_invalidation_fence_cb(&ifence->base);
 	if (!ifence->fence->error) {
-		queue_work(system_wq, &ifence->work);
+		queue_work(ifence->gt->ordered_wq, &ifence->work);
 	} else {
 		ifence->base.base.error = ifence->fence->error;

dma_fence_signal(&ifence->base.base);
--
2.34.1

author maintainer
#51 lore raw

Re: [PATCH v4 16/30] drm/xe: Use ordered WQ for TLB invalidation fences

Quoted 15 lines · Oak
Quoted 10 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 16/30] drm/xe: Use ordered WQ for TLB invalidation fences

Quoted 3 lines

TLB invalidation fences need to be ordered within an exec queue and if an unordered WQ is used TLB invalidation fences could be reordered. Use an ordered WQ to fix this.

Quoted 9 lines

The code change below makes sense to me. But I think we should improve the description. I think we need to explain what need to be in order and why. As I understand it, we need things to be in below order (just an example):
1)Gpu page table update (such as invalidate certain page table)
2)Invalidate tlb entries for above page table entries
3)Notify/call back to user fence to let user know above vm_unbind is complete.

Obviously 1) and 2) can't be re-ordered, because 2) should be triggered by 1). But 2) and 3) can be reordered if we use unordered WQ - why we want to use ordered wq here.

Simply saying TLB invalidation fences need to be ordered is too simple, at least it is hard for me to understand. Better to improve it.

Let me improve this in the next rev.

Matt

Quoted 26 lines

Thanks,
Oak

Quoted 22 lines
---
 drivers/gpu/drm/xe/xe_pt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 110d6917089b..a878e2217c7f 100644
@@ -1107,7 +1107,7 @@ static void invalidation_fence_cb(struct dma_fence
 *fence,
 
 	trace_xe_gt_tlb_invalidation_fence_cb(&ifence->base);
 	if (!ifence->fence->error) {
-		queue_work(system_wq, &ifence->work);
+		queue_work(ifence->gt->ordered_wq, &ifence->work);
 	} else {
 		ifence->base.base.error = ifence->fence->error;

dma_fence_signal(&ifence->base.base);
--
2.34.1

author maintainer
#52 lore raw

PATCH v4 17/30 drm/xe: Delete PT update selftest

In reply to Matthew Brost

IGTs (e.g. xe_vm) can provide the exact same coverage as the PT update selftest. The PT update selftest is dependent on internal functions which can change thus maintaining this test is costly and provide no extra coverage. Delete this test.

---
 drivers/gpu/drm/xe/tests/xe_migrate.c | 86 ---------------------------
 1 file changed, 86 deletions(-)
drivers/gpu/drm/xe/tests/xe_migrate.c +0 -86
index ce531498f57f..de2c1b7ec371 100644
@@ -62,36 +62,6 @@ static int run_sanity_job(struct xe_migrate *m, struct xe_device *xe,
 	return 0;
 }
 
-static void
-sanity_populate_cb(struct xe_migrate_pt_update *pt_update,
-		   struct xe_tile *tile, struct iosys_map *map, void *dst,
-		   u32 qword_ofs, u32 num_qwords,
-		   const struct xe_vm_pgtable_update *update)
-{
-	struct migrate_test_params *p =
-		to_migrate_test_params(xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));
-	int i;
-	u64 *ptr = dst;
-	u64 value;
-
-	for (i = 0; i < num_qwords; i++) {
-		value = (qword_ofs + i - update->ofs) * 0x1111111111111111ULL;
-		if (map)
-			xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) *
-				  sizeof(u64), u64, value);
-		else
-			ptr[i] = value;
-	}
-
-	kunit_info(xe_cur_kunit(), "Used %s.\n", map ? "CPU" : "GPU");
-	if (p->force_gpu && map)
-		KUNIT_FAIL(xe_cur_kunit(), "GPU pagetable update used CPU.\n");
-}
-
-static const struct xe_migrate_pt_update_ops sanity_ops = {
-	.populate = sanity_populate_cb,
-};
-
 #define check(_retval, _expected, str, _test)				\
 	do { if ((_retval) != (_expected)) {				\
 			KUNIT_FAIL(_test, "Sanity check failed: " str	\
@@ -209,57 +179,6 @@ static void test_copy_vram(struct xe_migrate *m, struct xe_bo *bo,
 	test_copy(m, bo, test, region);
 }
 
-static void test_pt_update(struct xe_migrate *m, struct xe_bo *pt,
-			   struct kunit *test, bool force_gpu)
-{
-	struct xe_device *xe = tile_to_xe(m->tile);
-	struct dma_fence *fence;
-	u64 retval, expected;
-	ktime_t then, now;
-	int i;
-
-	struct xe_vm_pgtable_update update = {
-		.ofs = 1,
-		.qwords = 0x10,
-		.pt_bo = pt,
-	};
-	struct xe_migrate_pt_update pt_update = {
-		.ops = &sanity_ops,
-	};
-	struct migrate_test_params p = {
-		.base.id = XE_TEST_LIVE_MIGRATE,
-		.force_gpu = force_gpu,
-	};
-
-	test->priv = &p;
-	/* Test xe_migrate_update_pgtables() updates the pagetable as expected */
-	expected = 0xf0f0f0f0f0f0f0f0ULL;
-	xe_map_memset(xe, &pt->vmap, 0, (u8)expected, pt->size);
-
-	then = ktime_get();
-	fence = xe_migrate_update_pgtables(m, m->q->vm, NULL, m->q, &update, 1,
-					   NULL, 0, &pt_update);
-	now = ktime_get();
-	if (sanity_fence_failed(xe, fence, "Migration pagetable update", test))
-		return;
-
-	kunit_info(test, "Updating without syncing took %llu us,\n",
-		   (unsigned long long)ktime_to_us(ktime_sub(now, then)));
-
-	dma_fence_put(fence);
-	retval = xe_map_rd(xe, &pt->vmap, 0, u64);
-	check(retval, expected, "PTE[0] must stay untouched", test);
-
-	for (i = 0; i < update.qwords; i++) {
-		retval = xe_map_rd(xe, &pt->vmap, (update.ofs + i) * 8, u64);
-		check(retval, i * 0x1111111111111111ULL, "PTE update", test);
-	}
-
-	retval = xe_map_rd(xe, &pt->vmap, 8 * (update.ofs + update.qwords),
-			   u64);
-	check(retval, expected, "PTE[0x11] must stay untouched", test);
-}
-
 static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
 {
 	struct xe_tile *tile = m->tile;
@@ -398,11 +317,6 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
 		test_copy_vram(m, big, test);
 	}
 
-	kunit_info(test, "Testing page table update using CPU if GPU idle.\n");
-	test_pt_update(m, pt, test, false);
-	kunit_info(test, "Testing page table update using GPU\n");
-	test_pt_update(m, pt, test, true);
-
 out:
 	xe_bb_free(bb, NULL);
 free_tiny:
Signature · 1 line
2.34.1
#53 lore raw

RE: [PATCH v4 17/30] drm/xe: Delete PT update selftest

Agree. Patch is reviewed-by: Oak Zeng <[email protected]>

Quoted 139 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 17/30] drm/xe: Delete PT update selftest

IGTs (e.g. xe_vm) can provide the exact same coverage as the PT update selftest. The PT update selftest is dependent on internal functions which can change thus maintaining this test is costly and provide no extra coverage. Delete this test.

---
 drivers/gpu/drm/xe/tests/xe_migrate.c | 86 ---------------------------
 1 file changed, 86 deletions(-)
drivers/gpu/drm/xe/tests/xe_migrate.c +0 -30
index ce531498f57f..de2c1b7ec371 100644
@@ -62,36 +62,6 @@ static int run_sanity_job(struct xe_migrate *m, struct
 xe_device *xe,
 	return 0;
 }
 
-static void
-sanity_populate_cb(struct xe_migrate_pt_update *pt_update,
-		   struct xe_tile *tile, struct iosys_map *map, void *dst,
-		   u32 qword_ofs, u32 num_qwords,
-		   const struct xe_vm_pgtable_update *update)
-{
-	struct migrate_test_params *p =
-
 	to_migrate_test_params(xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));
-	int i;
-	u64 *ptr = dst;
-	u64 value;
-
-	for (i = 0; i < num_qwords; i++) {
-		value = (qword_ofs + i - update->ofs) * 0x1111111111111111ULL;
-		if (map)
-			xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) *
-				  sizeof(u64), u64, value);
-		else
-			ptr[i] = value;
-	}
-
-	kunit_info(xe_cur_kunit(), "Used %s.\n", map ? "CPU" : "GPU");
-	if (p->force_gpu && map)
-		KUNIT_FAIL(xe_cur_kunit(), "GPU pagetable update used
 CPU.\n");
-}
-
-static const struct xe_migrate_pt_update_ops sanity_ops = {
-	.populate = sanity_populate_cb,
-};
-

#define check(_retval, _expected, str, _test) \

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -24
 	do { if ((_retval) != (_expected)) {				\
 			KUNIT_FAIL(_test, "Sanity check failed: " str	\
@@ -209,57 +179,6 @@ static void test_copy_vram(struct xe_migrate *m, struct
 xe_bo *bo,
 	test_copy(m, bo, test, region);
 }
 
-static void test_pt_update(struct xe_migrate *m, struct xe_bo *pt,
-			   struct kunit *test, bool force_gpu)
-{
-	struct xe_device *xe = tile_to_xe(m->tile);
-	struct dma_fence *fence;
-	u64 retval, expected;
-	ktime_t then, now;
-	int i;
-
-	struct xe_vm_pgtable_update update = {
-		.ofs = 1,
-		.qwords = 0x10,
-		.pt_bo = pt,
-	};
-	struct xe_migrate_pt_update pt_update = {
-		.ops = &sanity_ops,
-	};
-	struct migrate_test_params p = {
-		.base.id = XE_TEST_LIVE_MIGRATE,
-		.force_gpu = force_gpu,
-	};
-
-	test->priv = &p;
-	/* Test xe_migrate_update_pgtables() updates the pagetable as

expected */

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -5
-	expected = 0xf0f0f0f0f0f0f0f0ULL;
-	xe_map_memset(xe, &pt->vmap, 0, (u8)expected, pt->size);
-
-	then = ktime_get();
-	fence = xe_migrate_update_pgtables(m, m->q->vm, NULL, m->q,

&update, 1,

drivers/gpu/drm/xe/tests/xe_migrate.c +0 -27
-					   NULL, 0, &pt_update);
-	now = ktime_get();
-	if (sanity_fence_failed(xe, fence, "Migration pagetable update", test))
-		return;
-
-	kunit_info(test, "Updating without syncing took %llu us,\n",
-		   (unsigned long long)ktime_to_us(ktime_sub(now, then)));
-
-	dma_fence_put(fence);
-	retval = xe_map_rd(xe, &pt->vmap, 0, u64);
-	check(retval, expected, "PTE[0] must stay untouched", test);
-
-	for (i = 0; i < update.qwords; i++) {
-		retval = xe_map_rd(xe, &pt->vmap, (update.ofs + i) * 8, u64);
-		check(retval, i * 0x1111111111111111ULL, "PTE update", test);
-	}
-
-	retval = xe_map_rd(xe, &pt->vmap, 8 * (update.ofs + update.qwords),
-			   u64);
-	check(retval, expected, "PTE[0x11] must stay untouched", test);
-}
-
 static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
 {
 	struct xe_tile *tile = m->tile;
@@ -398,11 +317,6 @@ static void xe_migrate_sanity_test(struct xe_migrate *m,
 struct kunit *test)
 		test_copy_vram(m, big, test);
 	}
 
-	kunit_info(test, "Testing page table update using CPU if GPU idle.\n");
-	test_pt_update(m, pt, test, false);
-	kunit_info(test, "Testing page table update using GPU\n");
-	test_pt_update(m, pt, test, true);
-
 out:
 	xe_bb_free(bb, NULL);

free_tiny:
--
2.34.1

author maintainer
#54 lore raw

PATCH v4 18/30 drm/xe: Convert multiple bind ops into single job

In reply to Matthew Brost

This aligns with the uAPI of an array of binds or single bind that results in multiple GPUVA ops to be considered a single atomic operations.

---
 drivers/gpu/drm/xe/xe_bo_types.h     |    2 +
 drivers/gpu/drm/xe/xe_exec.c         |   25 +-
 drivers/gpu/drm/xe/xe_gt_pagefault.c |    6 +-
 drivers/gpu/drm/xe/xe_migrate.c      |  298 ++++----
 drivers/gpu/drm/xe/xe_migrate.h      |   32 +-
 drivers/gpu/drm/xe/xe_pt.c           | 1018 ++++++++++++++++----------
 drivers/gpu/drm/xe/xe_pt.h           |    7 +
 drivers/gpu/drm/xe/xe_pt_types.h     |   34 +
 drivers/gpu/drm/xe/xe_vm.c           |  533 ++++----------
 drivers/gpu/drm/xe/xe_vm.h           |    5 +-
 drivers/gpu/drm/xe/xe_vm_types.h     |   29 +-
 11 files changed, 969 insertions(+), 1020 deletions(-)
drivers/gpu/drm/xe/xe_bo_types.h +2 -0
index 14ef13b7b421..1ff5b5a68adc 100644
@@ -77,6 +77,8 @@ struct xe_bo {
 	} props;
 	/** @freed: List node for delayed put. */
 	struct llist_node freed;
+	/** @update_index: Update index if PT BO */
+	int update_index;
 	/** @created: Whether the bo has passed initial creation */
 	bool created;
 
drivers/gpu/drm/xe/xe_exec.c +2 -23
index 952496c6260d..4feff67620c4 100644
@@ -294,30 +294,9 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		err = PTR_ERR(rebind_fence);
 		goto err_put_job;
 	}
+	dma_fence_put(rebind_fence);
 
-	/*
-	 * 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)) {
-			dma_fence_put(vm->rebind_fence);
-			vm->rebind_fence = NULL;
-		} else {
-			dma_fence_get(vm->rebind_fence);
-			err = drm_sched_job_add_dependency(&job->drm,
-							   vm->rebind_fence);
-			if (err)
-				goto err_put_job;
-		}
-	}
-
-	/* Wait behind munmap style rebinds */
+	/* Wait for rebinds */
 	if (!xe_vm_in_lr_mode(vm)) {
 		err = drm_sched_job_add_resv_dependencies(&job->drm,
 							  xe_vm_resv(vm),
drivers/gpu/drm/xe/xe_gt_pagefault.c +3 -3
index 878e234166aa..bd8688b2f462 100644
@@ -208,9 +208,9 @@ static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
 
 	/* Bind VMA only to the GT that has faulted */
 	trace_xe_vma_pf_bind(vma);
-	xe_vm_populate_dummy_rebind(vm, vma);
-	vm->dummy_ops.op.tile_mask = BIT(tile->id);
-	vm->dummy_ops.op.q = xe_tile_migrate_exec_queue(tile);
+	xe_vm_populate_dummy_rebind(vm, vma, BIT(tile->id));
+	vm->dummy_ops.vops.pt_update_ops[tile->id].q =
+		xe_tile_migrate_exec_queue(tile);
 	fence = xe_vm_ops_execute(vm, &vm->dummy_ops.vops);
 	if (IS_ERR(fence)) {
 		ret = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_migrate.c +134 -164

409 lines not on this page · raw message

drivers/gpu/drm/xe/xe_migrate.h +20 -12
index 9935ce336bae..bd8eba1d3552 100644
@@ -47,6 +47,24 @@ struct xe_migrate_pt_update_ops {
 			 struct xe_tile *tile, struct iosys_map *map,
 			 void *pos, u32 ofs, u32 num_qwords,
 			 const struct xe_vm_pgtable_update *update);
+	/**
+	 * @clear: Clear a command buffer or page-table with ptes.
+	 * @pt_update: Embeddable callback argument.
+	 * @tile: The tile for the current operation.
+	 * @map: struct iosys_map into the memory to be populated.
+	 * @pos: If @map is NULL, map into the memory to be populated.
+	 * @ofs: qword offset into @map, unused if @map is NULL.
+	 * @num_qwords: Number of qwords to write.
+	 * @update: Information about the PTEs to be inserted.
+	 *
+	 * This interface is intended to be used as a callback into the
+	 * page-table system to populate command buffers or shared
+	 * page-tables with PTEs.
+	 */
+	void (*clear)(struct xe_migrate_pt_update *pt_update,
+		      struct xe_tile *tile, struct iosys_map *map,
+		      void *pos, u32 ofs, u32 num_qwords,
+		      const struct xe_vm_pgtable_update *update);
 
 	/**
 	 * @pre_commit: Callback to be called just before arming the
@@ -67,14 +85,10 @@ struct xe_migrate_pt_update_ops {
 struct xe_migrate_pt_update {
 	/** @ops: Pointer to the struct xe_migrate_pt_update_ops callbacks */
 	const struct xe_migrate_pt_update_ops *ops;
-	/** @vma: The vma we're updating the pagetable for. */
-	struct xe_vma *vma;
+	/** @vops: VMA operations */
+	struct xe_vma_ops *vops;
 	/** @job: The job if a GPU page-table update. NULL otherwise */
 	struct xe_sched_job *job;
-	/** @start: Start of update for the range fence */
-	u64 start;
-	/** @last: Last of update for the range fence */
-	u64 last;
 	/** @tile_id: Tile ID of the update */
 	u8 tile_id;
 };
@@ -96,12 +110,6 @@ struct xe_vm *xe_migrate_get_vm(struct xe_migrate *m);
 
 struct dma_fence *
 xe_migrate_update_pgtables(struct xe_migrate *m,
-			   struct xe_vm *vm,
-			   struct xe_bo *bo,
-			   struct xe_exec_queue *q,
-			   const struct xe_vm_pgtable_update *updates,
-			   u32 num_updates,
-			   struct xe_sync_entry *syncs, u32 num_syncs,
 			   struct xe_migrate_pt_update *pt_update);
 
 void xe_migrate_wait(struct xe_migrate *m);
drivers/gpu/drm/xe/xe_pt.c +631 -387

1,169 lines not on this page · raw message

drivers/gpu/drm/xe/xe_pt.h +7 -0
index 71a4fbfcff43..cbf8170d89cc 100644
@@ -17,6 +17,7 @@ struct xe_sync_entry;
 struct xe_tile;
 struct xe_vm;
 struct xe_vma;
+struct xe_vma_ops;
 
 /* Largest huge pte is currently 1GiB. May become device dependent. */
 #define MAX_HUGEPTE_LEVEL 2
@@ -34,6 +35,12 @@ void xe_pt_populate_empty(struct xe_tile *tile, struct xe_vm *vm,
 
 void xe_pt_destroy(struct xe_pt *pt, u32 flags, struct llist_head *deferred);
 
+int xe_pt_update_ops_prepare(struct xe_tile *tile, struct xe_vma_ops *vops);
+struct dma_fence *xe_pt_update_ops_run(struct xe_tile *tile,
+				       struct xe_vma_ops *vops);
+void xe_pt_update_ops_fini(struct xe_tile *tile, struct xe_vma_ops *vops);
+void xe_pt_update_ops_abort(struct xe_tile *tile, struct xe_vma_ops *vops);
+
 struct dma_fence *
 __xe_pt_bind_vma(struct xe_tile *tile, struct xe_vma *vma, struct xe_exec_queue *q,
 		 struct xe_sync_entry *syncs, u32 num_syncs,
drivers/gpu/drm/xe/xe_pt_types.h +34 -0
index 2093150f461e..16252f1be055 100644
@@ -86,4 +86,38 @@ struct xe_vm_pgtable_update_op {
 	bool rebind;
 };
 
+/** struct xe_vm_pgtable_update_ops: page table update operations */
+struct xe_vm_pgtable_update_ops {
+	/** @ops: operations */
+	struct xe_vm_pgtable_update_op *ops;
+	/** @deferred: deferred list to destroy PT entries */
+	struct llist_head deferred;
+	/** @q: exec queue for PT operations */
+	struct xe_exec_queue *q;
+	/** @start: start address of ops */
+	u64 start;
+	/** @last: last address of ops */
+	u64 last;
+	/** @num_ops: number of operations */
+	u32 num_ops;
+	/** @current_op: current operations */
+	u32 current_op;
+	/** @needs_userptr_lock: Needs userptr lock */
+	bool needs_userptr_lock;
+	/** @needs_invalidation: Needs invalidation */
+	bool needs_invalidation;
+	/**
+	 * @wait_vm_bookkeep: PT operations need to wait until VM is idle
+	 * (bookkeep dma-resv slots are idle) and stage all future VM activity
+	 * behind these operations (install PT operations into VM kernel
+	 * dma-resv slot).
+	 */
+	bool wait_vm_bookkeep;
+	/**
+	 * @wait_vm_kernel: PT operations need to wait until VM kernel dma-resv
+	 * slots are idle.
+	 */
+	bool wait_vm_kernel;
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +127 -406

648 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.h +4 -1
index b40160b1be01..8201ecb8f05a 100644
@@ -262,9 +262,12 @@ static inline struct dma_resv *xe_vm_resv(struct xe_vm *vm)
  */
 #define xe_vm_assert_held(vm) dma_resv_assert_held(xe_vm_resv(vm))
 
-void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma);
+void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma,
+				 u8 tile_mask);
 struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops *vops);
 
+void xe_vm_kill(struct xe_vm *vm, bool unlocked);
+
 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
 #define vm_dbg drm_dbg
 #else
drivers/gpu/drm/xe/xe_vm_types.h +5 -24
index f5d740dcbba3..83cb13275904 100644
@@ -166,29 +166,18 @@ struct xe_vma_op_prefetch {
 
 /** enum xe_vma_op_flags - flags for VMA operation */
 enum xe_vma_op_flags {
-	/** @XE_VMA_OP_FIRST: first VMA operation for a set of syncs */
-	XE_VMA_OP_FIRST			= BIT(0),
-	/** @XE_VMA_OP_LAST: last VMA operation for a set of syncs */
-	XE_VMA_OP_LAST			= BIT(1),
 	/** @XE_VMA_OP_COMMITTED: VMA operation committed */
-	XE_VMA_OP_COMMITTED		= BIT(2),
+	XE_VMA_OP_COMMITTED		= BIT(0),
 	/** @XE_VMA_OP_PREV_COMMITTED: Previous VMA operation committed */
-	XE_VMA_OP_PREV_COMMITTED	= BIT(3),
+	XE_VMA_OP_PREV_COMMITTED	= BIT(1),
 	/** @XE_VMA_OP_NEXT_COMMITTED: Next VMA operation committed */
-	XE_VMA_OP_NEXT_COMMITTED	= BIT(4),
+	XE_VMA_OP_NEXT_COMMITTED	= BIT(2),
 };
 
 /** struct xe_vma_op - VMA operation */
 struct xe_vma_op {
 	/** @base: GPUVA base operation */
 	struct drm_gpuva_op base;
-	/** @q: exec queue for this operation */
-	struct xe_exec_queue *q;
-	/**
-	 * @syncs: syncs for this operation, only used on first and last
-	 * operation
-	 */
-	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
 	/** @link: async operation link */
@@ -214,19 +203,14 @@ struct xe_vma_ops {
 	struct list_head list;
 	/** @vm: VM */
 	struct xe_vm *vm;
-	/** @q: exec queue these operations */
+	/** @q: exec queue for VMA operations */
 	struct xe_exec_queue *q;
 	/** @syncs: syncs these operation */
 	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
 	/** @pt_update_ops: page table update operations */
-	struct {
-		/** @ops: operations */
-		struct xe_vm_pgtable_update_op *ops;
-		/** @num_ops: number of operations */
-		u32 num_ops;
-	} pt_update_ops[XE_MAX_TILES_PER_DEVICE];
+	struct xe_vm_pgtable_update_ops pt_update_ops[XE_MAX_TILES_PER_DEVICE];
 };
 
 struct xe_vm {
@@ -283,9 +267,6 @@ struct xe_vm {
 	 */
 	struct list_head rebind_list;
 
-	/** @rebind_fence: rebind fence from execbuf */
-	struct dma_fence *rebind_fence;
-
 	/**
 	 * @destroy_work: worker to destroy VM, needed as a dma_fence signaling
 	 * from an irq context can be last put and the destroy needs to be able
Signature · 1 line
2.34.1
#55 lore raw

RE: [PATCH v4 18/30] drm/xe: Convert multiple bind ops into single job

Hi Matt,

This patch is very big to review. I wasn't able to look the whole patch. See some comments inline for now. I need to continue it.

Is it possible to divide into small patches?

Quoted 78 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Matthew Brost
Sent: Friday, March 8, 2024 12:08 AM
To: [email protected]
Cc: Brost, Matthew <[email protected]>
Subject: [PATCH v4 18/30] drm/xe: Convert multiple bind ops into single job

This aligns with the uAPI of an array of binds or single bind that results in multiple GPUVA ops to be considered a single atomic operations.

---
 drivers/gpu/drm/xe/xe_bo_types.h     |    2 +
 drivers/gpu/drm/xe/xe_exec.c         |   25 +-
 drivers/gpu/drm/xe/xe_gt_pagefault.c |    6 +-
 drivers/gpu/drm/xe/xe_migrate.c      |  298 ++++----
 drivers/gpu/drm/xe/xe_migrate.h      |   32 +-
 drivers/gpu/drm/xe/xe_pt.c           | 1018 ++++++++++++++++----------
 drivers/gpu/drm/xe/xe_pt.h           |    7 +
 drivers/gpu/drm/xe/xe_pt_types.h     |   34 +
 drivers/gpu/drm/xe/xe_vm.c           |  533 ++++----------
 drivers/gpu/drm/xe/xe_vm.h           |    5 +-
 drivers/gpu/drm/xe/xe_vm_types.h     |   29 +-
 11 files changed, 969 insertions(+), 1020 deletions(-)
drivers/gpu/drm/xe/xe_bo_types.h +2 -0
index 14ef13b7b421..1ff5b5a68adc 100644
@@ -77,6 +77,8 @@ struct xe_bo {
 	} props;
 	/** @freed: List node for delayed put. */
 	struct llist_node freed;
+	/** @update_index: Update index if PT BO */
+	int update_index;
 	/** @created: Whether the bo has passed initial creation */
 	bool created;
 
drivers/gpu/drm/xe/xe_exec.c +2 -23
index 952496c6260d..4feff67620c4 100644
@@ -294,30 +294,9 @@ int xe_exec_ioctl(struct drm_device *dev, void *data,
 struct drm_file *file)
 		err = PTR_ERR(rebind_fence);
 		goto err_put_job;
 	}
+	dma_fence_put(rebind_fence);
 
-	/*
-	 * 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)) {
-			dma_fence_put(vm->rebind_fence);
-			vm->rebind_fence = NULL;
-		} else {
-			dma_fence_get(vm->rebind_fence);
-			err = drm_sched_job_add_dependency(&job->drm,
-							   vm->rebind_fence);
-			if (err)
-				goto err_put_job;
-		}
-	}
-
-	/* Wait behind munmap style rebinds */
+	/* Wait for rebinds */
 	if (!xe_vm_in_lr_mode(vm)) {
 		err = drm_sched_job_add_resv_dependencies(&job->drm,
Quoted 1 line

xe_vm_resv(vm),

Can you explain why you can wait for rebind using above codes? I understand here we make all the fences in vm reservation object a dependency of the job. But did we add rebind_fence to vm's reservation object? I can't find such code in this patch

Quoted 6 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +0 -0
index 878e234166aa..bd8688b2f462 100644

drivers/gpu/drm/xe/xe_gt_pagefault.c static int handle_pagefault(struct xe_gt *gt, struct · line 214

Quoted 10 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +3 -3
@@ -208,9 +208,9 @@ static int handle_pagefault(struct xe_gt *gt, struct
 pagefault *pf)
 
 	/* Bind VMA only to the GT that has faulted */
 	trace_xe_vma_pf_bind(vma);
-	xe_vm_populate_dummy_rebind(vm, vma);
-	vm->dummy_ops.op.tile_mask = BIT(tile->id);
-	vm->dummy_ops.op.q = xe_tile_migrate_exec_queue(tile);
+	xe_vm_populate_dummy_rebind(vm, vma, BIT(tile->id));
+	vm->dummy_ops.vops.pt_update_ops[tile->id].q =
+		xe_tile_migrate_exec_queue(tile);

I guess I will ignore all those dummy_rebind/dummy_ops code for now.

Quoted 168 lines
drivers/gpu/drm/xe/xe_gt_pagefault.c +0 -0
 	fence = xe_vm_ops_execute(vm, &vm->dummy_ops.vops);
 	if (IS_ERR(fence)) {
drivers/gpu/drm/xe/xe_gt_pagefault.c +0 -0
 		ret = PTR_ERR(fence);
drivers/gpu/drm/xe/xe_migrate.c +1 -0
index 3fd76f912fda..cc0499f3702c 100644
@@ -1106,6 +1106,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate
 *m,
 }
 
 static void write_pgtable(struct xe_tile *tile, struct xe_bb *bb, u64 ppgtt_ofs,
+			  const struct xe_vm_pgtable_update_op *pt_op,
 			  const struct xe_vm_pgtable_update *update,
 			  struct xe_migrate_pt_update *pt_update)

{

drivers/gpu/drm/xe/xe_migrate.c +6 -2
@@ -1140,8 +1141,12 @@ static void write_pgtable(struct xe_tile *tile, struct
 xe_bb *bb, u64 ppgtt_ofs,
 		bb->cs[bb->len++] = MI_STORE_DATA_IMM |
 MI_SDI_NUM_QW(chunk);
 		bb->cs[bb->len++] = lower_32_bits(addr);
 		bb->cs[bb->len++] = upper_32_bits(addr);
-		ops->populate(pt_update, tile, NULL, bb->cs + bb->len, ofs,
 chunk,
-			      update);
+		if (pt_op->bind)
+			ops->populate(pt_update, tile, NULL, bb->cs + bb->len,
+				      ofs, chunk, update);
+		else
+			ops->clear(pt_update, tile, NULL, bb->cs + bb->len,
+				   ofs, chunk, update);
drivers/gpu/drm/xe/xe_migrate.c +25 -54
 		bb->len += chunk * 2;
 		ofs += chunk;
@@ -1166,114 +1171,58 @@ struct migrate_test_params {
 
 static struct dma_fence *
 xe_migrate_update_pgtables_cpu(struct xe_migrate *m,
-			       struct xe_vm *vm, struct xe_bo *bo,
-			       const struct  xe_vm_pgtable_update *updates,
-			       u32 num_updates, bool wait_vm,
 			       struct xe_migrate_pt_update *pt_update)
 {
 	XE_TEST_DECLARE(struct migrate_test_params *test =
 			to_migrate_test_params
 			(xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));)
 	const struct xe_migrate_pt_update_ops *ops = pt_update->ops;
-	struct dma_fence *fence;
+	struct xe_vm *vm = pt_update->vops->vm;
+	struct xe_vm_pgtable_update_ops *pt_update_ops =
+		&pt_update->vops->pt_update_ops[pt_update->tile_id];
 	int err;
-	u32 i;
+	u32 j, i;
 
 	if (XE_TEST_ONLY(test && test->force_gpu))
 		return ERR_PTR(-ETIME);
 
-	if (bo && !dma_resv_test_signaled(bo->ttm.base.resv,
-					  DMA_RESV_USAGE_KERNEL))
-		return ERR_PTR(-ETIME);
-
-	if (wait_vm && !dma_resv_test_signaled(xe_vm_resv(vm),
-					       DMA_RESV_USAGE_BOOKKEEP))
-		return ERR_PTR(-ETIME);
-
 	if (ops->pre_commit) {
 		pt_update->job = NULL;
 		err = ops->pre_commit(pt_update);
 		if (err)
 			return ERR_PTR(err);
 	}
-	for (i = 0; i < num_updates; i++) {
-		const struct xe_vm_pgtable_update *update = &updates[i];
-
-		ops->populate(pt_update, m->tile, &update->pt_bo->vmap,
 NULL,
-			      update->ofs, update->qwords, update);
-	}
-
-	if (vm) {
-		trace_xe_vm_cpu_bind(vm);
-		xe_device_wmb(vm->xe);
-	}
-
-	fence = dma_fence_get_stub();
-
-	return fence;
-}
-
-static bool no_in_syncs(struct xe_vm *vm, struct xe_exec_queue *q,
-			struct xe_sync_entry *syncs, u32 num_syncs)
-{
-	struct dma_fence *fence;
-	int i;
 
-	for (i = 0; i < num_syncs; i++) {
-		fence = syncs[i].fence;
-
-		if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				       &fence->flags))
-			return false;
-	}
-	if (q) {
-		fence = xe_exec_queue_last_fence_get(q, vm);
-		if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
-			dma_fence_put(fence);
-			return false;
+	for (j = 0; j < pt_update_ops->num_ops; ++j) {
+		const struct xe_vm_pgtable_update_op *pt_op =
+			&pt_update_ops->ops[j];
+
+		for (i = 0; i < pt_op->num_entries; i++) {
+			const struct xe_vm_pgtable_update *update =
+				&pt_op->entries[i];
+
+			if (pt_op->bind)
+				ops->populate(pt_update, m->tile,
+					      &update->pt_bo->vmap, NULL,
+					      update->ofs, update->qwords,
+					      update);
+			else
+				ops->clear(pt_update, m->tile,
+					   &update->pt_bo->vmap, NULL,
+					   update->ofs, update->qwords, update);
 		}
-		dma_fence_put(fence);
 	}
 
-	return true;
+	trace_xe_vm_cpu_bind(vm);
+	xe_device_wmb(vm->xe);
+
+	return dma_fence_get_stub();
 }
 
-/**
- * xe_migrate_update_pgtables() - Pipelined page-table update
- * @m: The migrate context.
- * @vm: The vm we'll be updating.
- * @bo: The bo whose dma-resv we will await before updating, or NULL if

userptr.
- * @q: The exec queue to be used for the update or NULL if the default
- * migration engine is to be used.
- * @updates: An array of update descriptors.
- * @num_updates: Number of descriptors in @updates.
- * @syncs: Array of xe_sync_entry to await before updating. Note that waits
- * will block the engine timeline.
- * @num_syncs: Number of entries in @syncs.
- * @pt_update: Pointer to a struct xe_migrate_pt_update, which contains
- * pointers to callback functions and, if subclassed, private arguments to
- * those.
- *
- * Perform a pipelined page-table update. The update descriptors are typically
- * built under the same lock critical section as a call to this function. If
- * using the default engine for the updates, they will be performed in the
- * order they grab the job_mutex. If different engines are used, external
- * synchronization is needed for overlapping updates to maintain page-table
- * consistency. Note that the meaing of "overlapping" is that the updates
- * touch the same page-table, which might be a higher-level page-directory.
- * If no pipelining is needed, then updates may be performed by the cpu.
- *
- * Return: A dma_fence that, when signaled, indicates the update completion.
- */

drivers/gpu/drm/xe/xe_migrate.c +0 -3
-struct dma_fence *
-xe_migrate_update_pgtables(struct xe_migrate *m,
-			   struct xe_vm *vm,

drivers/gpu/drm/xe/xe_migrate.c struct migrate_test_params { · line 1225

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +4 -6
-			   struct xe_bo *bo,
-			   struct xe_exec_queue *q,
-			   const struct xe_vm_pgtable_update *updates,
-			   u32 num_updates,
-			   struct xe_sync_entry *syncs, u32 num_syncs,
-			   struct xe_migrate_pt_update *pt_update)
+static struct dma_fence *
+__xe_migrate_update_pgtables(struct xe_migrate *m,
+			     struct xe_migrate_pt_update *pt_update,
+			     struct xe_vm_pgtable_update_ops *pt_update_ops)

Maybe rename this function to _gpu?

Quoted 250 lines

{

drivers/gpu/drm/xe/xe_migrate.c +21 -35
 	const struct xe_migrate_pt_update_ops *ops = pt_update->ops;
 	struct xe_tile *tile = m->tile;
@@ -1282,59 +1231,45 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	struct xe_sched_job *job;
 	struct dma_fence *fence;
 	struct drm_suballoc *sa_bo = NULL;
-	struct xe_vma *vma = pt_update->vma;
 	struct xe_bb *bb;
-	u32 i, batch_size, ppgtt_ofs, update_idx, page_ofs = 0;
+	u32 i, j, batch_size = 0, ppgtt_ofs, update_idx, page_ofs = 0;
+	u32 num_updates = 0, current_update = 0;
 	u64 addr;
 	int err = 0;
-	bool usm = !q && xe->info.has_usm;
-	bool first_munmap_rebind = vma &&
-		vma->gpuva.flags & XE_VMA_FIRST_REBIND;
-	struct xe_exec_queue *q_override = !q ? m->q : q;
-	u16 pat_index = xe->pat.idx[XE_CACHE_WB];
+	bool is_migrate = pt_update_ops->q == m->q;
+	bool usm = is_migrate && xe->info.has_usm;
 
-	/* Use the CPU if no in syncs and engine is idle */
-	if (no_in_syncs(vm, q, syncs, num_syncs) &&
 xe_exec_queue_is_idle(q_override)) {
-		fence =  xe_migrate_update_pgtables_cpu(m, vm, bo, updates,
-							num_updates,
-							first_munmap_rebind,
-							pt_update);
-		if (!IS_ERR(fence) || fence == ERR_PTR(-EAGAIN))
-			return fence;
+	for (i = 0; i < pt_update_ops->num_ops; ++i) {
+		struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops-
 >ops[i];
+		struct xe_vm_pgtable_update *updates = pt_op->entries;
+
+		num_updates += pt_op->num_entries;
+		for (j = 0; j < pt_op->num_entries; ++j) {
+			u32 num_cmds = DIV_ROUND_UP(updates[j].qwords,
 0x1ff);
+
+			/* align noop + MI_STORE_DATA_IMM cmd prefix */
+			batch_size += 4 * num_cmds + updates[j].qwords * 2;
+		}
 	}
 
 	/* fixed + PTE entries */
 	if (IS_DGFX(xe))
-		batch_size = 2;
+		batch_size += 2;
 	else
-		batch_size = 6 + num_updates * 2;
+		batch_size += 6 + num_updates * 2;
 
-	for (i = 0; i < num_updates; i++) {
-		u32 num_cmds = DIV_ROUND_UP(updates[i].qwords,
 MAX_PTE_PER_SDI);
-
-		/* align noop + MI_STORE_DATA_IMM cmd prefix */
-		batch_size += 4 * num_cmds + updates[i].qwords * 2;
-	}
-
-	/*
-	 * XXX: Create temp bo to copy from, if batch_size becomes too big?
-	 *
-	 * Worst case: Sum(2 * (each lower level page size) + (top level page size))
-	 * Should be reasonably bound..
-	 */
-	xe_tile_assert(tile, batch_size < SZ_128K);
-
-	bb = xe_bb_new(gt, batch_size, !q && xe->info.has_usm);
+	bb = xe_bb_new(gt, batch_size, usm);
 	if (IS_ERR(bb))
 		return ERR_CAST(bb);
 
 	/* For sysmem PTE's, need to map them in our hole.. */
 	if (!IS_DGFX(xe)) {
 		ppgtt_ofs = NUM_KERNEL_PDE - 1;
-		if (q) {
-			xe_tile_assert(tile, num_updates <=
 NUM_VMUSA_WRITES_PER_UNIT);
+		if (!is_migrate) {
+			u32 num_units = DIV_ROUND_UP(num_updates,
+

NUM_VMUSA_WRITES_PER_UNIT);

drivers/gpu/drm/xe/xe_migrate.c +1 -1
-			sa_bo = drm_suballoc_new(&m->vm_update_sa, 1,
+			sa_bo = drm_suballoc_new(&m->vm_update_sa,

num_units,

drivers/gpu/drm/xe/xe_migrate.c +15 -6
 						 GFP_KERNEL, true, 0);
 			if (IS_ERR(sa_bo)) {
 				err = PTR_ERR(sa_bo);
@@ -1354,14 +1289,26 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 		bb->cs[bb->len++] = ppgtt_ofs * XE_PAGE_SIZE + page_ofs;
 		bb->cs[bb->len++] = 0; /* upper_32_bits */
 
-		for (i = 0; i < num_updates; i++) {
-			struct xe_bo *pt_bo = updates[i].pt_bo;
+		for (i = 0; i < pt_update_ops->num_ops; ++i) {
+			struct xe_vm_pgtable_update_op *pt_op =
+				&pt_update_ops->ops[i];
+			struct xe_vm_pgtable_update *updates = pt_op-
 >entries;
 
-			xe_tile_assert(tile, pt_bo->size == SZ_4K);
+			for (j = 0; j < pt_op->num_entries; ++j, ++current_update)
 {
+				struct xe_vm *vm = pt_update->vops->vm;
+				struct xe_bo *pt_bo = updates[j].pt_bo;
 
-			addr = vm->pt_ops->pte_encode_bo(pt_bo, 0,
 pat_index, 0);
-			bb->cs[bb->len++] = lower_32_bits(addr);
-			bb->cs[bb->len++] = upper_32_bits(addr);
+				xe_tile_assert(tile, pt_bo->size == SZ_4K);
+
+				/* Map a PT at most once */
+				if (pt_bo->update_index < 0)
+					pt_bo->update_index = current_update;
+
+				addr = vm->pt_ops->pte_encode_bo(pt_bo, 0,
+								 XE_CACHE_WB,

0);

drivers/gpu/drm/xe/xe_migrate.c +27 -7
+				bb->cs[bb->len++] = lower_32_bits(addr);
+				bb->cs[bb->len++] = upper_32_bits(addr);
+			}
 		}
 
 		bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
@@ -1369,22 +1316,39 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 
 		addr = xe_migrate_vm_addr(ppgtt_ofs, 0) +
 			(page_ofs / sizeof(u64)) * XE_PAGE_SIZE;
-		for (i = 0; i < num_updates; i++)
-			write_pgtable(tile, bb, addr + i * XE_PAGE_SIZE,
-				      &updates[i], pt_update);
+		for (i = 0; i < pt_update_ops->num_ops; ++i) {
+			struct xe_vm_pgtable_update_op *pt_op =
+				&pt_update_ops->ops[i];
+			struct xe_vm_pgtable_update *updates = pt_op-
 >entries;
+
+			for (j = 0; j < pt_op->num_entries; ++j) {
+				struct xe_bo *pt_bo = updates[j].pt_bo;
+
+				write_pgtable(tile, bb, addr +
+					      pt_bo->update_index *
 XE_PAGE_SIZE,
+					      pt_op, &updates[j], pt_update);
+			}
+		}
 	} else {
 		/* phys pages, no preamble required */
 		bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
 		update_idx = bb->len;
 
-		for (i = 0; i < num_updates; i++)
-			write_pgtable(tile, bb, 0, &updates[i], pt_update);
+		for (i = 0; i < pt_update_ops->num_ops; ++i) {
+			struct xe_vm_pgtable_update_op *pt_op =
+				&pt_update_ops->ops[i];
+			struct xe_vm_pgtable_update *updates = pt_op-
 >entries;
+
+			for (j = 0; j < pt_op->num_entries; ++j)
+				write_pgtable(tile, bb, 0, pt_op, &updates[j],
+					      pt_update);
+		}
 	}
 
-	if (!q)
+	if (is_migrate)
 		mutex_lock(&m->job_mutex);
 
-	job = xe_bb_create_migration_job(q ?: m->q, bb,
+	job = xe_bb_create_migration_job(pt_update_ops->q, bb,
drivers/gpu/drm/xe/xe_migrate.c +18 -28
 					 xe_migrate_batch_base(m, usm),
 					 update_idx);
 	if (IS_ERR(job)) {
@@ -1392,32 +1356,6 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 		goto err_bb;
 	}
 
-	/* Wait on BO move */
-	if (bo) {
-		err = job_add_deps(job, bo->ttm.base.resv,
-				   DMA_RESV_USAGE_KERNEL);
-		if (err)
-			goto err_job;
-	}
-
-	/*
-	 * Munmap style VM unbind, need to wait for all jobs to be complete /
-	 * trigger preempts before moving forward
-	 */
-	if (first_munmap_rebind) {
-		err = job_add_deps(job, xe_vm_resv(vm),
-				   DMA_RESV_USAGE_BOOKKEEP);
-		if (err)
-			goto err_job;
-	}
-
-	err = xe_sched_job_last_fence_add_dep(job, vm);
-	for (i = 0; !err && i < num_syncs; i++)
-		err = xe_sync_entry_add_deps(&syncs[i], job);
-
-	if (err)
-		goto err_job;
-
 	if (ops->pre_commit) {
 		pt_update->job = job;
 		err = ops->pre_commit(pt_update);
@@ -1428,7 +1366,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	fence = dma_fence_get(&job->drm.s_fence->finished);
 	xe_sched_job_push(job);
 
-	if (!q)
+	if (is_migrate)
 		mutex_unlock(&m->job_mutex);
 
 	xe_bb_free(bb, fence);
@@ -1439,7 +1377,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 err_job:
 	xe_sched_job_put(job);
 err_bb:
-	if (!q)
+	if (is_migrate)
 		mutex_unlock(&m->job_mutex);
 	xe_bb_free(bb, NULL);
 err:
@@ -1447,6 +1385,38 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	return ERR_PTR(err);
 }
 
+/**
+ * xe_migrate_update_pgtables() - Pipelined page-table update
+ * @m: The migrate context.
+ * @pt_update: PT update arguments
+ *
+ * Perform a pipelined page-table update. The update descriptors are typically
+ * built under the same lock critical section as a call to this function. If
+ * using the default engine for the updates, they will be performed in the
+ * order they grab the job_mutex. If different engines are used, external
+ * synchronization is needed for overlapping updates to maintain page-table
+ * consistency. Note that the meaing of "overlapping" is that the updates
+ * touch the same page-table, which might be a higher-level page-directory.
+ * If no pipelining is needed, then updates may be performed by the cpu.
+ *
+ * Return: A dma_fence that, when signaled, indicates the update completion.
+ */

drivers/gpu/drm/xe/xe_migrate.c xe_migrate_update_pgtables(struct xe_migrate *m, · line 1413

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +10 -0
+struct dma_fence *
+xe_migrate_update_pgtables(struct xe_migrate *m,
+			   struct xe_migrate_pt_update *pt_update)
+
+{
+	struct xe_vm_pgtable_update_ops *pt_update_ops =
+		&pt_update->vops->pt_update_ops[pt_update->tile_id];
+	struct dma_fence *fence;
+
+	fence =  xe_migrate_update_pgtables_cpu(m, pt_update);

So now by default you use cpu to update page table first. Only if there is error you fall back to gpu page table update. Should we also consider performance here? For example, consider page table is usually in vram, isn't cpu writing to page table is usually slower than gpu writing?

Quoted 194 lines
drivers/gpu/drm/xe/xe_migrate.c +6 -0
+	if (!IS_ERR(fence))
+		return fence;
+
+	return __xe_migrate_update_pgtables(m, pt_update, pt_update_ops);
+}
+
 /**
  * xe_migrate_wait() - Complete all operations using the xe_migrate context
  * @m: Migrate context to wait for.
drivers/gpu/drm/xe/xe_migrate.h +20 -12
index 9935ce336bae..bd8eba1d3552 100644
@@ -47,6 +47,24 @@ struct xe_migrate_pt_update_ops {
 			 struct xe_tile *tile, struct iosys_map *map,
 			 void *pos, u32 ofs, u32 num_qwords,
 			 const struct xe_vm_pgtable_update *update);
+	/**
+	 * @clear: Clear a command buffer or page-table with ptes.
+	 * @pt_update: Embeddable callback argument.
+	 * @tile: The tile for the current operation.
+	 * @map: struct iosys_map into the memory to be populated.
+	 * @pos: If @map is NULL, map into the memory to be populated.
+	 * @ofs: qword offset into @map, unused if @map is NULL.
+	 * @num_qwords: Number of qwords to write.
+	 * @update: Information about the PTEs to be inserted.
+	 *
+	 * This interface is intended to be used as a callback into the
+	 * page-table system to populate command buffers or shared
+	 * page-tables with PTEs.
+	 */
+	void (*clear)(struct xe_migrate_pt_update *pt_update,
+		      struct xe_tile *tile, struct iosys_map *map,
+		      void *pos, u32 ofs, u32 num_qwords,
+		      const struct xe_vm_pgtable_update *update);
 
 	/**
 	 * @pre_commit: Callback to be called just before arming the
@@ -67,14 +85,10 @@ struct xe_migrate_pt_update_ops {
 struct xe_migrate_pt_update {
 	/** @ops: Pointer to the struct xe_migrate_pt_update_ops callbacks */
 	const struct xe_migrate_pt_update_ops *ops;
-	/** @vma: The vma we're updating the pagetable for. */
-	struct xe_vma *vma;
+	/** @vops: VMA operations */
+	struct xe_vma_ops *vops;
 	/** @job: The job if a GPU page-table update. NULL otherwise */
 	struct xe_sched_job *job;
-	/** @start: Start of update for the range fence */
-	u64 start;
-	/** @last: Last of update for the range fence */
-	u64 last;
 	/** @tile_id: Tile ID of the update */
 	u8 tile_id;
 };
@@ -96,12 +110,6 @@ struct xe_vm *xe_migrate_get_vm(struct xe_migrate
 *m);
 
 struct dma_fence *
 xe_migrate_update_pgtables(struct xe_migrate *m,
-			   struct xe_vm *vm,
-			   struct xe_bo *bo,
-			   struct xe_exec_queue *q,
-			   const struct xe_vm_pgtable_update *updates,
-			   u32 num_updates,
-			   struct xe_sync_entry *syncs, u32 num_syncs,
 			   struct xe_migrate_pt_update *pt_update);
 

void xe_migrate_wait(struct xe_migrate *m);

drivers/gpu/drm/xe/xe_pt.c +3 -0
index a878e2217c7f..5a8523d0a049 100644
@@ -8,12 +8,14 @@
 #include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_drm_client.h"
+#include "xe_exec_queue.h"
 #include "xe_gt.h"
 #include "xe_gt_tlb_invalidation.h"
 #include "xe_migrate.h"
 #include "xe_pt_types.h"
 #include "xe_pt_walk.h"
 #include "xe_res_cursor.h"
+#include "xe_sync.h"
 #include "xe_trace.h"
 #include "xe_ttm_stolen_mgr.h"
 #include "xe_vm.h"
@@ -324,6 +326,7 @@ xe_pt_new_shared(struct xe_walk_update *wupd, struct
 xe_pt *parent,
 	entry->pt = parent;
 	entry->flags = 0;
 	entry->qwords = 0;
+	entry->pt_bo->update_index = -1;
 
 	if (alloc_entries) {
drivers/gpu/drm/xe/xe_pt.c +1 -3
 		entry->pt_entries = kmalloc_array(XE_PDES,
@@ -831,9 +834,7 @@ static void xe_pt_commit_locks_assert(struct xe_vma
 *vma)
 
 	lockdep_assert_held(&vm->lock);
 
-	if (xe_vma_is_userptr(vma))
-		lockdep_assert_held_read(&vm->userptr.notifier_lock);
-	else if (!xe_vma_is_null(vma))
+	if (!xe_vma_is_userptr(vma) && !xe_vma_is_null(vma))
 		dma_resv_assert_held(xe_vma_bo(vma)->ttm.base.resv);
 
drivers/gpu/drm/xe/xe_pt.c +13 -6
 	xe_vm_assert_held(vm);
@@ -855,10 +856,8 @@ static void xe_pt_commit_bind(struct xe_vma *vma,
 		if (!rebind)
 			pt->num_live += entries[i].qwords;
 
-		if (!pt->level) {
-			kfree(entries[i].pt_entries);
+		if (!pt->level)
 			continue;
-		}
 
 		pt_dir = as_xe_pt_dir(pt);
 		for (j = 0; j < entries[i].qwords; j++) {
@@ -871,10 +870,18 @@ static void xe_pt_commit_bind(struct xe_vma *vma,
 
 			pt_dir->children[j_] = &newpte->base;
 		}
-		kfree(entries[i].pt_entries);
 	}
 }
 
+static void xe_pt_free_bind(struct xe_vm_pgtable_update *entries,
+			    u32 num_entries)
+{
+	u32 i;
+
+	for (i = 0; i < num_entries; i++)
+		kfree(entries[i].pt_entries);
+}
+
 static int
 xe_pt_prepare_bind(struct xe_tile *tile, struct xe_vma *vma,
 		   struct xe_vm_pgtable_update *entries, u32 *num_entries)
@@ -893,12 +900,13 @@ xe_pt_prepare_bind(struct xe_tile *tile, struct xe_vma
 *vma,
 
 static void xe_vm_dbg_print_entries(struct xe_device *xe,
 				    const struct xe_vm_pgtable_update *entries,
-				    unsigned int num_entries)
+				    unsigned int num_entries, bool bind)
 #if (IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM))
 {
 	unsigned int i;
 
-	vm_dbg(&xe->drm, "%u entries to update\n", num_entries);
+	vm_dbg(&xe->drm, "%s: %u entries to update\n", bind ? "bind" :
 "unbind",
+	       num_entries);
 	for (i = 0; i < num_entries; i++) {
drivers/gpu/drm/xe/xe_pt.c +12 -12
 		const struct xe_vm_pgtable_update *entry = &entries[i];
 		struct xe_pt *xe_pt = entry->pt;
@@ -919,66 +927,122 @@ static void xe_vm_dbg_print_entries(struct xe_device
 *xe,
 {}
 #endif
 
-#ifdef CONFIG_DRM_XE_USERPTR_INVAL_INJECT
+static int job_add_deps(struct xe_sched_job *job, struct dma_resv *resv,
+			enum dma_resv_usage usage)
+{
+	return drm_sched_job_add_resv_dependencies(&job->drm, resv,
 usage);
+}
 
-static int xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
+static bool no_in_syncs(struct xe_sync_entry *syncs, u32 num_syncs)
 {
-	u32 divisor = uvma->userptr.divisor ? uvma->userptr.divisor : 2;
-	static u32 count;
+	int i;
 
-	if (count++ % divisor == divisor - 1) {
-		struct xe_vm *vm = xe_vma_vm(&uvma->vma);
+	for (i = 0; i < num_syncs; i++) {
+		struct dma_fence *fence = syncs[i].fence;
 
-		uvma->userptr.divisor = divisor << 1;
-		spin_lock(&vm->userptr.invalidated_lock);
-		list_move_tail(&uvma->userptr.invalidate_link,
-			       &vm->userptr.invalidated);
-		spin_unlock(&vm->userptr.invalidated_lock);
-		return true;
+		if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
+				       &fence->flags))
+			return false;

drivers/gpu/drm/xe/xe_pt.c static void xe_vm_dbg_print_entries(struct xe_device · line 953

Quoted 10 lines
drivers/gpu/drm/xe/xe_pt.c +2 -4
 	}
 
-	return false;
+	return true;
 }
 
-#else
-
-static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
+static int vma_add_deps(struct xe_vma *vma, struct xe_sched_job *job)

From the implementation, it seems this is adding vma as a job's dependency...but the function name says add dependency to vma...

Quoted 222 lines
drivers/gpu/drm/xe/xe_pt.c +62 -26
 {
-	return false;
+	struct xe_bo *bo = xe_vma_bo(vma);
+
+	xe_bo_assert_held(bo);
+
+	if (bo && !bo->vm) {
+		if (!job) {
+			if (!dma_resv_test_signaled(bo->ttm.base.resv,
+						    DMA_RESV_USAGE_KERNEL))
+				return -ETIME;
+		} else {
+			return job_add_deps(job, bo->ttm.base.resv,
+					    DMA_RESV_USAGE_KERNEL);
+		}
+	}
+
+	return 0;
 }
 
-#endif
+static int op_add_deps(struct xe_vm *vm, struct xe_vma_op *op,
+		       struct xe_sched_job *job)
+{
+	int err = 0;
 
-/**
- * struct xe_pt_migrate_pt_update - Callback argument for pre-commit callbacks
- * @base: Base we derive from.
- * @bind: Whether this is a bind or an unbind operation. A bind operation
- *        makes the pre-commit callback error with -EAGAIN if it detects a
- *        pending invalidation.
- * @locked: Whether the pre-commit callback locked the userptr notifier lock
- *          and it needs unlocking.
- */
-struct xe_pt_migrate_pt_update {
-	struct xe_migrate_pt_update base;
-	bool bind;
-	bool locked;
-};
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		if (!op->map.immediate && xe_vm_in_fault_mode(vm))
+			break;
+
+		err = vma_add_deps(op->map.vma, job);
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		if (op->remap.prev)
+			err = vma_add_deps(op->remap.prev, job);
+		if (!err && op->remap.next)
+			err = vma_add_deps(op->remap.next, job);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = vma_add_deps(gpuva_to_vma(op->base.prefetch.va), job);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
 
-/*
- * This function adds the needed dependencies to a page-table update job
- * to make sure racing jobs for separate bind engines don't race writing
- * to the same page-table range, wreaking havoc. Initially use a single
- * fence for the entire VM. An optimization would use smaller granularity.
- */
 static int xe_pt_vm_dependencies(struct xe_sched_job *job,
-				 struct xe_range_fence_tree *rftree,
-				 u64 start, u64 last)
+				 struct xe_vm *vm,
+				 struct xe_vma_ops *vops,
+				 struct xe_vm_pgtable_update_ops
 *pt_update_ops,
+				 struct xe_range_fence_tree *rftree)
 {
 	struct xe_range_fence *rtfence;
 	struct dma_fence *fence;
-	int err;
+	struct xe_vma_op *op;
+	int err = 0, i;
+
+	xe_vm_assert_held(vm);
+
+	if (!job && !no_in_syncs(vops->syncs, vops->num_syncs))
+		return -ETIME;
+
+	if (!job && !xe_exec_queue_is_idle(pt_update_ops->q))
+		return -ETIME;
 
-	rtfence = xe_range_fence_tree_first(rftree, start, last);
+	if (pt_update_ops->wait_vm_bookkeep) {
+		if (!job) {
+			if (!dma_resv_test_signaled(xe_vm_resv(vm),
+

DMA_RESV_USAGE_BOOKKEEP))

drivers/gpu/drm/xe/xe_pt.c +68 -32
+				return -ETIME;
+		} else {
+			err = job_add_deps(job, xe_vm_resv(vm),
+					   DMA_RESV_USAGE_BOOKKEEP);
+			if (err)
+				return err;
+		}
+	} else if (pt_update_ops->wait_vm_kernel) {
+		if (!job) {
+			if (!dma_resv_test_signaled(xe_vm_resv(vm),
+						    DMA_RESV_USAGE_KERNEL))
+				return -ETIME;
+		} else {
+			err = job_add_deps(job, xe_vm_resv(vm),
+					   DMA_RESV_USAGE_KERNEL);
+			if (err)
+				return err;
+		}
+	}
+
+	rtfence = xe_range_fence_tree_first(rftree, pt_update_ops->start,
+					    pt_update_ops->last);
 	while (rtfence) {
 		fence = rtfence->fence;
@@ -996,80 +1060,136 @@ static int xe_pt_vm_dependencies(struct
 xe_sched_job *job,
 				return err;
 		}
 
-		rtfence = xe_range_fence_tree_next(rtfence, start, last);
+		rtfence = xe_range_fence_tree_next(rtfence,
+						   pt_update_ops->start,
+						   pt_update_ops->last);
 	}
 
-	return 0;
+	list_for_each_entry(op, &vops->list, link) {
+		err = op_add_deps(vm, op, job);
+		if (err)
+			return err;
+	}
+
+	for (i = 0; job && !err && i < vops->num_syncs; i++)
+		err = xe_sync_entry_add_deps(&vops->syncs[i], job);
+
+	return err;
 }
 
 static int xe_pt_pre_commit(struct xe_migrate_pt_update *pt_update)
 {
-	struct xe_range_fence_tree *rftree =
-		&xe_vma_vm(pt_update->vma)->rftree[pt_update->tile_id];
+	struct xe_vma_ops *vops = pt_update->vops;
+	struct xe_vm *vm = vops->vm;
+	struct xe_range_fence_tree *rftree = &vm->rftree[pt_update->tile_id];
+	struct xe_vm_pgtable_update_ops *pt_update_ops =
+		&vops->pt_update_ops[pt_update->tile_id];
+
+	return xe_pt_vm_dependencies(pt_update->job, vm, pt_update->vops,
+				     pt_update_ops, rftree);
+}
+
+#ifdef CONFIG_DRM_XE_USERPTR_INVAL_INJECT
+
+static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
+{
+	u32 divisor = uvma->userptr.divisor ? uvma->userptr.divisor : 2;
+	static u32 count;
+
+	if (count++ % divisor == divisor - 1) {
+		uvma->userptr.divisor = divisor << 1;
+		return true;
+	}
 
-	return xe_pt_vm_dependencies(pt_update->job, rftree,
-				     pt_update->start, pt_update->last);
+	return false;
 }
 
-static int xe_pt_userptr_pre_commit(struct xe_migrate_pt_update *pt_update)
+#else
+
+static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
 {
-	struct xe_pt_migrate_pt_update *userptr_update =
-		container_of(pt_update, typeof(*userptr_update), base);
-	struct xe_userptr_vma *uvma = to_userptr_vma(pt_update->vma);
-	unsigned long notifier_seq = uvma->userptr.notifier_seq;
-	struct xe_vm *vm = xe_vma_vm(&uvma->vma);
-	int err = xe_pt_vm_dependencies(pt_update->job,
-					&vm->rftree[pt_update->tile_id],
-					pt_update->start,
-					pt_update->last);
+	return false;
+}
 
-	if (err)
-		return err;
+#endif
 
-	userptr_update->locked = false;
+static void vma_check_userptr(struct xe_vm *vm, struct xe_vma *vma)
+{
+	struct xe_userptr_vma *uvma = to_userptr_vma(vma);
+	unsigned long notifier_seq = uvma->userptr.notifier_seq;
 
-	/*
-	 * Wait until nobody is running the invalidation notifier, and
-	 * since we're exiting the loop holding the notifier lock,
-	 * nobody can proceed invalidating either.
-	 *
-	 * Note that we don't update the vma->userptr.notifier_seq since
-	 * we don't update the userptr pages.
-	 */
-	do {
-		down_read(&vm->userptr.notifier_lock);
-		if (!mmu_interval_read_retry(&uvma->userptr.notifier,
-					     notifier_seq))
-			break;
+	lockdep_assert_held_read(&vm->userptr.notifier_lock);
 

drivers/gpu/drm/xe/xe_pt.c static int xe_pt_vm_dependencies(struct · line 1130

Quoted 10 lines
drivers/gpu/drm/xe/xe_pt.c +6 -3
-		up_read(&vm->userptr.notifier_lock);
+	if (uvma->userptr.initial_bind || xe_vm_in_fault_mode(vm))
+		return;
 
-		if (userptr_update->bind)
-			return -EAGAIN;
+	if (!mmu_interval_read_retry(&uvma->userptr.notifier,
+				     notifier_seq) &&
+	    !xe_pt_userptr_inject_eagain(uvma))
+		return;

After this, we need to retry the get_user_pages_fast thing but I don't see how this is achieved...

Oak

Quoted 1,716 lines
drivers/gpu/drm/xe/xe_pt.c +55 -16
-		notifier_seq = mmu_interval_read_begin(&uvma-
 >userptr.notifier);
-	} while (true);
+	spin_lock(&vm->userptr.invalidated_lock);
+	list_move_tail(&uvma->userptr.invalidate_link,
+		       &vm->userptr.invalidated);
+	spin_unlock(&vm->userptr.invalidated_lock);
 
-	/* Inject errors to test_whether they are handled correctly */
-	if (userptr_update->bind && xe_pt_userptr_inject_eagain(uvma)) {
-		up_read(&vm->userptr.notifier_lock);
-		return -EAGAIN;
+	if (xe_vm_in_preempt_fence_mode(vm)) {
+		struct dma_resv_iter cursor;
+		struct dma_fence *fence;
+
+		dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
+				    DMA_RESV_USAGE_BOOKKEEP);
+		dma_resv_for_each_fence_unlocked(&cursor, fence)
+			dma_fence_enable_sw_signaling(fence);
+		dma_resv_iter_end(&cursor);
 	}
+}
 
-	userptr_update->locked = true;
+static void op_check_userptr(struct xe_vm *vm, struct xe_vma_op *op)
+{
+	lockdep_assert_held_read(&vm->userptr.notifier_lock);
 
-	return 0;
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		if (!op->map.immediate && xe_vm_in_fault_mode(vm))
+			break;
+
+		vma_check_userptr(vm, op->map.vma);
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		if (op->remap.prev)
+			vma_check_userptr(vm, op->remap.prev);
+		if (op->remap.next)
+			vma_check_userptr(vm, op->remap.next);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		vma_check_userptr(vm, gpuva_to_vma(op->base.prefetch.va));
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
 }
 
-static const struct xe_migrate_pt_update_ops bind_ops = {
-	.populate = xe_vm_populate_pgtable,
-	.pre_commit = xe_pt_pre_commit,
-};
+static int xe_pt_userptr_pre_commit(struct xe_migrate_pt_update *pt_update)
+{
+	struct xe_vm *vm = pt_update->vops->vm;
+	struct xe_vma_ops *vops = pt_update->vops;
+	struct xe_vma_op *op;
+	int err;
 
-static const struct xe_migrate_pt_update_ops userptr_bind_ops = {
-	.populate = xe_vm_populate_pgtable,
-	.pre_commit = xe_pt_userptr_pre_commit,
-};
+	err = xe_pt_pre_commit(pt_update);
+	if (err)
+		return err;
+
+	down_read(&vm->userptr.notifier_lock);
+
+	list_for_each_entry(op, &vops->list, link)
+		op_check_userptr(vm, op);
+
+	return 0;
+}
 
 struct invalidation_fence {
drivers/gpu/drm/xe/xe_pt.c +0 -30
 	struct xe_gt_tlb_invalidation_fence base;
@@ -1166,181 +1286,6 @@ static int invalidation_fence_init(struct xe_gt *gt,
 	return ret && ret != -ENOENT ? ret : 0;
 }
 
-static void xe_pt_calc_rfence_interval(struct xe_vma *vma,
-				       struct xe_pt_migrate_pt_update *update,
-				       struct xe_vm_pgtable_update *entries,
-				       u32 num_entries)
-{
-	int i, level = 0;
-
-	for (i = 0; i < num_entries; i++) {
-		const struct xe_vm_pgtable_update *entry = &entries[i];
-
-		if (entry->pt->level > level)
-			level = entry->pt->level;
-	}
-
-	/* Greedy (non-optimal) calculation but simple */
-	update->base.start = ALIGN_DOWN(xe_vma_start(vma),
-					0x1ull << xe_pt_shift(level));
-	update->base.last = ALIGN(xe_vma_end(vma),
-				  0x1ull << xe_pt_shift(level)) - 1;
-}
-
-/**
- * __xe_pt_bind_vma() - Build and connect a page-table tree for the vma
- * address range.
- * @tile: The tile to bind for.
- * @vma: The vma to bind.
- * @q: The exec_queue with which to do pipelined page-table updates.
- * @syncs: Entries to sync on before binding the built tree to the live vm tree.
- * @num_syncs: Number of @sync entries.
- * @rebind: Whether we're rebinding this vma to the same address range

without
- * an unbind in-between.
- *
- * This function builds a page-table tree (see xe_pt_stage_bind() for more
- * information on page-table building), and the xe_vm_pgtable_update entries
- * abstracting the operations needed to attach it to the main vm tree. It
- * then takes the relevant locks and updates the metadata side of the main
- * vm tree and submits the operations for pipelined attachment of the
- * gpu page-table to the vm main tree, (which can be done either by the
- * cpu and the GPU).
- *
- * Return: A valid dma-fence representing the pipelined attachment operation
- * on success, an error pointer on error.
- */

drivers/gpu/drm/xe/xe_pt.c +0 -2
-struct dma_fence *
-__xe_pt_bind_vma(struct xe_tile *tile, struct xe_vma *vma, struct

xe_exec_queue *q,

drivers/gpu/drm/xe/xe_pt.c +0 -7
-		 struct xe_sync_entry *syncs, u32 num_syncs,
-		 bool rebind)
-{
-	struct xe_vm_pgtable_update entries[XE_VM_MAX_LEVEL * 2 + 1];
-	struct xe_pt_migrate_pt_update bind_pt_update = {
-		.base = {
-			.ops = xe_vma_is_userptr(vma) ? &userptr_bind_ops :

&bind_ops,

drivers/gpu/drm/xe/xe_pt.c +0 -42
-			.vma = vma,
-			.tile_id = tile->id,
-		},
-		.bind = true,
-	};
-	struct xe_vm *vm = xe_vma_vm(vma);
-	u32 num_entries;
-	struct dma_fence *fence;
-	struct invalidation_fence *ifence = NULL;
-	struct xe_range_fence *rfence;
-	int err;
-
-	bind_pt_update.locked = false;
-	xe_bo_assert_held(xe_vma_bo(vma));
-	xe_vm_assert_held(vm);
-
-	vm_dbg(&xe_vma_vm(vma)->xe->drm,
-	       "Preparing bind, with range [%llx...%llx) engine %p.\n",
-	       xe_vma_start(vma), xe_vma_end(vma), q);
-
-	err = xe_pt_prepare_bind(tile, vma, entries, &num_entries);
-	if (err)
-		goto err;
-	xe_tile_assert(tile, num_entries <= ARRAY_SIZE(entries));
-
-	xe_vm_dbg_print_entries(tile_to_xe(tile), entries, num_entries);
-	xe_pt_calc_rfence_interval(vma, &bind_pt_update, entries,
-				   num_entries);
-
-	/*
-	 * If rebind, we have to invalidate TLB on !LR vms to invalidate
-	 * cached PTEs point to freed memory. on LR vms this is done
-	 * automatically when the context is re-enabled by the rebind worker,
-	 * or in fault mode it was invalidated on PTE zapping.
-	 *
-	 * If !rebind, and scratch enabled VMs, there is a chance the scratch
-	 * PTE is already cached in the TLB so it needs to be invalidated.
-	 * on !LR VMs this is done in the ring ops preceding a batch, but on
-	 * non-faulting LR, in particular on user-space batch buffer chaining,
-	 * it needs to be done here.
-	 */
-	if ((rebind && !xe_vm_in_lr_mode(vm) && !vm->batch_invalidate_tlb)

||

drivers/gpu/drm/xe/xe_pt.c +0 -1
-	    (!rebind && xe_vm_has_scratch(vm) &&

xe_vm_in_preempt_fence_mode(vm))) {

drivers/gpu/drm/xe/xe_pt.c +0 -18
-		ifence = kzalloc(sizeof(*ifence), GFP_KERNEL);
-		if (!ifence)
-			return ERR_PTR(-ENOMEM);
-	}
-
-	rfence = kzalloc(sizeof(*rfence), GFP_KERNEL);
-	if (!rfence) {
-		kfree(ifence);
-		return ERR_PTR(-ENOMEM);
-	}
-
-	fence = xe_migrate_update_pgtables(tile->migrate,
-					   vm, xe_vma_bo(vma), q,
-					   entries, num_entries,
-					   syncs, num_syncs,
-					   &bind_pt_update.base);
-	if (!IS_ERR(fence)) {
-		bool last_munmap_rebind = vma->gpuva.flags &

XE_VMA_LAST_REBIND;

drivers/gpu/drm/xe/xe_pt.c +0 -17
-		LLIST_HEAD(deferred);
-		int err;
-
-		err = xe_range_fence_insert(&vm->rftree[tile->id], rfence,
-					    &xe_range_fence_kfree_ops,
-					    bind_pt_update.base.start,
-					    bind_pt_update.base.last, fence);
-		if (err)
-			dma_fence_wait(fence, false);
-
-		/* TLB invalidation must be done before signaling rebind */
-		if (ifence) {
-			int err = invalidation_fence_init(tile->primary_gt,
-							  ifence, fence,
-							  xe_vma_start(vma),
-							  xe_vma_end(vma),
-							  xe_vma_vm(vma)-
Quoted 1 line

usm.asid);

drivers/gpu/drm/xe/xe_pt.c +0 -16
-			if (err) {
-				dma_fence_put(fence);
-				kfree(ifence);
-				return ERR_PTR(err);
-			}
-			fence = &ifence->base.base;
-		}
-
-		/* add shared fence now for pagetable delayed destroy */
-		dma_resv_add_fence(xe_vm_resv(vm), fence, !rebind &&
-				   last_munmap_rebind ?
-				   DMA_RESV_USAGE_KERNEL :
-				   DMA_RESV_USAGE_BOOKKEEP);
-
-		if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm)
-			dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv,

fence,

drivers/gpu/drm/xe/xe_pt.c +2 -31
-					   DMA_RESV_USAGE_BOOKKEEP);
-		xe_pt_commit_bind(vma, entries, num_entries, rebind,
-				  bind_pt_update.locked ? &deferred : NULL);
-
-		/* This vma is live (again?) now */
-		vma->tile_present |= BIT(tile->id);
-
-		if (bind_pt_update.locked) {
-			to_userptr_vma(vma)->userptr.initial_bind = true;
-			up_read(&vm->userptr.notifier_lock);
-			xe_bo_put_commit(&deferred);
-		}
-		if (!rebind && last_munmap_rebind &&
-		    xe_vm_in_preempt_fence_mode(vm))
-			xe_vm_queue_rebind_worker(vm);
-	} else {
-		kfree(rfence);
-		kfree(ifence);
-		if (bind_pt_update.locked)
-			up_read(&vm->userptr.notifier_lock);
-		xe_pt_abort_bind(vma, entries, num_entries);
-	}
-
-	return fence;
-
-err:
-	return ERR_PTR(err);
-}
-
 struct xe_pt_stage_unbind_walk {
 	/** @base: The pagewalk base-class. */
 	struct xe_pt_walk base;
@@ -1491,8 +1436,8 @@ xe_migrate_clear_pgtable_callback(struct
 xe_migrate_pt_update *pt_update,
 				  void *ptr, u32 qword_ofs, u32 num_qwords,
 				  const struct xe_vm_pgtable_update *update)
 {
-	struct xe_vma *vma = pt_update->vma;
-	u64 empty = __xe_pt_empty_pte(tile, xe_vma_vm(vma), update->pt-
 >level);
+	struct xe_vm *vm = pt_update->vops->vm;
+	u64 empty = __xe_pt_empty_pte(tile, vm, update->pt->level);
 	int i;
drivers/gpu/drm/xe/xe_pt.c +357 -104
 	if (map && map->is_iomem)
@@ -1536,144 +1481,443 @@ xe_pt_commit_unbind(struct xe_vma *vma,
 	}
 }
 
-static const struct xe_migrate_pt_update_ops unbind_ops = {
-	.populate = xe_migrate_clear_pgtable_callback,
+static void
+xe_pt_update_ops_rfence_interval(struct xe_vm_pgtable_update_ops
 *pt_update_ops,
+				 struct xe_vma *vma)
+{
+	u32 current_op = pt_update_ops->current_op;
+	struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops-
 >ops[current_op];
+	int i, level = 0;
+	u64 start, last;
+
+	for (i = 0; i < pt_op->num_entries; i++) {
+		const struct xe_vm_pgtable_update *entry = &pt_op->entries[i];
+
+		if (entry->pt->level > level)
+			level = entry->pt->level;
+	}
+
+	/* Greedy (non-optimal) calculation but simple */
+	start = ALIGN_DOWN(xe_vma_start(vma), 0x1ull << xe_pt_shift(level));
+	last = ALIGN(xe_vma_end(vma), 0x1ull << xe_pt_shift(level)) - 1;
+
+	if (start < pt_update_ops->start)
+		pt_update_ops->start = start;
+	if (last > pt_update_ops->last)
+		pt_update_ops->last = last;
+}
+
+static int bind_op_prepare(struct xe_vm *vm, struct xe_tile *tile,
+			   struct xe_vm_pgtable_update_ops *pt_update_ops,
+			   struct xe_vma *vma)
+{
+	u32 current_op = pt_update_ops->current_op;
+	struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops-
 >ops[current_op];
+	struct llist_head *deferred = &pt_update_ops->deferred;
+	int err;
+
+	xe_bo_assert_held(xe_vma_bo(vma));
+
+	vm_dbg(&xe_vma_vm(vma)->xe->drm,
+	       "Preparing bind, with range [%llx...%llx)\n",
+	       xe_vma_start(vma), xe_vma_end(vma) - 1);
+
+	pt_op->bind = true;
+	pt_op->rebind = BIT(tile->id) & vma->tile_present;
+
+	err = xe_pt_prepare_bind(tile, vma, pt_op->entries,
+				 &pt_op->num_entries);
+	if (!err) {
+		xe_tile_assert(tile, pt_op->num_entries <=
+			       ARRAY_SIZE(pt_op->entries));
+		xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries,
+					pt_op->num_entries, true);
+
+		xe_pt_update_ops_rfence_interval(pt_update_ops, vma);
+		++pt_update_ops->current_op;
+		pt_update_ops->needs_userptr_lock |=
 xe_vma_is_userptr(vma);
+
+		/*
+		 * If rebind, we have to invalidate TLB on !LR vms to invalidate
+		 * cached PTEs point to freed memory. on LR vms this is done
+		 * automatically when the context is re-enabled by the rebind
+		 * worker, or in fault mode it was invalidated on PTE zapping.
+		 *
+		 * If !rebind, and scratch enabled VMs, there is a chance the
+		 * scratch PTE is already cached in the TLB so it needs to be
+		 * invalidated. on !LR VMs this is done in the ring ops
+		 * preceding a batch, but on non-faulting LR, in particular on
+		 * user-space batch buffer chaining, it needs to be done here.
+		 */
+		pt_update_ops->needs_invalidation |=
+			(pt_op->rebind && xe_vm_in_lr_mode(vm) &&
+			!vm->batch_invalidate_tlb) ||
+			(!pt_op->rebind && vm->scratch_pt[tile->id] &&
+			 xe_vm_in_preempt_fence_mode(vm));
+
+		/* FIXME: Don't commit right away */
+		xe_pt_commit_bind(vma, pt_op->entries, pt_op->num_entries,
+				  pt_op->rebind, deferred);
+	}
+
+	return err;
+}
+
+static int unbind_op_prepare(struct xe_tile *tile,
+			     struct xe_vm_pgtable_update_ops *pt_update_ops,
+			     struct xe_vma *vma)
+{
+	u32 current_op = pt_update_ops->current_op;
+	struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops-
 >ops[current_op];
+	struct llist_head *deferred = &pt_update_ops->deferred;
+
+	xe_bo_assert_held(xe_vma_bo(vma));
+
+	vm_dbg(&xe_vma_vm(vma)->xe->drm,
+	       "Preparing unbind, with range [%llx...%llx)\n",
+	       xe_vma_start(vma), xe_vma_end(vma) - 1);
+
+	pt_op->bind = false;
+	pt_op->rebind = false;
+
+	pt_op->num_entries = xe_pt_stage_unbind(tile, vma, pt_op->entries);
+
+	xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries,
+				pt_op->num_entries, false);
+	xe_pt_update_ops_rfence_interval(pt_update_ops, vma);
+	++pt_update_ops->current_op;
+	pt_update_ops->needs_userptr_lock |= xe_vma_is_userptr(vma);
+	pt_update_ops->needs_invalidation = true;
+
+	/* FIXME: Don't commit right away */
+	xe_pt_commit_unbind(vma, pt_op->entries, pt_op->num_entries,
+			    deferred);
+
+	return 0;
+}
+
+static int op_prepare(struct xe_vm *vm,
+		      struct xe_tile *tile,
+		      struct xe_vm_pgtable_update_ops *pt_update_ops,
+		      struct xe_vma_op *op)
+{
+	int err = 0;
+
+	xe_vm_assert_held(vm);
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		if (!op->map.immediate && xe_vm_in_fault_mode(vm))
+			break;
+
+		err = bind_op_prepare(vm, tile, pt_update_ops, op->map.vma);
+		pt_update_ops->wait_vm_kernel = true;
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		err = unbind_op_prepare(tile, pt_update_ops,
+					gpuva_to_vma(op->base.remap.unmap-
 >va));
+
+		if (!err && op->remap.prev) {
+			err = bind_op_prepare(vm, tile, pt_update_ops,
+					      op->remap.prev);
+			pt_update_ops->wait_vm_bookkeep = true;
+		}
+		if (!err && op->remap.next) {
+			err = bind_op_prepare(vm, tile, pt_update_ops,
+					      op->remap.next);
+			pt_update_ops->wait_vm_bookkeep = true;
+		}
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		err = unbind_op_prepare(tile, pt_update_ops,
+					gpuva_to_vma(op->base.unmap.va));
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		err = bind_op_prepare(vm, tile, pt_update_ops,
+				      gpuva_to_vma(op->base.prefetch.va));
+		pt_update_ops->wait_vm_kernel = true;
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+
+	return err;
+}
+
+static void
+xe_pt_update_ops_init(struct xe_vm_pgtable_update_ops *pt_update_ops)
+{
+	init_llist_head(&pt_update_ops->deferred);
+	pt_update_ops->start = ~0x0ull;
+	pt_update_ops->last = 0x0ull;
+}
+
+/**
+ * xe_pt_update_ops_prepare() - Prepare PT update operations
+ * @tile: Tile of PT update operations
+ * @vops: VMA operationa
+ *
+ * Prepare PT update operations which includes updating internal PT state,
+ * allocate memory for page tables, populate page table being pruned in, and
+ * create PT update operations for leaf insertion / removal.
+ *
+ * Return: 0 on success, negative error code on error.
+ */
+int xe_pt_update_ops_prepare(struct xe_tile *tile, struct xe_vma_ops *vops)
+{
+	struct xe_vm_pgtable_update_ops *pt_update_ops =
+		&vops->pt_update_ops[tile->id];
+	struct xe_vma_op *op;
+	int err;
+
+	lockdep_assert_held(&vops->vm->lock);
+	xe_vm_assert_held(vops->vm);
+
+	xe_pt_update_ops_init(pt_update_ops);
+
+	list_for_each_entry(op, &vops->list, link) {
+		err = op_prepare(vops->vm, tile, pt_update_ops, op);
+
+		if (err)
+			return err;
+	}
+
+	xe_tile_assert(tile, pt_update_ops->current_op ==
+		       pt_update_ops->num_ops);
+
+	return 0;
+}
+
+static void bind_op_commit(struct xe_vm *vm, struct xe_tile *tile,
+			   struct xe_vm_pgtable_update_ops *pt_update_ops,
+			   struct xe_vma *vma, struct dma_fence *fence)
+{
+	if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm)
+		dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence,
+				   pt_update_ops->wait_vm_bookkeep ?
+				   DMA_RESV_USAGE_KERNEL :
+				   DMA_RESV_USAGE_BOOKKEEP);
+	vma->tile_present |= BIT(tile->id);
+	if (xe_vma_is_userptr(vma)) {
+		lockdep_assert_held_read(&vm->userptr.notifier_lock);
+		to_userptr_vma(vma)->userptr.initial_bind = true;
+	}
+
+	/*
+	 * Kick rebind worker if this bind triggers preempt fences and not in
+	 * the rebind worker
+	 */
+	if (pt_update_ops->wait_vm_bookkeep &&
+	    xe_vm_in_preempt_fence_mode(vm) &&
+	    !current->mm)
+		xe_vm_queue_rebind_worker(vm);
+}
+
+static void unbind_op_commit(struct xe_vm *vm, struct xe_tile *tile,
+			     struct xe_vm_pgtable_update_ops *pt_update_ops,
+			     struct xe_vma *vma, struct dma_fence *fence)
+{
+	if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm)
+		dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence,
+				   pt_update_ops->wait_vm_bookkeep ?
+				   DMA_RESV_USAGE_KERNEL :
+				   DMA_RESV_USAGE_BOOKKEEP);
+	vma->tile_present &= ~BIT(tile->id);
+	if (!vma->tile_present) {
+		list_del_init(&vma->combined_links.rebind);
+		if (xe_vma_is_userptr(vma)) {
+			lockdep_assert_held_read(&vm->userptr.notifier_lock);
+
+			spin_lock(&vm->userptr.invalidated_lock);
+			list_del_init(&to_userptr_vma(vma)-
 >userptr.invalidate_link);
+			spin_unlock(&vm->userptr.invalidated_lock);
+		}
+	}
+}
+
+static void op_commit(struct xe_vm *vm,
+		      struct xe_tile *tile,
+		      struct xe_vm_pgtable_update_ops *pt_update_ops,
+		      struct xe_vma_op *op, struct dma_fence *fence)
+{
+	xe_vm_assert_held(vm);
+
+	switch (op->base.op) {
+	case DRM_GPUVA_OP_MAP:
+		if (!op->map.immediate && xe_vm_in_fault_mode(vm))
+			break;
+
+		bind_op_commit(vm, tile, pt_update_ops, op->map.vma, fence);
+		break;
+	case DRM_GPUVA_OP_REMAP:
+		unbind_op_commit(vm, tile, pt_update_ops,
+				 gpuva_to_vma(op->base.remap.unmap->va),
 fence);
+
+		if (op->remap.prev)
+			bind_op_commit(vm, tile, pt_update_ops, op-
 >remap.prev,
+				       fence);
+		if (op->remap.next)
+			bind_op_commit(vm, tile, pt_update_ops, op-
 >remap.next,
+				       fence);
+		break;
+	case DRM_GPUVA_OP_UNMAP:
+		unbind_op_commit(vm, tile, pt_update_ops,
+				 gpuva_to_vma(op->base.unmap.va), fence);
+		break;
+	case DRM_GPUVA_OP_PREFETCH:
+		bind_op_commit(vm, tile, pt_update_ops,
+			       gpuva_to_vma(op->base.prefetch.va), fence);
+		break;
+	default:
+		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
+	}
+}
+
+static const struct xe_migrate_pt_update_ops migrate_ops = {
+	.populate = xe_vm_populate_pgtable,
+	.clear = xe_migrate_clear_pgtable_callback,
 	.pre_commit = xe_pt_pre_commit,
 };
 
-static const struct xe_migrate_pt_update_ops userptr_unbind_ops = {
-	.populate = xe_migrate_clear_pgtable_callback,
+static const struct xe_migrate_pt_update_ops userptr_migrate_ops = {
+	.populate = xe_vm_populate_pgtable,
+	.clear = xe_migrate_clear_pgtable_callback,
 	.pre_commit = xe_pt_userptr_pre_commit,
 };
 
 /**
- * __xe_pt_unbind_vma() - Disconnect and free a page-table tree for the vma
- * address range.
- * @tile: The tile to unbind for.
- * @vma: The vma to unbind.
- * @q: The exec_queue with which to do pipelined page-table updates.
- * @syncs: Entries to sync on before disconnecting the tree to be destroyed.
- * @num_syncs: Number of @sync entries.
+ * xe_pt_update_ops_run() - Run PT update operations
+ * @tile: Tile of PT update operations
+ * @vops: VMA operationa
  *
- * This function builds a the xe_vm_pgtable_update entries abstracting the
- * operations needed to detach the page-table tree to be destroyed from the
- * man vm tree.
- * It then takes the relevant locks and submits the operations for
- * pipelined detachment of the gpu page-table from  the vm main tree,
- * (which can be done either by the cpu and the GPU), Finally it frees the
- * detached page-table tree.
+ * Run PT update operations which includes committing internal PT state
 changes,
+ * creating job for PT update operations for leaf insertion / removal, and
+ * installing job fence in various places.
  *
- * Return: A valid dma-fence representing the pipelined detachment operation
- * on success, an error pointer on error.
+ * Return: fence on success, negative ERR_PTR on error.
  */
 struct dma_fence *
-__xe_pt_unbind_vma(struct xe_tile *tile, struct xe_vma *vma, struct
 xe_exec_queue *q,
-		   struct xe_sync_entry *syncs, u32 num_syncs)
+xe_pt_update_ops_run(struct xe_tile *tile, struct xe_vma_ops *vops)
 {
-	struct xe_vm_pgtable_update entries[XE_VM_MAX_LEVEL * 2 + 1];
-	struct xe_pt_migrate_pt_update unbind_pt_update = {
-		.base = {
-			.ops = xe_vma_is_userptr(vma) ? &userptr_unbind_ops :
-			&unbind_ops,
-			.vma = vma,
-			.tile_id = tile->id,
-		},
-	};
-	struct xe_vm *vm = xe_vma_vm(vma);
-	u32 num_entries;
-	struct dma_fence *fence = NULL;
-	struct invalidation_fence *ifence;
+	struct xe_vm *vm = vops->vm;
+	struct xe_vm_pgtable_update_ops *pt_update_ops =
+		&vops->pt_update_ops[tile->id];
+	struct dma_fence *fence;
+	struct invalidation_fence *ifence = NULL;
 	struct xe_range_fence *rfence;
+	struct xe_vma_op *op;
+	int err = 0;
+	struct xe_migrate_pt_update update = {
+		.ops = pt_update_ops->needs_userptr_lock ?
+			&userptr_migrate_ops :
+			&migrate_ops,
+		.vops = vops,
+		.tile_id = tile->id
+	};
 
-	LLIST_HEAD(deferred);
-
-	xe_bo_assert_held(xe_vma_bo(vma));
+	lockdep_assert_held(&vm->lock);
 	xe_vm_assert_held(vm);
 
-	vm_dbg(&xe_vma_vm(vma)->xe->drm,
-	       "Preparing unbind, with range [%llx...%llx) engine %p.\n",
-	       xe_vma_start(vma), xe_vma_end(vma), q);
-
-	num_entries = xe_pt_stage_unbind(tile, vma, entries);
-	xe_tile_assert(tile, num_entries <= ARRAY_SIZE(entries));
-
-	xe_vm_dbg_print_entries(tile_to_xe(tile), entries, num_entries);
-	xe_pt_calc_rfence_interval(vma, &unbind_pt_update, entries,
-				   num_entries);
-
-	ifence = kzalloc(sizeof(*ifence), GFP_KERNEL);
-	if (!ifence)
-		return ERR_PTR(-ENOMEM);
+	if (pt_update_ops->needs_invalidation) {
+		ifence = kzalloc(sizeof(*ifence), GFP_KERNEL);
+		if (!ifence)
+			return ERR_PTR(-ENOMEM);
+	}
 
 	rfence = kzalloc(sizeof(*rfence), GFP_KERNEL);
 	if (!rfence) {
-		kfree(ifence);
-		return ERR_PTR(-ENOMEM);
+		err = -ENOMEM;
+		goto free_ifence;
 	}
 
-	/*
-	 * Even if we were already evicted and unbind to destroy, we need to
-	 * clear again here. The eviction may have updated pagetables at a
-	 * lower level, because it needs to be more conservative.
-	 */
-	fence = xe_migrate_update_pgtables(tile->migrate,
-					   vm, NULL, q ? q :
-					   vm->q[tile->id],
-					   entries, num_entries,
-					   syncs, num_syncs,
-					   &unbind_pt_update.base);
-	if (!IS_ERR(fence)) {
-		int err;
-
-		err = xe_range_fence_insert(&vm->rftree[tile->id], rfence,
-					    &xe_range_fence_kfree_ops,
-					    unbind_pt_update.base.start,
-					    unbind_pt_update.base.last, fence);
-		if (err)
-			dma_fence_wait(fence, false);
+	fence = xe_migrate_update_pgtables(tile->migrate, &update);
+	if (IS_ERR(fence)) {
+		err = PTR_ERR(fence);
+		goto free_rfence;
+	}
+
+	err = xe_range_fence_insert(&vm->rftree[tile->id], rfence,
+				    &xe_range_fence_kfree_ops,
+				    pt_update_ops->start,
+				    pt_update_ops->last, fence);
+	if (err)
+		dma_fence_wait(fence, false);
 
-		/* TLB invalidation must be done before signaling unbind */
+	/* tlb invalidation must be done before signaling rebind */
+	if (ifence) {
 		err = invalidation_fence_init(tile->primary_gt, ifence, fence,
-					      xe_vma_start(vma),
-					      xe_vma_end(vma),
-					      xe_vma_vm(vma)->usm.asid);
-		if (err) {
-			dma_fence_put(fence);
-			kfree(ifence);
-			return ERR_PTR(err);
-		}
+					      pt_update_ops->start,
+					      pt_update_ops->last,
+					      vm->usm.asid);
+		if (err)
+			goto put_fence;
 		fence = &ifence->base.base;
+	}
 
-		/* add shared fence now for pagetable delayed destroy */
-		dma_resv_add_fence(xe_vm_resv(vm), fence,
-				   DMA_RESV_USAGE_BOOKKEEP);
+	dma_resv_add_fence(xe_vm_resv(vm), fence,
+			   pt_update_ops->wait_vm_bookkeep ?
+			   DMA_RESV_USAGE_KERNEL :
+			   DMA_RESV_USAGE_BOOKKEEP);
 
-		/* This fence will be installed by caller when doing eviction */
-		if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm)
-			dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv,
 fence,
-					   DMA_RESV_USAGE_BOOKKEEP);
-		xe_pt_commit_unbind(vma, entries, num_entries,
-				    unbind_pt_update.locked ? &deferred : NULL);
-		vma->tile_present &= ~BIT(tile->id);
-	} else {
-		kfree(rfence);
-		kfree(ifence);
-	}
+	list_for_each_entry(op, &vops->list, link)
+		op_commit(vops->vm, tile, pt_update_ops, op, fence);
 
-	if (!vma->tile_present)
-		list_del_init(&vma->combined_links.rebind);
+	if (pt_update_ops->needs_userptr_lock)
+		up_read(&vm->userptr.notifier_lock);
 
-	if (unbind_pt_update.locked) {
-		xe_tile_assert(tile, xe_vma_is_userptr(vma));
+	return fence;
 
-		if (!vma->tile_present) {
-			spin_lock(&vm->userptr.invalidated_lock);
-			list_del_init(&to_userptr_vma(vma)-
Quoted 1 line

userptr.invalidate_link);

drivers/gpu/drm/xe/xe_pt.c +30 -3
-			spin_unlock(&vm->userptr.invalidated_lock);
-		}
+put_fence:
+	if (pt_update_ops->needs_userptr_lock)
 		up_read(&vm->userptr.notifier_lock);
-		xe_bo_put_commit(&deferred);
+	dma_fence_put(fence);
+free_rfence:
+	kfree(rfence);
+free_ifence:
+	kfree(ifence);
+
+	return ERR_PTR(err);
+}
+
+/**
+ * xe_pt_update_ops_fini() - Finish PT update operations
+ * @tile: Tile of PT update operations
+ * @vops: VMA operations
+ *
+ * Finish PT update operations by committing to destroy page table memory
+ */
+void xe_pt_update_ops_fini(struct xe_tile *tile, struct xe_vma_ops *vops)
+{
+	struct xe_vm_pgtable_update_ops *pt_update_ops =
+		&vops->pt_update_ops[tile->id];
+	int i;
+
+	lockdep_assert_held(&vops->vm->lock);
+	xe_vm_assert_held(vops->vm);
+
+	/* FIXME: Not 100% correct */
+	for (i = 0; i < pt_update_ops->num_ops; ++i) {
+		struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops-
Quoted 1 line

ops[i];

drivers/gpu/drm/xe/xe_pt.c +20 -1
+
+		if (pt_op->bind)
+			xe_pt_free_bind(pt_op->entries, pt_op->num_entries);
 	}
+	xe_bo_put_commit(&vops->pt_update_ops[tile->id].deferred);
+}
 
-	return fence;
+/**
+ * xe_pt_update_ops_abort() - Abort PT update operations
+ * @tile: Tile of PT update operations
+ * @vops: VMA operationa
+ *
+ *  Abort PT update operations by unwinding internal PT state
+ */
+void xe_pt_update_ops_abort(struct xe_tile *tile, struct xe_vma_ops *vops)
+{
+	lockdep_assert_held(&vops->vm->lock);
+	xe_vm_assert_held(vops->vm);
+
+	/* FIXME: Just kill VM for now + cleanup PTs */
+	xe_bo_put_commit(&vops->pt_update_ops[tile->id].deferred);
+	xe_vm_kill(vops->vm, false);
 }
drivers/gpu/drm/xe/xe_pt.h +7 -0
index 71a4fbfcff43..cbf8170d89cc 100644
@@ -17,6 +17,7 @@ struct xe_sync_entry;
 struct xe_tile;
 struct xe_vm;
 struct xe_vma;
+struct xe_vma_ops;
 
 /* Largest huge pte is currently 1GiB. May become device dependent. */
 #define MAX_HUGEPTE_LEVEL 2
@@ -34,6 +35,12 @@ void xe_pt_populate_empty(struct xe_tile *tile, struct
 xe_vm *vm,
 
 void xe_pt_destroy(struct xe_pt *pt, u32 flags, struct llist_head *deferred);
 
+int xe_pt_update_ops_prepare(struct xe_tile *tile, struct xe_vma_ops *vops);
+struct dma_fence *xe_pt_update_ops_run(struct xe_tile *tile,
+				       struct xe_vma_ops *vops);
+void xe_pt_update_ops_fini(struct xe_tile *tile, struct xe_vma_ops *vops);
+void xe_pt_update_ops_abort(struct xe_tile *tile, struct xe_vma_ops *vops);
+
 struct dma_fence *
 __xe_pt_bind_vma(struct xe_tile *tile, struct xe_vma *vma, struct

xe_exec_queue *q,

drivers/gpu/drm/xe/xe_pt.h +0 -0
 		 struct xe_sync_entry *syncs, u32 num_syncs,
drivers/gpu/drm/xe/xe_pt_types.h +29 -0
index 2093150f461e..16252f1be055 100644
@@ -86,4 +86,38 @@ struct xe_vm_pgtable_update_op {
 	bool rebind;
 };
 
+/** struct xe_vm_pgtable_update_ops: page table update operations */
+struct xe_vm_pgtable_update_ops {
+	/** @ops: operations */
+	struct xe_vm_pgtable_update_op *ops;
+	/** @deferred: deferred list to destroy PT entries */
+	struct llist_head deferred;
+	/** @q: exec queue for PT operations */
+	struct xe_exec_queue *q;
+	/** @start: start address of ops */
+	u64 start;
+	/** @last: last address of ops */
+	u64 last;
+	/** @num_ops: number of operations */
+	u32 num_ops;
+	/** @current_op: current operations */
+	u32 current_op;
+	/** @needs_userptr_lock: Needs userptr lock */
+	bool needs_userptr_lock;
+	/** @needs_invalidation: Needs invalidation */
+	bool needs_invalidation;
+	/**
+	 * @wait_vm_bookkeep: PT operations need to wait until VM is idle
+	 * (bookkeep dma-resv slots are idle) and stage all future VM activity
+	 * behind these operations (install PT operations into VM kernel
+	 * dma-resv slot).
+	 */
+	bool wait_vm_bookkeep;
+	/**
+	 * @wait_vm_kernel: PT operations need to wait until VM kernel dma-

resv

drivers/gpu/drm/xe/xe_pt_types.h +5 -0
+	 * slots are idle.
+	 */
+	bool wait_vm_kernel;
+};
+
 #endif
drivers/gpu/drm/xe/xe_vm.c +3 -7
index 72e9bdab79d5..47658465b735 100644
@@ -413,7 +413,7 @@ int __xe_vm_userptr_needs_repin(struct xe_vm *vm)
 
 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
 
-static void xe_vm_kill(struct xe_vm *vm, bool unlocked)
+void xe_vm_kill(struct xe_vm *vm, bool unlocked)
 {
 	struct xe_exec_queue *q;
 
@@ -577,13 +577,9 @@ static void preempt_rebind_work_func(struct
 work_struct *w)
 		err = PTR_ERR(rebind_fence);
 		goto out_unlock;
 	}
+	dma_fence_put(rebind_fence);
 
-	if (rebind_fence) {
-		dma_fence_wait(rebind_fence, false);
-		dma_fence_put(rebind_fence);
-	}
-
-	/* Wait on munmap style VM unbinds */
+	/* Wait on rebinds */
 	wait = dma_resv_wait_timeout(xe_vm_resv(vm),
 				     DMA_RESV_USAGE_KERNEL,
drivers/gpu/drm/xe/xe_vm.c +20 -1
 				     false, MAX_SCHEDULE_TIMEOUT);
@@ -759,11 +755,35 @@ int xe_vm_userptr_check_repin(struct xe_vm *vm)
  * xe_vm_populate_dummy_rebind() - Populate dummy rebind VMA ops
  * @vm: The VM.
  * @vma: VMA to populate dummy VMA ops
+ * @tile_mask: tile mask for VMA ops
  *
  * Populate dummy VMA ops which can be used to issue a rebind for the VMA
  */
-void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma)
+void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma,
+				 u8 tile_mask)
 {
+	int i;
+
+	for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
+		if (BIT(i) & tile_mask) {
+			struct xe_vm_pgtable_update_op *pt_op =
+				vm->dummy_ops.vops.pt_update_ops[i].ops;
+
+			memset(&vm->dummy_ops.vops.pt_update_ops[i], 0,
+			       sizeof(vm->dummy_ops.vops.pt_update_ops[i]));
+			vm->dummy_ops.vops.pt_update_ops[i].ops = pt_op;
+			vm->dummy_ops.vops.pt_update_ops[i].num_ops = 1;
+
+			/*
+			 * Wait for VM to be idle / schedule execs + resume
+			 * behind rebinds
+			 */
+			vm-
Quoted 1 line

dummy_ops.vops.pt_update_ops[i].wait_vm_bookkeep =

drivers/gpu/drm/xe/xe_vm.c +6 -1
+				true;
+		} else {
+			vm->dummy_ops.vops.pt_update_ops[i].num_ops = 0;
+		}
+	}
 	vm->dummy_ops.op.base.op = DRM_GPUVA_OP_MAP;
 	vm->dummy_ops.op.base.map.va.addr = vma->gpuva.va.addr;
 	vm->dummy_ops.op.base.map.va.range = vma->gpuva.va.range;
@@ -797,7 +817,7 @@ struct dma_fence *xe_vm_rebind(struct xe_vm *vm,
 bool rebind_worker)
 		else
 			trace_xe_vma_rebind_exec(vma);
 
-		xe_vm_populate_dummy_rebind(vm, vma);
+		xe_vm_populate_dummy_rebind(vm, vma, vma->tile_present);
 		fence = xe_vm_ops_execute(vm, &vm->dummy_ops.vops);
 		if (IS_ERR(fence))
drivers/gpu/drm/xe/xe_vm.c +0 -1
 			return fence;
@@ -1653,7 +1673,6 @@ static void vm_destroy_work_func(struct work_struct
 *w)
 		XE_WARN_ON(vm->pt_root[id]);
 
 	trace_xe_vm_free(vm);
-	dma_fence_put(vm->rebind_fence);
 	xe_vma_ops_fini(&vm->dummy_ops.vops);
 	kfree(vm);

}

drivers/gpu/drm/xe/xe_vm.c +0 -141
@@ -1691,147 +1710,6 @@ to_wait_exec_queue(struct xe_vm *vm, struct
 xe_exec_queue *q)
 	return q ? q : vm->q[0];
 }
 
-static struct dma_fence *
-xe_vm_unbind_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 xe_vm *vm = xe_vma_vm(vma);
-	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-	struct xe_tile *tile;
-	struct dma_fence *fence = NULL;
-	struct dma_fence **fences = NULL;
-	struct dma_fence_array *cf = NULL;
-	int cur_fence = 0;
-	int number_tiles = hweight8(vma->tile_present);
-	int err;
-	u8 id;
-
-	trace_xe_vma_unbind(vma);
-
-	if (number_tiles > 1) {
-		fences = kmalloc_array(number_tiles, sizeof(*fences),
-				       GFP_KERNEL);
-		if (!fences)
-			return ERR_PTR(-ENOMEM);
-	}
-
-	for_each_tile(tile, vm->xe, id) {
-		if (!(vma->tile_present & BIT(id)))
-			goto next;
-
-		fence = __xe_pt_unbind_vma(tile, vma, q ? q : vm->q[id],
-					   first_op ? syncs : NULL,
-					   first_op ? num_syncs : 0);
-		if (IS_ERR(fence)) {
-			err = PTR_ERR(fence);
-			goto err_fences;
-		}
-
-		if (fences)
-			fences[cur_fence++] = fence;
-
-next:
-		if (q && vm->pt_root[id] && !list_empty(&q->multi_gt_list))
-			q = list_next_entry(q, multi_gt_list);
-	}
-
-	if (fences) {
-		cf = dma_fence_array_create(number_tiles, fences,
-					    vm->composite_fence_ctx,
-					    vm->composite_fence_seqno++,
-					    false);
-		if (!cf) {
-			--vm->composite_fence_seqno;
-			err = -ENOMEM;
-			goto err_fences;
-		}
-	}
-
-	fence = cf ? &cf->base : !fence ?
-		xe_exec_queue_last_fence_get(wait_exec_queue, vm) : fence;
-
-	return fence;
-
-err_fences:
-	if (fences) {
-		while (cur_fence)
-			dma_fence_put(fences[--cur_fence]);
-		kfree(fences);
-	}
-
-	return ERR_PTR(err);
-}
-
-static struct dma_fence *
-xe_vm_bind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
-	       struct xe_sync_entry *syncs, u32 num_syncs,
-	       u8 tile_mask, bool first_op, bool last_op)
-{
-	struct xe_tile *tile;
-	struct dma_fence *fence;
-	struct dma_fence **fences = NULL;
-	struct dma_fence_array *cf = NULL;
-	struct xe_vm *vm = xe_vma_vm(vma);
-	int cur_fence = 0;
-	int number_tiles = hweight8(vma->tile_mask);
-	int err;
-	u8 id;
-
-	trace_xe_vma_bind(vma);
-
-	if (number_tiles > 1) {
-		fences = kmalloc_array(number_tiles, sizeof(*fences),
-				       GFP_KERNEL);
-		if (!fences)
-			return ERR_PTR(-ENOMEM);
-	}
-
-	for_each_tile(tile, vm->xe, id) {
-		if (!(tile_mask & BIT(id)))
-			goto next;
-
-		fence = __xe_pt_bind_vma(tile, vma, q ? q : vm->q[id],
-					 first_op ? syncs : NULL,
-					 first_op ? num_syncs : 0,
-					 vma->tile_present & BIT(id));
-		if (IS_ERR(fence)) {
-			err = PTR_ERR(fence);
-			goto err_fences;
-		}
-
-		if (fences)
-			fences[cur_fence++] = fence;
-
-next:
-		if (q && vm->pt_root[id] && !list_empty(&q->multi_gt_list))
-			q = list_next_entry(q, multi_gt_list);
-	}
-
-	if (fences) {
-		cf = dma_fence_array_create(number_tiles, fences,
-					    vm->composite_fence_ctx,
-					    vm->composite_fence_seqno++,
-					    false);
-		if (!cf) {
-			--vm->composite_fence_seqno;
-			err = -ENOMEM;
-			goto err_fences;
-		}
-	}
-
-	return cf ? &cf->base : fence;
-
-err_fences:
-	if (fences) {
-		while (cur_fence)
-			dma_fence_put(fences[--cur_fence]);
-		kfree(fences);
-	}
-
-	return ERR_PTR(err);
-}
-
 static struct xe_user_fence *

find_ufence_get(struct xe_sync_entry *syncs, u32 num_syncs) {

drivers/gpu/drm/xe/xe_vm.c +0 -41
@@ -1847,47 +1725,6 @@ find_ufence_get(struct xe_sync_entry *syncs, u32
 num_syncs)
 	return NULL;
 }
 
-static struct dma_fence *
-xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
-	   struct xe_bo *bo, struct xe_sync_entry *syncs, u32 num_syncs,
-	   u8 tile_mask, bool immediate, bool first_op, bool last_op)
-{
-	struct dma_fence *fence;
-	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(bo);
-
-	if (immediate) {
-		fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, tile_mask,
-				       first_op, last_op);
-		if (IS_ERR(fence))
-			return fence;
-	} else {
-		xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
-		fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-	}
-
-	return fence;
-}
-
-static struct dma_fence *
-xe_vm_unbind(struct xe_vm *vm, 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 *fence;
-
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(xe_vma_bo(vma));
-
-	fence = xe_vm_unbind_vma(vma, q, syncs, num_syncs, first_op,
 last_op);
-	if (IS_ERR(fence))
-		return fence;
-
-	return fence;
-}
-

#define ALL_DRM_XE_VM_CREATE_FLAGS
(DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \

drivers/gpu/drm/xe/xe_vm.c +0 -15
 				    DRM_XE_VM_CREATE_FLAG_LR_MODE | \
 				    DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
@@ -2028,21 +1865,6 @@ static const u32 region_to_mem_type[] = {
 	XE_PL_VRAM1,
 };
 
-static struct dma_fence *
-xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
-	       struct xe_exec_queue *q, struct xe_sync_entry *syncs,
-	       u32 num_syncs, bool first_op, bool last_op)
-{
-	struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm,
 q);
-
-	if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated))
 {
-		return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs,
 num_syncs,
-				  vma->tile_mask, true, first_op, last_op);
-	} else {
-		return xe_exec_queue_last_fence_get(wait_exec_queue, vm);
-	}
-}
-

static void prep_vma_destroy(struct xe_vm *vm, struct xe_vma *vma,

drivers/gpu/drm/xe/xe_vm.c +0 -1
 			     bool post_commit)
 {
@@ -2334,7 +2156,6 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm *vm,
 struct xe_exec_queue *q,
 				   struct xe_vma_ops *vops, bool last)
 {
 	struct xe_device *xe = vm->xe;
-	struct xe_vma_op *last_op = NULL;
 	struct drm_gpuva_op *__op;
 	struct xe_tile *tile;
drivers/gpu/drm/xe/xe_vm.c +0 -9
 	u8 id, tile_mask = 0;
@@ -2348,19 +2169,10 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 	drm_gpuva_for_each_op(__op, ops) {
 		struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
 		struct xe_vma *vma;
-		bool first = list_empty(&vops->list);
 		unsigned int flags = 0;
 
 		INIT_LIST_HEAD(&op->link);
 		list_add_tail(&op->link, &vops->list);
-
-		if (first) {
-			op->flags |= XE_VMA_OP_FIRST;
-			op->num_syncs = num_syncs;
-			op->syncs = syncs;
-		}
-
-		op->q = q;
 		op->tile_mask = tile_mask;
 
drivers/gpu/drm/xe/xe_vm.c +1 -59
 		switch (op->base.op) {
@@ -2471,196 +2283,21 @@ static int vm_bind_ioctl_ops_parse(struct xe_vm
 *vm, struct xe_exec_queue *q,
 		}
 		case DRM_GPUVA_OP_UNMAP:
 		case DRM_GPUVA_OP_PREFETCH:
+			/* FIXME: Need to skip some prefetch ops */
 			xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask);
 			break;
 		default:
 			drm_warn(&vm->xe->drm, "NOT POSSIBLE");
 		}
 
-		last_op = op;
-
 		err = xe_vma_op_commit(vm, op);
 		if (err)
 			return err;
 	}
 
-	/* FIXME: Unhandled corner case */
-	XE_WARN_ON(!last_op && last && !list_empty(&vops->list));
-
-	if (!last_op)
-		return 0;
-
-	if (last) {
-		last_op->flags |= XE_VMA_OP_LAST;
-		last_op->num_syncs = num_syncs;
-		last_op->syncs = syncs;
-	}
-
 	return 0;
 }
 
-static struct dma_fence *op_execute(struct xe_vm *vm, struct xe_vma *vma,
-				    struct xe_vma_op *op)
-{
-	struct dma_fence *fence = NULL;
-
-	lockdep_assert_held(&vm->lock);
-	xe_vm_assert_held(vm);
-	xe_bo_assert_held(xe_vma_bo(vma));
-
-	switch (op->base.op) {
-	case DRM_GPUVA_OP_MAP:
-		/* FIXME: Override vma->tile_mask for page faults */
-		fence = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
-				   op->syncs, op->num_syncs,
-				   op->map.immediate,
-				   op->tile_mask,
-				   op->flags & XE_VMA_OP_FIRST,
-				   op->flags & XE_VMA_OP_LAST);
-		break;
-	case DRM_GPUVA_OP_REMAP:
-	{
-		bool prev = !!op->remap.prev;
-		bool next = !!op->remap.next;
-
-		if (!op->remap.unmap_done) {
-			if (prev || next)
-				vma->gpuva.flags |= XE_VMA_FIRST_REBIND;
-			fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
-					     op->num_syncs,
-					     op->flags & XE_VMA_OP_FIRST,
-					     op->flags & XE_VMA_OP_LAST &&
-					     !prev && !next);
-			if (IS_ERR(fence))
-				break;
-			op->remap.unmap_done = true;
-		}
-
-		if (prev) {
-			op->remap.prev->gpuva.flags |= XE_VMA_LAST_REBIND;
-			dma_fence_put(fence);
-			fence = xe_vm_bind(vm, op->remap.prev, op->q,
-					   xe_vma_bo(op->remap.prev), op-
 >syncs,
-					   op->num_syncs, op->remap.prev-
 >tile_mask,
-					   true, false,
-					   op->flags & XE_VMA_OP_LAST

&& !next);

drivers/gpu/drm/xe/xe_vm.c +0 -1
-			op->remap.prev->gpuva.flags &=

~XE_VMA_LAST_REBIND;

drivers/gpu/drm/xe/xe_vm.c +0 -14
-			if (IS_ERR(fence))
-				break;
-			op->remap.prev = NULL;
-		}
-
-		if (next) {
-			op->remap.next->gpuva.flags |= XE_VMA_LAST_REBIND;
-			dma_fence_put(fence);
-			fence = xe_vm_bind(vm, op->remap.next, op->q,
-					   xe_vma_bo(op->remap.next),
-					   op->syncs, op->num_syncs,
-					   op->remap.next->tile_mask, true, false,
-					   op->flags & XE_VMA_OP_LAST);
-			op->remap.next->gpuva.flags &=

~XE_VMA_LAST_REBIND;

drivers/gpu/drm/xe/xe_vm.c +0 -10
-			if (IS_ERR(fence))
-				break;
-			op->remap.next = NULL;
-		}
-
-		break;
-	}
-	case DRM_GPUVA_OP_UNMAP:
-		fence = xe_vm_unbind(vm, vma, op->q, op->syncs,
-				     op->num_syncs, op->flags &

XE_VMA_OP_FIRST,

drivers/gpu/drm/xe/xe_vm.c +0 -4
-				     op->flags & XE_VMA_OP_LAST);
-		break;
-	case DRM_GPUVA_OP_PREFETCH:
-		fence = xe_vm_prefetch(vm, vma, op->q, op->syncs, op-
Quoted 1 line

num_syncs,

drivers/gpu/drm/xe/xe_vm.c +0 -28
-				       op->flags & XE_VMA_OP_FIRST,
-				       op->flags & XE_VMA_OP_LAST);
-		break;
-	default:
-		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
-	}
-
-	if (IS_ERR(fence))
-		trace_xe_vma_fail(vma);
-
-	return fence;
-}
-
-static struct dma_fence *
-__xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
-		    struct xe_vma_op *op)
-{
-	struct dma_fence *fence;
-	int err;
-
-retry_userptr:
-	fence = op_execute(vm, vma, op);
-	if (IS_ERR(fence) && PTR_ERR(fence) == -EAGAIN) {
-		lockdep_assert_held(&vm->lock);
-
-		if (op->base.op == DRM_GPUVA_OP_REMAP) {
-			if (!op->remap.unmap_done)
-				vma = gpuva_to_vma(op->base.remap.unmap-
Quoted 1 line

va);

drivers/gpu/drm/xe/xe_vm.c +0 -46
-			else if (op->remap.prev)
-				vma = op->remap.prev;
-			else
-				vma = op->remap.next;
-		}
-
-		if (xe_vma_is_userptr(vma)) {
-			err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
-			if (!err)
-				goto retry_userptr;
-
-			fence = ERR_PTR(err);
-			trace_xe_vma_fail(vma);
-		}
-	}
-
-	return fence;
-}
-
-static struct dma_fence *
-xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
-{
-	struct dma_fence *fence = ERR_PTR(-ENOMEM);
-
-	lockdep_assert_held(&vm->lock);
-
-	switch (op->base.op) {
-	case DRM_GPUVA_OP_MAP:
-		fence = __xe_vma_op_execute(vm, op->map.vma, op);
-		break;
-	case DRM_GPUVA_OP_REMAP:
-	{
-		struct xe_vma *vma;
-
-		if (!op->remap.unmap_done)
-			vma = gpuva_to_vma(op->base.remap.unmap->va);
-		else if (op->remap.prev)
-			vma = op->remap.prev;
-		else
-			vma = op->remap.next;
-
-		fence = __xe_vma_op_execute(vm, vma, op);
-		break;
-	}
-	case DRM_GPUVA_OP_UNMAP:
-		fence = __xe_vma_op_execute(vm, gpuva_to_vma(op-
Quoted 1 line

base.unmap.va),

drivers/gpu/drm/xe/xe_vm.c +26 -14
-					    op);
-		break;
-	case DRM_GPUVA_OP_PREFETCH:
-		fence = __xe_vma_op_execute(vm,
-					    gpuva_to_vma(op->base.prefetch.va),
-					    op);
-		break;
-	default:
-		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
-	}
-
-	return fence;
-}
-
 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
 			     bool post_commit, bool prev_post_commit,
 			     bool next_post_commit)
@@ -2837,6 +2474,32 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
 	return 0;
 }
 
+static int vm_ops_setup_tile_args(struct xe_vm *vm, struct xe_vma_ops *vops)
+{
+	struct xe_exec_queue *q = vops->q;
+	struct xe_tile *tile;
+	int number_tiles = 0;
+	u8 id;
+
+	for_each_tile(tile, vm->xe, id) {
+		if (vops->pt_update_ops[id].num_ops)
+			++number_tiles;
+
+		if (vops->pt_update_ops[id].q)
+			continue;
+
+		if (q) {
+			vops->pt_update_ops[id].q = q;
+			if (vm->pt_root[id] && !list_empty(&q->multi_gt_list))
+				q = list_next_entry(q, multi_gt_list);
+		} else {
+			vops->pt_update_ops[id].q = vm->q[id];
+		}
+	}
+
+	return number_tiles;
+}
+
 /**
  * xe_vm_ops_execute() - Execute VMA ops

* @vm: The VM.

drivers/gpu/drm/xe/xe_vm.c +69 -9
@@ -2848,21 +2511,81 @@ static int vm_bind_ioctl_ops_lock(struct drm_exec
 *exec,
  */
 struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops
 *vops)
 {
-	struct xe_vma_op *op, *next;
+	struct xe_tile *tile;
 	struct dma_fence *fence = NULL;
+	struct dma_fence **fences = NULL;
+	struct dma_fence_array *cf = NULL;
+	int number_tiles = 0, current_fence = 0, err;
+	u8 id;
+
+	number_tiles = vm_ops_setup_tile_args(vm, vops);
+	if (number_tiles == 0)
+		return ERR_PTR(-ENODATA);
+
+	if (number_tiles > 1) {
+		fences = kmalloc_array(number_tiles, sizeof(*fences),
+				       GFP_KERNEL);
+		if (!fences)
+			return ERR_PTR(-ENOMEM);
+	}
+
+	for_each_tile(tile, vm->xe, id) {
+		if (!vops->pt_update_ops[id].num_ops)
+			continue;
 
-	list_for_each_entry_safe(op, next, &vops->list, link) {
-		if (!IS_ERR(fence)) {
-			dma_fence_put(fence);
-			fence = xe_vma_op_execute(vm, op);
+		err = xe_pt_update_ops_prepare(tile, vops);
+		if (err) {
+			fence = ERR_PTR(err);
+			goto err_out;
 		}
-		if (IS_ERR(fence)) {
-			drm_warn(&vm->xe->drm, "VM op(%d) failed with %ld",
-				 op->base.op, PTR_ERR(fence));
-			fence = ERR_PTR(-ENOSPC);
+	}
+
+	for_each_tile(tile, vm->xe, id) {
+		if (!vops->pt_update_ops[id].num_ops)
+			continue;
+
+		fence = xe_pt_update_ops_run(tile, vops);
+		if (IS_ERR(fence))
+			goto err_out;
+
+		if (fences)
+			fences[current_fence++] = fence;
+	}
+
+	if (fences) {
+		cf = dma_fence_array_create(number_tiles, fences,
+					    vm->composite_fence_ctx,
+					    vm->composite_fence_seqno++,
+					    false);
+		if (!cf) {
+			--vm->composite_fence_seqno;
+			fence = ERR_PTR(-ENOMEM);
+			goto err_out;
 		}
+		fence = &cf->base;
+	}
+
+	for_each_tile(tile, vm->xe, id) {
+		if (!vops->pt_update_ops[id].num_ops)
+			continue;
+
+		xe_pt_update_ops_fini(tile, vops);
 	}
 
+	return fence;
+
+err_out:
+	for_each_tile(tile, vm->xe, id) {
+		if (!vops->pt_update_ops[id].num_ops)
+			continue;
+
+		xe_pt_update_ops_abort(tile, vops);
+	}
+	while (current_fence)
+		dma_fence_put(fences[--current_fence]);
+	kfree(fences);
+	kfree(cf);
+
 	return fence;

}

drivers/gpu/drm/xe/xe_vm.c +2 -4
@@ -2944,12 +2667,10 @@ static int vm_bind_ioctl_ops_execute(struct xe_vm
 *vm,
 		fence = xe_vm_ops_execute(vm, vops);
 		if (IS_ERR(fence)) {
 			err = PTR_ERR(fence);
-			/* FIXME: Killing VM rather than proper error handling */
-			xe_vm_kill(vm, false);
 			goto unlock;
-		} else {
-			vm_bind_ioctl_ops_install_fences(vm, vops, fence);
 		}
+
+		vm_bind_ioctl_ops_install_fences(vm, vops, fence);
 	}
 

unlock:

drivers/gpu/drm/xe/xe_vm.h +4 -1
index b40160b1be01..8201ecb8f05a 100644
@@ -262,9 +262,12 @@ static inline struct dma_resv *xe_vm_resv(struct xe_vm
 *vm)
  */
 #define xe_vm_assert_held(vm) dma_resv_assert_held(xe_vm_resv(vm))
 
-void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma);
+void xe_vm_populate_dummy_rebind(struct xe_vm *vm, struct xe_vma *vma,
+				 u8 tile_mask);
 struct dma_fence *xe_vm_ops_execute(struct xe_vm *vm, struct xe_vma_ops
 *vops);
 
+void xe_vm_kill(struct xe_vm *vm, bool unlocked);
+
 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)

#define vm_dbg drm_dbg
#else

drivers/gpu/drm/xe/xe_vm_types.h +1 -5
index f5d740dcbba3..83cb13275904 100644
@@ -166,29 +166,18 @@ struct xe_vma_op_prefetch {
 
 /** enum xe_vma_op_flags - flags for VMA operation */
 enum xe_vma_op_flags {
-	/** @XE_VMA_OP_FIRST: first VMA operation for a set of syncs */
-	XE_VMA_OP_FIRST			= BIT(0),
-	/** @XE_VMA_OP_LAST: last VMA operation for a set of syncs */
-	XE_VMA_OP_LAST			= BIT(1),
 	/** @XE_VMA_OP_COMMITTED: VMA operation committed */
-	XE_VMA_OP_COMMITTED		= BIT(2),
+	XE_VMA_OP_COMMITTED		= BIT(0),
 	/** @XE_VMA_OP_PREV_COMMITTED: Previous VMA operation

committed */

drivers/gpu/drm/xe/xe_vm_types.h +1 -1
-	XE_VMA_OP_PREV_COMMITTED	= BIT(3),
+	XE_VMA_OP_PREV_COMMITTED	= BIT(1),
 	/** @XE_VMA_OP_NEXT_COMMITTED: Next VMA operation committed

*/

drivers/gpu/drm/xe/xe_vm_types.h +3 -15
-	XE_VMA_OP_NEXT_COMMITTED	= BIT(4),
+	XE_VMA_OP_NEXT_COMMITTED	= BIT(2),
 };
 
 /** struct xe_vma_op - VMA operation */
 struct xe_vma_op {
 	/** @base: GPUVA base operation */
 	struct drm_gpuva_op base;
-	/** @q: exec queue for this operation */
-	struct xe_exec_queue *q;
-	/**
-	 * @syncs: syncs for this operation, only used on first and last
-	 * operation
-	 */
-	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
 	/** @link: async operation link */
@@ -214,19 +203,14 @@ struct xe_vma_ops {
 	struct list_head list;
 	/** @vm: VM */
 	struct xe_vm *vm;
-	/** @q: exec queue these operations */
+	/** @q: exec queue for VMA operations */
 	struct xe_exec_queue *q;
 	/** @syncs: syncs these operation */
 	struct xe_sync_entry *syncs;
 	/** @num_syncs: number of syncs */
 	u32 num_syncs;
 	/** @pt_update_ops: page table update operations */
-	struct {
-		/** @ops: operations */
-		struct xe_vm_pgtable_update_op *ops;
-		/** @num_ops: number of operations */
-		u32 num_ops;
-	} pt_update_ops[XE_MAX_TILES_PER_DEVICE];
+	struct xe_vm_pgtable_update_ops
 pt_update_ops[XE_MAX_TILES_PER_DEVICE];
 };
 

struct xe_vm {

drivers/gpu/drm/xe/xe_vm_types.h +0 -3
@@ -283,9 +267,6 @@ struct xe_vm {
 	 */
 	struct list_head rebind_list;
 
-	/** @rebind_fence: rebind fence from execbuf */
-	struct dma_fence *rebind_fence;
-
 	/**
 	 * @destroy_work: worker to destroy VM, needed as a dma_fence

signaling
* from an irq context can be last put and the destroy needs to be able
--
2.34.1