* [PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
@ 2026-12-02 9:53 ` Michal Hocko
2026-12-02 9:53 ` [PATCH 02/10] mm/oom_kill: keep the allocating mm eligible Michal Hocko
` (9 subsequent siblings)
10 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:53 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Michal Hocko <mhocko@suse.com>
SVA device faults at the limit of a v1 memcg with the OOM killer
disabled can leak a css reference, or cause a use-after-free of the
memcg.
handle_mm_fault() enables the memcg OOM handling for FAULT_FLAG_USER, so
a failed charge can leave a css reference in current->memcg_in_oom. On
VM_FAULT_OOM the caller has to drop it in pagefault_out_of_memory(). But
iommu_sva_handle_mm() sets the flag for faults that it handles in a
kworker, and never makes that call. The pointer stays on the kworker,
and fork copies it to usermode helpers without a reference:
kworker usermode helper
======= ===============
iommu_sva_handle_mm()
memcg1_oom_prepare()
css_get()
current->memcg_in_oom = memcg
call_usermodehelper_exec_work()
dup_task_struct()
handle_mm_fault()
css_put()
iommu_sva_handle_mm()
handle_mm_fault()
css_put()
Fix this by explicitly avoid oom_disabled path for workers. This would
simply fail the charge and bail out without triggering the oom killer
same as if this happened outside of #PF handler.
Fixes: fc36479db74e ("iommu: Add a page fault handler")
Cc: stable
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/memcontrol-v1.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index b7f286888507..f2391eea4e0d 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1100,7 +1100,8 @@ bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked)
* victim and then we have to bail out from the charge path.
*/
if (READ_ONCE(memcg->oom_kill_disable)) {
- if (current->in_user_fault) {
+ if (current->in_user_fault &&
+ !(current->flags & (PF_WQ_WORKER|PF_IO_WORKER))) {
css_get(&memcg->css);
current->memcg_in_oom = memcg;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH 02/10] mm/oom_kill: keep the allocating mm eligible
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
2026-12-02 9:53 ` [PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts Michal Hocko
@ 2026-12-02 9:53 ` Michal Hocko
2026-12-02 9:53 ` [PATCH 03/10] mm, oom: do not panic on constrained OOMs Michal Hocko
` (8 subsequent siblings)
10 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:53 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Chris Mason <mason@kernel.org>
On a NUMA machine, a process can exhaust nodes that its own task
mempolicy or cpuset. The mempolicy can kill unrelated tasks one after
the other, and then panic, while that process is never selected.
The OOM killer skips a task that oom_cpuset_eligible() rejects, on the
assumption that killing it frees nothing on the nodes that ran out. But
each of the two tests in that function can reject the process whose mm
is filling those nodes:
- In a mempolicy OOM, the function checks the mempolicy of the task.
But the nodemask of the allocation can come from the mempolicy of a
VMA, so a task with MPOL_BIND to other nodes can still fill the
exhausted nodes.
- In a cpuset OOM, the function compares the mems_allowed of the task
with that of current. mems_allowed is per task, so a vfork() child can
allocate in a different cpuset from its parent. oom_badness() skips
the child because it is in vfork(). The parent shares the mm, but it
is rejected because its cpuset has other nodes.
Tasks with no mempolicy, or in the root cpuset, pass these tests and are
killed instead. When none is left, out_of_memory() panics.
The fix used here is to treat every process that shares the mm of
current as eligible, since current is allocating on the exhausted nodes.
Fixes: 6f48d0ebd907 ("oom: select task from tasklist for mempolicy ooms")
Signed-off-by: Chris Mason <mason@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/oom_kill.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 9de57a23893c..30c314069d5b 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -82,7 +82,8 @@ static inline bool is_memcg_oom(struct oom_control *oc)
*
* Task eligibility is determined by whether or not a candidate task, @tsk,
* shares the same mempolicy nodes as current if it is bound by such a policy
- * and whether or not it has the same set of allowed cpuset nodes.
+ * and whether or not it has the same set of allowed cpuset nodes. A process
+ * that shares the mm of current is always eligible.
*
* This function is assuming oom-killer context and 'current' has triggered
* the oom-killer.
@@ -90,11 +91,23 @@ static inline bool is_memcg_oom(struct oom_control *oc)
static bool oom_cpuset_eligible(struct task_struct *start,
struct oom_control *oc)
{
+ struct mm_struct *mm = current->mm;
struct task_struct *tsk;
bool ret = false;
const nodemask_t *mask = oc->nodemask;
rcu_read_lock();
+ /*
+ * current allocates on the nodes of this oom, so its mm may have memory
+ * there. The tests below can miss that: the nodemask can come from the
+ * mempolicy of a vma while that of the task binds it to other nodes, and
+ * a process that shares the mm can be in another cpuset.
+ */
+ if (mm && process_shares_mm(start, mm)) {
+ ret = true;
+ goto unlock;
+ }
+
for_each_thread(start, tsk) {
if (mask) {
/*
@@ -114,6 +127,7 @@ static bool oom_cpuset_eligible(struct task_struct *start,
if (ret)
break;
}
+unlock:
rcu_read_unlock();
return ret;
@@ -407,8 +421,8 @@ static int dump_task(struct task_struct *p, void *arg)
* @oc: pointer to struct oom_control
*
* Dumps the current memory state of all eligible tasks. Tasks not in the same
- * memcg, not in the same cpuset, or bound to a disjoint set of mempolicy nodes
- * are not shown.
+ * memcg are not shown. Nor are tasks not in the same cpuset or bound to a
+ * disjoint set of mempolicy nodes, unless they share the mm of current.
* State information includes task's pid, uid, tgid, vm size, rss,
* pgtables_bytes, swapents, oom_score_adj value, and name.
*/
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH 03/10] mm, oom: do not panic on constrained OOMs
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
2026-12-02 9:53 ` [PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts Michal Hocko
2026-12-02 9:53 ` [PATCH 02/10] mm/oom_kill: keep the allocating mm eligible Michal Hocko
@ 2026-12-02 9:53 ` Michal Hocko
2026-12-02 9:53 ` [PATCH 04/10] mm/oom_kill: ratelimit memcg OOM reports with no victim Michal Hocko
` (7 subsequent siblings)
10 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:53 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Michal Hocko <mhocko@suse.com>
domain (cpuset, mempolicy or memcg) OOMs represent a partial shortage of
memory. There might still be memory available outside of the domain and
therefore panicing at this stage might disrupt otherwise operational
workloads. We already avoid panicing for memcg ooms. Node bound (by
mempolicy or cpusets) ooms shouldn't be any different from that.
Do not panic on any fail to find an oom victim unless this is full
system wide oom (CONSTRAINT_NONE) context.
Fixes: 6f48d0ebd907 ("oom: select task from tasklist for mempolicy ooms")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/oom_kill.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 30c314069d5b..660ab39e1df5 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -1168,10 +1168,11 @@ bool out_of_memory(struct oom_control *oc)
pr_warn("Out of memory and no killable processes...\n");
/*
* If we got here due to an actual allocation at the
- * system level, we cannot survive this and will enter
- * an endless loop in the allocator. Bail out now.
+ * system level without any constrains, we cannot survive this
+ * and will enter an endless loop in the allocator.
+ * Bail out now.
*/
- if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
+ if (!is_sysrq_oom(oc) && oc->constraint == CONSTRAINT_NONE)
panic("System is deadlocked on memory\n");
}
if (oc->chosen && oc->chosen != (void *)-1UL)
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH 04/10] mm/oom_kill: ratelimit memcg OOM reports with no victim
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (2 preceding siblings ...)
2026-12-02 9:53 ` [PATCH 03/10] mm, oom: do not panic on constrained OOMs Michal Hocko
@ 2026-12-02 9:53 ` Michal Hocko
2026-12-02 9:53 ` [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim Michal Hocko
` (6 subsequent siblings)
10 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:53 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Chris Mason <mason@kernel.org>
A memory cgroup that is at its limit and has no killable task can flood
the kernel log with OOM reports, and delay the OOM killer for the rest
of the system.
When a memcg OOM finds no victim, out_of_memory() prints the whole OOM
report and returns false. Nothing was freed, so the next charge that
invokes the OOM killer, or the next write that sets memory.max below the
usage, prints the same report again. Each report is printed with
oom_lock held, which every other OOM in the system needs as well.
The fix used here is to rate limit the report of a memcg OOM with no
victim. All memory cgroups share that rate limit, so a cgroup
that keeps running into such an OOM can suppress the report of another
cgroup. If that turns out to be too limiting we can make this per memcg
but there doesn't seem to be a strong reason to go that way for now
Fixes: 3100dab2aa09 ("mm: memcontrol: print proper OOM header when no eligible victim left")
Signed-off-by: Chris Mason <mason@kernel.org>
[mhocko@suse.com: reworked the original patch]
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/oom_kill.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 660ab39e1df5..7c97f788501d 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -1164,8 +1164,20 @@ bool out_of_memory(struct oom_control *oc)
select_bad_process(oc);
/* Found nothing?!?! */
if (!oc->chosen) {
- dump_header(oc);
- pr_warn("Out of memory and no killable processes...\n");
+ static DEFINE_RATELIMIT_STATE(memcg_rs, DEFAULT_RATELIMIT_INTERVAL,
+ DEFAULT_RATELIMIT_BURST);
+
+ /*
+ * A memcg oom does not panic, and its caller goes on. A memcg
+ * memcg with no killable task gets here with each charge that
+ * calls the oom killer, and with each write to memory.max, so do
+ * not dump each time.
+ */
+ if (!is_memcg_oom(oc) || __ratelimit(&memcg_rs)) {
+ dump_header(oc);
+ pr_warn("Out of memory and no killable processes...\n");
+ }
+
/*
* If we got here due to an actual allocation at the
* system level without any constrains, we cannot survive this
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (3 preceding siblings ...)
2026-12-02 9:53 ` [PATCH 04/10] mm/oom_kill: ratelimit memcg OOM reports with no victim Michal Hocko
@ 2026-12-02 9:53 ` Michal Hocko
2026-12-02 16:30 ` sashiko-bot
2026-12-02 9:53 ` [PATCH 06/10] mm, oom: check oom victims prior to numa placement Michal Hocko
` (5 subsequent siblings)
10 siblings, 1 reply; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:53 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Michal Hocko <mhocko@suse.com>
If an OOM victim shares its mm with another process, a global OOM can
panic the kernel if no other process is killable. Otherwise it can
needlessly kill further processes. Both need a victim with much memory
that the OOM reaper cannot free, such as page tables. They can happen if
the other process is frozen by the cgroup v1 freezer.
Thaw tasks that are killed because they are sharing mm with the oom
victim to address that.
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/oom_kill.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 7c97f788501d..6dc9424e6a05 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -990,6 +990,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
if (unlikely(p->flags & PF_KTHREAD))
continue;
do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
+ thaw_process(p);
}
rcu_read_unlock();
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim
2026-12-02 9:53 ` [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim Michal Hocko
@ 2026-12-02 16:30 ` sashiko-bot
2026-12-02 19:23 ` Michal Hocko
0 siblings, 1 reply; 21+ messages in thread
From: sashiko-bot @ 2026-12-02 16:30 UTC (permalink / raw)
To: Michal Hocko, David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, Chris Mason
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm, oom: thaw_process() fails to thaw shared-mm tasks in
__oom_kill_process()
--
commit 342e5dd06461083fcf31f5a288fe32e31c789a6e
Author: Michal Hocko <mhocko@suse.com>
mm, oom: thaw tasks sharing the mm with the oom victim
This commit calls thaw_process() on processes in other thread groups that
are killed because they share their mm with the OOM victim.
> diff --git a/mm/oom_kill.c b/mm/oom_kill.c
> index 56e65fa1b08e4..aca0e816b87a8 100644
> --- a/mm/oom_kill.c
> +++ b/mm/oom_kill.c
> @@ -990,6 +990,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
> if (unlikely(p->flags & PF_KTHREAD))
> continue;
> do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
> + thaw_process(p);
[Severity: High]
Can thaw_process(p) actually unfreeze p here when mark_oom_victim() was
only called on victim?
Because this loop skips same_thread_group(p, victim), p has its own
signal_struct where p->signal->oom_mm is still NULL, so
tsk_is_oom_victim() returns false for p's threads:
kernel/freezer.c:freezing_slow_path() {
...
if (tsk_is_oom_victim(p))
return false;
if (pm_nosig_freezing || cgroup1_freezing(p))
return true;
...
}
When thaw_process(p) wakes a thread in __refrigerator() (or a thread in a
TASK_FREEZABLE sleep that enters get_signal() -> try_to_freeze() before
checking SIGKILL), freezing(current) still returns true if its cgroup v1
freezer is frozen. The thread goes right back into TASK_FROZEN without
exiting, leaving the shared mm pinned and failing to avoid the extra OOM
kills or panic this patch aims to prevent.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202095450.296675-1-mhocko@kernel.org?part=5
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim
2026-12-02 16:30 ` sashiko-bot
@ 2026-12-02 19:23 ` Michal Hocko
0 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 19:23 UTC (permalink / raw)
To: sashiko-reviews
Cc: David Hildenbrand, David Rientjes, Ben Gardon, Johannes Weiner,
Andrew Morton, linux-mm, Chris Mason
On Wed 02-12-26 16:30:59, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [High] mm, oom: thaw_process() fails to thaw shared-mm tasks in
> __oom_kill_process()
> --
>
> commit 342e5dd06461083fcf31f5a288fe32e31c789a6e
> Author: Michal Hocko <mhocko@suse.com>
>
> mm, oom: thaw tasks sharing the mm with the oom victim
>
> This commit calls thaw_process() on processes in other thread groups that
> are killed because they share their mm with the OOM victim.
>
> > diff --git a/mm/oom_kill.c b/mm/oom_kill.c
> > index 56e65fa1b08e4..aca0e816b87a8 100644
> > --- a/mm/oom_kill.c
> > +++ b/mm/oom_kill.c
> > @@ -990,6 +990,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
> > if (unlikely(p->flags & PF_KTHREAD))
> > continue;
> > do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID);
> > + thaw_process(p);
>
> [Severity: High]
> Can thaw_process(p) actually unfreeze p here when mark_oom_victim() was
> only called on victim?
>
> Because this loop skips same_thread_group(p, victim), p has its own
> signal_struct where p->signal->oom_mm is still NULL, so
> tsk_is_oom_victim() returns false for p's threads:
>
> kernel/freezer.c:freezing_slow_path() {
> ...
> if (tsk_is_oom_victim(p))
> return false;
>
> if (pm_nosig_freezing || cgroup1_freezing(p))
> return true;
> ...
> }
>
> When thaw_process(p) wakes a thread in __refrigerator() (or a thread in a
> TASK_FREEZABLE sleep that enters get_signal() -> try_to_freeze() before
> checking SIGKILL), freezing(current) still returns true if its cgroup v1
> freezer is frozen. The thread goes right back into TASK_FROZEN without
> exiting, leaving the shared mm pinned and failing to avoid the extra OOM
> kills or panic this patch aims to prevent.
Correct. I believe the following should address the issue. I will fold
that into the patch. Thanks for pointing out. I always forget all these
details about freezer. I thought thaw_task would act merely on grounds
of pending SIGKILL. Mea culpa
diff --git a/kernel/freezer.c b/kernel/freezer.c
index a76bf957fb32..61a4c1607743 100644
--- a/kernel/freezer.c
+++ b/kernel/freezer.c
@@ -44,6 +44,9 @@ bool freezing_slow_path(struct task_struct *p)
if (tsk_is_oom_victim(p))
return false;
+ if (p->mm && mm_is_oom_victim(p->mm))
+ return false;
+
if (pm_nosig_freezing || cgroup1_freezing(p))
return true;
--
Michal Hocko
SUSE Labs
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 06/10] mm, oom: check oom victims prior to numa placement
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (4 preceding siblings ...)
2026-12-02 9:53 ` [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim Michal Hocko
@ 2026-12-02 9:53 ` Michal Hocko
2026-12-02 9:54 ` [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim Michal Hocko
` (4 subsequent siblings)
10 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:53 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Michal Hocko <mhocko@suse.com>
oom_evaluate_task checks oom_cpuset_eligible before potential inflight
oom victims. This allows to have several oom victims on disjunct numa
domains but it also allows to unnecessarily kill more tasks than
necessary. But if we have tasks that consume memory in multiple numa
nodes (e.g. via vma memory policy) then we can bail too early:
TaskA (nodemask=1, vma->mpol=0), TaskB (nodemask=1)
OOM1(nodemask=0): TaskA killed
OOM2(nodemask=1): TaskA invisble due to oom_cpuset_eligible but
it will be freeing memopry from node=1 too.
Check for existing oom victims before numa placement and bail out
if there is any oom victim even if it could be in a different oom
domain. This is a slow path and postponed OOM killing is almost always a
better choice than killing that is too eager.
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/oom_kill.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 6dc9424e6a05..6ff1e93ec6e3 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -318,10 +318,6 @@ static int oom_evaluate_task(struct task_struct *task, void *arg)
if (oom_unkillable_task(task))
goto next;
- /* p may not have freeable memory in nodemask */
- if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
- goto next;
-
/*
* This task already has access to memory reserves and is being killed.
* Don't allow any other task to have access to the reserves unless
@@ -334,6 +330,10 @@ static int oom_evaluate_task(struct task_struct *task, void *arg)
goto abort;
}
+ /* p may not have freeable memory in nodemask */
+ if (!is_memcg_oom(oc) && !oom_cpuset_eligible(task, oc))
+ goto next;
+
/*
* If task is allocating a lot of memory and has been marked to be
* killed first if it triggers an oom, then select it.
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (5 preceding siblings ...)
2026-12-02 9:53 ` [PATCH 06/10] mm, oom: check oom victims prior to numa placement Michal Hocko
@ 2026-12-02 9:54 ` Michal Hocko
2026-12-02 16:31 ` sashiko-bot
2026-12-02 19:22 ` Harry Yoo
2026-12-02 9:54 ` [PATCH 08/10] mm, " Michal Hocko
` (3 subsequent siblings)
10 siblings, 2 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:54 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Michal Hocko <mhocko@suse.com>
In a memory cgroup with a small limit, the OOM killer can kill a task
while most of the limit is taken up by memory that is already queued to
be freed after an RCU grace period.
Memory that was passed to call_rcu() stays charged until its callback
frees it. The task_struct and the credentials of an exited child are
freed that way. With MMU_GATHER_RCU_TABLE_FREE, so are its page tables.
Reclaim cannot free such memory, and call_rcu() does not throttle its
caller. So tasks that fork short-lived children can fill the limit with
memory that only waits to be freed. When a charge then fails at the
limit, try_charge_memcg() reclaims and drains the per-CPU stocks. It
then calls mem_cgroup_oom() without waiting for a grace period.
Call rcu_barrier before retrying the charge after unsuccessful reclaim
(slow path) to reduce this potential issue.
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/memcontrol.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 84f4a8fd4d16..a8dd933273c8 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2800,8 +2800,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
goto retry;
- if (nr_retries--)
+ if (nr_retries--) {
+ /* Wait for any potential call_rcu memory freers */
+ rcu_barrier();
goto retry;
+ }
if (gfp_mask & __GFP_RETRY_MAYFAIL)
goto nomem;
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 9:54 ` [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim Michal Hocko
@ 2026-12-02 16:31 ` sashiko-bot
2026-12-02 19:38 ` Michal Hocko
2026-12-02 19:22 ` Harry Yoo
1 sibling, 1 reply; 21+ messages in thread
From: sashiko-bot @ 2026-12-02 16:31 UTC (permalink / raw)
To: Michal Hocko, David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, Chris Mason
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] memcg: repeated rcu_barrier() on every retry loop in
try_charge_memcg()
- [Medium] memcg: per-CPU stocks not drained after rcu_barrier() in
try_charge_memcg()
--
commit cdd0f5947203961fb686dca72c1e12849c882b50
Author: Michal Hocko <mhocko@suse.com>
memcg, oom: wait for rcu_call callbacks before retrying reclaim
This commit calls rcu_barrier() in try_charge_memcg() before retrying the
charge after unsuccessful reclaim, so pending call_rcu() memory frees complete
before invoking the memcg OOM killer.
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index b75a61ddeded5..33d1e18abeda1 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2921,8 +2921,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
> if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
> goto retry;
>
> - if (nr_retries--)
> + if (nr_retries--) {
> + /* Wait for any potential call_rcu memory freers */
> + rcu_barrier();
> goto retry;
> + }
>
> if (gfp_mask & __GFP_RETRY_MAYFAIL)
> goto nomem;
[Severity: High]
Should rcu_barrier() be limited to run at most once per charge rather than on
every nr_retries iteration?
nr_retries starts at MAX_RECLAIM_RETRIES (16) and is reset to 16 after
mem_cgroup_oom(), while each rcu_barrier() serializes on the global
rcu_state.barrier_mutex, sends cross-CPU IPIs, and waits for a full RCU
callback drain. In addition to running 16 times before each OOM attempt,
costly high-order charges (nr_pages > 1 << PAGE_ALLOC_COSTLY_ORDER), such as
THP faults with GFP_TRANSHUGE on MADV_HUGEPAGE mappings near memory.max, fall
through the nr_reclaimed check above and block for 16 consecutive
rcu_barrier() calls before falling back to order-0 pages.
[Severity: Medium]
Can the charges freed by RCU callbacks during rcu_barrier() remain trapped in
per-CPU stocks without drain_all_stock() running again?
On the first failed reclaim pass, drain_all_stock(mem_over_limit) runs and
sets drained = true before jumping to retry, so rcu_barrier() is only reached
on a later pass after drained is already set:
mm/memcontrol.c:try_charge_memcg() {
...
if (!drained) {
drain_all_stock(mem_over_limit);
drained = true;
goto retry;
}
...
if (nr_retries--) {
/* Wait for any potential call_rcu memory freers */
rcu_barrier();
goto retry;
}
}
When RCU callbacks such as delayed_put_task_struct() or tlb_remove_table_rcu()
free slab objects and page tables during rcu_barrier(), they uncharge via
obj_cgroup_uncharge_pages() -> refill_stock(), which caches up to
MEMCG_STOCK_HIGH (64 pages) in the callback CPU's memcg_stock (plus up to a
page in obj_stock). Because drained is never reset after rcu_barrier(),
consume_stock() on retry only sees the charging CPU's local stock, leaving
charges freed on other CPUs cached in remote stocks and still triggering
mem_cgroup_oom() in small cgroups.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202095450.296675-1-mhocko@kernel.org?part=7
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 16:31 ` sashiko-bot
@ 2026-12-02 19:38 ` Michal Hocko
0 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 19:38 UTC (permalink / raw)
To: sashiko-reviews
Cc: David Hildenbrand, David Rientjes, Ben Gardon, Johannes Weiner,
Andrew Morton, linux-mm, Chris Mason
On Wed 02-12-26 16:31:01, sashiko-bot@kernel.org wrote:
> [Severity: Medium]
> Can the charges freed by RCU callbacks during rcu_barrier() remain trapped in
> per-CPU stocks without drain_all_stock() running again?
>
> On the first failed reclaim pass, drain_all_stock(mem_over_limit) runs and
> sets drained = true before jumping to retry, so rcu_barrier() is only reached
> on a later pass after drained is already set:
>
> mm/memcontrol.c:try_charge_memcg() {
> ...
> if (!drained) {
> drain_all_stock(mem_over_limit);
> drained = true;
> goto retry;
> }
> ...
> if (nr_retries--) {
> /* Wait for any potential call_rcu memory freers */
> rcu_barrier();
> goto retry;
> }
> }
Right and I do not see this to be a problem.
> When RCU callbacks such as delayed_put_task_struct() or tlb_remove_table_rcu()
> free slab objects and page tables during rcu_barrier(), they uncharge via
> obj_cgroup_uncharge_pages() -> refill_stock(), which caches up to
> MEMCG_STOCK_HIGH (64 pages) in the callback CPU's memcg_stock (plus up to a
> page in obj_stock). Because drained is never reset after rcu_barrier(),
> consume_stock() on retry only sees the charging CPU's local stock, leaving
> charges freed on other CPUs cached in remote stocks and still triggering
> mem_cgroup_oom() in small cgroups.
Right, maybe something worth looking into but in a separate patch. I
believe we can manage caches more efficiently in general rather than
chasing for few cpus. If anything I would base that on top of Joshua's
work (that I still owe a review).
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261202095450.296675-1-mhocko@kernel.org?part=7
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 9:54 ` [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim Michal Hocko
2026-12-02 16:31 ` sashiko-bot
@ 2026-12-02 19:22 ` Harry Yoo
2026-12-02 19:33 ` Michal Hocko
1 sibling, 1 reply; 21+ messages in thread
From: Harry Yoo @ 2026-12-02 19:22 UTC (permalink / raw)
To: Michal Hocko
Cc: David Hildenbrand, David Rientjes, Ben Gardon, Johannes Weiner,
mhocko, Andrew Morton, linux-mm, LKML, Chris Mason
On Wed, Dec 02, 2026 at 10:54:00AM +0100, Michal Hocko wrote:
> From: Michal Hocko <mhocko@suse.com>
>
> In a memory cgroup with a small limit, the OOM killer can kill a task
> while most of the limit is taken up by memory that is already queued to
> be freed after an RCU grace period.
>
> Memory that was passed to call_rcu() stays charged until its callback
> frees it. The task_struct and the credentials of an exited child are
> freed that way. With MMU_GATHER_RCU_TABLE_FREE, so are its page tables.
> Reclaim cannot free such memory, and call_rcu() does not throttle its
> caller.
>
> So tasks that fork short-lived children can fill the limit with
> memory that only waits to be freed. When a charge then fails at the
> limit, try_charge_memcg() reclaims and drains the per-CPU stocks. It
> then calls mem_cgroup_oom() without waiting for a grace period.
>
> Call rcu_barrier before retrying the charge after unsuccessful reclaim
> (slow path) to reduce this potential issue.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Michal Hocko <mhocko@suse.com>
> ---
I was wondering why we have to call rcu_barrier() for every retry
instead of just once before OOM. Retrying memory won't free page
tables anyway.
Apparently sashiko wondered it too.
> mm/memcontrol.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 84f4a8fd4d16..a8dd933273c8 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -2800,8 +2800,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
> if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
> goto retry;
>
> - if (nr_retries--)
> + if (nr_retries--) {
> + /* Wait for any potential call_rcu memory freers */
> + rcu_barrier();
> goto retry;
> + }
>
> if (gfp_mask & __GFP_RETRY_MAYFAIL)
> goto nomem;
> --
> 2.56.1
--
Cheers,
Harry / Hyeonggon
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 19:22 ` Harry Yoo
@ 2026-12-02 19:33 ` Michal Hocko
0 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 19:33 UTC (permalink / raw)
To: Harry Yoo
Cc: David Hildenbrand, David Rientjes, Ben Gardon, Johannes Weiner,
Andrew Morton, linux-mm, LKML, Chris Mason
On Wed 02-12-26 19:22:20, Harry Yoo wrote:
> On Wed, Dec 02, 2026 at 10:54:00AM +0100, Michal Hocko wrote:
> > From: Michal Hocko <mhocko@suse.com>
> >
> > In a memory cgroup with a small limit, the OOM killer can kill a task
> > while most of the limit is taken up by memory that is already queued to
> > be freed after an RCU grace period.
> >
> > Memory that was passed to call_rcu() stays charged until its callback
> > frees it. The task_struct and the credentials of an exited child are
> > freed that way. With MMU_GATHER_RCU_TABLE_FREE, so are its page tables.
> > Reclaim cannot free such memory, and call_rcu() does not throttle its
> > caller.
> >
> > So tasks that fork short-lived children can fill the limit with
> > memory that only waits to be freed. When a charge then fails at the
> > limit, try_charge_memcg() reclaims and drains the per-CPU stocks. It
> > then calls mem_cgroup_oom() without waiting for a grace period.
> >
> > Call rcu_barrier before retrying the charge after unsuccessful reclaim
> > (slow path) to reduce this potential issue.
> >
> > Reported-by: Chris Mason <mason@kernel.org>
> > Signed-off-by: Michal Hocko <mhocko@suse.com>
> > ---
>
> I was wondering why we have to call rcu_barrier() for every retry
> instead of just once before OOM. Retrying memory won't free page
> tables anyway.
>
> Apparently sashiko wondered it too.
I considered this to be a slow path (the full reclaim round to fail) so
additional wait on rcu_barrier doesn't seem all that terrible. Yet we
capture potential memory freers before retrying the charge.
I could have done something like
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index a8dd933273c8..7a1e9afed685 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2802,7 +2802,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
if (nr_retries--) {
/* Wait for any potential call_rcu memory freers */
- rcu_barrier();
+ if (!nr_retries)
+ rcu_barrier();
goto retry;
}
to reduce that to the very last run. But this is a bit confusing and in
the worst case we are going through more expensive reclaim rounds while
there might be rcu callbacks sitting and waiting. I do not think we have
a decent way of knowning and making that more directed. But if there is
a general agreement that this is the right way, I can go with that of
course. This would be really hard to measure without artificial
workloads which I do not want to really optimize for.
--
Michal Hocko
SUSE Labs
^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH 08/10] mm, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (6 preceding siblings ...)
2026-12-02 9:54 ` [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim Michal Hocko
@ 2026-12-02 9:54 ` Michal Hocko
2026-12-02 16:31 ` sashiko-bot
2026-12-02 9:54 ` [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer Michal Hocko
` (2 subsequent siblings)
10 siblings, 1 reply; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:54 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML
From: Michal Hocko <mhocko@suse.com>
Memory reclaim can miss quite a lot of pages freed through call_rcu
because it itself doen't trigger RCU grace period. cond_resched() is not
sufficient for that. Call rcu_barrier to wait for those callbacks to
execute before retrying the reclaim loop again.
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/page_alloc.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 6624293e4d78..399a7682b2c9 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -4701,6 +4701,9 @@ should_reclaim_retry(gfp_t gfp_mask, unsigned order,
schedule_timeout_uninterruptible(1);
else
cond_resched();
+
+ /* Wait for any potential call_rcu memory freers */
+ rcu_barrier();
out:
/* Before OOM, exhaust highatomic_reserve */
if (!ret)
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 08/10] mm, oom: wait for rcu_call callbacks before retrying reclaim
2026-12-02 9:54 ` [PATCH 08/10] mm, " Michal Hocko
@ 2026-12-02 16:31 ` sashiko-bot
0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-02 16:31 UTC (permalink / raw)
To: Michal Hocko, David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm: rcu_barrier() in should_reclaim_retry() runs after zone
watermark check
- [High] mm: should_reclaim_retry() calls rcu_barrier() even when
reclaim progresses
--
commit 80d44083709f0dc6896c3ee8f1c5829f534bd7fa
Author: Michal Hocko <mhocko@suse.com>
mm, oom: wait for rcu_call callbacks before retrying reclaim
This commit adds an rcu_barrier() call in should_reclaim_retry() to wait
for in-flight RCU callbacks to free memory before retrying reclaim.
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index aa1ed599c7c6b..c4dc421e7edf1 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -4713,6 +4713,9 @@ should_reclaim_retry(gfp_t gfp_mask, unsigned order,
> schedule_timeout_uninterruptible(1);
> else
> cond_resched();
> +
> + /* Wait for any potential call_rcu memory freers */
> + rcu_barrier();
> out:
> /* Before OOM, exhaust highatomic_reserve */
> if (!ret)
[Severity: High]
Does running rcu_barrier() after the zone watermark loop cause
should_reclaim_retry() to return false without re-checking the memory just
freed by the RCU callbacks?
Pages waiting on call_rcu() are not counted in zone_reclaimable_pages() or
NR_FREE_PAGES until their callbacks run. When remaining LRU and free
pages are below min_wmark, the loop leaves ret as false before
rcu_barrier() drains the pending callbacks, so should_reclaim_retry()
still returns false and __alloc_pages_slowpath() proceeds straight to
__alloc_pages_may_oom() instead of retrying the allocation.
[Severity: High]
Should rcu_barrier() be skipped when direct reclaim made progress
(did_some_progress is true and *no_progress_loops is reset to 0), similar
to how commit "memcg, oom: wait for rcu_call callbacks before retrying
reclaim" only calls rcu_barrier() in try_charge_memcg() when nr_reclaimed
is zero?
Calling rcu_barrier() unconditionally on every should_reclaim_retry() pass
serializes all multi-pass direct-reclaim threads on
rcu_state.barrier_mutex, sends cross-CPU IPIs, and waits for a full RCU
grace period even while normal reclaim is actively freeing pages.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202095450.296675-1-mhocko@kernel.org?part=8
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (7 preceding siblings ...)
2026-12-02 9:54 ` [PATCH 08/10] mm, " Michal Hocko
@ 2026-12-02 9:54 ` Michal Hocko
2026-12-02 16:31 ` sashiko-bot
2026-12-02 9:54 ` [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock Michal Hocko
2026-12-02 13:06 ` [PATCH 0/10] assorted oom fixes David Hildenbrand (Arm)
10 siblings, 1 reply; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:54 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Michal Hocko <mhocko@suse.com>
OOM victim could have released quite a lot of memory through rcu_call
path (e.g. page tables). Call rcu_barrier before setting MMF_OOM_SKIP
on the victim to wait for those to execute before the oom killer is
allowed to pick another victim.
Also move MMF_OOM_SKIP in exit_mmap path after page tables are freed
to capture as much memory as possible to be freed. This is safe from
the forward progress POV because oom_reaper will run concurrently in
case the exit path gets stuck. We are going to see less memory freed
by the oom reaper because it cannot tear down page tables directly
but that should be acceptable.
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
mm/mmap.c | 21 +++++++++------------
mm/oom_kill.c | 7 +++++++
2 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/mm/mmap.c b/mm/mmap.c
index 4cb18b217048..87b081f85370 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1313,20 +1313,8 @@ void exit_mmap(struct mm_struct *mm)
/* update_hiwater_rss(mm) here? but nobody should be looking */
/* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */
unmap_vmas(&tlb, &unmap);
-
- /*
- * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM
- * killer will only skip this mm if it's already freed all it can.
- */
- if (mm_is_oom_victim(mm))
- tlb_flush_mmu(&tlb);
mmap_read_unlock(mm);
- /*
- * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper
- * because the memory has been already unmapped.
- */
- mm_flags_set(MMF_OOM_SKIP, mm);
mmap_write_lock(mm);
unmap.mm_wr_locked = true;
mt_clear_in_rcu(&mm->mm_mt);
@@ -1334,6 +1322,15 @@ void exit_mmap(struct mm_struct *mm)
free_pgtables(&tlb, &unmap);
tlb_finish_mmu(&tlb);
+ /*
+ * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM
+ * killer will only skip this mm if it's already freed all it can.
+ */
+ if (mm_is_oom_victim(mm)) {
+ rcu_barrier();
+ mm_flags_set(MMF_OOM_SKIP, mm);
+ }
+
/*
* Walk the list again, actually closing and freeing it, with preemption
* enabled, without holding any MM locks besides the unreachable
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 6ff1e93ec6e3..e0c2e3e647aa 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -628,6 +628,13 @@ static void oom_reap_task(struct task_struct *tsk)
done:
tsk->oom_reaper_list = NULL;
+ /*
+ * Make sure that pre-exising call_rcu users will get a chance to
+ * free up memory. This might have been exiting task itself as well
+ * so call this before we hide the current victim from the oom killer.
+ */
+ rcu_barrier();
+
/*
* Hide this mm from OOM killer because it has been either reaped or
* somebody can't call mmap_write_unlock(mm).
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer
2026-12-02 9:54 ` [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer Michal Hocko
@ 2026-12-02 16:31 ` sashiko-bot
0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-02 16:31 UTC (permalink / raw)
To: Michal Hocko, David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, Chris Mason
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm: exit_mmap() delays tlb flush and MMF_OOM_SKIP across
free_pgtables()
--
commit 1c8250a513b4c920254c3d354feba98a795ac23c
Author: Michal Hocko <mhocko@suse.com>
mm, oom: add rcu_barrier before hiding victim from the oom killer
This commit adds rcu_barrier() before setting MMF_OOM_SKIP in
oom_reap_task() and exit_mmap(), and moves the MMF_OOM_SKIP update in
exit_mmap() after free_pgtables() and tlb_finish_mmu().
> diff --git a/mm/mmap.c b/mm/mmap.c
> index ec55ccbb88055..8974405faf490 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1317,20 +1317,8 @@ void exit_mmap(struct mm_struct *mm)
> /* update_hiwater_rss(mm) here? but nobody should be looking */
> /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */
> unmap_vmas(&tlb, &unmap);
> -
> - /*
> - * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM
> - * killer will only skip this mm if it's already freed all it can.
> - */
> - if (mm_is_oom_victim(mm))
> - tlb_flush_mmu(&tlb);
> mmap_read_unlock(mm);
>
> - /*
> - * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper
> - * because the memory has been already unmapped.
> - */
> - mm_flags_set(MMF_OOM_SKIP, mm);
> mmap_write_lock(mm);
> unmap.mm_wr_locked = true;
> mt_clear_in_rcu(&mm->mm_mt);
> @@ -1338,6 +1326,15 @@ void exit_mmap(struct mm_struct *mm)
> free_pgtables(&tlb, &unmap);
> tlb_finish_mmu(&tlb);
>
> + /*
> + * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM
> + * killer will only skip this mm if it's already freed all it can.
> + */
> + if (mm_is_oom_victim(mm)) {
> + rcu_barrier();
> + mm_flags_set(MMF_OOM_SKIP, mm);
> + }
[Severity: High]
Does removing tlb_flush_mmu(&tlb) after unmap_vmas() and delaying
MMF_OOM_SKIP across free_pgtables() leave gathered data folios unfreed
while blocking the OOM reaper on mmap_write_lock(mm)?
unmap_vmas() clears the PTEs and leaves up to MAX_GATHER_BATCH_COUNT
batches of unmapped data pages queued in exit_mmap()'s on-stack
struct mmu_gather tlb until tlb_finish_mmu(). Once the PTEs are cleared,
a concurrent oom_reap_task_mm() cannot reclaim those pages from the page
tables either.
free_pgtables() then runs under mmap_write_lock(mm) and takes sleeping VMA
and rmap locks (vma_start_write(), anon_vma->root->rwsem in
unlink_anon_vmas(), and mapping->i_mmap_rwsem in
unlink_file_vma_batch_final()), followed by rcu_barrier() still under
mmap_write_lock(mm).
While exit_mmap() is blocked in free_pgtables() or rcu_barrier() holding
mmap_write_lock(mm), oom_reap_task_mm() fails mmap_read_trylock(mm). After
MAX_OOM_REAP_RETRIES, oom_reap_task() sees MMF_OOM_SKIP still clear, emits
false "oom_reaper: unable to reap pid" warnings and lock dumps (regressing
97b1255cb27c ("mm,oom_reaper: check for MMF_OOM_SKIP before complaining")),
and sets MMF_OOM_SKIP while the gathered data pages in tlb are still
unfreed (regressing a35b6312255d ("mm/mmap: free gathered pages before
MMF_OOM_SKIP")).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202095450.296675-1-mhocko@kernel.org?part=9
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (8 preceding siblings ...)
2026-12-02 9:54 ` [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer Michal Hocko
@ 2026-12-02 9:54 ` Michal Hocko
2026-12-02 13:06 ` [PATCH 0/10] assorted oom fixes David Hildenbrand (Arm)
10 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 9:54 UTC (permalink / raw)
To: David Hildenbrand
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML, Chris Mason
From: Chris Mason <mason@kernel.org>
With the OOM killer disabled in a cgroup v1 memcg, a lost wakeup can
leave a task that contends for the memcg OOM lock asleep indefinitely in
mem_cgroup_oom_synchronize(), with no OOM notification sent for it.
Only the task that wins mem_cgroup_oom_trylock() notifies userspace. A
task that loses the trylock sleeps without a notification of its own, so
the holder has to wake it when the lock is dropped. Commit 18b1d18bc2bd
("memcg, oom: remove explicit wakeup in mem_cgroup_oom_synchronize()")
removed that wakeup, because userspace triggers one. But userspace
answers the holder's notification, and its wakeup only reaches the tasks
that are on memcg_oom_waitq at that time:
holder A contender B
======== ===========
mem_cgroup_oom_trylock()
mem_cgroup_oom_notify()
schedule()
try_charge_memcg() fails
woken by memcg1_oom_recover()
prepare_to_wait()
mem_cgroup_oom_trylock() fails
schedule()
mem_cgroup_oom_unlock()
The lock covers a subtree, so the holder can also be at an ancestor of
the contender's memcg: another waiter, or the OOM killer in the charge
path, where memcg1_oom_finish() drops the lock without a wakeup as well.
The contender can then sleep on while its memcg is still at the limit,
again without a notification of its own.
The fix used here is to wake the contenders from the unlock itself, so
that every path that drops the memcg OOM lock wakes them.
Userspace can get one more OOM notification after the holder has left,
if the memcg is still at its limit. An OOM kill in the charge path that
held the lock now also walks memcg_oom_waitq, which holds the waiters of
all memcgs, and is empty on cgroup v2.
Fixes: 18b1d18bc2bd ("memcg, oom: remove explicit wakeup in mem_cgroup_oom_synchronize()")
Signed-off-by: Chris Mason <mason@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/memcontrol-v1.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index f2391eea4e0d..bb850d1f0092 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -81,6 +81,7 @@ static struct lockdep_map memcg_oom_lock_dep_map = {
#endif
DEFINE_SPINLOCK(memcg_oom_lock);
+static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
struct cftype *cft)
@@ -958,6 +959,12 @@ static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
for_each_mem_cgroup_tree(iter, memcg)
iter->oom_lock = false;
spin_unlock(&memcg_oom_lock);
+
+ /*
+ * A task that lost the trylock sleeps without a notification of its
+ * own, so nothing but the unlock is sure to wake it.
+ */
+ __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
}
static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
@@ -985,8 +992,6 @@ static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
spin_unlock(&memcg_oom_lock);
}
-static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
-
struct oom_wait_info {
struct mem_cgroup *memcg;
wait_queue_entry_t wait;
--
2.56.1
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 0/10] assorted oom fixes
2026-12-02 9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
` (9 preceding siblings ...)
2026-12-02 9:54 ` [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock Michal Hocko
@ 2026-12-02 13:06 ` David Hildenbrand (Arm)
2026-12-02 19:01 ` Michal Hocko
10 siblings, 1 reply; 21+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-02 13:06 UTC (permalink / raw)
To: Michal Hocko
Cc: David Rientjes, Ben Gardon, Johannes Weiner, mhocko,
Andrew Morton, linux-mm, LKML
On 12/2/26 10:53, Michal Hocko wrote:
> Hi David,
> these are assorted oom related fixes. There is nothing super critical in
> there but the patch1 is using a potential UAF for oom_disabled (v1 only
> feature) situations so I've marked it for stable explicitly.
> All these are based on Chris Mason's reports so thanks for those!
Thanks, likely best for all to go through Andrew's tree. Unless this is 7.6
stuff that you could take through the memcg tree?
Thanks!
--
Cheers,
David
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 0/10] assorted oom fixes
2026-12-02 13:06 ` [PATCH 0/10] assorted oom fixes David Hildenbrand (Arm)
@ 2026-12-02 19:01 ` Michal Hocko
0 siblings, 0 replies; 21+ messages in thread
From: Michal Hocko @ 2026-12-02 19:01 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: David Rientjes, Ben Gardon, Johannes Weiner, Andrew Morton,
linux-mm, LKML
On Wed 02-12-26 14:06:16, David Hildenbrand wrote:
> On 12/2/26 10:53, Michal Hocko wrote:
> > Hi David,
> > these are assorted oom related fixes. There is nothing super critical in
> > there but the patch1 is using a potential UAF for oom_disabled (v1 only
> > feature) situations so I've marked it for stable explicitly.
> > All these are based on Chris Mason's reports so thanks for those!
>
> Thanks, likely best for all to go through Andrew's tree. Unless this is 7.6
> stuff that you could take through the memcg tree?
I am fine both ways. If we should wait for the next release cycle then
take patch 1 at least. That one doesn't have any dependencies so it can
go through either your or Andrew's tree.
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 21+ messages in thread