From: Shivendra Sharma <shivendra02467@gmail.com>
To: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>,
Miguel Ojeda <ojeda@kernel.org>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Tamir Duberstein" <tamird@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
"Shivendra Sharma" <shivendra02467@gmail.com>
Subject: [PATCH v2] rust: print: add safety documentation for %pA handling
Date: Wed, 11 Feb 2026 23:57:55 +0530 [thread overview]
Message-ID: <20260211182755.82220-1-shivendra02467@gmail.com> (raw)
In-Reply-To: <CAO26r3Rh7WEYrH9ksD3ary=2OUSmYj372iCAGx=VsGnXAN4ySQ@mail.gmail.com>
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@google.com>
Co-developed-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@gmail.com>
Signed-off-by: Shivendra Sharma <shivendra02467@gmail.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
prev parent reply other threads:[~2026-02-11 18:29 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
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 ` Shivendra Sharma [this message]
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=20260211182755.82220-1-shivendra02467@gmail.com \
--to=shivendra02467@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--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