mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() for large copies
@ 2026-11-16  7:25 Qi Xi
  2026-11-16  7:25 ` [PATCH v4 1/2] arm64: copy_template: add an offset-addressed 64-byte loop Qi Xi
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Qi Xi @ 2026-11-16  7:25 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, linux-kernel, Mark Rutland, Ben Niu,
	Jinjiang Tu, wangkefeng.wang, sunnanyong, xiqi2

From: Qi Xi <xiqi2@huawei.com>

Based on Ben Niu's "Faster Arm64 __arch_copy_from_user and
__arch_copy_to_user" work [1], this series continues the optimization
for __arch_copy_from_user() only, following Catalin's review of v3 [2].

For copies of 4096 bytes or more, __arch_copy_from_user() uses
privileged LDP instead of LDTR and toggles PSTATE.PAN for the duration
of the copy, together with an offset-addressed 64-byte loop added to
copy_template.S.  Smaller copies keep the original code.

copy_to_user() is deliberately left alone.  Clearing PAN and using
privileged STP there puts the PAN update in the middle of the user
store stream, which breaks hardware write streaming; on Kunpeng 920
the resulting regression outweighs the gain from paired stores.

Performance improvements measured on Kunpeng 920:

The ku_copy microbenchmark [3] (a kernel module that measures
copy_from_user throughput across various sizes by copying 1GB of
data in each test):
copy_from_user throughput change (positive = improvement):
8B-2048B: ~0% (not covered by this optimization, no regression)
4KB: +2.0%   8KB: +19.1%  16KB: +29.8%  32KB: +36.5%
64KB: +38.0%  128KB: +57.9%  256KB: +71.2%

Real-world workloads:
- iperf3 over virtio-net/tap in a two-level PVM stack
(L1 send to KVM L2 guest):
Throughput improved from 38.5 to 50.1 Gbps (+30%).
copy_from_user hotspot in the QEMU main-loop thread reduced
from 68% to 55%.
- meshproxy:
256KB request latency reduced from 0.91 ms to 0.78 ms (-14%)
at 16 connections / 16k QPS.

Changes since v3:
 - No global PAN disabling.  PAN is toggled only around the
   new fast path, so LDTR/STTR remain the default for every
   other copy and keep protecting against user-controlled kernel
   accesses, as Catalin pointed out.
 - Fault semantics fixed: every load in the 64-byte loop has its own
   exception table entry, and the fixup advances dst by the number of
   16-byte blocks that were actually stored.  A fault in the middle of
   the loop now returns the number of bytes really not copied instead.
 - A 4096-byte threshold is added; from there on the new path is
   used, since toggling PAN also has a cost.
 - copy_to_user() is deliberately left alone.

[1] https://lore.kernel.org/all/20251018052237.1368504-2-benniu@meta.com/
[2] https://lore.kernel.org/all/20260316123100.82932-1-xiqi2@huawei.com/
[3] https://github.com/mcfi/benchmark/tree/main/ku_copy

Qi Xi (2):
  arm64: copy_template: add an offset-addressed 64-byte loop
  arm64: copy_from_user: use privileged LDP/STP for large copies

 arch/arm64/lib/Makefile             |   2 +-
 arch/arm64/lib/copy_from_user.S     |   8 ++
 arch/arm64/lib/copy_from_user_opt.S | 114 ++++++++++++++++++++++++++++
 arch/arm64/lib/copy_template.S      |  35 +++++++++
 4 files changed, 158 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/lib/copy_from_user_opt.S

-- 
2.53.0


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

* [PATCH v4 1/2] arm64: copy_template: add an offset-addressed 64-byte loop
  2026-11-16  7:25 [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() for large copies Qi Xi
@ 2026-11-16  7:25 ` Qi Xi
  2026-11-16  7:25 ` [PATCH v4 2/2] arm64: copy_from_user: use privileged LDP/STP for large copies Qi Xi
  2026-12-01 11:58 ` [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() " Qi Xi
  2 siblings, 0 replies; 4+ messages in thread
From: Qi Xi @ 2026-11-16  7:25 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, linux-kernel, Mark Rutland, Ben Niu,
	Jinjiang Tu, wangkefeng.wang, sunnanyong, xiqi2

From: Qi Xi <xiqi2@huawei.com>

copy_template.S is shared by copy_from_user.S and copy_to_user.S.  In
its 64-byte loop every load and store is post-indexed, which on the
user side means two LDTR/STTR accesses followed by an address update.

Add a COPY_TEMPLATE_PAIRS variant of the loop that uses offset
addressing and advances src and dst once per iteration.  Without
per-access writeback, the loads and stores of an iteration can overlap
rather than serialise on the base register.

Defining COPY_TEMPLATE_PAIRS before including copy_template.S selects
the variant.

Co-developed-by: Ben Niu <benniu@meta.com>
Signed-off-by: Ben Niu <benniu@meta.com>
Signed-off-by: Jinjiang Tu <tujinjiang@huawei.com>
Signed-off-by: Qi Xi <xiqi2@huawei.com>
---
 arch/arm64/lib/copy_template.S | 35 ++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/arch/arm64/lib/copy_template.S b/arch/arm64/lib/copy_template.S
index 7f2f5a0e2fb9..12ab3bc8afa3 100644
--- a/arch/arm64/lib/copy_template.S
+++ b/arch/arm64/lib/copy_template.S
@@ -161,6 +161,40 @@ alternative_else_nop_endif
 	*/
 	.p2align	L1_CACHE_SHIFT
 .Lcpy_body_large:
+#ifdef COPY_TEMPLATE_PAIRS
+	/* pre-get 64 bytes data. */
+	ldp2	A_l, A_h, src, #0, 9997f
+	ldp2	B_l, B_h, src, #16, 9997f
+	ldp2	C_l, C_h, src, #32, 9997f
+	ldp2	D_l, D_h, src, #48, 9997f
+	add	src, src, #64
+1:
+	/*
+	* interlace the load of next 64 bytes data block with store of the last
+	* loaded 64 bytes data.
+	*
+	* The loads can fault on user memory.  Since dst is not advanced
+	* per access, each fault is fixed up to account for the 16-byte
+	* blocks already stored.
+	*/
+	stp2	A_l, A_h, dst, #0
+	ldp2	A_l, A_h, src, #0, .Lcopy_opt_fixup16
+	stp2	B_l, B_h, dst, #16
+	ldp2	B_l, B_h, src, #16, .Lcopy_opt_fixup32
+	stp2	C_l, C_h, dst, #32
+	ldp2	C_l, C_h, src, #32, .Lcopy_opt_fixup48
+	stp2	D_l, D_h, dst, #48
+	ldp2	D_l, D_h, src, #48, .Lcopy_opt_fixup64
+	add	dst, dst, #64
+	add	src, src, #64
+	subs	count, count, #64
+	b.ge	1b
+	stp2	A_l, A_h, dst, #0
+	stp2	B_l, B_h, dst, #16
+	stp2	C_l, C_h, dst, #32
+	stp2	D_l, D_h, dst, #48
+	add	dst, dst, #64
+#else
 	/* pre-get 64 bytes data. */
 	ldp1	A_l, A_h, src, #16
 	ldp1	B_l, B_h, src, #16
@@ -185,6 +219,7 @@ alternative_else_nop_endif
 	stp1	B_l, B_h, dst, #16
 	stp1	C_l, C_h, dst, #16
 	stp1	D_l, D_h, dst, #16
+#endif
 
 	tst	count, #0x3f
 	b.ne	.Ltail63
-- 
2.53.0


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

* [PATCH v4 2/2] arm64: copy_from_user: use privileged LDP/STP for large copies
  2026-11-16  7:25 [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() for large copies Qi Xi
  2026-11-16  7:25 ` [PATCH v4 1/2] arm64: copy_template: add an offset-addressed 64-byte loop Qi Xi
@ 2026-11-16  7:25 ` Qi Xi
  2026-12-01 11:58 ` [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() " Qi Xi
  2 siblings, 0 replies; 4+ messages in thread
From: Qi Xi @ 2026-11-16  7:25 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, linux-kernel, Mark Rutland, Ben Niu,
	Jinjiang Tu, wangkefeng.wang, sunnanyong, xiqi2

From: Qi Xi <xiqi2@huawei.com>

For copies of 4096 bytes or more, __arch_copy_from_user_opt() toggles
PSTATE.PAN, allowing it to use privileged LDP instead of LDTR,
together with the offset-addressed loop from copy_template.S.  Smaller
copies keep using the original code.

copy_to_user is left unchanged.  Using privileged stores there would
need the same PAN toggle, but a PAN update in the middle of the
user-side store stream breaks hardware write streaming, which costs
more than the paired stores save.

Signed-off-by: Qi Xi <xiqi2@huawei.com>
---
 arch/arm64/lib/Makefile             |   2 +-
 arch/arm64/lib/copy_from_user.S     |   8 ++
 arch/arm64/lib/copy_from_user_opt.S | 114 ++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/lib/copy_from_user_opt.S

diff --git a/arch/arm64/lib/Makefile b/arch/arm64/lib/Makefile
index 38ffd359123c..b7c1c579aec0 100644
--- a/arch/arm64/lib/Makefile
+++ b/arch/arm64/lib/Makefile
@@ -4,7 +4,7 @@
 KCSAN_SANITIZE_delay.o := n
 
 lib-y		:= clear_user.o delay.o copy_from_user.o		\
-		   copy_to_user.o copy_page.o				\
+		   copy_from_user_opt.o copy_to_user.o copy_page.o	\
 		   csum.o insn.o memchr.o memcpy.o			\
 		   memset.o memcmp.o strcmp.o strncmp.o strlen.o	\
 		   strnlen.o strchr.o strrchr.o tishift.o
diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
index 400057d607ec..ec23066c2a37 100644
--- a/arch/arm64/lib/copy_from_user.S
+++ b/arch/arm64/lib/copy_from_user.S
@@ -64,6 +64,12 @@ srcin	.req	x15
 SYM_FUNC_START(__arch_copy_from_user)
 	add	end, x0, x2
 	mov	srcin, x1
+	/*
+	 * Use the privileged LDP/STP path for large copies.  Below 4096
+	 * bytes the PAN toggle costs more than it saves.
+	 */
+	cmp	x2, #4096
+	b.hs	9002f
 #include "copy_template.S"
 	mov	x0, #0				// Nothing to copy
 	ret
@@ -79,5 +85,7 @@ USER(9998f, ldtrb tmp1w, [srcin])
 	strb	tmp1w, [dst], #1
 9998:	sub	x0, end, dst			// bytes not copied
 	ret
+
+9002:	b	__arch_copy_from_user_opt
 SYM_FUNC_END(__arch_copy_from_user)
 EXPORT_SYMBOL(__arch_copy_from_user)
diff --git a/arch/arm64/lib/copy_from_user_opt.S b/arch/arm64/lib/copy_from_user_opt.S
new file mode 100644
index 000000000000..2c0e3cb4798a
--- /dev/null
+++ b/arch/arm64/lib/copy_from_user_opt.S
@@ -0,0 +1,114 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 Huawei Ltd.
+ */
+
+#include <linux/linkage.h>
+
+#include <asm/asm-uaccess.h>
+#include <asm/assembler.h>
+#include <asm/cache.h>
+
+#define COPY_TEMPLATE_PAIRS
+
+	.macro ldrb1 reg, ptr, val
+	user_ldst 9998f, ldtrb, \reg, \ptr, \val
+	.endm
+
+	.macro strb1 reg, ptr, val
+	strb \reg, [\ptr], \val
+	.endm
+
+	.macro ldrh1 reg, ptr, val
+	user_ldst 9997f, ldtrh, \reg, \ptr, \val
+	.endm
+
+	.macro strh1 reg, ptr, val
+	strh \reg, [\ptr], \val
+	.endm
+
+	.macro ldr1 reg, ptr, val
+	user_ldst 9997f, ldtr, \reg, \ptr, \val
+	.endm
+
+	.macro str1 reg, ptr, val
+	str \reg, [\ptr], \val
+	.endm
+
+	.macro ldp1 reg1, reg2, ptr, val
+	user_ldp 9997f, \reg1, \reg2, \ptr, \val
+	.endm
+
+	.macro stp1 reg1, reg2, ptr, val
+	stp \reg1, \reg2, [\ptr], \val
+	.endm
+
+	.macro ldp2 reg1, reg2, ptr, val, fixup
+8888:	ldp \reg1, \reg2, [\ptr, \val]
+	_asm_extable_uaccess 8888b, \fixup
+	.endm
+
+	.macro stp2 reg1, reg2, ptr, val
+	stp \reg1, \reg2, [\ptr, \val]
+	.endm
+
+	.macro cpy1 dst, src, count
+	.arch_extension mops
+	USER_CPY(9997f, 0, cpyfprt [\dst]!, [\src]!, \count!)
+	USER_CPY(9996f, 0, cpyfmrt [\dst]!, [\src]!, \count!)
+	USER_CPY(9996f, 0, cpyfert [\dst]!, [\src]!, \count!)
+	.endm
+
+end	.req	x5
+srcin	.req	x15
+SYM_FUNC_START(__arch_copy_from_user_opt)
+	add	end, x0, x2
+	mov	srcin, x1
+
+#ifdef CONFIG_KASAN_HW_TAGS
+	/*
+	 * KASAN enables tag checking for EL1 accesses (SCTLR_EL1.TCF)
+	 * while the user's TCF0 mode may have checking disabled.
+	 * Suppress it for the copy, as uaccess_enable_privileged() does.
+	 */
+	ALTERNATIVE("nop", __stringify(SET_PSTATE_TCO(1)), ARM64_MTE)
+#endif
+	/*
+	 * PSTATE.PAN blocks EL1 access to user memory, so clear it for
+	 * the privileged LDP/STP below and restore it on exit.
+	 */
+	ALTERNATIVE("nop", __stringify(SET_PSTATE_PAN(0)), ARM64_HAS_PAN)
+
+#include "copy_template.S"
+
+	mov	x0, #0				// Nothing to copy
+
+.L__arch_copy_from_user_opt_exit:
+#ifdef CONFIG_KASAN_HW_TAGS
+	ALTERNATIVE("nop", __stringify(SET_PSTATE_TCO(0)), ARM64_MTE)
+#endif
+	ALTERNATIVE("nop", __stringify(SET_PSTATE_PAN(1)), ARM64_HAS_PAN)
+	ret
+
+	// Exception fixups
+.Lcopy_opt_fixup64:
+	add	dst, dst, #16
+.Lcopy_opt_fixup48:
+	add	dst, dst, #16
+.Lcopy_opt_fixup32:
+	add	dst, dst, #16
+.Lcopy_opt_fixup16:
+	add	dst, dst, #16
+	b	9998f
+
+9996:	b.cs	9997f
+	// Registers are in Option A format
+	add	dst, dst, count
+9997:	cmp	dst, dstin
+	b.ne	9998f
+	// Before being absolutely sure we couldn't copy anything, try harder
+USER(9998f, ldtrb tmp1w, [srcin])
+	strb	tmp1w, [dst], #1
+9998:	sub	x0, end, dst			// bytes not copied
+	b	.L__arch_copy_from_user_opt_exit
+SYM_FUNC_END(__arch_copy_from_user_opt)
-- 
2.53.0


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

* Re: [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() for large copies
  2026-11-16  7:25 [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() for large copies Qi Xi
  2026-11-16  7:25 ` [PATCH v4 1/2] arm64: copy_template: add an offset-addressed 64-byte loop Qi Xi
  2026-11-16  7:25 ` [PATCH v4 2/2] arm64: copy_from_user: use privileged LDP/STP for large copies Qi Xi
@ 2026-12-01 11:58 ` Qi Xi
  2 siblings, 0 replies; 4+ messages in thread
From: Qi Xi @ 2026-12-01 11:58 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, linux-kernel, Mark Rutland, Ben Niu,
	Jinjiang Tu, wangkefeng.wang, sunnanyong, Qi Xi

Kindly ping.  v4 has been up for two weeks now -
any suggestions?

Qi

On 16/11/2026 15:25, Qi Xi wrote:
> From: Qi Xi <xiqi2@huawei.com>
>
> Based on Ben Niu's "Faster Arm64 __arch_copy_from_user and
> __arch_copy_to_user" work [1], this series continues the optimization
> for __arch_copy_from_user() only, following Catalin's review of v3 [2].
>
> For copies of 4096 bytes or more, __arch_copy_from_user() uses
> privileged LDP instead of LDTR and toggles PSTATE.PAN for the duration
> of the copy, together with an offset-addressed 64-byte loop added to
> copy_template.S.  Smaller copies keep the original code.
>
> copy_to_user() is deliberately left alone.  Clearing PAN and using
> privileged STP there puts the PAN update in the middle of the user
> store stream, which breaks hardware write streaming; on Kunpeng 920
> the resulting regression outweighs the gain from paired stores.
>
> Performance improvements measured on Kunpeng 920:
>
> The ku_copy microbenchmark [3] (a kernel module that measures
> copy_from_user throughput across various sizes by copying 1GB of
> data in each test):
> copy_from_user throughput change (positive = improvement):
> 8B-2048B: ~0% (not covered by this optimization, no regression)
> 4KB: +2.0%   8KB: +19.1%  16KB: +29.8%  32KB: +36.5%
> 64KB: +38.0%  128KB: +57.9%  256KB: +71.2%
>
> Real-world workloads:
> - iperf3 over virtio-net/tap in a two-level PVM stack
> (L1 send to KVM L2 guest):
> Throughput improved from 38.5 to 50.1 Gbps (+30%).
> copy_from_user hotspot in the QEMU main-loop thread reduced
> from 68% to 55%.
> - meshproxy:
> 256KB request latency reduced from 0.91 ms to 0.78 ms (-14%)
> at 16 connections / 16k QPS.
>
> Changes since v3:
>   - No global PAN disabling.  PAN is toggled only around the
>     new fast path, so LDTR/STTR remain the default for every
>     other copy and keep protecting against user-controlled kernel
>     accesses, as Catalin pointed out.
>   - Fault semantics fixed: every load in the 64-byte loop has its own
>     exception table entry, and the fixup advances dst by the number of
>     16-byte blocks that were actually stored.  A fault in the middle of
>     the loop now returns the number of bytes really not copied instead.
>   - A 4096-byte threshold is added; from there on the new path is
>     used, since toggling PAN also has a cost.
>   - copy_to_user() is deliberately left alone.
>
> [1] https://lore.kernel.org/all/20251018052237.1368504-2-benniu@meta.com/
> [2] https://lore.kernel.org/all/20260316123100.82932-1-xiqi2@huawei.com/
> [3] https://github.com/mcfi/benchmark/tree/main/ku_copy
>
> Qi Xi (2):
>    arm64: copy_template: add an offset-addressed 64-byte loop
>    arm64: copy_from_user: use privileged LDP/STP for large copies
>
>   arch/arm64/lib/Makefile             |   2 +-
>   arch/arm64/lib/copy_from_user.S     |   8 ++
>   arch/arm64/lib/copy_from_user_opt.S | 114 ++++++++++++++++++++++++++++
>   arch/arm64/lib/copy_template.S      |  35 +++++++++
>   4 files changed, 158 insertions(+), 1 deletion(-)
>   create mode 100644 arch/arm64/lib/copy_from_user_opt.S
>

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

end of thread, other threads:[~2026-12-01 11:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16  7:25 [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() for large copies Qi Xi
2026-11-16  7:25 ` [PATCH v4 1/2] arm64: copy_template: add an offset-addressed 64-byte loop Qi Xi
2026-11-16  7:25 ` [PATCH v4 2/2] arm64: copy_from_user: use privileged LDP/STP for large copies Qi Xi
2026-12-01 11:58 ` [PATCH v4 0/2] arm64: speed up __arch_copy_from_user() " Qi Xi

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®