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")
Signed-off-by: Prike Liang <[email protected]>
---
 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(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index cb830f985536..d5c87de519ac 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
@@ -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
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -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
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -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;
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 d3149e6d40e4..4646dfbabe75 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
@@ -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

Reply via email to