Skip to contentSkip to conversation

x86/boot: Avoid redundant address overlap tests in memcpy().

PATCH In review Kuniyuki Iwashima 3 participants 4 messages Jan 23, 2022 – Jan 24, 2022 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/boot: Avoid redundant address overlap tests in memcpy().

When 'dest' and 'src' overlap, the boot time memcpy() calls memmove(), which tests the same condition again. GCC does not optimise it for now. Let's split the copy part of memmove() as ____memmove() and call it from memcpy().

---
 arch/x86/boot/compressed/string.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/string.c +11 -6
index 81fc1eaa3..4c0edb5d6 100644
@@ -50,26 +50,31 @@ void *memset(void *s, int c, size_t n)
 	return s;
 }
 
-void *memmove(void *dest, const void *src, size_t n)
+void *____memmove(void *dest, const void *src, size_t n)
 {
 	unsigned char *d = dest;
 	const unsigned char *s = src;
 
-	if (d <= s || d - s >= n)
-		return ____memcpy(dest, src, n);
-
 	while (n-- > 0)
 		d[n] = s[n];
 
 	return dest;
 }
 
-/* Detect and warn about potential overlaps, but handle them with memmove. */
+void *memmove(void *dest, const void *src, size_t n)
+{
+	if (dest <= src || dest - src >= n)
+		return ____memcpy(dest, src, n);
+
+	return ____memmove(dest, src, n);
+}
+
+/* Detect and warn about potential overlaps, but handle them with ____memmove(). */
 void *memcpy(void *dest, const void *src, size_t n)
 {
 	if (dest > src && dest - src < n) {
 		warn("Avoiding potentially unsafe overlapping memcpy()!");
-		return memmove(dest, src, n);
+		return ____memmove(dest, src, n);
 	}
 	return ____memcpy(dest, src, n);
 }
Signature · 1 line
2.30.2
#1 lore raw
Quoted 4 lines · Kuniyuki Iwashima
arch/x86/boot/compressed/string.c +1 -1
-void *memmove(void *dest, const void *src, size_t n)
+void *____memmove(void *dest, const void *src, size_t n)
  {
  	unsigned char *d = dest;

arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 62

Quoted 10 lines
arch/x86/boot/compressed/string.c +0 -3
  	const unsigned char *s = src;
 
-	if (d <= s || d - s >= n)
-		return ____memcpy(dest, src, n);
-
  	while (n-- > 0)
  		d[n] = s[n];
 
  	return dest;
  }

The ___ naming is pretty cruel. Could we call it memmove_no_overlap() or memmove_unsafe()? Surely we can put some *useful* bytes in the name rather than padding it out with _'s. No need to perpetuate the ____memcpy() naming.

Also, is this worth the churn? It probably saves less than 10 instructions, all of which are ridiculously cheap. Is there a *reason* for this other than being a pure cleanup?

#2 lore raw
From:   Dave Hansen <[email protected]>
Date:   Mon, 24 Jan 2022 09:38:40 -0800
Quoted 13 lines · Kuniyuki Iwashima
Quoted 12 lines
arch/x86/boot/compressed/string.c +1 -4
-void *memmove(void *dest, const void *src, size_t n)
+void *____memmove(void *dest, const void *src, size_t n)
  {
  	unsigned char *d = dest;
  	const unsigned char *s = src;
 
-	if (d <= s || d - s >= n)
-		return ____memcpy(dest, src, n);
-
  	while (n-- > 0)
  		d[n] = s[n];
Quoted 7 lines
Quoted 2 lines
  	return dest;
  }

The ___ naming is pretty cruel. Could we call it memmove_no_overlap() or memmove_unsafe()? Surely we can put some *useful* bytes in the name rather than padding it out with _'s. No need to perpetuate the ____memcpy() naming.

Sure. I will rename it to memmove_unsafe() because it supports another overlap case. (d > s)

Quoted 4 lines

Also, is this worth the churn? It probably saves less than 10 instructions, all of which are ridiculously cheap. Is there a *reason* for this other than being a pure cleanup?

This is just for cleanup.

maintainer
#3 lore raw
Quoted 5 lines · Kuniyuki Iwashima
Quoted 3 lines

Also, is this worth the churn? It probably saves less than 10 instructions, all of which are ridiculously cheap. Is there a *reason* for this other than being a pure cleanup?

This is just for cleanup.

Was wondering the same thing, whether this is even worth the trouble. I'm sure you can find real bugs to fix.

:-)

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette