From: "Chris Mason" <mason@kernel.org>
To: brauner@kernel.org, oleg@redhat.com, ast@kernel.org,
daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: "Felix Kuehling" <Felix.Kuehling@amd.com>,
amd-gfx@lists.freedesktop.org, akpm@linux-foundation.org,
peterz@infradead.org, mingo@redhat.com, kees@kernel.org,
viro@zeniv.linux.org.uk, jack@suse.cz, tj@kernel.org,
hannes@cmpxchg.org, mkoutny@suse.com, bsingharora@gmail.com,
eddyz87@gmail.com, memxor@gmail.com, yonghong.song@linux.dev,
martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org,
emil@etsalapatis.com, ihor.solodrai@linux.dev,
sched-ext@lists.linux.dev, cgroups@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
mason@kernel.org
Subject: [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits
Date: Sat, 05 Dec 2026 08:12:55 -0500 [thread overview]
Message-ID: <20261204233428.14861-7-mason@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-1-mason@kernel.org>
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@kernel.org>
---
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);
next prev parent reply other threads:[~2026-12-05 13:13 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 13:11 [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers Chris Mason
2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
2026-12-05 13:12 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Chris Mason
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:54 ` bot+bpf-ci
2026-12-05 13:12 ` [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Chris Mason
2026-12-05 13:12 ` [PATCH v2 5/8] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-05 13:12 ` Chris Mason [this message]
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason
2026-12-05 13:54 ` bot+bpf-ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204233428.14861-7-mason@kernel.org \
--to=mason@kernel.org \
--cc=Felix.Kuehling@amd.com \
--cc=akpm@linux-foundation.org \
--cc=amd-gfx@lists.freedesktop.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=bsingharora@gmail.com \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hannes@cmpxchg.org \
--cc=ihor.solodrai@linux.dev \
--cc=jack@suse.cz \
--cc=jolsa@kernel.org \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=sched-ext@lists.linux.dev \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox