drm/amdgpu: lock both VM and BO in amdgpu_gem_object_open
Subsystems (3)
PATCH 1/7 drm/amdgpu: lock both VM and BO in amdgpu_gem_object_open
The VM was not locked in the past since we initially only cleared the linked list element and not added it to any VM state.
But this has changed quite some time ago, we just never realized this problem because the VM state lock was masking it.
--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 19 +++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 22 ++++++++++++++----- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 10 +++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 ++ 4 files changed, 42 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +13 -6
@@ -878,6 +878,7 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,struct amdgpu_bo *bo[2 ] = {NULL ,NULL };struct amdgpu_bo_va *bo_va;bool same_hive = false; +struct drm_exec exec;int i, ret;if (!va) {
@@ -958,19 +959,25 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem, goto unwind; } -/* Add BO to VM internal data structures */ - ret =amdgpu_bo_reserve (bo[i], false); -if (ret) { -pr_debug ("Unable to reserve BO during memory attach" ); - goto unwind; +drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT ,0 ); + drm_exec_until_all_locked(&exec) { + ret =amdgpu_vm_lock_pd (vm, &exec,0 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) + goto unwind; + ret =drm_exec_lock_obj (&exec, &bo[i]->tbo.base); +drm_exec_retry_on_contention (&exec); +if (unlikely (ret)) + goto unwind; } + bo_va =amdgpu_vm_bo_find (vm, bo[i]);if (!bo_va) bo_va =amdgpu_vm_bo_add (adev, vm, bo[i]);else ++bo_va->ref_count; attachment[i]->bo_va = bo_va; -amdgpu_bo_unreserve (bo[i]); +drm_exec_fini (&exec);if (unlikely (!attachment[i]->bo_va)) { ret = -ENOMEM ; pr_err("Failed to add BO object to VM. ret == %d\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +17 -5
@@ -232,6 +232,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_va *bo_va;struct mm_struct *mm; +struct drm_exec exec;int r; mm =amdgpu_ttm_tt_get_usermm (abo->tbo.ttm);
@@ -242,9 +243,18 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, !amdgpu_vm_is_bo_always_valid(vm, abo))return -EPERM ; - r =amdgpu_bo_reserve (abo, false); -if (r) -return r; +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); + drm_exec_until_all_locked(&exec) { + r =drm_exec_prepare_obj (&exec, &abo->tbo.base,1 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (r)) + goto out_unlock; + + r =amdgpu_vm_lock_pd (vm, &exec,0 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (r)) + goto out_unlock; + }amdgpu_vm_bo_update_shared (abo); bo_va =amdgpu_vm_bo_find (vm, abo);
@@ -260,8 +270,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,amdgpu_bo_unreserve (abo);return r; } - -amdgpu_bo_unreserve (abo); +drm_exec_fini (&exec);/* Validate and add eviction fence to DMABuf imports with dynamic * attachment in compute VMs. Re-validation will be done by
@@ -294,7 +303,10 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj, } }mutex_unlock (&vm->process_info->lock); +return r; +out_unlock: +drm_exec_fini (&exec);return r; }
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +10 -0
@@ -1445,6 +1445,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv) {struct amdgpu_device *adev =drm_to_adev (dev);struct amdgpu_fpriv *fpriv; +struct drm_exec exec;int r, pasid;/* Ensure IB tests are run on ring */
@@ -1484,7 +1485,16 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)if (r) goto error_pasid; +drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); + drm_exec_until_all_locked(&exec) { + r =amdgpu_vm_lock_pd (&fpriv->vm, &exec,0 ); +drm_exec_retry_on_contention (&exec); +if (unlikely (r)) + goto error_vm; + } + fpriv->prt_va =amdgpu_vm_bo_add (adev, &fpriv->vm,NULL ); +drm_exec_fini (&exec);if (!fpriv->prt_va) { r = -ENOMEM ; goto error_vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
@@ -1735,6 +1735,8 @@ struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, {struct amdgpu_bo_va *bo_va; +amdgpu_vm_assert_locked (vm); + bo_va =kzalloc (sizeof (struct amdgpu_bo_va ),GFP_KERNEL );if (bo_va ==NULL ) {return NULL ;
Signature · 1 line
2.43.0
PATCH 2/7 drm/amdgpu: revert to old status lock handling v3
This reverts commit 7e64d0e5979157ee5fc83e58ac61b4a36803c7f1.
The problems were caused by not holding the VM lock while adding new BOs.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 146 ++++++++-------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 15 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c | 4 - 4 files changed, 68 insertions(+), 105 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -4
@@ -1058,12 +1058,12 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,struct amdgpu_bo *bo;int ret; -spin_lock (&vm->status_lock); +spin_lock (&vm->invalidated_lock);while (!list_empty (&vm->invalidated)) { bo_va =list_first_entry (&vm->invalidated, struct amdgpu_bo_va, base.vm_status); -spin_unlock (&vm->status_lock); +spin_unlock (&vm->invalidated_lock); bo = bo_va->base.bo; ret =drm_exec_prepare_obj (exec, &bo->tbo.base,2 );
@@ -1080,9 +1080,9 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,if (ret)return ret; -spin_lock (&vm->status_lock); +spin_lock (&vm->invalidated_lock); } -spin_unlock (&vm->status_lock); +spin_unlock (&vm->invalidated_lock);return 0 ; }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +55 -91
420 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +9 -6
@@ -205,11 +205,11 @@ struct amdgpu_vm_bo_base {/* protected by bo being reserved */ struct amdgpu_vm_bo_base *next; -/* protected by vm status_lock */ +/* protected by vm reservation and invalidated_lock */ struct list_head vm_status;/* if the bo is counted as shared in mem stats -* protected by vm status_lock */ +* protected by vm BO being reserved */ bool shared;/* protected by the BO being reserved */
@@ -345,10 +345,8 @@ struct amdgpu_vm {bool evicting;unsigned int saved_flags; -/* Lock to protect vm_bo add/del/move on all lists of vm */ -spinlock_t status_lock; - -/* Memory statistics for this vm, protected by status_lock */ +/* Memory statistics for this vm, protected by stats_lock */ +spinlock_t stats_lock;struct amdgpu_mem_stats stats[__AMDGPU_PL_NUM];/*
@@ -356,6 +354,8 @@ struct amdgpu_vm {* PDs, PTs or per VM BOs. The state transits are: * * evicted -> relocated (PDs, PTs) or moved (per VM BOs) -> idle +* +* Lists are protected by the root PD dma_resv lock. */ /* Per-VM and PT BOs who needs a validation */
@@ -376,7 +376,10 @@ struct amdgpu_vm {* state transits are: * * evicted_user or invalidated -> done +* +* Lists are protected by the invalidated_lock. */ +spinlock_t invalidated_lock;/* BOs for user mode queues that need a validation */ struct list_head evicted_user;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -4
@@ -544,9 +544,7 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry) entry->bo->vm_bo =NULL ;ttm_bo_set_bulk_move (&entry->bo->tbo,NULL ); -spin_lock (&entry->vm->status_lock);list_del (&entry->vm_status); -spin_unlock (&entry->vm->status_lock);amdgpu_bo_unref (&entry->bo); }
@@ -590,7 +588,6 @@ static void amdgpu_vm_pt_add_list(struct amdgpu_vm_update_params *params,struct amdgpu_vm_pt_cursor seek;struct amdgpu_vm_bo_base *entry; -spin_lock (¶ms->vm->status_lock);for_each_amdgpu_vm_pt_dfs_safe (params->adev, params->vm, cursor, seek, entry) {if (entry && entry->bo)list_move (&entry->vm_status, ¶ms->tlb_flush_waitlist);
@@ -598,7 +595,6 @@ static void amdgpu_vm_pt_add_list(struct amdgpu_vm_update_params *params,/* enter start node now */ list_move (&cursor->entry->vm_status, ¶ms->tlb_flush_waitlist); -spin_unlock (¶ms->vm->status_lock); }/**
Signature · 1 line
2.43.0
PATCH 3/7 drm/amdgpu: fix amdgpu_userq_evict
In reply to Christian KönigCanceling the delayed synchonized can deadlock!
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -7
@@ -1337,13 +1337,8 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,/* Signal current eviction fence */ amdgpu_eviction_fence_signal (evf_mgr, ev_fence); -if (evf_mgr->fd_closing) { -cancel_delayed_work_sync (&uq_mgr->resume_work); -return ; - } - -/* Schedule a resume work */ -schedule_delayed_work (&uq_mgr->resume_work,0 ); +if (!evf_mgr->fd_closing) +schedule_delayed_work (&uq_mgr->resume_work,0 ); } int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
Signature · 1 line
2.43.0
PATCH 4/7 drm/amdgpu: completely rework eviction fence handling
In reply to Christian KönigWell that was broken on multiple levels.
First of all a lot of checks where placed at incorrect locations, especially if the resume worker should run or not.
Then a bunch of code was just mid-layering because of incorrect assignment who should do what.
And finally comments explaining what happens instead of why.
Just re-write it from scratch, that should at least fix some of the hangs we are seeing.
Use RCU for the eviction fence pointer in the manager, the spinlock usage was mostly incorrect as well. Then finally remove all the nonsense checks and actually add them in the correct locations.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 4 +- .../drm/amd/amdgpu/amdgpu_eviction_fence.c | 213 ++++++------------ .../drm/amd/amdgpu/amdgpu_eviction_fence.h | 54 ++--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 10 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 29 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 +- 7 files changed, 112 insertions(+), 205 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -2
@@ -2975,9 +2975,9 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)int idx;if (fpriv &&drm_dev_enter (dev, &idx)) { - fpriv->evf_mgr.fd_closing = true; -amdgpu_eviction_fence_destroy (&fpriv->evf_mgr); +amdgpu_evf_mgr_shutdown (&fpriv->evf_mgr);amdgpu_userq_mgr_fini (&fpriv->userq_mgr); +amdgpu_evf_mgr_fini (&fpriv->evf_mgr);drm_dev_exit (idx); }
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +65 -148
267 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +28 -26
69 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -8
@@ -263,13 +263,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,else ++bo_va->ref_count; -/* attach gfx eviction fence */ - r =amdgpu_eviction_fence_attach (&fpriv->evf_mgr, abo); -if (r) { -DRM_DEBUG_DRIVER ("Failed to attach eviction fence to BO\n" ); -amdgpu_bo_unreserve (abo); -return r; - } +amdgpu_evf_mgr_attach_fence (&fpriv->evf_mgr, abo);drm_exec_fini (&exec);/* Validate and add eviction fence to DMABuf imports with dynamic
@@ -337,7 +331,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj, }if (!amdgpu_vm_is_bo_always_valid (vm, bo)) -amdgpu_eviction_fence_detach (&fpriv->evf_mgr, bo); +amdgpu_evf_mgr_detach_fence (&fpriv->evf_mgr, bo); bo_va =amdgpu_vm_bo_find (vm, bo);if (!bo_va || --bo_va->ref_count)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1 -4
@@ -1522,10 +1522,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)"Failed to init usermode queue manager (%d), use legacy workload submission only\n" , r); - r =amdgpu_eviction_fence_init (&fpriv->evf_mgr); -if (r) - goto error_vm; - +amdgpu_evf_mgr_init (&fpriv->evf_mgr);amdgpu_ctx_mgr_init (&fpriv->ctx_mgr, adev); file_priv->driver_priv = fpriv;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -16
@@ -480,17 +480,16 @@ void amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_eviction_fence_mgr *evf_mgr) { - struct amdgpu_eviction_fence *ev_fence; + struct dma_fence *ev_fence; retry:/* Flush any pending resume work to create ev_fence */ flush_delayed_work (&uq_mgr->resume_work);mutex_lock (&uq_mgr->userq_mutex); -spin_lock (&evf_mgr->ev_fence_lock); - ev_fence = evf_mgr->ev_fence; -spin_unlock (&evf_mgr->ev_fence_lock); -if (!ev_fence ||dma_fence_is_signaled (&ev_fence->base)) { + ev_fence =amdgpu_evf_mgr_get_fence (evf_mgr); +if (dma_fence_is_signaled (ev_fence)) { +dma_fence_put (ev_fence);mutex_unlock (&uq_mgr->userq_mutex);/* * Looks like there was no pending resume work,
@@ -499,6 +498,7 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,schedule_delayed_work (&uq_mgr->resume_work,0 ); goto retry; } +dma_fence_put (ev_fence); } int amdgpu_userq_create_object(struct amdgpu_userq_mgr *uq_mgr,
@@ -1214,7 +1214,7 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)dma_fence_wait (bo_va->last_pt_update, false);dma_fence_wait (vm->last_update, false); - ret =amdgpu_eviction_fence_replace_fence (&fpriv->evf_mgr, &exec); + ret =amdgpu_evf_mgr_rearm (&fpriv->evf_mgr, &exec);if (ret)drm_file_err (uq_mgr->file,"Failed to replace eviction fence\n" );
@@ -1234,11 +1234,13 @@ static void amdgpu_userq_restore_worker(struct work_struct *work) {struct amdgpu_userq_mgr *uq_mgr =work_to_uq_mgr (work, resume_work.work);struct amdgpu_fpriv *fpriv =uq_mgr_to_fpriv (uq_mgr); +struct dma_fence *ev_fence;int ret; -flush_delayed_work (&fpriv->evf_mgr.suspend_work); -mutex_lock (&uq_mgr->userq_mutex); + ev_fence =amdgpu_evf_mgr_get_fence (&fpriv->evf_mgr); +if (!dma_fence_is_signaled (ev_fence)) + goto unlock; ret =amdgpu_userq_vm_validate (uq_mgr);if (ret) {
@@ -1254,6 +1256,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work) unlock:mutex_unlock (&uq_mgr->userq_mutex); +dma_fence_put (ev_fence); } static int
@@ -1317,11 +1320,8 @@ amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) }void -amdgpu_userq_evict (struct amdgpu_userq_mgr *uq_mgr, -struct amdgpu_eviction_fence *ev_fence) +amdgpu_userq_evict (struct amdgpu_userq_mgr *uq_mgr,bool schedule_resume) { -struct amdgpu_fpriv *fpriv =uq_mgr_to_fpriv (uq_mgr); -struct amdgpu_eviction_fence_mgr *evf_mgr = &fpriv->evf_mgr;struct amdgpu_device *adev = uq_mgr->adev;int ret;
@@ -1334,10 +1334,7 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,if (ret)dev_err (adev->dev,"Failed to evict userqueue\n" ); -/* Signal current eviction fence */ -amdgpu_eviction_fence_signal (evf_mgr, ev_fence); - -if (!evf_mgr->fd_closing) +if (schedule_resume)schedule_delayed_work (&uq_mgr->resume_work,0 ); }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -1
@@ -127,7 +127,7 @@ void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_userq_obj *userq_obj);void amdgpu_userq_evict (struct amdgpu_userq_mgr *uq_mgr, -struct amdgpu_eviction_fence *ev_fence); +bool schedule_resume);void amdgpu_userq_ensure_ev_fence (struct amdgpu_userq_mgr *userq_mgr,struct amdgpu_eviction_fence_mgr *evf_mgr);
Signature · 1 line
2.43.0
PATCH 5/7 drm/amdgpu: fix adding eviction fence
In reply to Christian KönigWe can't add the eviction fence without validating the BO.
--- .../drm/amd/amdgpu/amdgpu_eviction_fence.c | 19 ++++++++++++++++--- .../drm/amd/amdgpu/amdgpu_eviction_fence.h | 4 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 9 ++++++--- 3 files changed, 24 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +16 -3
@@ -82,14 +82,27 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)mutex_unlock (&uq_mgr->userq_mutex); } -void amdgpu_evf_mgr_attach_fence (struct amdgpu_eviction_fence_mgr *evf_mgr, -struct amdgpu_bo *bo) +int amdgpu_evf_mgr_attach_fence (struct amdgpu_eviction_fence_mgr *evf_mgr, +struct amdgpu_bo *bo) {struct dma_fence *ev_fence =amdgpu_evf_mgr_get_fence (evf_mgr); +struct ttm_operation_ctx ctx = { false, false };struct dma_resv *resv = bo->tbo.base.resv; +int ret; + +if (!dma_fence_is_signaled (ev_fence)) { + +amdgpu_bo_placement_from_domain (bo, bo->allowed_domains); + ret =ttm_bo_validate (&bo->tbo, &bo->placement, &ctx); +if (!ret) +dma_resv_add_fence (resv, ev_fence, +DMA_RESV_USAGE_BOOKKEEP ); + }else { + ret =0 ; + } -dma_resv_add_fence (resv, ev_fence,DMA_RESV_USAGE_BOOKKEEP );dma_fence_put (ev_fence); +return ret; } int amdgpu_evf_mgr_rearm(struct amdgpu_eviction_fence_mgr *evf_mgr,
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +2 -2
@@ -58,8 +58,8 @@ amdgpu_evf_mgr_get_fence(struct amdgpu_eviction_fence_mgr *evf_mgr)return ev_fence; } -void amdgpu_evf_mgr_attach_fence (struct amdgpu_eviction_fence_mgr *evf_mgr, -struct amdgpu_bo *bo); +int amdgpu_evf_mgr_attach_fence (struct amdgpu_eviction_fence_mgr *evf_mgr, +struct amdgpu_bo *bo);int amdgpu_evf_mgr_rearm (struct amdgpu_eviction_fence_mgr *evf_mgr,struct drm_exec *exec); void amdgpu_evf_mgr_detach_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -3
@@ -258,12 +258,15 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,amdgpu_vm_bo_update_shared (abo); bo_va =amdgpu_vm_bo_find (vm, abo); -if (!bo_va) +if (!bo_va) { bo_va =amdgpu_vm_bo_add (adev, vm, abo); -else + r =amdgpu_evf_mgr_attach_fence (&fpriv->evf_mgr, abo); +if (r) + goto out_unlock; + }else { ++bo_va->ref_count; + } -amdgpu_evf_mgr_attach_fence (&fpriv->evf_mgr, abo);drm_exec_fini (&exec);/* Validate and add eviction fence to DMABuf imports with dynamic
Signature · 1 line
2.43.0
PATCH 6/7 drm/amdgpu: rework amdgpu_userq_wait_ioctl
In reply to Christian KönigLockdep was complaining about a number of issues here. Especially lock inversion between syncobj, dma_resv and copying things into userspace.
Rework the functionality. Split it up into multiple functions, consistenly use memdup_array_user(), fix the lock inversions, a few more bugs in error handling and a leak of dma_fence references.
--- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 633 ++++++++++-------- 1 file changed, 349 insertions(+), 284 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +349 -284
@@ -645,361 +645,426 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,return r; } -int amdgpu_userq_wait_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) +/* Count the number of expected fences so userspace can alloc a buffer */ +static int +amdgpu_userq_wait_count_fences (struct drm_file *filp, +struct drm_amdgpu_userq_wait *wait_info, +u32 *syncobj_handles,u32 *timeline_points, +u32 *timeline_handles, +struct drm_gem_object **gobj_write, +struct drm_gem_object **gobj_read) { -u32 *syncobj_handles, *timeline_points, *timeline_handles, *bo_handles_read, *bo_handles_write; -u32 num_syncobj, num_read_bo_handles, num_write_bo_handles; -struct drm_amdgpu_userq_fence_info *fence_info =NULL ; -struct drm_amdgpu_userq_wait *wait_info = data; -struct amdgpu_fpriv *fpriv = filp->driver_priv; -struct amdgpu_userq_mgr *userq_mgr = &fpriv->userq_mgr; -struct amdgpu_usermode_queue *waitq; -struct drm_gem_object **gobj_write; -struct drm_gem_object **gobj_read; -struct dma_fence **fences =NULL ; -u16 num_points, num_fences =0 ; -int r, i, rentry, wentry, cnt; +int num_read_bo_handles, num_write_bo_handles; +struct dma_fence_unwrap iter; +struct dma_fence *fence, *f; +unsigned int num_fences =0 ;struct drm_exec exec; +int i, r; + +/* +* This needs to be outside of the lock provided by drm_exec for +* DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT to work correctly. +*/ + +/* Count timeline fences */ +for (i =0 ; i < wait_info->num_syncobj_timeline_handles; i++) { + r =drm_syncobj_find_fence (filp, timeline_handles[i], + timeline_points[i], +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) +return r; + +dma_fence_unwrap_for_each (f, &iter, fence) + num_fences++; -if (!amdgpu_userq_enabled (dev)) -return -ENOTSUPP ; +dma_fence_put (fence); + } - num_read_bo_handles = wait_info->num_bo_read_handles; - bo_handles_read =memdup_user (u64_to_user_ptr (wait_info->bo_read_handles), -size_mul (sizeof (u32), num_read_bo_handles)); -if (IS_ERR (bo_handles_read)) -return PTR_ERR (bo_handles_read); +/* Count boolean fences */ +for (i =0 ; i < wait_info->num_syncobj_handles; i++) { + r =drm_syncobj_find_fence (filp, syncobj_handles[i],0 , +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) +return r; - num_write_bo_handles = wait_info->num_bo_write_handles; - bo_handles_write =memdup_user (u64_to_user_ptr (wait_info->bo_write_handles), -size_mul (sizeof (u32), num_write_bo_handles)); -if (IS_ERR (bo_handles_write)) { - r =PTR_ERR (bo_handles_write); - goto free_bo_handles_read; + num_fences++; +dma_fence_put (fence); } - num_syncobj = wait_info->num_syncobj_handles; - syncobj_handles =memdup_user (u64_to_user_ptr (wait_info->syncobj_handles), -size_mul (sizeof (u32), num_syncobj)); -if (IS_ERR (syncobj_handles)) { - r =PTR_ERR (syncobj_handles); - goto free_bo_handles_write; - } +/* Lock all the GEM objects */ +/* TODO: It is actually not necessary to lock them */ + num_read_bo_handles = wait_info->num_bo_read_handles; + num_write_bo_handles = wait_info->num_bo_write_handles; +drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT , + num_read_bo_handles + num_write_bo_handles); - num_points = wait_info->num_syncobj_timeline_handles; - timeline_handles =memdup_user (u64_to_user_ptr (wait_info->syncobj_timeline_handles), -sizeof (u32) * num_points); -if (IS_ERR (timeline_handles)) { - r =PTR_ERR (timeline_handles); - goto free_syncobj_handles; - } + drm_exec_until_all_locked(&exec) { + r =drm_exec_prepare_array (&exec, gobj_read, + num_read_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; - timeline_points =memdup_user (u64_to_user_ptr (wait_info->syncobj_timeline_points), -sizeof (u32) * num_points); -if (IS_ERR (timeline_points)) { - r =PTR_ERR (timeline_points); - goto free_timeline_handles; + r =drm_exec_prepare_array (&exec, gobj_write, + num_write_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; } - gobj_read =kmalloc_array (num_read_bo_handles,sizeof (*gobj_read),GFP_KERNEL ); -if (!gobj_read) { - r = -ENOMEM ; - goto free_timeline_points; - } +/* Count read fences */ +for (i =0 ; i < num_read_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; -for (rentry =0 ; rentry < num_read_bo_handles; rentry++) { - gobj_read[rentry] =drm_gem_object_lookup (filp, bo_handles_read[rentry]); -if (!gobj_read[rentry]) { - r = -ENOENT ; - goto put_gobj_read; - } +dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, +DMA_RESV_USAGE_READ , fence) + num_fences++; } - gobj_write =kmalloc_array (num_write_bo_handles,sizeof (*gobj_write),GFP_KERNEL ); -if (!gobj_write) { - r = -ENOMEM ; - goto put_gobj_read; - } +/* Count write fences */ +for (i =0 ; i < num_write_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; -for (wentry =0 ; wentry < num_write_bo_handles; wentry++) { - gobj_write[wentry] =drm_gem_object_lookup (filp, bo_handles_write[wentry]); -if (!gobj_write[wentry]) { - r = -ENOENT ; - goto put_gobj_write; - } +dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, +DMA_RESV_USAGE_WRITE , fence) + num_fences++; } -drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT , - (num_read_bo_handles + num_write_bo_handles)); - -/* Lock all BOs with retry handling */ - drm_exec_until_all_locked(&exec) { - r =drm_exec_prepare_array (&exec, gobj_read, num_read_bo_handles,1 ); -drm_exec_retry_on_contention (&exec); -if (r) { -drm_exec_fini (&exec); - goto put_gobj_write; - } + wait_info->num_fences = num_fences; + r =0 ; - r =drm_exec_prepare_array (&exec, gobj_write, num_write_bo_handles,1 ); -drm_exec_retry_on_contention (&exec); -if (r) { -drm_exec_fini (&exec); - goto put_gobj_write; - } - } +error_unlock: +/* Unlock all the GEM objects */ +drm_exec_fini (&exec); +return r; +} -if (!wait_info->num_fences) { -if (num_points) { -struct dma_fence_unwrap iter; -struct dma_fence *fence; -struct dma_fence *f; - -for (i =0 ; i < num_points; i++) { - r =drm_syncobj_find_fence (filp, timeline_handles[i], - timeline_points[i], -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto exec_fini; - -dma_fence_unwrap_for_each (f, &iter, fence) - num_fences++; - -dma_fence_put (fence); - } - } +static int +amdgpu_userq_wait_return_fence_info (struct drm_file *filp, +struct drm_amdgpu_userq_wait *wait_info, +u32 *syncobj_handles,u32 *timeline_points, +u32 *timeline_handles, +struct drm_gem_object **gobj_write, +struct drm_gem_object **gobj_read) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *userq_mgr = &fpriv->userq_mgr; +struct drm_amdgpu_userq_fence_info *fence_info; +int num_read_bo_handles, num_write_bo_handles; +struct amdgpu_usermode_queue *waitq; +struct dma_fence **fences, *fence, *f; +struct dma_fence_unwrap iter; +int num_points, num_syncobj; +unsigned int num_fences =0 ; +struct drm_exec exec; +int i, cnt, r; -/* Count syncobj's fence */ -for (i =0 ; i < num_syncobj; i++) { -struct dma_fence *fence; + fence_info =kmalloc_array (wait_info->num_fences,sizeof (*fence_info), +GFP_KERNEL ); +if (!fence_info) +return -ENOMEM ; - r =drm_syncobj_find_fence (filp, syncobj_handles[i], -0 , -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto exec_fini; + fences =kmalloc_array (wait_info->num_fences,sizeof (*fences), +GFP_KERNEL ); +if (!fences) { + r = -ENOMEM ; + goto free_fence_info; + } - num_fences++; -dma_fence_put (fence); - } +/* Retrieve timeline fences */ + num_points = wait_info->num_syncobj_timeline_handles; +for (i =0 ; i < num_points; i++) { + r =drm_syncobj_find_fence (filp, timeline_handles[i], + timeline_points[i], +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) + goto free_fences; -/* Count GEM objects fence */ -for (i =0 ; i < num_read_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; +dma_fence_unwrap_for_each (f, &iter, fence) { +if (num_fences >= wait_info->num_fences) { + r = -EINVAL ; + goto free_fences; + } -dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, -DMA_RESV_USAGE_READ , fence) - num_fences++; + fences[num_fences++] =dma_fence_get (f); } -for (i =0 ; i < num_write_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; +dma_fence_put (fence); + } -dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, -DMA_RESV_USAGE_WRITE , fence) - num_fences++; - } +/* Retrieve boolean fences */ + num_syncobj = wait_info->num_syncobj_handles; +for (i =0 ; i < num_syncobj; i++) { +struct dma_fence *fence; -/* -* Passing num_fences = 0 means that userspace doesn't want to -* retrieve userq_fence_info. If num_fences = 0 we skip filling -* userq_fence_info and return the actual number of fences on -* args->num_fences. -*/ - wait_info->num_fences = num_fences; - }else { -/* Array of fence info */ - fence_info =kmalloc_array (wait_info->num_fences,sizeof (*fence_info),GFP_KERNEL ); -if (!fence_info) { - r = -ENOMEM ; - goto exec_fini; - } + r =drm_syncobj_find_fence (filp, syncobj_handles[i],0 , +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) + goto free_fences; -/* Array of fences */ - fences =kmalloc_array (wait_info->num_fences,sizeof (*fences),GFP_KERNEL ); -if (!fences) { - r = -ENOMEM ; - goto free_fence_info; +if (num_fences >= wait_info->num_fences) { + r = -EINVAL ; + goto free_fences; } -/* Retrieve GEM read objects fence */ -for (i =0 ; i < num_read_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; + fences[num_fences++] = fence; +dma_fence_put (fence); + } -dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, -DMA_RESV_USAGE_READ , fence) { -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { - r = -EINVAL ; - goto free_fences; - } +/* Lock all the GEM objects */ + num_read_bo_handles = wait_info->num_bo_read_handles; + num_write_bo_handles = wait_info->num_bo_write_handles; +drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT , + num_read_bo_handles + num_write_bo_handles); - fences[num_fences++] = fence; -dma_fence_get (fence); - } - } + drm_exec_until_all_locked(&exec) { + r =drm_exec_prepare_array (&exec, gobj_read, + num_read_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; -/* Retrieve GEM write objects fence */ -for (i =0 ; i < num_write_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; + r =drm_exec_prepare_array (&exec, gobj_write, + num_write_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; + } -dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, -DMA_RESV_USAGE_WRITE , fence) { -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { - r = -EINVAL ; - goto free_fences; - } +/* Retrieve GEM read objects fence */ +for (i =0 ; i < num_read_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; - fences[num_fences++] = fence; -dma_fence_get (fence); +dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, +DMA_RESV_USAGE_READ , fence) { +if (num_fences >= wait_info->num_fences) { + r = -EINVAL ; + goto error_unlock; } - } -if (num_points) { -struct dma_fence_unwrap iter; -struct dma_fence *fence; -struct dma_fence *f; - -for (i =0 ; i < num_points; i++) { - r =drm_syncobj_find_fence (filp, timeline_handles[i], - timeline_points[i], -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto free_fences; - -dma_fence_unwrap_for_each (f, &iter, fence) { -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { - r = -EINVAL ; - goto free_fences; - } - -dma_fence_get (f); - fences[num_fences++] = f; - } - -dma_fence_put (fence); - } + fences[num_fences++] = fence; +dma_fence_get (fence); } + } -/* Retrieve syncobj's fence */ -for (i =0 ; i < num_syncobj; i++) { -struct dma_fence *fence; - - r =drm_syncobj_find_fence (filp, syncobj_handles[i], -0 , -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto free_fences; +/* Retrieve GEM write objects fence */ +for (i =0 ; i < num_write_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { +dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, +DMA_RESV_USAGE_WRITE , fence) { +if (num_fences >= wait_info->num_fences) { r = -EINVAL ; - goto free_fences; + goto error_unlock; } fences[num_fences++] = fence; +dma_fence_get (fence); } + } -/* -* Keep only the latest fences to reduce the number of values -* given back to userspace. -*/ - num_fences =dma_fence_dedup_array (fences, num_fences); +drm_exec_fini (&exec); - waitq =xa_load (&userq_mgr->userq_xa, wait_info->waitq_id); -if (!waitq) { - r = -EINVAL ; - goto free_fences; - } +/* +* Keep only the latest fences to reduce the number of values +* given back to userspace. +*/ + num_fences =dma_fence_dedup_array (fences, num_fences); -for (i =0 , cnt =0 ; i < num_fences; i++) { -struct amdgpu_userq_fence_driver *fence_drv; -struct amdgpu_userq_fence *userq_fence; -u32 index; - - userq_fence =to_amdgpu_userq_fence (fences[i]); -if (!userq_fence) { -/* -* Just waiting on other driver fences should -* be good for now -*/ - r =dma_fence_wait (fences[i], true); -if (r) { -dma_fence_put (fences[i]); - goto free_fences; - } + waitq =xa_load (&userq_mgr->userq_xa, wait_info->waitq_id); +if (!waitq) { + r = -EINVAL ; + goto free_fences; + } -dma_fence_put (fences[i]); -continue ; - } +for (i =0 , cnt =0 ; i < num_fences; i++) { +struct amdgpu_userq_fence_driver *fence_drv; +struct amdgpu_userq_fence *userq_fence; +u32 index; - fence_drv = userq_fence->fence_drv; + userq_fence =to_amdgpu_userq_fence (fences[i]); +if (!userq_fence) {/* -* We need to make sure the user queue release their reference -* to the fence drivers at some point before queue destruction. -* Otherwise, we would gather those references until we don't -* have any more space left and crash. +* Just waiting on other driver fences should +* be good for now */ - r =xa_alloc (&waitq->fence_drv_xa, &index, fence_drv, - xa_limit_32b,GFP_KERNEL ); -if (r) + r =dma_fence_wait (fences[i], true); +if (r) { +dma_fence_put (fences[i]); goto free_fences; - -amdgpu_userq_fence_driver_get (fence_drv); - -/* Store drm syncobj's gpu va address and value */ - fence_info[cnt].va = fence_drv->va; - fence_info[cnt].value = fences[i]->seqno; + }dma_fence_put (fences[i]); -/* Increment the actual userq fence count */ - cnt++; +continue ; } - wait_info->num_fences = cnt; -/* Copy userq fence info to user space */ -if (copy_to_user (u64_to_user_ptr (wait_info->out_fences), - fence_info, wait_info->num_fences *sizeof (*fence_info))) { - r = -EFAULT ; + fence_drv = userq_fence->fence_drv; +/* +* We need to make sure the user queue release their reference +* to the fence drivers at some point before queue destruction. +* Otherwise, we would gather those references until we don't +* have any more space left and crash. +*/ + r =xa_alloc (&waitq->fence_drv_xa, &index, fence_drv, + xa_limit_32b,GFP_KERNEL ); +if (r) goto free_fences; - } -kfree (fences); -kfree (fence_info); - } +amdgpu_userq_fence_driver_get (fence_drv); -drm_exec_fini (&exec); -for (i =0 ; i < num_read_bo_handles; i++) -drm_gem_object_put (gobj_read[i]); -kfree (gobj_read); +/* Store drm syncobj's gpu va address and value */ + fence_info[cnt].va = fence_drv->va; + fence_info[cnt].value = fences[i]->seqno; -for (i =0 ; i < num_write_bo_handles; i++) -drm_gem_object_put (gobj_write[i]); -kfree (gobj_write); +dma_fence_put (fences[i]); +/* Increment the actual userq fence count */ + cnt++; + } +/* The refcount in the array is already decremented */ + num_fences =0 ; -kfree (timeline_points); -kfree (timeline_handles); -kfree (syncobj_handles); -kfree (bo_handles_write); -kfree (bo_handles_read); + wait_info->num_fences = cnt; + r =0 ; -return 0 ; +/* Copy userq fence info to user space */ +if (copy_to_user (u64_to_user_ptr (wait_info->out_fences), + fence_info, cnt *sizeof (*fence_info))) { + r = -EFAULT ; + } free_fences: -while (num_fences-- >0 ) +while (num_fences--)dma_fence_put (fences[num_fences]);kfree (fences); + free_fence_info:kfree (fence_info); -exec_fini: +return r; + +error_unlock:drm_exec_fini (&exec); + goto free_fences; +} + + +int amdgpu_userq_wait_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) +{ +int num_points, num_syncobj, num_read_bo_handles, num_write_bo_handles; +u32 *syncobj_handles, *timeline_points, *timeline_handles, + *bo_handles_read, *bo_handles_write; +struct drm_amdgpu_userq_wait *wait_info = data; +struct drm_gem_object **gobj_write; +struct drm_gem_object **gobj_read; +int r, rentry, wentry; +void __user *ptr; + +if (!amdgpu_userq_enabled (dev)) +return -ENOTSUPP ; + + ptr =u64_to_user_ptr (wait_info->bo_read_handles); + num_read_bo_handles = wait_info->num_bo_read_handles; + bo_handles_read =memdup_array_user (ptr, num_read_bo_handles, +sizeof (u32)); +if (IS_ERR (bo_handles_read)) +return PTR_ERR (bo_handles_read); + + ptr =u64_to_user_ptr (wait_info->bo_write_handles); + num_write_bo_handles = wait_info->num_bo_write_handles; + bo_handles_write =memdup_array_user (ptr, num_write_bo_handles, +sizeof (u32)); +if (IS_ERR (bo_handles_write)) { + r =PTR_ERR (bo_handles_write); + goto free_bo_handles_read; + } + + num_syncobj = wait_info->num_syncobj_handles; + ptr =u64_to_user_ptr (wait_info->syncobj_handles); + syncobj_handles =memdup_array_user (ptr, num_syncobj,sizeof (u32)); +if (IS_ERR (syncobj_handles)) { + r =PTR_ERR (syncobj_handles); + goto free_bo_handles_write; + } + + num_points = wait_info->num_syncobj_timeline_handles; + ptr =u64_to_user_ptr (wait_info->syncobj_timeline_handles); + timeline_handles =memdup_array_user (ptr, num_points,sizeof (u32)); +if (IS_ERR (timeline_handles)) { + r =PTR_ERR (timeline_handles); + goto free_syncobj_handles; + } + + ptr =u64_to_user_ptr (wait_info->syncobj_timeline_points); + timeline_points =memdup_array_user (ptr, num_points,sizeof (u32)); +if (IS_ERR (timeline_points)) { + r =PTR_ERR (timeline_points); + goto free_timeline_handles; + } + + gobj_read =kmalloc_array (num_read_bo_handles,sizeof (*gobj_read), +GFP_KERNEL ); +if (!gobj_read) { + r = -ENOMEM ; + goto free_timeline_points; + } + +for (rentry =0 ; rentry < num_read_bo_handles; rentry++) { +u32 handle = bo_handles_read[rentry]; + + gobj_read[rentry] =drm_gem_object_lookup (filp, handle); +if (!gobj_read[rentry]) { + r = -ENOENT ; + goto put_gobj_read; + } + } + + gobj_write =kmalloc_array (num_write_bo_handles,sizeof (*gobj_write), +GFP_KERNEL ); +if (!gobj_write) { + r = -ENOMEM ; + goto put_gobj_read; + } + +for (wentry =0 ; wentry < num_write_bo_handles; wentry++) { +u32 handle = bo_handles_write[wentry]; + + gobj_write[wentry] =drm_gem_object_lookup (filp, handle); +if (!gobj_write[wentry]) { + r = -ENOENT ; + goto put_gobj_write; + } + } + +/* +* Passing num_fences = 0 means that userspace doesn't want to +* retrieve userq_fence_info. If num_fences = 0 we skip filling +* userq_fence_info and return the actual number of fences on +* args->num_fences. +*/ +if (!wait_info->num_fences) { + r =amdgpu_userq_wait_count_fences (filp, wait_info, + syncobj_handles, + timeline_points, + timeline_handles, + gobj_write, + gobj_read); + }else { + r =amdgpu_userq_wait_return_fence_info (filp, wait_info, + syncobj_handles, + timeline_points, + timeline_handles, + gobj_write, + gobj_read); + } + put_gobj_write:while (wentry-- >0 )drm_gem_object_put (gobj_write[wentry]);
Signature · 1 line
2.43.0
Re: [PATCH 6/7] drm/amdgpu: rework amdgpu_userq_wait_ioctl
Quoted 669 lines · Christian König
Lockdep was complaining about a number of issues here. Especially lock inversion between syncobj,
dma_resvand copying things into userspace.Rework the functionality. Split it up into multiple functions, consistenly use
memdup_array_user(), fix the lock inversions, a few more bugs in error handling and a leak ofdma_fencereferences.Signed-off-by Christian König <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 633 ++++++++++-------- 1 file changed, 349 insertions(+), 284 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +294
-284@@ -645,361 +645,426 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,return r; } -int amdgpu_userq_wait_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) +/* Count the number of expected fences so userspace can alloc a buffer */ +static int +amdgpu_userq_wait_count_fences (struct drm_file *filp, +struct drm_amdgpu_userq_wait *wait_info, +u32 *syncobj_handles,u32 *timeline_points, +u32 *timeline_handles, +struct drm_gem_object **gobj_write, +struct drm_gem_object **gobj_read) { -u32 *syncobj_handles, *timeline_points, *timeline_handles, *bo_handles_read, *bo_handles_write; -u32 num_syncobj, num_read_bo_handles, num_write_bo_handles; -struct drm_amdgpu_userq_fence_info *fence_info =NULL ; -struct drm_amdgpu_userq_wait *wait_info = data; -struct amdgpu_fpriv *fpriv = filp->driver_priv; -struct amdgpu_userq_mgr *userq_mgr = &fpriv->userq_mgr; -struct amdgpu_usermode_queue *waitq; -struct drm_gem_object **gobj_write; -struct drm_gem_object **gobj_read; -struct dma_fence **fences =NULL ; -u16 num_points, num_fences =0 ; -int r, i, rentry, wentry, cnt; +int num_read_bo_handles, num_write_bo_handles; +struct dma_fence_unwrap iter; +struct dma_fence *fence, *f; +unsigned int num_fences =0 ;struct drm_exec exec; +int i, r; + +/* +* This needs to be outside of the lock provided by drm_exec for +* DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT to work correctly. +*/ + +/* Count timeline fences */ +for (i =0 ; i < wait_info->num_syncobj_timeline_handles; i++) { + r =drm_syncobj_find_fence (filp, timeline_handles[i], + timeline_points[i], +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) +return r; + +dma_fence_unwrap_for_each (f, &iter, fence) + num_fences++; -if (!amdgpu_userq_enabled (dev)) -return -ENOTSUPP ; +dma_fence_put (fence); + } - num_read_bo_handles = wait_info->num_bo_read_handles; - bo_handles_read =memdup_user (u64_to_user_ptr (wait_info->bo_read_handles), -size_mul (sizeof (u32), num_read_bo_handles)); -if (IS_ERR (bo_handles_read)) -return PTR_ERR (bo_handles_read); +/* Count boolean fences */ +for (i =0 ; i < wait_info->num_syncobj_handles; i++) { + r =drm_syncobj_find_fence (filp, syncobj_handles[i],0 , +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) +return r; - num_write_bo_handles = wait_info->num_bo_write_handles; - bo_handles_write =memdup_user (u64_to_user_ptr (wait_info->bo_write_handles), -size_mul (sizeof (u32), num_write_bo_handles)); -if (IS_ERR (bo_handles_write)) { - r =PTR_ERR (bo_handles_write); - goto free_bo_handles_read; + num_fences++; +dma_fence_put (fence); } - num_syncobj = wait_info->num_syncobj_handles; - syncobj_handles =memdup_user (u64_to_user_ptr (wait_info->syncobj_handles), -size_mul (sizeof (u32), num_syncobj)); -if (IS_ERR (syncobj_handles)) { - r =PTR_ERR (syncobj_handles); - goto free_bo_handles_write; - } +/* Lock all the GEM objects */ +/* TODO: It is actually not necessary to lock them */ + num_read_bo_handles = wait_info->num_bo_read_handles; + num_write_bo_handles = wait_info->num_bo_write_handles; +drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT , + num_read_bo_handles + num_write_bo_handles); - num_points = wait_info->num_syncobj_timeline_handles; - timeline_handles =memdup_user (u64_to_user_ptr (wait_info->syncobj_timeline_handles), -sizeof (u32) * num_points); -if (IS_ERR (timeline_handles)) { - r =PTR_ERR (timeline_handles); - goto free_syncobj_handles; - } + drm_exec_until_all_locked(&exec) { + r =drm_exec_prepare_array (&exec, gobj_read, + num_read_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; - timeline_points =memdup_user (u64_to_user_ptr (wait_info->syncobj_timeline_points), -sizeof (u32) * num_points); -if (IS_ERR (timeline_points)) { - r =PTR_ERR (timeline_points); - goto free_timeline_handles; + r =drm_exec_prepare_array (&exec, gobj_write, + num_write_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; } - gobj_read =kmalloc_array (num_read_bo_handles,sizeof (*gobj_read),GFP_KERNEL ); -if (!gobj_read) { - r = -ENOMEM ; - goto free_timeline_points; - } +/* Count read fences */ +for (i =0 ; i < num_read_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; -for (rentry =0 ; rentry < num_read_bo_handles; rentry++) { - gobj_read[rentry] =drm_gem_object_lookup (filp, bo_handles_read[rentry]); -if (!gobj_read[rentry]) { - r = -ENOENT ; - goto put_gobj_read; - } +dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, +DMA_RESV_USAGE_READ , fence) + num_fences++; } - gobj_write =kmalloc_array (num_write_bo_handles,sizeof (*gobj_write),GFP_KERNEL ); -if (!gobj_write) { - r = -ENOMEM ; - goto put_gobj_read; - } +/* Count write fences */ +for (i =0 ; i < num_write_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; -for (wentry =0 ; wentry < num_write_bo_handles; wentry++) { - gobj_write[wentry] =drm_gem_object_lookup (filp, bo_handles_write[wentry]); -if (!gobj_write[wentry]) { - r = -ENOENT ; - goto put_gobj_write; - } +dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, +DMA_RESV_USAGE_WRITE , fence) + num_fences++; } -drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT , - (num_read_bo_handles + num_write_bo_handles)); - -/* Lock all BOs with retry handling */ - drm_exec_until_all_locked(&exec) { - r =drm_exec_prepare_array (&exec, gobj_read, num_read_bo_handles,1 ); -drm_exec_retry_on_contention (&exec); -if (r) { -drm_exec_fini (&exec); - goto put_gobj_write; - } + wait_info->num_fences = num_fences; + r =0 ; - r =drm_exec_prepare_array (&exec, gobj_write, num_write_bo_handles,1 ); -drm_exec_retry_on_contention (&exec); -if (r) { -drm_exec_fini (&exec); - goto put_gobj_write; - } - } +error_unlock: +/* Unlock all the GEM objects */ +drm_exec_fini (&exec); +return r; +} -if (!wait_info->num_fences) { -if (num_points) { -struct dma_fence_unwrap iter; -struct dma_fence *fence; -struct dma_fence *f; - -for (i =0 ; i < num_points; i++) { - r =drm_syncobj_find_fence (filp, timeline_handles[i], - timeline_points[i], -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto exec_fini; - -dma_fence_unwrap_for_each (f, &iter, fence) - num_fences++; - -dma_fence_put (fence); - } - } +static int +amdgpu_userq_wait_return_fence_info (struct drm_file *filp, +struct drm_amdgpu_userq_wait *wait_info, +u32 *syncobj_handles,u32 *timeline_points, +u32 *timeline_handles, +struct drm_gem_object **gobj_write, +struct drm_gem_object **gobj_read) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *userq_mgr = &fpriv->userq_mgr; +struct drm_amdgpu_userq_fence_info *fence_info; +int num_read_bo_handles, num_write_bo_handles; +struct amdgpu_usermode_queue *waitq; +struct dma_fence **fences, *fence, *f; +struct dma_fence_unwrap iter; +int num_points, num_syncobj; +unsigned int num_fences =0 ; +struct drm_exec exec; +int i, cnt, r; -/* Count syncobj's fence */ -for (i =0 ; i < num_syncobj; i++) { -struct dma_fence *fence; + fence_info =kmalloc_array (wait_info->num_fences,sizeof (*fence_info), +GFP_KERNEL ); +if (!fence_info) +return -ENOMEM ; - r =drm_syncobj_find_fence (filp, syncobj_handles[i], -0 , -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto exec_fini; + fences =kmalloc_array (wait_info->num_fences,sizeof (*fences), +GFP_KERNEL ); +if (!fences) { + r = -ENOMEM ; + goto free_fence_info; + } - num_fences++; -dma_fence_put (fence); - } +/* Retrieve timeline fences */ + num_points = wait_info->num_syncobj_timeline_handles; +for (i =0 ; i < num_points; i++) { + r =drm_syncobj_find_fence (filp, timeline_handles[i], + timeline_points[i], +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) + goto free_fences; -/* Count GEM objects fence */ -for (i =0 ; i < num_read_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; +dma_fence_unwrap_for_each (f, &iter, fence) { +if (num_fences >= wait_info->num_fences) { + r = -EINVAL ; + goto free_fences; + } -dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, -DMA_RESV_USAGE_READ , fence) - num_fences++; + fences[num_fences++] =dma_fence_get (f); } -for (i =0 ; i < num_write_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; +dma_fence_put (fence); + } -dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, -DMA_RESV_USAGE_WRITE , fence) - num_fences++; - } +/* Retrieve boolean fences */ + num_syncobj = wait_info->num_syncobj_handles; +for (i =0 ; i < num_syncobj; i++) { +struct dma_fence *fence; -/* -* Passing num_fences = 0 means that userspace doesn't want to -* retrieve userq_fence_info. If num_fences = 0 we skip filling -* userq_fence_info and return the actual number of fences on -* args->num_fences. -*/ - wait_info->num_fences = num_fences; - }else { -/* Array of fence info */ - fence_info =kmalloc_array (wait_info->num_fences,sizeof (*fence_info),GFP_KERNEL ); -if (!fence_info) { - r = -ENOMEM ; - goto exec_fini; - } + r =drm_syncobj_find_fence (filp, syncobj_handles[i],0 , +DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , + &fence); +if (r) + goto free_fences; -/* Array of fences */ - fences =kmalloc_array (wait_info->num_fences,sizeof (*fences),GFP_KERNEL ); -if (!fences) { - r = -ENOMEM ; - goto free_fence_info; +if (num_fences >= wait_info->num_fences) { + r = -EINVAL ; + goto free_fences; } -/* Retrieve GEM read objects fence */ -for (i =0 ; i < num_read_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; + fences[num_fences++] = fence; +dma_fence_put (fence); + } -dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, -DMA_RESV_USAGE_READ , fence) { -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { - r = -EINVAL ; - goto free_fences; - } +/* Lock all the GEM objects */ + num_read_bo_handles = wait_info->num_bo_read_handles; + num_write_bo_handles = wait_info->num_bo_write_handles; +drm_exec_init (&exec,DRM_EXEC_INTERRUPTIBLE_WAIT , + num_read_bo_handles + num_write_bo_handles); - fences[num_fences++] = fence; -dma_fence_get (fence); - } - } + drm_exec_until_all_locked(&exec) { + r =drm_exec_prepare_array (&exec, gobj_read, + num_read_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; -/* Retrieve GEM write objects fence */ -for (i =0 ; i < num_write_bo_handles; i++) { -struct dma_resv_iter resv_cursor; -struct dma_fence *fence; + r =drm_exec_prepare_array (&exec, gobj_write, + num_write_bo_handles,1 ); +drm_exec_retry_on_contention (&exec); +if (r) + goto error_unlock; + } -dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, -DMA_RESV_USAGE_WRITE , fence) { -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { - r = -EINVAL ; - goto free_fences; - } +/* Retrieve GEM read objects fence */ +for (i =0 ; i < num_read_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; - fences[num_fences++] = fence; -dma_fence_get (fence); +dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv, +DMA_RESV_USAGE_READ , fence) { +if (num_fences >= wait_info->num_fences) { + r = -EINVAL ; + goto error_unlock; } - } -if (num_points) { -struct dma_fence_unwrap iter; -struct dma_fence *fence; -struct dma_fence *f; - -for (i =0 ; i < num_points; i++) { - r =drm_syncobj_find_fence (filp, timeline_handles[i], - timeline_points[i], -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto free_fences; - -dma_fence_unwrap_for_each (f, &iter, fence) { -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { - r = -EINVAL ; - goto free_fences; - } - -dma_fence_get (f); - fences[num_fences++] = f; - } - -dma_fence_put (fence); - } + fences[num_fences++] = fence; +dma_fence_get (fence); } + } -/* Retrieve syncobj's fence */ -for (i =0 ; i < num_syncobj; i++) { -struct dma_fence *fence; - - r =drm_syncobj_find_fence (filp, syncobj_handles[i], -0 , -DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT , - &fence); -if (r) - goto free_fences; +/* Retrieve GEM write objects fence */ +for (i =0 ; i < num_write_bo_handles; i++) { +struct dma_resv_iter resv_cursor; +struct dma_fence *fence; -if (WARN_ON_ONCE (num_fences >= wait_info->num_fences)) { +dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv, +DMA_RESV_USAGE_WRITE , fence) { +if (num_fences >= wait_info->num_fences) { r = -EINVAL ; - goto free_fences; + goto error_unlock; } fences[num_fences++] = fence; +dma_fence_get (fence); } + } -/* -* Keep only the latest fences to reduce the number of values -* given back to userspace. -*/ - num_fences =dma_fence_dedup_array (fences, num_fences); +drm_exec_fini (&exec); - waitq =xa_load (&userq_mgr->userq_xa, wait_info->waitq_id); -if (!waitq) { - r = -EINVAL ; - goto free_fences; - } +/* +* Keep only the latest fences to reduce the number of values +* given back to userspace. +*/ + num_fences =dma_fence_dedup_array (fences, num_fences); -for (i =0 , cnt =0 ; i < num_fences; i++) { -struct amdgpu_userq_fence_driver *fence_drv; -struct amdgpu_userq_fence *userq_fence; -u32 index; - - userq_fence =to_amdgpu_userq_fence (fences[i]); -if (!userq_fence) { -/* -* Just waiting on other driver fences should -* be good for now -*/ - r =dma_fence_wait (fences[i], true); -if (r) { -dma_fence_put (fences[i]); - goto free_fences; - } + waitq =xa_load (&userq_mgr->userq_xa, wait_info->waitq_id); +if (!waitq) { + r = -EINVAL ; + goto free_fences; + } -dma_fence_put (fences[i]); -continue ; - } +for (i =0 , cnt =0 ; i < num_fences; i++) { +struct amdgpu_userq_fence_driver *fence_drv; +struct amdgpu_userq_fence *userq_fence; +u32 index; - fence_drv = userq_fence->fence_drv; + userq_fence =to_amdgpu_userq_fence (fences[i]); +if (!userq_fence) {/* -* We need to make sure the user queue release their reference -* to the fence drivers at some point before queue destruction. -* Otherwise, we would gather those references until we don't -* have any more space left and crash. +* Just waiting on other driver fences should +* be good for now */ - r =xa_alloc (&waitq->fence_drv_xa, &index, fence_drv, - xa_limit_32b,GFP_KERNEL ); -if (r) + r =dma_fence_wait (fences[i], true); +if (r) { +dma_fence_put (fences[i]); goto free_fences; - -amdgpu_userq_fence_driver_get (fence_drv); - -/* Store drm syncobj's gpu va address and value */ - fence_info[cnt].va = fence_drv->va; - fence_info[cnt].value = fences[i]->seqno; + }dma_fence_put (fences[i]); -/* Increment the actual userq fence count */ - cnt++; +continue ; } - wait_info->num_fences = cnt; -/* Copy userq fence info to user space */ -if (copy_to_user (u64_to_user_ptr (wait_info->out_fences), - fence_info, wait_info->num_fences *sizeof (*fence_info))) { - r = -EFAULT ; + fence_drv = userq_fence->fence_drv; +/* +* We need to make sure the user queue release their reference +* to the fence drivers at some point before queue destruction. +* Otherwise, we would gather those references until we don't +* have any more space left and crash. +*/ + r =xa_alloc (&waitq->fence_drv_xa, &index, fence_drv, + xa_limit_32b,GFP_KERNEL ); +if (r) goto free_fences; - } -kfree (fences); -kfree (fence_info); - } +amdgpu_userq_fence_driver_get (fence_drv); -drm_exec_fini (&exec); -for (i =0 ; i < num_read_bo_handles; i++) -drm_gem_object_put (gobj_read[i]); -kfree (gobj_read); +/* Store drm syncobj's gpu va address and value */ + fence_info[cnt].va = fence_drv->va; + fence_info[cnt].value = fences[i]->seqno; -for (i =0 ; i < num_write_bo_handles; i++) -drm_gem_object_put (gobj_write[i]); -kfree (gobj_write); +dma_fence_put (fences[i]); +/* Increment the actual userq fence count */ + cnt++; + } +/* The refcount in the array is already decremented */ + num_fences =0 ; -kfree (timeline_points); -kfree (timeline_handles); -kfree (syncobj_handles); -kfree (bo_handles_write); -kfree (bo_handles_read); + wait_info->num_fences = cnt; + r =0 ; -return 0 ; +/* Copy userq fence info to user space */ +if (copy_to_user (u64_to_user_ptr (wait_info->out_fences), + fence_info, cnt *sizeof (*fence_info))) { + r = -EFAULT ; + } free_fences: -while (num_fences-- >0 ) +while (num_fences--)dma_fence_put (fences[num_fences]);kfree (fences); + free_fence_info:kfree (fence_info); -exec_fini: +return r; + +error_unlock:drm_exec_fini (&exec); + goto free_fences; +} + + +int amdgpu_userq_wait_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) +{ +int num_points, num_syncobj, num_read_bo_handles, num_write_bo_handles; +u32 *syncobj_handles, *timeline_points, *timeline_handles, + *bo_handles_read, *bo_handles_write; +struct drm_amdgpu_userq_wait *wait_info = data; +struct drm_gem_object **gobj_write; +struct drm_gem_object **gobj_read; +int r, rentry, wentry; +void __user *ptr; + +if (!amdgpu_userq_enabled (dev)) +return -ENOTSUPP ; + + ptr =u64_to_user_ptr (wait_info->bo_read_handles); + num_read_bo_handles = wait_info->num_bo_read_handles; + bo_handles_read =memdup_array_user (ptr, num_read_bo_handles, +sizeof (u32)); +if (IS_ERR (bo_handles_read)) +return PTR_ERR (bo_handles_read); + + ptr =u64_to_user_ptr (wait_info->bo_write_handles); + num_write_bo_handles = wait_info->num_bo_write_handles; + bo_handles_write =memdup_array_user (ptr, num_write_bo_handles, +sizeof (u32)); +if (IS_ERR (bo_handles_write)) { + r =PTR_ERR (bo_handles_write); + goto free_bo_handles_read; + } + + num_syncobj = wait_info->num_syncobj_handles; + ptr =u64_to_user_ptr (wait_info->syncobj_handles); + syncobj_handles =memdup_array_user (ptr, num_syncobj,sizeof (u32)); +if (IS_ERR (syncobj_handles)) { + r =PTR_ERR (syncobj_handles); + goto free_bo_handles_write; + } + + num_points = wait_info->num_syncobj_timeline_handles; + ptr =u64_to_user_ptr (wait_info->syncobj_timeline_handles); + timeline_handles =memdup_array_user (ptr, num_points,sizeof (u32)); +if (IS_ERR (timeline_handles)) { + r =PTR_ERR (timeline_handles); + goto free_syncobj_handles; + } + + ptr =u64_to_user_ptr (wait_info->syncobj_timeline_points); + timeline_points =memdup_array_user (ptr, num_points,sizeof (u32)); +if (IS_ERR (timeline_points)) { + r =PTR_ERR (timeline_points); + goto free_timeline_handles; + } + + gobj_read = kmalloc_array(num_read_bo_handles, sizeof(*gobj_read),
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, · line 1022
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +10
-0+ GFP_KERNEL); +if (!gobj_read) { + r = -ENOMEM ; + goto free_timeline_points; + } + +for (rentry =0 ; rentry < num_read_bo_handles; rentry++) { +u32 handle = bo_handles_read[rentry]; + + gobj_read[rentry] =drm_gem_object_lookup (filp, handle);
FWIW this can likely be further compacted by using drm_gem_objects_lookup as I tried in:
https://lore.kernel.org/amd-gfx/[email protected]/
and
Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +33
-0+if (!gobj_read[rentry]) { + r = -ENOENT ; + goto put_gobj_read; + } + } + + gobj_write =kmalloc_array (num_write_bo_handles,sizeof (*gobj_write), +GFP_KERNEL ); +if (!gobj_write) { + r = -ENOMEM ; + goto put_gobj_read; + } + +for (wentry =0 ; wentry < num_write_bo_handles; wentry++) { +u32 handle = bo_handles_write[wentry]; + + gobj_write[wentry] =drm_gem_object_lookup (filp, handle); +if (!gobj_write[wentry]) { + r = -ENOENT ; + goto put_gobj_write; + } + } + +/* +* Passing num_fences = 0 means that userspace doesn't want to +* retrieve userq_fence_info. If num_fences = 0 we skip filling +* userq_fence_info and return the actual number of fences on +* args->num_fences. +*/ +if (!wait_info->num_fences) { + r = amdgpu_userq_wait_count_fences(filp, wait_info, + syncobj_handles, + timeline_points,
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, · line 1065
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +10
-0+ timeline_handles, + gobj_write, + gobj_read); + }else { + r =amdgpu_userq_wait_return_fence_info (filp, wait_info, + syncobj_handles, + timeline_points, + timeline_handles, + gobj_write, + gobj_read);
This also is potentially doable without the two copies of all the loops which walk over syncobjs, syncpoints and gem handles, like in:
https://lore.kernel.org/amd-gfx/[email protected]/
Anyway, I see that you are fixing some deeper problems as well so now I understand why I got no feedback on that whole series. :)
Still, there might be some small patches there worth revisiting but I can do that when the dust settles.
Regards,
Tvrtko
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2
-0+ } + put_gobj_write:while (wentry-- >0 )drm_gem_object_put (gobj_write[wentry]);
PATCH 7/7 drm/amdgpu: make amdgpu_user_wait_ioctl more resilent
In reply to Christian KönigWhen the memory allocated by userspace isn't sufficient for all the fences then just wait on them instead of returning an error.
--- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +28 -24
@@ -734,7 +734,7 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, num_fences++; } - wait_info->num_fences = num_fences; + wait_info->num_fences =min (num_fences,USHRT_MAX ); r =0 ; error_unlock:
@@ -743,6 +743,19 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp,return r; } +static int +amdgpu_userq_wait_add_fence (struct drm_amdgpu_userq_wait *wait_info, +struct dma_fence **fences,unsigned int *num_fences, +struct dma_fence *fence) +{ +/* As fallback shouldn't userspace allocate enough space */ +if (*num_fences >= wait_info->num_fences) +return dma_fence_wait (fence, true); + + fences[(*num_fences)++] =dma_fence_get (fence); +return 0 ; +} + static int amdgpu_userq_wait_return_fence_info(struct drm_file *filp, struct drm_amdgpu_userq_wait *wait_info,
@@ -786,12 +799,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, goto free_fences;dma_fence_unwrap_for_each (f, &iter, fence) { -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) goto free_fences; - } - - fences[num_fences++] =dma_fence_get (f); }dma_fence_put (fence);
@@ -808,12 +819,11 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,if (r) goto free_fences; -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) goto free_fences; - } - fences[num_fences++] = fence;dma_fence_put (fence); }
@@ -844,13 +854,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv,DMA_RESV_USAGE_READ , fence) { -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; - goto error_unlock; - } - - fences[num_fences++] = fence; -dma_fence_get (fence); + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) + goto free_fences; } }
@@ -861,13 +868,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv,DMA_RESV_USAGE_WRITE , fence) { -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; - goto error_unlock; - } - - fences[num_fences++] = fence; -dma_fence_get (fence); + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) + goto free_fences; } }
Signature · 1 line
2.43.0
Re: [PATCH 7/7] drm/amdgpu: make amdgpu_user_wait_ioctl more resilent
Quoted 2 lines · Christian König
When the memory allocated by userspace isn't sufficient for all the fences then just wait on them instead of returning an error.
Hmm..
Quoted 10 lines
Signed-off-by Christian König <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c amdgpu_userq_wait_count_fences(struct drm_file *filp, · line 737
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-1@@ -734,7 +734,7 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, num_fences++; } - wait_info->num_fences = num_fences; + wait_info->num_fences =min (num_fences,USHRT_MAX );
Oh it is actually a weakness in the uapi with wait_info->num_fences being u16. I did not pick that from the commit message straight away.
De-dup the array when over the uapi limit, and retry?
Is it userq fences or other fences that cause the spill in practice? If the former then the patch adds a kernel wait where there wasn't one before so de-duping more aggressively could maybe limit that path.
Regards,
Tvrtko
Quoted 91 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +27
-23r =0 ; error_unlock:@@ -743,6 +743,19 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp,return r; } +static int +amdgpu_userq_wait_add_fence (struct drm_amdgpu_userq_wait *wait_info, +struct dma_fence **fences,unsigned int *num_fences, +struct dma_fence *fence) +{ +/* As fallback shouldn't userspace allocate enough space */ +if (*num_fences >= wait_info->num_fences) +return dma_fence_wait (fence, true); + + fences[(*num_fences)++] =dma_fence_get (fence); +return 0 ; +} + static int amdgpu_userq_wait_return_fence_info(struct drm_file *filp, struct drm_amdgpu_userq_wait *wait_info,@@ -786,12 +799,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, goto free_fences;dma_fence_unwrap_for_each (f, &iter, fence) { -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) goto free_fences; - } - - fences[num_fences++] =dma_fence_get (f); }dma_fence_put (fence);@@ -808,12 +819,11 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,if (r) goto free_fences; -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) goto free_fences; - } - fences[num_fences++] = fence;dma_fence_put (fence); }@@ -844,13 +854,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,dma_resv_for_each_fence (&resv_cursor, gobj_read[i]->resv,DMA_RESV_USAGE_READ , fence) { -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; - goto error_unlock; - } - - fences[num_fences++] = fence; -dma_fence_get (fence); + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) + goto free_fences; } }@@ -861,13 +868,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,dma_resv_for_each_fence (&resv_cursor, gobj_write[i]->resv,DMA_RESV_USAGE_WRITE , fence) { -if (num_fences >= wait_info->num_fences) { - r = -EINVAL ; - goto error_unlock; - } - - fences[num_fences++] = fence; -dma_fence_get (fence); + r =amdgpu_userq_wait_add_fence (wait_info, fences, + &num_fences, f); +if (r) + goto free_fences; } }
Christian König
maintainerRe: [PATCH 7/7] drm/amdgpu: make amdgpu_user_wait_ioctl more resilent
Quoted 12 lines · Tvrtko Ursulin
Quoted 2 lines · Christian König
When the memory allocated by userspace isn't sufficient for all the fences then just wait on them instead of returning an error.
Hmm..
Quoted 5 lines
Signed-off-by Christian König <[email protected]>---
.../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 52 ++++++++++---------1 file changed, 28 insertions(+), 24 deletions(-)
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c amdgpu_userq_wait_count_fences(struct drm_file *filp, · line 737
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-0@@ -734,7 +734,7 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, num_fences++; } - wait_info->num_fences = num_fences; + wait_info->num_fences = min(num_fences, USHRT_MAX);Oh it is actually a weakness in the uapi with
wait_info->num_fencesbeing u16. I did not pick that from the commit message straight away.
No, that's not a weakness. It just doesn't make sense to return so many addr/value pairs.
Quoted 1 line
De-dup the array when over the uapi limit, and retry?
No, giving back addr/value pairs to userspace is just an optimization and not for technical correctness.
We could wait for everything inside the kernel, but that inserts a CPU bubble when submissions from multiple applications depend on each other.
Quoted 1 line
Is it userq fences or other fences that cause the spill in practice? If the former then the patch adds a kernel wait where there wasn't one before so de-duping more aggressively could maybe limit that path.
This is actually not a practical issue for performance. We just need to make sure that userspace can't abuse the API to kill X/Wayland for example by giving a BO to display with tons of fences on it.
Thanks for the comments,
Christian.
Quoted 87 lines
Regards,
Tvrtko
Quoted 81 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0r = 0;
error_unlock:drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0@@ -743,6 +743,19 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, return r; }+static int
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +12
-0+amdgpu_userq_wait_add_fence(struct drm_amdgpu_userq_wait *wait_info, + struct dma_fence **fences, unsigned int *num_fences, + struct dma_fence *fence) +{ + /* As fallback shouldn't userspace allocate enough space */ + if (*num_fences >= wait_info->num_fences) + return dma_fence_wait(fence, true); + + fences[(*num_fences)++] = dma_fence_get(fence); + return 0; +} +static int amdgpu_userq_wait_return_fence_info(struct
drm_file*filp, structdrm_amdgpu_userq_wait*wait_info,drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +6
-7@@ -786,12 +799,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, goto free_fences; dma_fence_unwrap_for_each(f, &iter, fence) { - if (num_fences >= wait_info->num_fences) { - r = -EINVAL; + r = amdgpu_userq_wait_add_fence(wait_info, fences, + &num_fences, f); + if (r) goto free_fences; - } - - fences[num_fences++] = dma_fence_get(f); } dma_fence_put(fence);@@ -808,12 +819,11 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, if (r) goto free_fences; - if (num_fences >= wait_info->num_fences) { - r = -EINVAL; + r = amdgpu_userq_wait_add_fence(wait_info, fences, + &num_fences, f); + if (r) goto free_fences; - } - fences[num_fences++] = fence; dma_fence_put(fence); } @@ -844,13 +854,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp,
dma_resv_for_each_fence(&resv_cursor, gobj_read[i]->resv, DMA_RESV_USAGE_READ, fence){
- if (num_fences>=wait_info->num_fences) {
- r =-EINVAL;
- gotoerror_unlock;
- }
-
- fences[num_fences++] = fence;
-dma_fence_get(fence);
+ r = amdgpu_userq_wait_add_fence(wait_info, fences,
+&num_fences, f);
+ if (r)
+ gotofree_fences;
}
}
@@ -861,13 +868,10 @@ amdgpu_userq_wait_return_fence_info(structdrm_file*filp,dma_resv_for_each_fence(&resv_cursor, gobj_write[i]->resv, DMA_RESV_USAGE_WRITE, fence){
- if (num_fences>=wait_info->num_fences) {
- r =-EINVAL;
- gotoerror_unlock;
- }
-
- fences[num_fences++] = fence;
-dma_fence_get(fence);
+ r = amdgpu_userq_wait_add_fence(wait_info, fences,
+&num_fences, f);
+ if (r)
+ gotofree_fences;
}
}
Re: [PATCH 7/7] drm/amdgpu: make amdgpu_user_wait_ioctl more resilent
Quoted 25 lines · Christian König
Quoted 12 lines · Tvrtko Ursulin
Quoted 2 lines · Christian König
When the memory allocated by userspace isn't sufficient for all the fences then just wait on them instead of returning an error.
Hmm..
Quoted 5 lines
Signed-off-by Christian König <[email protected]>--- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-)Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c amdgpu_userq_wait_count_fences(struct drm_file *filp, · line 737
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-0@@ -734,7 +734,7 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, num_fences++; } - wait_info->num_fences = num_fences; + wait_info->num_fences = min(num_fences, USHRT_MAX);Oh it is actually a weakness in the uapi with
wait_info->num_fencesbeing u16. I did not pick that from the commit message straight away.
Quoted 1 line
No, that's not a weakness. It just doesn't make sense to return so many addr/value pairs.
Okay I couldn't tell from the commit message if this was hit in testing or just pre-emptively prevented by the patch.
And maybe not a weakness in the uapi per se, but a weakness in the combination of the uapi and the implementation. What I mean here that the 1st pass of the uapi works on the exploded fence view, while the 2nd pass works on the per-context view, but the implementation of the 2nd pass relies on the temporary storage sized by the 1st, which is therefore an irrelevant number from the _uapi_ point of view.
Quoted 9 lines
Quoted 1 line
De-dup the array when over the uapi limit, and retry?
No, giving back addr/value pairs to userspace is just an optimization and not for technical correctness.
We could wait for everything inside the kernel, but that inserts a CPU bubble when submissions from multiple applications depend on each other.
Quoted 1 line
Is it userq fences or other fences that cause the spill in practice? If the former then the patch adds a kernel wait where there wasn't one before so de-duping more aggressively could maybe limit that path.
This is actually not a practical issue for performance. We just need to make sure that userspace can't abuse the API to kill X/Wayland for example by giving a BO to display with tons of fences on it.
Let me try to better explain what I meant. Considering the current flow is like this:
1. Enumerate
2. De-dupe
3. a) Pass userq fences to userspace
b) Wait on other fences in kernel
This patch adds (theoretical) kernel waits on *any* fence to the "1. Enumerate" stage even if the fence ends up being discarded in the de-dupe pass, and regardless of the fence type/owner.
I suppose my suggestion was just to add a de-dup pass to the out of space branch. Along the lines of:
static int
amdgpu_userq_wait_add_fence(struct drm_amdgpu_userq_wait *wait_info,
struct dma_fence **fences, unsigned int *num_fences, struct dma_fence *fence)
{
/* As fallback shouldn't userspace allocate enough space */
if (*num_fences >= wait_info->num_fences)
*num_fences = dma_fence_dedup_array(fences, *num_fences);
if (*num_fences >= wait_info->num_fences)
return dma_fence_wait(fence, true);
}
fences[(*num_fences)++] = dma_fence_get(fence);
return 0;
}But I agree, if the expectation is the overflow if purely theoretical then it doesn't really matter.
Regards,
Tvrtko
Quoted 83 lines
Quoted 82 lines
Quoted 81 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +26
-21r = 0; error_unlock:@@ -743,6 +743,19 @@ amdgpu_userq_wait_count_fences(struct drm_file *filp, return r; } +static int +amdgpu_userq_wait_add_fence (struct drm_amdgpu_userq_wait *wait_info, + struct dma_fence **fences, unsigned int *num_fences, + struct dma_fence *fence) +{ + /* As fallback shouldn't userspace allocate enough space */ + if (*num_fences >= wait_info->num_fences) + return dma_fence_wait(fence, true); + + fences[(*num_fences)++] = dma_fence_get(fence); + return 0; +} + static int amdgpu_userq_wait_return_fence_info(struct drm_file *filp, struct drm_amdgpu_userq_wait *wait_info,@@ -786,12 +799,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, goto free_fences; dma_fence_unwrap_for_each(f, &iter, fence) { - if (num_fences >= wait_info->num_fences) { - r = -EINVAL; + r = amdgpu_userq_wait_add_fence(wait_info, fences, + &num_fences, f); + if (r) goto free_fences; - } - - fences[num_fences++] = dma_fence_get(f); } dma_fence_put(fence);@@ -808,12 +819,11 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, if (r) goto free_fences; - if (num_fences >= wait_info->num_fences) { - r = -EINVAL; + r = amdgpu_userq_wait_add_fence(wait_info, fences, + &num_fences, f); + if (r) goto free_fences; - } - fences[num_fences++] = fence; dma_fence_put(fence); } @@ -844,13 +854,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, dma_resv_for_each_fence(&resv_cursor, gobj_read[i]->resv, DMA_RESV_USAGE_READ, fence) { - if (num_fences >= wait_info->num_fences) { - r = -EINVAL; - goto error_unlock; - } - - fences[num_fences++] = fence; - dma_fence_get(fence); + r = amdgpu_userq_wait_add_fence(wait_info, fences, + &num_fences, f); + if (r) + goto free_fences; } } @@ -861,13 +868,10 @@ amdgpu_userq_wait_return_fence_info(struct drm_file *filp, dma_resv_for_each_fence(&resv_cursor, gobj_write[i]->resv, DMA_RESV_USAGE_WRITE, fence) { - if (num_fences >= wait_info->num_fences) { - r = -EINVAL; - goto error_unlock; - } - - fences[num_fences++] = fence; - dma_fence_get(fence); + r = amdgpu_userq_wait_add_fence(wait_info, fences, + &num_fences, f); + if (r) + goto free_fences; } }