AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdgpu: track finished page table updates with a fence chain
@ 2026-11-24  4:45 chong li
  2026-11-24  5:04 ` sashiko-bot
  2026-11-25 10:45 ` Li, Chong(Alan)
  0 siblings, 2 replies; 4+ messages in thread
From: chong li @ 2026-11-24  4:45 UTC (permalink / raw)
  To: christian.koenig, timur.kristof; +Cc: amd-gfx, chong li

amdgpu_vm_tlb_seq() waits for the tlb_seq callback of a page table update
by taking the lock of vm->last_seq_flush. But every fence has its own
lock, and a newer update replaces vm->last_seq_flush without waiting for
the callbacks of older updates. A job scheduled after an older fence has
signaled, but before its callback has finished, takes the lock of the
newer fence, misses the tlb_seq increment and can reuse a VMID without
flushing the TLB after the page table update it waited for.

how to fix:
Stop incrementing tlb_seq from a fence callback. Put the fences of the
updates which need a TLB flush into a dma_fence_chain and let
amdgpu_vm_tlb_seq() add the number of updates which have signaled. A
dependent job then sees the sequence change together with the signaled
bit of the fence it waited for. The chain nodes are numbered
consecutively. The number of the newest node minus the number of
unsignaled nodes therefore also counts the signaled nodes which were
already pruned, in any completion order. Nothing references the VM from
a fence callback any more, so amdgpu_vm_fini() no longer needs to wait
for vm->last_seq_flush.

Signed-off-by: chong li <chongli2@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 77 +++++++-------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 31 ++++++-----
 2 files changed, 38 insertions(+), 70 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index a627a3092990..dafcebd95555 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -114,21 +114,6 @@ struct amdgpu_prt_cb {
 	struct dma_fence_cb cb;
 };
 
-/**
- * struct amdgpu_vm_tlb_seq_struct - Helper to increment the TLB flush sequence
- */
-struct amdgpu_vm_tlb_seq_struct {
-	/**
-	 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
-	 */
-	struct amdgpu_vm *vm;
-
-	/**
-	 * @cb: callback
-	 */
-	struct dma_fence_cb cb;
-};
-
 /**
  * amdgpu_vm_assert_locked - check if VM is correctly locked
  * @vm: the VM which schould be tested
@@ -1089,47 +1074,33 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 	return r;
 }
 
-/**
- * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
- * @fence: unused
- * @cb: the callback structure
- *
- * Increments the tlb sequence to make sure that future CS execute a VM flush.
- */
-static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
-				 struct dma_fence_cb *cb)
-{
-	struct amdgpu_vm_tlb_seq_struct *tlb_cb;
-
-	tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
-	atomic64_inc(&tlb_cb->vm->tlb_seq);
-	kfree(tlb_cb);
-}
-
 /**
  * amdgpu_vm_tlb_flush - prepare TLB flush
  *
  * @params: parameters for update
  * @fence: input fence to sync TLB flush with
- * @tlb_cb: the callback structure
+ * @chain: preallocated chain node to track the update with
  *
  * Increments the tlb sequence to make sure that future CS execute a VM flush.
  */
 static void
 amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
 		    struct dma_fence *fence,
-		    struct amdgpu_vm_tlb_seq_struct *tlb_cb)
+		    struct dma_fence_chain *chain)
 {
 	struct amdgpu_vm *vm = params->vm;
+	struct dma_fence *prev, *iter;
 
-	tlb_cb->vm = vm;
-	if (fence && !dma_fence_add_callback(fence, &tlb_cb->cb,
-					     amdgpu_vm_tlb_seq_cb)) {
-		dma_fence_put(vm->last_seq_flush);
-		vm->last_seq_flush = dma_fence_get(fence);
-	} else {
-		amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
-	}
+	/* The reference of vm->last_seq_flush to prev moves into the chain */
+	prev = rcu_dereference_protected(vm->last_seq_flush,
+					 lockdep_is_held(&vm->eviction_lock));
+	dma_fence_chain_init(chain, prev,
+			     fence ? dma_fence_get(fence) : dma_fence_get_stub(),
+			     prev ? prev->seqno + 1 : 1);
+	rcu_assign_pointer(vm->last_seq_flush, &chain->base);
+
+	/* Prune signaled nodes, not every VM reads the sequence */
+	dma_fence_chain_for_each(iter, &chain->base);
 
 	/* Prepare a TLB flush fence to be attached to PTs */
 	/* The check for need_tlb_fence should be dropped once we
@@ -1177,7 +1148,7 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			   struct ttm_resource *res, dma_addr_t *pages_addr,
 			   struct dma_fence **fence)
 {
-	struct amdgpu_vm_tlb_seq_struct *tlb_cb;
+	struct dma_fence_chain *chain;
 	struct amdgpu_vm_update_params params;
 	struct amdgpu_res_cursor cursor;
 	int r, idx;
@@ -1185,8 +1156,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
 		return -ENODEV;
 
-	tlb_cb = kmalloc_obj(*tlb_cb);
-	if (!tlb_cb) {
+	chain = dma_fence_chain_alloc();
+	if (!chain) {
 		drm_dev_exit(idx);
 		return -ENOMEM;
 	}
@@ -1291,14 +1262,14 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		goto error_free;
 
 	if (params.needs_flush) {
-		amdgpu_vm_tlb_flush(&params, *fence, tlb_cb);
-		tlb_cb = NULL;
+		amdgpu_vm_tlb_flush(&params, *fence, chain);
+		chain = NULL;
 	}
 
 	amdgpu_vm_pt_free_list(adev, &params);
 
 error_free:
-	kfree(tlb_cb);
+	dma_fence_chain_free(chain);
 	amdgpu_vm_end_critical(&params);
 	drm_dev_exit(idx);
 	return r;
@@ -2685,7 +2656,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
-	vm->last_seq_flush = dma_fence_get_stub();
+	RCU_INIT_POINTER(vm->last_seq_flush, NULL);
 	vm->last_tlb_inv = dma_fence_get_stub();
 	vm->generation = amdgpu_vm_generation(adev, NULL);
 
@@ -2730,7 +2701,6 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 
 error_free_delayed:
 	dma_fence_put(vm->last_update);
-	dma_fence_put(vm->last_seq_flush);
 	dma_fence_put(vm->last_tlb_inv);
 	dma_fence_put(vm->last_unlocked);
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
@@ -2863,7 +2833,6 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	struct amdgpu_bo_va_mapping *mapping, *tmp;
 	bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
 	struct amdgpu_bo *root;
-	unsigned long flags;
 	int i;
 
 	amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
@@ -2876,11 +2845,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 				      false, MAX_SCHEDULE_TIMEOUT);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
-	dma_fence_wait(vm->last_seq_flush, false);
-	/* Make sure that all fence callbacks have completed */
-	dma_fence_lock_irqsave(vm->last_seq_flush, flags);
-	dma_fence_unlock_irqrestore(vm->last_seq_flush, flags);
-	dma_fence_put(vm->last_seq_flush);
+	dma_fence_put(rcu_dereference_protected(vm->last_seq_flush, true));
 	dma_fence_put(vm->last_tlb_inv);
 
 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index f6285a9d9e98..6142a86a8b80 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -24,6 +24,7 @@
 #ifndef __AMDGPU_VM_H__
 #define __AMDGPU_VM_H__
 
+#include <linux/dma-fence-chain.h>
 #include <linux/idr.h>
 #include <linux/kfifo.h>
 #include <linux/rbtree.h>
@@ -330,7 +331,7 @@ struct amdgpu_vm {
 
 	/* Last TLB seq/fence */
 	atomic64_t		tlb_seq;
-	struct dma_fence	*last_seq_flush;
+	struct dma_fence __rcu	*last_seq_flush;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
 	struct dma_fence	*last_tlb_inv;
@@ -554,22 +555,24 @@ bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);
  */
 static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
 {
-	unsigned long flags;
-	spinlock_t *lock;
+	u64 seq = atomic64_read(&vm->tlb_seq);
+	struct dma_fence *head, *iter;
 
-	/*
-	 * Workaround to stop racing between the fence signaling and handling
-	 * the cb. The lock is static after initially setting it up, just make
-	 * sure that the dma_fence structure isn't freed up.
-	 */
 	rcu_read_lock();
-	lock = dma_fence_spinlock(vm->last_seq_flush);
+	head = dma_fence_get_rcu_safe(&vm->last_seq_flush);
 	rcu_read_unlock();
-
-	spin_lock_irqsave(lock, flags);
-	spin_unlock_irqrestore(lock, flags);
-
-	return atomic64_read(&vm->tlb_seq);
+	if (!head)
+		return seq;
+
+	/* Add the updates in the chain which have finished, in any order */
+	seq += head->seqno;
+	dma_fence_chain_for_each(iter, head) {
+		if (!dma_fence_is_signaled(dma_fence_chain_contained(iter)))
+			--seq;
+	}
+	dma_fence_put(head);
+
+	return seq;
 }
 
 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev,
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25 13:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  4:45 [PATCH] drm/amdgpu: track finished page table updates with a fence chain chong li
2026-11-24  5:04 ` sashiko-bot
2026-11-25 10:45 ` Li, Chong(Alan)
2026-11-25 13:54   ` Christian König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox