Skip to contentSkip to conversation

drm/amdkfd: don't flush the TLB of a device the process has no VM on

PATCH In review Jesse Zhang 3 participants 6 messages Nov 23

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

PATCH v3 1/3 drm/amdkfd: don't flush the TLB of a device the process has no VM on

A process has a PDD for every device, also the ones it never acquired a VM on, and pdd->drm_priv of those is NULL. kfd_flush_tlb() dereferences it unconditionally, which is how suspend_queues() oopsed before the previous patch.

Return early from kfd_flush_tlb() when there is no VM to flush, so no caller can oops on such a PDD.

---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7 -1
index 30797e1886d9..e84e364b4d64 100644
@@ -1608,7 +1608,13 @@ void kfd_signal_process_terminate_event(struct kfd_process *p);
 static inline void kfd_flush_tlb(struct kfd_process_device *pdd)
 {
 	struct amdgpu_device *adev = pdd->dev->adev;
-	struct amdgpu_vm *vm = drm_priv_to_vm(pdd->drm_priv);
+	struct amdgpu_vm *vm;
+
+	/* A PDD can exist before the process acquires a VM on this device. */
+	if (!pdd->drm_priv)
+		return;
+
+	vm = drm_priv_to_vm(pdd->drm_priv);
 
 	amdgpu_vm_flush_compute_tlb(adev, vm, TLB_FLUSH_HEAVYWEIGHT,
 				    pdd->dev->xcc_mask);
Signature · 1 line
2.50.1
#1 lore raw

PATCH v3 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.

Fixes 2b36de971d25 ("drm/amdkfd: add debug query exception info operation")
---
 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
index d5c87de519ac..14e7ee30cbdf 100644
@@ -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
index aae2c698103e..44b11950b7db 100644
@@ -1289,6 +1289,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
#2 lore raw

PATCH v3 3/3 drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails

In reply to 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)

An ENABLE of an already enabled session also overwrites the pointer temporarily, so the exit of the debugger of that session, which looks for its targets by this pointer before taking target->mutex, can miss the target and leave its debug mode enabled.

Set the pointer in kfd_dbg_trap_enable() only once the session is enabled, and clear it again when copying the runtime info fails. Step 3 now fails with -EPERM.

v2: clear the pointer instead of restoring it (Jon)
v3: set the pointer in kfd_dbg_trap_enable() (Jon)

Fixes 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and disable")
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 5 +----
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c   | 7 +++++--
 drivers/gpu/drm/amd/amdkfd/kfd_debug.h   | 4 ++--
 3 files changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -4
