Linux Hardening
 help / color / mirror / Atom feed
* [PATCH v11 0/7] mm: optimize zone-device memmap initialization
@ 2026-08-31 11:16 Li Zhe
  2026-08-31 11:16 ` [PATCH v11 1/7] mm: fix stale ZONE_DEVICE refcount comment Li Zhe
                   ` (7 more replies)
  0 siblings, 8 replies; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

memmap_init_zone_device() can take a noticeable amount of time when large
pmem namespaces are bound or rebound, because it initializes nearly
identical struct page descriptors one PFN at a time. This series reduces
that ZONE_DEVICE memmap initialization overhead by reusing prepared
struct page templates and, on x86, using memcpy_nontemporal() for the
template copy path.

The main target is large fsdax/devdax pmem configurations, where the
cost of initializing the memmap shows up directly in nd_pmem/dax_pmem
bind and rebind latency. This matters because the cost is paid in the
synchronous probe/bind path for large DAX/PMEM ZONE_DEVICE mappings.
Userspace workflows such as provisioning or reconfiguring
nd_pmem/dax_pmem namespaces, bringing hot-added PMEM-backed capacity
online, and recovering or rebinding a device after driver or device
changes all wait for this initialization to finish. Reducing this cost
will yield benefits as lower user-visible provisioning, hot-add,
recovery, and rebind latency for large DAX/PMEM devices.

Patches 1-2 are preparatory cleanups and helper extraction. Patches 3-4
add the template-copy path for head pages and compound tails. Patch 5
introduces memcpy_nontemporal(). Patch 6 switches the ZONE_DEVICE
template-copy path over to memcpy_nontemporal(). Patch 7 extends the x86
fixed-size memcpy_flushcache() inline cases used by the x86
memcpy_nontemporal() backend for struct page sized copies.

Architectures without a specialized memcpy_nontemporal() backend fall
back to memcpy(), so the generic template-copy optimization remains
available without arch-specific support. On x86, memcpy_nontemporal()
maps to the existing memcpy_flushcache() backend and can use the
fixed-size MOVNTI paths added by this series for struct page sized
copies.

memcpy_nontemporal() is only a copy primitive. It does not imply a drain
or a publication barrier. Callers that use it before a producer-consumer
or device-visible handoff must provide the required ordering. The
ZONE_DEVICE template-copy path uses it only while initializing struct
page metadata, so the copy primitive itself does not grow a separate
drain contract.

The numbers below measure the time spent in memmap_init_zone_device()
during driver bind/rebind. They are not measurements of the full
nd_pmem or dax_pmem bind/rebind operation.

Tested in an x86_64 QEMU/KVM VM with a 100 GB fsdax namespace device
configured with map=dev and a 100 GB devdax namespace (align=2097152)
on Intel Ice Lake server.

Test procedure:
Rebind the nd_pmem and dax_pmem drivers 30 times and collect the memmap
initialization time from the pr_debug() output of
memmap_init_zone_device().

Base(v7.3-rc1):
  Average of nd_pmem rebinds: 221.07 ms
  Average of dax_pmem rebinds: 191.20 ms

With this series applied:
  Average of nd_pmem rebinds:  71.93 ms
  Average of dax_pmem rebinds:  87.37 ms

This reduces the average memmap initialization time measured during
rebind by about 67.5% for nd_pmem and 54.3% for dax_pmem.

As an additional x86_64 data point, I also ran measurements on the same
physical host with a 100 GB PMEM region created via the memmap= kernel
command line, configured as fsdax and devdax namespaces with map=dev and
2 MiB alignment.

For brevity, the individual patches keep only the VM results rather than
including a second set of physical-host measurements throughout the
series. The physical-host numbers below are included only as
supplemental evidence that the same optimization also provides a similar
benefit on a non-virtualized system.

Test procedure:
Reconfigure the namespace mode, rebind the nd_pmem or dax_pmem driver
30 times, and collect the memmap initialization time from the pr_debug()
output of memmap_init_zone_device().

Base (v7.3-rc1):
  nd_pmem / fsdax: 205.90 ms
  dax_pmem / devdax: 225.43 ms

With this series applied:
  nd_pmem / fsdax: 69.13 ms
  dax_pmem / devdax: 90.67 ms

This reduces the measured memmap initialization time during rebind by
about 66.4% for nd_pmem and 59.8% for dax_pmem on that setup, which is
broadly consistent with the VM results above.

As another supplemental data point, I measured the test_hmm.ko module on
the same physical x86_64 host, using the test_hmm.ko setup from the
previous discussion that times ten 64 GB
memremap_pages()/memunmap_pages() iterations during module insertion[1].
By default, module insertion initializes two DEVICE_PRIVATE dmirror
devices, so two avg memremap values are reported; each value is the
average for one 64 GB chunk.

This is not the primary target workload of the series, but it exercises
the same large ZONE_DEVICE memmap initialization path and shows the same
direction of improvement.

Base (v7.3-rc1):
  avg memremap reported during module insertion: 116500596 ns, 116438028 ns

With this series applied:
  avg memremap reported during module insertion: 46953088 ns, 46428399 ns

This corresponds to about a 59.9% reduction based on the mean of the
reported values, which is again consistent with the pmem bind/rebind
results above.

I also include an arm64 data point for the generic template-copy part.
It was measured on an arm64 QEMU virt VM with 64 KB pages and a 100 GB
ACPI NVDIMM sparse backend. This setup does not use the x86 MOVNTI fast
paths, so it exercises the architecture-independent part of the
optimization.

For devdax, 2 MiB alignment is rejected in this 64 KB page setup, so the
devdax namespace was tested with the supported default 512 MiB
alignment.

Base (v7.3-rc1):
  Average of rebinds for nd_pmem driver: 27.93 ms
  Average of rebinds for dax_pmem driver: 27.87 ms

With this series applied:
  Average of rebinds for nd_pmem driver: 14.53 ms
  Average of rebinds for dax_pmem driver: 16.27 ms

This reduces the average memmap initialization time measured during
rebind by about 48.0% for nd_pmem and 41.6% for dax_pmem on that arm64
VM setup. Since this arm64 setup does not use the x86 MOVNTI fast paths,
the result also suggests that the generic template-copy optimization can
benefit architectures without an architecture-specific
memcpy_nontemporal() backend.

[1] https://lore.kernel.org/all/aiEoByaQdRR3xtM5@nvdebian.thelocal/

