From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 748872E7386 for ; Fri, 29 May 2026 07:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780040578; cv=none; b=L6AdNKr+WrIxKlRJKXAQopb/aeolCycbKyNxsN+AnJHUwD6c/pRzqgjXbvAtecSUimtB8mZpHueJxqiaYvmUa3Wl2kigkJeoOPFb5c4/Tpgo/ytTVpcF/ZJEtfE+Luqu/QIvF1lu1j4tuFIKm/0ZiSmFOKxaGLFkAfasFL6+LKY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780040578; c=relaxed/simple; bh=+3OoqitVlf6o9VTWbqjxpqBUxDY7YVKYEPVJKQIbZR4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dZ5x0TPHANg6/oDKdNHmzFIKatV1bNg5+SrDNdKfgkzwPD/puKdh3KSIB/grITwIJ/00/uT+q4iiE8Cp11BJmR/SVJ2H4fhrY4p9Kz7/pEEo54QecEIsIrkWt46fx6+d5VVUDHD/Lko2r67fubrN5NFbJW1xOhClfQxpEYuRgbM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=K293l0pA; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="K293l0pA" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AFB351688; Fri, 29 May 2026 00:42:50 -0700 (PDT) Received: from [10.164.19.8] (unknown [10.164.19.8]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CA10C3F905; Fri, 29 May 2026 00:42:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1780040575; bh=+3OoqitVlf6o9VTWbqjxpqBUxDY7YVKYEPVJKQIbZR4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=K293l0pAqE1I+QEdFqjyFcgqZuS+dRdFxPFwD+Vcy6nmDu3NgFpSNCxj7ug9Mv7DG 3Awb48ANtvpHZ+jM3g9rJUCDPKO0JfYDG+mksntpuUO1q1hcNBrsIAVXn6APphjxZq DGpb+EGupYUhPkZPAR/c6f48okrsn/fSRmydH1eg= Message-ID: <392dc0b2-8c00-4fc9-b6e3-055c87e58944@arm.com> Date: Fri, 29 May 2026 13:12:44 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 02/13] mm: extract ensure_on_mmlist() helper To: Usama Arif , Andrew Morton , david@kernel.org, chrisl@kernel.org, kasong@tencent.com, ljs@kernel.org, ziy@nvidia.com Cc: bhe@redhat.com, willy@infradead.org, youngjun.park@lge.com, hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, alex@ghiti.fr, kas@kernel.org, baohua@kernel.org, baolin.wang@linux.alibaba.com, npache@redhat.com, Liam.Howlett@oracle.com, ryan.roberts@arm.com, Vlastimil Babka , lance.yang@linux.dev, linux-kernel@vger.kernel.org, nphamcs@gmail.com, shikemeng@huaweicloud.com, kernel-team@meta.com References: <20260427100553.2754667-1-usama.arif@linux.dev> <20260427100553.2754667-3-usama.arif@linux.dev> Content-Language: en-US From: Dev Jain In-Reply-To: <20260427100553.2754667-3-usama.arif@linux.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 27/04/26 3:31 pm, 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 > --- Agree with David on the name. The unlikely hint is dropped from the fork path. I don't think we care. Reviewed-by: Dev Jain > 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); > + } > +} > + > #endif /* __MM_INTERNAL_H */ > diff --git a/mm/memory.c b/mm/memory.c > index ea6568571131..33d7cc274e23 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -937,14 +937,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, > if (swap_dup_entry_direct(entry) < 0) > return -EIO; > > - /* make sure dst_mm is on swapoff's mmlist. */ > - if (unlikely(list_empty(&dst_mm->mmlist))) { > - spin_lock(&mmlist_lock); > - if (list_empty(&dst_mm->mmlist)) > - list_add(&dst_mm->mmlist, > - &src_mm->mmlist); > - spin_unlock(&mmlist_lock); > - } > + ensure_on_mmlist(dst_mm); > /* Mark the swap entry as shared. */ > if (pte_swp_exclusive(orig_pte)) { > pte = pte_swp_clear_exclusive(orig_pte); > diff --git a/mm/rmap.c b/mm/rmap.c > index 78b7fb5f367c..057e18cb80b0 100644 > --- a/mm/rmap.c > +++ b/mm/rmap.c > @@ -2302,12 +2302,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, > set_pte_at(mm, address, pvmw.pte, pteval); > goto walk_abort; > } > - 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); > - } > + ensure_on_mmlist(mm); > dec_mm_counter(mm, MM_ANONPAGES); > inc_mm_counter(mm, MM_SWAPENTS); > swp_pte = swp_entry_to_pte(entry);