All of lore.kernel.org
 help / color / mirror / Atom feed
* [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging
@ 2026-11-19  9:04 Jesse Zhang
  2026-11-19  9:04 ` [v2 2/3] drm/amdkfd: return the memory violation data to the debugger Jesse Zhang
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Jesse Zhang @ 2026-11-19  9:04 UTC (permalink / raw)
  To: amd-gfx
  Cc: Alexander.Deucher, Felix.Kuehling, Jonathan.Kim, Jesse Zhang,
	Jonathan Kim

Debug activation and the debug operations go through all devices of the
process, also the ones it never acquired a VM on. With a dGPU and an
iGPU without per VMID debugging, debugging a process that only uses the
dGPU still takes the single debug VMID of the iGPU, so a second debugger
fails:

  amdgpu 0000:05:00.0: Trap debug id already reserved

and a trap override the iGPU doesn't support fails with -EACCES.

A process isn't expected to use a device it hasn't acquired by the time
its runtime is enabled. Only activate debugging on the devices with a VM
and track it in debug_active, set once the debug VMID is reserved so the
unwind also undoes the device that failed. Deactivation and the debug
operations only go over those devices and address watches on other
devices fail with -ENODEV. A device the process still acquires later is
only debugged from the next attach.

As kfd_dbg_trap_set_flags() now skips devices, rewind up to the last
updated device instead of using the number of updated devices as the
bound.

Suggested-by: Jonathan Kim <jonathan.kim@amd.com>
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 +-
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 60 ++++++++++++-------
 drivers/gpu/drm/amd/amdkfd/kfd_debug.h        |  2 +-
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |  6 ++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 +
 5 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 3d2136adb62b..3d768511d002 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -3039,7 +3039,7 @@ static int runtime_disable(struct kfd_process *p)
 
 	if (p->debug_trap_enabled) {
 		if (was_enabled)
-			kfd_dbg_trap_deactivate(p, false, 0);
+			kfd_dbg_trap_deactivate(p, false);
 
 		if (!p->is_runtime_retry)
 			kfd_dbg_ev_raise(KFD_EC_MASK(EC_PROCESS_RUNTIME),
@@ -3219,7 +3219,7 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v
 					args->clear_node_address_watch.gpu_id);
 
 		pdd = kfd_process_device_data_by_id(target, user_gpu_id);
-		if (user_gpu_id == -EINVAL || !pdd) {
+		if (user_gpu_id == -EINVAL || !pdd || !pdd->debug_active) {
 			r = -ENODEV;
 			goto unlock_out;
 		}
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index d5c87de519ac..b2d01613a21c 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
@@ -504,9 +504,13 @@ static void kfd_dbg_clear_process_address_watch(struct kfd_process *target)
 {
 	int i, j;
 
-	for (i = 0; i < target->n_pdds; i++)
+	for (i = 0; i < target->n_pdds; i++) {
+		if (!target->pdds[i]->debug_active)
+			continue;
+
 		for (j = 0; j < MAX_WATCH_ADDRESSES; j++)
 			kfd_dbg_trap_clear_dev_address_watch(target->pdds[i], j);
+	}
 }
 
 int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
@@ -517,8 +521,12 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 	for (i = 0; i < target->n_pdds; i++) {
 		uint32_t caps;
 		uint32_t caps2;
-		struct kfd_topology_device *topo_dev =
-			kfd_topology_device_by_id(target->pdds[i]->dev->id);
+		struct kfd_topology_device *topo_dev;
+
+		if (!target->pdds[i]->debug_active)
+			continue;
+
+		topo_dev = kfd_topology_device_by_id(target->pdds[i]->dev->id);
 		if (!topo_dev)
 			return -EINVAL;
 
@@ -549,7 +557,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 	for (i = 0; i < target->n_pdds; i++) {
 		struct kfd_process_device *pdd = target->pdds[i];
 
-		if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
+		if (!pdd->debug_active || !kfd_dbg_is_per_vmid_supported(pdd->dev))
 			continue;
 
 		if (!pdd->dev->kfd->shared_resources.enable_mes)
@@ -562,7 +570,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 			break;
 		}
 
-		rewind_count++;
+		rewind_count = i + 1;
 	}
 
 	/* Rewind flags */
@@ -572,7 +580,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 		for (i = 0; i < rewind_count; i++) {
 			struct kfd_process_device *pdd = target->pdds[i];
 
-			if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
+			if (!pdd->debug_active ||
+			    !kfd_dbg_is_per_vmid_supported(pdd->dev))
 				continue;
 
 			if (!pdd->dev->kfd->shared_resources.enable_mes)
@@ -588,12 +597,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 /* kfd_dbg_trap_deactivate:
  *	target: target process
  *	unwind: If this is unwinding a failed kfd_dbg_trap_enable()
- *	unwind_count:
- *		If unwind == true, how far down the pdd list we need
- *				to unwind
- *		else: ignored
  */
-void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwind_count)
+void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind)
 {
 	int i;
 
@@ -614,12 +619,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwind
 	for (i = 0; i < target->n_pdds; i++) {
 		struct kfd_process_device *pdd = target->pdds[i];
 
-		/* If this is an unwind, and we have unwound the required
-		 * enable calls on the pdd list, we need to stop now
-		 * otherwise we may mess up another debugger session.
-		 */
-		if (unwind && i == unwind_count)
-			break;
+		if (!pdd->debug_active)
+			continue;
+		pdd->debug_active = false;
 
 		kfd_process_set_trap_debug_flag(&pdd->qpd, false);
 
@@ -684,7 +686,7 @@ int kfd_dbg_trap_disable(struct kfd_process *target, bool drain_irqs)
 	 * attached running target runtime state to enable for re-attach.
 	 */
 	if (target->runtime_info.runtime_state == DEBUG_RUNTIME_STATE_ENABLED)
-		kfd_dbg_trap_deactivate(target, false, 0);
+		kfd_dbg_trap_deactivate(target, false);
 	else if (target->runtime_info.runtime_state != DEBUG_RUNTIME_STATE_DISABLED)
 		target->runtime_info.runtime_state = DEBUG_RUNTIME_STATE_ENABLED;
 
@@ -715,6 +717,9 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
 	for (i = 0; i < target->n_pdds; i++) {
 		struct kfd_process_device *pdd = target->pdds[i];
 
+		if (!pdd->drm_priv)
+			continue;
+
 		if (!kfd_dbg_is_per_vmid_supported(pdd->dev)) {
 			r = reserve_debug_trap_vmid(pdd->dev->dqm, &pdd->qpd);
 
@@ -727,6 +732,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
 			}
 		}
 
+		pdd->debug_active = true;
+
 		/* Disable GFX OFF to prevent garbage read/writes to debug registers.
 		 * If RLC restore of debug registers is not supported and runtime enable
 		 * hasn't done so already on ttmp setup request, restore the trap config registers.
@@ -775,7 +782,7 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
 	/* Enabling debug failed, we need to disable on
 	 * all GPUs so the enable is all or nothing.
 	 */
-	kfd_dbg_trap_deactivate(target, true, i);
+	kfd_dbg_trap_deactivate(target, true);
 	return r;
 }
 
@@ -825,7 +832,7 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 		atomic_inc(&target->debugger_process->debugged_process_count);
 
 	if (copy_to_user(runtime_info, (void *)&target->runtime_info, copy_size)) {
-		kfd_dbg_trap_deactivate(target, false, 0);
+		kfd_dbg_trap_deactivate(target, false);
 		fput(target->dbg_ev_file);
 		target->dbg_ev_file = NULL;
 		if (target->debugger_process)
@@ -851,7 +858,12 @@ static int kfd_dbg_validate_trap_override_request(struct kfd_process *p,
 
 	for (i = 0; i < p->n_pdds; i++) {
 		struct kfd_process_device *pdd = p->pdds[i];
-		int err = pdd->dev->kfd2kgd->validate_trap_override_request(
+		int err;
+
+		if (!pdd->debug_active)
+			continue;
+
+		err = pdd->dev->kfd2kgd->validate_trap_override_request(
 								pdd->dev->adev,
 								trap_override,
 								trap_mask_supported);
@@ -886,6 +898,9 @@ int kfd_dbg_trap_set_wave_launch_override(struct kfd_process *target,
 	for (i = 0; i < target->n_pdds; i++) {
 		struct kfd_process_device *pdd = target->pdds[i];
 
+		if (!pdd->debug_active)
+			continue;
+
 		amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
 		pdd->spi_dbg_override = pdd->dev->kfd2kgd->set_wave_launch_trap_override(
 				pdd->dev->adev,
@@ -922,6 +937,9 @@ int kfd_dbg_trap_set_wave_launch_mode(struct kfd_process *target,
 	for (i = 0; i < target->n_pdds; i++) {
 		struct kfd_process_device *pdd = target->pdds[i];
 
+		if (!pdd->debug_active)
+			continue;
+
 		amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
 		pdd->spi_dbg_launch_mode = pdd->dev->kfd2kgd->set_wave_launch_mode(
 				pdd->dev->adev,
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
index 5babf9b992cd..e93d77fccda2 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
@@ -25,7 +25,7 @@
 
 #include "kfd_priv.h"
 
-void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwind_count);
+void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind);
 int kfd_dbg_trap_activate(struct kfd_process *target);
 int kfd_dbg_ev_query_debug_event(struct kfd_process *process,
 			unsigned int *queue_id,
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
index b5d7d13f31a5..b8ef93dba0af 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
@@ -3851,6 +3851,9 @@ int resume_queues(struct kfd_process *p,
 		struct queue *q;
 		int r, per_device_resumed = 0;
 
+		if (!pdd->debug_active)
+			continue;
+
 		dqm_lock(dqm);
 
 		/* unmask queues that resume or already resumed as valid */
@@ -3954,6 +3957,9 @@ int suspend_queues(struct kfd_process *p,
 		struct queue *q;
 		int r, per_device_suspended = 0;
 
+		if (!pdd->debug_active)
+			continue;
+
 		mutex_lock(&p->event_mutex);
 		dqm_lock(dqm);
 
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 129363ad4e34..ed85b7fb896e 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -880,6 +880,8 @@ struct kfd_process_device {
 	uint32_t watch_points[4];
 	uint64_t watch_addresses[4];
 	uint32_t alloc_watch_ids;
+	/* Debugging is activated on the device */
+	bool debug_active;
 
 	/*
 	 * If this process has been checkpointed before, then the user
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [v2 2/3] drm/amdkfd: return the memory violation data to the debugger
  2026-11-19  9:04 [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging Jesse Zhang
@ 2026-11-19  9:04 ` Jesse Zhang
  2026-11-19  9:04 ` [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails Jesse Zhang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Jesse Zhang @ 2026-11-19  9:04 UTC (permalink / raw)
  To: amd-gfx
  Cc: Alexander.Deucher, Felix.Kuehling, Jonathan.Kim, Jesse Zhang,
	Jonathan Kim

kfd_dbg_ev_raise() saves the memory exception data of a GPU VM fault for a
debugger subscribed to EC_DEVICE_MEMORY_VIOLATION, but never sets
vm_fault_exc_data_size. QUERY_EXCEPTION_INFO copies that many bytes, so the
debugger always gets 0 bytes and no fault address.

With a process debugging itself and a KFD SDMA queue writing to an
unmapped VA:

  amdgpu 0000:03:00.0: [gfxhub] page fault (src_id:0 ring:40 vmid:8 pasid:63)
  amdgpu 0000:03:00.0:   in page starting at address 0x0000000060200000 from client 10
  amdgpu 0000:03:00.0: 	 Faulty UTCL2 client ID: SDMA0 (0xd)
  memory violation info: 0 bytes, va 0x0 gpu_id 0, fault at 0x60200000

With the size set:

  memory violation info: 32 bytes, va 0x60200000 gpu_id 51573, fault at 0x60200000

The data is also only freed by a query clearing the exception, free it with
the process device as well.

Fixes: 2b36de971d25 ("drm/amdkfd: add debug query exception info operation")
Reviewed-by: Jonathan Kim <jonathan.kim@amd.com>
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c   | 4 +++-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 1 +
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index b2d01613a21c..cfd43e417db9 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
@@ -143,7 +143,9 @@ bool kfd_dbg_ev_raise(uint64_t event_mask,
 							exception_data,
 							exception_data_size,
 							GFP_KERNEL);
-					if (!pdd->vm_fault_exc_data)
+					if (pdd->vm_fault_exc_data)
+						pdd->vm_fault_exc_data_size = exception_data_size;
+					else
 						pr_debug("Failed to allocate exception data memory");
 				} else {
 					pr_debug("Debugger exception data not saved\n");
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index f959f19d92d2..d57eebec063c 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -1288,6 +1288,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 		atomic_dec(&pdd->dev->kfd->kfd_processes_count);
 		mutex_destroy(&pdd->doorbell_mutex);
 
+		kfree(pdd->vm_fault_exc_data);
 		kfree(pdd);
 		p->pdds[i] = NULL;
 	}
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
  2026-11-19  9:04 [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging Jesse Zhang
  2026-11-19  9:04 ` [v2 2/3] drm/amdkfd: return the memory violation data to the debugger Jesse Zhang
@ 2026-11-19  9:04 ` Jesse Zhang
  2026-11-19 18:46   ` Kim, Jonathan
  2026-11-19  9:26 ` [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging sashiko-bot
  2026-11-19 17:17 ` Kim, Jonathan
  3 siblings, 1 reply; 9+ messages in thread
From: Jesse Zhang @ 2026-11-19  9:04 UTC (permalink / raw)
  To: amd-gfx; +Cc: Alexander.Deucher, Felix.Kuehling, Jonathan.Kim, Jesse Zhang

KFD_IOC_DBG_TRAP_ENABLE stores the caller in target->debugger_process
before calling kfd_dbg_trap_enable(), and nothing resets it when that
fails (-EALREADY, -ENODEV, -EBUSY, -EBADF for a bad dbg_fd, -EFAULT).
The permission check at the top of kfd_ioctl_set_debug_trap() accepts
any caller equal to target->debugger_process, so the caller keeps
debug access after it stopped ptracing the target. The pointer is also
not reference counted, so it dangles once the caller exits, and a later
successful ENABLE by the target itself increments
debugged_process_count of the freed process.

A parent that ptraces its child, tries to enable debugging with
dbg_fd = -1 (-EBADF) and then detaches can still enable debugging on
the child with a valid fd:

  1. ptraced, ENABLE with dbg_fd=-1      -> -9 (Bad file descriptor)
  2. ptrace detached
  3. not ptraced, ENABLE with valid fd  -> 0 (success)

Clear the pointer again when enabling fails, a session that isn't
enabled has no debugger. Step 3 now fails with -EPERM.

Return -EALREADY for an already enabled session before touching the
pointer at all: the exit of the debugger of that session looks for its
targets through target->debugger_process before it takes
target->mutex, so even a temporary change of the pointer could make it
skip the target and leave a dangling pointer behind.

v2: clear the pointer instead of restoring it, no local variable and
    braces needed; clarify the comment (Jon)

Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and disable")
Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 3d768511d002..9256a2468081 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -3227,6 +3227,16 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v
 
 	switch (args->op) {
 	case KFD_IOC_DBG_TRAP_ENABLE:
+		/*
+		 * Don't change the debugger of an enabled session, not even
+		 * temporarily: the exit of that debugger looks for its targets
+		 * by this pointer before it takes target->mutex.
+		 */
+		if (target->debug_trap_enabled) {
+			r = -EALREADY;
+			break;
+		}
+
 		if (target != p)
 			target->debugger_process = p;
 
@@ -3236,6 +3246,9 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v
 					&args->enable.rinfo_size);
 		if (!r)
 			target->exception_enable_mask = args->enable.exception_mask;
+		else
+			/* A session that isn't enabled has no debugger */
+			target->debugger_process = NULL;
 
 		break;
 	case KFD_IOC_DBG_TRAP_DISABLE:
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging
  2026-11-19  9:04 [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging Jesse Zhang
  2026-11-19  9:04 ` [v2 2/3] drm/amdkfd: return the memory violation data to the debugger Jesse Zhang
  2026-11-19  9:04 ` [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails Jesse Zhang
@ 2026-11-19  9:26 ` sashiko-bot
  2026-11-19 17:17 ` Kim, Jonathan
  3 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-19  9:26 UTC (permalink / raw)
  To: Jesse Zhang, amd-gfx

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/amdkfd: debug VMID hijack on non-active device in
  pm_map_process_v9()
- [Medium] drm/amdkfd: stale CWSR TMA debug flag in
  kfd_process_device_init_cwsr_dgpu()
--

commit 53d7a0d14fffa554af576ebf0d12a4cd46c01696
Author: Jesse Zhang <Jesse.Zhang@amd.com>

drm/amdkfd: leave the devices a process has no VM on out of debugging

This commit activates and runs debug operations only on devices where the
target process has acquired a VM, tracking per-device activation in
pdd->debug_active.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> index 71b34a57c2cc2..d5699c2e1f7fe 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c

[ ... ]

> @@ -626,12 +631,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwind
>  	for (i = 0; i < target->n_pdds; i++) {
>  		struct kfd_process_device *pdd = target->pdds[i];
>  
> -		/* If this is an unwind, and we have unwound the required
> -		 * enable calls on the pdd list, we need to stop now
> -		 * otherwise we may mess up another debugger session.
> -		 */
> -		if (unwind && i == unwind_count)
> -			break;
> +		if (!pdd->debug_active)
> +			continue;
> +		pdd->debug_active = false;
>  
>  		kfd_process_set_trap_debug_flag(&pdd->qpd, false);

[Severity: Medium]
Can this leave the CWSR TMA debug flag permanently enabled on a device
where pdd->debug_active is false?

When a process acquires a VM on a device, kfd_process_device_init_vm() ->
kfd_process_device_init_cwsr_dgpu() sets the TMA flag from the
process-wide debug_trap_enabled flag rather than pdd->debug_active:

drivers/gpu/drm/amd/amdkfd/kfd_process.c:kfd_process_device_init_cwsr_dgpu() {
    ...
	kfd_process_set_trap_debug_flag(&pdd->qpd,
					pdd->process->debug_trap_enabled);
    ...
}

If a debugged process acquires a VM on an additional device after
kfd_dbg_trap_activate() has already run, pdd->debug_active remains false
on that device while the TMA debug flag is set to true, and
kfd_dbg_trap_deactivate() will now skip clearing it on detach.  The same
happens if a debugger attaches before runtime enable, the process acquires
a VM, and either the debugger detaches before runtime enable or
kfd_dbg_trap_activate() fails before reaching that device.

Should kfd_process_device_init_cwsr_dgpu() pass pdd->debug_active instead
of pdd->process->debug_trap_enabled?

[ ... ]

> @@ -727,6 +729,9 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
>  	for (i = 0; i < target->n_pdds; i++) {
>  		struct kfd_process_device *pdd = target->pdds[i];
>  
> +		if (!pdd->drm_priv)
> +			continue;
> +
>  		if (!kfd_dbg_is_per_vmid_supported(pdd->dev)) {
>  			r = reserve_debug_trap_vmid(pdd->dev->dqm, &pdd->qpd);
>  
> @@ -739,6 +744,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
>  			}
>  		}
>  
> +		pdd->debug_active = true;

[Severity: High]
If a debugged process later acquires a VM and creates a queue on a
non-per-VMID device that was skipped here, can pm_map_process_v9() hijack
another process's reserved trap_debug_vmid on that device?

drivers/gpu/drm/amd/amdkfd/kfd_packet_manager_v9.c:pm_map_process_v9() {
    ...
	if (kfd->dqm->trap_debug_vmid && pdd->process->debug_trap_enabled &&
			pdd->process->runtime_info.runtime_state == DEBUG_RUNTIME_STATE_ENABLED) {
		packet->bitfields2.debug_vmid = kfd->dqm->trap_debug_vmid;
		packet->bitfields2.new_debug = 1;
	}
    ...
}

When process A is debugged only on a dGPU (so A has debug_trap_enabled
and runtime_state == DEBUG_RUNTIME_STATE_ENABLED, but pdd->debug_active is
false on the iGPU) and process B reserves kfd->dqm->trap_debug_vmid on
the iGPU, process A creating a queue on the iGPU will map its process
context with new_debug = 1 and process B's debug_vmid.

In addition, pm_map_process_aldebaran(), create_queue_cpsch(), and
add_queue_mes() still check process->debug_trap_enabled instead of
pdd->debug_active, applying single_memops, is_dbg_wa, and
skip_process_ctx_clear to devices where debugging was not activated.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119090557.3494649-1-Jesse.Zhang@amd.com?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging
  2026-11-19  9:04 [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging Jesse Zhang
                   ` (2 preceding siblings ...)
  2026-11-19  9:26 ` [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging sashiko-bot
@ 2026-11-19 17:17 ` Kim, Jonathan
  2026-11-20  7:58   ` Zhang, Jesse(Jie)
  3 siblings, 1 reply; 9+ messages in thread
From: Kim, Jonathan @ 2026-11-19 17:17 UTC (permalink / raw)
  To: Zhang, Jesse(Jie), amd-gfx@lists.freedesktop.org
  Cc: Deucher, Alexander, Kuehling, Felix, Zhang, Jesse(Jie)

Public

> -----Original Message-----
> From: Jesse Zhang <Jesse.Zhang@amd.com>
> Sent: Thursday, November 19, 2026 4:05 AM
> To: amd-gfx@lists.freedesktop.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> <Felix.Kuehling@amd.com>; Kim, Jonathan <Jonathan.Kim@amd.com>; Zhang,
> Jesse(Jie) <Jesse.Zhang@amd.com>; Kim, Jonathan <Jonathan.Kim@amd.com>
> Subject: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of
> debugging
>
> Debug activation and the debug operations go through all devices of the
> process, also the ones it never acquired a VM on. With a dGPU and an
> iGPU without per VMID debugging, debugging a process that only uses the
> dGPU still takes the single debug VMID of the iGPU, so a second debugger
> fails:

There can't be a second debugger on any process to begin with otherwise overrides/debug setup would conflict.
I think you mean a single debugger with multiple inferiors (target processes).
I'm getting a better idea of the issue now (but correct me if I'm wrong).

You have 2 types of devices, multi-process debug supported MES based hardware and single process debug supported device (non-MES) being debugged under a single process.
You then have the debugger trying to attach to more than 1 target process which is supported by one type of device but not the other.

If this is the case, we shouldn't touch the unwind function.
Debugging is process wide by design so multi-inferior debugging should fail on this setup (at least for the second inferior attach and onward).
The first process that attached successfully should still be OK.

What I'm still confused about is how we ended up being able to suspend on a process with a device with no VM acquire.
Runtime enable happens after topology discovery and we should acquire the VMs for all devices of that runtime instance.

What are the GDB reproducer steps?

Jon

>
>   amdgpu 0000:05:00.0: Trap debug id already reserved
>
> and a trap override the iGPU doesn't support fails with -EACCES.
>
> A process isn't expected to use a device it hasn't acquired by the time
> its runtime is enabled. Only activate debugging on the devices with a VM
> and track it in debug_active, set once the debug VMID is reserved so the
> unwind also undoes the device that failed. Deactivation and the debug
> operations only go over those devices and address watches on other
> devices fail with -ENODEV. A device the process still acquires later is
> only debugged from the next attach.
>
> As kfd_dbg_trap_set_flags() now skips devices, rewind up to the last
> updated device instead of using the number of updated devices as the
> bound.
>
> Suggested-by: Jonathan Kim <jonathan.kim@amd.com>
> Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> ---
>  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 +-
>  drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 60 ++++++++++++-------
>  drivers/gpu/drm/amd/amdkfd/kfd_debug.h        |  2 +-
>  .../drm/amd/amdkfd/kfd_device_queue_manager.c |  6 ++
>  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 +
>  5 files changed, 50 insertions(+), 24 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 3d2136adb62b..3d768511d002 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -3039,7 +3039,7 @@ static int runtime_disable(struct kfd_process *p)
>
>       if (p->debug_trap_enabled) {
>               if (was_enabled)
> -                     kfd_dbg_trap_deactivate(p, false, 0);
> +                     kfd_dbg_trap_deactivate(p, false);
>
>               if (!p->is_runtime_retry)
>
>       kfd_dbg_ev_raise(KFD_EC_MASK(EC_PROCESS_RUNTIME),
> @@ -3219,7 +3219,7 @@ static int kfd_ioctl_set_debug_trap(struct file *filep,
> struct kfd_process *p, v
>                                       args-
> >clear_node_address_watch.gpu_id);
>
>               pdd = kfd_process_device_data_by_id(target, user_gpu_id);
> -             if (user_gpu_id == -EINVAL || !pdd) {
> +             if (user_gpu_id == -EINVAL || !pdd || !pdd->debug_active) {
>                       r = -ENODEV;
>                       goto unlock_out;
>               }
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> index d5c87de519ac..b2d01613a21c 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> @@ -504,9 +504,13 @@ static void
> kfd_dbg_clear_process_address_watch(struct kfd_process *target)
>  {
>       int i, j;
>
> -     for (i = 0; i < target->n_pdds; i++)
> +     for (i = 0; i < target->n_pdds; i++) {
> +             if (!target->pdds[i]->debug_active)
> +                     continue;
> +
>               for (j = 0; j < MAX_WATCH_ADDRESSES; j++)
>                       kfd_dbg_trap_clear_dev_address_watch(target-
> >pdds[i], j);
> +     }
>  }
>
>  int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
> @@ -517,8 +521,12 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target,
> uint32_t *flags)
>       for (i = 0; i < target->n_pdds; i++) {
>               uint32_t caps;
>               uint32_t caps2;
> -             struct kfd_topology_device *topo_dev =
> -                     kfd_topology_device_by_id(target->pdds[i]->dev->id);
> +             struct kfd_topology_device *topo_dev;
> +
> +             if (!target->pdds[i]->debug_active)
> +                     continue;
> +
> +             topo_dev = kfd_topology_device_by_id(target->pdds[i]->dev-
> >id);
>               if (!topo_dev)
>                       return -EINVAL;
>
> @@ -549,7 +557,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target,
> uint32_t *flags)
>       for (i = 0; i < target->n_pdds; i++) {
>               struct kfd_process_device *pdd = target->pdds[i];
>
> -             if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
> +             if (!pdd->debug_active || !kfd_dbg_is_per_vmid_supported(pdd-
> >dev))
>                       continue;
>
>               if (!pdd->dev->kfd->shared_resources.enable_mes)
> @@ -562,7 +570,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target,
> uint32_t *flags)
>                       break;
>               }
>
> -             rewind_count++;
> +             rewind_count = i + 1;
>       }
>
>       /* Rewind flags */
> @@ -572,7 +580,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target,
> uint32_t *flags)
>               for (i = 0; i < rewind_count; i++) {
>                       struct kfd_process_device *pdd = target->pdds[i];
>
> -                     if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
> +                     if (!pdd->debug_active ||
> +                         !kfd_dbg_is_per_vmid_supported(pdd->dev))
>                               continue;
>
>                       if (!pdd->dev->kfd->shared_resources.enable_mes)
> @@ -588,12 +597,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target,
> uint32_t *flags)
>  /* kfd_dbg_trap_deactivate:
>   *   target: target process
>   *   unwind: If this is unwinding a failed kfd_dbg_trap_enable()
> - *   unwind_count:
> - *           If unwind == true, how far down the pdd list we need
> - *                           to unwind
> - *           else: ignored
>   */
> -void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int
> unwind_count)
> +void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind)
>  {
>       int i;
>
> @@ -614,12 +619,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process
> *target, bool unwind, int unwind
>       for (i = 0; i < target->n_pdds; i++) {
>               struct kfd_process_device *pdd = target->pdds[i];
>
> -             /* If this is an unwind, and we have unwound the required
> -              * enable calls on the pdd list, we need to stop now
> -              * otherwise we may mess up another debugger session.
> -              */
> -             if (unwind && i == unwind_count)
> -                     break;
> +             if (!pdd->debug_active)
> +                     continue;
> +             pdd->debug_active = false;
>
>               kfd_process_set_trap_debug_flag(&pdd->qpd, false);
>
> @@ -684,7 +686,7 @@ int kfd_dbg_trap_disable(struct kfd_process *target, bool
> drain_irqs)
>        * attached running target runtime state to enable for re-attach.
>        */
>       if (target->runtime_info.runtime_state ==
> DEBUG_RUNTIME_STATE_ENABLED)
> -             kfd_dbg_trap_deactivate(target, false, 0);
> +             kfd_dbg_trap_deactivate(target, false);
>       else if (target->runtime_info.runtime_state !=
> DEBUG_RUNTIME_STATE_DISABLED)
>               target->runtime_info.runtime_state =
> DEBUG_RUNTIME_STATE_ENABLED;
>
> @@ -715,6 +717,9 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
>       for (i = 0; i < target->n_pdds; i++) {
>               struct kfd_process_device *pdd = target->pdds[i];
>
> +             if (!pdd->drm_priv)
> +                     continue;
> +
>               if (!kfd_dbg_is_per_vmid_supported(pdd->dev)) {
>                       r = reserve_debug_trap_vmid(pdd->dev->dqm, &pdd-
> >qpd);
>
> @@ -727,6 +732,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
>                       }
>               }
>
> +             pdd->debug_active = true;
> +
>               /* Disable GFX OFF to prevent garbage read/writes to debug
> registers.
>                * If RLC restore of debug registers is not supported and runtime
> enable
>                * hasn't done so already on ttmp setup request, restore the trap
> config registers.
> @@ -775,7 +782,7 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
>       /* Enabling debug failed, we need to disable on
>        * all GPUs so the enable is all or nothing.
>        */
> -     kfd_dbg_trap_deactivate(target, true, i);
> +     kfd_dbg_trap_deactivate(target, true);
>       return r;
>  }
>
> @@ -825,7 +832,7 @@ int kfd_dbg_trap_enable(struct kfd_process *target,
> uint32_t fd,
>               atomic_inc(&target->debugger_process-
> >debugged_process_count);
>
>       if (copy_to_user(runtime_info, (void *)&target->runtime_info, copy_size))
> {
> -             kfd_dbg_trap_deactivate(target, false, 0);
> +             kfd_dbg_trap_deactivate(target, false);
>               fput(target->dbg_ev_file);
>               target->dbg_ev_file = NULL;
>               if (target->debugger_process)
> @@ -851,7 +858,12 @@ static int
> kfd_dbg_validate_trap_override_request(struct kfd_process *p,
>
>       for (i = 0; i < p->n_pdds; i++) {
>               struct kfd_process_device *pdd = p->pdds[i];
> -             int err = pdd->dev->kfd2kgd->validate_trap_override_request(
> +             int err;
> +
> +             if (!pdd->debug_active)
> +                     continue;
> +
> +             err = pdd->dev->kfd2kgd->validate_trap_override_request(
>                                                               pdd->dev-
> >adev,
>                                                               trap_override,
>
>       trap_mask_supported);
> @@ -886,6 +898,9 @@ int kfd_dbg_trap_set_wave_launch_override(struct
> kfd_process *target,
>       for (i = 0; i < target->n_pdds; i++) {
>               struct kfd_process_device *pdd = target->pdds[i];
>
> +             if (!pdd->debug_active)
> +                     continue;
> +
>               amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
>               pdd->spi_dbg_override = pdd->dev->kfd2kgd-
> >set_wave_launch_trap_override(
>                               pdd->dev->adev,
> @@ -922,6 +937,9 @@ int kfd_dbg_trap_set_wave_launch_mode(struct
> kfd_process *target,
>       for (i = 0; i < target->n_pdds; i++) {
>               struct kfd_process_device *pdd = target->pdds[i];
>
> +             if (!pdd->debug_active)
> +                     continue;
> +
>               amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
>               pdd->spi_dbg_launch_mode = pdd->dev->kfd2kgd-
> >set_wave_launch_mode(
>                               pdd->dev->adev,
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> index 5babf9b992cd..e93d77fccda2 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> @@ -25,7 +25,7 @@
>
>  #include "kfd_priv.h"
>
> -void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int
> unwind_count);
> +void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind);
>  int kfd_dbg_trap_activate(struct kfd_process *target);
>  int kfd_dbg_ev_query_debug_event(struct kfd_process *process,
>                       unsigned int *queue_id,
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> index b5d7d13f31a5..b8ef93dba0af 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> @@ -3851,6 +3851,9 @@ int resume_queues(struct kfd_process *p,
>               struct queue *q;
>               int r, per_device_resumed = 0;
>
> +             if (!pdd->debug_active)
> +                     continue;
> +
>               dqm_lock(dqm);
>
>               /* unmask queues that resume or already resumed as valid */
> @@ -3954,6 +3957,9 @@ int suspend_queues(struct kfd_process *p,
>               struct queue *q;
>               int r, per_device_suspended = 0;
>
> +             if (!pdd->debug_active)
> +                     continue;
> +
>               mutex_lock(&p->event_mutex);
>               dqm_lock(dqm);
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> index 129363ad4e34..ed85b7fb896e 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> @@ -880,6 +880,8 @@ struct kfd_process_device {
>       uint32_t watch_points[4];
>       uint64_t watch_addresses[4];
>       uint32_t alloc_watch_ids;
> +     /* Debugging is activated on the device */
> +     bool debug_active;
>
>       /*
>        * If this process has been checkpointed before, then the user
> --
> 2.50.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
  2026-11-19  9:04 ` [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails Jesse Zhang
@ 2026-11-19 18:46   ` Kim, Jonathan
  2026-11-20  8:17     ` Zhang, Jesse(Jie)
  0 siblings, 1 reply; 9+ messages in thread
From: Kim, Jonathan @ 2026-11-19 18:46 UTC (permalink / raw)
  To: Zhang, Jesse(Jie), amd-gfx@lists.freedesktop.org
  Cc: Deucher, Alexander, Kuehling, Felix, Zhang, Jesse(Jie)

Public

> -----Original Message-----
> From: Jesse Zhang <Jesse.Zhang@amd.com>
> Sent: Thursday, November 19, 2026 4:05 AM
> To: amd-gfx@lists.freedesktop.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> <Felix.Kuehling@amd.com>; Kim, Jonathan <Jonathan.Kim@amd.com>; Zhang,
> Jesse(Jie) <Jesse.Zhang@amd.com>
> Subject: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when
> DBG_TRAP_ENABLE fails
>
> KFD_IOC_DBG_TRAP_ENABLE stores the caller in target->debugger_process
> before calling kfd_dbg_trap_enable(), and nothing resets it when that
> fails (-EALREADY, -ENODEV, -EBUSY, -EBADF for a bad dbg_fd, -EFAULT).
> The permission check at the top of kfd_ioctl_set_debug_trap() accepts
> any caller equal to target->debugger_process, so the caller keeps
> debug access after it stopped ptracing the target. The pointer is also
> not reference counted, so it dangles once the caller exits, and a later
> successful ENABLE by the target itself increments
> debugged_process_count of the freed process.
>
> A parent that ptraces its child, tries to enable debugging with
> dbg_fd = -1 (-EBADF) and then detaches can still enable debugging on
> the child with a valid fd:
>
>   1. ptraced, ENABLE with dbg_fd=-1      -> -9 (Bad file descriptor)
>   2. ptrace detached
>   3. not ptraced, ENABLE with valid fd  -> 0 (success)
>
> Clear the pointer again when enabling fails, a session that isn't
> enabled has no debugger. Step 3 now fails with -EPERM.
>
> Return -EALREADY for an already enabled session before touching the
> pointer at all: the exit of the debugger of that session looks for its
> targets through target->debugger_process before it takes
> target->mutex, so even a temporary change of the pointer could make it
> skip the target and leave a dangling pointer behind.
>
> v2: clear the pointer instead of restoring it, no local variable and
>     braces needed; clarify the comment (Jon)
>
> Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and
> disable")
> Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> ---
>  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> index 3d768511d002..9256a2468081 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> @@ -3227,6 +3227,16 @@ static int kfd_ioctl_set_debug_trap(struct file *filep,
> struct kfd_process *p, v
>
>       switch (args->op) {
>       case KFD_IOC_DBG_TRAP_ENABLE:
> +             /*
> +              * Don't change the debugger of an enabled session, not even
> +              * temporarily: the exit of that debugger looks for its targets
> +              * by this pointer before it takes target->mutex.
> +              */
> +             if (target->debug_trap_enabled) {
> +                     r = -EALREADY;
> +                     break;
> +             }
> +
>               if (target != p)
>                       target->debugger_process = p;

Ok.  I think I see what you're saying now.
Current debugger is exiting but hasn't detached from target.
New debugger tries to attach and prematurely assigns target->debugger_process.
Current debugger is still exiting, doesn't find itself in target table because of new debugger target->debugger_process overwrite (since new debugger isn't holding the scru lock) and so current debugger doesn't disable target on exit.
New debugger hits -EALREADY because current debugger didn't disable the target so debug mode is left dangling.

Could we move this conditional assignment inside kfd_dbg_trap_enable after it sets debug_trap_enabled=true and before it atomic counts the caller process (pass p as an arg -> struct kfd_process *debugger)?

>
> @@ -3236,6 +3246,9 @@ static int kfd_ioctl_set_debug_trap(struct file *filep,
> struct kfd_process *p, v
>                                       &args->enable.rinfo_size);
>               if (!r)
>                       target->exception_enable_mask = args-
> >enable.exception_mask;
> +             else
> +                     /* A session that isn't enabled has no debugger */
> +                     target->debugger_process = NULL;

And maybe also put the NULL assignement in kfd_dbg_trap_enable as well when copy_to_user fails after the conditional debugger reference decrement.

Jon

>
>               break;
>       case KFD_IOC_DBG_TRAP_DISABLE:
> --
> 2.50.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging
  2026-11-19 17:17 ` Kim, Jonathan
@ 2026-11-20  7:58   ` Zhang, Jesse(Jie)
  2026-11-20 14:25     ` Kim, Jonathan
  0 siblings, 1 reply; 9+ messages in thread
From: Zhang, Jesse(Jie) @ 2026-11-20  7:58 UTC (permalink / raw)
  To: Kim, Jonathan, amd-gfx@lists.freedesktop.org
  Cc: Deucher, Alexander, Kuehling, Felix

Public

> -----Original Message-----
> From: Kim, Jonathan <Jonathan.Kim@amd.com>
> Sent: Friday, November 20, 2026 1:18 AM
> To: Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>; amd-
> gfx@lists.freedesktop.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> <Felix.Kuehling@amd.com>; Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>
> Subject: RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on
> out of debugging
>
> Public
>
> > -----Original Message-----
> > From: Jesse Zhang <Jesse.Zhang@amd.com>
> > Sent: Thursday, November 19, 2026 4:05 AM
> > To: amd-gfx@lists.freedesktop.org
> > Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> > <Felix.Kuehling@amd.com>; Kim, Jonathan <Jonathan.Kim@amd.com>;
> Zhang,
> > Jesse(Jie) <Jesse.Zhang@amd.com>; Kim, Jonathan
> <Jonathan.Kim@amd.com>
> > Subject: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on
> > out of debugging
> >
> > Debug activation and the debug operations go through all devices of
> > the process, also the ones it never acquired a VM on. With a dGPU and
> > an iGPU without per VMID debugging, debugging a process that only uses
> > the dGPU still takes the single debug VMID of the iGPU, so a second
> > debugger
> > fails:
>
> There can't be a second debugger on any process to begin with otherwise
> overrides/debug setup would conflict.
> I think you mean a single debugger with multiple inferiors (target processes).
> I'm getting a better idea of the issue now (but correct me if I'm wrong).
>
> You have 2 types of devices, multi-process debug supported MES based
> hardware and single process debug supported device (non-MES) being
> debugged under a single process.
> You then have the debugger trying to attach to more than 1 target process
> which is supported by one type of device but not the other.
>
> If this is the case, we shouldn't touch the unwind function.
> Debugging is process wide by design so multi-inferior debugging should fail
> on this setup (at least for the second inferior attach and onward).
> The first process that attached successfully should still be OK.
>
> What I'm still confused about is how we ended up being able to suspend on a
> process with a device with no VM acquire.
> Runtime enable happens after topology discovery and we should acquire the
> VMs for all devices of that runtime instance.
>
> What are the GDB reproducer steps?
You're right - I meant one debugger with two inferiors. I don't have a GDB reproducer. In the ROCr path I checked, VMs are acquired for all GPUs in the runtime instance before RUNTIME_ENABLE.

The crash reproducer is the test using the KFD ioctls directly. The child acquires only the dGPU VM before RUNTIME_ENABLE; the parent ptraces it, enables debugging and suspends its queue. The suspend loop then reaches the iGPU PDD with no VM and calls kfd_flush_tlb() because total_suspended is already nonzero.

I agree that the second inferior failing because of the iGPU's single-process debugging limit is expected. I'll drop this patch. Can we only keep the kfd_flush_tlb() NULL check, since direct use of the KFD ioctls can otherwise trigger a kernel oops.

Thanks,
Jess
>
> Jon
>
> >
> >   amdgpu 0000:05:00.0: Trap debug id already reserved
> >
> > and a trap override the iGPU doesn't support fails with -EACCES.
> >
> > A process isn't expected to use a device it hasn't acquired by the
> > time its runtime is enabled. Only activate debugging on the devices
> > with a VM and track it in debug_active, set once the debug VMID is
> > reserved so the unwind also undoes the device that failed.
> > Deactivation and the debug operations only go over those devices and
> > address watches on other devices fail with -ENODEV. A device the
> > process still acquires later is only debugged from the next attach.
> >
> > As kfd_dbg_trap_set_flags() now skips devices, rewind up to the last
> > updated device instead of using the number of updated devices as the
> > bound.
> >
> > Suggested-by: Jonathan Kim <jonathan.kim@amd.com>
> > Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> > ---
> >  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 +-
> >  drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 60 ++++++++++++-------
> >  drivers/gpu/drm/amd/amdkfd/kfd_debug.h        |  2 +-
> >  .../drm/amd/amdkfd/kfd_device_queue_manager.c |  6 ++
> >  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 +
> >  5 files changed, 50 insertions(+), 24 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > index 3d2136adb62b..3d768511d002 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > @@ -3039,7 +3039,7 @@ static int runtime_disable(struct kfd_process
> > *p)
> >
> >       if (p->debug_trap_enabled) {
> >               if (was_enabled)
> > -                     kfd_dbg_trap_deactivate(p, false, 0);
> > +                     kfd_dbg_trap_deactivate(p, false);
> >
> >               if (!p->is_runtime_retry)
> >
> >       kfd_dbg_ev_raise(KFD_EC_MASK(EC_PROCESS_RUNTIME),
> > @@ -3219,7 +3219,7 @@ static int kfd_ioctl_set_debug_trap(struct file
> > *filep, struct kfd_process *p, v
> >                                       args-
> > >clear_node_address_watch.gpu_id);
> >
> >               pdd = kfd_process_device_data_by_id(target, user_gpu_id);
> > -             if (user_gpu_id == -EINVAL || !pdd) {
> > +             if (user_gpu_id == -EINVAL || !pdd ||
> > + !pdd->debug_active) {
> >                       r = -ENODEV;
> >                       goto unlock_out;
> >               }
> > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > index d5c87de519ac..b2d01613a21c 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > @@ -504,9 +504,13 @@ static void
> > kfd_dbg_clear_process_address_watch(struct kfd_process *target)  {
> >       int i, j;
> >
> > -     for (i = 0; i < target->n_pdds; i++)
> > +     for (i = 0; i < target->n_pdds; i++) {
> > +             if (!target->pdds[i]->debug_active)
> > +                     continue;
> > +
> >               for (j = 0; j < MAX_WATCH_ADDRESSES; j++)
> >                       kfd_dbg_trap_clear_dev_address_watch(target-
> > >pdds[i], j);
> > +     }
> >  }
> >
> >  int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t
> > *flags) @@ -517,8 +521,12 @@ int kfd_dbg_trap_set_flags(struct
> > kfd_process *target, uint32_t *flags)
> >       for (i = 0; i < target->n_pdds; i++) {
> >               uint32_t caps;
> >               uint32_t caps2;
> > -             struct kfd_topology_device *topo_dev =
> > -                     kfd_topology_device_by_id(target->pdds[i]->dev->id);
> > +             struct kfd_topology_device *topo_dev;
> > +
> > +             if (!target->pdds[i]->debug_active)
> > +                     continue;
> > +
> > +             topo_dev =
> > + kfd_topology_device_by_id(target->pdds[i]->dev-
> > >id);
> >               if (!topo_dev)
> >                       return -EINVAL;
> >
> > @@ -549,7 +557,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > *target, uint32_t *flags)
> >       for (i = 0; i < target->n_pdds; i++) {
> >               struct kfd_process_device *pdd = target->pdds[i];
> >
> > -             if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
> > +             if (!pdd->debug_active ||
> > + !kfd_dbg_is_per_vmid_supported(pdd-
> > >dev))
> >                       continue;
> >
> >               if (!pdd->dev->kfd->shared_resources.enable_mes)
> > @@ -562,7 +570,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > *target, uint32_t *flags)
> >                       break;
> >               }
> >
> > -             rewind_count++;
> > +             rewind_count = i + 1;
> >       }
> >
> >       /* Rewind flags */
> > @@ -572,7 +580,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > *target, uint32_t *flags)
> >               for (i = 0; i < rewind_count; i++) {
> >                       struct kfd_process_device *pdd =
> > target->pdds[i];
> >
> > -                     if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
> > +                     if (!pdd->debug_active ||
> > +                         !kfd_dbg_is_per_vmid_supported(pdd->dev))
> >                               continue;
> >
> >                       if (!pdd->dev->kfd->shared_resources.enable_mes)
> > @@ -588,12 +597,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > *target, uint32_t *flags)
> >  /* kfd_dbg_trap_deactivate:
> >   *   target: target process
> >   *   unwind: If this is unwinding a failed kfd_dbg_trap_enable()
> > - *   unwind_count:
> > - *           If unwind == true, how far down the pdd list we need
> > - *                           to unwind
> > - *           else: ignored
> >   */
> > -void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind,
> > int
> > unwind_count)
> > +void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind)
> >  {
> >       int i;
> >
> > @@ -614,12 +619,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process
> > *target, bool unwind, int unwind
> >       for (i = 0; i < target->n_pdds; i++) {
> >               struct kfd_process_device *pdd = target->pdds[i];
> >
> > -             /* If this is an unwind, and we have unwound the required
> > -              * enable calls on the pdd list, we need to stop now
> > -              * otherwise we may mess up another debugger session.
> > -              */
> > -             if (unwind && i == unwind_count)
> > -                     break;
> > +             if (!pdd->debug_active)
> > +                     continue;
> > +             pdd->debug_active = false;
> >
> >               kfd_process_set_trap_debug_flag(&pdd->qpd, false);
> >
> > @@ -684,7 +686,7 @@ int kfd_dbg_trap_disable(struct kfd_process
> > *target, bool
> > drain_irqs)
> >        * attached running target runtime state to enable for re-attach.
> >        */
> >       if (target->runtime_info.runtime_state ==
> > DEBUG_RUNTIME_STATE_ENABLED)
> > -             kfd_dbg_trap_deactivate(target, false, 0);
> > +             kfd_dbg_trap_deactivate(target, false);
> >       else if (target->runtime_info.runtime_state !=
> > DEBUG_RUNTIME_STATE_DISABLED)
> >               target->runtime_info.runtime_state =
> > DEBUG_RUNTIME_STATE_ENABLED;
> >
> > @@ -715,6 +717,9 @@ int kfd_dbg_trap_activate(struct kfd_process
> *target)
> >       for (i = 0; i < target->n_pdds; i++) {
> >               struct kfd_process_device *pdd = target->pdds[i];
> >
> > +             if (!pdd->drm_priv)
> > +                     continue;
> > +
> >               if (!kfd_dbg_is_per_vmid_supported(pdd->dev)) {
> >                       r = reserve_debug_trap_vmid(pdd->dev->dqm, &pdd-
> > >qpd);
> >
> > @@ -727,6 +732,8 @@ int kfd_dbg_trap_activate(struct kfd_process
> *target)
> >                       }
> >               }
> >
> > +             pdd->debug_active = true;
> > +
> >               /* Disable GFX OFF to prevent garbage read/writes to
> > debug registers.
> >                * If RLC restore of debug registers is not supported
> > and runtime enable
> >                * hasn't done so already on ttmp setup request, restore
> > the trap config registers.
> > @@ -775,7 +782,7 @@ int kfd_dbg_trap_activate(struct kfd_process
> *target)
> >       /* Enabling debug failed, we need to disable on
> >        * all GPUs so the enable is all or nothing.
> >        */
> > -     kfd_dbg_trap_deactivate(target, true, i);
> > +     kfd_dbg_trap_deactivate(target, true);
> >       return r;
> >  }
> >
> > @@ -825,7 +832,7 @@ int kfd_dbg_trap_enable(struct kfd_process
> > *target, uint32_t fd,
> >               atomic_inc(&target->debugger_process-
> > >debugged_process_count);
> >
> >       if (copy_to_user(runtime_info, (void *)&target->runtime_info,
> > copy_size)) {
> > -             kfd_dbg_trap_deactivate(target, false, 0);
> > +             kfd_dbg_trap_deactivate(target, false);
> >               fput(target->dbg_ev_file);
> >               target->dbg_ev_file = NULL;
> >               if (target->debugger_process) @@ -851,7 +858,12 @@
> > static int kfd_dbg_validate_trap_override_request(struct kfd_process
> > *p,
> >
> >       for (i = 0; i < p->n_pdds; i++) {
> >               struct kfd_process_device *pdd = p->pdds[i];
> > -             int err = pdd->dev->kfd2kgd->validate_trap_override_request(
> > +             int err;
> > +
> > +             if (!pdd->debug_active)
> > +                     continue;
> > +
> > +             err = pdd->dev->kfd2kgd->validate_trap_override_request(
> >
> > pdd->dev-
> > >adev,
> >
> > trap_override,
> >
> >       trap_mask_supported);
> > @@ -886,6 +898,9 @@ int kfd_dbg_trap_set_wave_launch_override(struct
> > kfd_process *target,
> >       for (i = 0; i < target->n_pdds; i++) {
> >               struct kfd_process_device *pdd = target->pdds[i];
> >
> > +             if (!pdd->debug_active)
> > +                     continue;
> > +
> >               amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
> >               pdd->spi_dbg_override = pdd->dev->kfd2kgd-
> > >set_wave_launch_trap_override(
> >                               pdd->dev->adev, @@ -922,6 +937,9 @@ int
> > kfd_dbg_trap_set_wave_launch_mode(struct
> > kfd_process *target,
> >       for (i = 0; i < target->n_pdds; i++) {
> >               struct kfd_process_device *pdd = target->pdds[i];
> >
> > +             if (!pdd->debug_active)
> > +                     continue;
> > +
> >               amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
> >               pdd->spi_dbg_launch_mode = pdd->dev->kfd2kgd-
> > >set_wave_launch_mode(
> >                               pdd->dev->adev, diff --git
> > a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > index 5babf9b992cd..e93d77fccda2 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > @@ -25,7 +25,7 @@
> >
> >  #include "kfd_priv.h"
> >
> > -void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind,
> > int unwind_count);
> > +void kfd_dbg_trap_deactivate(struct kfd_process *target, bool
> > +unwind);
> >  int kfd_dbg_trap_activate(struct kfd_process *target);  int
> > kfd_dbg_ev_query_debug_event(struct kfd_process *process,
> >                       unsigned int *queue_id, diff --git
> > a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > index b5d7d13f31a5..b8ef93dba0af 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > @@ -3851,6 +3851,9 @@ int resume_queues(struct kfd_process *p,
> >               struct queue *q;
> >               int r, per_device_resumed = 0;
> >
> > +             if (!pdd->debug_active)
> > +                     continue;
> > +
> >               dqm_lock(dqm);
> >
> >               /* unmask queues that resume or already resumed as valid
> > */ @@ -3954,6 +3957,9 @@ int suspend_queues(struct kfd_process *p,
> >               struct queue *q;
> >               int r, per_device_suspended = 0;
> >
> > +             if (!pdd->debug_active)
> > +                     continue;
> > +
> >               mutex_lock(&p->event_mutex);
> >               dqm_lock(dqm);
> >
> > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > index 129363ad4e34..ed85b7fb896e 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > @@ -880,6 +880,8 @@ struct kfd_process_device {
> >       uint32_t watch_points[4];
> >       uint64_t watch_addresses[4];
> >       uint32_t alloc_watch_ids;
> > +     /* Debugging is activated on the device */
> > +     bool debug_active;
> >
> >       /*
> >        * If this process has been checkpointed before, then the user
> > --
> > 2.50.1
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
  2026-11-19 18:46   ` Kim, Jonathan
@ 2026-11-20  8:17     ` Zhang, Jesse(Jie)
  0 siblings, 0 replies; 9+ messages in thread
From: Zhang, Jesse(Jie) @ 2026-11-20  8:17 UTC (permalink / raw)
  To: Kim, Jonathan, amd-gfx@lists.freedesktop.org
  Cc: Deucher, Alexander, Kuehling, Felix

Public

> -----Original Message-----
> From: Kim, Jonathan <Jonathan.Kim@amd.com>
> Sent: Friday, November 20, 2026 2:46 AM
> To: Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>; amd-
> gfx@lists.freedesktop.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> <Felix.Kuehling@amd.com>; Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>
> Subject: RE: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when
> DBG_TRAP_ENABLE fails
>
> Public
>
> > -----Original Message-----
> > From: Jesse Zhang <Jesse.Zhang@amd.com>
> > Sent: Thursday, November 19, 2026 4:05 AM
> > To: amd-gfx@lists.freedesktop.org
> > Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> > <Felix.Kuehling@amd.com>; Kim, Jonathan <Jonathan.Kim@amd.com>;
> Zhang,
> > Jesse(Jie) <Jesse.Zhang@amd.com>
> > Subject: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when
> > DBG_TRAP_ENABLE fails
> >
> > KFD_IOC_DBG_TRAP_ENABLE stores the caller in target->debugger_process
> > before calling kfd_dbg_trap_enable(), and nothing resets it when that
> > fails (-EALREADY, -ENODEV, -EBUSY, -EBADF for a bad dbg_fd, -EFAULT).
> > The permission check at the top of kfd_ioctl_set_debug_trap() accepts
> > any caller equal to target->debugger_process, so the caller keeps
> > debug access after it stopped ptracing the target. The pointer is also
> > not reference counted, so it dangles once the caller exits, and a
> > later successful ENABLE by the target itself increments
> > debugged_process_count of the freed process.
> >
> > A parent that ptraces its child, tries to enable debugging with dbg_fd
> > = -1 (-EBADF) and then detaches can still enable debugging on the
> > child with a valid fd:
> >
> >   1. ptraced, ENABLE with dbg_fd=-1      -> -9 (Bad file descriptor)
> >   2. ptrace detached
> >   3. not ptraced, ENABLE with valid fd  -> 0 (success)
> >
> > Clear the pointer again when enabling fails, a session that isn't
> > enabled has no debugger. Step 3 now fails with -EPERM.
> >
> > Return -EALREADY for an already enabled session before touching the
> > pointer at all: the exit of the debugger of that session looks for its
> > targets through target->debugger_process before it takes
> > target->mutex, so even a temporary change of the pointer could make it
> > skip the target and leave a dangling pointer behind.
> >
> > v2: clear the pointer instead of restoring it, no local variable and
> >     braces needed; clarify the comment (Jon)
> >
> > Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and
> > disable")
> > Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> > ---
> >  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 +++++++++++++
> >  1 file changed, 13 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > index 3d768511d002..9256a2468081 100644
> > --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > @@ -3227,6 +3227,16 @@ static int kfd_ioctl_set_debug_trap(struct file
> > *filep, struct kfd_process *p, v
> >
> >       switch (args->op) {
> >       case KFD_IOC_DBG_TRAP_ENABLE:
> > +             /*
> > +              * Don't change the debugger of an enabled session, not even
> > +              * temporarily: the exit of that debugger looks for its targets
> > +              * by this pointer before it takes target->mutex.
> > +              */
> > +             if (target->debug_trap_enabled) {
> > +                     r = -EALREADY;
> > +                     break;
> > +             }
> > +
> >               if (target != p)
> >                       target->debugger_process = p;
>
> Ok.  I think I see what you're saying now.
> Current debugger is exiting but hasn't detached from target.
> New debugger tries to attach and prematurely assigns target-
> >debugger_process.
> Current debugger is still exiting, doesn't find itself in target table because of
> new debugger target->debugger_process overwrite (since new debugger isn't
> holding the scru lock) and so current debugger doesn't disable target on exit.
> New debugger hits -EALREADY because current debugger didn't disable the
> target so debug mode is left dangling.
>
> Could we move this conditional assignment inside kfd_dbg_trap_enable after
> it sets debug_trap_enabled=true and before it atomic counts the caller
> process (pass p as an arg -> struct kfd_process *debugger)?
>
> >
> > @@ -3236,6 +3246,9 @@ static int kfd_ioctl_set_debug_trap(struct file
> > *filep, struct kfd_process *p, v
> >                                       &args->enable.rinfo_size);
> >               if (!r)
> >                       target->exception_enable_mask = args-
> > >enable.exception_mask;
> > +             else
> > +                     /* A session that isn't enabled has no debugger */
> > +                     target->debugger_process = NULL;
>
> And maybe also put the NULL assignement in kfd_dbg_trap_enable as well
> when copy_to_user fails after the conditional debugger reference decrement.

Thanks Jon. I will update the v3 as suggested: kfd_dbg_trap_enable() takes the debugger argument and assigns target->debugger_process after setting debug_trap_enabled, before incrementing the debugger's count.

On copy_to_user() failure, it clears the pointer after decrementing the count. The extra -EALREADY check and pointer cleanup in the ioctl are no longer needed

Thanks
Jesse
>
> Jon
>
> >
> >               break;
> >       case KFD_IOC_DBG_TRAP_DISABLE:
> > --
> > 2.50.1
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging
  2026-11-20  7:58   ` Zhang, Jesse(Jie)
@ 2026-11-20 14:25     ` Kim, Jonathan
  0 siblings, 0 replies; 9+ messages in thread
From: Kim, Jonathan @ 2026-11-20 14:25 UTC (permalink / raw)
  To: Zhang, Jesse(Jie), amd-gfx@lists.freedesktop.org
  Cc: Deucher, Alexander, Kuehling, Felix

Public

> -----Original Message-----
> From: Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>
> Sent: Friday, November 20, 2026 2:58 AM
> To: Kim, Jonathan <Jonathan.Kim@amd.com>; amd-gfx@lists.freedesktop.org
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> <Felix.Kuehling@amd.com>
> Subject: RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out
> of debugging
>
> Public
>
> > -----Original Message-----
> > From: Kim, Jonathan <Jonathan.Kim@amd.com>
> > Sent: Friday, November 20, 2026 1:18 AM
> > To: Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>; amd-
> > gfx@lists.freedesktop.org
> > Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> > <Felix.Kuehling@amd.com>; Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>
> > Subject: RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on
> > out of debugging
> >
> > Public
> >
> > > -----Original Message-----
> > > From: Jesse Zhang <Jesse.Zhang@amd.com>
> > > Sent: Thursday, November 19, 2026 4:05 AM
> > > To: amd-gfx@lists.freedesktop.org
> > > Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Kuehling, Felix
> > > <Felix.Kuehling@amd.com>; Kim, Jonathan <Jonathan.Kim@amd.com>;
> > Zhang,
> > > Jesse(Jie) <Jesse.Zhang@amd.com>; Kim, Jonathan
> > <Jonathan.Kim@amd.com>
> > > Subject: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on
> > > out of debugging
> > >
> > > Debug activation and the debug operations go through all devices of
> > > the process, also the ones it never acquired a VM on. With a dGPU and
> > > an iGPU without per VMID debugging, debugging a process that only uses
> > > the dGPU still takes the single debug VMID of the iGPU, so a second
> > > debugger
> > > fails:
> >
> > There can't be a second debugger on any process to begin with otherwise
> > overrides/debug setup would conflict.
> > I think you mean a single debugger with multiple inferiors (target processes).
> > I'm getting a better idea of the issue now (but correct me if I'm wrong).
> >
> > You have 2 types of devices, multi-process debug supported MES based
> > hardware and single process debug supported device (non-MES) being
> > debugged under a single process.
> > You then have the debugger trying to attach to more than 1 target process
> > which is supported by one type of device but not the other.
> >
> > If this is the case, we shouldn't touch the unwind function.
> > Debugging is process wide by design so multi-inferior debugging should fail
> > on this setup (at least for the second inferior attach and onward).
> > The first process that attached successfully should still be OK.
> >
> > What I'm still confused about is how we ended up being able to suspend on a
> > process with a device with no VM acquire.
> > Runtime enable happens after topology discovery and we should acquire the
> > VMs for all devices of that runtime instance.
> >
> > What are the GDB reproducer steps?
> You're right — I meant one debugger with two inferiors. I don't have a GDB
> reproducer. In the ROCr path I checked, VMs are acquired for all GPUs in the
> runtime instance before RUNTIME_ENABLE.
>
> The crash reproducer is the test using the KFD ioctls directly. The child acquires
> only the dGPU VM before RUNTIME_ENABLE; the parent ptraces it, enables
> debugging and suspends its queue. The suspend loop then reaches the iGPU PDD
> with no VM and calls kfd_flush_tlb() because total_suspended is already nonzero.
>
> I agree that the second inferior failing because of the iGPU's single-process
> debugging limit is expected. I'll drop this patch. Can we only keep the
> kfd_flush_tlb() NULL check, since direct use of the KFD ioctls can otherwise
> trigger a kernel oops.

Ah thanks for the background on that.  I was scratching my head for bit 😝.
Yeah, the debugger has an implicit contract with the runtime that it will be debugging devices process-wide.
Null check on flush sounds good to me.

Jon

>
> Thanks,
> Jess
> >
> > Jon
> >
> > >
> > >   amdgpu 0000:05:00.0: Trap debug id already reserved
> > >
> > > and a trap override the iGPU doesn't support fails with -EACCES.
> > >
> > > A process isn't expected to use a device it hasn't acquired by the
> > > time its runtime is enabled. Only activate debugging on the devices
> > > with a VM and track it in debug_active, set once the debug VMID is
> > > reserved so the unwind also undoes the device that failed.
> > > Deactivation and the debug operations only go over those devices and
> > > address watches on other devices fail with -ENODEV. A device the
> > > process still acquires later is only debugged from the next attach.
> > >
> > > As kfd_dbg_trap_set_flags() now skips devices, rewind up to the last
> > > updated device instead of using the number of updated devices as the
> > > bound.
> > >
> > > Suggested-by: Jonathan Kim <jonathan.kim@amd.com>
> > > Signed-off-by: Jesse Zhang <Jesse.Zhang@amd.com>
> > > ---
> > >  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 +-
> > >  drivers/gpu/drm/amd/amdkfd/kfd_debug.c        | 60 ++++++++++++-------
> > >  drivers/gpu/drm/amd/amdkfd/kfd_debug.h        |  2 +-
> > >  .../drm/amd/amdkfd/kfd_device_queue_manager.c |  6 ++
> > >  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 +
> > >  5 files changed, 50 insertions(+), 24 deletions(-)
> > >
> > > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > > b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > > index 3d2136adb62b..3d768511d002 100644
> > > --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
> > > @@ -3039,7 +3039,7 @@ static int runtime_disable(struct kfd_process
> > > *p)
> > >
> > >       if (p->debug_trap_enabled) {
> > >               if (was_enabled)
> > > -                     kfd_dbg_trap_deactivate(p, false, 0);
> > > +                     kfd_dbg_trap_deactivate(p, false);
> > >
> > >               if (!p->is_runtime_retry)
> > >
> > >       kfd_dbg_ev_raise(KFD_EC_MASK(EC_PROCESS_RUNTIME),
> > > @@ -3219,7 +3219,7 @@ static int kfd_ioctl_set_debug_trap(struct file
> > > *filep, struct kfd_process *p, v
> > >                                       args-
> > > >clear_node_address_watch.gpu_id);
> > >
> > >               pdd = kfd_process_device_data_by_id(target, user_gpu_id);
> > > -             if (user_gpu_id == -EINVAL || !pdd) {
> > > +             if (user_gpu_id == -EINVAL || !pdd ||
> > > + !pdd->debug_active) {
> > >                       r = -ENODEV;
> > >                       goto unlock_out;
> > >               }
> > > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > > b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > > index d5c87de519ac..b2d01613a21c 100644
> > > --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
> > > @@ -504,9 +504,13 @@ static void
> > > kfd_dbg_clear_process_address_watch(struct kfd_process *target)  {
> > >       int i, j;
> > >
> > > -     for (i = 0; i < target->n_pdds; i++)
> > > +     for (i = 0; i < target->n_pdds; i++) {
> > > +             if (!target->pdds[i]->debug_active)
> > > +                     continue;
> > > +
> > >               for (j = 0; j < MAX_WATCH_ADDRESSES; j++)
> > >                       kfd_dbg_trap_clear_dev_address_watch(target-
> > > >pdds[i], j);
> > > +     }
> > >  }
> > >
> > >  int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t
> > > *flags) @@ -517,8 +521,12 @@ int kfd_dbg_trap_set_flags(struct
> > > kfd_process *target, uint32_t *flags)
> > >       for (i = 0; i < target->n_pdds; i++) {
> > >               uint32_t caps;
> > >               uint32_t caps2;
> > > -             struct kfd_topology_device *topo_dev =
> > > -                     kfd_topology_device_by_id(target->pdds[i]->dev->id);
> > > +             struct kfd_topology_device *topo_dev;
> > > +
> > > +             if (!target->pdds[i]->debug_active)
> > > +                     continue;
> > > +
> > > +             topo_dev =
> > > + kfd_topology_device_by_id(target->pdds[i]->dev-
> > > >id);
> > >               if (!topo_dev)
> > >                       return -EINVAL;
> > >
> > > @@ -549,7 +557,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > > *target, uint32_t *flags)
> > >       for (i = 0; i < target->n_pdds; i++) {
> > >               struct kfd_process_device *pdd = target->pdds[i];
> > >
> > > -             if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
> > > +             if (!pdd->debug_active ||
> > > + !kfd_dbg_is_per_vmid_supported(pdd-
> > > >dev))
> > >                       continue;
> > >
> > >               if (!pdd->dev->kfd->shared_resources.enable_mes)
> > > @@ -562,7 +570,7 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > > *target, uint32_t *flags)
> > >                       break;
> > >               }
> > >
> > > -             rewind_count++;
> > > +             rewind_count = i + 1;
> > >       }
> > >
> > >       /* Rewind flags */
> > > @@ -572,7 +580,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > > *target, uint32_t *flags)
> > >               for (i = 0; i < rewind_count; i++) {
> > >                       struct kfd_process_device *pdd =
> > > target->pdds[i];
> > >
> > > -                     if (!kfd_dbg_is_per_vmid_supported(pdd->dev))
> > > +                     if (!pdd->debug_active ||
> > > +                         !kfd_dbg_is_per_vmid_supported(pdd->dev))
> > >                               continue;
> > >
> > >                       if (!pdd->dev->kfd->shared_resources.enable_mes)
> > > @@ -588,12 +597,8 @@ int kfd_dbg_trap_set_flags(struct kfd_process
> > > *target, uint32_t *flags)
> > >  /* kfd_dbg_trap_deactivate:
> > >   *   target: target process
> > >   *   unwind: If this is unwinding a failed kfd_dbg_trap_enable()
> > > - *   unwind_count:
> > > - *           If unwind == true, how far down the pdd list we need
> > > - *                           to unwind
> > > - *           else: ignored
> > >   */
> > > -void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind,
> > > int
> > > unwind_count)
> > > +void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind)
> > >  {
> > >       int i;
> > >
> > > @@ -614,12 +619,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process
> > > *target, bool unwind, int unwind
> > >       for (i = 0; i < target->n_pdds; i++) {
> > >               struct kfd_process_device *pdd = target->pdds[i];
> > >
> > > -             /* If this is an unwind, and we have unwound the required
> > > -              * enable calls on the pdd list, we need to stop now
> > > -              * otherwise we may mess up another debugger session.
> > > -              */
> > > -             if (unwind && i == unwind_count)
> > > -                     break;
> > > +             if (!pdd->debug_active)
> > > +                     continue;
> > > +             pdd->debug_active = false;
> > >
> > >               kfd_process_set_trap_debug_flag(&pdd->qpd, false);
> > >
> > > @@ -684,7 +686,7 @@ int kfd_dbg_trap_disable(struct kfd_process
> > > *target, bool
> > > drain_irqs)
> > >        * attached running target runtime state to enable for re-attach.
> > >        */
> > >       if (target->runtime_info.runtime_state ==
> > > DEBUG_RUNTIME_STATE_ENABLED)
> > > -             kfd_dbg_trap_deactivate(target, false, 0);
> > > +             kfd_dbg_trap_deactivate(target, false);
> > >       else if (target->runtime_info.runtime_state !=
> > > DEBUG_RUNTIME_STATE_DISABLED)
> > >               target->runtime_info.runtime_state =
> > > DEBUG_RUNTIME_STATE_ENABLED;
> > >
> > > @@ -715,6 +717,9 @@ int kfd_dbg_trap_activate(struct kfd_process
> > *target)
> > >       for (i = 0; i < target->n_pdds; i++) {
> > >               struct kfd_process_device *pdd = target->pdds[i];
> > >
> > > +             if (!pdd->drm_priv)
> > > +                     continue;
> > > +
> > >               if (!kfd_dbg_is_per_vmid_supported(pdd->dev)) {
> > >                       r = reserve_debug_trap_vmid(pdd->dev->dqm, &pdd-
> > > >qpd);
> > >
> > > @@ -727,6 +732,8 @@ int kfd_dbg_trap_activate(struct kfd_process
> > *target)
> > >                       }
> > >               }
> > >
> > > +             pdd->debug_active = true;
> > > +
> > >               /* Disable GFX OFF to prevent garbage read/writes to
> > > debug registers.
> > >                * If RLC restore of debug registers is not supported
> > > and runtime enable
> > >                * hasn't done so already on ttmp setup request, restore
> > > the trap config registers.
> > > @@ -775,7 +782,7 @@ int kfd_dbg_trap_activate(struct kfd_process
> > *target)
> > >       /* Enabling debug failed, we need to disable on
> > >        * all GPUs so the enable is all or nothing.
> > >        */
> > > -     kfd_dbg_trap_deactivate(target, true, i);
> > > +     kfd_dbg_trap_deactivate(target, true);
> > >       return r;
> > >  }
> > >
> > > @@ -825,7 +832,7 @@ int kfd_dbg_trap_enable(struct kfd_process
> > > *target, uint32_t fd,
> > >               atomic_inc(&target->debugger_process-
> > > >debugged_process_count);
> > >
> > >       if (copy_to_user(runtime_info, (void *)&target->runtime_info,
> > > copy_size)) {
> > > -             kfd_dbg_trap_deactivate(target, false, 0);
> > > +             kfd_dbg_trap_deactivate(target, false);
> > >               fput(target->dbg_ev_file);
> > >               target->dbg_ev_file = NULL;
> > >               if (target->debugger_process) @@ -851,7 +858,12 @@
> > > static int kfd_dbg_validate_trap_override_request(struct kfd_process
> > > *p,
> > >
> > >       for (i = 0; i < p->n_pdds; i++) {
> > >               struct kfd_process_device *pdd = p->pdds[i];
> > > -             int err = pdd->dev->kfd2kgd->validate_trap_override_request(
> > > +             int err;
> > > +
> > > +             if (!pdd->debug_active)
> > > +                     continue;
> > > +
> > > +             err = pdd->dev->kfd2kgd->validate_trap_override_request(
> > >
> > > pdd->dev-
> > > >adev,
> > >
> > > trap_override,
> > >
> > >       trap_mask_supported);
> > > @@ -886,6 +898,9 @@ int kfd_dbg_trap_set_wave_launch_override(struct
> > > kfd_process *target,
> > >       for (i = 0; i < target->n_pdds; i++) {
> > >               struct kfd_process_device *pdd = target->pdds[i];
> > >
> > > +             if (!pdd->debug_active)
> > > +                     continue;
> > > +
> > >               amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
> > >               pdd->spi_dbg_override = pdd->dev->kfd2kgd-
> > > >set_wave_launch_trap_override(
> > >                               pdd->dev->adev, @@ -922,6 +937,9 @@ int
> > > kfd_dbg_trap_set_wave_launch_mode(struct
> > > kfd_process *target,
> > >       for (i = 0; i < target->n_pdds; i++) {
> > >               struct kfd_process_device *pdd = target->pdds[i];
> > >
> > > +             if (!pdd->debug_active)
> > > +                     continue;
> > > +
> > >               amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
> > >               pdd->spi_dbg_launch_mode = pdd->dev->kfd2kgd-
> > > >set_wave_launch_mode(
> > >                               pdd->dev->adev, diff --git
> > > a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > > b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > > index 5babf9b992cd..e93d77fccda2 100644
> > > --- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
> > > @@ -25,7 +25,7 @@
> > >
> > >  #include "kfd_priv.h"
> > >
> > > -void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind,
> > > int unwind_count);
> > > +void kfd_dbg_trap_deactivate(struct kfd_process *target, bool
> > > +unwind);
> > >  int kfd_dbg_trap_activate(struct kfd_process *target);  int
> > > kfd_dbg_ev_query_debug_event(struct kfd_process *process,
> > >                       unsigned int *queue_id, diff --git
> > > a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > > b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > > index b5d7d13f31a5..b8ef93dba0af 100644
> > > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> > > @@ -3851,6 +3851,9 @@ int resume_queues(struct kfd_process *p,
> > >               struct queue *q;
> > >               int r, per_device_resumed = 0;
> > >
> > > +             if (!pdd->debug_active)
> > > +                     continue;
> > > +
> > >               dqm_lock(dqm);
> > >
> > >               /* unmask queues that resume or already resumed as valid
> > > */ @@ -3954,6 +3957,9 @@ int suspend_queues(struct kfd_process *p,
> > >               struct queue *q;
> > >               int r, per_device_suspended = 0;
> > >
> > > +             if (!pdd->debug_active)
> > > +                     continue;
> > > +
> > >               mutex_lock(&p->event_mutex);
> > >               dqm_lock(dqm);
> > >
> > > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > > b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > > index 129363ad4e34..ed85b7fb896e 100644
> > > --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> > > @@ -880,6 +880,8 @@ struct kfd_process_device {
> > >       uint32_t watch_points[4];
> > >       uint64_t watch_addresses[4];
> > >       uint32_t alloc_watch_ids;
> > > +     /* Debugging is activated on the device */
> > > +     bool debug_active;
> > >
> > >       /*
> > >        * If this process has been checkpointed before, then the user
> > > --
> > > 2.50.1
> >
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-20 14:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  9:04 [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging Jesse Zhang
2026-11-19  9:04 ` [v2 2/3] drm/amdkfd: return the memory violation data to the debugger Jesse Zhang
2026-11-19  9:04 ` [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails Jesse Zhang
2026-11-19 18:46   ` Kim, Jonathan
2026-11-20  8:17     ` Zhang, Jesse(Jie)
2026-11-19  9:26 ` [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging sashiko-bot
2026-11-19 17:17 ` Kim, Jonathan
2026-11-20  7:58   ` Zhang, Jesse(Jie)
2026-11-20 14:25     ` Kim, Jonathan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.