IDR to xarray conversion and some small cleanups
Subsystems (2)
v4 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.
v4:
* Improved commit text in 3/12.
* Added fpriv removal TODO to 6/12.
* Keep amdgpu_ctx_init_entity() separate.
* Recorded more r-b's.
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 | 177 ++++++------------- 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, 186 insertions(+), 358 deletions(-)
Signature · 1 line
2.52.0
PATCH v4 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 v4 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 v4 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. Even if the structure is extended at some point, older kernels will note that they do not support the additional fields by rejecting the new structure size.
--
v2:
* Expand commit message a bit.
--- 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
PATCH v4 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 v4 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 v4 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.
---
v2:
* Added TODO note for improving amdgpu_xcp_select_scheds.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 4 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -2
@@ -232,7 +232,8 @@ 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); +/* TODO: Stop using fpriv here, we only need the xcp_id. */ + 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 +355,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
PATCH v4 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
@@ -490,14 +490,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 v4 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.
v3:
* Keep amdgpu_ctx_fini_entity as a separate function.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 54 ++++--------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 9 ++++- 2 files changed, 15 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +7 -47
@@ -284,6 +284,8 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev,if (!entity)return res; +drm_sched_entity_destroy (&entity->entity); +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]);
@@ -409,7 +411,7 @@ 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;
@@ -508,24 +510,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;
@@ -533,8 +517,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 ; }
@@ -750,15 +733,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)
@@ -927,29 +901,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
Re: [PATCH v4 08/12] drm/amdgpu: Consolidate ctx put
Quoted 20 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.
Quoted 2 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Suggested-by Christian König <[email protected]>
Looks like this was the last patch to review. I will pick up the set and try to push it to amd-staging-drm-next.
Regards,
Christian.
Quoted 137 lines
---
v2:
* Use separate variable fordrm_dev_enterfor readability.v3:
* Keepamdgpu_ctx_fini_entityas a separate function.--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 54 ++++--------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 9 ++++- 2 files changed, 15 insertions(+), 48 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +7
-47@@ -284,6 +284,8 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev,if (!entity)return res; +drm_sched_entity_destroy (&entity->entity); +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]);@@ -409,7 +411,7 @@ 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;@@ -508,24 +510,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;@@ -533,8 +517,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 ; }@@ -750,15 +733,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)@@ -927,29 +901,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);
Re: [PATCH v4 08/12] drm/amdgpu: Consolidate ctx put
Quoted 21 lines · Christian König
Quoted 20 lines · Tvrtko Ursulin
Currently there are two flavours of the context reference count destructor:
-
amdgpu_ctx_do_release(), used fromkref_putfrom places where the codethinks 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.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Suggested-by Christian König <[email protected]>Reviewed-by Christian König <[email protected]>Looks like this was the last patch to review. I will pick up the set and try to push it to amd-staging-drm-next.
Gentle reminder if we could try to validate the series via amd-staging-drm-next.
Also, what could I do on my end to get more confidence some edge case is not broken? Run the IGT tests? Is there an useful testlist which can be used with the IGT runner or that is not used on the AMD side?
Regards,
Tvrtko
Quoted 138 lines
Quoted 137 lines
---
v2:
* Use separate variable fordrm_dev_enterfor readability.v3:
* Keepamdgpu_ctx_fini_entityas a separate function.--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 54 ++++--------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 9 ++++- 2 files changed, 15 insertions(+), 48 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +7
-47@@ -284,6 +284,8 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev,if (!entity)return res; +drm_sched_entity_destroy (&entity->entity); +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]);@@ -409,7 +411,7 @@ 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;@@ -508,24 +510,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;@@ -533,8 +517,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 ; }@@ -750,15 +733,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)@@ -927,29 +901,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);
Christian König
maintainerRe: [PATCH v4 08/12] drm/amdgpu: Consolidate ctx put
Quoted 23 lines · Tvrtko Ursulin
Quoted 21 lines · Christian König
Quoted 20 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.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Suggested-by Christian König <[email protected]>Reviewed-by Christian König <[email protected]>Looks like this was the last patch to review. I will pick up the set and try to push it to amd-staging-drm-next.
Gentle reminder if we could try to validate the series via amd-staging-drm-next.
I pushed the first 7 patches from this series to amd-staging-drm-next and our CI systems seem to be happy with them.
But starting with patch #8 I either had rebase conflicts or the CI system seem to reject something here.
Can you rebase on top of amd-staging-drm-next and test a bit more? If you are confident that the patches work I can throw them into the CI system once more.
Regards,
Christian.
Quoted 136 lines
Also, what could I do on my end to get more confidence some edge case is not broken? Run the IGT tests? Is there an useful testlist which can be used with the IGT runner or that is not used on the AMD side?
Regards,
Tvrtko
Quoted 128 lines
Quoted 127 lines
---
v2:
* Use separate variable fordrm_dev_enterfor readability.v3:
* Keepamdgpu_ctx_fini_entityas a separate function.
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 54 ++++---------------------drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h| 9 ++++-2 files changed, 15 insertions(+), 48 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6
-42@@ -284,6 +284,8 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev, if (!entity) return res; + drm_sched_entity_destroy(&entity->entity); + 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]);@@ -409,7 +411,7 @@ 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;@@ -508,24 +510,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;@@ -533,8 +517,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; }@@ -750,15 +733,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)@@ -927,29 +901,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); } }diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.hindexcf8d700a22fe..b1fa7fe74569100644drivers/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);
Re: [PATCH v4 08/12] drm/amdgpu: Consolidate ctx put
Quoted 31 lines · Christian König
Quoted 30 lines · Tvrtko Ursulin
Quoted 27 lines · Christian König
Quoted 20 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.Quoted 2 lines
Signed-off-by Tvrtko Ursulin <[email protected]>Suggested-by Christian König <[email protected]>Reviewed-by Christian König <[email protected]>Looks like this was the last patch to review. I will pick up the set and try to push it to amd-staging-drm-next.
Quoted 7 lines
Quoted 1 line
Gentle reminder if we could try to validate the series via amd-staging-drm-next.
I pushed the first 7 patches from this series to amd-staging-drm-next and our CI systems seem to be happy with them.
But starting with patch #8 I either had rebase conflicts or the CI system seem to reject something here.
Can you rebase on top of amd-staging-drm-next and test a bit more? If you are confident that the patches work I can throw them into the CI system once more.
I don't see all of the first 7 in amd-staging-drm-next just yet, but I have rebased the rest of the series on top of what is there. With that, I so far did not manage to observe any failures or suspicious behaviour in the context management area. I can game, use the desktop, run some IGTs.
One thing I did observe are lockdep splats from amdgpu_device_skip_hw_access() when GPU resets are tested. Not sure if this is known or what?
Otherwise, are there any hints from the CI system as to what test cases or where in amdgpu things are going bad?
Regards,
Tvrtko
Quoted 136 lines
Quoted 135 lines
Also, what could I do on my end to get more confidence some edge case is not broken? Run the IGT tests? Is there an useful testlist which can be used with the IGT runner or that is not used on the AMD side?
Regards,
Tvrtko
Quoted 128 lines
Quoted 127 lines
---
v2:
* Use separate variable fordrm_dev_enterfor readability.v3:
* Keepamdgpu_ctx_fini_entityas a separate function.--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 54 ++++--------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 9 ++++- 2 files changed, 15 insertions(+), 48 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6
-42@@ -284,6 +284,8 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev, if (!entity) return res; + drm_sched_entity_destroy(&entity->entity); + 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]);@@ -409,7 +411,7 @@ 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;@@ -508,24 +510,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;@@ -533,8 +517,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; }@@ -750,15 +733,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)@@ -927,29 +901,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); } } diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.hindex
cf8d700a22fe..b1fa7fe74569100644drivers/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 v4 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
@@ -903,14 +903,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 v4 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
PATCH v4 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
PATCH v4 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
@@ -491,34 +491,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 ; }
@@ -527,20 +519,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 ;
@@ -554,7 +538,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 ; }
@@ -566,19 +551,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 ;
@@ -618,7 +595,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 ; }
@@ -627,26 +605,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; }
@@ -725,11 +695,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; }
@@ -865,8 +835,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 );
@@ -875,13 +844,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;
@@ -893,25 +862,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,
@@ -919,21 +884,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;
@@ -947,5 +912,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