Skip to contentSkip to conversation

drm/amdgpu: Explicit sync for PRT unmaps

PATCH Superseded Natalie Vock 1 participant 4 messages May 29

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover drm/amdgpu: Explicit sync for PRT unmaps

Hi,

here's another attempt at getting unmaps (especially PRT ones) to be synced explicitly, i.e. without the implicit sync to every other submission in the VM.

The motivator is that many games have come to heavily use sparse bindings, and they often rely on being able to overlap frame work on gfx/compute queues with sparse binding work on sdma. If we sync to every other submission, heavy sparse binding load can stall the gfx/compute submissions for quite a long while, leading to very noticeable stutters and hitches.

Currently, we have one free-list per VM, but random unrelated mappings might stick around in the free-list in addition to the unmaps performed in the current VM update. It is obviously invalid to clear those unrelated mappings with the same explicit sync semantics, so we need a way to separate out the mappings that can be cleared with explicit sync from those that can't.

To introduce that separation, patch 1 wraps all VM update operations in a "VM update context". This context encapsulates some state that is carried around different VM update functions, most importantly the mappings that were freed along the way. amdgpu_vm_clear_freed then only clears the mappings that were actually freed in the update operation.

Patch 2 builds on the update context to add some additional bookkeeping and track if every mapping from the context's free-list actually supports being cleared with explicit sync. If userspace requests it and all conditions are met, explicit sync is then enabled for the PT clearing operations.

In the UAPI, a new VA IOCTL flag is added to request explicit sync, and the minor driver version is bumped to inform userspace of support for the explicit sync flag.

---
Natalie Vock (3):
drm/amdgpu: Add and use vm_update_ctx struct drm/amdgpu: Implement explicit sync for PRT unmaps drm/amdgpu: Bump minor version

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c |  42 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |  30 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c          |  22 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c      |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c          |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c          |  61 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c          |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c        |  21 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c        |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           | 262 +++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h           |  93 +++++++-
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c           |  15 +-
 include/uapi/drm/amdgpu_drm.h                    |   2 +
 13 files changed, 396 insertions(+), 167 deletions(-)

---
base-commit: 0b5b03275d8e8961706e5f61903e41e44e8ebfaa
change-id: 20260529-prt-explicit-sync-3d74b9dd420c

Best regards,

Signature · 1 line
Natalie Vock <[email protected]>
#1 lore raw

PATCH 1/3 drm/amdgpu: Add and use vm_update_ctx struct

This adds a "context" struct to the functions responsible for mapping page tables (and committing the maps to the GPU page-tables).

The core idea here is to record the changes associated with specific VM mapping operations (most importantly, which mappings were freed by each map change). We need this to properly implement explicit sync for unmap operations, which can only be performed for unmaps of either always-valid or PRT mappings.

Before this patch, amdgpu_vm_clear_freed gets a list of pending unmap operations with no insight where they come from - it's very much possible that the list also contains unmap operations for which the sync dependencies are unknown, and implicit sync semantics have to be assumed.

By keeping a separate free-list in the context of each unmap operation and only falling back to the global per-VM freelist if amdgpu_vm_clear_freed can't be executed right now, we sidestep these issues. We can also record information about sync dependencies (and more, in subsequent patches) inside the context struct as well.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c |  42 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |  30 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c          |  22 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c      |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c          |  53 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c          |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c        |  21 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c        |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           | 225 ++++++++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h           |  88 ++++++++-
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c           |  15 +-
 11 files changed, 344 insertions(+), 164 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +34 -8
index 7c01492e69ddd..21759fe6bdfa0 100644
@@ -876,6 +876,7 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 	uint64_t va = mem->va;
 	struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
 	struct amdgpu_bo *bo[2] = {NULL, NULL};
+	struct amdgpu_vm_update_ctx ctx;
 	struct amdgpu_bo_va *bo_va;
 	bool same_hive = false;
 	struct drm_exec exec;
@@ -1000,8 +1001,11 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 			continue;
 		if (attachment[i]->bo_va) {
 			(void)amdgpu_bo_reserve(bo[i], true);
+			amdgpu_vm_update_ctx_init(
+				&ctx, adev, attachment[i]->bo_va->base.vm);
 			if (--attachment[i]->bo_va->ref_count == 0)
-				amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
+				amdgpu_vm_bo_del(&ctx, attachment[i]->bo_va);
+			amdgpu_vm_update_ctx_fini(&ctx);
 			amdgpu_bo_unreserve(bo[i]);
 			list_del(&attachment[i]->list);
 		}
@@ -1014,12 +1018,16 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 
 static void kfd_mem_detach(struct kfd_mem_attachment *attachment)
 {
+	struct amdgpu_vm_update_ctx ctx;
 	struct amdgpu_bo *bo = attachment->bo_va->base.bo;
 
 	pr_debug("\t remove VA 0x%llx in entry %p\n",
 			attachment->va, attachment);
+	amdgpu_vm_update_ctx_init(&ctx, attachment->adev,
+				  attachment->bo_va->base.vm);
 	if (--attachment->bo_va->ref_count == 0)
-		amdgpu_vm_bo_del(attachment->adev, attachment->bo_va);
+		amdgpu_vm_bo_del(&ctx, attachment->bo_va);
+	amdgpu_vm_update_ctx_fini(&ctx);
 	drm_gem_object_put(&bo->tbo.base);
 	list_del(&attachment->list);
 	kfree(attachment);
@@ -1273,22 +1281,27 @@ static int unmap_bo_from_gpuvm(struct kgd_mem *mem,
 	struct amdgpu_bo_va *bo_va = entry->bo_va;
 	struct amdgpu_device *adev = entry->adev;
 	struct amdgpu_vm *vm = bo_va->base.vm;
+	struct amdgpu_vm_update_ctx update_ctx;
 
 	if (bo_va->queue_refcount) {
 		pr_debug("bo_va->queue_refcount %d\n", bo_va->queue_refcount);
 		return -EBUSY;
 	}
 
-	(void)amdgpu_vm_bo_unmap(adev, bo_va, entry->va);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+
+	(void)amdgpu_vm_bo_unmap(&update_ctx, bo_va, entry->va);
 
 	/* VM entity stopped if process killed, don't clear freed pt bo */
 	if (!amdgpu_vm_ready(vm))
-		return 0;
+		goto out;
 
-	(void)amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
+	(void)amdgpu_vm_clear_freed(&update_ctx, &bo_va->last_pt_update);
 
 	(void)amdgpu_sync_fence(sync, bo_va->last_pt_update, GFP_KERNEL);
 
+out:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 	return 0;
 }
 
@@ -1298,6 +1311,7 @@ static int update_gpuvm_pte(struct kgd_mem *mem,
 {
 	struct amdgpu_bo_va *bo_va = entry->bo_va;
 	struct amdgpu_device *adev = entry->adev;
+	struct amdgpu_vm_update_ctx update_ctx;
 	int ret;
 
 	ret = kfd_mem_dmamap_attachment(mem, entry);
@@ -1305,7 +1319,10 @@ static int update_gpuvm_pte(struct kgd_mem *mem,
 		return ret;
 
 	/* Update the page tables  */
-	ret = amdgpu_vm_bo_update(adev, bo_va, false);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, bo_va->base.vm);
+	ret = amdgpu_vm_bo_update(&update_ctx, bo_va, false);
+	amdgpu_vm_update_ctx_fini(&update_ctx);
+
 	if (ret) {
 		pr_err("amdgpu_vm_bo_update failed\n");
 		return ret;
@@ -1319,12 +1336,17 @@ static int map_bo_to_gpuvm(struct kgd_mem *mem,
 			   struct amdgpu_sync *sync,
 			   bool no_update_pte)
 {
+	struct amdgpu_bo_va *bo_va = entry->bo_va;
+	struct amdgpu_device *adev = entry->adev;
+	struct amdgpu_vm_update_ctx update_ctx;
 	int ret;
 
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, bo_va->base.vm);
 	/* Set virtual address for the allocation */
-	ret = amdgpu_vm_bo_map(entry->adev, entry->bo_va, entry->va, 0,
+	ret = amdgpu_vm_bo_map(&update_ctx, bo_va, entry->va, 0,
 			       amdgpu_bo_size(entry->bo_va->base.bo),
 			       entry->pte_flags);
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 	if (ret) {
 		pr_err("Failed to map VA 0x%llx in vm. ret %d\n",
 				entry->va, ret);
@@ -2923,6 +2945,7 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *
 	struct amdgpu_sync sync_obj;
 	unsigned long failed_size = 0;
 	unsigned long total_size = 0;
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct drm_exec exec;
 	int ret;
 
@@ -3032,7 +3055,10 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *
 		struct amdgpu_fpriv *fpriv =
 			container_of(peer_vm, struct amdgpu_fpriv, vm);
 
-		ret = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
+		amdgpu_vm_update_ctx_init(&update_ctx, adev, peer_vm);
+		ret = amdgpu_vm_bo_update(&update_ctx, fpriv->prt_va, false);
+		amdgpu_vm_update_ctx_fini(&update_ctx);
+
 		if (ret) {
 			dev_dbg(adev->dev,
 				"Memory eviction: handle PRT moved failed, pid %8d. Try again.\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +20 -10
index 10d8dcc3a9721..ea9b3983f10b7 100644
@@ -1093,6 +1093,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_bo_list_entry *e;
 	struct amdgpu_bo_va *bo_va;
+	struct amdgpu_vm_update_ctx update_ctx;
 	unsigned int i;
 	int r;
 
@@ -1114,30 +1115,33 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	if (!amdgpu_vm_ready(vm))
 		return -EINVAL;
 
-	r = amdgpu_vm_clear_freed(adev, vm, NULL);
+	r = amdgpu_vm_delayed_free(adev, vm);
 	if (r)
 		return r;
 
-	r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+
+	r = amdgpu_vm_bo_update(&update_ctx, fpriv->prt_va, false);
 	if (r)
-		return r;
+		goto err_fini_ctx;
 
 	r = amdgpu_sync_fence(&p->sync, fpriv->prt_va->last_pt_update,
 			      GFP_KERNEL);
 	if (r)
-		return r;
+		goto err_fini_ctx;
 
 	if (fpriv->csa_va) {
 		bo_va = fpriv->csa_va;
 		BUG_ON(!bo_va);
-		r = amdgpu_vm_bo_update(adev, bo_va, false);
+
+		r = amdgpu_vm_bo_update(&update_ctx, bo_va, false);
 		if (r)
-			return r;
+			goto err_fini_ctx;
 
 		r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update,
 				      GFP_KERNEL);
 		if (r)
-			return r;
+			goto err_fini_ctx;
 	}
 
 	/* FIXME: In theory this loop shouldn't be needed any more when
@@ -1150,16 +1154,18 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 		if (bo_va == NULL)
 			continue;
 
-		r = amdgpu_vm_bo_update(adev, bo_va, false);
+		r = amdgpu_vm_bo_update(&update_ctx, bo_va, false);
 		if (r)
-			return r;
+			goto err_fini_ctx;
 
 		r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update,
 				      GFP_KERNEL);
 		if (r)
-			return r;
+			goto err_fini_ctx;
 	}
 
+	amdgpu_vm_update_ctx_fini(&update_ctx);
+
 	r = amdgpu_vm_handle_moved(adev, vm, &p->exec.ticket);
 	if (r)
 		return r;
@@ -1195,6 +1201,10 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
 	}
 
 	return 0;
+
+err_fini_ctx:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
+	return r;
 }
 
 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c +16 -6
index 11d70fe4f75a9..e1c85e3b211b0 100644
@@ -66,6 +66,7 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			  struct amdgpu_bo *bo, struct amdgpu_bo_va **bo_va,
 			  uint64_t csa_addr, uint32_t size)
 {
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct drm_exec exec;
 	int r;
 
@@ -88,17 +89,21 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error;
 	}
 
-	r = amdgpu_vm_bo_map(adev, *bo_va, csa_addr, 0, size,
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+
+	r = amdgpu_vm_bo_map(&update_ctx, *bo_va, csa_addr, 0, size,
 			     AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
 			     AMDGPU_PTE_EXECUTABLE);
 
 	if (r) {
 		drm_err(adev_to_drm(adev),
 			"failed to do bo_map on static CSA, err=%d\n", r);
-		amdgpu_vm_bo_del(adev, *bo_va);
-		goto error;
+		amdgpu_vm_bo_del(&update_ctx, *bo_va);
+		goto error_ctx;
 	}
 
+error_ctx:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 error:
 	drm_exec_fini(&exec);
 	return r;
@@ -108,6 +113,7 @@ int amdgpu_unmap_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			    struct amdgpu_bo *bo, struct amdgpu_bo_va *bo_va,
 			    uint64_t csa_addr)
 {
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct drm_exec exec;
 	int r;
 
@@ -124,15 +130,19 @@ int amdgpu_unmap_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		}
 	}
 
-	r = amdgpu_vm_bo_unmap(adev, bo_va, csa_addr);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+
+	r = amdgpu_vm_bo_unmap(&update_ctx, bo_va, csa_addr);
 	if (r) {
 		drm_err(adev_to_drm(adev),
 			"failed to do bo_unmap on static CSA, err=%d\n", r);
-		goto error;
+		goto error_ctx;
 	}
 
-	amdgpu_vm_bo_del(adev, bo_va);
+	amdgpu_vm_bo_del(&update_ctx, bo_va);
 
+error_ctx:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 error:
 	drm_exec_fini(&exec);
 	return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index 656c267dbe587..8036dbfa83352 100644
@@ -513,7 +513,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);
 		if (!r)
-			r = amdgpu_vm_clear_freed(adev, vm, NULL);
+			r = amdgpu_vm_delayed_free(adev, vm);
 
 		/* Don't pass 'ticket' to amdgpu_vm_handle_moved: we want the clear=true
 		 * path to be used otherwise we might update the PT of another process
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +30 -23

178 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -1
index 24526e92f9b8c..098417b1403ba 100644
@@ -1548,6 +1548,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 {
 	struct amdgpu_device *adev = drm_to_adev(dev);
 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct amdgpu_bo_list *list;
 	struct amdgpu_bo *pd;
 	u32 pasid;
@@ -1576,7 +1577,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	pasid = fpriv->vm.pasid;
 	pd = amdgpu_bo_ref(fpriv->vm.root.bo);
 	if (!WARN_ON(amdgpu_bo_reserve(pd, true))) {
-		amdgpu_vm_bo_del(adev, fpriv->prt_va);
+		amdgpu_vm_update_ctx_init(&update_ctx, adev, &fpriv->vm);
+		amdgpu_vm_bo_del(&update_ctx, fpriv->prt_va);
+		amdgpu_vm_update_ctx_fini(&update_ctx);
 		amdgpu_bo_unreserve(pd);
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c +14 -7
index f4be192235889..c1bebee4c6027 100644
@@ -67,6 +67,7 @@ static inline u64 amdgpu_seq64_get_va_base(struct amdgpu_device *adev)
 int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		     struct amdgpu_bo_va **bo_va)
 {
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct amdgpu_bo *bo;
 	struct drm_exec exec;
 	u64 seq64_addr;
@@ -94,22 +95,25 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	seq64_addr = amdgpu_seq64_get_va_base(adev) & AMDGPU_GMC_HOLE_MASK;
 
-	r = amdgpu_vm_bo_map(adev, *bo_va, seq64_addr, 0,
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+	r = amdgpu_vm_bo_map(&update_ctx, *bo_va, seq64_addr, 0,
 			     AMDGPU_VA_RESERVED_SEQ64_SIZE,
 			     AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_MTYPE_UC);
 	if (r) {
 		DRM_ERROR("failed to do bo_map on userq sem, err=%d\n", r);
-		amdgpu_vm_bo_del(adev, *bo_va);
-		goto error;
+		amdgpu_vm_bo_del(&update_ctx, *bo_va);
+		goto err_ctx;
 	}
 
-	r = amdgpu_vm_bo_update(adev, *bo_va, false);
+	r = amdgpu_vm_bo_update(&update_ctx, *bo_va, false);
 	if (r) {
 		DRM_ERROR("failed to do vm_bo_update on userq sem\n");
-		amdgpu_vm_bo_del(adev, *bo_va);
-		goto error;
+		amdgpu_vm_bo_del(&update_ctx, *bo_va);
+		goto err_ctx;
 	}
 
+err_ctx:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 error:
 	drm_exec_fini(&exec);
 	return r;
@@ -125,6 +129,7 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  */
 void amdgpu_seq64_unmap(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv)
 {
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct amdgpu_vm *vm;
 	struct amdgpu_bo *bo;
 	struct drm_exec exec;
@@ -149,7 +154,9 @@ void amdgpu_seq64_unmap(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv)
 			goto error;
 	}
 
-	amdgpu_vm_bo_del(adev, fpriv->seq64_va);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+	amdgpu_vm_bo_del(&update_ctx, fpriv->seq64_va);
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 
 	fpriv->seq64_va = NULL;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -1
index 154742bdd5a6f..fbad5106180f6 100644
@@ -1024,6 +1024,7 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
 			 struct amdgpu_vm *vm)
 {
 	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct amdgpu_bo_va *bo_va;
 	struct amdgpu_bo *bo;
 	int ret;
@@ -1045,8 +1046,10 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
 		if (ret)
 			return ret;
 
+		amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
 		/* This moves the bo_va to the idle list */
-		ret = amdgpu_vm_bo_update(adev, bo_va, false);
+		ret = amdgpu_vm_bo_update(&update_ctx, bo_va, false);
+		amdgpu_vm_update_ctx_fini(&update_ctx);
 		if (ret)
 			return ret;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +135 -90

530 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +78 -10

137 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +8 -7
index b169e577e5838..255ff4cd4f887 100644
@@ -2038,32 +2038,33 @@ static int mes_v12_1_map_test_bo(struct amdgpu_device *adev,
 				 struct amdgpu_bo *bo, struct amdgpu_vm *vm,
 				 struct amdgpu_bo_va **bo_va, u64 va, int size)
 {
-	struct amdgpu_sync sync;
+	struct amdgpu_vm_update_ctx update_ctx;
 	int r;
 
 	r = amdgpu_map_static_csa(adev, vm, bo, bo_va, va, size);
 	if (r)
 		return r;
 
-	amdgpu_sync_create(&sync);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
 
-	r = amdgpu_vm_bo_update(adev, *bo_va, false);
+	r = amdgpu_vm_bo_update(&update_ctx, *bo_va, false);
 	if (r) {
 		dev_err(adev->dev, "failed to do vm_bo_update on meta data\n");
 		goto error;
 	}
-	amdgpu_sync_fence(&sync, (*bo_va)->last_pt_update, GFP_KERNEL);
+	amdgpu_sync_fence(&update_ctx.sync, (*bo_va)->last_pt_update,
+			  GFP_KERNEL);
 
 	r = amdgpu_vm_update_pdes(adev, vm, false);
 	if (r) {
 		dev_err(adev->dev, "failed to update pdes on meta data\n");
 		goto error;
 	}
-	amdgpu_sync_fence(&sync, vm->last_update, GFP_KERNEL);
-	amdgpu_sync_wait(&sync, false);
+	amdgpu_sync_fence(&update_ctx.sync, vm->last_update, GFP_KERNEL);
+	amdgpu_sync_wait(&update_ctx.sync, false);
 
 error:
-	amdgpu_sync_free(&sync);
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 	return r;
 }
 
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/3 drm/amdgpu: Implement explicit sync for PRT unmaps

In reply to Natalie Vock

In certain cases, it is possible to skip syncing with every submission in the whole VM before unmapping pagetables. The important condition is that no memory must be released to the system while the GPU still has a chance to access it.

This is true for VM-always-valid buffers since the resv is shared with the VM's root PD and thus there will always be synchronization before unmapping, but also for PRT pages which do not point to any real memory location at all. Bypass implicit sync for unmapping for these buffers if userspace requests it. Userspace can request this via a new VM ioctl flag.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c |  8 ++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 49 +++++++++++++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  |  5 ++++
 include/uapi/drm/amdgpu_drm.h           |  2 ++
 4 files changed, 56 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +5 -3
index e95aaa5167538..a8d1cf3d0d5fc 100644
@@ -813,12 +813,13 @@ amdgpu_gem_va_update_vm(struct amdgpu_vm_update_ctx *ctx,
 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 			  struct drm_file *filp)
 {
-	const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
+	const uint32_t valid_flags =
+		AMDGPU_VM_EXPLICIT_SYNC | AMDGPU_VM_DELAY_UPDATE |
 		AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
 		AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
 		AMDGPU_VM_PAGE_NOALLOC;
-	const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
-		AMDGPU_VM_PAGE_PRT;
+	const uint32_t prt_flags = AMDGPU_VM_EXPLICIT_SYNC |
+				   AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_PRT;
 
 	struct drm_amdgpu_gem_va *args = data;
 	struct drm_gem_object *gobj;
@@ -896,6 +897,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	}
 
 	amdgpu_vm_update_ctx_init(&update_ctx, adev, &fpriv->vm);
+	update_ctx.explicit_sync_unmap = args->flags & AMDGPU_VM_EXPLICIT_SYNC;
 
 	/* Add input syncobj fences (if any) for synchronization. */
 	r = amdgpu_gem_add_input_fence(filp, &update_ctx,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +44 -5

97 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5 -0
index 0bd3fa1100dd6..ae1a8e8f6433b 100644
@@ -293,6 +293,8 @@ struct amdgpu_vm_update_ctx {
 	 * i.e. all previous submissions in the VM are waited on.
 	 */
 	bool unmap_synced;
+
+	bool explicit_sync_unmap;
 };
 
 /**
@@ -660,6 +662,9 @@ int amdgpu_vm_pt_map_tables(struct amdgpu_device *adev, struct amdgpu_vm *vm);
 
 bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);
 
+void amdgpu_vm_update_ctx_add_freed_mapping(
+	struct amdgpu_vm_update_ctx *ctx, struct amdgpu_bo_va *bo_va,
+	struct amdgpu_bo_va_mapping *mapping);
 int amdgpu_vm_update_ctx_ensure_unmap_synced(struct amdgpu_vm_update_ctx *ctx);
 
 /**
include/uapi/drm/amdgpu_drm.h +2 -0
index 9f3090db2f163..1f70a3ff0564a 100644
@@ -900,6 +900,8 @@ struct drm_amdgpu_gem_list_handles_entry {
 #define AMDGPU_VM_MTYPE_RW		(5 << 5)
 /* don't allocate MALL */
 #define AMDGPU_VM_PAGE_NOALLOC		(1 << 9)
+/* Bypass implicit synchronization for VM updates*/
+#define AMDGPU_VM_EXPLICIT_SYNC		(1 << 10)
 
 struct drm_amdgpu_gem_va {
 	/** GEM object handle */
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/3 drm/amdgpu: Bump minor version

In reply to Natalie Vock

To signal availability of explicit PRT sync to userspace.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
index 1781c0c3d010f..f3b86fa4b181e 100644
@@ -127,9 +127,10 @@
  * - 3.62.0 - Add AMDGPU_IDS_FLAGS_MODE_PF, AMDGPU_IDS_FLAGS_MODE_VF & AMDGPU_IDS_FLAGS_MODE_PT
  * - 3.63.0 - GFX12 display DCC supports 256B max compressed block size
  * - 3.64.0 - Userq IP support query
+ * - 3.65.0 - VM PRT explicit sync
  */
 #define KMS_DRIVER_MAJOR	3
-#define KMS_DRIVER_MINOR	64
+#define KMS_DRIVER_MINOR	65
 #define KMS_DRIVER_PATCHLEVEL	0
 
 /*
Signature · 1 line
2.54.0