[PATCH v3 4/6] rust: sync: replace `kernel::c_str!` with C-Strings

Tamir Duberstein posted 6 patches 1 month ago
[PATCH v3 4/6] rust: sync: replace `kernel::c_str!` with C-Strings
Posted by Tamir Duberstein 1 month ago
From: Tamir Duberstein <tamird@gmail.com>

C-String literals were added in Rust 1.77. Replace instances of
`kernel::c_str!` with C-String literals where possible.

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 rust/kernel/sync.rs            | 5 ++---
 rust/kernel/sync/completion.rs | 2 +-
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
index cf5b638a097d..4e503036e123 100644
--- a/rust/kernel/sync.rs
+++ b/rust/kernel/sync.rs
@@ -48,7 +48,6 @@ impl LockClassKey {
     ///
     /// # Examples
     /// ```
-    /// # use kernel::c_str;
     /// # use kernel::alloc::KBox;
     /// # use kernel::types::ForeignOwnable;
     /// # use kernel::sync::{LockClassKey, SpinLock};
@@ -60,7 +59,7 @@ impl LockClassKey {
     /// {
     ///     stack_pin_init!(let num: SpinLock<u32> = SpinLock::new(
     ///         0,
-    ///         c_str!("my_spinlock"),
+    ///         c"my_spinlock",
     ///         // SAFETY: `key_ptr` is returned by the above `into_foreign()`, whose
     ///         // `from_foreign()` has not yet been called.
     ///         unsafe { <Pin<KBox<LockClassKey>> as ForeignOwnable>::borrow(key_ptr) }
@@ -119,6 +118,6 @@ macro_rules! optional_name {
         $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
     };
     ($name:literal) => {
-        $crate::c_str!($name)
+        $name
     };
 }
diff --git a/rust/kernel/sync/completion.rs b/rust/kernel/sync/completion.rs
index c50012a940a3..97d39c248793 100644
--- a/rust/kernel/sync/completion.rs
+++ b/rust/kernel/sync/completion.rs
@@ -34,7 +34,7 @@
 /// impl MyTask {
 ///     fn new() -> Result<Arc<Self>> {
 ///         let this = Arc::pin_init(pin_init!(MyTask {
-///             work <- new_work!("MyTask::work"),
+///             work <- new_work!(c"MyTask::work"),
 ///             done <- Completion::new(),
 ///         }), GFP_KERNEL)?;
 ///

-- 
2.51.2
Re: [PATCH v3 4/6] rust: sync: replace `kernel::c_str!` with C-Strings
Posted by Miguel Ojeda 3 weeks, 6 days ago
On Thu, Nov 13, 2025 at 11:58 PM Tamir Duberstein <tamird@kernel.org> wrote:
>
> @@ -119,6 +118,6 @@ macro_rules! optional_name {
>          $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!()))
>      };
>      ($name:literal) => {
> -        $crate::c_str!($name)
> +        $name
>      };
>  }

This requires the next commit plus it needs callers of `new_spinlock!`
in Binder to be fixed at the same time.

Cheers,
Miguel
Re: [PATCH v3 4/6] rust: sync: replace `kernel::c_str!` with C-Strings
Posted by Miguel Ojeda 3 weeks, 6 days ago
On Mon, Nov 17, 2025 at 12:09 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> This requires the next commit plus it needs callers of `new_spinlock!`
> in Binder to be fixed at the same time.

Actually, do we even want callers to have to specify `c`?

For instance, in the `module!` macro we originally had `b`, and
removed it for simplicity of callers:

    b13c9880f909 ("rust: macros: take string literals in `module!`")

Cheers,
Miguel
Re: [PATCH v3 4/6] rust: sync: replace `kernel::c_str!` with C-Strings
Posted by Alice Ryhl 3 weeks, 5 days ago
On Mon, Nov 17, 2025 at 12:52:22AM +0100, Miguel Ojeda wrote:
> On Mon, Nov 17, 2025 at 12:09 AM Miguel Ojeda
> <miguel.ojeda.sandonis@gmail.com> wrote:
> >
> > This requires the next commit plus it needs callers of `new_spinlock!`
> > in Binder to be fixed at the same time.
> 
> Actually, do we even want callers to have to specify `c`?
> 
> For instance, in the `module!` macro we originally had `b`, and
> removed it for simplicity of callers:
> 
>     b13c9880f909 ("rust: macros: take string literals in `module!`")

Yeah I think ideally the new_spinlock! macro invokes c_str! on the
provided string literal to avoid users of the macro from needing the
c prefix.

Alice
Re: [PATCH v3 4/6] rust: sync: replace `kernel::c_str!` with C-Strings
Posted by Tamir Duberstein 3 weeks, 5 days ago
On Mon, Nov 17, 2025 at 9:49 AM Alice Ryhl <aliceryhl@google.com> wrote:
>
> On Mon, Nov 17, 2025 at 12:52:22AM +0100, Miguel Ojeda wrote:
> > On Mon, Nov 17, 2025 at 12:09 AM Miguel Ojeda
> > <miguel.ojeda.sandonis@gmail.com> wrote:
> > >
> > > This requires the next commit plus it needs callers of `new_spinlock!`
> > > in Binder to be fixed at the same time.
> >
> > Actually, do we even want callers to have to specify `c`?
> >
> > For instance, in the `module!` macro we originally had `b`, and
> > removed it for simplicity of callers:
> >
> >     b13c9880f909 ("rust: macros: take string literals in `module!`")
>
> Yeah I think ideally the new_spinlock! macro invokes c_str! on the
> provided string literal to avoid users of the macro from needing the
> c prefix.
>
> Alice

Makes sense and done in v4 (which contains only a small portion of
this patch and no others).