From: sashiko-bot@kernel.org
To: "Chris Mason" <mason@kernel.org>, bpf@vger.kernel.org
Cc: sched-ext@lists.linux.dev
Subject: Re: [PATCH 2/3] exit: don't leave stale parent and leader pointers
Date: Thu, 03 Dec 2026 20:31:41 +0000 [thread overview]
Message-ID: <sashiko-outbox-248636@kernel.org> (raw)
In-Reply-To: <20261203200612.26376-3-mason@kernel.org>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] exit: plain store to p->group_leader in release_task() races
with RCU readers
- [Medium] exit: leader pinning creates refcount cycle with BPF task
storage kptr
- [Medium] exit: release_task() bounces leader cacheline 0 via usage
refcount
- [Low] exit: unlocked notify_count read in release_task() races with
unmarked writers
--
commit 6d90216e0968878272ede88e228cf18f77903929
Author: Chris Mason <mason@kernel.org>
exit: don't leave stale parent and leader pointers
This commit resets real_parent and parent to init_task in release_task()
and pins a released sub-thread's group_leader until __put_task_struct() so
BPF programs holding a reference to a released task cannot follow dangling
pointers.
> diff --git a/kernel/exit.c b/kernel/exit.c
> index d5e7bc2af804a..74d7a93355cbc 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
[ ... ]
> @@ -262,6 +263,17 @@ void release_task(struct task_struct *p)
> */
> thread_pid = get_pid(task_pid(p));
>
> + /*
> + * A released thread pins its leader, which lockless next_thread()
> + * needs. Until @p is released its leader can neither change nor go
> + * away. notify_count < 0 means mt-exec: @p is the old leader, and
> + * must not pin the new one, or repeated execs could build a chain.
> + */
> + leader = p->group_leader;
> + pin_leader = leader != p && READ_ONCE(p->signal->notify_count) >= 0;
> + if (pin_leader)
> + get_task_struct(leader);
> +
[Severity: Low]
Does this unlocked READ_ONCE(p->signal->notify_count) race with unmarked
writers to sig->notify_count?
Because release_task() reads notify_count before taking tasklist_lock and
without sighand->siglock, it can run concurrently with plain stores in
de_thread() (under siglock or read_lock(&tasklist_lock)) and unmarked
decrements in __exit_signal() (under tasklist_lock and siglock). That
triggers KCSAN data-race warnings during concurrent multi-threaded execve()
and sub-thread exits.
Moving the pin_leader check into __exit_signal() under sighand->siglock, or
annotating the writers with WRITE_ONCE(), would avoid the data race.
[Severity: Medium]
Does calling get_task_struct(leader) on every non-leader thread exit
introduce unnecessary cacheline 0 false sharing on the thread group leader?
Together with put_task_struct(tsk->group_leader) in __put_task_struct(),
every exiting sub-thread now performs two atomic updates to leader->usage,
even when no BPF programs are loaded. In struct task_struct, usage sits in
cacheline 0 alongside thread_info, __state, stack, and flags, so frequent
short-lived worker exits (such as thread pools or io-wq workers) bounce the
active leader's hottest cacheline across CPUs and contend on leader->usage
during mass thread teardown.
Only check_unsafe_exec() and cgroup_migrate()/cgroup_attach_task() walk
threads with next_thread() locklessly; converting those two sites to
for_each_thread() would allow release_task() to set p->group_leader = p
unconditionally without touching leader->usage.
[ ... ]
> @@ -288,6 +299,15 @@ void release_task(struct task_struct *p)
> leader->exit_state = EXIT_DEAD;
> }
>
> + /*
> + * @p is off its lists, so nobody can update these pointers any more,
> + * but they have to stay valid for whoever holds a reference to @p.
> + */
> + RCU_INIT_POINTER(p->real_parent, &init_task);
> + RCU_INIT_POINTER(p->parent, &init_task);
> + if (!pin_leader)
> + p->group_leader = p;
> +
> write_unlock_irq(&tasklist_lock);
[Severity: Medium]
Can this unmarked store to p->group_leader race with lockless RCU readers?
BPF programs (via BTF_TYPE_SAFE_RCU), task_is_descendant(), and
next_thread() read group_leader under RCU without holding tasklist_lock.
During multi-threaded execve(), de_thread() sets leader->group_leader = tsk
and then calls release_task(leader), where pin_leader is false, so this
plain assignment mutates p->group_leader concurrently with RCU readers,
triggering KCSAN warnings and permitting store tearing. On every normal
leader exit, leader == p also leaves pin_leader false and performs a
redundant plain write even though p->group_leader is already p.
Guarding the assignment with if (!pin_leader && leader != p) and using
WRITE_ONCE(p->group_leader, p) would eliminate both the data race and the
redundant store.
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 3734aaa717130..fbad2637d89d0 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -793,6 +793,9 @@ void __put_task_struct(struct task_struct *tsk)
> exit_creds(tsk);
> delayacct_tsk_free(tsk);
> put_signal_struct(tsk->signal);
> + /* release_task() has pinned the leader of a thread */
> + if (tsk->group_leader != tsk)
> + put_task_struct(tsk->group_leader);
> sched_core_free(tsk);
> free_task(tsk);
> }
[Severity: Medium]
Can pinning the leader until __put_task_struct() form a reference-count
cycle with BPF_MAP_TYPE_TASK_STORAGE?
BPF task local storage allows BPF_KPTR_REF fields, so a BPF program can
store a bpf_task_acquire() reference to a sub-thread T in its thread group
leader L's task storage. That kptr keeps T->rcu_users >= 1 until
free_task(L) calls bpf_task_storage_free(L). With this change, once both T
and L exit, T pins L->usage until __put_task_struct(T) runs, while L's task
storage prevents __put_task_struct(T) from ever running, leaking both
task_structs and their shared signal_struct until the BPF map is destroyed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203200612.26376-1-mason@kernel.org?part=2
next prev parent reply other threads:[~2026-12-03 20:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
2026-12-03 20:31 ` sashiko-bot
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
2026-12-03 20:31 ` sashiko-bot [this message]
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-03 20:42 ` 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=sashiko-outbox-248636@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=mason@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=sched-ext@lists.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