* [PATCH] rust: kernel: document safety for rust_fmt_argument
@ 2026-01-28 20:21 Shivendra Sharma
2026-02-10 15:00 ` Miguel Ojeda
0 siblings, 1 reply; 2+ messages in thread
From: Shivendra Sharma @ 2026-01-28 20:21 UTC (permalink / raw)
To: ojeda, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr
Cc: rust-for-linux, linux-kernel, shivendra02467
Currently, `rust_fmt_argument` uses a TODO for its safety rationale and
suppresses the `missing_safety_doc` clippy warning.
Following feedback from a previous attempt [1], replace the
suppression with a formal "# Safety" section and update the internal
"// SAFETY" comments to reference these requirements. Additionally,
add a comment to the C header file pointing to the Rust documentation
for safety preconditions.
[1] https://lore.kernel.org/rust-for-linux/CANiq72=b=Z+6dk4XWK6QWcKDJx7SYXL0obAFFfWp8xpE1CFkkw@mail.gmail.com/
Signed-off-by: Shivendra Sharma <shivendra02467@gmail.com>
---
include/linux/sprintf.h | 7 ++++++-
rust/kernel/print.rs | 14 +++++++++++---
2 files changed, 17 insertions(+), 4 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 2d743d78d220..30da8b974398 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -14,7 +14,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,
@@ -22,9 +27,12 @@
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 safety requirements of this function guarantee that `ptr`
+ // points to a valid `fmt::Arguments`.
let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
w.pos().cast()
}
--
2.52.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] rust: kernel: document safety for rust_fmt_argument
2026-01-28 20:21 [PATCH] rust: kernel: document safety for rust_fmt_argument Shivendra Sharma
@ 2026-02-10 15:00 ` Miguel Ojeda
0 siblings, 0 replies; 2+ messages in thread
From: Miguel Ojeda @ 2026-02-10 15:00 UTC (permalink / raw)
To: Shivendra Sharma, Muchamad Coirul Anwar
Cc: ojeda, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, rust-for-linux, linux-kernel
On Wed, Jan 28, 2026 at 9:21 PM Shivendra Sharma
<shivendra02467@gmail.com> wrote:
>
> Currently, `rust_fmt_argument` uses a TODO for its safety rationale and
> suppresses the `missing_safety_doc` clippy warning.
>
> Following feedback from a previous attempt [1], replace the
> suppression with a formal "# Safety" section and update the internal
> "// SAFETY" comments to reference these requirements. Additionally,
> add a comment to the C header file pointing to the Rust documentation
> for safety preconditions.
>
> [1] https://lore.kernel.org/rust-for-linux/CANiq72=b=Z+6dk4XWK6QWcKDJx7SYXL0obAFFfWp8xpE1CFkkw@mail.gmail.com/
>
> Signed-off-by: Shivendra Sharma <shivendra02467@gmail.com>
Related:
https://lore.kernel.org/rust-for-linux/20260205042132.40772-1-muchamadcoirulanwar@gmail.com/
Cc'ing Muchamad.
Could you (i.e. both) please coordinate the patches, perhaps merging
them into a single series? You may want to use Co-developed-by and/or
have a different author per patch etc.
Thanks!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-10 15:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-28 20:21 [PATCH] rust: kernel: document safety for rust_fmt_argument Shivendra Sharma
2026-02-10 15:00 ` Miguel Ojeda
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®