From: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
To: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>
Cc: Benjamin Herrenschmidt <benh@amazon.com>,
Kuniyuki Iwashima <kuniyu@amazon.co.jp>,
Kuniyuki Iwashima <kuni1840@gmail.com>, <x86@kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy().
Date: Sun, 23 Jan 2022 10:58:07 +0900 [thread overview]
Message-ID: <20220123015807.45005-1-kuniyu@amazon.co.jp> (raw)
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
next reply other threads:[~2022-01-23 1:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-23 1:58 Kuniyuki Iwashima [this message]
2022-01-24 17:38 ` [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy() Dave Hansen
2022-01-24 22:14 ` Kuniyuki Iwashima
2022-01-24 22:48 ` Borislav Petkov
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=20220123015807.45005-1-kuniyu@amazon.co.jp \
--to=kuniyu@amazon.co.jp \
--cc=benh@amazon.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=kuni1840@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--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