Skip to contentSkip to conversation

drm/amdgpu: track finished page table updates with a fence chain

PATCH New chong li 2 participants 2 messages Nov 24

Subsystems (2)
lore
Share & tools
lore

chong li

author
#0 lore raw

PATCH 1/1 drm/amdgpu: track finished page table updates with a fence chain

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.

---
 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(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +21 -56
index a627a3092990..dafcebd95555 100644
@@ -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) {
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +17 -14
index f6285a9d9e98..6142a86a8b80 100644
@@ -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,
Signature · 1 line
2.48.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/amdgpu: O(N^2) chain walk in amdgpu_vm_tlb_flush() and amdgpu_vm_tlb_seq() - [Low] drm/amdgpu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/amdgpu: O(N^2) chain walk in amdgpu_vm_tlb_flush() and amdgpu_vm_tlb_seq()
- [Low] drm/amdgpu: missing Fixes trailer for TLB sequence race fix --

commit bb4d183d85e4f91ff2a4b58dd0ca99081bbe5e40
Author: chong li <[email protected]>

Quoted 22 lines

drm/amdgpu: track finished page table updates with a fence chain

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.

Quoted 1 line

[Severity: Low]
Should this bug fix include a Fixes tag?

Fixes 5255e146c99a ("drm/amdgpu: rework TLB flushing")
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index 94b1cfe05e3e0..4e5a1286ffcbb 100644

[ ... ]

Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +12 -9
@@ -1085,47 +1070,33 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
 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);

[ ... ]

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -0
index f6285a9d9e989..6142a86a8b802 100644

[ ... ]

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +6 -13
@@ -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;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo); · line 576

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +9 -0
+	/* 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;
 }

[Severity: Medium]
Does walking vm->last_seq_flush with dma_fence_chain_for_each() in amdgpu_vm_tlb_flush() and amdgpu_vm_tlb_seq() turn batches of N in-flight page table updates into an O(N^2) chain walk?

dma_fence_chain_walk() only unlinks signaled predecessors and visits every unsignaled node in the chain, acquiring and releasing a reference at each step. When N updates requiring a TLB flush are queued while their SDMA fences are still unsignaled (such as amdgpu_vm_clear_freed() looping over vm->freed while SDMA jobs wait on active VM submissions), each amdgpu_vm_tlb_flush() call walks all prior unsignaled nodes in vm->last_seq_flush. In addition, amdgpu_vmid_grab() calls amdgpu_vm_tlb_seq() up to twice while holding the per-VMHUB id_mgr->lock mutex, walking all unsignaled chain nodes under the lock, and amdgpu_vm_tlb_flush() allocates and chains a node even when fence is NULL or already signaled.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1