mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aary Milind Kinge <kingeaary@gmail.com>
To: Miguel Ojeda <ojeda@kernel.org>
Cc: Alice Ryhl <aliceryhl@google.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	Not-Buddy <kingeaary@gmail.com>
Subject: [PATCH] rust: devres: use compile-time type_name for debug info
Date: Tue, 26 May 2026 15:13:29 +0530	[thread overview]
Message-ID: <20260526094329.533943-1-kingeaary@gmail.com> (raw)

From: Not-Buddy <kingeaary@gmail.com>

The devres_set_node_dbginfo function previously used a hardcoded
c"Devres<T>" string for all device resource nodes because
core::any::type_name was not stabilized as a const fn. This made
debugging device resources difficult as all nodes shared the same name.

Enable the `const_type_name` unstable feature in the `kernel` crate
and replace the hardcoded placeholder with the actual generic type
name at compile time.

To satisfy the C-API's requirement for a null-terminated string, the
bytes are copied into a locally evaluated const array buffer, appended
with a null byte, and promoted to static read-only memory by reference
before being passed to devres_set_node_dbginfo.

Signed-off-by: Aary Milind Kinge <kingeaary@gmail.com>
---
 rust/kernel/devres.rs | 41 ++++++++++++++++++++++++++++++++++++++---
 rust/kernel/lib.rs    |  1 +
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
index 9e5f93aed20c..989e9d9a78c4 100644
--- a/rust/kernel/devres.rs
+++ b/rust/kernel/devres.rs
@@ -185,6 +185,42 @@ pub(super) unsafe fn devres_node_remove(
 }
 
 impl<T: Send> Devres<T> {
+    // 1. Get the standard Rust string at compile time
+    const TYPE_NAME: &'static str = core::any::type_name::<T>();
+
+    // 2. Store the actual bytes in a const array (a VALUE, not a reference)
+    const TYPE_NAME_BUF: [u8; 128] = {
+        let bytes = Self::TYPE_NAME.as_bytes();
+        let mut buf = [0u8; 128];
+        let mut i = 0;
+
+        while i < bytes.len() && i < 127 {
+            buf[i] = bytes[i];
+            i += 1;
+        }
+        buf[i] = 0; // The null terminator
+        buf
+    };
+
+    // 3. Take a reference to the array (which promotes it to static memory)
+    const TYPE_NAME_CSTR: &'static crate::str::CStr = {
+        let static_buf: &'static [u8; 128] = &Self::TYPE_NAME_BUF;
+
+        // Find the length up to the null byte
+        let mut len = 0;
+        while len < 128 && static_buf[len] != 0 {
+            len += 1;
+        }
+
+        // SAFETY: `static_buf` is promoted to static memory, and we verified the null byte.
+        unsafe {
+            crate::str::CStr::from_bytes_with_nul_unchecked(core::slice::from_raw_parts(
+                static_buf.as_ptr(),
+                len + 1,
+            ))
+        }
+    };
+
     /// Creates a new [`Devres`] instance of the given `data`.
     ///
     /// The `data` encapsulated within the returned `Devres` instance' `data` will be
@@ -209,9 +245,8 @@ pub fn new<E>(dev: &Device<Bound>, data: impl PinInit<T, E>) -> Result<Self>
                     unsafe {
                         base::devres_set_node_dbginfo(
                             node,
-                            // TODO: Use `core::any::type_name::<T>()` once it is a `const fn`,
-                            // such that we can convert the `&str` to a `&CStr` at compile-time.
-                            c"Devres<T>".as_char_ptr(),
+                            // Injects the statically promoted C-string pointer
+                            Self::TYPE_NAME_CSTR.as_char_ptr(),
                             core::mem::size_of::<Revocable<T>>(),
                         )
                     };
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index b72b2fbe046d..d49fd6edd85f 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -21,6 +21,7 @@
 //
 // Expected to become stable.
 #![feature(arbitrary_self_types)]
+#![feature(const_type_name)]
 #![feature(derive_coerce_pointee)]
 //
 // To be determined.
-- 
2.51.0


             reply	other threads:[~2026-05-26  9:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-26  9:43 Aary Milind Kinge [this message]
2026-05-26 11:58 ` [PATCH v2] rust: devres: optimize type name allocation and fix truncation Aary Milind Kinge
2026-05-26 12:37   ` Miguel Ojeda
2026-05-26 15:03     ` David Laight
2026-05-26 16:19   ` Greg KH
2026-05-27 10:50     ` Aary Kinge
2026-05-27 11:01 ` [PATCH] rust: devres: use compile-time type_name for debug info Gary Guo
2026-05-27 11:25   ` Miguel Ojeda

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=20260526094329.533943-1-kingeaary@gmail.com \
    --to=kingeaary@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.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

Powered by JetHome