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

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.