rust: add Kconfig.test
Subsystems (8)
cover rust: add Kconfig.test
There are 6 individual Rust KUnit tests. All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image on my x86_64 buld. As Rust matures, this bloating will inevitably grow.
Add Kconfig.test, which provides a RUST_KUNIT_TESTS menu, and all individual tests under it.
Yury Norov (3):
rust: tests: drop 'use crate' in bitmap and atomic KUnit tests
rust: testing: add Kconfig for KUnit test
Documentation: rust: testing: add Kconfig guidance
Documentation/rust/testing.rst | 5 ++- init/Kconfig | 2 + rust/kernel/Kconfig.test | 76 ++++++++++++++++++++++++++++ rust/kernel/alloc/allocator.rs | 1 + rust/kernel/alloc/kvec.rs | 1 + rust/kernel/bitmap.rs | 5 +-- rust/kernel/kunit.rs | 1 + rust/kernel/str.rs | 1 + rust/kernel/sync/atomic/predefine.rs | 5 +-- 9 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 rust/kernel/Kconfig.test
Signature · 1 line
2.51.0
PATCH 1/3 rust: tests: drop 'use crate' in bitmap and atomic KUnit tests
The following patch makes usage of macros::kunit_tests crate conditional on the corresponding configs. When the configs are disabled, compiler warns on unused crate. So, embed it in unit test declaration.
--- rust/kernel/bitmap.rs | 4 +--- rust/kernel/sync/atomic/predefine.rs | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)
rust/kernel/bitmap.rs +1 -3
@@ -499,9 +499,7 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> { } } -use macros::kunit_tests; - -#[kunit_tests(rust_kernel_bitmap)] +#[macros::kunit_tests(rust_kernel_bitmap)] mod tests {use super ::*;use kernel::alloc::flags::GFP_KERNEL ;
rust/kernel/sync/atomic/predefine.rs +1 -3
@@ -154,9 +154,7 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } -use crate ::macros::kunit_tests; - -#[kunit_tests(rust_atomics)] +#[macros::kunit_tests(rust_atomics)] mod tests {use super ::super ::*;
Signature · 1 line
2.51.0
Re: [PATCH 1/3] rust: tests: drop 'use crate' in bitmap and atomic KUnit tests
Le 17/04/2026 à 11:15 AM, Yury Norov a écrit :
Quoted 6 lines
The following patch makes usage of
macros::kunit_testscrate conditional on the corresponding configs. When the configs are disabled, compiler warns on unused crate. So, embed it in unit test declaration.Signed-off-by Yury Norov <[email protected]>---
Looks good to me!
Cheers,
-- David
Quoted 34 lines
rust/kernel/bitmap.rs | 4 +--- rust/kernel/sync/atomic/predefine.rs | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)rust/kernel/bitmap.rs +1
-3@@ -499,9 +499,7 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> { } } -use macros::kunit_tests; - -#[kunit_tests(rust_kernel_bitmap)] +#[macros::kunit_tests(rust_kernel_bitmap)] mod tests {use super ::*;use kernel::alloc::flags::GFP_KERNEL ;rust/kernel/sync/atomic/predefine.rs +1
-3@@ -154,9 +154,7 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } -use crate ::macros::kunit_tests; - -#[kunit_tests(rust_atomics)] +#[macros::kunit_tests(rust_atomics)] mod tests {use super ::super ::*;
PATCH 2/3 rust: testing: add Kconfig for KUnit test
In reply to Yury NorovThere are 6 individual Rust KUnit tests. 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.
---
This doesn't create a new entry in MAINTAINERS for the Kconfig.test, so the new file just follows the implicit rule for the rust/ directory. Please let me know if the explicit entry is needed.
init/Kconfig | 2 + rust/kernel/Kconfig.test | 76 ++++++++++++++++++++++++++++ rust/kernel/alloc/allocator.rs | 1 + rust/kernel/alloc/kvec.rs | 1 + rust/kernel/bitmap.rs | 1 + rust/kernel/kunit.rs | 1 + rust/kernel/str.rs | 1 + rust/kernel/sync/atomic/predefine.rs | 1 + 8 files changed, 84 insertions(+) create mode 100644 rust/kernel/Kconfig.test
init/Kconfig +2 -0
@@ -2208,6 +2208,8 @@ config RUST If unsure, say N. +source "rust/kernel/Kconfig.test" + config RUSTC_VERSION_TEXT string depends on RUST
rust/kernel/Kconfig.test new file +76 -0
76 lines not on this page · raw message
rust/kernel/alloc/allocator.rs +1 -0
@@ -251,6 +251,7 @@ unsafe fn realloc( } } +#[cfg( CONFIG_RUST_ALLOCATOR_KUNIT_TEST )] #[macros::kunit_tests(rust_allocator)] mod tests {use super ::*;
rust/kernel/alloc/kvec.rs +1 -0
@@ -1351,6 +1351,7 @@ fn drop(&mut self) { } } +#[cfg( CONFIG_RUST_KVEC_KUNIT_TEST )] #[macros::kunit_tests(rust_kvec)] mod tests {use super ::*;
rust/kernel/bitmap.rs +1 -0
@@ -499,6 +499,7 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> { } } +#[cfg( CONFIG_RUST_BITMAP_KUNIT_TEST )] #[macros::kunit_tests(rust_kernel_bitmap)] mod tests {use super ::*;
rust/kernel/kunit.rs +1 -0
@@ -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 ::*;
rust/kernel/str.rs +1 -0
@@ -415,6 +415,7 @@ macro_rules! c_str { }}; } +#[cfg( CONFIG_RUST_STR_KUNIT_TEST )] #[kunit_tests(rust_kernel_str)] mod tests {use super ::*;
rust/kernel/sync/atomic/predefine.rs +1 -0
@@ -154,6 +154,7 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } +#[cfg( CONFIG_RUST_ATOMICS_KUNIT_TEST )] #[macros::kunit_tests(rust_atomics)] mod tests {use super ::super ::*;
Signature · 1 line
2.51.0
Re: [PATCH 2/3] rust: testing: add Kconfig for KUnit test
Le 17/04/2026 à 11:15 AM, Yury Norov a écrit :
Quoted 6 lines
There are 6 individual Rust KUnit tests. 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.testwhich includes aRUST_KUNIT_TESTSmenu, and all individual tests under it.
I think this makes a lot of sense for tests within the kernel crate (and any other 'system' crates). Individual drivers probably should still keep their Kconfig options under the corresponding subsystem.
Quoted 10 lines
As usual, new tests are all enabled if KUNIT_ALL_TESTS=y.
Suggested-by Alice Ryhl <[email protected]>Signed-off-by Yury Norov <[email protected]>---
This doesn't create a new entry inMAINTAINERSfor theKconfig.test, so the new file just follows the implicit rule for therust/directory. Please let me know if the explicit entry is needed.
I think it's fine to leave this as implicitly part of rust/, personally.
Still, this looks good.
Cheers,
-- David
Quoted 178 lines
init/Kconfig | 2 + rust/kernel/Kconfig.test | 76 ++++++++++++++++++++++++++++ rust/kernel/alloc/allocator.rs | 1 + rust/kernel/alloc/kvec.rs | 1 + rust/kernel/bitmap.rs | 1 + rust/kernel/kunit.rs | 1 + rust/kernel/str.rs | 1 + rust/kernel/sync/atomic/predefine.rs | 1 + 8 files changed, 84 insertions(+)create mode 100644
rust/kernel/Kconfig.testinit/Kconfig +2
-0@@ -2208,6 +2208,8 @@ config RUST If unsure, say N. +source "rust/kernel/Kconfig.test" + config RUSTC_VERSION_TEXT string depends on RUSTrust/kernel/Kconfig.test new file +76
-0@@ -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 APIs" 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 APIs" 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. + +endifrust/kernel/alloc/allocator.rs +1
-0@@ -251,6 +251,7 @@ unsafe fn realloc( } } +#[cfg( CONFIG_RUST_ALLOCATOR_KUNIT_TEST )] #[macros::kunit_tests(rust_allocator)] mod tests {use super ::*;rust/kernel/alloc/kvec.rs +1
-0@@ -1351,6 +1351,7 @@ fn drop(&mut self) { } } +#[cfg( CONFIG_RUST_KVEC_KUNIT_TEST )] #[macros::kunit_tests(rust_kvec)] mod tests {use super ::*;rust/kernel/bitmap.rs +1
-0@@ -499,6 +499,7 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> { } } +#[cfg( CONFIG_RUST_BITMAP_KUNIT_TEST )] #[macros::kunit_tests(rust_kernel_bitmap)] mod tests {use super ::*;rust/kernel/kunit.rs +1
-0@@ -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 ::*;rust/kernel/str.rs +1
-0@@ -415,6 +415,7 @@ macro_rules! c_str { }}; } +#[cfg( CONFIG_RUST_STR_KUNIT_TEST )] #[kunit_tests(rust_kernel_str)] mod tests {use super ::*;rust/kernel/sync/atomic/predefine.rs +1
-0@@ -154,6 +154,7 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } +#[cfg( CONFIG_RUST_ATOMICS_KUNIT_TEST )] #[macros::kunit_tests(rust_atomics)] mod tests {use super ::super ::*;
Re: [PATCH 2/3] rust: testing: add Kconfig for KUnit test
In reply to Yury NorovQuoted 2 lines · Yury Norov
There are 6 individual Rust KUnit tests. All the tests are compiled
I think here you mean "test suite", i.e. there are many more individual KUnit tests than that -- hundreds at the moment (mostly doctests).
Quoted 2 lines
unconditionally now, which adds ~200 kB to the kernel image for me on
x86_64. As Rust matures, this bloating will inevitably grow.
Yeah, options for major/relevant sets sound good, even after we split the crate. Not sure about granularity, i.e. whether we will want a Kconfig for every single test suite, though... Especially because we can end up with a ton of Kconfig symbols; but I see there are a bunch of those already in the tree in the C side, so I guess this is expected/fine, especially given David's review.
(By the way, KUnit is not intended for production kernels, so I am not sure it really counts as bloat in that sense.)
Thanks!
Cheers,
Miguel
PATCH 3/3 Documentation: rust: testing: add Kconfig guidance
In reply to Yury NorovNow that rust KUnit tests are protected with Kconfig, update the documentation to mention it.
--- Documentation/rust/testing.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/rust/testing.rst +4 -1
@@ -141,10 +141,13 @@ These tests are introduced by the ``kunit_tests`` procedural macro, which takes the name of the test suite as an argument. For instance, assume we want to test the function ``f`` from the documentation -tests section. We could write, in the same file where we have our function: +tests section. We could write, in the same file where we have our function. +Each test is protected with the corresponding Kconfig option, see +rust/kernel/Kconfig.test. .. code-block:: rust + #[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] #[kunit_tests(rust_kernel_mymod)] mod tests { use super::*;
Signature · 1 line
2.51.0
Re: [PATCH 3/3] Documentation: rust: testing: add Kconfig guidance
Le 17/04/2026 à 11:15 AM, Yury Norov a écrit :
Quoted 5 lines
Now that rust KUnit tests are protected with
Kconfig, update the documentation to mention it.Signed-off-by Yury Norov <[email protected]>---
Looks good to me.
Cheers,
-- David
Quoted 22 lines
Documentation/rust/testing.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)Documentation/rust/testing.rst +4
-1@@ -141,10 +141,13 @@ These tests are introduced by the ``kunit_tests`` procedural macro, which takes the name of the test suite as an argument. For instance, assume we want to test the function ``f`` from the documentation -tests section. We could write, in the same file where we have our function: +tests section. We could write, in the same file where we have our function. +Each test is protected with the corresponding Kconfig option, see +rust/kernel/Kconfig.test. .. code-block:: rust + #[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] #[kunit_tests(rust_kernel_mymod)] mod tests { use super::*;
Quoted 11 lines · Yury Norov
There are 6 individual Rust KUnit tests. All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image on my
x86_64buld. As Rust matures, this bloating will inevitably grow.Add
Kconfig.test, which provides aRUST_KUNIT_TESTSmenu, and all individual tests under it.Yury Norov (3):
rust: tests: drop 'use crate' in bitmap and atomic KUnit tests
rust: testing: addKconfigfor KUnit test
Documentation: rust: testing: addKconfigguidance
Quoted 12 lines
Documentation/rust/testing.rst | 5 ++- init/Kconfig | 2 + rust/kernel/Kconfig.test | 76 ++++++++++++++++++++++++++++ rust/kernel/alloc/allocator.rs | 1 + rust/kernel/alloc/kvec.rs | 1 + rust/kernel/bitmap.rs | 5 +-- rust/kernel/kunit.rs | 1 + rust/kernel/str.rs | 1 + rust/kernel/sync/atomic/predefine.rs | 5 +-- 9 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 rust/kernel/Kconfig.test
Miguel Ojeda
AppliedQuoted 7 lines · Yury Norov
There are 6 individual Rust KUnit tests. All the tests are compiled unconditionally now, which adds ~200 kB to the kernel image on my
x86_64buld. As Rust matures, this bloating will inevitably grow.Add
Kconfig.test, which provides aRUST_KUNIT_TESTSmenu, and all individual tests under it.
Applied to rust-next -- thanks everyone!
[ 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 ]
[ Fixed the paragraph by moving the new sentence above. Added gate
in the other example as well. Applied proper formatting. Reworded slightly. - Miguel ]
Cheers,
Miguel