* [PATCH 0/2] cgroup: list processes whose leader has exited where their threads are
@ 2026-11-24 3:35 Zhan Xusheng
2026-11-24 3:35 ` [PATCH 1/2] cgroup: list a process whose leader has exited where its " Zhan Xusheng
2026-11-24 3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
0 siblings, 2 replies; 3+ messages in thread
From: Zhan Xusheng @ 2026-11-24 3:35 UTC (permalink / raw)
To: tj
Cc: hannes, mkoutny, mhocko, roman.gushchin, shakeel.butt,
muchun.song, akpm, kolyshkin, chris, cgroups, linux-mm,
linux-kernel, zhanxusheng
From: Zhan Xusheng <zhanxusheng@xiaomi.com>
This implements Tejun's suggestion in [1]. Patch 1 makes PROCS walks
visit an exited leader left behind by a cgroup.procs migration at the
first live thread of its process. Patch 2 makes the memcg OOM group
lookup do the same.
Unlike [1], the leader does not record the destination css_set at
migration time. The walk looks up the css_set of the first live thread
instead, because a recorded css_set goes stale when the threads move
without a cgroup.procs write. With a version that recorded it, writing
"+pids" to the parent's cgroup.subtree_control moved the threads to a
new css_set, and dst's cgroup.procs was empty again.
/proc/$PID/cgroup still shows the leader's cgroup, so for such a
process it now differs from the cgroup whose cgroup.procs lists it.
/proc/$PID/task/$TID/cgroup of a live thread shows the new cgroup, as
before.
Tested in a 2-CPU QEMU guest with KASAN and lockdep on for-7.4-fixes
acec9637e03c. The process has an exited leader and two live threads,
and one thread ID is written to dst/cgroup.procs:
base 1/2 1/2+2/2
dst/cgroup.procs empty pid pid
OOM in dst no kill killed killed
OOM in src moved not killed not killed
process
killed
OOM in dst with oom.group and other moved both
another process in dst process process killed
killed killed
test_kill, 5 runs 5 failures pass pass
test_core, test_freezer, test_pids and test_memcontrol give the same
results as base. Patch 2 also applies to mm-hotfixes-unstable and
mm-new.
[1] https://lore.kernel.org/all/305c5803b904a57c188b505d6ed9e1e4@kernel.org/
Zhan Xusheng (2):
cgroup: list a process whose leader has exited where its threads are
mm: memcontrol: find the OOM group of a process where its threads are
include/linux/cgroup.h | 18 ++++++
include/linux/sched.h | 2 +
kernel/cgroup/cgroup.c | 66 +++++++++++++++++++++-
mm/memcontrol.c | 2 +-
tools/testing/selftests/cgroup/test_kill.c | 16 ++++--
5 files changed, 96 insertions(+), 8 deletions(-)
base-commit: acec9637e03ca54bd3c3a0c1a6ad5c6dfa07c0df
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] cgroup: list a process whose leader has exited where its threads are
2026-11-24 3:35 [PATCH 0/2] cgroup: list processes whose leader has exited where their threads are Zhan Xusheng
@ 2026-11-24 3:35 ` Zhan Xusheng
2026-11-24 3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
1 sibling, 0 replies; 3+ messages in thread
From: Zhan Xusheng @ 2026-11-24 3:35 UTC (permalink / raw)
To: tj
Cc: hannes, mkoutny, mhocko, roman.gushchin, shakeel.butt,
muchun.song, akpm, kolyshkin, chris, cgroups, linux-mm,
linux-kernel, zhanxusheng, stable
From: Zhan Xusheng <zhanxusheng@xiaomi.com>
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@kernel.org>
Link: https://lore.kernel.org/all/305c5803b904a57c188b505d6ed9e1e4@kernel.org/
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] mm: memcontrol: find the OOM group of a process where its threads are
2026-11-24 3:35 [PATCH 0/2] cgroup: list processes whose leader has exited where their threads are Zhan Xusheng
2026-11-24 3:35 ` [PATCH 1/2] cgroup: list a process whose leader has exited where its " Zhan Xusheng
@ 2026-11-24 3:35 ` Zhan Xusheng
1 sibling, 0 replies; 3+ messages in thread
From: Zhan Xusheng @ 2026-11-24 3:35 UTC (permalink / raw)
To: tj
Cc: hannes, mkoutny, mhocko, roman.gushchin, shakeel.butt,
muchun.song, akpm, kolyshkin, chris, cgroups, linux-mm,
linux-kernel, zhanxusheng, stable
From: Zhan Xusheng <zhanxusheng@xiaomi.com>
When a cgroup.procs migration leaves an exited leader behind, a
CSS_TASK_ITER_PROCS walk visits the leader at the first live thread of
its process. A memcg OOM walk of the cgroup the threads were moved to
can then return that leader as the victim. mem_cgroup_get_oom_group()
takes the memcg from the leader, which is still the old one, so
memory.oom.group of the cgroup the process is in has no effect. With
memory.oom.group set in dst and a second process in dst, an OOM in dst
killed only the moved process.
Add task_procs_css(), which gives the css of the process a PROCS walk
returned, and use it to look up the memcg.
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
include/linux/cgroup.h | 18 ++++++++++++++++++
kernel/cgroup/cgroup.c | 11 +++++++++++
mm/memcontrol.c | 2 +-
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 5dfa915a630e..cb05808f2172 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -548,6 +548,24 @@ static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
return task_css_check(task, subsys_id, false);
}
+struct css_set *task_procs_css_set(struct task_struct *task);
+
+/**
+ * task_procs_css - css of the process a CSS_TASK_ITER_PROCS walk returned
+ * @task: the task returned by the walk
+ * @subsys_id: the subsystem ID of interest
+ *
+ * The walk can return an exiting thread group leader that a cgroup.procs
+ * migration left behind for where the rest of its process is. Use this
+ * instead of task_css() to find the css of the process. Must be called
+ * under rcu_read_lock().
+ */
+static inline struct cgroup_subsys_state *task_procs_css(struct task_struct *task,
+ int subsys_id)
+{
+ return task_procs_css_set(task)->subsys[subsys_id];
+}
+
/**
* task_get_css - find and get the css for (task, subsys)
* @task: the target task
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index ab442f94e29d..0be7f96ce074 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -5237,6 +5237,17 @@ static bool procs_stand_in(struct task_struct *task)
return leader != task && procs_live_thread(leader) == task;
}
+/**
+ * task_procs_css_set - css_set of the process a PROCS walk returned
+ * @task: the task returned by the walk
+ *
+ * See task_procs_css().
+ */
+struct css_set *task_procs_css_set(struct task_struct *task)
+{
+ return task_css_set(procs_live_thread(task));
+}
+
static void css_task_iter_advance(struct css_task_iter *it)
{
struct task_struct *task;
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 1271d390b617..5704e4415a16 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2003,7 +2003,7 @@ struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
rcu_read_lock();
- memcg = mem_cgroup_from_task(victim);
+ memcg = mem_cgroup_from_css(task_procs_css(victim, memory_cgrp_id));
if (mem_cgroup_is_root(memcg))
goto out;
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 3:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 3:35 [PATCH 0/2] cgroup: list processes whose leader has exited where their threads are Zhan Xusheng
2026-11-24 3:35 ` [PATCH 1/2] cgroup: list a process whose leader has exited where its " Zhan Xusheng
2026-11-24 3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox