From: David Laight <david.laight.linux@gmail.com>
To: Usama Arif <usama.arif@linux.dev>
Cc: Borislav Petkov <bp@alien8.de>,
dave.hansen@linux.intel.com, dsterba@suse.com, hpa@zytor.com,
linux-kernel@vger.kernel.org, mingo@redhat.com, terrelln@fb.com,
tglx@kernel.org, x86@kernel.org, torvalds@linux-foundation.org,
kas@kernel.org, hannes@cmpxchg.org, riel@surriel.com,
shakeel.butt@linux.dev, kernel-team@meta.com
Subject: Re: [PATCH v3 2/2] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM
Date: Fri, 13 Nov 2026 16:03:00 +0000 [thread overview]
Message-ID: <20261113160300.3f0d0a37@pumpkin> (raw)
In-Reply-To: <20261113141137.1113517-1-usama.arif@linux.dev>
On Fri, 13 Nov 2026 06:11:36 -0800
Usama Arif <usama.arif@linux.dev> wrote:
> How about something like what I have added at the end, would that be more
> acceptable in terms of maintainability?
I was thinking more of something like:
(written in the email so untested)
static always_inline void *memcpy(void *dst, const void *src, size_t len)
{
void *orig_dst = dst;
asm volatile( ".pushsection ".memcpy_calls\n"
"\t.long . - 1f\n"
".popsection\n"
"1:\trep movsb\n"
"\tnop; nop"
: "+c (len), "+D" (dst), "+S" (src)
:: ASM_CALL_CONSTRAINT);
return orig_dst;
}
An unused return value will be optimised away at all the call sites.
That will work (maybe slowly) on all cpu.
Then some code reads all the addresses from .memcpy_calls and, if necessary,
patches the 5 bytes to a call to the appropriate function.
It might be worth giving the code one or two scratch registers so that
a short copy loop doesn't need to spill to stack (perhaps %rax and %rdx).
It'll matter less for a longer copy loop.
So you might have:
_memcpy_erms:
cmp $nnnn, %rcx
jb _memcpy_words
rep movsb
RET
_memcpy_words:
cmpb $nnnn %rcx
jb _memcpy_bytes
mov %rax,%rcx
or %rcx,%rdi
or %rcx,%rsi
test $7,%rcx
jnz _memcpy_misaligned
sub $8,%rax
1: mov %rcx,[%rsi]
mov %rdx,8[%rsi]
mov [%rdi],%rcx
mov 8[%rdi],%rdx
add $16,%rdi
add $16,%rsi
sub $16,%rax
jnc 1b:
jz 2f
mov %rax,[%si]
mov [%rdi]%rax]
2: RET
That isn't the best 2-word loop by any means.
And you may not care about misaligned pointers.
etc
David
next prev parent reply other threads:[~2026-11-13 16:03 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 13:20 [PATCH v3 0/2] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM Usama Arif
2026-11-11 13:20 ` [PATCH v3 1/2] x86/lib: Keep the scalar memcpy() count in RCX Usama Arif
2026-11-11 13:20 ` [PATCH v3 2/2] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM Usama Arif
2026-11-11 13:56 ` Peter Zijlstra
2026-11-11 23:46 ` Borislav Petkov
2026-11-12 9:37 ` David Laight
2026-11-13 14:31 ` Usama Arif
2026-11-13 14:11 ` Usama Arif
2026-11-13 16:03 ` David Laight [this message]
2026-11-11 18:19 ` [PATCH v3 0/2] " Kiryl Shutsemau
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=20261113160300.3f0d0a37@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=dsterba@suse.com \
--cc=hannes@cmpxchg.org \
--cc=hpa@zytor.com \
--cc=kas@kernel.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=riel@surriel.com \
--cc=shakeel.butt@linux.dev \
--cc=terrelln@fb.com \
--cc=tglx@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=usama.arif@linux.dev \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.