From: Yury Norov Date: Fri, 17 Apr 2026 03:15:28 +0000 (-0400) Subject: rust: tests: add Kconfig for KUnit test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e74b7a3f5aee02c7df33c79991fd867ce421051b;p=thirdparty%2Flinux.git rust: tests: add Kconfig for KUnit test There are 6 individual Rust KUnit test suites (plus the doctests one). All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image for me on x86_64. As Rust matures, this bloating will inevitably grow. Add Kconfig.test which includes a RUST_KUNIT_TESTS menu, and all individual tests under it. As usual, new tests are all enabled if KUNIT_ALL_TESTS=y. Suggested-by: Alice Ryhl Signed-off-by: Yury Norov Reviewed-by: David Gow Acked-by: Gary Guo Link: https://patch.msgid.link/20260417031531.315281-3-ynorov@nvidia.com [ Fixed capitalization. Used singular for "API" for consistency. Reworded to clarify these are suites and that there exists the doctests one (which is the biggest at the moment by far). - Miguel ] Signed-off-by: Miguel Ojeda --- diff --git a/init/Kconfig b/init/Kconfig index 7ef3fa222ca32..bf1281569678d 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -2213,6 +2213,8 @@ config RUST If unsure, say N. +source "rust/kernel/Kconfig.test" + config RUSTC_VERSION_TEXT string depends on RUST diff --git a/rust/kernel/Kconfig.test b/rust/kernel/Kconfig.test new file mode 100644 index 0000000000000..3a8208b309699 --- /dev/null +++ b/rust/kernel/Kconfig.test @@ -0,0 +1,76 @@ +# SPDX-License-Identifier: GPL-2.0-only +menuconfig RUST_KUNIT_TESTS + bool "Rust KUnit tests" + depends on KUNIT && RUST + default KUNIT_ALL_TESTS + help + This menu collects all options for Rust KUnit tests. + See Documentation/rust/testing.rst for how to protect + unit tests with these options. + + Say Y here to enable Rust KUnit tests. + + If unsure, say N. + +if RUST_KUNIT_TESTS +config RUST_ALLOCATOR_KUNIT_TEST + bool "KUnit tests for Rust allocator API" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust allocator API. + These are only for development and testing, not for regular + kernel use cases. + + If unsure, say N. + +config RUST_KVEC_KUNIT_TEST + bool "KUnit tests for Rust KVec API" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust KVec API. + These are only for development and testing, not for + regular kernel use cases. + + If unsure, say N. + +config RUST_BITMAP_KUNIT_TEST + bool "KUnit tests for Rust bitmap API" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust bitmap API. + These are only for development and testing, not for regular + kernel use cases. + + If unsure, say N. + +config RUST_KUNIT_SELFTEST + bool "KUnit selftests for Rust" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit selftests. These are only + for development and testing, not for regular kernel + use cases. + + If unsure, say N. + +config RUST_STR_KUNIT_TEST + bool "KUnit tests for Rust strings API" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust strings API. + These are only for development and testing, not for regular + kernel use cases. + + If unsure, say N. + +config RUST_ATOMICS_KUNIT_TEST + bool "KUnit tests for Rust atomics API" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for the Rust atomics API. + These are only for development and testing, not for regular + kernel use cases. + + If unsure, say N. + +endif diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 562e41925ada5..cd4203f27aed0 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -265,6 +265,7 @@ unsafe impl Allocator for KVmalloc { } } +#[cfg(CONFIG_RUST_ALLOCATOR_KUNIT_TEST)] #[macros::kunit_tests(rust_allocator)] mod tests { use super::*; diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs index 6f35484387de9..f7af62835aa84 100644 --- a/rust/kernel/alloc/kvec.rs +++ b/rust/kernel/alloc/kvec.rs @@ -1508,6 +1508,7 @@ impl<'vec, T> Drop for DrainAll<'vec, T> { } } +#[cfg(CONFIG_RUST_KVEC_KUNIT_TEST)] #[macros::kunit_tests(rust_kvec)] mod tests { use super::*; diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs index 894043c9e460b..b27e0ec80d643 100644 --- a/rust/kernel/bitmap.rs +++ b/rust/kernel/bitmap.rs @@ -499,6 +499,7 @@ impl Bitmap { } } +#[cfg(CONFIG_RUST_BITMAP_KUNIT_TEST)] #[macros::kunit_tests(rust_kernel_bitmap)] mod tests { use super::*; diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs index a1edf7491579b..cdee5f27bd7fa 100644 --- a/rust/kernel/kunit.rs +++ b/rust/kernel/kunit.rs @@ -329,6 +329,7 @@ pub fn in_kunit_test() -> bool { !unsafe { bindings::kunit_get_current_test() }.is_null() } +#[cfg(CONFIG_RUST_KUNIT_SELFTEST)] #[kunit_tests(rust_kernel_kunit)] mod tests { use super::*; diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 8311d91549e15..a435674f05eab 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -415,6 +415,7 @@ macro_rules! c_str { }}; } +#[cfg(CONFIG_RUST_STR_KUNIT_TEST)] #[kunit_tests(rust_kernel_str)] mod tests { use super::*; diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs index 84fcd7cfcb73c..7468153429e18 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -154,6 +154,7 @@ unsafe impl super::AtomicAdd for usize { } } +#[cfg(CONFIG_RUST_ATOMICS_KUNIT_TEST)] #[macros::kunit_tests(rust_atomics)] mod tests { use super::super::*;