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 03/10] drm/amdgpu: Make VM maps pending per update context
Date: Tue, 17 Nov 2026 13:50:09 +0100 [thread overview]
Message-ID: <20261117-prt-explicit-sync-v2-3-bb3851cb6a0c@pixelcluster.dev> (raw)
In-Reply-To: <20261117-prt-explicit-sync-v2-0-bb3851cb6a0c@pixelcluster.dev>
For later being able to discard pending changes when an error is
encountered afterwards.
Signed-off-by: Natalie Vock <nat@pixelcluster.dev>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 5 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c | 2 +
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 1 +
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c | 1 +
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 124 +++++++++++++++++------
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 11 ++
7 files changed, 113 insertions(+), 33 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index 08ab754e56d3d..b20b84d0a54bb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -1351,13 +1351,16 @@ static int map_bo_to_gpuvm(struct kgd_mem *mem,
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);
+ amdgpu_vm_update_ctx_fini(&update_ctx);
return ret;
}
+ amdgpu_vm_update_ctx_commit(&update_ctx);
+ amdgpu_vm_update_ctx_fini(&update_ctx);
+
if (no_update_pte)
return 0;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c
index f33dfdb6efe2f..8ec6b46e3f25a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c
@@ -22,6 +22,7 @@
* * Author: Monk.liu@amd.com
*/
+#include "amdgpu_vm.h"
#include <drm/drm_exec.h>
#include "amdgpu.h"
@@ -94,6 +95,7 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
r = amdgpu_vm_bo_map(&update_ctx, *bo_va, csa_addr, 0, size,
AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
AMDGPU_PTE_EXECUTABLE);
+ amdgpu_vm_update_ctx_commit(&update_ctx);
if (r) {
drm_err(adev_to_drm(adev),
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
index 55bdf5070837a..2816d0851bf01 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -1044,6 +1044,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
break;
}
+ amdgpu_vm_update_ctx_commit(&update_ctx);
+
/*
* Once the VA operation is done, update the VM and obtain the fence
* that represents the last relevant update for this mapping. This
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
index 2e6a8e6f577a7..e0090dc10682f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
@@ -64,6 +64,7 @@ struct amdgpu_bo_param {
struct amdgpu_bo_va_mapping {
struct amdgpu_bo_va *bo_va;
struct list_head list;
+ struct list_head pending_list;
struct rb_node rb;
uint64_t start;
uint64_t last;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c
index 63068be7ae4fd..84a14a5cc0be2 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c
@@ -104,6 +104,7 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
amdgpu_vm_bo_del(adev, *bo_va);
goto err_ctx;
}
+ amdgpu_vm_update_ctx_commit(&update_ctx);
err_ctx:
amdgpu_vm_update_ctx_fini(&update_ctx);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index 90f27feb84257..97cf04da6a025 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -1589,6 +1589,73 @@ static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
}
}
+/**
+ * amdgpu_vm_bo_commit_insert - insert a new mapping
+ *
+ * @adev: amdgpu_device pointer
+ * @bo_va: bo_va to store the address
+ * @mapping: the mapping to insert
+ *
+ * Insert a new mapping into all structures.
+ */
+static void amdgpu_vm_bo_commit_insert(struct amdgpu_device *adev,
+ struct amdgpu_bo_va *bo_va,
+ struct amdgpu_bo_va_mapping *mapping)
+{
+ struct amdgpu_vm *vm = bo_va->base.vm;
+ struct amdgpu_bo *bo = bo_va->base.bo;
+
+ mapping->bo_va = bo_va;
+ list_add(&mapping->list, &bo_va->invalids);
+ amdgpu_vm_it_insert(mapping, &vm->va);
+
+ if (mapping->flags & AMDGPU_VM_PAGE_PRT)
+ amdgpu_vm_prt_get(adev);
+
+ if (bo && !bo_va->base.moved)
+ amdgpu_vm_bo_needs_update(&bo_va->base);
+
+ trace_amdgpu_vm_bo_map(bo_va, mapping);
+}
+
+void amdgpu_vm_update_ctx_commit(struct amdgpu_vm_update_ctx *ctx)
+{
+ struct amdgpu_bo_va_mapping *mapping, *next;
+
+ list_for_each_entry_safe(mapping, next, &ctx->new, pending_list) {
+ list_del_init(&mapping->pending_list);
+ amdgpu_vm_bo_commit_insert(ctx->adev, mapping->bo_va, mapping);
+ }
+}
+
+static int
+amdgpu_vm_update_ctx_add_new_mapping(struct amdgpu_vm_update_ctx *ctx,
+ struct amdgpu_bo_va *bo_va,
+ struct amdgpu_bo_va_mapping *mapping)
+{
+ struct interval_tree_node *pending;
+
+ if (interval_tree_iter_first(&ctx->pending_maps, mapping->start,
+ mapping->last))
+ return -EINVAL;
+
+ pending = kzalloc_obj(*pending);
+ if (!pending)
+ return -ENOMEM;
+
+ if (interval_tree_iter_first(&ctx->pending_maps, mapping->start,
+ mapping->last))
+ return -EINVAL;
+
+ pending->start = mapping->start;
+ pending->last = mapping->last;
+ interval_tree_insert(pending, &ctx->pending_maps);
+
+ mapping->bo_va = bo_va;
+ list_add(&mapping->pending_list, &ctx->new);
+ return 0;
+}
+
/**
* amdgpu_vm_update_ctx_ensure_unmap_synced - VM update sync helper
* @ctx: context for updating the VM
@@ -1632,6 +1699,9 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx,
memset(ctx, 0, sizeof(*ctx));
ctx->adev = adev;
ctx->vm = vm;
+
+ ctx->pending_maps = RB_ROOT_CACHED;
+ INIT_LIST_HEAD(&ctx->new);
INIT_LIST_HEAD(&ctx->freed);
amdgpu_sync_create(&ctx->sync);
}
@@ -1645,8 +1715,17 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx,
*/
void amdgpu_vm_update_ctx_fini(struct amdgpu_vm_update_ctx *ctx)
{
+ struct interval_tree_node *pending, *next_pending;
+
list_splice(&ctx->freed, &ctx->vm->delayed_freed);
amdgpu_sync_free(&ctx->sync);
+
+ rbtree_postorder_for_each_entry_safe(pending, next_pending, &ctx->pending_maps.rb_root, rb)
+ kfree(pending);
+
+ ctx->pending_maps = RB_ROOT_CACHED;
+
+ WARN_ON(!list_empty(&ctx->new));
}
/**
@@ -1901,36 +1980,6 @@ struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
return bo_va;
}
-
-/**
- * amdgpu_vm_bo_insert_map - insert a new mapping
- *
- * @adev: amdgpu_device pointer
- * @bo_va: bo_va to store the address
- * @mapping: the mapping to insert
- *
- * Insert a new mapping into all structures.
- */
-static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
- struct amdgpu_bo_va *bo_va,
- struct amdgpu_bo_va_mapping *mapping)
-{
- struct amdgpu_vm *vm = bo_va->base.vm;
-
- mapping->bo_va = bo_va;
- list_add(&mapping->list, &bo_va->invalids);
- amdgpu_vm_it_insert(mapping, &vm->va);
-
- if (mapping->flags & AMDGPU_VM_PAGE_PRT)
- amdgpu_vm_prt_get(adev);
-
- /* PRT mappings have no BO and are kept off the state lists */
- if (bo_va->base.bo && !bo_va->base.moved)
- amdgpu_vm_bo_needs_update(&bo_va->base);
-
- trace_amdgpu_vm_bo_map(bo_va, mapping);
-}
-
/* Validate operation parameters to prevent potential abuse */
static int amdgpu_vm_verify_parameters(struct amdgpu_device *adev,
struct amdgpu_bo *bo,
@@ -2015,8 +2064,14 @@ int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx,
mapping->last = eaddr;
mapping->offset = offset;
mapping->flags = flags;
+ mapping->bo_va = bo_va;
+ INIT_LIST_HEAD(&mapping->pending_list);
- amdgpu_vm_bo_insert_map(ctx->adev, bo_va, mapping);
+ r = amdgpu_vm_update_ctx_add_new_mapping(ctx, bo_va, mapping);
+ if (r) {
+ kfree(mapping);
+ return r;
+ }
return 0;
}
@@ -2071,8 +2126,13 @@ int amdgpu_vm_bo_replace_map(struct amdgpu_vm_update_ctx *ctx,
mapping->last = eaddr;
mapping->offset = offset;
mapping->flags = flags;
+ INIT_LIST_HEAD(&mapping->pending_list);
- amdgpu_vm_bo_insert_map(ctx->adev, bo_va, mapping);
+ r = amdgpu_vm_update_ctx_add_new_mapping(ctx, bo_va, mapping);
+ if (r) {
+ kfree(mapping);
+ return r;
+ }
return 0;
}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index 8445599d9539a..d35494440cb5a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -279,6 +279,16 @@ struct amdgpu_vm_update_ctx {
*/
struct amdgpu_vm *vm;
+ /**
+ * @mapped_intervals: Interval tree of VA ranges mapped by this update operation.
+ */
+ struct rb_root_cached pending_maps;
+
+ /**
+ * @new: Pending new mappings not yet applied to bookkeeping structures.
+ */
+ struct list_head new;
+
/**
* @freed: A list of mappings that are freed and pending PT/PD updates during
* this update operation.
@@ -583,6 +593,7 @@ 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_commit(struct amdgpu_vm_update_ctx *ctx);
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);
--
2.56.1
next prev 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 ` [PATCH v2 02/10] drm/amdgpu: Add and use vm_update_ctx struct Natalie Vock
2026-11-17 12:50 ` Natalie Vock [this message]
2026-11-17 12:50 ` [PATCH v2 04/10] drm/amdgpu: Make removed/cleared mappings pending per update context 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-3-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®