BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chris Mason" <mason@kernel.org>,
	brauner@kernel.org, oleg@redhat.com, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, bpf@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, linux-mm@kvack.org
Subject: Re: [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits
Date: Sat, 05 Dec 2026 13:40:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-251594@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-7-mason@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/amdkfd: missing READ_ONCE() on thread->group_leader in
  create_process()
--

commit f69fe80acbca9fdd8995442c66a399acab3c12dc
Author: Chris Mason <mason@kernel.org>

drm/amdkfd: fix crashes if a debugged process exits

This commit caches the target task's mm via get_task_mm() in
kfd_create_process() and takes the group_leader reference under RCU after
checking pid_alive() in create_process().

> 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

[ ... ]

> @@ -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();

[Severity: Medium]
Can passing thread->group_leader without READ_ONCE() into get_task_struct()
cause a refcount mismatch and use-after-free?

get_task_struct() is an inline helper that calls refcount_inc(&t->usage)
(atomic_fetch_add_relaxed()) and returns t.  On architectures where
atomic_fetch_add_relaxed() has no compiler memory clobber (such as arm64
LSE), the compiler can reload the unmarked thread->group_leader across
refcount_inc():

CPU0                               CPU1
kfd_ioctl_set_debug_trap()
  ...
    create_process(thread, ...)
      pid_alive(thread) == true
      t = thread->group_leader
      refcount_inc(&t->usage)
                                   release_task(thread)
                                     reset_parent_and_leader(thread)
                                       WRITE_ONCE(thread->group_leader,
                                                  thread)
      process->lead_thread =
        thread->group_leader       <-- reloaded without refcount

With commit "exit: reset a released task's parent and leader" updating
group_leader concurrently, a reload leaks the old leader's reference and
stores the new one without a reference.  kfd_process_wq_release() then
underflows the refcount, leading to a use-after-free (and a KCSAN data race).

Using READ_ONCE(thread->group_leader) here prevents both the reload and the
data race.  Although the follow-up RFC commit "signal: add
task_group_leader()" later wraps this access in READ_ONCE(), this non-RFC
fix should use it directly in case it is merged or backported on its own.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204233428.14861-1-mason@kernel.org?part=6

  reply	other threads:[~2026-12-05 13:40 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:36   ` sashiko-bot
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 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:40   ` sashiko-bot [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

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=sashiko-outbox-251594@kernel.org \
    --to=sashiko-bot@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=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-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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