From nobody Mon Jun 8 21:46:13 2026 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5711F3F7A82 for ; Tue, 26 May 2026 11:58:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779796737; cv=none; b=ZPvWKuBVBX6gqMpuZK95y8brBnUwi8qo+cAeOgihqccsX/0VJuF2uirQc91FSICqPjSs7agp5v3DNSZDqvaSqfF8LbEBLrASK9tCUPBk/dlW9Urhwzrtce8x1tPi9pz4SSxWShiVliZxq+i7GBs8/TrIvVJYCJ7oeoLj4FUkJls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779796737; c=relaxed/simple; bh=OJ+7WARB6N+fmIk2dER+UhbV1OiqJ7xjdzGz3EOuTHQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kE/SieTtgEPjKx18ZiuOVJSIe9+jrhvGelnc35n2QHqO5U9qqgcpdn8xh7ke9up0TBlbNppwzQZK04B6opvJtH7/4Zp6LFWcT2M8UiagItyIpfWGp3sJx3+MCfQjKjp6zm1l6c9ZVXqRvcW7+a2Yflye4OeSDoCUjfkSwXpW0rw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VCAg474F; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VCAg474F" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-c7b9f54d3deso8584132a12.1 for ; Tue, 26 May 2026 04:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779796724; x=1780401524; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=g8unaYiQY+V8GcRg3BTBUGdma2PqnW6N3aPu9s5QNeI=; b=VCAg474Fd+bBrUCGMThU6H+s4Qc3gjtWXsT/tP9X3JEZZqWnfjSqxyZXgrr9R+Tuds dBt6Kh4yOYE3MpzEs6VjMA+hylJ5tOgtUdvHbW47/PHDJ+llj7NXz4/djUn4SikbNQ8y S+lRn+r+kCOpnDIIZLddjIg5fkfyWPV5FZJiDfZmCDKjHHuldWm4IUppKMSQyKixo3HN Vf94vx5yCML9mixc5EzPcDRMfVyPh3pwVM3k+a0IXiCju/JDIGUkjgkVW50SZEoK/Rxd rhHp3eY2t6XYaaYU3AgGsGAyvjyEP+c0pvweZYzgNbPsiOIMJFuGr8itbUcFljJDFD4E OuFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779796724; x=1780401524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=g8unaYiQY+V8GcRg3BTBUGdma2PqnW6N3aPu9s5QNeI=; b=WPKQyY8fWoAbnIZr71xxNDVxVSheGjmsMXB/hZywk1xxptm9+TVPmkssTYDoSODlmL RawYbfwk+x1UywskrPyvZTwK2yJuXClgXbzQSqbGTxFnGL3LCsSa0Mkn0WR/1ydgTEJd eyAzgX3isp156NUUoYGlja6NaZbZrdXRrJFVMtiDVrfcps+z+w4IJw83fhu3dRYFy1R5 QuJkjk3su1nqsbccI0vhSrpToBnmxqu/2uCzR9drBmnFFtRLWEfkV5T8ZrN/H5S6CZx+ Zz3c59WgFL1o1jqn/1+fdGP6Md+osjN5uYSBK7SQvImt4i2kAny1gg7a3l/RcMj9zwAy BEMQ== X-Forwarded-Encrypted: i=1; AFNElJ+LugWmry+5TC7OyEGcacnWJoMTDrba8PkycD1itLShR/yiz2XMvyLbD+1/qiMhSo65e901rXoEBuhYD9s=@vger.kernel.org X-Gm-Message-State: AOJu0Yy2TmZY3i0Zt56USivn4CyIJ0ww+HM09EYQdcCJpt0o3m0e+wdX WzvffEYZxFIoEpbL0ioMa7IA9YZMl86qWNpv9XgiFZicrU9EIlGuFV0h X-Gm-Gg: Acq92OHK4PveQEeo+VruHiuQtHJQ7y66oNQp8P2TSs91b5IZN5Ir2oJ3f8vk4snS7Sb viQmjpaAjv7sjj52e9ABOOUu/ylKNqbN8aTTB8sTbbf9Kcqaqzky2/jrQDFVyGuw0zfX3+bTkR+ NTKrMeq9BWl7eM7o9HM0pOwBNkkIXzitB4mo6hHt2Rxs6tkAYG3fepE8K9edO5+05CrOysBtaA2 ejRl2eEWkVZYWVmLBTrS4BYBXtG7pgNPmBdoSkd/AidS7YdJ3w0D9eiARuGkrCD9vhhM8jZp5c6 /6PAUScoGIL5WS7iFLCHRqbyTONO3hKn+RBIvdwozqSaJNbXR/aAYzEnR7K5NpCiIgvkdZarPp4 KV8+9YIjb190bSeGheAXoLPs90cy7CpGGb5w6GkJZMFHNPqqjkMg/xzIvG+RdYce3C+AQRntU3q wnqQaq5Afa+5nz70wMK+P1IDFPfVEs/jLQ/vYmiXcdLV+flORNZPeS+e9Qw2d5Koiu3VOjoV0ay Kn7ATBgDWgP1TVUVQ== X-Received: by 2002:a05:6a21:164a:b0:3a2:bd1d:d679 with SMTP id adf61e73a8af0-3b328fd02e8mr19252415637.48.1779796724176; Tue, 26 May 2026 04:58:44 -0700 (PDT) Received: from Zenbook-Duo.tailc6e68.ts.net ([2401:4900:884c:3eef:47fa:897:ce83:6985]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84164fe0bb8sm11355693b3a.51.2026.05.26.04.58.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2026 04:58:43 -0700 (PDT) From: Aary Milind Kinge To: Miguel Ojeda Cc: Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Aary Milind Kinge Subject: [PATCH v2] rust: devres: optimize type name allocation and fix truncation Date: Tue, 26 May 2026 17:28:25 +0530 Message-ID: <20260526115825.1480768-1-kingeaary@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260526094329.533943-1-kingeaary@gmail.com> References: <20260526094329.533943-1-kingeaary@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The unconditional 128-byte const array allocation for every unique `Devres` 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 =E2=80=94 copying only the first 124 bytes =E2=80=94 to clearly indicate truncation rather th= an silently dropping trailing characters. Signed-off-by: Aary Milind Kinge --- 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( } =20 impl Devres { + #[cfg(CONFIG_DEBUG_DEVRES)] + const TYPE_NAME: &'static str =3D core::any::type_name::(); + + #[cfg(CONFIG_DEBUG_DEVRES)] + const TYPE_NAME_BUF: [u8; 128] =3D { + let bytes =3D Self::TYPE_NAME.as_bytes(); + let mut buf =3D [0u8; 128]; + let mut i =3D 0; + + if bytes.len() > 127 { + // Copy exactly 124 bytes, then append '...' to clearly indica= te truncation + while i < 124 { + buf[i] =3D bytes[i]; + i +=3D 1; + } + buf[124] =3D b'.'; + buf[125] =3D b'.'; + buf[126] =3D b'.'; + buf[127] =3D 0; // Null terminator + } else { + // Copy normally + while i < bytes.len() { + buf[i] =3D bytes[i]; + i +=3D 1; + } + buf[i] =3D 0; // Null terminator + } + buf + }; + + #[cfg(CONFIG_DEBUG_DEVRES)] + const TYPE_NAME_CSTR: &'static crate::str::CStr =3D { + let static_buf: &'static [u8; 128] =3D &Self::TYPE_NAME_BUF; + + let mut len =3D 0; + while len < 128 && static_buf[len] !=3D 0 { + len +=3D 1; + } + + // SAFETY: `static_buf` is promoted to static memory, and we verif= ied the null byte. + unsafe { + crate::str::CStr::from_bytes_with_nul_unchecked(core::slice::f= rom_raw_parts( + static_buf.as_ptr(), + len + 1, + )) + } + }; + + #[cfg(not(CONFIG_DEBUG_DEVRES))] + const TYPE_NAME_CSTR: &'static crate::str::CStr =3D crate::c_str!(""); + /// Creates a new [`Devres`] instance of the given `data`. /// /// The `data` encapsulated within the returned `Devres` instance' `da= ta` will be @@ -209,9 +260,7 @@ pub fn new(dev: &Device, data: impl PinInit) -> Result unsafe { base::devres_set_node_dbginfo( node, - // TODO: Use `core::any::type_name::()` onc= e it is a `const fn`, - // such that we can convert the `&str` to a `&= CStr` at compile-time. - c"Devres".as_char_ptr(), + Self::TYPE_NAME_CSTR.as_char_ptr(), core::mem::size_of::>(), ) }; 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)] --=20 2.51.0