drm/amdgpu: Rework VM update ops for batching and explicit sync
Subsystems (4)
v2 cover drm/amdgpu: Rework VM update ops for batching and explicit sync
Hi,
this is v2 of my attempt at improving sparse binding perf.
The motivator is that many games have come to heavily use sparse bindings, and they often rely on being able to overlap frame work on gfx/compute queues with sparse binding work on sdma. If PRT mappings are synced to every other submissions like they are right now, heavy sparse binding load can stall the gfx/compute submissions for quite a long while, leading to very noticeable stutters and hitches.
Another problem is that each sparse binding update requires a separate ioctl, so the CPU will also spend significant time constantly switching between user and kernel mode to execute the binding changes one at a time.
To remedy this, this patchset introduces a "VM update context" structure. This context encapsulates some state that is carried around different VM update functions, in patch 1 this is the mappings that were freed along the way. amdgpu_vm_clear_freed then only clears the mappings that were actually freed in the update operation, which is a requirement for being able to explicitly sync that unmap.
In later patches, the update context is expanded to hold a set of pending mapping changes, so that a future ioctl that changes many mappings at a time has a place to keep pending changes while every change is validated, before applying everything atomically if things look okay.
Best,
Natalie
---
Changes in v2:
- Added pending change structures to update context in order to batch changes
- Link to v1: https://patch.msgid.link/[email protected]
---
Natalie Vock (10):
drm/amdgpu: Remove unused parameter from amdgpu_vm_pte_fragment drm/amdgpu: Add and use vm_update_ctx struct
drm/amdgpu: Make VM maps pending per update context drm/amdgpu: Make removed/cleared mappings pending per update context drm/amdgpu: Track dirty bo_va structures in update ctx drm/amdgpu: Add function to roll back a VM update ctx drm/amdgpu: Move some VA op validation to a helper drm/amdgpu: Only reject conflicting mappings if they are not cleared drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl
drm/amdgpu: Bump minor version
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 33 +- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 31 +- drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c | 21 +- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 6 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 477 ++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 3 + drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c | 9 +- drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 10 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 660 +++++++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 100 +++- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 8 +- include/uapi/drm/amdgpu_drm.h | 46 ++ 15 files changed, 1099 insertions(+), 311 deletions(-)
---
base-commit: 65b7b0d2cf8c3cba99d599493ac96f3ab32c0a06
change-id: 20260529-prt-explicit-sync-3d74b9dd420c
Best regards,
Signature · 1 line
Natalie Vock <[email protected]>
PATCH v2 1/10 drm/amdgpu: Remove unused parameter from amdgpu_vm_pte_fragment
Trivial cleanup. No functional change.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +3 -5
@@ -745,14 +745,13 @@ static void amdgpu_vm_pte_update_flags(struct amdgpu_vm_update_params *params,* @params: see amdgpu_vm_update_params definition * @start: first PTE to handle * @end: last PTE to handle -* @flags: hw mapping flags * @frag: resulting fragment size * @frag_end: end of this fragment * * Returns the first possible fragment for the start and end address. */ static void amdgpu_vm_pte_fragment (struct amdgpu_vm_update_params *params, -uint64_t start,uint64_t end,uint64_t flags, +uint64_t start,uint64_t end,unsigned int *frag,uint64_t *frag_end) {/**
@@ -825,8 +824,7 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,int r;/* figure out the initial fragment */ -amdgpu_vm_pte_fragment (params, frag_start, end, flags, &frag, - &frag_end); +amdgpu_vm_pte_fragment (params, frag_start, end, &frag, &frag_end);/* walk over the address space and update the PTs */ amdgpu_vm_pt_start (adev, params->vm, start, &cursor);
@@ -932,7 +930,7 @@ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,if (frag_start >= frag_end) {/* figure out the next fragment */ amdgpu_vm_pte_fragment (params, frag_start, end, - flags, &frag, &frag_end); + &frag, &frag_end);if (frag < shift)break ; }
Signature · 1 line
2.56.1
PATCH v2 2/10 drm/amdgpu: Add and use vm_update_ctx struct
In reply to Natalie VockThis adds a "context" struct to the functions responsible for mapping page tables (and committing the maps to the GPU page-tables).
The core idea here is to record the changes associated with specific VM mapping operations (most importantly, which mappings were freed by each map change). We need this to properly implement explicit sync for unmap operations, which can only be performed for unmaps of either always-valid or PRT mappings.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 30 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 31 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c | 18 +- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 53 ++--- drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 10 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 248 +++++++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 59 +++++- 10 files changed, 317 insertions(+), 144 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +24 -6
@@ -1286,22 +1286,27 @@ static int unmap_bo_from_gpuvm(struct kgd_mem *mem,struct amdgpu_bo_va *bo_va = entry->bo_va;struct amdgpu_device *adev = entry->adev;struct amdgpu_vm *vm = bo_va->base.vm; +struct amdgpu_vm_update_ctx update_ctx;if (bo_va->queue_refcount) {pr_debug ("bo_va->queue_refcount %d\n" , bo_va->queue_refcount);return -EBUSY ; } - (void )amdgpu_vm_bo_unmap (adev, bo_va, entry->va); +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + + (void )amdgpu_vm_bo_unmap (&update_ctx, bo_va, entry->va);/* VM entity stopped if process killed, don't clear freed pt bo */ if (!amdgpu_vm_ready (vm)) -return 0 ; + goto out; - (void )amdgpu_vm_clear_freed (adev, vm, &bo_va->last_pt_update); + (void )amdgpu_vm_clear_freed (&update_ctx, &bo_va->last_pt_update); (void )amdgpu_sync_fence (sync, bo_va->last_pt_update,GFP_KERNEL ); +out: +amdgpu_vm_update_ctx_fini (&update_ctx);return 0 ; }
@@ -1311,6 +1316,7 @@ static int update_gpuvm_pte(struct kgd_mem *mem, {struct amdgpu_bo_va *bo_va = entry->bo_va;struct amdgpu_device *adev = entry->adev; +struct amdgpu_vm_update_ctx update_ctx;int ret; ret =kfd_mem_dmamap_attachment (mem, entry);
@@ -1318,7 +1324,10 @@ static int update_gpuvm_pte(struct kgd_mem *mem,return ret;/* Update the page tables */ - ret =amdgpu_vm_bo_update (adev, bo_va, false); +amdgpu_vm_update_ctx_init (&update_ctx, adev, bo_va->base.vm); + ret =amdgpu_vm_bo_update (&update_ctx, bo_va, false); +amdgpu_vm_update_ctx_fini (&update_ctx); +if (ret) {pr_err ("amdgpu_vm_bo_update failed\n" );return ret;
@@ -1332,12 +1341,17 @@ static int map_bo_to_gpuvm(struct kgd_mem *mem, struct amdgpu_sync *sync, bool no_update_pte) { +struct amdgpu_bo_va *bo_va = entry->bo_va; +struct amdgpu_device *adev = entry->adev; +struct amdgpu_vm_update_ctx update_ctx;int ret; +amdgpu_vm_update_ctx_init (&update_ctx, adev, bo_va->base.vm);/* Set virtual address for the allocation */ - ret =amdgpu_vm_bo_map (entry->adev, entry->bo_va, entry->va,0 , + ret =amdgpu_vm_bo_map (&update_ctx, bo_va, entry->va,0 ,amdgpu_bo_size (entry->bo_va->base.bo), entry->pte_flags); +amdgpu_vm_update_ctx_fini (&update_ctx);if (ret) {pr_err ("Failed to map VA 0x%llx in vm. ret %d\n" , entry->va, ret);
@@ -2982,6 +2996,7 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *struct amdgpu_sync sync_obj;unsigned long failed_size =0 ;unsigned long total_size =0 ; +struct amdgpu_vm_update_ctx update_ctx;struct drm_exec exec;int ret;
@@ -3091,7 +3106,10 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence __rcu *struct amdgpu_fpriv *fpriv = container_of(peer_vm, struct amdgpu_fpriv, vm); - ret =amdgpu_vm_bo_update (adev, fpriv->prt_va, false); +amdgpu_vm_update_ctx_init (&update_ctx, adev, peer_vm); + ret =amdgpu_vm_bo_update (&update_ctx, fpriv->prt_va, false); +amdgpu_vm_update_ctx_fini (&update_ctx); +if (ret) { dev_dbg(adev->dev, "Memory eviction: handle PRT moved failed, pid %8d. Try again.\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +20 -11
@@ -1126,6 +1126,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct amdgpu_bo_va *bo_va; +struct amdgpu_vm_update_ctx update_ctx;unsigned int i;int r;
@@ -1147,34 +1148,36 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (!amdgpu_vm_ready (vm))return -EINVAL ; - r =amdgpu_vm_clear_freed (adev, vm,NULL ); + r =amdgpu_vm_delayed_free (adev, vm,NULL );if (r)return r; - r =amdgpu_vm_bo_update (adev, fpriv->prt_va, false); +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + + r =amdgpu_vm_bo_update (&update_ctx, fpriv->prt_va, false);if (r) -return r; + goto err_fini_ctx; r =amdgpu_sync_fence (&p->sync, fpriv->prt_va->last_pt_update,GFP_KERNEL );if (r) -return r; + goto err_fini_ctx;if (fpriv->csa_va) { bo_va = fpriv->csa_va; - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false);if (r) -return r; + goto err_fini_ctx; r =amdgpu_sync_fence (&p->sync, bo_va->last_pt_update,GFP_KERNEL );if (r) -return r; + goto err_fini_ctx; }if (fpriv->seq64_va) { bo_va = fpriv->seq64_va; - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false);if (r) {DRM_ERROR ("failed to do vm_bo_update on userq sem\n" );return r;
@@ -1196,16 +1199,18 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (bo_va ==NULL )continue ; - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false);if (r) -return r; + goto err_fini_ctx; r =amdgpu_sync_fence (&p->sync, bo_va->last_pt_update,GFP_KERNEL );if (r) -return r; + goto err_fini_ctx; } +amdgpu_vm_update_ctx_fini (&update_ctx); + r =amdgpu_vm_handle_moved (adev, vm,drm_exec_ticket (&p->exec));if (r)return r;
@@ -1228,6 +1233,10 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p) }return 0 ; + +err_fini_ctx: +amdgpu_vm_update_ctx_fini (&update_ctx); +return r; }static int amdgpu_cs_sync_rings (struct amdgpu_cs_parser *p)
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c +14 -4
@@ -66,6 +66,7 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, struct amdgpu_bo *bo, struct amdgpu_bo_va **bo_va, uint64_t csa_addr, uint32_t size) { + struct amdgpu_vm_update_ctx update_ctx;struct drm_exec exec;int r;
@@ -88,7 +89,9 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, goto error; } - r =amdgpu_vm_bo_map (adev, *bo_va, csa_addr,0 , size, +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + + r =amdgpu_vm_bo_map (&update_ctx, *bo_va, csa_addr,0 , size,AMDGPU_PTE_READABLE |AMDGPU_PTE_WRITEABLE |AMDGPU_PTE_EXECUTABLE );
@@ -96,9 +99,11 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,drm_err (adev_to_drm (adev),"failed to do bo_map on static CSA, err=%d\n" , r);amdgpu_vm_bo_del (adev, *bo_va); - goto error; + goto error_ctx; } +error_ctx: +amdgpu_vm_update_ctx_fini (&update_ctx); error:drm_exec_fini (&exec);return r;
@@ -108,6 +113,7 @@ int amdgpu_unmap_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, struct amdgpu_bo *bo, struct amdgpu_bo_va *bo_va, uint64_t csa_addr) { + struct amdgpu_vm_update_ctx update_ctx;struct drm_exec exec;int r;
@@ -124,15 +130,19 @@ int amdgpu_unmap_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, } } - r =amdgpu_vm_bo_unmap (adev, bo_va, csa_addr); +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + + r =amdgpu_vm_bo_unmap (&update_ctx, bo_va, csa_addr);if (r) {drm_err (adev_to_drm (adev),"failed to do bo_unmap on static CSA, err=%d\n" , r); - goto error; + goto error_ctx; }amdgpu_vm_bo_del (adev, bo_va); +error_ctx: +amdgpu_vm_update_ctx_fini (&update_ctx); error:drm_exec_fini (&exec);return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
@@ -564,7 +564,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 );if (!r) - r =amdgpu_vm_clear_freed (adev, vm,NULL ); + r =amdgpu_vm_delayed_free (adev, vm,NULL );/* Don't pass 'ticket' to amdgpu_vm_handle_moved: we want the clear=true * path to be used otherwise we might update the PT of another process
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +29 -24
170 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c +6 -2
@@ -67,6 +67,7 @@ static inline u64 amdgpu_seq64_get_va_base(struct amdgpu_device *adev)int amdgpu_seq64_map (struct amdgpu_device *adev,struct amdgpu_vm *vm,struct amdgpu_bo_va **bo_va) { +struct amdgpu_vm_update_ctx update_ctx;struct amdgpu_bo *bo;struct drm_exec exec;u64 seq64_addr;
@@ -94,15 +95,18 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm, seq64_addr =amdgpu_seq64_get_va_base (adev) &AMDGPU_GMC_HOLE_MASK ; - r =amdgpu_vm_bo_map (adev, *bo_va, seq64_addr,0 , +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + r =amdgpu_vm_bo_map (&update_ctx, *bo_va, seq64_addr,0 ,AMDGPU_VA_RESERVED_SEQ64_SIZE ,AMDGPU_VM_PAGE_READABLE |AMDGPU_VM_MTYPE_UC );if (r) {DRM_ERROR ("failed to do bo_map on userq sem, err=%d\n" , r);amdgpu_vm_bo_del (adev, *bo_va); - goto error; + goto err_ctx; } +err_ctx: +amdgpu_vm_update_ctx_fini (&update_ctx); error:drm_exec_fini (&exec);return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -1
@@ -3077,7 +3077,7 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo)* reserved by amdgpu_vm_lock_pd() above (it reserves 2 + * num_fences on the same VM root PD dma_resv). */ - r =amdgpu_vm_clear_freed (adev, vm,NULL ); + r =amdgpu_vm_delayed_free (adev, vm,NULL );if (!r) r =amdgpu_vm_handle_moved (adev, vm, &exec.ticket);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -2
@@ -994,6 +994,7 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec, struct amdgpu_vm *vm) {struct ttm_operation_ctx ctx = { false, false }; +struct amdgpu_vm_update_ctx update_ctx;struct amdgpu_bo_va *bo_va;struct amdgpu_bo *bo, *tmp;int ret;
@@ -1018,8 +1019,10 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,if (ret)return ret; +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm);/* This moves the bo_va to the idle list */ - ret =amdgpu_vm_bo_update (adev, bo_va, false); + ret =amdgpu_vm_bo_update (&update_ctx, bo_va, false); +amdgpu_vm_update_ctx_fini (&update_ctx);if (ret)return ret;
@@ -1038,6 +1041,7 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)bool invalidated = false, new_addition = false;struct ttm_operation_ctx ctx = { true, false };struct amdgpu_device *adev = uq_mgr->adev; +struct amdgpu_vm_update_ctx update_ctx;struct amdgpu_hmm_range *range;struct amdgpu_vm *vm = &fpriv->vm;unsigned long key, tmp_key;
@@ -1140,7 +1144,9 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)* explicitly here (as the CS path does) so sparse mappings survive a * VRAM-lost reset. */ - ret =amdgpu_vm_bo_update (adev, fpriv->prt_va, false); +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + ret =amdgpu_vm_bo_update (&update_ctx, fpriv->prt_va, false); +amdgpu_vm_update_ctx_fini (&update_ctx);if (ret) goto unlock_all;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +164 -84
550 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +50 -9
105 lines not on this page · raw message
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 02/10] drm/amdgpu: Add and use vm_update_ctx struct
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] drm/amdgpu: fence and sync leaks in amdgpu_gem_add_input_fence() error paths - [High] drm/amdgpu…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] drm/amdgpu: fence and sync leaks in amdgpu_gem_add_input_fence() error paths
- [High] drm/amdgpu: stale vm->delayed_freed entries overwrite new PTEs in amdgpu_gem_va_update_vm()
- [Medium] drm/amdgpu: swallowed error and sync leak in amdgpu_vm_handle_moved()
- [Medium] drm/amdgpu: update_ctx.sync leak on seq64_va error in amdgpu_cs_vm_handling()
- [Medium] drm/amdgpu: input fences dropped in amdgpu_gem_va_ioctl() without PT range update
- [Medium] drm/amdgpu: cross-BO fence accumulation in amdgpu_vm_bo_update()
- [Low] drm/amdgpu: outdated kernel-doc comments for amdgpu_vm_bo_update() and vm_update_ctx
--
commit 613a377be8b434a7253a6f919cefd9f59fe0c3de
Author: Natalie Vock <[email protected]>
drm/amdgpu: Add and use vm_update_ctx struct
This commit introduces struct amdgpu_vm_update_ctx to track freed mappings and synchronization state across VM mapping and page table update operations, and moves deferred unmaps onto vm->delayed_freed.
Quoted 57 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +11
-8@@ -1126,6 +1126,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct amdgpu_bo_va *bo_va; +struct amdgpu_vm_update_ctx update_ctx;unsigned int i;int r;@@ -1147,34 +1148,36 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (!amdgpu_vm_ready (vm))return -EINVAL ; - r =amdgpu_vm_clear_freed (adev, vm,NULL ); + r =amdgpu_vm_delayed_free (adev, vm,NULL );if (r)return r; - r =amdgpu_vm_bo_update (adev, fpriv->prt_va, false); +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); + + r =amdgpu_vm_bo_update (&update_ctx, fpriv->prt_va, false);if (r) -return r; + goto err_fini_ctx; r =amdgpu_sync_fence (&p->sync, fpriv->prt_va->last_pt_update,GFP_KERNEL );if (r) -return r; + goto err_fini_ctx;if (fpriv->csa_va) { bo_va = fpriv->csa_va; - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false);if (r) -return r; + goto err_fini_ctx; r =amdgpu_sync_fence (&p->sync, bo_va->last_pt_update,GFP_KERNEL );if (r) -return r; + goto err_fini_ctx; }if (fpriv->seq64_va) { bo_va = fpriv->seq64_va; - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false);if (r) {DRM_ERROR ("failed to do vm_bo_update on userq sem\n" );return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0} r =amdgpu_sync_fence (&p->sync, bo_va->last_pt_update,GFP_KERNEL );if (r)return r; }
[Severity: Medium]
Do the two error returns in the fpriv->seq64_va block leak update_ctx.sync? Both paths return r directly instead of jumping to err_fini_ctx to call amdgpu_vm_update_ctx_fini(&update_ctx), leaking any amdgpu_sync_entry allocations and dma_fence references added during the earlier csa_va or seq64_va updates.
[ ... ]
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-0@@ -51,6 +51,7 @@static int amdgpu_gem_add_input_fence (struct drm_file *filp, +struct amdgpu_vm_update_ctx *update_ctx,uint64_t syncobj_handles_array,uint32_t num_syncobj_handles) {@@ -77,9 +78,10 @@ amdgpu_gem_add_input_fence(struct drm_file *filp,if (ret)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_add_input_fence(struct drm_file *filp, · line 86
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3
-2goto free_memdup; -dma_fence_wait (fence, false); + ret =amdgpu_sync_fence (&update_ctx->sync, fence,GFP_KERNEL ); +if (ret) + goto free_memdup; -/* TODO: optimize async handling */ dma_fence_put (fence); }
[Severity: High]
Does jumping to free_memdup when amdgpu_sync_fence() fails leak the fence reference acquired by drm_syncobj_find_fence()?
[ ... ]
Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3
-5@@ -808,8 +811,7 @@ int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,* otherwise stub fence in case of error. */ static struct dma_fence * -amdgpu_gem_va_update_vm (struct amdgpu_device *adev, -struct amdgpu_vm *vm, +amdgpu_gem_va_update_vm (struct amdgpu_vm_update_ctx *ctx,struct amdgpu_bo_va *bo_va,uint32_t operation) {@@ -817,10 +819,9 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,int r =0 ;/* If the VM is not ready return only a stub. */ -if (!amdgpu_vm_ready (vm)) +if (!amdgpu_vm_ready (ctx->vm))return dma_fence_get_stub (); -/* * First clean up any freed mappings in the VM. * @@ -828,20 +829,20 @@ amdgpu_gem_va_update_vm(struct amdgpu_device *adev,* schedules GPU work. If nothing needs clearing, @fence can remain as * the original vm->last_update. */ - r =amdgpu_vm_clear_freed (adev, vm, &vm->last_update); + r =amdgpu_vm_clear_freed (ctx, &ctx->vm->last_update);if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_va_update_vm(struct amdgpu_device *adev, · line 842
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-1goto error;/* For MAP/REPLACE we also need to update the BO mappings. */ if (operation ==AMDGPU_VA_OP_MAP || operation ==AMDGPU_VA_OP_REPLACE ) { - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (ctx, bo_va, false);if (r) goto error; }
[Severity: High]
Can stale entries on ctx->vm->delayed_freed overwrite newly mapped PTEs?
When an unmap is deferred (for example via AMDGPU_VM_DELAY_UPDATE), amdgpu_vm_bo_unmap() removes the mapping from vm->va and amdgpu_vm_update_ctx_fini() splices it onto vm->delayed_freed. If userspace then maps a BO at the same VA without AMDGPU_VM_DELAY_UPDATE, amdgpu_gem_va_update_vm() only clears ctx->freed from the current ioctl and writes the new BO's valid PTEs while the old unmap remains on vm->delayed_freed. A later amdgpu_vm_delayed_free() in amdgpu_cs_vm_handling() or amdgpu_gem_object_close() will then zero those active PTEs and cause GPU page faults.
In addition, amdgpu_userq_vm_validate_and_restore_queue() and KFD VM updates never call amdgpu_vm_delayed_free(), leaving mappings on vm->delayed_freed uncleared in those paths.
[ ... ]
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3
-1@@ -967,8 +969,10 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,if (r) goto error_put_gobj; +amdgpu_vm_update_ctx_init (&update_ctx, adev, &fpriv->vm); +/* Add input syncobj fences (if any) for synchronization. */ - r =amdgpu_gem_add_input_fence (filp, + r =amdgpu_gem_add_input_fence (filp, &update_ctx, args->input_fence_syncobj_handles, args->num_syncobj_handles);if (r)goto
error_put_gobj;
[ ... ]
Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-1@@ -1049,7 +1053,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, (!adev->debug_vm || timeline_syncobj)) {struct dma_fence *fence; - fence =amdgpu_gem_va_update_vm (adev, &fpriv->vm, bo_va, + fence =amdgpu_gem_va_update_vm (&update_ctx, bo_va, args->operation);if (timeline_syncobj) {if (!args->vm_timeline_point) {@@ -1070,6 +1074,7 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, } error: +amdgpu_vm_update_ctx_fini (&update_ctx);dma_fence_chain_free (timeline_chain);if (timeline_syncobj)drm_syncobj_put (timeline_syncobj);
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0
-0drm_exec_fini (&exec); error_put_gobj:drm_gem_object_put (gobj);return r; }
[Severity: High]
Does jumping to error_put_gobj when amdgpu_gem_add_input_fence() fails leak update_ctx.sync? If amdgpu_gem_add_input_fence() adds a fence to update_ctx.sync on the first handle and then fails on a later handle, error_put_gobj skips amdgpu_vm_update_ctx_fini(&update_ctx) and leaks the accumulated amdgpu_sync_entry allocations and dma_fence references.
[Severity: Medium]
Are input fences in update_ctx.sync silently dropped when amdgpu_vm_update_range() does not run?
Previously, amdgpu_gem_add_input_fence() waited on each input fence with dma_fence_wait(). Now the fences are queued in update_ctx.sync, which is only consumed by amdgpu_vm_update_range(). When AMDGPU_VM_DELAY_UPDATE is set, or when an operation such as AMDGPU_VA_OP_CLEAR on an unmapped range does not emit a PT range update, amdgpu_vm_update_ctx_fini() frees update_ctx.sync without waiting on the input fences or chaining them into timeline_syncobj.
Quoted 50 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-13@@ -1319,37 +1319,31 @@ void amdgpu_vm_get_memory(struct amdgpu_vm *vm,* Returns: * 0 for success, -EINVAL for failure. */ -int amdgpu_vm_bo_update (struct amdgpu_device *adev,struct amdgpu_bo_va *bo_va, -bool clear) +int amdgpu_vm_bo_update (struct amdgpu_vm_update_ctx *ctx, +struct amdgpu_bo_va *bo_va,bool clear) {struct amdgpu_bo *bo = bo_va->base.bo; -struct amdgpu_vm *vm = bo_va->base.vm; +struct amdgpu_vm *vm = ctx->vm;struct amdgpu_bo_va_mapping *mapping;struct dma_fence **last_update;dma_addr_t *pages_addr =NULL ;struct ttm_resource *mem; -struct amdgpu_sync sync;bool flush_tlb = clear;uint64_t vram_base;uint64_t flags;bool uncached;int r; -amdgpu_sync_create (&sync);if (clear) { mem =NULL ; -/* Implicitly sync to command submissions in the same VM before -* unmapping. -*/ - r =amdgpu_sync_resv (adev, &sync, vm->root.bo->tbo.base.resv, -AMDGPU_SYNC_EQ_OWNER , vm); + r =amdgpu_vm_update_ctx_ensure_unmap_synced (ctx);if (r) - goto error_free; + goto error;if (bo) { - r =amdgpu_sync_kfd (&sync, bo->tbo.base.resv); + r =amdgpu_sync_kfd (&ctx->sync, bo->tbo.base.resv);if (r) - goto error_free; + goto error; } }else if (!bo) { mem =NULL ;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, · line 1375
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-2@@ -1374,16 +1368,16 @@ int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, pages_addr = bo->tbo.ttm->dma_address;/* Implicitly sync to moving fences before mapping anything */ - r =amdgpu_sync_resv (adev, &sync, bo->tbo.base.resv, + r =amdgpu_sync_resv (ctx->adev, &ctx->sync, bo->tbo.base.resv,AMDGPU_SYNC_EXPLICIT , vm);if (r) - goto error_free; + goto error; }
[Severity: Medium]
Does sharing ctx->sync across multiple amdgpu_vm_bo_update() calls cause later BO updates to wait on unrelated fences?
amdgpu_vm_bo_update() adds per-BO moving fences, VM-wide AMDGPU_SYNC_EQ_OWNER fences, and KFD eviction fences to ctx->sync before checking whether bo_va->invalids is empty, and never clears per-BO fences between calls. When amdgpu_cs_vm_handling() or amdgpu_vm_handle_moved() reuses one update_ctx across a loop of BOs, every subsequent PT update waits on all earlier BOs' moving fences (even for BOs whose bo_va->invalids was empty), and an earlier clear=true BO forces later clear=false updates in amdgpu_vm_handle_moved() to wait on prior VM submissions and enable signaling on KFD eviction fences.
[ ... ]
Quoted 38 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-3@@ -1679,18 +1748,22 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) { + struct amdgpu_vm_update_ctx update_ctx;struct amdgpu_bo_va *bo_va, *tmp;struct dma_resv *resv;struct amdgpu_bo *bo;bool clear, unlock;int r; +amdgpu_vm_update_ctx_init (&update_ctx, adev, vm); +list_for_each_entry_safe (bo_va, tmp, &vm->always_valid.needs_update, base.vm_status) {/* Per VM BOs never need to bo cleared in the page tables */ - r =amdgpu_vm_bo_update (adev, bo_va, false); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false); +if (r) -return r; + goto error; }spin_lock (&vm->individual_lock);@@ -1716,7 +1789,7 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev, unlock = false; } - r =amdgpu_vm_bo_update (adev, bo_va, clear); + r =amdgpu_vm_bo_update (&update_ctx, bo_va, clear);if (unlock)dma_resv_unlock (resv);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0if (r)return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_handle_moved(struct amdgpu_device *adev, · line 1816
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-0@@ -1736,6 +1809,9 @@ int amdgpu_vm_handle_moved(struct amdgpu_device *adev, }spin_unlock (&vm->individual_lock); +error: +amdgpu_vm_update_ctx_fini (&update_ctx); +return 0 ; }
[Severity: Medium]
Does amdgpu_vm_handle_moved() swallow errors from the always_valid loop and leak update_ctx.sync on errors in the individual loop?
When amdgpu_vm_bo_update() fails on an entry in vm->always_valid.needs_update, jumping to error returns 0 instead of r, so callers such as amdgpu_cs_vm_handling() proceed with stale page tables. Conversely, when amdgpu_vm_bo_update() fails in the vm->individual.needs_update loop, returning r directly skips amdgpu_vm_update_ctx_fini(&update_ctx) and leaks update_ctx.sync.
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_task_info { · line 271
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5
-0@@ -264,6 +264,39 @@ struct amdgpu_task_info {struct kref refcount; }; +/** +* struct amdgpu_vm_update_ctx +* +* Encapsulates various state that is carried around during VM updates. +*/
[Severity: Low]
This isn't a bug, but scripts/kernel-doc expects a short description on the struct amdgpu_vm_update_ctx line, and the kernel-doc comments on amdgpu_vm_bo_update() and amdgpu_gem_va_update_vm() still list @adev and @vm instead of @ctx.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/10 drm/amdgpu: Make VM maps pending per update context
In reply to Natalie VockFor later being able to discard pending changes when an error is encountered afterwards.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 124 +++++++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 11 ++ 7 files changed, 113 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -1
@@ -1351,13 +1351,16 @@ static int map_bo_to_gpuvm(struct kgd_mem *mem, ret =amdgpu_vm_bo_map (&update_ctx, bo_va, entry->va,0 ,amdgpu_bo_size (entry->bo_va->base.bo), entry->pte_flags); -amdgpu_vm_update_ctx_fini (&update_ctx);if (ret) {pr_err ("Failed to map VA 0x%llx in vm. ret %d\n" , entry->va, ret); +amdgpu_vm_update_ctx_fini (&update_ctx);return ret; } +amdgpu_vm_update_ctx_commit (&update_ctx); +amdgpu_vm_update_ctx_fini (&update_ctx); +if (no_update_pte)return 0 ;
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c +2 -0
@@ -22,6 +22,7 @@* * Author: [email protected] */ +#include "amdgpu_vm.h" #include <drm/drm_exec.h> #include "amdgpu.h"
@@ -94,6 +95,7 @@ int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, r =amdgpu_vm_bo_map (&update_ctx, *bo_va, csa_addr,0 , size,AMDGPU_PTE_READABLE |AMDGPU_PTE_WRITEABLE |AMDGPU_PTE_EXECUTABLE ); +amdgpu_vm_update_ctx_commit (&update_ctx);if (r) { drm_err(adev_to_drm(adev),
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -0
@@ -1044,6 +1044,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,break ; } +amdgpu_vm_update_ctx_commit (&update_ctx); +/* * Once the VA operation is done, update the VM and obtain the fence * that represents the last relevant update for this mapping. This
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
@@ -64,6 +64,7 @@ struct amdgpu_bo_param {struct amdgpu_bo_va_mapping {struct amdgpu_bo_va *bo_va;struct list_head list; +struct list_head pending_list;struct rb_node rb;uint64_t start;uint64_t last;
drivers/gpu/drm/amd/amdgpu/amdgpu_seq64.c +1 -0
@@ -104,6 +104,7 @@ int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,amdgpu_vm_bo_del (adev, *bo_va); goto err_ctx; } +amdgpu_vm_update_ctx_commit (&update_ctx); err_ctx:amdgpu_vm_update_ctx_fini (&update_ctx);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +92 -32
164 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +11 -0
@@ -279,6 +279,16 @@ struct amdgpu_vm_update_ctx {*/ struct amdgpu_vm *vm; +/** +* @mapped_intervals: Interval tree of VA ranges mapped by this update operation. +*/ +struct rb_root_cached pending_maps; + +/** +* @new: Pending new mappings not yet applied to bookkeeping structures. +*/ +struct list_head new; +/** * @freed: A list of mappings that are freed and pending PT/PD updates during * this update operation.
@@ -583,6 +593,7 @@ bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);void amdgpu_vm_update_ctx_init (struct amdgpu_vm_update_ctx *ctx,struct amdgpu_device *adev,struct amdgpu_vm *vm); +void amdgpu_vm_update_ctx_commit (struct amdgpu_vm_update_ctx *ctx);void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx);int amdgpu_vm_update_ctx_ensure_unmap_synced (struct amdgpu_vm_update_ctx *ctx);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 03/10] drm/amdgpu: Make VM maps pending per update context
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] drm/amdgpu: 64-bit PFN truncation in amdgpu_vm_update_ctx_add_new_mapping() - [Low] drm/amdgpu…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] drm/amdgpu: 64-bit PFN truncation in amdgpu_vm_update_ctx_add_new_mapping()
- [Low] drm/amdgpu: duplicate interval check in amdgpu_vm_update_ctx_add_new_mapping()
- [Low] drm/amdgpu: fix pending_maps kernel-doc in struct amdgpu_vm_update_ctx
--
commit 471887082bbdcd3f5636c1bb4feae875d96ebdb8
Author: Natalie Vock <[email protected]>
drm/amdgpu: Make VM maps pending per update context
This commit stages new VM mappings in struct amdgpu_vm_update_ctx and inserts them into the VM bookkeeping structures when amdgpu_vm_update_ctx_commit() is called.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0@@ -1589,6 +1589,73 @@ static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
[ ... ]
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8
-0+static int +amdgpu_vm_update_ctx_add_new_mapping (struct amdgpu_vm_update_ctx *ctx, +struct amdgpu_bo_va *bo_va, +struct amdgpu_bo_va_mapping *mapping) +{ +struct interval_tree_node *pending; + + if (interval_tree_iter_first(&ctx->pending_maps, mapping->start,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 1648
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10
-0+ mapping->last)) +return -EINVAL ; + + pending =kzalloc_obj (*pending); +if (!pending) +return -ENOMEM ; + +if (interval_tree_iter_first (&ctx->pending_maps, mapping->start, + mapping->last)) +return -EINVAL ;
[Severity: Low]
Is this second interval_tree_iter_first() check redundant, and would it leak pending if ever taken? ctx->pending_maps is not modified since the identical check before kzalloc_obj(), and returning -EINVAL here skips kfree(pending).
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 1652
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-0+ + pending->start = mapping->start; + pending->last = mapping->last; +interval_tree_insert (pending, &ctx->pending_maps);
[Severity: Medium]
Can using struct interval_tree_node here truncate 64-bit GPU page frame numbers on 32-bit architectures?
mapping->start and mapping->last are uint64_t PFNs (which is why amdgpu_vm.c defines amdgpu_vm_it with uint64_t via INTERVAL_TREE_DEFINE), whereas struct interval_tree_node and interval_tree_iter_first() use unsigned long. On 32-bit builds, GPU virtual addresses at or above 16 TiB (PFN >= 1ULL << 32) have their upper 32 bits truncated when querying and inserting into ctx->pending_maps, which can cause false -EINVAL conflicts, missed overlaps, or inverted intervals where pending->start > pending->last.
While a mapping is pending on ctx->new prior to amdgpu_vm_bo_commit_insert(), its embedded rb and uint64_t start/last/__subtree_last fields are unused, so inserting mapping directly into ctx->pending_maps via the amdgpu_vm_it helpers would also avoid allocating pending.
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm_update_ctx { · line 285
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +4
-0@@ -279,6 +279,16 @@ struct amdgpu_vm_update_ctx {*/ struct amdgpu_vm *vm; +/** +* @mapped_intervals: Interval tree of VA ranges mapped by this update operation. +*/ +struct rb_root_cached pending_maps;
[Severity: Low]
The kernel-doc tag @mapped_intervals does not match the struct member name pending_maps, which triggers a scripts/kernel-doc warning.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/10 drm/amdgpu: Make removed/cleared mappings pending per update context
In reply to Natalie VockThis completes the effort to make all mapping changes pending to be able to easily revert to the previous state in case an error is encountered after unmapping.
In certain cases, it is possible to skip syncing with every submission in the whole VM before unmapping pagetables. The important condition is that no memory must be released to the system while the GPU still has a chance to access it. This is true for VM-always-valid buffers since the resv is shared with the VM's root PD and thus there will always be synchronization before unmapping, but also for PRT pages which do not point to any real memory location at all.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 7 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 305 ++++++++++++++++++++------------ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 24 +++ 4 files changed, 220 insertions(+), 117 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c +1 -0
@@ -140,6 +140,7 @@ int amdgpu_unmap_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, "failed to do bo_unmap on static CSA, err=%d\n", r); goto error_ctx; } +amdgpu_vm_update_ctx_commit (&update_ctx);amdgpu_vm_bo_del (adev, bo_va);
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -1
@@ -1044,7 +1044,12 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,break ; } -amdgpu_vm_update_ctx_commit (&update_ctx); +if (!r) { + r =amdgpu_vm_update_ctx_execute_replace (&update_ctx); +if (r) + goto error; +amdgpu_vm_update_ctx_commit (&update_ctx); + }/* * Once the VA operation is done, update the VM and obtain the fence
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +189 -116
410 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +24 -0
@@ -284,11 +284,27 @@ struct amdgpu_vm_update_ctx {*/ struct rb_root_cached pending_maps; +/** +* @cleared_ranges: Interval tree of VA ranges cleared by this update operation. +*/ +struct rb_root_cached cleared_ranges; +/** * @new: Pending new mappings not yet applied to bookkeeping structures. */ struct list_head new; +/** +* @deleted: Pending freed mappings not yet applied to bookkeeping structures. +*/ +struct list_head deleted; + +/** +* @replaced: Pending mappings that have been cleared, potentially partially, +* and are not yet applied to bookkeeping structures. +*/ +struct list_head cleared; +/** * @freed: A list of mappings that are freed and pending PT/PD updates during * this update operation.
@@ -305,6 +321,13 @@ struct amdgpu_vm_update_ctx {* i.e. all previous submissions in the VM are waited on. */ bool unmap_synced; + +/** +* @explicit_sync_unmap: Whether it's allowed to sync explicitly (i.e. only wait +* on explicit dependencies instead of every submission that may have used the BO) +* before committing unmaps. +*/ +bool explicit_sync_unmap; };struct amdgpu_vm_fault_info {
@@ -593,6 +616,7 @@ bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo);void amdgpu_vm_update_ctx_init (struct amdgpu_vm_update_ctx *ctx,struct amdgpu_device *adev,struct amdgpu_vm *vm); +int amdgpu_vm_update_ctx_execute_replace (struct amdgpu_vm_update_ctx *ctx);void amdgpu_vm_update_ctx_commit (struct amdgpu_vm_update_ctx *ctx);void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx);int amdgpu_vm_update_ctx_ensure_unmap_synced (struct amdgpu_vm_update_ctx *ctx);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 04/10] drm/amdgpu: Make removed/cleared mappings pending per update context
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/amdgpu: missing amdgpu_vm_update_ctx_commit() in unmap_bo_from_gpuvm() - [High] drm/amdgpu…
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] drm/amdgpu: missing amdgpu_vm_update_ctx_commit() in unmap_bo_from_gpuvm()
- [High] drm/amdgpu: unclamped cleared mapping unmaps surviving gaps in clear_freed()
- [Medium] drm/amdgpu: uncleaned pending lists in amdgpu_vm_update_ctx_fini() on error
- [Medium] drm/amdgpu: cur_end moves backward on nested ranges in execute_replace()
- [Medium] drm/amdgpu: 32-bit PFN truncation in cleared_ranges interval_tree_node
- [Low] drm/amdgpu: mismatched @replaced kernel-doc tag for cleared list_head
--
commit 2c940ee3e7377ed3b2aa248bd851e7573259f51f
Author: Natalie Vock <[email protected]>
drm/amdgpu: Make removed/cleared mappings pending per update context
This commit defers VM mapping removals and range clears into lists and interval trees on struct amdgpu_vm_update_ctx until the context is committed, and allows explicit synchronization when unmapping always-valid BOs or PRT mappings.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
[ ... ]
Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +25
-0@@ -1652,10 +1716,103 @@ amdgpu_vm_update_ctx_add_new_mapping(struct amdgpu_vm_update_ctx *ctx,interval_tree_insert (pending, &ctx->pending_maps); mapping->bo_va = bo_va; +WARN_ON (!list_empty (&mapping->pending_list));list_add (&mapping->pending_list, &ctx->new);return 0 ; } +static void +amdgpu_vm_update_ctx_add_replaced_mapping (struct amdgpu_vm_update_ctx *ctx, +struct amdgpu_bo_va_mapping *mapping) +{ +if (list_empty (&mapping->pending_list)) +list_add (&mapping->pending_list, &ctx->cleared); +} + +static void +amdgpu_vm_update_ctx_add_freed_mapping (struct amdgpu_vm_update_ctx *ctx, +struct amdgpu_bo_va_mapping *mapping) +{ +WARN_ON (!list_empty (&mapping->pending_list)); +list_add (&mapping->pending_list, &ctx->deleted); +} + +int amdgpu_vm_update_ctx_execute_replace (struct amdgpu_vm_update_ctx *ctx) +{ +LIST_HEAD (new_maps); +struct interval_tree_node *map; +struct amdgpu_bo_va_mapping *cleared, *next, *gap; +uint64_t cur_end; +int r; +
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c amdgpu_vm_update_ctx_add_new_mapping(struct amdgpu_vm_update_ctx *ctx, · line 1757
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10
-0+list_for_each_entry_safe (cleared, next, &ctx->cleared, pending_list) { + cur_end = cleared->start; + + map =interval_tree_iter_first (&ctx->cleared_ranges, cleared->start, cleared->last); +while (map && map->start <= cleared->last) { +if (map->start <= cur_end) { + cur_end = map->last +1 ; + map =interval_tree_iter_next (map, cleared->start, cleared->last); +continue ; + }
[Severity: Medium]
Can cur_end move backward here when ctx->cleared_ranges contains nested or overlapping intervals? interval_tree_iter_first() and interval_tree_iter_next() walk intervals in ascending order of map->start, so if an earlier interval extends past a later one (for example, [0x10, 0x20] followed by [0x12, 0x15]), the second iteration sees map->start <= cur_end and rewinds cur_end from 0x21 back to 0x16, causing the already cleared subrange [0x16, 0x20] to be treated as an uncleared gap and re-mapped (or rejected as a duplicate in amdgpu_vm_update_ctx_add_new_mapping()). Using max(cur_end, map->last + 1) would prevent cur_end from decreasing.
Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +28
-0+ + gap =kmalloc_obj (*gap); +if (!gap) { + r = -ENOMEM ; + goto error; + } + + gap->bo_va = cleared->bo_va; + gap->start = cur_end; + gap->last = map->start -1 ; + gap->offset = cleared->offset; + gap->offset += (gap->start - cleared->start) <<AMDGPU_GPU_PAGE_SHIFT ; + gap->flags = cleared->flags; +list_add (&gap->pending_list, &new_maps); + + cur_end = map->last +1 ; + map =interval_tree_iter_next (map, cleared->start, cleared->last); + } + +if (cur_end <= cleared->last) { + gap =kmalloc_obj (*gap); +if (!gap) { + r = -ENOMEM ; + goto error; + } + + gap->bo_va = cleared->bo_va; + gap->start = cur_end;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c amdgpu_vm_update_ctx_add_new_mapping(struct amdgpu_vm_update_ctx *ctx, · line 1795
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10
-0+ gap->last = cleared->last; + gap->offset = cleared->offset; + gap->offset += (gap->start - cleared->start) <<AMDGPU_GPU_PAGE_SHIFT ; + gap->flags = cleared->flags; +list_add (&gap->pending_list, &new_maps); + } + +list_del_init (&cleared->pending_list); +amdgpu_vm_update_ctx_add_freed_mapping (ctx, cleared); + }
[Severity: High]
Does queueing the unclamped cleared mapping onto ctx->deleted cause amdgpu_vm_clear_freed() to wipe the hardware PTEs for the surviving gap subranges too?
Previously, amdgpu_vm_bo_clear_mappings() clamped tmp->start and tmp->last to [saddr, eaddr] before adding tmp to ctx->freed, so amdgpu_vm_clear_freed() only cleared PTEs in the unmapped range. With this change, cleared keeps its full [cleared->start, cleared->last] span when amdgpu_vm_bo_commit_remove() moves it to ctx->freed, while the surviving gap mappings are added to cleared->bo_va->invalids. In amdgpu_gem_va_update_vm(), amdgpu_vm_clear_freed() then zeroes the hardware PTEs across the entire original mapping, and the surviving gaps are never re-populated in the page tables for AMDGPU_VA_OP_CLEAR (which never calls amdgpu_vm_bo_update()) or for AMDGPU_VA_OP_REPLACE when the split mapping belongs to another BO or fpriv->prt_va:
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:amdgpu_gem_va_update_vm() {
...
r = amdgpu_vm_clear_freed(ctx, &ctx->vm->last_update);
if (r)
goto error;
/* For MAP/REPLACE we also need to update the BO mappings. */
if (operation == AMDGPU_VA_OP_MAP ||
operation == AMDGPU_VA_OP_REPLACE) {
r = amdgpu_vm_bo_update(ctx, bo_va, false);
if (r)
goto error;
}
...
}Even when replacing a subrange with the same bo_va, amdgpu_vm_clear_freed() clears the full original range and flushes the TLB before amdgpu_vm_bo_update() restores the surviving subranges, which can fault concurrent GPU work accessing the unmodified parts of the mapping.
[ ... ]
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-3@@ -1715,17 +1877,23 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx,*/ void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx) { -struct interval_tree_node *pending, *next_pending; +struct interval_tree_node *node, *next_node;list_splice (&ctx->freed, &ctx->vm->delayed_freed);amdgpu_sync_free (&ctx->sync); -rbtree_postorder_for_each_entry_safe (pending, next_pending, &ctx->pending_maps.rb_root, rb) -kfree (pending); +rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->pending_maps.rb_root, rb) { +kfree (node); + }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, · line 1897
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5
-0+rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->cleared_ranges.rb_root, rb) { +kfree (node); + } ctx->pending_maps =RB_ROOT_CACHED ; + ctx->cleared_ranges =RB_ROOT_CACHED ;WARN_ON (!list_empty (&ctx->new)); +WARN_ON (!list_empty (&ctx->deleted)); }
[Severity: Medium]
If an error occurs before amdgpu_vm_update_ctx_commit() is called—for example, when amdgpu_vm_update_ctx_add_new_mapping() fails in amdgpu_vm_bo_replace_map() after amdgpu_vm_bo_clear_mappings() has already populated ctx->cleared, or when an allocation or mapping insertion fails midway through amdgpu_vm_update_ctx_execute_replace()—does amdgpu_vm_update_ctx_fini() leak ctx->new mappings and leave live mappings linked into the stack-allocated ctx->deleted and ctx->cleared lists?
In this commit, jumping to error in amdgpu_gem_va_ioctl() hits the WARN_ON() checks, leaks any struct amdgpu_bo_va_mapping objects already moved to ctx->new, and leaves existing mappings with mapping->pending_list pointing into the popped update_ctx stack frame. Note that commit "drm/amdgpu: Add function to roll back a VM update ctx" later in the series adds amdgpu_vm_update_ctx_rollback(), but that helper still only unlinks ctx->new entries without calling kfree(mapping) and does not unlink entries on ctx->cleared at all.
[ ... ]
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-11@@ -2178,24 +2345,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx *ctx,return -ENOENT ; } -/* It's unlikely to happen that the mapping userq hasn't been idled -* during user requests GEM unmap IOCTL except for forcing the unmap -* from user space. -*/ -if (unlikely (bo_va->userq_va_mapped)) -amdgpu_userq_gem_va_unmap_validate (ctx->adev, mapping); - -list_del (&mapping->list); -amdgpu_vm_it_remove (mapping, &vm->va); - mapping->bo_va =NULL ; -trace_amdgpu_vm_bo_unmap (bo_va, mapping);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx *ctx, · line 2350
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-7- -if (valid) -list_add (&mapping->list, &ctx->freed); -else -amdgpu_vm_free_mapping (ctx->adev, vm, mapping, - bo_va->last_pt_update); - +amdgpu_vm_update_ctx_add_freed_mapping (ctx, mapping);return 0 ; }
[Severity: High]
Does unmap_bo_from_gpuvm() also need to call amdgpu_vm_update_ctx_commit() after amdgpu_vm_bo_unmap()?
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c:unmap_bo_from_gpuvm() {
struct amdgpu_bo_va *bo_va = entry->bo_va;
struct amdgpu_vm *vm = peer_vm;
struct amdgpu_vm_update_ctx update_ctx;
amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
(void)amdgpu_vm_bo_unmap(&update_ctx, bo_va, entry->va);
/* VM entity stopped if process killed, don't clear freed pt bo */
if (!amdgpu_vm_ready(vm))
goto out;
(void)amdgpu_vm_clear_freed(&update_ctx, &bo_va->last_pt_update);
(void)amdgpu_sync_fence(sync, bo_va->last_pt_update, GFP_KERNEL);out:
amdgpu_vm_update_ctx_fini(&update_ctx); return 0; }
Because amdgpu_vm_bo_unmap() now only queues the mapping onto update_ctx.deleted until amdgpu_vm_update_ctx_commit() runs, unmap_bo_from_gpuvm() passes an empty update_ctx.freed list to amdgpu_vm_clear_freed() and never removes the mapping from bo_va or vm->va. Every KFD unmap leaves the GPU page tables mapped, triggers WARN_ON(!list_empty(&ctx->deleted)) in amdgpu_vm_update_ctx_fini(), and leaves mapping->pending_list pointing at the stack-allocated update_ctx (or rolls the unmap back once commit "drm/amdgpu: Add function to roll back a VM update ctx" is applied).
Quoted 104 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-76@@ -2214,8 +2364,8 @@ int amdgpu_vm_bo_unmap(struct amdgpu_vm_update_ctx *ctx,int amdgpu_vm_bo_clear_mappings (struct amdgpu_vm_update_ctx *ctx,uint64_t saddr,uint64_t size) { -struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; -LIST_HEAD (removed); +struct interval_tree_node *cleared_range; +struct amdgpu_bo_va_mapping *tmp;uint64_t eaddr;int r;@@ -2223,96 +2373,23 @@ int amdgpu_vm_bo_clear_mappings(struct amdgpu_vm_update_ctx *ctx,if (r)return r; - saddr /=AMDGPU_GPU_PAGE_SIZE ; - eaddr = saddr + (size -1 ) /AMDGPU_GPU_PAGE_SIZE ; - -/* Allocate all the needed memory */ - before =kzalloc_obj (*before); -if (!before) + cleared_range =kmalloc_obj (*cleared_range); +if (!cleared_range)return -ENOMEM ; -INIT_LIST_HEAD (&before->list); - after =kzalloc_obj (*after); -if (!after) { -kfree (before); -return -ENOMEM ; - } -INIT_LIST_HEAD (&after->list); + saddr /=AMDGPU_GPU_PAGE_SIZE ; + eaddr = saddr + (size -1 ) /AMDGPU_GPU_PAGE_SIZE ;/* Now gather all removed mappings */ tmp =amdgpu_vm_it_iter_first (&ctx->vm->va, saddr, eaddr);while (tmp) { -/* Remember mapping split at the start */ -if (tmp->start < saddr) { - before->start = tmp->start; - before->last = saddr -1 ; - before->offset = tmp->offset; - before->flags = tmp->flags; - before->bo_va = tmp->bo_va; -list_add (&before->list, &tmp->bo_va->invalids); - } - -/* Remember mapping split at the end */ -if (tmp->last > eaddr) { - after->start = eaddr +1 ; - after->last = tmp->last; - after->offset = tmp->offset; - after->offset += (after->start - tmp->start) <<AMDGPU_GPU_PAGE_SHIFT ; - after->flags = tmp->flags; - after->bo_va = tmp->bo_va; -list_add (&after->list, &tmp->bo_va->invalids); - } - -list_del (&tmp->list); -list_add (&tmp->list, &removed); - +amdgpu_vm_update_ctx_add_replaced_mapping (ctx, tmp); tmp =amdgpu_vm_it_iter_next (tmp, saddr, eaddr); } -/* And free them up */ -list_for_each_entry_safe (tmp, next, &removed, list) { -amdgpu_vm_it_remove (tmp, &ctx->vm->va); -list_del (&tmp->list); - -if (tmp->start < saddr) - tmp->start = saddr; -if (tmp->last > eaddr) - tmp->last = eaddr; - - tmp->bo_va =NULL ; -list_add (&tmp->list, &ctx->freed); -trace_amdgpu_vm_bo_unmap (NULL , tmp); - } - -/* Insert partial mapping before the range */ -if (!list_empty (&before->list)) { -struct amdgpu_bo *bo = before->bo_va->base.bo; - -amdgpu_vm_it_insert (before, &ctx->vm->va); -if (before->flags &AMDGPU_VM_PAGE_PRT ) -amdgpu_vm_prt_get (ctx->adev); - -if (amdgpu_vm_is_bo_always_valid (ctx->vm, bo) && - !before->bo_va->base.moved) -amdgpu_vm_bo_needs_update (&before->bo_va->base); - }else { -kfree (before); - } - -/* Insert partial mapping after the range */ -if (!list_empty (&after->list)) { -struct amdgpu_bo *bo = after->bo_va->base.bo; - -amdgpu_vm_it_insert (after, &ctx->vm->va); -if (after->flags &AMDGPU_VM_PAGE_PRT ) -amdgpu_vm_prt_get (ctx->adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_clear_mappings(struct amdgpu_vm_update_ctx *ctx, · line 2392
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3
-7- -if (amdgpu_vm_is_bo_always_valid (ctx->vm, bo) && - !after->bo_va->base.moved) -amdgpu_vm_bo_needs_update (&after->bo_va->base); - }else { -kfree (after); - } + cleared_range->start = saddr; + cleared_range->last = eaddr; +interval_tree_insert (cleared_range, &ctx->cleared_ranges);
[Severity: Medium]
On 32-bit architectures, does storing 64-bit GPU page frame numbers into struct interval_tree_node truncate saddr and eaddr?
struct interval_tree_node uses unsigned long for start and last, whereas GPU PFNs are uint64_t (up to 36 bits on 48-bit GPU VA spaces, which is why amdgpu_vm.c defines its own uint64_t amdgpu_vm_it interval tree). When a mapping at or above 16 TiB (PFN >= 1ULL << 32) is cleared on a 32-bit build, cleared_range->start and cleared_range->last are truncated to 32 bits; in amdgpu_vm_update_ctx_execute_replace(), cur_end = map->last + 1 then drops below the 64-bit cleared->start, causing (gap->start - cleared->start) << AMDGPU_GPU_PAGE_SHIFT to underflow and produce an out-of-bounds gap->offset.
[ ... ]
Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5
-0@@ -284,11 +284,27 @@ struct amdgpu_vm_update_ctx {*/ struct rb_root_cached pending_maps; +/** +* @cleared_ranges: Interval tree of VA ranges cleared by this update operation. +*/ +struct rb_root_cached cleared_ranges; +/** * @new: Pending new mappings not yet applied to bookkeeping structures. */ struct list_head new;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h struct amdgpu_vm_update_ctx { · line 306
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +10
-0+/** +* @deleted: Pending freed mappings not yet applied to bookkeeping structures. +*/ +struct list_head deleted; + +/** +* @replaced: Pending mappings that have been cleared, potentially partially, +* and are not yet applied to bookkeeping structures. +*/ +struct list_head cleared;
[Severity: Low]
This kernel-doc comment uses @replaced instead of @cleared to document struct list_head cleared, which triggers a kernel-doc warning on W=1 builds.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v2 5/10 drm/amdgpu: Track dirty bo_va structures in update ctx
In reply to Natalie VockSo we can later update all changed BOs without needing to do tracking separately.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 16 ++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
@@ -80,6 +80,8 @@ struct amdgpu_bo_va {/* protected by bo being reserved */ unsigned ref_count; +struct list_head update_link; +/* all other members protected by the VM PD being reserved */ struct dma_fence *last_pt_update;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +14 -2
@@ -1718,6 +1718,8 @@ amdgpu_vm_update_ctx_add_new_mapping(struct amdgpu_vm_update_ctx *ctx, mapping->bo_va = bo_va;WARN_ON (!list_empty (&mapping->pending_list));list_add (&mapping->pending_list, &ctx->new); +if (list_empty (&mapping->bo_va->update_link)) +list_add (&mapping->bo_va->update_link, &ctx->dirty_bo_vas);return 0 ; }
@@ -1733,8 +1735,10 @@ static void amdgpu_vm_update_ctx_add_freed_mapping(struct amdgpu_vm_update_ctx *ctx, struct amdgpu_bo_va_mapping *mapping) { - WARN_ON(!list_empty(&mapping->pending_list)); -list_add (&mapping->pending_list, &ctx->deleted); + if (list_empty(&mapping->bo_va->update_link)) + list_add(&mapping->bo_va->update_link, &ctx->dirty_bo_vas); +if (list_empty (&mapping->pending_list)) +list_add (&mapping->pending_list, &ctx->deleted); }int amdgpu_vm_update_ctx_execute_replace (struct amdgpu_vm_update_ctx *ctx)
@@ -1861,6 +1865,7 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, ctx->pending_maps =RB_ROOT_CACHED ; ctx->cleared_ranges =RB_ROOT_CACHED ; +INIT_LIST_HEAD (&ctx->dirty_bo_vas);INIT_LIST_HEAD (&ctx->new);INIT_LIST_HEAD (&ctx->deleted);INIT_LIST_HEAD (&ctx->freed);
@@ -1878,10 +1883,16 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx,void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx) {struct interval_tree_node *node, *next_node; +struct amdgpu_bo_va *bo_va, *next_bo_va;list_splice (&ctx->freed, &ctx->vm->delayed_freed);amdgpu_sync_free (&ctx->sync); +list_for_each_entry_safe (bo_va, next_bo_va, &ctx->dirty_bo_vas, + update_link) { +list_del_init (&bo_va->update_link); + } +rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->pending_maps.rb_root, rb) {kfree (node); }
@@ -2135,6 +2146,7 @@ struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, bo_va->ref_count =1 ; bo_va->last_pt_update =dma_fence_get_stub (); +INIT_LIST_HEAD (&bo_va->update_link);INIT_LIST_HEAD (&bo_va->valids);INIT_LIST_HEAD (&bo_va->invalids);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5 -0
@@ -305,6 +305,11 @@ struct amdgpu_vm_update_ctx {*/ struct list_head cleared; +/** +* @dirty_bo_vas: amdgpu_bo_va structures touched by current pending mapping changes. +*/ +struct list_head dirty_bo_vas; +/** * @freed: A list of mappings that are freed and pending PT/PD updates during * this update operation.
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 05/10] drm/amdgpu: Track dirty bo_va structures in update ctx
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdgpu: UAF on bo_va in amdgpu_vm_update_ctx_fini() after amdgpu_vm_bo_del() - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/amdgpu: UAF on bo_va in amdgpu_vm_update_ctx_fini() after amdgpu_vm_bo_del()
- [High] drm/amdgpu: skipped delete in amdgpu_vm_update_ctx_add_freed_mapping()
- [Low] drm/amdgpu: update_link placed under wrong lock comment in amdgpu_bo_va
--
commit 3f1753904c640653510160bf642992a7a55fbc3d
Author: Natalie Vock <[email protected]>
drm/amdgpu: Track dirty bo_va structures in update ctx
This commit adds an update_link list head to struct amdgpu_bo_va and tracks modified bo_va structures in a dirty_bo_vas list on struct amdgpu_vm_update_ctx.
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo_va { · line 87
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2
-0@@ -80,6 +80,8 @@ struct amdgpu_bo_va {/* protected by bo being reserved */ unsigned ref_count; +struct list_head update_link; +/* all other members protected by the VM PD being reserved */ struct dma_fence *last_pt_update;
[Severity: Low]
Should update_link be placed below the "all other members protected by the VM PD being reserved" comment? It is modified by the update_ctx helpers while only the VM PD reservation lock is held, including when the individual BO is NULL (such as fpriv->prt_va) or not reserved (when clearing overlapping mappings of other BOs in the VM).
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0
[ ... ]
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static void · line 1742
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-2@@ -1733,8 +1735,10 @@ static void amdgpu_vm_update_ctx_add_freed_mapping(struct amdgpu_vm_update_ctx *ctx, struct amdgpu_bo_va_mapping *mapping) { - WARN_ON(!list_empty(&mapping->pending_list)); -list_add (&mapping->pending_list, &ctx->deleted); + if (list_empty(&mapping->bo_va->update_link)) + list_add(&mapping->bo_va->update_link, &ctx->dirty_bo_vas); +if (list_empty (&mapping->pending_list)) +list_add (&mapping->pending_list, &ctx->deleted); }
[Severity: High]
Can skipping list_add(&mapping->pending_list, &ctx->deleted) when mapping->pending_list is non-empty leave freed mappings in vm->va?
Within one update_ctx, amdgpu_vm_bo_clear_mappings() and amdgpu_vm_bo_replace_map() put overlapping mappings onto ctx->cleared via mapping->pending_list; if amdgpu_vm_bo_unmap() is then called on that mapping in the same context, amdgpu_vm_update_ctx_add_freed_mapping() leaves it on ctx->cleared instead of moving it to ctx->deleted, so amdgpu_vm_update_ctx_execute_replace() re-inserts any non-cleared remainder.
Furthermore, if an update_ctx is aborted before amdgpu_vm_update_ctx_execute_replace() finishes processing ctx->cleared (for example, if amdgpu_vm_bo_replace_map() fails in amdgpu_vm_update_ctx_add_new_mapping()), neither amdgpu_vm_update_ctx_fini() here nor amdgpu_vm_update_ctx_rollback() in commit "drm/amdgpu: Add function to roll back a VM update ctx" unlinks entries from ctx->cleared. When the BO is later removed in amdgpu_vm_bo_del(), amdgpu_vm_update_ctx_add_freed_mapping() silently skips adding the mapping to update_ctx.deleted and kfree(bo_va) frees bo_va while the mapping remains in vm->va with a dangling mapping->bo_va pointer, causing a use-after-free on subsequent vm->va traversals.
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0
-0int amdgpu_vm_update_ctx_execute_replace (struct amdgpu_vm_update_ctx *ctx)
[ ... ]
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, · line 1894
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +5
-0@@ -1878,10 +1883,16 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx,void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx) {struct interval_tree_node *node, *next_node; +struct amdgpu_bo_va *bo_va, *next_bo_va;list_splice (&ctx->freed, &ctx->vm->delayed_freed);amdgpu_sync_free (&ctx->sync); +list_for_each_entry_safe (bo_va, next_bo_va, &ctx->dirty_bo_vas, + update_link) { +list_del_init (&bo_va->update_link); + }
[Severity: High]
Does this cause a use-after-free on bo_va when a caller unmaps and deletes a bo_va before finalizing update_ctx?
In amdgpu_unmap_static_csa(), amdgpu_vm_bo_unmap() adds bo_va->update_link to update_ctx.dirty_bo_vas, and amdgpu_vm_bo_del() then frees bo_va before amdgpu_vm_update_ctx_fini() runs:
drivers/gpu/drm/amd/amdgpu/amdgpu_csa.c:amdgpu_unmap_static_csa() {
...
r = amdgpu_vm_bo_unmap(&update_ctx, bo_va, csa_addr);
if (r) {
drm_err(adev_to_drm(adev),
"failed to do bo_unmap on static CSA, err=%d\n", r);
goto error_ctx;
}
amdgpu_vm_update_ctx_commit(&update_ctx);
amdgpu_vm_bo_del(adev, bo_va);error_ctx:
amdgpu_vm_update_ctx_fini(&update_ctx);
...
}Because amdgpu_vm_bo_del() calls kfree(bo_va) without unlinking bo_va->update_link from the outer update_ctx.dirty_bo_vas, amdgpu_vm_update_ctx_fini() executes list_del_init(&bo_va->update_link) on the freed bo_va. Unlinking bo_va->update_link in amdgpu_vm_bo_del() before kfree(bo_va) would avoid this.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v2 6/10 drm/amdgpu: Add function to roll back a VM update ctx
In reply to Natalie VockUsed for discarding pending operations and retrying. This is needed when we do lots of mapping operations at once and drm_exec tells us to drop everything and retry to avoid deadlocks.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 41 +++++++++++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 1 + 2 files changed, 27 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +26 -15
@@ -1874,37 +1874,48 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, }/** -* amdgpu_vm_update_ctx_fini - finish a VM update operation -* @ctx: The context of the update +* amdgpu_vm_update_ctx_rollback - clear pending VM updates +* @ctx: The context to roll back * -* Adds any leftover mappings needing to be cleared to the VM's delayed free list, -* then frees up resources used by the context. +* Cleans up any pending mapping changes, but does not reset +* anything else (like attached syncobjs). */ -void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx) +void amdgpu_vm_update_ctx_rollback (struct amdgpu_vm_update_ctx *ctx) { +struct amdgpu_bo_va_mapping *mapping, *next;struct interval_tree_node *node, *next_node;struct amdgpu_bo_va *bo_va, *next_bo_va; -list_splice (&ctx->freed, &ctx->vm->delayed_freed); -amdgpu_sync_free (&ctx->sync); +list_for_each_entry_safe (mapping, next, &ctx->new, pending_list) +list_del_init (&mapping->pending_list); +list_for_each_entry_safe (mapping, next, &ctx->deleted, pending_list) +list_del_init (&mapping->pending_list);list_for_each_entry_safe (bo_va, next_bo_va, &ctx->dirty_bo_vas, - update_link) { + update_link)list_del_init (&bo_va->update_link); - } -rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->pending_maps.rb_root, rb) { +rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->pending_maps.rb_root, rb)kfree (node); - } -rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->cleared_ranges.rb_root, rb) { +rbtree_postorder_for_each_entry_safe (node, next_node, &ctx->cleared_ranges.rb_root, rb)kfree (node); - } ctx->pending_maps =RB_ROOT_CACHED ; ctx->cleared_ranges =RB_ROOT_CACHED ; +} -WARN_ON (!list_empty (&ctx->new)); -WARN_ON (!list_empty (&ctx->deleted)); +/** +* amdgpu_vm_update_ctx_fini - finish a VM update operation +* @ctx: The context of the update +* +* Adds any leftover mappings needing to be cleared to the VM's delayed free list, +* then frees up resources used by the context. +*/ +void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx) +{ +amdgpu_vm_update_ctx_rollback (ctx); +list_splice (&ctx->freed, &ctx->vm->delayed_freed); +amdgpu_sync_free (&ctx->sync); }/**
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
@@ -623,6 +623,7 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, struct amdgpu_vm *vm);int amdgpu_vm_update_ctx_execute_replace (struct amdgpu_vm_update_ctx *ctx);void amdgpu_vm_update_ctx_commit (struct amdgpu_vm_update_ctx *ctx); +void amdgpu_vm_update_ctx_rollback (struct amdgpu_vm_update_ctx *ctx);void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx);int amdgpu_vm_update_ctx_ensure_unmap_synced (struct amdgpu_vm_update_ctx *ctx);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 06/10] drm/amdgpu: Add function to roll back a VM update ctx
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/amdgpu: leak of ctx->new mappings in amdgpu_vm_update_ctx_rollback() - [High] drm/amdgpu…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/amdgpu: leak of ctx->new mappings in amdgpu_vm_update_ctx_rollback()
- [High] drm/amdgpu: missing ctx->cleared unlink in amdgpu_vm_update_ctx_rollback()
--
commit b40894c15667a95794d5db4dfea6f22963fb9ec5
Author: Natalie Vock <[email protected]>
drm/amdgpu: Add function to roll back a VM update ctx
This commit introduces amdgpu_vm_update_ctx_rollback() to discard pending mapping changes in a VM update context and calls it from amdgpu_vm_update_ctx_fini().
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-5@@ -1874,37 +1874,48 @@ void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, }/** -* amdgpu_vm_update_ctx_fini - finish a VM update operation -* @ctx: The context of the update +* amdgpu_vm_update_ctx_rollback - clear pending VM updates +* @ctx: The context to roll back * -* Adds any leftover mappings needing to be cleared to the VM's delayed free list, -* then frees up resources used by the context. +* Cleans up any pending mapping changes, but does not reset +* anything else (like attached syncobjs). */ -void amdgpu_vm_update_ctx_fini (struct amdgpu_vm_update_ctx *ctx)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, · line 1890
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4
-2+void amdgpu_vm_update_ctx_rollback (struct amdgpu_vm_update_ctx *ctx) { +struct amdgpu_bo_va_mapping *mapping, *next;struct interval_tree_node *node, *next_node;struct amdgpu_bo_va *bo_va, *next_bo_va; -list_splice (&ctx->freed, &ctx->vm->delayed_freed); -amdgpu_sync_free (&ctx->sync); +list_for_each_entry_safe (mapping, next, &ctx->new, pending_list) +list_del_init (&mapping->pending_list);
[Severity: High]
Does this leak each struct amdgpu_bo_va_mapping on ctx->new?
Unlike mappings on ctx->deleted, newly allocated mappings added by amdgpu_vm_update_ctx_add_new_mapping() are not inserted into bo_va->invalids or vm->va until amdgpu_vm_update_ctx_commit() runs. If a rollback or an error before commit hits this loop, unlinking mapping->pending_list without calling kfree(mapping) drops the only reference to those allocations.
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c void amdgpu_vm_update_ctx_init(struct amdgpu_vm_update_ctx *ctx, · line 1892
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-0+list_for_each_entry_safe (mapping, next, &ctx->deleted, pending_list) +list_del_init (&mapping->pending_list);
[Severity: High]
Should amdgpu_vm_update_ctx_rollback() also unlink mappings on ctx->cleared?
amdgpu_vm_bo_clear_mappings() links overlapping live mappings from vm->va onto ctx->cleared via mapping->pending_list. If a replace or clear operation rolls back or fails before amdgpu_vm_update_ctx_execute_replace() drains ctx->cleared, those mappings stay linked to ctx->cleared while ctx->cleared_ranges is reset. If the context is then destroyed, list_empty(&mapping->pending_list) remains false on the live mappings in vm->va, causing later unmaps and amdgpu_vm_bo_del() to skip removing them from vm->va before freeing bo_va, which leaves dangling pointers and leads to a use-after-free.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v2 7/10 drm/amdgpu: Move some VA op validation to a helper
In reply to Natalie VockTo be reused in a new VA ioctl in a subsequent patch.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 71 +++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +40 -31
@@ -878,9 +878,11 @@ amdgpu_gem_va_update_vm(struct amdgpu_vm_update_ctx *ctx,return fence; } -int amdgpu_gem_va_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) +static int amdgpu_gem_va_validate_args (struct amdgpu_device *adev,uint64_t va, +uint64_t size,uint32_t flags) { +uint64_t vm_size, map_end; +const uint32_t valid_flags =AMDGPU_VM_DELAY_UPDATE |AMDGPU_VM_PAGE_READABLE |AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
@@ -888,53 +890,60 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,const uint32_t prt_flags =AMDGPU_VM_DELAY_UPDATE |AMDGPU_VM_PAGE_PRT ; -struct drm_amdgpu_gem_va *args = data; -struct drm_gem_object *gobj; -struct amdgpu_device *adev =drm_to_adev (dev); -struct amdgpu_fpriv *fpriv = filp->driver_priv; -struct amdgpu_bo *abo; -struct amdgpu_bo_va *bo_va; -struct drm_syncobj *timeline_syncobj =NULL ; -struct dma_fence_chain *timeline_chain =NULL ; -struct amdgpu_vm_update_ctx update_ctx; -struct drm_exec exec; -uint64_t vm_size, tmp; -int r =0 ; - -/* Validate virtual address range against reserved regions. */ -if (args->va_address <AMDGPU_VA_RESERVED_BOTTOM ) { -dev_dbg (dev->dev, +if (va <AMDGPU_VA_RESERVED_BOTTOM ) { +dev_dbg (adev_to_drm (adev)->dev,"va_address 0x%llx is in reserved area 0x%llx\n" , - args->va_address,AMDGPU_VA_RESERVED_BOTTOM ); + va,AMDGPU_VA_RESERVED_BOTTOM );return -EINVAL ; } -if (args->va_address >=AMDGPU_GMC_HOLE_START && - args->va_address <AMDGPU_GMC_HOLE_END ) { -dev_dbg (dev->dev, +if (va >=AMDGPU_GMC_HOLE_START && va <AMDGPU_GMC_HOLE_END ) { +dev_dbg (adev_to_drm (adev)->dev,"va_address 0x%llx is in VA hole 0x%llx-0x%llx\n" , - args->va_address,AMDGPU_GMC_HOLE_START , + va,AMDGPU_GMC_HOLE_START ,AMDGPU_GMC_HOLE_END );return -EINVAL ; } - args->va_address &=AMDGPU_GMC_HOLE_MASK ; - vm_size = adev->vm_manager.max_pfn *AMDGPU_GPU_PAGE_SIZE ; vm_size -=AMDGPU_VA_RESERVED_TOP ; -if (check_add_overflow (args->va_address, args->map_size, &tmp) || tmp > vm_size) { -dev_dbg (dev->dev, +if (check_add_overflow (va &AMDGPU_GMC_HOLE_MASK , size, &map_end) || map_end > vm_size) { +dev_dbg (adev_to_drm (adev)->dev,"va_address 0x%llx is in top reserved area 0x%llx\n" , - args->va_address + args->map_size, vm_size); + va + size, vm_size);return -EINVAL ; } -if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) { -dev_dbg (dev->dev,"invalid flags combination 0x%08X\n" , - args->flags); +if ((flags & ~valid_flags) && (flags & ~prt_flags)) { +dev_dbg (adev_to_drm (adev)->dev,"invalid flags combination 0x%08X\n" , + flags);return -EINVAL ; } +return 0 ; +} + +int amdgpu_gem_va_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) +{ +struct drm_amdgpu_gem_va *args = data; +struct drm_gem_object *gobj; +struct amdgpu_device *adev =drm_to_adev (dev); +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_bo *abo; +struct amdgpu_bo_va *bo_va; +struct drm_syncobj *timeline_syncobj =NULL ; +struct dma_fence_chain *timeline_chain =NULL ; +struct amdgpu_vm_update_ctx update_ctx; +struct drm_exec exec; +int r =0 ; + + r =amdgpu_gem_va_validate_args (adev, args->va_address, args->map_size, args->flags); +if (r) +return r; + + args->va_address &=AMDGPU_GMC_HOLE_MASK ; +/* Validate operation type. */ switch (args->operation) {case AMDGPU_VA_OP_MAP :
Signature · 1 line
2.56.1
PATCH v2 8/10 drm/amdgpu: Only reject conflicting mappings if they are not cleared
In reply to Natalie VockCleared mappings will not immediately disappear from the VM interval tree, instead they will be in a pending list. For the REPLACE_MAPPING op to work correctly, skip rejecting these conflicts as they will be properly resolved later.
--- 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
@@ -2241,7 +2241,7 @@ int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx, eaddr = saddr + (size -1 ) /AMDGPU_GPU_PAGE_SIZE ; tmp =amdgpu_vm_it_iter_first (&vm->va, saddr, eaddr); -if (tmp) { +if (tmp &&list_empty (&tmp->pending_list)) {/* bo and tmp overlap, invalid addr */ dev_err (ctx->adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 08/10] drm/amdgpu: Only reject conflicting mappings if they are not cleared
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Critical] drm/amdgpu: amdgpu_vm_update_ctx_rollback() fails to unlink ctx->cleared - [High] drm/amdgpu…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Critical] drm/amdgpu: amdgpu_vm_update_ctx_rollback() fails to unlink ctx->cleared
- [High] drm/amdgpu: missing interval tree iteration in amdgpu_vm_bo_map()
- [High] drm/amdgpu: amdgpu_vm_bo_map() ignores conflicts on partially cleared ranges
- [High] drm/amdgpu: amdgpu_vm_update_ctx_rollback() leaks ctx->new mappings
- [High] drm/amdgpu: missing amdgpu_vm_update_ctx_commit() in unmap_bo_from_gpuvm()
--
commit 4bdb539ca2c3dc59efa5a70eaf09b4e87c351cb1
Author: Natalie Vock <[email protected]>
drm/amdgpu: Only reject conflicting mappings if they are not cleared
This commit updates amdgpu_vm_bo_map() to only reject an overlapping mapping in the VM interval tree when its pending_list is empty.
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx, · line 2247
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-1@@ -2241,7 +2241,7 @@ int amdgpu_vm_bo_map(struct amdgpu_vm_update_ctx *ctx, eaddr = saddr + (size -1 ) /AMDGPU_GPU_PAGE_SIZE ; tmp =amdgpu_vm_it_iter_first (&vm->va, saddr, eaddr); -if (tmp) { +if (tmp &&list_empty (&tmp->pending_list)) {/* bo and tmp overlap, invalid addr */ dev_err (ctx->adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
[Severity: High]
Can amdgpu_vm_bo_map() miss a conflict when [saddr, eaddr] overlaps more than one mapping in vm->va?
If the first node returned by amdgpu_vm_it_iter_first() has a non-empty pending_list (because it was unmapped or cleared earlier in the same update context) and a subsequent overlapping node in [saddr, eaddr] is still active with an empty pending_list, amdgpu_vm_bo_map() never walks the remaining nodes via amdgpu_vm_it_iter_next(). amdgpu_vm_update_ctx_commit() then inserts the new mapping into vm->va on top of the active mapping.
Should this loop with amdgpu_vm_it_iter_next(tmp, saddr, eaddr) over all overlapping nodes in [saddr, eaddr] and reject the map if any node has an empty pending_list?
[Severity: High]
Does checking list_empty(&tmp->pending_list) also allow mapping over the uncleared portion of a partially cleared mapping?
When amdgpu_vm_bo_clear_mappings() partially clears an existing mapping tmp, it adds the entire unsplit mapping to ctx->cleared via tmp->pending_list while recording only the cleared sub-range in ctx->cleared_ranges:
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_bo_clear_mappings() {
...
tmp = amdgpu_vm_it_iter_first(&ctx->vm->va, saddr, eaddr);
while (tmp) {
amdgpu_vm_update_ctx_add_replaced_mapping(ctx, tmp);
tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
}
cleared_range->start = saddr;
cleared_range->last = eaddr;
interval_tree_insert(cleared_range, &ctx->cleared_ranges);
...
}Because list_empty(&tmp->pending_list) is false across all of [tmp->start, tmp->last], a subsequent amdgpu_vm_bo_map() overlapping the still-active part of tmp is not rejected here. If a later clear in the same update context covers that remaining range, amdgpu_vm_update_ctx_execute_replace() produces no gap mapping to collide with ctx->pending_maps, so the invalid map succeeds without ever reporting a conflict.
[Severity: Critical]
What happens to tmp->pending_list when an update context is rolled back after staging cleared mappings on ctx->cleared?
In commit "drm/amdgpu: Add function to roll back a VM update ctx", amdgpu_vm_update_ctx_rollback() unlinks ctx->new and ctx->deleted, but does not unlink ctx->cleared:
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_update_ctx_rollback() {
...
list_for_each_entry_safe(mapping, next, &ctx->new, pending_list)
list_del_init(&mapping->pending_list);
list_for_each_entry_safe(mapping, next, &ctx->deleted, pending_list)
list_del_init(&mapping->pending_list);
...
}If an operation rolls back after amdgpu_vm_bo_clear_mappings() or amdgpu_vm_bo_replace_map() has placed a live mapping onto ctx->cleared, that mapping stays in vm->va with mapping->pending_list pointing into the expired stack-allocated update_ctx.cleared head. Because list_empty(&mapping->pending_list) then remains false, this check in amdgpu_vm_bo_map() treats the live mapping as cleared and allows conflicting mappings over it. In addition, amdgpu_vm_update_ctx_add_freed_mapping() skips any mapping whose pending_list is not empty, so amdgpu_vm_bo_del() frees bo_va while leaving its mapping in vm->va with a dangling bo_va pointer, leading to a use-after-free on later VM interval tree walks.
[Severity: High]
Does rolling back a context after amdgpu_vm_bo_map() queues a new mapping leak the mapping allocation?
amdgpu_vm_bo_map() allocates mapping with kmalloc_obj(*mapping) and queues it on ctx->new via amdgpu_vm_update_ctx_add_new_mapping(), only inserting it into vm->va and bo_va->invalids when amdgpu_vm_update_ctx_commit() runs. When a later step fails (such as amdgpu_vm_update_ctx_execute_replace() after mapping over a partially cleared range) or hits lock contention, amdgpu_vm_update_ctx_rollback() calls list_del_init(&mapping->pending_list) on each entry in ctx->new without freeing mapping.
[Severity: High]
In commit "drm/amdgpu: Make removed/cleared mappings pending per update context", does unmap_bo_from_gpuvm() also miss a call to amdgpu_vm_update_ctx_commit() before clearing freed mappings?
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c:unmap_bo_from_gpuvm() {
...
amdgpu_vm_update_ctx_init(&update_ctx, adev, vm);
(void)amdgpu_vm_bo_unmap(&update_ctx, bo_va, entry->va);
/* VM entity stopped if process killed, don't clear freed pt bo */
if (!amdgpu_vm_ready(vm))
goto out;
(void)amdgpu_vm_clear_freed(&update_ctx, &bo_va->last_pt_update);
(void)amdgpu_sync_fence(sync, bo_va->last_pt_update, GFP_KERNEL);out:
amdgpu_vm_update_ctx_fini(&update_ctx); return 0; }
Because amdgpu_vm_bo_unmap() only stages the mapping on update_ctx.deleted until amdgpu_vm_update_ctx_commit() moves it to update_ctx.freed and removes it from vm->va, amdgpu_vm_clear_freed() sees an empty freed list and amdgpu_vm_update_ctx_fini() rolls the unmap back, leaving the mapping active in vm->va.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v2 9/10 drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl
In reply to Natalie VockThis new ioctl is a modernized version of AMDGPU_GEM_VA, with the main changes being support for many operations batched in one ioctl, and a better sync model that allows passing multiple syncobjs for both wait and signal. Timeline syncobj are now properly supported in both wait and signal cases.
One breaking change compared to AMDGPU_GEM_VA is that synchronization is now explicit (where possible), meaning that previous submissions are not necessarily waited on.
Using this ioctl allows applications that hammer lots of sparse operations, particularly some games running under Proton, to see significant reductions in stuttering.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 348 +++++++++++++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h | 2 + include/uapi/drm/amdgpu_drm.h | 46 +++++ 4 files changed, 396 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
@@ -3124,7 +3124,8 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {DRM_IOCTL_DEF_DRV (AMDGPU_USERQ_WAIT , amdgpu_userq_wait_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ),DRM_IOCTL_DEF_DRV (AMDGPU_GEM_LIST_HANDLES , amdgpu_gem_list_handles_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ),DRM_IOCTL_DEF_DRV (AMDGPU_PROC_OPTIONS , amdgpu_proc_options_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ), -DRM_IOCTL_DEF_DRV (AMDGPU_UALINK_HANDLE , amdgpu_gem_ualink_handle_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ) +DRM_IOCTL_DEF_DRV (AMDGPU_UALINK_HANDLE , amdgpu_gem_ualink_handle_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ), + DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA2, amdgpu_gem_va2_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), }; static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +346 -2
391 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +2 -0
@@ -65,6 +65,8 @@ int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);int amdgpu_gem_va_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); +int amdgpu_gem_va2_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp);int amdgpu_gem_op_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
include/uapi/drm/amdgpu_drm.h +46 -0
64 lines not on this page · raw message
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 09/10] drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl
Thank you for your contribution! Sashiko AI review found 15 potential issues to consider: - [High] drm/amdgpu: wrong argument to amdgpu_vm_bo_unmap() in amdgpu_gem_va2_ioctl() - [High] drm/amdgpu…
Thank you for your contribution! Sashiko AI review found 15 potential issues to consider:
- [High] drm/amdgpu: wrong argument to amdgpu_vm_bo_unmap() in amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: syncobj_handles leak in amdgpu_gem_prepare_timeline_nodes()
- [High] drm/amdgpu: uninitialized fence pointer in amdgpu_gem_va2_ioctl() error path
- [High] drm/amdgpu: lost bo_va->last_pt_update and TLB fences in amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: wrong struct in DRM_IOCTL_AMDGPU_GEM_VA2 macro definition
- [High] drm/amdgpu: missing runtime PM acquisition in amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: unlocked BO update on CLEAR/REPLACE in amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: unlocked amdgpu_vm_bo_find() call in amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: gobj ref leak on lock contention/error in amdgpu_gem_va2_ioctl()
- [High] drm/amdgpu: memory leak and list corruption in amdgpu_vm_update_ctx_rollback()
- [High] drm/amdgpu: fence ref leak when merging fences in amdgpu_gem_va2_ioctl()
- [Medium] drm/amdgpu: delay never set to true in amdgpu_gem_va2_ioctl()
- [Medium] drm/amdgpu: wrong pointer in PTR_ERR() in amdgpu_gem_add_input_fence()
- [Medium] drm/amdgpu: gobj ref leak on drm_exec_lock_obj() contention or error
- [Medium] drm/amdgpu: in-place VA masking in amdgpu_gem_va2_ioctl() breaks retry
--
commit dddcd1a96bab3eebca95b4fd6d51bf06141d06e1
Author: Natalie Vock <[email protected]>
drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl
This commit adds the DRM_IOCTL_AMDGPU_GEM_VA2 ioctl to batch multiple VM mapping operations in a single call with multiple input and output binary or timeline syncobjs.
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3
-0@@ -53,10 +53,13 @@ static int amdgpu_gem_add_input_fence(struct drm_file *filp, struct amdgpu_vm_update_ctx *update_ctx, uint64_t syncobj_handles_array, + uint64_t syncobj_points_array, uint32_t num_syncobj_handles) { struct dma_fence *fence;uint32_t *syncobj_handles; +uint64_t *syncobj_points; +uint64_t point;int ret, i;if (!num_syncobj_handles)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_add_input_fence(struct drm_file *filp, · line 79
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +7
-0@@ -67,6 +70,17 @@ amdgpu_gem_add_input_fence(struct drm_file *filp,if (IS_ERR (syncobj_handles))return PTR_ERR (syncobj_handles); +if (syncobj_points_array) { + syncobj_points =memdup_user (u64_to_user_ptr (syncobj_points_array), +size_mul (sizeof (uint64_t ), num_syncobj_handles)); +if (IS_ERR (syncobj_points)) { +kfree (syncobj_handles); +return PTR_ERR (syncobj_handles); + }
[Severity: Medium]
Should this return PTR_ERR(syncobj_points) instead of PTR_ERR(syncobj_handles)? syncobj_handles is a valid kernel heap pointer here, so returning PTR_ERR(syncobj_handles) leaks its lower 32 bits as the ioctl return value (or returns 0 if 4 GiB-aligned).
[ ... ]
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13
-0+static int amdgpu_gem_prepare_timeline_nodes ( +struct drm_file *filp,uint64_t syncobj_handles_array, +uint64_t syncobj_points_array,uint32_t num_syncobjs, +struct amdgpu_gem_timeline_out *out) +{ +uint32_t *syncobj_handles; +uint32_t i; +int r; + + syncobj_handles =memdup_user (u64_to_user_ptr (syncobj_handles_array), +size_mul (sizeof (uint32_t ), num_syncobjs)); +if (IS_ERR (syncobj_handles)) +return PTR_ERR (syncobj_handles);
[ ... ]
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +19
-0+for (i =0 ; i < num_syncobjs; i++) { + out->syncobjs[i] =drm_syncobj_find (filp, syncobj_handles[i]); +if (!out->syncobjs[i]) { + r = -ENOENT ; + goto err_loop_rollback; + } + +if (!out->timeline_points[i]) +continue ; + + out->chains[i] =dma_fence_chain_alloc (); +if (!out->chains[i]) { + r = -ENOMEM ; + goto err_loop_rollback; + } + +continue ; +err_loop_rollback: +for (;;) {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c amdgpu_gem_add_input_fence(struct drm_file *filp, · line 178
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+if (out->syncobjs[i]) +drm_syncobj_put (out->syncobjs[i]); +if (out->chains[i]) +dma_fence_chain_free (out->chains[i]); +if (i-- ==0 ) + goto err_free_sync; + } + } + +return 0 ;
[Severity: High]
Does the success path here leak syncobj_handles? kfree(syncobj_handles) is only called on the error path at err_free_handles, so every call with num_syncobjs > 0 leaks the memdup_user() buffer.
[ ... ]
Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +22
-0@@ -1100,6 +1218,232 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,return r; } +int amdgpu_gem_va2_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) +{ +struct drm_amdgpu_gem_va2 *args = data; +struct amdgpu_device *adev =drm_to_adev (dev); +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct drm_amdgpu_gem_va_mapping *mappings; +struct amdgpu_gem_timeline_out tl_out; +struct amdgpu_vm_update_ctx update_ctx; +struct drm_gem_object *gobj; +struct amdgpu_bo_va *bo_va; +struct drm_exec exec; +bool delay = false; +u32 i; +int r; + + mappings =vmemdup_array_user (u64_to_user_ptr (args->mappings), + args->num_mappings, +sizeof (struct drm_amdgpu_gem_va_mapping )); +if (IS_ERR (mappings)) +return PTR_ERR (mappings); +
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1252
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ r =amdgpu_gem_prepare_timeline_nodes (filp, args->syncobjs_out, + args->timeline_points_out, + args->num_syncobjs_out, &tl_out); +if (r) + goto out_free_mappings; + +drm_exec_init (&exec, +DRM_EXEC_INTERRUPTIBLE_WAIT |DRM_EXEC_IGNORE_DUPLICATES , +0 ); +amdgpu_vm_update_ctx_init (&update_ctx, adev, &fpriv->vm);
[Severity: High]
Does amdgpu_gem_va2_ioctl() need to acquire a runtime PM reference via PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(), like amdgpu_gem_va_ioctl() does? Without it, non-delayed VM updates can write to VRAM, flush HDP via MMIO, or submit SDMA jobs while the GPU is runtime-suspended.
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13
-0+ update_ctx.explicit_sync_unmap = true; + + r =amdgpu_gem_add_input_fence (filp, &update_ctx, args->syncobjs_in, + args->timeline_points_in, + args->num_syncobjs_in); +if (r) + goto out; + + drm_exec_until_all_locked(&exec) + { + r =amdgpu_vm_lock_pd (&fpriv->vm, &exec,2 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (r))
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1275
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ goto out; + +for (i =0 ; i < args->num_mappings; ++i) { + r =amdgpu_gem_va_validate_args (adev, mappings[i].va, + mappings[i].map_size, + mappings[i].flags); +if (r) + goto out; + + mappings[i].va &=AMDGPU_GMC_HOLE_MASK ;
[Severity: Medium]
Can masking mappings[i].va in place break retries on lock contention? For an address in the upper canonical range (>= AMDGPU_GMC_HOLE_END), masking it with AMDGPU_GMC_HOLE_MASK places it inside [AMDGPU_GMC_HOLE_START, AMDGPU_GMC_HOLE_END), so if drm_exec_until_all_locked() retries the loop, amdgpu_gem_va_validate_args() will reject mappings[i].va with -EINVAL.
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +7
-0+ +if (mappings[i].flags &AMDGPU_VM_DELAY_UPDATE ) { +/* Can't delay if we're supposed to signal fences */ +if (args->num_syncobjs_out) { + r = -EINVAL ; + goto out; + }
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1292
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+/* If one mapping is delayed, all mappings must be delayed */ +if (i && !delay) { + r = -EINVAL ; + goto out; + } + }else if (delay) { +/* If one mapping is not delayed, no mappings may be delayed */ + r = -EINVAL ; + goto out; + }
[Severity: Medium]
Is delay ever set to true when AMDGPU_VM_DELAY_UPDATE is set? Because delay stays false, any batch with two or more delayed mappings fails with -EINVAL at if (i && !delay), a batch where only mappings[0] is delayed is not rejected, and page table updates are still executed immediately.
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6
-0+ +if ((mappings[i].operation !=AMDGPU_VA_OP_CLEAR ) && + !(mappings[i].flags &AMDGPU_VM_PAGE_PRT )) { + gobj =drm_gem_object_lookup ( + filp, mappings[i].handle); +if (gobj ==NULL ) {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1308
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ r = -ENOENT ; + goto out; + } + bo_va =amdgpu_vm_bo_find ( + &fpriv->vm,gem_to_amdgpu_bo (gobj)); +if (!bo_va) { +drm_gem_object_put (gobj); + r = -ENOENT ; + goto out; + }
[Severity: High]
Is it safe to call amdgpu_vm_bo_find() before locking gobj with drm_exec_lock_obj()? amdgpu_vm_bo_find() walks bo->vm_bo, which is protected by bo->tbo.base.resv; on a shared BO, another DRM file can concurrently modify bo->vm_bo and free its amdgpu_bo_va in amdgpu_vm_bo_del().
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1321
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13
-0+ }else { + gobj =NULL ; + bo_va = mappings[i].operation != +AMDGPU_VA_OP_CLEAR ? + fpriv->prt_va : +NULL ; + } + +if (gobj) { + r =drm_exec_lock_obj (&exec, gobj); +if (drm_exec_is_contended (&exec)) +amdgpu_vm_update_ctx_rollback ( + &update_ctx);
[Severity: High]
When amdgpu_vm_update_ctx_rollback() runs here on lock contention (or via amdgpu_vm_update_ctx_fini() on error), does it leak ctx->new mappings and corrupt ctx->cleared?
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c:amdgpu_vm_update_ctx_rollback() {
...
list_for_each_entry_safe(mapping, next, &ctx->new, pending_list)
list_del_init(&mapping->pending_list);
list_for_each_entry_safe(mapping, next, &ctx->deleted, pending_list)
list_del_init(&mapping->pending_list);
...
}Entries on ctx->new were allocated by amdgpu_vm_bo_map() or amdgpu_vm_bo_replace_map() and are unlinked without kfree(mapping). In addition, ctx->cleared is never unlinked at all, leaving live mappings in fpriv->vm.va with mapping->pending_list pointing into update_ctx.cleared on the stack.
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1322
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1
-0+drm_exec_retry_on_contention (&exec);
[Severity: Medium]
Does drm_exec_retry_on_contention(&exec) leak the gobj reference from drm_gem_object_lookup()? drm_exec_lock_obj() takes its own reference for exec->contended on -EDEADLK, and jumping back to the start of the loop skips drm_gem_object_put(gobj) below.
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1282
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +3
-0+if (unlikely (r)) + goto out; + }
[Severity: High]
Does jumping to out when drm_exec_lock_obj() returns a non-contention error (such as -EINTR or -ENOMEM) also leak the gobj lookup reference? drm_exec_lock_obj() does not record gobj in exec->objects on failure, and goto out bypasses drm_gem_object_put(gobj).
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1338
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +13
-0+ +switch (mappings[i].operation) { +case AMDGPU_VA_OP_MAP : + r =amdgpu_vm_bo_map (&update_ctx, bo_va, + mappings[i].va, + mappings[i].offset_in_bo, + mappings[i].map_size, + mappings[i].flags); +break ; +case AMDGPU_VA_OP_UNMAP : + r =amdgpu_vm_bo_unmap (&update_ctx, bo_va, + mappings[i].flags); +break ;
[Severity: High]
Should amdgpu_vm_bo_unmap() be passed mappings[i].va instead of mappings[i].flags? Passing flags as the virtual address makes AMDGPU_VA_OP_UNMAP look up page 0 and fail with -ENOENT.
[ ... ]
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-0+if (!delay && (!adev->debug_vm || args->num_syncobjs_out)) { +struct dma_fence *fence, *merged; + +if (!amdgpu_vm_ready (&fpriv->vm)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1384
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ fence =dma_fence_get_stub (); + goto insert_fence; + } + + r =amdgpu_vm_clear_freed (&update_ctx, &fpriv->vm.last_update); +if (r) { +drm_err (adev_to_drm (adev), +"Could not clear freed PT entries: %d\n" , r); + goto insert_fence; + }
[Severity: High]
If amdgpu_vm_clear_freed() fails, does goto insert_fence use an uninitialized fence pointer? fence is only initialized afterwards, so insert_fence will pass stack garbage to drm_syncobj_replace_fence(), drm_syncobj_add_point(), and dma_fence_put().
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1396
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12
-0+ + fence =dma_fence_get (fpriv->vm.last_update); + +list_for_each_entry (bo_va, &update_ctx.dirty_bo_vas, + update_link) { + r =amdgpu_vm_bo_update (&update_ctx, bo_va, false); +if (r) { +drm_err (adev_to_drm (adev), +"Could not update BO PT entries: %d\n" , r); + goto insert_fence; + } + }
[Severity: High]
Can this loop call amdgpu_vm_bo_update() on BOs whose dma_resv is not locked?
AMDGPU_VA_OP_CLEAR locks no BO, and AMDGPU_VA_OP_REPLACE only locks the new BO from mappings[i].handle. However, amdgpu_vm_update_ctx_execute_replace() adds the bo_va of every existing mapping that was cleared or split to update_ctx.dirty_bo_vas, so amdgpu_vm_bo_update() then accesses bo->tbo.resource and calls amdgpu_sync_resv() on those affected BOs without holding their reservation locks.
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1405
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9
-0+ +list_for_each_entry (bo_va, &update_ctx.dirty_bo_vas, + update_link) { +if (!amdgpu_vm_is_bo_always_valid (&fpriv->vm, + bo_va->base.bo)) { +dma_fence_put (bo_va->last_pt_update); + bo_va->last_pt_update =dma_fence_get (fence); + } + }
[Severity: High]
Does this discard the page table update fences just generated by amdgpu_vm_bo_update()?
For non-always-valid BOs (and PRT mappings where bo is NULL), amdgpu_vm_bo_update() stores the new PT update fence in bo_va->last_pt_update rather than fpriv->vm.last_update. Overwriting bo_va->last_pt_update with the pre-update fence without merging bo_va->last_pt_update (or fpriv->vm.last_tlb_inv) into fence drops those PT update and TLB invalidation fences from both bo_va->last_pt_update and the signaled output syncobjs.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-0+ +/* Always update PDEs after we touched the mappings. */ + r =amdgpu_vm_update_pdes (adev, &fpriv->vm, false); +if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, · line 1419
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10
-0+ goto insert_fence; + + merged =dma_fence_unwrap_merge (fence, fpriv->vm.last_update); +if (!merged) { +/* As fallback in OOM situations */ +dma_fence_wait (fpriv->vm.last_update, false); +dma_fence_wait (fence, false); + merged =dma_fence_get_stub (); + } + fence = merged;
[Severity: High]
Does assigning fence = merged without calling dma_fence_put(fence) leak the fence reference acquired before the amdgpu_vm_bo_update() loop?
include/uapi/drm/amdgpu_drm.h extern "C" { · line 87
Quoted 9 lines
include/uapi/drm/amdgpu_drm.h +1
-0@@ -83,6 +84,7 @@ extern "C" {#define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)#define DRM_IOCTL_AMDGPU_PROC_OPTIONS DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROC_OPTIONS, struct drm_amdgpu_proc_options)#define DRM_IOCTL_AMDGPU_UALINK_HANDLE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_UALINK_HANDLE, union drm_amdgpu_ualink_handle) +#define DRM_IOCTL_AMDGPU_GEM_VA2 DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_VA2, struct drm_amdgpu_gem_va)
[Severity: High]
Should DRM_IOCTL_AMDGPU_GEM_VA2 use struct drm_amdgpu_gem_va2 instead of struct drm_amdgpu_gem_va? struct drm_amdgpu_gem_va is 64 bytes whereas struct drm_amdgpu_gem_va2 is 56 bytes, so drm_ioctl() will copy 8 bytes past the end of the user struct.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Dave Airlie
maintainerRe: [PATCH v2 09/10] drm/amdgpu: Add new AMDGPU_GEM_VA2 ioctl
In reply to Natalie VockQuoted 28 lines · Natalie Vock
This new ioctl is a modernized version of
AMDGPU_GEM_VA, with the main changes being support for many operations batched in one ioctl, and a better sync model that allows passing multiple syncobjs for both wait and signal. Timeline syncobj are now properly supported in both wait and signal cases.One breaking change compared to
AMDGPU_GEM_VAis that synchronization is now explicit (where possible), meaning that previous submissions are not necessarily waited on.Using this ioctl allows applications that hammer lots of sparse operations, particularly some games running under Proton, to see significant reductions in stuttering.
Signed-off-by Natalie Vock <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 348 +++++++++++++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h | 2 + include/uapi/drm/amdgpu_drm.h | 46 +++++ 4 files changed, 396 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c const struct drm_ioctl_desc amdgpu_ioctls_kms[] = { · line 3129
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2
-1@@ -3124,7 +3124,8 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {DRM_IOCTL_DEF_DRV (AMDGPU_USERQ_WAIT , amdgpu_userq_wait_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ),DRM_IOCTL_DEF_DRV (AMDGPU_GEM_LIST_HANDLES , amdgpu_gem_list_handles_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ),DRM_IOCTL_DEF_DRV (AMDGPU_PROC_OPTIONS , amdgpu_proc_options_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ), - DRM_IOCTL_DEF_DRV(AMDGPU_UALINK_HANDLE, amdgpu_gem_ualink_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW) +DRM_IOCTL_DEF_DRV (AMDGPU_UALINK_HANDLE , amdgpu_gem_ualink_handle_ioctl,DRM_AUTH |DRM_RENDER_ALLOW ), + DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA2, amdgpu_gem_va2_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), };
Can we just call it VM_BIND for consistency with xe and nouveau?
Ideally of course we'd use gpuvm for amd, but even without that it would be good to at least have somewhat consistent naming just so grepping find things.
Dave.
PATCH v2 10/10 drm/amdgpu: Bump minor version
In reply to Natalie VockTo signal availability of the new AMDGPU_GEM_VA2 ioctl to userspace.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
@@ -129,9 +129,10 @@* - 3.64.0 - Userq IP support query * - 3.65.0 - Add userq syncobj timeline signaling support * - 3.66.0 - Allow jpeg type 4 packets and VCE SEI command +* - 3.67.0 - Add AMDGPU_GEM_VA2 ioctl */ #define KMS_DRIVER_MAJOR 3 -#define KMS_DRIVER_MINOR 66 +#define KMS_DRIVER_MINOR 67#define KMS_DRIVER_PATCHLEVEL 0/*
Signature · 1 line
2.56.1