* [PATCH 0/2] rust: kernel: add missing safety documentation
@ 2025-11-22 19:09 Navaneeth K
2025-11-22 19:09 ` [PATCH 1/2] rust: kernel: document safety for rust_fmt_argument Navaneeth K
2025-11-22 19:09 ` [PATCH 2/2] rust: kernel: document safety for as_str_unchecked Navaneeth K
0 siblings, 2 replies; 5+ messages in thread
From: Navaneeth K @ 2025-11-22 19:09 UTC (permalink / raw)
To: ojeda, alex.gaynor; +Cc: wedsonaf, rust-for-linux, linux-kernel, Navaneeth K
This series adds missing // SAFETY: comments to unsafe blocks in
the Rust kernel abstractions, replacing existing TODOs.
This aligns the code with the project's requirement that all
unsafe blocks must be documented with a safety rationale.
Navaneeth K (2):
rust: kernel: document safety for rust_fmt_argument
rust: kernel: document safety for as_str_unchecked
rust/kernel/print.rs | 2 +-
rust/kernel/str.rs | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] rust: kernel: document safety for rust_fmt_argument
2025-11-22 19:09 [PATCH 0/2] rust: kernel: add missing safety documentation Navaneeth K
@ 2025-11-22 19:09 ` Navaneeth K
2026-01-20 12:58 ` Miguel Ojeda
2025-11-22 19:09 ` [PATCH 2/2] rust: kernel: document safety for as_str_unchecked Navaneeth K
1 sibling, 1 reply; 5+ messages in thread
From: Navaneeth K @ 2025-11-22 19:09 UTC (permalink / raw)
To: ojeda, alex.gaynor; +Cc: wedsonaf, rust-for-linux, linux-kernel, Navaneeth K
Add a proper // SAFETY: comment for the unsafe pointer dereference in
rust_fmt_argument. The comment explains that the caller guarantees
the pointer validity, satisfying the Rust-for-Linux safety documentation
requirements.
Signed-off-by: Navaneeth K <knavaneeth786@gmail.com>
---
rust/kernel/print.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index 2d743d78d220..49fa87dfbce8 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -24,7 +24,7 @@
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: The caller guarantees that `ptr` points to a valid `fmt::Arguments`.
let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
w.pos().cast()
}
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 1/2] rust: kernel: document safety for rust_fmt_argument
2025-11-22 19:09 ` [PATCH 1/2] rust: kernel: document safety for rust_fmt_argument Navaneeth K
@ 2026-01-20 12:58 ` Miguel Ojeda
0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2026-01-20 12:58 UTC (permalink / raw)
To: Navaneeth K; +Cc: ojeda, alex.gaynor, wedsonaf, rust-for-linux, linux-kernel
On Sat, Nov 22, 2025 at 8:09 PM Navaneeth K <knavaneeth786@gmail.com> wrote:
>
> + // SAFETY: The caller guarantees that `ptr` points to a valid `fmt::Arguments`.
Thanks for the patch!
The caller is indeed supposed to pass a proper `ptr`, but we should
actually require it, i.e. this line above:
#[expect(clippy::missing_safety_doc)]
should be replaced with docs with a `# Safety` section that requires
it so that then we can apply the guarantee as you did in the `//
SAFETY:` doc.
Also, in addition to that parameter, we should double-check we put
enough safety preconditions on the other parameters. If we do that,
then we could also update the "C contract guarantees" safety comment
too to just refer to those.
(We could perhaps even mention in the C header that the safety
preconditions are written in the Rust docs for completeness.)
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] rust: kernel: document safety for as_str_unchecked
2025-11-22 19:09 [PATCH 0/2] rust: kernel: add missing safety documentation Navaneeth K
2025-11-22 19:09 ` [PATCH 1/2] rust: kernel: document safety for rust_fmt_argument Navaneeth K
@ 2025-11-22 19:09 ` Navaneeth K
2025-11-24 9:34 ` Alice Ryhl
1 sibling, 1 reply; 5+ messages in thread
From: Navaneeth K @ 2025-11-22 19:09 UTC (permalink / raw)
To: ojeda, alex.gaynor; +Cc: wedsonaf, rust-for-linux, linux-kernel, Navaneeth K
Replace the TODO safety comment with a proper explanation.
The safety of from_utf8_unchecked relies on the caller guaranteeing
that the input bytes are valid UTF-8.
Signed-off-by: Navaneeth K <knavaneeth786@gmail.com>
---
rust/kernel/str.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 5c74e5f77601..2801388c6bd1 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -385,7 +385,7 @@ pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
/// ```
#[inline]
pub unsafe fn as_str_unchecked(&self) -> &str {
- // SAFETY: TODO.
+ // SAFETY: The caller guarantees that the contents are valid UTF-8.
unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
}
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 2/2] rust: kernel: document safety for as_str_unchecked
2025-11-22 19:09 ` [PATCH 2/2] rust: kernel: document safety for as_str_unchecked Navaneeth K
@ 2025-11-24 9:34 ` Alice Ryhl
0 siblings, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2025-11-24 9:34 UTC (permalink / raw)
To: Navaneeth K; +Cc: ojeda, alex.gaynor, wedsonaf, rust-for-linux, linux-kernel
On Sat, Nov 22, 2025 at 07:09:08PM +0000, Navaneeth K wrote:
> Replace the TODO safety comment with a proper explanation.
> The safety of from_utf8_unchecked relies on the caller guaranteeing
> that the input bytes are valid UTF-8.
>
> Signed-off-by: Navaneeth K <knavaneeth786@gmail.com>
> ---
> rust/kernel/str.rs | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 5c74e5f77601..2801388c6bd1 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -385,7 +385,7 @@ pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
> /// ```
> #[inline]
> pub unsafe fn as_str_unchecked(&self) -> &str {
> - // SAFETY: TODO.
> + // SAFETY: The caller guarantees that the contents are valid UTF-8.
> unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
As far as I know another patch is deleting this method, so I don't think
there is anything to fix anymore.
Alice
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-01-20 12:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-22 19:09 [PATCH 0/2] rust: kernel: add missing safety documentation Navaneeth K
2025-11-22 19:09 ` [PATCH 1/2] rust: kernel: document safety for rust_fmt_argument Navaneeth K
2026-01-20 12:58 ` Miguel Ojeda
2025-11-22 19:09 ` [PATCH 2/2] rust: kernel: document safety for as_str_unchecked Navaneeth K
2025-11-24 9:34 ` Alice Ryhl
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®