From: Rasmus Villemoes <mail@rasmusvillemoes.dk>
To: Linus Torvalds <torvalds@linux-foundation.org>,
Borislav Petkov <bp@alien8.de>
Cc: x86-ml <x86@kernel.org>, Andy Lutomirski <luto@kernel.org>,
Josh Poimboeuf <jpoimboe@redhat.com>,
lkml <linux-kernel@vger.kernel.org>
Subject: Re: [RFC] Improve memset
Date: Fri, 13 Sep 2019 11:18:00 +0200 [thread overview]
Message-ID: <9dc9f1e6-5d19-167c-793d-2f4a5ebee097@rasmusvillemoes.dk> (raw)
In-Reply-To: <CAHk-=wismo3SQvvKXg8j0W-eC+5Q-ctcYfr1QV3K-i90w5caBA@mail.gmail.com>
On 13/09/2019 11.00, Linus Torvalds wrote:
> On Fri, Sep 13, 2019 at 8:22 AM Borislav Petkov <bp@alien8.de> wrote:
>>
>> since the merge window is closing in and y'all are on a conference, I
>> thought I should take another stab at it. It being something which Ingo,
>> Linus and Peter have suggested in the past at least once.
>>
>> Instead of calling memset:
>>
>> ffffffff8100cd8d: e8 0e 15 7a 00 callq ffffffff817ae2a0 <__memset>
>>
>> and having a JMP inside it depending on the feature supported, let's simply
>> have the REP; STOSB directly in the code:
>
> That's probably fine for when the memset *is* a call, but:
>
>> The result is this:
>>
>> static __always_inline void *memset(void *dest, int c, size_t n)
>> {
>> void *ret, *dummy;
>>
>> asm volatile(ALTERNATIVE_2_REVERSE("rep; stosb",
>
> Forcing this code means that if you do
>
> struct { long hi, low; } a;
> memset(&a, 0, sizeof(a));
>
> you force that "rep stosb". Which is HORRID.
>
> The compiler should turn it into just one single 8-byte store. But
> because you took over all of memset(), now that doesn't happen.
OK, that answers my question.
> So we do need to have gcc do the __builtin_memset() for the simple cases..
Something like
if (__builtin_constant_p(c) && __builtin_constant_p(n) && n <= 32)
return __builtin_memset(dest, c, n);
might be enough? Of course it would be sad if 32 was so high that this
turned into a memset() call, but there's -mmemset-strategy= if one wants
complete control. Though that's of course build-time, so can't consider
differences between cpu models.
Rasmus
next prev parent reply other threads:[~2019-09-13 9:18 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-13 7:22 [RFC] Improve memset Borislav Petkov
2019-09-13 7:35 ` Ingo Molnar
2019-09-13 7:50 ` Borislav Petkov
2019-09-13 8:51 ` Rasmus Villemoes
2019-09-13 9:00 ` Linus Torvalds
2019-09-13 9:18 ` Rasmus Villemoes [this message]
2019-09-13 10:42 ` Borislav Petkov
2019-09-13 16:36 ` Borislav Petkov
2019-09-16 9:18 ` Rasmus Villemoes
2019-09-16 17:25 ` Linus Torvalds
2019-09-16 17:40 ` Andy Lutomirski
2019-09-16 21:29 ` Linus Torvalds
2019-09-16 23:13 ` Andy Lutomirski
2019-09-16 23:26 ` Linus Torvalds
2019-09-17 8:15 ` Borislav Petkov
2019-09-17 10:55 ` David Laight
2019-09-17 20:10 ` Josh Poimboeuf
2019-09-17 20:45 ` Linus Torvalds
2019-09-19 12:55 ` Borislav Petkov
2019-09-19 12:49 ` Borislav Petkov
2019-09-14 9:29 Alexey Dobriyan
2019-09-14 11:39 ` 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=9dc9f1e6-5d19-167c-793d-2f4a5ebee097@rasmusvillemoes.dk \
--to=mail@rasmusvillemoes.dk \
--cc=bp@alien8.de \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=torvalds@linux-foundation.org \
--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;
as well as URLs for NNTP newsgroup(s).