Re: [PATCH 2/6] mm: extract mm_prepare_for_swap_entries() helper

From: David Hildenbrand (Arm)

Date: Wed Jul 01 2026 - 13:03:30 EST


On 6/30/26 18:34, 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 mm_prepare_for_swap_entries() 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.
>
> Reviewed-by: Dev Jain <dev.jain@xxxxxxx>
> Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
> 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 8e0df3d1c6f9..0e3db9451e5c 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -1956,4 +1956,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.
> + */

Comment fits into a single line and I don't think we need it (function name is
self-explaining enough).

> +static inline void mm_prepare_for_swap_entries(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);
> + }
> +}
> +

Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

--
Cheers,

David