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 4/6] mm/highmem: introduce copy_mc_user_highpages()
Date: Tue, 1 Dec 2026 08:29:50 +0000	[thread overview]
Message-ID: <20261201-batpagecopy-v4-4-dd82a573e573@amd.com> (raw)
In-Reply-To: <20261201-batpagecopy-v4-0-dd82a573e573@amd.com>

copy_user_large_folio() copies a hugetlb folio one page at a time with
copy_mc_user_highpage(), in the order given by process_huge_page(). To
convert it to copying page ranges, introduce copy_mc_user_highpages(),
range variant similar to copy_mc_highpages().

It batches the same way, with copy_mc_can_batch() and __copy_mc_pages().
Where it can batch, copy_mc_user_highpage() uses neither the user
address nor the VMA, so one call over the whole range is equivalent.

On a machine check, the batched copy unpoisons none of the range, while
copying page by page unpoisons the pages before the poisoned one. This
is fine, as all callers of copy_user_large_folio() free the destination
folio on error.

Signed-off-by: Shivank Garg <shivankg@amd.com>
---
 include/linux/highmem.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 6d5f3fa320d7..b65d879d6f3f 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -551,6 +551,50 @@ static inline int copy_mc_highpages(struct page *to, struct page *from,
 	return 0;
 }
 
+/**
+ * copy_mc_user_highpages() - copy a page range to be mapped to user space,
+ *			      with machine check handling
+ * @to: first destination page
+ * @from: first source page
+ * @vaddr: user address the first destination page will be mapped at
+ * @vma: VMA the destination will be mapped in
+ * @nr_pages: number of pages
+ *
+ * Range variant of copy_mc_user_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_user_highpages(struct page *to, struct page *from,
+		unsigned long vaddr, struct vm_area_struct *vma,
+		unsigned long nr_pages)
+{
+	unsigned long i;
+
+	might_sleep();
+
+	/*
+	 * Where we can batch, copy_mc_user_highpage() does not use @vaddr,
+	 * @vma or copy_user_page() either, so a single call is equivalent.
+	 */
+	if (copy_mc_can_batch()) {
+		if (__copy_mc_pages(to, from, nr_pages))
+			return -EHWPOISON;
+		for (i = 0; i < nr_pages; i++)
+			kmsan_unpoison_memory(page_address(to + i), PAGE_SIZE);
+		return 0;
+	}
+
+	for (i = 0; i < nr_pages; i++) {
+		cond_resched();
+		if (copy_mc_user_highpage(to + i, from + i,
+					  vaddr + i * PAGE_SIZE, vma))
+			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)

-- 
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 ` Shivank Garg [this message]
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-4-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®