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 5/6] mm: factor out the locality range computation from folio_zero_user()
Date: Tue, 1 Dec 2026 08:29:51 +0000	[thread overview]
Message-ID: <20261201-batpagecopy-v4-5-dd82a573e573@amd.com> (raw)
In-Reply-To: <20261201-batpagecopy-v4-0-dd82a573e573@amd.com>

folio_zero_user() splits a folio into three page ranges around the
faulting page and clears them in order, so that the faulting page and
its neighbours are cleared last and stay cache hot. Copying a folio to
be mapped at a fault has the same locality requirement, and an upcoming
patch converts copy_user_large_folio() to the same scheme.

Move the range computation into folio_user_ranges() so that both can
share it. Rename FOLIO_ZERO_LOCALITY_RADIUS to FOLIO_LOCALITY_RADIUS and
generalise the comment above it, as it now describes both.

No functional change.

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

diff --git a/mm/memory.c b/mm/memory.c
index dfa307a9228c..bd2c276e83aa 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -7767,31 +7767,35 @@ static void clear_contig_highpages(struct page *page, unsigned long addr,
 }
 
 /*
- * When zeroing a folio, we want to differentiate between pages in the
- * vicinity of the faulting address where we have spatial and temporal
+ * When zeroing or copying a folio, we want to differentiate between pages in
+ * the vicinity of the faulting address where we have spatial and temporal
  * locality, and those far away where we don't.
  *
  * Use a radius of 2 for determining the local neighbourhood.
  */
-#define FOLIO_ZERO_LOCALITY_RADIUS	2
+#define FOLIO_LOCALITY_RADIUS	2
 
-/**
- * folio_zero_user - Zero a folio which will be mapped to userspace.
- * @folio: The folio to zero.
- * @addr_hint: The address accessed by the user or the base address.
+/*
+ * Split a folio into the page index ranges to process, in order:
+ *
+ *  r[0]: pages to the right of the faulting page's neighbourhood
+ *  r[1]: pages to the left of the faulting page's neighbourhood
+ *  r[2]: the faulting page and its immediate neighbourhood, to be processed
+ *        last to keep its cachelines hot
+ *
+ * A range is empty if its length, range_len(), is 0.
  */
-void folio_zero_user(struct folio *folio, unsigned long addr_hint)
+static void folio_user_ranges(struct folio *folio, unsigned long addr_hint,
+			      struct range r[3])
 {
 	const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio));
 	const long fault_idx = (addr_hint - base_addr) / PAGE_SIZE;
 	const struct range pg = DEFINE_RANGE(0, folio_nr_pages(folio) - 1);
-	const long radius = FOLIO_ZERO_LOCALITY_RADIUS;
-	struct range r[3];
-	int i;
+	const long radius = FOLIO_LOCALITY_RADIUS;
 
 	/*
-	 * Faulting page and its immediate neighbourhood. Will be cleared at the
-	 * end to keep its cachelines hot.
+	 * Faulting page and its immediate neighbourhood. Will be processed at
+	 * the end to keep its cachelines hot.
 	 */
 	r[2] = DEFINE_RANGE(fault_idx - radius < (long)pg.start ? pg.start : fault_idx - radius,
 			    fault_idx + radius > (long)pg.end   ? pg.end   : fault_idx + radius);
@@ -7802,6 +7806,20 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint)
 
 	/* Region to the right of the fault: always valid for the common fault_idx=0 case. */
 	r[0] = DEFINE_RANGE(r[2].end + 1, pg.end);
+}
+
+/**
+ * folio_zero_user - Zero a folio which will be mapped to userspace.
+ * @folio: The folio to zero.
+ * @addr_hint: The address accessed by the user or the base address.
+ */
+void folio_zero_user(struct folio *folio, unsigned long addr_hint)
+{
+	const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio));
+	struct range r[3];
+	int i;
+
+	folio_user_ranges(folio, addr_hint, r);
 
 	for (i = 0; i < ARRAY_SIZE(r); i++) {
 		const unsigned long addr = base_addr + r[i].start * PAGE_SIZE;

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