Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
@ 2026-11-16  6:05 tip-bot2 for Nathan Chancellor
  2026-11-16  9:40 ` Ingo Molnar
  0 siblings, 1 reply; 7+ messages in thread
From: tip-bot2 for Nathan Chancellor @ 2026-11-16  6:05 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Nathan Chancellor, Borislav Petkov (AMD), stable, x86,
	linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     2946884aff3d0e67227df3b4d3bcd9c3357a3b78
Gitweb:        https://git.kernel.org/tip/2946884aff3d0e67227df3b4d3bcd9c3357a3b78
Author:        Nathan Chancellor <nathan@kernel.org>
AuthorDate:    Sun, 15 Nov 2026 21:30:43 -07:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sun, 15 Nov 2026 20:43:50 -08:00

x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing

When cross compiling x86_64 from an aarch64 machine using clang with
CONFIG_X86_NATIVE_CPU enabled (such as when using allmodconfig), there may be
a slew of warnings from each .c file, which makes it nearly impossible to find
real issues.

  '+crc' is not a recognized feature for this target (ignoring feature)
  '+fp-armv8' is not a recognized feature for this target (ignoring feature)
  '+lse' is not a recognized feature for this target (ignoring feature)
  '+neon' is not a recognized feature for this target (ignoring feature)
  '+sha2' is not a recognized feature for this target (ignoring feature)
  '+sha3' is not a recognized feature for this target (ignoring feature)
  '-rand' is not a recognized feature for this target (ignoring feature)
  '-sme' is not a recognized feature for this target (ignoring feature)
  '-sme2' is not a recognized feature for this target (ignoring feature)
  '-sve' is not a recognized feature for this target (ignoring feature)

Internally, clang expands '-march=native' into a list of target features to
enable based on the detected CPU. The x86 part of the clang driver notes

  // FIXME: Reject attempts to use -march=native unless the target matches
  // the host.

which was first added by the initial '-march=native' support in clang in 2009¹
(other architecture drivers appear to have a similar problem).  Due to the lack
of validation that the host CPU is an x86_64 CPU, '-march=native' is expanded
using the aarch64 host CPU's target features, which are unrecognized by the x86
backend.

While this should obviously be fixed in the compiler, this does not help
currently released toolchains that have this issue. As a simple workaround that
makes sense regardless of the toolchain issue, only allow CONFIG_X86_NATIVE_CPU
to be enabled when CONFIG_COMPILE_TEST is not set (i.e., the user intends to run
the kernel, not just build it), which will exclude it from allmodconfig.

  ¹https://github.com/llvm/llvm-project/commit/e13ada6a3b236f505b24501d456dec2518e4944c

Fixes: ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20261113-x86-no-march-native-on-non-x86_64-v1-1-cbc8b096d1c4@kernel.org
---
 arch/x86/Kconfig.cpu | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
index 6e7a366..32d22cb 100644
--- a/arch/x86/Kconfig.cpu
+++ b/arch/x86/Kconfig.cpu
@@ -217,6 +217,7 @@ config RUSTC_HAS_APXF
 config X86_NATIVE_CPU
 	bool "Build and optimize for local/native CPU"
 	depends on X86_64
+	depends on !COMPILE_TEST
 	depends on CC_HAS_MARCH_NATIVE
 	depends on !RUST || RUSTC_HAS_APXF
 	help

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
  2026-11-16  6:05 [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing tip-bot2 for Nathan Chancellor
@ 2026-11-16  9:40 ` Ingo Molnar
  2026-11-16 13:01   ` Nathan Chancellor
  0 siblings, 1 reply; 7+ messages in thread
From: Ingo Molnar @ 2026-11-16  9:40 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-tip-commits, Nathan Chancellor, Borislav Petkov (AMD),
	stable, x86, Linus Torvalds


* tip-bot2 for Nathan Chancellor <tip-bot2@linutronix.de> wrote:

> which was first added by the initial '-march=native' support in clang in 2009¹
> (other architecture drivers appear to have a similar problem).  Due to the lack
> of validation that the host CPU is an x86_64 CPU, '-march=native' is expanded
> using the aarch64 host CPU's target features, which are unrecognized by the x86
> backend.
> 
> While this should obviously be fixed in the compiler, this does not help
> currently released toolchains that have this issue. As a simple workaround that
> makes sense regardless of the toolchain issue, only allow CONFIG_X86_NATIVE_CPU
> to be enabled when CONFIG_COMPILE_TEST is not set (i.e., the user intends to run
> the kernel, not just build it), which will exclude it from allmodconfig.
> 
>   ¹https://github.com/llvm/llvm-project/commit/e13ada6a3b236f505b24501d456dec2518e4944c
> 
> Fixes: ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'")
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
> Cc: stable@vger.kernel.org
> Link: https://lore.kernel.org/r/20261113-x86-no-march-native-on-non-x86_64-v1-1-cbc8b096d1c4@kernel.org
> ---
>  arch/x86/Kconfig.cpu | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
> index 6e7a366..32d22cb 100644
> --- a/arch/x86/Kconfig.cpu
> +++ b/arch/x86/Kconfig.cpu
> @@ -217,6 +217,7 @@ config RUSTC_HAS_APXF
>  config X86_NATIVE_CPU
>  	bool "Build and optimize for local/native CPU"
>  	depends on X86_64
> +	depends on !COMPILE_TEST

I really don't like this patch. People *do* use this feature,
and it's good to have it enabled in allmodconfig.

Another problem is that this turns off most automated x86-on-x86
build testing of CONFIG_X86_NATIVE_CPU in essence, at least for the
typical configs that CI farms test, just to make much more rare
types of cross-builds a bit more convenient...

Can we just limit the feature to when host == target, ie. to not
work it around in such a crude fashion?

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
  2026-11-16  9:40 ` Ingo Molnar
