2021-12-16 20:00:38

by Bill Wendling

[permalink] [raw]
Subject: Re: [PATCH] x86: use builtins to read eflags

()On Wed, Dec 15, 2021 at 3:26 PM Peter Zijlstra <[email protected]> wrote:
>
> On Wed, Dec 15, 2021 at 01:18:47PM -0800, Bill Wendling wrote:
> > GCC and Clang both have builtins to read from and write to the
> > EFLAGS register. This allows the compiler to determine the best way
> > to generate the code, which can improve code generation.
>
> Only because clang is still brain-dead wrt "rm" constraints, right?
>
That's one reason. That needs to be addressed, but builtins are
usually better than using inline assembly.

> > Signed-off-by: Bill Wendling <[email protected]>
> > ---
> > arch/x86/include/asm/irqflags.h | 24 +++++-------------------
> > 1 file changed, 5 insertions(+), 19 deletions(-)
> >
> > diff --git a/arch/x86/include/asm/irqflags.h b/arch/x86/include/asm/irqflags.h
> > index c5ce9845c999..574fb44b82f7 100644
> > --- a/arch/x86/include/asm/irqflags.h
> > +++ b/arch/x86/include/asm/irqflags.h
> > @@ -15,25 +15,11 @@
> > * Interrupt control:
> > */
> >
> > -/* Declaration required for gcc < 4.9 to prevent -Werror=missing-prototypes */
> > -extern inline unsigned long native_save_fl(void);
> > -extern __always_inline unsigned long native_save_fl(void)
> > -{
> > - unsigned long flags;
> > -
> > - /*
> > - * "=rm" is safe here, because "pop" adjusts the stack before
> > - * it evaluates its effective address -- this is part of the
> > - * documented behavior of the "pop" instruction.
> > - */
> > - asm volatile("# __raw_save_flags\n\t"
> > - "pushf ; pop %0"
> > - : "=rm" (flags)
> > - : /* no input */
> > - : "memory");
> > -
> > - return flags;
> > -}
> > +#ifdef CONFIG_X86_64
> > +#define native_save_fl() __builtin_ia32_readeflags_u64()
> > +#else
> > +#define native_save_fl() __builtin_ia32_readeflags_u32()
> > +#endif
>
> Also note the thing was extern inline, and there's actually an
> out-of-line symbol for them too. The out-of-line thing is explicitly
> using %rax due to paravirt muck.
>
> I'm thinking you wrecked that bit.

If you prefer, it could be written like so:

extern inline unsigned long native_save_fl(void);
extern __always_inline unsigned long native_save_fl(void)
{
#ifdef CONFIG_X86_64
return __builtin_ia32_readeflags_u64();
#else
return __builtin_ia32_readeflags_u32();
#endif
}

-bw


2021-12-16 20:07:36

by Nick Desaulniers

[permalink] [raw]
Subject: Re: [PATCH] x86: use builtins to read eflags

On Thu, Dec 16, 2021 at 12:00 PM Bill Wendling <[email protected]> wrote:
>
> ()On Wed, Dec 15, 2021 at 3:26 PM Peter Zijlstra <[email protected]> wrote:
> >
> > Also note the thing was extern inline, and there's actually an
> > out-of-line symbol for them too. The out-of-line thing is explicitly
> > using %rax due to paravirt muck.

Oh, shoot, I wrote that.
d0a8d9378d16e
That was before we had __attribute__(no_stack_protector). Once the
minimal supported versions of both compilers support that, I'd love to
revert d0a8d9378d16e.

extern inline is the thing that makes me most nervous about moving the
kernel from -std=gnu89 to anything newer. I should check whether
gnu99 uses c99 extern inline or gnu inline...

> >
> > I'm thinking you wrecked that bit.
>
> If you prefer, it could be written like so:
>
> extern inline unsigned long native_save_fl(void);
> extern __always_inline unsigned long native_save_fl(void)
> {
> #ifdef CONFIG_X86_64
> return __builtin_ia32_readeflags_u64();
> #else
> return __builtin_ia32_readeflags_u32();
> #endif
> }

Yes, that would fix the `extern inline` issue. I wonder if this is prettier as:

return IS_ENABLED(CONFIG_X86_64) ? __builtin_ia32_readeflags_u64() :
__builtin_ia32_readeflags_u32();
--
Thanks,
~Nick Desaulniers