From: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
To: ojeda@kernel.org
Cc: boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com,
lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
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,
Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
Subject: [PATCH] rust: print: add safety comments for %pA formatting
Date: Thu, 5 Feb 2026 11:21:32 +0700 [thread overview]
Message-ID: <20260205042132.40772-1-muchamadcoirulanwar@gmail.com> (raw)
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.
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
next reply other threads:[~2026-02-05 4:23 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-05 4:21 Muchamad Coirul Anwar [this message]
2026-02-05 8:19 ` Alice Ryhl
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=20260205042132.40772-1-muchamadcoirulanwar@gmail.com \
--to=muchamadcoirulanwar@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--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=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
all inboxes | Powered by JetHome®