[PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets

From: Chris Mason

Date: Sat Dec 05 2026 - 08:12:30 EST


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@xxxxxxxxxx>
---
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.
*/