drm/amdkfd: leave the devices a process has no VM on out of debugging
Subsystems (3)
PATCH 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:
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.
--- 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(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -2
@@ -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; }
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +39 -21
153 lines not on this page · raw message
drivers/gpu/drm/amd/amdkfd/kfd_debug.h +1 -1
@@ -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,
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +6 -0
@@ -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);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
@@ -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
Signature · 1 line
2.50.1
PATCH v2 2/3 drm/amdkfd: return the memory violation data to the debugger
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.
--- 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(-)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3 -1
@@ -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" );
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
@@ -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 ; }
Signature · 1 line
2.50.1
PATCH v2 3/3 drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
In reply to Jesse ZhangKFD_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)
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +13 -0
@@ -3227,6 +3227,16 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, vswitch (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 :
Signature · 1 line
2.50.1
RE: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
Public
Quoted 59 lines
-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Thursday, November 19, 2026 4:05 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: [v2 3/3] drm/amdkfd: don't keep the debugger pointer whenDBG_TRAP_ENABLEfails
KFD_IOC_DBG_TRAP_ENABLEstores the caller intarget->debugger_processbefore callingkfd_dbg_trap_enable(), and nothing resets it when that fails (-EALREADY,-ENODEV,-EBUSY,-EBADFfor a baddbg_fd,-EFAULT). The permission check at the top ofkfd_ioctl_set_debug_trap()accepts any caller equal totarget->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 incrementsdebugged_process_countof 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
-EALREADYfor an already enabled session before touching the pointer at all: the exit of the debugger of that session looks for its targets throughtarget->debugger_processbefore it takestarget->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 anddisable")
Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -3227,6 +3227,16 @@ static int kfd_ioctl_set_debug_trap(struct file *filep,struct
kfd_process*p, vswitch (args->op) { case KFD_IOC_DBG_TRAP_ENABLE:drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0+/* +* Don't change the debugger of an enabled session, not even
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v · line 3241
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +8
-0+* 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)?
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v · line 3251
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -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-Quoted 1 line
enable.exception_mask;
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3
-0+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
Quoted 5 lines
break; case KFD_IOC_DBG_TRAP_DISABLE:--
2.50.1
RE: [v2 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails
Public
Quoted 103 lines
-----Original Message-----
From: Kim, Jonathan <[email protected]>
Sent: Friday, November 20, 2026 2:46 AM
To: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: RE: [v2 3/3] drm/amdkfd: don't keep the debugger pointer whenDBG_TRAP_ENABLEfailsPublic
Quoted 6 lines
-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Thursday, November 19, 2026 4:05 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>;Zhang,
Quoted 53 lines
Jesse(Jie) <[email protected]>
Subject: [v2 3/3] drm/amdkfd: don't keep the debugger pointer whenDBG_TRAP_ENABLEfails
KFD_IOC_DBG_TRAP_ENABLEstores the caller intarget->debugger_processbefore callingkfd_dbg_trap_enable(), and nothing resets it when that fails (-EALREADY,-ENODEV,-EBUSY,-EBADFfor a baddbg_fd,-EFAULT). The permission check at the top ofkfd_ioctl_set_debug_trap()accepts any caller equal totarget->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 incrementsdebugged_process_countof 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
-EALREADYfor an already enabled session before touching the pointer at all: the exit of the debugger of that session looks for its targets throughtarget->debugger_processbefore it takestarget->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 anddisable")
Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0@@ -3227,6 +3227,16 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, vswitch (args->op) {case KFD_IOC_DBG_TRAP_ENABLE : +/* +* Don't change the debugger of an enabled session, not even drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_set_debug_trap(struct file · line 3242
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +8
-0+* 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-Quoted 1 line
debugger_process.Current debugger is still exiting, doesn't find itself in target table because of new debugger
target->debugger_processoverwrite (since new debugger isn't holding the scru lock) and so current debugger doesn't disable target on exit. New debugger hits-EALREADYbecause current debugger didn't disable the target so debug mode is left dangling.Could we move this conditional assignment inside
kfd_dbg_trap_enableafter it sets debug_trap_enabled=true and before it atomic counts the caller process (pass p as an arg -> structkfd_process*debugger)?Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -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-
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v · line 3251
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3
-0>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_enableas well whencopy_to_userfails 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
Quoted 9 lines
Jon
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0break ;case
KFD_IOC_DBG_TRAP_DISABLE:
--
2.50.1
[email protected]
AI reviewThank 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…
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 <[email protected]>
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.
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-0
[ ... ]
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-1@@ -626,12 +631,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwindfor (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
drivers/gpu/drm/amd/amdkfd/kfd_debug.c void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwind · line 638
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-5-* 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?
[ ... ]
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0@@ -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);
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process *target) · line 747
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1
-0@@ -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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Public
Quoted 10 lines
-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Thursday, November 19, 2026 4:05 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Zhang, Jesse(Jie) <[email protected]>; Kim, Jonathan <[email protected]>
Subject: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debugging
Quoted 5 lines
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
Quoted 311 lines
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 <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- 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(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-1@@ -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),
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -3219,7 +3219,7 @@ static int kfd_ioctl_set_debug_trap(struct file *filep,struct
kfd_process*p, v
args-Quoted 1 line
clear_node_address_watch.gpu_id);
pdd = kfd_process_device_data_by_id(target, user_gpu_id);drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-1-if (user_gpu_id == -EINVAL || !pdd) { +if (user_gpu_id == -EINVAL || !pdd || !pdd->debug_active) { r = -ENODEV ; goto unlock_out; }drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5
-1@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6
-2@@ -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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1
-1@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1
-1@@ -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 */drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-1@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1
-5@@ -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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +4
-7@@ -614,12 +619,9 @@ void kfd_dbg_trap_deactivate(struct kfd_process *target, bool unwind, int unwindfor (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, booldrain_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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5
-0@@ -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.drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-2@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6
-1@@ -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);drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0@@ -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-Quoted 1 line
set_wave_launch_trap_override(
pdd->dev->adev,drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0@@ -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-Quoted 1 line
set_wave_launch_mode(
pdd->dev->adev,drivers/gpu/drm/amd/amdkfd/kfd_debug.h +1
-1@@ -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,drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +6
-0@@ -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);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-1@@ -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
Public
Quoted 47 lines
-----Original Message-----
From: Kim, Jonathan <[email protected]>
Sent: Friday, November 20, 2026 1:18 AM
To: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debuggingPublic
Quoted 6 lines
-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Thursday, November 19, 2026 4:05 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>;Zhang,
Quoted 1 line
Jesse(Jie) <[email protected]>; Kim, Jonathan
Quoted 9 lines
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.
Quoted 6 lines
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
Quoted 327 lines
Jon
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process
Quoted 177 lines
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 <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- 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(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-1@@ -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),
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-1@@ -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; }drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5
-1@@ -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 @@ intkfd_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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +7
-2-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-Quoted 1 line
id);
if (!topo_dev) return -EINVAL;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-2@@ -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 */drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-1@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-5@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5
-7+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 unwindfor (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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-0@@ -715,6 +717,9 @@ int kfd_dbg_trap_activate(struct kfd_process*target)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0for (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)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-0} } + 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.drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-0@@ -775,7 +782,7 @@ int kfd_dbg_trap_activate(struct kfd_process*target)
Quoted 120 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-2/* 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];drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6
-1- 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-
Quoted 1 line
adev,
trap_override,trap_mask_supported);drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0@@ -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-Quoted 1 line
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];drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0+if (!pdd->debug_active) +continue ; +amdgpu_gfx_off_ctrl (pdd->dev->adev, false); pdd->spi_dbg_launch_mode = pdd->dev->kfd2kgd-Quoted 1 line
set_wave_launch_mode(
pdd->dev->adev, diff --git
a/drivers/gpu/drm/amd/amdkfd/kfd_debug.hb/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
index5babf9b992cd..e93d77fccda2100644drivers/gpu/drm/amd/amdkfd/kfd_debug.h +2
-1@@ -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.cb/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.cindexb5d7d13f31a5..b8ef93dba0af100644drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3
-0@@ -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;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3
-0+if (!pdd->debug_active) +continue ; +mutex_lock (&p->event_mutex);dqm_lock (dqm);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-1@@ -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
Public
Quoted 73 lines
-----Original Message-----
From: Zhang, Jesse(Jie) <[email protected]>
Sent: Friday, November 20, 2026 2:58 AM
To: Kim, Jonathan <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Subject: RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debuggingPublic
Quoted 47 lines
-----Original Message-----
From: Kim, Jonathan <[email protected]>
Sent: Friday, November 20, 2026 1:18 AM
To: Zhang, Jesse(Jie) <[email protected]>; amd- [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: RE: [v2 1/3] drm/amdkfd: leave the devices a process has no VM on out of debuggingPublic
Quoted 6 lines
-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Thursday, November 19, 2026 4:05 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>;Zhang,
Quoted 1 line
Jesse(Jie) <[email protected]>; Kim, Jonathan
Quoted 9 lines
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.
Quoted 6 lines
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 callskfd_flush_tlb()becausetotal_suspendedis already nonzero.
Quoted 4 lines
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
Quoted 331 lines
Thanks,
JessQuoted 327 lines
Jon
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process
Quoted 177 lines
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 <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- 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(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-1@@ -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),
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-1@@ -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; }drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5
-1@@ -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 @@ intkfd_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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +7
-2-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-Quoted 1 line
id);
if (!topo_dev) return -EINVAL;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-2@@ -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 */drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-1@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-5@@ -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)drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5
-7+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 unwindfor (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;drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-0@@ -715,6 +717,9 @@ int kfd_dbg_trap_activate(struct kfd_process*target)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0for (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)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_activate(struct kfd_process
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-0} } + 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.drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0
-0@@ -775,7 +782,7 @@ int kfd_dbg_trap_activate(struct kfd_process*target)
Quoted 120 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2
-2/* 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];drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6
-1- 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-
Quoted 1 line
adev,
trap_override,trap_mask_supported);drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0@@ -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-Quoted 1 line
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];drivers/gpu/drm/amd/amdkfd/kfd_debug.c +3
-0+if (!pdd->debug_active) +continue ; +amdgpu_gfx_off_ctrl (pdd->dev->adev, false); pdd->spi_dbg_launch_mode = pdd->dev->kfd2kgd-Quoted 1 line
set_wave_launch_mode(
pdd->dev->adev, diff --git
a/drivers/gpu/drm/amd/amdkfd/kfd_debug.hb/drivers/gpu/drm/amd/amdkfd/kfd_debug.h
index5babf9b992cd..e93d77fccda2100644drivers/gpu/drm/amd/amdkfd/kfd_debug.h +2
-1@@ -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.cb/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.cindexb5d7d13f31a5..b8ef93dba0af100644drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3
-0@@ -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;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3
-0+if (!pdd->debug_active) +continue ; +mutex_lock (&p->event_mutex);dqm_lock (dqm);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-1@@ -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