public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: Ke Sun <sunke@kylinos.cn>
Cc: "Dirk Behme" <dirk.behme@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Petr Mladek" <pmladek@suse.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Benno Lossin" <lossin@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"John Ogness" <john.ogness@linutronix.de>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@gmail.com>,
	"Ke Sun" <sk.alvin.x@gmail.com>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v8 3/4] rust: fmt: Default raw pointer formatting to HashedPtr
Date: Fri, 2 Jan 2026 11:17:11 +0000	[thread overview]
Message-ID: <20260102111711.0b2f864e.gary@garyguo.net> (raw)
In-Reply-To: <20260101081605.1300953-4-sunke@kylinos.cn>

On Thu, 1 Jan 2026 16:16:03 +0800
Ke Sun <sunke@kylinos.cn> wrote:

> Make raw pointers (*const T, *mut T) automatically use HashedPtr when
> formatted with {:p}, providing safe default behavior for kernel pointers.
> 
> This allows users to format raw pointers directly:
>     pr_info!("{:p}\n", ptr);  // Automatically hashed
> 
> While still allowing explicit use of wrapper types when needed:
>     pr_info!("{:p}\n", RawPtr(ptr));
> 
> Signed-off-by: Ke Sun <sunke@kylinos.cn>
> ---
>  rust/kernel/fmt.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 63 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/fmt.rs b/rust/kernel/fmt.rs
> index 84d634201d90a..8be185d522c01 100644
> --- a/rust/kernel/fmt.rs
> +++ b/rust/kernel/fmt.rs
> @@ -6,6 +6,11 @@
>  
>  pub use core::fmt::{Arguments, Debug, Error, Formatter, Result, Write};
>  
> +use crate::ptr::{
> +    HashedPtr,
> +    RawPtr, //
> +};
> +
>  /// Internal adapter used to route allow implementations of formatting traits for foreign types.
>  ///
>  /// It is inserted automatically by the [`fmt!`] macro and is not meant to be used directly.
> @@ -28,7 +33,64 @@ fn fmt(&self, f: &mut Formatter<'_>) -> Result {
>  }
>  
>  use core::fmt::{Binary, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex};
> -impl_fmt_adapter_forward!(Debug, LowerHex, UpperHex, Octal, Binary, Pointer, LowerExp, UpperExp);
> +impl_fmt_adapter_forward!(Debug, LowerHex, UpperHex, Octal, Binary, LowerExp, UpperExp);
> +
> +// Special handling for Pointer: default to HashedPtr for raw pointers.
> +// This overrides the default Pointer implementation for raw pointers to use hashing,
> +// which is the safe default behavior for kernel pointers.
> +impl<T> Pointer for Adapter<*const T> {
> +    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +        let Self(ptr) = self;
> +        Pointer::fmt(&HashedPtr(*ptr), f)
> +    }
> +}
> +
> +impl<T> Pointer for Adapter<*mut T> {
> +    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +        let Self(ptr) = self;
> +        Pointer::fmt(&HashedPtr(*ptr), f)
> +    }
> +}
> +
> +// Handle references to raw pointers (needed when pointers are passed by reference in macros).
> +impl<T> Pointer for Adapter<&*const T> {
> +    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +        let Self(ptr) = self;
> +        Pointer::fmt(&HashedPtr(**ptr), f)
> +    }
> +}
> +
> +impl<T> Pointer for Adapter<&*mut T> {
> +    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +        let Self(ptr) = self;
> +        Pointer::fmt(&HashedPtr(**ptr), f)
> +    }
> +}
> +
> +// For wrapper types that implement Pointer (like HashedPtr, RawPtr),
> +// forward to their implementation. This allows explicit wrapper types to use their
> +// own formatting logic instead of being converted to HashedPtr.
> +macro_rules! impl_pointer_adapter_forward {
> +    ($($ty:ident),* $(,)?) => {
> +        $(
> +            impl<T> Pointer for Adapter<$ty<T>> {
> +                fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +                    let Self(t) = self;
> +                    Pointer::fmt(t, f)
> +                }
> +            }
> +
> +            impl<T> Pointer for Adapter<&$ty<T>> {
> +                fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> +                    let Self(t) = self;
> +                    Pointer::fmt(*t, f)
> +                }
> +            }
> +        )*
> +    };
> +}
> +
> +impl_pointer_adapter_forward!(HashedPtr, RawPtr);

This implementation means that nobody can implement the `Pointer` trait
outside the kernel crate.

Should we do something similar to `Display` trait and have a kernel copy
so people can still have their custom `Pointer` implementation?

Best,
Gary

>  
>  /// A copy of [`core::fmt::Display`] that allows us to implement it for foreign types.
>  ///


  reply	other threads:[~2026-01-02 11:17 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-01  8:16 [PATCH v8 0/4] rust: Add safe pointer formatting support Ke Sun
2026-01-01  8:16 ` [PATCH v8 1/4] lib/vsprintf: Export ptr_to_hashval() for Rust kernel crate use Ke Sun
2026-01-02 12:15   ` Andy Shevchenko
2026-01-01  8:16 ` [PATCH v8 2/4] rust: kernel: Add pointer wrapper types for safe pointer formatting Ke Sun
2026-01-02  7:57   ` Dirk Behme
2026-01-02 11:06     ` Gary Guo
2026-01-02 11:13   ` Gary Guo
2026-01-02 12:17   ` Andy Shevchenko
2026-01-02 12:33     ` Danilo Krummrich
2026-01-02 12:43       ` Ke Sun
2026-01-01  8:16 ` [PATCH v8 3/4] rust: fmt: Default raw pointer formatting to HashedPtr Ke Sun
2026-01-02 11:17   ` Gary Guo [this message]
2026-01-02 17:39   ` Petr Mladek
2026-01-05  8:19     ` Alice Ryhl
2026-01-06  3:06       ` Ke Sun
2026-01-06 16:45       ` Petr Mladek
2026-01-06 17:18         ` Alice Ryhl
2026-01-06 21:00           ` Andy Shevchenko
2026-01-06 21:06             ` Alice Ryhl
2026-01-01  8:16 ` [PATCH v8 4/4] docs: rust: Add pointer formatting documentation Ke Sun

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=20260102111711.0b2f864e.gary@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dirk.behme@gmail.com \
    --cc=john.ogness@linutronix.de \
    --cc=linux@rasmusvillemoes.dk \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sk.alvin.x@gmail.com \
    --cc=sunke@kylinos.cn \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    /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