drm/amdgpu: switch vm->tlb_seq to dma_fence_chain handling v3
Subsystems (2)
PATCH 1/1 drm/amdgpu: switch vm->tlb_seq to dma_fence_chain handling v3
The vm->tlb_seq handling for per VMID based TLB invalidation was always rather fragile and relied on grabbing the dma_fence spinlock to make sure that callbacks executed in the right order. Otherwise it could happen that different callbacks raced with each other (e.g. callback 1 starting worker while callback 2 incremented the sequence which is read by worker).
Alan now pointed out that even this doesn't work correctly because different PTDE/PTE updates might use different locks and their signaling is not necessarily in the same order and on the same GPU.
Ditch the whole approach and drop the callback, instead use a dma_fence_chain to track all PDE/PTE updates and just iterate over them to get the last completed one when asked. Also make sure that PDE updates use the same path as PTE updates for consistency.
This not only fixes rare GPU page faults in the kernel queues but also potentially issues with the user queues should a PDE update make a TLB invalidation necessary.
v2: also handle the case where the chain head is already signaled as
pointed out by Alan.
v3: keep stub fence and add NULL pointer check for CPU update
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 172 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 32 +--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c | 6 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c | 3 - .../gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c | 2 +- 5 files changed, 97 insertions(+), 118 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +89 -83
@@ -27,6 +27,7 @@*/ #include <linux/dma-fence-array.h> +#include <linux/dma-fence-chain.h> #include <linux/interval_tree_generic.h> #include <linux/idr.h> #include <linux/dma-buf.h>
@@ -114,21 +115,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
@@ -1026,6 +1012,71 @@ uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)return result; } +/** +* amdgpu_vm_tlb_seq - return tlb flush sequence number +* @vm: the amdgpu_vm structure to query +* +* Returns the tlb flush sequence number which indicates that the VM TLBs needs +* to be invalidated whenever the sequence number change. +*/ +uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm) +{ +struct dma_fence *fence; +u64 tlb_seq =0 ; + +rcu_read_lock (); + fence =dma_fence_get_rcu_safe (&vm->last_seq_flush); +rcu_read_unlock (); + +if (dma_fence_is_signaled (dma_fence_chain_contained (fence))) { + tlb_seq = fence->seqno; +dma_fence_put (fence); + }else { +do { + tlb_seq =to_dma_fence_chain (fence)->prev_seqno; + fence =dma_fence_chain_walk (fence); + }while (fence); + } +return tlb_seq; +} + +/** +* amdgpu_vm_tlb_flush - prepare TLB flush +* +* @params: parameters for update +* @chain: dma_fence_chain for the signaling callback +* @fence: input fence to sync TLB flush 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_chain *chain, +struct dma_fence *fence) +{ +struct amdgpu_vm *vm = params->vm; +struct dma_fence *prev; + + ++vm->tlb_seq; + prev =rcu_dereference_protected (vm->last_seq_flush, +lockdep_is_held (&vm->eviction_lock)); +dma_fence_chain_init (chain, prev,dma_fence_get (fence), vm->tlb_seq); +rcu_assign_pointer (vm->last_seq_flush, &chain->base); + +/* Prepare a TLB flush fence to be attached to PTs */ +/* The check for need_tlb_fence should be dropped once we +* sort out the issues with KIQ/MES TLB invalidation timeouts. +*/ +if (!params->unlocked && vm->need_tlb_fence) { +amdgpu_vm_tlb_fence_create (params->adev, vm, fence, + &vm->last_tlb_inv); + +/* Makes sure no PD/PT is freed before the flush */ +dma_resv_add_fence (vm->root.bo->tbo.base.resv, + vm->last_tlb_inv,DMA_RESV_USAGE_BOOKKEEP ); + } +} +/** * amdgpu_vm_update_pdes - make sure that all directories are valid *
@@ -1043,6 +1094,7 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev, {struct amdgpu_vm_update_params params;struct amdgpu_vm_bo_base *entry, *tmp; +struct dma_fence_chain *chain;bool flush_tlb_needed = false;int r, idx;
@@ -1054,11 +1106,21 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev,if (!drm_dev_enter (adev_to_drm (adev), &idx))return -ENODEV ; + chain =dma_fence_chain_alloc (); +if (!chain) { + r = -ENOMEM ; + goto error_dev_exit; + } +memset (¶ms,0 ,sizeof (params)); params.adev = adev; params.vm = vm; params.immediate = immediate; + r =amdgpu_vm_begin_critical (¶ms); +if (r) + goto error; + r = vm->update_funcs->prepare (¶ms,NULL ,AMDGPU_KERNEL_JOB_ID_VM_UPDATE_PDES );if (r)
@@ -1078,73 +1140,22 @@ int amdgpu_vm_update_pdes(struct amdgpu_device *adev, goto error;if (flush_tlb_needed) -atomic64_inc (&vm->tlb_seq); +amdgpu_vm_tlb_flush (¶ms, chain, vm->last_update); +else +dma_fence_chain_free (chain);list_for_each_entry_safe (entry, tmp, &vm->kernel.needs_update, vm_status)amdgpu_vm_bo_idle (entry); error: +amdgpu_vm_end_critical (¶ms); + +error_dev_exit:drm_dev_exit (idx);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 -* -* 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 amdgpu_vm *vm = params->vm; - - 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); - } - -/* Prepare a TLB flush fence to be attached to PTs */ -/* The check for need_tlb_fence should be dropped once we -* sort out the issues with KIQ/MES TLB invalidation timeouts. -*/ -if (!params->unlocked && vm->need_tlb_fence) { -amdgpu_vm_tlb_fence_create (params->adev, vm, fence, - &vm->last_tlb_inv); - -/* Makes sure no PD/PT is freed before the flush */ -dma_resv_add_fence (vm->root.bo->tbo.base.resv, - vm->last_tlb_inv,DMA_RESV_USAGE_BOOKKEEP ); - } -} -/** * amdgpu_vm_update_range - update a range in the vm page table *
@@ -1177,16 +1188,16 @@ 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 amdgpu_vm_update_params params;struct amdgpu_res_cursor cursor; +struct dma_fence_chain *chain;int r, idx;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 +1302,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, chain, *fence); + 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;
@@ -2863,7 +2874,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,10 +2886,6 @@ 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 (vm->last_tlb_inv);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +3 -29
@@ -329,8 +329,8 @@ struct amdgpu_vm {struct drm_sched_entity delayed;/* Last TLB seq/fence */ -atomic64_t tlb_seq; -struct dma_fence *last_seq_flush; +u64 tlb_seq; +struct dma_fence __rcu *last_seq_flush;atomic64_t kfd_last_flushed_seq;uint64_t tlb_fence_context;struct dma_fence *last_tlb_inv;
@@ -545,38 +545,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); -/** -* amdgpu_vm_tlb_seq - return tlb flush sequence number -* @vm: the amdgpu_vm structure to query -* -* Returns the tlb flush sequence number which indicates that the VM TLBs needs -* to be invalidated whenever the sequence number change. -*/ -static inline uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm) -{ -unsigned long flags; -spinlock_t *lock; - -/* -* 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); -rcu_read_unlock (); - -spin_lock_irqsave (lock, flags); -spin_unlock_irqrestore (lock, flags); - -return atomic64_read (&vm->tlb_seq); -} -void amdgpu_vm_update_fault_cache (struct amdgpu_device *adev,unsigned int pasid,uint64_t addr,uint32_t status,unsigned int vmhub); +uint64_t amdgpu_vm_tlb_seq (struct amdgpu_vm *vm); void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev, struct amdgpu_vm *vm, struct dma_fence *dep,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_cpu.c +4 -2
@@ -122,8 +122,10 @@ static int amdgpu_vm_cpu_commit(struct amdgpu_vm_update_params *p, {struct amdgpu_device *adev = p->adev; -if (p->needs_flush) -atomic64_inc (&p->vm->tlb_seq); +if (fence) { +dma_fence_put (*fence); + *fence =dma_fence_get_stub (); + }mb ();/* A reset flushed the HDP anyway, so that here can be skipped when a reset is ongoing */
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c +0 -3
@@ -117,9 +117,6 @@ static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,WARN_ON (ib->length_dw ==0 );amdgpu_ring_pad_ib (ring, ib); -if (p->needs_flush) -atomic64_inc (&p->vm->tlb_seq); -WARN_ON (ib->length_dw > p->num_dw_left); f =amdgpu_job_submit (p->job);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_tlb_fence.c +1 -1
@@ -112,7 +112,7 @@ void amdgpu_vm_tlb_fence_create(struct amdgpu_device *adev,spin_lock_init (&f->lock);dma_fence_init64 (&f->base, &amdgpu_tlb_fence_ops, &f->lock, - vm->tlb_fence_context,atomic64_read (&vm->tlb_seq)); + vm->tlb_fence_context, vm->tlb_seq);/* TODO: We probably need a separate wq here */ dma_fence_get (&f->base);
Signature · 1 line
2.43.0