Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims

From: elenchist

Date: Thu Nov 12 2026 - 05:47:07 EST


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@xxxxxxxxx>

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@xxxxxxxxx/