linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Clement Courbet <courbet@google.com>
To: unlisted-recipients:; (no To-header on input)
Cc: "H . Peter Anvin" <hpa@zytor.com>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Michal Marek" <michal.lkml@markovi.net>,
	"Linux Kbuild mailing list" <linux-kbuild@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	"Kees Cook" <keescook@chromium.org>,
	"Tony Luck" <tony.luck@intel.com>,
	"Dmitry Vyukov" <dvyukov@google.com>,
	"Michael Ellerman" <mpe@ellerman.id.au>,
	"Joe Perches" <joe@perches.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Daniel Axtens" <dja@axtens.net>,
	"Arvind Sankar" <nivedita@alum.mit.edu>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Alexandru Ardelean" <alexandru.ardelean@analog.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"maintainer : X86 ARCHITECTURE" <x86@kernel.org>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Paul E . McKenney" <paulmck@kernel.org>,
	"Daniel Kiper" <daniel.kiper@oracle.com>,
	"Bruce Ashfield" <bruce.ashfield@gmail.com>,
	"Marco Elver" <elver@google.com>,
	"Vamshi K Sthambamkadi" <vamshi.k.sthambamkadi@gmail.com>,
	"Andi Kleen" <ak@suse.de>,
	"Dávid Bolvanský" <david.bolvansky@gmail.com>,
	"Eli Friedman" <efriedma@quicinc.com>
Subject: Re: [PATCH 0/4] -ffreestanding/-fno-builtin-* patches
Date: Wed, 19 Aug 2020 14:19:13 +0200	[thread overview]
Message-ID: <20200819121913.3374601-1-courbet@google.com> (raw)
In-Reply-To: <CAKwvOdkJxmSXg+v1pG4+DkhoJzEE47smce6pB=Zhy6viY_++xw@mail.gmail.com>

On Tue, Aug 18, 2020 at 9:58 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
On Tue, Aug 18, 2020 at 12:25 PM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> On Tue, Aug 18, 2020 at 12:19 PM Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
> >
> > And honestly, a compiler that uses 'bcmp' is just broken. WTH? It's
> > the year 2020, we don't use bcmp. It's that simple. Fix your damn
> > broken compiler and use memcmp. The argument that memcmp is more
> > expensive than bcmp is garbage legacy thinking from four decades ago.
> >
> > It's likely the other way around, where people have actually spent
> > time on memcmp, but not on bcmp.
> >
> >                Linus
>
> You'll have to ask Clement about that.  I'm not sure I ever saw the
> "faster bcmp than memcmp" implementation, but I was told "it exists"
> when I asked for a revert when all of our kernel builds went red.

If **is** possible to make bcmp much faster then memcmp. We have one
such implementation internally (it's scheduled to be released as part of
llvm-libc some time this year), but most libc implementations just alias to
memcmp.

Below is a graph showing the impact of releasing this compiler optimization
with our optimized bcmp on the google fleet (the cumulative memcmp+bcmp usage
of all programs running on google datacenters, including the kernel). Scale and
dates have been redacted for obvious reasons, but note that the graph starts at
y=0, so you can compare the values relative to each other. Note how as memcmp
is progressively being replaced by bcmp (more and more programs being
recompiled with the compiler patch), the cumulative usage of memory
comparison drops significantly.
 
https://drive.google.com/file/d/1p8z1ilw2xaAJEnx_5eu-vflp3tEOv0qY/view?usp=sharing

The reasons why bcmp can be faster are:
 - typical libc implementations use the hardware to its full capacity, e.g. for
bcmp we can use vector loads and compares, which can process up to 64 bytes
(avx512) in one instruction. It's harder to implement memcmp with these for
little-endian architectures as there is no vector bswap. Because the kernel
only uses GPRs I can see how that might not perfectly fit the kernel use case.
But the kernel really is a special case, the compiler is written for most
programs, not specifically for the kernel, and most programs should benefit from
this optimization.
 - bcmp() does not have to look at the bytes in order, e.g. it can look at the
first and last . This is useful when comparing buffers that have common
prefixes (as happens in mostly sorted containers, and we have data that shows
that this is a quite common instance).
 

