rust: kunit: enforce test configurability
Subsystems (13)
cover rust: kunit: enforce test configurability
Make every Rust KUnit test suite require the Kconfig option that controls it, and let the kunit_tests macro apply the corresponding #[cfg] attribute.
Most Rust KUnit suites currently combine a separate #[cfg] attribute with #[kunit_tests]. The macro does not enforce this pattern, however, and the Nova continuation suite currently has no dedicated test option.
Add a Nova Core KUnit option first and use it to guard the existing suite. Then require a controlling Kconfig symbol in kunit_tests by moving the guard into the macro. Update all callers and documentation as well.
Yury Norov (2):
gpu: nova-core: add a KUnit test configuration option
rust: kunit: move test configuration gating into macro
Documentation/rust/testing.rst | 11 ++++--- drivers/gpu/nova-core/Kconfig | 12 ++++++++ .../gpu/nova-core/gsp/cmdq/continuation.rs | 2 +- rust/kernel/alloc/allocator.rs | 3 +- rust/kernel/alloc/kvec.rs | 3 +- rust/kernel/bitfield.rs | 3 +- rust/kernel/bitmap.rs | 3 +- rust/kernel/kunit.rs | 3 +- rust/kernel/str.rs | 3 +- rust/kernel/sync/atomic/predefine.rs | 3 +- rust/macros/kunit.rs | 30 +++++++++++++++++-- rust/macros/lib.rs | 6 ++-- 12 files changed, 55 insertions(+), 27 deletions(-)
base-commit: aaa4e12f32b6552db1469a796312b25a0def7164
Signature · 1 line
2.53.0
PATCH 1/2 gpu: nova-core: add a KUnit test configuration option
The continuation-record KUnit tests are currently built whenever both Nova Core and KUnit are enabled, providing no way to select the tests independently.
Add CONFIG_NOVA_CORE_KUNIT_TEST and use it to guard the test suite. Follow the usual KUnit convention of enabling the option through CONFIG_KUNIT_ALL_TESTS.
--- drivers/gpu/nova-core/Kconfig | 12 ++++++++++++ drivers/gpu/nova-core/gsp/cmdq/continuation.rs | 1 + 2 files changed, 13 insertions(+)
drivers/gpu/nova-core/Kconfig +12 -0
@@ -15,3 +15,15 @@ config NOVA_CORE This driver is work in progress and may not be functional. If M is selected, the module will be called nova-core. + +config NOVA_CORE_KUNIT_TEST + bool "KUnit tests for Nova Core" if !KUNIT_ALL_TESTS + depends on NOVA_CORE && KUNIT + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for internal components of the Nova + Core driver. The tests currently do not require GPU hardware. These + are only for development and testing, not for regular kernel use + cases. + + If unsure, say N.
drivers/gpu/nova-core/gsp/cmdq/continuation.rs +1 -0
@@ -167,6 +167,7 @@ fn init_variable_payload( } } +#[cfg( CONFIG_NOVA_CORE_KUNIT_TEST )] #[kunit_tests(nova_core_gsp_continuation)] mod tests {use super ::*;
Signature · 1 line
2.53.0
Re: [PATCH 1/2] gpu: nova-core: add a KUnit test configuration option
Le 18/08/2026 à 11:23 PM, Yury Norov a écrit :
Quoted 11 lines
The continuation-record KUnit tests are currently built whenever both Nova Core and KUnit are enabled, providing no way to select the tests independently.
Add
CONFIG_NOVA_CORE_KUNIT_TESTand use it to guard the test suite. Follow the usual KUnit convention of enabling the option throughCONFIG_KUNIT_ALL_TESTS.Assisted-by Codex:gpt-5.6-solSigned-off-by Yury Norov <[email protected]>---
Thanks. I imagine this should go in via drm, rather than the KUnit tree. I'm not totally convinced by patch #2, but let's get this patch in regardless. Maybe it even should have a Fixes tag:
Cheers,
-- David
Quoted 36 lines
drivers/gpu/nova-core/Kconfig | 12 ++++++++++++ drivers/gpu/nova-core/gsp/cmdq/continuation.rs | 1 + 2 files changed, 13 insertions(+)drivers/gpu/nova-core/Kconfig +12
-0@@ -15,3 +15,15 @@ config NOVA_CORE This driver is work in progress and may not be functional. If M is selected, the module will be called nova-core. + +config NOVA_CORE_KUNIT_TEST + bool "KUnit tests for Nova Core" if !KUNIT_ALL_TESTS + depends on NOVA_CORE && KUNIT + default KUNIT_ALL_TESTS + help + This option enables KUnit tests for internal components of the Nova + Core driver. The tests currently do not require GPU hardware. These + are only for development and testing, not for regular kernel use + cases. + + If unsure, say N.drivers/gpu/nova-core/gsp/cmdq/continuation.rs +1
-0@@ -167,6 +167,7 @@ fn init_variable_payload( } } +#[cfg( CONFIG_NOVA_CORE_KUNIT_TEST )] #[kunit_tests(nova_core_gsp_continuation)] mod tests {use super ::*;
PATCH 2/2 rust: kunit: move test configuration gating into macro
In reply to Yury NorovKUnit test modules currently need a separate cfg attribute in addition to the kunit_tests attribute. This makes every test suite repeat the same two-attribute pattern.
Declaring a KUnit test without a preceding #[cfg] attribute is also possible, causing it to be built whenever KUnit and its containing code are enabled.
Require the controlling Kconfig symbol as the second kunit_tests argument and have the macro emit the cfg attribute itself. Update all existing users to the new form.
--- Documentation/rust/testing.rst | 11 ++++--- .../gpu/nova-core/gsp/cmdq/continuation.rs | 3 +- rust/kernel/alloc/allocator.rs | 3 +- rust/kernel/alloc/kvec.rs | 3 +- rust/kernel/bitfield.rs | 3 +- rust/kernel/bitmap.rs | 3 +- rust/kernel/kunit.rs | 3 +- rust/kernel/str.rs | 3 +- rust/kernel/sync/atomic/predefine.rs | 3 +- rust/macros/kunit.rs | 30 +++++++++++++++++-- rust/macros/lib.rs | 6 ++-- 11 files changed, 43 insertions(+), 28 deletions(-)
Documentation/rust/testing.rst +5 -6
@@ -138,9 +138,10 @@ these are also fairly similar to what you would expect from userspace, and they are also mapped to KUnit. These tests are introduced by the ``kunit_tests`` procedural macro, which takes -the name of the test suite as an argument. +the name of the test suite and its controlling Kconfig option as arguments. The +Kconfig option is required, and the macro uses it to guard the test suite. -Each test suite should be guarded by a Kconfig option in +Each test suite should have a Kconfig option, typically in ``rust/kernel/Kconfig.test``. For instance, assume we want to test the function ``f`` from the documentation
@@ -148,8 +149,7 @@ tests section. We could write, in the same file where we have our function: .. code-block:: rust - #[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] - #[kunit_tests(rust_kernel_mymod)] + #[kunit_tests(rust_kernel_mymod, CONFIG_RUST_MYMOD_KUNIT_TEST)] mod tests { use super::*;
@@ -177,8 +177,7 @@ the unit type ``()``) or ``Result`` (i.e. any ``Result<T, E>``). For instance: .. code-block:: rust - #[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] - #[kunit_tests(rust_kernel_mymod)] + #[kunit_tests(rust_kernel_mymod, CONFIG_RUST_MYMOD_KUNIT_TEST)] mod tests { use super::*;
drivers/gpu/nova-core/gsp/cmdq/continuation.rs +1 -2
@@ -167,8 +167,7 @@ fn init_variable_payload( } } -#[cfg( CONFIG_NOVA_CORE_KUNIT_TEST )] -#[kunit_tests(nova_core_gsp_continuation)] +#[kunit_tests(nova_core_gsp_continuation, CONFIG_NOVA_CORE_KUNIT_TEST )] mod tests {use super ::*;
rust/kernel/alloc/allocator.rs +1 -2
@@ -265,8 +265,7 @@ unsafe fn realloc( } } -#[cfg( CONFIG_RUST_ALLOCATOR_KUNIT_TEST )] -#[macros::kunit_tests(rust_allocator)] +#[macros::kunit_tests(rust_allocator, CONFIG_RUST_ALLOCATOR_KUNIT_TEST )] mod tests {use super ::*;use core::mem::MaybeUninit ;
rust/kernel/alloc/kvec.rs +1 -2
@@ -1508,8 +1508,7 @@ fn drop(&mut self) { } } -#[cfg( CONFIG_RUST_KVEC_KUNIT_TEST )] -#[macros::kunit_tests(rust_kvec)] +#[macros::kunit_tests(rust_kvec, CONFIG_RUST_KVEC_KUNIT_TEST )] mod tests {use super ::*;use crate ::prelude::*;
rust/kernel/bitfield.rs +1 -2
@@ -548,8 +548,7 @@ fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result { }; } -#[cfg( CONFIG_RUST_BITFIELD_KUNIT_TEST )] -#[::kernel::macros::kunit_tests(rust_kernel_bitfield)] +#[::kernel::macros::kunit_tests(rust_kernel_bitfield, CONFIG_RUST_BITFIELD_KUNIT_TEST )] mod tests {use core::convert::TryFrom ;
rust/kernel/bitmap.rs +1 -2
@@ -499,8 +499,7 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> { } } -#[cfg( CONFIG_RUST_BITMAP_KUNIT_TEST )] -#[macros::kunit_tests(rust_kernel_bitmap)] +#[macros::kunit_tests(rust_kernel_bitmap, CONFIG_RUST_BITMAP_KUNIT_TEST )] mod tests {use super ::*;use kernel::alloc::flags::GFP_KERNEL ;
rust/kernel/kunit.rs +1 -2
@@ -330,8 +330,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)] +#[kunit_tests(rust_kernel_kunit, CONFIG_RUST_KUNIT_SELFTEST )] mod tests {use super ::*;
rust/kernel/str.rs +1 -2
@@ -428,8 +428,7 @@ macro_rules! c_str { }}; } -#[cfg( CONFIG_RUST_STR_KUNIT_TEST )] -#[kunit_tests(rust_kernel_str)] +#[kunit_tests(rust_kernel_str, CONFIG_RUST_STR_KUNIT_TEST )] mod tests {use super ::*;
rust/kernel/sync/atomic/predefine.rs +1 -2
@@ -152,8 +152,7 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } -#[cfg( CONFIG_RUST_ATOMICS_KUNIT_TEST )] -#[macros::kunit_tests(rust_atomics)] +#[macros::kunit_tests(rust_atomics, CONFIG_RUST_ATOMICS_KUNIT_TEST )] mod tests {use super ::super ::*;
rust/macros/kunit.rs +27 -3
@@ -13,6 +13,10 @@ ToTokens, // };use syn::{ + parse::{ +Parse , +ParseStream ,// + }, parse_quote,Error ,Ident ,
@@ -20,9 +24,28 @@ ItemMod, LitCStr, Result, // + Token, }; -pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<TokenStream> { +pub(crate) struct KunitTestArgs { + test_suite: Ident, + config: Ident, +} + +impl Parse for KunitTestArgs { + fn parse(input: ParseStream<'_>) -> Result<Self> { + let test_suite = input.parse()?; + input.parse::<Token![,]>()?; + let config = input.parse()?; + + Ok(Self { test_suite, config }) + } +} + +pub(crate) fn kunit_tests( + KunitTestArgs { test_suite, config }: KunitTestArgs, + mut module: ItemMod, +) -> Result<TokenStream> { if test_suite.to_string().len() > 255 { return Err(Error::new_spanned( test_suite,
@@ -34,7 +57,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Tokelet Some ((module_brace, module_items)) = module.content.take ()else {Err (Error ::new_spanned ( module, -"`#[kunit_tests(test_name)]` attribute should only be applied to inline modules" , +"`#[kunit_tests(test_name, CONFIG_KUNIT_TEST)]` attribute should only be applied to inline modules" , ))? };
@@ -42,6 +65,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke module .attrs .insert (0 ,parse_quote! (#[cfg( CONFIG_KUNIT = "y" )] )); + module.attrs.insert (0 ,parse_quote! (#[cfg(#config)] ));let mut processed_items =Vec ::new ();let mut test_cases =Vec ::new ();
@@ -51,7 +75,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke// The code generated for the following test module: // // ``` -// #[kunit_tests(kunit_test_suit_name)] +// #[kunit_tests(kunit_test_suite_name, CONFIG_KUNIT_TEST)] // mod tests { // #[test] // fn foo() {
rust/macros/lib.rs +3 -3
@@ -464,14 +464,14 @@ pub fn paste(input: TokenStream) -> TokenStream {/// Registers a KUnit test suite and its test cases using a user-space like syntax. /// -/// This macro should be used on modules. If `CONFIG_KUNIT` (in `.config`) is `n`, the target module -/// is ignored. +/// This macro should be used on modules. The second argument is the Kconfig symbol that controls +/// the test suite. If it or `CONFIG_KUNIT` (in `.config`) is `n`, the target module is ignored. /// /// # Examples /// /// ```ignore /// # use kernel::prelude::*; -/// #[kunit_tests(kunit_test_suit_name)] +/// #[kunit_tests(kunit_test_suite_name, CONFIG_KUNIT_TEST)] /// mod tests { /// #[test] /// fn foo() {
Signature · 1 line
2.53.0
David Gow
maintainerRe: [PATCH 2/2] rust: kunit: move test configuration gating into macro
Le 18/08/2026 à 11:23 PM, Yury Norov a écrit :
Quoted 8 lines
KUnit test modules currently need a separate cfg attribute in addition to the
kunit_testsattribute. This makes every test suite repeat the same two-attribute pattern.Declaring a KUnit test without a preceding
#[cfg]attribute is also possible, causing it to be built whenever KUnit and its containing code are enabled.
Quoted 7 lines
Require the controlling
Kconfigsymbol as the secondkunit_testsargument and have the macro emit the cfg attribute itself. Update all existing users to the new form.Assisted-by Codex:gpt-5.6-solSigned-off-by Yury Norov <[email protected]>---
Thanks.
I agree that every test needs to be behind a Kconfig option, though I'm not totally convinced that this is the optimal solution, particularly for a problem which should only affect test builds.
Personally, I lean towards keeping the #[cfg()] attribute separate here, for mostly minor stylistic reasons. In particular, in C we already have a number of existing tests where multiple suites share a Kconfig symbol. While there's no reason we couldn't just pass the same symbol to several different kunit_suite() attributes, it would be nicer to group them all behind a single #[cfg()] where that makes sense (or even, going forward, to support having them live in their own separate module/crate).
And personally, I find it a bit clearer: people already understand #[cfg()], and having #[kunit_suite()] just accept the suite name better matches suite registration in C.
That being said: I don't think there's anything fundamentally impossible with implementing something like this (be it now, or later): worst-case we'd end up with some redundant Kconfig entries and uglier chains of attributes. So if this continues to be a problem (or enough people think I'm wrong here), we can always change it later.
If we want to improve on the current situation, though, something like a checkpatch.pl check or a lint would be a good way of making it less likely for checks to slip through the cracks. And if we're particularly worried about test size (or runtime), there are things we could look at to improve things there. Moving tests to separate modules is probably a bit harder to do in Rust than in C, but we could possibly have a separate set of config options for large tests, and better support filtering slow tests in Rust (which is on my to-do list).
(Finally, there are also a few minor issues with the patch below, which would need to be fixed if there's a v2.)
Cheers,
-- David
Quoted 178 lines
Documentation/rust/testing.rst | 11 ++++--- .../gpu/nova-core/gsp/cmdq/continuation.rs | 3 +- rust/kernel/alloc/allocator.rs | 3 +- rust/kernel/alloc/kvec.rs | 3 +- rust/kernel/bitfield.rs | 3 +- rust/kernel/bitmap.rs | 3 +- rust/kernel/kunit.rs | 3 +- rust/kernel/str.rs | 3 +- rust/kernel/sync/atomic/predefine.rs | 3 +- rust/macros/kunit.rs | 30 +++++++++++++++++-- rust/macros/lib.rs | 6 ++-- 11 files changed, 43 insertions(+), 28 deletions(-)Documentation/rust/testing.rst +5
-6@@ -138,9 +138,10 @@ these are also fairly similar to what you would expect from userspace, and they are also mapped to KUnit. These tests are introduced by the ``kunit_tests`` procedural macro, which takes -the name of the test suite as an argument. +the name of the test suite and its controlling Kconfig option as arguments. The +Kconfig option is required, and the macro uses it to guard the test suite. -Each test suite should be guarded by a Kconfig option in +Each test suite should have a Kconfig option, typically in ``rust/kernel/Kconfig.test``. For instance, assume we want to test the function ``f`` from the documentation@@ -148,8 +149,7 @@ tests section. We could write, in the same file where we have our function: .. code-block:: rust - #[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] - #[kunit_tests(rust_kernel_mymod)] + #[kunit_tests(rust_kernel_mymod, CONFIG_RUST_MYMOD_KUNIT_TEST)] mod tests { use super::*;@@ -177,8 +177,7 @@ the unit type ``()``) or ``Result`` (i.e. any ``Result<T, E>``). For instance: .. code-block:: rust - #[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] - #[kunit_tests(rust_kernel_mymod)] + #[kunit_tests(rust_kernel_mymod, CONFIG_RUST_MYMOD_KUNIT_TEST)] mod tests { use super::*;drivers/gpu/nova-core/gsp/cmdq/continuation.rs +1
-2@@ -167,8 +167,7 @@ fn init_variable_payload( } } -#[cfg( CONFIG_NOVA_CORE_KUNIT_TEST )] -#[kunit_tests(nova_core_gsp_continuation)] +#[kunit_tests(nova_core_gsp_continuation, CONFIG_NOVA_CORE_KUNIT_TEST )] mod tests {use super ::*;rust/kernel/alloc/allocator.rs +1
-2@@ -265,8 +265,7 @@ unsafe fn realloc( } } -#[cfg( CONFIG_RUST_ALLOCATOR_KUNIT_TEST )] -#[macros::kunit_tests(rust_allocator)] +#[macros::kunit_tests(rust_allocator, CONFIG_RUST_ALLOCATOR_KUNIT_TEST )] mod tests {use super ::*;use core::mem::MaybeUninit ;rust/kernel/alloc/kvec.rs +1
-2@@ -1508,8 +1508,7 @@ fn drop(&mut self) { } } -#[cfg( CONFIG_RUST_KVEC_KUNIT_TEST )] -#[macros::kunit_tests(rust_kvec)] +#[macros::kunit_tests(rust_kvec, CONFIG_RUST_KVEC_KUNIT_TEST )] mod tests {use super ::*;use crate ::prelude::*;rust/kernel/bitfield.rs +1
-2@@ -548,8 +548,7 @@ fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result { }; } -#[cfg( CONFIG_RUST_BITFIELD_KUNIT_TEST )] -#[::kernel::macros::kunit_tests(rust_kernel_bitfield)] +#[::kernel::macros::kunit_tests(rust_kernel_bitfield, CONFIG_RUST_BITFIELD_KUNIT_TEST )] mod tests {use core::convert::TryFrom ;rust/kernel/bitmap.rs +1
-2@@ -499,8 +499,7 @@ pub fn next_zero_bit(&self, start: usize) -> Option<usize> { } } -#[cfg( CONFIG_RUST_BITMAP_KUNIT_TEST )] -#[macros::kunit_tests(rust_kernel_bitmap)] +#[macros::kunit_tests(rust_kernel_bitmap, CONFIG_RUST_BITMAP_KUNIT_TEST )] mod tests {use super ::*;use kernel::alloc::flags::GFP_KERNEL ;rust/kernel/kunit.rs +1
-2@@ -330,8 +330,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)] +#[kunit_tests(rust_kernel_kunit, CONFIG_RUST_KUNIT_SELFTEST )] mod tests {use super ::*;rust/kernel/str.rs +1
-2@@ -428,8 +428,7 @@ macro_rules! c_str { }}; } -#[cfg( CONFIG_RUST_STR_KUNIT_TEST )] -#[kunit_tests(rust_kernel_str)] +#[kunit_tests(rust_kernel_str, CONFIG_RUST_STR_KUNIT_TEST )] mod tests {use super ::*;rust/kernel/sync/atomic/predefine.rs +1
-2@@ -152,8 +152,7 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } -#[cfg( CONFIG_RUST_ATOMICS_KUNIT_TEST )] -#[macros::kunit_tests(rust_atomics)] +#[macros::kunit_tests(rust_atomics, CONFIG_RUST_ATOMICS_KUNIT_TEST )] mod tests {use super ::super ::*;rust/macros/kunit.rs +4
-0@@ -13,6 +13,10 @@ ToTokens, // };use syn::{ + parse::{ +Parse , +ParseStream ,// + }, parse_quote,Error ,Ident ,
rust/macros/kunit.rs line 32
Quoted 10 lines
rust/macros/kunit.rs +4
-1@@ -20,9 +24,28 @@ ItemMod, LitCStr, Result, // + Token, }; -pub (crate )fn kunit_tests (test_suite:Ident ,mut module:ItemMod ) ->Result <TokenStream > { +pub (crate )struct KunitTestArgs { + test_suite:Ident , + config:Ident ,
#[cfg()] accepts more than just an Ident here: if we want to explicitly gate things on either multiple config options, or on config options being built-in/modules/etc, then we might want to allow something more complex.
Quoted 20 lines
rust/macros/kunit.rs +16
-0+} + +impl Parse for KunitTestArgs { + fn parse(input: ParseStream<'_>) -> Result<Self> { + let test_suite = input.parse()?; + input.parse::<Token![,]>()?; + let config = input.parse()?; + + Ok(Self { test_suite, config }) + } +} + +pub(crate) fn kunit_tests( + KunitTestArgs { test_suite, config }: KunitTestArgs, + mut module: ItemMod, +) -> Result<TokenStream> { if test_suite.to_string().len() > 255 { return Err(Error::new_spanned( test_suite,
rust/macros/kunit.rs pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke · line 60
Quoted 5 lines
rust/macros/kunit.rs +1
-1@@ -34,7 +57,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke let Some((module_brace, module_items)) = module.content.take() else { Err(Error::new_spanned( module, - "`#[kunit_tests(test_name)]` attribute should only be applied to inline modules", + "`#[kunit_tests(test_name, CONFIG_KUNIT_TEST)]` attribute should only be applied to inline modules",
This is a much uglier message, and CONFIG_KUNIT_TEST is not an ideal example to have here. Could we just have a placeholder, e.g. #[kunit_tests(...)].
Quoted 12 lines
rust/macros/kunit.rs +1
-0))? };@@ -42,6 +65,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke module .attrs .insert (0 ,parse_quote! (#[cfg( CONFIG_KUNIT = "y" )] )); + module.attrs.insert (0 ,parse_quote! (#[cfg(#config)] ));let mut processed_items =Vec ::new ();let mut test_cases =Vec ::new ();
rust/macros/kunit.rs pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke · line 78
Quoted 5 lines
rust/macros/kunit.rs +1
-1@@ -51,7 +75,7 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke// The code generated for the following test module: // // ``` -// #[kunit_tests(kunit_test_suit_name)] +// #[kunit_tests(kunit_test_suite_name, CONFIG_KUNIT_TEST)]
I really would rather these examples not state CONFIG_KUNIT_TEST here:
it's the config option for tests for KUnit itself, and I'd rather not imply that people should add their tests to it. CONFIG_KUNIT_EXAMPLE_TEST would be better.
Quoted 12 lines
rust/macros/kunit.rs +0
-0// mod tests { // #[test] // fn foo() { rust/macros/lib.rs +0
-1@@ -464,14 +464,14 @@ pub fn paste(input: TokenStream) -> TokenStream {/// Registers a KUnit test suite and its test cases using a user-space like syntax. /// -/// This macro should be used on modules. If `CONFIG_KUNIT` (in `.config`) is `n`, the target module
rust/macros/lib.rs pub fn paste(input: TokenStream) -> TokenStream { · line 474
Quoted 10 lines
rust/macros/lib.rs +3
-2-/// is ignored. +/// This macro should be used on modules. The second argument is the Kconfig symbol that controls +/// the test suite. If it or `CONFIG_KUNIT` (in `.config`) is `n`, the target module is ignored. /// /// # Examples /// /// ```ignore /// # use kernel::prelude::*; -/// #[kunit_tests(kunit_test_suit_name)] +/// #[kunit_tests(kunit_test_suite_name, CONFIG_KUNIT_TEST)]
As above, let's not use CONFIG_KUNIT_TEST in the documentation here.
Quoted 3 lines
rust/macros/lib.rs +0
-0/// mod tests { /// #[test] /// fn foo() {
Quoted 4 lines · Yury Norov
Make every Rust KUnit test suite require the
Kconfigoption that controls it, and let thekunit_testsmacro apply the corresponding#[cfg]attribute.
If we are sure we always want at least one cfg guarding them, then yeah, this makes sense (we could ask to write the cfg bit inside, for "greppability", and for clarity / less ambiguity later on).
David: are there cases on KUnit where you would recommend/prefer something different?
For instance, I could imagine a Rust mod for testing purposes already gated by a cfg that is meant to contain many tests, and then different suites inside that for control (possibly with extra cfgs, but maybe none too for some).
Cheers,
Miguel
Gary Guo
maintainerQuoted 10 lines · Miguel Ojeda
Quoted 4 lines · Yury Norov
Make every Rust KUnit test suite require the
Kconfigoption that controls it, and let thekunit_testsmacro apply the corresponding#[cfg]attribute.If we are sure we always want at least one
cfgguarding them, then yeah, this makes sense (we could ask to write thecfgbit inside, for "greppability", and for clarity / less ambiguity later on).
Quoted 7 lines
David: are there cases on KUnit where you would recommend/prefer something different?
For instance, I could imagine a Rust
modfor testing purposes already gated by acfgthat is meant to contain many tests, and then different suites inside that for control (possibly with extracfgs, but maybe none too for some).
There might also be cases where we want some other conditional (like combination of cfgs) to gate.
I am okay with gating existing ones under new cfgs, but requiring one in macro invocation itself sounds bit excessive, and also doesn't look nice :)
If we decide on actually requiring one, a better option might me for me to implement a lint in klint to produce a warning that is suppressable if people actually don't want to use cfgs.
Best,
Gary
Quoted 14 lines · Gary Guo
Quoted 13 lines · Miguel Ojeda
Quoted 4 lines · Yury Norov
Make every Rust KUnit test suite require the
Kconfigoption that controls it, and let thekunit_testsmacro apply the corresponding#[cfg]attribute.If we are sure we always want at least one
cfgguarding them, then yeah, this makes sense (we could ask to write thecfgbit inside, for "greppability", and for clarity / less ambiguity later on).David: are there cases on KUnit where you would recommend/prefer something different?
Quoted 7 lines
Quoted 4 lines
For instance, I could imagine a Rust
modfor testing purposes already gated by acfgthat is meant to contain many tests, and then different suites inside that for control (possibly with extracfgs, but maybe none too for some).There might also be cases where we want some other conditional (like combination of cfgs) to gate.
But not a single current case. All the current tests are flat and
simple: every test has it's unique gate config. Do we need a more complicated scheme? I doubt that.
If there's a simple case of CONFIG_A && CONFIG_B, one can stack them up:
#[cfg(CONFIG_THIS)]
#[kunit_tests(rust_kernel_bitmap, CONFIG_THAT)]If there's something more complicated... Let's wait for at least one real test like that, and not speculate on non-existing cases.
Quoted 2 lines
I am okay with gating existing ones under new cfgs, but requiring one in macro invocation itself sounds bit excessive, and also doesn't look nice :)
This series begins with "enforce", so it's not about being nice. The generic kernel tries to save every single bit of memory and nanosecond of runtime. That's a secret of Linux success IMO.
In the mother kernel every single test, performance benchmark or even extra functionality is configurable, so that non-developer users don't pay for the functionality they don't need. In Rust, before e74b7a3f5a there was no way to throw the tests out. And even after that, we still have such tests.
Let's stop being nice and make this bad habit explicitly impossible.
Quoted 3 lines
If we decide on actually requiring one, a better option might me for me to implement a lint in klint to produce a warning that is suppressable if people actually don't want to use cfgs.
This is not a coding style, it's a factual error. So it should be caught at compile time as an explicit error.
Thanks,
Yury
Quoted 3 lines · Yury Norov
If there's something more complicated... Let's wait for at least one real test like that, and not speculate on non-existing cases.
I asked because the issue is that this enforces a policy early on, i.e. if a user happens to need it, then they will have to dive into the implementation to change it or, likely, work around it or assume they shouldn't do that.
In other words, there is a cost to enforce something too early on.
So it is a balance, depending on what we expect, which is why I asked David about his experience here.
Quoted 3 lines
This series begins with "enforce", so it's not about being nice. The generic kernel tries to save every single bit of memory and nanosecond of runtime. That's a secret of Linux success IMO.
It is great to save bits and nanoseconds, but KUnit is explicitly not meant for production.
Quoted 3 lines
pay for the functionality they don't need. In Rust, before
e74b7a3f5athere was no way to throw the tests out. And even after that, we still have such tests.
False, you could disable KUnit (or make it m) -- which is what you are supposed to do in production.
So, no, we were not bloating every Rust-enabled kernel out there with tests.
Quoted 2 lines
This is not a coding style, it's a factual error. So it should be caught at compile time as an explicit error.
Klint and lints in general are not just for coding style.
And lints are also compile-time, and they can stop the build too.
Cheers,
Miguel
Gary Guo
maintainerQuoted 20 lines · Yury Norov
Quoted 19 lines · Gary Guo
Quoted 17 lines · Miguel Ojeda
Quoted 4 lines · Yury Norov
Make every Rust KUnit test suite require the
Kconfigoption that controls it, and let thekunit_testsmacro apply the corresponding#[cfg]attribute.If we are sure we always want at least one
cfgguarding them, then yeah, this makes sense (we could ask to write thecfgbit inside, for "greppability", and for clarity / less ambiguity later on).David: are there cases on KUnit where you would recommend/prefer something different?
For instance, I could imagine a Rust
modfor testing purposes already gated by acfgthat is meant to contain many tests, and then different suites inside that for control (possibly with extracfgs, but maybe none too for some).
Quoted 6 lines
Quoted 2 lines
There might also be cases where we want some other conditional (like combination of cfgs) to gate.
But not a single current case. All the current tests are flat and
simple: every test has it's unique gate config. Do we need a more complicated scheme? I doubt that.
A common case in Rust crates is when some shared code exists when either of two features are enabled, do
#[cfg(any(feature_a, feature_b))]
sure, with Kconfig you can add new config and select based on that.
I see this as an issue with composition. #[cfg] and #[kunit_tests] are two orthogonal attributes so one shouldn't (and shouldn't need to) be absorbed into another.
For a crate, one might want to have multiple kunit test suites in a shared module. For that, you currently can do
#[cfg(CONFIG_THIS)]
mod tests;and have #[kunit_tests] insides the tests module freely. Your design would not allow this (or would require a always-enabled feature to be passed in to appease the macro). Also, for a leaf driver crate, all #[kunit_tests] would likely share a single config, so there's repetition as well.
There's also an issue with doc tests. Unlike explicit kunit tests, the test suite is generated and you don't have to stick your attributes. Currently we have all abstractions in a single kernel crate, but when the new build system for Rust lands, we would have each subsystem being their own crate, and obviously we would need a mechanism to control when doc tests are executed.
A more reasonable approach IMO would be to specify provide a global gate to all kunit tests within a crate. So, e.g. for Nova core, just add
pass the Kconfig CONFIG_NOVA_CORE_KUNIT_TEST name to makefile and it'll gate all kunit tests within the crate.
Best,
Gary
Quoted 22 lines · Gary Guo
Quoted 10 lines · Miguel Ojeda
Quoted 4 lines · Yury Norov
Make every Rust KUnit test suite require the
Kconfigoption that controls it, and let thekunit_testsmacro apply the corresponding#[cfg]attribute.If we are sure we always want at least one
cfgguarding them, then yeah, this makes sense (we could ask to write thecfgbit inside, for "greppability", and for clarity / less ambiguity later on).Quoted 7 lines
David: are there cases on KUnit where you would recommend/prefer something different?
For instance, I could imagine a Rust
modfor testing purposes already gated by acfgthat is meant to contain many tests, and then different suites inside that for control (possibly with extracfgs, but maybe none too for some).There might also be cases where we want some other conditional (like combination of cfgs) to gate.
Quoted 7 lines
I am okay with gating existing ones under new cfgs, but requiring one in macro invocation itself sounds bit excessive, and also doesn't look nice :)
If we decide on actually requiring one, a better option might me for me to implement a lint in klint to produce a warning that is suppressable if people actually don't want to use cfgs.
In addition to KUnit, there is also a hardware-dependent IRQ test [1], that is normally configured to be skipped. Unless we decide, during review, that this kind of test is a Bad Idea.
This is likely independent of the KUnit selections, but I want us to just be aware of it in case it influences things here.
[1] https://lore.kernel.org/[email protected]
thanks,
Signature · 1 line
John Hubbard