Skip to contentSkip to conversation

x86: Mandate asm-goto support

PATCH In review Peter Zijlstra 6 participants 12 messages Mar 19, 2018 – Mar 27, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86: Mandate asm-goto support

Its been many _many_ years since asm-goto got introduced and recently there's been some desire to unconditionally rely on it.

This makes the x86 build hard rely on asm-goto (effectively lifts the minimum to GCC-4.5).

#1 lore raw

PATCH 1/2 x86: Force asm-goto

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

---
 Makefile          |   13 +++++++------
 arch/x86/Makefile |    4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)
Makefile +7 -6
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-ex
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
+# check for 'asm goto'
+ifeq ($(shell $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
+  CC_HAVE_ASM_GOTO := 1
+  KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
+  KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
+endif
+
 ifeq ($(config-targets),1)
 # ===========================================================================
 # *config targets only - make sure prerequisites are updated, and descend
@@ -658,12 +665,6 @@ KBUILD_CFLAGS += $(call cc-ifversion, -l
 # Tell gcc to never replace conditional load with a non-conditional one
 KBUILD_CFLAGS	+= $(call cc-option,--param=allow-store-data-races=0)
 
-# check for 'asm goto'
-ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
-	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
-	KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
-endif
-
 include scripts/Makefile.kcov
 include scripts/Makefile.gcc-plugins
 
arch/x86/Makefile +4 -0
@@ -181,6 +181,10 @@ ifdef CONFIG_FUNCTION_GRAPH_TRACER
   endif
 endif
 
+ifndef CC_HAVE_ASM_GOTO
+  $(error Compiler lacks asm-goto support.)
+endif
+
 #
 # Jump labels need '-maccumulate-outgoing-args' for gcc < 4.5.2 to prevent a
 # GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46226).  There's no way
#2 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

Quoted 16 lines · Peter Zijlstra

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

---
 Makefile          |   13 +++++++------
 arch/x86/Makefile |    4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)

Makefile RETPOLINE_CFLAGS_CLANG := -mretpoline-ex · line 498

Quoted 5 lines
Makefile +2 -0
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-ex
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
+# check for 'asm goto'
+ifeq ($(shell $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)

Why are you not using the shell-cached version which you remove further down?

Thanks,

tglx

#3 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

Quoted 21 lines · Thomas Gleixner
Quoted 19 lines · Peter Zijlstra

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

---
 Makefile          |   13 +++++++------
 arch/x86/Makefile |    4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)
Makefile +0 -0
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-ex
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
Quoted 5 lines

Makefile RETPOLINE_CFLAGS_CLANG := -mretpoline-ex · line 498

Quoted 2 lines
Makefile +2 -0
+# check for 'asm goto'
+ifeq ($(shell $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)

Why are you not using the shell-cached version which you remove further down?

Bah, I'm an idiot. I fixed that up at least two times, but clearly I forgot to check that again before sending.

---
Subject: x86: Force asm-goto
From: Peter Zijlstra <[email protected]>
Date: Wed Jan 17 11:42:07 CET 2018

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

---
 Makefile          |   13 +++++++------
 arch/x86/Makefile |    4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)
Makefile +7 -6
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-ex
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
+# check for 'asm goto'
+ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
+  CC_HAVE_ASM_GOTO := 1
+  KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
+  KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
+endif
+
 ifeq ($(config-targets),1)
 # ===========================================================================
 # *config targets only - make sure prerequisites are updated, and descend
@@ -658,12 +665,6 @@ KBUILD_CFLAGS += $(call cc-ifversion, -l
 # Tell gcc to never replace conditional load with a non-conditional one
 KBUILD_CFLAGS	+= $(call cc-option,--param=allow-store-data-races=0)
 
-# check for 'asm goto'
-ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
-	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
-	KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
-endif
-
 include scripts/Makefile.kcov
 include scripts/Makefile.gcc-plugins
 
arch/x86/Makefile +4 -0
@@ -181,6 +181,10 @@ ifdef CONFIG_FUNCTION_GRAPH_TRACER
   endif
 endif
 
+ifndef CC_HAVE_ASM_GOTO
+  $(error Compiler lacks asm-goto support.)
+endif
+
 #
 # Jump labels need '-maccumulate-outgoing-args' for gcc < 4.5.2 to prevent a
 # GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46226).  There's no way
#4 lore raw

[tip:x86/build] x86: Force asm-goto

x86: Force asm-goto
Commit-ID:  e501ce957a786ecd076ea0cfb10b114e6e4d0f40
Gitweb:     https://git.kernel.org/tip/e501ce957a786ecd076ea0cfb10b114e6e4d0f40
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Wed, 17 Jan 2018 11:42:07 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 20 Mar 2018 10:58:02 +0100

x86: Force asm-goto

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

---
 Makefile          | 13 +++++++------
 arch/x86/Makefile |  4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)
Makefile +7 -6
index d65e2e229017..6fb6fd28a124 100644
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-external-thunk
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
+# check for 'asm goto'
+ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
+  CC_HAVE_ASM_GOTO := 1
+  KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
+  KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
+endif
+
 ifeq ($(config-targets),1)
 # ===========================================================================
 # *config targets only - make sure prerequisites are updated, and descend
@@ -658,12 +665,6 @@ KBUILD_CFLAGS += $(call cc-ifversion, -lt, 0409, \
 # Tell gcc to never replace conditional load with a non-conditional one
 KBUILD_CFLAGS	+= $(call cc-option,--param=allow-store-data-races=0)
 
-# check for 'asm goto'
-ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
-	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
-	KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
-endif
-
 include scripts/Makefile.kcov
 include scripts/Makefile.gcc-plugins
 
arch/x86/Makefile +4 -0
index 498c1b812300..d798e36d103c 100644
@@ -181,6 +181,10 @@ ifdef CONFIG_FUNCTION_GRAPH_TRACER
   endif
 endif
 
+ifndef CC_HAVE_ASM_GOTO
+  $(error Compiler lacks asm-goto support.)
+endif
+
 #
 # Jump labels need '-maccumulate-outgoing-args' for gcc < 4.5.2 to prevent a
 # GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46226).  There's no way
#5 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

In reply to Peter Zijlstra
Quoted 36 lines · Peter Zijlstra
Quoted 21 lines · Thomas Gleixner
Quoted 19 lines · Peter Zijlstra

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

---
 Makefile          |   13 +++++++------
 arch/x86/Makefile |    4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)
Makefile +0 -0
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-ex
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
Quoted 5 lines

Makefile RETPOLINE_CFLAGS_CLANG := -mretpoline-ex · line 498

Quoted 2 lines
Makefile +2 -0
+# check for 'asm goto'
+ifeq ($(shell $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)

Why are you not using the shell-cached version which you remove further down?

Bah, I'm an idiot. I fixed that up at least two times, but clearly I forgot to check that again before sending.

---
Subject: x86: Force asm-goto
From: Peter Zijlstra <[email protected]>
Date: Wed Jan 17 11:42:07 CET 2018

Quoted 6 lines

We want to start using asm-goto to guarantee the absence of dynamic branches (and thus speculation).

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

With this patch applied I'm not able to do "make kernelversion" without any toolchain to get the kernel version, I was able to do that before.

Cheers,
Anders

Quoted 49 lines
---
 Makefile          |   13 +++++++------
 arch/x86/Makefile |    4 ++++
 2 files changed, 11 insertions(+), 6 deletions(-)
Makefile +7 -6
@@ -494,6 +494,13 @@ RETPOLINE_CFLAGS_CLANG := -mretpoline-ex
 RETPOLINE_CFLAGS := $(call cc-option,$(RETPOLINE_CFLAGS_GCC),$(call cc-option,$(RETPOLINE_CFLAGS_CLANG)))
 export RETPOLINE_CFLAGS
 
+# check for 'asm goto'
+ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
+  CC_HAVE_ASM_GOTO := 1
+  KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
+  KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
+endif
+
 ifeq ($(config-targets),1)
 # ===========================================================================
 # *config targets only - make sure prerequisites are updated, and descend
@@ -658,12 +665,6 @@ KBUILD_CFLAGS += $(call cc-ifversion, -l
 # Tell gcc to never replace conditional load with a non-conditional one
 KBUILD_CFLAGS	+= $(call cc-option,--param=allow-store-data-races=0)
 
-# check for 'asm goto'
-ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y)
-	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
-	KBUILD_AFLAGS += -DCC_HAVE_ASM_GOTO
-endif
-
 include scripts/Makefile.kcov
 include scripts/Makefile.gcc-plugins
 
arch/x86/Makefile +4 -0
@@ -181,6 +181,10 @@ ifdef CONFIG_FUNCTION_GRAPH_TRACER
   endif
 endif
 
+ifndef CC_HAVE_ASM_GOTO
+  $(error Compiler lacks asm-goto support.)
+endif
+
 #
 # Jump labels need '-maccumulate-outgoing-args' for gcc < 4.5.2 to prevent a
 # GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46226).  There's no way
#6 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

In reply to Peter Zijlstra
Quoted 3 lines · On Mon, 19 Mar 2018 16:41:25 +0100 Peter Zijlstra <[email protected]> wrote:

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

Given that, how about a patch to Documentation/Changes to reflect the new reality? It's not in RST yet, so you should be able to go there without ill effect :)

Thanks,

jon

#7 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

Quoted 10 lines · Jonathan Corbet
Quoted 3 lines · On Mon, 19 Mar 2018 16:41:25 +0100 Peter Zijlstra <[email protected]> wrote:

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

Given that, how about a patch to Documentation/Changes to reflect the new reality? It's not in RST yet, so you should be able to go there without ill effect :)

That thing says:

GCC
---

The gcc version requirements may vary depending on the type of CPU in your computer. jon

Which is still entirely accurate. And afaik the 3.2 minimum listed there still works for !x86.

#8 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

Quoted 7 lines · On Tue, 20 Mar 2018 16:50:36 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 3 lines

Given that, how about a patch to Documentation/Changes to reflect the new reality? It's not in RST yet, so you should be able to go there without ill effect :)

That thing says:

Quoted 8 lines

GCC
---

The gcc version requirements may vary depending on the type of CPU in your computer. jon

Which is still entirely accurate. And afaik the 3.2 minimum listed there still works for !x86.

Well, other recent discussions established that nothing earlier than 4.1 has been observed to work anywhere for some time.

I guess what we really need is a little table of architecture-specific minimum GCC versions so that our users actually know what's required. I'll see if I can find a moment to pull that together at some point.

jon

#9 lore raw

Re: [PATCH 1/2] x86: Force asm-goto

In reply to Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
Quoted 10 lines · Jonathan Corbet
Quoted 3 lines · On Mon, 19 Mar 2018 16:41:25 +0100 Peter Zijlstra <[email protected]> wrote:

A primary prerequisite for this is of course that the compiler supports asm-goto. This effecively lifts the minimum GCC version to build an x86 kernel to gcc-4.5.

Given that, how about a patch to Documentation/Changes to reflect the new reality? It's not in RST yet, so you should be able to go there without ill effect :)

That thing says:

Quoted 8 lines

GCC
---

The gcc version requirements may vary depending on the type of CPU in your computer. jon

Which is still entirely accurate. And afaik the 3.2 minimum listed there still works for !x86.

Maybe accurate, but pretty user hostile :-(.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#10 lore raw

PATCH 2/2 x86: Remove FAST_FEATURE_TESTS

In reply to Peter Zijlstra

Since we want to rely on static branches to avoid speculation, remove any possible fallback code for static_cpu_has.

---
 arch/x86/Kconfig                  |   11 -----------
 arch/x86/include/asm/cpufeature.h |    8 --------
 2 files changed, 19 deletions(-)
arch/x86/Kconfig +0 -11
@@ -393,17 +393,6 @@ config X86_FEATURE_NAMES
 
 	  If in doubt, say Y.
 
-config X86_FAST_FEATURE_TESTS
-	bool "Fast CPU feature tests" if EMBEDDED
-	default y
-	---help---
-	  Some fast-paths in the kernel depend on the capabilities of the CPU.
-	  Say Y here for the kernel to patch in the appropriate code at runtime
-	  based on the capabilities of the CPU. The infrastructure for patching
-	  code at runtime takes up some additional space; space-constrained
-	  embedded systems may wish to say N here to produce smaller, slightly
-	  slower code.
-
 config X86_X2APIC
 	bool "Support x2apic"
 	depends on X86_LOCAL_APIC && X86_64 && (IRQ_REMAP || HYPERVISOR_GUEST)
arch/x86/include/asm/cpufeature.h +0 -8
@@ -140,7 +140,6 @@ extern void clear_cpu_cap(struct cpuinfo
 
 #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
 
-#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_X86_FAST_FEATURE_TESTS)
 /*
  * Static testing of CPU features.  Used the same as boot_cpu_has().
  * These will statically patch the target code for additional
@@ -196,13 +195,6 @@ static __always_inline __pure bool _stat
 		boot_cpu_has(bit) :				\
 		_static_cpu_has(bit)				\
 )
-#else
-/*
- * Fall back to dynamic for gcc versions which don't support asm goto. Should be
- * a minority now anyway.
- */
-#define static_cpu_has(bit)		boot_cpu_has(bit)
-#endif
 
 #define cpu_has_bug(c, bit)		cpu_has(c, (bit))
 #define set_cpu_bug(c, bit)		set_cpu_cap(c, (bit))
#11 lore raw

[tip:x86/build] x86: Remove FAST_FEATURE_TESTS

x86: Remove FAST_FEATURE_TESTS
Commit-ID:  d0266046ad54e0c964941364cd82a0d0478ce286
Gitweb:     https://git.kernel.org/tip/d0266046ad54e0c964941364cd82a0d0478ce286
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Mon, 19 Mar 2018 16:41:26 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 20 Mar 2018 10:58:03 +0100

x86: Remove FAST_FEATURE_TESTS

Since we want to rely on static branches to avoid speculation, remove any possible fallback code for static_cpu_has.

---
 arch/x86/Kconfig                  | 11 -----------
 arch/x86/include/asm/cpufeature.h |  8 --------
 2 files changed, 19 deletions(-)
arch/x86/Kconfig +0 -11
index 0fa71a78ec99..cb5b5907dbd6 100644
@@ -393,17 +393,6 @@ config X86_FEATURE_NAMES
 
 	  If in doubt, say Y.
 
-config X86_FAST_FEATURE_TESTS
-	bool "Fast CPU feature tests" if EMBEDDED
-	default y
-	---help---
-	  Some fast-paths in the kernel depend on the capabilities of the CPU.
-	  Say Y here for the kernel to patch in the appropriate code at runtime
-	  based on the capabilities of the CPU. The infrastructure for patching
-	  code at runtime takes up some additional space; space-constrained
-	  embedded systems may wish to say N here to produce smaller, slightly
-	  slower code.
-
 config X86_X2APIC
 	bool "Support x2apic"
 	depends on X86_LOCAL_APIC && X86_64 && (IRQ_REMAP || HYPERVISOR_GUEST)
arch/x86/include/asm/cpufeature.h +0 -8
index 736771c9822e..b27da9602a6d 100644
@@ -140,7 +140,6 @@ extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
 
 #define setup_force_cpu_bug(bit) setup_force_cpu_cap(bit)
 
-#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_X86_FAST_FEATURE_TESTS)
 /*
  * Static testing of CPU features.  Used the same as boot_cpu_has().
  * These will statically patch the target code for additional
@@ -196,13 +195,6 @@ t_no:
 		boot_cpu_has(bit) :				\
 		_static_cpu_has(bit)				\
 )
-#else
-/*
- * Fall back to dynamic for gcc versions which don't support asm goto. Should be
- * a minority now anyway.
- */
-#define static_cpu_has(bit)		boot_cpu_has(bit)
-#endif
 
 #define cpu_has_bug(c, bit)		cpu_has(c, (bit))
 #define set_cpu_bug(c, bit)		set_cpu_cap(c, (bit))