[PATCH 2/5] rust: print: use `CStr` for __LOG_PREFIX

From: Gary Guo

Date: Tue Jul 28 2026 - 11:15:49 EST


Currently `call_printk` uses `&[u8]` for log prefix and as a safety
precondition require it to be null-terminated. Change it to use `CStr` so
this requirement is encoded in type invariant.

Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/kernel/lib.rs | 2 +-
rust/kernel/print.rs | 11 ++++-------
rust/macros/module.rs | 2 +-
scripts/rustdoc_test_gen.rs | 2 +-
4 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 68f4d9a3425d..5436daa48ef5 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -146,7 +146,7 @@
pub use uapi;

/// Prefix to appear before log messages printed from within the `kernel` crate.
-const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
+const __LOG_PREFIX: &core::ffi::CStr = c"rust_kernel";

/// The top level entrypoint to implementing a kernel module.
///
diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index 066ef7a7d5ce..470c1001f37d 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -87,15 +87,14 @@ pub mod format_strings {
///
/// # Safety
///
-/// The format string must be one of the ones in [`format_strings`], and
-/// the module name must be null-terminated.
+/// The format string must be one of the ones in [`format_strings`].
///
/// [`_printk`]: srctree/include/linux/_printk.h
#[doc(hidden)]
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
pub unsafe fn call_printk(
format_string: &[u8; format_strings::LENGTH],
- module_name: &[u8],
+ module_name: &CStr,
args: fmt::Arguments<'_>,
) {
// `_printk` does not seem to fail in any path.
@@ -104,7 +103,7 @@ pub unsafe fn call_printk(
unsafe {
bindings::_printk(
format_string.as_ptr(),
- module_name.as_ptr(),
+ module_name.as_char_ptr(),
core::ptr::from_ref(&args).cast::<c_void>(),
);
}
@@ -157,9 +156,7 @@ macro_rules! print_macro (
match $crate::prelude::fmt!($($arg)+) {
// SAFETY: This hidden macro should only be called by the documented
// printing macros which ensure the format string is one of the fixed
- // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
- // by the `module!` proc macro or fixed values defined in a kernel
- // crate.
+ // ones.
args => unsafe {
$crate::print::call_printk(
&$format_string,
diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 025323fb030d..cfc27a40b7f1 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -496,7 +496,7 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> {
/// The module name.
///
/// Used by the printing macros, e.g. [`info!`].
- const __LOG_PREFIX: &[u8] = #name_cstr.to_bytes_with_nul();
+ const __LOG_PREFIX: &::core::ffi::CStr = #name_cstr;

// SAFETY: `__this_module` is constructed by the kernel at load time and will not be
// freed until the module is unloaded.
diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs
index d61a77219a8c..18fdeac305a7 100644
--- a/scripts/rustdoc_test_gen.rs
+++ b/scripts/rustdoc_test_gen.rs
@@ -230,7 +230,7 @@ macro_rules! assert_eq {{
BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()),
r#"//! `kernel` crate documentation tests.

-const __LOG_PREFIX: &[u8] = b"rust_doctests_kernel\0";
+const __LOG_PREFIX: &::core::ffi::CStr = c"rust_doctests_kernel";

{rust_tests}
"#

--
2.54.0