Skip to contentSkip to conversation

drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

PATCH In review Prike Liang 2 participants 12 messages Nov 10 – Nov 17

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

PATCH 1/6 drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

With MES RS64 process/gang context loading, MES keeps the process contexts in an array in its local memory and looks them up by the slot index the driver passes. SET_SHADER_DEBUGGER has a process_context_array_index field for this, but amdgpu_mes_set_shader_debugger() and amdgpu_mes_flush_shader_debugger() only pass the process context address, so MES always gets slot 0.

KFD sends the flush variant of this packet for every process that exits (kfd_process_dequeue_from_device()). In RS64 mode the flush therefore hits slot 0, which belongs to whichever process allocated it first, instead of the exiting process's slot:
- the owner of slot 0 loses its process context while its queues are still running, so its next GPU accesses fault;
- the exiting process's slot is never flushed, so the next process that gets it starts with a stale context.

So the KFDIPCTest.BasicTest failure on Navi3x was caused on the case when parent holds slot 0, the child's exit flush wipes the parent's process context, and the parent's next access to the shared IPC buffer faults.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c                | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h                | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c                 | 2 ++
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c                 | 3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 1 +
 5 files changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +6 -0
index 7864afab5b40..99b803f95c56 100644
@@ -763,6 +763,7 @@ int amdgpu_mes_hdp_flush(struct amdgpu_device *adev)
 
 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
 				uint64_t process_context_addr,
+				uint32_t process_context_array_index,
 				uint32_t spi_gdbg_per_vmid_cntl,
 				const uint32_t *tcp_watch_cntl,
 				const uint64_t *watch_addresses,
@@ -782,6 +783,8 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
 	op_input.xcc_id = xcc_id;
 	op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
 	op_input.set_shader_debugger.process_context_addr = process_context_addr;
+	op_input.set_shader_debugger.process_context_array_index =
+		process_context_array_index;
 	op_input.set_shader_debugger.flags.u32all = flags;
 
 	/* use amdgpu mes_flush_shader_debugger instead */
@@ -811,6 +814,7 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
 
 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
 				     uint64_t process_context_addr,
+				     uint32_t process_context_array_index,
 				     uint32_t xcc_id)
 {
 	struct mes_misc_op_input op_input = {0};
@@ -825,6 +829,8 @@ int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
 	op_input.xcc_id = xcc_id;
 	op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
 	op_input.set_shader_debugger.process_context_addr = process_context_addr;
+	op_input.set_shader_debugger.process_context_array_index =
+		process_context_array_index;
 	op_input.set_shader_debugger.flags.process_ctx_flush = true;
 
 	amdgpu_mes_lock(&adev->mes);
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +4 -1
index e5dbcf8ef890..9358090165aa 100644
@@ -450,6 +450,7 @@ struct mes_misc_op_input {
 			uint32_t tcp_watch_cntl[4];
 			uint64_t watch_addresses[4];
 			uint32_t trap_en;
+			uint32_t process_context_array_index;
 		} set_shader_debugger;
 
 		struct {
@@ -653,6 +654,7 @@ int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,
 int amdgpu_mes_hdp_flush(struct amdgpu_device *adev);
 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
 				uint64_t process_context_addr,
+				uint32_t process_context_array_index,
 				uint32_t spi_gdbg_per_vmid_cntl,
 				const uint32_t *tcp_watch_cntl,
 				const uint64_t *watch_addresses,
@@ -660,7 +662,8 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
 				bool trap_en,
 				uint32_t xcc_id);
 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
-				uint64_t process_context_addr, uint32_t xcc_id);
+				uint64_t process_context_addr,
+				uint32_t process_context_array_index, uint32_t xcc_id);
 
 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
 						   enum amdgpu_mes_priority_level prio);
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +2 -0
index 60b27278a598..2617c2b7f101 100644
@@ -1043,6 +1043,8 @@ static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
 				upper_32_bits(input->set_shader_debugger.watch_addresses[i]) & 0xffff;
 		}
 		misc_pkt.set_shader_debugger.trap_en = input->set_shader_debugger.trap_en;
+		misc_pkt.set_shader_debugger.process_context_array_index =
+				input->set_shader_debugger.process_context_array_index;
 		break;
 	case MES_MISC_OP_CHANGE_CONFIG:
 		if ((mes->adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) < 0x63) {
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -1
index 71b34a57c2cc..cb830f985536 100644
@@ -373,7 +373,8 @@ int kfd_dbg_set_mes_debug_mode(struct kfd_process_device *pdd, bool sq_trap_en)
 	}
 
 	return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
-					pdd->proc_ctx_gpu_addr, spi_dbg_cntl,
+					pdd->proc_ctx_gpu_addr,
+					pdd->proc_ctx_array_index, spi_dbg_cntl,
 					pdd->watch_points,
 					pdd->watch_addresses,
 					flags, sq_trap_en,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1 -0
index b68e884b1c37..d3149e6d40e4 100644
@@ -96,6 +96,7 @@ void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
 	    down_read_trylock(&dev->adev->reset_domain->sem)) {
 		amdgpu_mes_flush_shader_debugger(dev->adev,
 						 pdd->proc_ctx_gpu_addr,
+						 pdd->proc_ctx_array_index,
 						 ffs(pdd->dev->xcc_mask) - 1);
 		up_read(&dev->adev->reset_domain->sem);
 	}
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/6 drm/amdkfd: allocate the MES process context slot with its buffer

In MES RS64 mode, pqm_create_queue() allocates a process context slot right after the process context buffer, but only on the path where it allocates the buffer itself:
- kfd_dbg_set_mes_debug_mode() allocates the buffer as well, without a slot. A process that a debugger attaches to before it creates its first queue therefore never gets a slot: its queues use slot 0, and it frees slot 0 when it exits, while another process may hold it.
- If no slot is available, the buffer is kept without a slot, and the process's later queues use slot 0 the same way.

Allocate the buffer and the slot together in one helper that both paths use, and free the buffer again if no slot is available.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
---
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 19 ++--------
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      | 35 +++++++++++++++++++
 .../amd/amdkfd/kfd_process_queue_manager.c    | 26 ++------------
 4 files changed, 42 insertions(+), 39 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3 -16