index 3d2136adb62b..5785cb8b4393 100644
@@ -3227,10 +3227,7 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, struct kfd_process *p, v
 
 	switch (args->op) {
 	case KFD_IOC_DBG_TRAP_ENABLE:
-		if (target != p)
-			target->debugger_process = p;
-
-		r = kfd_dbg_trap_enable(target,
+		r = kfd_dbg_trap_enable(target, p,
 					args->enable.dbg_fd,
 					(void __user *)args->enable.rinfo_ptr,
 					&args->enable.rinfo_size);
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +5 -2
index 14e7ee30cbdf..f7d5dc90f8e1 100644
@@ -781,8 +781,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
 	return r;
 }
 
-int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
-			void __user *runtime_info, uint32_t *runtime_size)
+int kfd_dbg_trap_enable(struct kfd_process *target, struct kfd_process *debugger,
+			uint32_t fd, void __user *runtime_info, uint32_t *runtime_size)
 {
 	struct file *f;
 	uint32_t copy_size;
@@ -823,6 +823,8 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 	kref_get(&target->ref);
 	target->debug_trap_enabled = true;
 
+	if (target != debugger)
+		target->debugger_process = debugger;
 	if (target->debugger_process)
 		atomic_inc(&target->debugger_process->debugged_process_count);
 
@@ -832,6 +834,7 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 		target->dbg_ev_file = NULL;
 		if (target->debugger_process)
 			atomic_dec(&target->debugger_process->debugged_process_count);
+		target->debugger_process = NULL;
 		target->debug_trap_enabled = false;
 		kfd_unref_process(target);
 		r = -EFAULT;
drivers/gpu/drm/amd/amdkfd/kfd_debug.h +2 -2
index 5babf9b992cd..f675fe6ed353 100644
@@ -44,8 +44,8 @@ bool kfd_dbg_ev_raise(uint64_t event_mask,
 			void *exception_data,
 			size_t exception_data_size);
 int kfd_dbg_trap_disable(struct kfd_process *target, bool drain_irqs);
-int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
-			void __user *runtime_info,
+int kfd_dbg_trap_enable(struct kfd_process *target, struct kfd_process *debugger,
+			uint32_t fd, void __user *runtime_info,
 			uint32_t *runtime_info_size);
 int kfd_dbg_trap_set_wave_launch_override(struct kfd_process *target,
 					uint32_t trap_override,
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH v3 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/amdkfd: premature debugger_process store in kfd_dbg_trap_enable() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/amdkfd: premature debugger_process store in kfd_dbg_trap_enable()
--

commit aae136214c876252feb4eb6f8aa225bc102db225
Author: Jesse Zhang <[email protected]>

drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails

This commit moves the assignment of target->debugger_process into kfd_dbg_trap_enable() and clears it if copy_to_user() fails, preventing a failed KFD_IOC_DBG_TRAP_ENABLE call from leaving target->debugger_process set.

Quoted 25 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +4 -2
index a025c3189753b..cf62aaffc2600 100644
@@ -793,8 +793,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
 	return r;
 }
 
-int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
-			void __user *runtime_info, uint32_t *runtime_size)
+int kfd_dbg_trap_enable(struct kfd_process *target, struct kfd_process *debugger,
+			uint32_t fd, void __user *runtime_info, uint32_t *runtime_size)
 {
 	struct file *f;
 	uint32_t copy_size;
@@ -835,6 +835,8 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 	kref_get(&target->ref);
 	target->debug_trap_enabled = true;
 
+	if (target != debugger)
+		target->debugger_process = debugger;
 	if (target->debugger_process)
 		atomic_inc(&target->debugger_process->debugged_process_count);
 

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd, · line 852

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1 -0
@@ -844,6 +846,7 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 		target->dbg_ev_file = NULL;
 		if (target->debugger_process)
 			atomic_dec(&target->debugger_process->debugged_process_count);
+		target->debugger_process = NULL;
 		target->debug_trap_enabled = false;
 		kfd_unref_process(target);
 		r = -EFAULT;

[Severity: Medium]
Can setting target->debugger_process before copy_to_user() still allow the caller to bypass the ptrace check in kfd_ioctl_set_debug_trap()?

kfd_ioctl_set_debug_trap() checks target->debugger_process before taking target->mutex:

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c:kfd_ioctl_set_debug_trap() {
    ...
	rcu_read_lock();
	if (target != p && ptrace_parent(target->lead_thread) != current
			&& target->debugger_process != p) {
		pr_err("PID %i is not PTRACED and cannot be debugged\n", args->pid);
		r = -EPERM;
	}
	rcu_read_unlock();

	if (r)
		goto out;

	mutex_lock(&target->mutex);
    ...
}

If copy_to_user() sleeps (for example via userfaultfd) and then fails with -EFAULT, a second thread in the debugger process can pass the unlocked permission check after ptrace is detached and wait on target->mutex:

CPU0 CPU1
kfd_ioctl_set_debug_trap()
mutex_lock(&target->mutex)
kfd_dbg_trap_enable()

    target->debugger_process = p
    copy_to_user() [sleeps]
                                  [ptrace detached]
                                  kfd_ioctl_set_debug_trap()
                                    target->debugger_process == p
                                    mutex_lock(&target->mutex)
    /* copy_to_user() fails */
    target->debugger_process = NULL
    target->debug_trap_enabled = false

mutex_unlock(&target->mutex)
kfd_dbg_trap_enable() <-- succeeds without ptrace

Could copy_to_user() be called before setting target->debugger_process and activating the debug session?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#4 lore raw

RE: [PATCH v3 3/3] drm/amdkfd: don't keep the debugger pointer when DBG_TRAP_ENABLE fails

In reply to Jesse Zhang

Public

Quoted 86 lines

-----Original Message-----
From: Jesse Zhang <[email protected]>
Sent: Sunday, November 22, 2026 9:47 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: [PATCH v3 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)

An ENABLE of an already enabled session also overwrites the pointer temporarily, so the exit of the debugger of that session, which looks for its targets by this pointer before taking target->mutex, can miss the target and leave its debug mode enabled.

Set the pointer in kfd_dbg_trap_enable() only once the session is enabled, and clear it again when copying the runtime info fails. Step 3 now fails with -EPERM.

v2: clear the pointer instead of restoring it (Jon)
v3: set the pointer in kfd_dbg_trap_enable() (Jon)

Fixes 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and

disable")

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 5 +----
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c   | 7 +++++--
 drivers/gpu/drm/amd/amdkfd/kfd_debug.h   | 4 ++--
 3 files changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index 3d2136adb62b..5785cb8b4393 100644
@@ -3227,10 +3227,7 @@ static int kfd_ioctl_set_debug_trap(struct file *filep,

struct kfd_process *p, v

      switch (args->op) {
      case KFD_IOC_DBG_TRAP_ENABLE:
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -4
-             if (target != p)
-                     target->debugger_process = p;
-
-             r = kfd_dbg_trap_enable(target,
+             r = kfd_dbg_trap_enable(target, p,
                                      args->enable.dbg_fd,
                                      (void __user *)args->enable.rinfo_ptr,
                                      &args->enable.rinfo_size);
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -2
index 14e7ee30cbdf..f7d5dc90f8e1 100644
@@ -781,8 +781,8 @@ int kfd_dbg_trap_activate(struct kfd_process *target)
      return r;
 }
 
-int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
-                     void __user *runtime_info, uint32_t *runtime_size)
+int kfd_dbg_trap_enable(struct kfd_process *target, struct kfd_process
 *debugger,
+                     uint32_t fd, void __user *runtime_info, uint32_t
 *runtime_size)
 {
      struct file *f;
      uint32_t copy_size;

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_enable(struct kfd_process *target, · line 829

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -0
@@ -823,6 +823,8 @@ int kfd_dbg_trap_enable(struct kfd_process *target,
 uint32_t fd,
      kref_get(&target->ref);
      target->debug_trap_enabled = true;
 
+     if (target != debugger)
+             target->debugger_process = debugger;
      if (target->debugger_process)

We can probably get rid of this test and tuck the atomic_inc under the target != debugger test after debugger_process has been assigned, as create_process will assign debugger_process as NULL by default.

With that fixed this patch is Reviewed-by: Jonathan Kim <[email protected]>

Jon

Quoted 32 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0 -0
              atomic_inc(&target->debugger_process-
Quoted 1 line

debugged_process_count);

drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1 -0
@@ -832,6 +834,7 @@ int kfd_dbg_trap_enable(struct kfd_process *target,
 uint32_t fd,
              target->dbg_ev_file = NULL;
              if (target->debugger_process)
                      atomic_dec(&target->debugger_process-
 >debugged_process_count);
+             target->debugger_process = NULL;
              target->debug_trap_enabled = false;
              kfd_unref_process(target);
              r = -EFAULT;
drivers/gpu/drm/amd/amdkfd/kfd_debug.h +2 -2
index 5babf9b992cd..f675fe6ed353 100644
@@ -44,8 +44,8 @@ bool kfd_dbg_ev_raise(uint64_t event_mask,
                      void *exception_data,
                      size_t exception_data_size);
 int kfd_dbg_trap_disable(struct kfd_process *target, bool drain_irqs);
-int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
-                     void __user *runtime_info,
+int kfd_dbg_trap_enable(struct kfd_process *target, struct kfd_process
 *debugger,
+                     uint32_t fd, void __user *runtime_info,
                      uint32_t *runtime_info_size);
 int kfd_dbg_trap_set_wave_launch_override(struct kfd_process *target,

uint32_t trap_override,
--
2.50.1

#5 lore raw
In reply to Jesse Zhang
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/amdkfd: commit message cites missing patch and omits Fixes trailer --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/amdkfd: commit message cites missing patch and omits Fixes trailer
--

commit 49a7c752d976cee4f89634bba3990ab398ece2e3
Author: Jesse Zhang <[email protected]>

Quoted 7 lines

drm/amdkfd: don't flush the TLB of a device the process has no VM on

A process has a PDD for every device, also the ones it never acquired a VM on, and pdd->drm_priv of those is NULL. kfd_flush_tlb() dereferences it unconditionally, which is how suspend_queues() oopsed before the previous patch.

Quoted 5 lines

Return early from kfd_flush_tlb() when there is no VM to flush, so no caller can oops on such a PDD.

[Severity: Low]
There is no preceding patch in the tree modifying suspend_queues(), which still calls kfd_flush_tlb(pdd) on PDDs without a VM when total_suspended >
0. Because this commit directly fixes that NULL pointer dereference, should the reference to a previous patch be updated and a Fixes tag added?

Fixes f5c4f88e0f9c ("drm/amdkfd: Add TLB flush after MES queue eviction/suspension")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1