* [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping
@ 2026-07-23 7:08 Dev Jain
2026-07-23 7:08 ` [PATCH 1/8] mm/swapfile: add batched version of folio_dup_swap Dev Jain
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:08 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
Speed up unmapping of anonymous swapbacked large folios by clearing
the ptes, and setting swap ptes, in one go.
The following benchmark (stolen from Barry) is used to measure the
time taken to swapout 256M worth of memory backed by 64K large folios:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#define SIZE_MB 256
#define SIZE_BYTES (SIZE_MB * 1024 * 1024)
int main() {
void *addr = mmap(NULL, SIZE_BYTES, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == 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) != 0) {
perror("madvise(MADV_PAGEOUT) failed");
munmap(addr, SIZE_BYTES);
return 1;
}
clock_gettime(CLOCK_MONOTONIC, &end);
long duration_ns = (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;
}
Performance as measured on a Linux VM on Apple M3 (arm64):
Vanilla - Mean: 37401913 ns, std dev: 12%
Patched - Mean: 17420282 ns, std dev: 11%
resulting in more than 2x speedup.
No regression observed on 4K folios.
Performance as measured on bare metal x86:
Vanilla - mean: 54986286 ns, std dev: 1.5%
Patched - mean: 51930795 ns, std dev: 3%
I tried magnifying the difference on x86 by using 1M large folios, but
can't spot an obvious improvement (looks like my system is too fast to
benefit from batched atomic operations!), hinting that the benefit lies
mainly in the reduction of ptep_get() calls and the reduction of TLB
flushes during contpte-unfolding, on arm64.
No regression is observed on 4K folios on x86 too.
---
Applies on mm-new (3d18f3499c48). mm-selftests pass.
This breakout patchset is the final completion of
https://lore.kernel.org/all/20260526063635.61721-1-dev.jain@arm.com/
The extra addition is the generic set_softleaf_ptes() helper.
Dev Jain (8):
mm/swapfile: add batched version of folio_dup_swap
mm/swapfile: add batched version of folio_put_swap
mm/rmap: mm/rmap: Add batched version of folio_try_share_anon_rmap_pte
mm/internal: rename swap offset helpers to softleaf offset
mm/internal: add set_softleaf_ptes
mm/memory: use set_softleaf_ptes for uffd-wp markers
mm: move anon-exclusive batch helper to internal.h
mm/rmap: batch unmap anonymous swap-backed large folios
include/linux/rmap.h | 51 +++++++++++++-------
mm/internal.h | 79 +++++++++++++++++++++++++------
mm/memory.c | 20 +++-----
mm/mprotect.c | 17 -------
mm/rmap.c | 109 +++++++++++++++++++++++++++++++------------
mm/shmem.c | 8 ++--
mm/swap.h | 35 ++++++++++++--
mm/swapfile.c | 31 ++++++------
8 files changed, 235 insertions(+), 115 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/8] mm/swapfile: add batched version of folio_dup_swap
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
@ 2026-07-23 7:08 ` Dev Jain
2026-07-23 7:08 ` [PATCH 2/8] mm/swapfile: add batched version of folio_put_swap Dev Jain
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:08 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
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 == 1 and
nr_pages == folio_nr_pages(folio). Generalize this to any nr_pages.
Currently we have a not-so-nice logic of passing in subpage == NULL if
we mean to exercise the logic on the entire folio, and subpage != 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) <david@kernel.org>
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/rmap.c | 2 +-
mm/shmem.c | 2 +-
mm/swap.h | 18 ++++++++++++++++--
mm/swapfile.c | 14 ++++++--------
4 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/mm/rmap.c b/mm/rmap.c
index b7ead3e9f0641..4ca809a596dfe 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2148,7 +2148,7 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
swp_entry_t entry = page_swap_entry(page);
struct mm_struct *mm = vma->vm_mm;
- if (folio_dup_swap(folio, page) < 0)
+ if (folio_dup_swap_pages(folio, page, 1) < 0)
return false;
/*
diff --git a/mm/shmem.c b/mm/shmem.c
index 5071177059a96..501365158f8d8 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1698,7 +1698,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
spin_unlock(&shmem_swaplist_lock);
}
- folio_dup_swap(folio, NULL);
+ folio_dup_swap(folio);
shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap));
BUG_ON(folio_mapped(folio));
diff --git a/mm/swap.h b/mm/swap.h
index abd26588abd22..bbf665a33014b 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -251,7 +251,8 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_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);
/* For internal use */
@@ -375,7 +376,8 @@ static inline int folio_alloc_swap(struct folio *folio)
return -EINVAL;
}
-static inline int folio_dup_swap(struct folio *folio, struct page *page)
+static inline int folio_dup_swap_pages(struct folio *folio, struct page *page,
+ unsigned long nr_pages)
{
return -EINVAL;
}
@@ -470,6 +472,18 @@ static inline void __swap_cache_replace_folio(struct swap_cluster_info *ci,
}
#endif /* CONFIG_SWAP */
+/**
+ * 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;
int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 5d15913dcf863..0709a6266cf1f 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1785,9 +1785,10 @@ int folio_alloc_swap(struct folio *folio)
}
/**
- * 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 == 0 and pinned
@@ -1801,18 +1802,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 = folio->swap;
- unsigned long nr_pages = folio_nr_pages(folio);
VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
- if (page) {
- entry.val += folio_page_idx(folio, page);
- nr_pages = 1;
- }
+ entry.val += folio_page_idx(folio, page);
return swap_dup_entries_cluster(swap_entry_to_info(entry),
swp_offset(entry), nr_pages);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/8] mm/swapfile: add batched version of folio_put_swap
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
2026-07-23 7:08 ` [PATCH 1/8] mm/swapfile: add batched version of folio_dup_swap Dev Jain
@ 2026-07-23 7:08 ` Dev Jain
2026-07-23 7:08 ` [PATCH 3/8] mm/rmap: mm/rmap: Add batched version of folio_try_share_anon_rmap_pte Dev Jain
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:08 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
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 == 1 and
nr_pages == folio_nr_pages(folio). Generalize this to any nr_pages.
Currently we have a not-so-nice logic of passing in subpage == NULL if
we mean to exercise the logic on the entire folio, and subpage != 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) <david@kernel.org>
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/memory.c | 6 +++---
mm/rmap.c | 4 ++--
mm/shmem.c | 6 +++---
mm/swap.h | 17 +++++++++++++++--
mm/swapfile.c | 17 ++++++++---------
5 files changed, 31 insertions(+), 19 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index a620d425ec95e..d88df15018268 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5198,7 +5198,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
if (unlikely(folio != 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
@@ -5207,12 +5207,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
VM_WARN_ON_ONCE_FOLIO(folio_nr_pages(folio) != 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 != 1 && nr_pages != folio_nr_pages(folio));
folio_add_anon_rmap_ptes(folio, page, nr_pages, vma, address,
rmap_flags);
- folio_put_swap(folio, nr_pages == 1 ? page : NULL);
+ folio_put_swap_pages(folio, page, nr_pages);
}
VM_BUG_ON(!folio_test_anon(folio) ||
diff --git a/mm/rmap.c b/mm/rmap.c
index 4ca809a596dfe..c1988dae8d4a2 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2157,13 +2157,13 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_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;
}
/* 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;
}
diff --git a/mm/shmem.c b/mm/shmem.c
index 501365158f8d8..bd7fae7033644 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -1719,7 +1719,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *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);
}
/*
@@ -2149,7 +2149,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
nr_pages = 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
@@ -2379,7 +2379,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
if (sgp == SGP_WRITE)
folio_mark_accessed(folio);
- 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 bbf665a33014b..aae1fc9b6addf 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -253,7 +253,8 @@ extern int swap_retry_table_alloc(swp_entry_t entry, gfp_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);
/* For internal use */
extern void __swap_cluster_free_entries(struct swap_info_struct *si,
@@ -382,7 +383,8 @@ static inline int folio_dup_swap_pages(struct folio *folio, struct page *page,
return -EINVAL;
}
-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)
{
}
@@ -484,6 +486,17 @@ static inline int folio_dup_swap(struct folio *folio)
folio_nr_pages(folio));
}
+/**
+ * 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;
int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio,
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 0709a6266cf1f..ef214b924bf50 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1817,27 +1817,25 @@ int folio_dup_swap_pages(struct folio *folio, struct page *page,
}
/**
- * 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 locked.
- * @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 are
* still pinned by the swap cache. User may call folio_free_swap to free them.
* 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 = folio->swap;
- unsigned long nr_pages = folio_nr_pages(folio);
struct swap_info_struct *si = __swap_entry_to_info(entry);
VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio);
VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
- if (page) {
- entry.val += folio_page_idx(folio, page);
- nr_pages = 1;
- }
+ entry.val += folio_page_idx(folio, page);
swap_put_entries_cluster(si, swp_offset(entry), nr_pages, false);
}
@@ -2570,7 +2568,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
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);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/8] mm/rmap: mm/rmap: Add batched version of folio_try_share_anon_rmap_pte
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
2026-07-23 7:08 ` [PATCH 1/8] mm/swapfile: add batched version of folio_dup_swap Dev Jain
2026-07-23 7:08 ` [PATCH 2/8] mm/swapfile: add batched version of folio_put_swap Dev Jain
@ 2026-07-23 7:08 ` Dev Jain
2026-07-23 7:09 ` [PATCH 4/8] mm/internal: rename swap offset helpers to softleaf offset Dev Jain
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:08 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
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 == 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 = 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.
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
include/linux/rmap.h | 51 +++++++++++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 17 deletions(-)
diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index a174758f77775..4c66c83d2228e 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -706,17 +706,18 @@ static inline int folio_try_dup_anon_rmap_pmd(struct folio *folio,
}
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 = !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);
- /* device private folios cannot get pinned via GUP. */
- if (unlikely(folio_is_device_private(folio))) {
- ClearPageAnonExclusive(page);
- return 0;
- }
+ /* We only clear anon-exclusive from head page of PMD folio. */
+ if (level == PGTABLE_LEVEL_PMD)
+ nr_pages = 1;
/*
* We have to make sure that when we clear PageAnonExclusive, that
@@ -760,29 +761,38 @@ static __always_inline int __folio_try_share_anon_rmap(struct folio *folio,
* so we use explicit ones here.
*/
- /* 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();
- 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 == 0)
+ break;
+ page++;
+ }
/*
* 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;
}
/**
- * 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 table
* entries cleared/invalidated.
@@ -798,10 +808,17 @@ static __always_inline int __folio_try_share_anon_rmap(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);
}
/**
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 4/8] mm/internal: rename swap offset helpers to softleaf offset
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
` (2 preceding siblings ...)
2026-07-23 7:08 ` [PATCH 3/8] mm/rmap: mm/rmap: Add batched version of folio_try_share_anon_rmap_pte Dev Jain
@ 2026-07-23 7:09 ` Dev Jain
2026-07-23 7:09 ` [PATCH 5/8] mm/internal: add set_softleaf_ptes Dev Jain
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:09 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
In preparation for adding a helper to set softleaf ptes in one go,
generalize the swap entry helpers shifting the swap offset by delta,
for softleaves.
Note that the soft-dirty bit, exclusive bit and uffd bit preservation
will still work for non-swap softleaves, since a softleaf entry is
constructed out of a type and offset, and those bits are ahead of
the soft-dirty, exclusive and uffd bits.
For example, for a migration entry, pte_swp_exclusive() will return
false, as the exclusivity is encoded in the type itself
(SOFTLEAF_MIGRATION_READ_EXCLUSIVE).
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/internal.h | 29 +++++++++++++++--------------
mm/memory.c | 4 ++--
2 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 51ee4c1310908..ce52188a8a65d 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -475,16 +475,16 @@ unsigned int folio_pte_batch(struct folio *folio, pte_t *ptep, pte_t pte,
unsigned int max_nr);
/**
- * 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 = softleaf_from_pte(pte);
pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry),
@@ -502,15 +502,16 @@ static inline pte_t pte_move_swp_offset(pte_t pte, long delta)
/**
- * pte_next_swp_offset - Increment the swap entry offset field of a swap pte.
- * @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, including
- * swap type, and any swp pte bits. The resulting pte is returned.
+ * Increments 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_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);
}
/**
@@ -530,7 +531,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 = pte_next_swp_offset(pte);
+ pte_t expected_pte = pte_next_softleaf_offset(pte);
const pte_t *end_ptep = start_ptep + max_nr;
pte_t *ptep = start_ptep + 1;
@@ -542,7 +543,7 @@ static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
if (!pte_same(pte, expected_pte))
break;
- expected_pte = pte_next_swp_offset(expected_pte);
+ expected_pte = pte_next_softleaf_offset(expected_pte);
ptep++;
}
diff --git a/mm/memory.c b/mm/memory.c
index d88df15018268..7a8442fda0b51 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4753,7 +4753,7 @@ static bool can_swapin_thp(struct vm_fault *vmf, pte_t *ptep, int nr_pages)
idx = (vmf->address - addr) / PAGE_SIZE;
pte = ptep_get(ptep);
- 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
@@ -5062,7 +5062,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
folio_ptep = vmf->pte - idx;
folio_pte = 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) != nr)
goto check_folio;
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5/8] mm/internal: add set_softleaf_ptes
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
` (3 preceding siblings ...)
2026-07-23 7:09 ` [PATCH 4/8] mm/internal: rename swap offset helpers to softleaf offset Dev Jain
@ 2026-07-23 7:09 ` Dev Jain
2026-07-23 7:09 ` [PATCH 6/8] mm/memory: use set_softleaf_ptes for uffd-wp markers Dev Jain
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:09 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
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.
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/internal.h | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/mm/internal.h b/mm/internal.h
index ce52188a8a65d..3fc1c1c776ca3 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -514,6 +514,39 @@ static inline pte_t pte_next_softleaf_offset(pte_t pte)
return pte_move_softleaf_offset(pte, 1);
}
+/**
+ * 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 offsets,
+ * 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 addr,
+ pte_t *ptep, pte_t pte, unsigned long nr)
+{
+ softleaf_t entry;
+ bool advance;
+
+ entry = softleaf_from_pte(pte);
+ advance = softleaf_is_swap(entry) || softleaf_has_pfn(entry);
+
+ for (;;) {
+ set_pte_at(mm, addr, ptep, pte);
+ if (--nr == 0)
+ break;
+ if (advance)
+ pte = pte_next_softleaf_offset(pte);
+ ptep++;
+ addr += 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.
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 6/8] mm/memory: use set_softleaf_ptes for uffd-wp markers
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
` (4 preceding siblings ...)
2026-07-23 7:09 ` [PATCH 5/8] mm/internal: add set_softleaf_ptes Dev Jain
@ 2026-07-23 7:09 ` Dev Jain
2026-07-23 7:09 ` [PATCH 7/8] mm: move anon-exclusive batch helper to internal.h Dev Jain
2026-07-23 7:09 ` [PATCH 8/8] mm/rmap: batch unmap anonymous swap-backed large folios Dev Jain
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:09 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
Use set_softleaf_ptes() to store multiple uffd-wp marker entries instead
of open coding the batched setting.
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/memory.c | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 7a8442fda0b51..390ea3aefbccb 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1733,14 +1733,8 @@ bool cond_install_uffd_wp_ptes(struct vm_area_struct *vma,
if (likely(!arm_uffd_pte))
return false;
- for (;;) {
- set_pte_at(vma->vm_mm, addr, ptep,
- make_pte_marker(PTE_MARKER_UFFD_WP));
- if (--nr_ptes == 0)
- break;
- ptep++;
- addr += PAGE_SIZE;
- }
+ set_softleaf_ptes(vma->vm_mm, addr, ptep,
+ make_pte_marker(PTE_MARKER_UFFD_WP), nr_ptes);
return true;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 7/8] mm: move anon-exclusive batch helper to internal.h
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
` (5 preceding siblings ...)
2026-07-23 7:09 ` [PATCH 6/8] mm/memory: use set_softleaf_ptes for uffd-wp markers Dev Jain
@ 2026-07-23 7:09 ` Dev Jain
2026-07-23 7:09 ` [PATCH 8/8] mm/rmap: batch unmap anonymous swap-backed large folios Dev Jain
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:09 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
In preparation for optimizing large folio unmapping, we need to reuse
the page_anon_exclusive_batch helper in rmap.c. Therefore, move it from
mprotect.c to internal.h.
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/internal.h | 17 +++++++++++++++++
mm/mprotect.c | 17 -----------------
2 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 3fc1c1c776ca3..df799d0c24e0b 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -474,6 +474,23 @@ static inline unsigned int folio_pte_batch_flags(struct folio *folio,
unsigned int folio_pte_batch(struct folio *folio, pte_t *ptep, pte_t pte,
unsigned int max_nr);
+/*
+ * Get max length of consecutive PTEs pointing to PageAnonExclusive() pages 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 max_len,
+ struct page *first_page, bool expected_anon_exclusive)
+{
+ int idx;
+
+ for (idx = start_idx + 1; idx < start_idx + max_len; ++idx) {
+ if (expected_anon_exclusive != PageAnonExclusive(first_page + idx))
+ break;
+ }
+ return idx - start_idx;
+}
+
/**
* pte_move_softleaf_offset - Move the softleaf entry offset field of a
* softleaf pte forward or backward by delta
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 2888ee638d872..75c2b1a00eb36 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -138,23 +138,6 @@ static __always_inline void prot_commit_flush_ptes(struct vm_area_struct *vma,
tlb_flush_pte_range(tlb, addr, nr_ptes * PAGE_SIZE);
}
-/*
- * Get max length of consecutive ptes pointing to PageAnonExclusive() pages 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 max_len,
- struct page *first_page, bool expected_anon_exclusive)
-{
- int idx;
-
- for (idx = start_idx + 1; idx < start_idx + max_len; ++idx) {
- if (expected_anon_exclusive != 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_writable(),
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 8/8] mm/rmap: batch unmap anonymous swap-backed large folios
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
` (6 preceding siblings ...)
2026-07-23 7:09 ` [PATCH 7/8] mm: move anon-exclusive batch helper to internal.h Dev Jain
@ 2026-07-23 7:09 ` Dev Jain
7 siblings, 0 replies; 9+ messages in thread
From: Dev Jain @ 2026-07-23 7:09 UTC (permalink / raw)
To: akpm, david, ljs, hughd, chrisl, kasong
Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang,
baolin.wang, shikemeng, nphamcs, baoquan.he, baohua,
youngjun.park, linux-mm, linux-kernel, rppt, surenb, mhocko,
pfalcato, ryan.roberts, anshuman.khandual
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 the function
__folio_try_share_anon_rmap), repeated calls to page_vma_mapped_walk(),
to name a few. In general, batching helps us to execute similar code
together, making the execution of the program more memory and
CPU friendly.
On arm64-contpte, batching also helps us 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()
won't look at the bits of the underlying page, we need to process
sub-batches of ptes pointing to pages which are same w.r.t exclusivity,
and batch set only those ptes to swap ptes in one go.
arch_unmap_one() is only defined for sparc64; I am not comfortable
regarding the nuances between retrieving the pfn from pte_pfn() or from
(paddr = pte_val(oldpte) & _PAGE_PADDR_4V).
(And, pte_next_pfn() can't even be called from arch_unmap_one() because
that file does not include pgtable.h) So just disable the
"sparc64-anon-swapbacked" case for now.
We need to take care of rmap accounting (folio_remove_rmap_ptes) and
reference accounting (folio_put_refs) when anon folio unmap succeeds.
In case we partially batch the large folio and fail, we need to correctly
do the accounting for pages which were successfully unmapped. So, put
this accounting code (which is finish_folio_unmap()) in
__ttu_anon_swapbacked_folio() itself, instead of doing some horrible
goto jumping at the callsite of ttu_anon_folio().
Similarly, do the finish_folio_unmap() 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, then
we must skip over this batch.
The page_vma_mapped_walk API ensures this - check_pte() will return true
only if any of [pvmw->pfn, pvmw->pfn + nr_pages) is mapped by the pte.
There is no pfn underlying a swap pte, so check_pte returns false and we
keep skipping until we hit a present pte, which is where we want to start
unmapping from next.
Remove the label finish_unmap since no goto callers are left now.
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
mm/rmap.c | 109 +++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 80 insertions(+), 29 deletions(-)
diff --git a/mm/rmap.c b/mm/rmap.c
index c1988dae8d4a2..d28a113e8c705 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1964,11 +1964,11 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio,
end_addr = pmd_addr_end(addr, vma->vm_end);
max_nr = (end_addr - addr) >> PAGE_SHIFT;
- /* 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;
- if (pte_unused(pte))
+ if (__is_defined(__HAVE_ARCH_UNMAP_ONE) && folio_test_anon(folio) &&
+ folio_test_swapbacked(folio))
return 1;
/*
@@ -2139,16 +2139,25 @@ static pte_t swp_pte_prepare(swp_entry_t entry, pte_t old_pte,
return swp_pte;
}
-static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
+static void finish_folio_unmap(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 = folio_test_anon(folio) &&
- PageAnonExclusive(page);
swp_entry_t entry = page_swap_entry(page);
struct mm_struct *mm = vma->vm_mm;
+ pte_t swp_pte;
- if (folio_dup_swap_pages(folio, page, 1) < 0)
+ if (folio_dup_swap_pages(folio, page, nr_pages) < 0)
return false;
/*
@@ -2157,21 +2166,57 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_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 != 1);
+ folio_put_swap_pages(folio, page, nr_pages);
return false;
}
/* 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;
}
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 = swp_pte_prepare(entry, pteval, anon_exclusive);
+ set_softleaf_ptes(mm, address, ptep, swp_pte, nr_pages);
+ finish_folio_unmap(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 = 0;
+
+ while (nr_pages) {
+ bool anon_exclusive = PageAnonExclusive(first_page + batch_idx);
+ unsigned long len = 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 -= len;
+ if (!nr_pages)
+ break;
+
+ pteval = pte_advance_pfn(pteval, len);
+ address += len * PAGE_SIZE;
+ batch_idx += len;
+ ptep += len;
+ }
+
return true;
}
@@ -2184,15 +2229,22 @@ static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
* See handle_pte_fault() ...
*/
if (WARN_ON_ONCE(folio_test_swapbacked(folio) !=
- folio_test_swapcache(folio)))
+ folio_test_swapcache(folio))) {
+ set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages);
return false;
+ }
- 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(vma, folio, page, nr_pages);
+ return true;
+ }
- /* nr_pages > 1 not supported yet */
return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep,
- pteval);
+ pteval, nr_pages);
}
/*
@@ -2373,13 +2425,15 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
*/
dec_mm_counter(mm, mm_counter(folio));
} else if (folio_test_anon(folio)) {
+
+ /* finish_folio_unmap 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;
- }
- goto finish_unmap;
+ if (nr_pages == folio_nr_pages(folio))
+ goto walk_done;
+ continue;
} else {
/*
* This is a locked file-backed folio,
@@ -2394,11 +2448,8 @@ static bool try_to_unmap_one(struct folio *folio, struct 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(vma, folio, page, nr_pages);
/*
* If we are sure that we batched the entire folio and cleared
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-07-23 7:10 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-23 7:08 [PATCH 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
2026-07-23 7:08 ` [PATCH 1/8] mm/swapfile: add batched version of folio_dup_swap Dev Jain
2026-07-23 7:08 ` [PATCH 2/8] mm/swapfile: add batched version of folio_put_swap Dev Jain
2026-07-23 7:08 ` [PATCH 3/8] mm/rmap: mm/rmap: Add batched version of folio_try_share_anon_rmap_pte Dev Jain
2026-07-23 7:09 ` [PATCH 4/8] mm/internal: rename swap offset helpers to softleaf offset Dev Jain
2026-07-23 7:09 ` [PATCH 5/8] mm/internal: add set_softleaf_ptes Dev Jain
2026-07-23 7:09 ` [PATCH 6/8] mm/memory: use set_softleaf_ptes for uffd-wp markers Dev Jain
2026-07-23 7:09 ` [PATCH 7/8] mm: move anon-exclusive batch helper to internal.h Dev Jain
2026-07-23 7:09 ` [PATCH 8/8] mm/rmap: batch unmap anonymous swap-backed large folios Dev Jain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox