* [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(¶ms, *fence, tlb_cb); - tlb_cb = NULL; + amdgpu_vm_tlb_flush(¶ms, *fence, chain); + chain = NULL; } amdgpu_vm_pt_free_list(adev, ¶ms); error_free: - kfree(tlb_cb); + dma_fence_chain_free(chain); amdgpu_vm_end_critical(¶ms); 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
* Re: [PATCH] drm/amdgpu: track finished page table updates with a fence chain 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) 1 sibling, 0 replies; 4+ messages in thread From: sashiko-bot @ 2026-11-24 5:04 UTC (permalink / raw) To: chong li; +Cc: amd-gfx 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 <chongli2@amd.com> > 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. > > Signed-off-by: chong li <chongli2@amd.com> [Severity: Low] Should this bug fix include a Fixes tag? Fixes: 5255e146c99a ("drm/amdgpu: rework TLB flushing") > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c > index 94b1cfe05e3e0..4e5a1286ffcbb 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c [ ... ] > @@ -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); [ ... ] > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h > index f6285a9d9e989..6142a86a8b802 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h [ ... ] > @@ -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; > } [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. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261124044544.3561866-1-chongli2@amd.com?part=1 ^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH] drm/amdgpu: track finished page table updates with a fence chain 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 1 sibling, 1 reply; 4+ messages in thread From: Li, Chong(Alan) @ 2026-11-25 10:45 UTC (permalink / raw) To: Koenig, Christian, timur.kristof@gmail.com; +Cc: amd-gfx@lists.freedesktop.org AMD General > Yeah that doesn't work either. There is no guarantee that the sequence > numbers in the chain are contigious because of garbage collection. My patch does not need the numbers of the remaining nodes to be contiguous. It only uses the seqno of the head. For the nodes in the chain it only checks whether their fence has signaled: seq += head->seqno; dma_fence_chain_for_each(iter, head) { if (!dma_fence_is_signaled(dma_fence_chain_contained(iter))) --seq; } - The nodes are numbered 1, 2, 3, ... when they are added, so head->seqno is the number of updates added so far. - Garbage collection only removes nodes whose fence has signaled, so every unsignaled node is still in the chain. - head->seqno minus the number of unsignaled nodes is therefore the number of finished updates, no matter which nodes were collected or in which order they finished. > I will fix the code to break on the first signaled one and send that > patch out again. If I understand this correctly, the loop starts from the head and breaks at the head when the head has signaled, so the older nodes are not checked. When an older node signals later, amdgpu_vm_tlb_seq() still returns the seqno of the head, so a job which waits for the older node can skip the flush. The count in my patch changes whenever a node signals, also in this case. Could you take another look at my patch? If you prefer to do the fix yourself, I can help review the new version. -----Original Message----- From: Li, Chong(Alan) <Chong.Li@amd.com> Sent: Tuesday, November 24, 2026 12:46 PM To: Koenig, Christian <Christian.Koenig@amd.com>; timur.kristof@gmail.com Cc: amd-gfx@lists.freedesktop.org; Li, Chong(Alan) <Chong.Li@amd.com> Subject: [PATCH] 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. 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(¶ms, *fence, tlb_cb); - tlb_cb = NULL; + amdgpu_vm_tlb_flush(¶ms, *fence, chain); + chain = NULL; } amdgpu_vm_pt_free_list(adev, ¶ms); error_free: - kfree(tlb_cb); + dma_fence_chain_free(chain); amdgpu_vm_end_critical(¶ms); 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
* Re: [PATCH] drm/amdgpu: track finished page table updates with a fence chain 2026-11-25 10:45 ` Li, Chong(Alan) @ 2026-11-25 13:54 ` Christian König 0 siblings, 0 replies; 4+ messages in thread From: Christian König @ 2026-11-25 13:54 UTC (permalink / raw) To: Li, Chong(Alan), timur.kristof@gmail.com; +Cc: amd-gfx@lists.freedesktop.org On 11/25/26 11:45, Li, Chong(Alan) wrote: > AMD General > >> Yeah that doesn't work either. There is no guarantee that the sequence >> numbers in the chain are contigious because of garbage collection. > > My patch does not need the numbers of the remaining nodes to be > contiguous. It only uses the seqno of the head. For the nodes in the > chain it only checks whether their fence has signaled: > > seq += head->seqno; > dma_fence_chain_for_each(iter, head) { > if (!dma_fence_is_signaled(dma_fence_chain_contained(iter))) > --seq; > } > > - The nodes are numbered 1, 2, 3, ... when they are added, so > head->seqno is the number of updates added so far. > - Garbage collection only removes nodes whose fence has signaled, so > every unsignaled node is still in the chain. > - head->seqno minus the number of unsignaled nodes is therefore the > number of finished updates, no matter which nodes were collected or > in which order they finished. Except for the first fence you will never see a signaled fence returned by dma_fence_chain_for_each(). Additional to that it can be that multiple threads walk the dma_fence_chain up at the same time resulting in different results on different CPUs. That is not necessarily incorrect but also not the best for debugging either. >> I will fix the code to break on the first signaled one and send that >> patch out again. > > If I understand this correctly, the loop starts from the head and > breaks at the head when the head has signaled, so the older nodes are > not checked. When an older node signals later, amdgpu_vm_tlb_seq() > still returns the seqno of the head, so a job which waits for the older > node can skip the flush. > > The count in my patch changes whenever a node signals, also in this case. > Could you take another look at my patch? > If you prefer to do the fix yourself, I can help review the new version. I will send out a fixed version in a minute or so. Regards, Christian. > > > -----Original Message----- > From: Li, Chong(Alan) <Chong.Li@amd.com> > Sent: Tuesday, November 24, 2026 12:46 PM > To: Koenig, Christian <Christian.Koenig@amd.com>; timur.kristof@gmail.com > Cc: amd-gfx@lists.freedesktop.org; Li, Chong(Alan) <Chong.Li@amd.com> > Subject: [PATCH] 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. > > 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(¶ms, *fence, tlb_cb); > - tlb_cb = NULL; > + amdgpu_vm_tlb_flush(¶ms, *fence, chain); > + chain = NULL; > } > > amdgpu_vm_pt_free_list(adev, ¶ms); > > error_free: > - kfree(tlb_cb); > + dma_fence_chain_free(chain); > amdgpu_vm_end_critical(¶ms); > 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 [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 an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.