From: Tamir Duberstein <tamird@kernel.org>
To: Gary Guo <gary@garyguo.net>
Cc: Miguel Ojeda <ojeda@kernel.org>, Boqun Feng <boqun@kernel.org>,
Björn Roy Baron <bjorn3_gh@protonmail.com>,
Benno Lossin <lossin@kernel.org>,
Andreas Hindborg <a.hindborg@kernel.org>,
Alice Ryhl <aliceryhl@google.com>,
Trevor Gross <tmgross@umich.edu>,
Danilo Krummrich <dakr@kernel.org>,
Breno Leitao <leitao@debian.org>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Brendan Higgins <brendan.higgins@linux.dev>,
David Gow <davidgow@google.com>, Rae Moar <raemoar63@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
Waiman Long <longman@redhat.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
dri-devel@lists.freedesktop.org, linux-kselftest@vger.kernel.org,
kunit-dev@googlegroups.com,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH] rust: rename `kernel::c_str!` to `str_to_cstr!`
Date: Mon, 2 Mar 2026 12:45:41 -0500 [thread overview]
Message-ID: <CAJ-ks9mOq69HHALheyFNc-0TqUTDGb1CEB4zqERXYhK+oMn3PQ@mail.gmail.com> (raw)
In-Reply-To: <DGSH4L653R41.1JXV6RIKG0V4@garyguo.net>
On Mon, Mar 2, 2026 at 12:31 PM Gary Guo <gary@garyguo.net> wrote:
>
> On Mon Mar 2, 2026 at 5:20 PM GMT, Tamir Duberstein wrote:
> > Now that all literals are C-Strings, rename and update the documentation
> > of this macro to clarify its intended purpose.
> >
> > Link: https://github.com/Rust-for-Linux/linux/issues/1075
> > Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> > Signed-off-by: Tamir Duberstein <tamird@kernel.org>
>
> Do we really need to perform the rename? I think updating the documentation
> should be good enough?
>
> If the only concern is misuse of the macro then perhaps I can just add a
> lint for this in klint.
That would be better! I'll change v2 to only update the documentation.
>
> > ---
> > This patch completes the work of replacing our custom `CStr` with
> > upstream's.
> > ---
> > rust/kernel/bug.rs | 2 +-
> > rust/kernel/configfs.rs | 2 +-
> > rust/kernel/drm/ioctl.rs | 2 +-
> > rust/kernel/kunit.rs | 3 ++-
> > rust/kernel/str.rs | 19 ++++++++++++++-----
> > rust/kernel/sync.rs | 4 ++--
> > rust/kernel/sync/lock/global.rs | 3 ++-
> > rust/kernel/workqueue.rs | 6 +++---
> > 8 files changed, 26 insertions(+), 15 deletions(-)
> >
> > diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
> > index ed943960f851..f7cb673b1766 100644
> > --- a/rust/kernel/bug.rs
> > +++ b/rust/kernel/bug.rs
> > @@ -80,7 +80,7 @@ macro_rules! warn_flags {
> > // with a valid null-terminated string.
> > unsafe {
> > $crate::bindings::warn_slowpath_fmt(
> > - $crate::c_str!(::core::file!()).as_char_ptr(),
> > + $crate::str_to_cstr!(::core::file!()).as_char_ptr(),
> > line!() as $crate::ffi::c_int,
> > $flags as $crate::ffi::c_uint,
> > ::core::ptr::null(),
> > diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs
> > index 2339c6467325..930f17bb2041 100644
> > --- a/rust/kernel/configfs.rs
> > +++ b/rust/kernel/configfs.rs
> > @@ -1000,7 +1000,7 @@ macro_rules! configfs_attrs {
> > $crate::configfs::Attribute<$attr, $data, $data> =
> > unsafe {
> > $crate::configfs::Attribute::new(
> > - $crate::c_str!(::core::stringify!($name)),
> > + $crate::str_to_cstr!(::core::stringify!($name)),
> > )
> > };
> > )*
> > diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs
> > index cf328101dde4..6a87f489dc88 100644
> > --- a/rust/kernel/drm/ioctl.rs
> > +++ b/rust/kernel/drm/ioctl.rs
> > @@ -157,7 +157,7 @@ macro_rules! declare_drm_ioctls {
> > },
> > flags: $flags,
> > name: $crate::str::as_char_ptr_in_const_context(
> > - $crate::c_str!(::core::stringify!($cmd)),
> > + $crate::str_to_cstr!(::core::stringify!($cmd)),
> > ),
> > }
> > ),*];
> > diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
> > index f93f24a60bdd..5802a3507ecc 100644
> > --- a/rust/kernel/kunit.rs
> > +++ b/rust/kernel/kunit.rs
> > @@ -59,7 +59,8 @@ macro_rules! kunit_assert {
> >
> > static FILE: &'static $crate::str::CStr = $file;
> > static LINE: i32 = ::core::line!() as i32 - $diff;
> > - static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
> > + static CONDITION: &'static $crate::str::CStr =
> > + $crate::str_to_cstr!(stringify!($condition));
> >
> > // SAFETY: FFI call without safety requirements.
> > let kunit_test = unsafe { $crate::bindings::kunit_get_current_test() };
> > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> > index fa87779d2253..8bb40de007d4 100644
> > --- a/rust/kernel/str.rs
> > +++ b/rust/kernel/str.rs
> > @@ -376,19 +376,28 @@ fn as_ref(&self) -> &BStr {
> > }
> > }
> >
> > -/// Creates a new [`CStr`] from a string literal.
> > +/// Creates a new [`CStr`] at compile time.
> > ///
> > -/// The string literal should not contain any `NUL` bytes.
> > +/// Rust supports C string literals since Rust 1.77, and they should be used instead of this macro
> > +/// where possible. This macro exists to allow static *non-literal* C strings to be created at
> > +/// compile time. This is most often used in other macros.
> > +///
> > +/// # Panics
> > +///
> > +/// This macro panics if the operand contains an interior `NUL` byte.
> > ///
> > /// # Examples
> > ///
> > /// ```
> > -/// # use kernel::c_str;
> > +/// # use kernel::str_to_cstr;
> > /// # use kernel::str::CStr;
> > -/// const MY_CSTR: &CStr = c_str!("My awesome CStr!");
> > +/// const MY_CSTR: &CStr = str_to_cstr!(concat!(file!(), ":", line!(), ": My CStr!"));
>
> Perhaps keep a copy of the old example and add a comment saying this is allowed
> but c"literal" should be preferred.
Done in v2.
>
> Best,
> Gary
Thanks for having a look!
Tamir
next prev parent reply other threads:[~2026-03-02 17:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-02 17:20 [PATCH] rust: rename `kernel::c_str!` to `str_to_cstr!` Tamir Duberstein
2026-03-02 17:31 ` Gary Guo
2026-03-02 17:45 ` Tamir Duberstein [this message]
2026-03-03 3:00 ` Claude review: " Claude Code Review Bot
2026-03-03 3:00 ` Claude Code Review Bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAJ-ks9mOq69HHALheyFNc-0TqUTDGb1CEB4zqERXYhK+oMn3PQ@mail.gmail.com \
--to=tamird@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brendan.higgins@linux.dev \
--cc=dakr@kernel.org \
--cc=davidgow@google.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=kunit-dev@googlegroups.com \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lossin@kernel.org \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=raemoar63@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox