* Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims
2026-11-08 8:41 [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims Chris Down
@ 2026-11-09 15:20 ` Michal Hocko
2026-11-09 18:58 ` Shakeel Butt
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Michal Hocko @ 2026-11-09 15:20 UTC (permalink / raw)
To: Chris Down
Cc: Andrew Morton, Johannes Weiner, Roman Gushchin, Shakeel Butt,
Muchun Song, Tejun Heo, Sebastian Andrzej Siewior, Zhan Xusheng,
cgroups, linux-mm, linux-kernel, kernel-team
On Sun 08-11-26 00:41:37, Chris Down wrote:
> When oom_evaluate_task() comes across a victim we've already killed, it
> backs off until the victim's mm has MMF_OOM_SKIP. This gives the victim
> a chance to free some memory before we kill anything else. However,
> since commit a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup"),
> css_task_iter skips exiting tasks once signal->live reaches zero. That
> happens before exit_mm(), so a single-threaded victim can disappear
> from the memcg scan while it still has all its memory.
>
> This can kill another process when the first kill would have been
> enough to recover, and we've seen it happen in production. If the
> second victim is in a cgroup with memory.oom.group enabled, the extra
> kill can take down the whole workload.
>
> In tests I used a 64 MiB memcg with swap disabled. I started a process
> that faults in 44 MiB of anonymous memory, submits a 4 KiB direct AIO
> read to null_blk, and goes to sleep. I set null_blk to take 10 seconds
> to complete the read so the victim would have to wait for it in
> exit_aio(). Two seconds after starting the first process, I started a
> second one that tries to fault in 40 MiB.
>
> The 40 MiB allocation hits memory.max partway through and triggers an
> OOM kill. The OOM killer picks the first process, which has more
> resident memory. With the read still pending, the victim blocks in
> exit_aio() before it can tear down its address space.
>
> memory.events showed oom_kill 2 in three runs. In one of them the first
> process hadn't submitted the read yet when it was killed. The slow read
> just makes the window easier to hit.
>
> The cgroup iterator hides these tasks so that cgroup.procs doesn't
> show processes that waitpid() may already have reaped. That's fine for
> cgroup.procs, but the OOM scan needs to see a victim even after its last
> thread has started exiting. We already have CSS_TASK_ITER_WITH_DEAD
> for sched_ext, so use it for mem_cgroup_scan_tasks() as well.
>
> With this applied, OOM selection backs off while the first victim still
> has memory to free. The 40 MiB allocation completes and memory.events
> shows oom_kill 1 in three test runs.
>
> Fixes: a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Down <chris@chrisdown.name>
Acked-by: Michal Hocko <mhocko@suse.com>
Thanks!
> ---
> v2: Give more concrete details in changelog (akpm)
>
> mm/memcontrol.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 84f4a8fd4d16..cb7cfae9ce5a 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1451,7 +1451,13 @@ void mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
> struct css_task_iter it;
> struct task_struct *task;
>
> - css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it);
> + /*
> + * An OOM victim can start exiting well before its mm is
> + * 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);
> while (!ret && (task = css_task_iter_next(&it))) {
> ret = fn(task, arg);
> /* Avoid potential softlockup warning */
>
> base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims
2026-11-08 8:41 [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims Chris Down
2026-11-09 15:20 ` Michal Hocko
@ 2026-11-09 18:58 ` Shakeel Butt
2026-11-12 10:34 ` elenchist
2026-11-12 17:11 ` Johannes Weiner
3 siblings, 0 replies; 6+ messages in thread
From: Shakeel Butt @ 2026-11-09 18:58 UTC (permalink / raw)
To: Chris Down
Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Roman Gushchin,
Muchun Song, Tejun Heo, Sebastian Andrzej Siewior, Zhan Xusheng,
cgroups, linux-mm, linux-kernel, kernel-team
On Sun, Nov 08, 2026 at 12:41:37AM -0800, Chris Down wrote:
> When oom_evaluate_task() comes across a victim we've already killed, it
> backs off until the victim's mm has MMF_OOM_SKIP. This gives the victim
> a chance to free some memory before we kill anything else. However,
> since commit a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup"),
> css_task_iter skips exiting tasks once signal->live reaches zero. That
> happens before exit_mm(), so a single-threaded victim can disappear
> from the memcg scan while it still has all its memory.
>
> This can kill another process when the first kill would have been
> enough to recover, and we've seen it happen in production. If the
> second victim is in a cgroup with memory.oom.group enabled, the extra
> kill can take down the whole workload.
>
> In tests I used a 64 MiB memcg with swap disabled. I started a process
> that faults in 44 MiB of anonymous memory, submits a 4 KiB direct AIO
> read to null_blk, and goes to sleep. I set null_blk to take 10 seconds
> to complete the read so the victim would have to wait for it in
> exit_aio(). Two seconds after starting the first process, I started a
> second one that tries to fault in 40 MiB.
>
> The 40 MiB allocation hits memory.max partway through and triggers an
> OOM kill. The OOM killer picks the first process, which has more
> resident memory. With the read still pending, the victim blocks in
> exit_aio() before it can tear down its address space.
>
> memory.events showed oom_kill 2 in three runs. In one of them the first
> process hadn't submitted the read yet when it was killed. The slow read
> just makes the window easier to hit.
>
> The cgroup iterator hides these tasks so that cgroup.procs doesn't
> show processes that waitpid() may already have reaped. That's fine for
> cgroup.procs, but the OOM scan needs to see a victim even after its last
> thread has started exiting. We already have CSS_TASK_ITER_WITH_DEAD
> for sched_ext, so use it for mem_cgroup_scan_tasks() as well.
>
> With this applied, OOM selection backs off while the first victim still
> has memory to free. The 40 MiB allocation completes and memory.events
> shows oom_kill 1 in three test runs.
>
> Fixes: a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Down <chris@chrisdown.name>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims
2026-11-08 8:41 [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims Chris Down
2026-11-09 15:20 ` Michal Hocko
2026-11-09 18:58 ` Shakeel Butt
@ 2026-11-12 10:34 ` elenchist
2026-11-12 10:45 ` elenchist
2026-11-12 17:11 ` Johannes Weiner
3 siblings, 1 reply; 6+ messages in thread
From: elenchist @ 2026-11-12 10:34 UTC (permalink / raw)
To: Chris Down
Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Roman Gushchin,
Shakeel Butt, Muchun Song, Tejun Heo, Sebastian Andrzej Siewior,
Zhan Xusheng, cgroups, linux-mm, linux-kernel, kernel-team,
regressions, stable
On Sun, Nov 08, 2026 at 12:41:37AM -0800, Chris Down wrote:
> memory.events showed oom_kill 2 in three runs. In one of them the first
> process hadn't submitted the read yet when it was killed. The slow read
> just makes the window easier to hit.
It doesn't need AIO at all. A plain memcg OOM hits it most of the
time: the victim only spends 10-20ms in exit_mmap(), and if the
allocating task fails a charge again in that window, a second (and
sometimes a third) task gets killed.
Reproducer: memory.max=400M, no swap. A holds 150M, B 100M, C 50M, and
T grows by 10M every 0.3s. Only A should be killed.
---8<--- overkill.c
/* A holds 150M, B 100M, C 50M; T grows by 10M every 0.3s up to 200M.
* Run inside a memcg with memory.max=400M. Only A should be killed. */
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
static char *volatile sink; /* keep the stores */
static void hold(size_t mb)
{
sink = malloc(mb << 20);
memset(sink, 1, mb << 20);
}
int main(void)
{
const char *name[] = { "A", "B", "C", "T" };
size_t mb[] = { 150, 100, 50, 0 };
pid_t pid[4];
for (int i = 0; i < 4; i++) {
pid[i] = fork();
if (pid[i])
continue;
if (mb[i]) {
hold(mb[i]);
} else {
sleep(3);
for (int j = 0; j < 20; j++, usleep(300000))
hold(10);
}
pause();
}
sleep(10);
for (int i = 0; i < 4; i++) {
int st, dead = waitpid(pid[i], &st, WNOHANG) == pid[i];
printf("%s %s¥n", name[i],
dead && WIFSIGNALED(st) ? "killed" : "alive");
if (!dead) {
kill(pid[i], SIGKILL);
waitpid(pid[i], NULL, 0);
}
}
return 0;
}
---8<--- run.sh
#!/bin/sh
# cgroup v2, no swap, as root: gcc -O2 -o overkill overkill.c; ./run.sh
CG=/sys/fs/cgroup/overkill
mkdir $CG && echo 400M > $CG/memory.max || exit 1
sh -c "echo ¥$¥$ > $CG/cgroup.procs && exec ./overkill"
grep '^oom_kill ' $CG/memory.events
rmdir $CG
---8<---
Over-kill (oom_kill >= 2) per 20 runs, ThinkPad X1 Carbon Gen3
(i5-5200U, 8 GB), x86_64. "4a" is the same layout driven from a
systemd-run scope with Python tasks; "C" is the program above:
kernel 4a C
6.8.0-149-generic (Ubuntu) 0/20 -
7.0.0-42-generic (Ubuntu) 19/20 19/20
v7.4-rc1 20/20 -
v7.4-rc1 + this patch 0/20 0/20 *
v7.0 + 60f21a264930 + this patch 2/20 0/20
* an earlier, longer version of the same program
v2 has the same code as v1, which is what I tested (identical tree
after git am). On v7.0 the patch also needs CSS_TASK_ITER_WITH_DEAD
from 60f21a264930 ("cgroup, sched_ext: Include exiting tasks in cgroup
iter"); only its cgroup hunks apply there, so that's what I used. The
2/20 left on that kernel look like a separate issue that mainline has
already fixed. On v7.0, MGLRU's page table walk in T's reclaim can
hold an mm_users reference on A's mm (get_next_mm() used
mmget_not_zero() until d14514c66cb9 "mm/vmscan: prevent MGLRU reclaim
from pinning address space" in v7.1-rc1). A's own mmput() is then
not the last one, so A finishes exiting and drops out of the scan
right away, and exit_mmap() runs later from mmput_async() in a
kworker. The next OOM picks B. I traced 2 such over-kills in 40
runs; with MGLRU page table walks turned off (lru_gen/enabled =
0x0005) I got 0/40. So a 7.0.y backport probably wants d14514c66cb9
as well; it applies to v7.0 and v6.19 as is. I haven't built v7.0
with all three.
Zhan's series [1] is a different problem (thread groups migrated after
their leader exited); v7.4-rc1 with it still gave 20/20 here. The two
patches change the same css_task_iter_start() line, so they will
conflict.
Tested-by: elenchist <elenchist.dev@gmail.com>
Adding the regressions list: a72f73c4dd9b is in v7.0 and was also
backported to 6.19.9.
#regzbot introduced: a72f73c4dd9b
#regzbot title: memcg OOM kills more tasks while the first victim exits
#regzbot fix: mm: memcontrol: Don't lose track of exiting OOM victims
I used an AI coding assistant (Claude Code) to build the kernels, run
the tests, analyse the traces and draft this mail; I have checked the
results and the text.
[1] https://lore.kernel.org/linux-mm/20261027144016.2582880-1-zhanxusheng1024@gmail.com/
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims
2026-11-12 10:34 ` elenchist
@ 2026-11-12 10:45 ` elenchist
0 siblings, 0 replies; 6+ messages in thread
From: elenchist @ 2026-11-12 10:45 UTC (permalink / raw)
To: Chris Down
Cc: Andrew Morton, Johannes Weiner, Michal Hocko, Roman Gushchin,
Shakeel Butt, Muchun Song, Tejun Heo, Sebastian Andrzej Siewior,
Zhan Xusheng, cgroups, linux-mm, linux-kernel, kernel-team,
regressions, stable
My mail client turned the backslashes in the reproducer into yen
signs, sorry. Every yen sign there should be a backslash: the printf
format in overkill.c ends in backslash-n, and in run.sh each of the
two "$" right after "echo" has a backslash in front of it.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims
2026-11-08 8:41 [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims Chris Down
` (2 preceding siblings ...)
2026-11-12 10:34 ` elenchist
@ 2026-11-12 17:11 ` Johannes Weiner
3 siblings, 0 replies; 6+ messages in thread
From: Johannes Weiner @ 2026-11-12 17:11 UTC (permalink / raw)
To: Chris Down
Cc: Andrew Morton, Michal Hocko, Roman Gushchin, Shakeel Butt,
Muchun Song, Tejun Heo, Sebastian Andrzej Siewior, Zhan Xusheng,
cgroups, linux-mm, linux-kernel, kernel-team
On Sun, Nov 08, 2026 at 12:41:37AM -0800, Chris Down wrote:
> When oom_evaluate_task() comes across a victim we've already killed, it
> backs off until the victim's mm has MMF_OOM_SKIP. This gives the victim
> a chance to free some memory before we kill anything else. However,
> since commit a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup"),
> css_task_iter skips exiting tasks once signal->live reaches zero. That
> happens before exit_mm(), so a single-threaded victim can disappear
> from the memcg scan while it still has all its memory.
>
> This can kill another process when the first kill would have been
> enough to recover, and we've seen it happen in production. If the
> second victim is in a cgroup with memory.oom.group enabled, the extra
> kill can take down the whole workload.
>
> In tests I used a 64 MiB memcg with swap disabled. I started a process
> that faults in 44 MiB of anonymous memory, submits a 4 KiB direct AIO
> read to null_blk, and goes to sleep. I set null_blk to take 10 seconds
> to complete the read so the victim would have to wait for it in
> exit_aio(). Two seconds after starting the first process, I started a
> second one that tries to fault in 40 MiB.
>
> The 40 MiB allocation hits memory.max partway through and triggers an
> OOM kill. The OOM killer picks the first process, which has more
> resident memory. With the read still pending, the victim blocks in
> exit_aio() before it can tear down its address space.
>
> memory.events showed oom_kill 2 in three runs. In one of them the first
> process hadn't submitted the read yet when it was killed. The slow read
> just makes the window easier to hit.
>
> The cgroup iterator hides these tasks so that cgroup.procs doesn't
> show processes that waitpid() may already have reaped. That's fine for
> cgroup.procs, but the OOM scan needs to see a victim even after its last
> thread has started exiting. We already have CSS_TASK_ITER_WITH_DEAD
> for sched_ext, so use it for mem_cgroup_scan_tasks() as well.
>
> With this applied, OOM selection backs off while the first victim still
> has memory to free. The 40 MiB allocation completes and memory.events
> shows oom_kill 1 in three test runs.
>
> Fixes: a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Chris Down <chris@chrisdown.name>
Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
^ permalink raw reply [flat|nested] 6+ messages in thread