From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CFB2C3ABDA for ; Thu, 15 May 2025 01:38:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC7EB6B00DB; Wed, 14 May 2025 21:38:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A771F6B00DD; Wed, 14 May 2025 21:38:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EF936B00DF; Wed, 14 May 2025 21:38:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6BDB16B00DB for ; Wed, 14 May 2025 21:38:28 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 910C21A13F2 for ; Thu, 15 May 2025 01:38:28 +0000 (UTC) X-FDA: 83443432296.26.23FB8DE Received: from mail-vk1-f173.google.com (mail-vk1-f173.google.com [209.85.221.173]) by imf13.hostedemail.com (Postfix) with ESMTP id B95A220002 for ; Thu, 15 May 2025 01:38:26 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="RGLx/ti2"; spf=pass (imf13.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.173 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747273106; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=oYMghdxO9pFq0+ESkScJLJY1d2a+Vc4kdlZhuynEBPc=; b=qO6NiCHNb4wg0uDivlaKlZK8VnCN4STBG9avlpaeDxrjF3PiKCYjEQ2T8KFXUVsxU13txn oqHnQbGRK1ivrF7D9xm/zSPTRBSY/bmwBCJs8kc77995nh7NgrudzU5W2YdbjXPzzgX+0+ pqsi/KVWwMbQytJJZP2L6J7mPp6DtjA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="RGLx/ti2"; spf=pass (imf13.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.173 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747273106; a=rsa-sha256; cv=none; b=izcUlbStZi/6rMYhKDlnZAwnYelRFTMvFiw0l6Yent3SROJDTnz75vgEOQH192w9PRobFW I+4yN3kxrAYKqimpTK9xyV/Kym6iKO2QvF9390s1FeCAlzvY13phgc4EUbaZ9EVrILEa7n T0NAHsi+xJzbHM7yzVZ8966DaIZXBxY= Received: by mail-vk1-f173.google.com with SMTP id 71dfb90a1353d-52c6f67c6c5so122067e0c.2 for ; Wed, 14 May 2025 18:38:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747273106; x=1747877906; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=oYMghdxO9pFq0+ESkScJLJY1d2a+Vc4kdlZhuynEBPc=; b=RGLx/ti2QIRLmOBDe7gwKVJdmuTyqhn3svNZ7TBCtTpYmOhVA051cq8nH6hqtyAEX/ Re5ihoBKZAtQJl0mRg4e93rrXLzCwomAZxUzsyLA6heMHJae4x6GfNcqdPGKJJSb5GJz RFr887RKlU/dNzB72TU2xaXfv/+4X1MAz5mbWr74O4cptwWBjcaJeIc1caoJt7ID1XiL 4+9c+K+FdNS3AkD7zrwJFaRXpHC5xEBIa8TEMZWBG5kgAd3JvqHRLs5hu5W/c5LjiZJ7 RA0Ybamrl0nMrvTSF1d0CnQmWzRPAYFSt9Jnp3CylyN+9T1OemBffl143g7y8MpXlwK5 3jhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747273106; x=1747877906; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oYMghdxO9pFq0+ESkScJLJY1d2a+Vc4kdlZhuynEBPc=; b=J5ABUIBHFeGu6EmpDb2LnmYARsNgS3+zDOY12WmzaZ7Y/3A217J5rKV99ArLkm4X9m 8YVPsPPkYdBTUwcrjAKFXuQZjxnydwtjRQsBtddUMVByHdQXIdBLE+WrTjSHQY+ZhdLm 7ggagM3LC+2e9yHA5zpgH7JXCYlr66Ce80Kecdw82IuSaO2Lpe8hd1Oir9n1kw6OKgVT z7PvRFCko2rWIhWaqGjh08LCuRRomorjJV+Q0vG3lpYe/gOC0KvnrcuvuHL4PRbri3dM aZRO5w/AdwUmMXY4ndB0+KyW6DkwQtesWBVBjMeLJndeAPdLrMYNhnfSVwxlm1PukZTH DlzA== X-Forwarded-Encrypted: i=1; AJvYcCWmEb+cDp2AdyZjPJ/lot96gFvtlGixEg68ds67fmAh/67fK7XEcCkt4cy8htxk6w/+CZ5cvNaaiw==@kvack.org X-Gm-Message-State: AOJu0YwNzlpXctSZapzQ4Rzv54OscUN6Lvke5lFE5rZkqYqtj1UHzNBQ 2LKO0FcovcJr6mHyDilL+2ICvGRDQzOx+J1RBP+Kvb+qr8lP0zBaGZ+OK1wAWCHP3qhedTHHDSn Nh3ziwy6Q6ncVTqKjA5xGAiV5qkw= X-Gm-Gg: ASbGncsMCx4TGOLWKvQRvw+yp/X0IUkkcAjxHfhBn0r0ts9jCyaVZnhLlpr5NeJ2Dd3 r3TQHY75GIpeh+RrJbab97ApVmHjx2Bb43YW3uxgnKeEBbXmFjtSdQo74RqVqx2ENZP5YrHyh27 UfKegwhgzbHHSHpQrzTFuZxHFFR4fxmQn95w== X-Google-Smtp-Source: AGHT+IF/5qSRWeZ3Cq+D1z5u3w9zBV9BRyfNKTGRiJC97bcFdw12ub0R7bzxUi1Naw1SnMBz819Mu6bYRkxRaA27wHg= X-Received: by 2002:a05:6122:912:b0:520:5e9b:49b2 with SMTP id 71dfb90a1353d-52dabcd46a3mr622270e0c.3.1747273105778; Wed, 14 May 2025 18:38:25 -0700 (PDT) MIME-Version: 1.0 References: <20250513084620.58231-1-21cnbao@gmail.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Thu, 15 May 2025 13:38:14 +1200 X-Gm-Features: AX0GCFtN6iaPtZGxKGN96XY6BsSFYzlmAT7eZ7Y0tPtY-N9M43BI4GSzm0TdzGE Message-ID: Subject: Re: [PATCH RFC] mm: make try_to_unmap_one support batched unmap for anon large folios To: Dev Jain Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , David Hildenbrand , Baolin Wang , Ryan Roberts , Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Rik van Riel , Harry Yoo , Kairui Song , Chris Li , Baoquan He , Dan Schatzberg , Kaixiong Yu , Fan Ni , Tangquan Zheng Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: B95A220002 X-Rspamd-Server: rspam09 X-Stat-Signature: yti3zgtuopuquk1qz3m5cdzfitgipdoy X-HE-Tag: 1747273106-50055 X-HE-Meta: U2FsdGVkX1/xewTq8mjYWuH92QJ4ugESLZSgGjx4wL6SoVFqWtC6ZwlLHu6sP+CuDumdwycJaAy6uqwxbGisphqOLbTw6tO/XR8hxKAd3bTg6EcYXL094xC0QGLsAxAd2yS+IToXVncEis8E7cgVM/Av2OYsaxJUDEVvj3miin7TFS6b56EUtVB5aQWF5uNpX6LrGCcJwdl8YhDreCzzJ48MzZvPiq4/5odIWuZCz7HmX3f3vreB1Iq4BD7SeVex5Ie1PGn2KawTxoVZCF/PMMsCDzVZNFOFl8EpUWIB5t5v7OAiDyCyPWcGNg8R3IvC5G73/yQKFRDypcxgdUMqI/36PyAUTN2lKItKiItWp+xnOOFhGiQPQN3EUFqIVvAk9qFFE0Zw7QzZ05IVyw6EtxEniNyTTs5HZYk2UgHHQaDx7e6RHhmHkbNgmt5DQfyj2tPjBT/k7/xUVITU7CgQ9hiufx7iF7FNp5zUIP/spwvSKXnR4ny6gGnOt54HzoIEtN9QGL8ms+IuFq6hgtdwUGi7L1b3AaM7764Lug7XdvfQgKO3FliMWQurL9zNG0S5jCoyvEVSEQijPt9ZyOgREyP52va2novsx8M5eOeMafTNY689sIxxjg0ZTE6DELNAKx07eTd68bgc8zt3uDEhT6sGhCoIMNovJVLW4ck0hIcMurcjeXSh/bOZu7u/mFZ0e4TP1ksd/VVQ2yfcS69yNuAhpDRj3Q3fgR0rFAZn6BxoZpn5XanBUqXotMwtACuqy0nw2Nsko5+RiNzZ7winpo6BA2uroqkt5wruDD9n+kXHEXrhKOMAhHaOvo3BzuJ9AKUf9Yp7AVjvo3+ag4cLURZQAG1c8ua2YEQz/PKnst2CcJ3BgZ3YiyEnU2ckBDWmyMcaWypnAgU5fSAV6M6hZu4rSBTkFs/vGFf7oU4o5O4dsJbm4bCCz3KZXVfdZZrjPPCmDD7v0gLDRXXPVZu q8Fh8OYL TNkHAPnE1ZTaD8kC1qyT2cmZ4i1nZzZpxp9cI5XoOwSTqM5Kc2aYRoCuGW2YvjO6ThUgZMZh50c/BJJSKVVzvp/29xGFCN+JfRis9zbvLyeaXiDLGCRerMFil04UNEL7UPvUHMYLeukdlvJUvdZMV3vCyr2M0GyEYw7dvHwcjFmNgW5DafiMGWwSqHsUjHyof6PAw4bwLdWtLHwrGLca32vxXKJT+WFMKqhHKepnWc93Ea8BhfoWju8G+Ll92p9RGufejpovJlQS7YtYnPULceyh98ybAmteF2J+ceu0qtoXIZ10Jd2A6rvw8myXuZ4YSAdY/xxy7unJVX5wofnZ26BNuLAvLZkKKkckbym2WjBU+/IL+hTO3wpDOh77NlsgcvFGo9jax6Dl6wubI0B2MO/mIMexljvuR5J5ZmNnXPgS1AN9rr/QBZEdVfGhCIYzmYCZGL6m7bGwijUIxeSB4OWjc08oXeoQl3RHLIpopcYHNVGJmFXJJ+tRxw8sQffqoksCeurlwEdv8EFTBGhU/YMDq7iVIuYV6GNfz/nsblELYhDCyZo0LE4xrnwJwEWB+ALJsezNC2VVb//7C4H3oY7n3VeSEQ0X3BUazQaAgcaSlfIIpAmWH7FZ9RvcTNaWBYZxw/o7yzgaj8ou1H2skoaotr5DQPoBG5O7SXGmxD7fTRiBivg9AkDkjKJxXFCvqu885GQSWoQHa+nvDGkwIaj9gA+G5Mj9y/1wwqL4bsqyq5CV88mzVAfVi5A101yFW97QWSRxrumk75EBFn3+N8MXwa2b5DCbPi37iqJ0JPtRbVW2KqnNVN6Agz+XfOm1PUXy3E6rTCj3JBRZK4KPHTA1PKGnPPzI8HXem X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, May 14, 2025 at 7:56=E2=80=AFPM Dev Jain wrote: > > > > On 13/05/25 2:16 pm, Barry Song wrote: > > From: Barry Song > > > > My commit 354dffd29575c ("mm: support batched unmap for lazyfree large > > folios during reclamation") introduced support for unmapping entire > > lazyfree anonymous large folios at once, instead of one page at a time. > > This patch extends that support to generic (non-lazyfree) anonymous > > large folios. > > > > Handling __folio_try_share_anon_rmap() and swap_duplicate() becomes > > extremely complex=E2=80=94if not outright impractical=E2=80=94for non-e= xclusive > > anonymous folios. As a result, this patch limits support to exclusive > > large folios. Fortunately, most anonymous folios are exclusive in > > practice, so this restriction should be acceptable in the majority of > > cases. > > > > SPARC is currently the only architecture that implements > > arch_unmap_one(), which also needs to be batched for consistency. > > However, this is not yet supported, so the platform is excluded for > > now. > > > > Using the following micro-benchmark to measure the time taken to perfor= m > > PAGEOUT on 256MB of 64KiB anonymous large folios. > > > > #define _GNU_SOURCE > > #include > > #include > > #include > > #include > > #include > > #include > > #include > > > > #define SIZE_MB 256 > > #define SIZE_BYTES (SIZE_MB * 1024 * 1024) > > > > int main() { > > void *addr =3D mmap(NULL, SIZE_BYTES, PROT_READ | PROT_WRITE, > > MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > > if (addr =3D=3D MAP_FAILED) { > > perror("mmap failed"); > > return 1; > > } > > > > memset(addr, 0, SIZE_BYTES); > > > > struct timespec start, end; > > clock_gettime(CLOCK_MONOTONIC, &start); > > > > if (madvise(addr, SIZE_BYTES, MADV_PAGEOUT) !=3D 0) { > > perror("madvise(MADV_PAGEOUT) failed"); > > munmap(addr, SIZE_BYTES); > > return 1; > > } > > > > clock_gettime(CLOCK_MONOTONIC, &end); > > > > long duration_ns =3D (end.tv_sec - start.tv_sec) * 1e9 + > > (end.tv_nsec - start.tv_nsec); > > printf("madvise(MADV_PAGEOUT) took %ld ns (%.3f ms)\n", > > duration_ns, duration_ns / 1e6); > > > > munmap(addr, SIZE_BYTES); > > return 0; > > } > > > > w/o patch: > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 1337334000 ns (1337.334 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 1340471008 ns (1340.471 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 1385718992 ns (1385.719 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 1366070000 ns (1366.070 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 1347834992 ns (1347.835 ms) > > > > w/patch: > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 698178000 ns (698.178 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 708570000 ns (708.570 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 693884000 ns (693.884 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 693366000 ns (693.366 ms) > > ~ # ./a.out > > madvise(MADV_PAGEOUT) took 690790000 ns (690.790 ms) > > > > We found that the time to reclaim this memory was reduced by half. > > > > Cc: David Hildenbrand > > Cc: Baolin Wang > > Cc: Ryan Roberts > > Cc: Lorenzo Stoakes > > Cc: Liam R. Howlett > > Cc: Vlastimil Babka > > Cc: Mike Rapoport > > Cc: Suren Baghdasaryan > > Cc: Michal Hocko > > Cc: Rik van Riel > > Cc: Harry Yoo > > Cc: Kairui Song > > Cc: Chris Li > > Cc: Baoquan He > > Cc: Dan Schatzberg > > Cc: Kaixiong Yu > > Cc: Fan Ni > > Cc: Tangquan Zheng > > Signed-off-by: Barry Song > > --- > > include/linux/swap.h | 4 +-- > > mm/memory.c | 2 +- > > mm/rmap.c | 79 +++++++++++++++++++++++++++++--------------= - > > mm/swapfile.c | 10 ++++-- > > 4 files changed, 62 insertions(+), 33 deletions(-) > > > > diff --git a/include/linux/swap.h b/include/linux/swap.h > > index bc0e1c275fc0..8fbb8ce72016 100644 > > --- a/include/linux/swap.h > > +++ b/include/linux/swap.h > > @@ -479,7 +479,7 @@ void put_swap_folio(struct folio *folio, swp_entry_= t entry); > > extern swp_entry_t get_swap_page_of_type(int); > > extern int add_swap_count_continuation(swp_entry_t, gfp_t); > > extern void swap_shmem_alloc(swp_entry_t, int); > > -extern int swap_duplicate(swp_entry_t); > > +extern int swap_duplicate(swp_entry_t, int nr); > > Missed writing the parameter name here. Thanks =E2=80=94 I noticed this as well after sending. But compilation went through without any issues. > > > extern int swapcache_prepare(swp_entry_t entry, int nr); > > extern void swap_free_nr(swp_entry_t entry, int nr_pages); > > extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); > > @@ -546,7 +546,7 @@ static inline void swap_shmem_alloc(swp_entry_t swp= , int nr) > > { > > } > > > > -static inline int swap_duplicate(swp_entry_t swp) > > +static inline int swap_duplicate(swp_entry_t swp, int nr) > > { > > return 0; > > } > > diff --git a/mm/memory.c b/mm/memory.c > > index 99af83434e7c..5a7e4c0e89c7 100644 > > --- a/mm/memory.c > > +++ b/mm/memory.c > > @@ -803,7 +803,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struc= t mm_struct *src_mm, > > swp_entry_t entry =3D pte_to_swp_entry(orig_pte); > > > > if (likely(!non_swap_entry(entry))) { > > - if (swap_duplicate(entry) < 0) > > + if (swap_duplicate(entry, 1) < 0) > > return -EIO; > > > > /* make sure dst_mm is on swapoff's mmlist. */ > > diff --git a/mm/rmap.c b/mm/rmap.c > > index fb63d9256f09..2607e02a0960 100644 > > --- a/mm/rmap.c > > +++ b/mm/rmap.c > > @@ -1845,23 +1845,42 @@ void folio_remove_rmap_pud(struct folio *folio,= struct page *page, > > #endif > > } > > > > -/* We support batch unmapping of PTEs for lazyfree large folios */ > > +/* > > + * We support batch unmapping of PTEs for lazyfree or exclusive anon l= arge > > + * folios > > + */ > > static inline bool can_batch_unmap_folio_ptes(unsigned long addr, > > - struct folio *folio, pte_t *ptep) > > + struct folio *folio, pte_t *ptep, bool exclusive) > > { > > const fpb_t fpb_flags =3D FPB_IGNORE_DIRTY | FPB_IGNORE_SOFT_DIRT= Y; > > int max_nr =3D folio_nr_pages(folio); > > +#ifndef __HAVE_ARCH_UNMAP_ONE > > + bool no_arch_unmap =3D true; > > +#else > > + bool no_arch_unmap =3D false; > > +#endif > > pte_t pte =3D ptep_get(ptep); > > + int mapped_nr; > > > > - if (!folio_test_anon(folio) || folio_test_swapbacked(folio)) > > + if (!folio_test_anon(folio)) > > return false; > > if (pte_unused(pte)) > > return false; > > if (pte_pfn(pte) !=3D folio_pfn(folio)) > > return false; > > > > - return folio_pte_batch(folio, addr, ptep, pte, max_nr, fpb_flags,= NULL, > > - NULL, NULL) =3D=3D max_nr; > > + mapped_nr =3D folio_pte_batch(folio, addr, ptep, pte, max_nr, fpb= _flags, NULL, > > + NULL, NULL); > > + if (mapped_nr !=3D max_nr) > > + return false; > > + if (!folio_test_swapbacked(folio)) > > + return true; > > + > > + /* > > + * The large folio is fully mapped and its mapcount is the same a= s its > > + * number of pages, it must be exclusive. > > + */ > > + return no_arch_unmap && exclusive && folio_mapcount(folio) =3D=3D= max_nr; > > } > > > > /* > > @@ -2025,7 +2044,8 @@ static bool try_to_unmap_one(struct folio *folio,= struct vm_area_struct *vma, > > folio_mark_dirty(folio); > > } else if (likely(pte_present(pteval))) { > > if (folio_test_large(folio) && !(flags & TTU_HWPO= ISON) && > > - can_batch_unmap_folio_ptes(address, folio, pv= mw.pte)) > > + can_batch_unmap_folio_ptes(address, folio, pv= mw.pte, > > + anon_exclusive)) > > nr_pages =3D folio_nr_pages(folio); > > end_addr =3D address + nr_pages * PAGE_SIZE; > > flush_cache_range(vma, address, end_addr); > > @@ -2141,8 +2161,8 @@ static bool try_to_unmap_one(struct folio *folio,= struct vm_area_struct *vma, > > goto discard; > > } > > > > - if (swap_duplicate(entry) < 0) { > > - set_pte_at(mm, address, pvmw.pte, pteval)= ; > > + if (swap_duplicate(entry, nr_pages) < 0) { > > + set_ptes(mm, address, pvmw.pte, pteval, n= r_pages); > > goto walk_abort; > > } > > > > @@ -2159,9 +2179,10 @@ static bool try_to_unmap_one(struct folio *folio= , struct vm_area_struct *vma, > > > > /* See folio_try_share_anon_rmap(): clear PTE fir= st. */ > > if (anon_exclusive && > > - folio_try_share_anon_rmap_pte(folio, subpage)= ) { > > - swap_free(entry); > > - set_pte_at(mm, address, pvmw.pte, pteval)= ; > > + __folio_try_share_anon_rmap(folio, subpage, n= r_pages, > > + RMAP_LEVEL_PTE)) = { > > + swap_free_nr(entry, nr_pages); > > + set_ptes(mm, address, pvmw.pte, pteval, n= r_pages); > > goto walk_abort; > > } > > if (list_empty(&mm->mmlist)) { > > @@ -2170,23 +2191,27 @@ static bool try_to_unmap_one(struct folio *foli= o, struct vm_area_struct *vma, > > list_add(&mm->mmlist, &init_mm.mm= list); > > spin_unlock(&mmlist_lock); > > } > > - dec_mm_counter(mm, MM_ANONPAGES); > > - inc_mm_counter(mm, MM_SWAPENTS); > > - swp_pte =3D swp_entry_to_pte(entry); > > - if (anon_exclusive) > > - swp_pte =3D pte_swp_mkexclusive(swp_pte); > > - if (likely(pte_present(pteval))) { > > - if (pte_soft_dirty(pteval)) > > - swp_pte =3D pte_swp_mksoft_dirty(= swp_pte); > > - if (pte_uffd_wp(pteval)) > > - swp_pte =3D pte_swp_mkuffd_wp(swp= _pte); > > - } else { > > - if (pte_swp_soft_dirty(pteval)) > > - swp_pte =3D pte_swp_mksoft_dirty(= swp_pte); > > - if (pte_swp_uffd_wp(pteval)) > > - swp_pte =3D pte_swp_mkuffd_wp(swp= _pte); > > + add_mm_counter(mm, MM_ANONPAGES, -nr_pages); > > + add_mm_counter(mm, MM_SWAPENTS, nr_pages); > > + /* TODO: let set_ptes() support swp_offset advanc= e */ > > + for (pte_t *ptep =3D pvmw.pte; address < end_addr= ; > > + entry.val++, address +=3D PAGE_SIZE, ptep++)= { > > + swp_pte =3D swp_entry_to_pte(entry); > > + if (anon_exclusive) > > + swp_pte =3D pte_swp_mkexclusive(s= wp_pte); > > + if (likely(pte_present(pteval))) { > > + if (pte_soft_dirty(pteval)) > > + swp_pte =3D pte_swp_mksof= t_dirty(swp_pte); > > + if (pte_uffd_wp(pteval)) > > + swp_pte =3D pte_swp_mkuff= d_wp(swp_pte); > > + } else { > > + if (pte_swp_soft_dirty(pteval)) > > + swp_pte =3D pte_swp_mksof= t_dirty(swp_pte); > > + if (pte_swp_uffd_wp(pteval)) > > + swp_pte =3D pte_swp_mkuff= d_wp(swp_pte); > > + } > > + set_pte_at(mm, address, ptep, swp_pte); > > } > > - set_pte_at(mm, address, pvmw.pte, swp_pte); > > } else { > > /* > > * This is a locked file-backed folio, > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > index 026090bf3efe..189e3474ffc6 100644 > > --- a/mm/swapfile.c > > +++ b/mm/swapfile.c > > @@ -3550,13 +3550,17 @@ static int __swap_duplicate(swp_entry_t entry, = unsigned char usage, int nr) > > > > offset =3D swp_offset(entry); > > VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); > > - VM_WARN_ON(usage =3D=3D 1 && nr > 1); > > ci =3D lock_cluster(si, offset); > > > > err =3D 0; > > for (i =3D 0; i < nr; i++) { > > count =3D si->swap_map[offset + i]; > > > > + /* > > + * We only support batched swap_duplicate() for unmapping > > + * exclusive large folios where count should be zero > > + */ > > + VM_WARN_ON(usage =3D=3D 1 && nr > 1 && swap_count(count))= ; > > /* > > * swapin_readahead() doesn't check if a swap entry is va= lid, so the > > * swap entry could be SWAP_MAP_BAD. Check here with lock= held. > > @@ -3626,11 +3630,11 @@ void swap_shmem_alloc(swp_entry_t entry, int nr= ) > > * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT= ), which > > * might occur if a page table entry has got corrupted. > > */ > > -int swap_duplicate(swp_entry_t entry) > > +int swap_duplicate(swp_entry_t entry, int nr) > > { > > int err =3D 0; > > > > - while (!err && __swap_duplicate(entry, 1, 1) =3D=3D -ENOMEM) > > + while (!err && __swap_duplicate(entry, 1, nr) =3D=3D -ENOMEM) > > err =3D add_swap_count_continuation(entry, GFP_ATOMIC); > > return err; > > } > Best Regards Barry