mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 6/6] mm: copy_user_large_folio(): copy page ranges
Date: Tue, 1 Dec 2026 08:29:52 +0000	[thread overview]
Message-ID: <20261201-batpagecopy-v4-6-dd82a573e573@amd.com> (raw)
In-Reply-To: <20261201-batpagecopy-v4-0-dd82a573e573@amd.com>

copy_user_large_folio() still copies a folio one page at a time in the
order given by process_huge_page(), with a separate loop in
copy_user_gigantic_page() for gigantic folios, although folio_zero_user()
moved to clearing page ranges.

process_huge_page() processes the pages farthest from the faulting page
first, converging on it by alternating between left and right. That keeps
the faulting page cache hot, but its non-sequential access pattern
defeats the hardware prefetcher, which is why clearing was moved away
from it in commit 9890ecab6ad9 ("mm: folio_zero_user: clear pages
sequentially")

Make copy_user_large_folio() mirror folio_zero_user(): split the folio
into the same three page ranges with folio_user_ranges() and copy each
range with copy_mc_user_highpages(). The range around the faulting page
(the faulting page and two pages either side of it) is copied last, as
clearing does since commit 93552c9a3350 ("mm: folio_zero_user: cache
neighbouring pages"). This keeps the cache locality that
process_huge_page() was introduced for in commit c9f4cd713835 ("mm, huge
page: copy target sub-page last when copy huge page")

process_huge_page() was only used by copy_user_large_folio(), and
copy_user_gigantic_page() and struct copy_subpage_arg with its callback
only existed to feed those two, so remove them.

On an AMD EPYC Zen 6 system, a single thread bound to one CPU writes to
each hugetlb page of a MAP_PRIVATE mapping of a populated hugetlb memfd.
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

With the single-call copy disabled, the new copy order alone gives 25.87
and 25.80 GB/s (~15%) for 2MB pages. 1GB pages were already copied
sequentially and don't change. The rest of the gain comes from copying
each range with one call.

Signed-off-by: Shivank Garg <shivankg@amd.com>
---
 mm/memory.c | 139 ++++++++++++++----------------------------------------------
 1 file changed, 32 insertions(+), 107 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index bd2c276e83aa..329ad8099e6b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -7678,66 +7678,6 @@ EXPORT_SYMBOL(__might_fault);
 #endif
 
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
-/*
- * Process all subpages of the specified huge page with the specified
- * operation.  The target subpage will be processed last to keep its
- * cache lines hot.
- */
-static inline int process_huge_page(
-	unsigned long addr_hint, unsigned int nr_pages,
-	int (*process_subpage)(unsigned long addr, int idx, void *arg),
-	void *arg)
-{
-	int i, n, base, l, ret;
-	unsigned long addr = addr_hint &
-		~(((unsigned long)nr_pages << PAGE_SHIFT) - 1);
-
-	/* Process target subpage last to keep its cache lines hot */
-	might_sleep();
-	n = (addr_hint - addr) / PAGE_SIZE;
-	if (2 * n <= nr_pages) {
-		/* If target subpage in first half of huge page */
-		base = 0;
-		l = n;
-		/* Process subpages at the end of huge page */
-		for (i = nr_pages - 1; i >= 2 * n; i--) {
-			cond_resched();
-			ret = process_subpage(addr + i * PAGE_SIZE, i, arg);
-			if (ret)
-				return ret;
-		}
-	} else {
-		/* If target subpage in second half of huge page */
-		base = nr_pages - 2 * (nr_pages - n);
-		l = nr_pages - n;
-		/* Process subpages at the begin of huge page */
-		for (i = 0; i < base; i++) {
-			cond_resched();
-			ret = process_subpage(addr + i * PAGE_SIZE, i, arg);
-			if (ret)
-				return ret;
-		}
-	}
-	/*
-	 * Process remaining subpages in left-right-left-right pattern
-	 * towards the target subpage
-	 */
-	for (i = 0; i < l; i++) {
-		int left_idx = base + i;
-		int right_idx = base + 2 * l - 1 - i;
-
-		cond_resched();
-		ret = process_subpage(addr + left_idx * PAGE_SIZE, left_idx, arg);
-		if (ret)
-			return ret;
-		cond_resched();
-		ret = process_subpage(addr + right_idx * PAGE_SIZE, right_idx, arg);
-		if (ret)
-			return ret;
-	}
-	return 0;
-}
-
 static void clear_contig_highpages(struct page *page, unsigned long addr,
 		unsigned int nr_pages)
 {
@@ -7831,61 +7771,46 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint)
 	}
 }
 
-static int copy_user_gigantic_page(struct folio *dst, struct folio *src,
-				   unsigned long addr_hint,
-				   struct vm_area_struct *vma,
-				   unsigned int nr_pages)
+/**
+ * copy_user_large_folio - Copy a large folio which will be mapped to userspace.
+ * @dst: The destination folio.
+ * @src: The source folio.
+ * @addr_hint: The address accessed by the user or the base address.
+ * @vma: The VMA @dst will be mapped in.
+ *
+ * Copy page ranges, with the one around @addr_hint last to keep its
+ * cachelines hot, see folio_user_ranges(). On a machine check, the poisoned
+ * source page is queued for memory_failure() and the remaining pages are
+ * not copied.
+ *
+ * Context: Process context. May sleep.
+ * Return: 0 on success, -EHWPOISON if a machine check was handled.
+ */
+int copy_user_large_folio(struct folio *dst, struct folio *src,
+			  unsigned long addr_hint, struct vm_area_struct *vma)
 {
-	unsigned long addr = ALIGN_DOWN(addr_hint, folio_size(dst));
-	struct page *dst_page;
-	struct page *src_page;
-	int i;
+	const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(dst));
+	struct range r[3];
+	int i, ret;
 
-	for (i = 0; i < nr_pages; i++) {
-		dst_page = folio_page(dst, i);
-		src_page = folio_page(src, i);
+	folio_user_ranges(dst, addr_hint, r);
 
-		cond_resched();
-		if (copy_mc_user_highpage(dst_page, src_page,
-					  addr + i*PAGE_SIZE, vma))
-			return -EHWPOISON;
-	}
-	return 0;
-}
-
-struct copy_subpage_arg {
-	struct folio *dst;
-	struct folio *src;
-	struct vm_area_struct *vma;
-};
+	for (i = 0; i < ARRAY_SIZE(r); i++) {
+		const unsigned long addr = base_addr + r[i].start * PAGE_SIZE;
+		const long nr_pages = (long)range_len(&r[i]);
 
-static int copy_subpage(unsigned long addr, int idx, void *arg)
-{
-	struct copy_subpage_arg *copy_arg = arg;
-	struct page *dst = folio_page(copy_arg->dst, idx);
-	struct page *src = folio_page(copy_arg->src, idx);
+		if (nr_pages <= 0)
+			continue;
 
-	if (copy_mc_user_highpage(dst, src, addr, copy_arg->vma))
-		return -EHWPOISON;
+		ret = copy_mc_user_highpages(folio_page(dst, r[i].start),
+					     folio_page(src, r[i].start),
+					     addr, vma, nr_pages);
+		if (ret)
+			return ret;
+	}
 	return 0;
 }
 
-int copy_user_large_folio(struct folio *dst, struct folio *src,
-			  unsigned long addr_hint, struct vm_area_struct *vma)
-{
-	unsigned int nr_pages = folio_nr_pages(dst);
-	struct copy_subpage_arg arg = {
-		.dst = dst,
-		.src = src,
-		.vma = vma,
-	};
-
-	if (unlikely(nr_pages > MAX_ORDER_NR_PAGES))
-		return copy_user_gigantic_page(dst, src, addr_hint, vma, nr_pages);
-
-	return process_huge_page(addr_hint, nr_pages, copy_subpage, &arg);
-}
-
 long copy_folio_from_user(struct folio *dst_folio,
 			   const void __user *usr_src,
 			   bool allow_pagefault)

-- 
2.53.0


      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 ` [PATCH v4 2/6] mm: introduce copy_mc_highpages() and batch copy in folio_mc_copy() Shivank Garg
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 ` Shivank Garg [this message]

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-6-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®