From: Shivank Garg <shivankg@amd.com>
To: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Borislav Petkov <bp@alien8.de>,
Ankur Arora <ankur.a.arora@oracle.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
"Thomas Gleixner" <tglx@kernel.org>,
Bharata B Rao <bharata@amd.com>,
"Hrushikesh Salunke" <hsalunke@amd.com>,
Ingo Molnar <mingo@redhat.com>, "H . Peter Anvin" <hpa@zytor.com>,
David Laight <david.laight.linux@gmail.com>,
David Rientjes <rientjes@google.com>,
Linus Torvalds <torvalds@linuxfoundation.org>,
"Teja Vojjala" <tejavojjala@google.com>,
Huan Nguyen <nhuhuan@google.com>,
"Pedro Falcato" <pfalcato@suse.de>,
Peter Zijlstra <peterz@infradead.org>, Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>, Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>,
Ying Huang <ying.huang@linux.alibaba.com>
Cc: <linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
<x86@kernel.org>, Shivank Garg <shivankg@amd.com>
Subject: [PATCH v4 2/6] mm: introduce copy_mc_highpages() and batch copy in folio_mc_copy()
Date: Tue, 1 Dec 2026 08:29:48 +0000 [thread overview]
Message-ID: <20261201-batpagecopy-v4-2-dd82a573e573@amd.com> (raw)
In-Reply-To: <20261201-batpagecopy-v4-0-dd82a573e573@amd.com>
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 <shivankg@amd.com>
---
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
next prev parent reply other threads:[~2026-12-01 8:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 8:29 [PATCH v4 0/6] mm: copy page ranges with one copy_mc_to_kernel() call Shivank Garg
2026-12-01 8:29 ` [PATCH v4 1/6] mm/highmem: introduce copy_mc_can_batch() and __copy_mc_pages() Shivank Garg
2026-12-01 8:29 ` Shivank Garg [this message]
2026-12-01 8:29 ` [PATCH v4 3/6] mm/khugepaged: copy each folio with copy_mc_highpages() in collapse_file() Shivank Garg
2026-12-01 8:29 ` [PATCH v4 4/6] mm/highmem: introduce copy_mc_user_highpages() Shivank Garg
2026-12-01 8:29 ` [PATCH v4 5/6] mm: factor out the locality range computation from folio_zero_user() Shivank Garg
2026-12-01 8:29 ` [PATCH v4 6/6] mm: copy_user_large_folio(): copy page ranges Shivank Garg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-batpagecopy-v4-2-dd82a573e573@amd.com \
--to=shivankg@amd.com \
--cc=akpm@linux-foundation.org \
--cc=ankur.a.arora@oracle.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=bharata@amd.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=david.laight.linux@gmail.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=hpa@zytor.com \
--cc=hsalunke@amd.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=nhuhuan@google.com \
--cc=nico.pache@linux.dev \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=surenb@google.com \
--cc=tejavojjala@google.com \
--cc=tglx@kernel.org \
--cc=torvalds@linuxfoundation.org \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=x86@kernel.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®