index cb830f985536..d5c87de519ac 100644
@@ -350,27 +350,14 @@ int kfd_dbg_set_mes_debug_mode(struct kfd_process_device *pdd, bool sq_trap_en)
 {
 	uint32_t spi_dbg_cntl = pdd->spi_dbg_override | pdd->spi_dbg_launch_mode;
 	uint32_t flags = pdd->process->dbg_flags;
-	struct amdgpu_device *adev = pdd->dev->adev;
 	int r;
 
 	if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
 		return 0;
 
-	if (!pdd->proc_ctx_cpu_ptr) {
-		r = amdgpu_amdkfd_alloc_kernel_mem(adev,
-			AMDGPU_MES_PROC_CTX_SIZE,
-			AMDGPU_GEM_DOMAIN_GTT,
-			&pdd->proc_ctx_bo,
-			&pdd->proc_ctx_gpu_addr,
-			&pdd->proc_ctx_cpu_ptr,
-			false);
-		if (r) {
-			dev_err(adev->dev,
-			"failed to allocate process context bo\n");
-			return r;
-		}
-		memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
-	}
+	r = kfd_process_device_alloc_proc_ctx(pdd);
+	if (r)
+		return r;
 
 	return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
 					pdd->proc_ctx_gpu_addr,
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 158fa70a1a8e..0a4e8639c4b5 100644
@@ -1140,6 +1140,7 @@ struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *pro
 
 int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id);
 
+int kfd_process_device_alloc_proc_ctx(struct kfd_process_device *pdd);
 int kfd_process_device_init_vm(struct kfd_process_device *pdd,
 			       struct file *drm_file);
 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
drivers/gpu/drm/amd/amdkfd/kfd_process.c +35 -0
index 546721b4d59a..102fcb401646 100644
@@ -1204,6 +1204,41 @@ static void kfd_process_profiler_release(struct kfd_process *p, struct kfd_proce
 	up_read(&adev->reset_domain->sem);
 }
 
