Skip to content

rust: kernel: add cfg_select! and use it for config-based selection

Nika Krasnova linux-kselftest lkml rust-for-linux 1 version In review

Get series with b4
b4 am [email protected] change-id 20260629-backport-cfg_select-54f5e3d034ed

Versions newest first

  1. v1 Latest2 patches 5 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2rust: kernel: add cfg_select! backport for config-based selectionrust/kernel/cfg.rs rust/kernel/lib.rs rust/kernel/prelude.rs+299 -00 replies
2/2rust: kernel: migrate to cfg_select! for config-based selectionrust/kernel/cpu.rs rust/kernel/cpumask.rs rust/kernel/debugfs.rs +9 more+366 -3240 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/2] rust: kernel: add cfg_select! and use it for config-based selection

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.

---
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

Signature · 1 line
Nika Krasnova