Li Zhe (7):
  mm: fix stale ZONE_DEVICE refcount comment
  mm: add a set_page_section_from_pfn() helper
  mm: add a template-based fast path for zone-device page init
  mm: extend the template fast path to zone-device compound tails
  string: introduce memcpy_nontemporal()
  mm: use memcpy_nontemporal() in zone-device template copies
  x86/string: extend memcpy_flushcache() fixed-size fastpaths

 arch/x86/include/asm/string_64.h | 83 ++++++++++++++++++++++++++------
 include/linux/mm.h               | 15 ++++--
 include/linux/string.h           | 13 +++++
 mm/mm_init.c                     | 72 +++++++++++++++++++++------
 4 files changed, 149 insertions(+), 34 deletions(-)

---
v10: https://lore.kernel.org/all/20260810122057.30447-1-lizhe.67@bytedance.com/
v9: https://lore.kernel.org/all/20260803070929.86075-1-lizhe.67@bytedance.com/
v8: https://lore.kernel.org/all/20260727123429.5673-1-lizhe.67@bytedance.com/
v7: https://lore.kernel.org/all/20260720120259.1545-1-lizhe.67@bytedance.com/
v6: https://lore.kernel.org/all/20260709112520.24857-1-lizhe.67@bytedance.com/
v5: https://lore.kernel.org/all/20260701090553.62691-1-lizhe.67@bytedance.com/
v4: https://lore.kernel.org/all/20260603080152.64728-1-lizhe.67@bytedance.com/
v3: https://lore.kernel.org/all/20260527033636.28231-1-lizhe.67@bytedance.com/
v2: https://lore.kernel.org/all/20260521040124.10608-1-lizhe.67@bytedance.com/
v1: https://lore.kernel.org/all/20260515082045.63029-1-lizhe.67@bytedance.com/

Changelogs:

v10->v11:
- Rebased the series on v7.3-rc1.
- Refresh the benchmark numbers on v7.3-rc1.
- Dropped the standalone helper split around __init_zone_device_page(); keep
  the existing helper shape and layer the template path directly on top.
  Suggested by Mike Rapoport.
- Move the first head-page/tail-page initialization out of the template-copy
  loops. Suggested by Mike Rapoport.
- Fold the template PFN-dependent field refresh into the template-copy helper
  instead of keeping a separate zone_device_page_update_template() helper.
  Suggested by Mike Rapoport.

For changelogs of earlier revisions, please refer to the v10 cover letter.

-- 
2.20.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v11 1/7] mm: fix stale ZONE_DEVICE refcount comment
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-08-31 11:16 ` [PATCH v11 2/7] mm: add a set_page_section_from_pfn() helper Li Zhe
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

The comment in __init_zone_device_page() still uses the old
MEMORY_TYPE_* names and implies that FS_DAX pages regain a
refcount of 1 in the free path. That no longer matches the code.

Update the comment to describe the current policy correctly:
MEMORY_DEVICE_GENERIC pages regain a refcount of 1 in the free path,
while the remaining ZONE_DEVICE types start from 0 here and raise the
count again when the allocator or driver hands the page out.

No functional change intended.

Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Alistair Popple <apopple@nvidia.com>
Reviewed-by: Muchun Song <muchun.song@linux.dev>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
---
 mm/mm_init.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/mm/mm_init.c b/mm/mm_init.c
index 1533aebafb68..8f73d5850db2 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1000,13 +1000,9 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn,
 	page->zone_device_data = NULL;
 
 	/*
-	 * ZONE_DEVICE pages other than MEMORY_TYPE_GENERIC are released
-	 * directly to the driver page allocator which will set the page count
-	 * to 1 when allocating the page.
-	 *
-	 * MEMORY_TYPE_GENERIC and MEMORY_TYPE_FS_DAX pages automatically have
-	 * their refcount reset to one whenever they are freed (ie. after
-	 * their refcount drops to 0).
+	 * MEMORY_DEVICE_GENERIC pages regain a refcount of 1 in the free
+	 * path. The remaining ZONE_DEVICE types start from 0 here and raise
+	 * the count again when the allocator or driver hands the page out.
 	 */
 	switch (pgmap->type) {
 	case MEMORY_DEVICE_FS_DAX:
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v11 2/7] mm: add a set_page_section_from_pfn() helper
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
  2026-08-31 11:16 ` [PATCH v11 1/7] mm: fix stale ZONE_DEVICE refcount comment Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-08-31 11:16 ` [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init Li Zhe
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

Callers that want to update section bits from a PFN currently need to
open-code:

	set_page_section(page, pfn_to_section_nr(pfn));

and guard that sequence with #ifdef SECTION_IN_PAGE_FLAGS.

Add set_page_section_from_pfn() to wrap that update in one place. When
section bits are stored in page flags, the helper derives the section
number from the PFN and updates the page flags. Otherwise keep it as a
no-op so callers can use one helper without open-coding
SECTION_IN_PAGE_FLAGS.

Convert set_page_links() to use the new helper so later ZONE_DEVICE
fast-path patches can also update section bits without open-coding
SECTION_IN_PAGE_FLAGS at each callsite.

This keeps the PFN-to-section translation local to the configurations
that actually store section bits in struct page flags, and avoids
exposing that detail to generic callers.

No functional change intended.

Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Acked-by: Muchun Song <muchun.song@linux.dev>
Reviewed-by: Balbir Singh <balbirs@nvidia.com>
---
 include/linux/mm.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index dd09c438fa23..6640c7522822 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2644,12 +2644,23 @@ static inline void set_page_section(struct page *page, unsigned long section)
 	page->flags.f |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
 }
 
+static inline void set_page_section_from_pfn(struct page *page,
+		unsigned long pfn)
+{
+	set_page_section(page, pfn_to_section_nr(pfn));
+}
+
 static inline unsigned long memdesc_section(const memdesc_flags_t *mdf)
 {
 	ASSERT_EXCLUSIVE_BITS(mdf->f, SECTIONS_MASK << SECTIONS_PGSHIFT);
 	return (mdf->f >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
 }
 #else /* !SECTION_IN_PAGE_FLAGS */
+static inline void set_page_section_from_pfn(struct page *page,
+		unsigned long pfn)
+{
+}
+
 static inline unsigned long memdesc_section(const memdesc_flags_t *mdf)
 {
 	return 0;
@@ -2872,9 +2883,7 @@ static inline void set_page_links(struct page *page, enum zone_type zone,
 {
 	set_page_zone(page, zone);
 	set_page_node(page, node);
-#ifdef SECTION_IN_PAGE_FLAGS
-	set_page_section(page, pfn_to_section_nr(pfn));
-#endif
+	set_page_section_from_pfn(page, pfn);
 }
 
 /**
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
  2026-08-31 11:16 ` [PATCH v11 1/7] mm: fix stale ZONE_DEVICE refcount comment Li Zhe
  2026-08-31 11:16 ` [PATCH v11 2/7] mm: add a set_page_section_from_pfn() helper Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-09-02  4:16   ` Mike Rapoport
  2026-08-31 11:16 ` [PATCH v11 4/7] mm: extend the template fast path to zone-device compound tails Li Zhe
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

memmap_init_zone_device() repeats nearly identical head-page
initialization for each PFN. Initialize the first real ZONE_DEVICE head
page through the existing path, copy that final state into a reusable
template, refresh the PFN-dependent fields in that template before each
copy, and copy it into the remaining destination pages.

Use the template path unconditionally. The page_ref_set tracepoint is
primarily a debugging aid, while this code is still initializing struct
pages before they are handed out. From the perspective of users of those
pages, the initialization-time refcount transitions are not part of the
observable page lifetime.

This means page_ref_set will no longer observe every initialization-time
refcount assignment for copied ZONE_DEVICE head pages. The impact is
controlled because the final initialized struct page state is unchanged,
and keeping a separate non-template path only for this local tracepoint
observability would add complexity to the common path.

This patch accelerates head-page initialization. The pfns_per_compound
== 1 case gets the full benefit here, compound tails are handled in the
next patch.

Tested in a VM with a 100 GB fsdax namespace device configured with
map=dev on Intel Ice Lake server. This test exercises the nd_pmem rebind
path (pfns_per_compound == 1).

Test procedure:
Rebind the nd_pmem driver 30 times and collect the memmap initialization
time from the pr_debug() output of memmap_init_zone_device().

Base(v7.3-rc1):
  Average of rebinds for nd_pmem driver: 221.07 ms

With this patch and its prerequisites applied:
  Average of rebinds for nd_pmem driver: 155.00 ms

This reduces the average memmap initialization time measured during
rebind from 221.07 ms to 155.00 ms, or about 29.9%.

Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
---
 mm/mm_init.c | 38 +++++++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/mm/mm_init.c b/mm/mm_init.c
index 8f73d5850db2..ba4148de2b28 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1017,6 +1017,17 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn,
 	}
 }
 
+static void zone_device_page_init_from_template(struct page *page,
+		unsigned long pfn, struct page *template)
+{
+	set_page_section_from_pfn(template, pfn);
+#ifdef WANT_PAGE_VIRTUAL
+	if (!is_highmem_idx(ZONE_DEVICE))
+		set_page_address(template, __va(pfn << PAGE_SHIFT));
+#endif
+	memcpy(page, template, sizeof(*page));
+}
+
 /*
  * With compound page geometry and when struct pages are stored in ram most
  * tail pages are reused. Consequently, the amount of unique struct pages to
@@ -1079,6 +1090,8 @@ void __ref memmap_init_zone_device(struct zone *zone,
 	unsigned long zone_idx = zone_idx(zone);
 	unsigned long start = jiffies;
 	int nid = pgdat->node_id;
+	struct page template;
+	struct page *page;
 
 	if (WARN_ON_ONCE(!pgmap || zone_idx != ZONE_DEVICE))
 		return;
@@ -1093,10 +1106,29 @@ void __ref memmap_init_zone_device(struct zone *zone,
 		nr_pages = end_pfn - start_pfn;
 	}
 
-	for (pfn = start_pfn; pfn < end_pfn; pfn += pfns_per_compound) {
-		struct page *page = pfn_to_page(pfn);
+	if (!nr_pages)
+		return;
 
-		__init_zone_device_page(page, pfn, zone_idx, nid, pgmap);
+	/*
+	 * Seed the reusable head-page template from the first real struct
+	 * page. The normal page-init and refcount helpers must operate on
+	 * a real memmap entry rather than a stack object.
+	 */
+	pfn = start_pfn;
+	page = pfn_to_page(pfn);
+	__init_zone_device_page(page, pfn, zone_idx, nid, pgmap);
+	memcpy(&template, page, sizeof(*page));
+	if (pfns_per_compound != 1)
+		memmap_init_compound(page, pfn, zone_idx, nid, pgmap,
+				     compound_nr_pages(pfn, altmap, pgmap));
+	pfn += pfns_per_compound;
+
+	/* Initialize the remaining head pages from template. */
+	for (; pfn < end_pfn; pfn += pfns_per_compound) {
+		page = pfn_to_page(pfn);
+
+		zone_device_page_init_from_template(page, pfn,
+						    &template);
 
 		if (IS_ALIGNED(pfn, PAGES_PER_SECTION))
 			cond_resched();
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v11 4/7] mm: extend the template fast path to zone-device compound tails
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
                   ` (2 preceding siblings ...)
  2026-08-31 11:16 ` [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-09-02  4:18   ` Mike Rapoport
  2026-08-31 11:16 ` [PATCH v11 5/7] string: introduce memcpy_nontemporal() Li Zhe
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

The template fast path from the previous patch only accelerates head
pages. Compound tails in memmap_init_compound() still go through the
normal initialization path one by one.

Build separate head and tail templates and reuse one prepared tail
template across the tail pages in a compound range. Head pages preserve
the existing refcount policy, while compound tails always start with a
refcount of 0 after prep_compound_tail().

This extends the template-copy fast path to pfns_per_compound > 1.
Tail-page PFN-dependent fields are refreshed in the reusable tail
template before each copy.

Do not keep a separate non-template fallback for compound tails either.
These pages are still under memmap initialization, and the
initialization-time refcount updates are not part of the observable
lifetime of pages handed out later.

The impact is controlled for the same reason as for head pages. The
first tail page still seeds the reusable tail template through the
normal tail initialization sequence, and the copied tail pages have the
same final initialized state except for the PFN-dependent fields
refreshed before each copy.

Tested in a VM with a 100 GB devdax namespace (align=2097152) on Intel
Ice Lake server. This test exercises the dax_pmem rebind path and
measures memmap initialization latency.

Test procedure:
Unbind and rebind the dax_pmem driver 30 times, collect memmap
initialization time from the pr_debug() output of
memmap_init_zone_device().

Base(v7.3-rc1):
  Average of rebinds for dax_pmem driver: 191.20 ms

With this patch and its prerequisites applied:
  Average of rebinds for dax_pmem driver: 176.87 ms

This reduces the average memmap initialization time measured during
rebind from 191.20 ms to 176.87 ms, or about 7.5%.

Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
---
 mm/mm_init.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/mm/mm_init.c b/mm/mm_init.c
index ba4148de2b28..04ba37d95bfd 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1060,6 +1060,8 @@ static void __ref memmap_init_compound(struct page *head,
 {
 	unsigned long pfn, end_pfn = head_pfn + nr_pages;
 	unsigned int order = pgmap->vmemmap_shift;
+	struct page template;
+	struct page *page;
 
 	/*
 	 * We have to initialize the pages, including setting up page links.
@@ -1068,13 +1070,23 @@ static void __ref memmap_init_compound(struct page *head,
 	 * the pages in the same go.
 	 */
 	__SetPageHead(head);
-	for (pfn = head_pfn + 1; pfn < end_pfn; pfn++) {
-		struct page *page = pfn_to_page(pfn);
 
-		__init_zone_device_page(page, pfn, zone_idx, nid, pgmap);
-		prep_compound_tail(page, head, order);
-		set_page_count(page, 0);
-	}
+	/*
+	 * All tails of the same compound page share the state established by
+	 * prep_compound_tail(). Reuse one tail template for the whole range and
+	 * refresh only the PFN-dependent fields in that template before each copy.
+	 */
+	pfn = head_pfn + 1;
+	page = pfn_to_page(pfn);
+	__init_zone_device_page(page, pfn, zone_idx, nid, pgmap);
+	prep_compound_tail(page, head, order);
+	set_page_count(page, 0);
+	memcpy(&template, page, sizeof(*page));
+
+	/* Initialize the remaining tail pages from template. */
+	for (pfn = head_pfn + 2; pfn < end_pfn; pfn++)
+		zone_device_page_init_from_template(pfn_to_page(pfn), pfn,
+						    &template);
 	prep_compound_head(head, order);
 }
 
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v11 5/7] string: introduce memcpy_nontemporal()
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
                   ` (3 preceding siblings ...)
  2026-08-31 11:16 ` [PATCH v11 4/7] mm: extend the template fast path to zone-device compound tails Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-08-31 11:16 ` [PATCH v11 6/7] mm: use memcpy_nontemporal() in zone-device template copies Li Zhe
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

