From: sashiko-bot@kernel.org
To: "Zhan Xusheng" <zhanxusheng1024@gmail.com>, akpm@linux-foundation.org
Cc: hannes@cmpxchg.org, mhocko@suse.com, roman.gushchin@linux.dev,
shakeel.butt@linux.dev, muchun.song@linux.dev,
penguin-kernel@i-love.sakura.ne.jp, tj@kernel.org,
mkoutny@suse.com, chris@chrisdown.name, cui.tao@linux.dev,
linux-mm@kvack.org, zhanxusheng@xiaomi.com
Subject: Re: [PATCH v3] mm: memcontrol: don't miss processes whose leader has exited in OOM scans
Date: Sun, 22 Nov 2026 07:32:46 +0000 [thread overview]
Message-ID: <sashiko-outbox-227786@kernel.org> (raw)
In-Reply-To: <20261121073222.834282-1-zhanxusheng1024@gmail.com>
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
prev parent reply other threads:[~2026-11-22 7:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-227786@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=chris@chrisdown.name \
--cc=cui.tao@linux.dev \
--cc=hannes@cmpxchg.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=mkoutny@suse.com \
--cc=muchun.song@linux.dev \
--cc=penguin-kernel@i-love.sakura.ne.jp \
--cc=roman.gushchin@linux.dev \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=tj@kernel.org \
--cc=zhanxusheng1024@gmail.com \
--cc=zhanxusheng@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox