Re: [PATCH] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM
From: Kiryl Shutsemau
Date: Thu Oct 15 2026 - 06:46:26 EST
On Tue, Oct 13, 2026 at 03:04:42AM -0700, Usama Arif wrote:
> SYM_TYPED_FUNC_START(__memcpy)
> - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM
> + ALTERNATIVE_2 "jmp memcpy_orig", \
> + "cmpq $512, %rdx; jb memcpy_orig", X86_FEATURE_ERMS, \
> + "", X86_FEATURE_FSRM
This grows the patch site from 2 bytes to 13. On FSRM the replacement is
empty, and 13 bytes is more than ASM_NOP_MAX, so add_nop() puts an
unconditional jump here, executed on every memcpy() call on FSRM
machines. :/
Maybe it is worth moving the patch site into memcpy_orig()? It already
has a 32-byte check. Which brings me to the other question:
Why 512? memmove() uses REP MOVSB from 32 bytes on ERMS. Can we do the
same here? Then we don't need a new branch at all.
--- a/arch/x86/lib/memcpy_64.S
+++ b/arch/x86/lib/memcpy_64.S
@@ -50,6 +50,8 @@
cmpq $0x20, %rdx
jb .Lhandle_tail
+ ALTERNATIVE "", __stringify(movq %rdx, %rcx; rep movsb; RET), X86_FEATURE_ERMS
+
/*
* We check whether memory false dependence could occur,
* then jump to corresponding copy mode.
--
Kiryl Shutsemau / Kirill A. Shutemov