From: Michal Hocko <mhocko@suse.com>
To: sashiko-reviews@lists.linux.dev
Cc: David Hildenbrand <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
"Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
David Rientjes <rientjes@google.com>,
Ben Gardon <bgardon@google.com>,
Johannes Weiner <hannes@cmpxchg.org>,
linux-mm@kvack.org, Chris Mason <mason@kernel.org>
Subject: Re: [PATCH v2] mm, oom: add rcu_barrier before hiding victim from the oom killer
Date: Thu, 3 Dec 2026 09:46:05 +0100 [thread overview]
Message-ID: <axEsTXWnUA9cwArC@tiehlicka> (raw)
In-Reply-To: <sashiko-outbox-246678@kernel.org>
On Wed 02-12-26 22:08:16, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] mm: delaying MMF_OOM_SKIP in exit_mmap() breaks oom_reaper
> synchronization
> - [Medium] mm, oom: redundant rcu_barrier() in exit_mmap() and
> oom_reap_task()
> --
>
> commit d8b9a20b6dcaba9ce0a826a457c923a1fe4ea9c1
> Author: Michal Hocko <mhocko@suse.com>
>
> mm, oom: add rcu_barrier before hiding victim from the oom killer
>
> This commit calls rcu_barrier() before setting MMF_OOM_SKIP in
> oom_reap_task() and exit_mmap(), and moves setting MMF_OOM_SKIP in
> exit_mmap() after free_pgtables() under mm_is_oom_victim(mm).
>
> > diff --git a/mm/mmap.c b/mm/mmap.c
> > index ec55ccbb88055..deec8f3d20d05 100644
> > --- a/mm/mmap.c
> > +++ b/mm/mmap.c
> > @@ -1318,19 +1318,11 @@ void exit_mmap(struct mm_struct *mm)
> > /* 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.
> > - */
> > + /* Free batched pages during unmap_vmas ASAP for oom victims */
> > 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 +1330,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: Medium]
> Does delaying MMF_OOM_SKIP until after free_pgtables() and rcu_barrier(),
> and gating it on mm_is_oom_victim(mm), break exit_mmap() synchronization
> with oom_reap_task() and process_mrelease()?
>
> oom_reap_task_mm() and oom_reap_task() rely on exit_mmap() setting
> MMF_OOM_SKIP before acquiring mmap_write_lock(mm):
>
> mm/oom_kill.c:oom_reap_task_mm() {
> ...
> /*
> * MMF_OOM_SKIP is set by exit_mmap when the OOM reaper can't
> * work on the mm anymore. The check for MMF_OOM_SKIP must run
> * under mmap_lock for reading because it serializes against the
> * mmap_write_lock();mmap_write_unlock() cycle in exit_mmap().
> */
> if (mm_flags_test(MMF_OOM_SKIP, mm)) {
> trace_skip_task_reaping(tsk->pid);
> goto out_unlock;
> }
> ...
> }
>
> With MMF_OOM_SKIP moved after free_pgtables() and rcu_barrier(),
> exit_mmap() holds mmap_write_lock(mm) across sleeping anon_vma, i_mmap
> and RCU barrier waits while MMF_OOM_SKIP is still clear, even though
> unmap_vmas() and tlb_flush_mmu() have already freed the victim's
> anonymous pages.
Correct
> If oom_reap_task() runs during this phase,
> mmap_read_trylock(mm) fails repeatedly and oom_reap_task() finds
> MMF_OOM_SKIP unset after MAX_OOM_REAP_RETRIES, emitting false "unable to
> reap" warnings, task dumps, and debug_show_all_locks() output.
Right. This is likely unnecessary but I would consider it mostly
harmless. debug_show_all_locks is nop without CONFIG_LOCKDEP which is
the case for most production workloads. Dumping the victims stack
is more noisy than necessary but I would rather wait for this being a
real problem than developing a specific solution for that.
> Leaving MMF_OOM_SKIP unset between mmap_read_unlock(mm) and
> mmap_write_lock(mm) also lets oom_reap_task_mm() grab mmap_read_lock(mm)
> in that window, walk already-unmapped VMAs while blocking exit_mmap()
> from starting free_pgtables(), and set MMF_OOM_SKIP in oom_reap_task()
> before page tables are freed.
Yes. We cannot have both. oom_reaper is delayed to allow normal victim
to finish properly before meddling with it.
> In addition, gating mm_flags_set(MMF_OOM_SKIP, mm) on
> mm_is_oom_victim(mm) means processes killed from userspace never set
> MMF_OOM_SKIP in exit_mmap(), breaking the MMF_OOM_SKIP checks in
> process_mrelease().
diff --git a/mm/mmap.c b/mm/mmap.c
index 0b19c720473a..eeb39eaeb23a 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1330,10 +1330,9 @@ void exit_mmap(struct mm_struct *mm)
* 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)) {
+ if (mm_is_oom_victim(mm))
rcu_barrier();
- mm_flags_set(MMF_OOM_SKIP, mm);
- }
+ mm_flags_set(MMF_OOM_SKIP, mm);
/*
* Walk the list again, actually closing and freeing it, with preemption
> > diff --git a/mm/oom_kill.c b/mm/oom_kill.c
> > index 3fb70495f0826..6ff2650e1d58c 100644
> > --- a/mm/oom_kill.c
> > +++ b/mm/oom_kill.c
> > @@ -614,6 +614,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).
>
> [Severity: Medium]
> Can oom_reap_task() and exit_mmap() both run a redundant rcu_barrier() on
> the same victim when MMF_OOM_SKIP is already set?
They can. Unless I miss something I do not see that as necessarily wrong
thing to do. On the oom_reaper side of things this could slow down
operation unnecessarily but this is not a fast path and it should
provide a way to ensure we do not allow oom killer to go after another
victim while there is memory being freed through rcu.
On the exit_mmap side we are flushing after the victims active
contribution to the rcu freed data structures from the page table
freeing.
So I would say prefer a simpler solution for now.
Let's see whether there is a general agreement with those decisions
before I send v3.
--
Michal Hocko
SUSE Labs
next prev parent reply other threads:[~2026-12-03 8:46 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
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 20:21 ` Joshua Hahn
2026-12-02 21:28 ` 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 ` [PATCH 03/10] mm, oom: do not panic on constrained OOMs Michal Hocko
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 ` [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
2026-12-02 9:53 ` [PATCH 06/10] mm, oom: check oom victims prior to numa placement Michal Hocko
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
2026-12-02 19:33 ` Michal Hocko
2026-12-02 9:54 ` [PATCH 08/10] mm, " Michal Hocko
2026-12-02 16:31 ` sashiko-bot
2026-12-02 19:44 ` 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 16:31 ` sashiko-bot
2026-12-02 19:53 ` Michal Hocko
2026-12-02 19:57 ` Lorenzo Stoakes (ARM)
2026-12-02 21:21 ` [PATCH v2] " Michal Hocko
2026-12-02 22:08 ` sashiko-bot
2026-12-03 8:46 ` Michal Hocko [this message]
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)
2026-12-02 19:01 ` Michal Hocko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=axEsTXWnUA9cwArC@tiehlicka \
--to=mhocko@suse.com \
--cc=akpm@linux-foundation.org \
--cc=bgardon@google.com \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mason@kernel.org \
--cc=rientjes@google.com \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox