Skip to contentSkip to conversation

IDR to xarray conversion and some small cleanups

PATCH Superseded Tvrtko Ursulin 2 participants 29 messages Dec 19, 2025 – Jan 8

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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.

Tvrtko Ursulin (12):
drm/amdgpu/mes: Remove idr leftovers
drm/amdgpu: Simplify sorting of the bo list drm/amdgpu: Remove unneccessary memset in bo list creation 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 | 153 ++++++-------
 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     | 225 ++++++--------------
 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, 205 insertions(+), 361 deletions(-)
Signature · 1 line
2.51.1
#1 lore raw

PATCH v2 1/12 drm/amdgpu/mes: Remove idr leftovers

Commit
cb17fff3a254 ("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
index 9c182ce501af..505619d504ea 100644
@@ -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
index e989225b354b..f45129277479 100644
@@ -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
index 8ddc4df06a1f..ab9e6199b01d 100644
@@ -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
index 51101b0aa2fa..4f78dd93939c 100644
@@ -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.51.1
#2 lore raw

PATCH v2 2/12 drm/amdgpu: Simplify sorting of the bo list

In reply to Tvrtko Ursulin

Sort 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
index 66fb37b64388..87ec46c56a6e 100644
@@ -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.51.1
#3 lore raw

PATCH v2 3/12 drm/amdgpu: Remove unneccessary memset in bo list creation

In reply to Tvrtko Ursulin

The code will either fully populate the allocated array with userspace provided data, or abort and free the memory, so there is no need to clear the array before populating it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -1
index 87ec46c56a6e..72c5096c3b18 100644
@@ -201,7 +201,6 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 		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);
Signature · 1 line
2.51.1
maintainer
#4 lore raw

Re: [PATCH v2 03/12] drm/amdgpu: Remove unneccessary memset in bo list creation

Quoted 14 lines · Tvrtko Ursulin

The code will either fully populate the allocated array with userspace provided data, or abort and free the memory, so there is no need to clear the array before populating it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -0
index 87ec46c56a6e..72c5096c3b18 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in, · line 206

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -1
@@ -201,7 +201,6 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 		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);

I think we should just nuke the complete else branch this code is part of.

The case when in->bo_info_size != sizeof(struct drm_amdgpu_bo_list_entry) was never used and most likely will never be used.

Regards,
Christian.

#5 lore raw

PATCH v2 4/12 drm/amdgpu: Remove the bo list mutex

In reply to Tvrtko Ursulin

The 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
index 72c5096c3b18..fbac929f711c 100644
@@ -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
index 2b5e7c46a39d..1acf53f8b2f9 100644
@@ -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
index 203223fd0b54..bc9cee3fdf27 100644
@@ -881,8 +881,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
@@ -998,7 +996,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;
 }
 
@@ -1381,7 +1378,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;
 }
 
@@ -1453,28 +1449,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.51.1
maintainer Reviewed-by on v2 4/12
#6 lore raw

Re: [PATCH v2 04/12] drm/amdgpu: Remove the bo list mutex

Quoted 4 lines · Tvrtko Ursulin

The bo list is immutable during command submission since the drm_exec conversion so we can remove the mutex.

Quoted 104 lines
---
 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
index 72c5096c3b18..fbac929f711c 100644
@@ -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
index 2b5e7c46a39d..1acf53f8b2f9 100644
@@ -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
index 203223fd0b54..bc9cee3fdf27 100644
@@ -881,8 +881,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
@@ -998,7 +996,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;
 }
 
@@ -1381,7 +1378,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;
 }
 
@@ -1453,28 +1449,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;
#7 lore raw

PATCH v2 5/12 drm/amdgpu: Remove a few holes from struct amdgpu_ctx

In reply to Tvrtko Ursulin

Re-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
index 090dfe86f75b..aed758d0acaa 100644
@@ -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.51.1
maintainer Reviewed-by on v2 5/12
#8 lore raw

Re: [PATCH v2 05/12] drm/amdgpu: Remove a few holes from struct amdgpu_ctx

Quoted 6 lines · Tvrtko Ursulin

Re-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 */

Quoted 8 lines

