linux-toolchains.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Bill Wendling <morbo@google.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)"
	<x86@kernel.org>, Nathan Chancellor <nathan@kernel.org>,
	Juergen Gross <jgross@suse.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Andy Lutomirski <luto@kernel.org>,
	llvm@lists.linux.dev, LKML <linux-kernel@vger.kernel.org>,
	linux-toolchains <linux-toolchains@vger.kernel.org>
Subject: Re: [PATCH v5] x86: use builtins to read eflags
Date: Thu, 17 Mar 2022 21:13:05 +0100	[thread overview]
Message-ID: <878rt8gwxa.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <CAHk-=whJfKN8Jag=8DS=pbZR3TY90znUOP6Km+TLRJ9dZEgNqw@mail.gmail.com> (Linus Torvalds's message of "Thu, 17 Mar 2022 11:52:26 -0700")

* Linus Torvalds:

> You can actually operate on EFLAGS at multiple granularities.
>
>  - normal pushf/popf. Don't do it unless you are doing system software.

There's one exception: PUSHF/twiddle/POPF/PUSHF/compare is the
recommended sequence to detect CPUID support on i386 (in userspace and
elsewhere).

>  - you can use lahf/sahc to load/store only the arithmetic flags
> into/from AH. Deprecated, and going away, but historically supported.

And these instructions were missing from the original long mode, but
they were added back.

> Yes, yes, that complete mental breakdown with pushf/popf did get
> fixed, but it really makes me very wary of thinking that we should
> ever use a built-in that compiler writers really fundamentally got so
> wrong before.
>
> What would make me think that you'd get it right now? In user space,
> you'll basically never actually see the whole system flags issues, so
> your test-cases would never work or be very contrieved. You'd have to
> really work at it to see the problems.

I think as the result of the nature of that kind of bug it does not
matter whether you use a compiler builtin to access the flags (to put
their combined value into a general-purpose register).

GCC doesn't have barriers in the built-ins (if we are talking about
__builtin_ia32_readeflags_u64 and __builtin_ia32_writeeflags_u64).  I
expect they are actually pretty useless, and were merely added for
completeness of the intrinsics headers.

It's not that you can write

  unsigned a, b, c;
  // …
  c = a + b;

and examine __builtin_ia32_readeflags_u64() to see if there was an
overflow.  Neither GCC nor Clang model the EFLAGS register and
arithmetic expression side effects to make this possible.

Thanks,
Florian


  parent reply	other threads:[~2022-03-17 20:13 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20220210223134.233757-1-morbo@google.com>
     [not found] ` <20220301201903.4113977-1-morbo@google.com>
     [not found]   ` <CAGG=3QWh90r5C3gmTj9zxiJb-mwD=PGqGwZZTjAfyi1NCb1_9w@mail.gmail.com>
     [not found]     ` <AC3D873E-A28B-41F1-8BF4-2F6F37BCEEB4@zytor.com>
     [not found]       ` <CAGG=3QVu5QjQK8m2FWiYn-XQuVBjUGXcbznSbK22jVMB5GAutw@mail.gmail.com>
     [not found]         ` <F5296439-4CA3-4F31-BD91-5ED1510BC382@zytor.com>
2022-03-17 18:00           ` [PATCH v5] x86: use builtins to read eflags Nick Desaulniers
2022-03-17 18:52             ` Linus Torvalds
2022-03-17 19:45               ` Bill Wendling
2022-03-17 20:13                 ` Linus Torvalds
2022-03-17 21:10                   ` Bill Wendling
2022-03-17 21:21                     ` Linus Torvalds
2022-03-17 21:45                       ` Bill Wendling
2022-03-17 22:51                         ` Linus Torvalds
2022-03-17 23:14                           ` Linus Torvalds
2022-03-17 23:19                           ` Segher Boessenkool
2022-03-17 23:31                             ` Linus Torvalds
2022-03-18  0:05                               ` Segher Boessenkool
2022-03-17 22:37                 ` Segher Boessenkool
2022-03-17 20:13               ` Florian Weimer [this message]
2022-03-17 20:36                 ` Linus Torvalds
2022-03-18  0:25                   ` Segher Boessenkool
2022-03-18  1:21                     ` Linus Torvalds
2022-03-18  1:50                       ` Linus Torvalds
2022-03-17 21:05               ` Andrew Cooper
2022-03-17 21:39                 ` Linus Torvalds
2022-03-18 17:59                   ` Andy Lutomirski
2022-03-18 18:19                     ` Linus Torvalds
2022-03-18 21:48                       ` Andrew Cooper
2022-03-18 23:10                         ` Linus Torvalds
2022-03-18 23:42                           ` Segher Boessenkool
2022-03-19  1:13                             ` Linus Torvalds
2022-03-19 23:15                             ` Andy Lutomirski
2022-03-18 22:09                       ` Segher Boessenkool
2022-03-18 22:33                         ` H. Peter Anvin
2022-03-18 22:36                         ` David Laight
2022-03-18 22:47                           ` H. Peter Anvin
2022-03-18 22:43                       ` David Laight
2022-03-18 23:03                         ` H. Peter Anvin
2022-03-18 23:04                   ` Segher Boessenkool
2022-03-18 23:52                     ` 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=878rt8gwxa.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-toolchains@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --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).