folio_mc_copy() copies a folio one page at a time with copy_mc_highpage(), so a 2MB folio makes 512 separate copy_mc_to_kernel() calls. Each page pays the startup cost of the string instruction ("REP; MOVSB" on x86 CPUs with ERMS) plus a cond_resched(). Introduce copy_mc_highpages(), the page range variant of copy_mc_highpage(). Where copy_mc_can_batch() allows it, copy the whole range with a single __copy_mc_pages() call, so the CPU can optimize for the larger extent. Otherwise, fall back to copy_mc_highpage() one page at a time, with cond_resched() before each page, as clear_contig_highpages() does. Switch folio_mc_copy() to copy the whole folio with copy_mc_highpages(). On a machine check, it still returns -EHWPOISON. On an AMD EPYC Zen 6 system (single socket, NPS2; nodes 0 and 1 are DRAM, node 2 is CXL), a single move_pages() call migrates 1GB of anonymous memory, as 512 2MB THPs or as four 1GB hugetlb pages. Throughput in GiB/s, mean ± stddev of 10 runs: 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 With node 0 limited to 16GB of free memory and demotion enabled, a single thread bound to node 0 writes 96GB of anonymous THP memory, so ~80GB has to be demoted to CXL. Median of 10 runs: 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. Signed-off-by: Shivank Garg --- include/linux/highmem.h | 35 +++++++++++++++++++++++++++++++++++ mm/util.c | 14 ++------------ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 1bb0c1b707e3..6d5f3fa320d7 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -516,6 +516,41 @@ static inline int __copy_mc_pages(struct page *to, struct page *from, return -EHWPOISON; } +/** + * copy_mc_highpages() - copy a page range with machine check handling + * @to: first destination page + * @from: first source page + * @nr_pages: number of pages + * + * Range variant of copy_mc_highpage(). On a machine check, the poisoned + * source page is queued for memory_failure(). + * + * Context: Process context. May sleep. + * Return: 0 on success, -EHWPOISON if a machine check was handled. + */ +static inline int copy_mc_highpages(struct page *to, struct page *from, + unsigned long nr_pages) +{ + unsigned long i; + + might_sleep(); + + if (copy_mc_can_batch()) { + if (__copy_mc_pages(to, from, nr_pages)) + return -EHWPOISON; + for (i = 0; i < nr_pages; i++) + kmsan_copy_page_meta(to + i, from + i); + return 0; + } + + for (i = 0; i < nr_pages; i++) { + cond_resched(); + if (copy_mc_highpage(to + i, from + i)) + return -EHWPOISON; + } + return 0; +} + static inline void memcpy_page(struct page *dst_page, size_t dst_off, struct page *src_page, size_t src_off, size_t len) diff --git a/mm/util.c b/mm/util.c index 338f259b0035..d13e10716e2d 100644 --- a/mm/util.c +++ b/mm/util.c @@ -776,18 +776,8 @@ EXPORT_SYMBOL(folio_copy); int folio_mc_copy(struct folio *dst, struct folio *src) { - long nr = folio_nr_pages(src); - long i = 0; - - for (;;) { - if (copy_mc_highpage(folio_page(dst, i), folio_page(src, i))) - return -EHWPOISON; - if (++i == nr) - break; - cond_resched(); - } - - return 0; + return copy_mc_highpages(folio_page(dst, 0), folio_page(src, 0), + folio_nr_pages(src)); } EXPORT_SYMBOL(folio_mc_copy); -- 2.53.0