x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM
PATCH v2 1/1 x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM
Since commit 68674f94ffc9 ("x86: don't use REP_GOOD or ERMS for small memory copies"), memcpy() uses REP MOVSB only on FSRM CPUs, so ERMS CPUs without FSRM, such as Skylake through Cooper Lake, copy every size with the scalar loop. Large copies matter there: in Meta's x86 fleet, about a third of the time in memcpy_orig() is btrfs zstd decompression copying pages into and out of zstd's buffers.
Use REP MOVSB from 2048 bytes on ERMS CPUs. On a 1.4 GHz Cooper Lake, an aligned, cache-hot 4 KiB memcpy() then takes 66.8% less time (123.4 ns instead of 371.7 ns), and btrfs zstd reads take 4% less time. Smaller copies keep the scalar loop: using REP MOVSB from 32 bytes made 32/64/128/256-byte copies 114.9%/54.4%/66.1%/23.2% slower.
To keep the dispatch cheap, fold memcpy_orig() into __memcpy(). With FSRM, "rep movsb; RET" replaces the 32-byte length check. With ERMS, a shift of the length in RDX selects REP MOVSB; RCX already holds the count. Copies below 32 bytes skip the check, which is a single NOP without ERMS.
The FSRM replacement now contains a RET. apply_returns() runs before apply_alternatives(), so with the ITS return thunk it decides whether to keep the thunk from the RET's address in .altinstr_replacement. Align the replacement to a cache line so that the RET keeps it; the thunk is safe wherever the replacement is copied.
---
v1 -> v2: https://lore.kernel.org/r/[email protected]
- Raise the threshold from 512 to 2048 bytes. REP MOVSB was slower than the scalar loop for some misaligned copies of 512 to 640 bytes.
- Fold memcpy_orig() into __memcpy(). With FSRM, "rep movsb; RET" now replaces the 32-byte length check (David Laight, Linus Torvalds). In v1, the 13-byte empty FSRM replacement became a jump that FSRM CPUs took on every call (Kiryl Shutsemau).
- Do the ERMS check after the 32-byte check, so copies below 32 bytes skip it (Kiryl Shutsemau), and keep the count in RCX so REP MOVSB needs no reload (David Laight).
- Align the FSRM replacement so that its RET keeps the ITS return thunk.
- Fix the comment that called the return value the source input.
--- arch/x86/lib/memcpy_64.S | 101 ++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 44 deletions(-)
arch/x86/lib/memcpy_64.S +57 -44
@@ -27,28 +27,33 @@ * * The 'rep movsb' itself is small enough to replace the call, but the * two register moves blow up the code. And one of them is "needed" - * only for the return value that is the same as the source input, + * only for the return value that is the same as the destination input, * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb - RET -SYM_FUNC_END(__memcpy) -EXPORT_SYMBOL(__memcpy) -SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy) -SYM_PIC_ALIAS(memcpy) -EXPORT_SYMBOL(memcpy) - -SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax +#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRM - cmpq $0x20, %rdx - jb .Lhandle_tail + /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMS /* * We check whether memory false dependence could occur,
@@ -56,9 +61,9 @@ SYM_FUNC_START_LOCAL(memcpy_orig) */ cmp %dil, %sil jl .Lcopy_backward - subq $0x20, %rdx + subq $0x20, %rcx .Lcopy_forward_loop: - subq $0x20, %rdx + subq $0x20, %rcx /* * Move in blocks of 4x8 bytes:
@@ -75,23 +80,23 @@ SYM_FUNC_START_LOCAL(memcpy_orig) movq %r11, 3*8(%rdi) leaq 4*8(%rdi), %rdi jae .Lcopy_forward_loop - addl $0x20, %edx + addl $0x20, %ecx jmp .Lhandle_tail .Lcopy_backward: /* * Calculate copy position to tail. */ - addq %rdx, %rsi - addq %rdx, %rdi - subq $0x20, %rdx + addq %rcx, %rsi + addq %rcx, %rdi + subq $0x20, %rcx /* * At most 3 ALU operations in one cycle, * so append NOPS in the same 16 bytes trunk. */ .p2align 4 .Lcopy_backward_loop: - subq $0x20, %rdx + subq $0x20, %rcx movq -1*8(%rsi), %r8 movq -2*8(%rsi), %r9 movq -3*8(%rsi), %r10
@@ -107,11 +112,11 @@ SYM_FUNC_START_LOCAL(memcpy_orig) /* * Calculate copy position to head. */ - addl $0x20, %edx - subq %rdx, %rsi - subq %rdx, %rdi + addl $0x20, %ecx + subq %rcx, %rsi + subq %rcx, %rdi .Lhandle_tail: - cmpl $16, %edx + cmpl $16, %ecx jb .Lless_16bytes /*
@@ -119,55 +124,63 @@ SYM_FUNC_START_LOCAL(memcpy_orig) */ movq 0*8(%rsi), %r8 movq 1*8(%rsi), %r9 - movq -2*8(%rsi, %rdx), %r10 - movq -1*8(%rsi, %rdx), %r11 + movq -2*8(%rsi, %rcx), %r10 + movq -1*8(%rsi, %rcx), %r11 movq %r8, 0*8(%rdi) movq %r9, 1*8(%rdi) - movq %r10, -2*8(%rdi, %rdx) - movq %r11, -1*8(%rdi, %rdx) + movq %r10, -2*8(%rdi, %rcx) + movq %r11, -1*8(%rdi, %rcx) RET .p2align 4 .Lless_16bytes: - cmpl $8, %edx + cmpl $8, %ecx jb .Lless_8bytes /* * Move data from 8 bytes to 15 bytes. */ movq 0*8(%rsi), %r8 - movq -1*8(%rsi, %rdx), %r9 + movq -1*8(%rsi, %rcx), %r9 movq %r8, 0*8(%rdi) - movq %r9, -1*8(%rdi, %rdx) + movq %r9, -1*8(%rdi, %rcx) RET .p2align 4 .Lless_8bytes: - cmpl $4, %edx + cmpl $4, %ecx jb .Lless_3bytes /* * Move data from 4 bytes to 7 bytes. */ - movl (%rsi), %ecx - movl -4(%rsi, %rdx), %r8d - movl %ecx, (%rdi) - movl %r8d, -4(%rdi, %rdx) + movl (%rsi), %edx + movl -4(%rsi, %rcx), %r8d + movl %edx, (%rdi) + movl %r8d, -4(%rdi, %rcx) RET .p2align 4 .Lless_3bytes: - subl $1, %edx + subl $1, %ecx jb .Lend /* * Move data from 1 bytes to 3 bytes. */ - movzbl (%rsi), %ecx + movzbl (%rsi), %edx jz .Lstore_1byte movzbq 1(%rsi), %r8 - movzbq (%rsi, %rdx), %r9 + movzbq (%rsi, %rcx), %r9 movb %r8b, 1(%rdi) - movb %r9b, (%rdi, %rdx) + movb %r9b, (%rdi, %rcx) .Lstore_1byte: - movb %cl, (%rdi) + movb %dl, (%rdi) .Lend: RET -SYM_FUNC_END(memcpy_orig) +.Lerms: + rep movsb + RET +SYM_FUNC_END(__memcpy) +EXPORT_SYMBOL(__memcpy) + +SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy) +SYM_PIC_ALIAS(memcpy) +EXPORT_SYMBOL(memcpy)
Signature · 1 line
2.53.0-Meta
Quoted 25 lines · On Wed, 4 Nov 2026 14:28:06 -0800 Usama Arif <[email protected]> wrote:
Since commit
68674f94ffc9("x86: don't useREP_GOODor ERMS for small memory copies"),memcpy()uses REP MOVSB only on FSRM CPUs, so ERMS CPUs without FSRM, such as Skylake through Cooper Lake, copy every size with the scalar loop. Large copies matter there: in Meta's x86 fleet, about a third of the time inmemcpy_orig()is btrfs zstd decompression copying pages into and out of zstd's buffers.Use REP MOVSB from 2048 bytes on ERMS CPUs. On a 1.4 GHz Cooper Lake, an aligned, cache-hot 4 KiB
memcpy()then takes 66.8% less time (123.4 ns instead of 371.7 ns), and btrfs zstd reads take 4% less time. Smaller copies keep the scalar loop: using REP MOVSB from 32 bytes made 32/64/128/256-byte copies 114.9%/54.4%/66.1%/23.2% slower.To keep the dispatch cheap, fold
memcpy_orig()into__memcpy(). With FSRM, "rep movsb; RET" replaces the 32-byte length check. With ERMS, a shift of the length in RDX selects REP MOVSB; RCX already holds the count. Copies below 32 bytes skip the check, which is a single NOP without ERMS.The FSRM replacement now contains a RET.
apply_returns()runs beforeapply_alternatives(), so with the ITS return thunk it decides whether to keep the thunk from the RET's address in .altinstr_replacement. Align the replacement to a cache line so that the RET keeps it; the thunk is safe wherever the replacement is copied.
Quoted 5 lines
Signed-off-by Usama Arif <[email protected]>---
v1 -> v2: https://lore.kernel.org/r/[email protected]
- Raise the threshold from 512 to 2048 bytes. REP MOVSB was slower than the scalar loop for some misaligned copies of 512 to 640 bytes.
Which cpu type was that?
Do I remember the older zen cpu falling back to byte copies? I wonder if it is possibly to do a run-time patch of the constant. In all the tests I've done on Intel cpu (going back to sandy bridge) the only alignment that made any difference was 64 byte (32 on older cpu) alignment of the destination buffer.
That could be 'for further study'.
There are some very bad alignment pairs on some cpu.
On zen5 if (IIRC) (src - dest) % PAGE_SIZE is between 1 and 127 then the both setup and per-byte cost of REP MOSVB are significantly higher (the byte alignment makes minimal difference throughout).
I don't remember finding anything similar on the Intel cpu I tested. But it is probably not worth checking for them because the cost of the comparison will be larger overall.
David
Quoted 205 lines
- Fold
memcpy_orig()into__memcpy(). With FSRM, "rep movsb; RET" now replaces the 32-byte length check (David Laight, Linus Torvalds). In v1, the 13-byte empty FSRM replacement became a jump that FSRM CPUs took on every call (Kiryl Shutsemau).
- Do the ERMS check after the 32-byte check, so copies below 32 bytes skip it (Kiryl Shutsemau), and keep the count in RCX so REP MOVSB needs no reload (David Laight).
- Align the FSRM replacement so that its RET keeps the ITS return thunk.
- Fix the comment that called the return value the source input.--- arch/x86/lib/memcpy_64.S | 101 ++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 44 deletions(-)arch/x86/lib/memcpy_64.S +57
-44@@ -27,28 +27,33 @@ * * The 'rep movsb' itself is small enough to replace the call, but the * two register moves blow up the code. And one of them is "needed" - * only for the return value that is the same as the source input, + * only for the return value that is the same as the destination input, * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb - RET -SYM_FUNC_END(__memcpy) -EXPORT_SYMBOL(__memcpy) -SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy) -SYM_PIC_ALIAS(memcpy) -EXPORT_SYMBOL(memcpy) - -SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax +#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRM - cmpq $0x20, %rdx - jb .Lhandle_tail + /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMS /* * We check whether memory false dependence could occur,@@ -56,9 +61,9 @@ SYM_FUNC_START_LOCAL(memcpy_orig) */ cmp %dil, %sil jl .Lcopy_backward - subq $0x20, %rdx + subq $0x20, %rcx .Lcopy_forward_loop: - subq $0x20, %rdx + subq $0x20, %rcx /* * Move in blocks of 4x8 bytes:@@ -75,23 +80,23 @@ SYM_FUNC_START_LOCAL(memcpy_orig) movq %r11, 3*8(%rdi) leaq 4*8(%rdi), %rdi jae .Lcopy_forward_loop - addl $0x20, %edx + addl $0x20, %ecx jmp .Lhandle_tail .Lcopy_backward: /* * Calculate copy position to tail. */ - addq %rdx, %rsi - addq %rdx, %rdi - subq $0x20, %rdx + addq %rcx, %rsi + addq %rcx, %rdi + subq $0x20, %rcx /* * At most 3 ALU operations in one cycle, * so append NOPS in the same 16 bytes trunk. */ .p2align 4 .Lcopy_backward_loop: - subq $0x20, %rdx + subq $0x20, %rcx movq -1*8(%rsi), %r8 movq -2*8(%rsi), %r9 movq -3*8(%rsi), %r10@@ -107,11 +112,11 @@ SYM_FUNC_START_LOCAL(memcpy_orig) /* * Calculate copy position to head. */ - addl $0x20, %edx - subq %rdx, %rsi - subq %rdx, %rdi + addl $0x20, %ecx + subq %rcx, %rsi + subq %rcx, %rdi .Lhandle_tail: - cmpl $16, %edx + cmpl $16, %ecx jb .Lless_16bytes /*@@ -119,55 +124,63 @@ SYM_FUNC_START_LOCAL(memcpy_orig) */ movq 0*8(%rsi), %r8 movq 1*8(%rsi), %r9 - movq -2*8(%rsi, %rdx), %r10 - movq -1*8(%rsi, %rdx), %r11 + movq -2*8(%rsi, %rcx), %r10 + movq -1*8(%rsi, %rcx), %r11 movq %r8, 0*8(%rdi) movq %r9, 1*8(%rdi) - movq %r10, -2*8(%rdi, %rdx) - movq %r11, -1*8(%rdi, %rdx) + movq %r10, -2*8(%rdi, %rcx) + movq %r11, -1*8(%rdi, %rcx) RET .p2align 4 .Lless_16bytes: - cmpl $8, %edx + cmpl $8, %ecx jb .Lless_8bytes /* * Move data from 8 bytes to 15 bytes. */ movq 0*8(%rsi), %r8 - movq -1*8(%rsi, %rdx), %r9 + movq -1*8(%rsi, %rcx), %r9 movq %r8, 0*8(%rdi) - movq %r9, -1*8(%rdi, %rdx) + movq %r9, -1*8(%rdi, %rcx) RET .p2align 4 .Lless_8bytes: - cmpl $4, %edx + cmpl $4, %ecx jb .Lless_3bytes /* * Move data from 4 bytes to 7 bytes. */ - movl (%rsi), %ecx - movl -4(%rsi, %rdx), %r8d - movl %ecx, (%rdi) - movl %r8d, -4(%rdi, %rdx) + movl (%rsi), %edx + movl -4(%rsi, %rcx), %r8d + movl %edx, (%rdi) + movl %r8d, -4(%rdi, %rcx) RET .p2align 4 .Lless_3bytes: - subl $1, %edx + subl $1, %ecx jb .Lend /* * Move data from 1 bytes to 3 bytes. */ - movzbl (%rsi), %ecx + movzbl (%rsi), %edx jz .Lstore_1byte movzbq 1(%rsi), %r8 - movzbq (%rsi, %rdx), %r9 + movzbq (%rsi, %rcx), %r9 movb %r8b, 1(%rdi) - movb %r9b, (%rdi, %rdx) + movb %r9b, (%rdi, %rcx) .Lstore_1byte: - movb %cl, (%rdi) + movb %dl, (%rdi) .Lend: RET -SYM_FUNC_END(memcpy_orig) +.Lerms: + rep movsb + RET +SYM_FUNC_END(__memcpy) +EXPORT_SYMBOL(__memcpy) + +SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy) +SYM_PIC_ALIAS(memcpy) +EXPORT_SYMBOL(memcpy)
Quoted 28 lines · David Laight
Quoted 25 lines · On Wed, 4 Nov 2026 14:28:06 -0800 Usama Arif <[email protected]> wrote:
Since commit
68674f94ffc9("x86: don't useREP_GOODor ERMS for small memory copies"),memcpy()uses REP MOVSB only on FSRM CPUs, so ERMS CPUs without FSRM, such as Skylake through Cooper Lake, copy every size with the scalar loop. Large copies matter there: in Meta's x86 fleet, about a third of the time inmemcpy_orig()is btrfs zstd decompression copying pages into and out of zstd's buffers.Use REP MOVSB from 2048 bytes on ERMS CPUs. On a 1.4 GHz Cooper Lake, an aligned, cache-hot 4 KiB
memcpy()then takes 66.8% less time (123.4 ns instead of 371.7 ns), and btrfs zstd reads take 4% less time. Smaller copies keep the scalar loop: using REP MOVSB from 32 bytes made 32/64/128/256-byte copies 114.9%/54.4%/66.1%/23.2% slower.To keep the dispatch cheap, fold
memcpy_orig()into__memcpy(). With FSRM, "rep movsb; RET" replaces the 32-byte length check. With ERMS, a shift of the length in RDX selects REP MOVSB; RCX already holds the count. Copies below 32 bytes skip the check, which is a single NOP without ERMS.The FSRM replacement now contains a RET.
apply_returns()runs beforeapply_alternatives(), so with the ITS return thunk it decides whether to keep the thunk from the RET's address in .altinstr_replacement. Align the replacement to a cache line so that the RET keeps it; the thunk is safe wherever the replacement is copied.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Usama Arif <[email protected]>---
v1 -> v2: https://lore.kernel.org/r/[email protected]
- Raise the threshold from 512 to 2048 bytes. REP MOVSB was slower than the scalar loop for some misaligned copies of 512 to 640 bytes.Which cpu type was that?
Cooper Lake (Xeon Platinum 8321HC).
Quoted 1 line
Do I remember the older zen cpu falling back to byte copies?
I just checked, and Zen 1 and Zen 2 have neither ERMS nor FSRM, so they keep the scalar loop. Zen 3 onwards has both, and FSRM already uses REP MOVSB for every size.
Quoted 1 line
I wonder if it is possibly to do a run-time patch of the constant.
It might be possible. But I wanted to keep the patch usecase driven. The current usecase is coming from zstd/btrfs. If we find a usecase where it might be faster for smaller copies, it might become worth looking into then?
Quoted 4 lines
In all the tests I've done on Intel cpu (going back to sandy bridge) the only alignment that made any difference was 64 byte (32 on older cpu) alignment of the destination buffer.
Also I should have been clearer in the changelog, by "misaligned copies of 512 to 640 bytes", I had tested a 513-byte copy with the source at page offset 1 and destination at 63, it took on average 62 ns with REP MOVSB versus 49 ns with scalar. I had done a couple of other tests at 640 bytes as well.
Quoted 6 lines
That could be 'for further study'.
There are some very bad alignment pairs on some cpu.
On zen5 if (IIRC) (src - dest) %PAGE_SIZEis between 1 and 127 then the both setup and per-byte cost of REP MOSVB are significantly higher (the byte alignment makes minimal difference throughout).
I am seeing similar on Cooper Lake.
Quoted 4 lines
I don't remember finding anything similar on the Intel cpu I tested. But it is probably not worth checking for them because the cost of the comparison will be larger overall.
Yes, I agree.
Do you think this version of the patch seems acceptable?
I feel like we should only investigate for lower size if we can find a usecase.
Quoted 208 lines
David
Quoted 205 lines
- Fold
memcpy_orig()into__memcpy(). With FSRM, "rep movsb; RET" now replaces the 32-byte length check (David Laight, Linus Torvalds). In v1, the 13-byte empty FSRM replacement became a jump that FSRM CPUs took on every call (Kiryl Shutsemau).
- Do the ERMS check after the 32-byte check, so copies below 32 bytes skip it (Kiryl Shutsemau), and keep the count in RCX so REP MOVSB needs no reload (David Laight).
- Align the FSRM replacement so that its RET keeps the ITS return thunk.
- Fix the comment that called the return value the source input.--- arch/x86/lib/memcpy_64.S | 101 ++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 44 deletions(-)arch/x86/lib/memcpy_64.S +57
-44@@ -27,28 +27,33 @@ * * The 'rep movsb' itself is small enough to replace the call, but the * two register moves blow up the code. And one of them is "needed" - * only for the return value that is the same as the source input, + * only for the return value that is the same as the destination input, * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb - RET -SYM_FUNC_END(__memcpy) -EXPORT_SYMBOL(__memcpy) -SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy) -SYM_PIC_ALIAS(memcpy) -EXPORT_SYMBOL(memcpy) - -SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax +#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRM - cmpq $0x20, %rdx - jb .Lhandle_tail + /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMS /* * We check whether memory false dependence could occur,@@ -56,9 +61,9 @@ SYM_FUNC_START_LOCAL(memcpy_orig) */ cmp %dil, %sil jl .Lcopy_backward - subq $0x20, %rdx + subq $0x20, %rcx .Lcopy_forward_loop: - subq $0x20, %rdx + subq $0x20, %rcx /* * Move in blocks of 4x8 bytes:@@ -75,23 +80,23 @@ SYM_FUNC_START_LOCAL(memcpy_orig) movq %r11, 3*8(%rdi) leaq 4*8(%rdi), %rdi jae .Lcopy_forward_loop - addl $0x20, %edx + addl $0x20, %ecx jmp .Lhandle_tail .Lcopy_backward: /* * Calculate copy position to tail. */ - addq %rdx, %rsi - addq %rdx, %rdi - subq $0x20, %rdx + addq %rcx, %rsi + addq %rcx, %rdi + subq $0x20, %rcx /* * At most 3 ALU operations in one cycle, * so append NOPS in the same 16 bytes trunk. */ .p2align 4 .Lcopy_backward_loop: - subq $0x20, %rdx + subq $0x20, %rcx movq -1*8(%rsi), %r8 movq -2*8(%rsi), %r9 movq -3*8(%rsi), %r10@@ -107,11 +112,11 @@ SYM_FUNC_START_LOCAL(memcpy_orig) /* * Calculate copy position to head. */ - addl $0x20, %edx - subq %rdx, %rsi - subq %rdx, %rdi + addl $0x20, %ecx + subq %rcx, %rsi + subq %rcx, %rdi .Lhandle_tail: - cmpl $16, %edx + cmpl $16, %ecx jb .Lless_16bytes /*@@ -119,55 +124,63 @@ SYM_FUNC_START_LOCAL(memcpy_orig) */ movq 0*8(%rsi), %r8 movq 1*8(%rsi), %r9 - movq -2*8(%rsi, %rdx), %r10 - movq -1*8(%rsi, %rdx), %r11 + movq -2*8(%rsi, %rcx), %r10 + movq -1*8(%rsi, %rcx), %r11 movq %r8, 0*8(%rdi) movq %r9, 1*8(%rdi) - movq %r10, -2*8(%rdi, %rdx) - movq %r11, -1*8(%rdi, %rdx) + movq %r10, -2*8(%rdi, %rcx) + movq %r11, -1*8(%rdi, %rcx) RET .p2align 4 .Lless_16bytes: - cmpl $8, %edx + cmpl $8, %ecx jb .Lless_8bytes /* * Move data from 8 bytes to 15 bytes. */ movq 0*8(%rsi), %r8 - movq -1*8(%rsi, %rdx), %r9 + movq -1*8(%rsi, %rcx), %r9 movq %r8, 0*8(%rdi) - movq %r9, -1*8(%rdi, %rdx) + movq %r9, -1*8(%rdi, %rcx) RET .p2align 4 .Lless_8bytes: - cmpl $4, %edx + cmpl $4, %ecx jb .Lless_3bytes /* * Move data from 4 bytes to 7 bytes. */ - movl (%rsi), %ecx - movl -4(%rsi, %rdx), %r8d - movl %ecx, (%rdi) - movl %r8d, -4(%rdi, %rdx) + movl (%rsi), %edx + movl -4(%rsi, %rcx), %r8d + movl %edx, (%rdi) + movl %r8d, -4(%rdi, %rcx) RET .p2align 4 .Lless_3bytes: - subl $1, %edx + subl $1, %ecx jb .Lend /* * Move data from 1 bytes to 3 bytes. */ - movzbl (%rsi), %ecx + movzbl (%rsi), %edx jz .Lstore_1byte movzbq 1(%rsi), %r8 - movzbq (%rsi, %rdx), %r9 + movzbq (%rsi, %rcx), %r9 movb %r8b, 1(%rdi) - movb %r9b, (%rdi, %rdx) + movb %r9b, (%rdi, %rcx) .Lstore_1byte: - movb %cl, (%rdi) + movb %dl, (%rdi) .Lend: RET -SYM_FUNC_END(memcpy_orig) +.Lerms: + rep movsb + RET +SYM_FUNC_END(__memcpy) +EXPORT_SYMBOL(__memcpy) + +SYM_FUNC_ALIAS_MEMFUNC(memcpy, __memcpy) +SYM_PIC_ALIAS(memcpy) +EXPORT_SYMBOL(memcpy)
Review
Quoted 13 lines · Usama Arif
(quoted diff) +13
-0+#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRM
Does this mean any RET in a replacement needs the same alignment? Would anything catch it if the next one doesn't have it?
Linus' layout keeps the RET out of the replacement, with no padding:
movq %rdi, %rax
movq %rdx, %rcx
ALTERNATIVE "jmp memcpy_orig", "rep movsb", X86_FEATURE_FSRM RET
Non-FSRM takes the same jump it takes today. Does that avoid the ITS problem? Why not this layout?
Review
Quoted 6 lines
(quoted diff) +6
-0+ /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMS
The RCX/RDX swap is most of the diff, and all it saves is one MOV on copies of 2K and up.
What about something like this? Untested.
arch/x86/lib/memcpy_64.S +8 -5
@@ -31,11 +31,9 @@ * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb + ALTERNATIVE "jmp memcpy_orig", "rep movsb", X86_FEATURE_FSRM RET SYM_FUNC_END(__memcpy) EXPORT_SYMBOL(__memcpy)
@@ -45,11 +43,11 @@ SYM_PIC_ALIAS(memcpy) EXPORT_SYMBOL(memcpy) SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax - cmpq $0x20, %rdx jb .Lhandle_tail + ALTERNATIVE "", __stringify(shrq $11, %rcx; jnz .Lerms), X86_FEATURE_ERMS + /* * We check whether memory false dependence could occur, * then jump to corresponding copy mode.
@@ -169,5 +167,10 @@ SYM_FUNC_START_LOCAL(memcpy_orig) .Lend: RET + +.Lerms: + movq %rdx, %rcx + rep movsb + RET SYM_FUNC_END(memcpy_orig)
If you keep the swap, please make it a separate patch with no functional change.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 17 lines · On Tue, 10 Nov 2026 16:25:54 +0000 Kiryl Shutsemau <[email protected]> wrote:
Review
Quoted 13 lines · Usama Arif
(quoted diff) +13
-0+#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMDoes this mean any RET in a replacement needs the same alignment? Would anything catch it if the next one doesn't have it?
My suspicion is that ideally 'yes'.
But someone said (in this thread) that it is worse for RET in the first half a cache line - so aligning the replacement that way is better.)
Quoted 9 lines
Linus' layout keeps the RET out of the replacement, with no padding:
movq %rdi, %rax
movq %rdx, %rcx
ALTERNATIVE "jmpmemcpy_orig", "rep movsb",X86_FEATURE_FSRMRETNon-FSRM takes the same jump it takes today. Does that avoid the ITS problem? Why not this layout?
It adds a jump to the non-FSRM path.
Cold cache that is likely to be measurable.
(But not if RET ends up being anything other than 0xc3 - that will just kill everything.)
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +6
-0+ /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMSThe RCX/RDX swap is most of the diff, and all it saves is one MOV on copies of 2K and up.
Register-register moves don't cost much (they can even be done my the register renaming logic rather than the alu - but still have to be fetched and decoded), but needing them is a bit of 'lack of attention detail' when a simple backtrack can get the value where it is needed. gcc is very bad at it, a compiler really ought to do a lot better.
David
Quoted 47 lines
What about something like this? Untested.
arch/x86/lib/memcpy_64.S +8
-5@@ -31,11 +31,9 @@ * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb + ALTERNATIVE "jmp memcpy_orig", "rep movsb", X86_FEATURE_FSRM RET SYM_FUNC_END(__memcpy) EXPORT_SYMBOL(__memcpy)@@ -45,11 +43,11 @@ SYM_PIC_ALIAS(memcpy) EXPORT_SYMBOL(memcpy) SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax - cmpq $0x20, %rdx jb .Lhandle_tail + ALTERNATIVE "", __stringify(shrq $11, %rcx; jnz .Lerms), X86_FEATURE_ERMS + /* * We check whether memory false dependence could occur, * then jump to corresponding copy mode.@@ -169,5 +167,10 @@ SYM_FUNC_START_LOCAL(memcpy_orig) .Lend: RET + +.Lerms: + movq %rdx, %rcx + rep movsb + RET SYM_FUNC_END(memcpy_orig)If you keep the swap, please make it a separate patch with no functional change.
Quoted 21 lines · On Tue, 10 Nov 2026 17:06:55 +0000 David Laight <[email protected]>…
Quoted 17 lines · On Tue, 10 Nov 2026 16:25:54 +0000 Kiryl Shutsemau <[email protected]> wrote:
Review
Quoted 13 lines · Usama Arif
(quoted diff) +13
-0+#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMDoes this mean any RET in a replacement needs the same alignment? Would anything catch it if the next one doesn't have it?
Quoted 3 lines
My suspicion is that ideally 'yes'.
But someone said (in this thread) that it is worse for RET in the first half a cache line - so aligning the replacement that way is better.)
Yes, ITS requires mitigation for a return in the first half of a cache line. Aligning the old replacement there kept its thunk when copied. The next version will keep RET in ordinary text, so ITS uses its execution address and the replacement alignment is unnecessary.
Quoted 8 lines
Quoted 8 lines
Linus' layout keeps the RET out of the replacement, with no padding:
movq %rdi, %rax
movq %rdx, %rcx
ALTERNATIVE "jmpmemcpy_orig", "rep movsb",X86_FEATURE_FSRMRET
Quoted 5 lines
Quoted 2 lines
Non-FSRM takes the same jump it takes today. Does that avoid the ITS problem? Why not this layout?
It adds a jump to the non-FSRM path.
Cold cache that is likely to be measurable.
The upstream code already jumps to memcpy_orig(); I have the next version retaining that jump. Preparing RCX at the public entry adds a MOV on the scalar path. The return stays in ordinary text so ITS uses its execution address. I will post the next version today so that its easier to have a discussion
Quoted 71 lines
(But not if RET ends up being anything other than
0xc3- that will just kill everything.)Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +6
-0+ /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMSThe RCX/RDX swap is most of the diff, and all it saves is one MOV on copies of 2K and up.
Register-register moves don't cost much (they can even be done my the register renaming logic rather than the alu - but still have to be fetched and decoded), but needing them is a bit of 'lack of attention detail' when a simple backtrack can get the value where it is needed. gcc is very bad at it, a compiler really ought to do a lot better.
David
Quoted 47 lines
What about something like this? Untested.
arch/x86/lib/memcpy_64.S +8
-5@@ -31,11 +31,9 @@ * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb + ALTERNATIVE "jmp memcpy_orig", "rep movsb", X86_FEATURE_FSRM RET SYM_FUNC_END(__memcpy) EXPORT_SYMBOL(__memcpy)@@ -45,11 +43,11 @@ SYM_PIC_ALIAS(memcpy) EXPORT_SYMBOL(memcpy) SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax - cmpq $0x20, %rdx jb .Lhandle_tail + ALTERNATIVE "", __stringify(shrq $11, %rcx; jnz .Lerms), X86_FEATURE_ERMS + /* * We check whether memory false dependence could occur, * then jump to corresponding copy mode.@@ -169,5 +167,10 @@ SYM_FUNC_START_LOCAL(memcpy_orig) .Lend: RET + +.Lerms: + movq %rdx, %rcx + rep movsb + RET SYM_FUNC_END(memcpy_orig)If you keep the swap, please make it a separate patch with no functional change.
Quoted 17 lines · On Tue, 10 Nov 2026 16:25:54 +0000 Kiryl Shutsemau <[email protected]> wrote:
Review
Quoted 13 lines · Usama Arif
(quoted diff) +13
-0+#ifdef CONFIG_MITIGATION_ITS + /* + * Returns are patched before alternatives. Align the replacement + * source so its return is in the first half of a cache line and ITS + * retains its thunk. The copied return can then execute in either + * half of a cache line. + */ + .pushsection .altinstr_replacement, "ax" + .balign 64 + .popsection +#endif + ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMDoes this mean any RET in a replacement needs the same alignment? Would anything catch it if the next one doesn't have it?
The issue is specific to address-dependent return handling, such as ITS. apply_returns() runs before alternatives, so its decision uses the replacement source address. Copying an unthunked return from the safe half to the vulnerable half can invalidate that decision. The patch had no check of the copied return's final ITS placement.
I have kept RET in ordinary text, so the decision now uses the address where it executes. There is no return in either replacement and no replacement alignment to maintain.
Quoted 10 lines
Linus' layout keeps the RET out of the replacement, with no padding:
movq %rdi, %rax
movq %rdx, %rcx
ALTERNATIVE "jmpmemcpy_orig", "rep movsb",X86_FEATURE_FSRMRETNon-FSRM takes the same jump it takes today. Does that avoid the ITS problem? Why not this layout?
Yes. I have adopted that layout and kept memcpy_orig() separate. The entry prepares RAX and RCX; the scalar function no longer repeats that preparation. FSRM replaces only the jump with REP MOVSB, leaving RET in ordinary text. This resolves the ITS source-address issue.
Quoted 10 lines
Review
Quoted 7 lines
(quoted diff) +6
-0+ /* + * RCX holds the count for REP MOVSB and scalar copying; RDX is scratch. + * Shift RDX to check length >= 2048 without destroying the REP count. + * The 10-byte alternative fits a NOP without ERMS. + */ + ALTERNATIVE "", __stringify(shrq $11, %rdx; jnz .Lerms), X86_FEATURE_ERMSThe RCX/RDX swap is most of the diff, and all it saves is one MOV on copies of 2K and up.
Agreed that the register change obscured the functional change. I have split it into a preparatory patch. It consistently uses RCX for scalar counts and indexes, and RDX for small-copy data. The subsequent ERMS patch can shift RDX while preserving RCX for REP MOVSB.
Quoted 2 lines
What about something like this? Untested.
The entry layout is now the one you suggested. I retained the RCX count in a separate patch, so the ERMS arm needs no count reload. The gate remains after the 32-byte check and keeps the 2048 threshold.
Quoted 42 lines
arch/x86/lib/memcpy_64.S +8
-5@@ -31,11 +31,9 @@ * which the compiler could/should do much better anyway. */ SYM_TYPED_FUNC_START(__memcpy) - ALTERNATIVE "jmp memcpy_orig", "", X86_FEATURE_FSRM - movq %rdi, %rax movq %rdx, %rcx - rep movsb + ALTERNATIVE "jmp memcpy_orig", "rep movsb", X86_FEATURE_FSRM RET SYM_FUNC_END(__memcpy) EXPORT_SYMBOL(__memcpy)@@ -45,11 +43,11 @@ SYM_PIC_ALIAS(memcpy) EXPORT_SYMBOL(memcpy) SYM_FUNC_START_LOCAL(memcpy_orig) - movq %rdi, %rax - cmpq $0x20, %rdx jb .Lhandle_tail + ALTERNATIVE "", __stringify(shrq $11, %rcx; jnz .Lerms), X86_FEATURE_ERMS + /* * We check whether memory false dependence could occur, * then jump to corresponding copy mode.@@ -169,5 +167,10 @@ SYM_FUNC_START_LOCAL(memcpy_orig) .Lend: RET + +.Lerms: + movq %rdx, %rcx + rep movsb + RET SYM_FUNC_END(memcpy_orig)
Quoted 2 lines
If you keep the swap, please make it a separate patch with no functional change.
Done. The first patch changes the scalar register allocation and initializes RCX in memcpy_orig(), with no copy-policy change. The second patch hoists that preparation and adds the ERMS path and FSRM layout.
Quoted 4 lines
Signature · 2 lines
-- Kiryl Shutsemau / Kirill A. Shutemov
arch/x86/lib/memcpy_64.S line 49
Quoted 3 lines · Usama Arif
arch/x86/lib/memcpy_64.S +2
-0+ ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRM
Let's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Quoted 8 lines · On Tue, 10 Nov 2026 09:53:07 -0800 Linus Torvalds…
arch/x86/lib/memcpy_64.S line 49
Quoted 3 lines · Usama Arif
arch/x86/lib/memcpy_64.S +2
-0+ ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMLet's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
You'd definitely want the RET in the code itself, and then overwrite it with something else.
When I suggested it I did wonder whether the code patching really had the right functionality.
Someone should try to measure it [1], but I actually suspect that the only cost in following a 'rep movs' with an unconditional 'jmp' would be for an I-cache load (or, worse, a TLB miss).
I expect the pipeline front end (which handles the jmp) will do all its work while the setup for rep movs is happening (even for FSRM).
That would mean you could pair the "cmpq $0x20, %rcx; jb .Lhandle_tail" with "rep movsb; jmps do_RET; nop".
David
[1] I need to dig out my 'rep movs' performance measuring code and run it on a few different cpu types.
But I not got anything newer than an i7-7 or a zen[1-4].
Quoted 2 lines
Linus
H. Peter Anvin
reviewerQuoted 10 lines · Linus Torvalds
arch/x86/lib/memcpy_64.S line 49
Quoted 3 lines · Usama Arif
arch/x86/lib/memcpy_64.S +2
-0+ ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMLet's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
For C (or Rust, or C++, ...) it is obviously bad chicken.
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Linus Torvalds
Let's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
It's assembly, but it's assembly that potentially gets rewritten. objtool already knows about __x86_return_thunk being special (although I think it's the _target_, not the call site, it cares about).
I would like to not have magic in stuff that is in the alternatives section. We've had issues with relocations etc before.
That said, grepping a bit for this, it turns out we already seem to be doing that in memmove_64.S, although it was hidden from my first grep:
#define CHECK_LEN cmp $0x20, %rdx; jb 1f
#define MEMMOVE_BYTES movq %rdx, %rcx; rep movsb; RET
.Lmemmove_begin_forward:
ALTERNATIVE_2 __stringify(CHECK_LEN), \
__stringify(CHECK_LEN; MEMMOVE_BYTES), X86_FEATURE_ERMS, \
__stringify(MEMMOVE_BYTES), X86_FEATURE_FSRMwhich is pretty disgusting. But I guess that train has passed..
Linus
Quoted 21 lines · On Tue, 10 Nov 2026 12:59:02 -0800 Linus Torvalds…
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Linus Torvalds
Let's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
It's assembly, but it's assembly that potentially gets rewritten. objtool already knows about
__x86_return_thunkbeing special (although I think it's the _target_, not the call site, it cares about).I would like to not have magic in stuff that is in the alternatives section. We've had issues with relocations etc before.
That said, grepping a bit for this, it turns out we already seem to be doing that in
memmove_64.S, although it was hidden from my first grep:
Quoted 8 lines
#define CHECK_LEN cmp $0x20, %rdx; jb 1f #define MEMMOVE_BYTES movq %rdx, %rcx; rep movsb; RET .Lmemmove_begin_forward: ALTERNATIVE_2 __stringify(CHECK_LEN), \ __stringify(CHECK_LEN; MEMMOVE_BYTES), X86_FEATURE_ERMS, \ __stringify(MEMMOVE_BYTES), X86_FEATURE_FSRMwhich is pretty disgusting. But I guess that train has passed..
Or no one has realised it ran straight through the buffers :-)
RET is likely to be a 5 byte jmp instruction.
One of the checks for letting it be replaced by 0xc3,(0xcc)*4 is (addr & 0x20). That will be wrong for the alternatives (half the time) so a quarter of the time the train will go straight through the buffers.
David
Quoted 2 lines
Linus
Borislav Petkov
maintainerQuoted 24 lines · Linus Torvalds
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Linus Torvalds
Let's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
It's assembly, but it's assembly that potentially gets rewritten. objtool already knows about
__x86_return_thunkbeing special (although I think it's the _target_, not the call site, it cares about).I would like to not have magic in stuff that is in the alternatives section. We've had issues with relocations etc before.
That said, grepping a bit for this, it turns out we already seem to be doing that in
memmove_64.S, although it was hidden from my first grep:#define
CHECK_LENcmp $0x20, %rdx; jb 1f
#defineMEMMOVE_BYTESmovq %rdx, %rcx; rep movsb; RET .Lmemmove_begin_forward:
Quoted 7 lines
ALTERNATIVE_2 __stringify(CHECK_LEN), \ __stringify(CHECK_LEN; MEMMOVE_BYTES), X86_FEATURE_ERMS, \ __stringify(MEMMOVE_BYTES), X86_FEATURE_FSRMwhich is pretty disgusting. But I guess that train has passed..
Linus
Yah, I never liked this piece of gunk. Lemme give it a fresh look when things relax a bit...
Signature · 1 line
Small device. Typos and formatting crap
Quoted 29 lines · On Wed, 11 Nov 2026 01:16:37 +0000 Borislav Petkov <[email protected]> wrote:
Quoted 28 lines · Linus Torvalds
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Linus Torvalds
Let's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
It's assembly, but it's assembly that potentially gets rewritten. objtool already knows about
__x86_return_thunkbeing special (although I think it's the _target_, not the call site, it cares about).I would like to not have magic in stuff that is in the alternatives section. We've had issues with relocations etc before.
That said, grepping a bit for this, it turns out we already seem to be doing that in
memmove_64.S, although it was hidden from my first grep:#define
CHECK_LENcmp $0x20, %rdx; jb 1f
#defineMEMMOVE_BYTESmovq %rdx, %rcx; rep movsb; RET .Lmemmove_begin_forward:ALTERNATIVE_2 __stringify(CHECK_LEN), \ __stringify(CHECK_LEN; MEMMOVE_BYTES), X86_FEATURE_ERMS, \ __stringify(MEMMOVE_BYTES), X86_FEATURE_FSRM
Quoted 5 lines
Quoted 3 lines
which is pretty disgusting. But I guess that train has passed..
Linus
Yah, I never liked this piece of gunk. Lemme give it a fresh look when things relax a bit...
I have sent v3 that doesnt put RET in an alternative [1]
Quoted 8 lines · On Tue, 10 Nov 2026 12:17:51 -0800 "H. Peter Anvin" <[email protected]> wrote:
Quoted 7 lines · Linus Torvalds
arch/x86/lib/memcpy_64.S line 49
Quoted 3 lines · Usama Arif
arch/x86/lib/memcpy_64.S +2
-0+ ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMLet's not do that.
Quoted 7 lines
Quoted 3 lines
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
For C (or Rust, or C++, ...) it is obviously bad chicken.
Because RET is a horrid sequence subject to a lot of patching to avoid multiple speculative execution issues.
If is were just the 0xc3 opcode byte it wouldn't be an issue.
David
H. Peter Anvin
reviewerQuoted 17 lines · David Laight
Quoted 14 lines · On Tue, 10 Nov 2026 12:17:51 -0800 "H. Peter Anvin" <[email protected]> wrote:
Quoted 10 lines · Linus Torvalds
arch/x86/lib/memcpy_64.S line 49
Quoted 3 lines · Usama Arif
arch/x86/lib/memcpy_64.S +2
-0+ ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMLet's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
Linus
Why is that, *given that it is in assembly*?
Quoted 7 lines
Quoted 1 line
For C (or Rust, or C++, ...) it is obviously bad chicken.
Because RET is a horrid sequence subject to a lot of patching to avoid multiple speculative execution issues.
If is were just the0xc3opcode byte it wouldn't be an issue.David
Ugh. Right.
Quoted 8 lines · On Tue, 10 Nov 2026 09:53:07 -0800 Linus Torvalds…
arch/x86/lib/memcpy_64.S line 49
Quoted 3 lines · Usama Arif
arch/x86/lib/memcpy_64.S +2
-0+ ALTERNATIVE __stringify(cmpq $0x20, %rcx; jb .Lhandle_tail), \ + __stringify(rep movsb; RET), X86_FEATURE_FSRMLet's not do that.
Putting magic things like RET in alternatives is just a bad bad idea.
I will send v3 today with RET in ordinary text.
Thanks!
Usama
Quoted 3 lines
Linus