All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy().
@ 2022-01-23  1:58 Kuniyuki Iwashima
  2022-01-24 17:38 ` Dave Hansen
  0 siblings, 1 reply; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-01-23  1:58 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen
  Cc: Benjamin Herrenschmidt, Kuniyuki Iwashima, Kuniyuki Iwashima,
	x86, linux-kernel

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

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
 arch/x86/boot/compressed/string.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/string.c
index 81fc1eaa3..4c0edb5d6 100644
--- a/arch/x86/boot/compressed/string.c
+++ b/arch/x86/boot/compressed/string.c
@@ -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);
 }
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy().
  2022-01-23  1:58 [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy() Kuniyuki Iwashima
@ 2022-01-24 17:38 ` Dave Hansen
  2022-01-24 22:14   ` Kuniyuki Iwashima
  0 siblings, 1 reply; 4+ messages in thread
From: Dave Hansen @ 2022-01-24 17:38 UTC (permalink / raw)
  To: Kuniyuki Iwashima, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen
  Cc: Benjamin Herrenschmidt, Kuniyuki Iwashima, x86, linux-kernel

On 1/22/22 17:58, Kuniyuki Iwashima wrote:
> -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;
>   }

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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy().
  2022-01-24 17:38 ` Dave Hansen
@ 2022-01-24 22:14   ` Kuniyuki Iwashima
  2022-01-24 22:48     ` Borislav Petkov
  0 siblings, 1 reply; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-01-24 22:14 UTC (permalink / raw)
  To: dave.hansen
  Cc: benh, bp, dave.hansen, kuni1840, kuniyu, linux-kernel, mingo, tglx, x86

From:   Dave Hansen <dave.hansen@intel.com>
Date:   Mon, 24 Jan 2022 09:38:40 -0800
> On 1/22/22 17:58, Kuniyuki Iwashima wrote:
> > -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;
> >   }
> 
> 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)


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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy().
  2022-01-24 22:14   ` Kuniyuki Iwashima
@ 2022-01-24 22:48     ` Borislav Petkov
  0 siblings, 0 replies; 4+ messages in thread
From: Borislav Petkov @ 2022-01-24 22:48 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: dave.hansen, benh, dave.hansen, kuni1840, linux-kernel, mingo, tglx, x86

On Tue, Jan 25, 2022 at 07:14:47AM +0900, Kuniyuki Iwashima wrote:
> > 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.

:-)

-- 
Regards/Gruss,
    Boris.

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-24 23:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-23  1:58 [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy() Kuniyuki Iwashima
2022-01-24 17:38 ` Dave Hansen
2022-01-24 22:14   ` Kuniyuki Iwashima
2022-01-24 22:48     ` Borislav Petkov

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.