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,
Aary Milind Kinge <kingeaary@gmail.com>
Subject: [PATCH v2] rust: devres: optimize type name allocation and fix truncation
Date: Tue, 26 May 2026 17:28:25 +0530 [thread overview]
Message-ID: <20260526115825.1480768-1-kingeaary@gmail.com> (raw)
In-Reply-To: <20260526094329.533943-1-kingeaary@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-05-26 11:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-26 9:43 [PATCH] rust: devres: use compile-time type_name for debug info Aary Milind Kinge
2026-05-26 11:58 ` Aary Milind Kinge [this message]
2026-05-26 12:37 ` [PATCH v2] rust: devres: optimize type name allocation and fix truncation 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=20260526115825.1480768-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
all inboxes | Powered by JetHome®