From 8ce797daed8d066a8dcec75b26ab2d3955cc8226 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Thu, 8 Oct 2026 11:21:09 +0100 Subject: [PATCH] kernel: support building Rust in the kernel With CONFIG_KERNEL_RUST pass RUSTC, BINDGEN, RUST_LIB_SRC and LIBCLANG_PATH to the kernel build. These are the variables scripts/rust_is_available.sh checks. KERNEL_RUST is enabled by default with any Rust toolchain, selects bindgen and follows the kernel's architecture requirements: ARM32 needs little-endian ARMv7, and ARM32 and LoongArch require KASAN to be disabled. CONFIG_RUST makes further symbols visible, which kconfig asks for because the generic configuration does not answer them. Answer them in the generic configuration so that targets need no changes. Filter the rustc and bindgen version texts out of the kernel configuration comparison. Suggested-by: Andre Heider Signed-off-by: Daniel Golle --- include/kernel.mk | 10 ++++++++++ target/linux/generic/config-6.18 | 11 +++++++++++ target/linux/generic/config-filter | 2 ++ toolchain/Config.in | 16 +++++++++++++++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/kernel.mk b/include/kernel.mk index e6850d105f1..0149cf1b640 100644 --- a/include/kernel.mk +++ b/include/kernel.mk @@ -134,6 +134,16 @@ ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL KERNELRELEASE=$(LINUX_VERSION) endif +ifdef CONFIG_KERNEL_RUST + include $(INCLUDE_DIR)/rust-values.mk + + KERNEL_MAKE_FLAGS += \ + RUSTC="$(RUSTC)" \ + BINDGEN="$(STAGING_DIR_HOST)/bin/bindgen" \ + RUST_LIB_SRC="$(STAGING_DIR_HOST)/lib/rustlib/src/rust/library" \ + LIBCLANG_PATH="$(RUST_LIBCLANG_PATH)" +endif + KERNEL_MAKEOPTS = -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS) ifdef CONFIG_USE_SPARSE diff --git a/target/linux/generic/config-6.18 b/target/linux/generic/config-6.18 index 0f217c437f5..a53f075575e 100644 --- a/target/linux/generic/config-6.18 +++ b/target/linux/generic/config-6.18 @@ -223,6 +223,7 @@ CONFIG_ALLOW_DEV_COREDUMP=y # CONFIG_AMPERE_ERRATUM_AC04_CPU_23 is not set # CONFIG_AMT is not set # CONFIG_ANDROID_BINDER_IPC is not set +# CONFIG_ANDROID_BINDER_IPC_RUST is not set # CONFIG_ANON_VMA_NAME is not set # CONFIG_AOSONG_AGS02MA is not set # CONFIG_APDS9160 is not set @@ -717,6 +718,7 @@ CONFIG_BLK_DEV_LOOP_MIN_COUNT=8 # CONFIG_BLK_DEV_PMEM is not set # CONFIG_BLK_DEV_RAM is not set # CONFIG_BLK_DEV_RBD is not set +# CONFIG_BLK_DEV_RUST_NULL is not set # CONFIG_BLK_DEV_SD is not set # CONFIG_BLK_DEV_SR is not set # CONFIG_BLK_DEV_THROTTLING is not set @@ -1085,6 +1087,7 @@ CONFIG_CONSTRUCTORS=y # CONFIG_CPUFREQ_ARCH_CUR_FREQ is not set # CONFIG_CPUFREQ_DT is not set # CONFIG_CPUFREQ_DT_PLATDEV is not set +# CONFIG_CPUFREQ_DT_RUST is not set # CONFIG_CPUFREQ_VIRT is not set # CONFIG_CPU_BIG_ENDIAN is not set # CONFIG_CPU_BPREDICT_DISABLE is not set @@ -2306,6 +2309,7 @@ CONFIG_FIB_RULES=y # CONFIG_FIELDBUS_DEV is not set CONFIG_FILE_LOCKING=y # CONFIG_FIND_BIT_BENCHMARK is not set +# CONFIG_FIND_BIT_BENCHMARK_RUST is not set # CONFIG_FIREWIRE is not set # CONFIG_FIREWIRE_NOSY is not set # CONFIG_FIRMWARE_EDID is not set @@ -4814,6 +4818,7 @@ CONFIG_NLS_DEFAULT="iso8859-1" # CONFIG_NOA1305 is not set # CONFIG_NOP_USB_XCEIV is not set # CONFIG_NOTIFIER_ERROR_INJECTION is not set +# CONFIG_NOVA_CORE is not set # CONFIG_NOZOMI is not set # CONFIG_NO_HZ is not set # CONFIG_NO_HZ_FULL is not set @@ -5731,6 +5736,12 @@ CONFIG_RTC_SYSTOHC_DEVICE="rtc0" CONFIG_RT_MUTEXES=y CONFIG_RUNTIME_TESTING_MENU=y # CONFIG_RUST is not set +# CONFIG_RUST_BITMAP_HARDENED is not set +# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set +# CONFIG_RUST_DEBUG_ASSERTIONS is not set +# CONFIG_RUST_FW_LOADER_ABSTRACTIONS is not set +CONFIG_RUST_OVERFLOW_CHECKS=y +# CONFIG_RUST_PHYLIB_ABSTRACTIONS is not set # CONFIG_RV is not set # CONFIG_RXGK is not set CONFIG_RXKAD=y diff --git a/target/linux/generic/config-filter b/target/linux/generic/config-filter index ebce3a5e7bf..43c1b113121 100644 --- a/target/linux/generic/config-filter +++ b/target/linux/generic/config-filter @@ -26,7 +26,9 @@ CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES=.* CONFIG_RUSTC_LLVM_VERSION=.* CONFIG_RUSTC_SUPPORTS_.*=.* CONFIG_RUSTC_VERSION=.* +CONFIG_RUSTC_VERSION_TEXT=.* CONFIG_RUST_IS_AVAILABLE=.* +CONFIG_BINDGEN_VERSION_TEXT=.* # CONFIG_SET_FS is not set # CONFIG_TASKS_.* is not set CONFIG_TOOLS_SUPPORT_RELR=.* diff --git a/toolchain/Config.in b/toolchain/Config.in index b0be67eeff6..f9e3b447e37 100644 --- a/toolchain/Config.in +++ b/toolchain/Config.in @@ -416,6 +416,19 @@ config RUST_PANIC_IMMEDIATE_ABORT all crates of a program, the standard library included, so this applies to the whole build. +config KERNEL_RUST + bool + prompt "Enable Rust in the kernel" if RUST_TOOLCHAINOPTS + depends on USE_RUST_TOOLCHAIN + depends on aarch64 || (arm && arm_v7 && !BIG_ENDIAN && !KERNEL_KASAN) \ + || (loongarch64 && !KERNEL_KASAN) || x86_64 + select BUILD_BINDGEN + default y + help + Builds bindgen and passes RUSTC/BINDGEN/RUST_LIB_SRC to the kernel + Kbuild, enabling CONFIG_RUST to be selected in the kernel .config. + riscv64 is excluded: kernel Rust on riscv64 requires CC_IS_CLANG. + config BUILD_BINDGEN bool prompt "Build bindgen (C-header to Rust bindings)" if RUST_TOOLCHAINOPTS @@ -423,7 +436,8 @@ config BUILD_BINDGEN select USE_LLVM_BUILD if !USE_LLVM_HOST && !USE_LLVM_PREBUILT default y help - Installs bindgen-cli to the host toolchain. + Installs bindgen-cli to the host toolchain. Required for kernel Rust. + Automatically selected by KERNEL_RUST. config USE_CBINDGEN bool