rust: print: Document SAFETY invariants in rust_fmt_argument and call_printk
PATCH 1/1 rust: print: Document SAFETY invariants in rust_fmt_argument and call_printk
Replace remaining "SAFETY: TODO" comments with proper explanations.
Document why the raw pointer cast to fmt::Arguments is valid and clarify the invariants required when calling _printk through FFI.
This does not change behaviour.
--- rust/kernel/print.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
rust/kernel/print.rs +10 -2
@@ -24,7 +24,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: `ptr` originates from `call_printk`, which passes a valid +// reference to `fmt::Arguments` using `core::ptr::from_ref`. Therefore, +// it is properly aligned, non-null, and valid for the duration of this call. +let _ = w.write_fmt (unsafe { *ptr.cast ::<fmt::Arguments <'_>>() }); w.pos ().cast () }
@@ -104,7 +108,11 @@ pub unsafe fn call_printk( ) {// `_printk` does not seem to fail in any path. #[cfg( CONFIG_PRINTK )] -// SAFETY: TODO. +// SAFETY: `_printk` is called with a fixed format string generated +// at compile time, a null-terminated module name, and a pointer to +// `fmt::Arguments` obtained via `core::ptr::from_ref`. All pointers +// are valid for the duration of the call and respect the C ABI. +unsafe { bindings::_printk ( format_string.as_ptr(),
Signature · 1 line
2.53.0
Quoted 10 lines · Ali Zain
Replace remaining "SAFETY: TODO" comments with proper explanations.
Document why the raw pointer cast to
fmt::Argumentsis valid and clarify the invariants required when calling_printkthrough FFI.This does not change behaviour.
Signed-off-by Ali Zain <[email protected]>
Thanks! Please see:
https://lore.kernel.org/rust-for-linux/[email protected]/
Cheers,
Miguel
A message that is not in the archive (sent off-list or not yet received): CAGjWTa0VznxHuq_tQ=dLusxOUZtngZgsCbmdC=7xeVtM90ypSg@mail.gmail.com
Quoted 7 lines · Ali Zain
Thanks for the pointer.
I see that this topic is already being addressed in the v2 thread. I will follow that discussion and focus on other Rust areas.
Appreciate the guidance.
You're welcome, and welcome to the kernel too! :)
(By the way, if you send patches, please send it to all the names in the relevant entries in the MAINTAINERS file, e.g. here those in the "RUST" entry -- you can use scripts/get_maintainer.pl to help with that)
Cheers,
Miguel
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Ali,
kernel test robot noticed the following build errors:
[auto build test ERROR on rust/rust-next]
[also build test ERROR on linus/master v6.19 next-20260212] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Ali-Zain/rust-print-Document-SAFETY-invariants-in-rust_fmt_argument-and-call_printk/20260212-205645 base: https://github.com/Rust-for-Linux/linux rust-next patch link: https://lore.kernel.org/r/20260212125427.122362-1-alizainuimx%40gmail.com patch subject: [PATCH] rust: print: Document SAFETY invariants in rust_fmt_argument and call_printk config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260212/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260212/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
PATH=/opt/cross/clang-20/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
INFO PATH=/opt/cross/rustc-1.88.0-bindgen-0.72.1/cargo/bin:/opt/cross/clang-20/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/bin/timeout -k 100 12h /usr/bin/make KCFLAGS=\ -fno-crash-diagnostics\ -Wno-error=return-type\ -Wreturn-type\ -funsigned-char\ -Wundef\ -falign-functions=64 W=1 --keep-going LLVM=1 -j32 -C source O=/kbuild/obj/consumer/x86_64-rhel-9.4-rust ARCH=x86_64 SHELL=/bin/bash rustfmtcheck
make: Entering directory '/kbuild/src/consumer'
make[1]: Entering directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust'
>> Diff in rust/kernel/print.rs:117:
// at compile time, a null-terminated module name, and a pointer to
// `fmt::Arguments` obtained via `core::ptr::from_ref`. All pointers
// are valid for the duration of the call and respect the C ABI.
-
unsafe {
bindings::_printk(
format_string.as_ptr(),
>> Diff in rust/kernel/print.rs:117:
// at compile time, a null-terminated module name, and a pointer to
// `fmt::Arguments` obtained via `core::ptr::from_ref`. All pointers
// are valid for the duration of the call and respect the C ABI.
-
unsafe {
bindings::_printk(
format_string.as_ptr(), make[2]: *** [Makefile:1888: rustfmt] Error 123
make[2]: Target 'rustfmtcheck' not remade because of errors. make[1]: Leaving directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust' make[1]: *** [Makefile:248: __sub-make] Error 2
make[1]: Target 'rustfmtcheck' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2
make: Target 'rustfmtcheck' not remade because of errors.
make: Leaving directory '/kbuild/src/consumer'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 1/1 rust: print: Document SAFETY invariants in rust_fmt_argument and call_printk
In reply to Ali ZainReplace remaining "SAFETY: TODO" comments with proper explanations.
Document why the raw pointer cast to fmt::Arguments is valid and clarify the invariants required when calling _printk through FFI.
This does not change behaviour.
v2:
- Fix rustfmt formatting issue reported by 0-day CI.
- Remove extra blank line before unsafe block.
--- rust/kernel/list.rs | 2 ++ rust/kernel/print.rs | 1 - 2 files changed, 2 insertions(+), 1 deletion(-)
rust/kernel/list.rs +2 -0
@@ -589,6 +589,8 @@ pub unsafe fn remove(&mut self, item: &T) -> Option<ListArc<T, ID>> {// or in this list (with the same `ID`). // - We hold `&mut self`, so there is no concurrent mutation of // the link fields. +// - `T::view_links(item)` returns the embedded link fields for this +// object, which remain valid for the lifetime of `item`. let mut item =unsafe {ListLinks ::fields (T ::view_links (item)) };// SAFETY: The user provided a reference, and reference are never dangling.
rust/kernel/print.rs +0 -1
@@ -112,7 +112,6 @@ pub unsafe fn call_printk(// at compile time, a null-terminated module name, and a pointer to // `fmt::Arguments` obtained via `core::ptr::from_ref`. All pointers // are valid for the duration of the call and respect the C ABI. -unsafe { bindings::_printk ( format_string.as_ptr(),
Signature · 1 line
2.53.0
Quoted 7 lines · Ali Zain
Replace remaining "SAFETY: TODO" comments with proper explanations.
Document why the raw pointer cast to
fmt::Argumentsis valid and clarify the invariants required when calling_printkthrough FFI.
Quoted 7 lines
This does not change behaviour.
v2:
- Fix rustfmt formatting issue reported by 0-day CI.
- Remove extra blank line before unsafe block.Signed-off-by Ali Zain <[email protected]>
Since this is meant to be v2, please mark your patch as v2 using flags like git format-patch -v2.
What is the base for this patch, by the way?
Also, you included the changelog within the commit message -- they usually go after the first --- line, so that it is not included.
There is also an unrelated change in print.rs.
I hope that helps.
Cheers,
Miguel