Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/kbuild/64: Add the CONFIG_X86_NATIVE_CPU option to locally optimize the kernel with '-march=native'

Add a 'native' option that allows users to build an optimized kernel for
their local machine (i.e. the machine which is used to build the kernel)
by passing '-march=native' to CFLAGS.

The idea comes from Linus' reply to Arnd's initial proposal:

https://lore.kernel.org/all/CAHk-=wji1sV93yKbc==Z7OSSHBiDE=LAdG_d5Y-zPBrnSs0k2A@mail.gmail.com/

Here are some numbers comparing 'generic' to 'native' on a Skylake dual-core
laptop (generic --> native):

- vmlinux and compressed modules size:
125'907'744 bytes --> 125'595'280 bytes (-0.248 %)
18'810 kilobytes --> 18'770 kilobytes (-0.213 %)

- phoronix, average of 3 runs:
ffmpeg:
130.99 --> 131.15 (+0.122 %)
nginx:
10'650 --> 10'725 (+0.704 %)
hackbench (lower is better):
102.27 --> 99.50 (-2.709 %)

- xz compression of firefox tarball (lower is better):
319.57 seconds --> 317.34 seconds (-0.698 %)

- stress-ng, bogoops, average of 3 15-second runs:
fork:
111'744 --> 115'509 (+3.397 %)
bsearch:
7'211 --> 7'436 (+3.120 %)
memfd:
3'591 --> 3'604 (+0.362 %)
mmapfork:
630 --> 629 (-0.159 %)
schedmix:
42'715 --> 43'251 (+1.255 %)
epoll:
2'443'767 --> 2'454'413 (+0.436 %)
vm:
1'442'256 --> 1'486'615 (+3.076 %)

- schbench (two message threads), 30-second runs:
304 rps --> 305 rps (+0.329 %)

There is little difference both in terms of size and of performance, however
the native build comes out on top ever so slightly.

[ mingo: Renamed the option to CONFIG_X86_NATIVE_CPU, expanded the help text
and added Linus's Suggested-by tag. ]

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Tor Vic <torvic9@mailbox.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Link: https://lore.kernel.org/r/20250321142859.13889-1-torvic9@mailbox.org

authored by

Tor Vic and committed by
Ingo Molnar
ea1dcca1 2df0c02d

+19
+14
arch/x86/Kconfig.cpu
··· 245 245 246 246 endchoice 247 247 248 + config X86_NATIVE_CPU 249 + bool "Build and optimize for local/native CPU" 250 + depends on X86_64 251 + default n 252 + help 253 + Optimize for the current CPU used to compile the kernel. 254 + Use this option if you intend to build the kernel for your 255 + local machine. 256 + 257 + Note that such a kernel might not work optimally on a 258 + different x86 machine. 259 + 260 + If unsure, say N. 261 + 248 262 config X86_GENERIC 249 263 bool "Generic x86 support" 250 264 depends on X86_32
+5
arch/x86/Makefile
··· 173 173 # Use -mskip-rax-setup if supported. 174 174 KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup) 175 175 176 + ifdef CONFIG_X86_NATIVE_CPU 177 + KBUILD_CFLAGS += -march=native 178 + KBUILD_RUSTFLAGS += -Ctarget-cpu=native 179 + else 176 180 KBUILD_CFLAGS += -march=x86-64 -mtune=generic 177 181 KBUILD_RUSTFLAGS += -Ctarget-cpu=x86-64 -Ztune-cpu=generic 182 + endif 178 183 179 184 KBUILD_CFLAGS += -mno-red-zone 180 185 KBUILD_CFLAGS += -mcmodel=kernel