Re: [PATCH v2 2/6] mm: extract mm_prepare_for_swap_entries() helper
From: Kiryl Shutsemau
Date: Mon Jul 06 2026 - 09:14:54 EST
On Mon, Jul 06, 2026 at 01:23:29PM +0100, Usama Arif wrote:
>
>
> On 06/07/2026 13:01, Kiryl Shutsemau wrote:
> > On Mon, Jul 06, 2026 at 04:42:04AM -0700, 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>
> >> Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> >> Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> >> ---
> >> mm/internal.h | 10 ++++++++++
> >> mm/memory.c | 9 +--------
> >> mm/rmap.c | 7 +------
> >> 3 files changed, 12 insertions(+), 14 deletions(-)
> >>
> >> diff --git a/mm/internal.h b/mm/internal.h
> >> index 430aa72a4575..0360648e5726 100644
> >> --- a/mm/internal.h
> >> +++ b/mm/internal.h
> >> @@ -1955,4 +1955,14 @@ 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);
> >>
> >> +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);
> >
> > The patch is fine, but the name of the field is awful. Seeing mmlist I
> > assumed it listed all mm_structs. It has to have "swap" somewhere in the
> > field name.
> >
>
> I can send a separate patch once this is replaced to replace mmlist with
> swap_mmlist if that sounds better?
Yep, better.
--
Kiryl Shutsemau / Kirill A. Shutemov