> Also, to Clement's credit, every patch I've ever seen from Clement is
> backed up by data; typically fleetwide profiles at Google.  "we spend
> a lot of time in memcmp, particularly comparing the result against
> zero and no other value; hmm...how do we spend less time in
> memcmp...oh, well there's another library function with slightly
> different semantics we can call instead."  I don't think anyone would
> consider the optimization batshit crazy given the number of cycles
> saved across the fleet.  That an embedded project didn't provide an
> implementation, is a footnote that can be fixed in the embedded
> project, either by using -ffreestanding or -fno-builtin-bcmp, which is
> what this series proposes to do.

  reply	other threads:[~2020-08-19 12:19 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-17 22:02 [PATCH 0/4] -ffreestanding/-fno-builtin-* patches Nick Desaulniers
2020-08-17 22:02 ` [PATCH 1/4] Makefile: add -fno-builtin-stpcpy Nick Desaulniers
2020-08-17 22:31   ` H. Peter Anvin
2020-08-17 23:36     ` Nick Desaulniers
2020-08-18 19:21     ` Kees Cook
2020-08-18  7:10   ` Ard Biesheuvel
2020-08-18  7:25     ` Greg KH
2020-08-18  7:29       ` Ard Biesheuvel
2020-08-18  7:34         ` Greg KH
2020-08-18 19:23   ` Kees Cook
2020-08-17 22:02 ` [PATCH 2/4] Revert "lib/string.c: implement a basic bcmp" Nick Desaulniers
2020-08-18  5:44   ` Nathan Chancellor
2020-08-18 18:00     ` Nick Desaulniers
2020-08-18 19:24       ` Kees Cook
2020-08-17 22:02 ` [PATCH 3/4] x86/boot: use -fno-builtin-bcmp Nick Desaulniers
2020-08-18 19:24   ` Kees Cook
2020-08-17 22:02 ` [PATCH 4/4] x86: don't build CONFIG_X86_32 as -ffreestanding Nick Desaulniers
2020-08-18 19:24   ` Kees Cook
2021-01-07  0:27   ` Fangrui Song
2022-04-07 15:34   ` [tip: x86/build] x86/build: Don't " tip-bot2 for Nick Desaulniers
2022-04-07 17:01     ` Nick Desaulniers
2022-04-07 22:28       ` Borislav Petkov
2020-08-17 22:44 ` [PATCH 0/4] -ffreestanding/-fno-builtin-* patches H. Peter Anvin
2020-08-18 17:56   ` Nick Desaulniers
2020-08-18 19:02     ` H. Peter Anvin
2020-08-18 19:13       ` Linus Torvalds
2020-08-18 19:25         ` Nick Desaulniers
2020-08-18 19:58           ` Nick Desaulniers
2020-08-19 12:19             ` Clement Courbet [this message]
2020-08-18 20:24         ` Arvind Sankar
2020-08-18 20:27           ` Nick Desaulniers
2020-08-18 20:58             ` Nick Desaulniers
2020-08-18 21:41               ` Arvind Sankar
2020-08-18 21:51                 ` Dávid Bolvanský
2020-08-18 21:59                 ` Nick Desaulniers
2020-08-18 22:05                   ` Dávid Bolvanský
2020-08-18 23:22                     ` Nick Desaulniers
2020-08-20 14:56                 ` Rasmus Villemoes
2020-08-20 17:56                   ` Arvind Sankar
2020-08-20 18:05                     ` Dávid Bolvanský
2020-08-20 23:33                     ` Linus Torvalds
2020-08-21 17:29                       ` Arvind Sankar
2020-08-21 17:54                         ` Linus Torvalds
2020-08-21 18:02                           ` Linus Torvalds
2020-08-21 19:14                             ` Arvind Sankar
2020-08-21 19:23                               ` Linus Torvalds
2020-08-21 19:57                           ` Arvind Sankar
2020-08-21 20:03                             ` Peter Zijlstra
2020-08-21 21:39                             ` Linus Torvalds
2020-08-22  0:12                               ` Nick Desaulniers
2020-08-22 12:20                                 ` David Laight
2020-08-21  6:45                     ` Rasmus Villemoes
2020-08-24 15:57                 ` Masahiro Yamada
2020-08-24 17:34                   ` Arvind Sankar
2020-08-25  7:10                     ` Nick Desaulniers
2020-08-25  7:31                       ` Nick Desaulniers
2020-08-25 12:28                       ` Masahiro Yamada
2020-08-25 14:02                         ` Nick Desaulniers
2020-08-26 13:28                           ` Masahiro Yamada
2020-08-18 21:53               ` David Laight
2020-08-20 22:41               ` H. Peter Anvin
2020-08-20 23:17                 ` Arvind Sankar
2020-08-18 19:35       ` Nick Desaulniers
2020-08-18 22:25 ` Arvind Sankar
2020-08-18 22:59   ` Nick Desaulniers
2020-08-18 23:51     ` Arvind Sankar
2020-08-19  0:20     ` Arvind Sankar
2020-08-19  8:26   ` 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=20200819121913.3374601-1-courbet@google.com \
    --to=courbet@google.com \
    --cc=ak@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=alexandru.ardelean@analog.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=bruce.ashfield@gmail.com \
    --cc=daniel.kiper@oracle.com \
    --cc=david.bolvansky@gmail.com \
    --cc=dja@axtens.net \
    --cc=dvyukov@google.com \
    --cc=efriedma@quicinc.com \
    --cc=elver@google.com \
    --cc=hpa@zytor.com \
    --cc=joe@perches.com \
    --cc=joel@joelfernandes.org \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=michal.lkml@markovi.net \
    --cc=mingo@redhat.com \
    --cc=mpe@ellerman.id.au \
    --cc=nivedita@alum.mit.edu \
    --cc=paulmck@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=vamshi.k.sthambamkadi@gmail.com \
    --cc=x86@kernel.org \
    --cc=yury.norov@gmail.com \
    /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).