Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [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
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ 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] 6+ 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-25  3:36   ` sashiko-bot
  2026-11-24  3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
  2026-11-24 18:47 ` [PATCH 0/2] cgroup: list processes whose leader has exited where their " Tejun Heo
  2 siblings, 1 reply; 6+ 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] 6+ 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
  2026-11-25  3:36   ` sashiko-bot
  2026-11-24 18:47 ` [PATCH 0/2] cgroup: list processes whose leader has exited where their " Tejun Heo
  2 siblings, 1 reply; 6+ 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] 6+ messages in thread

* Re: [PATCH 0/2] cgroup: list processes whose leader has exited where their 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 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
@ 2026-11-24 18:47 ` Tejun Heo
  2 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2026-11-24 18:47 UTC (permalink / raw)
  To: Zhan Xusheng
  Cc: hannes, mkoutny, mhocko, roman.gushchin, shakeel.butt,
	muchun.song, akpm, kolyshkin, chris, cgroups, linux-mm,
	linux-kernel, zhanxusheng

Hello, Zhan.

On Tue, Nov 24, 2026 at 11:35:45AM +0800, Zhan Xusheng wrote:
> 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.

The main problem I see is that it's difficult to keep the walks intact
this way. The thread that a leader is visited at is picked by testing
PF_EXITING, which isn't synchronized with css_set_lock, so, as the comment
in patch 1 notes, a walk can skip a process or visit it more than once. A
walk shouldn't miss processes.

It also looks fragile in general. AFAICS, only cgroup_migrate() sets
cg_leader_left, so when a cgroup.subtree_control write moves the threads
to a new css_set, a walk of that controller's css, e.g. the memcg OOM
scan, still misses the process. And as a walk can now return a leader
whose css_set isn't the process's, every user has to know to use
task_procs_css().

I'm trying out a different approach.

Thanks.

--
tejun


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] cgroup: list a process whose leader has exited where its threads are
  2026-11-24  3:35 ` [PATCH 1/2] cgroup: list a process whose leader has exited where its " Zhan Xusheng
@ 2026-11-25  3:36   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-25  3:36 UTC (permalink / raw)
  To: Zhan Xusheng, tj
  Cc: hannes, mkoutny, mhocko, roman.gushchin, shakeel.butt,
	muchun.song, akpm, kolyshkin, chris, linux-mm, zhanxusheng

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] cgroup: use-after-free on group_leader in css_task_iter_next()
  and procs_stand_in()
- [Medium] cgroup: missing cg_leader_left update in
  cgroup_update_dfl_csses()
- [Medium] cgroup: __cgroup_procs_write() checks permissions against
  left-behind leader cgroup
- [Medium] cgroup: quadratic thread traversal under css_set_lock in
  procs_stand_in()
- [Low] cgroup: procs_live_thread() fallback to leader resurrects
  process in old cgroup
--

commit e947916a597995307d4cc64d14f765928b1b3027
Author: Zhan Xusheng <zhanxusheng@xiaomi.com>

cgroup: list a process whose leader has exited where its threads are

This commit visits a thread group leader that was left behind during a
cgroup.procs migration at the first live thread of the process during
CSS_TASK_ITER_PROCS walks.

Link: https://lore.kernel.org/all/305c5803b904a57c188b505d6ed9e1e4@kernel.org/

> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index b3657e72db99e..ab442f94e29d2 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);

[Severity: Medium]
Does setting cg_leader_left only in cgroup_migrate() miss migrations driven
by cgroup_update_dfl_csses() and cgroup_transfer_tasks()?

When a controller such as memory is enabled or disabled via
cgroup.subtree_control, cgroup_update_dfl_csses() calls
cgroup_migrate_add_task() and cgroup_migrate_execute() directly without
going through cgroup_migrate().  If a process in a descendant cgroup
already has an exited leader, the live threads move to the new css_set
while the leader stays on the old css_set with cg_leader_left still false,
so controller-scoped CSS_TASK_ITER_PROCS walks such as
mem_cgroup_scan_tasks() will still miss the process.

Should __cgroup_procs_write() also resolve src_cgrp from the live thread's
css_set rather than task_cgroup_from_root(task, &cgrp_dfl_root)?  For
threadgroup migrations, task is the group leader, so after one migration
leaves the exited leader behind, a second cgroup.procs migration checks
cgroup_attach_permissions() against the stale cgroup where the leader
exited rather than the cgroup currently holding the live threads.

> @@ -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) {\n> +		if (!(t->flags & PF_EXITING)) {
> +			if (task_css_set(t) == task_css_set(leader))
> +				return leader;
> +			return t;
> +		}
> +	}
> +	return leader;
> +}

[Severity: Low]
When the last live thread of a migrated process enters do_exit(), can
falling back to returning leader at the end of procs_live_thread() make the
process transiently reappear in its old cgroup?

In exit_signals(), a dying thread sets PF_EXITING before decrementing
signal->live.  If a CSS_TASK_ITER_PROCS walk on the old cgroup visits the
left-behind leader on dying_tasks during that window, every thread already
has PF_EXITING set while signal->live is still non-zero, so
procs_live_thread(leader) returns leader and css_task_iter_advance() does
not skip it.

> +
> +static bool procs_stand_in(struct task_struct *task)
> +{
> +	struct task_struct *leader = task->group_leader;
> +
> +	return leader != task && procs_live_thread(leader) == task;
> +}

[Severity: Medium]
Can calling procs_stand_in() for every non-leader thread cause quadratic
thread-list traversal under css_set_lock?

When an N-thread process with cg_leader_left set is exiting, threads that
have set PF_EXITING in exit_signals() remain on cset->tasks and
signal->thread_head until they pass exit_notify() and do_cgroup_task_dead().
Because neither css_task_iter_advance() nor procs_stand_in() filters out
tasks with PF_EXITING or tasks on the same css_set as their leader before
calling procs_live_thread(leader), a single css_task_iter_advance() call
under spin_lock_irqsave(&css_set_lock) walks all N threads on cset->tasks
and rescans across the exiting threads at the head of signal->thread_head
each time.  In addition, cgroup_migrate() sets cg_leader_left even when no
tasks move or cgroup_migrate_execute() fails, enabling this scan when all
threads are still on the leader's css_set.

> +
>  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 */

[Severity: High]
Can css_task_iter_next() and procs_stand_in() dereference a freed
group_leader after the stand-in thread exits?

A non-leader thread T does not hold a reference on its group_leader L, and
T->cg_list remains linked on dst_cset->tasks until the end of do_exit() in
do_task_dead() -> cgroup_task_dead() (or the deferred irq_work on
CONFIG_PREEMPT_RT).  Once T passes release_task(T) -> __exit_signal(T), L
can be reaped and RCU-freed while T is still on dst_cset->tasks, while
cgroup_task_free(L) only skips iterators on L's own src_cset:

CPU0                                  CPU1
css_task_iter_start()
  css_task_iter_advance()
    /* parks it->task_pos at T */
  spin_unlock_irqrestore(&css_set_lock)
                                      do_exit() /* last live thread T */
                                        exit_notify()
                                          release_task(T)
                                          /* zap_leader or parent wait */
                                          release_task(L)
                                            put_task_struct_rcu_user(L)
                                        /* RCU grace period elapses */
                                        delayed_put_task_struct(L)
                                          free_task(L)
css_task_iter_next()
  spin_lock_irqsave(&css_set_lock)
  it->cur_task = T->group_leader
  tryget_task_struct(L)  <-- use-after-free

The same stale T->group_leader is also dereferenced in
css_task_iter_advance() -> procs_stand_in(T) -> procs_live_thread(L) if a
sibling thread T2 calls execve() -> de_thread() after T passes
__exit_signal(T): de_thread() reaps and RCU-frees L without updating
T->group_leader (because T was already unlinked from signal->thread_head),
and signal->live remains non-zero because T2 is alive, so
css_task_iter_advance() does not skip T before calling procs_stand_in(T).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124033547.2373352-1-zhanxusheng1024@gmail.com?part=1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] mm: memcontrol: find the OOM group of a process where its threads are
  2026-11-24  3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
@ 2026-11-25  3:36   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-25  3:36 UTC (permalink / raw)
  To: Zhan Xusheng, tj
  Cc: hannes, mkoutny, mhocko, roman.gushchin, shakeel.butt,
	muchun.song, akpm, kolyshkin, chris, linux-mm, zhanxusheng

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: memcontrol: task_procs_css_set() returns stale css_set if
  threads exit
- [Medium] cgroup: missing cg_leader_left update in
  cgroup_update_dfl_csses()
- [Low] mm: memcontrol: use of task_cgroup() in
  mem_cgroup_print_oom_context()
--

commit 00e8b03872e16150479767210c746337c6018f3f
Author: Zhan Xusheng <zhanxusheng@xiaomi.com>

mm: memcontrol: find the OOM group of a process where its threads are

This commit adds task_procs_css_set() and task_procs_css() to resolve the
css of a process returned by a CSS_TASK_ITER_PROCS walk via
procs_live_thread(), and uses it in mem_cgroup_get_oom_group() so that a
migrated process with an exited thread group leader is matched against the
memcg where its live threads reside.

> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index ab442f94e29d2..0be7f96ce074d 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));
> +}

[Severity: Medium]
Can task_procs_css_set() return the exited leader's old css_set if the
remaining threads in the destination cgroup enter do_exit() before
mem_cgroup_get_oom_group() runs?

When the victim is an exited leader left behind by a cgroup.procs
migration, victim->mm is NULL, so task_will_free_mem(victim) in
oom_kill_process() returns false and does not short-circuit before
mem_cgroup_get_oom_group().  If the remaining threads in the destination
cgroup set PF_EXITING between select_bad_process() and
mem_cgroup_get_oom_group() (for example while dump_header() is printing),
procs_live_thread() skips every thread with PF_EXITING and falls back to
returning the leader on the old css_set:

kernel/cgroup/cgroup.c:procs_live_thread() {
	...
	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;
}

mem_cgroup_get_oom_group() will then miss memory.oom.group on the
destination cgroup, or kill unrelated processes in the old source cgroup
if the old cgroup is also under oom_domain and has memory.oom.group set.

[Severity: Medium]
Does task_procs_css_set() also return the exited leader's stale css_set
after cgroup_update_dfl_csses() moves live threads to a new css_set on a
cgroup.subtree_control write?

When a thread group leader exits while sibling threads remain alive,
do_cgroup_task_dead() moves the leader to src_cset->dying_tasks.  If a
controller such as memory is then enabled on an ancestor,
cgroup_update_dfl_csses() migrates only tasks on src_cset->tasks to the
new dst_cset, leaving the exited leader on src_cset->dying_tasks without
setting leader->cg_leader_left = true (which is only set in
cgroup_migrate()).  Because cg_leader_left is still false,
procs_live_thread() returns the leader on src_cset, so
task_procs_css() in mem_cgroup_get_oom_group() resolves to the ancestor's
memcg on src_cset instead of the child's new memcg on dst_cset, and
CSS_TASK_ITER_PROCS walks on the child's new CSS skip the process because
procs_stand_in() evaluates to false.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 1271d390b617e..5704e4415a16f 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;

[Severity: Low]
Should mem_cgroup_print_oom_context() also use task_procs_css() instead of
task_cgroup(p, memory_cgrp_id)?

oom_kill_process() passes the same victim task to dump_oom_victim() right
before calling mem_cgroup_get_oom_group(), so task_memcg= in the OOM log
will still print the old source cgroup where the exited leader was left
behind.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124033547.2373352-1-zhanxusheng1024@gmail.com?part=2


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-25  3:36 UTC | newest]

Thread overview: 6+ 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-25  3:36   ` sashiko-bot
2026-11-24  3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
2026-11-25  3:36   ` sashiko-bot
2026-11-24 18:47 ` [PATCH 0/2] cgroup: list processes whose leader has exited where their " Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox