rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: kernel: remove support for unused host `#[test]`s
@ 2025-07-26 18:07 Miguel Ojeda
  2025-07-29  6:27 ` David Gow
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Miguel Ojeda @ 2025-07-26 18:07 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Danilo Krummrich, Brendan Higgins,
	David Gow, Masahiro Yamada
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Rae Moar, linux-kselftest, kunit-dev, Nathan Chancellor,
	Nicolas Schier, linux-kbuild, linux-kernel, patches

Since commit 028df914e546 ("rust: str: convert `rusttest` tests into
KUnit"), we do not have anymore host `#[test]`s that run in the host.

Moreover, we do not plan to add any new ones -- tests should generally
run within KUnit, since there they are built the same way the kernel
does. While we may want to have some way to define tests that can also
be run outside the kernel, we still want to test within the kernel too
[1], and thus would likely use a custom syntax anyway to define them.

Thus simplify the `rusttest` target by removing support for host
`#[test]`s for the `kernel` crate.

This still maintains the support for the `macros` crate, even though we
do not have any such tests there.

Link: https://lore.kernel.org/rust-for-linux/CABVgOS=AKHSfifp0S68K3jgNZAkALBr=7iFb=niryG5WDxjSrg@mail.gmail.com/ [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/Makefile        | 9 +--------
 rust/kernel/alloc.rs | 6 +++---
 rust/kernel/error.rs | 4 ++--
 rust/kernel/lib.rs   | 2 +-
 4 files changed, 7 insertions(+), 14 deletions(-)

diff --git a/rust/Makefile b/rust/Makefile
index 115b63b7d1e3..5290b37868dd 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -235,7 +235,7 @@ quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
 	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
 		$(rustc_test_run_flags)
 
-rusttest: rusttest-macros rusttest-kernel
+rusttest: rusttest-macros
 
 rusttest-macros: private rustc_target_flags = --extern proc_macro \
 	--extern macros --extern kernel --extern pin_init
@@ -245,13 +245,6 @@ rusttest-macros: $(src)/macros/lib.rs \
 	+$(call if_changed,rustc_test)
 	+$(call if_changed,rustdoc_test)
 
-rusttest-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
-    --extern build_error --extern macros --extern bindings --extern uapi
-rusttest-kernel: $(src)/kernel/lib.rs rusttestlib-ffi rusttestlib-kernel \
-    rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
-    rusttestlib-uapi rusttestlib-pin_init FORCE
-	+$(call if_changed,rustc_test)
-
 ifdef CONFIG_CC_IS_CLANG
 bindgen_c_flags = $(c_flags)
 else
diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index a2c49e5494d3..335ae3271fa8 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -2,16 +2,16 @@
 
 //! Implementation of the kernel's memory allocation infrastructure.
 
-#[cfg(not(any(test, testlib)))]
+#[cfg(not(testlib))]
 pub mod allocator;
 pub mod kbox;
 pub mod kvec;
 pub mod layout;
 
-#[cfg(any(test, testlib))]
+#[cfg(testlib)]
 pub mod allocator_test;
 
-#[cfg(any(test, testlib))]
+#[cfg(testlib)]
 pub use self::allocator_test as allocator;
 
 pub use self::kbox::Box;
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 3dee3139fcd4..7812aca1b6ef 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -157,7 +157,7 @@ pub fn to_ptr<T>(self) -> *mut T {
     }
 
     /// Returns a string representing the error, if one exists.
-    #[cfg(not(any(test, testlib)))]
+    #[cfg(not(testlib))]
     pub fn name(&self) -> Option<&'static CStr> {
         // SAFETY: Just an FFI call, there are no extra safety requirements.
         let ptr = unsafe { bindings::errname(-self.0.get()) };
@@ -174,7 +174,7 @@ pub fn name(&self) -> Option<&'static CStr> {
     /// When `testlib` is configured, this always returns `None` to avoid the dependency on a
     /// kernel function so that tests that use this (e.g., by calling [`Result::unwrap`]) can still
     /// run in userspace.
-    #[cfg(any(test, testlib))]
+    #[cfg(testlib)]
     pub fn name(&self) -> Option<&'static CStr> {
         None
     }
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index e13d6ed88fa6..8a0153f61732 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -197,7 +197,7 @@ pub const fn as_ptr(&self) -> *mut bindings::module {
     }
 }
 
-#[cfg(not(any(testlib, test)))]
+#[cfg(not(testlib))]
 #[panic_handler]
 fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
     pr_emerg!("{}\n", info);

base-commit: 89be9a83ccf1f88522317ce02f854f30d6115c41
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: kernel: remove support for unused host `#[test]`s
  2025-07-26 18:07 [PATCH] rust: kernel: remove support for unused host `#[test]`s Miguel Ojeda
