From nobody Mon Jun 8 21:58:56 2026 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 DA5BA3D75B1 for ; Tue, 26 May 2026 09:44:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779788647; cv=none; b=CNJC/f+o3RfZeErEnIiQGp8mbg2uMK3zbKZl1Bls80BnUBbEEbNOxpxJ2WCN7RnnfLQqnitFUUDm0dZxLIeLa8MhDPOMqftiWxYU1J4FCPmfBsWy5BgLwoq7ANYu/KWuHmK7Gqm/KU84gs/vVGf30/MBaX1cKPpubxxpRiRPqW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779788647; c=relaxed/simple; bh=xSBymoxFsw8QxwUqEJx9/I737cxoIr9CI0TAp+DKvLs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=FuDPTUSTiQNsMWyUm/BWbm2pLP/U3lCcz8Df6ZuaIYRI7oYk+qEddHv6aJpzzRsSYNAx9roIus/RvhB11JZ/FFS5uq78bbhTzhjGdSxammCbjFpaGS5Ll/bEvOLvJAjRu/orkK/sRXIJ5NbZbQU7zhRuTN7Hfe5d8Z3kDOM9X5s= 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=EU4FtfMw; arc=none smtp.client-ip=209.85.216.53 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="EU4FtfMw" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-36af4b7840aso980111a91.3 for ; Tue, 26 May 2026 02:44:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779788645; x=1780393445; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=givk+B9IBegvbSx5HWlHHmcrLx0wxGpQj5mKDfbiDXI=; b=EU4FtfMwqOm6tApiegisDnSRVSR+z1tobmwGVU2Of7vIh9hdlXUSlVpW0TV+1lZ2mb dS0NqwDP6UtVqbiHxCKBZXyhXpVpz4AunkVe8UY3XI+XPAcxsQjTEDZDTFJIVFyuHKKG nfgwHnQx+joDiqPba2g764W3gUqXk1RI6UwBMsGw2dW3KJNxxVR2bDiMra3V/OcAyyCq CkIMtbH8MWpkOhwEXf1t1aFD3mEB/z18QD84dFCA4+MHll0DJ+BBVUgUxE5yng58XKLq yDlN/eQ3oYASco6HHbzuZMzk9kwJhKubfZtUUDFz5s217q0hz1chx6NbXzH1lJx18uxL 6Ouw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779788645; x=1780393445; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=givk+B9IBegvbSx5HWlHHmcrLx0wxGpQj5mKDfbiDXI=; b=EMvbynlwDW0GmNQLl2OgGKRnQPF4deKVP2z/PJuAzoe1nkqdiGkR/Inw7hPHTA+fy4 Sz+rFyqppkIYOfKalQMt1rQotZI/MhiVnJann6jjLnGDUpaYlwHm5wplTw7++TQ6iFpp /ILAe4QmGZef9vKhqAZ6PDEoQHKd2iAd/xjF3jRdwmAHwjF1NK33hK3SzmPWbaVbRxd7 PIYDw1l1pDDhYzWMS1fGIah5K43Tc4lN85t9LmfOctZik0f2CGizcx02mAdF7UCNSBny 6IvTxUZiw78j9FbdFeSDS2M9MgCcEdK6SUSkGX1qdOLQUhixJT1sQdPc0JhNq6KvwTvP sO5Q== X-Forwarded-Encrypted: i=1; AFNElJ/b2sJuvaRSnENTSg1TKWrj0hkjIK4suXtZ0D4wnjZhkcp4p7NVK+6Mzl61xQGdpLY/TIXnE5r03PY5wNc=@vger.kernel.org X-Gm-Message-State: AOJu0YxupAI41w9wQUfnhRjPE/W8r2gluQEncvBP1qbyvS5bDpKZBhKG Y3oqcPZMl8HlYl1Yu71WZ4DNdg9h4BZWXfwLdZEQOJPCjFHtGrPMJgv9 X-Gm-Gg: Acq92OGSCHPUpbuqKToI1wa14I4pYhD108ZxcgsVSXvAXO0/3wiJUB3+K4cbWmMa2Rl BIVHidGAhCOMwZ1JCXOFygWO0NNbjuwv7PE3/0TkqISXDPtwtEjZp1jKW9w1CNaV6B8aaVwNs1d mp3DGF95zFhqqxDOmkoedrkw15k6WdBDyGMnlLqmGHI5apxBLt3x1NO7iyMbBtPS3Tck/uEVfXU vIU3bdWNVnVnHXKBCWi2NVQ8E9Qit4FsF0mdaY+rLCBM8bpCrpbbSOghUsU/XW4+DjsGkIxRugv uIInpU87ifuepGQT2YagkoQAJpFD5QSDn+H7ev3yu6fJjaGr5c64tGTOrInNlmHGxpm580qNWxV HodFi3apfE8KSAOYjui006D4xVfwO2GhjvWbiwRlkYmdR8mAwGMVAokHn5i08UG4xce8695AVzy 1BYiwnESV6Od+9PNyr8mgYV7VVR3g5Glfl7JfqNtYPsSW7IXxrAHtdy4xKw+da0hu5Okuq9guUQ GS+vi5+ItrG X-Received: by 2002:a17:90b:3505:b0:35c:30a8:330 with SMTP id 98e67ed59e1d1-36a671ddedcmr17296613a91.0.1779788645132; Tue, 26 May 2026 02:44:05 -0700 (PDT) Received: from Zenbook-Duo.domain.name ([2401:4900:884c:3eef:84d1:476b:9262:5d0c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36a6d810692sm5301016a91.9.2026.05.26.02.44.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2026 02:44:04 -0700 (PDT) From: Aary Milind Kinge To: Miguel Ojeda Cc: Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Not-Buddy Subject: [PATCH] rust: devres: use compile-time type_name for debug info Date: Tue, 26 May 2026 15:13:29 +0530 Message-ID: <20260526094329.533943-1-kingeaary@gmail.com> X-Mailer: git-send-email 2.51.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Not-Buddy The devres_set_node_dbginfo function previously used a hardcoded c"Devres" 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 --- 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( } =20 impl Devres { + // 1. Get the standard Rust string at compile time + const TYPE_NAME: &'static str =3D core::any::type_name::(); + + // 2. Store the actual bytes in a const array (a VALUE, not a referenc= e) + 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; + + while i < bytes.len() && i < 127 { + buf[i] =3D bytes[i]; + i +=3D 1; + } + buf[i] =3D 0; // The null terminator + buf + }; + + // 3. Take a reference to the array (which promotes it to static memor= y) + const TYPE_NAME_CSTR: &'static crate::str::CStr =3D { + let static_buf: &'static [u8; 128] =3D &Self::TYPE_NAME_BUF; + + // Find the length up to the null byte + 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, + )) + } + }; + /// Creates a new [`Devres`] instance of the given `data`. /// /// The `data` encapsulated within the returned `Devres` instance' `da= ta` will be @@ -209,9 +245,8 @@ 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(), + // Injects the statically promoted C-string po= inter + 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..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. --=20 2.51.0