The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Michal Hocko <mhocko@kernel.org>
Cc: David Hildenbrand <david@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>,
	David Rientjes <rientjes@google.com>,
	 Ben Gardon <bgardon@google.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	linux-mm@kvack.org,  LKML <linux-kernel@vger.kernel.org>,
	Michal Hocko <mhocko@suse.com>, Chris Mason <mason@kernel.org>
Subject: Re: [PATCH v3] mm, oom: move MMF_OOM_SKIP after page table releasing in exit_mmap
Date: Fri, 4 Dec 2026 09:26:56 +0000	[thread overview]
Message-ID: <axKEnjvt0jROIfTu@gremlin> (raw)
In-Reply-To: <20261204090827.568530-1-mhocko@kernel.org>

On Fri, Dec 04, 2026 at 10:08:27AM +0100, Michal Hocko wrote:
> From: Michal Hocko <mhocko@suse.com>
>
> OOM victim could have consumed quite a lot of memory through page tables
> and those are currently released after we set MMF_OOM_SKIP in the
> exit_mmap path. If the memory consumed through the address space is not
> sufficient to recover from the OOM (e.g. most of it is shared memory
> etc.) then we could go after another oom victim even though page tables
> could have released enough memory to prevent from that.
>
> Move MMF_OOM_SKIP in exit_mmap path after page tables freeing to capture
> as much memory as possible to be released. We need to wait for RCU
> callbacks to see that memory freed.
>
> In order to prevent oom reaper or process_mrelease to stumble over freed
> page tables they are checking MMF_OOM_SKIP under mmap_lock for read
> which is synchronized with MMF_OOM_SKIP set under exclusive lock after
> page tables are freed.
>
> The move is safe from the forward progress POV too because oom_reaper
> will run concurrently in case the exit path gets stuck and also if RCU
> callbacks are not making forward progress for whatever reason and it
> will bail out by setting MMF_OOM_SKIP. Up until exit_mmap takes the
> exclusive mmap_lock they will cooperate in releasing the address space.
> 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.
> Please note that the reaper execution is delayed so the freeing should
> be typically done the exit path.
>
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Michal Hocko <mhocko@suse.com>

Nice, lovely to see my RCU page table work coming in useful here :))

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

> ---
>  mm/mmap.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
>
> Changes
> since v1
> 	- restore tlb_flush_mmu after unmap_vmas
> since v2
> 	- dropped rcu_barrier from the oom_reaper path as there might be
> 	  an obscure case when RCU grace period might allocate
> 	  https://lore.kernel.org/all/ed6c5f64-7afe-45a2-8d18-e4e1c0052840@paulmck-laptop/T/#u
> 	- set MMF_OOM_SKIP unconditionally in exit_mmap path
> 	- reworded the changelog to better describe the actual problem
> 	  and explain the coordination and synchronization between exit,
> 	  process_mrelease and oom_reaper
>
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 4cb18b217048..eeb39eaeb23a 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1314,19 +1314,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);
> @@ -1334,6 +1326,14 @@ 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
> --
> 2.56.1
>

--
Cheers, Lorenzo

  reply	other threads:[~2026-12-04  9:27 UTC|newest]

Thread overview: 28+ 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  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 19:22   ` Harry Yoo
2026-12-02 19:33     ` Michal Hocko
2026-12-02  9:54 ` [PATCH 08/10] mm, " Michal Hocko
2026-12-04  8:39   ` 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 21:21   ` [PATCH v2] " Michal Hocko
2026-12-03 12:24     ` Lorenzo Stoakes (ARM)
2026-12-03 17:39       ` Paul E. McKenney
2026-12-03 18:11         ` Michal Hocko
2026-12-03 20:02           ` Paul E. McKenney
2026-12-03 20:42             ` Michal Hocko
2026-12-03 21:36               ` Paul E. McKenney
2026-12-04  8:24                 ` Michal Hocko
2026-12-04  9:08     ` [PATCH v3] mm, oom: move MMF_OOM_SKIP after page table releasing in exit_mmap Michal Hocko
2026-12-04  9:26       ` Lorenzo Stoakes (ARM) [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=axKEnjvt0jROIfTu@gremlin \
    --to=ljs@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@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