* [PATCH] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM
@ 2026-10-13 10:04 Usama Arif
0 siblings, 0 replies; only message in thread
From: Usama Arif @ 2026-10-13 10:04 UTC (permalink / raw)
To: bp, dave.hansen, dsterba, hpa, linux-kernel, mingo, terrelln,
tglx, usama.arif, x86, torvalds, kas, hannes, riel, shakeel.butt,
kernel-team
Commit 68674f94ffc9 ("x86: don't use REP_GOOD or ERMS for small
memory copies") stopped selecting REP MOVSB from ERMS alone because it
is not suitable for short copies. Consequently, ordinary out-of-line
__memcpy() calls on CPUs with ERMS but without FSRM use memcpy_orig()
for every length. Its main loop copies 32 bytes per iteration and can be
slower than REP MOVSB for large copies, as measured below on Cooper Lake.
This matters for btrfs zstd decompression. The btrfs-tree commit
e27a6e4823f3 ("btrfs: zstd: avoid a copy in zstd_decompress_bio()")
removes btrfs' copy from a scratch buffer into the page cache by letting
zstd write there directly. It does not remove the copies inside
streaming zstd. ZSTD_decompressStream() still uses memcpy() to flush
decompressed data from its internal output buffer to the supplied page
(zdss_flush), and to stage compressed input when the next block crosses
an input folio supplied by btrfs (zdss_load). In Meta's fleet profile
of x86 kernels, these two btrfs/zstd call sites accounted for about 34%
of the sampled profile weight attributed to memcpy_orig().
On CPUs without FSRM, select REP MOVSB for copies of 512 bytes and above
when ERMS is present. Keep memcpy_orig() below 512 bytes. The later FSRM
alternative continues to select REP MOVSB for all sizes, including when
ERMS is not exposed.
Userspace replicas of the original and patched entry paths were run on
a turbo-disabled Xeon Platinum 8321HC (Cooper Lake), capped at
1.401 GHz. Results average two runs and scale the per-call time to
65,536 4 KB calls, or 256 MB in aggregate.
Here "hot" means cache-resident. The rows model zdss_flush() from
zstd's L2-resident output buffer, zdss_load() from a cold compressed
folio into zstd's hot input buffer, and a page copy into a cold
destination.
orig REP MOVSB reduction
zdss_flush: L2 -> hot page 28.0 ms 11.9 ms 57.5%
zdss_load: cold -> hot buffer 46.1 ms 28.9 ms 37.3%
page copy: hot -> cold page 29.8 ms 22.5 ms 24.5%
REP MOVSB was faster in every tested scenario at 512 bytes and above.
Copies below 512 bytes showed no consistent performance change across the
possible function alignments.
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
The above benchmarks to model copies in zstd were created using LLM:
https://gist.github.com/uarif1/8409ccd34284f128dbea5822ec628ef3
---
arch/x86/lib/memcpy_64.S | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/x86/lib/memcpy_64.S b/arch/x86/lib/memcpy_64.S
index 12a23fa7c44c5..a7b3ce4fae0b4 100644
--- a/arch/x86/lib/memcpy_64.S
+++ b/arch/x86/lib/memcpy_64.S
@@ -31,7 +31,9 @@
* which the compiler could/should do much better anyway.
*/
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
movq %rdi, %rax
movq %rdx, %rcx
--
2.53.0-Meta
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-10-13 10:04 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-13 10:04 [PATCH] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM Usama Arif
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®