/* 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.

Quoted 35 lines
---
 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
index 090dfe86f75b..aed758d0acaa 100644
@@ -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 {
#9 lore raw

PATCH v2 6/12 drm/amdgpu: Remove duplicate struct member

In reply to Tvrtko Ursulin

Struct 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
index afedea02188d..41c05358d86d 100644
@@ -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)
@@ -349,7 +349,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
index aed758d0acaa..cf8d700a22fe 100644
@@ -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.51.1
maintainer
#10 lore raw

Re: [PATCH v2 06/12] drm/amdgpu: Remove duplicate struct member

Quoted 14 lines · Tvrtko Ursulin

Struct 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 +0 -0
index afedea02188d..41c05358d86d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 237

Quoted 7 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);
 		r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
 						&num_scheds, &scheds);

Well that code is utterly nonsense to begin with. amdgpu_xcp_select_scheds() needs the xcp id to select from and not fpriv.

Can you look into re-structuring this so that we don't need that cast?

Thanks in advance,
Christian.

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -1
 		if (r)
@@ -349,7 +349,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
index aed758d0acaa..cf8d700a22fe 100644
@@ -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];
 };
 
#11 lore raw

Re: [PATCH v2 06/12] drm/amdgpu: Remove duplicate struct member

Quoted 18 lines · Christian König
Quoted 17 lines · Tvrtko Ursulin

Struct 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 +0 -0
index afedea02188d..41c05358d86d 100644
@@ -232,7 +232,7 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
  	} else {
  		struct amdgpu_fpriv *fpriv;
 
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 237

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
-		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);

Well that code is utterly nonsense to begin with. amdgpu_xcp_select_scheds() needs the xcp id to select from and not fpriv.

Can you look into re-structuring this so that we don't need that cast?

I had a look and so far only cleanup it up visually a bit so there is fewer long array subscript dereferences and confusion between sel_xcp_id and priv->xcp_id.

But on a more fundamental level, since it needs to write to fpriv, the caller will need to have it one way or the other, no?

And then I noticed not only the atomic_read/inc usage is dodgy, but the fpriv->xcp_id assignment itself is racy. Two threads submitting to the same new entity appears can end up with a refcount imbalance and probably worse.

Shall I replace the ref_cnt atomic with a mutex and protect the whole selection?

Regards,

Tvrtko

Quoted 22 lines
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -1
  		if (r)
@@ -349,7 +349,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
index aed758d0acaa..cf8d700a22fe 100644
@@ -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];
  };
 
#12 lore raw

Re: [PATCH v2 06/12] drm/amdgpu: Remove duplicate struct member

Quoted 30 lines · Tvrtko Ursulin
Quoted 26 lines · Christian König
Quoted 14 lines · Tvrtko Ursulin

Struct 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 +0 -0
index afedea02188d..41c05358d86d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct · line 238

Quoted 8 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);
           r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
                           &num_scheds, &scheds);

Well that code is utterly nonsense to begin with. amdgpu_xcp_select_scheds() needs the xcp id to select from and not fpriv.

Quoted 1 line

Can you look into re-structuring this so that we don't need that cast?

Quoted 7 lines

I had a look and so far only cleanup it up visually a bit so there is fewer long array subscript dereferences and confusion between sel_xcp_id and priv->xcp_id.

But on a more fundamental level, since it needs to write to fpriv, the caller will need to have it one way or the other, no?

And then I noticed not only the atomic_read/inc usage is dodgy, but the fpriv->xcp_id assignment itself is racy. Two threads submitting to the same new entity appears can end up with a refcount imbalance and probably worse.

Shall I replace the ref_cnt atomic with a mutex and protect the whole selection?

Or maybe there is no race?

fpriv->xcp_id is first assigned in amdgpu_driver_open_kms() and there it looks it can mostly fail or succeed. I say mostly because the one silent failure path (not failing the device open) I see if xcp->ddev would be NULL. I am not sure if/when that can happen? If it can happen and that is the reason ctx init needs to retry the xcp_id selection? In which case it is racy.

Regards,

Tvrtko

Quoted 26 lines

Regards,

Tvrtko

Quoted 20 lines
Quoted 19 lines

          if (r)

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
@@ -349,7 +349,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
index aed758d0acaa..cf8d700a22fe 100644
@@ -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];
   };
maintainer
#13 lore raw

Re: [PATCH v2 06/12] drm/amdgpu: Remove duplicate struct member

Quoted 34 lines · Tvrtko Ursulin
Quoted 32 lines · Tvrtko Ursulin
Quoted 24 lines · Christian König
Quoted 14 lines · Tvrtko Ursulin

Struct 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 +0 -0
index afedea02188d..41c05358d86d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 236

Quoted 6 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);
           r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
                           &num_scheds, &scheds);

Well that code is utterly nonsense to begin with. amdgpu_xcp_select_scheds() needs the xcp id to select from and not fpriv.

Quoted 1 line

Can you look into re-structuring this so that we don't need that cast?

I had a look and so far only cleanup it up visually a bit so there is fewer long array subscript dereferences and confusion between sel_xcp_id and priv->xcp_id.

But on a more fundamental level, since it needs to write to fpriv, the caller will need to have it one way or the other, no?

Quoted 7 lines
Quoted 3 lines

And then I noticed not only the atomic_read/inc usage is dodgy, but the fpriv->xcp_id assignment itself is racy. Two threads submitting to the same new entity appears can end up with a refcount imbalance and probably worse.

Shall I replace the ref_cnt atomic with a mutex and protect the whole selection?

Or maybe there is no race?

fpriv->xcp_id is first assigned in amdgpu_driver_open_kms() and there it looks it can mostly fail or succeed. I say mostly because the one silent failure path (not failing the device open) I see if xcp->ddev would be NULL. I am not sure if/when that can happen? If it can happen and that is the reason ctx init needs to retry the xcp_id selection? In which case it is racy.

xcp_id selection can only happen in amdgpu_driver_open_kms(), anytime later and you have completely messed up page tables.

So no xcp code should ever overwrite fpriv->xcp_id. If we have stuff like that then that is clearly a bug as well.

Thanks for pointing that out,
Christian.

Quoted 31 lines

Regards,

Tvrtko

Quoted 25 lines

Regards,

Tvrtko

Quoted 19 lines
Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
           if (r)
@@ -349,7 +349,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
index aed758d0acaa..cf8d700a22fe 100644
@@ -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];
   };
#14 lore raw

Re: [PATCH v2 06/12] drm/amdgpu: Remove duplicate struct member

Quoted 41 lines · Christian König
Quoted 40 lines · Tvrtko Ursulin
Quoted 28 lines · Tvrtko Ursulin
Quoted 24 lines · Christian König
Quoted 14 lines · Tvrtko Ursulin

Struct 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 +0 -0
index afedea02188d..41c05358d86d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip, · line 236

Quoted 6 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);
           r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
                           &num_scheds, &scheds);

Well that code is utterly nonsense to begin with. amdgpu_xcp_select_scheds() needs the xcp id to select from and not fpriv.

Quoted 1 line

Can you look into re-structuring this so that we don't need that cast?

Quoted 7 lines

I had a look and so far only cleanup it up visually a bit so there is fewer long array subscript dereferences and confusion between sel_xcp_id and priv->xcp_id.

But on a more fundamental level, since it needs to write to fpriv, the caller will need to have it one way or the other, no?

And then I noticed not only the atomic_read/inc usage is dodgy, but the fpriv->xcp_id assignment itself is racy. Two threads submitting to the same new entity appears can end up with a refcount imbalance and probably worse.

Shall I replace the ref_cnt atomic with a mutex and protect the whole selection?

Or maybe there is no race?

Quoted 7 lines
Quoted 1 line

fpriv->xcp_id is first assigned in amdgpu_driver_open_kms() and there it looks it can mostly fail or succeed. I say mostly because the one silent failure path (not failing the device open) I see if xcp->ddev would be NULL. I am not sure if/when that can happen? If it can happen and that is the reason ctx init needs to retry the xcp_id selection? In which case it is racy.

xcp_id selection can only happen in amdgpu_driver_open_kms(), anytime later and you have completely messed up page tables.

So no xcp code should ever overwrite fpriv->xcp_id. If we have stuff like that then that is clearly a bug as well.

Thanks for pointing that out,

Only if post open fpriv->xcp_id ends up AMDGPU_XCP_NO_PARTITION due finding an xcp with xcp->ddev == NULL. Again, I am not sure if/when that can happen. But if it does, next ctx entity init will retry via amdgpu_xcp_select_scheds.

Should I keep this patch as is for now and re-sent the series with the other updates? IMO it is still worth dropping the duplicate member even if it is followed up by more work in this area.

Regards,

Tvrtko

Quoted 22 lines
Quoted 21 lines
Quoted 20 lines
Quoted 19 lines
Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -1
           if (r)
@@ -349,7 +349,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
index aed758d0acaa..cf8d700a22fe 100644
@@ -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];
   };
#15 lore raw

PATCH v2 7/12 drm/amdgpu: Remove arbitrary number of contexts limitation

In reply to Tvrtko Ursulin

There 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
index 9f9774f58ce1..80dba6276aa8 100644
@@ -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
index 41c05358d86d..dd1e562dbaed 100644
@@ -484,14 +484,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.51.1
maintainer Reviewed-by on v2 7/12
#16 lore raw

Re: [PATCH v2 07/12] drm/amdgpu: Remove arbitrary number of contexts limitation

Quoted 6 lines · Tvrtko Ursulin

There 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.

Quoted 39 lines
---
 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
index 9f9774f58ce1..80dba6276aa8 100644
@@ -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
index 41c05358d86d..dd1e562dbaed 100644
@@ -484,14 +484,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);
#17 lore raw

PATCH v2 8/12 drm/amdgpu: Consolidate ctx put

In reply to Tvrtko Ursulin

Currently 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 103 +++++++-----------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |   9 ++-
 2 files changed, 38 insertions(+), 74 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +30 -73
index dd1e562dbaed..f3d5a7180bda 100644
@@ -269,26 +269,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)
 {
@@ -403,28 +383,47 @@ 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;
 
 	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]);
 		}
 	}
 
-	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
+	if (drm_dev_enter(adev_to_drm(adev), &i)) {
 		amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
-		drm_dev_exit(idx);
+		drm_dev_exit(i);
 	}
 
 	kfree(ctx);
@@ -502,24 +501,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;
@@ -527,8 +508,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;
 }
@@ -744,15 +724,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)
@@ -921,29 +892,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
index cf8d700a22fe..b1fa7fe74569 100644
@@ -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.51.1
maintainer
#18 lore raw

Re: [PATCH v2 08/12] drm/amdgpu: Consolidate ctx put

Quoted 100 lines · Tvrtko Ursulin

Currently 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 103 +++++++-----------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |   9 ++-
 2 files changed, 38 insertions(+), 74 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +22 -25
index dd1e562dbaed..f3d5a7180bda 100644
@@ -269,26 +269,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)
 {
@@ -403,28 +383,47 @@ 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;
 
 	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);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx, · line 426

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -2
+
+			atomic64_add(ktime_to_ns(t), &mgr->time_spend[i]);
 		}
 	}
 
-	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
+	if (drm_dev_enter(adev_to_drm(adev), &i)) {
 		amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
-		drm_dev_exit(idx);
+		drm_dev_exit(i);

Please keep i and idx separate here.

Apart from that looks good to me.

Regards,
Christian.

Quoted 107 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -46
 	}
 
 	kfree(ctx);
@@ -502,24 +501,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;
@@ -527,8 +508,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;
 }
@@ -744,15 +724,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)
@@ -921,29 +892,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
index cf8d700a22fe..b1fa7fe74569 100644
@@ -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);
#19 lore raw

Re: [PATCH v2 08/12] drm/amdgpu: Consolidate ctx put

Quoted 106 lines · Christian König
Quoted 105 lines · Tvrtko Ursulin

Currently 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.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 103 +++++++-----------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |   9 ++-
  2 files changed, 38 insertions(+), 74 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +24 -25
index dd1e562dbaed..f3d5a7180bda 100644
@@ -269,26 +269,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)
  {
@@ -403,28 +383,47 @@ 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;
 
  	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]);
  		}
  	}
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx, · line 426

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -2
-	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
+	if (drm_dev_enter(adev_to_drm(adev), &i)) {
  		amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
-		drm_dev_exit(idx);
+		drm_dev_exit(i);

Please keep i and idx separate here.

Done.

Regards,

Tvrtko

Quoted 114 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 107 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -46
  	}
 
  	kfree(ctx);
@@ -502,24 +501,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;
@@ -527,8 +508,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;
  }
@@ -744,15 +724,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)
@@ -921,29 +892,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
index cf8d700a22fe..b1fa7fe74569 100644
@@ -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);
#20 lore raw

PATCH v2 9/12 drm/amdgpu: Remove live context error log and skip

In reply to Tvrtko Ursulin

According 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
index f3d5a7180bda..b69dd3061e2c 100644
@@ -894,14 +894,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.51.1
maintainer Reviewed-by on v2 9/12
#21 lore raw

Re: [PATCH v2 09/12] drm/amdgpu: Remove live context error log and skip

Quoted 6 lines · Tvrtko Ursulin

According 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.

Quoted 24 lines
---
 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
index f3d5a7180bda..b69dd3061e2c 100644
@@ -894,14 +894,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)
#22 lore raw

PATCH v2 10/12 drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

In reply to Tvrtko Ursulin

IDR 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 81 +++++++++------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 +--
 4 files changed, 42 insertions(+), 57 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 80dba6276aa8..ec3cbe70012a 100644
@@ -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 +35 -46

134 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -2
index 1acf53f8b2f9..cf127bc66f53 100644
@@ -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
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,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)
@@ -1492,8 +1491,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;
@@ -1529,11 +1528,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.51.1
maintainer
#23 lore raw

Re: [PATCH v2 10/12] drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

Quoted 143 lines · Tvrtko Ursulin

IDR 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 81 +++++++++------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 +--
 4 files changed, 42 insertions(+), 57 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 80dba6276aa8..ec3cbe70012a 100644
@@ -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 +18 -37
index fbac929f711c..59def86cdc04 100644
@@ -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,31 +139,20 @@ 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)
@@ -215,12 +197,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);
@@ -234,19 +216,19 @@ 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;

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data, · line 229

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -3
-		}
 
-		handle = r;
 		break;
 
 	case AMDGPU_BO_LIST_OP_DESTROY:
-		amdgpu_bo_list_destroy(fpriv, handle);
+		list = xa_erase(&fpriv->bo_list_handles, handle);
+		if (list)
+			amdgpu_bo_list_put(list);

It's usually good practice to make *_put() functions take NULL as argument.

Apart from that looks good to me.

Regards,
Christian.

Quoted 94 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +14 -6
 		handle = 0;
+
 		break;
 
 	case AMDGPU_BO_LIST_OP_UPDATE:
@@ -255,16 +237,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
index 1acf53f8b2f9..cf127bc66f53 100644
@@ -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
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,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)
@@ -1492,8 +1491,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;
@@ -1529,11 +1528,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;
#24 lore raw

Re: [PATCH v2 10/12] drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

Quoted 149 lines · Christian König
Quoted 148 lines · Tvrtko Ursulin

IDR 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.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 81 +++++++++------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 +--
  4 files changed, 42 insertions(+), 57 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 80dba6276aa8..ec3cbe70012a 100644
@@ -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 +18 -39
index fbac929f711c..59def86cdc04 100644
@@ -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,31 +139,20 @@ 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)
@@ -215,12 +197,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);
@@ -234,19 +216,19 @@ 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;
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data, · line 229

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -1
  	case AMDGPU_BO_LIST_OP_DESTROY:
-		amdgpu_bo_list_destroy(fpriv, handle);
+		list = xa_erase(&fpriv->bo_list_handles, handle);
+		if (list)
+			amdgpu_bo_list_put(list);

It's usually good practice to make *_put() functions take NULL as argument.

Done.

Regards,

Tvrtko

Quoted 101 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 94 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +14 -6
  		handle = 0;
+
  		break;
 
  	case AMDGPU_BO_LIST_OP_UPDATE:
@@ -255,16 +237,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
index 1acf53f8b2f9..cf127bc66f53 100644
@@ -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
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,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)
@@ -1492,8 +1491,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;
@@ -1529,11 +1528,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;
#25 lore raw

PATCH v2 11/12 drm/amdgpu: Remove output parameter in bo list handling

In reply to Tvrtko Ursulin

Removing 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 | 82 ++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 54 +++++++-------
 3 files changed, 72 insertions(+), 81 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +39 -43
index 59def86cdc04..2c907f9e8258 100644
@@ -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)
@@ -160,8 +158,8 @@ 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)
 {
 	const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
 	const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
@@ -172,26 +170,23 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 	/* 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;
 
 		info = kvmalloc_array(bo_number, info_size, GFP_KERNEL);
 		if (!info)
-			return -ENOMEM;
+			return ERR_PTR(-ENOMEM);
 
 		for (i = 0; i < bo_number; ++i, uptr += bo_info_size) {
 			if (copy_from_user(&info[i], uptr, bytes)) {
 				kvfree(info);
-				return -EFAULT;
+				return ERR_PTR(-EFAULT);
 			}
 		}
 	}
 
-	*info_param = info;
-	return 0;
+	return info;
 }
 
 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
@@ -199,27 +194,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;
 
@@ -231,12 +223,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;
@@ -256,21 +256,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
index cf127bc66f53..bde912150824 100644
@@ -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.51.1
maintainer
#26 lore raw

Re: [PATCH v2 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.

Looks good but probably needs to be rebased on changes in previous commits in this series, so I'm skipping that one for now.

Regards,
Christian.

Quoted 345 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 82 ++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 54 +++++++-------
 3 files changed, 72 insertions(+), 81 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +39 -43
index 59def86cdc04..2c907f9e8258 100644
@@ -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)
@@ -160,8 +158,8 @@ 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)
 {
 	const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
 	const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
@@ -172,26 +170,23 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 	/* 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;
 
 		info = kvmalloc_array(bo_number, info_size, GFP_KERNEL);
 		if (!info)
-			return -ENOMEM;
+			return ERR_PTR(-ENOMEM);
 
 		for (i = 0; i < bo_number; ++i, uptr += bo_info_size) {
 			if (copy_from_user(&info[i], uptr, bytes)) {
 				kvfree(info);
-				return -EFAULT;
+				return ERR_PTR(-EFAULT);
 			}
 		}
 	}
 
-	*info_param = info;
-	return 0;
+	return info;
 }
 
 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
@@ -199,27 +194,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;
 
@@ -231,12 +223,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;
@@ -256,21 +256,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
index cf127bc66f53..bde912150824 100644
@@ -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
index bc9cee3fdf27..69d0cf67d150 100644
@@ -152,24 +152,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 */
@@ -857,6 +852,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;
@@ -869,23 +865,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;
 
