From: Cian McGuire <cianmcguire167@gmail.com>
To: Miguel Ojeda <ojeda@kernel.org>
Cc: "Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Alice Ryhl" <aliceryhl@google.com>,
"Lyude Paul" <lyude@redhat.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Onur Özkan" <work@onurozkan.dev>,
"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>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
"Cian McGuire" <cianmcguire167@gmail.com>
Subject: [PATCH 2/4] rust: print: document safety of _printk FFI calls
Date: Fri, 24 Jul 2026 22:49:38 +0100 [thread overview]
Message-ID: <20260724214940.1070221-2-cianmcguire167@gmail.com> (raw)
In-Reply-To: <20260724214940.1070221-1-cianmcguire167@gmail.com>
Two calls in this file had their SAFETY comments left as "TODO.": the
cast back to `fmt::Arguments` in `rust_fmt_argument`, and the call to
`bindings::_printk` in `call_printk`.
Both are two ends of the same round trip. `call_printk` passes a raw
pointer to its `args: fmt::Arguments<'_>` parameter into `_printk`,
whose format string always contains a `%pA` specifier for it. The `%pA`
case in `lib/vsprintf.c`'s pointer formatter calls back into
`rust_fmt_argument` with that same pointer, synchronously, as one call
in a `switch` dispatch that returns its result immediately -- there is
no deferred use or storage of the pointer on the C side. So `args`
remains alive on `call_printk`'s stack for the whole time
`rust_fmt_argument` is running with `ptr` pointing at it.
The `_printk` call itself is also justified by this function's own
`# Safety` section: `format_string` is guaranteed to be one of the fixed
`format_strings` statics (traced through every `pr_*!` macro, each of
which hardcodes a specific one), so its `%s`/`%pA` specifiers always
match the two extra arguments supplied, and `module_name` is guaranteed
null-terminated by both of its possible origins (`kernel::__LOG_PREFIX`
and the one generated by the `module!` proc macro).
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/351
Signed-off-by: Cian McGuire <cianmcguire167@gmail.com>
---
rust/kernel/print.rs | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index 6fd84389a858..0e341262f83e 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: This function is only reachable via the `%pA` format specifier handled in
+ // `lib/vsprintf.c`, which calls it synchronously with the `ptr` that `call_printk`
+ // derived from a live `&fmt::Arguments`, before that reference's scope can end. `ptr`
+ // is therefore valid and properly aligned for a `fmt::Arguments<'_>` for the duration
+ // of this call.
let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
w.pos().cast()
}
@@ -109,7 +113,11 @@ pub unsafe fn call_printk(
) {
// `_printk` does not seem to fail in any path.
#[cfg(CONFIG_PRINTK)]
- // SAFETY: TODO.
+ // SAFETY: By this function's safety requirements, `format_string` is one of the fixed
+ // `format_strings` statics, so its `%s`/`%pA` specifiers match the two arguments
+ // supplied below, and `module_name` is null-terminated as `%s` requires. The third
+ // argument points at `args`, which remains valid for the duration of this call and is
+ // only read back synchronously, through `%pA`, by `rust_fmt_argument`.
unsafe {
bindings::_printk(
format_string.as_ptr(),
--
2.55.0
next prev parent reply other threads:[~2026-07-24 21:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-24 21:49 [PATCH 1/4] rust: aref: document safety of ARef doctest example Cian McGuire
2026-07-24 21:49 ` Cian McGuire [this message]
2026-07-24 21:49 ` [PATCH 3/4] rust: list: document safety of List::remove Cian McGuire
2026-07-24 21:49 ` [PATCH 4/4] rust: workqueue: document safety of Pin<KBox<T>> work item impls Cian McGuire
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=20260724214940.1070221-2-cianmcguire167@gmail.com \
--to=cianmcguire167@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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®