+/* Allocate the MES process context and, in RS64 mode, its slot together. */
+int kfd_process_device_alloc_proc_ctx(struct kfd_process_device *pdd)
+{
+	struct amdgpu_device *adev = pdd->dev->adev;
+	int r;
+
+	if (pdd->proc_ctx_cpu_ptr)
+		return 0;
+
+	r = amdgpu_amdkfd_alloc_kernel_mem(adev, AMDGPU_MES_PROC_CTX_SIZE,
+					   AMDGPU_GEM_DOMAIN_GTT,
+					   &pdd->proc_ctx_bo,
+					   &pdd->proc_ctx_gpu_addr,
+					   &pdd->proc_ctx_cpu_ptr, false);
+	if (r) {
+		dev_err(adev->dev, "failed to allocate process context bo\n");
+		return r;
+	}
+	memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
+
+	if (!adev->mes.use_rs64mem)
+		return 0;
+
+	r = amdgpu_mes_alloc_proc_ctx_index(&adev->mes,
+					    &pdd->proc_ctx_array_index);
+	if (r) {
+		dev_err(adev->dev, "failed to allocate process context index\n");
+		amdgpu_amdkfd_free_kernel_mem(adev, &pdd->proc_ctx_bo);
+		pdd->proc_ctx_gpu_addr = 0;
+		pdd->proc_ctx_cpu_ptr = NULL;
+	}
+
+	return r;
+}
+
 static void kfd_process_destroy_pdds(struct kfd_process *p)
 {
 	int i;
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +3 -23
index d3149e6d40e4..4646dfbabe75 100644
@@ -387,30 +387,10 @@ int pqm_create_queue(struct process_queue_manager *pqm,
 		dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
 
 	/* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */
-	if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
-		retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
-						     AMDGPU_MES_PROC_CTX_SIZE,
-						     AMDGPU_GEM_DOMAIN_GTT,
-						     &pdd->proc_ctx_bo,
-						     &pdd->proc_ctx_gpu_addr,
-						     &pdd->proc_ctx_cpu_ptr,
-						     false);
-		if (retval) {
-			dev_err(dev->adev->dev, "failed to allocate process context bo\n");
+	if (dev->kfd->shared_resources.enable_mes) {
+		retval = kfd_process_device_alloc_proc_ctx(pdd);
+		if (retval)
 			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
#2 lore raw

RE: [PATCH 2/6] drm/amdkfd: allocate the MES process context slot with its buffer

AMD General

You can add me as acked . The code changes looks ok to me , but I'm not sure whether the behavior will be changed since according to your description original driver always use slot 0 , now it has its own slot for each process . Not sure whether it's matter .

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 10, 2026 8:05 AM
To: [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 2/6] drm/amdkfd: allocate the MES process context slot with its buffer

In MES RS64 mode, pqm_create_queue() allocates a process context slot right after the process context buffer, but only on the path where it allocates the buffer itself:
- kfd_dbg_set_mes_debug_mode() allocates the buffer as well, without a
slot. A process that a debugger attaches to before it creates its
first queue therefore never gets a slot: its queues use slot 0, and
it frees slot 0 when it exits, while another process may hold it.
- If no slot is available, the buffer is kept without a slot, and the
process's later queues use slot 0 the same way.

Allocate the buffer and the slot together in one helper that both paths use, and free the buffer again if no slot is available.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
---
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 19 ++--------
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      | 35 +++++++++++++++++++
 .../amd/amdkfd/kfd_process_queue_manager.c    | 26 ++------------
 4 files changed, 42 insertions(+), 39 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3 -16
index cb830f985536..d5c87de519ac 100644
@@ -350,27 +350,14 @@ int kfd_dbg_set_mes_debug_mode(struct kfd_process_device *pdd, bool sq_trap_en)  {
        uint32_t spi_dbg_cntl = pdd->spi_dbg_override | pdd->spi_dbg_launch_mode;
        uint32_t flags = pdd->process->dbg_flags;
-       struct amdgpu_device *adev = pdd->dev->adev;
        int r;
 
        if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
                return 0;
 
-       if (!pdd->proc_ctx_cpu_ptr) {
-               r = amdgpu_amdkfd_alloc_kernel_mem(adev,
-                       AMDGPU_MES_PROC_CTX_SIZE,
-                       AMDGPU_GEM_DOMAIN_GTT,
-                       &pdd->proc_ctx_bo,
-                       &pdd->proc_ctx_gpu_addr,
-                       &pdd->proc_ctx_cpu_ptr,
-                       false);
-               if (r) {
-                       dev_err(adev->dev,
-                       "failed to allocate process context bo\n");
-                       return r;
-               }
-               memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
-       }
+       r = kfd_process_device_alloc_proc_ctx(pdd);
+       if (r)
+               return r;
 
        return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
                                        pdd->proc_ctx_gpu_addr,
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 158fa70a1a8e..0a4e8639c4b5 100644
@@ -1140,6 +1140,7 @@ struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *pro
 
 int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id);
 
+int kfd_process_device_alloc_proc_ctx(struct kfd_process_device *pdd);
 int kfd_process_device_init_vm(struct kfd_process_device *pdd,
                               struct file *drm_file);
 struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev, diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c

index 546721b4d59a..102fcb401646 100644

drivers/gpu/drm/amd/amdkfd/kfd_process.c +35 -0
@@ -1204,6 +1204,41 @@ static void kfd_process_profiler_release(struct kfd_process *p, struct kfd_proce
        up_read(&adev->reset_domain->sem);
 }
 
+/* Allocate the MES process context and, in RS64 mode, its slot
+together. */ int kfd_process_device_alloc_proc_ctx(struct
+kfd_process_device *pdd) {
+       struct amdgpu_device *adev = pdd->dev->adev;
+       int r;
+
+       if (pdd->proc_ctx_cpu_ptr)
+               return 0;
+
+       r = amdgpu_amdkfd_alloc_kernel_mem(adev, AMDGPU_MES_PROC_CTX_SIZE,
+                                          AMDGPU_GEM_DOMAIN_GTT,
+                                          &pdd->proc_ctx_bo,
+                                          &pdd->proc_ctx_gpu_addr,
+                                          &pdd->proc_ctx_cpu_ptr, false);
+       if (r) {
+               dev_err(adev->dev, "failed to allocate process context bo\n");
+               return r;
+       }
+       memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
+
+       if (!adev->mes.use_rs64mem)
+               return 0;
+
+       r = amdgpu_mes_alloc_proc_ctx_index(&adev->mes,
+                                           &pdd->proc_ctx_array_index);
+       if (r) {
+               dev_err(adev->dev, "failed to allocate process context index\n");
+               amdgpu_amdkfd_free_kernel_mem(adev, &pdd->proc_ctx_bo);
+               pdd->proc_ctx_gpu_addr = 0;
+               pdd->proc_ctx_cpu_ptr = NULL;
+       }
+
+       return r;
+}
+
 static void kfd_process_destroy_pdds(struct kfd_process *p)  {
        int i;
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +3 -23
index d3149e6d40e4..4646dfbabe75 100644
@@ -387,30 +387,10 @@ int pqm_create_queue(struct process_queue_manager *pqm,
                dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
 
        /* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */
-       if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
-               retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
-                                                    AMDGPU_MES_PROC_CTX_SIZE,
-                                                    AMDGPU_GEM_DOMAIN_GTT,
-                                                    &pdd->proc_ctx_bo,
-                                                    &pdd->proc_ctx_gpu_addr,
-                                                    &pdd->proc_ctx_cpu_ptr,
-                                                    false);
-               if (retval) {
-                       dev_err(dev->adev->dev, "failed to allocate process context bo\n");
+       if (dev->kfd->shared_resources.enable_mes) {
+               retval = kfd_process_device_alloc_proc_ctx(pdd);
+               if (retval)
                        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);

--
2.34.1

#3 lore raw

RE: [PATCH 2/6] drm/amdkfd: allocate the MES process context slot with its buffer

AMD General

Quoted 9 lines

-----Original Message-----
From: Liu, Shaoyun <[email protected]>
Sent: Tuesday, November 17, 2026 12:31 AM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>
Subject: RE: [PATCH 2/6] drm/amdkfd: allocate the MES process context slot with its buffer

Quoted 6 lines

AMD General

You can add me as acked . The code changes looks ok to me , but I'm not sure whether the behavior will be changed since according to your description original driver always use slot 0 , now it has its own slot for each process . Not sure whether it's matter .

Previously, kfd_dbg_set_mes_debug_mode() did not allocate an MES context index slot, defaulting to slot 0. Consequently, when the KFD debugger freed slot 0 on teardown, it inadvertently released a slot that another process was actively using. Fix this by ensuring a valid MES context index slot is properly allocated for the shader debugger.

Regards,
Prike

Quoted 176 lines

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 10, 2026 8:05 AM
To: [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 2/6] drm/amdkfd: allocate the MES process context slot with its buffer

In MES RS64 mode, pqm_create_queue() allocates a process context slot right after the process context buffer, but only on the path where it allocates the buffer itself:
- kfd_dbg_set_mes_debug_mode() allocates the buffer as well, without a slot. A process that a debugger attaches to before it creates its first queue therefore never gets a slot: its queues use slot 0, and it frees slot 0 when it exits, while another process may hold it.
- If no slot is available, the buffer is kept without a slot, and the process's later queues use slot 0 the same way.

Allocate the buffer and the slot together in one helper that both paths use, and free the buffer again if no slot is available.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
---
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 19 ++--------
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      | 35 +++++++++++++++++++
 .../amd/amdkfd/kfd_process_queue_manager.c    | 26 ++------------
 4 files changed, 42 insertions(+), 39 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3 -16
index cb830f985536..d5c87de519ac 100644
@@ -350,27 +350,14 @@ int kfd_dbg_set_mes_debug_mode(struct
 kfd_process_device *pdd, bool sq_trap_en)  {
        uint32_t spi_dbg_cntl = pdd->spi_dbg_override | pdd->spi_dbg_launch_mode;
        uint32_t flags = pdd->process->dbg_flags;
-       struct amdgpu_device *adev = pdd->dev->adev;
        int r;
 
        if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
                return 0;
 
-       if (!pdd->proc_ctx_cpu_ptr) {
-               r = amdgpu_amdkfd_alloc_kernel_mem(adev,
-                       AMDGPU_MES_PROC_CTX_SIZE,
-                       AMDGPU_GEM_DOMAIN_GTT,
-                       &pdd->proc_ctx_bo,
-                       &pdd->proc_ctx_gpu_addr,
-                       &pdd->proc_ctx_cpu_ptr,
-                       false);
-               if (r) {
-                       dev_err(adev->dev,
-                       "failed to allocate process context bo\n");
-                       return r;
-               }
-               memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
-       }
+       r = kfd_process_device_alloc_proc_ctx(pdd);
+       if (r)
+               return r;
 
        return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
                                        pdd->proc_ctx_gpu_addr, diff --git

a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h index 158fa70a1a8e..0a4e8639c4b5 100644

drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
@@ -1140,6 +1140,7 @@ struct kfd_process_device
 *kfd_process_device_data_by_id(struct kfd_process *pro
 
 int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id);
 
+int kfd_process_device_alloc_proc_ctx(struct kfd_process_device *pdd);
 int kfd_process_device_init_vm(struct kfd_process_device *pdd,
                               struct file *drm_file);  struct kfd_process_device

*kfd_bind_process_to_device(struct kfd_node *dev, diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 546721b4d59a..102fcb401646 100644

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -1204,6 +1204,41 @@ static void kfd_process_profiler_release(struct

kfd_process *p, struct kfd_proce

        up_read(&adev->reset_domain->sem);

}

drivers/gpu/drm/amd/amdkfd/kfd_process.c +10 -0
+/* Allocate the MES process context and, in RS64 mode, its slot
+together. */ int kfd_process_device_alloc_proc_ctx(struct
+kfd_process_device *pdd) {
+       struct amdgpu_device *adev = pdd->dev->adev;
+       int r;
+
+       if (pdd->proc_ctx_cpu_ptr)
+               return 0;
+
+       r = amdgpu_amdkfd_alloc_kernel_mem(adev,

AMDGPU_MES_PROC_CTX_SIZE,

drivers/gpu/drm/amd/amdkfd/kfd_process.c +25 -0
+                                          AMDGPU_GEM_DOMAIN_GTT,
+                                          &pdd->proc_ctx_bo,
+                                          &pdd->proc_ctx_gpu_addr,
+                                          &pdd->proc_ctx_cpu_ptr, false);
+       if (r) {
+               dev_err(adev->dev, "failed to allocate process context bo\n");
+               return r;
+       }
+       memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
+
+       if (!adev->mes.use_rs64mem)
+               return 0;
+
+       r = amdgpu_mes_alloc_proc_ctx_index(&adev->mes,
+                                           &pdd->proc_ctx_array_index);
+       if (r) {
+               dev_err(adev->dev, "failed to allocate process context index\n");
+               amdgpu_amdkfd_free_kernel_mem(adev, &pdd->proc_ctx_bo);
+               pdd->proc_ctx_gpu_addr = 0;
+               pdd->proc_ctx_cpu_ptr = NULL;
+       }
+
+       return r;
+}
+
 static void kfd_process_destroy_pdds(struct kfd_process *p)  {
        int i;
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +3 -23
index d3149e6d40e4..4646dfbabe75 100644
@@ -387,30 +387,10 @@ int pqm_create_queue(struct process_queue_manager
 *pqm,
                dev->dqm->ops.register_process(dev->dqm, &pdd->qpd);
 
        /* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */
-       if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
-               retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
-                                                    AMDGPU_MES_PROC_CTX_SIZE,
-                                                    AMDGPU_GEM_DOMAIN_GTT,
-                                                    &pdd->proc_ctx_bo,
-                                                    &pdd->proc_ctx_gpu_addr,
-                                                    &pdd->proc_ctx_cpu_ptr,
-                                                    false);
-               if (retval) {
-                       dev_err(dev->adev->dev, "failed to allocate process context bo\n");
+       if (dev->kfd->shared_resources.enable_mes) {
+               retval = kfd_process_device_alloc_proc_ctx(pdd);
+               if (retval)
                        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);

--
2.34.1

#4 lore raw

PATCH 3/6 drm/amdkfd: fix the MES gang context slot error paths

In reply to Prike Liang

In MES RS64 mode, init_user_queue() allocates a gang context slot for each queue after its gang context buffer, but two error paths don't release what has been allocated:
- If no slot is available, init_user_queue() returns without freeing the gang context buffer.
- If init_user_queue() succeeds but the DQM fails to create the queue, pqm_create_queue() only frees the queue struct. That leaks the slot, and also the gang context buffer and the wptr BO's GART mapping, which already leaked there before RS64.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
---
 .../amd/amdkfd/kfd_process_queue_manager.c    | 28 ++++++++++++-------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +18 -10
index 4646dfbabe75..6cc0365e9b11 100644
@@ -187,6 +187,13 @@ int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
 	return 0;
 }
 
+static void pqm_free_mes_queue_res(struct kfd_node *dev, struct queue *q)
+{
+	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);
+	amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&q->wptr_bo_gart);
+}
+
 static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
 				     struct process_queue_node *pqn)
 {
@@ -211,11 +218,8 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
 		pdd->qpd.num_gws = 0;
 	}
 
-	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);
-	}
+	if (dev->kfd->shared_resources.enable_mes)
+		pqm_free_mes_queue_res(dev, pqn->q);
 }
 
 void pqm_uninit(struct process_queue_manager *pqm)
@@ -270,7 +274,8 @@ static int init_user_queue(struct process_queue_manager *pqm,
 	if (dev->kfd->shared_resources.enable_mes) {
 		if (!q_properties->wptr_bo) {
 			pr_debug("Queue initialization with shared MES requires queue buffers to be initialized\n");
-			return -EINVAL;
+			retval = -EINVAL;
+			goto cleanup;
 		}
 
 		retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
@@ -291,7 +296,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 						&(*q)->gang_ctx_array_index);
 			if (retval) {
 				pr_err("failed to allocate gang context index slot\n");
-				goto cleanup;
+				goto free_gang_ctx_bo;
 			}
 		}
 		/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
@@ -300,7 +305,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 		if (dev->adev != amdgpu_ttm_adev(q_properties->wptr_bo->tbo.bdev)) {
 			pr_err("Queue memory allocated to wrong device\n");
 			retval = -EINVAL;
-			goto free_gang_ctx_bo;
+			goto free_gang_ctx_index;
 		}
 
 		retval = amdgpu_amdkfd_map_gtt_bo_to_gart(q_properties->wptr_bo,
@@ -308,15 +313,16 @@ static int init_user_queue(struct process_queue_manager *pqm,
 							  pqm->process->kgd_process_info);
 		if (retval) {
 			pr_err("Failed to map wptr bo to GART\n");
-			goto free_gang_ctx_bo;
+			goto free_gang_ctx_index;
 		}
 	}
 
 	pr_debug("PQM After init queue");
 	return 0;
 
-free_gang_ctx_bo:
+free_gang_ctx_index:
 	amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
+free_gang_ctx_bo:
 	amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
 	uninit_queue(*q);
@@ -486,6 +492,8 @@ int pqm_create_queue(struct process_queue_manager *pqm,
 	return retval;
 
 err_create_queue:
+	if (q && dev->kfd->shared_resources.enable_mes)
+		pqm_free_mes_queue_res(dev, q);
 	uninit_queue(q);
 	if (kq)
 		kernel_queue_uninit(kq);
Signature · 1 line
2.34.1
#5 lore raw

RE: [PATCH 3/6] drm/amdkfd: fix the MES gang context slot error paths

AMD General

For Patch 3,4,5,6 . You can added me as acked-by :

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 10, 2026 8:05 AM
To: [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 3/6] drm/amdkfd: fix the MES gang context slot error paths

In MES RS64 mode, init_user_queue() allocates a gang context slot for each queue after its gang context buffer, but two error paths don't release what has been allocated:
- If no slot is available, init_user_queue() returns without freeing
the gang context buffer.
- If init_user_queue() succeeds but the DQM fails to create the queue,
pqm_create_queue() only frees the queue struct. That leaks the slot,
and also the gang context buffer and the wptr BO's GART mapping,
which already leaked there before RS64.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
---
 .../amd/amdkfd/kfd_process_queue_manager.c    | 28 ++++++++++++-------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +6 -0
index 4646dfbabe75..6cc0365e9b11 100644
@@ -187,6 +187,13 @@ int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p)
        return 0;
 }
 
+static void pqm_free_mes_queue_res(struct kfd_node *dev, struct queue
+*q) {
+       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);
+       amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&q->wptr_bo_gart); }
+
 static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
                                     struct process_queue_node *pqn)  { @@ -211,11 +218,8 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
                pdd->qpd.num_gws = 0;
        }

