* [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions
@ 2026-08-07 15:17 Chang S. Bae
2026-08-07 16:58 ` Miguel Ojeda
0 siblings, 1 reply; 6+ messages in thread
From: Chang S. Bae @ 2026-08-07 15:17 UTC (permalink / raw)
To: linux-kernel
Cc: x86, rust-for-linux, tglx, mingo, bp, dave.hansen, hpa, ojeda,
nathan, boqun, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
tmgross, dakr, daniel.almeida, tamird, acourbot, work,
chang.seok.bae, Omar Avelar, stable
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 the commit.
But 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.
For C, GCC 14 and Clang 18 both support disabling APX with -mno-apxf,
whose availability can be detected via cc-option.
For Rust, APX must be explicitly disabled because any backend supporting
APX may otherwise emit APX instructions with target-cpu=native. Pass
features=-apxf through the generated JSON to avoid unstable-feature
warnings.
Support for this gating also depends on the Rust/LLVM combination. Rust
1.88 introduced the `apxf` feature option, but versions prior to 1.93 may
emit an `apxf` attribute which LLVM 23 or later can interpret. Restrict
native Rust builds accordingly.
Fixes: ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'")
Reported-by: Omar Avelar <omar.avelar@intel.com>
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Cc: <stable@vger.kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
---
V1 -> V2:
* Use the JSON generator, and specify Rust/LLVM versions (Miguel Ojeda)
* Lean into cc-option simply, and add a review tag (Nathan Chancellor)
* I also ended up adding Fixes: just for easy backporting.
---
arch/x86/Kconfig.cpu | 11 +++++++++++
arch/x86/Makefile | 5 +++++
scripts/generate_rust_target.rs | 5 +++++
3 files changed, 21 insertions(+)
diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu
index e4654388d794..6e7a366f0798 100644
--- a/arch/x86/Kconfig.cpu
+++ b/arch/x86/Kconfig.cpu
@@ -204,10 +204,21 @@ config CC_HAS_MARCH_NATIVE
# usage warnings that only appear wth '-march=native'.
depends on CC_IS_GCC || CLANG_VERSION >= 190100
+config RUSTC_HAS_APXF
+ # The kernel isn't ready for in-kernel APX instructions. Without
+ # explicit frontend gating of APX, the backend may emit those
+ # instructions in native builds.
+ #
+ # Rust 1.88 added the `apxf` feature option, but versions before 1.93
+ # emit an `apxf` target attribute that only LLVM 23+ can interpret.
+ def_bool (RUSTC_VERSION >= 108800 && RUSTC_LLVM_MAJOR_VERSION >= 23) || \
+ RUSTC_VERSION >= 109300
+
config X86_NATIVE_CPU
bool "Build and optimize for local/native CPU"
depends on X86_64
depends on CC_HAS_MARCH_NATIVE
+ depends on !RUST || RUSTC_HAS_APXF
help
Optimize for the current CPU used to compile the kernel.
Use this option if you intend to build the kernel for your
diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 598f178102ee..866912ed2ec7 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -161,6 +161,11 @@ else
ifdef CONFIG_X86_NATIVE_CPU
KBUILD_CFLAGS += -march=native
+ # Prevent the compiler from generating APX instructions. The kernel is
+ # not yet prepared for general in-kernel APX use.
+ KBUILD_CFLAGS += $(call cc-option,-mno-apxf)
+
+ # generate_rust_target.rs handles Rust APX gating
KBUILD_RUSTFLAGS += -Ctarget-cpu=native
else
KBUILD_CFLAGS += -march=x86-64 -mtune=generic
diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs
index 3bf296581a88..e0f9714ebdea 100644
--- a/scripts/generate_rust_target.rs
+++ b/scripts/generate_rust_target.rs
@@ -224,6 +224,11 @@ fn main() {
features += ",+harden-sls-ijmp";
features += ",+harden-sls-ret";
}
+ if cfg.has("X86_NATIVE_CPU") {
+ // Prevent the backend from generating APX instructions. The kernel is not yet prepared
+ // for general in-kernel APX use.
+ features += ",-apxf";
+ }
ts.push("features", features);
ts.push("llvm-target", "x86_64-linux-gnu");
ts.push("supported-sanitizers", ["kcfi", "kernel-address"]);
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions
2026-08-07 15:17 [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions Chang S. Bae
@ 2026-08-07 16:58 ` Miguel Ojeda
2026-08-08 3:21 ` Chang S. Bae
0 siblings, 1 reply; 6+ messages in thread
From: Miguel Ojeda @ 2026-08-07 16:58 UTC (permalink / raw)
To: Chang S. Bae
Cc: linux-kernel, x86, rust-for-linux, tglx, mingo, bp, dave.hansen,
hpa, ojeda, nathan, boqun, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, daniel.almeida, tamird, acourbot, work,
Omar Avelar, stable
On Fri, Aug 7, 2026 at 5:43 PM Chang S. Bae <chang.seok.bae@intel.com> wrote:
>
> 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 the commit.
I think this sentence may be cut? I guess this refers to the commit in
the Fixes tag.
> For Rust, APX must be explicitly disabled because any backend supporting
> APX may otherwise emit APX instructions with target-cpu=native. Pass
> features=-apxf through the generated JSON to avoid unstable-feature
> warnings.
So, just to confirm: is Rust 1.88 (with a new enough LLVM) enough to
avoid the warnings? Maybe a link to the tracking issue would be nice:
https://github.com/rust-lang/rust/issues/139284
> Support for this gating also depends on the Rust/LLVM combination. Rust
> 1.88 introduced the `apxf` feature option, but versions prior to 1.93 may
> emit an `apxf` attribute which LLVM 23 or later can interpret. Restrict
> native Rust builds accordingly.
>
> Fixes: ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'")
This looks like it exists in 6.18.y+, so I assume this should have:
Cc: stable@vger.kernel.org
Thanks!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions
2026-08-07 16:58 ` Miguel Ojeda
@ 2026-08-08 3:21 ` Chang S. Bae
2026-08-10 15:24 ` Miguel Ojeda
0 siblings, 1 reply; 6+ messages in thread
From: Chang S. Bae @ 2026-08-08 3:21 UTC (permalink / raw)
To: Miguel Ojeda
Cc: linux-kernel, x86, rust-for-linux, tglx, mingo, bp, dave.hansen,
hpa, ojeda, nathan, boqun, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, daniel.almeida, tamird, acourbot, work,
Omar Avelar, stable
On 8/7/2026 9:58 AM, Miguel Ojeda wrote:
> On Fri, Aug 7, 2026 at 5:43 PM Chang S. Bae <chang.seok.bae@intel.com> wrote:
>>
>> 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 the commit.
>
> I think this sentence may be cut? I guess this refers to the commit in
> the Fixes tag.
Yes, I intended “the commit” to refer to the one in the Fixes: tag. To
make it explicit, I can add the commit back here:
ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU ...")
>> For Rust, APX must be explicitly disabled because any backend supporting
>> APX may otherwise emit APX instructions with target-cpu=native. Pass
>> features=-apxf through the generated JSON to avoid unstable-feature
>> warnings.
>
> So, just to confirm: is Rust 1.88 (with a new enough LLVM) enough to
> avoid the warnings? Maybe a link to the tracking issue would be nice:
>
> https://github.com/rust-lang/rust/issues/139284
Passing `-apxf` through the generated target JSON avoids this warning:
warning: unstable feature specified for `-Ctarget-feature`: `apxf`
|
= note: this feature is not stably supported; its behavior can change
in the future
I’ll add the link to reference the status of the Rust support. Thanks.
>> Support for this gating also depends on the Rust/LLVM combination. Rust
>> 1.88 introduced the `apxf` feature option, but versions prior to 1.93 may
>> emit an `apxf` attribute which LLVM 23 or later can interpret. Restrict
>> native Rust builds accordingly.
>>
>> Fixes: ea1dcca1de12 ("x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'")
>
> This looks like it exists in 6.18.y+, so I assume this should have:
>
> Cc: stable@vger.kernel.org
Yes, I have included it.
Thanks,
Chang
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions
2026-08-08 3:21 ` Chang S. Bae
@ 2026-08-10 15:24 ` Miguel Ojeda
2026-09-03 5:23 ` Borislav Petkov
0 siblings, 1 reply; 6+ messages in thread
From: Miguel Ojeda @ 2026-08-10 15:24 UTC (permalink / raw)
To: Chang S. Bae
Cc: linux-kernel, x86, rust-for-linux, tglx, mingo, bp, dave.hansen,
hpa, ojeda, nathan, boqun, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, daniel.almeida, tamird, acourbot, work,
Omar Avelar, stable
On Sat, Aug 8, 2026 at 5:21 AM Chang S. Bae <chang.seok.bae@intel.com> wrote:
>
> Yes, I intended “the commit” to refer to the one in the Fixes: tag. To
> make it explicit, I can add the commit back here:
> I’ll add the link to reference the status of the Rust support. Thanks.
> Yes, I have included it.
Thanks! With those:
Acked-by: Miguel Ojeda <ojeda@kernel.org>
I guess this would go through the x86 tree? Otherwise, please let me know.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions
2026-08-10 15:24 ` Miguel Ojeda
@ 2026-09-03 5:23 ` Borislav Petkov
2026-09-03 14:10 ` Chang S. Bae
0 siblings, 1 reply; 6+ messages in thread
From: Borislav Petkov @ 2026-09-03 5:23 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Chang S. Bae, linux-kernel, x86, rust-for-linux, tglx, mingo,
dave.hansen, hpa, ojeda, nathan, boqun, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, daniel.almeida, tamird,
acourbot, work, Omar Avelar, stable
On Mon, Aug 10, 2026 at 05:24:55PM +0200, Miguel Ojeda wrote:
> On Sat, Aug 8, 2026 at 5:21 AM Chang S. Bae <chang.seok.bae@intel.com> wrote:
> >
> > Yes, I intended “the commit” to refer to the one in the Fixes: tag. To
> > make it explicit, I can add the commit back here:
>
> > I’ll add the link to reference the status of the Rust support. Thanks.
>
> > Yes, I have included it.
>
> Thanks! With those:
>
> Acked-by: Miguel Ojeda <ojeda@kernel.org>
>
> I guess this would go through the x86 tree? Otherwise, please let me know.
I can take a v2 with feedback from this thread integrated.
Thx.
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions
2026-09-03 5:23 ` Borislav Petkov
@ 2026-09-03 14:10 ` Chang S. Bae
0 siblings, 0 replies; 6+ messages in thread
From: Chang S. Bae @ 2026-09-03 14:10 UTC (permalink / raw)
To: Borislav Petkov, Miguel Ojeda
Cc: linux-kernel, x86, rust-for-linux, tglx, mingo, dave.hansen, hpa,
ojeda, nathan, boqun, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, daniel.almeida, tamird, acourbot, work,
Omar Avelar, stable
On 9/2/2026 10:23 PM, Borislav Petkov wrote:
>
> I can take a v2 with feedback from this thread integrated.
>
Thanks for the consideration! But there is one more thing to do:
Recently we found a sub-option in both compilers to disable EGPR use
only while allowing other APX features. Let me conclude this in V3.
Sorry, I should have commented this here.
Thanks,
Chang
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-09-03 14:11 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-07 15:17 [PATCH v2] x86/build/64: Prevent native builds from generating APX instructions Chang S. Bae
2026-08-07 16:58 ` Miguel Ojeda
2026-08-08 3:21 ` Chang S. Bae
2026-08-10 15:24 ` Miguel Ojeda
2026-09-03 5:23 ` Borislav Petkov
2026-09-03 14:10 ` Chang S. Bae
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox