[PATCH v2] rust: devres: optimize type name allocation and fix truncation

From: Aary Milind Kinge

Date: Tue May 26 2026 - 08:07:04 EST


The unconditional 128-byte const array allocation for every unique
`Devres<T>` caused unnecessary .rodata bloat in production builds,
and type names exceeding 127 bytes were silently truncated without
any indication in debug logs.

Gate the `TYPE_NAME`, `TYPE_NAME_BUF`, and `TYPE_NAME_CSTR` constants
behind `#[cfg(CONFIG_DEBUG_DEVRES)]` so the 128-byte buffer is only
allocated when device resource debugging is enabled. For production
builds, use `crate::c_str!("")` as a zero-cost empty C-string fallback.

When the type name is too long to fit in the 127-byte buffer (plus null
terminator), the copy routine now appends "..." at the end — copying
only the first 124 bytes — to clearly indicate truncation rather than
silently dropping trailing characters.

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

diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
index 9e5f93aed20c..79cfe7d9a415 100644
--- a/rust/kernel/devres.rs
+++ b/rust/kernel/devres.rs
@@ -185,6 +185,57 @@ pub(super) unsafe fn devres_node_remove(
}

impl<T: Send> Devres<T> {
+ #[cfg(CONFIG_DEBUG_DEVRES)]
+ const TYPE_NAME: &'static str = core::any::type_name::<T>();
+
+ #[cfg(CONFIG_DEBUG_DEVRES)]
+ const TYPE_NAME_BUF: [u8; 128] = {
+ let bytes = Self::TYPE_NAME.as_bytes();
+ let mut buf = [0u8; 128];
+ let mut i = 0;
+
+ if bytes.len() > 127 {
+ // Copy exactly 124 bytes, then append '...' to clearly indicate truncation
+ while i < 124 {
+ buf[i] = bytes[i];
+ i += 1;
+ }
+ buf[124] = b'.';
+ buf[125] = b'.';
+ buf[126] = b'.';
+ buf[127] = 0; // Null terminator
+ } else {
+ // Copy normally
+ while i < bytes.len() {
+ buf[i] = bytes[i];
+ i += 1;
+ }
+ buf[i] = 0; // Null terminator
+ }
+ buf
+ };
+
+ #[cfg(CONFIG_DEBUG_DEVRES)]
+ const TYPE_NAME_CSTR: &'static crate::str::CStr = {
+ let static_buf: &'static [u8; 128] = &Self::TYPE_NAME_BUF;
+
+ 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,
+ ))
+ }
+ };
+
+ #[cfg(not(CONFIG_DEBUG_DEVRES))]
+ const TYPE_NAME_CSTR: &'static crate::str::CStr = crate::c_str!("");
+
/// Creates a new [`Devres`] instance of the given `data`.
///
/// The `data` encapsulated within the returned `Devres` instance' `data` will be
@@ -209,9 +260,7 @@ 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(),
+ 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..48df181788f6 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -22,6 +22,7 @@
// Expected to become stable.
#![feature(arbitrary_self_types)]
#![feature(derive_coerce_pointee)]
+#![feature(const_type_name)]
//
// To be determined.
#![feature(used_with_arg)]
--
2.51.0