mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: devres: use compile-time type_name for debug info
@ 2026-05-26  9:43 Aary Milind Kinge
  2026-05-26 11:58 ` [PATCH v2] rust: devres: optimize type name allocation and fix truncation Aary Milind Kinge
  2026-05-27 11:01 ` [PATCH] rust: devres: use compile-time type_name for debug info Gary Guo
  0 siblings, 2 replies; 8+ messages in thread
From: Aary Milind Kinge @ 2026-05-26  9:43 UTC (permalink / raw)
  To: Miguel Ojeda; +Cc: Alice Ryhl, rust-for-linux, linux-kernel, Not-Buddy

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


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

end of thread, other threads:[~2026-05-27 11:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-26  9:43 [PATCH] rust: devres: use compile-time type_name for debug info Aary Milind Kinge
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

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