Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Li Zhe" <lizhe.67@bytedance.com>
To: <akpm@linux-foundation.org>, <apopple@nvidia.com>,
	<arnd@arndb.de>,  <balbirs@nvidia.com>, <bp@alien8.de>,
	<dave.hansen@linux.intel.com>,  <david@kernel.org>,
	<kees@kernel.org>, <mingo@redhat.com>,  <muchun.song@linux.dev>,
	<rppt@kernel.org>, <tglx@kernel.org>
Cc: <linux-arch@vger.kernel.org>, <linux-hardening@vger.kernel.org>,
	 <linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>,
	<x86@kernel.org>,  <lizhe.67@bytedance.com>
Subject: [PATCH v9 6/8] string: introduce memcpy_nontemporal()
Date: Mon,  3 Aug 2026 15:09:27 +0800	[thread overview]
Message-ID: <20260803070929.86075-7-lizhe.67@bytedance.com> (raw)
In-Reply-To: <20260803070929.86075-1-lizhe.67@bytedance.com>

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


  parent reply	other threads:[~2026-08-03  7:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03  7:09 [PATCH v9 0/8] mm: optimize zone-device memmap initialization Li Zhe
2026-08-03  7:09 ` [PATCH v9 1/8] mm: fix stale ZONE_DEVICE refcount comment Li Zhe
2026-08-03  7:09 ` [PATCH v9 2/8] mm: factor zone-device page init helpers out of __init_zone_device_page Li Zhe
2026-08-03  7:09 ` [PATCH v9 3/8] mm: add a set_page_section_from_pfn() helper Li Zhe
2026-08-03  7:09 ` [PATCH v9 4/8] mm: add a template-based fast path for zone-device page init Li Zhe
2026-08-03  8:39   ` Muchun Song
2026-08-05  9:50     ` Li Zhe
2026-08-03  7:09 ` [PATCH v9 5/8] mm: extend the template fast path to zone-device compound tails Li Zhe
2026-08-03  7:09 ` Li Zhe [this message]
2026-08-03  7:09 ` [PATCH v9 7/8] mm: use memcpy_nontemporal() in zone-device template copies Li Zhe
2026-08-03  7:09 ` [PATCH v9 8/8] x86/string: extend memcpy_flushcache() fixed-size fastpaths Li Zhe
2026-08-04 20:35   ` Borislav Petkov
2026-08-05 11:04     ` Li Zhe
2026-08-03 21:40 ` [PATCH v9 0/8] mm: optimize zone-device memmap initialization Andrew Morton
2026-08-05  9:49   ` Li Zhe

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=20260803070929.86075-7-lizhe.67@bytedance.com \
    --to=lizhe.67@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=arnd@arndb.de \
    --cc=balbirs@nvidia.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=muchun.song@linux.dev \
    --cc=rppt@kernel.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /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