[PATCH 1/2] cgroup: list a process whose leader has exited where its threads are
From: Zhan Xusheng
Date: Mon Nov 23 2026 - 22:36:05 EST
From: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
A cgroup.procs migration skips exiting tasks. If the thread group
leader of a process has exited, writing the ID of one of its threads to
cgroup.procs moves the live threads and leaves the zombie leader behind.
CSS_TASK_ITER_PROCS walks only visit leaders, so the process stays in
the old cgroup's cgroup.procs and is missing from the new one's.
With a process whose main thread has called pthread_exit() and whose
other threads allocate memory:
# echo $TID > dst/cgroup.procs
# cat src/cgroup.procs
$PID
# cat dst/cgroup.procs
#
When dst reaches memory.max, the memcg OOM killer finds no process in
dst and kills nothing: 5453 oom events and no oom_kill in 10 seconds.
An OOM in src picks the moved process instead.
Keep the leader on its css_set, and make PROCS walks visit it at the
first live thread of the process when that thread is on another
css_set. Update test_cgkill_exited_leader_migrated, which expected
dst's cgroup.procs to be empty.
Suggested-by: Tejun Heo <tj@xxxxxxxxxx>
Link: https://lore.kernel.org/all/305c5803b904a57c188b505d6ed9e1e4@xxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
include/linux/sched.h | 2 +
kernel/cgroup/cgroup.c | 55 +++++++++++++++++++++-
tools/testing/selftests/cgroup/test_kill.c | 16 +++++--
3 files changed, 66 insertions(+), 7 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 8b3d47a325cc..04fe82aea0e1 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1352,6 +1352,8 @@ struct task_struct {
struct css_set __rcu *cgroups;
/* cg_list protected by css_set_lock and tsk->alloc_lock: */
struct list_head cg_list;
+ /* exiting leader left behind by a cgroup.procs migration */
+ bool cg_leader_left;
#ifdef CONFIG_PREEMPT_RT
struct llist_node cg_dead_lnode;
#endif /* CONFIG_PREEMPT_RT */
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e72db99..ab442f94e29d 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3081,6 +3081,9 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,
if (!threadgroup)
break;
} while_each_thread(leader, task);
+ /* see procs_live_thread() */
+ if (threadgroup && (leader->flags & PF_EXITING))
+ WRITE_ONCE(leader->cg_leader_left, true);
spin_unlock_irq(&css_set_lock);
return cgroup_migrate_execute(mgctx);
@@ -5200,6 +5203,40 @@ static void css_task_iter_skip(struct css_task_iter *it,
}
}
+/*
+ * A cgroup.procs migration skips a leader that is already exiting, as the
+ * controllers don't deal with exiting tasks, so the leader stays on its
+ * css_set while the rest of its process moves. CSS_TASK_ITER_PROCS walks
+ * visit such a leader at the first live thread of the process instead, if
+ * that thread is on a different css_set.
+ *
+ * PF_EXITING isn't set under css_set_lock, so a walk that races with a
+ * thread starting to exit may visit the process twice or miss it.
+ */
+static struct task_struct *procs_live_thread(struct task_struct *leader)
+{
+ struct task_struct *t;
+
+ if (!READ_ONCE(leader->cg_leader_left))
+ return leader;
+
+ for_each_thread(leader, t) {
+ if (!(t->flags & PF_EXITING)) {
+ if (task_css_set(t) == task_css_set(leader))
+ return leader;
+ return t;
+ }
+ }
+ return leader;
+}
+
+static bool procs_stand_in(struct task_struct *task)
+{
+ struct task_struct *leader = task->group_leader;
+
+ return leader != task && procs_live_thread(leader) == task;
+}
+
static void css_task_iter_advance(struct css_task_iter *it)
{
struct task_struct *task;
@@ -5246,8 +5283,18 @@ static void css_task_iter_advance(struct css_task_iter *it)
goto repeat;
if (it->flags & CSS_TASK_ITER_PROCS) {
- /* if PROCS, skip over tasks which aren't group leaders */
- if (!thread_group_leader(task))
+ /*
+ * If PROCS, skip over tasks which aren't group leaders, except
+ * for the thread a left-behind leader is visited at.
+ */
+ if (!thread_group_leader(task)) {
+ if (!procs_stand_in(task))
+ goto repeat;
+ return;
+ }
+
+ /* and leaders visited at another thread */
+ if (procs_live_thread(task) != task)
goto repeat;
/* and dying leaders w/o live member threads */
@@ -5322,6 +5369,9 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
while (it->task_pos && !it->cur_task) {
it->cur_task = list_entry(it->task_pos, struct task_struct,
cg_list);
+ /* a left-behind leader is visited at a live thread */
+ if (it->flags & CSS_TASK_ITER_PROCS)
+ it->cur_task = it->cur_task->group_leader;
/* a task on dying_tasks with zero refcount is only valid for
* RCU readers, not even interesting for
* CSS_TASK_ITER_WITH_DEAD, find another one */
@@ -6841,6 +6891,7 @@ void cgroup_fork(struct task_struct *child)
{
RCU_INIT_POINTER(child->cgroups, &init_css_set);
INIT_LIST_HEAD(&child->cg_list);
+ child->cg_leader_left = false;
}
/**
diff --git a/tools/testing/selftests/cgroup/test_kill.c b/tools/testing/selftests/cgroup/test_kill.c
index 756bc7193d4b..102255b9559a 100644
--- a/tools/testing/selftests/cgroup/test_kill.c
+++ b/tools/testing/selftests/cgroup/test_kill.c
@@ -406,15 +406,16 @@ static int test_cgkill_exited_leader(const char *root)
}
/*
- * Test that cgroup.kill kills the threads of a process that were migrated
- * to the cgroup after their thread group leader had exited, and so are
- * not listed in its cgroup.procs.
+ * Test that a process whose thread group leader has exited is listed in the
+ * cgroup.procs of the cgroup its threads were migrated to, and that
+ * cgroup.kill there kills it.
*/
static int test_cgkill_exited_leader_migrated(const char *root)
{
char *parent = NULL, *src = NULL, *dst = NULL;
int ret = KSFT_FAIL;
pid_t pid = -ESRCH;
+ char buf[32];
parent = cg_name(root, "cg_test_exited_leader_migrated");
if (!parent)
@@ -438,12 +439,17 @@ static int test_cgkill_exited_leader_migrated(const char *root)
if (wait_for_zombie_leader(pid))
goto cleanup;
- /* The exiting leader stays behind, only the live thread is moved. */
+ /*
+ * Only the live thread is moved, but the process is listed where the
+ * thread is.
+ */
if (cg_enter(dst, pid))
goto cleanup;
+ snprintf(buf, sizeof(buf), "%d\n", pid);
if (cg_read_strcmp(dst, "cgroup.events", "populated 1\n") ||
- cg_read_strcmp(dst, "cgroup.procs", ""))
+ cg_read_strcmp(dst, "cgroup.procs", buf) ||
+ cg_read_strcmp(src, "cgroup.procs", ""))
goto cleanup;
if (cg_write(dst, "cgroup.kill", "1"))
--
2.43.0