- 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);

- }
+ if (dev->kfd->shared_resources.enable_mes)
+ pqm_free_mes_queue_res(dev, pqn->q);
}

void pqm_uninit(struct process_queue_manager *pqm) @@ -270,7 +274,8 @@ static int init_user_queue(struct process_queue_manager *pqm,

        if (dev->kfd->shared_resources.enable_mes) {
                if (!q_properties->wptr_bo) {
                        pr_debug("Queue initialization with shared MES requires queue buffers to be initialized\n");
-                       return -EINVAL;
+                       retval = -EINVAL;
+                       goto cleanup;
                }

                retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev,
@@ -291,7 +296,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
                                                &(*q)->gang_ctx_array_index);
                        if (retval) {
                                pr_err("failed to allocate gang context index slot\n");
-                               goto cleanup;
+                               goto free_gang_ctx_bo;
                        }
                }
                /* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work @@ -300,7 +305,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
                if (dev->adev != amdgpu_ttm_adev(q_properties->wptr_bo->tbo.bdev)) {
                        pr_err("Queue memory allocated to wrong device\n");
                        retval = -EINVAL;
-                       goto free_gang_ctx_bo;
+                       goto free_gang_ctx_index;
                }

                retval = amdgpu_amdkfd_map_gtt_bo_to_gart(q_properties->wptr_bo,
@@ -308,15 +313,16 @@ static int init_user_queue(struct process_queue_manager *pqm,
                                                          pqm->process->kgd_process_info);
                if (retval) {
                        pr_err("Failed to map wptr bo to GART\n");
-                       goto free_gang_ctx_bo;
+                       goto free_gang_ctx_index;
                }
        }

        pr_debug("PQM After init queue");
        return 0;

-free_gang_ctx_bo:
+free_gang_ctx_index:

        amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
+free_gang_ctx_bo:
        amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
        uninit_queue(*q);
@@ -486,6 +492,8 @@ int pqm_create_queue(struct process_queue_manager *pqm,
        return retval;

err_create_queue:
+ if (q && dev->kfd->shared_resources.enable_mes)

+               pqm_free_mes_queue_res(dev, q);
        uninit_queue(q);
        if (kq)
                kernel_queue_uninit(kq);

--
2.34.1

#6 lore raw

PATCH 4/6 drm/amdkfd: pass the gang context slot when resetting a hung MES queue

In reply to Prike Liang

reset_queues_mes() describes the hung queue to amdgpu_gfx_reset_mes_compute() with a remove queue input that lacks the queue's gang context slot. If MES finds no hung queue, amdgpu_gfx_reset_mes_compute() removes this queue with remove_hw_queue(), and in MES RS64 mode MES then removes the gang in slot 0 instead of the queue's.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -0
index 86b70748617b..878159275841 100644
@@ -479,6 +479,7 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
 	queue_input.doorbell_offset = q->properties.doorbell_off;
 	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
 	queue_input.remove_queue_after_reset = false;
 	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
Signature · 1 line
2.34.1
#7 lore raw

PATCH 5/6 drm/amdgpu/mes: keep the RS64 context slot bitmaps across MES re-init

In reply to Prike Liang

mes_v11_0_hw_init() queries the MES context array sizes on every hardware init, including after a GPU reset or a resume, and amdgpu_mes_rs64mem_setup_bitmaps() then allocates new, empty process and gang context slot bitmaps. The old bitmaps leak, and with them the record of which slots live processes and queues still hold, so the next allocation can hand out a slot that is in use, and two processes or queues end up sharing a MES context.

Keep the existing bitmaps when MES is re-initialized. The array sizes come from the firmware and don't change.

Fixes 4ae2e536c643 ("drm/amdgpu/mes11: get MES process/gang contex size")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +4 -0
index 99b803f95c56..b917e6d0cff8 100644
@@ -1076,6 +1076,10 @@ int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
 	if (!mes->use_rs64mem || !mes->ctx_array_size_cpu_ptr)
 		return 0;
 
+	/* Keep the slots live processes and queues hold across MES re-init. */
+	if (mes->proc_ctx_bitmap && mes->gang_ctx_bitmap)
+		return 0;
+
 	/*
 	 * MES FW wrote the sizes to the GPU buffer:
 	 *   ctx_array_size_cpu_ptr[0] = proc_ctx_array_size (N)
Signature · 1 line
2.34.1
#8 lore raw

PATCH 6/6 drm/amdgpu/mes11: enable MES RS64 process/gang context loading again

In reply to Prike Liang

Commit c56525193b5a ("drm/amdgpu/mes11: disable MES RS64 process/gang context loading") turned RS64 context loading off because KFDIPCTest.BasicTest failed with it on Navi31/32/33: the parent's GPU accesses to the shared IPC buffer faulted until MES stopped responding. The previous patches fix the context slot handling that would explain this.

Load the process and gang contexts from MES local memory again, which reduces the MES firmware and driver handshake latency.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -1
index 2617c2b7f101..254a2ffc2429 100644
@@ -1880,7 +1880,7 @@ static int mes_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 	struct amdgpu_device *adev = ip_block->adev;
 	int pipe, r, bo_size;
 
-	adev->mes.use_rs64mem = false;
+	adev->mes.use_rs64mem = true;
 	adev->mes.funcs = &mes_v11_0_funcs;
 	adev->mes.kiq_hw_init = &mes_v11_0_kiq_hw_init;
 	adev->mes.kiq_hw_fini = &mes_v11_0_kiq_hw_fini;
Signature · 1 line
2.34.1
#9 lore raw
In reply to Prike Liang

AMD General

Please also copy process_context_array_index in mes_v12_0.c and mes_v12_1.c. The v12 API has the field.

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 10, 2026 8:05 AM
To: [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 1/6] drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

With MES RS64 process/gang context loading, MES keeps the process contexts in an array in its local memory and looks them up by the slot index the driver passes. SET_SHADER_DEBUGGER has a process_context_array_index field for this, but amdgpu_mes_set_shader_debugger() and amdgpu_mes_flush_shader_debugger()
only pass the process context address, so MES always gets slot 0.

KFD sends the flush variant of this packet for every process that exits (kfd_process_dequeue_from_device()). In RS64 mode the flush therefore hits slot 0, which belongs to whichever process allocated it first, instead of the exiting process's slot:
- the owner of slot 0 loses its process context while its queues are
still running, so its next GPU accesses fault;
- the exiting process's slot is never flushed, so the next process
that gets it starts with a stale context.

So the KFDIPCTest.BasicTest failure on Navi3x was caused on the case when parent holds slot 0, the child's exit flush wipes the parent's process context, and the parent's next access to the shared IPC buffer faults.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c                | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h                | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c                 | 2 ++
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c                 | 3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 1 +
 5 files changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +3 -0
index 7864afab5b40..99b803f95c56 100644
@@ -763,6 +763,7 @@ int amdgpu_mes_hdp_flush(struct amdgpu_device *adev)
 
 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
                                uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
                                uint32_t spi_gdbg_per_vmid_cntl,
                                const uint32_t *tcp_watch_cntl,
                                const uint64_t *watch_addresses,
@@ -782,6 +783,8 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
        op_input.xcc_id = xcc_id;
        op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
        op_input.set_shader_debugger.process_context_addr = process_context_addr;
+       op_input.set_shader_debugger.process_context_array_index =
+               process_context_array_index;
        op_input.set_shader_debugger.flags.u32all = flags;
 
        /* use amdgpu mes_flush_shader_debugger instead */ @@ -811,6 +814,7 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,

