Skip to contentSkip to conversation

drm/amdkfd: Unwind debug trap enable on copy_to_user failure

PATCH In review Yongqiang Sun 2 participants 2 messages Jun 2

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

PATCH 1/1 drm/amdkfd: Unwind debug trap enable on copy_to_user failure

If kfd_dbg_trap_enable() fails while copying runtime_info to userspace, it had already activated the trap, set debug_trap_enabled, taken an extra process reference, and opened the debug event file. Return -EFAULT without unwinding that state, leaving inconsistent trap state and a refcount imbalance that could break later DISABLE/ENABLE.

On copy_to_user failure, deactivate the trap and undo the rest of the enable setup before returning.

---
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6 -0
index 0f7aa51b629e..0dd1fd448059 100644
@@ -832,6 +832,12 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 
 	if (copy_to_user(runtime_info, (void *)&target->runtime_info, copy_size)) {
 		kfd_dbg_trap_deactivate(target, false, 0);
+		fput(target->dbg_ev_file);
+		target->dbg_ev_file = NULL;
+		if (target->debugger_process)
+			atomic_dec(&target->debugger_process->debugged_process_count);
+		target->debug_trap_enabled = false;
+		kfd_unref_process(target);
 		r = -EFAULT;
 	}
 
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Yongqiang Sun

If kfd_dbg_trap_enable() fails while copying runtime_info to userspace, it had already activated the trap, set debug_trap_enabled, taken an extra process reference, and opened the debug event file. Return -EFAULT without unwinding that state, leaving inconsistent trap state and a refcount imbalance that could break later DISABLE/ENABLE.

On copy_to_user failure, deactivate the trap and undo the rest of the enable setup before returning.

Quoted 24 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6 -1
index 0f7aa51b629e..0dd1fd448059 100644
@@ -832,6 +832,12 @@ int kfd_dbg_trap_enable(struct kfd_process *target, uint32_t fd,
 
        if (copy_to_user(runtime_info, (void *)&target->runtime_info, copy_size)) {
                kfd_dbg_trap_deactivate(target, false, 0);
+               fput(target->dbg_ev_file);
+               target->dbg_ev_file = NULL;
+               if (target->debugger_process)
+                       atomic_dec(&target->debugger_process->debugged_process_count);
+               target->debug_trap_enabled = false;
+               kfd_unref_process(target);
                r = -EFAULT;
        }
 
--

2.43.0