Skip to contentSkip to conversation

drm/amdgpu/mes: refactor the amdgpu_mes_alloc/free_proc|gang()

PATCH In review Prike Liang 2 participants 3 messages Aug 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 drm/amdgpu/mes: refactor the amdgpu_mes_alloc/free_proc|gang()

- Unify amdgpu_mes_alloc/free_proc|gang_ctx_index to provide centralized RS64mem bitmap management for both KGD and KFD.
- Retrieve the bitmap bit for userq contex index based on a per process granularity.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c    | 28 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h    |  8 +++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  |  6 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 26 ++++++++++++++------
 5 files changed, 45 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +14 -14
index 9af7c97af5be..5998e05ecd79 100644
@@ -1040,13 +1040,13 @@ int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
  * amdgpu_mes_alloc_proc_ctx_index - allocate a process context slot
  *
  * @mes: MES instance
- * @queue: Usermode queue receiving the allocated process context index
+ * @index: the allocated process context index
  *
  * Returns 0 on success, -ENOSPC if all slots are used, or
  * -EOPNOTSUPP if RS64 local memory is unavailable.
  */
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t *index)
 {
 	unsigned long bit;
 
@@ -1061,7 +1061,7 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
 		return -ENOSPC;
 	}
 	set_bit(bit, mes->proc_ctx_bitmap);
-	queue->proc_ctx_array_index = (uint32_t)bit;
+	*index = (uint32_t)bit;
 	amdgpu_mes_unlock(mes);
 
 	return 0;
@@ -1071,18 +1071,18 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_proc_ctx_index - free a process context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose process context index is released
+  * @index: process context index is released
   */
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t index)
 {
 	if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
 		return;
-	if (queue->proc_ctx_array_index >= mes->proc_ctx_array_size)
+	if (index >= mes->proc_ctx_array_size)
 		return;
 
 	amdgpu_mes_lock(mes);
-	clear_bit(queue->proc_ctx_array_index, mes->proc_ctx_bitmap);
+	clear_bit(index, mes->proc_ctx_bitmap);
 	amdgpu_mes_unlock(mes);
 }
 
@@ -1090,13 +1090,13 @@ void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_alloc_gang_ctx_index - allocate a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue receiving the allocated gang context index
+  * @index: the allocated gang context index
   *
   * Returns 0 on success, -ENOSPC if all slots are used, or
   * -EOPNOTSUPP if RS64 local memory is unavailable.
   */
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t *index)
 {
 	unsigned long bit;
 
@@ -1111,7 +1111,7 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
 		return -ENOSPC;
 	}
 	set_bit(bit, mes->gang_ctx_bitmap);
-	queue->gang_ctx_array_index = bit;
+	*index = bit;
 	amdgpu_mes_unlock(mes);
 
 	return 0;
@@ -1121,18 +1121,18 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_gang_ctx_index - free a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose gang context index is released
+  * @index: gang context index is released
   */
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t index)
 {
 	if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
 		return;
-	if (queue->gang_ctx_array_index >= mes->gang_ctx_array_size)
+	if (index >= mes->gang_ctx_array_size)
 		return;
 
 	amdgpu_mes_lock(mes);
-	clear_bit(queue->gang_ctx_array_index, mes->gang_ctx_bitmap);
+	clear_bit(index, mes->gang_ctx_bitmap);
 	amdgpu_mes_unlock(mes);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +4 -4
index c67db2d6e122..977c057dcce8 100644
@@ -636,11 +636,11 @@ int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes);
 void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes);
 int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes);
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t *index);
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t index);
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t *index);
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t index);
 #endif /* __AMDGPU_MES_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
index 571413c9a611..5dd4fe258cdb 100644
@@ -1244,6 +1244,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
 	xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
 	userq_mgr->adev = adev;
 	userq_mgr->file = file_priv;
+	userq_mgr->proc_ctx_allocated = false;
 	mutex_init(&userq_mgr->proc_ctx_lock);
 
 	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
@@ -1272,6 +1273,7 @@ void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr)
 
 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 {
+	struct amdgpu_mes *mes = &userq_mgr->adev->mes;
 	struct amdgpu_usermode_queue *queue;
 	unsigned long queue_id = 0;
 
@@ -1298,6 +1300,10 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 	 */
 	cancel_work_sync(&userq_mgr->reset_work);
 
+	if (userq_mgr->proc_ctx_allocated) {
+		amdgpu_mes_free_proc_ctx_index(mes, userq_mgr->proc_ctx_array_index);
+		userq_mgr->proc_ctx_allocated = false;
+	}
 	amdgpu_bo_free_kernel(&userq_mgr->proc_ctx_obj.obj,
 			      &userq_mgr->proc_ctx_obj.gpu_addr,
 			      &userq_mgr->proc_ctx_obj.cpu_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -1
index 402dc2816f04..4dcf6151de6a 100644
@@ -101,7 +101,6 @@ struct amdgpu_usermode_queue {
 		u64 va_array[6];
 	} userq_vas;
 
-	uint32_t	proc_ctx_array_index;
 	uint32_t	gang_ctx_array_index;
 };
 
@@ -133,6 +132,8 @@ struct amdgpu_userq_mgr {
 	struct mutex			proc_ctx_lock;
 	struct amdgpu_userq_obj		proc_ctx_obj;
 
+	bool				proc_ctx_allocated;
+	uint32_t			proc_ctx_array_index;
 	/**
 	 * @reset_work:
 	 *
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +19 -7
index a914198a4d62..ae403bb89441 100644
@@ -143,10 +143,24 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
 	queue_input.doorbell_offset = userq_props->doorbell_index;
 	queue_input.page_table_base_addr = amdgpu_gmc_pd_addr(queue->vm->root.bo);
 	queue_input.wptr_mc_addr = queue->wptr_obj.gpu_addr;
+
 	if (mes->use_rs64mem) {
-		amdgpu_mes_alloc_proc_ctx_index(mes, queue);
-		queue_input.process_context_array_index = queue->proc_ctx_array_index;
-		amdgpu_mes_alloc_gang_ctx_index(mes, queue);
+		 if (!uq_mgr->proc_ctx_allocated) {
+			r = amdgpu_mes_alloc_proc_ctx_index(mes, &uq_mgr->proc_ctx_array_index);
+			if (r) {
+				DRM_ERROR("Failed to allocate userq process index err:%d\n", r);
+				return r;
+			}
+			uq_mgr->proc_ctx_allocated = true;
+		}
+
+		r = amdgpu_mes_alloc_gang_ctx_index(mes, &queue->gang_ctx_array_index);
+		if (r) {
+			DRM_ERROR("Failed to allocate userq gang index err:%d\n", r);
+			amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
+			return r;
+		}
+		queue_input.process_context_array_index = uq_mgr->proc_ctx_array_index;
 		queue_input.gang_context_array_index = queue->gang_ctx_array_index;
 	}
 	amdgpu_mes_lock(&adev->mes);
@@ -179,10 +193,8 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
 	amdgpu_mes_unlock(&adev->mes);
-	if (mes->use_rs64mem) {
-		amdgpu_mes_free_proc_ctx_index(mes, queue);
-		amdgpu_mes_free_gang_ctx_index(mes, queue);
-	}
+	if (mes->use_rs64mem)
+		amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
 	if (r)
 		DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
 	return r;
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 2/2 drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation in
the per KFD device process and queue creation.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |  5 ++++-
 .../amd/amdkfd/kfd_process_queue_manager.c    | 22 ++++++++++++++++++-
 4 files changed, 31 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -0
index 51ee9c39104b..ea9d87450eae 100644
@@ -226,8 +226,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	/* MES unit for quantum is 100ns */
 	queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /* Equivalent to 10ms. */
 	queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
+	queue_input.process_context_array_index = pdd->proc_ctx_array_index;
 	queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
 	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 	queue_input.inprocess_gang_priority = q->properties.priority;
 	queue_input.gang_global_priority_level =
 					AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
@@ -303,6 +305,7 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
 	queue_input.remove_queue_after_reset = flush_mes_queue;
 	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index bcb929002839..2ea1cfd330a9 100644
@@ -635,6 +635,7 @@ struct queue {
 	void *gang_ctx_bo;
 	uint64_t gang_ctx_gpu_addr;
 	void *gang_ctx_cpu_ptr;
+	uint32_t gang_ctx_array_index;
 
 	struct amdgpu_bo *wptr_bo_gart;
 };
@@ -871,6 +872,8 @@ struct kfd_process_device {
 	uint64_t proc_ctx_gpu_addr;
 	void *proc_ctx_cpu_ptr;
 
+	uint32_t proc_ctx_array_index;
+
 	/* Tracks queue reset status */
 	bool has_reset_queue;
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -1
index eb508fe3ded7..0a7c1900da95 100644
@@ -1215,9 +1215,12 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 		kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
 		if (pdd->dev->kfd->shared_resources.enable_mes &&
-			pdd->proc_ctx_cpu_ptr)
+			pdd->proc_ctx_cpu_ptr) {
+			amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
+						       pdd->proc_ctx_array_index);
 			amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
 						   &pdd->proc_ctx_bo);
+		}
 		/*
 		 * before destroying pdd, make sure to report availability
 		 * for auto suspend
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +21 -1
index 8054e4fe0381..4b9f195b0ef2 100644
@@ -210,6 +210,7 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
 	}
 
 	if (dev->kfd->shared_resources.enable_mes) {
+		amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index);
 		amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
 		amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
 	}
@@ -282,7 +283,15 @@ static int init_user_queue(struct process_queue_manager *pqm,
 			goto cleanup;
 		}
 		memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
-
+		/* Bind one MES gang context slot per queue (gang). */
+		if (dev->adev->mes.use_rs64mem) {
+			retval = amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
+						&(*q)->gang_ctx_array_index);
+			if (retval) {
+				pr_err("failed to allocate gang context index slot\n");
+				goto cleanup;
+			}
+		}
 		/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
 		 * on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
 		 */
