Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
@ 2026-11-21  7:32 Zhan Xusheng
  2026-11-22  7:32 ` sashiko-bot
  2026-11-23 10:11 ` Michal Hocko
  0 siblings, 2 replies; 8+ messages in thread
From: Zhan Xusheng @ 2026-11-21  7:32 UTC (permalink / raw)
  To: akpm
  Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song,
	penguin-kernel, tj, mkoutny, chris, cui.tao, cgroups, linux-mm,
	linux-kernel, stable, zhanxusheng

From: Zhan Xusheng <zhanxusheng@xiaomi.com>

mem_cgroup_scan_tasks() walks with CSS_TASK_ITER_PROCS, so it sees only
thread group leaders, plus a dying leader that still has live threads.
Moving a process whose leader has exited, by writing its PID to
cgroup.procs, moves the live threads but leaves the leader behind, as
cgroup_migrate_add_task() skips exiting tasks.

The destination's OOM killer then cannot see the process.  If it is
alone there, every attempt ends with "Out of memory and no killable
processes..." and the charge is retried indefinitely, and
memory.oom.group kills nothing; otherwise another process is killed in
its place.  The source's OOM killer still finds the leader, reaches the
moved threads' mm through find_lock_task_mm(), and kills a process that
has left the memcg.

Walk all tasks and let one task of each thread group in the memcg stand
for it: the first one that is not exiting, the first one that has not
exited yet once the whole group is exiting, or the leader once every
thread has exited, so that an OOM victim stays visible until it is gone,
as the leader does today.  Exiting threads left behind do not count
while the group has live threads elsewhere.

Tested in a VM with memory.max=64M: in each case above the process using
the memory is now killed, and a process whose exited leader was not
moved, or whose leader is alive, is killed as before, with and without
memory.oom.group.

Fixes: f168a9a54ec3 ("mm: memcontrol: use CSS_TASK_ITER_PROCS at mem_cgroup_scan_tasks()")
Cc: stable@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
v3:
 - Rebase on "mm: memcontrol: don't lose track of exiting OOM victims"
   in mm-hotfixes-unstable and keep CSS_TASK_ITER_WITH_DEAD
 - Let the leader stand for the group once every thread has exited, so
   such a victim stays visible as it does with that patch alone

Moving the exited leader along with its threads came up on v1, and
there has been no answer from the cgroup side.  cgroup.kill had the
same case: da13e0e72a73 ("cgroup: fix cgroup.kill for exited thread
group leader") walks all threads instead of the leaders, and
5f4a3f743acc ("cgroup: don't kill migrated processes through their
exiting threads") then had to keep an exiting thread left behind from
standing for a process that has moved out.  This patch does the same
for the memcg scan; the second case is what the live_elsewhere check
covers.

Retested on mm-hotfixes-unstable 2ba4d2c9ae3c: the cases in the
changelog behave as described, and with the reproducer from the review
of Chris's patch, memory.max=400M, 10 runs, oom_kill is 1 in every run
with and without this patch.

v2: https://lore.kernel.org/all/20261027144016.2582880-1-zhanxusheng1024@gmail.com/
v1: https://lore.kernel.org/all/20261026120248.2188024-1-zhanxusheng1024@gmail.com/

 mm/memcontrol.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index afbb638e7e1c..3355341666db 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1426,6 +1426,44 @@ static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
 						dead_memcg);
 }
 
+/*
+ * Let one task of each thread group in @memcg stand for the group: the
+ * first one that is not exiting, or, once the whole group is exiting, the
+ * first one that has not exited yet, so that an OOM victim stays visible.
+ * The leader is not enough: a group whose leader has exited can be moved
+ * without it.  Exiting threads left behind in @memcg do not count while the
+ * group has live threads elsewhere.
+ */
+static bool mem_cgroup_scan_skip(struct mem_cgroup *memcg,
+				 struct task_struct *task)
+{
+	struct task_struct *t, *dying = NULL;
+	bool live_elsewhere = false;
+
+	guard(rcu)();
+	__for_each_thread(task->signal, t) {
+		bool here;
+
+		if (READ_ONCE(t->exit_state))
+			continue;
+		here = mem_cgroup_from_task(t) == memcg;
+		if (!(READ_ONCE(t->flags) & PF_EXITING)) {
+			if (here)
+				return t != task;
+			live_elsewhere = true;
+		} else if (here && !dying) {
+			dying = t;
+		}
+	}
+	/*
+	 * Once every thread has exited, the leader stands for the group until
+	 * it is gone, as with CSS_TASK_ITER_PROCS.
+	 */
+	if (!dying)
+		return live_elsewhere || !thread_group_leader(task);
+	return live_elsewhere || dying != task;
+}
+
 /**
  * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
  * @memcg: hierarchy root
@@ -1456,10 +1494,10 @@ void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
 		 * torn down. Keep it visible so oom_evaluate_task() can
 		 * back off while there's still memory to free.
 		 */
