From a960c2cdb19e237f284a3b96eb8a0359abcf0e63 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 31 Mar 2026 10:57:49 +0000 Subject: kbuild: rust: add AutoFDO support This patch enables AutoFDO build support for Rust code within the Linux kernel. This allows Rust code to be profiled and optimized based on the profile. The RUSTFLAGS variable was suffixed with *_AUTOFDO_CLANG to match the naming of the config option, which is called CONFIG_AUTOFDO_CLANG. This implementation has been verified in Android, first by inspecting the object files and confirming that they look correct. After that, it was verified as below: 1. Running the binderAddInts benchmark [1] with Rust Binder built as rust_binder.ko module, using a Pixel 9 Pro. 2. Collecting a profile on a Pixel 10 Pro XL using the app-launch benchmark, which starts different apps many times, on a device with Rust Binder as a built-in kernel module. (C Binder was not present on the device.) 3. Using the collected profile, run the binderAddInts benchmark again with Rust Binder built both as a rust_binder.ko module, and as a built-in kernel module. 4. In both cases, Rust Binder without AutoFDO was approximately 13% slower than the AutoFDO optimized version. Built-in vs .ko did not make a measurable performance difference. All of the above was verified in conjunction with my helpers inlining series [2], which confirmed that this worked correctly for helpers too once [3] was fixed in the helpers inlining series. Link: https://android.googlesource.com/platform/system/extras/+/920f089/tests/binder/benchmarks/binderAddInts.cpp [1] Link: https://lore.kernel.org/r/20260203-inline-helpers-v2-0-beb8547a03c9@google.com [2] Link: https://lore.kernel.org/r/aasPsbMEsX6iGUl8@google.com [3] Reviewed-by: Rong Xu Reviewed-by: Gary Guo Tested-by: Alice Ryhl Signed-off-by: Alice Ryhl Acked-by: Nicolas Schier Acked-by: Nathan Chancellor Link: https://patch.msgid.link/20260331-autofdo-v2-1-eb5c5964820d@google.com [ Reworded for typos. - Miguel ] Signed-off-by: Miguel Ojeda --- scripts/Makefile.autofdo | 6 +++++- scripts/Makefile.lib | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'scripts') diff --git a/scripts/Makefile.autofdo b/scripts/Makefile.autofdo index 1caf2457e585..3f08acab4549 100644 --- a/scripts/Makefile.autofdo +++ b/scripts/Makefile.autofdo @@ -3,14 +3,18 @@ # Enable available and selected Clang AutoFDO features. CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminator=true -mllvm -improved-fs-discriminator=true +RUSTFLAGS_AUTOFDO_CLANG := -Zdebug-info-for-profiling -Cllvm-args=-enable-fs-discriminator=true -Cllvm-args=-improved-fs-discriminator=true ifndef CONFIG_DEBUG_INFO CFLAGS_AUTOFDO_CLANG += -gmlt + RUSTFLAGS_AUTOFDO_CLANG += -Cdebuginfo=line-tables-only endif ifdef CLANG_AUTOFDO_PROFILE CFLAGS_AUTOFDO_CLANG += -fprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -ffunction-sections CFLAGS_AUTOFDO_CLANG += -fsplit-machine-functions + RUSTFLAGS_AUTOFDO_CLANG += -Zprofile-sample-use=$(CLANG_AUTOFDO_PROFILE) -Zfunction-sections=y + RUSTFLAGS_AUTOFDO_CLANG += -Cllvm-args=-split-machine-functions endif ifdef CONFIG_LTO_CLANG_THIN @@ -21,4 +25,4 @@ ifdef CONFIG_LTO_CLANG_THIN KBUILD_LDFLAGS += -plugin-opt=-split-machine-functions endif -export CFLAGS_AUTOFDO_CLANG +export CFLAGS_AUTOFDO_CLANG RUSTFLAGS_AUTOFDO_CLANG diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 0718e39cedda..eaddf6637669 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -123,6 +123,9 @@ ifeq ($(CONFIG_AUTOFDO_CLANG),y) _c_flags += $(if $(patsubst n%,, \ $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \ $(CFLAGS_AUTOFDO_CLANG)) +_rust_flags += $(if $(patsubst n%,, \ + $(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \ + $(RUSTFLAGS_AUTOFDO_CLANG)) endif # -- cgit v1.2.3 From 72d33b8bfeacbfdccf2cda4650e8e002def036f8 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 8 Apr 2026 08:32:17 +0000 Subject: rust: kasan: add support for Software Tag-Based KASAN This adds support for Software Tag-Based KASAN (KASAN_SW_TAGS) when CONFIG_RUST is enabled. This requires that rustc includes support for the kernel-hwaddress sanitizer, which is available since 1.96.0 [1]. Unlike with clang, we need to pass -Zsanitizer-recover in addition to -Zsanitizer because the option is not implied automatically. The kasan makefile uses different names for the flags depending on whether CC is clang or gcc, but as we require that CC is clang when using KASAN, we do not need to try to handle mixed gcc/llvm builds when Rust is enabled. Link: https://github.com/rust-lang/rust/pull/153049 [1] Reviewed-by: Danilo Krummrich Signed-off-by: Alice Ryhl Reviewed-by: Gary Guo Link: https://patch.msgid.link/20260408-kasan-rust-sw-tags-v3-2-e07964d14363@google.com Signed-off-by: Miguel Ojeda --- init/Kconfig | 2 +- scripts/Makefile.kasan | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'scripts') diff --git a/init/Kconfig b/init/Kconfig index 826a7d768ca3..7ef3fa222ca3 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -2199,7 +2199,7 @@ config RUST depends on !CFI || HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC select CFI_ICALL_NORMALIZE_INTEGERS if CFI depends on !KASAN || CC_IS_CLANG - depends on !KASAN_SW_TAGS + depends on !KASAN_SW_TAGS || RUSTC_VERSION >= 109600 help Enables Rust support in the kernel. diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index 0ba2aac3b8dc..91504e81247a 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -71,8 +71,6 @@ ifdef CONFIG_KASAN_SW_TAGS CFLAGS_KASAN := -fsanitize=kernel-hwaddress -# This sets flags that will enable SW_TAGS KASAN once enabled in Rust. These -# will not work today, and is guarded against in dependencies for CONFIG_RUST. RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress \ -Zsanitizer-recover=kernel-hwaddress -- cgit v1.2.3 From 7594302d9ddd9f198173a658250e7b7debc6ed76 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Tue, 2 Jun 2026 17:16:38 +0200 Subject: kbuild: rust: rename flag to `-Zdebuginfo-for-profiling` for Rust >= 1.98 Starting with Rust 1.98.0 (expected 2026-08-20), the `-Zdebug-info-for-profiling` flag has been renamed to `-Zdebuginfo-for-profiling` (i.e. one less dash, to match `debuginfo`s in other flags) [1]. Without this change, one gets in the latest nightlies: error: unknown unstable option: `debug-info-for-profiling` Thus pass the right name. Link: https://github.com/rust-lang/rust/pull/156887 [1] Reviewed-by: Alice Ryhl Acked-by: Nathan Chancellor Link: https://patch.msgid.link/20260602151638.14358-1-ojeda@kernel.org Signed-off-by: Miguel Ojeda --- scripts/Makefile.autofdo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scripts') diff --git a/scripts/Makefile.autofdo b/scripts/Makefile.autofdo index 3f08acab4549..1442043da139 100644 --- a/scripts/Makefile.autofdo +++ b/scripts/Makefile.autofdo @@ -3,7 +3,7 @@ # Enable available and selected Clang AutoFDO features. CFLAGS_AUTOFDO_CLANG := -fdebug-info-for-profiling -mllvm -enable-fs-discriminator=true -mllvm -improved-fs-discriminator=true -RUSTFLAGS_AUTOFDO_CLANG := -Zdebug-info-for-profiling -Cllvm-args=-enable-fs-discriminator=true -Cllvm-args=-improved-fs-discriminator=true +RUSTFLAGS_AUTOFDO_CLANG := $(if $(call rustc-min-version,109800),-Zdebuginfo-for-profiling,-Zdebug-info-for-profiling) -Cllvm-args=-enable-fs-discriminator=true -Cllvm-args=-improved-fs-discriminator=true ifndef CONFIG_DEBUG_INFO CFLAGS_AUTOFDO_CLANG += -gmlt -- cgit v1.2.3 From 8b1549eee9c7f27b4f4f7c7f575c3c50065b89ff Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Mon, 8 Jun 2026 16:14:20 +0200 Subject: scripts: generate_rust_analyzer: support passing env vars A future commit adding `zerocopy` support will need to pass an environment variable during its build. Thus add support for an `--envs` parameter, similar to `--cfgs`, that allows to pass a map of variables to set for a given crate. This allows us to keep a single source of truth for those values. No change intended in the generated `rust-project.json`. Acked-by: Tamir Duberstein Link: https://patch.msgid.link/20260608141439.182634-2-ojeda@kernel.org Signed-off-by: Miguel Ojeda --- scripts/generate_rust_analyzer.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'scripts') diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py index d5f9a0ca742c..2477209e1e94 100755 --- a/scripts/generate_rust_analyzer.py +++ b/scripts/generate_rust_analyzer.py @@ -26,6 +26,14 @@ def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]: return crates_cfgs +def args_crates_envs(envs: List[str]) -> Dict[str, Dict[str, str]]: + crates_envs = {} + for env in envs: + crate, vals = env.split("=", 1) + crates_envs[crate] = dict(v.split("=", 1) for v in vals.split()) + + return crates_envs + class Dependency(TypedDict): crate: int name: str @@ -61,6 +69,7 @@ def generate_crates( sysroot_src: pathlib.Path, external_src: Optional[pathlib.Path], cfgs: List[str], + envs: List[str], core_edition: str, ) -> List[Crate]: # Generate the configuration list. @@ -74,6 +83,7 @@ def generate_crates( # Now fill the crates list. crates: List[Crate] = [] crates_cfgs = args_crates_cfgs(cfgs) + crates_envs = args_crates_envs(envs) def get_crate_name(path: pathlib.Path) -> str: return invoke_rustc(["--print", "crate-name", str(path)]) @@ -92,6 +102,10 @@ def generate_crates( is_workspace_member if is_workspace_member is not None else True ) edition = edition if edition is not None else "2021" + crate_env = { + "RUST_MODFILE": "This is only for rust-analyzer", + **crates_envs.get(display_name, {}), + } return { "display_name": display_name, "root_module": str(root_module), @@ -99,9 +113,7 @@ def generate_crates( "deps": deps, "cfg": cfg, "edition": edition, - "env": { - "RUST_MODFILE": "This is only for rust-analyzer" - } + "env": crate_env, } def append_proc_macro_crate( @@ -347,6 +359,7 @@ def main() -> None: parser = argparse.ArgumentParser() parser.add_argument('--verbose', '-v', action='store_true') parser.add_argument('--cfgs', action='append', default=[]) + parser.add_argument('--envs', action='append', default=[]) parser.add_argument("core_edition") parser.add_argument("srctree", type=pathlib.Path) parser.add_argument("objtree", type=pathlib.Path) @@ -357,6 +370,7 @@ def main() -> None: class Args(argparse.Namespace): verbose: bool cfgs: List[str] + envs: List[str] srctree: pathlib.Path objtree: pathlib.Path sysroot: pathlib.Path @@ -372,7 +386,7 @@ def main() -> None: ) rust_project = { - "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.core_edition), + "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.envs, args.core_edition), "sysroot": str(args.sysroot), } -- cgit v1.2.3 From 567621523ab7a2bc4a923757cba30bde8900447a Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Mon, 8 Jun 2026 16:14:31 +0200 Subject: rust: zerocopy: enable support in kbuild With all the new files in place and ready from the new crate, enable the support for it in the build system. In addition, skip formatting for this vendored crate. Finally, there are no generated symbols expected from `zerocopy`, thus skip adding the `exports` generation. Link: https://patch.msgid.link/20260608141439.182634-13-ojeda@kernel.org Signed-off-by: Miguel Ojeda --- Makefile | 1 + rust/Makefile | 45 +++++++++++++++++++++++++++++++++------ scripts/Makefile.build | 1 + scripts/generate_rust_analyzer.py | 10 +++++++-- 4 files changed, 49 insertions(+), 8 deletions(-) (limited to 'scripts') diff --git a/Makefile b/Makefile index 3a265e7e3347..33135f39bbee 100644 --- a/Makefile +++ b/Makefile @@ -1956,6 +1956,7 @@ rustfmt: -path $(srctree)/rust/proc-macro2 \ -o -path $(srctree)/rust/quote \ -o -path $(srctree)/rust/syn \ + -o -path $(srctree)/rust/zerocopy \ \) -prune -o \ -type f -a -name '*.rs' -a ! -name '*generated*' -print \ | xargs $(RUSTFMT) $(rustfmt_flags) diff --git a/rust/Makefile b/rust/Makefile index 7b1db0d5d423..a87b33926cdf 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -6,6 +6,8 @@ rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o always-$(CONFIG_RUST) += exports_core_generated.h +obj-$(CONFIG_RUST) += zerocopy.o + ifdef CONFIG_RUST_INLINE_HELPERS always-$(CONFIG_RUST) += helpers/helpers.bc helpers/helpers_module.bc else @@ -81,6 +83,12 @@ core-flags := \ --edition=$(core-edition) \ $(call cfgs-to-flags,$(core-cfgs)) +zerocopy-flags := \ + --cap-lints=allow + +zerocopy-envs := \ + CARGO_PKG_VERSION=0.8.50 + proc_macro2-cfgs := \ feature="proc-macro" \ wrap_proc_macro \ @@ -167,7 +175,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $< # command-like flags to solve the issue. Meanwhile, we use the non-custom case # and then retouch the generated files. rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \ - rustdoc-kernel rustdoc-pin_init + rustdoc-kernel rustdoc-pin_init rustdoc-zerocopy $(Q)grep -Ehro ' rust-project.json @@ -670,6 +694,13 @@ $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE +$(call if_changed_rule,rustc_library) +$(obj)/zerocopy.o: private skip_clippy = 1 +$(obj)/zerocopy.o: private skip_gendwarfksyms = 1 +$(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs) +$(obj)/zerocopy.o: private rustc_target_flags = $(zerocopy-flags) +$(obj)/zerocopy.o: $(src)/zerocopy/src/lib.rs $(obj)/compiler_builtins.o FORCE + +$(call if_changed_rule,rustc_library) + $(obj)/pin_init.o: private skip_gendwarfksyms = 1 $(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags) $(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \ @@ -705,9 +736,11 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \ +$(call if_changed_rule,rustc_library) $(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \ - --extern build_error --extern macros --extern bindings --extern uapi + --extern build_error --extern macros --extern bindings --extern uapi \ + --extern zerocopy $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \ - $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE + $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \ + $(obj)/zerocopy.o FORCE +$(call if_changed_rule,rustc_library) ifdef CONFIG_JUMP_LABEL diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 3498d25b15e8..ddf0461dda6a 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -329,6 +329,7 @@ rust_common_cmd = \ -Zcrate-attr=no_std \ -Zcrate-attr='feature($(rust_allowed_features))' \ -Zunstable-options --extern pin_init --extern kernel \ + --extern zerocopy \ --crate-type rlib -L $(objtree)/rust/ \ --sysroot=/dev/null \ --out-dir $(dir $@) --emit=dep-info=$(depfile) diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py index 2477209e1e94..80f7647f633a 100755 --- a/scripts/generate_rust_analyzer.py +++ b/scripts/generate_rust_analyzer.py @@ -276,6 +276,12 @@ def generate_crates( [core, compiler_builtins], ) + zerocopy = append_crate( + "zerocopy", + srctree / "rust" / "zerocopy" / "src" / "lib.rs", + [core, compiler_builtins], + ) + def append_crate_with_generated( display_name: str, deps: List[Dependency], @@ -304,7 +310,7 @@ def generate_crates( bindings = append_crate_with_generated("bindings", [core, ffi, pin_init]) uapi = append_crate_with_generated("uapi", [core, ffi, pin_init]) kernel = append_crate_with_generated( - "kernel", [core, macros, build_error, pin_init, ffi, bindings, uapi] + "kernel", [core, macros, build_error, pin_init, ffi, bindings, uapi, zerocopy] ) scripts = srctree / "scripts" @@ -349,7 +355,7 @@ def generate_crates( append_crate( crate_name, path, - [core, kernel, pin_init], + [core, kernel, pin_init, zerocopy], cfg=generated_cfg, ) -- cgit v1.2.3 From 506054980429497d106261568bd39c22a40e0fdd Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Mon, 8 Jun 2026 16:14:36 +0200 Subject: rust: zerocopy-derive: enable support in kbuild With all the new files in place and ready from the new crate, enable the support for it in the build system. In addition, skip formatting for this vendored crate. Link: https://patch.msgid.link/20260608141439.182634-18-ojeda@kernel.org Signed-off-by: Miguel Ojeda --- Makefile | 1 + rust/Makefile | 41 +++++++++++++++++++++++++++++++-------- scripts/Makefile.build | 2 +- scripts/generate_rust_analyzer.py | 10 ++++++++-- 4 files changed, 43 insertions(+), 11 deletions(-) (limited to 'scripts') diff --git a/Makefile b/Makefile index 33135f39bbee..c71c43bc3658 100644 --- a/Makefile +++ b/Makefile @@ -1957,6 +1957,7 @@ rustfmt: -o -path $(srctree)/rust/quote \ -o -path $(srctree)/rust/syn \ -o -path $(srctree)/rust/zerocopy \ + -o -path $(srctree)/rust/zerocopy-derive \ \) -prune -o \ -type f -a -name '*.rs' -a ! -name '*generated*' -print \ | xargs $(RUSTFMT) $(rustfmt_flags) diff --git a/rust/Makefile b/rust/Makefile index a87b33926cdf..2fbdebb93bf2 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -52,10 +52,11 @@ ifdef CONFIG_RUST procmacro-name = $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name $(1) --crate-type proc-macro -