public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
Cc: ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net,
	 bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@kernel.org,  tmgross@umich.edu, dakr@kernel.org,
	tamird@kernel.org,  gregkh@linuxfoundation.org,
	fujita.tomonori@gmail.com,  andrewjballance@gmail.com,
	rust-for-linux@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: print: add safety comments for %pA formatting
Date: Thu, 5 Feb 2026 08:19:49 +0000	[thread overview]
Message-ID: <aYRSpYCIaRmsNvip@google.com> (raw)
In-Reply-To: <20260205042132.40772-1-muchamadcoirulanwar@gmail.com>

On Thu, Feb 05, 2026 at 11:21:32AM +0700, Muchamad Coirul Anwar wrote:
> The safety comments in `rust_fmt_argument` and `call_printk` were
> previously marked as TODO.
> 
> This patch adds the missing safety documentation explaining why
> dereferencing the pointers and calling the C `_printk` function
> is safe in these contexts. It clarifies the contract between
> `lib/vsprintf.c` and the Rust implementation regarding the `%pA`
> format specifier.
> 
> Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
> ---
>  rust/kernel/print.rs | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
> index 6fd84389a858..3e6ff8f42d95 100644
> --- a/rust/kernel/print.rs
> +++ b/rust/kernel/print.rs
> @@ -29,7 +29,11 @@
>      use fmt::Write;
>      // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
>      let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
> -    // SAFETY: TODO.
> +    // SAFETY: The C implementation of `vsprintf` (in `lib/vsprintf.c`) specifically
> +    // calls this function ONLY when processing the `%pA` format specifier.
> +    // On the Rust side (`call_printk`), we guarantee that `%pA` is always paired
> +    // with a valid pointer to `fmt::Arguments`.
> +    // Therefore, dereferencing `ptr` here is safe.

There are multiple places that %pA is passed. For example, there is also
seq_file.rs and probably more.

Perhaps remove the reference to call_printk here?

// SAFETY: The C implementation of `vsprintf` (in `lib/vsprintf.c`) specifically
// calls this function ONLY when processing the `%pA` format specifier.
// On the Rust side, we always pair `%pA` with a valid pointer to
// `fmt::Arguments`.

>      let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
>      w.pos().cast()
>  }
> @@ -109,7 +113,9 @@ pub unsafe fn call_printk(
>  ) {
>      // `_printk` does not seem to fail in any path.
>      #[cfg(CONFIG_PRINTK)]
> -    // SAFETY: TODO.
> +    // SAFETY: The format string is constructed to use `%pA`, which corresponds to the
> +    // pointer to `fmt::Arguments` passed as the third argument.
> +    // Since `args` is a valid reference, casting it to a pointer is safe.
>      unsafe {
>          bindings::_printk(
>              format_string.as_ptr(),
> -- 
> 2.50.0
> 

  reply	other threads:[~2026-02-05  8:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-05  4:21 [PATCH] rust: print: add safety comments for %pA formatting Muchamad Coirul Anwar
2026-02-05  8:19 ` Alice Ryhl [this message]
2026-02-10 15:00 ` Miguel Ojeda
     [not found]   ` <CAO26r3TawBQQ6994h+wDTdiobuT8cwU1jGKDs1mh7HdUp=KpLA@mail.gmail.com>
     [not found]     ` <CABLcDQBjuZc0edQnoD7SfWj8qEH=ciognT6RPfQPO4r7gcODBA@mail.gmail.com>
2026-02-11 13:44       ` Muchamad Coirul Anwar
2026-02-11 18:27         ` [PATCH v2] rust: print: add safety documentation for %pA handling Shivendra Sharma

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=aYRSpYCIaRmsNvip@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=andrewjballance@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=muchamadcoirulanwar@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    /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