Skip to contentSkip to conversation

Consolidate and fix root PD fence resverations

PATCH Superseded Benjamin Cheng 3 participants 19 messages Nov 18 – Nov 19

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Consolidate and fix root PD fence resverations

In [1] I originally wanted to just stop using amdgpu_bo_fence. However while attempting to address Christian's comment about reserving the fences upfront when the PD is locked, I realized that there are many places that are not reserving enough fences in the worst-case.

I went through and audited every lock of the VM root PD and I think I have caught everything missing fence reservations in this series. I have lightly tested this and didn't see any issues, and in general this is reserving more fences than the typical case so it shouldn't cause any BUG_ONs.

Patch 1 through 3 here are the reservation fixes. Basically since we started enabling more clear/move entities to be used, we should be reserving the worst-case space upfront. Also when the TLB flush fence was added we did not add reserve space for it.

Patch 4 and 5 are cleanups that I found (not everywhere locking the PD does validation or VM updates), and patches 6 through 8 are the amdgpu_bo_fence() removal series from [1].

[1]: https://lore.kernel.org/amd-gfx/[email protected]/

Benjamin Cheng (8):
drm/amdgpu/vm: reserve more fences in amdgpu_vm_lock_pd drm/amdgpu/dma_buf: reserve AMDGPU_VM_UPDATE_FENCES for move_notify drm/amdgpu/vm: reserve AMDGPU_VM_UPDATE_FENCES for handle_fault drm/amdgpu: reserve only what the caller asks for in lock_pd drm/amdgpu/userq: remove extra fence request when locking PD drm/amdgpu/vm: stop using amdgpu_bo_fence drm/amdgpu/uvd: stop using amdgpu_bo_fence drm/amdgpu/object: get rid of amdgpu_bo_fence

 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 13 ++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |  5 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    | 25 -------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  2 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c    | 12 +++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     |  6 ++---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c       | 17 ++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 14 ++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  8 ++++++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c    |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c          |  3 ++-
 14 files changed, 58 insertions(+), 56 deletions(-)
Signature · 1 line
2.56.1
#1 lore raw

PATCH 1/8 drm/amdgpu/vm: reserve more fences in amdgpu_vm_lock_pd

Any page table update may currently add up to three fences to the root PD's resv (last_unlocked, SDMA commit, and TLB flush). In addition if any PD/PT BOs need to be allocated, it can add up to TTM_NUM_MOVE_FENCES on top.

Introduce AMDGPU_VM_UPDATE_FENCES for this count and reserve it in amdgpu_vm_lock_pd().

Fixes d8a3f0a0348d ("drm/amdgpu: implement TLB flush fence")
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
Fixes ddc3c87584e2 ("drm/amdgpu: clear vm pts upon a vm->generation mismatch")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     | 3 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h     | 8 ++++++++
 3 files changed, 12 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -3
index 2d4a17e11a1f..2403cd911058 100644
@@ -3073,9 +3073,9 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)
 		struct amdgpu_vm *vm = bo_base->vm;
 
 		/*
-		 * Fences for the two SDMA page table updates were already
-		 * reserved by amdgpu_vm_lock_pd() above (it reserves 2 +
-		 * num_fences on the same VM root PD dma_resv).
+		 * Fences for the two page table updates were already
+		 * reserved by amdgpu_vm_lock_pd() above on the same VM
+		 * root PD dma_resv.
 		 */
 		r = amdgpu_vm_clear_freed(adev, vm, NULL);
 		if (!r)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -2
index 8cc5964f29c0..6d8d639e5986 100644
@@ -462,9 +462,8 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec,
 		      unsigned int num_fences)
 {
-	/* We need at least two fences for the VM PD/PT updates */
 	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base,
-				    2 + num_fences);
+				    AMDGPU_VM_UPDATE_FENCES + num_fences);
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +8 -0
index f6285a9d9e98..8117bec01e4e 100644
@@ -187,6 +187,14 @@ struct amdgpu_bo_vm;
 #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
 #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
 
+/*
+ * The number of fence slots needed on the root PD resv for a page table
+ * update: last_unlocked, the SDMA commit fence, the TLB flush fence, plus
+ * the TTM eviction/move fences of any PD/PT BO that is validated or allocated.
+ * This needs to be bumped whenever a new fence is added.
+ */
+#define AMDGPU_VM_UPDATE_FENCES (3 + TTM_NUM_MOVE_FENCES)
+
 /* VMPT level enumerate, and the hiberachy is:
  * PDB3->PDB2->PDB1->PDB0->PTB
  */
Signature · 1 line
2.56.1
maintainer
#2 lore raw

Re: [PATCH 1/8] drm/amdgpu/vm: reserve more fences in amdgpu_vm_lock_pd

Quoted 4 lines · Benjamin Cheng

