Re: [PATCH v3] mm, oom: move MMF_OOM_SKIP after page table releasing in exit_mmap

From: Lorenzo Stoakes (ARM)

Date: Fri Dec 04 2026 - 04:27:06 EST


On Fri, Dec 04, 2026 at 10:08:27AM +0100, Michal Hocko wrote:
> From: Michal Hocko <mhocko@xxxxxxxx>
>
> 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@xxxxxxxxxx>
> Signed-off-by: Michal Hocko <mhocko@xxxxxxxx>

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

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>

> ---
> 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