[PATCH] x86/build/64: Prevent native builds from generating APX instructions
From: Chang S. Bae
Date: Wed Jul 08 2026 - 17:45:07 EST
Omar reported this broad concern to me, when resolving a separate issue
with his custom module. CONFIG_X86_NATIVE_CPU=y allows builds to
opportunistically emit APX instructions when the build host supports APX
since commit:
ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'")
The kernel is not yet prepared to use APX internally. For example, there
is no context-switch support for general in-kernel use of the extended
GPRs.
Explicitly disable APX when building with `-march=native`.
Since GCC 14 and LLVM 18, both compilers support APX. LLVM 19 is already
the minimum version to support native builds from:
ad9b861824ac ("x86/kbuild/64: Restrict clang versions that can use '-march=native'")
RUST supports APX detection via XCR0 since v1.91 release. While the
support is not official yet, conservatively set that version as the
minimum.
Reported-by: Omar Avelar <omar.avelar@xxxxxxxxx>
Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
Cc: <stable@xxxxxxxxxxxxxxx> # v6.16+
Cc: Miguel Ojeda <ojeda@xxxxxxxxxx>
Cc: Nathan Chancellor <nathan@xxxxxxxxxx>
---
I don't think it is fair to point out that commit to fix here. The issue
looks to be just in a hindsight. It was also merged at the same cycle
when APX userspace enabling was picked up.
Fortunately no APX systems are publicly available yet, but guard against
this before such hardware becomes generally available.
---
arch/x86/Makefile | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 598f178102ee..256948e65073 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -161,7 +161,15 @@ else
ifdef CONFIG_X86_NATIVE_CPU
KBUILD_CFLAGS += -march=native
- KBUILD_RUSTFLAGS += -Ctarget-cpu=native
+ # Do not generate APX instructions as in-kernel use isn't ready
+ ifdef CONFIG_CC_IS_GCC
+ KBUILD_CFLAGS += $(if $(call gcc-min-version,140000),-mno-apxf,)
+ endif
+ ifdef CONFIG_CC_IS_CLANG
+ # The minimum version for native build already supports the option
+ KBUILD_CFLAGS += -mno-apxf
+ endif
+ KBUILD_RUSTFLAGS += -Ctarget-cpu=native $(if $(call rust-min-version,109100),-Ctarget-feature=-apxf,)
else
KBUILD_CFLAGS += -march=x86-64 -mtune=generic
KBUILD_RUSTFLAGS += -Ctarget-cpu=x86-64 -Ztune-cpu=generic
--
2.51.0