int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
uint64_t process_context_addr,
+ uint32_t process_context_array_index,

                                     uint32_t xcc_id)
 {
        struct mes_misc_op_input op_input = {0}; @@ -825,6 +829,8 @@ int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
        op_input.xcc_id = xcc_id;
        op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
        op_input.set_shader_debugger.process_context_addr = process_context_addr;
+       op_input.set_shader_debugger.process_context_array_index =
+               process_context_array_index;
        op_input.set_shader_debugger.flags.process_ctx_flush = true;

        amdgpu_mes_lock(&adev->mes);
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +4 -1
index e5dbcf8ef890..9358090165aa 100644
@@ -450,6 +450,7 @@ struct mes_misc_op_input {
                        uint32_t tcp_watch_cntl[4];
                        uint64_t watch_addresses[4];
                        uint32_t trap_en;
+                       uint32_t process_context_array_index;
                } set_shader_debugger;
 
                struct {
@@ -653,6 +654,7 @@ int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device *adev,  int amdgpu_mes_hdp_flush(struct amdgpu_device *adev);  int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
                                uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
                                uint32_t spi_gdbg_per_vmid_cntl,
                                const uint32_t *tcp_watch_cntl,
                                const uint64_t *watch_addresses,
@@ -660,7 +662,8 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
                                bool trap_en,
                                uint32_t xcc_id);
 int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
-                               uint64_t process_context_addr, uint32_t xcc_id);
+                               uint64_t process_context_addr,
+                               uint32_t process_context_array_index, uint32_t xcc_id);
 
 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device *adev,
                                                   enum amdgpu_mes_priority_level prio); diff --git a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c

index 60b27278a598..2617c2b7f101 100644

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +2 -0
@@ -1043,6 +1043,8 @@ static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
                                upper_32_bits(input->set_shader_debugger.watch_addresses[i]) & 0xffff;
                }
                misc_pkt.set_shader_debugger.trap_en = input->set_shader_debugger.trap_en;
+               misc_pkt.set_shader_debugger.process_context_array_index =
+                               input->set_shader_debugger.process_context_array_index;
                break;
        case MES_MISC_OP_CHANGE_CONFIG:
                if ((mes->adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) < 0x63) { diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c

index 71b34a57c2cc..cb830f985536 100644

drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -1
@@ -373,7 +373,8 @@ int kfd_dbg_set_mes_debug_mode(struct kfd_process_device *pdd, bool sq_trap_en)
        }
 
        return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
-                                       pdd->proc_ctx_gpu_addr, spi_dbg_cntl,
+                                       pdd->proc_ctx_gpu_addr,
+                                       pdd->proc_ctx_array_index, spi_dbg_cntl,
                                        pdd->watch_points,
                                        pdd->watch_addresses,
                                        flags, sq_trap_en,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1 -0
index b68e884b1c37..d3149e6d40e4 100644
@@ -96,6 +96,7 @@ void kfd_process_dequeue_from_device(struct kfd_process_device *pdd)
            down_read_trylock(&dev->adev->reset_domain->sem)) {
                amdgpu_mes_flush_shader_debugger(dev->adev,
                                                 pdd->proc_ctx_gpu_addr,
+                                                pdd->proc_ctx_array_index,
                                                 ffs(pdd->dev->xcc_mask) - 1);
                up_read(&dev->adev->reset_domain->sem);
        }

--
2.34.1

#10 lore raw

AMD General

Yes, I plan to enable MES context index loading for GFX11 first. How about we handle GFX12 and GFX13 in a separate follow-up patch series?

