IDR to xarray conversion and some small cleanups
Subsystems (2)
v3 cover IDR to xarray conversion and some small cleanups
Context conversion patches need a lot of scrutiny (and passing through the automated test suites), while the bo list changes are perhaps a bit of a bike shed, although they do result in less lines of code and hopefully easier to read.
BO list sorting is really independent, as is the IDR MES cleanup (which could as well have the warnings removed). Just say the work and I can easily split them out.
Series has been smoke tested, including lockdep and game + desktop workloads so at least shouldn't be too obviously broken.
v2:
* Re-order to pull reviewed to the head.
* Added more patches, some suggested by Christian, some I spotted new issues or cleanup opportunities.
* No need for kref_get_unless_zero when under the xa_lock.
* RCU is no longer needed for bo list free.
v3:
* Handle NULL bo list in amdgpu_bo_list_put.
* Use separate variable for drm_dev_enter for readability.
* Applied r-b's received so far.
Tvrtko Ursulin (12):
drm/amdgpu/mes: Remove idr leftovers
drm/amdgpu: Simplify sorting of the bo list
drm/amdgpu: Drop support for variable struct drm_amdgpu_bo_list_entry
size
drm/amdgpu: Remove the bo list mutex
drm/amdgpu: Remove a few holes from struct amdgpu_ctx drm/amdgpu: Remove duplicate struct member
drm/amdgpu: Remove arbitrary number of contexts limitation drm/amdgpu: Consolidate ctx put
drm/amdgpu: Remove live context error log and skip drm/amdgpu: Replace idr with xarray in amdgpu_bo_list drm/amdgpu: Remove output parameter in bo list handling drm/amdgpu: Convert context manager to xarray
drivers/gpu/drm/amd/amdgpu/amdgpu.h | 5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 179 +++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 22 +- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 67 +++--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 221 ++++++-------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 27 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 11 +- drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 9 - drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 3 - drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 8 +- drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c | 18 +- drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c | 18 +- 12 files changed, 206 insertions(+), 382 deletions(-)
Signature · 1 line
2.52.0
PATCH v3 1/12 drm/amdgpu/mes: Remove idr leftovers
Commitcb17fff3a254 ("drm/amdgpu/mes: remove unused functions") removed most of the code using these IDRs but forgot to remove the struct members and init/destroy paths.
There is also interrupt handling code in SDMA 5.0 and 5.2 which appears to be using it, but is is unreachable since nothing ever allocates the relevant IDR. We replace those with one time warnings just to avoid any functional difference, but it is also possible they should be removed.
References: cb17fff3a254 ("drm/amdgpu/mes: remove unused functions")
--- drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 9 --------- drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 3 --- drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c | 18 +++--------------- drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c | 18 +++--------------- 4 files changed, 6 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +0 -9
@@ -94,9 +94,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev) adev->mes.adev = adev; -idr_init (&adev->mes.pasid_idr); -idr_init (&adev->mes.gang_id_idr); -idr_init (&adev->mes.queue_id_idr);ida_init (&adev->mes.doorbell_ida);spin_lock_init (&adev->mes.queue_id_lock);mutex_init (&adev->mes.mutex_hidden);
@@ -218,9 +215,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev) adev->mes.query_status_fence_offs[i]); } -idr_destroy (&adev->mes.pasid_idr); -idr_destroy (&adev->mes.gang_id_idr); -idr_destroy (&adev->mes.queue_id_idr);ida_destroy (&adev->mes.doorbell_ida);mutex_destroy (&adev->mes.mutex_hidden);return r;
@@ -248,9 +242,6 @@ void amdgpu_mes_fini(struct amdgpu_device *adev)amdgpu_mes_doorbell_free (adev); -idr_destroy (&adev->mes.pasid_idr); -idr_destroy (&adev->mes.gang_id_idr); -idr_destroy (&adev->mes.queue_id_idr);ida_destroy (&adev->mes.doorbell_ida);mutex_destroy (&adev->mes.mutex_hidden); }
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +0 -3
@@ -68,9 +68,6 @@ struct amdgpu_mes {struct mutex mutex_hidden; -struct idr pasid_idr; -struct idr gang_id_idr; -struct idr queue_id_idr;struct ida doorbell_ida;spinlock_t queue_id_lock;
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +3 -15
@@ -1704,24 +1704,12 @@ static int sdma_v5_0_process_trap_irq(struct amdgpu_device *adev, struct amdgpu_irq_src *source, struct amdgpu_iv_entry *entry) { - uint32_t mes_queue_id = entry->src_data[0]; - DRM_DEBUG("IH: SDMA trap\n"); -if (adev->enable_mes && (mes_queue_id &AMDGPU_FENCE_MES_QUEUE_FLAG )) { -struct amdgpu_mes_queue *queue; - - mes_queue_id &=AMDGPU_FENCE_MES_QUEUE_ID_MASK ; - -spin_lock (&adev->mes.queue_id_lock); - queue =idr_find (&adev->mes.queue_id_idr, mes_queue_id); -if (queue) { -DRM_DEBUG ("process smda queue id = %d\n" , mes_queue_id); -amdgpu_fence_process (queue->ring); - } -spin_unlock (&adev->mes.queue_id_lock); +if (drm_WARN_ON_ONCE (&adev->ddev, + adev->enable_mes && + (entry->src_data[0 ] &AMDGPU_FENCE_MES_QUEUE_FLAG )))return 0 ; - }switch (entry->client_id) {case SOC15_IH_CLIENTID_SDMA0 :
drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +3 -15
@@ -1617,24 +1617,12 @@ static int sdma_v5_2_process_trap_irq(struct amdgpu_device *adev, struct amdgpu_irq_src *source, struct amdgpu_iv_entry *entry) { - uint32_t mes_queue_id = entry->src_data[0]; - DRM_DEBUG("IH: SDMA trap\n"); -if (adev->enable_mes && (mes_queue_id &AMDGPU_FENCE_MES_QUEUE_FLAG )) { -struct amdgpu_mes_queue *queue; - - mes_queue_id &=AMDGPU_FENCE_MES_QUEUE_ID_MASK ; - -spin_lock (&adev->mes.queue_id_lock); - queue =idr_find (&adev->mes.queue_id_idr, mes_queue_id); -if (queue) { -DRM_DEBUG ("process smda queue id = %d\n" , mes_queue_id); -amdgpu_fence_process (queue->ring); - } -spin_unlock (&adev->mes.queue_id_lock); +if (drm_WARN_ON_ONCE (&adev->ddev, + adev->enable_mes && + (entry->src_data[0 ] &AMDGPU_FENCE_MES_QUEUE_FLAG )))return 0 ; - }switch (entry->client_id) {case SOC15_IH_CLIENTID_SDMA0 :
Signature · 1 line
2.52.0
PATCH v3 2/12 drm/amdgpu: Simplify sorting of the bo list
In reply to Tvrtko UrsulinSort function only cares about the sign so we can replace the conditionals with a single subtraction.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -5
@@ -60,11 +60,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b) {const struct amdgpu_bo_list_entry *a = _a, *b = _b; -if (a->priority > b->priority) -return 1 ; -if (a->priority < b->priority) -return -1 ; -return 0 ; +BUILD_BUG_ON (AMDGPU_BO_LIST_MAX_PRIORITY >=INT_MAX ); + +return (int )a->priority - (int )b->priority; } int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
Signature · 1 line
2.52.0
PATCH v3 3/12 drm/amdgpu: Drop support for variable struct drm_amdgpu_bo_list_entry size
In reply to Tvrtko UrsulinUserspace always uses struct drm_amdgpu_bo_list_in->bo_info_size equal to sizeof(struct drm_amdgpu_bo_list_entry) and there are no plans to extend it.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 31 +++++---------------- 1 file changed, 7 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +7 -24
@@ -182,33 +182,16 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in,struct drm_amdgpu_bo_list_entry **info_param) { -const uint32_t info_size =sizeof (struct drm_amdgpu_bo_list_entry ); -const void __user *uptr =u64_to_user_ptr (in->bo_info_ptr); -const uint32_t bo_info_size = in->bo_info_size; -const uint32_t bo_number = in->bo_number;struct drm_amdgpu_bo_list_entry *info; -/* copy the handle array from userspace to a kernel buffer */ -if (likely (info_size == bo_info_size)) { - info =vmemdup_array_user (uptr, bo_number, info_size); -if (IS_ERR (info)) -return PTR_ERR (info); - }else { -const uint32_t bytes =min (bo_info_size, info_size); -unsigned i; +if (in->bo_info_size !=sizeof (struct drm_amdgpu_bo_list_entry )) +return -EINVAL ; - info =kvmalloc_array (bo_number, info_size,GFP_KERNEL ); -if (!info) -return -ENOMEM ; - -memset (info,0 , bo_number * info_size); -for (i =0 ; i < bo_number; ++i, uptr += bo_info_size) { -if (copy_from_user (&info[i], uptr, bytes)) { -kvfree (info); -return -EFAULT ; - } - } - } + info =vmemdup_array_user (u64_to_user_ptr (in->bo_info_ptr), + in->bo_number, +sizeof (struct drm_amdgpu_bo_list_entry )); +if (IS_ERR (info)) +return PTR_ERR (info); *info_param = info;return 0 ;
Signature · 1 line
2.52.0
Christian König
maintainerRe: [PATCH v3 03/12] drm/amdgpu: Drop support for variable struct drm_amdgpu_bo_list_entry size
Quoted 3 lines · Tvrtko Ursulin
Userspace always uses struct
drm_amdgpu_bo_list_in->bo_info_sizeequal tosizeof(struct drm_amdgpu_bo_list_entry)and there are no plans to extend it.
Rather write something like ..."even if the structure is extended at some point older kernels should note that they don't support additional fields by rejecting the new structure size.".
Quoted 3 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Suggested-by Christian König <[email protected]>
With the commit message clarified a bit Reviewed-by: Christian König <[email protected]>
Quoted 49 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 31 +++++---------------- 1 file changed, 7 insertions(+), 24 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +7
-24@@ -182,33 +182,16 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in,struct drm_amdgpu_bo_list_entry **info_param) { -const uint32_t info_size =sizeof (struct drm_amdgpu_bo_list_entry ); -const void __user *uptr =u64_to_user_ptr (in->bo_info_ptr); -const uint32_t bo_info_size = in->bo_info_size; -const uint32_t bo_number = in->bo_number;struct drm_amdgpu_bo_list_entry *info; -/* copy the handle array from userspace to a kernel buffer */ -if (likely (info_size == bo_info_size)) { - info =vmemdup_array_user (uptr, bo_number, info_size); -if (IS_ERR (info)) -return PTR_ERR (info); - }else { -const uint32_t bytes =min (bo_info_size, info_size); -unsigned i; +if (in->bo_info_size !=sizeof (struct drm_amdgpu_bo_list_entry )) +return -EINVAL ; - info =kvmalloc_array (bo_number, info_size,GFP_KERNEL ); -if (!info) -return -ENOMEM ; - -memset (info,0 , bo_number * info_size); -for (i =0 ; i < bo_number; ++i, uptr += bo_info_size) { -if (copy_from_user (&info[i], uptr, bytes)) { -kvfree (info); -return -EFAULT ; - } - } - } + info =vmemdup_array_user (u64_to_user_ptr (in->bo_info_ptr), + in->bo_number, +sizeof (struct drm_amdgpu_bo_list_entry )); +if (IS_ERR (info)) +return PTR_ERR (info); *info_param = info;return 0 ;
PATCH v3 4/12 drm/amdgpu: Remove the bo list mutex
In reply to Tvrtko UrsulinThe bo list is immutable during command submission since the drm_exec conversion so we can remove the mutex.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 3 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 4 ---- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 15 ++++----------- 3 files changed, 5 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -2
@@ -41,7 +41,7 @@ static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu) { struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list, rhead); -mutex_destroy (&list->bo_list_mutex); +kvfree (list); }
@@ -133,7 +133,6 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,trace_amdgpu_cs_bo_status (list->num_entries, total_size); -mutex_init (&list->bo_list_mutex); *result = list;return 0 ;
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +0 -4
@@ -51,10 +51,6 @@ struct amdgpu_bo_list {unsigned first_userptr;unsigned num_entries; -/* Protect access during command submission. -*/ -struct mutex bo_list_mutex; - struct amdgpu_bo_list_entry entries[] __counted_by(num_entries); };
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +4 -11
@@ -874,8 +874,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,return r; } -mutex_lock (&p->bo_list->bo_list_mutex); -/* Get userptr backing pages. If pages are updated after registered * in amdgpu_gem_userptr_ioctl(), amdgpu_cs_list_validate() will do * amdgpu_ttm_backend_bind() to flush and invalidate new pages
@@ -991,7 +989,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,amdgpu_hmm_range_free (e->range); e->range =NULL ; } -mutex_unlock (&p->bo_list->bo_list_mutex);return r; }
@@ -1372,7 +1369,6 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,amdgpu_vm_move_to_lru_tail (p->adev, &fpriv->vm);mutex_unlock (&p->adev->notifier_lock); -mutex_unlock (&p->bo_list->bo_list_mutex);return 0 ; }
@@ -1444,28 +1440,25 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) r =amdgpu_cs_patch_jobs (&parser);if (r) - goto error_backoff; + goto error_fini; r =amdgpu_cs_vm_handling (&parser);if (r) - goto error_backoff; + goto error_fini; r =amdgpu_cs_sync_rings (&parser);if (r) - goto error_backoff; + goto error_fini;trace_amdgpu_cs_ibs (&parser); r =amdgpu_cs_submit (&parser, data);if (r) - goto error_backoff; + goto error_fini;amdgpu_cs_parser_fini (&parser);return 0 ; -error_backoff: -mutex_unlock (&parser.bo_list->bo_list_mutex); - error_fini:amdgpu_cs_parser_fini (&parser);return r;
Signature · 1 line
2.52.0
PATCH v3 5/12 drm/amdgpu: Remove a few holes from struct amdgpu_ctx
In reply to Tvrtko UrsulinRe-order the struct members a bit to avoid some holes:
/* size: 408, cachelines: 7, members: 15 */
/* sum members: 393, holes: 4, sum holes: 15 */
/* last cacheline: 24 bytes */
/* size: 400, cachelines: 7, members: 15 */
/* sum members: 393, holes: 1, sum holes: 7 */
/* last cacheline: 16 bytes */
While doing so we notice a duplicate but will address than in the following patch.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +6 -6
@@ -44,20 +44,20 @@ struct amdgpu_ctx_entity {struct amdgpu_ctx {struct kref refcount; -struct amdgpu_ctx_mgr *mgr; +spinlock_t ring_lock;unsigned reset_counter;unsigned reset_counter_query; -uint64_t generation; -spinlock_t ring_lock; -struct amdgpu_ctx_entity *entities[AMDGPU_HW_IP_NUM ][AMDGPU_MAX_ENTITY_NUM ]; -bool preamble_presented;int32_t init_priority;int32_t override_priority; +uint32_t stable_pstate;atomic_t guilty; +bool preamble_presented; +uint64_t generation;unsigned long ras_counter_ce;unsigned long ras_counter_ue; -uint32_t stable_pstate; +struct amdgpu_ctx_mgr *mgr;struct amdgpu_ctx_mgr *ctx_mgr; +struct amdgpu_ctx_entity *entities[AMDGPU_HW_IP_NUM ][AMDGPU_MAX_ENTITY_NUM ]; };struct amdgpu_ctx_mgr {
Signature · 1 line
2.52.0
PATCH v3 6/12 drm/amdgpu: Remove duplicate struct member
In reply to Tvrtko UrsulinStruct amdgpu_ctx contains two copies of the pointer to the context manager. Remove one.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -2
@@ -232,7 +232,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, }else {struct amdgpu_fpriv *fpriv; - fpriv = container_of(ctx->ctx_mgr, struct amdgpu_fpriv, ctx_mgr); + fpriv = container_of(ctx->mgr, struct amdgpu_fpriv, ctx_mgr); r =amdgpu_xcp_select_scheds (adev, hw_ip, hw_prio, fpriv, &num_scheds, &scheds);if (r)
@@ -354,7 +354,6 @@ static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,else ctx->stable_pstate = current_stable_pstate; - ctx->ctx_mgr = &(fpriv->ctx_mgr);return 0 ; }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -1
@@ -56,7 +56,6 @@ struct amdgpu_ctx {unsigned long ras_counter_ce;unsigned long ras_counter_ue;struct amdgpu_ctx_mgr *mgr; -struct amdgpu_ctx_mgr *ctx_mgr;struct amdgpu_ctx_entity *entities[AMDGPU_HW_IP_NUM ][AMDGPU_MAX_ENTITY_NUM ]; };
Signature · 1 line
2.52.0
Christian König
maintainerRe: [PATCH v3 06/12] drm/amdgpu: Remove duplicate struct member
Quoted 14 lines · Tvrtko Ursulin
Struct
amdgpu_ctxcontains two copies of the pointer to the context manager. Remove one.Signed-off-by Tvrtko Ursulin <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 3 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 235
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1
-1@@ -232,7 +232,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, }else {struct amdgpu_fpriv *fpriv; - fpriv = container_of(ctx->ctx_mgr, struct amdgpu_fpriv, ctx_mgr); + fpriv = container_of(ctx->mgr, struct amdgpu_fpriv, ctx_mgr);
Please add "/* TODO: stop using fpriv here, we only need the xcp_id */".
With that done Reviewed-by: Christian König <[email protected]>
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-1r =amdgpu_xcp_select_scheds (adev, hw_ip, hw_prio, fpriv, &num_scheds, &scheds);if (r)@@ -354,7 +354,6 @@ static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,else ctx->stable_pstate = current_stable_pstate; - ctx->ctx_mgr = &(fpriv->ctx_mgr);return 0 ; }drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0
-1@@ -56,7 +56,6 @@ struct amdgpu_ctx {unsigned long ras_counter_ce;unsigned long ras_counter_ue;struct amdgpu_ctx_mgr *mgr; -struct amdgpu_ctx_mgr *ctx_mgr;struct amdgpu_ctx_entity *entities[AMDGPU_HW_IP_NUM ][AMDGPU_MAX_ENTITY_NUM ]; };
PATCH v3 7/12 drm/amdgpu: Remove arbitrary number of contexts limitation
In reply to Tvrtko UrsulinThere is no need for an arbitrary limit to number of contexts userspace can be allowed to create. Remove the AMDGPU_VM_MAX_NUM_CTX (4096) and allow for full 32-bit of handles to be allocated.
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 - drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -1
@@ -274,7 +274,6 @@ extern int amdgpu_rebar;extern int amdgpu_wbrf;extern int amdgpu_user_queue; -#define AMDGPU_VM_MAX_NUM_CTX 4096#define AMDGPU_SG_THRESHOLD (256*1024*1024)#define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS 3000#define AMDGPU_MAX_USEC_TIMEOUT 100000/* 100 ms */
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -3
@@ -489,14 +489,14 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,return -ENOMEM ;mutex_lock (&mgr->lock); - r =idr_alloc (&mgr->ctx_handles, ctx,1 ,AMDGPU_VM_MAX_NUM_CTX ,GFP_KERNEL ); -if (r <0 ) { + *id =1 ; + r =idr_alloc_u32 (&mgr->ctx_handles, ctx, id,UINT_MAX ,GFP_KERNEL ); +if (r) {mutex_unlock (&mgr->lock);kfree (ctx);return r; } - *id = (uint32_t )r; r =amdgpu_ctx_init (mgr, priority, filp, ctx);if (r) {idr_remove (&mgr->ctx_handles, *id);
Signature · 1 line
2.52.0
PATCH v3 8/12 drm/amdgpu: Consolidate ctx put
In reply to Tvrtko UrsulinCurrently there are two flavours of the context reference count destructor:
- amdgpu_ctx_do_release(), used from kref_put from places where the code thinks context may have been used, or is in active use, and;
- amdgpu_ctx_fini(), used when code is sure context entities have already been idled.
Since amdgpu_ctx_do_release() calls amdgpu_ctx_fini() after having idled and destroyed the scheduler entities, we can consolidate the two into a single function.
Functional difference is that now drm_sched_entity_destroy() is called on context manager shutdown (file close), where previously it was drm_sched_entity_fini(). But the former is a superset of the latter, and during file close the flush method is also called, which calls drm_sched_entity_flush(), which is also called by drm_sched_entity_destroy(). And as it is safe to attempt to flush a never used entity, or flush it twice, there is actually no functional change.
---
v2:
* Use separate variable for drm_dev_enter for readability.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 99 +++++++------------------ drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 9 ++- 2 files changed, 36 insertions(+), 72 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +28 -71
@@ -274,26 +274,6 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,return r; } -static ktime_t amdgpu_ctx_fini_entity (struct amdgpu_device *adev, -struct amdgpu_ctx_entity *entity) -{ -ktime_t res =ns_to_ktime (0 ); -int i; - -if (!entity) -return res; - -for (i =0 ; i < amdgpu_sched_jobs; ++i) { - res =ktime_add (res,amdgpu_ctx_fence_time (entity->fences[i])); -dma_fence_put (entity->fences[i]); - } - -amdgpu_xcp_release_sched (adev, entity); - -kfree (entity); -return res; -} -static int amdgpu_ctx_get_stable_pstate (struct amdgpu_ctx *ctx,u32 *stable_pstate) {
@@ -408,22 +388,41 @@ static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,return r; } -static void amdgpu_ctx_fini (struct kref *ref) +void amdgpu_ctx_fini (struct kref *ref) { struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);struct amdgpu_ctx_mgr *mgr = ctx->mgr;struct amdgpu_device *adev = mgr->adev; -unsigned i, j, idx; +int i, j, k, idx;if (!adev)return ;for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) {for (j =0 ; j <AMDGPU_MAX_ENTITY_NUM ; ++j) { -ktime_t spend; +struct amdgpu_ctx_entity *entity = ctx->entities[i][j]; +ktime_t t =ns_to_ktime (0 ); - spend =amdgpu_ctx_fini_entity (adev, ctx->entities[i][j]); -atomic64_add (ktime_to_ns (spend), &mgr->time_spend[i]); +if (!entity) +continue ; + +drm_sched_entity_destroy (&entity->entity); + +for (k =0 ; k < amdgpu_sched_jobs; k++) { +struct dma_fence *fence = entity->fences[k]; + +if (!fence) +continue ; + + t =ktime_add (t,amdgpu_ctx_fence_time (fence)); +dma_fence_put (fence); + } + +amdgpu_xcp_release_sched (adev, entity); + +kfree (entity); + +atomic64_add (ktime_to_ns (t), &mgr->time_spend[i]); } }
@@ -507,24 +506,6 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,return r; } -static void amdgpu_ctx_do_release (struct kref *ref) -{ -struct amdgpu_ctx *ctx; -u32 i, j; - - ctx = container_of(ref, struct amdgpu_ctx, refcount); -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -if (!ctx->entities[i][j]) -continue ; - -drm_sched_entity_destroy (&ctx->entities[i][j]->entity); - } - } - -amdgpu_ctx_fini (ref); -} -static int amdgpu_ctx_free (struct amdgpu_fpriv *fpriv,uint32_t id) {struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
@@ -532,8 +513,7 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)mutex_lock (&mgr->lock); ctx =idr_remove (&mgr->ctx_handles, id); -if (ctx) -kref_put (&ctx->refcount, amdgpu_ctx_do_release); +amdgpu_ctx_put (ctx);mutex_unlock (&mgr->lock);return ctx ?0 : -EINVAL ; }
@@ -749,15 +729,6 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)return ctx; } -int amdgpu_ctx_put (struct amdgpu_ctx *ctx) -{ -if (ctx ==NULL ) -return -EINVAL ; - -kref_put (&ctx->refcount, amdgpu_ctx_do_release); -return 0 ; -} -uint64_t amdgpu_ctx_add_fence (struct amdgpu_ctx *ctx,struct drm_sched_entity *entity,struct dma_fence *fence)
@@ -926,29 +897,15 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)static void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr) {struct amdgpu_ctx *ctx; -struct idr *idp; -uint32_t id, i, j; +uint32_t id; - idp = &mgr->ctx_handles; - -idr_for_each_entry (idp, ctx, id) { +idr_for_each_entry (&mgr->ctx_handles, ctx, id) {if (kref_read (&ctx->refcount) !=1 ) {DRM_ERROR ("ctx %p is still alive\n" , ctx);continue ; } -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -struct drm_sched_entity *entity; - -if (!ctx->entities[i][j]) -continue ; - - entity = &ctx->entities[i][j]->entity; -drm_sched_entity_fini (entity); - } - } -kref_put (&ctx->refcount, amdgpu_ctx_fini); +amdgpu_ctx_put (ctx); } }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +8 -1
@@ -70,7 +70,14 @@ struct amdgpu_ctx_mgr {extern const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM ];struct amdgpu_ctx *amdgpu_ctx_get (struct amdgpu_fpriv *fpriv,uint32_t id); -int amdgpu_ctx_put (struct amdgpu_ctx *ctx); + +void amdgpu_ctx_fini (struct kref *kref); + +static inline void amdgpu_ctx_put (struct amdgpu_ctx *ctx) +{ +if (ctx) +kref_put (&ctx->refcount, amdgpu_ctx_fini); +}int amdgpu_ctx_get_entity (struct amdgpu_ctx *ctx,u32 hw_ip,u32 instance,u32 ring,struct drm_sched_entity **entity);
Signature · 1 line
2.52.0
Christian König
maintainerRe: [PATCH v3 08/12] drm/amdgpu: Consolidate ctx put
Quoted 48 lines · Tvrtko Ursulin
Currently there are two flavours of the context reference count destructor:
-
amdgpu_ctx_do_release(), used fromkref_putfrom places where the code thinks context may have been used, or is in active use, and;
-amdgpu_ctx_fini(), used when code is sure context entities have already been idled.Since
amdgpu_ctx_do_release()callsamdgpu_ctx_fini()after having idled and destroyed the scheduler entities, we can consolidate the two into a single function.Functional difference is that now
drm_sched_entity_destroy()is called on context manager shutdown (file close), where previously it wasdrm_sched_entity_fini(). But the former is a superset of the latter, and during file close the flush method is also called, which callsdrm_sched_entity_flush(), which is also called bydrm_sched_entity_destroy(). And as it is safe to attempt to flush a never used entity, or flush it twice, there is actually no functional change.Signed-off-by Tvrtko Ursulin <[email protected]>Suggested-by Christian König <[email protected]>---
v2:
* Use separate variable fordrm_dev_enterfor readability.--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 99 +++++++------------------ drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 9 ++- 2 files changed, 36 insertions(+), 72 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-10@@ -274,26 +274,6 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,return r; } -static ktime_t amdgpu_ctx_fini_entity (struct amdgpu_device *adev, -struct amdgpu_ctx_entity *entity) -{ -ktime_t res =ns_to_ktime (0 ); -int i; - -if (!entity) -return res; - -for (i =0 ; i < amdgpu_sched_jobs; ++i) {
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 296
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0
-10- res =ktime_add (res,amdgpu_ctx_fence_time (entity->fences[i])); -dma_fence_put (entity->fences[i]); - } - -amdgpu_xcp_release_sched (adev, entity); - -kfree (entity); -return res; -} -
Thinking more about it it probably better to keep this function separated even when it is only used once, e.g. just to have amdgpu_ctx_init_entity() and amdgpu_ctx_fini_entity() properly paired.
Apart from that looks good to me.
Regards,
Christian.
Quoted 154 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +28
-51static int amdgpu_ctx_get_stable_pstate (struct amdgpu_ctx *ctx,u32 *stable_pstate) {@@ -408,22 +388,41 @@ static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,return r; } -static void amdgpu_ctx_fini (struct kref *ref) +void amdgpu_ctx_fini (struct kref *ref) { struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);struct amdgpu_ctx_mgr *mgr = ctx->mgr;struct amdgpu_device *adev = mgr->adev; -unsigned i, j, idx; +int i, j, k, idx;if (!adev)return ;for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) {for (j =0 ; j <AMDGPU_MAX_ENTITY_NUM ; ++j) { -ktime_t spend; +struct amdgpu_ctx_entity *entity = ctx->entities[i][j]; +ktime_t t =ns_to_ktime (0 ); - spend =amdgpu_ctx_fini_entity (adev, ctx->entities[i][j]); -atomic64_add (ktime_to_ns (spend), &mgr->time_spend[i]); +if (!entity) +continue ; + +drm_sched_entity_destroy (&entity->entity); + +for (k =0 ; k < amdgpu_sched_jobs; k++) { +struct dma_fence *fence = entity->fences[k]; + +if (!fence) +continue ; + + t =ktime_add (t,amdgpu_ctx_fence_time (fence)); +dma_fence_put (fence); + } + +amdgpu_xcp_release_sched (adev, entity); + +kfree (entity); + +atomic64_add (ktime_to_ns (t), &mgr->time_spend[i]); } }@@ -507,24 +506,6 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,return r; } -static void amdgpu_ctx_do_release (struct kref *ref) -{ -struct amdgpu_ctx *ctx; -u32 i, j; - - ctx = container_of(ref, struct amdgpu_ctx, refcount); -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -if (!ctx->entities[i][j]) -continue ; - -drm_sched_entity_destroy (&ctx->entities[i][j]->entity); - } - } - -amdgpu_ctx_fini (ref); -} -static int amdgpu_ctx_free (struct amdgpu_fpriv *fpriv,uint32_t id) {struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;@@ -532,8 +513,7 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)mutex_lock (&mgr->lock); ctx =idr_remove (&mgr->ctx_handles, id); -if (ctx) -kref_put (&ctx->refcount, amdgpu_ctx_do_release); +amdgpu_ctx_put (ctx);mutex_unlock (&mgr->lock);return ctx ?0 : -EINVAL ; }@@ -749,15 +729,6 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)return ctx; } -int amdgpu_ctx_put (struct amdgpu_ctx *ctx) -{ -if (ctx ==NULL ) -return -EINVAL ; - -kref_put (&ctx->refcount, amdgpu_ctx_do_release); -return 0 ; -} -uint64_t amdgpu_ctx_add_fence (struct amdgpu_ctx *ctx,struct drm_sched_entity *entity,struct dma_fence *fence)@@ -926,29 +897,15 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)static void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr) {struct amdgpu_ctx *ctx; -struct idr *idp; -uint32_t id, i, j; +uint32_t id; - idp = &mgr->ctx_handles; - -idr_for_each_entry (idp, ctx, id) { +idr_for_each_entry (&mgr->ctx_handles, ctx, id) {if (kref_read (&ctx->refcount) !=1 ) {DRM_ERROR ("ctx %p is still alive\n" , ctx);continue ; } -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -struct drm_sched_entity *entity; - -if (!ctx->entities[i][j]) -continue ; - - entity = &ctx->entities[i][j]->entity; -drm_sched_entity_fini (entity); - } - } -kref_put (&ctx->refcount, amdgpu_ctx_fini); +amdgpu_ctx_put (ctx); } }drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +8
-1@@ -70,7 +70,14 @@ struct amdgpu_ctx_mgr {extern const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM ];struct amdgpu_ctx *amdgpu_ctx_get (struct amdgpu_fpriv *fpriv,uint32_t id); -int amdgpu_ctx_put (struct amdgpu_ctx *ctx); + +void amdgpu_ctx_fini (struct kref *kref); + +static inline void amdgpu_ctx_put (struct amdgpu_ctx *ctx) +{ +if (ctx) +kref_put (&ctx->refcount, amdgpu_ctx_fini); +}int amdgpu_ctx_get_entity (struct amdgpu_ctx *ctx,u32 hw_ip,u32 instance,u32 ring,struct drm_sched_entity **entity);
PATCH v3 9/12 drm/amdgpu: Remove live context error log and skip
In reply to Tvrtko UrsulinAccording to Christian the skip can only cause memory leaks if it would to trigger, while it does nothing for the fact context manager will still get zapped with live back references from dangling contexts.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -7
@@ -899,14 +899,8 @@ static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)struct amdgpu_ctx *ctx;uint32_t id; -idr_for_each_entry (&mgr->ctx_handles, ctx, id) { -if (kref_read (&ctx->refcount) !=1 ) { -DRM_ERROR ("ctx %p is still alive\n" , ctx); -continue ; - } - +idr_for_each_entry (&mgr->ctx_handles, ctx, id)amdgpu_ctx_put (ctx); - } }void amdgpu_ctx_mgr_fini (struct amdgpu_ctx_mgr *mgr)
Signature · 1 line
2.52.0
PATCH v3 10/12 drm/amdgpu: Replace idr with xarray in amdgpu_bo_list
In reply to Tvrtko UrsulinIDR is deprecated so let's replace it with xarray.
Conversion is mostly 1:1 apart from AMDGPU_BO_LIST_OP_UPDATE which was implemented with idr_replace, and has now been replaced with a sequence of xa_load and xa_cmpxchg. Should userspace attempt multi-threaded update operations on the same handle it could theoretically hit a new -ENOENT path. But I believe this is purely theoretical and still safe.
Also, since we have removed the RCU protection around the handle lookup we also removed the RCU freeing of the list.
---
v2:
* Dropped RCU freeing of the list.
v3:
* Handle NULL in amdgpu_bo_list_put.
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 83 +++++++++------------ drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 11 +-- 4 files changed, 43 insertions(+), 58 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
@@ -50,6 +50,7 @@#include <linux/hashtable.h> #include <linux/dma-fence.h> #include <linux/pci.h> +#include <linux/xarray.h> #include <drm/ttm/ttm_bo.h> #include <drm/ttm/ttm_placement.h>
@@ -499,8 +500,7 @@ struct amdgpu_fpriv {struct amdgpu_bo_va *prt_va;struct amdgpu_bo_va *csa_va;struct amdgpu_bo_va *seq64_va; -struct mutex bo_list_lock; -struct idr bo_list_handles; +struct xarray bo_list_handles;struct amdgpu_ctx_mgr ctx_mgr;struct amdgpu_userq_mgr userq_mgr;
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +36 -47
140 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -2
@@ -43,7 +43,6 @@ struct amdgpu_bo_list_entry { };struct amdgpu_bo_list { -struct rcu_head rhead;struct kref refcount;struct amdgpu_bo *gds_obj;struct amdgpu_bo *gws_obj;
@@ -54,7 +53,7 @@ struct amdgpu_bo_list { struct amdgpu_bo_list_entry entries[] __counted_by(num_entries); }; -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,int id, +int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id,struct amdgpu_bo_list **result);void amdgpu_bo_list_put (struct amdgpu_bo_list *list); int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -7
@@ -1451,8 +1451,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)if (r) goto error_vm; -mutex_init (&fpriv->bo_list_lock); -idr_init_base (&fpriv->bo_list_handles,1 ); +xa_init_flags (&fpriv->bo_list_handles,XA_FLAGS_ALLOC1 ); r =amdgpu_userq_mgr_init (&fpriv->userq_mgr, file_priv, adev);if (r)
@@ -1498,8 +1497,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,struct amdgpu_fpriv *fpriv = file_priv->driver_priv;struct amdgpu_bo_list *list;struct amdgpu_bo *pd; +unsigned long handle;u32 pasid; -int handle;if (!fpriv)return ;
@@ -1535,11 +1534,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,amdgpu_pasid_free_delayed (pd->tbo.base.resv, pasid);amdgpu_bo_unref (&pd); -idr_for_each_entry (&fpriv->bo_list_handles, list, handle) +xa_for_each (&fpriv->bo_list_handles, handle, list)amdgpu_bo_list_put (list); - -idr_destroy (&fpriv->bo_list_handles); -mutex_destroy (&fpriv->bo_list_lock); +xa_destroy (&fpriv->bo_list_handles);kfree (fpriv); file_priv->driver_priv =NULL ;
Signature · 1 line
2.52.0
Re: [PATCH v3 10/12] drm/amdgpu: Replace idr with xarray in amdgpu_bo_list
Quoted 8 lines · Tvrtko Ursulin
IDR is deprecated so let's replace it with xarray.
Conversion is mostly 1:1 apart from
AMDGPU_BO_LIST_OP_UPDATEwhich was implemented withidr_replace, and has now been replaced with a sequence ofxa_loadandxa_cmpxchg. Should userspace attempt multi-threaded update operations on the same handle it could theoretically hit a new-ENOENTpath. But I believe this is purely theoretical and still safe.
Quoted 4 lines
Also, since we have removed the RCU protection around the handle lookup we also removed the RCU freeing of the list.
Signed-off-by Tvrtko Ursulin <[email protected]>
Quoted 244 lines
---
v2:
* Dropped RCU freeing of the list.v3:
* Handle NULL inamdgpu_bo_list_put.--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 83 +++++++++------------ drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 11 +-- 4 files changed, 43 insertions(+), 58 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu.h +2
-2@@ -50,6 +50,7 @@#include <linux/hashtable.h> #include <linux/dma-fence.h> #include <linux/pci.h> +#include <linux/xarray.h> #include <drm/ttm/ttm_bo.h> #include <drm/ttm/ttm_placement.h> @@ -499,8 +500,7 @@ struct amdgpu_fpriv {struct amdgpu_bo_va *prt_va;struct amdgpu_bo_va *csa_va;struct amdgpu_bo_va *seq64_va; -struct mutex bo_list_lock; -struct idr bo_list_handles; +struct xarray bo_list_handles;struct amdgpu_ctx_mgr ctx_mgr;struct amdgpu_userq_mgr userq_mgr;drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +36
-47@@ -37,14 +37,6 @@#define AMDGPU_BO_LIST_MAX_PRIORITY 32u#define AMDGPU_BO_LIST_NUM_BUCKETS (AMDGPU_BO_LIST_MAX_PRIORITY + 1) -static void amdgpu_bo_list_free_rcu (struct rcu_head *rcu) -{ - struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list, - rhead); - -kvfree (list); -} -static void amdgpu_bo_list_free (struct kref *ref) { struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,@@ -53,7 +45,8 @@ static void amdgpu_bo_list_free(struct kref *ref)amdgpu_bo_list_for_each_entry (e, list)amdgpu_bo_unref (&e->bo); -call_rcu (&list->rhead, amdgpu_bo_list_free_rcu); + +kvfree (list); }static int amdgpu_bo_list_entry_cmp (const void *_a,const void *_b)@@ -146,36 +139,26 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, } -static void amdgpu_bo_list_destroy (struct amdgpu_fpriv *fpriv,int id) -{ -struct amdgpu_bo_list *list; - -mutex_lock (&fpriv->bo_list_lock); - list =idr_remove (&fpriv->bo_list_handles, id); -mutex_unlock (&fpriv->bo_list_lock); -if (list) -kref_put (&list->refcount, amdgpu_bo_list_free); -} - -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,int id, +int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id,struct amdgpu_bo_list **result) { -rcu_read_lock (); - *result =idr_find (&fpriv->bo_list_handles, id); +struct amdgpu_bo_list *list; -if (*result &&kref_get_unless_zero (&(*result)->refcount)) { -rcu_read_unlock (); -return 0 ; - } +xa_lock (&fpriv->bo_list_handles); + list =xa_load (&fpriv->bo_list_handles, id); +if (list) +kref_get (&list->refcount); +xa_unlock (&fpriv->bo_list_handles); -rcu_read_unlock (); - *result =NULL ; -return -ENOENT ; + *result = list; + +return list ?0 : -ENOENT ; }void amdgpu_bo_list_put (struct amdgpu_bo_list *list) { -kref_put (&list->refcount, amdgpu_bo_list_free); +if (list) +kref_put (&list->refcount, amdgpu_bo_list_free); } int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,@@ -199,12 +182,12 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,int amdgpu_bo_list_ioctl (struct drm_device *dev,void *data,struct drm_file *filp) { -struct amdgpu_device *adev =drm_to_adev (dev);struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_device *adev =drm_to_adev (dev); +struct drm_amdgpu_bo_list_entry *info =NULL ; +struct amdgpu_bo_list *list, *prev, *curr;union drm_amdgpu_bo_list *args = data;uint32_t handle = args->in.list_handle; -struct drm_amdgpu_bo_list_entry *info =NULL ; -struct amdgpu_bo_list *list, *old;int r; r =amdgpu_bo_create_list_entry_array (&args->in, &info);@@ -218,19 +201,18 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,if (r) goto error_free; -mutex_lock (&fpriv->bo_list_lock); - r =idr_alloc (&fpriv->bo_list_handles, list,1 ,0 ,GFP_KERNEL ); -mutex_unlock (&fpriv->bo_list_lock); -if (r <0 ) { + r =xa_alloc (&fpriv->bo_list_handles, &handle, list, + xa_limit_32b,GFP_KERNEL ); +if (r) goto error_put_list; - } - handle = r;break ;case AMDGPU_BO_LIST_OP_DESTROY : -amdgpu_bo_list_destroy (fpriv, handle); + list =xa_erase (&fpriv->bo_list_handles, handle); +amdgpu_bo_list_put (list); handle =0 ; +break ;case AMDGPU_BO_LIST_OP_UPDATE :@@ -239,16 +221,23 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,if (r) goto error_free; -mutex_lock (&fpriv->bo_list_lock); - old =idr_replace (&fpriv->bo_list_handles, list, handle); -mutex_unlock (&fpriv->bo_list_lock); + curr =xa_load (&fpriv->bo_list_handles, handle); +if (!curr) { + r = -ENOENT ; + goto error_put_list; + } -if (IS_ERR (old)) { - r =PTR_ERR (old); + prev =xa_cmpxchg (&fpriv->bo_list_handles, handle, curr, list, +GFP_KERNEL ); +if (xa_is_err (prev)) { + r =xa_err (prev); + goto error_put_list; + }else if (prev != curr) { + r = -ENOENT ; goto error_put_list; } -amdgpu_bo_list_put (old); +amdgpu_bo_list_put (curr);break ;default :drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1
-2@@ -43,7 +43,6 @@ struct amdgpu_bo_list_entry { };struct amdgpu_bo_list { -struct rcu_head rhead;struct kref refcount;struct amdgpu_bo *gds_obj;struct amdgpu_bo *gws_obj;@@ -54,7 +53,7 @@ struct amdgpu_bo_list { struct amdgpu_bo_list_entry entries[] __counted_by(num_entries); }; -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,int id, +int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id,struct amdgpu_bo_list **result);void amdgpu_bo_list_put (struct amdgpu_bo_list *list); int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4
-7@@ -1451,8 +1451,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)if (r) goto error_vm; -mutex_init (&fpriv->bo_list_lock); -idr_init_base (&fpriv->bo_list_handles,1 ); +xa_init_flags (&fpriv->bo_list_handles,XA_FLAGS_ALLOC1 ); r =amdgpu_userq_mgr_init (&fpriv->userq_mgr, file_priv, adev);if (r)@@ -1498,8 +1497,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,struct amdgpu_fpriv *fpriv = file_priv->driver_priv;struct amdgpu_bo_list *list;struct amdgpu_bo *pd; +unsigned long handle;u32 pasid; -int handle;if (!fpriv)return ;@@ -1535,11 +1534,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,amdgpu_pasid_free_delayed (pd->tbo.base.resv, pasid);amdgpu_bo_unref (&pd); -idr_for_each_entry (&fpriv->bo_list_handles, list, handle) +xa_for_each (&fpriv->bo_list_handles, handle, list)amdgpu_bo_list_put (list); - -idr_destroy (&fpriv->bo_list_handles); -mutex_destroy (&fpriv->bo_list_lock); +xa_destroy (&fpriv->bo_list_handles);kfree (fpriv); file_priv->driver_priv =NULL ;
PATCH v3 11/12 drm/amdgpu: Remove output parameter in bo list handling
In reply to Tvrtko UrsulinRemoving the output parameter from a few functions should result in more readable code and also enables us to save some lines.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 84 ++++++++++----------- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 54 ++++++------- 3 files changed, 71 insertions(+), 84 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +38 -46
@@ -58,9 +58,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)return (int )a->priority - (int )b->priority; } -int amdgpu_bo_list_create (struct amdgpu_device *adev,struct drm_file *filp, -struct drm_amdgpu_bo_list_entry *info, -size_t num_entries,struct amdgpu_bo_list **result) +struct amdgpu_bo_list * +amdgpu_bo_list_create (struct amdgpu_device *adev,struct drm_file *filp, +struct drm_amdgpu_bo_list_entry *info,size_t num_entries) {unsigned last_entry =0 , first_userptr = num_entries;struct amdgpu_bo_list_entry *array;
@@ -71,7 +71,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, list =kvzalloc (struct_size (list, entries, num_entries),GFP_KERNEL );if (!list) -return -ENOMEM ; +return ERR_PTR (-ENOMEM );kref_init (&list->refcount);
@@ -126,8 +126,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,trace_amdgpu_cs_bo_status (list->num_entries, total_size); - *result = list; -return 0 ; +return list; error_free:for (i =0 ; i < last_entry; ++i)
@@ -135,12 +134,11 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,for (i = first_userptr; i < num_entries; ++i)amdgpu_bo_unref (&array[i].bo);kvfree (list); -return r; +return ERR_PTR (r); } -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id, -struct amdgpu_bo_list **result) +struct amdgpu_bo_list *amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id) {struct amdgpu_bo_list *list;
@@ -148,11 +146,11 @@ int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id, list =xa_load (&fpriv->bo_list_handles, id);if (list)kref_get (&list->refcount); +else + list =ERR_PTR (-ENOENT );xa_unlock (&fpriv->bo_list_handles); - *result = list; - -return list ?0 : -ENOENT ; +return list; }void amdgpu_bo_list_put (struct amdgpu_bo_list *list)
@@ -161,22 +159,15 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)kref_put (&list->refcount, amdgpu_bo_list_free); } -int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in, -struct drm_amdgpu_bo_list_entry **info_param) +struct drm_amdgpu_bo_list_entry * +amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in) { -struct drm_amdgpu_bo_list_entry *info; -if (in->bo_info_size !=sizeof (struct drm_amdgpu_bo_list_entry )) -return -EINVAL ; +return ERR_PTR (-EINVAL ); - info =vmemdup_array_user (u64_to_user_ptr (in->bo_info_ptr), +return vmemdup_array_user (u64_to_user_ptr (in->bo_info_ptr), in->bo_number,sizeof (struct drm_amdgpu_bo_list_entry )); -if (IS_ERR (info)) -return PTR_ERR (info); - - *info_param = info; -return 0 ; } int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
@@ -184,27 +175,24 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data, {struct amdgpu_fpriv *fpriv = filp->driver_priv;struct amdgpu_device *adev =drm_to_adev (dev); -struct drm_amdgpu_bo_list_entry *info =NULL ;struct amdgpu_bo_list *list, *prev, *curr;union drm_amdgpu_bo_list *args = data;uint32_t handle = args->in.list_handle; +struct drm_amdgpu_bo_list_entry *info;int r; - r =amdgpu_bo_create_list_entry_array (&args->in, &info); -if (r) -return r; -switch (args->in.operation) {case AMDGPU_BO_LIST_OP_CREATE : - r =amdgpu_bo_list_create (adev, filp, info, args->in.bo_number, - &list); -if (r) - goto error_free; +case AMDGPU_BO_LIST_OP_UPDATE : + info =amdgpu_bo_create_list_entry_array (&args->in); +if (IS_ERR (info)) +return PTR_ERR (info); - r =xa_alloc (&fpriv->bo_list_handles, &handle, list, - xa_limit_32b,GFP_KERNEL ); -if (r) - goto error_put_list; + list =amdgpu_bo_list_create (adev, filp, info, + args->in.bo_number); +kvfree (info); +if (IS_ERR (list)) +return PTR_ERR (list);break ;
@@ -215,12 +203,20 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,break ; -case AMDGPU_BO_LIST_OP_UPDATE : - r =amdgpu_bo_list_create (adev, filp, info, args->in.bo_number, - &list); +default : +return -EINVAL ; + }; + +switch (args->in.operation) { +case AMDGPU_BO_LIST_OP_CREATE : + r =xa_alloc (&fpriv->bo_list_handles, &handle, list, + xa_limit_32b,GFP_KERNEL );if (r) - goto error_free; + goto error_put_list; +break ; + +case AMDGPU_BO_LIST_OP_UPDATE : curr =xa_load (&fpriv->bo_list_handles, handle);if (!curr) { r = -ENOENT ;
@@ -240,21 +236,17 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,amdgpu_bo_list_put (curr);break ; +case AMDGPU_BO_LIST_OP_DESTROY :default : - r = -EINVAL ; - goto error_free; +/* Handled above. */ }memset (args,0 ,sizeof (*args)); args->out.list_handle = handle; -kvfree (info);return 0 ; error_put_list:amdgpu_bo_list_put (list); - -error_free: -kvfree (info);return r; }
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +8 -9
@@ -53,17 +53,16 @@ struct amdgpu_bo_list { struct amdgpu_bo_list_entry entries[] __counted_by(num_entries); }; -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id, -struct amdgpu_bo_list **result); +struct amdgpu_bo_list *amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id);void amdgpu_bo_list_put (struct amdgpu_bo_list *list); -int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in, -struct drm_amdgpu_bo_list_entry **info_param); +struct drm_amdgpu_bo_list_entry * +amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in); -int amdgpu_bo_list_create (struct amdgpu_device *adev, -struct drm_file *filp, -struct drm_amdgpu_bo_list_entry *info, -size_t num_entries, -struct amdgpu_bo_list **list); +struct amdgpu_bo_list * +amdgpu_bo_list_create (struct amdgpu_device *adev, +struct drm_file *filp, +struct drm_amdgpu_bo_list_entry *info, +size_t num_entries);#define amdgpu_bo_list_for_each_entry (e, list) \ for (e = list->entries; \
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +25 -29
105 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v3 11/12] drm/amdgpu: Remove output parameter in bo list handling
Quoted 4 lines · Tvrtko Ursulin
Removing the output parameter from a few functions should result in more readable code and also enables us to save some lines.
Signed-off-by Tvrtko Ursulin <[email protected]>
Quoted 331 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 84 ++++++++++----------- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 54 ++++++------- 3 files changed, 71 insertions(+), 84 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +38
-46@@ -58,9 +58,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)return (int )a->priority - (int )b->priority; } -int amdgpu_bo_list_create (struct amdgpu_device *adev,struct drm_file *filp, -struct drm_amdgpu_bo_list_entry *info, -size_t num_entries,struct amdgpu_bo_list **result) +struct amdgpu_bo_list * +amdgpu_bo_list_create (struct amdgpu_device *adev,struct drm_file *filp, +struct drm_amdgpu_bo_list_entry *info,size_t num_entries) {unsigned last_entry =0 , first_userptr = num_entries;struct amdgpu_bo_list_entry *array;@@ -71,7 +71,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, list =kvzalloc (struct_size (list, entries, num_entries),GFP_KERNEL );if (!list) -return -ENOMEM ; +return ERR_PTR (-ENOMEM );kref_init (&list->refcount);@@ -126,8 +126,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,trace_amdgpu_cs_bo_status (list->num_entries, total_size); - *result = list; -return 0 ; +return list; error_free:for (i =0 ; i < last_entry; ++i)@@ -135,12 +134,11 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,for (i = first_userptr; i < num_entries; ++i)amdgpu_bo_unref (&array[i].bo);kvfree (list); -return r; +return ERR_PTR (r); } -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id, -struct amdgpu_bo_list **result) +struct amdgpu_bo_list *amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id) {struct amdgpu_bo_list *list;@@ -148,11 +146,11 @@ int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id, list =xa_load (&fpriv->bo_list_handles, id);if (list)kref_get (&list->refcount); +else + list =ERR_PTR (-ENOENT );xa_unlock (&fpriv->bo_list_handles); - *result = list; - -return list ?0 : -ENOENT ; +return list; }void amdgpu_bo_list_put (struct amdgpu_bo_list *list)@@ -161,22 +159,15 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)kref_put (&list->refcount, amdgpu_bo_list_free); } -int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in, -struct drm_amdgpu_bo_list_entry **info_param) +struct drm_amdgpu_bo_list_entry * +amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in) { -struct drm_amdgpu_bo_list_entry *info; -if (in->bo_info_size !=sizeof (struct drm_amdgpu_bo_list_entry )) -return -EINVAL ; +return ERR_PTR (-EINVAL ); - info =vmemdup_array_user (u64_to_user_ptr (in->bo_info_ptr), +return vmemdup_array_user (u64_to_user_ptr (in->bo_info_ptr), in->bo_number,sizeof (struct drm_amdgpu_bo_list_entry )); -if (IS_ERR (info)) -return PTR_ERR (info); - - *info_param = info; -return 0 ; } int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,@@ -184,27 +175,24 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data, {struct amdgpu_fpriv *fpriv = filp->driver_priv;struct amdgpu_device *adev =drm_to_adev (dev); -struct drm_amdgpu_bo_list_entry *info =NULL ;struct amdgpu_bo_list *list, *prev, *curr;union drm_amdgpu_bo_list *args = data;uint32_t handle = args->in.list_handle; +struct drm_amdgpu_bo_list_entry *info;int r; - r =amdgpu_bo_create_list_entry_array (&args->in, &info); -if (r) -return r; -switch (args->in.operation) {case AMDGPU_BO_LIST_OP_CREATE : - r =amdgpu_bo_list_create (adev, filp, info, args->in.bo_number, - &list); -if (r) - goto error_free; +case AMDGPU_BO_LIST_OP_UPDATE : + info =amdgpu_bo_create_list_entry_array (&args->in); +if (IS_ERR (info)) +return PTR_ERR (info); - r =xa_alloc (&fpriv->bo_list_handles, &handle, list, - xa_limit_32b,GFP_KERNEL ); -if (r) - goto error_put_list; + list =amdgpu_bo_list_create (adev, filp, info, + args->in.bo_number); +kvfree (info); +if (IS_ERR (list)) +return PTR_ERR (list);break ;@@ -215,12 +203,20 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,break ; -case AMDGPU_BO_LIST_OP_UPDATE : - r =amdgpu_bo_list_create (adev, filp, info, args->in.bo_number, - &list); +default : +return -EINVAL ; + }; + +switch (args->in.operation) { +case AMDGPU_BO_LIST_OP_CREATE : + r =xa_alloc (&fpriv->bo_list_handles, &handle, list, + xa_limit_32b,GFP_KERNEL );if (r) - goto error_free; + goto error_put_list; +break ; + +case AMDGPU_BO_LIST_OP_UPDATE : curr =xa_load (&fpriv->bo_list_handles, handle);if (!curr) { r = -ENOENT ;@@ -240,21 +236,17 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,amdgpu_bo_list_put (curr);break ; +case AMDGPU_BO_LIST_OP_DESTROY :default : - r = -EINVAL ; - goto error_free; +/* Handled above. */ }memset (args,0 ,sizeof (*args)); args->out.list_handle = handle; -kvfree (info);return 0 ; error_put_list:amdgpu_bo_list_put (list); - -error_free: -kvfree (info);return r; }drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +8
-9@@ -53,17 +53,16 @@ struct amdgpu_bo_list { struct amdgpu_bo_list_entry entries[] __counted_by(num_entries); }; -int amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id, -struct amdgpu_bo_list **result); +struct amdgpu_bo_list *amdgpu_bo_list_get (struct amdgpu_fpriv *fpriv,u32 id);void amdgpu_bo_list_put (struct amdgpu_bo_list *list); -int amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in, -struct drm_amdgpu_bo_list_entry **info_param); +struct drm_amdgpu_bo_list_entry * +amdgpu_bo_create_list_entry_array (struct drm_amdgpu_bo_list_in *in); -int amdgpu_bo_list_create (struct amdgpu_device *adev, -struct drm_file *filp, -struct drm_amdgpu_bo_list_entry *info, -size_t num_entries, -struct amdgpu_bo_list **list); +struct amdgpu_bo_list * +amdgpu_bo_list_create (struct amdgpu_device *adev, +struct drm_file *filp, +struct drm_amdgpu_bo_list_entry *info, +size_t num_entries);#define amdgpu_bo_list_for_each_entry (e, list) \ for (e = list->entries; \drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +25
-29@@ -145,24 +145,19 @@ static int amdgpu_cs_p1_bo_handles(struct amdgpu_cs_parser *p, struct drm_amdgpu_bo_list_in *data) {struct drm_amdgpu_bo_list_entry *info; -int r; +struct amdgpu_bo_list *list; - r =amdgpu_bo_create_list_entry_array (data, &info); -if (r) -return r; - - r =amdgpu_bo_list_create (p->adev, p->filp, info, data->bo_number, - &p->bo_list); -if (r) - goto error_free; + info =amdgpu_bo_create_list_entry_array (data); +if (IS_ERR (info)) +return PTR_ERR (info); + list =amdgpu_bo_list_create (p->adev, p->filp, info, data->bo_number);kvfree (info); +if (IS_ERR (list)) +return PTR_ERR (list); + + p->bo_list = list;return 0 ; - -error_free: -kvfree (info); - -return r; }/* Copy the data from userspace and go over it the first time */ @@ -850,6 +845,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, {struct amdgpu_fpriv *fpriv = p->filp->driver_priv;struct ttm_operation_ctx ctx = { true, false }; +struct amdgpu_bo_list *list =NULL ;struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct drm_gem_object *obj;@@ -862,23 +858,24 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,if (p->bo_list)return -EINVAL ; - r =amdgpu_bo_list_get (fpriv, cs->in.bo_list_handle, - &p->bo_list); -if (r) -return r; + list =amdgpu_bo_list_get (fpriv, cs->in.bo_list_handle); }else if (!p->bo_list) {/* Create a empty bo_list when no handle is provided */ - r =amdgpu_bo_list_create (p->adev, p->filp,NULL ,0 , - &p->bo_list); -if (r) -return r; + list =amdgpu_bo_list_create (p->adev, p->filp,NULL ,0 ); } +if (IS_ERR (list)) +return PTR_ERR (list); +else if (list) + p->bo_list = list; +else + list = p->bo_list; +/* Get userptr backing pages. If pages are updated after registered * in amdgpu_gem_userptr_ioctl(), amdgpu_cs_list_validate() will do * amdgpu_ttm_backend_bind() to flush and invalidate new pages */ -amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) { +amdgpu_bo_list_for_each_userptr_entry (e, list) {bool userpage_invalidated = false;struct amdgpu_bo *bo = e->bo;@@ -906,7 +903,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,if (unlikely (r)) goto out_free_user_pages; -amdgpu_bo_list_for_each_entry (e, p->bo_list) { +amdgpu_bo_list_for_each_entry (e, list) {/* One fence for TTM and one for each CS job */ r =drm_exec_prepare_obj (&p->exec, &e->bo->tbo.base,1 + p->gang_size);@@ -926,7 +923,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, } } -amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) { +amdgpu_bo_list_for_each_userptr_entry (e, list) {struct mm_struct *usermm; usermm =amdgpu_ttm_tt_get_usermm (e->bo->tbo.ttm);@@ -979,13 +976,12 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, p->bytes_moved_vis);for (i =0 ; i < p->gang_size; ++i) -amdgpu_job_set_resources (p->jobs[i], p->bo_list->gds_obj, - p->bo_list->gws_obj, - p->bo_list->oa_obj); +amdgpu_job_set_resources (p->jobs[i], list->gds_obj, + list->gws_obj, list->oa_obj);return 0 ; out_free_user_pages: -amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) { +amdgpu_bo_list_for_each_userptr_entry (e, list) {amdgpu_hmm_range_free (e->range); e->range =NULL ; }
PATCH v3 12/12 drm/amdgpu: Convert context manager to xarray
In reply to Tvrtko UrsulinIDR is deprecated so let's convert the context manager to xarray.
In doing so we remove the context manager mutex and switch call sites which required the guarantee context cannot go away while they walk the list of context, or otherwise operate on them, to use reference counting.
This allows us to use the built in xarray spinlock for all operations and just temporarily drop it when we need to call sleeping functions.
---
v2:
* No need for kref_get_unless_zero when locked.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 119 ++++++++-------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c | 8 +- 3 files changed, 48 insertions(+), 84 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +42 -77
@@ -487,34 +487,26 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,if (!ctx)return -ENOMEM ; -mutex_lock (&mgr->lock); - *id =1 ; - r =idr_alloc_u32 (&mgr->ctx_handles, ctx, id,UINT_MAX ,GFP_KERNEL ); -if (r) { -mutex_unlock (&mgr->lock); -kfree (ctx); -return r; - } - r =amdgpu_ctx_init (mgr, priority, filp, ctx);if (r) { -idr_remove (&mgr->ctx_handles, *id); - *id =0 ;kfree (ctx); +return r; } -mutex_unlock (&mgr->lock); + + r =xa_alloc (&mgr->ctx_handles, id, ctx, xa_limit_32b,GFP_KERNEL ); +if (r) +amdgpu_ctx_put (ctx); +return r; }static int amdgpu_ctx_free (struct amdgpu_fpriv *fpriv,uint32_t id) { -struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;struct amdgpu_ctx *ctx; -mutex_lock (&mgr->lock); - ctx =idr_remove (&mgr->ctx_handles, id); + ctx =xa_erase (&fpriv->ctx_mgr.ctx_handles, id);amdgpu_ctx_put (ctx); -mutex_unlock (&mgr->lock); +return ctx ?0 : -EINVAL ; }
@@ -523,20 +515,12 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev, union drm_amdgpu_ctx_out *out) {struct amdgpu_ctx *ctx; -struct amdgpu_ctx_mgr *mgr;unsigned reset_counter; -if (!fpriv) + ctx =amdgpu_ctx_get (fpriv, id); +if (!ctx)return -EINVAL ; - mgr = &fpriv->ctx_mgr; -mutex_lock (&mgr->lock); - ctx =idr_find (&mgr->ctx_handles, id); -if (!ctx) { -mutex_unlock (&mgr->lock); -return -EINVAL ; - } -/* TODO: these two are always zero */ out->state.flags =0x0 ; out->state.hangs =0x0 ;
@@ -550,7 +534,8 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev, out->state.reset_status =AMDGPU_CTX_UNKNOWN_RESET ; ctx->reset_counter_query = reset_counter; -mutex_unlock (&mgr->lock); +amdgpu_ctx_put (ctx); +return 0 ; }
@@ -562,19 +547,11 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev, {struct amdgpu_ras *con =amdgpu_ras_get_context (adev);struct amdgpu_ctx *ctx; -struct amdgpu_ctx_mgr *mgr; -if (!fpriv) + ctx =amdgpu_ctx_get (fpriv, id); +if (!ctx)return -EINVAL ; - mgr = &fpriv->ctx_mgr; -mutex_lock (&mgr->lock); - ctx =idr_find (&mgr->ctx_handles, id); -if (!ctx) { -mutex_unlock (&mgr->lock); -return -EINVAL ; - } - out->state.flags =0x0 ; out->state.hangs =0x0 ;
@@ -614,7 +591,8 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev, msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS)); } -mutex_unlock (&mgr->lock); +amdgpu_ctx_put (ctx); +return 0 ; }
@@ -623,26 +601,18 @@ static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev, bool set, u32 *stable_pstate) { struct amdgpu_ctx *ctx; -struct amdgpu_ctx_mgr *mgr;int r; -if (!fpriv) + ctx =amdgpu_ctx_get (fpriv, id); +if (!ctx)return -EINVAL ; - mgr = &fpriv->ctx_mgr; -mutex_lock (&mgr->lock); - ctx =idr_find (&mgr->ctx_handles, id); -if (!ctx) { -mutex_unlock (&mgr->lock); -return -EINVAL ; - } -if (set) r =amdgpu_ctx_set_stable_pstate (ctx, *stable_pstate);else r =amdgpu_ctx_get_stable_pstate (ctx, stable_pstate); -mutex_unlock (&mgr->lock); +amdgpu_ctx_put (ctx);return r; }
@@ -721,11 +691,11 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id) mgr = &fpriv->ctx_mgr; -mutex_lock (&mgr->lock); - ctx =idr_find (&mgr->ctx_handles, id); +xa_lock (&mgr->ctx_handles); + ctx =xa_load (&mgr->ctx_handles, id);if (ctx)kref_get (&ctx->refcount); -mutex_unlock (&mgr->lock); +xa_unlock (&mgr->ctx_handles);return ctx; }
@@ -861,8 +831,7 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,unsigned int i; mgr->adev = adev; -mutex_init (&mgr->lock); -idr_init_base (&mgr->ctx_handles,1 ); +xa_init_flags (&mgr->ctx_handles,XA_FLAGS_ALLOC1 );for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i)atomic64_set (&mgr->time_spend[i],0 );
@@ -871,13 +840,13 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,long amdgpu_ctx_mgr_entity_flush (struct amdgpu_ctx_mgr *mgr,long timeout) {struct amdgpu_ctx *ctx; -struct idr *idp; -uint32_t id, i, j; +unsigned long id; +int i, j; - idp = &mgr->ctx_handles; - -mutex_lock (&mgr->lock); -idr_for_each_entry (idp, ctx, id) { +xa_lock (&mgr->ctx_handles); +xa_for_each (&mgr->ctx_handles, id, ctx) { +kref_get (&ctx->refcount); +xa_unlock (&mgr->ctx_handles);for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) {for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) {struct drm_sched_entity *entity;
@@ -889,25 +858,21 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout) timeout =drm_sched_entity_flush (entity, timeout); } } +amdgpu_ctx_put (ctx); +xa_lock (&mgr->ctx_handles); } -mutex_unlock (&mgr->lock); +xa_unlock (&mgr->ctx_handles);return timeout; } -static void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr) -{ -struct amdgpu_ctx *ctx; -uint32_t id; - -idr_for_each_entry (&mgr->ctx_handles, ctx, id) -amdgpu_ctx_put (ctx); -} -void amdgpu_ctx_mgr_fini (struct amdgpu_ctx_mgr *mgr) { -amdgpu_ctx_mgr_entity_fini (mgr); -idr_destroy (&mgr->ctx_handles); -mutex_destroy (&mgr->lock); +struct amdgpu_ctx *ctx; +unsigned long id; + +xa_for_each (&mgr->ctx_handles, id, ctx) +amdgpu_ctx_put (ctx); +xa_destroy (&mgr->ctx_handles); } void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
@@ -915,21 +880,21 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr, {struct amdgpu_ctx *ctx;unsigned int hw_ip, i; -uint32_t id; +unsigned long id;/* * This is a little bit racy because it can be that a ctx or a fence are * destroyed just in the moment we try to account them. But that is ok * since exactly that case is explicitely allowed by the interface. */ -mutex_lock (&mgr->lock);for (hw_ip =0 ; hw_ip <AMDGPU_HW_IP_NUM ; ++hw_ip) {uint64_t ns =atomic64_read (&mgr->time_spend[hw_ip]); usage[hw_ip] =ns_to_ktime (ns); } -idr_for_each_entry (&mgr->ctx_handles, ctx, id) { +xa_lock (&mgr->ctx_handles); +xa_for_each (&mgr->ctx_handles, id, ctx) {for (hw_ip =0 ; hw_ip <AMDGPU_HW_IP_NUM ; ++hw_ip) {for (i =0 ; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {struct amdgpu_ctx_entity *centity;
@@ -943,5 +908,5 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr, } } } -mutex_unlock (&mgr->lock); +xa_unlock (&mgr->ctx_handles); }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +2 -3
@@ -25,6 +25,7 @@#include <linux/ktime.h> #include <linux/types.h> +#include <linux/xarray.h> #include "amdgpu_ring.h"
@@ -61,9 +62,7 @@ struct amdgpu_ctx {struct amdgpu_ctx_mgr {struct amdgpu_device *adev; -struct mutex lock; -/* protected by lock */ -struct idr ctx_handles; +struct xarray ctx_handles;atomic64_t time_spend[AMDGPU_HW_IP_NUM ]; };
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +4 -4
@@ -39,7 +39,7 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,struct amdgpu_fpriv *fpriv;struct amdgpu_ctx_mgr *mgr;struct amdgpu_ctx *ctx; -uint32_t id; +unsigned long id;int r;if (fd_empty (f))
@@ -50,10 +50,10 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,return r; mgr = &fpriv->ctx_mgr; -mutex_lock (&mgr->lock); -idr_for_each_entry (&mgr->ctx_handles, ctx, id) +xa_lock (&mgr->ctx_handles); +xa_for_each (&mgr->ctx_handles, id, ctx)amdgpu_ctx_priority_override (ctx, priority); -mutex_unlock (&mgr->lock); +xa_unlock (&mgr->ctx_handles);return 0 ; }
Signature · 1 line
2.52.0