@@ -913,7 +910,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);
@@ -933,7 +930,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);
@@ -986,13 +983,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;
 	}
#27 lore raw

PATCH v2 12/12 drm/amdgpu: Convert context manager to xarray

In reply to Tvrtko Ursulin

IDR 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
index b69dd3061e2c..af0375bc11f5 100644
@@ -482,34 +482,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;
 }
 
@@ -518,20 +510,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;
@@ -545,7 +529,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;
 }
 
@@ -557,19 +542,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;
 
@@ -609,7 +586,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;
 }
 
@@ -618,26 +596,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;
 }
 
@@ -716,11 +686,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;
 }
 
@@ -856,8 +826,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);
@@ -866,13 +835,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;
@@ -884,25 +853,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,
@@ -910,21 +875,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;
@@ -938,5 +903,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
index b1fa7fe74569..8427a7d18cf3 100644
@@ -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
index 341beec59537..471d27b2db01 100644
@@ -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.51.1
maintainer Reviewed-by on v2 12/12
#28 lore raw

Re: [PATCH v2 12/12] drm/amdgpu: Convert context manager to xarray

Quoted 10 lines · Tvrtko Ursulin

IDR 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.

Quoted 313 lines

---
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
index b69dd3061e2c..af0375bc11f5 100644
@@ -482,34 +482,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;
 }
 
@@ -518,20 +510,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;
@@ -545,7 +529,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;
 }
 
@@ -557,19 +542,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;
 
@@ -609,7 +586,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;
 }
 
@@ -618,26 +596,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;
 }
 
@@ -716,11 +686,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;
 }
 
@@ -856,8 +826,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);
@@ -866,13 +835,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;
@@ -884,25 +853,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,
@@ -910,21 +875,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;
@@ -938,5 +903,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
index b1fa7fe74569..8427a7d18cf3 100644
@@ -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
index 341beec59537..471d27b2db01 100644
@@ -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;
 }