* [PATCH 2/3] rust: kunit: allow same test name with different #[cfg(...)]
2026-09-29 14:07 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori
@ 2026-09-29 14:07 ` Nicolás Antinori
2026-09-29 14:07 ` [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori
2026-09-29 16:02 ` [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori
2 siblings, 0 replies; 5+ messages in thread
From: Nicolás Antinori @ 2026-09-29 14:07 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
Sometimes it is necessary to test the same code paths under different
configurations. The `#[cfg(...)]` macro can be used to check if specific
configurations are enabled and run tests accordingly.
Currently, defining multiple tests with the same name under different
`#[cfg(...)]` attributes results in a compilation error. This patch
removes that restriction, allowing identical test names across different
configurations.
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
rust/kernel/kunit.rs | 22 ++++++++++++++++++++++
rust/macros/kunit.rs | 20 +++++++++++++++-----
2 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
index 5fa4d994e0f7..5ff988ab76a4 100644
--- a/rust/kernel/kunit.rs
+++ b/rust/kernel/kunit.rs
@@ -347,6 +347,28 @@ fn rust_test_kunit_in_kunit_test() {
assert!(in_kunit_test());
}
+ // Tests with cfg have the same name on purpose because we are implicitly testing that
+ // tests with the same name but different configs do not throw a compilation error
+ #[test]
+ #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "y")]
+ fn rust_test_kunit_run_only_active_cfg() {
+ assert!(in_kunit_test());
+ }
+
+ #[test]
+ #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "n")]
+ fn rust_test_kunit_run_only_active_cfg() {
+ // This test should never run because of the `cfg`.
+ assert!(false)
+ }
+
+ #[test]
+ #[cfg(CONFIG_RUST_KUNIT_SELFTEST = "INVALID_VALUE")]
+ fn rust_test_kunit_run_only_active_cfg() {
+ // This test should never run because of the `cfg`.
+ assert!(false)
+ }
+
#[test]
#[should_panic]
fn rust_test_kunit_panic_in_kunit_test() {
diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
index f1d0210e6a49..18cfa463c8db 100644
--- a/rust/macros/kunit.rs
+++ b/rust/macros/kunit.rs
@@ -45,6 +45,8 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke
let mut processed_items = Vec::new();
let mut test_cases = Vec::new();
+ let mut test_cases_cfgs = Vec::new();
+ let mut test_case_number: u32 = 1;
// Generate the test KUnit test suite and a test case for each `#[test]`.
//
@@ -135,7 +137,7 @@ macro_rules! assert_eq {
// Add back the test item.
processed_items.push(Item::Fn(f));
- let kunit_wrapper_fn_name = format_ident!("kunit_rust_wrapper_{test}");
+ let kunit_wrapper_fn_name = format_ident!("kunit_rust_wrapper_{test}_{test_case_number}");
let test_cstr = LitCStr::new(
&CString::new(test_str.as_str()).expect("identifier cannot contain NUL"),
test.span(),
@@ -172,15 +174,23 @@ macro_rules! assert_eq {
case.attr.rust_should_panic = #should_panic;
case
}));
+ test_cases_cfgs.push(cfg_attrs);
+ test_case_number += 1;
}
- let num_tests_plus_1 = test_cases.len() + 1;
+ // This dummy array is used to calculate the exact number of tests cases that are active
+ // depending on their #[cfg(...)] attribute.
+ processed_items.push(parse_quote!(
+ const TEST_CASES_COUNT: &[()] = &[#(#(#test_cases_cfgs)* (),)* ()];
+ ));
+
processed_items.push(parse_quote! {
- static mut TEST_CASES: [::kernel::bindings::kunit_case; #num_tests_plus_1] = [
- #(#test_cases,)*
- ::pin_init::zeroed(),
+ static mut TEST_CASES: [::kernel::bindings::kunit_case; TEST_CASES_COUNT.len()] = [
+ #(#(#test_cases_cfgs)* { #test_cases },)*
+ ::pin_init::zeroed()
];
});
+
processed_items.push(parse_quote! {
::kernel::kunit_unsafe_test_suite!(#test_suite, TEST_CASES);
});
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case
2026-09-29 14:07 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori
2026-09-29 14:07 ` [PATCH 2/3] rust: kunit: allow " Nicolás Antinori
@ 2026-09-29 14:07 ` Nicolás Antinori
2026-09-29 16:02 ` [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori
2 siblings, 0 replies; 5+ messages in thread
From: Nicolás Antinori @ 2026-09-29 14:07 UTC (permalink / raw)
To: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda
Cc: Nicolás Antinori, Alexandre Courbot, Andreas Hindborg,
Benno Lossin, Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
Rust KUnit tests now support `#[should_panic]` attribute. Uncomment the
test so it can be run when CONFIG_RUST_BITMAP_HARDENED=y.
Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
---
rust/kernel/bitmap.rs | 40 ++++++++++++++++++----------------------
1 file changed, 18 insertions(+), 22 deletions(-)
diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
index b27e0ec80d64..a9935b2be8c5 100644
--- a/rust/kernel/bitmap.rs
+++ b/rust/kernel/bitmap.rs
@@ -571,33 +571,29 @@ fn bitmap_set_clear_find() -> Result<(), AllocError> {
Ok(())
}
+ #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
#[test]
fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
- // TODO: Kunit #[test]s do not support `cfg` yet,
- // so we add it here in the body.
- #[cfg(not(CONFIG_RUST_BITMAP_HARDENED))]
- {
- let mut b = BitmapVec::new(128, GFP_KERNEL)?;
- b.set_bit(2048);
- b.set_bit_atomic(2048);
- b.clear_bit(2048);
- b.clear_bit_atomic(2048);
- assert_eq!(None, b.next_bit(2048));
- assert_eq!(None, b.next_zero_bit(2048));
- assert_eq!(None, b.last_bit());
- }
+ let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+ b.set_bit(2048);
+ b.set_bit_atomic(2048);
+ b.clear_bit(2048);
+ b.clear_bit_atomic(2048);
+ assert_eq!(None, b.next_bit(2048));
+ assert_eq!(None, b.next_zero_bit(2048));
+ assert_eq!(None, b.last_bit());
Ok(())
}
- // TODO: uncomment once kunit supports [should_panic] and `cfg`.
- // #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
- // #[test]
- // #[should_panic]
- // fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
- // let mut b = BitmapVec::new(128, GFP_KERNEL)?;
- //
- // b.set_bit(2048);
- // }
+ #[cfg(CONFIG_RUST_BITMAP_HARDENED)]
+ #[test]
+ #[should_panic]
+ fn owned_bitmap_out_of_bounds() -> Result<(), AllocError> {
+ let mut b = BitmapVec::new(128, GFP_KERNEL)?;
+
+ b.set_bit(2048);
+ Ok(())
+ }
#[test]
fn bitmap_copy_and_extend() -> Result<(), AllocError> {
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]
2026-09-29 14:07 [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori
2026-09-29 14:07 ` [PATCH 2/3] rust: kunit: allow " Nicolás Antinori
2026-09-29 14:07 ` [PATCH 3/3] rust: bitmap: kunit: uncomment owned_bitmap_out_of_bounds panic case Nicolás Antinori
@ 2026-09-29 16:02 ` Nicolás Antinori
2026-09-29 16:22 ` Miguel Ojeda
2 siblings, 1 reply; 5+ messages in thread
From: Nicolás Antinori @ 2026-09-29 16:02 UTC (permalink / raw)
To: Nicolás Antinori, Alice Ryhl, Burak Emir, Brendan Higgins,
David Gow, Miguel Ojeda
Cc: Alexandre Courbot, Andreas Hindborg, Benno Lossin,
Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
On Tue Sep 29, 2026 at 11:07 AM -03, Nicolás Antinori wrote:
> This patch series intends to implement two features for KUnit tests
> written in Rust. The work is based on a TODO comment made in the
> `bitmap.rs` module [1].
>
> Changelog:
> - RFC patch: [3]
>
> 1. Supporting `#[should_panic]` [2]:
For some reason, GMail is not letting me send the first patch of the
series as it marks it as spam. I'll check what happens and send a PATCH
RESEND of the series or reply this series with thah patch (I already
tried that and failed).
Apologies for the inconvenience.
Regards,
Nicolás
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)]
2026-09-29 16:02 ` [PATCH 0/3] rust: kunit: support #[should_panic] and same test name with different #[cfg(...)] Nicolás Antinori
@ 2026-09-29 16:22 ` Miguel Ojeda
0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2026-09-29 16:22 UTC (permalink / raw)
To: Nicolás Antinori
Cc: Alice Ryhl, Burak Emir, Brendan Higgins, David Gow, Miguel Ojeda,
Alexandre Courbot, Andreas Hindborg, Benno Lossin,
Björn Roy Baron, Boqun Feng, Brigham Campbell,
Daniel Almeida, Danilo Krummrich, Gary Guo, Jori Koolstra,
Onur Özkan, Rae Moar, Shuah Khan, Tamir Duberstein,
Trevor Gross, Yury Norov, linux-kernel, rust-for-linux,
linux-kernel-mentees
On Tue, Sep 29, 2026 at 6:02 PM Nicolás Antinori
<nico.antinori.7@gmail.com> wrote:
>
> For some reason, GMail is not letting me send the first patch of the
> series as it marks it as spam. I'll check what happens and send a PATCH
> RESEND of the series or reply this series with thah patch (I already
> tried that and failed).
>
> Apologies for the inconvenience.
I would suggest trying the `b4` web submission endpoint in that case:
https://b4.docs.kernel.org/en/latest/contributor/send.html
I hope that helps.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread