[PATCH v3 0/3] mm/rmap: batch the unmap of large folios during migration

From: Shivank Garg

Date: Thu Nov 26 2026 - 04:53:38 EST


Migrating a PTE-mapped large folio replaces its present PTEs with
migration entries one page at a time: try_to_migrate_one() repeats
page_vma_mapped_walk(), the PTE clear and flush, the migration entry
setup and the rmap/refcount update for every subpage (256 iterations for
a 1M folio).

This series lets try_to_migrate_one() convert a batch of consecutive PTEs
that map the same large folio in one go, mirroring what
try_to_unmap_one() already does, with some refactoring on the way.

Besides the walk overhead, batching avoids a spurious deferred split:
unmapping an anonymous mTHP one PTE at a time makes it look partially
mapped partway through, so it is queued for deferred split even though it
is about to be fully unmapped. Joanne's underused mTHP shrinker series [1]
sends every underused mTHP split through this path, which makes this a
blocker there [2].

Performance:

AMD EPYC Zen5, 2-socket system (1 node per socket), mitigations=off.
move_pages() migrating 1GB between the two nodes, mTHP of the given
size; base is mm-core/for-next + Dev's v5 [3]. Throughput in GB/s,
mean of 40 runs (2 boots, both directions):

folio base patched gain
4K 3.39±0.02 3.39±0.02 +0.1%
16K 4.65±0.02 5.12±0.04 +10.3%
64K 5.51±0.04 6.46±0.05 +17.2%
256K 5.78±0.07 7.47±0.01 +29.2%
1M 5.94±0.02 7.64±0.02 +28.6%
2M 9.09±0.03 9.08±0.03 -0.2% (PMD-mapped)

Time spent in try_to_migrate() during that move (ftrace function
profiler, mean of 6 runs):

folio base patched delta
16K 43.7 ms 28.2 ms -35%
64K 45.6 ms 20.0 ms -56%
256K 44.2 ms 8.1 ms -82%
1M 41.8 ms 7.1 ms -83%

On AMD EPYC Zen3 (refer V2 cover), the unmap-side batching gave
around +25.0% at 64K, +38.4% at 256K and +39.5% at 1M.

The restore-side batching from v2 is split out and will be posted
separately.

Applies on mm-core/for-next + Dev's "Optimize anonymous swapbacked large
folio unmapping" v5 [3].

[1] https://lore.kernel.org/linux-mm/20261103212004.1356327-1-joannelkoong@xxxxxxxxx/
[2] https://lore.kernel.org/linux-mm/20261109220958.2071687-1-joannelkoong@xxxxxxxxx/
[3] https://lore.kernel.org/linux-mm/20261123053543.295467-1-dev.jain@xxxxxxx/

Signed-off-by: Shivank Garg <shivankg@xxxxxxx>
---
Changes in v3:
- try_to_migrate_one(): memory_failure() can poison a tail page without
holding the folio lock, so check PageHWPoison for every subpage in the
batch. (Lance, Miaohe, Ying)
- Split the series: this posting is only the unmap side changes. Restore
side batching will be posted separately.
- Rebase onto mm-core/for-next and Dev's series v5; adapt to
finish_folio_unmap_batch() and page_anon_exclusive_batch() changes.
- Use vma_test_any_mask() in try_to_migrate_hugetlb_one()
- Link to v2: https://lore.kernel.org/r/20260813-migrate-rmap-batch-v2-0-3c5424c555c7@xxxxxxx

Changes in v2:

- Rebase onto mm-new, drop the borrowed folio_try_share_anon_rmap_ptes() patch
and add it as dependency on Dev's series and adapt to recent migrate/rmap changes.
- Switch to recently added batched helpers - finish_folio_unmap, set_softleaf_ptes,
and page_anon_exclusive_batch.
- Rename migration_softleaf_entry_to_pte() to migration_entry_to_pte(). (David)
- Split PTE construction from hugetlb handling into separate patch. (David)
Mirror the split in mm/rmap.c patch as well.
- Rename make_migration_swp_pte() to make_migration_pte().
- Early exit the single-entry hugetlb page_vma_mapped_walk() paths - removes
unnecessary nesting.
- Add the missing page_vma_mapped_walk_done() in remove_migration_pte_hugetlb(),
which leaked the huge-PTE lock. (syzbot ci, Sashiko)
- Fix handling of hwpoisoned subpages in the batched unmap. Read PageHWPoison()
once and skip batching when mapped page is poisoned, and use same result for
hwpoison handling. (Sashiko)
- Remove the pte_unused(pteval) handling from try_to_migrate_hugetlb_one() (Sashiko).
- Link to v1: https://lore.kernel.org/r/20260712-migrate-rmap-batch-v1-0-872a734431d1@xxxxxxx

---
Shivank Garg (3):
mm/rmap: factor out migration PTE construction
mm/rmap: split try_to_migrate_hugetlb_one() out of try_to_migrate_one()
mm/rmap: batch the unmap of large folios in try_to_migrate_one()

mm/rmap.c | 434 +++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 277 insertions(+), 157 deletions(-)
---
base-commit: 518cbccb58e4ff8ce9d7ed609036c559dfdcf62e
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mm/core.git for-next
change-id: 20260701-migrate-rmap-batch-8ecc04de1905
prerequisite-message-id: 20261123053543.295467-1-dev.jain@xxxxxxx
prerequisite-patch-id: 5dc23d00736b03a60a420bce56fdb59aabb57d97
prerequisite-patch-id: 87ecef5648d4102fd0ac1d2c1875aee3476635eb
prerequisite-patch-id: 43f01b72e0fdde751109e0f471c30b15d9f192ac
prerequisite-patch-id: 18bf3505aa2499f4e18446b4930e503d4085346d
prerequisite-patch-id: e79055c5dcb6fae5cb425feddec0e809959fd563
prerequisite-patch-id: 98bc99c6bc274594946d022b3998f5f1dc70012f
prerequisite-patch-id: dd35ec00b82bf235f2e41e286d16eab6a3fd1e9e
prerequisite-patch-id: d6f05c7d00e839fea93a5637b68348a1df345cc4

Best regards,
--
Shivank Garg <shivankg@xxxxxxx>