Introduce memcpy_nontemporal() for write-once copy sites that want a
named non-temporal copy primitive.

On x86_64, override the helper in arch/x86/include/asm/string_64.h using
the usual self-macro pattern, next to the existing memcpy_flushcache()
backend that memcpy_nontemporal() wraps.

include/linux/string.h provides the generic memcpy_nontemporal()
fallback as

  #define memcpy_nontemporal(dst, src, len) \
	((void)memcpy(dst, src, len))

instead of an inline wrapper, so architectures without a specialized
backend keep the usual memcpy() FORTIFY coverage when the compiler can
still see object sizes at the original call site. It also makes the
memcpy_nontemporal() API uniformly void, matching memcpy_flushcache()
and the x86 backend, so callers cannot accidentally depend on a return
value on fallback architectures.

memcpy_nontemporal() is only a copy primitive. It does not imply a drain
or a publication barrier. Callers that use it before a producer-consumer
or device-visible handoff must provide the required ordering at that
handoff point.

The immediate user is the ZONE_DEVICE template-copy path. It populates
struct page descriptors in a write-once pattern, so a regular cached
memcpy() can incur avoidable write-allocate traffic and cache pollution
for data with little near-term reuse.

Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
---
 arch/x86/include/asm/string_64.h | 12 ++++++++++++
 include/linux/string.h           | 13 +++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h
index 4635616863f5..21ae515ae35a 100644
--- a/arch/x86/include/asm/string_64.h
+++ b/arch/x86/include/asm/string_64.h
@@ -100,6 +100,18 @@ static __always_inline void memcpy_flushcache(void *dst, const void *src, size_t
 	}
 	__memcpy_flushcache(dst, src, cnt);
 }
+
+#define memcpy_nontemporal memcpy_nontemporal
+/*
+ * Reuse the existing x86 flushcache backend as the non-temporal copy
+ * primitive.
+ */
+static __always_inline void memcpy_nontemporal(void *dst, const void *src,
+		size_t cnt)
+{
+	memcpy_flushcache(dst, src, cnt);
+}
+
 #endif
 
 #endif /* __KERNEL__ */
diff --git a/include/linux/string.h b/include/linux/string.h
index 5702daca4326..6cb5cdd01158 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -278,6 +278,19 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 }
 #endif
 
+#ifndef memcpy_nontemporal
+/*
+ * memcpy_nontemporal() requests a non-temporal copy when the
+ * architecture has a suitable backend. Architectures without a
+ * specialized backend fall back to memcpy(). Keep this as a
+ * function-like macro so the compiler can still see the original
+ * memcpy() call site and preserve the usual FORTIFY coverage when
+ * object sizes remain visible there, while keeping the API void.
+ */
+#define memcpy_nontemporal(dst, src, len) \
+	((void)memcpy(dst, src, len))
+#endif
+
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *str, char old, char new);
 
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v11 6/7] mm: use memcpy_nontemporal() in zone-device template copies
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
                   ` (4 preceding siblings ...)
  2026-08-31 11:16 ` [PATCH v11 5/7] string: introduce memcpy_nontemporal() Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-08-31 11:16 ` [PATCH v11 7/7] x86/string: extend memcpy_flushcache() fixed-size fastpaths Li Zhe
  2026-08-31 23:45 ` [PATCH v11 0/7] mm: optimize zone-device memmap initialization Andrew Morton
  7 siblings, 0 replies; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

The template fast path currently uses memcpy() for the actual struct
page copy. Switch zone_device_page_init_from_template() to
memcpy_nontemporal().

ZONE_DEVICE memmap initialization is largely write-once: each struct
page is populated once, and most destination cachelines are not expected
to be reused immediately afterwards. On x86, a regular cached memcpy()
can therefore incur write-allocate traffic by pulling destination
cachelines into the cache before writeback, and can populate the cache
with data that has little near-term reuse. Using memcpy_nontemporal()
lets this path request nontemporal stores for that copy pattern, which
can reduce cache pollution and avoid part of the associated
write-allocate overhead, while architectures without a specialized
backend still fall back to memcpy().

Do not add a KASAN/KMSAN-specific fallback around this call site. As
Muchun pointed out, special KASAN handling for memcpy_flushcache() or
memcpy_nontemporal(), if needed, belongs in the low-level helper rather
than in this ZONE_DEVICE caller.

No separate drain is added here. memcpy_nontemporal() is used only as
the copy primitive while memmap_init_zone_device() is still initializing
the struct page array. The ordinary stores that follow in this path,
such as compound-page setup, are part of the same CPU's initialization
sequence; they are not used as a publication store that tells another CPU
or device to consume data written by the non-temporal copy.

Therefore this call site does not need a helper-level drain for
correctness. Callers that use memcpy_nontemporal() as part of a
producer-consumer or device-visible handoff must add the required
ordering themselves.

Tested in a VM with a 100 GB fsdax namespace device configured with
map=dev and a 100 GB devdax namespace (align=2097152) on Intel Ice Lake
server.

Test procedure:
Rebind the nd_pmem and dax_pmem driver 30 times and collect the memmap
initialization time from the pr_debug() output of
memmap_init_zone_device().

Base(v7.3-rc1):
  Average of rebinds for nd_pmem driver: 221.07 ms
  Average of rebinds for dax_pmem driver: 191.20 ms

With this patch and its prerequisites applied:
  Average of rebinds for nd_pmem driver: 150.40 ms
  Average of rebinds for dax_pmem driver: 161.83 ms

This reduces the average memmap initialization time measured during rebind
by about 32.0% for nd_pmem and 15.4% for dax_pmem.

Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
---
 mm/mm_init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/mm_init.c b/mm/mm_init.c
index 04ba37d95bfd..5a61c0c83fa7 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1025,7 +1025,7 @@ static void zone_device_page_init_from_template(struct page *page,
 	if (!is_highmem_idx(ZONE_DEVICE))
 		set_page_address(template, __va(pfn << PAGE_SHIFT));
 #endif
-	memcpy(page, template, sizeof(*page));
+	memcpy_nontemporal(page, template, sizeof(*page));
 }
 
 /*
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v11 7/7] x86/string: extend memcpy_flushcache() fixed-size fastpaths
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
                   ` (5 preceding siblings ...)
  2026-08-31 11:16 ` [PATCH v11 6/7] mm: use memcpy_nontemporal() in zone-device template copies Li Zhe
@ 2026-08-31 11:16 ` Li Zhe
  2026-08-31 23:45 ` [PATCH v11 0/7] mm: optimize zone-device memmap initialization Andrew Morton
  7 siblings, 0 replies; 14+ messages in thread
From: Li Zhe @ 2026-08-31 11:16 UTC (permalink / raw)
  To: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx
  Cc: linux-arch, linux-hardening, linux-kernel, linux-mm, x86,
	lizhe.67

The x86 memcpy_nontemporal() helper maps to memcpy_flushcache(), and the
ZONE_DEVICE template-copy path uses it to copy one struct page at a
time.

The relevant copy size is sizeof(struct page). On x86_64, the base struct
page layout is 64 bytes. Adding either the KMSAN metadata pointers or an
out-of-flags last_cpupid field can make it 80 bytes after alignment, and
enabling both can make it 96 bytes.

memcpy_flushcache() currently only has inline fixed-size cases for 4, 8,
and 16 bytes. As a result, these constant-sized struct page copies fall
through to __memcpy_flushcache() even though the compiler knows the copy
size at the call site.

Add fixed-size MOVNTI cases up to 96 bytes so the ZONE_DEVICE
template-copy path can keep these struct page copies in the inline
memcpy_flushcache() path.

This matters for ZONE_DEVICE memmap initialization because the copy
happens once per initialized struct page. For a 100 GB fsdax namespace
with map=dev, this is about 25 million struct page copies during nd_pmem
binding or rebinding.

Tested in a VM with a 100 GB fsdax namespace device configured with
map=dev and a 100 GB devdax namespace (align=2097152) on Intel Ice Lake
server.

Test procedure:
Rebind the nd_pmem and dax_pmem drivers 30 times and collect the memmap
initialization time from the pr_debug() output of
memmap_init_zone_device().

With memcpy_nontemporal() used by the ZONE_DEVICE template-copy path:
  Average of rebinds for nd_pmem driver: 150.40 ms
  Average of rebinds for dax_pmem driver: 161.83 ms

With this x86 fixed-size fastpath patch applied:
  Average of rebinds for nd_pmem driver: 71.93 ms
  Average of rebinds for dax_pmem driver: 87.37 ms

This further reduces the average memmap initialization time measured
during rebind by about 52.2% for nd_pmem and 46.0% for dax_pmem.

Suggested-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Li Zhe <lizhe.67@bytedance.com>
Acked-by: Borislav Petkov (AMD) <bp@alien8.de>
---
 arch/x86/include/asm/string_64.h | 71 +++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 15 deletions(-)

diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h
index 21ae515ae35a..831d3dda3b38 100644
--- a/arch/x86/include/asm/string_64.h
+++ b/arch/x86/include/asm/string_64.h
@@ -82,23 +82,64 @@ int strcmp(const char *cs, const char *ct);
 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
 #define __HAVE_ARCH_MEMCPY_FLUSHCACHE 1
 void __memcpy_flushcache(void *dst, const void *src, size_t cnt);
-static __always_inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
+
+static __always_inline void movnti_4(void *dst, const void *src)
+{
+	asm volatile("movntil %1, %0"
+		     : "=m"(*(u32 *)dst)
+		     : "r"(*(const u32 *)src)
+		     : "memory");
+}
+
+static __always_inline void movnti_8(void *dst, const void *src)
+{
+	asm volatile("movntiq %1, %0"
+		     : "=m"(*(u64 *)dst)
+		     : "r"(*(const u64 *)src)
+		     : "memory");
+}
+
+static __always_inline void movnti_16(void *dst, const void *src)
+{
+	movnti_8(dst, src);
+	movnti_8(dst + 8, src + 8);
+}
+
+static __always_inline void movnti_32(void *dst, const void *src)
+{
+	movnti_16(dst, src);
+	movnti_16(dst + 16, src + 16);
+}
+
+static __always_inline void movnti_64(void *dst, const void *src)
+{
+	movnti_32(dst, src);
+	movnti_32(dst + 32, src + 32);
+}
+
+static __always_inline void memcpy_flushcache(void *dst, const void *src,
+					      size_t cnt)
 {
-	if (__builtin_constant_p(cnt)) {
-		switch (cnt) {
-			case 4:
-				asm ("movntil %1, %0" : "=m"(*(u32 *)dst) : "r"(*(u32 *)src));
-				return;
-			case 8:
-				asm ("movntiq %1, %0" : "=m"(*(u64 *)dst) : "r"(*(u64 *)src));
-				return;
-			case 16:
-				asm ("movntiq %1, %0" : "=m"(*(u64 *)dst) : "r"(*(u64 *)src));
-				asm ("movntiq %1, %0" : "=m"(*(u64 *)(dst + 8)) : "r"(*(u64 *)(src + 8)));
-				return;
-		}
+	if (!__builtin_constant_p(cnt))
+		return __memcpy_flushcache(dst, src, cnt);
+
+	/*
+	 * The relevant fixed-size copies here are the x86_64 struct page sizes:
+	 * 64, 80, and 96 bytes. Keep 32-byte and 48-byte copies inline as well
+	 * instead of sending those nearby fixed-size cases back to
+	 * __memcpy_flushcache().
+	 */
+	switch (cnt) {
+	case 4:  movnti_4(dst, src); break;
+	case 8:  movnti_8(dst, src); break;
+	case 16: movnti_16(dst, src); break;
+	case 32: movnti_32(dst, src); break;
+	case 48: movnti_32(dst, src); movnti_16(dst + 32, src + 32); break;
+	case 64: movnti_64(dst, src); break;
+	case 80: movnti_64(dst, src); movnti_16(dst + 64, src + 64); break;
+	case 96: movnti_64(dst, src); movnti_32(dst + 64, src + 64); break;
+	default: __memcpy_flushcache(dst, src, cnt); break;
 	}
-	__memcpy_flushcache(dst, src, cnt);
 }
 
 #define memcpy_nontemporal memcpy_nontemporal
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH v11 0/7] mm: optimize zone-device memmap initialization
  2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
                   ` (6 preceding siblings ...)
  2026-08-31 11:16 ` [PATCH v11 7/7] x86/string: extend memcpy_flushcache() fixed-size fastpaths Li Zhe
@ 2026-08-31 23:45 ` Andrew Morton
  2026-09-01  2:58   ` Li Zhe
  7 siblings, 1 reply; 14+ messages in thread
From: Andrew Morton @ 2026-08-31 23:45 UTC (permalink / raw)
  To: Li Zhe
  Cc: apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx, linux-arch, linux-hardening,
	linux-kernel, linux-mm, x86

On Mon, 31 Aug 2026 19:16:31 +0800 "Li Zhe" <lizhe.67@bytedance.com> wrote:

> memmap_init_zone_device() can take a noticeable amount of time when large
> pmem namespaces are bound or rebound, because it initializes nearly
> identical struct page descriptors one PFN at a time. This series reduces
> that ZONE_DEVICE memmap initialization overhead by reusing prepared
> struct page templates and, on x86, using memcpy_nontemporal() for the
> template copy path.
> 
> ...
>
> This reduces the average memmap initialization time measured during
> rebind by about 48.0% for nd_pmem and 41.6% for dax_pmem on that arm64
> VM setup. Since this arm64 setup does not use the x86 MOVNTI fast paths,
> the result also suggests that the generic template-copy optimization can
> benefit architectures without an architecture-specific
> memcpy_nontemporal() backend.

Well that's nice.

Sashiko seems to have found some new things to complain about:
	https://sashiko.dev/#/patchset/20260831111638.76012-1-lizhe.67@bytedance.com



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v11 0/7] mm: optimize zone-device memmap initialization
  2026-08-31 23:45 ` [PATCH v11 0/7] mm: optimize zone-device memmap initialization Andrew Morton
@ 2026-09-01  2:58   ` Li Zhe
  2026-09-01 23:07     ` Andrew Morton
  0 siblings, 1 reply; 14+ messages in thread
From: Li Zhe @ 2026-09-01  2:58 UTC (permalink / raw)
  To: Andrew Morton
  Cc: apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx, linux-arch, linux-hardening,
	linux-kernel, linux-mm, x86

On 9/1/26 7:45 AM, Andrew Morton wrote:
> On Mon, 31 Aug 2026 19:16:31 +0800 "Li Zhe" <lizhe.67@bytedance.com> wrote:
>
>> memmap_init_zone_device() can take a noticeable amount of time when large
>> pmem namespaces are bound or rebound, because it initializes nearly
>> identical struct page descriptors one PFN at a time. This series reduces
>> that ZONE_DEVICE memmap initialization overhead by reusing prepared
>> struct page templates and, on x86, using memcpy_nontemporal() for the
>> template copy path.
>>
>> ...
>>
>> This reduces the average memmap initialization time measured during
>> rebind by about 48.0% for nd_pmem and 41.6% for dax_pmem on that arm64
>> VM setup. Since this arm64 setup does not use the x86 MOVNTI fast paths,
>> the result also suggests that the generic template-copy optimization can
>> benefit architectures without an architecture-specific
>> memcpy_nontemporal() backend.
> Well that's nice.
>
> Sashiko seems to have found some new things to complain about:
> 	https://sashiko.dev/#/patchset/20260831111638.76012-1-lizhe.67@bytedance.com
>
Hi Andrew,

Thanks for taking a look.

For the comment on patch 5 about the cnt == 0 case, I agree that
memcpy_flushcache() should preserve the usual zero-length memcpy
semantics. This is a pre-existing issue in the x86
memcpy_flushcache()/__memcpy_flushcache() implementation, not a bug
introduced by this series. The new ZONE_DEVICE call site added by this
series always copies sizeof(struct page), so it cannot hit the
zero-length case.

Since this is a pre-existing issue and is independent of this patchset,
would you prefer me to send a separate standalone fix for the x86
memcpy_flushcache() zero-length case, rather than folding it into this
series?

For the MOVNTI ordering concern in patch 6, this was discussed in the
previous round. Based on that discussion, I believe the current code is
correct, so I do not plan any additional code changes for these items.

Thanks,
Zhe

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v11 0/7] mm: optimize zone-device memmap initialization
  2026-09-01  2:58   ` Li Zhe
@ 2026-09-01 23:07     ` Andrew Morton
  0 siblings, 0 replies; 14+ messages in thread
From: Andrew Morton @ 2026-09-01 23:07 UTC (permalink / raw)
  To: Li Zhe
  Cc: apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, rppt, tglx, linux-arch, linux-hardening,
	linux-kernel, linux-mm, x86

On Tue, 1 Sep 2026 10:58:09 +0800 "Li Zhe" <lizhe.67@bytedance.com> wrote:

> On 9/1/26 7:45 AM, Andrew Morton wrote:
> > On Mon, 31 Aug 2026 19:16:31 +0800 "Li Zhe" <lizhe.67@bytedance.com> wrote:
> >
> >> memmap_init_zone_device() can take a noticeable amount of time when large
> >> pmem namespaces are bound or rebound, because it initializes nearly
> >> identical struct page descriptors one PFN at a time. This series reduces
> >> that ZONE_DEVICE memmap initialization overhead by reusing prepared
> >> struct page templates and, on x86, using memcpy_nontemporal() for the
> >> template copy path.
> >>
> >> ...
> >>
> >> This reduces the average memmap initialization time measured during
> >> rebind by about 48.0% for nd_pmem and 41.6% for dax_pmem on that arm64
> >> VM setup. Since this arm64 setup does not use the x86 MOVNTI fast paths,
> >> the result also suggests that the generic template-copy optimization can
> >> benefit architectures without an architecture-specific
> >> memcpy_nontemporal() backend.
> > Well that's nice.
> >
> > Sashiko seems to have found some new things to complain about:
> > 	https://sashiko.dev/#/patchset/20260831111638.76012-1-lizhe.67@bytedance.com
> >
> Hi Andrew,
> 
> Thanks for taking a look.
> 
> For the comment on patch 5 about the cnt == 0 case, I agree that
> memcpy_flushcache() should preserve the usual zero-length memcpy
> semantics. This is a pre-existing issue in the x86
> memcpy_flushcache()/__memcpy_flushcache() implementation, not a bug
> introduced by this series. The new ZONE_DEVICE call site added by this
> series always copies sizeof(struct page), so it cannot hit the
> zero-length case.

OK, thanks.

> Since this is a pre-existing issue and is independent of this patchset,
> would you prefer me to send a separate standalone fix for the x86
> memcpy_flushcache() zero-length case, rather than folding it into this
> series?

A standalone thing please.  It sounds like a candidate for the x86
tree, as long as Sashiko is wrong in implying that your [5/7] could
trigger this bug.

> For the MOVNTI ordering concern in patch 6, this was discussed in the
> previous round. Based on that discussion, I believe the current code is
> correct, so I do not plan any additional code changes for these items.
> 
> Thanks,
> Zhe

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init
  2026-08-31 11:16 ` [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init Li Zhe
@ 2026-09-02  4:16   ` Mike Rapoport
  2026-09-03  2:58     ` Li Zhe
  0 siblings, 1 reply; 14+ messages in thread
From: Mike Rapoport @ 2026-09-02  4:16 UTC (permalink / raw)
  To: Li Zhe
  Cc: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, tglx, linux-arch, linux-hardening, linux-kernel,
	linux-mm, x86

On Mon, Aug 31, 2026 at 07:16:34PM +0800, Li Zhe wrote:
> memmap_init_zone_device() repeats nearly identical head-page
> initialization for each PFN. Initialize the first real ZONE_DEVICE head
> page through the existing path, copy that final state into a reusable
> template, refresh the PFN-dependent fields in that template before each
> copy, and copy it into the remaining destination pages.
> 
> Use the template path unconditionally. The page_ref_set tracepoint is
> primarily a debugging aid, while this code is still initializing struct
> pages before they are handed out. From the perspective of users of those
> pages, the initialization-time refcount transitions are not part of the
> observable page lifetime.
> 
> This means page_ref_set will no longer observe every initialization-time
> refcount assignment for copied ZONE_DEVICE head pages. The impact is
> controlled because the final initialized struct page state is unchanged,
> and keeping a separate non-template path only for this local tracepoint
> observability would add complexity to the common path.
> 
> This patch accelerates head-page initialization. The pfns_per_compound
> == 1 case gets the full benefit here, compound tails are handled in the
> next patch.
> 
> Tested in a VM with a 100 GB fsdax namespace device configured with
> map=dev on Intel Ice Lake server. This test exercises the nd_pmem rebind
> path (pfns_per_compound == 1).
> 
> Test procedure:
> Rebind the nd_pmem driver 30 times and collect the memmap initialization
> time from the pr_debug() output of memmap_init_zone_device().
> 
> Base(v7.3-rc1):
>   Average of rebinds for nd_pmem driver: 221.07 ms
> 
> With this patch and its prerequisites applied:
>   Average of rebinds for nd_pmem driver: 155.00 ms
> 
> This reduces the average memmap initialization time measured during
> rebind from 221.07 ms to 155.00 ms, or about 29.9%.
> 
> Signed-off-by: Li Zhe <lizhe.67@bytedance.com>

With one nit below

Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

> ---
>  mm/mm_init.c | 38 +++++++++++++++++++++++++++++++++++---
>  1 file changed, 35 insertions(+), 3 deletions(-)
> 
> @@ -1093,10 +1106,29 @@ void __ref memmap_init_zone_device(struct zone *zone,
>  		nr_pages = end_pfn - start_pfn;
>  	}
>  
> -	for (pfn = start_pfn; pfn < end_pfn; pfn += pfns_per_compound) {
> -		struct page *page = pfn_to_page(pfn);
> +	if (!nr_pages)
> +		return;
>  
> -		__init_zone_device_page(page, pfn, zone_idx, nid, pgmap);
> +	/*
> +	 * Seed the reusable head-page template from the first real struct
> +	 * page. The normal page-init and refcount helpers must operate on
> +	 * a real memmap entry rather than a stack object.
> +	 */
> +	pfn = start_pfn;
> +	page = pfn_to_page(pfn);
> +	__init_zone_device_page(page, pfn, zone_idx, nid, pgmap);
> +	memcpy(&template, page, sizeof(*page));
> +	if (pfns_per_compound != 1)
> +		memmap_init_compound(page, pfn, zone_idx, nid, pgmap,
> +				     compound_nr_pages(pfn, altmap, pgmap));
> +	pfn += pfns_per_compound;
> +
> +	/* Initialize the remaining head pages from template. */
> +	for (; pfn < end_pfn; pfn += pfns_per_compound) {
> +		page = pfn_to_page(pfn);
> +
> +		zone_device_page_init_from_template(page, pfn,
> +						    &template);

Nit: this seems to fit into 80-chars line without wrapping.

>  
>  		if (IS_ALIGNED(pfn, PAGES_PER_SECTION))
>  			cond_resched();
> -- 
> 2.20.1

-- 
Sincerely yours,
Mike.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v11 4/7] mm: extend the template fast path to zone-device compound tails
  2026-08-31 11:16 ` [PATCH v11 4/7] mm: extend the template fast path to zone-device compound tails Li Zhe
@ 2026-09-02  4:18   ` Mike Rapoport
  0 siblings, 0 replies; 14+ messages in thread
From: Mike Rapoport @ 2026-09-02  4:18 UTC (permalink / raw)
  To: Li Zhe
  Cc: akpm, apopple, arnd, balbirs, bp, dave.hansen, david, kees, mingo,
	muchun.song, tglx, linux-arch, linux-hardening, linux-kernel,
	linux-mm, x86

On Mon, Aug 31, 2026 at 07:16:35PM +0800, Li Zhe wrote:
> The template fast path from the previous patch only accelerates head
> pages. Compound tails in memmap_init_compound() still go through the
> normal initialization path one by one.
> 
> Build separate head and tail templates and reuse one prepared tail
> template across the tail pages in a compound range. Head pages preserve
> the existing refcount policy, while compound tails always start with a
> refcount of 0 after prep_compound_tail().
> 
> This extends the template-copy fast path to pfns_per_compound > 1.
> Tail-page PFN-dependent fields are refreshed in the reusable tail
> template before each copy.
> 
> Do not keep a separate non-template fallback for compound tails either.
> These pages are still under memmap initialization, and the
> initialization-time refcount updates are not part of the observable
> lifetime of pages handed out later.
> 
> The impact is controlled for the same reason as for head pages. The
> first tail page still seeds the reusable tail template through the
> normal tail initialization sequence, and the copied tail pages have the
> same final initialized state except for the PFN-dependent fields
> refreshed before each copy.
> 
> Tested in a VM with a 100 GB devdax namespace (align=2097152) on Intel
> Ice Lake server. This test exercises the dax_pmem rebind path and
> measures memmap initialization latency.
> 
> Test procedure:
> Unbind and rebind the dax_pmem driver 30 times, collect memmap
> initialization time from the pr_debug() output of
> memmap_init_zone_device().
> 
> Base(v7.3-rc1):
>   Average of rebinds for dax_pmem driver: 191.20 ms
> 
> With this patch and its prerequisites applied:
>   Average of rebinds for dax_pmem driver: 176.87 ms
> 
> This reduces the average memmap initialization time measured during
> rebind from 191.20 ms to 176.87 ms, or about 7.5%.
> 
> Signed-off-by: Li Zhe <lizhe.67@bytedance.com>

Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

> ---
>  mm/mm_init.c | 24 ++++++++++++++++++------
>  1 file changed, 18 insertions(+), 6 deletions(-)

-- 
Sincerely yours,
Mike.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init
  2026-09-02  4:16   ` Mike Rapoport
@ 2026-09-03  2:58     ` Li Zhe
  0 siblings, 0 replies; 14+ messages in thread
From: Li Zhe @ 2026-09-03  2:58 UTC (permalink / raw)
  To: rppt, akpm
  Cc: apopple, arnd, balbirs, bp, dave.hansen, david, kees, linux-arch,
	linux-hardening, linux-kernel, linux-mm, lizhe.67, mingo,
	muchun.song, tglx, x86

On Wed, 2 Sep 2026 07:16:12 +0300, <rppt@kernel.org> wrote:

> > +	/* Initialize the remaining head pages from template. */
> > +	for (; pfn < end_pfn; pfn += pfns_per_compound) {
> > +		page = pfn_to_page(pfn);
> > +
> > +		zone_device_page_init_from_template(page, pfn,
> > +						    &template);
> 
> Nit: this seems to fit into 80-chars line without wrapping.

Hi Mike,

Thanks for the review. Yes, this fits on one line.

Hi Andrew, could you please squash the following fixup into patch 3?

---
diff --git a/mm/mm_init.c b/mm/mm_init.c
index ba4148de2b28..84fe53633e44 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -1127,8 +1127,7 @@ void __ref memmap_init_zone_device(struct zone *zone,
 	for (; pfn < end_pfn; pfn += pfns_per_compound) {
 		page = pfn_to_page(pfn);
 
-		zone_device_page_init_from_template(page, pfn,
-						    &template);
+		zone_device_page_init_from_template(page, pfn, &template);
 
 		if (IS_ALIGNED(pfn, PAGES_PER_SECTION))
 			cond_resched();
---

Thanks,
Zhe

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-09-03  2:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-31 11:16 [PATCH v11 0/7] mm: optimize zone-device memmap initialization Li Zhe
2026-08-31 11:16 ` [PATCH v11 1/7] mm: fix stale ZONE_DEVICE refcount comment Li Zhe
2026-08-31 11:16 ` [PATCH v11 2/7] mm: add a set_page_section_from_pfn() helper Li Zhe
2026-08-31 11:16 ` [PATCH v11 3/7] mm: add a template-based fast path for zone-device page init Li Zhe
2026-09-02  4:16   ` Mike Rapoport
2026-09-03  2:58     ` Li Zhe
2026-08-31 11:16 ` [PATCH v11 4/7] mm: extend the template fast path to zone-device compound tails Li Zhe
2026-09-02  4:18   ` Mike Rapoport
2026-08-31 11:16 ` [PATCH v11 5/7] string: introduce memcpy_nontemporal() Li Zhe
2026-08-31 11:16 ` [PATCH v11 6/7] mm: use memcpy_nontemporal() in zone-device template copies Li Zhe
2026-08-31 11:16 ` [PATCH v11 7/7] x86/string: extend memcpy_flushcache() fixed-size fastpaths Li Zhe
2026-08-31 23:45 ` [PATCH v11 0/7] mm: optimize zone-device memmap initialization Andrew Morton
2026-09-01  2:58   ` Li Zhe
2026-09-01 23:07     ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox