Re: [PATCH] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM

From: Kiryl Shutsemau

Date: Wed Oct 21 2026 - 06:00:35 EST


On Sat, Oct 17, 2026 at 12:27:25PM -0700, Usama Arif wrote:
> diff --git a/arch/x86/lib/memcpy_64.S b/arch/x86/lib/memcpy_64.S
> index 12a23fa7c44c5..6b3e203b6087c 100644
> --- a/arch/x86/lib/memcpy_64.S
> +++ b/arch/x86/lib/memcpy_64.S
> @@ -31,9 +31,10 @@
> * which the compiler could/should do much better anyway.
> */
> SYM_TYPED_FUNC_START(__memcpy)
> - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM
> + /* The FSRM destination move shares the space used by the ERMS jump. */
> + ALTERNATIVE_2 "jmp memcpy_orig", "jmp memcpy_erms", X86_FEATURE_ERMS, \
> + "movq %rdi, %rax", X86_FEATURE_FSRM
>
> - movq %rdi, %rax

Moving MOVQ inside ALTERNATIVE_2 is ugly...

And it only turns a 5-byte NOP into a 2-byte one on FSRM.
What is the point?

> movq %rdx, %rcx
> rep movsb
> RET
> @@ -171,3 +172,11 @@ SYM_FUNC_START_LOCAL(memcpy_orig)
> RET
> SYM_FUNC_END(memcpy_orig)
>
> +SYM_FUNC_START_LOCAL(memcpy_erms)
> + cmpq $2048, %rdx
> + jb memcpy_orig
> + movq %rdi, %rax
> + movq %rdx, %rcx
> + rep movsb
> + RET
> +SYM_FUNC_END(memcpy_erms)

Copies below 2048 now take two jumps to reach memcpy_orig() instead of
one, and David's point is that nearly all kernel copies are that small.

Can the check go in memcpy_orig() after the existing 32-byte compare?

--
Kiryl Shutsemau / Kirill A. Shutemov