@ 2025-07-29  6:27 ` David Gow
  2025-07-29  8:27 ` Danilo Krummrich
  2025-08-25 14:31 ` Danilo Krummrich
  2 siblings, 0 replies; 5+ messages in thread
From: David Gow @ 2025-07-29  6:27 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Danilo Krummrich, Brendan Higgins, Masahiro Yamada,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Rae Moar, linux-kselftest, kunit-dev, Nathan Chancellor,
	Nicolas Schier, linux-kbuild, linux-kernel, patches

[-- Attachment #1: Type: text/plain, Size: 4658 bytes --]

On Sun, 27 Jul 2025 at 02:08, Miguel Ojeda <ojeda@kernel.org> wrote:
>
> Since commit 028df914e546 ("rust: str: convert `rusttest` tests into
> KUnit"), we do not have anymore host `#[test]`s that run in the host.
>
> Moreover, we do not plan to add any new ones -- tests should generally
> run within KUnit, since there they are built the same way the kernel
> does. While we may want to have some way to define tests that can also
> be run outside the kernel, we still want to test within the kernel too
> [1], and thus would likely use a custom syntax anyway to define them.
>
> Thus simplify the `rusttest` target by removing support for host
> `#[test]`s for the `kernel` crate.
>
> This still maintains the support for the `macros` crate, even though we
> do not have any such tests there.
>
> Link: https://lore.kernel.org/rust-for-linux/CABVgOS=AKHSfifp0S68K3jgNZAkALBr=7iFb=niryG5WDxjSrg@mail.gmail.com/ [1]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---

With my (biased) KUnit hat on, this looks good to me.

_Maybe_ it's worth updating Documentation/rust/testing.rst, though it
already says these are "mostly used for testing the macros crate's
examples".

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  rust/Makefile        | 9 +--------
>  rust/kernel/alloc.rs | 6 +++---
>  rust/kernel/error.rs | 4 ++--
>  rust/kernel/lib.rs   | 2 +-
>  4 files changed, 7 insertions(+), 14 deletions(-)
>
> diff --git a/rust/Makefile b/rust/Makefile
> index 115b63b7d1e3..5290b37868dd 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -235,7 +235,7 @@ quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
>         $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
>                 $(rustc_test_run_flags)
>
> -rusttest: rusttest-macros rusttest-kernel
> +rusttest: rusttest-macros
>
>  rusttest-macros: private rustc_target_flags = --extern proc_macro \
>         --extern macros --extern kernel --extern pin_init
> @@ -245,13 +245,6 @@ rusttest-macros: $(src)/macros/lib.rs \
>         +$(call if_changed,rustc_test)
>         +$(call if_changed,rustdoc_test)
>
> -rusttest-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
> -    --extern build_error --extern macros --extern bindings --extern uapi
> -rusttest-kernel: $(src)/kernel/lib.rs rusttestlib-ffi rusttestlib-kernel \
> -    rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
> -    rusttestlib-uapi rusttestlib-pin_init FORCE
> -       +$(call if_changed,rustc_test)
> -
>  ifdef CONFIG_CC_IS_CLANG
>  bindgen_c_flags = $(c_flags)
>  else
> diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
> index a2c49e5494d3..335ae3271fa8 100644
> --- a/rust/kernel/alloc.rs
> +++ b/rust/kernel/alloc.rs
> @@ -2,16 +2,16 @@
>
>  //! Implementation of the kernel's memory allocation infrastructure.
>
> -#[cfg(not(any(test, testlib)))]
> +#[cfg(not(testlib))]
>  pub mod allocator;
>  pub mod kbox;
>  pub mod kvec;
>  pub mod layout;
>
> -#[cfg(any(test, testlib))]
> +#[cfg(testlib)]
>  pub mod allocator_test;
>
> -#[cfg(any(test, testlib))]
> +#[cfg(testlib)]
>  pub use self::allocator_test as allocator;
>
>  pub use self::kbox::Box;
> diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> index 3dee3139fcd4..7812aca1b6ef 100644
> --- a/rust/kernel/error.rs
> +++ b/rust/kernel/error.rs
> @@ -157,7 +157,7 @@ pub fn to_ptr<T>(self) -> *mut T {
>      }
>
>      /// Returns a string representing the error, if one exists.
> -    #[cfg(not(any(test, testlib)))]
> +    #[cfg(not(testlib))]
>      pub fn name(&self) -> Option<&'static CStr> {
>          // SAFETY: Just an FFI call, there are no extra safety requirements.
>          let ptr = unsafe { bindings::errname(-self.0.get()) };
> @@ -174,7 +174,7 @@ pub fn name(&self) -> Option<&'static CStr> {
>      /// When `testlib` is configured, this always returns `None` to avoid the dependency on a
>      /// kernel function so that tests that use this (e.g., by calling [`Result::unwrap`]) can still
>      /// run in userspace.
> -    #[cfg(any(test, testlib))]
> +    #[cfg(testlib)]
>      pub fn name(&self) -> Option<&'static CStr> {
>          None
>      }
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index e13d6ed88fa6..8a0153f61732 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -197,7 +197,7 @@ pub const fn as_ptr(&self) -> *mut bindings::module {
>      }
>  }
>
> -#[cfg(not(any(testlib, test)))]
> +#[cfg(not(testlib))]
>  #[panic_handler]
>  fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
>      pr_emerg!("{}\n", info);
>
> base-commit: 89be9a83ccf1f88522317ce02f854f30d6115c41
> --
> 2.50.1
>

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5281 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: kernel: remove support for unused host `#[test]`s
  2025-07-26 18:07 [PATCH] rust: kernel: remove support for unused host `#[test]`s Miguel Ojeda
  2025-07-29  6:27 ` David Gow
@ 2025-07-29  8:27 ` Danilo Krummrich
  2025-08-16 21:21   ` Miguel Ojeda
  2025-08-25 14:31 ` Danilo Krummrich
  2 siblings, 1 reply; 5+ messages in thread
