linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
  • [parent not found: <20211215232605.GJ16608@worktop.programming.kicks-ass.net>]
  • * [PATCH v2] x86: use builtins to read eflags
           [not found] <20211215211847.206208-1-morbo@google.com>
           [not found] ` <87mtl1l63m.ffs@tglx>
           [not found] ` <20211215232605.GJ16608@worktop.programming.kicks-ass.net>
    @ 2021-12-29  2:12 ` Bill Wendling
      2022-01-27 20:56   ` Bill Wendling
                         ` (2 more replies)
      2 siblings, 3 replies; 63+ messages in thread
    From: Bill Wendling @ 2021-12-29  2:12 UTC (permalink / raw)
      To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
    	H . Peter Anvin, Nathan Chancellor, Nick Desaulniers,
    	Juergen Gross, Peter Zijlstra, Andy Lutomirski, llvm
      Cc: linux-kernel, Bill Wendling
    
    GCC and Clang both have builtins to read and write the EFLAGS register.
    This allows the compiler to determine the best way to generate this
    code, which can improve code generation.
    
    This issue arose due to Clang's issue with the "=rm" constraint.  Clang
    chooses to be conservative in these situations, and so uses memory
    instead of registers. This is a known issue, which is currently being
    addressed.
    
    However, using builtins is benefiical in general, because it removes the
    burden of determining what's the way to read the flags register from the
    programmer and places it on to the compiler, which has the information
    needed to make that decision. Indeed, this piece of code has had several
    changes over the years, some of which were pinging back and forth to
    determine the correct constraints to use.
    
    With this change, Clang generates better code:
    
    Original code:
            movq    $0, -48(%rbp)
            #APP
            # __raw_save_flags
            pushfq
            popq    -48(%rbp)
            #NO_APP
            movq    -48(%rbp), %rbx
    
    New code:
            pushfq
            popq    %rbx
            #APP
    
    Note that the stack slot in the original code is no longer needed in the
    new code, saving a small amount of stack space.
    
    Signed-off-by: Bill Wendling <morbo@google.com>
    ---
    v2: - Kept the original function to retain the out-of-line symbol.
        - Improved the commit message.
        - Note that I couldn't use Nick's suggestion of
    
            return IS_ENABLED(CONFIG_X86_64) ? ...
    
          because Clang complains about using __builtin_ia32_readeflags_u32 in
          64-bit mode.
    ---
     arch/x86/include/asm/irqflags.h | 19 +++++--------------
     1 file changed, 5 insertions(+), 14 deletions(-)
    
    diff --git a/arch/x86/include/asm/irqflags.h b/arch/x86/include/asm/irqflags.h
    index c5ce9845c999..27f919ea7ac3 100644
    --- a/arch/x86/include/asm/irqflags.h
    +++ b/arch/x86/include/asm/irqflags.h
    @@ -19,20 +19,11 @@
     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
    +	return __builtin_ia32_readeflags_u64();
    +#else
    +	return __builtin_ia32_readeflags_u32();
    +#endif
     }
     
     static __always_inline void native_irq_disable(void)
    -- 
    2.34.1.448.ga2b2bfdf31-goog
    
    
    ^ permalink raw reply related	[flat|nested] 63+ messages in thread

  • end of thread, other threads:[~2022-03-19 23:16 UTC | newest]
    
    Thread overview: 63+ messages (download: mbox.gz / follow: Atom feed)
    -- links below jump to the message on this page --
         [not found] <20211215211847.206208-1-morbo@google.com>
         [not found] ` <87mtl1l63m.ffs@tglx>
    2021-12-16 19:55   ` [PATCH] x86: use builtins to read eflags Bill Wendling
    2021-12-17 12:48     ` Peter Zijlstra
    2021-12-17 19:39     ` Thomas Gleixner
    2022-03-14 23:09     ` H. Peter Anvin
    2022-03-15  0:08       ` Bill Wendling
         [not found] ` <20211215232605.GJ16608@worktop.programming.kicks-ass.net>
    2021-12-16 20:00   ` Bill Wendling
    2021-12-16 20:07     ` Nick Desaulniers
    2021-12-29  2:12 ` [PATCH v2] " Bill Wendling
    2022-01-27 20:56   ` Bill Wendling
    2022-02-04  0:16   ` Thomas Gleixner
    2022-02-04  0:58     ` Bill Wendling
    2022-02-04  0:57   ` [PATCH v3] " Bill Wendling
    2022-02-07 22:11     ` Nick Desaulniers
    2022-02-08  9:14       ` David Laight
    2022-02-08 23:18         ` Bill Wendling
    2022-02-14 23:53         ` Nick Desaulniers
    2022-02-10 22:31     ` [PATCH v4] " Bill Wendling
    2022-02-11 16:40       ` David Laight
    2022-02-11 19:25         ` Bill Wendling
    2022-02-11 22:09           ` David Laight
    2022-02-11 23:33             ` Bill Wendling
    2022-02-12  0:24           ` Nick Desaulniers
    2022-02-12  9:23             ` Bill Wendling
    2022-02-15  0:33               ` Nick Desaulniers
    2022-03-01 20:19       ` [PATCH v5] " Bill Wendling
    2022-03-14 23:07         ` Bill Wendling
         [not found]           ` <AC3D873E-A28B-41F1-8BF4-2F6F37BCEEB4@zytor.com>
    2022-03-15  7:19             ` Bill Wendling
    2022-03-17 15:43               ` H. Peter Anvin
    2022-03-17 18:00                 ` 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
    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
    

    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).