mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@kernel.org>
To: David Hildenbrand <david@kernel.org>
Cc: David Rientjes <rientjes@google.com>,
	Ben Gardon <bgardon@google.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	mhocko@suse.com, Andrew Morton <akpm@linux-foundation.org>,
	<linux-mm@kvack.org>, LKML <linux-kernel@vger.kernel.org>,
	Chris Mason <mason@kernel.org>
Subject: [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer
Date: Wed,  2 Dec 2026 10:54:02 +0100	[thread overview]
Message-ID: <20261202095450.296675-10-mhocko@kernel.org> (raw)
In-Reply-To: <20261202095450.296675-1-mhocko@kernel.org>

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


  parent reply	other threads:[~2026-12-02  9:55 UTC|newest]

Thread overview: 12+ 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  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  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  9:54 ` [PATCH 08/10] mm, " Michal Hocko
2026-12-02  9:54 ` 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)

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=20261202095450.296675-10-mhocko@kernel.org \
    --to=mhocko@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bgardon@google.com \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    /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

all inboxes | Powered by JetHome®