[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260205042132.40772-1-muchamadcoirulanwar@gmail.com>
Date: Thu, 5 Feb 2026 11:21:32 +0700
From: Muchamad Coirul Anwar <muchamadcoirulanwar@...il.com>
To: ojeda@...nel.org
Cc: boqun@...nel.org,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
lossin@...nel.org,
a.hindborg@...nel.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
dakr@...nel.org,
tamird@...nel.org,
gregkh@...uxfoundation.org,
fujita.tomonori@...il.com,
andrewjballance@...il.com,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
Muchamad Coirul Anwar <muchamadcoirulanwar@...il.com>
Subject: [PATCH] rust: print: add safety comments for %pA formatting
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@...il.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
Powered by blists - more mailing lists