[PATCH] rust: devres: use compile-time type_name for debug info

From: Aary Milind Kinge

Date: Tue May 26 2026 - 05:50:58 EST


From: Not-Buddy <kingeaary@xxxxxxxxx>

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@xxxxxxxxx>
---
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