public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/release/2.28/master] x86-64: Avoid rep movsb with short distance [BZ #27130]
@ 2021-01-12 15:44 H.J. Lu
  0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2021-01-12 15:44 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=56fbd0ba218c59a1cc8c29464f153d45a4c887c0

commit 56fbd0ba218c59a1cc8c29464f153d45a4c887c0
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jan 12 05:15:49 2021 -0800

    x86-64: Avoid rep movsb with short distance [BZ #27130]
    
    When copying with "rep movsb", if the distance between source and
    destination is N*4GB + [1..63] with N >= 0, performance may be very
    slow.  This patch updates memmove-vec-unaligned-erms.S for AVX and
    AVX512 versions with the distance in RCX:
    
            cmpl    $63, %ecx
            // Don't use "rep movsb" if ECX <= 63
            jbe     L(Don't use rep movsb")
            Use "rep movsb"
    
    Benchtests data with bench-memcpy, bench-memcpy-large, bench-memcpy-random
    and bench-memcpy-walk on Skylake, Ice Lake and Tiger Lake show that its
    performance impact is within noise range as "rep movsb" is only used for
    data size >= 4KB.
    
    (cherry picked from commit 3ec5d83d2a237d39e7fd6ef7a0bc8ac4c171a4a5)

Diff:
---
 NEWS                                                |  1 +
 .../x86_64/multiarch/memmove-vec-unaligned-erms.S   | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/NEWS b/NEWS
index 1884f741e6..271f033314 100644
--- a/NEWS
+++ b/NEWS
@@ -76,6 +76,7 @@ The following bugs are resolved with this release:
   [25414] 'glob' use-after-free bug (CVE-2020-1752)
   [25423] Array overflow in backtrace on powerpc
   [25933] Off by one error in __strncmp_avx2
+  [27130] "rep movsb" performance issue
 
 Security related changes:
 
diff --git a/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S b/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S
index 9bab1147d5..5aaadc233f 100644
--- a/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S
+++ b/sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S
@@ -67,6 +67,13 @@
 # define REP_MOVSB_THRESHOLD	(2048 * (VEC_SIZE / 16))
 #endif
 
+/* Avoid short distance rep movsb only with non-SSE vector.  */
+#ifndef AVOID_SHORT_DISTANCE_REP_MOVSB
+# define AVOID_SHORT_DISTANCE_REP_MOVSB (VEC_SIZE > 16)
+#else
+# define AVOID_SHORT_DISTANCE_REP_MOVSB 0
+#endif
+
 #ifndef PREFETCH
 # define PREFETCH(addr) prefetcht0 addr
 #endif
@@ -257,7 +264,21 @@ L(movsb):
 #  error Unsupported REP_MOVSB_THRESHOLD and VEC_SIZE!
 # endif
 	jb	L(more_8x_vec_backward)
+# if AVOID_SHORT_DISTANCE_REP_MOVSB
+	movq	%rdi, %rcx
+	subq	%rsi, %rcx
+	jmp	2f
+# endif
 1:
+# if AVOID_SHORT_DISTANCE_REP_MOVSB
+	movq	%rsi, %rcx
+	subq	%rdi, %rcx
+2:
+/* Avoid "rep movsb" if RCX, the distance between source and destination,
+   is N*4GB + [1..63] with N >= 0.  */
+	cmpl	$63, %ecx
+	jbe	L(more_2x_vec)	/* Avoid "rep movsb" if ECX <= 63.  */
+# endif
 	mov	%RDX_LP, %RCX_LP
 	rep movsb
 L(nop):


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-01-12 15:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-12 15:44 [glibc/release/2.28/master] x86-64: Avoid rep movsb with short distance [BZ #27130] H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).