@ 2026-11-16 13:01   ` Nathan Chancellor
  2026-11-16 15:48     ` Linus Torvalds
  0 siblings, 1 reply; 7+ messages in thread
From: Nathan Chancellor @ 2026-11-16 13:01 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: linux-kernel, linux-tip-commits, Borislav Petkov (AMD), stable,
	x86, Linus Torvalds

On Mon, Nov 16, 2026 at 10:40:01AM +0100, Ingo Molnar wrote:
> Can we just limit the feature to when host == target, ie. to not
> work it around in such a crude fashion?

That was my v1 but Boris didn't like it:

  https://lore.kernel.org/20261113-x86-no-march-native-on-non-x86_64-v1-1-cbc8b096d1c4@kernel.org

-- 
Cheers,
Nathan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
  2026-11-16 13:01   ` Nathan Chancellor
@ 2026-11-16 15:48     ` Linus Torvalds
  2026-11-16 15:53       ` Linus Torvalds
                         ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Linus Torvalds @ 2026-11-16 15:48 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Ingo Molnar, linux-kernel, linux-tip-commits,
	Borislav Petkov (AMD), stable, x86

On Mon, 16 Nov 2026 at 05:02, Nathan Chancellor <nathan@kernel.org> wrote:
>
> On Mon, Nov 16, 2026 at 10:40:01AM +0100, Ingo Molnar wrote:
> > Can we just limit the feature to when host == target, ie. to not
> > work it around in such a crude fashion?
>
> That was my v1 but Boris didn't like it:
>
>   https://lore.kernel.org/20261113-x86-no-march-native-on-non-x86_64-v1-1-cbc8b096d1c4@kernel.org

Well, that is admittedly very crude and hacky.

Maybe we should have some config variables that depend on HOSTCC, and
detect it *that* way. It does appear that we don't have any model for
cross-compiling in the Kconfig setup, but maybe  we should..

I'd actually love to have support for making the compiler itself part
of Kconfig, so that the gcc-vs-clang could be *in* the .config file
rather than being an external thing to it.

I've mentioned that before, and the problem has typically been that we
end up depending on 'CC' outside of the Kconfig scripts and
independently of the current config.

The recent work to move the compiler flags into Kconfig itself may
have lessened some of those dependencies, but there's probably still
lots of small details.

But for the whole "cross-compile", maybe we can just say that
X86_NATIVE_CPU depends on '$CC' being the same as '$HOSTCC'.

Or even do '$(CC) -v" and '$(HOSTCC) -v' and check for 'Target' being
the same, and having a CONFIG_CROSSCOMPILE thing for that?

               Linus

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
  2026-11-16 15:48     ` Linus Torvalds
@ 2026-11-16 15:53       ` Linus Torvalds
  2026-11-16 20:45       ` Nathan Chancellor
  2026-11-16 22:36       ` David Laight
  2 siblings, 0 replies; 7+ messages in thread
From: Linus Torvalds @ 2026-11-16 15:53 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Ingo Molnar, linux-kernel, linux-tip-commits,
	Borislav Petkov (AMD), stable, x86

On Mon, 16 Nov 2026 at 07:48, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> Or even do '$(CC) -v" and '$(HOSTCC) -v' and check for 'Target' being
> the same, and having a CONFIG_CROSSCOMPILE thing for that?

... and maybe we can just make '-march=native' be a generic setting
and *not* make it x86-specific?

Although it appears that at least on arm, it should do '-mtune=native'
in addition to -march.

                 Linus

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
  2026-11-16 15:48     ` Linus Torvalds
  2026-11-16 15:53       ` Linus Torvalds
@ 2026-11-16 20:45       ` Nathan Chancellor
  2026-11-16 22:36       ` David Laight
  2 siblings, 0 replies; 7+ messages in thread
From: Nathan Chancellor @ 2026-11-16 20:45 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Ingo Molnar, linux-kernel, linux-tip-commits,
	Borislav Petkov (AMD), stable, x86

On Mon, Nov 16, 2026 at 07:48:34AM -0800, Linus Torvalds wrote:
> On Mon, 16 Nov 2026 at 05:02, Nathan Chancellor <nathan@kernel.org> wrote:
> >
> > On Mon, Nov 16, 2026 at 10:40:01AM +0100, Ingo Molnar wrote:
> > > Can we just limit the feature to when host == target, ie. to not
> > > work it around in such a crude fashion?
> >
> > That was my v1 but Boris didn't like it:
> >
> >   https://lore.kernel.org/20261113-x86-no-march-native-on-non-x86_64-v1-1-cbc8b096d1c4@kernel.org
> 
> Well, that is admittedly very crude and hacky.

I guess that's one way to describe it.

> Maybe we should have some config variables that depend on HOSTCC, and
> detect it *that* way. It does appear that we don't have any model for
> cross-compiling in the Kconfig setup, but maybe  we should..
> 
> I'd actually love to have support for making the compiler itself part
> of Kconfig, so that the gcc-vs-clang could be *in* the .config file
> rather than being an external thing to it.

I guess you mean having CC and HOSTCC and friends as values in Kconfig?

> I've mentioned that before, and the problem has typically been that we
> end up depending on 'CC' outside of the Kconfig scripts and
> independently of the current config.
> 
> The recent work to move the compiler flags into Kconfig itself may
> have lessened some of those dependencies, but there's probably still
> lots of small details.
> 
> But for the whole "cross-compile", maybe we can just say that
> X86_NATIVE_CPU depends on '$CC' being the same as '$HOSTCC'.

Unfortunately, that would not for the LLVM case that the commit message
brings up since HOSTCC == CC (we switch targets using '--target'),

> Or even do '$(CC) -v" and '$(HOSTCC) -v' and check for 'Target' being
> the same, and having a CONFIG_CROSSCOMPILE thing for that?

Hmmm, not a bad idea. That does seems to work based on my initial tests,
which would probably be the most generic way to solve this? I am not
sure I want to extrapolate '-march=native' out of x86 right now since I
think architecture maintainers should have the option to support it or
not. However, this would at least lay the ground work for generically
abstracting it (hopefully making Boris happy) and allowing it to still
be enabled in the allmodconfig case (hopefully making Ingo happy) while
still resolving my original issue.

diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
index 6e7a366f0798..4643b3482d94 100644
--- a/arch/x86/Kconfig.cpu
+++ b/arch/x86/Kconfig.cpu
@@ -217,6 +217,7 @@ config RUSTC_HAS_APXF
 config X86_NATIVE_CPU
 	bool "Build and optimize for local/native CPU"
 	depends on X86_64
+	depends on !CROSS_COMPILING
 	depends on CC_HAS_MARCH_NATIVE
 	depends on !RUST || RUSTC_HAS_APXF
 	help
diff --git a/scripts/Kconfig.toolchain b/scripts/Kconfig.toolchain
index 2acbd40a38f1..ac2e3de50ac0 100644
--- a/scripts/Kconfig.toolchain
+++ b/scripts/Kconfig.toolchain
@@ -32,6 +32,9 @@ config CLANG_VERSION
 	default $(cc-version) if CC_IS_CLANG
 	default 0
 
+config CROSS_COMPILING
+	def_bool $(success,$(srctree)/scripts/check-cross-compiling.sh "$(HOSTCC)" "$(CC) $(CLANG_FLAGS)")
+
 config AS_IS_GNU
 	def_bool $(success,test "$(as-name)" = GNU)
 
diff --git a/scripts/check-cross-compiling.sh b/scripts/check-cross-compiling.sh
new file mode 100755
index 000000000000..fd1af38926a9
--- /dev/null
+++ b/scripts/check-cross-compiling.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+hostcc="$1"
+cc="$2"
+
+get_target() {
+    "$@" -v 2>&1 | grep -o 'Target: \([^-]\+\)'
+}
+if ! hostcc_target=$(get_target $hostcc); then
+    echo >&2 "Could not get target for HOSTCC!"
+    return 1
+fi
+if ! cc_target=$(get_target $cc); then
+    echo >&2 "Could not get target for CC!"
+    return 1
+fi
+
+test "$hostcc_target" != "$cc_target"
-- 
Cheers,
Nathan

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing
  2026-11-16 15:48     ` Linus Torvalds
  2026-11-16 15:53       ` Linus Torvalds
  2026-11-16 20:45       ` Nathan Chancellor