Regards,
Prike

Quoted 182 lines

-----Original Message-----
From: Liu, Shaoyun <[email protected]>
Sent: Monday, November 16, 2026 11:39 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>
Subject: RE: [PATCH 1/6] drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

AMD General

Please also copy process_context_array_index in mes_v12_0.c and mes_v12_1.c. The v12 API has the field.

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 10, 2026 8:05 AM
To: [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 1/6] drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

With MES RS64 process/gang context loading, MES keeps the process contexts in an array in its local memory and looks them up by the slot index the driver passes. SET_SHADER_DEBUGGER has a process_context_array_index field for this, but amdgpu_mes_set_shader_debugger() and amdgpu_mes_flush_shader_debugger() only pass the process context address, so MES always gets slot 0.

KFD sends the flush variant of this packet for every process that exits (kfd_process_dequeue_from_device()). In RS64 mode the flush therefore hits slot 0, which belongs to whichever process allocated it first, instead of the exiting process's slot:
- the owner of slot 0 loses its process context while its queues are still running, so its next GPU accesses fault;
- the exiting process's slot is never flushed, so the next process
that gets it starts with a stale context.

So the KFDIPCTest.BasicTest failure on Navi3x was caused on the case when parent holds slot 0, the child's exit flush wipes the parent's process context, and the parent's next access to the shared IPC buffer faults.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c                | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h                | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c                 | 2 ++
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c                 | 3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 1 +
 5 files changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +1 -0
index 7864afab5b40..99b803f95c56 100644
@@ -763,6 +763,7 @@ int amdgpu_mes_hdp_flush(struct amdgpu_device *adev)
 
 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
                                uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
                                uint32_t spi_gdbg_per_vmid_cntl,
                                const uint32_t *tcp_watch_cntl,
                                const uint64_t *watch_addresses, @@ -782,6 +783,8 @@ int

amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,

        op_input.xcc_id = xcc_id;
        op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
        op_input.set_shader_debugger.process_context_addr = process_context_addr;
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +2 -0
+       op_input.set_shader_debugger.process_context_array_index =
+               process_context_array_index;
        op_input.set_shader_debugger.flags.u32all = flags;
 
        /* use amdgpu mes_flush_shader_debugger instead */ @@ -811,6 +814,7 @@

int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,

int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
uint64_t process_context_addr,
+ uint32_t
+ process_context_array_index,

                                     uint32_t xcc_id)  {
        struct mes_misc_op_input op_input = {0}; @@ -825,6 +829,8 @@ int
amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
        op_input.xcc_id = xcc_id;
        op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
        op_input.set_shader_debugger.process_context_addr = process_context_addr;
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +2 -0
+       op_input.set_shader_debugger.process_context_array_index =
+               process_context_array_index;
        op_input.set_shader_debugger.flags.process_ctx_flush = true;
 
        amdgpu_mes_lock(&adev->mes);
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +2 -0
index e5dbcf8ef890..9358090165aa 100644
@@ -450,6 +450,7 @@ struct mes_misc_op_input {
                        uint32_t tcp_watch_cntl[4];
                        uint64_t watch_addresses[4];
                        uint32_t trap_en;
+                       uint32_t process_context_array_index;
                } set_shader_debugger;
 
                struct {
@@ -653,6 +654,7 @@ int amdgpu_mes_reg_write_reg_wait(struct amdgpu_device
 *adev,  int amdgpu_mes_hdp_flush(struct amdgpu_device *adev);  int
 amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
                                uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
                                uint32_t spi_gdbg_per_vmid_cntl,
                                const uint32_t *tcp_watch_cntl,
                                const uint64_t *watch_addresses, @@ -660,7 +662,8 @@ int

amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,

                                bool trap_en,
                                uint32_t xcc_id);  int

amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +3 -1
-                               uint64_t process_context_addr, uint32_t xcc_id);
+                               uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
+ uint32_t xcc_id);
 
 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct amdgpu_device

*adev,

                                                   enum amdgpu_mes_priority_level prio); diff --git

a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c
b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c
index 60b27278a598..2617c2b7f101 100644

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +3 -0
@@ -1043,6 +1043,8 @@ static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
                                upper_32_bits(input->set_shader_debugger.watch_addresses[i])
 & 0xffff;
                }
                misc_pkt.set_shader_debugger.trap_en = input-
 >set_shader_debugger.trap_en;
+               misc_pkt.set_shader_debugger.process_context_array_index =
+
+ input->set_shader_debugger.process_context_array_index;
                break;
        case MES_MISC_OP_CHANGE_CONFIG:
                if ((mes->adev->mes.sched_version &

AMDGPU_MES_VERSION_MASK) < 0x63) { diff --git
a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index 71b34a57c2cc..cb830f985536 100644

drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3 -1
@@ -373,7 +373,8 @@ int kfd_dbg_set_mes_debug_mode(struct
 kfd_process_device *pdd, bool sq_trap_en)
        }
 
        return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
-                                       pdd->proc_ctx_gpu_addr, spi_dbg_cntl,
+                                       pdd->proc_ctx_gpu_addr,
+                                       pdd->proc_ctx_array_index,
+ spi_dbg_cntl,
                                        pdd->watch_points,
                                        pdd->watch_addresses,
                                        flags, sq_trap_en, diff --git

a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c index b68e884b1c37..d3149e6d40e4 100644

drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +2 -1
@@ -96,6 +96,7 @@ void kfd_process_dequeue_from_device(struct
 kfd_process_device *pdd)
            down_read_trylock(&dev->adev->reset_domain->sem)) {
                amdgpu_mes_flush_shader_debugger(dev->adev,
                                                 pdd->proc_ctx_gpu_addr,
+
+ pdd->proc_ctx_array_index,
                                                 ffs(pdd->dev->xcc_mask) - 1);
                up_read(&dev->adev->reset_domain->sem);
        }
--

2.34.1

#11 lore raw

AMD General

Ok , sounds good to me . you can add me as acked-by

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Monday, November 16, 2026 8:59 PM
To: Liu, Shaoyun <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>
Subject: RE: [PATCH 1/6] drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

AMD General

Yes, I plan to enable MES context index loading for GFX11 first. How about we handle GFX12 and GFX13 in a separate follow-up patch series?

Regards,
Prike

Quoted 193 lines

-----Original Message-----
From: Liu, Shaoyun <[email protected]>
Sent: Monday, November 16, 2026 11:39 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>
Subject: RE: [PATCH 1/6] drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

AMD General

Please also copy process_context_array_index in mes_v12_0.c and mes_v12_1.c. The v12 API has the field.

Regards
Shaoyun.liu

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 10, 2026 8:05 AM
To: [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 1/6] drm/amdgpu/mes: pass the process context slot to SET_SHADER_DEBUGGER

With MES RS64 process/gang context loading, MES keeps the process
contexts in an array in its local memory and looks them up by the slot index the driver passes. SET_SHADER_DEBUGGER has a process_context_array_index field for this,
but
amdgpu_mes_set_shader_debugger() and
amdgpu_mes_flush_shader_debugger()
only pass the process context address, so MES always gets slot 0.

KFD sends the flush variant of this packet for every process that exits (kfd_process_dequeue_from_device()). In RS64 mode the flush therefore hits slot 0, which belongs to whichever process allocated it first, instead of the exiting process's
slot:
- the owner of slot 0 loses its process context while its queues are still running, so its next GPU accesses fault;
- the exiting process's slot is never flushed, so the next process that gets it starts with a stale context.

So the KFDIPCTest.BasicTest failure on Navi3x was caused on the case when parent holds slot 0, the child's exit flush wipes the parent's process context, and the parent's next access to the shared IPC buffer faults.

Fixes e32b68c6b135 ("drm/amdkfd: enable rs64mem for kfd queue")
Assisted-by Copilot:claude-opus-5.5
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c                | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h                | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c                 | 2 ++
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c                 | 3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 1 +
 5 files changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +1 -0
index 7864afab5b40..99b803f95c56 100644
@@ -763,6 +763,7 @@ int amdgpu_mes_hdp_flush(struct amdgpu_device
 *adev)
 
 int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
                                uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
                                uint32_t spi_gdbg_per_vmid_cntl,
                                const uint32_t *tcp_watch_cntl,

const uint64_t *watch_addresses, @@
-782,6 +783,8 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,

        op_input.xcc_id = xcc_id;
        op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
        op_input.set_shader_debugger.process_context_addr =

process_context_addr;

drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +3 -0
+       op_input.set_shader_debugger.process_context_array_index =
+               process_context_array_index;
        op_input.set_shader_debugger.flags.u32all = flags;
 
        /* use amdgpu mes_flush_shader_debugger instead */ @@ -811,6
+814,7 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device

*adev,

int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
uint64_t process_context_addr,
+ uint32_t
+ process_context_array_index,

                                     uint32_t xcc_id)  {
        struct mes_misc_op_input op_input = {0}; @@ -825,6 +829,8 @@
int amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,
        op_input.xcc_id = xcc_id;
        op_input.op = MES_MISC_OP_SET_SHADER_DEBUGGER;
        op_input.set_shader_debugger.process_context_addr =

process_context_addr;

drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +2 -0
+       op_input.set_shader_debugger.process_context_array_index =
+               process_context_array_index;
        op_input.set_shader_debugger.flags.process_ctx_flush = true;
 
        amdgpu_mes_lock(&adev->mes);
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +1 -0
index e5dbcf8ef890..9358090165aa 100644
@@ -450,6 +450,7 @@ struct mes_misc_op_input {
                        uint32_t tcp_watch_cntl[4];
                        uint64_t watch_addresses[4];
                        uint32_t trap_en;
+                       uint32_t process_context_array_index;
                } set_shader_debugger;
 
                struct {
@@ -653,6 +654,7 @@ int amdgpu_mes_reg_write_reg_wait(struct

amdgpu_device *adev, int amdgpu_mes_hdp_flush(struct amdgpu_device *adev); int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,
uint64_t process_context_addr,
+ uint32_t process_context_array_index,
uint32_t spi_gdbg_per_vmid_cntl, const uint32_t *tcp_watch_cntl, const uint64_t *watch_addresses, @@
-660,7 +662,8 @@ int amdgpu_mes_set_shader_debugger(struct amdgpu_device *adev,

                                bool trap_en,
                                uint32_t xcc_id);  int

amdgpu_mes_flush_shader_debugger(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +3 -1
-                               uint64_t process_context_addr, uint32_t xcc_id);
+                               uint64_t process_context_addr,
+                               uint32_t process_context_array_index,
+ uint32_t xcc_id);
 
 uint32_t amdgpu_mes_get_aggregated_doorbell_index(struct

amdgpu_device *adev,
enum
amdgpu_mes_priority_level prio); diff --git a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c
index 60b27278a598..2617c2b7f101 100644

drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +4 -0
@@ -1043,6 +1043,8 @@ static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
 
 upper_32_bits(input->set_shader_debugger.watch_addresses[i])
 & 0xffff;
                }
                misc_pkt.set_shader_debugger.trap_en = input-
 >set_shader_debugger.trap_en;
+
+ misc_pkt.set_shader_debugger.process_context_array_index =
+
+ input->set_shader_debugger.process_context_array_index;
                break;
        case MES_MISC_OP_CHANGE_CONFIG:
                if ((mes->adev->mes.sched_version &

AMDGPU_MES_VERSION_MASK) < 0x63) { diff --git
a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index 71b34a57c2cc..cb830f985536 100644

drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3 -1
@@ -373,7 +373,8 @@ int kfd_dbg_set_mes_debug_mode(struct
 kfd_process_device *pdd, bool sq_trap_en)
        }
 
        return amdgpu_mes_set_shader_debugger(pdd->dev->adev,
-                                       pdd->proc_ctx_gpu_addr, spi_dbg_cntl,
+                                       pdd->proc_ctx_gpu_addr,
+                                       pdd->proc_ctx_array_index,
+ spi_dbg_cntl,
                                        pdd->watch_points,
                                        pdd->watch_addresses,
                                        flags, sq_trap_en, diff --git

a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c index b68e884b1c37..d3149e6d40e4 100644

drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +2 -1
@@ -96,6 +96,7 @@ void kfd_process_dequeue_from_device(struct
 kfd_process_device *pdd)
            down_read_trylock(&dev->adev->reset_domain->sem)) {
                amdgpu_mes_flush_shader_debugger(dev->adev,
 
 pdd->proc_ctx_gpu_addr,
+
+ pdd->proc_ctx_array_index,
                                                 ffs(pdd->dev->xcc_mask) - 1);
                up_read(&dev->adev->reset_domain->sem);
        }
--

2.34.1