mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Aary Milind Kinge <kingeaary@gmail.com>
Cc: Miguel Ojeda <ojeda@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: devres: optimize type name allocation and fix truncation
Date: Tue, 26 May 2026 18:19:26 +0200	[thread overview]
Message-ID: <2026052607-underhand-mauve-dcaa@gregkh> (raw)
In-Reply-To: <20260526115825.1480768-1-kingeaary@gmail.com>

On Tue, May 26, 2026 at 05:28:25PM +0530, Aary Milind Kinge wrote:
> 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
> 
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- This looks like a new version of a previously submitted patch, but you
  did not list below the --- line any changes from the previous version.
  Please read the section entitled "The canonical patch format" in the
  kernel file, Documentation/process/submitting-patches.rst for what
  needs to be done here to properly describe this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot

  parent reply	other threads:[~2026-05-26 16:20 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 ` [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 [this message]
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=2026052607-underhand-mauve-dcaa@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=aliceryhl@google.com \
    --cc=kingeaary@gmail.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