From: Danilo Krummrich @ 2025-07-29  8:27 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Brendan Higgins, David Gow, Masahiro Yamada,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Rae Moar, linux-kselftest, kunit-dev, Nathan Chancellor,
	Nicolas Schier, linux-kbuild, linux-kernel, patches

On Sat Jul 26, 2025 at 8:07 PM CEST, Miguel Ojeda wrote:
> Since commit 028df914e546 ("rust: str: convert `rusttest` tests into
> KUnit"), we do not have anymore host `#[test]`s that run in the host.
>
> Moreover, we do not plan to add any new ones -- tests should generally
> run within KUnit, since there they are built the same way the kernel
> does. While we may want to have some way to define tests that can also
> be run outside the kernel, we still want to test within the kernel too
> [1], and thus would likely use a custom syntax anyway to define them.
>
> Thus simplify the `rusttest` target by removing support for host
> `#[test]`s for the `kernel` crate.
>
> This still maintains the support for the `macros` crate, even though we
> do not have any such tests there.
>
> Link: https://lore.kernel.org/rust-for-linux/CABVgOS=AKHSfifp0S68K3jgNZAkALBr=7iFb=niryG5WDxjSrg@mail.gmail.com/ [1]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Nice! This should also allow us to remove the Cmalloc allocator subsequently.

Reviewed-by: Danilo Krummrich <dakr@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: kernel: remove support for unused host `#[test]`s
  2025-07-29  8:27 ` Danilo Krummrich
@ 2025-08-16 21:21   ` Miguel Ojeda
  0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2025-08-16 21:21 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Miguel Ojeda, Alex Gaynor, Brendan Higgins, David Gow,
	Masahiro Yamada, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	rust-for-linux, Rae Moar, linux-kselftest, kunit-dev,
	Nathan Chancellor, Nicolas Schier, linux-kbuild, linux-kernel,
	patches

On Tue, Jul 29, 2025 at 10:27 AM Danilo Krummrich <dakr@kernel.org> wrote:
>
> Nice! This should also allow us to remove the Cmalloc allocator subsequently.

Yeah, unless we want to use it in the `macros` crate for an example or
similar -- I didn't include the removal directly here mainly to split
that decision into another step, but given this other issue in
`rusttest` I just sent:

    https://lore.kernel.org/rust-for-linux/20250816204215.2719559-1-ojeda@kernel.org/

I think it is easiest to just get rid of it (and anyway eventually I
want to run the tests from the macros crate within the kernel too).

I have sent a patch for that here:

    https://lore.kernel.org/rust-for-linux/20250816211900.2731720-1-ojeda@kernel.org/

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: kernel: remove support for unused host `#[test]`s
  2025-07-26 18:07 [PATCH] rust: kernel: remove support for unused host `#[test]`s Miguel Ojeda
  2025-07-29  6:27 ` David Gow
  2025-07-29  8:27 ` Danilo Krummrich
@ 2025-08-25 14:31 ` Danilo Krummrich
  2 siblings, 0 replies; 5+ messages in thread
From: Danilo Krummrich @ 2025-08-25 14:31 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Alex Gaynor, Brendan Higgins, David Gow, Masahiro Yamada,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Rae Moar, linux-kselftest, kunit-dev, Nathan Chancellor,
	Nicolas Schier, linux-kbuild, linux-kernel, patches

On Sat Jul 26, 2025 at 8:07 PM CEST, Miguel Ojeda wrote:
> Since commit 028df914e546 ("rust: str: convert `rusttest` tests into
> KUnit"), we do not have anymore host `#[test]`s that run in the host.
>
> Moreover, we do not plan to add any new ones -- tests should generally
> run within KUnit, since there they are built the same way the kernel
> does. While we may want to have some way to define tests that can also
> be run outside the kernel, we still want to test within the kernel too
> [1], and thus would likely use a custom syntax anyway to define them.
>
> Thus simplify the `rusttest` target by removing support for host
> `#[test]`s for the `kernel` crate.
>
> This still maintains the support for the `macros` crate, even though we
> do not have any such tests there.
>
> Link: https://lore.kernel.org/rust-for-linux/CABVgOS=AKHSfifp0S68K3jgNZAkALBr=7iFb=niryG5WDxjSrg@mail.gmail.com/ [1]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Applied to alloc-next, thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-08-25 14:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-26 18:07 [PATCH] rust: kernel: remove support for unused host `#[test]`s Miguel Ojeda
2025-07-29  6:27 ` David Gow
2025-07-29  8:27 ` Danilo Krummrich
2025-08-16 21:21   ` Miguel Ojeda
2025-08-25 14:31 ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).