From nobody Thu Oct 2 02:04:51 2025 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (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 D0E5327990D for ; Thu, 25 Sep 2025 13:43:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758807787; cv=none; b=uMmjN3MPPVAEY0ejlI0AoDrkxarlVOwPlYoDvPfqHSPSVbDJnfMkXODUSuP3tKq80/NpuvItnxPELqIWaMynLjPCx0Zi+I+Fk8YwiDTjxI76ISLvvIIA8+lMS5H0TzyMbIwccCGdLriBfNw26bEzXxUcTI8GrKruaJGHIVHFOKw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758807787; c=relaxed/simple; bh=56qk4oXZToDCRjUenEK2FztuuA7wWME8i0VRUMgg2i0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=s5ho5QeSLAtfIK+yrl6PPFD+3twsqEEufLUQa309+l+8bPhnhGVGpE5XmD+cn9sd+8bC37oCRFVaVd6zxzDp7jVvZkIvZz+KFKAcNYYFi146F9fr+P5fhYGt7rgoaSRnQWkuzuwrqhrA1WmzhYfVuCi8EYW+oNY6SF+uHtHAitQ= 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=fPqwqYGk; arc=none smtp.client-ip=209.85.222.180 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="fPqwqYGk" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-80b7a6b2b47so126765185a.0 for ; Thu, 25 Sep 2025 06:43:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758807784; x=1759412584; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=HiEqMd3xsyy/ksNexRFAfYvN9OsAs0xxvpBCNib+PpM=; b=fPqwqYGkAPyS2GsHS3ZjEMWjFhNCbocZjRUMb1PJ8s0mnO3m5Q7ZLbZB6gXiS6MkpN zJIuGHzg3Pg6g2mpfzxAqdAtWPluEqAejLGJq9eIf58ujpqDvS+YBWxiYdae4nOpJZI0 GCxYyLWEEJwGd5lIPpbTE2mD/2coZMfW08utmOksup6o4DX7AEE0DPxZY1uXd8idP0EG +4VVzkIbLzT7qqLZbFl9wNfyPo4LlxXMRwKPTDuNljoG7bppKUNoCw8T+08iojPm49es oBfK7W5j6Maxg4KPDLHfyKSplP3zBn3gzy9SdbGIHkoOneW/cVinJTaNjrpcSnuV1yAO Y5bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758807784; x=1759412584; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HiEqMd3xsyy/ksNexRFAfYvN9OsAs0xxvpBCNib+PpM=; b=UPahJbqob+AqZhaU3D04jIO5s/j0Mm4pRFgrADMdH2ZZk6AyzuIUMR9tVLA9iRXCXh S0Px8JMhRFyc4JFI5p6ZKIBrkjqZtQyr3wJTz1cXuIVuaY4qMHGR6K6SfZ8nEqESxqIp hnTnp+1wOYSj1XdOdWCrhctUIx9G8ghunJZ4jkzQ/N/eEJd1O1yJWdYw5yKyQ2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SMTP id 6a1803df08f44-7fc374b2447mr37447426d6.15.1758807783375; Thu, 25 Sep 2025 06:43:03 -0700 (PDT) Received: from 137.1.168.192.in-addr.arpa ([2600:4808:6353:5c00:7c:b286:dba3:5ba8]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-800fa5f6dd7sm11852546d6.0.2025.09.25.06.43.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Sep 2025 06:43:02 -0700 (PDT) From: Tamir Duberstein Date: Thu, 25 Sep 2025 09:42:53 -0400 Subject: [PATCH v16 2/3] rust: support formatting of foreign types 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 Message-Id: <20250925-cstr-core-v16-2-5cdcb3470ec2@gmail.com> References: <20250925-cstr-core-v16-0-5cdcb3470ec2@gmail.com> In-Reply-To: <20250925-cstr-core-v16-0-5cdcb3470ec2@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Luis Chamberlain , Russ Weight , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Christian Brauner , Alexander Viro , Jan Kara Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-fsdevel@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openssh-sha256; t=1758807776; l=11126; i=tamird@gmail.com; h=from:subject:message-id; bh=56qk4oXZToDCRjUenEK2FztuuA7wWME8i0VRUMgg2i0=; b=U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgtYz36g7iDMSkY5K7Ab51ksGX7hJgs MRt+XVZTrIzMVIAAAAGcGF0YXR0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5AAAA QFS5FBG5hG6X0Xkek0CL7w9zjaW6ziGcSIXaI/fHnn4mWXQaWHA2ook+z/gflzaX3Y+2kzogSEE fyz6dlk+WKw8= X-Developer-Key: i=tamird@gmail.com; a=openssh; fpr=SHA256:264rPmnnrb+ERkS7DDS3tuwqcJss/zevJRzoylqMsbc Introduce a `fmt!` macro which wraps all arguments in `kernel::fmt::Adapter` and a `kernel::fmt::Display` trait. This enables formatting of foreign types (like `core::ffi::CStr`) that do not implement `core::fmt::Display` due to concerns around lossy conversions which do not apply in the kernel. Suggested-by: Alice Ryhl Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/t= opic/Custom.20formatting/with/516476467 Acked-by: Greg Kroah-Hartman Reviewed-by: Alice Ryhl Acked-by: Danilo Krummrich Reviewed-by: Benno Lossin Signed-off-by: Tamir Duberstein --- rust/kernel/fmt.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++- rust/kernel/prelude.rs | 3 +- rust/macros/fmt.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ rust/macros/lib.rs | 19 ++++++++++ rust/macros/quote.rs | 7 ++++ 5 files changed, 207 insertions(+), 3 deletions(-) diff --git a/rust/kernel/fmt.rs b/rust/kernel/fmt.rs index 0306e8388968..84d634201d90 100644 --- a/rust/kernel/fmt.rs +++ b/rust/kernel/fmt.rs @@ -4,4 +4,89 @@ //! //! This module is intended to be used in place of `core::fmt` in kernel c= ode. =20 -pub use core::fmt::{Arguments, Debug, Display, Error, Formatter, Result, W= rite}; +pub use core::fmt::{Arguments, Debug, Error, Formatter, Result, Write}; + +/// Internal adapter used to route allow implementations of formatting tra= its for foreign types. +/// +/// It is inserted automatically by the [`fmt!`] macro and is not meant to= be used directly. +/// +/// [`fmt!`]: crate::prelude::fmt! +#[doc(hidden)] +pub struct Adapter(pub T); + +macro_rules! impl_fmt_adapter_forward { + ($($trait:ident),* $(,)?) =3D> { + $( + impl $trait for Adapter { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + let Self(t) =3D self; + $trait::fmt(t, f) + } + } + )* + }; +} + +use core::fmt::{Binary, LowerExp, LowerHex, Octal, Pointer, UpperExp, Uppe= rHex}; +impl_fmt_adapter_forward!(Debug, LowerHex, UpperHex, Octal, Binary, Pointe= r, LowerExp, UpperExp); + +/// A copy of [`core::fmt::Display`] that allows us to implement it for fo= reign types. +/// +/// Types should implement this trait rather than [`core::fmt::Display`]. = Together with the +/// [`Adapter`] type and [`fmt!`] macro, it allows for formatting foreign = types (e.g. types from +/// core) which do not implement [`core::fmt::Display`] directly. +/// +/// [`fmt!`]: crate::prelude::fmt! +pub trait Display { + /// Same as [`core::fmt::Display::fmt`]. + fn fmt(&self, f: &mut Formatter<'_>) -> Result; +} + +impl Display for &T { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Display::fmt(*self, f) + } +} + +impl core::fmt::Display for Adapter<&T> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + let Self(t) =3D self; + Display::fmt(t, f) + } +} + +macro_rules! impl_display_forward { + ($( + $( { $($generics:tt)* } )? $ty:ty $( { where $($where:tt)* } )? + ),* $(,)?) =3D> { + $( + impl$($($generics)*)? Display for $ty $(where $($where)*)? { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + core::fmt::Display::fmt(self, f) + } + } + )* + }; +} + +impl_display_forward!( + bool, + char, + core::panic::PanicInfo<'_>, + Arguments<'_>, + i128, + i16, + i32, + i64, + i8, + isize, + str, + u128, + u16, + u32, + u64, + u8, + usize, + {} crate::sync::Arc {where crate::sync::Arc: core::fm= t::Display}, + {} crate::sync::UniqueArc {where crate::sync::UniqueArc<= T>: core::fmt::Display}, +); diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index 198d09a31449..26424ad7e989 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -25,7 +25,7 @@ pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec= , Vec}; =20 #[doc(no_inline)] -pub use macros::{export, kunit_tests, module, vtable}; +pub use macros::{export, fmt, kunit_tests, module, vtable}; =20 pub use pin_init::{init, pin_data, pin_init, pinned_drop, InPlaceWrite, In= it, PinInit, Zeroable}; =20 @@ -36,7 +36,6 @@ pub use super::dbg; pub use super::{dev_alert, dev_crit, dev_dbg, dev_emerg, dev_err, dev_info= , dev_notice, dev_warn}; pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr= _notice, pr_warn}; -pub use core::format_args as fmt; =20 pub use super::{try_init, try_pin_init}; =20 diff --git a/rust/macros/fmt.rs b/rust/macros/fmt.rs new file mode 100644 index 000000000000..2f4b9f6e2211 --- /dev/null +++ b/rust/macros/fmt.rs @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0 + +use proc_macro::{Ident, TokenStream, TokenTree}; +use std::collections::BTreeSet; + +/// Please see [`crate::fmt`] for documentation. +pub(crate) fn fmt(input: TokenStream) -> TokenStream { + let mut input =3D input.into_iter(); + + let first_opt =3D input.next(); + let first_owned_str; + let mut names =3D BTreeSet::new(); + let first_span =3D { + let Some((mut first_str, first_span)) =3D (match first_opt.as_ref(= ) { + Some(TokenTree::Literal(first_lit)) =3D> { + first_owned_str =3D first_lit.to_string(); + Some(first_owned_str.as_str()).and_then(|first| { + let first =3D first.strip_prefix('"')?; + let first =3D first.strip_suffix('"')?; + Some((first, first_lit.span())) + }) + } + _ =3D> None, + }) else { + return first_opt.into_iter().chain(input).collect(); + }; + + // Parse `identifier`s from the format string. + // + // See https://doc.rust-lang.org/std/fmt/index.html#syntax. + while let Some((_, rest)) =3D first_str.split_once('{') { + first_str =3D rest; + if let Some(rest) =3D first_str.strip_prefix('{') { + first_str =3D rest; + continue; + } + if let Some((name, rest)) =3D first_str.split_once('}') { + first_str =3D rest; + let name =3D name.split_once(':').map_or(name, |(name, _)|= name); + if !name.is_empty() && !name.chars().all(|c| c.is_ascii_di= git()) { + names.insert(name); + } + } + } + first_span + }; + + let adapter =3D quote_spanned!(first_span =3D> ::kernel::fmt::Adapter); + + let mut args =3D TokenStream::from_iter(first_opt); + { + let mut flush =3D |args: &mut TokenStream, current: &mut TokenStre= am| { + let current =3D std::mem::take(current); + if !current.is_empty() { + let (lhs, rhs) =3D (|| { + let mut current =3D current.into_iter(); + let mut acc =3D TokenStream::new(); + while let Some(tt) =3D current.next() { + // Split on `=3D` only once to handle cases like `= a =3D b =3D c`. + if matches!(&tt, TokenTree::Punct(p) if p.as_char(= ) =3D=3D '=3D') { + names.remove(acc.to_string().as_str()); + // Include the `=3D` itself to keep the handli= ng below uniform. + acc.extend([tt]); + return (Some(acc), current.collect::()); + } + acc.extend([tt]); + } + (None, acc) + })(); + args.extend(quote_spanned!(first_span =3D> #lhs #adapter(&= #rhs))); + } + }; + + let mut current =3D TokenStream::new(); + for tt in input { + match &tt { + TokenTree::Punct(p) if p.as_char() =3D=3D ',' =3D> { + flush(&mut args, &mut current); + &mut args + } + _ =3D> &mut current, + } + .extend([tt]); + } + flush(&mut args, &mut current); + } + + for name in names { + let name =3D Ident::new(name, first_span); + args.extend(quote_spanned!(first_span =3D> , #name =3D #adapter(&#= name))); + } + + quote_spanned!(first_span =3D> ::core::format_args!(#args)) +} diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index fa847cf3a9b5..793f712dbf7c 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -15,6 +15,7 @@ mod quote; mod concat_idents; mod export; +mod fmt; mod helpers; mod kunit; mod module; @@ -201,6 +202,24 @@ pub fn export(attr: TokenStream, ts: TokenStream) -> T= okenStream { export::export(attr, ts) } =20 +/// Like [`core::format_args!`], but automatically wraps arguments in [`ke= rnel::fmt::Adapter`]. +/// +/// This macro allows generating `fmt::Arguments` while ensuring that each= argument is wrapped with +/// `::kernel::fmt::Adapter`, which customizes formatting behavior for ker= nel logging. +/// +/// Named arguments used in the format string (e.g. `{foo}`) are detected = and resolved from local +/// bindings. All positional and named arguments are automatically wrapped. +/// +/// This macro is an implementation detail of other kernel logging macros = like [`pr_info!`] and +/// should not typically be used directly. +/// +/// [`kernel::fmt::Adapter`]: ../kernel/fmt/struct.Adapter.html +/// [`pr_info!`]: ../kernel/macro.pr_info.html +#[proc_macro] +pub fn fmt(input: TokenStream) -> TokenStream { + fmt::fmt(input) +} + /// Concatenate two identifiers. /// /// This is useful in macros that need to declare or reference items with = names diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs index acc140c18653..ddfc21577539 100644 --- a/rust/macros/quote.rs +++ b/rust/macros/quote.rs @@ -48,6 +48,7 @@ macro_rules! quote_spanned { ($span:expr =3D> $($tt:tt)*) =3D> {{ let mut tokens =3D ::proc_macro::TokenStream::new(); { + #[allow(unused_variables)] let span =3D $span; quote_spanned!(@proc tokens span $($tt)*); } @@ -146,6 +147,12 @@ macro_rules! quote_spanned { )]); quote_spanned!(@proc $v $span $($tt)*); }; + (@proc $v:ident $span:ident & $($tt:tt)*) =3D> { + $v.extend([::proc_macro::TokenTree::Punct( + ::proc_macro::Punct::new('&', ::proc_macro::Spacing::Alone), + )]); + quote_spanned!(@proc $v $span $($tt)*); + }; (@proc $v:ident $span:ident _ $($tt:tt)*) =3D> { $v.extend([::proc_macro::TokenTree::Ident( ::proc_macro::Ident::new("_", $span), --=20 2.51.0