@@ -304,6 +313,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 	return 0;
 
 free_gang_ctx_bo:
+	amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
 	amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
 	uninit_queue(*q);
@@ -386,6 +396,16 @@ int pqm_create_queue(struct process_queue_manager *pqm,
 			goto err_allocate_pqn;
 		}
 		memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
+		/* Bind one MES process context slot to the whole process
+		 * (per device); every queue of this process reuses it.
+		 */
+		if (dev->adev->mes.use_rs64mem)
+			retval = amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
+						&pdd->proc_ctx_array_index);
+			if (retval) {
+				dev_err(dev->adev->dev, "failed to allocate process context index\n");
+				goto err_allocate_pqn;
+			}
 	}
 
 	pqn = kzalloc_obj(*pqn);
Signature · 1 line
2.34.1
Reviewed-by on v3 1/2
#2 lore raw
In reply to Prike Liang

AMD General

One inline comment. With that fixed, the series is

________________________________
From: Liang, Prike <[email protected]>
Sent: Monday, August 3, 2026 11:16 PM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>; Liu, Shaoyun <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v3 1/2] drm/amdgpu/mes: refactor the amdgpu_mes_alloc/free_proc|gang()

- Unify amdgpu_mes_alloc/free_proc|gang_ctx_index to provide centralized RS64mem bitmap management for both KGD and KFD.
- Retrieve the bitmap bit for userq contex index based on a per process granularity.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c    | 28 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h    |  8 +++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  |  6 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 26 ++++++++++++++------
 5 files changed, 45 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +14 -14
index 9af7c97af5be..5998e05ecd79 100644
@@ -1040,13 +1040,13 @@ int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
  * amdgpu_mes_alloc_proc_ctx_index - allocate a process context slot
  *
  * @mes: MES instance
- * @queue: Usermode queue receiving the allocated process context index
+ * @index: the allocated process context index
  *
  * Returns 0 on success, -ENOSPC if all slots are used, or
  * -EOPNOTSUPP if RS64 local memory is unavailable.
  */
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue)
+                                   uint32_t *index)
 {
         unsigned long bit;
 
@@ -1061,7 +1061,7 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
                 return -ENOSPC;
         }
         set_bit(bit, mes->proc_ctx_bitmap);
-       queue->proc_ctx_array_index = (uint32_t)bit;
+       *index = (uint32_t)bit;
         amdgpu_mes_unlock(mes);
 
         return 0;
@@ -1071,18 +1071,18 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_proc_ctx_index - free a process context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose process context index is released
+  * @index: process context index is released
   */
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue)
+                                   uint32_t index)
 {
         if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
                 return;
-       if (queue->proc_ctx_array_index >= mes->proc_ctx_array_size)
+       if (index >= mes->proc_ctx_array_size)
                 return;
 
         amdgpu_mes_lock(mes);
-       clear_bit(queue->proc_ctx_array_index, mes->proc_ctx_bitmap);
+       clear_bit(index, mes->proc_ctx_bitmap);
         amdgpu_mes_unlock(mes);
 }
 
@@ -1090,13 +1090,13 @@ void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_alloc_gang_ctx_index - allocate a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue receiving the allocated gang context index
+  * @index: the allocated gang context index
   *
   * Returns 0 on success, -ENOSPC if all slots are used, or
   * -EOPNOTSUPP if RS64 local memory is unavailable.
   */
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue)
+                                   uint32_t *index)
 {
         unsigned long bit;
 
@@ -1111,7 +1111,7 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
                 return -ENOSPC;
         }
         set_bit(bit, mes->gang_ctx_bitmap);
-       queue->gang_ctx_array_index = bit;
+       *index = bit;
         amdgpu_mes_unlock(mes);
 
         return 0;
