[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20260211182755.82220-1-shivendra02467@gmail.com>
Date: Wed, 11 Feb 2026 23:57:55 +0530
From: Shivendra Sharma <shivendra02467@...il.com>
To: Muchamad Coirul Anwar <muchamadcoirulanwar@...il.com>,
Miguel Ojeda <ojeda@...nel.org>
Cc: Alice Ryhl <aliceryhl@...gle.com>,
Boqun Feng <boqun@...nel.org>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>,
Tamir Duberstein <tamird@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
Shivendra Sharma <shivendra02467@...il.com>
Subject: [PATCH v2] rust: print: add safety documentation for %pA handling
Add missing safety documentation to `rust_fmt_argument` and `call_printk`.
These comments clarify the safety requirements for dereferencing
pointers and calling the C `_printk` function, specifically
addressing the contract between `lib/vsprintf.c` and Rust regarding
the `%pA` format specifier.
The safety guarantee is made generic to cover all users of `%pA`,
including `seq_file.rs`.
Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
Co-developed-by: Muchamad Coirul Anwar <muchamadcoirulanwar@...il.com>
Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@...il.com>
Signed-off-by: Shivendra Sharma <shivendra02467@...il.com>
---
v2:
- Combined work with Muchamad Coirul Anwar.
- Refined safety comments to be generic per Alice Ryhl's feedback.
- Added formal "# Safety" section to rust_fmt_argument per Miguel Ojeda's
previous suggestion.
- Added documentation to include/linux/sprintf.h.
- Links to previous versions:
v1 (Shivendra): https://lore.kernel.org/rust-for-linux/20260128202130.196419-1-shivendra02467@gmail.com/
v1 (Muchamad): https://lore.kernel.org/rust-for-linux/20260205042132.40772-1-muchamadcoirulanwar@gmail.com/
include/linux/sprintf.h | 7 ++++++-
rust/kernel/print.rs | 19 +++++++++++++++----
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/include/linux/sprintf.h b/include/linux/sprintf.h
index f06f7b785091..f915829cbba5 100644
--- a/include/linux/sprintf.h
+++ b/include/linux/sprintf.h
@@ -25,7 +25,12 @@ __scanf(2, 0) int vsscanf(const char *, const char *, va_list);
extern bool no_hash_pointers;
void hash_pointers_finalize(bool slub_debug);
-/* Used for Rust formatting ('%pA') */
+/**
+ * Used for Rust formatting ('%pA').
+ *
+ * Safety preconditions are documented in the Rust implementation
+ * (rust/kernel/print.rs).
+ */
char *rust_fmt_argument(char *buf, char *end, const void *ptr);
#endif /* _LINUX_KERNEL_SPRINTF_H */
diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index 6fd84389a858..15d4039a7646 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -19,7 +19,12 @@
};
// Called from `vsprintf` with format specifier `%pA`.
-#[expect(clippy::missing_safety_doc)]
+///
+/// # Safety
+///
+/// - `buf` must be valid for writes until `end`.
+/// - `ptr` must point to a valid `fmt::Arguments` instance.
+/// - The `fmt::Arguments` must remain valid for the duration of the call.
#[export]
unsafe extern "C" fn rust_fmt_argument(
buf: *mut c_char,
@@ -27,9 +32,13 @@
ptr: *const c_void,
) -> *mut c_char {
use fmt::Write;
- // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
+ // SAFETY: The safety requirements of this function (see above)
+ // guarantee that `buf` and `end` define a valid range.
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, we always pair `%pA` with a valid pointer to
+ // `fmt::Arguments`, satisfying the requirements of this function (see above).
let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
w.pos().cast()
}
@@ -109,7 +118,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.51.2
Powered by blists - more mailing lists