From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0DFBE2AD2E; Thu, 24 Sep 2026 13:11:30 +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=1790255492; cv=none; b=EdTha99hF4YImxdvo5e5KsNKbfyTeWbHpJB6ECYgqODqV0y1WEXRu7xTMjRYEtaBaW55bfvyvRdS028yJhgGMDFEbAErE+gNZQ1IaDbCfAih9z47lVTj/dIIIw55MBJGapMHkeaT4Evy1G+1b10jytCIBJO1PqWjR8RAfrl5Rhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255492; c=relaxed/simple; bh=GBTBAttj6YqnCR/uq6LNIwfyQljyiydrN9JFyuIuVt8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sNkRdbNVVlHyBhQc2CyIhBvPnuTEEIQZ+AzxqdumARm5O7Y8uNj4+tBIQMsNXyuIrHXrnKKrnQ8F3AM/Ind0aCf/xAJzmmW2S/bBA6OWfDpjRUCT91uamWRntY+XsadUa9cxD3P4i8N3ep8DAz67v01qF/sZ00YujGc/dKkbPQo= 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=rKLdvoPc; 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="rKLdvoPc" 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 D9BEF1570; Thu, 24 Sep 2026 06:11:26 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 4170C3F86F; Thu, 24 Sep 2026 06:11:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255490; bh=GBTBAttj6YqnCR/uq6LNIwfyQljyiydrN9JFyuIuVt8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rKLdvoPcWH10UQwc6ZxfGlsPCWitBVRMqBo/pDpRBjg7890TBpz/IEflern/JSchq scRdr+IQOwuoDOtEZqFEUZ9GnAjXanwvxbjOPI0RB+K9XXhHdGFqrI4ustGAy6toDR hYJKQANAFQgq2qEgHl6lsXUoC0oztWYTDxsY/FhM= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 1/9] mm/swapfile: add batched version of folio_dup_swap Date: Thu, 24 Sep 2026 13:09:29 +0000 Message-ID: <20260924131106.1730494-2-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add folio_dup_swap_pages to handle a batch of consecutive pages. Note that folio_dup_swap already can handle a subset of this: nr_pages =3D=3D 1 = and nr_pages =3D=3D folio_nr_pages(folio). Generalize this to any nr_pages. Currently we have a not-so-nice logic of passing in subpage =3D=3D NULL if we mean to exercise the logic on the entire folio, and subpage !=3D NULL if we want to exercise the logic on only that subpage. Remove this indirection: the caller invokes folio_dup_swap_pages() if it wants to operate on a range of pages in the folio (i.e nr_pages may be anything between 1 and folio_nr_pages()), and invokes folio_dup_swap() if it wants to operate on the entire folio. Acked-by: David Hildenbrand (Arm) Reviewed-by: Barry Song Signed-off-by: Dev Jain --- mm/rmap.c | 2 +- mm/shmem.c | 2 +- mm/swap.h | 18 ++++++++++++++++-- mm/swapfile.c | 18 ++++++++---------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 6661bc11ce658..a70dbc8d544e1 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2150,7 +2150,7 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_= struct *vma, swp_entry_t entry =3D folio_page_swap_entry(folio, page); struct mm_struct *mm =3D vma->vm_mm; =20 - if (folio_dup_swap(folio, page) < 0) + if (folio_dup_swap_pages(folio, page, 1) < 0) return false; =20 /* diff --git a/mm/shmem.c b/mm/shmem.c index b572c60f2af85..d5bd9506ce500 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1914,7 +1914,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct fo= lio *folio, spin_unlock(&shmem_swaplist_lock); } =20 - folio_dup_swap(folio, NULL); + folio_dup_swap(folio); shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap)); =20 BUG_ON(folio_mapped(folio)); diff --git a/mm/swap.h b/mm/swap.h index b3b54c28929a1..47290510de264 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -244,7 +244,8 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gf= p_t gfp); * folio_put_swap(): does the opposite thing of folio_dup_swap(). */ int folio_alloc_swap(struct folio *folio); -int folio_dup_swap(struct folio *folio, struct page *page); +int folio_dup_swap_pages(struct folio *folio, struct page *page, + unsigned long nr_pages); void folio_put_swap(struct folio *folio, struct page *page); =20 /* For internal use */ @@ -368,7 +369,8 @@ static inline int folio_alloc_swap(struct folio *folio) return -EINVAL; } =20 -static inline int folio_dup_swap(struct folio *folio, struct page *page) +static inline int folio_dup_swap_pages(struct folio *folio, struct page *p= age, + unsigned long nr_pages) { return -EINVAL; } @@ -464,6 +466,18 @@ static inline void __swap_cache_replace_folio(struct s= wap_cluster_info *ci, } #endif /* CONFIG_SWAP */ =20 +/** + * folio_dup_swap() - Increase swap count of all swap entries of a folio. + * @folio: folio with swap entries bound. + * + * See folio_dup_swap_pages() for more information. + */ +static inline int folio_dup_swap(struct folio *folio) +{ + return folio_dup_swap_pages(folio, folio_page(folio, 0), + folio_nr_pages(folio)); +} + extern const struct swap_ops swap_bdev_ops; =20 int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, diff --git a/mm/swapfile.c b/mm/swapfile.c index c1c5fbb3c909d..75167b8580cc0 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1799,9 +1799,10 @@ int folio_alloc_swap(struct folio *folio) } =20 /** - * folio_dup_swap() - Increase swap count of swap entries of a folio. + * folio_dup_swap_pages() - Increase swap count of swap entries of a folio. * @folio: folio with swap entries bounded. - * @page: if not NULL, only increase the swap count of this page. + * @page: the first page in the folio to increase the swap count for. + * @nr_pages: the number of pages in the folio to increase the swap count = for. * * Typically called when the folio is unmapped and have its swap entry to * take its place: Swap entries allocated to a folio has count =3D=3D 0 an= d pinned @@ -1815,18 +1816,15 @@ int folio_alloc_swap(struct folio *folio) * swap_put_entries_direct on its swap entry before this helper returns, or * the swap count may underflow. */ -int folio_dup_swap(struct folio *folio, struct page *page) +int folio_dup_swap_pages(struct folio *folio, struct page *page, + unsigned long nr_pages) { - swp_entry_t entry =3D folio->swap; - unsigned long nr_pages =3D folio_nr_pages(folio); + swp_entry_t entry =3D folio_page_swap_entry(folio, page); + unsigned long idx =3D folio_page_idx(folio, page); =20 VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio); - - if (page) { - entry =3D folio_page_swap_entry(folio, page); - nr_pages =3D 1; - } + VM_WARN_ON_FOLIO(idx + nr_pages > folio_nr_pages(folio), folio); =20 return swap_dup_entries_cluster(swap_entry_to_info(entry), swp_offset(entry), nr_pages); --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AAAF83C9ED0; Thu, 24 Sep 2026 13:11:39 +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=1790255502; cv=none; b=W6Hos91vniUwH96mLyovjvq+VdFz1T4vHFIDxkUk/TW5GrpnPv9BJbIyszeEaJaXZ3Sc1hZH+3bjGR4q0YroB3LyNXt5ZYTLR0N69/PDSqM5JD2Jju4yRvcXMFkKK44Gm9h6UY1UEV00jhq9nRbFff/gbHztd+/Es0HqyHi0Hc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255502; c=relaxed/simple; bh=r91eES0EP0FmtjnUnc9tQIU8yObnhTUHKWz83dXANF8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ha0VkwJUEYBbj1l4CCMFp4862p6qUNneMfi99WJ1GMiVf1qdSY6UQ18/b9n+Tn/0rkG6cAYJy5tdXnazAUk//0hBcjFLojUqwA4tCewQSiYo+h1FtMbaUqN51DVuZivmdfAR6T0WOWfP6EvMWpX83HWQVkD9n8Rz2WVacVsBj0I= 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=LHgzJ1D+; 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="LHgzJ1D+" 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 8667C1756; Thu, 24 Sep 2026 06:11:35 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E38E13F86F; Thu, 24 Sep 2026 06:11:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255499; bh=r91eES0EP0FmtjnUnc9tQIU8yObnhTUHKWz83dXANF8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LHgzJ1D+YUx8Zf/vSao78zcMMuId0jb4jC2PGpCdBi+0m7bCpyVpvCW27m/U9+kHG 0jgysohKuvpAQbD2MptRuX5aW08iuQ2fZMpCQdb3FfIprMEQRXhTK+voRr85hhrE1i zPAGOgg+yN4NqhDcxIWQNOYQbDZzoFnTCWDmr20w= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 2/9] mm/swapfile: add batched version of folio_put_swap Date: Thu, 24 Sep 2026 13:09:30 +0000 Message-ID: <20260924131106.1730494-3-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add folio_put_swap_pages to handle a batch of consecutive pages. Note that folio_put_swap already can handle a subset of this: nr_pages =3D=3D 1 = and nr_pages =3D=3D folio_nr_pages(folio). Generalize this to any nr_pages. Currently we have a not-so-nice logic of passing in subpage =3D=3D NULL if we mean to exercise the logic on the entire folio, and subpage !=3D NULL if we want to exercise the logic on only that subpage. Remove this indirection: the caller invokes folio_put_swap_pages() if it wants to operate on a range of pages in the folio (i.e nr_pages may be anything between 1 and folio_nr_pages()), and invokes folio_put_swap() if it wants to operate on the entire folio. Acked-by: David Hildenbrand (Arm) Reviewed-by: Barry Song Signed-off-by: Dev Jain --- mm/memory.c | 6 +++--- mm/rmap.c | 4 ++-- mm/shmem.c | 6 +++--- mm/swap.h | 17 +++++++++++++++-- mm/swapfile.c | 21 ++++++++++----------- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 338fce99e7119..a7e979bfc814b 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5267,7 +5267,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (unlikely(folio !=3D swapcache)) { folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE); folio_add_lru_vma(folio, vma); - folio_put_swap(swapcache, NULL); + folio_put_swap(swapcache); } else if (!folio_test_anon(folio)) { /* * We currently only expect !anon folios that are fully @@ -5276,12 +5276,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) VM_WARN_ON_ONCE_FOLIO(folio_nr_pages(folio) !=3D nr_pages, folio); VM_WARN_ON_ONCE_FOLIO(folio_mapped(folio), folio); folio_add_new_anon_rmap(folio, vma, address, rmap_flags); - folio_put_swap(folio, NULL); + folio_put_swap(folio); } else { VM_WARN_ON_ONCE(nr_pages !=3D 1 && nr_pages !=3D folio_nr_pages(folio)); folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, address, rmap_flags); - folio_put_swap(folio, nr_pages =3D=3D 1 ? page : NULL); + folio_put_swap_pages(folio, page, nr_pages); } =20 VM_BUG_ON(!folio_test_anon(folio) || diff --git a/mm/rmap.c b/mm/rmap.c index a70dbc8d544e1..fa9fc8374fc26 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2159,13 +2159,13 @@ static bool ttu_anon_swapbacked_folio(struct vm_are= a_struct *vma, * so we'll not check/care. */ if (arch_unmap_one(mm, vma, address, pteval) < 0) { - folio_put_swap(folio, page); + folio_put_swap_pages(folio, page, 1); return false; } =20 /* See folio_try_share_anon_rmap(): clear PTE first. */ if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, page)) { - folio_put_swap(folio, page); + folio_put_swap_pages(folio, page, 1); return false; } =20 diff --git a/mm/shmem.c b/mm/shmem.c index d5bd9506ce500..5f46061c6320c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1935,7 +1935,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct fo= lio *folio, /* Swap entry might be erased by racing shmem_free_swap() */ if (!error) { shmem_recalc_inode(inode, 0, -nr_pages); - folio_put_swap(folio, NULL); + folio_put_swap(folio); } =20 /* @@ -2368,7 +2368,7 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, =20 nr_pages =3D folio_nr_pages(folio); folio_wait_writeback(folio); - folio_put_swap(folio, NULL); + folio_put_swap(folio); swap_cache_del_folio(folio); /* * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks @@ -2598,7 +2598,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, if (sgp =3D=3D SGP_WRITE) folio_mark_accessed(folio); =20 - folio_put_swap(folio, NULL); + folio_put_swap(folio); swap_cache_del_folio(folio); folio_mark_dirty(folio); put_swap_device(si); diff --git a/mm/swap.h b/mm/swap.h index 47290510de264..7ae915f594034 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -246,7 +246,8 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gf= p_t gfp); int folio_alloc_swap(struct folio *folio); int folio_dup_swap_pages(struct folio *folio, struct page *page, unsigned long nr_pages); -void folio_put_swap(struct folio *folio, struct page *page); +void folio_put_swap_pages(struct folio *folio, struct page *page, + unsigned long nr_pages); =20 /* For internal use */ extern void __swap_cluster_free_entries(struct swap_info_struct *si, @@ -375,7 +376,8 @@ static inline int folio_dup_swap_pages(struct folio *fo= lio, struct page *page, return -EINVAL; } =20 -static inline void folio_put_swap(struct folio *folio, struct page *page) +static inline void folio_put_swap_pages(struct folio *folio, struct page *= page, + unsigned long nr_pages) { } =20 @@ -478,6 +480,17 @@ static inline int folio_dup_swap(struct folio *folio) folio_nr_pages(folio)); } =20 +/** + * folio_put_swap() - Decrease swap count of all swap entries of a folio. + * @folio: folio with swap entries bound. + * + * See folio_put_swap_pages() for more information. + */ +static inline void folio_put_swap(struct folio *folio) +{ + folio_put_swap_pages(folio, folio_page(folio, 0), folio_nr_pages(folio)); +} + extern const struct swap_ops swap_bdev_ops; =20 int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, diff --git a/mm/swapfile.c b/mm/swapfile.c index 75167b8580cc0..8b47e7f1e1931 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1831,27 +1831,25 @@ int folio_dup_swap_pages(struct folio *folio, struc= t page *page, } =20 /** - * folio_put_swap() - Decrease swap count of swap entries of a folio. + * folio_put_swap_pages() - Decrease swap count of swap entries of a folio. * @folio: folio with swap entries bounded, must be in swap cache and lock= ed. - * @page: if not NULL, only decrease the swap count of this page. + * @page: the first page in the folio to decrease the swap count for. + * @nr_pages: the number of pages in the folio to decrease the swap count = for. * * This won't free the swap slots even if swap count drops to zero, they a= re * still pinned by the swap cache. User may call folio_free_swap to free t= hem. * Context: Caller must ensure the folio is locked and in the swap cache. */ -void folio_put_swap(struct folio *folio, struct page *page) +void folio_put_swap_pages(struct folio *folio, struct page *page, + unsigned long nr_pages) { - swp_entry_t entry =3D folio->swap; - unsigned long nr_pages =3D folio_nr_pages(folio); + swp_entry_t entry =3D folio_page_swap_entry(folio, page); struct swap_info_struct *si =3D __swap_entry_to_info(entry); + unsigned long idx =3D folio_page_idx(folio, page); =20 VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio); - - if (page) { - entry =3D folio_page_swap_entry(folio, page); - nr_pages =3D 1; - } + VM_WARN_ON_FOLIO(idx + nr_pages > folio_nr_pages(folio), folio); =20 swap_put_entries_cluster(si, swp_offset(entry), nr_pages, false); } @@ -2535,7 +2533,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_= t *pmd, =20 setpte: set_pte_at(vma->vm_mm, addr, pte, new_pte); - folio_put_swap(swapcache, folio_file_page(swapcache, swp_offset(entry))); + folio_put_swap_pages(swapcache, + folio_file_page(swapcache, swp_offset(entry)), 1); out: if (pte) pte_unmap_unlock(pte, ptl); --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5EB6047D477; Thu, 24 Sep 2026 13:11:48 +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=1790255510; cv=none; b=FMTkiLIaxK0SHFyVLyFlBAqWJMFF4agGb5N7sEq3NIkXLyv6KpzWpKJ0FnQOKldCisKYUXaVBpkoC/oJs+qdMpU61L3oL8ZPLTJBBHfVOU7waYtP9/RjE5+JUCzB2QJ9I8KyRZpf5T++Nw23lmTu0qJLKxpRcVz9DfFHK5YbVXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255510; c=relaxed/simple; bh=t0y4o1rliThr4hhOhNaBbuyJQwol8ZLmJ5nvLciVyts=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WbHf2+3tH9pckk4zD6fhNahx946U5+nZoy/D9qsWQ0vB43S2tINVaND+NdAVhKXLhFE1iQ+zjlvUmFhbuCYRd6HVToaQXXAFFAlnYqqYXIXkeU3p7gLjFgJPY/ONzBBXmcBH1Iv+5V6kXX+ujK5sXQqOBhpcV/eznUr+rYT9GCY= 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=XWlR09P/; 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="XWlR09P/" 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 38E9B175D; Thu, 24 Sep 2026 06:11:44 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 92ADC3F86F; Thu, 24 Sep 2026 06:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255507; bh=t0y4o1rliThr4hhOhNaBbuyJQwol8ZLmJ5nvLciVyts=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XWlR09P/WJPRcS6Dm9gpe+RbtWMIfPap7pd5WOSclV/6JI/76RijBjK25b3/9uKqd fTLOU5ZWceR9RaKhVrIRYgPg2Z4GkwSYS1zpwFKBMdP5WMLd9dpuwm5sR5qxxqp4gz x06F0kOnPKOd/0/Qsdd9EwEk5xAb65psn5+oPEj0= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 3/9] mm: move anon-exclusive batch helper to rmap.h Date: Thu, 24 Sep 2026 13:09:31 +0000 Message-ID: <20260924131106.1730494-4-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for optimizing large folio unmapping, we need to reuse the page_anon_exclusive_batch helper in rmap.c and rmap.h and obey the existing use in mprotect.c . Therefore, move it from mprotect.c to rmap.h. While at it, change return type, start_idx and max_len to unsigned long type for future proofing against THP support at >=3D PUD level. Also shorten expected_anon_exclusive -> anon_exclusive. Signed-off-by: Dev Jain Reviewed-by: Barry Song --- include/linux/rmap.h | 17 +++++++++++++++++ mm/mprotect.c | 18 +----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 74cca0e3c7264..62ef511a6175a 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -106,6 +106,23 @@ enum ttu_flags { =20 #ifdef CONFIG_MMU =20 +/* + * Get max length of consecutive PTEs pointing to PageAnonExclusive() page= s or + * !PageAnonExclusive() pages, starting from start_idx. Caller must enforce + * that the PTEs point to consecutive pages of the same anon large folio. + */ +static __always_inline unsigned long page_anon_exclusive_batch(unsigned lo= ng start_idx, + unsigned long max_len, struct page *first_page, bool anon_exclusive) +{ + unsigned long idx; + + for (idx =3D start_idx + 1; idx < start_idx + max_len; ++idx) { + if (anon_exclusive !=3D PageAnonExclusive(first_page + idx)) + break; + } + return idx - start_idx; +} + void anon_vma_init(void); /* create anon_vma_cachep */ =20 #ifdef CONFIG_MM_ID diff --git a/mm/mprotect.c b/mm/mprotect.c index a1b6d29bf0390..cb996dbd0e525 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -138,23 +139,6 @@ static __always_inline void prot_commit_flush_ptes(str= uct vm_area_struct *vma, tlb_flush_pte_range(tlb, addr, nr_ptes * PAGE_SIZE); } =20 -/* - * Get max length of consecutive ptes pointing to PageAnonExclusive() page= s or - * !PageAnonExclusive() pages, starting from start_idx. Caller must enforce - * that the ptes point to consecutive pages of the same anon large folio. - */ -static __always_inline int page_anon_exclusive_batch(int start_idx, int ma= x_len, - struct page *first_page, bool expected_anon_exclusive) -{ - int idx; - - for (idx =3D start_idx + 1; idx < start_idx + max_len; ++idx) { - if (expected_anon_exclusive !=3D PageAnonExclusive(first_page + idx)) - break; - } - return idx - start_idx; -} - /* * This function is a result of trying our very best to retain the * "avoid the write-fault handler" optimization. In can_change_pte_writabl= e(), --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 73A18387590; Thu, 24 Sep 2026 13:11:57 +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=1790255519; cv=none; b=Ja2n3fYc+PyWcvmibkwpdd6Dex3dnezAzZBNK4ro82TnX7oBEuW7yK4e0mcr2x9b8kn1lfGYja1+RL2gW3qgZVxwFt0nlUOc+1W2PLHCfOAgN/gPpA402zwn5gasajAphj7i0Y4TiJ01uM9tti8CxkP1EET7ZU1yIRdk0oxhOps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255519; c=relaxed/simple; bh=omnc/jiGce2ioWghxdLYMfvILVqD+Qx1+Ygyy+n/n2I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pDahzF3R2aJwl3YnOAeKORm0nVLqdkdQzxVrSIY1iD7tfDe4r5NUQrQjDkb/FdjdLX82iJxlNEml7ehmZleVoLxecezXRRlP3siqf5Y06q5XtINNmGsKY4afCshgWelgRGlMzyradaUzYDhMxelLzESiAeQWTN8huW2t/Fqe5iQ= 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=Cqw+z1JR; 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="Cqw+z1JR" 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 D57051A32; Thu, 24 Sep 2026 06:11:52 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 4174C3F86F; Thu, 24 Sep 2026 06:11:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255516; bh=omnc/jiGce2ioWghxdLYMfvILVqD+Qx1+Ygyy+n/n2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cqw+z1JRNnoGVg2M6BZ0XIIofy3CKrxfcxsiHv5K+w04E+MA396UFlL/9MTxcEloj ud6CPr2NiTciVXHDU2JreHt0oDkzaKPTyWyHjt7MGCOjQR57BRSR//s31IDbO3h2TW 1507rdZMwHyYs34HsTPCcSn5rAfhP9nMsXTEXPHw= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 4/9] mm/rmap: Add batched version of folio_try_share_anon_rmap_pte Date: Thu, 24 Sep 2026 13:09:32 +0000 Message-ID: <20260924131106.1730494-5-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To enable batched unmapping of anonymous folios, we need to handle the sharing of exclusive pages. Hence, a batched version of folio_try_share_anon_rmap_pte is required. Currently, the sole purpose of nr_pages in __folio_try_share_anon_rmap is to do some rmap sanity checks. Now, clear the PageAnonExclusive bit on a batch of nr_pages. Refactor the function such that the clearing of the bit can be done at one place without duplication. Note that __folio_try_share_anon_rmap can receive nr_pages =3D=3D HPAGE_PMD= _NR from the PMD path, but currently we only clear the bit on the head page. Retain this behaviour by setting nr_pages =3D 1 in case the caller is folio_try_share_anon_rmap_pmd. While at it, convert nr_pages to unsigned long to future-proof from overflow in case P4D-huge mappings etc get supported down the road. I haven't made such a change in each function receiving nr_pages in try_to_unmap_one - perhaps this can be done incrementally. Add two WARN's: check that the batch is entirely exclusive (for PMD callers, need to check only head page), and that there are only PTE/PMD paths converging into __folio_try_share_anon_rmap. Signed-off-by: Dev Jain Reviewed-by: Barry Song --- include/linux/rmap.h | 56 ++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 62ef511a6175a..91b5763ef466f 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -723,17 +723,23 @@ static inline int folio_try_dup_anon_rmap_pmd(struct = folio *folio, } =20 static __always_inline int __folio_try_share_anon_rmap(struct folio *folio, - struct page *page, int nr_pages, enum pgtable_level level) + struct page *page, unsigned long nr_pages, enum pgtable_level level) { + /* device private folios cannot get pinned via GUP. */ + const bool pinnable =3D !folio_is_device_private(folio); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); VM_WARN_ON_FOLIO(!PageAnonExclusive(page), folio); + __folio_rmap_sanity_checks(folio, page, nr_pages, level); =20 - /* device private folios cannot get pinned via GUP. */ - if (unlikely(folio_is_device_private(folio))) { - ClearPageAnonExclusive(page); - return 0; - } + VM_WARN_ON_ONCE(level !=3D PGTABLE_LEVEL_PTE && level !=3D PGTABLE_LEVEL_= PMD); + + /* We only clear anon-exclusive from head page of PMD folio. */ + if (level =3D=3D PGTABLE_LEVEL_PMD) + nr_pages =3D 1; + + VM_WARN_ON_FOLIO(page_anon_exclusive_batch(0, nr_pages, page, true) !=3D = nr_pages, folio); =20 /* * We have to make sure that when we clear PageAnonExclusive, that @@ -777,29 +783,38 @@ static __always_inline int __folio_try_share_anon_rma= p(struct folio *folio, * so we use explicit ones here. */ =20 - /* Paired with the memory barrier in try_grab_folio(). */ - if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) - smp_mb(); + if (likely(pinnable)) { + /* Paired with the memory barrier in try_grab_folio(). */ + if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) + smp_mb(); =20 - if (unlikely(folio_maybe_dma_pinned(folio))) - return -EBUSY; - ClearPageAnonExclusive(page); + if (unlikely(folio_maybe_dma_pinned(folio))) + return -EBUSY; + } + + for (;;) { + ClearPageAnonExclusive(page); + if (--nr_pages =3D=3D 0) + break; + page++; + } =20 /* * This is conceptually a smp_wmb() paired with the smp_rmb() in * gup_must_unshare(). */ - if (IS_ENABLED(CONFIG_HAVE_GUP_FAST)) + if (likely(pinnable) && IS_ENABLED(CONFIG_HAVE_GUP_FAST)) smp_mb__after_atomic(); return 0; } =20 /** - * folio_try_share_anon_rmap_pte - try marking an exclusive anonymous page - * mapped by a PTE possibly shared to prepare + * folio_try_share_anon_rmap_ptes - try marking exclusive anonymous pages + * mapped by PTEs possibly shared to prepare * for KSM or temporary unmapping * @folio: The folio to share a mapping of - * @page: The mapped exclusive page + * @page: The first mapped exclusive page of the batch in the folio + * @nr_pages: The number of pages to share in the folio (batch size) * * The caller needs to hold the page table lock and has to have the page t= able * entries cleared/invalidated. @@ -815,10 +830,17 @@ static __always_inline int __folio_try_share_anon_rma= p(struct folio *folio, * Returns 0 if marking the mapped page possibly shared succeeded. Returns * -EBUSY otherwise. */ +static inline int folio_try_share_anon_rmap_ptes(struct folio *folio, + struct page *page, unsigned long nr_pages) +{ + return __folio_try_share_anon_rmap(folio, page, nr_pages, + PGTABLE_LEVEL_PTE); +} + static inline int folio_try_share_anon_rmap_pte(struct folio *folio, struct page *page) { - return __folio_try_share_anon_rmap(folio, page, 1, PGTABLE_LEVEL_PTE); + return folio_try_share_anon_rmap_ptes(folio, page, 1); } =20 /** --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 21001480DFF; Thu, 24 Sep 2026 13:12:07 +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=1790255534; cv=none; b=ONNOLjNZ9OsvoXuazVa5PXlRHmwc0DcUS1jFJiHHkaJI0alINbrf6FeFhe2yqnqGya0S8r3ySp2e7rEB7qyXcVtmpvGv26SUf1bTvaa1Vcgro59Mk5JiEyxGrU+XAuWX93VKNC/GiVuOqbUglhZgqvcHnyde9VM1e16ekLRjrZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255534; c=relaxed/simple; bh=g7Rf1C/r+3UV9DV7BimkhOjVMuui/avyxCs3Ib9oOag=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ax0MlDhtca19CyiBihzgjDyK2o9aybCEFb8liqA1T3a21LYsVTwEi+SWgP3sY5QI46TNoFCTnuXGBVhjfQjaq8Ukkept9SXNhenQ47rP5Ld1xFEm5AaPQeycnkrfQKgcc3dM4HdskH3SEoh4bqb0eDUPigVusaLmepucGEAMdgE= 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=Cfo1n7cK; 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="Cfo1n7cK" 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 843B41A25; Thu, 24 Sep 2026 06:12:01 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E32D13F86F; Thu, 24 Sep 2026 06:11:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255525; bh=g7Rf1C/r+3UV9DV7BimkhOjVMuui/avyxCs3Ib9oOag=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cfo1n7cKqSBF0J1b7mKA4hCNrsqXwIm1Qrks7/DPrg9wW4WneiyYMlggjNGGcY5bb 9py0RdAWMFugivPBGE7o73xWnJ4s+Dgb1tRRt6NeU2m9uIE6O6IY2Wvbfi9OSN4xnZ IYN0VSofbPnDP8Tn90KOQ49+T4AcqGb26KIDc4lQ= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 5/9] mm/internal: rename swap offset helpers to softleaf offset Date: Thu, 24 Sep 2026 13:09:33 +0000 Message-ID: <20260924131106.1730494-6-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for adding a helper to set softleaf PTEs in one go, generalize the helpers that advance a swap-entry offset so they operate on softleaf entries. Softleaf entries use the same type/offset layout as swap entries, so advancing the offset works for swap softleaves and PFN-bearing softleaves. Preserve soft-dirty and uffd bits across the move. The swap-exclusive bit is meaningful only for swap softleaves, so guard that preservation with softleaf_is_swap(). Reviewed-by: Barry Song Signed-off-by: Dev Jain --- mm/internal.h | 31 ++++++++++++++++--------------- mm/memory.c | 4 ++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 3b9fdb826162d..b8ec379b90bb0 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -469,16 +469,16 @@ unsigned int folio_pte_batch(struct folio *folio, pte= _t *ptep, pte_t pte, unsigned int max_nr); =20 /** - * pte_move_swp_offset - Move the swap entry offset field of a swap pte - * forward or backward by delta - * @pte: The initial pte state; must be a swap entry + * pte_move_softleaf_offset - Move the softleaf entry offset field of a + * softleaf pte forward or backward by delta + * @pte: The initial pte state; must be a softleaf entry * @delta: The direction and the offset we are moving; forward if delta * is positive; backward if delta is negative * - * Moves the swap offset, while maintaining all other fields, including - * swap type, and any swp pte bits. The resulting pte is returned. + * Moves the softleaf offset, while maintaining all other fields, including + * softleaf type, and any softleaf pte bits. The resulting pte is returned. */ -static inline pte_t pte_move_swp_offset(pte_t pte, long delta) +static inline pte_t pte_move_softleaf_offset(pte_t pte, long delta) { const softleaf_t entry =3D softleaf_from_pte(pte); pte_t new =3D __swp_entry_to_pte(__swp_entry(swp_type(entry), @@ -486,7 +486,7 @@ static inline pte_t pte_move_swp_offset(pte_t pte, long= delta) =20 if (pte_swp_soft_dirty(pte)) new =3D pte_swp_mksoft_dirty(new); - if (pte_swp_exclusive(pte)) + if (softleaf_is_swap(entry) && pte_swp_exclusive(pte)) new =3D pte_swp_mkexclusive(new); if (pte_swp_uffd(pte)) new =3D pte_swp_mkuffd(new); @@ -496,15 +496,16 @@ static inline pte_t pte_move_swp_offset(pte_t pte, lo= ng delta) =20 =20 /** - * pte_next_swp_offset - Increment the swap entry offset field of a swap p= te. - * @pte: The initial pte state; must be a swap entry. + * pte_next_softleaf_offset - Increment the softleaf entry offset field of= a + * non-present pte. + * @pte: The initial pte state; must be a softleaf entry. * - * Increments the swap offset, while maintaining all other fields, includi= ng - * swap type, and any swp pte bits. The resulting pte is returned. + * Increments the softleaf offset, while maintaining all other fields, inc= luding + * softleaf type, and any softleaf pte bits. The resulting pte is returned. */ -static inline pte_t pte_next_swp_offset(pte_t pte) +static inline pte_t pte_next_softleaf_offset(pte_t pte) { - return pte_move_swp_offset(pte, 1); + return pte_move_softleaf_offset(pte, 1); } =20 /** @@ -524,7 +525,7 @@ static inline pte_t pte_next_swp_offset(pte_t pte) */ static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) { - pte_t expected_pte =3D pte_next_swp_offset(pte); + pte_t expected_pte =3D pte_next_softleaf_offset(pte); const pte_t *end_ptep =3D start_ptep + max_nr; pte_t *ptep =3D start_ptep + 1; =20 @@ -536,7 +537,7 @@ static inline int swap_pte_batch(pte_t *start_ptep, int= max_nr, pte_t pte) =20 if (!pte_same(pte, expected_pte)) break; - expected_pte =3D pte_next_swp_offset(expected_pte); + expected_pte =3D pte_next_softleaf_offset(expected_pte); ptep++; } =20 diff --git a/mm/memory.c b/mm/memory.c index a7e979bfc814b..60fd0a08b35f5 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4816,7 +4816,7 @@ static bool can_swapin_thp(struct vm_fault *vmf, pte_= t *ptep, int nr_pages) idx =3D (vmf->address - addr) / PAGE_SIZE; pte =3D ptep_get(ptep); =20 - if (!pte_same(pte, pte_move_swp_offset(vmf->orig_pte, -idx))) + if (!pte_same(pte, pte_move_softleaf_offset(vmf->orig_pte, -idx))) return false; /* * swap_read_folio() can't handle the case a large folio is hybridly @@ -5131,7 +5131,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) =20 folio_ptep =3D vmf->pte - idx; folio_pte =3D ptep_get(folio_ptep); - if (!pte_same(folio_pte, pte_move_swp_offset(vmf->orig_pte, -idx)) || + if (!pte_same(folio_pte, pte_move_softleaf_offset(vmf->orig_pte, -idx)) = || swap_pte_batch(folio_ptep, nr, folio_pte) !=3D nr) goto check_folio; =20 --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9149C481FCE; Thu, 24 Sep 2026 13:12:15 +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=1790255538; cv=none; b=HM8LcFJJEu+4AVYxYQYVZhq5rwCQXT6JdRnSse/IOO9VypKOGDDSaSNMi4iv0HEg37QuUxELKma/X14CBAIgllf53HAHM4OhiAR7XKE6Pv6DXGT9hH0o2TTKKaw+3pY21hZgYs3AZVIEPIFr9id6uHqB8xj22UUZxkfLtZ6Totk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255538; c=relaxed/simple; bh=SdgJCddjPp7/Mvalka61vw2zQLeW42ALZN0qZGzm4do=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JmHh4oQ010SZkJtUtBJsRfX8KNjP8Fa36VKy4c6+dFDryvdAFoK48km36MTLuedVSDn0uLv2ODncCMgra9P+YXOriC/DIgcYUqiRl3fFnIS8JOYYCaJEHlZemGvYElNmgOhkTIc48lu4hDBSAKLBWilb93zvEHLEmiYJG0qQIT8= 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=XGt0CeCZ; 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="XGt0CeCZ" 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 30BFB1BCB; Thu, 24 Sep 2026 06:12:10 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 906783F86F; Thu, 24 Sep 2026 06:12:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255533; bh=SdgJCddjPp7/Mvalka61vw2zQLeW42ALZN0qZGzm4do=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XGt0CeCZXa84341ORqXF1Qk53S9zQuTR0fAfx2PSczJ/Sfzz5uIjthi3J94axjDk6 GMjtt4emAlgBb0XiCPUbJwHR3xR6FaetJXSRzB8mtYD19DrZkM312UGHrqy+hykZYb +2EeSHa7InZYwv+F6wTirMo5P72UNRg18QhSkUu4= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 6/9] mm/internal: add set_softleaf_ptes Date: Thu, 24 Sep 2026 13:09:34 +0000 Message-ID: <20260924131106.1730494-7-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently we have a helper called set_ptes() which is used to set consecutive present ptes in the pgtables. To do the same operation but to set "consecutive" nonpresent (softleaf) ptes, add set_softleaf_ptes(). The softleaves which have a notion of consecutivity is swap softleaf, and those grouped by softeaf_has_pfn(). The latter is trivial; future code can convert present ptes pointing to the same large folio to swap softleaves with consecutive offsets using this helper. The other case is softleaf markers. They do not have a notion of a swap offset or PFN, so future code can use set_softleaf_ptes() to store multiple (same) markers on the ptes. Reviewed-by: Barry Song Signed-off-by: Dev Jain --- mm/internal.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mm/internal.h b/mm/internal.h index b8ec379b90bb0..0ffc9565c3273 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -508,6 +508,39 @@ static inline pte_t pte_next_softleaf_offset(pte_t pte) return pte_move_softleaf_offset(pte, 1); } =20 +/** + * set_softleaf_ptes - Set consecutive softleaf PTEs. + * @mm: Address space the PTEs belong to. + * @addr: Address of the first PTE. + * @ptep: Page table pointer for the first PTE. + * @pte: PTE to set for the first entry. + * @nr: Number of PTEs to set. + * + * Install @nr softleaf PTEs, advancing @pte when its softleaf entry + * represents consecutive offsets. Swap entries advance through swap offse= ts, + * PFN softleaf entries advance through PFNs (encoded by swap offset), and + * marker entries are repeated unchanged. + */ +static inline void set_softleaf_ptes(struct mm_struct *mm, unsigned long a= ddr, + pte_t *ptep, pte_t pte, unsigned long nr) +{ + softleaf_t entry; + bool advance; + + entry =3D softleaf_from_pte(pte); + advance =3D softleaf_is_swap(entry) || softleaf_has_pfn(entry); + + for (;;) { + set_pte_at(mm, addr, ptep, pte); + if (--nr =3D=3D 0) + break; + if (advance) + pte =3D pte_next_softleaf_offset(pte); + ptep++; + addr +=3D PAGE_SIZE; + } +} + /** * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries * @start_ptep: Page table pointer for the first entry. --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 81A284825AE; Thu, 24 Sep 2026 13:12:25 +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=1790255551; cv=none; b=TZ11zkgbikWNhnMwBdQjMkBJv0J3uc9H+8ixbuDYw0DSwYkHcpQcD0Gz2zu3Uq551FI8FFiostl0y8tTZwaN0Jzil3m48Paxza2eRo6jQ0uYsxutcFCna6wRx0QNCWD5qXE2rdVxpHSvNvZtRyEZ/RIwsQJuTRyCCjScE3fInvQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255551; c=relaxed/simple; bh=zg4Gpqj8nP6uUFsMj+aijEa4Qysw1f63qlOvk3k1K+M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lTAHa48u/1TMcXKeNgmWoqmZa7aRLp1b0ggiFnJQlBwCrmvH7fmnYQvaQ2YY20Tb7VsKV9dHt4NX3T1VJQQ8EOXdVWjluestfb+hURCotjwFqzGPAwxFa70y0OA+HnDMUxPq2EDgzFaFw9L2KN9KesehvXcvgAn34XGHmvkzNHI= 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=e9ypsXuA; 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="e9ypsXuA" 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 DC7691C14; Thu, 24 Sep 2026 06:12:18 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3D3483FAA1; Thu, 24 Sep 2026 06:12:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255542; bh=zg4Gpqj8nP6uUFsMj+aijEa4Qysw1f63qlOvk3k1K+M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e9ypsXuAUEFUhUGiy2kuIWnONzTqWzmAlFPKs4AetqbG7BtTn9f3qUlgSGgefbBFk TolsSjzBbvi0sBApOPtUgZ/la9iLY1I3EWzAWzImW+e6a+yk4iaRJoyXCpF2qrQRgV DNGkB4orI3Y2KQ0YV5/f+7q+t2n9XurcSsoRDn14= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 7/9] mm/memory: use set_softleaf_ptes for uffd-wp markers Date: Thu, 24 Sep 2026 13:09:35 +0000 Message-ID: <20260924131106.1730494-8-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use set_softleaf_ptes() to store multiple uffd-wp marker entries instead of open coding the batched setting. Reviewed-by: Barry Song Signed-off-by: Dev Jain --- mm/memory.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 60fd0a08b35f5..bed5cd90356f4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1700,14 +1700,8 @@ bool cond_install_uffd_wp_ptes(struct vm_area_struct= *vma, if (likely(!arm_uffd_pte)) return false; =20 - for (;;) { - set_pte_at(vma->vm_mm, addr, ptep, - make_pte_marker(PTE_MARKER_UFFD_WP)); - if (--nr_ptes =3D=3D 0) - break; - ptep++; - addr +=3D PAGE_SIZE; - } + set_softleaf_ptes(vma->vm_mm, addr, ptep, + make_pte_marker(PTE_MARKER_UFFD_WP), nr_ptes); =20 return true; } --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EDCDC47C101; Thu, 24 Sep 2026 13:12:33 +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=1790255557; cv=none; b=QO0s7RwIuoPArJPrYb7GPCShOUufzqz0sEBlFBuQvXRT/djeO8E2qy3+HovcSDHn41PhHpaToZIptpPTk6EOchcLq83iA0npEMhFm0EV7yzaV8pjGQpGv3WiNFT570ca9gWh6tsJQRDgBJApFq1BqQtwR6qpuAXcwQsvJM1CDWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255557; c=relaxed/simple; bh=txlwaNOphbyzLjasGFqesnrxD8k7UTgRoeelpj3nSkY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TLcvWS4XmcNCh6baYyPuhb6Iq666kelfeZ9fkvP3z28SQSJeWOCPJuFagEhj1lC7Rd6PLlllh29qS4sTlRH6GVWYnVN/zerK4PjMqwBK1Bt/YzcR/pHVM5l0QEh1Zh6EKOlKHs8sYaW7koTH/f8shkycuTKjI/Lj2wtTZBlc6XA= 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=vFFNGawH; 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="vFFNGawH" 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 8C7141CC4; Thu, 24 Sep 2026 06:12:27 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E86483F86F; Thu, 24 Sep 2026 06:12:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255551; bh=txlwaNOphbyzLjasGFqesnrxD8k7UTgRoeelpj3nSkY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vFFNGawHM/e7japbUpCIeLaxmSCTglVmLe6CGVhgDoMraTuSwgM3dSy92YUHRVh+8 DdxX/c3lgo5YIcIMBT/5paBjc9YQToAHgOopC698l8wAjFV5/c+46Bd6RpgXzZp6AW XmC9aVWOsMUh9AFKFrDlINaDyKJvNqvHfwt/NRsA= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 8/9] mm/rmap: batch unmap anonymous swap-backed large folios Date: Thu, 24 Sep 2026 13:09:36 +0000 Message-ID: <20260924131106.1730494-9-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enable batch clearing of PTEs and batch swap setting of PTEs for anon swap-backed folio unmapping. Processing all PTEs of a large folio in one go helps us batch across atomics (add_mm_counter() etc), barriers in __folio_try_share_anon_rmap(), and repeated calls to page_vma_mapped_walk(). In general, batching helps execute similar code together, making the path more memory and CPU friendly. On arm64-contpte, batching also helps avoid redundant ptep_get() calls and TLB flushes while breaking the contpte mapping. The handling of anon-exclusivity is very similar to commit cac1db8c3aad ("mm: optimize mprotect() by PTE batching"). Since folio_unmap_pte_batch() does not look at the bits of the underlying page, process sub-batches of PTEs pointing to pages with the same exclusivity state, and batch set only those PTEs to swap PTEs in one go. Disable batching for sparc (because of arch_unmap_one), we will batch this later. Rmap accounting and reference accounting must happen when anon folio unmap succeeds. If a large folio is only partially batched or a later sub-batch fails, account only the pages that were actually unmapped. Put that accounting in __ttu_anon_swapbacked_folio() itself instead of using goto jumps at the try_to_unmap_one() callsite. Similarly, do the finish_folio_unmap_batch() in ttu_anon_folio() itself for= the non-swapbacked lazyfree case. If the batch length is less than the number of pages in the folio, skip over this batch. page_vma_mapped_walk() handles this: check_pte() returns true only if any of [pvmw->pfn, pvmw->pfn + nr_pages) is mapped by the PTE. Swap PTEs have no underlying PFN, so check_pte() returns false until the walk reaches the next present PTE to unmap. Remove the finish_unmap label since no goto callers are left now. Signed-off-by: Dev Jain --- mm/rmap.c | 109 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 80 insertions(+), 29 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index fa9fc8374fc26..cdda9efd80bc3 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1966,12 +1966,13 @@ static inline unsigned int folio_unmap_pte_batch(st= ruct folio *folio, end_addr =3D pmd_addr_end(addr, vma->vm_end); max_nr =3D (end_addr - addr) >> PAGE_SHIFT; =20 - /* We only support lazyfree or file folios batching for now ... */ - if (folio_test_anon(folio) && folio_test_swapbacked(folio)) + if (pte_unused(pte)) return 1; =20 - if (pte_unused(pte)) +#ifdef __HAVE_ARCH_UNMAP_ONE + if (folio_test_anon(folio) && folio_test_swapbacked(folio)) return 1; +#endif =20 /* * If unmap fails, we need to restore the ptes. To avoid accidentally @@ -2141,16 +2142,25 @@ static pte_t swp_pte_prepare(swp_entry_t entry, pte= _t old_pte, return swp_pte; } =20 -static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma, +static void finish_folio_unmap_batch(struct vm_area_struct *vma, + struct folio *folio, struct page *page, unsigned long nr_pages) +{ + folio_remove_rmap_ptes(folio, page, nr_pages, vma); + if (vma->vm_flags & VM_LOCKED) + mlock_drain_local(); + folio_put_refs(folio, nr_pages); +} + +static bool __ttu_anon_swapbacked_folio(struct vm_area_struct *vma, struct folio *folio, struct page *page, unsigned long address, - pte_t *ptep, pte_t pteval) + pte_t *ptep, pte_t pteval, unsigned long nr_pages, + bool anon_exclusive) { - const bool anon_exclusive =3D folio_test_anon(folio) && - PageAnonExclusive(page); swp_entry_t entry =3D folio_page_swap_entry(folio, page); struct mm_struct *mm =3D vma->vm_mm; + pte_t swp_pte; =20 - if (folio_dup_swap_pages(folio, page, 1) < 0) + if (folio_dup_swap_pages(folio, page, nr_pages) < 0) return false; =20 /* @@ -2159,21 +2169,57 @@ static bool ttu_anon_swapbacked_folio(struct vm_are= a_struct *vma, * so we'll not check/care. */ if (arch_unmap_one(mm, vma, address, pteval) < 0) { - folio_put_swap_pages(folio, page, 1); + VM_WARN_ON(nr_pages !=3D 1); + folio_put_swap_pages(folio, page, nr_pages); return false; } =20 /* See folio_try_share_anon_rmap(): clear PTE first. */ - if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, page)) { - folio_put_swap_pages(folio, page, 1); + if (anon_exclusive && + folio_try_share_anon_rmap_ptes(folio, page, nr_pages)) { + folio_put_swap_pages(folio, page, nr_pages); return false; } =20 mm_prepare_for_swap_entries(mm); - dec_mm_counter(mm, MM_ANONPAGES); - inc_mm_counter(mm, MM_SWAPENTS); - set_pte_at(mm, address, ptep, - swp_pte_prepare(entry, pteval, anon_exclusive)); + add_mm_counter(mm, MM_ANONPAGES, -nr_pages); + add_mm_counter(mm, MM_SWAPENTS, nr_pages); + swp_pte =3D swp_pte_prepare(entry, pteval, anon_exclusive); + set_softleaf_ptes(mm, address, ptep, swp_pte, nr_pages); + finish_folio_unmap_batch(vma, folio, page, nr_pages); + return true; +} + +static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma, + struct folio *folio, struct page *first_page, + unsigned long address, pte_t *ptep, pte_t pteval, + unsigned long nr_pages) +{ + unsigned long batch_idx =3D 0; + + while (nr_pages) { + bool anon_exclusive =3D PageAnonExclusive(first_page + batch_idx); + unsigned long len =3D page_anon_exclusive_batch(batch_idx, + nr_pages, first_page, anon_exclusive); + + if (!__ttu_anon_swapbacked_folio(vma, folio, + first_page + batch_idx, address, ptep, pteval, + len, anon_exclusive)) { + /* Restore the remaining PTEs that were cleared. */ + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages); + return false; + } + + nr_pages -=3D len; + if (!nr_pages) + break; + + pteval =3D pte_advance_pfn(pteval, len); + address +=3D len * PAGE_SIZE; + batch_idx +=3D len; + ptep +=3D len; + } + return true; } =20 @@ -2186,15 +2232,22 @@ static bool ttu_anon_folio(struct vm_area_struct *v= ma, struct folio *folio, * See handle_pte_fault() ... */ if (WARN_ON_ONCE(folio_test_swapbacked(folio) !=3D - folio_test_swapcache(folio))) + folio_test_swapcache(folio))) { + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages); return false; + } =20 - if (!folio_test_swapbacked(folio)) - return ttu_anon_lazyfree_folio(vma, folio, nr_pages); + if (!folio_test_swapbacked(folio)) { + if (!ttu_anon_lazyfree_folio(vma, folio, nr_pages)) { + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages); + return false; + } + finish_folio_unmap_batch(vma, folio, page, nr_pages); + return true; + } =20 - /* nr_pages > 1 not supported yet */ return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep, - pteval); + pteval, nr_pages); } =20 /* @@ -2374,13 +2427,14 @@ static bool try_to_unmap_one(struct folio *folio, s= truct vm_area_struct *vma, */ dec_mm_counter(mm, mm_counter(folio)); } else if (folio_test_anon(folio)) { + /* finish_folio_unmap_batch handled internally */ if (!ttu_anon_folio(vma, folio, page, address, - pvmw.pte, pteval, nr_pages)) { - set_ptes(mm, address, pvmw.pte, pteval, nr_pages); + pvmw.pte, pteval, nr_pages)) goto walk_abort; - } =20 - goto finish_unmap; + if (nr_pages =3D=3D folio_nr_pages(folio)) + goto walk_done; + continue; } else { /* * This is a locked file-backed folio, @@ -2395,11 +2449,8 @@ static bool try_to_unmap_one(struct folio *folio, st= ruct vm_area_struct *vma, */ add_mm_counter(mm, mm_counter_file(folio), -nr_pages); } -finish_unmap: - folio_remove_rmap_ptes(folio, page, nr_pages, vma); - if (vma->vm_flags & VM_LOCKED) - mlock_drain_local(); - folio_put_refs(folio, nr_pages); + + finish_folio_unmap_batch(vma, folio, page, nr_pages); =20 /* * If we are sure that we batched the entire folio and cleared --=20 2.43.0 From nobody Sat Sep 26 13:46:57 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7631A481658; Thu, 24 Sep 2026 13:12:41 +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=1790255563; cv=none; b=hKdErGnJzZa0RG704f19PilQy0KVYB91550xU23Ql2yWI7+ZK+yEWWl/cQylu15g8V32N3xNwepMg3t8TbT612GolcBlijErtp6g01+IquQTs31Dj9u88pEu25St0r5nW/bdmWS8dwHFn8+dooBe/JW2Nx1HTP3BqekzYoBo2dk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790255563; c=relaxed/simple; bh=rKlBajLDFkN2AhByG94LAOJ8WdXEzVoumoEj/FiHf8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FpOFnL0fB+QXm0bzGmuEcLkBdqh2xkE+J2KzpMkYXnld4lmANRNKINjDLBLXNeXHU1R6og9rnSquPWYwkZn8JMKgpLtdTP2rSbS1W5Ppio50q1UBI37b+Dcl6212Iy73NKSi3hoe02qq6B8avKhIbgNr5bOG5GxZaWcdzxYn+M4= 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=jP960BVS; 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="jP960BVS" 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 38E671C14; Thu, 24 Sep 2026 06:12:36 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 964643F86F; Thu, 24 Sep 2026 06:12:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1790255559; bh=rKlBajLDFkN2AhByG94LAOJ8WdXEzVoumoEj/FiHf8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jP960BVSqgyxTgDWZPsvCQbDyJePhK8xvAxDnAB52vhEKQOxjZMKBeLajhfcJDwNp T6iYh+Lf0LsI37GDyEv25pgdadsx8eRWwkksQYEbgm6exutQP0v6ATCTsyRNdQEvJN eG+1UldqwEENt1HdcTcUDvYDbjM9nebJVX508sv4= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com, davem@davemloft.net, andreas@gaisler.com Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, jgg@ziepe.ca, thuth@redhat.com, sparclinux@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH v3 9/9] mm, sparc: batch arch_unmap_one() Date: Thu, 24 Sep 2026 13:09:37 +0000 Message-ID: <20260924131106.1730494-10-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924131106.1730494-1-dev.jain@arm.com> References: <20260924131106.1730494-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The anonymous large-folio unmap batching path can clear and replace multiple PTEs at once, but architectures like sparc need arch_unmap_one() to preserve per-page metadata before swap entries are installed. sparc uses this hook to save ADI tags. Extend arch_unmap_one() to operate on a pte range. sparc uses the count to walk the PTE range and save ADI tags for entries carrying _PAGE_MCD_4V. There is no reverse operation for a partially saved range: saving ADI tags only records a copy for later restore, so if a later entry fails and the core keeps the original PTEs, there is no architectural state to unsave. Signed-off-by: Dev Jain --- arch/sparc/include/asm/pgtable_64.h | 10 +++++++--- arch/sparc/kernel/adi_64.c | 21 +++++++++++++++++++++ include/linux/pgtable.h | 6 +++--- mm/rmap.c | 11 +++-------- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/p= gtable_64.h index 44d1333065a6a..f7dd1872884e4 100644 --- a/arch/sparc/include/asm/pgtable_64.h +++ b/arch/sparc/include/asm/pgtable_64.h @@ -1037,6 +1037,9 @@ void adi_restore_tags(struct mm_struct *mm, struct vm= _area_struct *vma, int adi_save_tags(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, pte_t oldpte); =20 +int adi_save_tags_range(struct mm_struct *mm, struct vm_area_struct *vma, + unsigned long addr, pte_t oldpte, unsigned long nr); + #define __HAVE_ARCH_DO_SWAP_PAGE static inline void arch_do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, @@ -1057,10 +1060,11 @@ static inline void arch_do_swap_page(struct mm_stru= ct *mm, #define __HAVE_ARCH_UNMAP_ONE static inline int arch_unmap_one(struct mm_struct *mm, struct vm_area_struct *vma, - unsigned long addr, pte_t oldpte) + unsigned long addr, pte_t oldpte, + unsigned long nr) { - if (adi_state.enabled && (pte_val(oldpte) & _PAGE_MCD_4V)) - return adi_save_tags(mm, vma, addr, oldpte); + if (adi_state.enabled) + return adi_save_tags_range(mm, vma, addr, oldpte, nr); return 0; } =20 diff --git a/arch/sparc/kernel/adi_64.c b/arch/sparc/kernel/adi_64.c index 18036a43cf568..ffa378accefe3 100644 --- a/arch/sparc/kernel/adi_64.c +++ b/arch/sparc/kernel/adi_64.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -394,3 +395,23 @@ int adi_save_tags(struct mm_struct *mm, struct vm_area= _struct *vma, =20 return 0; } + +int adi_save_tags_range(struct mm_struct *mm, struct vm_area_struct *vma, + unsigned long addr, pte_t oldpte, unsigned long nr) +{ + unsigned long i; + + for (i =3D 0; i < nr; i++, addr +=3D PAGE_SIZE, + oldpte =3D pte_next_pfn(oldpte)) { + int ret; + + if (!(pte_val(oldpte) & _PAGE_MCD_4V)) + continue; + + ret =3D adi_save_tags(mm, vma, addr, oldpte); + if (ret) + return ret; + } + + return 0; +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index e3c8ab96941c5..d5c78dc938760 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1431,12 +1431,12 @@ static inline void arch_do_swap_page_nr(struct mm_s= truct *mm, * restored when the page is swapped back in. SPARC M7 and newer * processors support an ADI (Application Data Integrity) tag for the * page as metadata for the page. arch_unmap_one() can save this - * metadata on a swap-out of a page. + * metadata on swap-out of one or more pages. */ static inline int arch_unmap_one(struct mm_struct *mm, struct vm_area_struct *vma, - unsigned long addr, - pte_t orig_pte) + unsigned long addr, pte_t orig_pte, + unsigned long nr) { return 0; } diff --git a/mm/rmap.c b/mm/rmap.c index cdda9efd80bc3..774ddc75194b1 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1959,6 +1959,7 @@ static inline unsigned int folio_unmap_pte_batch(stru= ct folio *folio, =20 if (flags & TTU_HWPOISON) return 1; + if (!folio_test_large(folio)) return 1; =20 @@ -1969,11 +1970,6 @@ static inline unsigned int folio_unmap_pte_batch(str= uct folio *folio, if (pte_unused(pte)) return 1; =20 -#ifdef __HAVE_ARCH_UNMAP_ONE - if (folio_test_anon(folio) && folio_test_swapbacked(folio)) - return 1; -#endif - /* * If unmap fails, we need to restore the ptes. To avoid accidentally * upgrading write permissions for ptes that were not originally @@ -2168,8 +2164,7 @@ static bool __ttu_anon_swapbacked_folio(struct vm_are= a_struct *vma, * architectures where we could have PFN swap PTEs, * so we'll not check/care. */ - if (arch_unmap_one(mm, vma, address, pteval) < 0) { - VM_WARN_ON(nr_pages !=3D 1); + if (arch_unmap_one(mm, vma, address, pteval, nr_pages) < 0) { folio_put_swap_pages(folio, page, nr_pages); return false; } @@ -2746,7 +2741,7 @@ static bool try_to_migrate_one(struct folio *folio, s= truct vm_area_struct *vma, * architectures where we could have PFN swap PTEs, * so we'll not check/care. */ - if (arch_unmap_one(mm, vma, address, pteval) < 0) { + if (arch_unmap_one(mm, vma, address, pteval, 1) < 0) { if (folio_test_hugetlb(folio)) set_huge_pte_at(mm, address, pvmw.pte, pteval, hsz); --=20 2.43.0