Any page table update may currently add up to three fences to the root PD's resv (last_unlocked, SDMA commit, and TLB flush). In addition if any PD/PT BOs need to be allocated, it can add up to TTM_NUM_MOVE_FENCES on top.

That is incorrect. The last_unlocked and SDMA commit is on the same timeline, so we only need two.

Quoted 2 lines

Introduce AMDGPU_VM_UPDATE_FENCES for this count and reserve it in amdgpu_vm_lock_pd().

The function amdgpu_vm_lock_pd() was introduced to avoid such a define.

Regards,
Christian.

Quoted 62 lines
Fixes d8a3f0a0348d ("drm/amdgpu: implement TLB flush fence")
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
Fixes ddc3c87584e2 ("drm/amdgpu: clear vm pts upon a vm->generation mismatch")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     | 3 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h     | 8 ++++++++
 3 files changed, 12 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -3
index 2d4a17e11a1f..2403cd911058 100644
@@ -3073,9 +3073,9 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)
 		struct amdgpu_vm *vm = bo_base->vm;
 
 		/*
-		 * Fences for the two SDMA page table updates were already
-		 * reserved by amdgpu_vm_lock_pd() above (it reserves 2 +
-		 * num_fences on the same VM root PD dma_resv).
+		 * Fences for the two page table updates were already
+		 * reserved by amdgpu_vm_lock_pd() above on the same VM
+		 * root PD dma_resv.
 		 */
 		r = amdgpu_vm_clear_freed(adev, vm, NULL);
 		if (!r)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -2
index 8cc5964f29c0..6d8d639e5986 100644
@@ -462,9 +462,8 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec,
 		      unsigned int num_fences)
 {
-	/* We need at least two fences for the VM PD/PT updates */
 	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base,
-				    2 + num_fences);
+				    AMDGPU_VM_UPDATE_FENCES + num_fences);
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +8 -0
index f6285a9d9e98..8117bec01e4e 100644
@@ -187,6 +187,14 @@ struct amdgpu_bo_vm;
 #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)
 #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1)
 
+/*
+ * The number of fence slots needed on the root PD resv for a page table
+ * update: last_unlocked, the SDMA commit fence, the TLB flush fence, plus
+ * the TTM eviction/move fences of any PD/PT BO that is validated or allocated.
+ * This needs to be bumped whenever a new fence is added.
+ */
+#define AMDGPU_VM_UPDATE_FENCES (3 + TTM_NUM_MOVE_FENCES)
+
 /* VMPT level enumerate, and the hiberachy is:
  * PDB3->PDB2->PDB1->PDB0->PTB
  */
#3 lore raw

PATCH 2/8 drm/amdgpu/dma_buf: reserve AMDGPU_VM_UPDATE_FENCES for move_notify

In reply to Benjamin Cheng

This is about to update the VM so we need to use the macro previously introduced to make sure there are enough fence slots.

Fixes d8a3f0a0348d ("drm/amdgpu: implement TLB flush fence")
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index 7b85152d6872..d3a7cf9c274c 100644
@@ -562,7 +562,7 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)
 		}
 
 		/* Reserve fences for two SDMA page table updates */
-		r = dma_resv_reserve_fences(resv, 2);
+		r = dma_resv_reserve_fences(resv, AMDGPU_VM_UPDATE_FENCES);
 		if (!r)
 			r = amdgpu_vm_clear_freed(adev, vm, NULL);
 
Signature · 1 line
2.56.1
maintainer
#4 lore raw

Re: [PATCH 2/8] drm/amdgpu/dma_buf: reserve AMDGPU_VM_UPDATE_FENCES for move_notify

Quoted 2 lines · Benjamin Cheng

This is about to update the VM so we need to use the macro previously introduced to make sure there are enough fence slots.

The amdgpu_dma_buf_move_notify() path should use the unlocked approach I'm currently working on the KFD team and Timur.

So that here is unnecessary to begin with.

Regards,
Christian.

Quoted 21 lines
Fixes d8a3f0a0348d ("drm/amdgpu: implement TLB flush fence")
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index 7b85152d6872..d3a7cf9c274c 100644
@@ -562,7 +562,7 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)
 		}
 
 		/* Reserve fences for two SDMA page table updates */
-		r = dma_resv_reserve_fences(resv, 2);
+		r = dma_resv_reserve_fences(resv, AMDGPU_VM_UPDATE_FENCES);
 		if (!r)
 			r = amdgpu_vm_clear_freed(adev, vm, NULL);
 
#5 lore raw

PATCH 3/8 drm/amdgpu/vm: reserve AMDGPU_VM_UPDATE_FENCES for handle_fault

In reply to Benjamin Cheng

Reserving 1 hasn't been enough here for a while (last_unlocked was never considered originally).

Fixes 2b665c3735b3 ("drm/amdgpu: reserve fence slot to update page table")
Fixes d8a3f0a0348d ("drm/amdgpu: implement TLB flush fence")
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 6d8d639e5986..f4a836594e7c 100644
@@ -3186,7 +3186,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 		value = 0;
 	}
 