@ 2026-11-16 22:36       ` David Laight
  2 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2026-11-16 22:36 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Nathan Chancellor, Ingo Molnar, linux-kernel, linux-tip-commits,
	Borislav Petkov (AMD), stable, x86

On Mon, 16 Nov 2026 07:48:34 -0800
Linus Torvalds <torvalds@linux-foundation.org> wrote:

> On Mon, 16 Nov 2026 at 05:02, Nathan Chancellor <nathan@kernel.org> wrote:
> >
> > On Mon, Nov 16, 2026 at 10:40:01AM +0100, Ingo Molnar wrote:  
> > > Can we just limit the feature to when host == target, ie. to not
> > > work it around in such a crude fashion?  
> >
> > That was my v1 but Boris didn't like it:
> >
> >   https://lore.kernel.org/20261113-x86-no-march-native-on-non-x86_64-v1-1-cbc8b096d1c4@kernel.org  
> 
> Well, that is admittedly very crude and hacky.
> 
> Maybe we should have some config variables that depend on HOSTCC, and
> detect it *that* way. It does appear that we don't have any model for
> cross-compiling in the Kconfig setup, but maybe  we should..
> 
> I'd actually love to have support for making the compiler itself part
> of Kconfig, so that the gcc-vs-clang could be *in* the .config file
> rather than being an external thing to it.
> 
> I've mentioned that before, and the problem has typically been that we
> end up depending on 'CC' outside of the Kconfig scripts and
> independently of the current config.

I don't even remember a check that the compiler is right for the architecture.
I'm sure I've got strange errors when I've forgotten I need to specify the
compiler for a cross build - and then forgotten where I'd put the compiler,
Oh and forgotten all the things you need to set to get clang to do a
cross compile.

David 

> 
> The recent work to move the compiler flags into Kconfig itself may
> have lessened some of those dependencies, but there's probably still
> lots of small details.
> 
> But for the whole "cross-compile", maybe we can just say that
> X86_NATIVE_CPU depends on '$CC' being the same as '$HOSTCC'.
> 
> Or even do '$(CC) -v" and '$(HOSTCC) -v' and check for 'Target' being
> the same, and having a CONFIG_CROSSCOMPILE thing for that?
> 
>                Linus
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-16 22:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  6:05 [tip: x86/urgent] x86/build: Do not allow X86_NATIVE_CPU to be enabled when compile testing tip-bot2 for Nathan Chancellor
2026-11-16  9:40 ` Ingo Molnar
2026-11-16 13:01   ` Nathan Chancellor
2026-11-16 15:48     ` Linus Torvalds
2026-11-16 15:53       ` Linus Torvalds
2026-11-16 20:45       ` Nathan Chancellor
2026-11-16 22:36       ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox