[PATCH 0/2] rust: kernel: add cfg_select! and use it for config-based selection
From: Nika Krasnova
Date: Sun Jun 28 2026 - 23:52:37 EST
Conditional compilation in the kernel's Rust code is expressed with
paired #[cfg(CONFIG_FOO)] / #[cfg(not(CONFIG_FOO))] attributes when
selecting between two or more implementations of an item. This is
verbose and easy to get out of sync.
The standard library gained core::cfg_select! for exactly this (stable
since Rust 1.95.0): one macro that emits the first arm whose cfg
predicate holds. The kernel's MSRV is 1.85.0, so it is not yet
available.
Patch 1 adds a macro_rules! backport under the same name and arm syntax,
so call sites can be written today and switch to core::cfg_select!
unchanged once the MSRV reaches 1.95.0. Patch 2 converts the existing
paired #[cfg] selections in the kernel crate to it.
One selection is deliberately left as #[cfg]: a #[macro_export]
macro_rules! referred to by an absolute path ($crate::) cannot be
wrapped in cfg_select! (rust-lang/rust#52234); see patch 2 for details.
Testing:
- KUnit: rust_kernel_cfg 14/14, rust_doctests_kernel 322/322 (x86_64).
- clippy (CLIPPY=1) clean; rustfmt clean; checkpatch --strict: no
errors (only the benign new-file MAINTAINERS warning).
- Built x86_64 with CONFIG_DEBUG_FS=y and =n to cover both debugfs arms.
- Other arches / CONFIG_64BIT=n arms not built locally; relying on CI.
Link: https://github.com/Rust-for-Linux/linux/issues/1183
Signed-off-by: Nika Krasnova <nika@xxxxxxxxxxxx>
---
Nika Krasnova (2):
rust: kernel: add cfg_select! backport for config-based selection
rust: kernel: migrate to cfg_select! for config-based selection
rust/kernel/cfg.rs | 297 +++++++++++++++++++++++++++++++++++
rust/kernel/cpu.rs | 21 +--
rust/kernel/cpumask.rs | 42 ++---
rust/kernel/debugfs.rs | 145 +++++++++--------
rust/kernel/driver.rs | 44 +++---
rust/kernel/drm/device.rs | 53 ++++---
rust/kernel/error.rs | 47 +++---
rust/kernel/kunit.rs | 54 ++++---
rust/kernel/lib.rs | 78 ++++-----
rust/kernel/mm.rs | 36 +++--
rust/kernel/prelude.rs | 1 +
rust/kernel/sync/atomic.rs | 54 ++++---
rust/kernel/sync/atomic/predefine.rs | 35 +++--
rust/kernel/time.rs | 82 +++++-----
14 files changed, 665 insertions(+), 324 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260629-backport-cfg_select-54f5e3d034ed
--
Nika Krasnova