-	r = dma_resv_reserve_fences(vm->root.bo->tbo.base.resv, 1);
+	r = dma_resv_reserve_fences(vm->root.bo->tbo.base.resv, AMDGPU_VM_UPDATE_FENCES);
 	if (r) {
 		pr_debug("failed %d to reserve fence slot\n", r);
 		goto error_unlock;
Signature · 1 line
2.56.1
maintainer
#6 lore raw

Re: [PATCH 3/8] drm/amdgpu/vm: reserve AMDGPU_VM_UPDATE_FENCES for handle_fault

Quoted 2 lines · Benjamin Cheng

Reserving 1 hasn't been enough here for a while (last_unlocked was never considered originally).

Again completely unnecessary. The fault handling can't use a TLB fence.

Regards,
Christian.

Quoted 22 lines
Fixes 2b665c3735b3 ("drm/amdgpu: reserve fence slot to update page table")
Fixes d8a3f0a0348d ("drm/amdgpu: implement TLB flush fence")
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 6d8d639e5986..f4a836594e7c 100644
@@ -3186,7 +3186,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
 		value = 0;
 	}
 
-	r = dma_resv_reserve_fences(vm->root.bo->tbo.base.resv, 1);
+	r = dma_resv_reserve_fences(vm->root.bo->tbo.base.resv, AMDGPU_VM_UPDATE_FENCES);
 	if (r) {
 		pr_debug("failed %d to reserve fence slot\n", r);
 		goto error_unlock;
#7 lore raw

PATCH 4/8 drm/amdgpu: reserve only what the caller asks for in lock_pd

In reply to Benjamin Cheng

amdgpu_vm_lock_pd() reserved AMDGPU_VM_UPDATE_FENCES for every caller, including ones that only lock the root PD to add or remove a mapping and never update the page tables.

Address this by making it only reserve what the caller asks for, so that such callers have the option to not over-reserve. The only touched call-sites in this change are the ones that do require fences.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c          |  5 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c       |  6 ++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c        |  6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           |  8 +++++---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c             |  3 ++-
 7 files changed, 27 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8 -5
index a6616447abc8..56f761089b87 100644
@@ -1188,7 +1188,8 @@ static int reserve_bo_and_vm(struct kgd_mem *mem,
 	ctx->sync = &mem->sync;
 	drm_exec_init(&ctx->exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
 	drm_exec_until_all_locked(&ctx->exec) {
-		ret = amdgpu_vm_lock_pd(vm, &ctx->exec, 2);
+		ret = amdgpu_vm_lock_pd(vm, &ctx->exec,
+					AMDGPU_VM_UPDATE_FENCES + 2);
 		drm_exec_retry_on_contention(&ctx->exec);
 		if (unlikely(ret))
 			goto error;
@@ -1235,8 +1236,8 @@ static int reserve_bo_and_cond_vms(struct kgd_mem *mem,
 				&& map_type != BO_VM_ALL))
 				continue;
 
-			ret = amdgpu_vm_lock_pd(entry->bo_va->base.vm,
-						&ctx->exec, 2);
+			ret = amdgpu_vm_lock_pd(entry->bo_va->base.vm, &ctx->exec,
+						AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&ctx->exec);
 			if (unlikely(ret))
 				goto error;
@@ -2729,7 +2730,8 @@ static int validate_invalid_user_pages(struct amdkfd_process_info *process_info)
 		/* Reserve all the page directories */
 		list_for_each_entry(peer_vm, &process_info->vm_list_head,
 				    vm_list_node) {
-			ret = amdgpu_vm_lock_pd(peer_vm, &exec, 2);
+			ret = amdgpu_vm_lock_pd(peer_vm, &exec,
+						AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(ret))
 				goto unreserve_out;
@@ -2993,7 +2995,8 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *
 	drm_exec_until_all_locked(&exec) {
 		list_for_each_entry(peer_vm, &process_info->vm_list_head,
 				    vm_list_node) {
-			ret = amdgpu_vm_lock_pd(peer_vm, &exec, 2);
+			ret = amdgpu_vm_lock_pd(peer_vm, &exec,
+						AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(ret)) {
 				pr_err("Locking VM PD failed, ret: %d\n", ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -1
index 71e2b83faa12..771ab4388eb1 100644
@@ -940,7 +940,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	}
 
 	drm_exec_until_all_locked(&p->exec) {
-		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec, 1 + p->gang_size);
+		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec,
+				      AMDGPU_VM_UPDATE_FENCES + 1 + p->gang_size);
 		drm_exec_retry_on_contention(&p->exec);
 		if (unlikely(r))
 			goto out_free_user_pages;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -2
index eb7473ed9e0e..dfb44426b1e2 100644
@@ -345,7 +345,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 		if (unlikely(r))
 			goto out_unlock;
 
-		r = amdgpu_vm_lock_pd(vm, &exec, 0);
+		r = amdgpu_vm_lock_pd(vm, &exec, AMDGPU_VM_UPDATE_FENCES);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(r))
 			goto out_unlock;
@@ -1002,7 +1002,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 				goto error;
 		}
 
-		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
+		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec,
+				      AMDGPU_VM_UPDATE_FENCES + 2);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(r))
 			goto error;
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +4 -2
index 2403cd911058..8a613d83ed53 100644
@@ -3053,7 +3053,8 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)
 			goto fini;
 
 		for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
-			r = amdgpu_vm_lock_pd(bo_base->vm, &exec, 0);
+			r = amdgpu_vm_lock_pd(bo_base->vm, &exec,
+					      AMDGPU_VM_UPDATE_FENCES);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(r))
 				goto fini;
@@ -4788,7 +4789,8 @@ static int amdgpu_ualink_reserve_npa_vm_and_bos(struct amdgpu_device *adev,
 				goto out;
 		}
 
-		r = amdgpu_vm_lock_pd(&adev->ualink.npa_vm, exec, 0);
+		r = amdgpu_vm_lock_pd(&adev->ualink.npa_vm, exec,
+				      AMDGPU_VM_UPDATE_FENCES);
 		drm_exec_retry_on_contention(exec);
 		if (unlikely(r))
 			goto out;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -3
index ed6f3b67e872..73e58d1a32ba 100644
@@ -1054,10 +1054,10 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
 		/*
-		 * Rearm adds one BOOKKEEP fence and validation may queue move fences
-		 * on the root PD BO, so reserve caller-side slots accordingly.
+		 * Rearm adds one BOOKKEEP fence and validation requires
+		 * AMDGPU_VM_UPDATE_FENCES.
 		 */
-		ret = amdgpu_vm_lock_pd(vm, &exec, TTM_NUM_MOVE_FENCES + 1);
+		ret = amdgpu_vm_lock_pd(vm, &exec, AMDGPU_VM_UPDATE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto unlock_all;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -3
index f4a836594e7c..daf0e75d0bb1 100644
@@ -455,15 +455,17 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
  *
  * @vm: vm providing the BOs
  * @exec: drm execution context
- * @num_fences: number of extra fences to reserve
+ * @num_fences: number of fence slots to reserve, 0 to only lock the PD
  *
  * Lock the VM root PD in the DRM execution context.
  */
 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec,
 		      unsigned int num_fences)
 {
-	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base,
-				    AMDGPU_VM_UPDATE_FENCES + num_fences);
+	if (!num_fences)
+		return drm_exec_lock_obj(exec, &vm->root.bo->tbo.base);
+
+	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base, num_fences);
 }
 
 /**
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
index 3f32507e2aae..6dcc12289e92 100644
@@ -1686,7 +1686,8 @@ static int svm_range_reserve_bos(struct svm_validate_context *ctx, bool intr)
 			}
 			vm = drm_priv_to_vm(pdd->drm_priv);
 
-			r = amdgpu_vm_lock_pd(vm, &ctx->exec, 2);
+			r = amdgpu_vm_lock_pd(vm, &ctx->exec,
+					      AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&ctx->exec);
 			if (unlikely(r)) {
 				pr_debug("failed %d to reserve bo\n", r);
Signature · 1 line
2.56.1
maintainer
#8 lore raw

Re: [PATCH 4/8] drm/amdgpu: reserve only what the caller asks for in lock_pd

Quoted 3 lines · Benjamin Cheng

amdgpu_vm_lock_pd() reserved AMDGPU_VM_UPDATE_FENCES for every caller, including ones that only lock the root PD to add or remove a mapping and never update the page tables.

Yeah clear NAK.

First of all reserving fences is cheap there is no need to avoid that.

Then that completely defines the purpose of the amdgpu_vm_lock_pd() function.

Regards,
Christian.

Quoted 179 lines

Address this by making it only reserve what the caller asks for, so that such callers have the option to not over-reserve. The only touched call-sites in this change are the ones that do require fences.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c          |  5 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c       |  6 ++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c        |  6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           |  8 +++++---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c             |  3 ++-
 7 files changed, 27 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8 -5
index a6616447abc8..56f761089b87 100644
@@ -1188,7 +1188,8 @@ static int reserve_bo_and_vm(struct kgd_mem *mem,
 	ctx->sync = &mem->sync;
 	drm_exec_init(&ctx->exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
 	drm_exec_until_all_locked(&ctx->exec) {
-		ret = amdgpu_vm_lock_pd(vm, &ctx->exec, 2);
+		ret = amdgpu_vm_lock_pd(vm, &ctx->exec,
+					AMDGPU_VM_UPDATE_FENCES + 2);
 		drm_exec_retry_on_contention(&ctx->exec);
 		if (unlikely(ret))
 			goto error;
@@ -1235,8 +1236,8 @@ static int reserve_bo_and_cond_vms(struct kgd_mem *mem,
 				&& map_type != BO_VM_ALL))
 				continue;
 
-			ret = amdgpu_vm_lock_pd(entry->bo_va->base.vm,
-						&ctx->exec, 2);
+			ret = amdgpu_vm_lock_pd(entry->bo_va->base.vm, &ctx->exec,
+						AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&ctx->exec);
 			if (unlikely(ret))
 				goto error;
@@ -2729,7 +2730,8 @@ static int validate_invalid_user_pages(struct amdkfd_process_info *process_info)
 		/* Reserve all the page directories */
 		list_for_each_entry(peer_vm, &process_info->vm_list_head,
 				    vm_list_node) {
-			ret = amdgpu_vm_lock_pd(peer_vm, &exec, 2);
+			ret = amdgpu_vm_lock_pd(peer_vm, &exec,
+						AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(ret))
 				goto unreserve_out;
@@ -2993,7 +2995,8 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *
 	drm_exec_until_all_locked(&exec) {
 		list_for_each_entry(peer_vm, &process_info->vm_list_head,
 				    vm_list_node) {
-			ret = amdgpu_vm_lock_pd(peer_vm, &exec, 2);
+			ret = amdgpu_vm_lock_pd(peer_vm, &exec,
+						AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(ret)) {
 				pr_err("Locking VM PD failed, ret: %d\n", ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -1
index 71e2b83faa12..771ab4388eb1 100644
@@ -940,7 +940,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 	}
 
 	drm_exec_until_all_locked(&p->exec) {
-		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec, 1 + p->gang_size);
+		r = amdgpu_vm_lock_pd(&fpriv->vm, &p->exec,
+				      AMDGPU_VM_UPDATE_FENCES + 1 + p->gang_size);
 		drm_exec_retry_on_contention(&p->exec);
 		if (unlikely(r))
 			goto out_free_user_pages;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -2
index eb7473ed9e0e..dfb44426b1e2 100644
@@ -345,7 +345,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 		if (unlikely(r))
 			goto out_unlock;
 
-		r = amdgpu_vm_lock_pd(vm, &exec, 0);
+		r = amdgpu_vm_lock_pd(vm, &exec, AMDGPU_VM_UPDATE_FENCES);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(r))
 			goto out_unlock;
@@ -1002,7 +1002,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 				goto error;
 		}
 
-		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
+		r = amdgpu_vm_lock_pd(&fpriv->vm, &exec,
+				      AMDGPU_VM_UPDATE_FENCES + 2);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(r))
 			goto error;
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +4 -2
index 2403cd911058..8a613d83ed53 100644
@@ -3053,7 +3053,8 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)
 			goto fini;
 
 		for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
-			r = amdgpu_vm_lock_pd(bo_base->vm, &exec, 0);
+			r = amdgpu_vm_lock_pd(bo_base->vm, &exec,
+					      AMDGPU_VM_UPDATE_FENCES);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(r))
 				goto fini;
@@ -4788,7 +4789,8 @@ static int amdgpu_ualink_reserve_npa_vm_and_bos(struct amdgpu_device *adev,
 				goto out;
 		}
 
-		r = amdgpu_vm_lock_pd(&adev->ualink.npa_vm, exec, 0);
+		r = amdgpu_vm_lock_pd(&adev->ualink.npa_vm, exec,
+				      AMDGPU_VM_UPDATE_FENCES);
 		drm_exec_retry_on_contention(exec);
 		if (unlikely(r))
 			goto out;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -3
index ed6f3b67e872..73e58d1a32ba 100644
@@ -1054,10 +1054,10 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
 		/*
-		 * Rearm adds one BOOKKEEP fence and validation may queue move fences
-		 * on the root PD BO, so reserve caller-side slots accordingly.
+		 * Rearm adds one BOOKKEEP fence and validation requires
+		 * AMDGPU_VM_UPDATE_FENCES.
 		 */
-		ret = amdgpu_vm_lock_pd(vm, &exec, TTM_NUM_MOVE_FENCES + 1);
+		ret = amdgpu_vm_lock_pd(vm, &exec, AMDGPU_VM_UPDATE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto unlock_all;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -3
index f4a836594e7c..daf0e75d0bb1 100644
@@ -455,15 +455,17 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
  *
  * @vm: vm providing the BOs
  * @exec: drm execution context
- * @num_fences: number of extra fences to reserve
+ * @num_fences: number of fence slots to reserve, 0 to only lock the PD
  *
  * Lock the VM root PD in the DRM execution context.
  */
 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec,
 		      unsigned int num_fences)
 {
-	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base,
-				    AMDGPU_VM_UPDATE_FENCES + num_fences);
+	if (!num_fences)
+		return drm_exec_lock_obj(exec, &vm->root.bo->tbo.base);
+
+	return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base, num_fences);
 }
 
 /**
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
index 3f32507e2aae..6dcc12289e92 100644
@@ -1686,7 +1686,8 @@ static int svm_range_reserve_bos(struct svm_validate_context *ctx, bool intr)
 			}
 			vm = drm_priv_to_vm(pdd->drm_priv);
 
-			r = amdgpu_vm_lock_pd(vm, &ctx->exec, 2);
+			r = amdgpu_vm_lock_pd(vm, &ctx->exec,
+					      AMDGPU_VM_UPDATE_FENCES + 2);
 			drm_exec_retry_on_contention(&ctx->exec);
 			if (unlikely(r)) {
 				pr_debug("failed %d to reserve bo\n", r);
#9 lore raw

PATCH 5/8 drm/amdgpu/userq: remove extra fence request when locking PD

In reply to Benjamin Cheng

These two fences are never used, so just request no fences (lock only).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
index dd976a50e053..855e521eb201 100644
@@ -382,7 +382,7 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(queue->vm, &exec, 1);
+		ret = amdgpu_vm_lock_pd(queue->vm, &exec, 0);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto lock_error;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
index 7abb9d95ef59..8b07e21d2686 100644
@@ -48,7 +48,7 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
+		ret = amdgpu_vm_lock_pd(vm, &exec, 0);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto fail_lock;
Signature · 1 line
2.56.1
#10 lore raw

Re: [PATCH 5/8] drm/amdgpu/userq: remove extra fence request when locking PD

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] drm/amdgpu: ignored WPTR mapping…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] drm/amdgpu: ignored WPTR mapping offsets in userqueue synchronization [1]
--

commit c736ecb92e4ba4e91728f33ea27cd11d90100ae5
Author: Benjamin Cheng <[email protected]>

drm/amdgpu/userq: remove extra fence request when locking PD

This commit changes amdgpu_vm_lock_pd() calls in amdgpu_userq_fence_read_wptr() and mes_userq_create_wptr_mapping() to request zero fence slots instead of one.

Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
index 6d1f5811d39c3..07a0af9feaf08 100644
@@ -380,7 +380,7 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(queue->vm, &exec, 1);
+		ret = amdgpu_vm_lock_pd(queue->vm, &exec, 0);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto lock_error;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 7abb9d95ef59a..8b07e21d26862 100644

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_create_wptr_mapping(struct amdgpu_device *adev, · line 54

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
@@ -48,7 +48,7 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
+		ret = amdgpu_vm_lock_pd(vm, &exec, 0);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto fail_lock;

[Severity: High]
This problem wasn't introduced by this patch, but does mes_userq_create_wptr_mapping() ignore the internal mapping and sub-page offset when setting queue->wptr_obj.gpu_addr [1]?

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c:mes_userq_create_wptr_mapping() {
    ...
	queue->wptr_obj.gpu_addr = amdgpu_bo_gpu_offset(wptr_obj->obj);
    ...
}

When a usermode queue places its WPTR at a non-zero offset within the backing BO, mes_userq_create_wptr_mapping() sets queue->wptr_obj.gpu_addr to the base GPU offset of wptr_obj->obj without adding the mapping and sub-page offsets (and if amdgpu_userq_fence_read_wptr() reads *ptr at offset 0 directly, it misses the offset on the CPU side as well). This causes the firmware and CPU fence polling to read the wrong memory address, leading to premature fence signaling or command submission deadlocks.

[1] https://sashiko.dev/#/bug/linux-485b27a5-4aad-464a-b5b7-ad02f7bafded

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer Reviewed-by on v1 5/8
#11 lore raw

Re: [PATCH 5/8] drm/amdgpu/userq: remove extra fence request when locking PD

In reply to Benjamin Cheng
Quoted 3 lines · Benjamin Cheng

These two fences are never used, so just request no fences (lock only).

Quoted 31 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
index dd976a50e053..855e521eb201 100644
@@ -382,7 +382,7 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(queue->vm, &exec, 1);
+		ret = amdgpu_vm_lock_pd(queue->vm, &exec, 0);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto lock_error;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
index 7abb9d95ef59..8b07e21d2686 100644
@@ -48,7 +48,7 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 2);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
+		ret = amdgpu_vm_lock_pd(vm, &exec, 0);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto fail_lock;
#12 lore raw

PATCH 6/8 drm/amdgpu/vm: stop using amdgpu_bo_fence

In reply to Benjamin Cheng

Switch to stop using this deprecated helper.

The original could be incorrect under certain circumstances, since the amdgpu_bo_fence only reserves one slot, and at the time of calling the SDMA commit and TLB flush fences were not added yet (the reserve_fences is not additive). In addition this allows us to switch to use BOOKKEEP usage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
index daf0e75d0bb1..2a8abc9684b7 100644
@@ -1216,7 +1216,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
 		struct dma_fence *tmp = dma_fence_get_stub();
 
-		amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true);
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv, vm->last_unlocked,
+				   DMA_RESV_USAGE_BOOKKEEP);
 		swap(vm->last_unlocked, tmp);
 		dma_fence_put(tmp);
 	}
Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 6/8
#13 lore raw

Re: [PATCH 6/8] drm/amdgpu/vm: stop using amdgpu_bo_fence

Quoted 9 lines · Benjamin Cheng

Switch to stop using this deprecated helper.

The original could be incorrect under certain circumstances, since the amdgpu_bo_fence only reserves one slot, and at the time of calling the SDMA commit and TLB flush fences were not added yet (the reserve_fences is not additive). In addition this allows us to switch to use BOOKKEEP usage.

Quoted 18 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
index daf0e75d0bb1..2a8abc9684b7 100644
@@ -1216,7 +1216,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
 		struct dma_fence *tmp = dma_fence_get_stub();
 
-		amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true);
+		dma_resv_add_fence(vm->root.bo->tbo.base.resv, vm->last_unlocked,
+				   DMA_RESV_USAGE_BOOKKEEP);
 		swap(vm->last_unlocked, tmp);
 		dma_fence_put(tmp);
 	}
#14 lore raw

PATCH 7/8 drm/amdgpu/uvd: stop using amdgpu_bo_fence

In reply to Benjamin Cheng

This helper is deprecated since it is incorrect in most circumstances. This was a circumstance where it was mostly correct but it is the only remaining usage, so get rid of it here.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +14 -3
index d88dde859615..9570db033b18 100644
@@ -1154,16 +1154,24 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
 		if (r)
 			goto err_free;
 	} else {
+		r = amdgpu_bo_reserve(bo, true);
+		if (r)
+			goto err_free;
+
 		r = drm_sched_job_add_resv_dependencies(&job->base,
 							bo->tbo.base.resv,
 							DMA_RESV_USAGE_KERNEL);
 		if (r)
-			goto err_free;
+			goto err_unreserve;
 
 		f = amdgpu_job_submit(job);
 
-		amdgpu_bo_reserve(bo, true);
-		amdgpu_bo_fence(bo, f, false);
+		r = dma_resv_reserve_fences(bo->tbo.base.resv, 1);
+		if (r) {
+			dma_fence_wait(f, false);
+			r = 0;
+		} else
+			dma_resv_add_fence(bo->tbo.base.resv, f, DMA_RESV_USAGE_BOOKKEEP);
 		amdgpu_bo_unreserve(bo);
 	}
 
@@ -1173,6 +1181,9 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
 
 	return 0;
 
+err_unreserve:
+	amdgpu_bo_unreserve(bo);
+
 err_free:
 	amdgpu_job_free(job);
 	return r;
Signature · 1 line
2.56.1
maintainer
#15 lore raw

Re: [PATCH 7/8] drm/amdgpu/uvd: stop using amdgpu_bo_fence

Quoted 23 lines · Benjamin Cheng

This helper is deprecated since it is incorrect in most circumstances. This was a circumstance where it was mostly correct but it is the only remaining usage, so get rid of it here.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +4 -0
index d88dde859615..9570db033b18 100644
@@ -1154,16 +1154,24 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
 		if (r)
 			goto err_free;
 	} else {
+		r = amdgpu_bo_reserve(bo, true);
+		if (r)
+			goto err_free;
+
 		r = drm_sched_job_add_resv_dependencies(&job->base,
 							bo->tbo.base.resv,

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo, · line 1169

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +2 -3
 							DMA_RESV_USAGE_KERNEL);
 		if (r)
-			goto err_free;
+			goto err_unreserve;
 
 		f = amdgpu_job_submit(job);
 
-		amdgpu_bo_reserve(bo, true);
-		amdgpu_bo_fence(bo, f, false);
+		r = dma_resv_reserve_fences(bo->tbo.base.resv, 1);

That line should come before the amdgpu_job_submit().

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo, · line 1171

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +2 -0
+		if (r) {
+			dma_fence_wait(f, false);

This way you also don't need that hack here.

Regards,
Christian.

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +6 -0
+			r = 0;
+		} else
+			dma_resv_add_fence(bo->tbo.base.resv, f, DMA_RESV_USAGE_BOOKKEEP);
 		amdgpu_bo_unreserve(bo);
 	}
 
@@ -1173,6 +1181,9 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
 
 	return 0;
 
+err_unreserve:
+	amdgpu_bo_unreserve(bo);
+
 err_free:
 	amdgpu_job_free(job);
 	return r;
#16 lore raw

PATCH 8/8 drm/amdgpu/object: get rid of amdgpu_bo_fence

In reply to Benjamin Cheng

All uses of this function have been removed, so we can now get rid of this function.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 25 ----------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 --
 2 files changed, 27 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -25
index d4651f2d437c..ba9b5ed71cf7 100644
@@ -1463,31 +1463,6 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
 	return 0;
 }
 
-/**
- * amdgpu_bo_fence - add fence to buffer object
- *
- * @bo: buffer object in question
- * @fence: fence to add
- * @shared: true if fence should be added shared
- *
- */
-void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
-		     bool shared)
-{
-	struct dma_resv *resv = bo->tbo.base.resv;
-	int r;
-
-	r = dma_resv_reserve_fences(resv, 1);
-	if (r) {
-		/* As last resort on OOM we block for the fence */
-		dma_fence_wait(fence, false);
-		return;
-	}
-
-	dma_resv_add_fence(resv, fence, shared ? DMA_RESV_USAGE_READ :
-			   DMA_RESV_USAGE_WRITE);
-}
-
 /**
  * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -2
index 8e67a8e60572..33a1bfce79e1 100644
@@ -305,8 +305,6 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 			   struct ttm_resource *new_mem);
 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo);
 vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo);
-void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
-		     bool shared);
 int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv,
 			     enum amdgpu_sync_mode sync_mode, void *owner,
 			     bool intr);
Signature · 1 line
2.56.1
maintainer Reviewed-by on v1 8/8
#17 lore raw

Re: [PATCH 8/8] drm/amdgpu/object: get rid of amdgpu_bo_fence

Quoted 4 lines · Benjamin Cheng

All uses of this function have been removed, so we can now get rid of this function.

Quoted 54 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 25 ----------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 --
 2 files changed, 27 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -25
index d4651f2d437c..ba9b5ed71cf7 100644
@@ -1463,31 +1463,6 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
 	return 0;
 }
 
-/**
- * amdgpu_bo_fence - add fence to buffer object
- *
- * @bo: buffer object in question
- * @fence: fence to add
- * @shared: true if fence should be added shared
- *
- */
-void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
-		     bool shared)
-{
-	struct dma_resv *resv = bo->tbo.base.resv;
-	int r;
-
-	r = dma_resv_reserve_fences(resv, 1);
-	if (r) {
-		/* As last resort on OOM we block for the fence */
-		dma_fence_wait(fence, false);
-		return;
-	}
-
-	dma_resv_add_fence(resv, fence, shared ? DMA_RESV_USAGE_READ :
-			   DMA_RESV_USAGE_WRITE);
-}
-
 /**
  * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -2
index 8e67a8e60572..33a1bfce79e1 100644
@@ -305,8 +305,6 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
 			   struct ttm_resource *new_mem);
 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo);
 vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo);
-void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
-		     bool shared);
 int amdgpu_bo_sync_wait_resv(struct amdgpu_device *adev, struct dma_resv *resv,
 			     enum amdgpu_sync_mode sync_mode, void *owner,
 			     bool intr);
#18 lore raw
In reply to Benjamin Cheng
Quoted 22 lines · Benjamin Cheng

In [1] I originally wanted to just stop using amdgpu_bo_fence. However while attempting to address Christian's comment about reserving the fences upfront when the PD is locked, I realized that there are many places that are not reserving enough fences in the worst-case.

I went through and audited every lock of the VM root PD and I think I have caught everything missing fence reservations in this series. I have lightly tested this and didn't see any issues, and in general this is reserving more fences than the typical case so it shouldn't cause any BUG_ONs.

Patch 1 through 3 here are the reservation fixes. Basically since we started enabling more clear/move entities to be used, we should be reserving the worst-case space upfront. Also when the TLB flush fence was added we did not add reserve space for it.

Patch 4 and 5 are cleanups that I found (not everywhere locking the PD does validation or VM updates), and patches 6 through 8 are the amdgpu_bo_fence() removal series from [1].

[1]: https://lore.kernel.org/amd-gfx/[email protected]/

Quoted 9 lines

Benjamin Cheng (8):
drm/amdgpu/vm: reserve more fences in amdgpu_vm_lock_pd drm/amdgpu/dma_buf: reserve AMDGPU_VM_UPDATE_FENCES for move_notify drm/amdgpu/vm: reserve AMDGPU_VM_UPDATE_FENCES for handle_fault drm/amdgpu: reserve only what the caller asks for in lock_pd drm/amdgpu/userq: remove extra fence request when locking PD drm/amdgpu/vm: stop using amdgpu_bo_fence drm/amdgpu/uvd: stop using amdgpu_bo_fence drm/amdgpu/object: get rid of amdgpu_bo_fence

Sorry, please disregard this series. I forgot two patches, sending them out shortly...

Quoted 17 lines
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 13 ++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c        |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |  5 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    | 25 -------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  2 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c    | 12 +++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     |  6 ++---
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c       | 17 ++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c        | 14 ++++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |  8 ++++++
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c    |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c          |  3 ++-
 14 files changed, 58 insertions(+), 56 deletions(-)