[PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits

From: Chris Mason

Date: Sat Dec 05 2026 - 08:13:22 EST


kfd_create_process() hits a NULL dereference or a use-after-free if a
process exits during a KFD_IOC_DBG_TRAP_ENABLE for it.

It takes a reference on thread->mm, but it and create_process() read the
pointer again:

kfd_create_process(thread) thread exits
========================== ============
mmget_not_zero(thread->mm)
exit_mm()
/* thread->mm = NULL */
mmput(thread->mm)
/* NULL dereference */

create_process() takes a reference on the group leader without RCU or a
pid_alive() check:

create_process(thread) process exits
====================== =============
leader = thread->group_leader
release_task(leader)
/* freed after a grace period */
get_task_struct(leader)
/* use-after-free */

The fix used here is to remember the mm, and to take the leader's reference
under RCU if the thread is still alive.

Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and disable")
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
---
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +-
drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 ++-
drivers/gpu/drm/amd/amdkfd/kfd_process.c | 33 +++++++++++++++---------
3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 3984b2c3bf45d..a46a4e498ac51 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -3363,7 +3363,7 @@ static int kfd_ioctl_create_process(struct file *filep, struct kfd_process *p, v
return -EINVAL;
}

- process = create_process(current, false);
+ process = create_process(current, current->mm, false);
if (IS_ERR(process)) {
mutex_unlock(&kfd_processes_mutex);
return PTR_ERR(process);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 70d67f9d8cd75..e70d47ec8617d 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -1113,7 +1113,8 @@ struct amdkfd_ioctl_desc {
};
bool kfd_dev_is_large_bar(struct kfd_node *dev);

-struct kfd_process *create_process(const struct task_struct *thread, bool primary);
+struct kfd_process *create_process(const struct task_struct *thread,
+ struct mm_struct *mm, bool primary);
int kfd_process_create_wq(void);
void kfd_process_destroy_wq(void);
void kfd_cleanup_processes(void);
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index 336c66d9c5a73..458637a81f66c 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -66,8 +66,6 @@ static struct workqueue_struct *kfd_process_wq;
*/
static struct workqueue_struct *kfd_restore_wq;

-static struct kfd_process *find_process(const struct task_struct *thread,
- bool ref);
static void kfd_process_ref_release(struct kref *ref);

static void evict_process_worker(struct work_struct *work);
@@ -966,9 +964,11 @@ static void kfd_process_free_id(struct kfd_process *process)
struct kfd_process *kfd_create_process(struct task_struct *thread)
{
struct kfd_process *process;
+ struct mm_struct *mm;
int ret;

- if (!(thread->mm && mmget_not_zero(thread->mm)))
+ mm = get_task_mm(thread);
+ if (!mm)
return ERR_PTR(-EINVAL);

/* If the process just called exec(3), it is possible that the
@@ -999,13 +999,13 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
}

/* A prior open of /dev/kfd could have already created the process.
- * find_process will increase process kref in this case
+ * kfd_lookup_process_by_mm will increase process kref in this case
*/
- process = find_process(thread, true);
+ process = kfd_lookup_process_by_mm(mm);
if (process) {
pr_debug("Process already found\n");
} else {
- process = create_process(thread, true);
+ process = create_process(thread, mm, true);
if (IS_ERR(process))
goto out;

@@ -1025,7 +1025,7 @@ struct kfd_process *kfd_create_process(struct task_struct *thread)
}
out:
mutex_unlock(&kfd_processes_mutex);
- mmput(thread->mm);
+ mmput(mm);

return process;
}
@@ -1680,7 +1680,8 @@ void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
* On return the kfd_process is fully operational and will be freed when the
* mm is released
*/
-struct kfd_process *create_process(const struct task_struct *thread, bool primary)
+struct kfd_process *create_process(const struct task_struct *thread,
+ struct mm_struct *mm, bool primary)
{
struct kfd_process *process;
struct mmu_notifier *mn;
@@ -1692,13 +1693,20 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar

kref_init(&process->ref);
mutex_init(&process->mutex);
- process->mm = thread->mm;
- process->lead_thread = thread->group_leader;
+ process->mm = mm;
/*
* Take the reference on the lead thread before the process can be
* released through kfd_process_wq_release(), which drops it again.
+ * A process that's being debugged can exit meanwhile.
*/
- get_task_struct(process->lead_thread);
+ rcu_read_lock();
+ if (pid_alive(thread))
+ process->lead_thread = get_task_struct(thread->group_leader);
+ rcu_read_unlock();
+ if (!process->lead_thread) {
+ err = -ESRCH;
+ goto err_lead_thread;
+ }
process->n_pdds = 0;
process->queues_paused = false;

@@ -1816,8 +1824,9 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar
err_process_pqm_init:
kfd_event_free_process(process);
err_event_init:
- mutex_destroy(&process->mutex);
put_task_struct(process->lead_thread);
+err_lead_thread:
+ mutex_destroy(&process->mutex);
kfree(process);
err_alloc_process:
return ERR_PTR(err);