mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Natalie Vock <nat@pixelcluster.dev>
To: "Felix Kuehling" <Felix.Kuehling@amd.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"Christian König" <christian.koenig@amd.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Timur Kristóf" <timur.kristof@gmail.com>
Cc: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	 linux-kernel@vger.kernel.org,
	Natalie Vock <nat@pixelcluster.dev>
Subject: [PATCH v2 02/10] drm/amdgpu: Add and use vm_update_ctx struct
Date: Tue, 17 Nov 2026 13:50:08 +0100	[thread overview]
Message-ID: <20261117-prt-explicit-sync-v2-2-bb3851cb6a0c@pixelcluster.dev> (raw)
In-Reply-To: <20261117-prt-explicit-sync-v2-0-bb3851cb6a0c@pixelcluster.dev>

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.

Signed-off-by: Natalie Vock <nat@pixelcluster.dev>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c |  30 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c           |  31 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c          |  18 +-
 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_seq64.c        |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c       |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c        |  10 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c           | 248 +++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h           |  59 +++++-
 10 files changed, 317 insertions(+), 144 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index f060b206c255f..08ab754e56d3d 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -1286,22 +1286,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;
 }
 
@@ -1311,6 +1316,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);
@@ -1318,7 +1324,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;
@@ -1332,12 +1341,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);
@@ -2982,6 +2996,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;
 
@@ -3091,7 +3106,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",
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
index 71e2b83faa12b..d1f5ec9a3ef0f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
@@ -1126,6 +1126,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;
 
@@ -1147,34 +1148,36 @@ 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, NULL);
 	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;
-		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;
 	}
 
 	if (fpriv->seq64_va) {
 		bo_va = fpriv->seq64_va;
-		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");
 			return r;
@@ -1196,16 +1199,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, drm_exec_ticket(&p->exec));
 	if (r)
 		return r;
@@ -1228,6 +1233,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)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c
index 11d70fe4f75a9..f33dfdb6efe2f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c
@@ -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,7 +89,9 @@ 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);
 
@@ -96,9 +99,11 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		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;
+		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);
 
+error_ctx:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 error:
 	drm_exec_fini(&exec);
 	return r;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c
index 7b85152d6872c..cecb2e95b776c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c
@@ -564,7 +564,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, NULL);
 
 		/* 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
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
index af8f96c8a2b59..55bdf5070837a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -51,6 +51,7 @@
 
 static int
 amdgpu_gem_add_input_fence(struct drm_file *filp,
+			   struct amdgpu_vm_update_ctx *update_ctx,
 			   uint64_t syncobj_handles_array,
 			   uint32_t num_syncobj_handles)
 {
@@ -77,9 +78,10 @@ amdgpu_gem_add_input_fence(struct drm_file *filp,
 		if (ret)
 			goto free_memdup;
 
-		dma_fence_wait(fence, false);
+		ret = amdgpu_sync_fence(&update_ctx->sync, fence, GFP_KERNEL);
+		if (ret)
+			goto free_memdup;
 
-		/* TODO: optimize async handling */
 		dma_fence_put(fence);
 	}
 
@@ -361,12 +363,13 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 	if (!bo_va)
 		goto out_unlock;
 
+
 	amdgpu_vm_bo_del(adev, bo_va);
 	amdgpu_vm_bo_update_shared(bo);
 	if (!amdgpu_vm_ready(vm))
 		goto out_unlock;
 
-	r = amdgpu_vm_clear_freed(adev, vm, &fence);
+	r = amdgpu_vm_delayed_free(adev, vm, &fence);
 	if (unlikely(r < 0) && !drm_dev_is_unplugged(adev_to_drm(adev)))
 		dev_err(adev->dev, "failed to clear page "
 			"tables on GEM object close (%ld)\n", r);
@@ -808,8 +811,7 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  * otherwise stub fence in case of error.
  */
 static struct dma_fence *
-amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
-			struct amdgpu_vm *vm,
+amdgpu_gem_va_update_vm(struct amdgpu_vm_update_ctx *ctx,
 			struct amdgpu_bo_va *bo_va,
 			uint32_t operation)
 {
@@ -817,10 +819,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 	int r = 0;
 
 	/* If the VM is not ready return only a stub. */
-	if (!amdgpu_vm_ready(vm))
+	if (!amdgpu_vm_ready(ctx->vm))
 		return dma_fence_get_stub();
 
-
 	/*
 	 * First clean up any freed mappings in the VM.
 	 *
@@ -828,20 +829,20 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 	 * schedules GPU work. If nothing needs clearing, @fence can remain as
 	 * the original vm->last_update.
 	 */
-	r = amdgpu_vm_clear_freed(adev, vm, &vm->last_update);
+	r = amdgpu_vm_clear_freed(ctx, &ctx->vm->last_update);
 	if (r)
 		goto error;
 
 	/* For MAP/REPLACE we also need to update the BO mappings. */
 	if (operation == AMDGPU_VA_OP_MAP ||
 	    operation == AMDGPU_VA_OP_REPLACE) {
-		r = amdgpu_vm_bo_update(adev, bo_va, false);
+		r = amdgpu_vm_bo_update(ctx, bo_va, false);
 		if (r)
 			goto error;
 	}
 
 	/* Always update PDEs after we touched the mappings. */
-	r = amdgpu_vm_update_pdes(adev, vm, false);
+	r = amdgpu_vm_update_pdes(ctx->adev, ctx->vm, false);
 
 error:
 	if (r && r != -ERESTARTSYS)
@@ -849,28 +850,28 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
 
 	if ((operation == AMDGPU_VA_OP_MAP ||
 	     operation == AMDGPU_VA_OP_REPLACE) &&
-	    !amdgpu_vm_is_bo_always_valid(vm, bo_va->base.bo)) {
+	    !amdgpu_vm_is_bo_always_valid(ctx->vm, bo_va->base.bo)) {
 
 		/*
 		 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
 		 * bo_va->last_pt_update and vm->last_update or otherwise we
 		 * potentially miss the PDE updates or TLB invalidation.
 		 */
-		fence = dma_fence_unwrap_merge(vm->last_update,
-					       vm->last_tlb_inv,
+		fence = dma_fence_unwrap_merge(ctx->vm->last_update,
+					       ctx->vm->last_tlb_inv,
 					       bo_va->last_pt_update);
 		if (!fence)
 			dma_fence_wait(bo_va->last_pt_update, false);
 
 	} else {
-		fence = dma_fence_unwrap_merge(vm->last_update,
-					       vm->last_tlb_inv);
+		fence = dma_fence_unwrap_merge(ctx->vm->last_update,
+					       ctx->vm->last_tlb_inv);
 	}
 
 	if (!fence) {
 		/* As fallback in OOM situations */
-		dma_fence_wait(vm->last_update, false);
-		dma_fence_wait(vm->last_tlb_inv, false);
+		dma_fence_wait(ctx->vm->last_update, false);
+		dma_fence_wait(ctx->vm->last_tlb_inv, false);
 		fence = dma_fence_get_stub();
 	}
 
@@ -895,6 +896,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	struct amdgpu_bo_va *bo_va;
 	struct drm_syncobj *timeline_syncobj = NULL;
 	struct dma_fence_chain *timeline_chain = NULL;
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct drm_exec exec;
 	uint64_t vm_size, tmp;
 	int r = 0;
@@ -967,8 +969,10 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	if (r)
 		goto error_put_gobj;
 
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, &fpriv->vm);
+
 	/* Add input syncobj fences (if any) for synchronization. */
-	r = amdgpu_gem_add_input_fence(filp,
+	r = amdgpu_gem_add_input_fence(filp, &update_ctx,
 				       args->input_fence_syncobj_handles,
 				       args->num_syncobj_handles);
 	if (r)
@@ -1018,21 +1022,21 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 
 	switch (args->operation) {
 	case AMDGPU_VA_OP_MAP:
-		r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
+		r = amdgpu_vm_bo_map(&update_ctx, bo_va, args->va_address,
 				     args->offset_in_bo, args->map_size,
 				     args->flags);
 		break;
 	case AMDGPU_VA_OP_UNMAP:
-		r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
+		r = amdgpu_vm_bo_unmap(&update_ctx, bo_va, args->va_address);
 		break;
 
 	case AMDGPU_VA_OP_CLEAR:
-		r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
-						args->va_address,
+		r = amdgpu_vm_bo_clear_mappings(&update_ctx, args->va_address,
 						args->map_size);
 		break;
 	case AMDGPU_VA_OP_REPLACE:
-		r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
+		r = amdgpu_vm_bo_replace_map(&update_ctx, bo_va,
+					     args->va_address,
 					     args->offset_in_bo, args->map_size,
 					     args->flags);
 		break;
@@ -1049,7 +1053,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	    (!adev->debug_vm || timeline_syncobj)) {
 		struct dma_fence *fence;
 
-		fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
+		fence = amdgpu_gem_va_update_vm(&update_ctx, bo_va,
 						args->operation);
 		if (timeline_syncobj) {
 			if (!args->vm_timeline_point) {
@@ -1070,6 +1074,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
 	}
 
 error:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 	dma_fence_chain_free(timeline_chain);
 	if (timeline_syncobj)
 		drm_syncobj_put(timeline_syncobj);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c
index fbf9ec7d467b4..63068be7ae4fd 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c
@@ -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,15 +95,18 @@ 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;
+		goto err_ctx;
 	}
 
+err_ctx:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 error:
 	drm_exec_fini(&exec);
 	return r;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
index 11f022c56667a..b1bfd00dd2935 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
@@ -3077,7 +3077,7 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)
 		 * reserved by amdgpu_vm_lock_pd() above (it reserves 2 +
 		 * num_fences on the same VM root PD dma_resv).
 		 */
-		r = amdgpu_vm_clear_freed(adev, vm, NULL);
+		r = amdgpu_vm_delayed_free(adev, vm, NULL);
 		if (!r)
 			r = amdgpu_vm_handle_moved(adev, vm, &exec.ticket);
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
index ed6f3b67e8720..4d4b1b6da1ff5 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
@@ -994,6 +994,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, *tmp;
 	int ret;
@@ -1018,8 +1019,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;
 
@@ -1038,6 +1041,7 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 	bool invalidated = false, new_addition = false;
 	struct ttm_operation_ctx ctx = { true, false };
 	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct amdgpu_hmm_range *range;
 	struct amdgpu_vm *vm = &fpriv->vm;
 	unsigned long key, tmp_key;
@@ -1140,7 +1144,9 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 	 * explicitly here (as the CS path does) so sparse mappings survive a
 	 * VRAM-lost reset.
 	 */
-	ret = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+	ret = amdgpu_vm_bo_update(&update_ctx, fpriv->prt_va, false);
+	amdgpu_vm_update_ctx_fini(&update_ctx);
 	if (ret)
 		goto unlock_all;
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index 94b1cfe05e3e0..90f27feb84257 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -1319,37 +1319,31 @@ void amdgpu_vm_get_memory(struct amdgpu_vm *vm,
  * Returns:
  * 0 for success, -EINVAL for failure.
  */
-int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
-			bool clear)
+int amdgpu_vm_bo_update(struct amdgpu_vm_update_ctx *ctx,
+			struct amdgpu_bo_va *bo_va, bool clear)
 {
 	struct amdgpu_bo *bo = bo_va->base.bo;
-	struct amdgpu_vm *vm = bo_va->base.vm;
+	struct amdgpu_vm *vm = ctx->vm;
 	struct amdgpu_bo_va_mapping *mapping;
 	struct dma_fence **last_update;
 	dma_addr_t *pages_addr = NULL;
 	struct ttm_resource *mem;
-	struct amdgpu_sync sync;
 	bool flush_tlb = clear;
 	uint64_t vram_base;
 	uint64_t flags;
 	bool uncached;
 	int r;
 
-	amdgpu_sync_create(&sync);
 	if (clear) {
 		mem = NULL;
 
-		/* Implicitly sync to command submissions in the same VM before
-		 * unmapping.
-		 */
-		r = amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.base.resv,
-				     AMDGPU_SYNC_EQ_OWNER, vm);
+		r = amdgpu_vm_update_ctx_ensure_unmap_synced(ctx);
 		if (r)
-			goto error_free;
+			goto error;
 		if (bo) {
-			r = amdgpu_sync_kfd(&sync, bo->tbo.base.resv);
+			r = amdgpu_sync_kfd(&ctx->sync, bo->tbo.base.resv);
 			if (r)
-				goto error_free;
+				goto error;
 		}
 	} else if (!bo) {
 		mem = NULL;
@@ -1374,16 +1368,16 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 			pages_addr = bo->tbo.ttm->dma_address;
 
 		/* Implicitly sync to moving fences before mapping anything */
-		r = amdgpu_sync_resv(adev, &sync, bo->tbo.base.resv,
+		r = amdgpu_sync_resv(ctx->adev, &ctx->sync, bo->tbo.base.resv,
 				     AMDGPU_SYNC_EXPLICIT, vm);
 		if (r)
-			goto error_free;
+			goto error;
 	}
 
 	if (bo) {
 		struct amdgpu_device *bo_adev;
 
-		flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
+		flags = amdgpu_ttm_tt_pte_flags(ctx->adev, bo->tbo.ttm, mem);
 
 		if (amdgpu_bo_encrypted(bo))
 			flags |= AMDGPU_PTE_TMZ;
@@ -1422,18 +1416,18 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 			update_flags &= ~AMDGPU_PTE_WRITEABLE;
 
 		/* Apply ASIC specific mapping flags */
-		amdgpu_gmc_get_vm_pte(adev, vm, bo, mapping->flags,
+		amdgpu_gmc_get_vm_pte(ctx->adev, vm, bo, mapping->flags,
 				      &update_flags);
 
 		trace_amdgpu_vm_bo_update(mapping);
 
-		r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb,
-					   !uncached, &sync, mapping->start,
+		r = amdgpu_vm_update_range(ctx->adev, vm, false, false, flush_tlb,
+					   !uncached, &ctx->sync, mapping->start,
 					   mapping->last, update_flags,
 					   mapping->offset, vram_base, mem,
 					   pages_addr, last_update);
 		if (r)
-			goto error_free;
+			goto error;
 	}
 
 	/* If the BO is not in its preferred location add it back to
@@ -1466,8 +1460,7 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
 			trace_amdgpu_vm_bo_mapping(mapping);
 	}
 
-error_free:
-	amdgpu_sync_free(&sync);
+error:
 	return r;
 }
 
@@ -1596,55 +1589,103 @@ static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	}
 }
 
+/**
+ * amdgpu_vm_update_ctx_ensure_unmap_synced - VM update sync helper
+ * @ctx: context for updating the VM
+ *
+ * Ensures that ctx->sync will synchronize to everything that's necessary
+ * to unmap freed pages associated with the context.
+ */
+int amdgpu_vm_update_ctx_ensure_unmap_synced(struct amdgpu_vm_update_ctx *ctx)
+{
+	int r;
+
+	if (ctx->unmap_synced)
+		return 0;
+
+	/*
+	 * Implicitly sync to command submissions in the same VM before
+	 * unmapping, unless we unmap with explicit sync.
+	 */
+	r = amdgpu_sync_resv(ctx->adev, &ctx->sync,
+			     ctx->vm->root.bo->tbo.base.resv,
+			     AMDGPU_SYNC_EQ_OWNER, ctx->vm);
+	if (r)
+		return r;
+
+	ctx->unmap_synced = true;
+
+	return 0;
+}
+
+/**
+ * amdgpu_vm_update_ctx_init - create a context for VM update operations
+ * @ctx: The new context to create
+ * @adev: The device associated with the VM update
+ * @vm: The VM being updated
+ *
+ * Initializes the context to an empty state.
+ */
+void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx,
+			       struct amdgpu_device *adev, struct amdgpu_vm *vm)
+{
+	memset(ctx, 0, sizeof(*ctx));
+	ctx->adev = adev;
+	ctx->vm = vm;
+	INIT_LIST_HEAD(&ctx->freed);
+	amdgpu_sync_create(&ctx->sync);
+}
+
+/**
+ * amdgpu_vm_update_ctx_fini - finish a VM update operation
+ * @ctx: The context of the update
+ *
+ * Adds any leftover mappings needing to be cleared to the VM's delayed free list,
+ * then frees up resources used by the context.
+ */
+void amdgpu_vm_update_ctx_fini(struct amdgpu_vm_update_ctx *ctx)
+{
+	list_splice(&ctx->freed, &ctx->vm->delayed_freed);
+	amdgpu_sync_free(&ctx->sync);
+}
+
 /**
  * amdgpu_vm_clear_freed - clear freed BOs in the PT
  *
- * @adev: amdgpu_device pointer
- * @vm: requested vm
+ * @ctx: Context for VM updates
  * @fence: optional resulting fence (unchanged if no work needed to be done
  * or if an error occurred)
  *
- * Make sure all freed BOs are cleared in the PT.
+ * Make sure all BOs freed by VM updates in the context are cleared in the PT.
  * PTs have to be reserved and mutex must be locked!
  *
  * Returns:
  * 0 for success.
  *
  */
-int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
-			  struct amdgpu_vm *vm,
+int amdgpu_vm_clear_freed(struct amdgpu_vm_update_ctx *ctx,
 			  struct dma_fence **fence)
 {
 	struct amdgpu_bo_va_mapping *mapping;
 	struct dma_fence *f = NULL;
-	struct amdgpu_sync sync;
 	int r;
 
-	if (list_empty(&vm->freed))
-		return 0;
-
-	/*
-	 * Implicitly sync to command submissions in the same VM before
-	 * unmapping.
-	 */
-	amdgpu_sync_create(&sync);
-	r = amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.base.resv,
-			     AMDGPU_SYNC_EQ_OWNER, vm);
+	r = amdgpu_vm_update_ctx_ensure_unmap_synced(ctx);
 	if (r)
-		goto error_free;
+		return r;
 
-	while (!list_empty(&vm->freed)) {
-		mapping = list_first_entry(&vm->freed,
-			struct amdgpu_bo_va_mapping, list);
+	while (!list_empty(&ctx->freed)) {
+		mapping = list_first_entry(&ctx->freed,
+					   struct amdgpu_bo_va_mapping, list);
 		list_del(&mapping->list);
 
-		r = amdgpu_vm_update_range(adev, vm, false, false, true, false,
-					   &sync, mapping->start, mapping->last,
+		r = amdgpu_vm_update_range(ctx->adev, ctx->vm, false, false, true, false,
+					   &ctx->sync, mapping->start, mapping->last,
 					   0, 0, 0, NULL, NULL, &f);
-		amdgpu_vm_free_mapping(adev, vm, mapping, f);
+		amdgpu_vm_free_mapping(ctx->adev, ctx->vm, mapping, f);
 		if (r) {
 			dma_fence_put(f);
-			goto error_free;
+			return r;
 		}
 	}
 
@@ -1655,12 +1696,40 @@ int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
 		dma_fence_put(f);
 	}
 
-error_free:
-	amdgpu_sync_free(&sync);
 	return r;
 
 }
 
+/**
+ * amdgpu_vm_delayed_free - execute delayed PT clearing
+ *
+ * @adev: Device to use for freeing
+ * @vm: VM whose BOs should be freed
+ * @fence: optional resulting fence (unchanged if no work needed to be done
+ * or if an error occurred)
+ *
+ * Go over the list of BOs whose PT clearing was delayed and clear all of them.
+ * PTs have to be reserved and mutex must be locked!
+ *
+ * Returns:
+ * 0 for success.
+ *
+ */
+int amdgpu_vm_delayed_free(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+			   struct dma_fence **fence)
+{
+	struct amdgpu_vm_update_ctx ctx;
+	int r;
+
+	amdgpu_vm_update_ctx_init(&ctx, adev, vm);
+
+	list_splice_init(&vm->delayed_freed, &ctx.freed);
+	r = amdgpu_vm_clear_freed(&ctx, fence);
+
+	amdgpu_vm_update_ctx_fini(&ctx);
+	return r;
+}
+
 /**
  * amdgpu_vm_handle_moved - handle moved BOs in the PT
  *
@@ -1679,18 +1748,22 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 			   struct amdgpu_vm *vm,
 			   struct ww_acquire_ctx *ticket)
 {
+	struct amdgpu_vm_update_ctx update_ctx;
 	struct amdgpu_bo_va *bo_va, *tmp;
 	struct dma_resv *resv;
 	struct amdgpu_bo *bo;
 	bool clear, unlock;
 	int r;
 
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+
 	list_for_each_entry_safe(bo_va, tmp, &vm->always_valid.needs_update,
 				 base.vm_status) {
 		/* Per VM BOs never need to bo cleared in the page tables */
-		r = amdgpu_vm_bo_update(adev, bo_va, false);
+		r = amdgpu_vm_bo_update(&update_ctx, bo_va, false);
+
 		if (r)
-			return r;
+			goto error;
 	}
 
 	spin_lock(&vm->individual_lock);
@@ -1716,7 +1789,7 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 			unlock = false;
 		}
 
-		r = amdgpu_vm_bo_update(adev, bo_va, clear);
+		r = amdgpu_vm_bo_update(&update_ctx, bo_va, clear);
 
 		if (unlock)
 			dma_resv_unlock(resv);
@@ -1736,6 +1809,9 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 	}
 	spin_unlock(&vm->individual_lock);
 
+error:
+	amdgpu_vm_update_ctx_fini(&update_ctx);
+
 	return 0;
 }
 
@@ -1889,7 +1965,7 @@ static int amdgpu_vm_verify_parameters(struct amdgpu_device *adev,
 /**
  * amdgpu_vm_bo_map - map bo inside a vm
  *
- * @adev: amdgpu_device pointer
+ * @ctx: VM update context
  * @bo_va: bo_va to store the address
  * @saddr: where to map the BO
  * @offset: requested offset in the BO
@@ -1903,7 +1979,7 @@ static int amdgpu_vm_verify_parameters(struct amdgpu_device *adev,
  *
  * Object has to be reserved and unreserved outside!
  */
-int amdgpu_vm_bo_map(struct amdgpu_device *adev,
+int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx,
 		     struct amdgpu_bo_va *bo_va,
 		     uint64_t saddr, uint64_t offset,
 		     uint64_t size, uint32_t flags)
@@ -1914,7 +1990,7 @@ int amdgpu_vm_bo_map(struct amdgpu_device *adev,
 	uint64_t eaddr;
 	int r;
 
-	r = amdgpu_vm_verify_parameters(adev, bo, saddr, offset, size);
+	r = amdgpu_vm_verify_parameters(ctx->adev, bo, saddr, offset, size);
 	if (r)
 		return r;
 
@@ -1924,7 +2000,8 @@ int amdgpu_vm_bo_map(struct amdgpu_device *adev,
 	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
 	if (tmp) {
 		/* bo and tmp overlap, invalid addr */
-		dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
+		dev_err(ctx->adev->dev,
+			"bo %p va 0x%010Lx-0x%010Lx conflict with "
 			"0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
 			tmp->start, tmp->last + 1);
 		return -EINVAL;
@@ -1939,7 +2016,7 @@ int amdgpu_vm_bo_map(struct amdgpu_device *adev,
 	mapping->offset = offset;
 	mapping->flags = flags;
 
-	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
+	amdgpu_vm_bo_insert_map(ctx->adev, bo_va, mapping);
 
 	return 0;
 }
@@ -1947,7 +2024,7 @@ int amdgpu_vm_bo_map(struct amdgpu_device *adev,
 /**
  * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  *
- * @adev: amdgpu_device pointer
+ * @ctx: VM update context
  * @bo_va: bo_va to store the address
  * @saddr: where to map the BO
  * @offset: requested offset in the BO
@@ -1962,7 +2039,7 @@ int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  *
  * Object has to be reserved and unreserved outside!
  */
-int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
+int amdgpu_vm_bo_replace_map(struct amdgpu_vm_update_ctx *ctx,
 			     struct amdgpu_bo_va *bo_va,
 			     uint64_t saddr, uint64_t offset,
 			     uint64_t size, uint32_t flags)
@@ -1972,7 +2049,7 @@ int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
 	uint64_t eaddr;
 	int r;
 
-	r = amdgpu_vm_verify_parameters(adev, bo, saddr, offset, size);
+	r = amdgpu_vm_verify_parameters(ctx->adev, bo, saddr, offset, size);
 	if (r)
 		return r;
 
@@ -1981,7 +2058,7 @@ int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
 	if (!mapping)
 		return -ENOMEM;
 
-	r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
+	r = amdgpu_vm_bo_clear_mappings(ctx, saddr, size);
 	if (r) {
 		kfree(mapping);
 		return r;
@@ -1995,7 +2072,7 @@ int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
 	mapping->offset = offset;
 	mapping->flags = flags;
 
-	amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
+	amdgpu_vm_bo_insert_map(ctx->adev, bo_va, mapping);
 
 	return 0;
 }
@@ -2003,7 +2080,7 @@ int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
 /**
  * amdgpu_vm_bo_unmap - remove bo mapping from vm
  *
- * @adev: amdgpu_device pointer
+ * @ctx: VM update context
  * @bo_va: bo_va to remove the address from
  * @saddr: where to the BO is mapped
  *
@@ -2014,12 +2091,12 @@ int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  *
  * Object has to be reserved and unreserved outside!
  */
-int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
+int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx *ctx,
 		       struct amdgpu_bo_va *bo_va,
 		       uint64_t saddr)
 {
 	struct amdgpu_bo_va_mapping *mapping;
-	struct amdgpu_vm *vm = bo_va->base.vm;
+	struct amdgpu_vm *vm = ctx->vm;
 	bool valid = true;
 
 	saddr /= AMDGPU_GPU_PAGE_SIZE;
@@ -2046,7 +2123,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 	 * from user space.
 	 */
 	if (unlikely(bo_va->userq_va_mapped))
-		amdgpu_userq_gem_va_unmap_validate(adev, mapping);
+		amdgpu_userq_gem_va_unmap_validate(ctx->adev, mapping);
 
 	list_del(&mapping->list);
 	amdgpu_vm_it_remove(mapping, &vm->va);
@@ -2054,9 +2131,9 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 	trace_amdgpu_vm_bo_unmap(bo_va, mapping);
 
 	if (valid)
-		list_add(&mapping->list, &vm->freed);
+		list_add(&mapping->list, &ctx->freed);
 	else
-		amdgpu_vm_free_mapping(adev, vm, mapping,
+		amdgpu_vm_free_mapping(ctx->adev, vm, mapping,
 				       bo_va->last_pt_update);
 
 	return 0;
@@ -2065,8 +2142,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
 /**
  * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  *
- * @adev: amdgpu_device pointer
- * @vm: VM structure to use
+ * @ctx: VM update context
  * @saddr: start of the range
  * @size: size of the range
  *
@@ -2075,8 +2151,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  * Returns:
  * 0 for success, error for failure.
  */
-int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
-				struct amdgpu_vm *vm,
+int amdgpu_vm_bo_clear_mappings(struct amdgpu_vm_update_ctx *ctx,
 				uint64_t saddr, uint64_t size)
 {
 	struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
@@ -2084,7 +2159,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 	uint64_t eaddr;
 	int r;
 
-	r = amdgpu_vm_verify_parameters(adev, NULL, saddr, 0, size);
+	r = amdgpu_vm_verify_parameters(ctx->adev, NULL, saddr, 0, size);
 	if (r)
 		return r;
 
@@ -2105,7 +2180,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 	INIT_LIST_HEAD(&after->list);
 
 	/* Now gather all removed mappings */
-	tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
+	tmp = amdgpu_vm_it_iter_first(&ctx->vm->va, saddr, eaddr);
 	while (tmp) {
 		/* Remember mapping split at the start */
 		if (tmp->start < saddr) {
@@ -2136,7 +2211,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 
 	/* And free them up */
 	list_for_each_entry_safe(tmp, next, &removed, list) {
-		amdgpu_vm_it_remove(tmp, &vm->va);
+		amdgpu_vm_it_remove(tmp, &ctx->vm->va);
 		list_del(&tmp->list);
 
 		if (tmp->start < saddr)
@@ -2145,7 +2220,7 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 		    tmp->last = eaddr;
 
 		tmp->bo_va = NULL;
-		list_add(&tmp->list, &vm->freed);
+		list_add(&tmp->list, &ctx->freed);
 		trace_amdgpu_vm_bo_unmap(NULL, tmp);
 	}
 
@@ -2153,11 +2228,11 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 	if (!list_empty(&before->list)) {
 		struct amdgpu_bo *bo = before->bo_va->base.bo;
 
-		amdgpu_vm_it_insert(before, &vm->va);
+		amdgpu_vm_it_insert(before, &ctx->vm->va);
 		if (before->flags & AMDGPU_VM_PAGE_PRT)
-			amdgpu_vm_prt_get(adev);
+			amdgpu_vm_prt_get(ctx->adev);
 
-		if (amdgpu_vm_is_bo_always_valid(vm, bo) &&
+		if (amdgpu_vm_is_bo_always_valid(ctx->vm, bo) &&
 		    !before->bo_va->base.moved)
 			amdgpu_vm_bo_needs_update(&before->bo_va->base);
 	} else {
@@ -2168,11 +2243,11 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
 	if (!list_empty(&after->list)) {
 		struct amdgpu_bo *bo = after->bo_va->base.bo;
 
-		amdgpu_vm_it_insert(after, &vm->va);
+		amdgpu_vm_it_insert(after, &ctx->vm->va);
 		if (after->flags & AMDGPU_VM_PAGE_PRT)
-			amdgpu_vm_prt_get(adev);
+			amdgpu_vm_prt_get(ctx->adev);
 
-		if (amdgpu_vm_is_bo_always_valid(vm, bo) &&
+		if (amdgpu_vm_is_bo_always_valid(ctx->vm, bo) &&
 		    !after->bo_va->base.moved)
 			amdgpu_vm_bo_needs_update(&after->bo_va->base);
 	} else {
@@ -2248,6 +2323,7 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
 	struct amdgpu_bo *bo = bo_va->base.bo;
 	struct amdgpu_vm *vm = bo_va->base.vm;
 	struct amdgpu_vm_bo_base **base;
+	struct amdgpu_vm_update_ctx update_ctx;
 
 	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
 
@@ -2271,12 +2347,14 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
 	list_del(&bo_va->base.vm_status);
 	spin_unlock(&vm->individual_lock);
 
+	amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
+
 	list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
 		list_del(&mapping->list);
 		amdgpu_vm_it_remove(mapping, &vm->va);
 		mapping->bo_va = NULL;
 		trace_amdgpu_vm_bo_unmap(bo_va, mapping);
-		list_add(&mapping->list, &vm->freed);
+		list_add(&mapping->list, &update_ctx.freed);
 	}
 	list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
 		list_del(&mapping->list);
@@ -2285,6 +2363,8 @@ void amdgpu_vm_bo_del(struct amdgpu_device *adev,
 				       bo_va->last_pt_update);
 	}
 
+	amdgpu_vm_update_ctx_fini(&update_ctx);
+
 	dma_fence_put(bo_va->last_pt_update);
 
 	if (bo && bo_va->is_xgmi)
@@ -2656,7 +2736,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	amdgpu_vm_bo_status_init(&vm->always_valid);
 	spin_lock_init(&vm->individual_lock);
 	amdgpu_vm_bo_status_init(&vm->individual);
-	INIT_LIST_HEAD(&vm->freed);
+	INIT_LIST_HEAD(&vm->delayed_freed);
 	INIT_KFIFO(vm->faults);
 	spin_lock_init(&vm->stats_lock);
 
@@ -2883,7 +2963,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	dma_fence_put(vm->last_seq_flush);
 	dma_fence_put(vm->last_tlb_inv);
 
-	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
+	list_for_each_entry_safe(mapping, tmp, &vm->delayed_freed, list) {
 		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
 			amdgpu_vm_prt_fini(adev, vm);
 			prt_fini_needed = false;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index f6285a9d9e989..8445599d9539a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -264,6 +264,39 @@ struct amdgpu_task_info {
 	struct kref	refcount;
 };
 
+/**
+ * struct amdgpu_vm_update_ctx
+ *
+ * Encapsulates various state that is carried around during VM updates.
+ */
+struct amdgpu_vm_update_ctx {
+	/**
+	 * @adev: amdgpu device the update is done on
+	 */
+	struct amdgpu_device *adev;
+	/**
+	 * @vm: target VM of the update
+	 */
+	struct amdgpu_vm *vm;
+
+	/**
+	 * @freed: A list of mappings that are freed and pending PT/PD updates during
+	 * this update operation.
+	 */
+	struct list_head freed;
+
+	/**
+	 * @sync: Container for everything to sync to before committing PT/PD updates.
+	 */
+	struct amdgpu_sync sync;
+
+	/**
+	 * @unmap_synced: Whether the sync is strong enough to guarantee a safe unmap,
+	 * i.e. all previous submissions in the VM are waited on.
+	 */
+	bool unmap_synced;
+};
+
 struct amdgpu_vm_fault_info {
 	/* fault address */
 	uint64_t	addr;
@@ -318,7 +351,7 @@ struct amdgpu_vm {
 	 * This list contains amdgpu_bo_va_mapping objects which have been freed
 	 * but not updated in the PTs
 	 */
-	struct list_head	freed;
+	struct list_head	delayed_freed;
 
 	/* contains the page directory */
 	struct amdgpu_vm_bo_base     root;
@@ -453,9 +486,10 @@ void amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
 		     bool *need_pipe_sync, bool *emit_gds_needed);
 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 			  struct amdgpu_vm *vm, bool immediate);
-int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
-			  struct amdgpu_vm *vm,
+int amdgpu_vm_clear_freed(struct amdgpu_vm_update_ctx *ctx,
 			  struct dma_fence **fence);
+int amdgpu_vm_delayed_free(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+			   struct dma_fence **fence);
 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
 			   struct amdgpu_vm *vm,
 			   struct ww_acquire_ctx *ticket);
@@ -472,7 +506,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			   uint64_t offset, uint64_t vram_base,
 			   struct ttm_resource *res, dma_addr_t *pages_addr,
 			   struct dma_fence **fence);
-int amdgpu_vm_bo_update(struct amdgpu_device *adev,
+int amdgpu_vm_bo_update(struct amdgpu_vm_update_ctx *ctx,
 			struct amdgpu_bo_va *bo_va,
 			bool clear);
 bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
@@ -488,20 +522,21 @@ struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
 				      struct amdgpu_vm *vm,
 				      struct amdgpu_bo *bo);
-int amdgpu_vm_bo_map(struct amdgpu_device *adev,
+
+int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx,
 		     struct amdgpu_bo_va *bo_va,
 		     uint64_t addr, uint64_t offset,
 		     uint64_t size, uint32_t flags);
-int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
+int amdgpu_vm_bo_replace_map(struct amdgpu_vm_update_ctx *ctx,
 			     struct amdgpu_bo_va *bo_va,
 			     uint64_t addr, uint64_t offset,
 			     uint64_t size, uint32_t flags);
-int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
+int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx *ctx,
 		       struct amdgpu_bo_va *bo_va,
 		       uint64_t addr);
-int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
-				struct amdgpu_vm *vm,
+int amdgpu_vm_bo_clear_mappings(struct amdgpu_vm_update_ctx *ctx,
 				uint64_t saddr, uint64_t size);
+
 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
 							 uint64_t addr);
 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket);
@@ -545,6 +580,12 @@ 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_init(struct amdgpu_vm_update_ctx *ctx,
+			       struct amdgpu_device *adev,
+			       struct amdgpu_vm *vm);
+void amdgpu_vm_update_ctx_fini(struct amdgpu_vm_update_ctx *ctx);
+int amdgpu_vm_update_ctx_ensure_unmap_synced(struct amdgpu_vm_update_ctx *ctx);
+
 /**
  * amdgpu_vm_tlb_seq - return tlb flush sequence number
  * @vm: the amdgpu_vm structure to query

-- 
2.56.1


  parent reply	other threads:[~2026-11-17 12:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 12:50 [PATCH v2 00/10] drm/amdgpu: Rework VM update ops for batching and explicit sync Natalie Vock
2026-11-17 12:50 ` [PATCH v2 01/10] drm/amdgpu: Remove unused parameter from amdgpu_vm_pte_fragment Natalie Vock
2026-11-17 12:50 ` Natalie Vock [this message]
2026-11-17 12:50 ` [PATCH v2 03/10] drm/amdgpu: Make VM maps pending per update context Natalie Vock
2026-11-17 12:50 ` [PATCH v2 04/10] drm/amdgpu: Make removed/cleared mappings " Natalie Vock
2026-11-17 12:50 ` [PATCH v2 05/10] drm/amdgpu: Track dirty bo_va structures in update ctx Natalie Vock
2026-11-17 12:50 ` [PATCH v2 06/10] drm/amdgpu: Add function to roll back a VM " Natalie Vock
2026-11-17 12:50 ` [PATCH v2 07/10] drm/amdgpu: Move some VA op validation to a helper Natalie Vock
2026-11-17 12:50 ` [PATCH v2 08/10] drm/amdgpu: Only reject conflicting mappings if they are not cleared Natalie Vock
2026-11-17 12:50 ` [PATCH v2 09/10] drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl Natalie Vock
2026-11-17 12:50 ` [PATCH v2 10/10] drm/amdgpu: Bump minor version Natalie Vock

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117-prt-explicit-sync-v2-2-bb3851cb6a0c@pixelcluster.dev \
    --to=nat@pixelcluster.dev \
    --cc=Felix.Kuehling@amd.com \
    --cc=airlied@gmail.com \
    --cc=alexander.deucher@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=timur.kristof@gmail.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®