* [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
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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 9:53 ` [PATCH 06/10] mm, oom: check oom victims prior to numa placement Michal Hocko
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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 9:54 ` [PATCH 08/10] mm, " Michal Hocko
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ 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] 11+ 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 9:54 ` [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer Michal Hocko
2026-12-02 9:54 ` [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock Michal Hocko
9 siblings, 0 replies; 11+ 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] 11+ 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 9:54 ` [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock Michal Hocko
9 siblings, 0 replies; 11+ 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] 11+ 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
9 siblings, 0 replies; 11+ 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] 11+ messages in thread