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: 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 2/8] exec, cgroup, prlimit: prepare for group_leader resets
Date: Sat, 05 Dec 2026 08:12:00 -0500 [thread overview]
Message-ID: <20261204233428.14861-3-mason@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-1-mason@kernel.org>
This is a prep patch that allows us to reset group_leader in
release_task().
exec and cgroup migration walk the threads with next_thread(t), which
wraps from the last thread to t->group_leader. If t is released meanwhile,
that would be t itself, and the walk would never end. So use
for_each_thread() there.
do_prlimit() reads tsk->group_leader for task_lock(), and again for
task_unlock(). So let prlimit64() hold tasklist_lock unless tsk is current.
Signed-off-by: Chris Mason <mason@kernel.org>
---
fs/exec.c | 4 ++--
include/linux/sched/signal.h | 4 ++--
kernel/cgroup/cgroup.c | 24 ++++++++++++------------
kernel/sys.c | 4 ++--
4 files changed, 18 insertions(+), 18 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455c..b3b80a47fb657 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1621,10 +1621,10 @@ static void check_unsafe_exec(struct linux_binprm *bprm)
* from another sub-thread until de_thread() succeeds, this
* state is protected by cred_guard_mutex we hold.
*/
- n_fs = 1;
+ n_fs = 0;
read_seqlock_excl(&p->fs->seq);
rcu_read_lock();
- for_other_threads(p, t) {
+ for_each_thread(p, t) {
if (t->fs == p->fs)
n_fs++;
}
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index 0adb51ab15428..cf73f4dd6946a 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -663,8 +663,8 @@ extern void flush_itimer_signals(void);
extern bool current_is_single_threaded(void);
/*
- * Without tasklist/siglock it is only rcu-safe if g can't exit/exec,
- * otherwise next_thread(t) will never reach g after list_del_rcu(g).
+ * Needs tasklist/siglock, otherwise next_thread(t) may never reach g
+ * after t or g has been released. Use for_each_thread() under RCU.
*/
#define while_each_thread(g, t) \
while ((t = next_thread(t)) != g)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e72db99e..640af2d4551d1 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3075,12 +3075,12 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,
* spin_lock_irq() implies RCU critical section here.
*/
spin_lock_irq(&css_set_lock);
- task = leader;
- do {
- cgroup_migrate_add_task(task, mgctx);
- if (!threadgroup)
- break;
- } while_each_thread(leader, task);
+ if (threadgroup) {
+ for_each_thread(leader, task)
+ cgroup_migrate_add_task(task, mgctx);
+ } else {
+ cgroup_migrate_add_task(leader, mgctx);
+ }
spin_unlock_irq(&css_set_lock);
return cgroup_migrate_execute(mgctx);
@@ -3103,12 +3103,12 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
/* look up all src csets */
spin_lock_irq(&css_set_lock);
- task = leader;
- do {
- cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
- if (!threadgroup)
- break;
- } while_each_thread(leader, task);
+ if (threadgroup) {
+ for_each_thread(leader, task)
+ cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);
+ } else {
+ cgroup_migrate_add_src(task_css_set(leader), dst_cgrp, &mgctx);
+ }
spin_unlock_irq(&css_set_lock);
/* prepare dst csets and commit */
diff --git a/kernel/sys.c b/kernel/sys.c
index 755bfa5ccd92c..9cb8eac08b7d2 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1764,10 +1764,10 @@ SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
get_task_struct(tsk);
rcu_read_unlock();
- need_tasklist = !same_thread_group(tsk, current);
+ need_tasklist = tsk != current;
if (need_tasklist) {
/*
- * Ensure we can't race with group exit or de_thread(),
+ * Ensure we can't race with release_task() or de_thread(),
* so tsk->group_leader can't be freed or changed until
* read_unlock(tasklist_lock) below.
*/
next prev parent reply other threads:[~2026-12-05 13:12 UTC|newest]
Thread overview: 21+ 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 ` Chris Mason [this message]
2026-12-05 15:35 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Oleg Nesterov
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: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 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:40 ` sashiko-bot
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 15:29 ` Oleg Nesterov
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
2026-12-05 15:24 ` Oleg Nesterov
2026-12-05 15:59 ` [PATCH] change next_thread() to use list_next_entry_circular() Oleg Nesterov
2026-12-05 16:11 ` sashiko-bot
2026-12-05 16:19 ` Oleg Nesterov
2026-12-05 17:46 ` Chris Mason
2026-12-05 18:17 ` Oleg Nesterov
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-3-mason@kernel.org \
--to=mason@kernel.org \
--cc=akpm@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.