llvm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Nick Desaulniers <ndesaulniers@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org,  "H. Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Kees Cook <keescook@chromium.org>,
	 linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	 Andy Lutomirski <luto@kernel.org>, Ma Ling <ling.ma@intel.com>
Subject: Re: [PATCH] x86, mem: move memmove to out of line assembler
Date: Fri, 23 Sep 2022 10:29:58 -0700	[thread overview]
Message-ID: <CAHk-=wiAwEA5KekL4+t+v6qAdKLSOQvBkGbumvk+Rxhaay8aFg@mail.gmail.com> (raw)
In-Reply-To: <20220923170218.1188423-1-ndesaulniers@google.com>

On Fri, Sep 23, 2022 at 10:02 AM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> memmove is quite large and probably shouldn't be inlined due to size
> alone. A noinline function attribute would be the simplest fix, but
> there's a few things that stand out with the current definition:

I don't think your patch is wrong, and it's not that different from
what the x86-64 code did long ago back in 2011 in commit 9599ec0471de
("x86-64, mem: Convert memmove() to assembly file and fix return value
bug")

But I wonder if we might be better off getting rid of that horrid
memmove thing entirely. The original thing seems to be from 2010,
where commit 3b4b682becdf ("x86, mem: Optimize memmove for small size
and unaligned cases") did this thing for both 32-bit and 64-bit code.

And it's really not likely used all that much - memcpy is the *much*
more important function, and that's the one that has actually been
updated for modern CPU's instead of looking like it's some copy from
glibc or whatever.

To make things even stranger, on the x86-64 side, we actually have
*two* copies of 'memmove()' - it looks like memcpy_orig() is already a
memmove, in that it does that

        cmp  %dil, %sil
        jl .Lcopy_backward

thing that seems to mean that it already handles the overlapping case.

Anyway, that 32-bit memmove() implemented (badly) as inline asm does
need to go. As you point out, it seems to get the clobbers wrong too,
so it's actively buggy and just happens to work because there's
nothing else in that function, and it clobbers %bx that is supposed to
be callee-saved, but *presumably* the compile has to allocate %bx one
of the other registers, so it will save and restore %bx anyway.

So that current memmove() seems to be truly horrendously buggy, but in
a "it happens to work" way. Your patch seems an improvement, but I get
the feeling that it could be improved a lot more...

              Linus

  reply	other threads:[~2022-09-23 17:30 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-23 17:02 [PATCH] x86, mem: move memmove to out of line assembler Nick Desaulniers
2022-09-23 17:29 ` Linus Torvalds [this message]
2022-09-23 17:55   ` Nick Desaulniers
2022-09-23 18:05     ` Linus Torvalds
2022-09-27 17:03       ` Nick Desaulniers
2022-09-27 17:28         ` [PATCH v2] " Nick Desaulniers
2022-09-27 18:41           ` Kees Cook
2022-09-27 19:23           ` Kees Cook
2022-09-27 20:01             ` Nick Desaulniers
2022-09-27 20:36               ` Kees Cook
2022-09-27 21:02                 ` [PATCH v3] " Nick Desaulniers
2022-09-27 21:14                   ` Kees Cook
2022-09-28  7:24                   ` Rasmus Villemoes
2022-09-28 19:00                     ` Linus Torvalds
2022-09-28 19:06                     ` Nick Desaulniers
2022-09-28 20:49                       ` Nick Desaulniers
2022-09-28 21:05                         ` [PATCH v4] " Nick Desaulniers
2022-09-28 22:03                           ` Kees Cook
2022-09-29  7:01                           ` Ingo Molnar
2022-09-29  8:02                           ` Ingo Molnar
2022-09-29 17:26                             ` Nick Desaulniers
2022-09-30  9:55                           ` David Laight
2022-09-30 16:43                             ` Nick Desaulniers
2022-09-30 16:46                               ` Linus Torvalds
2022-09-30 18:55                                 ` [PATCH v5] " Nick Desaulniers
2022-09-30 10:14                           ` [PATCH v4] " David Laight

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='CAHk-=wiAwEA5KekL4+t+v6qAdKLSOQvBkGbumvk+Rxhaay8aFg@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=keescook@chromium.org \
    --cc=ling.ma@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --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;
as well as URLs for NNTP newsgroup(s).