From: Shivendra Sharma <shivendra02467@gmail.com>
To: ojeda@kernel.org, boqun.feng@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
dakr@kernel.org
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
shivendra02467@gmail.com
Subject: [PATCH] rust: kernel: document safety for rust_fmt_argument
Date: Thu, 29 Jan 2026 01:51:30 +0530 [thread overview]
Message-ID: <20260128202130.196419-1-shivendra02467@gmail.com> (raw)
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
next reply other threads:[~2026-01-28 20:21 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-28 20:21 Shivendra Sharma [this message]
2026-02-10 15:00 ` Miguel Ojeda
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=20260128202130.196419-1-shivendra02467@gmail.com \
--to=shivendra02467@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.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®