@@ -1121,18 +1121,18 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_gang_ctx_index - free a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose gang context index is released
+  * @index: gang context index is released
   */
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue)
+                                   uint32_t index)
 {
         if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
                 return;
-       if (queue->gang_ctx_array_index >= mes->gang_ctx_array_size)
+       if (index >= mes->gang_ctx_array_size)
                 return;
 
         amdgpu_mes_lock(mes);
-       clear_bit(queue->gang_ctx_array_index, mes->gang_ctx_bitmap);
+       clear_bit(index, mes->gang_ctx_bitmap);
         amdgpu_mes_unlock(mes);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +4 -4
index c67db2d6e122..977c057dcce8 100644
@@ -636,11 +636,11 @@ int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes);
 void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes);
 int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes);
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t *index);
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t index);
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t *index);
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t index);
 #endif /* __AMDGPU_MES_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
index 571413c9a611..5dd4fe258cdb 100644
@@ -1244,6 +1244,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
         xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
         userq_mgr->adev = adev;
         userq_mgr->file = file_priv;
+       userq_mgr->proc_ctx_allocated = false;
         mutex_init(&userq_mgr->proc_ctx_lock);
 
         INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
@@ -1272,6 +1273,7 @@ void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr)
 
 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 {
+       struct amdgpu_mes *mes = &userq_mgr->adev->mes;
         struct amdgpu_usermode_queue *queue;
         unsigned long queue_id = 0;
 
@@ -1298,6 +1300,10 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
          */
         cancel_work_sync(&userq_mgr->reset_work);
 
+       if (userq_mgr->proc_ctx_allocated) {
+               amdgpu_mes_free_proc_ctx_index(mes, userq_mgr->proc_ctx_array_index);
+               userq_mgr->proc_ctx_allocated = false;
+       }
         amdgpu_bo_free_kernel(&userq_mgr->proc_ctx_obj.obj,
                               &userq_mgr->proc_ctx_obj.gpu_addr,
                               &userq_mgr->proc_ctx_obj.cpu_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -1
index 402dc2816f04..4dcf6151de6a 100644
@@ -101,7 +101,6 @@ struct amdgpu_usermode_queue {
                 u64 va_array[6];
         } userq_vas;
 
-       uint32_t        proc_ctx_array_index;
         uint32_t        gang_ctx_array_index;
 };
 
@@ -133,6 +132,8 @@ struct amdgpu_userq_mgr {
         struct mutex                    proc_ctx_lock;
         struct amdgpu_userq_obj         proc_ctx_obj;
 
+       bool                            proc_ctx_allocated;
+       uint32_t                        proc_ctx_array_index;
         /**
          * @reset_work:
          *
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +14 -3
index a914198a4d62..ae403bb89441 100644
@@ -143,10 +143,24 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
         queue_input.doorbell_offset = userq_props->doorbell_index;
         queue_input.page_table_base_addr = amdgpu_gmc_pd_addr(queue->vm->root.bo);
         queue_input.wptr_mc_addr = queue->wptr_obj.gpu_addr;
+
         if (mes->use_rs64mem) {
-               amdgpu_mes_alloc_proc_ctx_index(mes, queue);
-               queue_input.process_context_array_index = queue->proc_ctx_array_index;
-               amdgpu_mes_alloc_gang_ctx_index(mes, queue);
+                if (!uq_mgr->proc_ctx_allocated) {
+                       r = amdgpu_mes_alloc_proc_ctx_index(mes, &uq_mgr->proc_ctx_array_index);
+                       if (r) {
+                               DRM_ERROR("Failed to allocate userq process index err:%d\n", r);
+                               return r;
+                       }
+                       uq_mgr->proc_ctx_allocated = true;
+               }
+
+               r = amdgpu_mes_alloc_gang_ctx_index(mes, &queue->gang_ctx_array_index);
+               if (r) {
+                       DRM_ERROR("Failed to allocate userq gang index err:%d\n", r);
+                       amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
 

No need to call free on failure, otherwise it will free index '0' accidentally.

+ return r;
+ }

+               queue_input.process_context_array_index = uq_mgr->proc_ctx_array_index;
                 queue_input.gang_context_array_index = queue->gang_ctx_array_index;
         }
         amdgpu_mes_lock(&adev->mes);
@@ -179,10 +193,8 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
         amdgpu_mes_lock(&adev->mes);
         r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
         amdgpu_mes_unlock(&adev->mes);

- if (mes->use_rs64mem) {
- amdgpu_mes_free_proc_ctx_index(mes, queue);
- amdgpu_mes_free_gang_ctx_index(mes, queue);
- }
+ if (mes->use_rs64mem)

+               amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
         if (r)
                 DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
         return r;

--
2.34.1