Skip to contentSkip to conversation

rust: add Kconfig.test

PATCH Applied Yury Norov 4 participants 10 messages Apr 17 – Jun 8

Subsystems (8)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

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
author
#1 lore raw

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
index 83d7dea99137..894043c9e460 100644
@@ -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
index 1d53834fcb12..84fcd7cfcb73 100644
@@ -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
maintainer Reviewed-by on v1 1/3
#2 lore raw

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_tests crate conditional on the corresponding configs. When the configs are disabled, compiler warns on unused crate. So, embed it in unit test declaration.

---

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
index 83d7dea99137..894043c9e460 100644
@@ -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
index 1d53834fcb12..84fcd7cfcb73 100644
@@ -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::*;
 
author
#3 lore raw

PATCH 2/3 rust: testing: add Kconfig for KUnit test

In reply to Yury Norov

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.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
index 43875ef36752..4af544514e6c 100644
@@ -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
index 63bfb91b3671..0d3434bca867 100644
@@ -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
index ac8d6f763ae8..563c760c8105 100644
@@ -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
index 894043c9e460..b27e0ec80d64 100644
@@ -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
index a1edf7491579..cdee5f27bd7f 100644
@@ -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
index 8311d91549e1..a435674f05ea 100644
@@ -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
index 84fcd7cfcb73..7468153429e1 100644
@@ -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
maintainer Reviewed-by on v1 2/3
#4 lore raw

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.test which includes a RUST_KUNIT_TESTS menu, 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.

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

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

init/Kconfig +2 -0
index 43875ef36752..4af544514e6c 100644
@@ -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
new file mode 100644
index 000000000000..a05243696a01
@@ -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.
+
+endif
rust/kernel/alloc/allocator.rs +1 -0
index 63bfb91b3671..0d3434bca867 100644
@@ -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
index ac8d6f763ae8..563c760c8105 100644
@@ -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
index 894043c9e460..b27e0ec80d64 100644
@@ -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
index a1edf7491579..cdee5f27bd7f 100644
@@ -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
index 8311d91549e1..a435674f05ea 100644
@@ -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
index 84fcd7cfcb73..7468153429e1 100644
@@ -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::*;
#5 lore raw

Re: [PATCH 2/3] rust: testing: add Kconfig for KUnit test

In reply to Yury Norov
Quoted 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

author
#6 lore raw

PATCH 3/3 Documentation: rust: testing: add Kconfig guidance

In reply to Yury Norov

Now 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
index f43cb77bcc69..24de173471b2 100644
@@ -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
maintainer Reviewed-by on v1 3/3
#7 lore raw

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.

---

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
index f43cb77bcc69..24de173471b2 100644
@@ -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::*;
maintainer Acked-by on the v1 series
#8 lore raw
In reply to Yury Norov
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_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

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
Applied
#9 lore raw
In reply to Yury Norov
Quoted 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_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.

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