[PATCH v4 0/6] mm: copy page ranges with one copy_mc_to_kernel() call
From: Shivank Garg
Date: Tue Dec 01 2026 - 03:30:47 EST
On a Zen 6 system with CXL memory, move_pages() gets ~1.4x faster for
2MB THPs and up to ~2.8x faster for 1GB hugetlb pages, and when demoting
to CXL, kswapd uses ~31% less CPU per GB demoted. Hugetlb copy on write
gets ~1.5x faster for 2MB pages and ~2x faster for 1GB pages, and
MADV_COLLAPSE of a tmpfs file made of large folios gets ~11% faster
(numbers below).
Copying a folio with machine check handling makes one
copy_mc_to_kernel() call per page. This series copies a page range with
a single call where possible (x86-64 and ppc64), for folio migration,
hugetlb copy on write and khugepaged file collapse.
Test setup: AMD EPYC Zen 6, single socket, NPS2 (nodes 0 and 1 are
DRAM, node 2 is CXL), base (mm/core.git for-next) vs the whole series.
move_pages() throughput
-----------------------
A single move_pages() call migrates 1GB of anonymous memory, as 512
2MB THPs or as four 1GB hugetlb pages. Mean ± stddev, GiB/s:
2MB THP:
direction base patched speedup
DRAM0 -> DRAM1 19.57 ± 0.17 27.51 ± 0.78 1.41x
DRAM1 -> DRAM0 19.44 ± 0.12 27.33 ± 0.27 1.41x
DRAM0 -> CXL 18.66 ± 0.18 26.90 ± 0.51 1.44x
CXL -> DRAM0 15.38 ± 0.59 16.60 ± 0.24 1.08x
1GB hugetlb:
direction base patched speedup
DRAM0 -> DRAM1 20.34 ± 0.13 53.88 ± 1.48 2.65x
DRAM1 -> DRAM0 19.10 ± 2.65 53.78 ± 0.55 2.82x
DRAM0 -> CXL 19.53 ± 0.08 44.10 ± 0.19 2.26x
CXL -> DRAM0 16.41 ± 0.07 20.89 ± 0.06 1.27x
kswapd demotion
---------------
Node 0 is limited to 16GB of free memory and demotion is enabled. A
single thread bound to node 0 writes 96GB of anonymous THP memory, so
~80GB has to be demoted to CXL.
base patched
kswapd CPU time 4.25 s 3.44 s (-19%)
kswapd CPU per GB demoted 63.6 ms 44.1 ms (-31%)
demoted by direct reclaim 14.1 GB 0.46 GB
allocstall 7427 370
With the patch, kswapd keeps up with the writer, so the writer rarely
falls back to direct reclaim.
MADV_COLLAPSE
-------------
The thread runs MADV_COLLAPSE on a 1GB tmpfs file (huge=within_size)
whose page cache is made of folios of one size. Time per 2MB collapse
in microseconds, mean ± stddev of 30 runs over three boots:
folio size base patched
4KB 234.0 ± 2.7 237.0 ± 2.3 (noise)
64KB 95.2 ± 1.8 89.0 ± 0.8 (-6%)
256KB 69.1 ± 1.6 62.7 ± 0.5 (-9%)
1MB 68.2 ± 1.5 60.6 ± 0.3 (-11%)
hugetlb copy on write
---------------------
The thread writes to each hugetlb page of a MAP_PRIVATE mapping of a
populated hugetlb memfd, so each write takes a copy on write fault, at
the first or at the middle page of the folio. Copy throughput (GB/s,
mean ± stddev):
page size fault at base patched speedup
2MB first 22.40 ± 0.23 33.46 ± 0.65 1.49x
2MB middle 22.76 ± 0.31 33.37 ± 0.64 1.47x
1GB first 27.10 ± 0.03 53.25 ± 0.59 1.96x
1GB middle 27.10 ± 0.03 53.35 ± 0.57 1.97x
The new copy order alone gives ~15% for 2MB pages and nothing for 1GB
pages; the rest comes from the single-call copy.
Changes in v4:
- Convert copy_user_large_folio() to copy page ranges with the new
copy_mc_user_highpages(), and drop process_huge_page() and
copy_user_gigantic_page() (David).
- Copy each folio with copy_mc_highpages() in collapse_file().
- Move the batching check and the single-call copy into
copy_mc_can_batch() and __copy_mc_pages() (for copy_mc_user_highpages
cleanups).
- Link to v3: https://lore.kernel.org/r/20261113-batpagecopy-v3-1-297f5b2fd28e@xxxxxxx
Changes in v3:
- Copy the whole range with one copy_mc_to_kernel() call only when
CONFIG_PREEMPTION is set, otherwise copy page by page with
cond_resched(), following clear_contig_highpages() [1] (Pedro, David).
- Handle the machine check first in copy_mc_highpages() (Pedro).
- Rebase onto mm/core.git for-next.
- Link to v2: https://lore.kernel.org/r/20261019-batpagecopy-v2-1-d75e7d0dfaae@xxxxxxx
[1] https://lore.kernel.org/r/20261109-clear-pages-batch-v3-1-6693efe18dff@xxxxxxx
Changes in v2:
- Drop the folio_copy() changes as there is little to gain here.
On architectures without copy_mc_to_kernel(), copy_mc_highpages()
falls back to the per-page copy_mc_highpage(), which ends up in
copy_highpage(). A batched copy_highpages() could help there too,
but I haven't tested it on those architectures, so that is left for
a follow-up.
- Add COPY_MC_PAGES_NON_PREEMPT_BATCH to limit how much is copied
between cond_resched() calls on non-preemptible kernels.
- Rebase onto mm-new (2887e63d2e8e).
- v1: https://lore.kernel.org/all/20260427142036.111940-2-shivankg@xxxxxxx
---
Shivank Garg (6):
mm/highmem: introduce copy_mc_can_batch() and __copy_mc_pages()
mm: introduce copy_mc_highpages() and batch copy in folio_mc_copy()
mm/khugepaged: copy each folio with copy_mc_highpages() in collapse_file()
mm/highmem: introduce copy_mc_user_highpages()
mm: factor out the locality range computation from folio_zero_user()
mm: copy_user_large_folio(): copy page ranges
include/linux/highmem.h | 105 +++++++++++++++++++++++++++
mm/khugepaged.c | 14 ++--
mm/memory.c | 183 +++++++++++++++++-------------------------------
mm/util.c | 14 +---
4 files changed, 176 insertions(+), 140 deletions(-)
---
base-commit: 89b4b2b15b2063e1bf593e609c3c63313b7ff160
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mm/core.git for-next
change-id: 20260714-batpagecopy-5a2b45910e5d
Best regards,
--
Shivank Garg <shivankg@xxxxxxx>