Re: [PATCH 02/13] mm: extract ensure_on_mmlist() helper

From: David Hildenbrand (Arm)

Date: Wed May 13 2026 - 09:57:57 EST


On 4/27/26 12:01, Usama Arif wrote:
> When a swap entry is installed in a page table, the mm must be added
> to init_mm.mmlist so that swapoff can find and unuse its swap entries.
> This double-checked locking pattern is currently open-coded in
> try_to_unmap_one() and copy_nonpresent_pte().
>
> Move it into ensure_on_mmlist() in mm/internal.h and convert both
> callers so it can be reused by upcoming PMD-level swap entry code
> paths that also need to register the mm with swapoff.
>
> copy_nonpresent_pte() previously inserted into &src_mm->mmlist rather
> than &init_mm.mmlist, but the insertion point is irrelevant, mmlist
> is a circular list and swapoff walks it entirely from init_mm.mmlist,
> so only membership matters, not position.
>
> Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> ---
> mm/internal.h | 13 +++++++++++++
> mm/memory.c | 9 +--------
> mm/rmap.c | 7 +------
> 3 files changed, 15 insertions(+), 14 deletions(-)
>
> diff --git a/mm/internal.h b/mm/internal.h
> index 5a2ddcf68e0b..7de489689f54 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -1952,4 +1952,17 @@ static inline int get_sysctl_max_map_count(void)
> bool may_expand_vm(struct mm_struct *mm, const vma_flags_t *vma_flags,
> unsigned long npages);
>
> +/*
> + * Ensure @mm is on the init_mm.mmlist so swapoff can find it.
> + */
> +static inline void ensure_on_mmlist(struct mm_struct *mm)
> +{
> + if (list_empty(&mm->mmlist)) {
> + spin_lock(&mmlist_lock);
> + if (list_empty(&mm->mmlist))
> + list_add(&mm->mmlist, &init_mm.mmlist);
> + spin_unlock(&mmlist_lock);
> + }
> +}

Instead of talking about the low level detail ("add to mmlist"), maybe we could
just talk about the high-level goal: make sure that the MM can hold swap entries.


mm_prepare_for_swap()

or sth like that?

--
Cheers,

David