* Re: [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims
[not found] <avA1wXhTMs9eY2NF@chrisdown.name>
@ 2026-11-12 10:34 ` elenchist
2026-11-12 10:45 ` elenchist
2026-11-23 6:24 ` [merged mm-hotfixes-stable] mm-memcontrol-dont-lose-track-of-exiting-oom-victims.patch removed from -mm tree Andrew Morton
1 sibling, 1 reply; 3+ 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] 3+ messages in thread* [merged mm-hotfixes-stable] mm-memcontrol-dont-lose-track-of-exiting-oom-victims.patch removed from -mm tree
[not found] <avA1wXhTMs9eY2NF@chrisdown.name>
2026-11-12 10:34 ` [PATCH v2] mm: memcontrol: Don't lose track of exiting OOM victims elenchist
@ 2026-11-23 6:24 ` Andrew Morton
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2026-11-23 6:24 UTC (permalink / raw)
To: mm-commits, zhanxusheng1024, tj, stable, shakeel.butt,
roman.gushchin, muchun.song, mhocko, mhocko, hannes,
elenchist.dev, bigeasy, chris, akpm
The quilt patch titled
Subject: mm: memcontrol: don't lose track of exiting OOM victims
has been removed from the -mm tree. Its filename was
mm-memcontrol-dont-lose-track-of-exiting-oom-victims.patch
This patch was dropped because it was merged into the mm-hotfixes-stable branch
of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: Chris Down <chris@chrisdown.name>
Subject: mm: memcontrol: don't lose track of exiting OOM victims
Date: Sun, 08 Nov 2026 00:41:37 -0800
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.
Link: https://patch.msgid.link/avA1wXhTMs9eY2NF@chrisdown.name
Fixes: a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup")
Signed-off-by: Chris Down <chris@chrisdown.name>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Michal Hocko <mhocko@suse.com>
Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
Tested-by: elenchist <elenchist.dev@gmail.com>
Link: https://lore.kernel.org/6B8E9261-3FF8-441B-8F47-7D35834A8D35@gmail.com
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Tejun Heo <tj@kernel.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Zhan Xusheng <zhanxusheng1024@gmail.com>
Cc: <stable@vger.kernel.org>
mm/memcontrol.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
--- a/mm/memcontrol.c~mm-memcontrol-dont-lose-track-of-exiting-oom-victims
+++ a/mm/memcontrol.c
@@ -1451,7 +1451,13 @@ void mem_cgroup_scan_tasks(struct mem_cg
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 */
_
Patches currently in -mm which might be from chris@chrisdown.name are
selftests-mm-add-uffdio_move-huge-zeropage-pmd-regression-test.patch
^ permalink raw reply [flat|nested] 3+ messages in thread