Consolidate and fix root PD fence resverations
Subsystems (3)
v2 cover Consolidate and fix root PD fence resverations
In [1] I originally wanted to just stop using amdgpu_bo_fence. However while attempting to address Christian's comment about reserving the fences upfront when the PD is locked, I realized that there are many places that are not reserving enough fences in the worst-case.
I went through and audited every lock of the VM root PD and I think I have caught everything missing fence reservations in this series. I have lightly tested this and didn't see any issues, and in general this is reserving more fences than the typical case so it shouldn't cause any BUG_ONs.
Patch 1 is mostly an independent fix. It is not related to the VM root PD, but follows almost the same logic as patch 2 through 5 below.
Patch 2 through 5 here are the reservation fixes. Basically since we started enabling more clear/move entities to be used, we should be reserving the worst-case space upfront. Also when the TLB flush fence was added we did not add reserve space for it.
Patch 6 and 7 are cleanups that I found (not everywhere locking the PD does validation or VM updates), and patches 8 through 10 are the amdgpu_bo_fence() removal series from [1].
[1]: https://lore.kernel.org/amd-gfx/[email protected]/
Benjamin Cheng (10):
drm/amdkfd: reserve TTM move fences
drm/amdgpu/gem: reserve fences on the VM root PD drm/amdgpu/vm: reserve more fences in amdgpu_vm_lock_pd drm/amdgpu/dma_buf: reserve AMDGPU_VM_UPDATE_FENCES for move_notify drm/amdgpu/vm: reserve AMDGPU_VM_UPDATE_FENCES for handle_fault drm/amdgpu: reserve only what the caller asks for in lock_pd drm/amdgpu/userq: remove extra fence request when locking PD drm/amdgpu/vm: stop using amdgpu_bo_fence drm/amdgpu/uvd: stop using amdgpu_bo_fence drm/amdgpu/object: get rid of amdgpu_bo_fence
.../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 16 +++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 3 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 13 ++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 25 ------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 2 -- drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 12 +++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 ++--- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 17 ++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 8 ++++++ drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 3 ++- 14 files changed, 68 insertions(+), 57 deletions(-)
Signature · 1 line
2.56.1
PATCH v2 1/10 drm/amdkfd: reserve TTM move fences
Validation of the BO can add up to TTM_NUM_MOVE_FENCES, so we should fix that.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -1
@@ -3009,7 +3009,8 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *struct drm_gem_object *gobj; gobj = &mem->bo->tbo.base; - ret =drm_exec_prepare_obj (&exec, gobj,1 ); + ret =drm_exec_prepare_obj (&exec, gobj, +TTM_NUM_MOVE_FENCES +1 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) {pr_err ("drm_exec_prepare_obj failed, ret: %d\n" , ret);
Signature · 1 line
2.56.1
Re: [PATCH v2 01/10] drm/amdkfd: reserve TTM move fences
Quoted 5 lines · Benjamin Cheng
Validation of the BO can add up to
TTM_NUM_MOVE_FENCES, so we should fix that.Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")Signed-off-by Benjamin Cheng <[email protected]>
Quoted 18 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2
-1@@ -3009,7 +3009,8 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *struct drm_gem_object *gobj; gobj = &mem->bo->tbo.base; - ret =drm_exec_prepare_obj (&exec, gobj,1 ); + ret =drm_exec_prepare_obj (&exec, gobj, +TTM_NUM_MOVE_FENCES +1 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) {pr_err ("drm_exec_prepare_obj failed, ret: %d\n" , ret);
PATCH v2 2/10 drm/amdgpu/gem: reserve fences on the VM root PD
In reply to Benjamin ChengWhen creating a BO that shares the VM root PD's resv, TTM eviction fences are added to the resv, and there is another fence added for the BO clear job.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +8 -0
@@ -504,6 +504,14 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,if (r)return r; +/* ttm_bo_init_reserved() + clear fence */ + r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv, +TTM_NUM_MOVE_FENCES +1 ); +if (r) { +amdgpu_bo_unreserve (vm->root.bo); +return r; + } + resv = vm->root.bo->tbo.base.resv; }
Signature · 1 line
2.56.1
Re: [PATCH v2 02/10] drm/amdgpu/gem: reserve fences on the VM root PD
Quoted 3 lines · Benjamin Cheng
When creating a BO that shares the VM root PD's resv, TTM eviction fences are added to the resv, and there is another fence added for the BO clear job.
Good catch.
Quoted 1 line
Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")
Yeah that is most likely incorrect. The bug existed for as long as the driver was upstream.
It's just that nobody ever noticed because it doesn't matter in practice (the VM root PD should always have enough free slots).
It's just that the automated checkers could complain if we start to run them together with lockdep enablement.
Quoted 1 line
Signed-off-by Benjamin Cheng <[email protected]>
Quoted 23 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 8 ++++++++ 1 file changed, 8 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +8
-0@@ -504,6 +504,14 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,if (r)return r; +/* ttm_bo_init_reserved() + clear fence */ + r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv, +TTM_NUM_MOVE_FENCES +1 ); +if (r) { +amdgpu_bo_unreserve (vm->root.bo); +return r; + } + resv = vm->root.bo->tbo.base.resv; }
PATCH v2 3/10 drm/amdgpu/vm: reserve more fences in amdgpu_vm_lock_pd
In reply to Benjamin ChengAny page table update may currently add up to three fences to the root PD's resv (last_unlocked, SDMA commit, and TLB flush). In addition if any PD/PT BOs need to be allocated, it can add up to TTM_NUM_MOVE_FENCES on top.
Introduce AMDGPU_VM_UPDATE_FENCES for this count and reserve it in amdgpu_vm_lock_pd().
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 6 +++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 8 ++++++++ 3 files changed, 12 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -3
@@ -3073,9 +3073,9 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)struct amdgpu_vm *vm = bo_base->vm;/* -* Fences for the two SDMA page table updates were already -* reserved by amdgpu_vm_lock_pd() above (it reserves 2 + -* num_fences on the same VM root PD dma_resv). +* Fences for the two page table updates were already +* reserved by amdgpu_vm_lock_pd() above on the same VM +* root PD dma_resv. */ r =amdgpu_vm_clear_freed (adev, vm,NULL );if (!r)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -2
@@ -462,9 +462,8 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,int amdgpu_vm_lock_pd (struct amdgpu_vm *vm,struct drm_exec *exec,unsigned int num_fences) { -/* We need at least two fences for the VM PD/PT updates */ return drm_exec_prepare_obj (exec, &vm->root.bo->tbo.base, -2 + num_fences); +AMDGPU_VM_UPDATE_FENCES + num_fences); }/**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +8 -0
@@ -187,6 +187,14 @@ struct amdgpu_bo_vm;#define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0)#define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1) +/* +* The number of fence slots needed on the root PD resv for a page table +* update: last_unlocked, the SDMA commit fence, the TLB flush fence, plus +* the TTM eviction/move fences of any PD/PT BO that is validated or allocated. +* This needs to be bumped whenever a new fence is added. +*/ +#define AMDGPU_VM_UPDATE_FENCES (3 + TTM_NUM_MOVE_FENCES) +/* VMPT level enumerate, and the hiberachy is: * PDB3->PDB2->PDB1->PDB0->PTB */
Signature · 1 line
2.56.1
PATCH v2 4/10 drm/amdgpu/dma_buf: reserve AMDGPU_VM_UPDATE_FENCES for move_notify
In reply to Benjamin ChengThis is about to update the VM so we need to use the macro previously introduced to make sure there are enough fence slots.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
@@ -562,7 +562,7 @@ amdgpu_dma_buf_move_notify(struct dma_buf_attachment *attach) }/* Reserve fences for two SDMA page table updates */ - r =dma_resv_reserve_fences (resv,2 ); + r =dma_resv_reserve_fences (resv,AMDGPU_VM_UPDATE_FENCES );if (!r) r =amdgpu_vm_clear_freed (adev, vm,NULL );
Signature · 1 line
2.56.1
PATCH v2 5/10 drm/amdgpu/vm: reserve AMDGPU_VM_UPDATE_FENCES for handle_fault
In reply to Benjamin ChengReserving 1 hasn't been enough here for a while (last_unlocked was never considered originally).
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -3186,7 +3186,7 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, value =0 ; } - r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,1 ); + r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,AMDGPU_VM_UPDATE_FENCES );if (r) {pr_debug ("failed %d to reserve fence slot\n" , r); goto error_unlock;
Signature · 1 line
2.56.1
PATCH v2 6/10 drm/amdgpu: reserve only what the caller asks for in lock_pd
In reply to Benjamin Chengamdgpu_vm_lock_pd() reserved AMDGPU_VM_UPDATE_FENCES for every caller, including ones that only lock the root PD to add or remove a mapping and never update the page tables.
Address this by making it only reserve what the caller asks for, so that such callers have the option to not over-reserve. The only touched call-sites in this change are the ones that do require fences.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 3 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 5 +++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 6 ++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 +++++--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 3 ++- 7 files changed, 27 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +8 -5
@@ -1188,7 +1188,8 @@ static int reserve_bo_and_vm(struct kgd_mem *mem, ctx->sync = &mem->sync;drm_exec_init (&ctx->exec,DRM_EXEC_INTERRUPTIBLE_WAIT ,0 ); drm_exec_until_all_locked(&ctx->exec) { - ret =amdgpu_vm_lock_pd (vm, &ctx->exec,2 ); + ret =amdgpu_vm_lock_pd (vm, &ctx->exec, +AMDGPU_VM_UPDATE_FENCES +2 );drm_exec_retry_on_contention (&ctx->exec);if (unlikely (ret)) goto error;
@@ -1235,8 +1236,8 @@ static int reserve_bo_and_cond_vms(struct kgd_mem *mem, && map_type != BO_VM_ALL))continue ; - ret =amdgpu_vm_lock_pd (entry->bo_va->base.vm, - &ctx->exec,2 ); + ret =amdgpu_vm_lock_pd (entry->bo_va->base.vm, &ctx->exec, +AMDGPU_VM_UPDATE_FENCES +2 );drm_exec_retry_on_contention (&ctx->exec);if (unlikely (ret)) goto error;
@@ -2729,7 +2730,8 @@ static int validate_invalid_user_pages(struct amdkfd_process_info *process_info)/* Reserve all the page directories */ list_for_each_entry (peer_vm, &process_info->vm_list_head, vm_list_node) { - ret =amdgpu_vm_lock_pd (peer_vm, &exec,2 ); + ret =amdgpu_vm_lock_pd (peer_vm, &exec, +AMDGPU_VM_UPDATE_FENCES +2 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) goto unreserve_out;
@@ -2993,7 +2995,8 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu * drm_exec_until_all_locked(&exec) {list_for_each_entry (peer_vm, &process_info->vm_list_head, vm_list_node) { - ret =amdgpu_vm_lock_pd (peer_vm, &exec,2 ); + ret =amdgpu_vm_lock_pd (peer_vm, &exec, +AMDGPU_VM_UPDATE_FENCES +2 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) {pr_err ("Locking VM PD failed, ret: %d\n" , ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -1
@@ -940,7 +940,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, } drm_exec_until_all_locked(&p->exec) { - r =amdgpu_vm_lock_pd (&fpriv->vm, &p->exec,1 + p->gang_size); + r =amdgpu_vm_lock_pd (&fpriv->vm, &p->exec, +AMDGPU_VM_UPDATE_FENCES +1 + p->gang_size);drm_exec_retry_on_contention (&p->exec);if (unlikely (r)) goto out_free_user_pages;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3 -2
@@ -345,7 +345,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,if (unlikely (r)) goto out_unlock; - r =amdgpu_vm_lock_pd (vm, &exec,0 ); + r =amdgpu_vm_lock_pd (vm, &exec,AMDGPU_VM_UPDATE_FENCES );drm_exec_retry_on_contention (&exec);if (unlikely (r)) goto out_unlock;
@@ -1002,7 +1002,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, goto error; } - r =amdgpu_vm_lock_pd (&fpriv->vm, &exec,2 ); + r =amdgpu_vm_lock_pd (&fpriv->vm, &exec, +AMDGPU_VM_UPDATE_FENCES +2 );drm_exec_retry_on_contention (&exec);if (unlikely (r)) goto error;
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +4 -2
@@ -3053,7 +3053,8 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo) goto fini;for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { - r =amdgpu_vm_lock_pd (bo_base->vm, &exec,0 ); + r =amdgpu_vm_lock_pd (bo_base->vm, &exec, +AMDGPU_VM_UPDATE_FENCES );drm_exec_retry_on_contention (&exec);if (unlikely (r)) goto fini;
@@ -4788,7 +4789,8 @@ static int amdgpu_ualink_reserve_npa_vm_and_bos(struct amdgpu_device *adev, goto out; } - r =amdgpu_vm_lock_pd (&adev->ualink.npa_vm, exec,0 ); + r =amdgpu_vm_lock_pd (&adev->ualink.npa_vm, exec, +AMDGPU_VM_UPDATE_FENCES );drm_exec_retry_on_contention (exec);if (unlikely (r)) goto out;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -3
@@ -1054,10 +1054,10 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); drm_exec_until_all_locked(&exec) {/* -* Rearm adds one BOOKKEEP fence and validation may queue move fences -* on the root PD BO, so reserve caller-side slots accordingly. +* Rearm adds one BOOKKEEP fence and validation requires +* AMDGPU_VM_UPDATE_FENCES. */ - ret =amdgpu_vm_lock_pd (vm, &exec,TTM_NUM_MOVE_FENCES +1 ); + ret =amdgpu_vm_lock_pd (vm, &exec,AMDGPU_VM_UPDATE_FENCES +1 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) goto unlock_all;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -3
@@ -455,15 +455,17 @@ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,* * @vm: vm providing the BOs * @exec: drm execution context -* @num_fences: number of extra fences to reserve +* @num_fences: number of fence slots to reserve, 0 to only lock the PD * * Lock the VM root PD in the DRM execution context. */ int amdgpu_vm_lock_pd (struct amdgpu_vm *vm,struct drm_exec *exec,unsigned int num_fences) { -return drm_exec_prepare_obj (exec, &vm->root.bo->tbo.base, -AMDGPU_VM_UPDATE_FENCES + num_fences); +if (!num_fences) +return drm_exec_lock_obj (exec, &vm->root.bo->tbo.base); + +return drm_exec_prepare_obj (exec, &vm->root.bo->tbo.base, num_fences); }/**
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -1
@@ -1686,7 +1686,8 @@ static int svm_range_reserve_bos(struct svm_validate_context *ctx, bool intr) } vm =drm_priv_to_vm (pdd->drm_priv); - r =amdgpu_vm_lock_pd (vm, &ctx->exec,2 ); + r =amdgpu_vm_lock_pd (vm, &ctx->exec, +AMDGPU_VM_UPDATE_FENCES +2 );drm_exec_retry_on_contention (&ctx->exec);if (unlikely (r)) {pr_debug ("failed %d to reserve bo\n" , r);
Signature · 1 line
2.56.1
PATCH v2 7/10 drm/amdgpu/userq: remove extra fence request when locking PD
In reply to Benjamin ChengThese two fences are never used, so just request no fences (lock only).
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 2 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
@@ -382,7 +382,7 @@ static int amdgpu_userq_fence_read_wptr(struct amdgpu_device *adev,drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,2 ); drm_exec_until_all_locked(&exec) { - ret =amdgpu_vm_lock_pd (queue->vm, &exec,1 ); + ret =amdgpu_vm_lock_pd (queue->vm, &exec,0 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) goto lock_error;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -1
@@ -48,7 +48,7 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,2 ); drm_exec_until_all_locked(&exec) { - ret =amdgpu_vm_lock_pd (vm, &exec,1 ); + ret =amdgpu_vm_lock_pd (vm, &exec,0 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) goto fail_lock;
Signature · 1 line
2.56.1
PATCH v2 8/10 drm/amdgpu/vm: stop using amdgpu_bo_fence
In reply to Benjamin ChengSwitch to stop using this deprecated helper.
The original could be incorrect under certain circumstances, since the amdgpu_bo_fence only reserves one slot, and at the time of calling the SDMA commit and TLB flush fences were not added yet (the reserve_fences is not additive). In addition this allows us to switch to use BOOKKEEP usage.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1216,7 +1216,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,if (!unlocked && !dma_fence_is_signaled (vm->last_unlocked)) {struct dma_fence *tmp =dma_fence_get_stub (); -amdgpu_bo_fence (vm->root.bo, vm->last_unlocked, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, vm->last_unlocked, +DMA_RESV_USAGE_BOOKKEEP );swap (vm->last_unlocked, tmp);dma_fence_put (tmp); }
Signature · 1 line
2.56.1
PATCH v2 9/10 drm/amdgpu/uvd: stop using amdgpu_bo_fence
In reply to Benjamin ChengThis helper is deprecated since it is incorrect in most circumstances. This was a circumstance where it was mostly correct but it is the only remaining usage, so get rid of it here.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +14 -3
@@ -1154,16 +1154,24 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,if (r) goto err_free; }else { + r =amdgpu_bo_reserve (bo, true); +if (r) + goto err_free; + r =drm_sched_job_add_resv_dependencies (&job->base, bo->tbo.base.resv,DMA_RESV_USAGE_KERNEL );if (r) - goto err_free; + goto err_unreserve; f =amdgpu_job_submit (job); -amdgpu_bo_reserve (bo, true); -amdgpu_bo_fence (bo, f, false); + r =dma_resv_reserve_fences (bo->tbo.base.resv,1 ); +if (r) { +dma_fence_wait (f, false); + r =0 ; + }else +dma_resv_add_fence (bo->tbo.base.resv, f,DMA_RESV_USAGE_BOOKKEEP );amdgpu_bo_unreserve (bo); }
@@ -1173,6 +1181,9 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,return 0 ; +err_unreserve: +amdgpu_bo_unreserve (bo); + err_free:amdgpu_job_free (job);return r;
Signature · 1 line
2.56.1
PATCH v2 10/10 drm/amdgpu/object: get rid of amdgpu_bo_fence
In reply to Benjamin ChengAll uses of this function have been removed, so we can now get rid of this function.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 25 ---------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 2 -- 2 files changed, 27 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -25
@@ -1463,31 +1463,6 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)return 0 ; } -/** -* amdgpu_bo_fence - add fence to buffer object -* -* @bo: buffer object in question -* @fence: fence to add -* @shared: true if fence should be added shared -* -*/ -void amdgpu_bo_fence (struct amdgpu_bo *bo,struct dma_fence *fence, -bool shared) -{ -struct dma_resv *resv = bo->tbo.base.resv; -int r; - - r =dma_resv_reserve_fences (resv,1 ); -if (r) { -/* As last resort on OOM we block for the fence */ -dma_fence_wait (fence, false); -return ; - } - -dma_resv_add_fence (resv, fence, shared ?DMA_RESV_USAGE_READ : -DMA_RESV_USAGE_WRITE ); -} -/** * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences *
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -2
@@ -305,8 +305,6 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, struct ttm_resource *new_mem);void amdgpu_bo_release_notify (struct ttm_buffer_object *bo);vm_fault_t amdgpu_bo_fault_reserve_notify (struct ttm_buffer_object *bo); -void amdgpu_bo_fence (struct amdgpu_bo *bo,struct dma_fence *fence, -bool shared);int amdgpu_bo_sync_wait_resv (struct amdgpu_device *adev,struct dma_resv *resv,enum amdgpu_sync_mode sync_mode,void *owner,bool intr);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 10/10] drm/amdgpu/object: get rid of amdgpu_bo_fence
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: removing amdgpu_bo_fence() breaks amdgpu_gem_object_close() build --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: removing amdgpu_bo_fence() breaks amdgpu_gem_object_close() build
--
commit 6a4de07fd2509683647edc1a8831db741d691fd3
Author: Benjamin Cheng <[email protected]>
drm/amdgpu/object: get rid of amdgpu_bo_fence
This commit removes the definition and declaration of amdgpu_bo_fence().
Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-18@@ -1441,31 +1441,6 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)return 0 ; } -/** -* amdgpu_bo_fence - add fence to buffer object -* -* @bo: buffer object in question -* @fence: fence to add -* @shared: true if fence should be added shared -* -*/ -void amdgpu_bo_fence (struct amdgpu_bo *bo,struct dma_fence *fence, -bool shared) -{ -struct dma_resv *resv = bo->tbo.base.resv; -int r; - - r =dma_resv_reserve_fences (resv,1 ); -if (r) { -/* As last resort on OOM we block for the fence */ -dma_fence_wait (fence, false);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo) · line 1446
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-7-return ; - } - -dma_resv_add_fence (resv, fence, shared ?DMA_RESV_USAGE_READ : -DMA_RESV_USAGE_WRITE ); -} -/** * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences *
[Severity: High]
Does removing amdgpu_bo_fence() break the build in amdgpu_gem_object_close()?
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:amdgpu_gem_object_close() {
...
r = amdgpu_vm_clear_freed(adev, vm, &fence);
if (unlikely(r < 0) && !drm_dev_is_unplugged(adev_to_drm(adev)))
dev_err(adev->dev, "failed to clear page "
"tables on GEM object close (%ld)\n", r);
if (r || !fence)
goto out_unlock;
amdgpu_bo_fence(bo, fence, true);
dma_fence_put(fence);
...
}amdgpu_gem_object_close() still calls amdgpu_bo_fence(), so removing it causes compilation of amdgpu_gem.c to fail with an implicit function declaration error.
Also note that in amdgpu_gem_object_close(), drm_exec_prepare_obj(&exec, &bo->tbo.base, 1) is followed by amdgpu_vm_lock_pd(vm, &exec, AMDGPU_VM_UPDATE_FENCES). Because dma_resv_reserve_fences() is not additive, only AMDGPU_VM_UPDATE_FENCES slots remain reserved when bo shares the VM root PD dma_resv, while amdgpu_vm_clear_freed() plus adding the fence to bo requires AMDGPU_VM_UPDATE_FENCES + 1 slots if replaced with a direct dma_resv_add_fence() call.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH v3 1/7 drm/amdkfd: reserve TTM move fences
In reply to Benjamin ChengValidation of the BO can add up to TTM_NUM_MOVE_FENCES, so we should fix that.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -1
@@ -3009,7 +3009,8 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *struct drm_gem_object *gobj; gobj = &mem->bo->tbo.base; - ret =drm_exec_prepare_obj (&exec, gobj,1 ); + ret =drm_exec_prepare_obj (&exec, gobj, +TTM_NUM_MOVE_FENCES +1 );drm_exec_retry_on_contention (&exec);if (unlikely (ret)) {pr_err ("drm_exec_prepare_obj failed, ret: %d\n" , ret);
Signature · 1 line
2.56.1
PATCH v3 2/7 drm/amdgpu/gem: reserve fences on the VM root PD
When creating a BO that shares the VM root PD's resv, TTM eviction fences are added to the resv, and there is another fence added for the BO clear job.
---
v3:
- Christian pointed out this is most likely only a theoretical bug that doesn't matter in practice so dropped the Fixes tag.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +8 -0
@@ -504,6 +504,14 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,if (r)return r; +/* ttm_bo_init_reserved() + clear fence */ + r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv, +TTM_NUM_MOVE_FENCES +1 ); +if (r) { +amdgpu_bo_unreserve (vm->root.bo); +return r; + } + resv = vm->root.bo->tbo.base.resv; }
Signature · 1 line
2.56.1
PATCH v3 3/7 drm/amdgpu/vm: reserve TTM_NUM_MOVE_FENCES in amdgpu_vm_lock_pd
In reply to Benjamin ChengPage table updates may result in PT/PD allocation, which can add up to TTM_NUM_MOVE_FENCES to the resv.
---
v3:
(Christian)
- Don't introduce AMDGPU_VM_UPDATE_FENCES
- Use 2 instead of 3 here since last_unlocked and SDMA commit are from the same entity
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -464,7 +464,7 @@ int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec, {/* We need at least two fences for the VM PD/PT updates */ return drm_exec_prepare_obj (exec, &vm->root.bo->tbo.base, -2 + num_fences); +2 +TTM_NUM_MOVE_FENCES + num_fences); }/**
Signature · 1 line
2.56.1
Christian König
maintainerRe: [PATCH v3 3/7] drm/amdgpu/vm: reserve TTM_NUM_MOVE_FENCES in amdgpu_vm_lock_pd
Quoted 21 lines · Benjamin Cheng
Page table updates may result in PT/PD allocation, which can add up to
TTM_NUM_MOVE_FENCESto the resv.Fixes 897ee11ec020 ("drm/amdgpu: create multiple clear/move ttm entities")Fixes ddc3c87584e2 ("drm/amdgpu: clear vm pts upon a vm->generation mismatch")Signed-off-by Benjamin Cheng <[email protected]>---
v3:
(Christian)
- Don't introduceAMDGPU_VM_UPDATE_FENCES
- Use 2 instead of 3 here sincelast_unlockedand SDMA commit are from the same entity--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec, · line 465
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0@@ -464,7 +464,7 @@ int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec, {/* We need at least two fences for the VM PD/PT updates */
Please update the comment as well:
/*
* One fence slot for clearing newly allocated PDs/PTs and PDE/PTE updates,
* another fence slots for TLB invalidation fences, TTM_NUM_MOVE_FENCES
* for validating PDs/PTs after eviction plus anything the caller requested. */
With that done Reviewed-by: Christian König <[email protected]>
Regards,
Christian.
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1return drm_exec_prepare_obj (exec, &vm->root.bo->tbo.base, -2 + num_fences); +2 +TTM_NUM_MOVE_FENCES + num_fences); }/**
PATCH v3 4/7 drm/amdgpu/vm: reserve space for last_unlocked in handle_fault
In reply to Benjamin ChengNormally last_unlocked lives on the same entity (vm->delayed) as the SDMA commit so they would use the same slot, but in this case handle_fault calls update_range in immediate mode, making the SDMA commit live in the immediate entity.
Since the two fences are not in the same entity, we need to reserve two fence slots here.
---
v3:
(Christian)
- No TLB fence or TTM_NUM_MOVE_FENCES need to be added here.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5 -1
@@ -3187,7 +3187,11 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, value =0 ; } - r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,1 ); +/* update_range operates in immediate mode here meaning the SDMA commit +* is on the immediate entity, while the last_unlocked comes from the +* delayed entity. +*/ + r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,2 );if (r) {pr_debug ("failed %d to reserve fence slot\n" , r); goto error_unlock;
Signature · 1 line
2.56.1
Christian König
maintainerRe: [PATCH v3 4/7] drm/amdgpu/vm: reserve space for last_unlocked in handle_fault
Quoted 23 lines · Benjamin Cheng
Normally
last_unlockedlives on the same entity (vm->delayed) as the SDMA commit so they would use the same slot, but in this casehandle_faultcallsupdate_rangein immediate mode, making the SDMA commit live in the immediate entity.Since the two fences are not in the same entity, we need to reserve two fence slots here.
Fixes 2b665c3735b3 ("drm/amdgpu: reserve fence slot to update page table")Signed-off-by Benjamin Cheng <[email protected]>---
v3:
(Christian)
- No TLB fence orTTM_NUM_MOVE_FENCESneed to be added here.--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, · line 3193
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-1@@ -3187,7 +3187,11 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, value =0 ; } - r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,1 ); +/* update_range operates in immediate mode here meaning the SDMA commit +* is on the immediate entity, while the last_unlocked comes from the +* delayed entity. +*/
Oh that is a good catch but clearly not the right way of fixing this.
We should not touch the last_unlocked fence in any way possible while doing an immediate update to handle a page fault.
My patch set already drops that handling, so we should be save for now. Please review patch "drm/amdgpu/vm: add amdgpu_vm_pt_leaves() v4" on the mailing list instead.
Thanks,
Christian.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0+ r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,2 );if (r) {pr_debug ("failed %d to reserve fence slot\n" , r); goto error_unlock;
Re: [PATCH v3 4/7] drm/amdgpu/vm: reserve space for last_unlocked in handle_fault
Quoted 34 lines · Christian König
Quoted 23 lines · Benjamin Cheng
Normally
last_unlockedlives on the same entity (vm->delayed) as the SDMA commit so they would use the same slot, but in this casehandle_faultcallsupdate_rangein immediate mode, making the SDMA commit live in the immediate entity.Since the two fences are not in the same entity, we need to reserve two fence slots here.
Fixes 2b665c3735b3 ("drm/amdgpu: reserve fence slot to update page table")Signed-off-by Benjamin Cheng <[email protected]>---
v3:
(Christian)
- No TLB fence orTTM_NUM_MOVE_FENCESneed to be added here.--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, · line 3193
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-1@@ -3187,7 +3187,11 @@ bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, value =0 ; } - r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,1 ); +/* update_range operates in immediate mode here meaning the SDMA commit +* is on the immediate entity, while the last_unlocked comes from the +* delayed entity. +*/ Oh that is a good catch but clearly not the right way of fixing this.
Quoted 3 lines
We should not touch the
last_unlockedfence in any way possible while doing an immediate update to handle a page fault.My patch set already drops that handling, so we should be save for now. Please review patch "drm/amdgpu/vm: add
amdgpu_vm_pt_leaves()v4" on the mailing list instead.
Hi Christian,
I have not seen v4 of that patch on the mailing list yet. Is it still being worked on or did you forget to send it?
Best,
Ben
Quoted 7 lines
Thanks,
Christian.Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0+ r =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,2 );if (r) {pr_debug ("failed %d to reserve fence slot\n" , r); goto error_unlock;
PATCH v3 5/7 drm/amdgpu/vm: stop using amdgpu_bo_fence
In reply to Benjamin ChengSwitch to stop using this deprecated helper.
The original could be incorrect under certain circumstances, since the amdgpu_bo_fence only reserves one slot, and at the time of calling the SDMA commit and TLB flush fences were not added yet (the reserve_fences is not additive). In addition this allows us to switch to use BOOKKEEP usage.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -1
@@ -1215,7 +1215,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,if (!unlocked && !dma_fence_is_signaled (vm->last_unlocked)) {struct dma_fence *tmp =dma_fence_get_stub (); -amdgpu_bo_fence (vm->root.bo, vm->last_unlocked, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, vm->last_unlocked, +DMA_RESV_USAGE_BOOKKEEP );swap (vm->last_unlocked, tmp);dma_fence_put (tmp); }
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v3 5/7] drm/amdgpu/vm: stop using amdgpu_bo_fence
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu/vm: unreserved fence slot usage in amdgpu_vm_update_range() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu/vm: unreserved fence slot usage in amdgpu_vm_update_range()
--
commit 62a2ba953c5e8b9b18301c84fc57052868fd3e60
Author: Benjamin Cheng <[email protected]>
drm/amdgpu/vm: stop using amdgpu_bo_fence
This commit replaces the amdgpu_bo_fence() call in amdgpu_vm_update_range() with a direct dma_resv_add_fence() call using DMA_RESV_USAGE_BOOKKEEP.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1222
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-1@@ -1215,7 +1215,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,if (!unlocked && !dma_fence_is_signaled (vm->last_unlocked)) {struct dma_fence *tmp =dma_fence_get_stub (); -amdgpu_bo_fence (vm->root.bo, vm->last_unlocked, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, vm->last_unlocked, +DMA_RESV_USAGE_BOOKKEEP );swap (vm->last_unlocked, tmp);dma_fence_put (tmp); }
[Severity: High]
Can this dma_resv_add_fence() call consume an unreserved fence slot and hit BUG_ON(fobj->num_fences >= fobj->max_fences) when vm->root.bo already holds a newer fence from vm->delayed?
Callers such as amdgpu_vm_lock_pd() and amdgpu_dma_buf_move_notify() reserve only two fence slots for VM updates (one for vm->delayed and one for the TLB flush fence), relying on vm->last_unlocked and locked SDMA commits sharing the vm->delayed slot. However, dma_resv_add_fence() only replaces an unsignaled same-context slot when dma_fence_is_later_or_same(fence, old) is true.
An unlocked update on vm->delayed can leave vm->last_unlocked with a seqno older than the fence already in vm->root.bo->tbo.base.resv:
CPU0 (locked update) CPU1 (SVM MMU notifier)amdgpu_vm_update_range()
/* checks vm->last_unlocked */
amdgpu_vm_ptes_update()
amdgpu_vm_pt_alloc()
amdgpu_vm_end_critical()
svm_range_unmap_from_gpu()
amdgpu_vm_update_range(unlocked=true)
/* submits seqno N on vm->delayed */
/* vm->last_unlocked = seqno N */
amdgpu_vm_begin_critical() /* submits seqno N+1 on vm->delayed */
/* adds seqno N+1 to vm->root.bo */
amdgpu_vm_update_range()
dma_resv_add_fence(..., vm->last_unlocked, ...)
/* seqno N < seqno N+1: allocates a new slot */
BUG_ON(fobj->num_fences >= fobj->max_fences)The same ordering can occur when an unlocked update sets vm->last_unlocked before amdgpu_vm_update_pdes(), which submits on vm->delayed and adds its fence to vm->root.bo without flushing vm->last_unlocked.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v3 5/7] drm/amdgpu/vm: stop using amdgpu_bo_fence
Quoted 33 lines · On 2026-11-19 10:04, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu/vm: unreserved fence slot usage inamdgpu_vm_update_range()
--commit
62a2ba953c5e8b9b18301c84fc57052868fd3e60
Author: Benjamin Cheng <[email protected]>drm/amdgpu/vm: stop using
amdgpu_bo_fenceThis commit replaces the
amdgpu_bo_fence()call inamdgpu_vm_update_range()with a directdma_resv_add_fence()call usingDMA_RESV_USAGE_BOOKKEEP.Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1222
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-1@@ -1215,7 +1215,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,if (!unlocked && !dma_fence_is_signaled (vm->last_unlocked)) {struct dma_fence *tmp =dma_fence_get_stub (); -amdgpu_bo_fence (vm->root.bo, vm->last_unlocked, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, vm->last_unlocked, +DMA_RESV_USAGE_BOOKKEEP );swap (vm->last_unlocked, tmp);dma_fence_put (tmp); }[Severity: High]
Can thisdma_resv_add_fence()call consume an unreserved fence slot and hitBUG_ON(fobj->num_fences >= fobj->max_fences)whenvm->root.boalready holds a newer fence fromvm->delayed?
Quoted 6 lines
Callers such as
amdgpu_vm_lock_pd()andamdgpu_dma_buf_move_notify()reserve only two fence slots for VM updates (one forvm->delayedand one for the TLB flush fence), relying onvm->last_unlockedand locked SDMA commits sharing thevm->delayedslot. However,dma_resv_add_fence()only replaces an unsignaled same-context slot whendma_fence_is_later_or_same(fence, old)is true.
Hi Christian,
I think this edge-case deserves to be fixed in dma_resv_add_fence() right?
I don't see any reason we need to add another fence to the resv if it already has a fence from the same context that is later-or-same and same-or-stronger usage.
Thanks,
Ben
Quoted 27 lines
An unlocked update on
vm->delayedcan leavevm->last_unlockedwith a seqno older than the fence already invm->root.bo->tbo.base.resv:CPU0 (locked update) CPU1 (SVM MMU notifier)
amdgpu_vm_update_range()
/* checksvm->last_unlocked*/
amdgpu_vm_ptes_update()amdgpu_vm_pt_alloc() amdgpu_vm_end_critical() svm_range_unmap_from_gpu() amdgpu_vm_update_range(unlocked=true) /* submits seqno N on vm->delayed */ /* vm->last_unlocked = seqno N */ amdgpu_vm_begin_critical()/* submits seqno N+1 on
vm->delayed*/
/* adds seqno N+1 tovm->root.bo*/
amdgpu_vm_update_range()
dma_resv_add_fence(..., vm->last_unlocked, ...)/* seqno N < seqno N+1: allocates a new slot */ BUG_ON(fobj->num_fences >= fobj->max_fences)The same ordering can occur when an unlocked update sets
vm->last_unlockedbeforeamdgpu_vm_update_pdes(), which submits onvm->delayedand adds its fence tovm->root.bowithout flushingvm->last_unlocked.
Christian König
maintainerRe: [PATCH v3 5/7] drm/amdgpu/vm: stop using amdgpu_bo_fence
Quoted 41 lines · Benjamin Cheng
Quoted 33 lines · On 2026-11-19 10:04, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu/vm: unreserved fence slot usage inamdgpu_vm_update_range()
--commit
62a2ba953c5e8b9b18301c84fc57052868fd3e60
Author: Benjamin Cheng <[email protected]>drm/amdgpu/vm: stop using
amdgpu_bo_fenceThis commit replaces the
amdgpu_bo_fence()call inamdgpu_vm_update_range()with a directdma_resv_add_fence()call usingDMA_RESV_USAGE_BOOKKEEP.Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1222
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-1@@ -1215,7 +1215,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,if (!unlocked && !dma_fence_is_signaled (vm->last_unlocked)) {struct dma_fence *tmp =dma_fence_get_stub (); -amdgpu_bo_fence (vm->root.bo, vm->last_unlocked, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, vm->last_unlocked, +DMA_RESV_USAGE_BOOKKEEP );swap (vm->last_unlocked, tmp);dma_fence_put (tmp); }[Severity: High]
Can thisdma_resv_add_fence()call consume an unreserved fence slot and hitBUG_ON(fobj->num_fences >= fobj->max_fences)whenvm->root.boalready holds a newer fence fromvm->delayed?Quoted 6 lines
Callers such as
amdgpu_vm_lock_pd()andamdgpu_dma_buf_move_notify()reserve only two fence slots for VM updates (one forvm->delayedand one for the TLB flush fence), relying onvm->last_unlockedand locked SDMA commits sharing thevm->delayedslot. However,dma_resv_add_fence()only replaces an unsignaled same-context slot whendma_fence_is_later_or_same(fence, old)is true.
Quoted 3 lines
Hi Christian,
I think this edge-case deserves to be fixed in
dma_resv_add_fence()right?
That's not an issue in the first place.
Quoted 1 line
I don't see any reason we need to add another fence to the resv if it already has a fence from the same context that is later-or-same and same-or-stronger usage.
Maybe, but we shouldn't add fences out of order without allocating an extra slot. The point is rather what the AI claims as problem is nonsense.
The last_unlocked fence is either the signaled stub fence in which case it won't be added or a unlocked update which is after the last update already added and before the update we want to create now.
So all fences from the same context are still added in order, but the AI doesn't seem to understand that.
Regards,
christian.
Quoted 33 lines
Thanks,
Ben
Quoted 27 lines
An unlocked update on
vm->delayedcan leavevm->last_unlockedwith a seqno older than the fence already invm->root.bo->tbo.base.resv:CPU0 (locked update) CPU1 (SVM MMU notifier)
amdgpu_vm_update_range()
/* checksvm->last_unlocked*/
amdgpu_vm_ptes_update()amdgpu_vm_pt_alloc() amdgpu_vm_end_critical() svm_range_unmap_from_gpu() amdgpu_vm_update_range(unlocked=true) /* submits seqno N on vm->delayed */ /* vm->last_unlocked = seqno N */ amdgpu_vm_begin_critical()/* submits seqno N+1 on
vm->delayed*/
/* adds seqno N+1 tovm->root.bo*/
amdgpu_vm_update_range()
dma_resv_add_fence(..., vm->last_unlocked, ...)/* seqno N < seqno N+1: allocates a new slot */ BUG_ON(fobj->num_fences >= fobj->max_fences)The same ordering can occur when an unlocked update sets
vm->last_unlockedbeforeamdgpu_vm_update_pdes(), which submits onvm->delayedand adds its fence tovm->root.bowithout flushingvm->last_unlocked.
Re: [PATCH v3 5/7] drm/amdgpu/vm: stop using amdgpu_bo_fence
Quoted 43 lines · Christian König
Quoted 42 lines · Benjamin Cheng
Quoted 33 lines · On 2026-11-19 10:04, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu/vm: unreserved fence slot usage inamdgpu_vm_update_range()
--commit
62a2ba953c5e8b9b18301c84fc57052868fd3e60
Author: Benjamin Cheng <[email protected]>drm/amdgpu/vm: stop using
amdgpu_bo_fenceThis commit replaces the
amdgpu_bo_fence()call inamdgpu_vm_update_range()with a directdma_resv_add_fence()call usingDMA_RESV_USAGE_BOOKKEEP.Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, · line 1222
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-1@@ -1215,7 +1215,8 @@ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,if (!unlocked && !dma_fence_is_signaled (vm->last_unlocked)) {struct dma_fence *tmp =dma_fence_get_stub (); -amdgpu_bo_fence (vm->root.bo, vm->last_unlocked, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, vm->last_unlocked, +DMA_RESV_USAGE_BOOKKEEP );swap (vm->last_unlocked, tmp);dma_fence_put (tmp); }[Severity: High]
Can thisdma_resv_add_fence()call consume an unreserved fence slot and hitBUG_ON(fobj->num_fences >= fobj->max_fences)whenvm->root.boalready holds a newer fence fromvm->delayed?Quoted 6 lines
Callers such as
amdgpu_vm_lock_pd()andamdgpu_dma_buf_move_notify()reserve only two fence slots for VM updates (one forvm->delayedand one for the TLB flush fence), relying onvm->last_unlockedand locked SDMA commits sharing thevm->delayedslot. However,dma_resv_add_fence()only replaces an unsignaled same-context slot whendma_fence_is_later_or_same(fence, old)is true.Hi Christian,
Quoted 7 lines
Quoted 1 line
I think this edge-case deserves to be fixed in
dma_resv_add_fence()right?That's not an issue in the first place.
Quoted 1 line
I don't see any reason we need to add another fence to the resv if it already has a fence from the same context that is later-or-same and same-or-stronger usage.
Maybe, but we shouldn't add fences out of order without allocating an extra slot. The point is rather what the AI claims as problem is nonsense.
The
last_unlockedfence is either the signaled stub fence in which case it won't be added or a unlocked update which is after the last update already added and before the update we want to create now.
Yeah, I see that now. Thanks a lot for the explanation.
Quoted 37 lines
So all fences from the same context are still added in order, but the AI doesn't seem to understand that.
Regards,
christian.Quoted 32 lines
Thanks,
Ben
Quoted 27 lines
An unlocked update on
vm->delayedcan leavevm->last_unlockedwith a seqno older than the fence already invm->root.bo->tbo.base.resv:CPU0 (locked update) CPU1 (SVM MMU notifier)
amdgpu_vm_update_range()
/* checksvm->last_unlocked*/
amdgpu_vm_ptes_update()amdgpu_vm_pt_alloc() amdgpu_vm_end_critical() svm_range_unmap_from_gpu() amdgpu_vm_update_range(unlocked=true) /* submits seqno N on vm->delayed */ /* vm->last_unlocked = seqno N */ amdgpu_vm_begin_critical()/* submits seqno N+1 on
vm->delayed*/
/* adds seqno N+1 tovm->root.bo*/
amdgpu_vm_update_range()
dma_resv_add_fence(..., vm->last_unlocked, ...)/* seqno N < seqno N+1: allocates a new slot */ BUG_ON(fobj->num_fences >= fobj->max_fences)The same ordering can occur when an unlocked update sets
vm->last_unlockedbeforeamdgpu_vm_update_pdes(), which submits onvm->delayedand adds its fence tovm->root.bowithout flushingvm->last_unlocked.
PATCH v3 6/7 drm/amdgpu/uvd: stop using amdgpu_bo_fence
In reply to Benjamin ChengThis helper is deprecated since it is incorrect in most circumstances. This was a circumstance where it was mostly correct but it is the only remaining usage, so get rid of it here.
---
v3:
- Moved the reserve_fences call above submit (Christian)
--- drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +17 -3
@@ -1154,16 +1154,27 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,if (r) goto err_free; }else { + r =amdgpu_bo_reserve (bo, true); +if (r) + goto err_free; + r =drm_sched_job_add_resv_dependencies (&job->base, bo->tbo.base.resv,DMA_RESV_USAGE_KERNEL );if (r) - goto err_free; + goto err_unreserve; + + r =dma_resv_reserve_fences (bo->tbo.base.resv,1 ); f =amdgpu_job_submit (job); -amdgpu_bo_reserve (bo, true); -amdgpu_bo_fence (bo, f, false); +if (r) { +dma_fence_wait (f, false); + r =0 ; + }else { +dma_resv_add_fence (bo->tbo.base.resv, f, +DMA_RESV_USAGE_BOOKKEEP ); + }amdgpu_bo_unreserve (bo); }
@@ -1173,6 +1184,9 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,return 0 ; +err_unreserve: +amdgpu_bo_unreserve (bo); + err_free:amdgpu_job_free (job);return r;
Signature · 1 line
2.56.1
Re: [PATCH v3 6/7] drm/amdgpu/uvd: stop using amdgpu_bo_fence
Quoted 5 lines · Benjamin Cheng
This helper is deprecated since it is incorrect in most circumstances. This was a circumstance where it was mostly correct but it is the only remaining usage, so get rid of it here.
Signed-off-by Benjamin Cheng <[email protected]>
Quoted 52 lines
---
v3:
- Moved thereserve_fencescall above submit (Christian)--- drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +17
-3@@ -1154,16 +1154,27 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,if (r) goto err_free; }else { + r =amdgpu_bo_reserve (bo, true); +if (r) + goto err_free; + r =drm_sched_job_add_resv_dependencies (&job->base, bo->tbo.base.resv,DMA_RESV_USAGE_KERNEL );if (r) - goto err_free; + goto err_unreserve; + + r =dma_resv_reserve_fences (bo->tbo.base.resv,1 ); f =amdgpu_job_submit (job); -amdgpu_bo_reserve (bo, true); -amdgpu_bo_fence (bo, f, false); +if (r) { +dma_fence_wait (f, false); + r =0 ; + }else { +dma_resv_add_fence (bo->tbo.base.resv, f, +DMA_RESV_USAGE_BOOKKEEP ); + }amdgpu_bo_unreserve (bo); }@@ -1173,6 +1184,9 @@ static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,return 0 ; +err_unreserve: +amdgpu_bo_unreserve (bo); + err_free:amdgpu_job_free (job);return r;
PATCH v3 7/7 drm/amdgpu/object: get rid of amdgpu_bo_fence
In reply to Benjamin ChengAll uses of this function have been removed, so we can now get rid of this function.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 25 ---------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 2 -- 2 files changed, 27 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -25
@@ -1463,31 +1463,6 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)return 0 ; } -/** -* amdgpu_bo_fence - add fence to buffer object -* -* @bo: buffer object in question -* @fence: fence to add -* @shared: true if fence should be added shared -* -*/ -void amdgpu_bo_fence (struct amdgpu_bo *bo,struct dma_fence *fence, -bool shared) -{ -struct dma_resv *resv = bo->tbo.base.resv; -int r; - - r =dma_resv_reserve_fences (resv,1 ); -if (r) { -/* As last resort on OOM we block for the fence */ -dma_fence_wait (fence, false); -return ; - } - -dma_resv_add_fence (resv, fence, shared ?DMA_RESV_USAGE_READ : -DMA_RESV_USAGE_WRITE ); -} -/** * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences *
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -2
@@ -305,8 +305,6 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, struct ttm_resource *new_mem);void amdgpu_bo_release_notify (struct ttm_buffer_object *bo);vm_fault_t amdgpu_bo_fault_reserve_notify (struct ttm_buffer_object *bo); -void amdgpu_bo_fence (struct amdgpu_bo *bo,struct dma_fence *fence, -bool shared);int amdgpu_bo_sync_wait_resv (struct amdgpu_device *adev,struct dma_resv *resv,enum amdgpu_sync_mode sync_mode,void *owner,bool intr);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v3 7/7] drm/amdgpu/object: get rid of amdgpu_bo_fence
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: build failure in amdgpu_gem_object_close() after removing amdgpu_bo_fence() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: build failure in amdgpu_gem_object_close() after removing amdgpu_bo_fence()
--
commit 0712370b9809b23c4190422dc542ee1195e75ba2
Author: Benjamin Cheng <[email protected]>
drm/amdgpu/object: get rid of amdgpu_bo_fence
This commit removes the amdgpu_bo_fence() helper and its declaration.
Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-18@@ -1441,31 +1441,6 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)return 0 ; } -/** -* amdgpu_bo_fence - add fence to buffer object -* -* @bo: buffer object in question -* @fence: fence to add -* @shared: true if fence should be added shared -* -*/ -void amdgpu_bo_fence (struct amdgpu_bo *bo,struct dma_fence *fence, -bool shared) -{ -struct dma_resv *resv = bo->tbo.base.resv; -int r; - - r =dma_resv_reserve_fences (resv,1 ); -if (r) { -/* As last resort on OOM we block for the fence */ -dma_fence_wait (fence, false);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo) · line 1446
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0
-7-return ; - } - -dma_resv_add_fence (resv, fence, shared ?DMA_RESV_USAGE_READ : -DMA_RESV_USAGE_WRITE ); -} -/** * amdgpu_bo_sync_wait_resv - Wait for BO reservation fences *
[Severity: High]
Does removing amdgpu_bo_fence() break the build in amdgpu_gem_object_close()?
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:amdgpu_gem_object_close() {
...
r = amdgpu_vm_clear_freed(adev, vm, &fence);
if (unlikely(r < 0) && !drm_dev_is_unplugged(adev_to_drm(adev)))
dev_err(adev->dev, "failed to clear page "
"tables on GEM object close (%ld)\n", r);
if (r || !fence)
goto out_unlock;
amdgpu_bo_fence(bo, fence, true);
dma_fence_put(fence);
...
}amdgpu_gem_object_close() still calls amdgpu_bo_fence(), so compiling amdgpu_gem.c fails with an implicit declaration error.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7