mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Usama Arif <usama.arif@linux.dev>
Cc: 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] x86/lib: Use REP MOVSB for large memcpy() on ERMS CPUs without FSRM
Date: Sat, 17 Oct 2026 22:42:36 +0100	[thread overview]
Message-ID: <20261017224236.5af552a6@pumpkin> (raw)
In-Reply-To: <20261013100442.2658311-1-usama.arif@linux.dev>

On Tue, 13 Oct 2026 03:04:42 -0700
Usama Arif <usama.arif@linux.dev> wrote:

> 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.

But pretty much all the memcpy() in the kernel are small.
You are adding a conditional branch to all to of them.

If you have somewhere doing long copies then the code should probably
be calling a different function.

You also need to test on a variety of cpu, especially including older zen.
IIRC some of those don't behave as the cpu features might indicate.

Intel cpu going back to ivy bridge (and maybe sandy bridge) treat 'rep movsb'
and 'rep movsq' exactly the same, the only alignment that ever matter is
64 byte alignment (possibly less on ivy/sandy bridge) of the destination
buffer - the copies run twice as fast if 64 byte aligned.
After the initial overhead (which my tests didn't measure) the bytes/clock
was moderately constant across all buffer sizes.
Amd cpu are different, even zen-5 has a big performance hit if (IIRC)
the page offset of source in less than 128 bytes below that of the
destination (the byte alignment doesn't matter).

I need to redo those measurement having worked out hot to time the
instruction setup costs.

David

> 
> 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


      parent reply	other threads:[~2026-10-17 21:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 10:04 Usama Arif
2026-10-15 10:39 ` Kiryl Shutsemau
2026-10-17 19:27   ` Usama Arif
2026-10-17 21:42 ` David Laight [this message]

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=20261017224236.5af552a6@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 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®