-		css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS |
-				    CSS_TASK_ITER_WITH_DEAD, &it);
+		css_task_iter_start(&iter->css, CSS_TASK_ITER_WITH_DEAD, &it);
 		while (!ret && (task = css_task_iter_next(&it))) {
-			ret = fn(task, arg);
+			if (!mem_cgroup_scan_skip(iter, task))
+				ret = fn(task, arg);
 			/* Avoid potential softlockup warning */
 			cond_resched();
 		}

base-commit: 2ba4d2c9ae3cfdad96ee925c92b14d7acd886954
-- 
2.43.0



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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-21  7:32 [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans Zhan Xusheng
@ 2026-11-22  7:32 ` sashiko-bot
  2026-11-23  9:33   ` Zhan Xusheng
  2026-11-23 10:11 ` Michal Hocko
  1 sibling, 1 reply; 8+ messages in thread
From: sashiko-bot @ 2026-11-22  7:32 UTC (permalink / raw)
  To: Zhan Xusheng, akpm
  Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song,
	penguin-kernel, tj, mkoutny, chris, cui.tao, linux-mm,
	zhanxusheng

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: memcontrol: mem_cgroup_scan_skip() exposes left-behind
  exiting threads
--

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

mm: memcontrol: don't miss processes whose leader has exited in OOM scans

This commit replaces CSS_TASK_ITER_PROCS in mem_cgroup_scan_tasks() with
a scan over all tasks filtered by mem_cgroup_scan_skip(), so that
processes migrated after their thread group leader has exited remain
visible to memcg OOM scans.

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index afbb638e7e1cb..3355341666db5 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1426,6 +1426,44 @@ static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
>  						dead_memcg);
>  }
>  
> +/*
> + * Let one task of each thread group in @memcg stand for the group: the
> + * first one that is not exiting, or, once the whole group is exiting, the
> + * first one that has not exited yet, so that an OOM victim stays visible.
> + * The leader is not enough: a group whose leader has exited can be moved
> + * without it.  Exiting threads left behind in @memcg do not count while the
> + * group has live threads elsewhere.
> + */
> +static bool mem_cgroup_scan_skip(struct mem_cgroup *memcg,
> +				 struct task_struct *task)
> +{
> +	struct task_struct *t, *dying = NULL;
> +	bool live_elsewhere = false;
> +
> +	guard(rcu)();
> +	__for_each_thread(task->signal, t) {
> +		bool here;
> +
> +		if (READ_ONCE(t->exit_state))
> +			continue;
> +		here = mem_cgroup_from_task(t) == memcg;
> +		if (!(READ_ONCE(t->flags) & PF_EXITING)) {
> +			if (here)
> +				return t != task;
> +			live_elsewhere = true;
> +		} else if (here && !dying) {
> +			dying = t;
> +		}
> +	}
> +	/*
> +	 * Once every thread has exited, the leader stands for the group until
> +	 * it is gone, as with CSS_TASK_ITER_PROCS.
> +	 */
> +	if (!dying)
> +		return live_elsewhere || !thread_group_leader(task);
> +	return live_elsewhere || dying != task;
> +}

[Severity: Medium]
Can mem_cgroup_scan_skip() expose an exiting thread left behind in the
source memcg once the migrated threads in the destination memcg begin
exiting?

When a multi-threaded process is migrated while one of its threads already
has PF_EXITING set, cgroup_migrate_add_task() skips the exiting thread and
leaves it on the source cset->tasks until do_cgroup_task_dead().  With
CSS_TASK_ITER_WITH_DEAD, css_task_iter_next() continues returning that
left-behind thread even after the last migrated thread enters do_exit().

Once the migrated threads in the destination memcg set PF_EXITING (for
example, after being OOM-killed there or calling exit_group()), they have
here == false and PF_EXITING set, so the loop above ignores them without
setting live_elsewhere.  If the left-behind thread in the source memcg
still has exit_state == 0, dying is set to it and mem_cgroup_scan_skip()
returns false; if the left-behind thread is the group leader with
exit_state != 0 (or a leader concurrently migrated out of @memcg after
css_task_iter_next() returned it), dying is NULL and the !dying fallback
also returns false without checking whether unexited threads exist
elsewhere or whether @task is still in @memcg.

An OOM scan in the source memcg then visits the migrated process through
the left-behind thread: if the process is an OOM victim in the destination
memcg, tsk_is_oom_victim() causes the source memcg's OOM scan to back off
without killing anything in the source memcg, and if a migrated thread has
not yet reached exit_mm(), find_lock_task_mm() on the left-behind thread
can still find the migrated process's mm and select it as the source
memcg's OOM victim.

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


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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-22  7:32 ` sashiko-bot
@ 2026-11-23  9:33   ` Zhan Xusheng
  0 siblings, 0 replies; 8+ messages in thread
From: Zhan Xusheng @ 2026-11-23  9:33 UTC (permalink / raw)
  To: akpm
  Cc: hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song,
	penguin-kernel, tj, mkoutny, chris, cui.tao, linux-mm,
	zhanxusheng

On Sun, Nov 22, 2026 at 07:32:46AM +0000, sashiko-bot@kernel.org wrote:
> Can mem_cgroup_scan_skip() expose an exiting thread left behind in the
> source memcg once the migrated threads in the destination memcg begin
> exiting?

Yes, but only once every thread that moved is exiting, so the process
is going away.  The source scan then either backs off on an OOM victim
until MMF_OOM_SKIP, or selects the exiting process and sends SIGKILL to
a process that is dying anyway.  Neither kills a live process, and the
charge is retried once the process is gone.  Without this patch the
source scan sees the left-behind leader all the time, not only while
the process exits.

Counting exiting threads in other memcgs too would close it, but would
also hide an exiting group that cgroup v1 split across memcgs from all
of them, so a concurrent OOM there could kill another process instead
of waiting for it.  I'd rather keep v3 as it is.

Thanks,
Zhan Xusheng


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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-21  7:32 [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans Zhan Xusheng
  2026-11-22  7:32 ` sashiko-bot
@ 2026-11-23 10:11 ` Michal Hocko
  2026-11-23 11:13   ` Tejun Heo
  1 sibling, 1 reply; 8+ messages in thread
From: Michal Hocko @ 2026-11-23 10:11 UTC (permalink / raw)
  To: Zhan Xusheng, Tejun Heo
  Cc: akpm, hannes, roman.gushchin, shakeel.butt, muchun.song,
	penguin-kernel, tj, mkoutny, chris, cui.tao, cgroups, linux-mm,
	linux-kernel, stable, zhanxusheng

On Sat 21-11-26 15:32:22, Zhan Xusheng wrote:
> Moving the exited leader along with its threads came up on v1, and
> there has been no answer from the cgroup side.  cgroup.kill had the
> same case: da13e0e72a73 ("cgroup: fix cgroup.kill for exited thread
> group leader") walks all threads instead of the leaders, and
> 5f4a3f743acc ("cgroup: don't kill migrated processes through their
> exiting threads") then had to keep an exiting thread left behind from
> standing for a process that has moved out.  This patch does the same
> for the memcg scan; the second case is what the live_elsewhere check
> covers.

Tejun, is there any strong reason to keep the exiting thread group
leader in the original cgroup? This makes CSS_TASK_ITER_PROCS iteration
really tricky and error prone as above examples show.

-- 
Michal Hocko
SUSE Labs


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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-23 10:11 ` Michal Hocko
@ 2026-11-23 11:13   ` Tejun Heo
  2026-11-23 11:39     ` Zhan Xusheng
  2026-11-23 11:49     ` Michal Hocko
  0 siblings, 2 replies; 8+ messages in thread
From: Tejun Heo @ 2026-11-23 11:13 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Zhan Xusheng, akpm, hannes, roman.gushchin, shakeel.butt,
	muchun.song, penguin-kernel, mkoutny, chris, cui.tao, cgroups,
	linux-mm, linux-kernel, stable, zhanxusheng

Hello, Michal.

On Mon, Nov 23, 2026 at 11:11:27AM +0100, Michal Hocko wrote:
> Tejun, is there any strong reason to keep the exiting thread group
> leader in the original cgroup? This makes CSS_TASK_ITER_PROCS iteration
> really tricky and error prone as above examples show.

No, and I agree that the leader should follow the rest of the process on
cgroup.procs migrations. It's this way because controllers primarily care
about resources and exiting and zombie tasks don't matter much in that
regard, so migration skips them and controllers don't have to deal with
them. I don't want to change that. It doesn't make much sense to teach
all controllers about them.

That rules out the full migration path for the leader, and moving it
without one looks fragile. If cgroup core switches its css_set without
involving the controllers, ->release() sees a different css from the one
the task exited in, which breaks e.g. pids uncharging, and that still
doesn't work for a leader which is exiting but not dead yet as it's still
running.

Maybe the leader can pretend to be in the new cgroup instead. On
cgroup.procs migrations, record the destination css_set in an exiting or
dead leader without touching the css_set it's actually on. When a
CSS_TASK_ITER_PROCS iteration finds the first live thread of a process
whose leader is exiting or dead, it looks at the leader and visits it
there if the recorded css_set agrees.

That leaves the leader's actual css_set and everything that the
controllers see as they are now. Only the iteration changes, and exiting
and dead leaders are handled the same way.

Thanks.

--
tejun


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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-23 11:13   ` Tejun Heo
@ 2026-11-23 11:39     ` Zhan Xusheng
  2026-11-23 11:49       ` Michal Hocko
  2026-11-23 11:49     ` Michal Hocko
  1 sibling, 1 reply; 8+ messages in thread
From: Zhan Xusheng @ 2026-11-23 11:39 UTC (permalink / raw)
  To: tj, mhocko
  Cc: akpm, hannes, roman.gushchin, shakeel.butt, muchun.song,
	penguin-kernel, mkoutny, chris, cui.tao, cgroups, linux-mm,
	linux-kernel, zhanxusheng

Hi Tejun,

On Mon, Nov 23, 2026 at 01:13:25AM -1000, Tejun Heo wrote:
> Maybe the leader can pretend to be in the new cgroup instead.

Thanks, I'll implement that and send it to cgroups.  The memcg scan can
then stay on CSS_TASK_ITER_PROCS, so this patch is not needed.

Thanks,
Zhan Xusheng


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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-23 11:13   ` Tejun Heo
  2026-11-23 11:39     ` Zhan Xusheng
@ 2026-11-23 11:49     ` Michal Hocko
  1 sibling, 0 replies; 8+ messages in thread
From: Michal Hocko @ 2026-11-23 11:49 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Zhan Xusheng, akpm, hannes, roman.gushchin, shakeel.butt,
	muchun.song, penguin-kernel, mkoutny, chris, cui.tao, cgroups,
	linux-mm, linux-kernel, stable, zhanxusheng

On Mon 23-11-26 01:13:25, Tejun Heo wrote:
> Hello, Michal.
> 
> On Mon, Nov 23, 2026 at 11:11:27AM +0100, Michal Hocko wrote:
> > Tejun, is there any strong reason to keep the exiting thread group
> > leader in the original cgroup? This makes CSS_TASK_ITER_PROCS iteration
> > really tricky and error prone as above examples show.
> 
> No, and I agree that the leader should follow the rest of the process on
> cgroup.procs migrations. It's this way because controllers primarily care
> about resources and exiting and zombie tasks don't matter much in that
> regard, so migration skips them and controllers don't have to deal with
> them. I don't want to change that. It doesn't make much sense to teach
> all controllers about them.

Agreed!

> That rules out the full migration path for the leader, and moving it
> without one looks fragile. If cgroup core switches its css_set without
> involving the controllers, ->release() sees a different css from the one
> the task exited in, which breaks e.g. pids uncharging, and that still
> doesn't work for a leader which is exiting but not dead yet as it's still
> running.

I see

> Maybe the leader can pretend to be in the new cgroup instead. On
> cgroup.procs migrations, record the destination css_set in an exiting or
> dead leader without touching the css_set it's actually on. When a
> CSS_TASK_ITER_PROCS iteration finds the first live thread of a process
> whose leader is exiting or dead, it looks at the leader and visits it
> there if the recorded css_set agrees.
> 
> That leaves the leader's actual css_set and everything that the
> controllers see as they are now. Only the iteration changes, and exiting
> and dead leaders are handled the same way.

Makes sense. Thanks a lot Tejun! This would allow to use
CSS_TASK_ITER_PROCS for the oom path.

Thanks!

-- 
Michal Hocko
SUSE Labs


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

* Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
  2026-11-23 11:39     ` Zhan Xusheng
@ 2026-11-23 11:49       ` Michal Hocko
  0 siblings, 0 replies; 8+ messages in thread
From: Michal Hocko @ 2026-11-23 11:49 UTC (permalink / raw)
  To: Zhan Xusheng
  Cc: tj, akpm, hannes, roman.gushchin, shakeel.butt, muchun.song,
	penguin-kernel, mkoutny, chris, cui.tao, cgroups, linux-mm,
	linux-kernel, zhanxusheng

On Mon 23-11-26 19:39:51, Zhan Xusheng wrote:
> Hi Tejun,
> 
> On Mon, Nov 23, 2026 at 01:13:25AM -1000, Tejun Heo wrote:
> > Maybe the leader can pretend to be in the new cgroup instead.
> 
> Thanks, I'll implement that and send it to cgroups.  The memcg scan can
> then stay on CSS_TASK_ITER_PROCS, so this patch is not needed.

Thanks!
-- 
Michal Hocko
SUSE Labs


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

end of thread, other threads:[~2026-11-23 11:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  7:32 [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans Zhan Xusheng
2026-11-22  7:32 ` sashiko-bot
2026-11-23  9:33   ` Zhan Xusheng
2026-11-23 10:11 ` Michal Hocko
2026-11-23 11:13   ` Tejun Heo
2026-11-23 11:39     ` Zhan Xusheng
2026-11-23 11:49       ` Michal Hocko
2026-11-23 11:49     ` Michal Hocko

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