Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 0/8] rust: use `kernel::{fmt,prelude::fmt!}`
This is series 2a/5 of the migration to `core::ffi::CStr`[0].
20250704-core-cstr-prepare-v1-0-a91524037783 at gmail.com.
This series depends on the prior series[0] and is intended to go through
the rust tree to reduce the number of release cycles required to
complete the work.
Subsystem maintainers: I would appreciate your `Acked-by`s so that this
can be taken through Miguel's tree (where the other series must go).
[0] https://lore.kernel.org/all/20250704-core-cstr-prepare-v1-0-a91524037783 at
gmail.com/
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
Changes in v2:
- Rebase on rust-next.
- Drop pin-init patch, which is no longer needed.
- Link to v1:
https://lore.kernel.org/r/20250709-core-cstr-fanout-1-v1-0-64308e7203fc at
gmail.com
---
Tamir Duberstein (8):
gpu: nova-core: use `kernel::{fmt,prelude::fmt!}`
rust: alloc: use `kernel::{fmt,prelude::fmt!}`
rust: block: use `kernel::{fmt,prelude::fmt!}`
rust: device: use `kernel::{fmt,prelude::fmt!}`
rust: file: use `kernel::{fmt,prelude::fmt!}`
rust: kunit: use `kernel::{fmt,prelude::fmt!}`
rust: seq_file: use `kernel::{fmt,prelude::fmt!}`
rust: sync: use `kernel::{fmt,prelude::fmt!}`
drivers/block/rnull.rs | 2 +-
drivers/gpu/nova-core/gpu.rs | 3 +--
drivers/gpu/nova-core/regs/macros.rs | 6 +++---
rust/kernel/alloc/kbox.rs | 2 +-
rust/kernel/alloc/kvec.rs | 2 +-
rust/kernel/alloc/kvec/errors.rs | 2 +-
rust/kernel/block/mq.rs | 2 +-
rust/kernel/block/mq/gen_disk.rs | 2 +-
rust/kernel/block/mq/raw_writer.rs | 3 +--
rust/kernel/device.rs | 6 +++---
rust/kernel/fs/file.rs | 5 +++--
rust/kernel/kunit.rs | 8 ++++----
rust/kernel/seq_file.rs | 6 +++---
rust/kernel/sync/arc.rs | 2 +-
scripts/rustdoc_test_gen.rs | 2 +-
15 files changed, 26 insertions(+), 27 deletions(-)
---
base-commit: cc84ef3b88f407e8bd5a5f7b6906d1e69851c856
change-id: 20250709-core-cstr-fanout-1-f20611832272
prerequisite-change-id: 20250704-core-cstr-prepare-9b9e6a7bd57e:v1
prerequisite-patch-id: 83b1239d1805f206711a5a936bbb61c83227d573
prerequisite-patch-id: a0355dd0efcc945b0565dc4e5a0f42b5a3d29c7e
prerequisite-patch-id: 8585bf441cfab705181f5606c63483c2e88d25aa
prerequisite-patch-id: 04ec344c0bc23f90dbeac10afe26df1a86ce53ec
prerequisite-patch-id: a2fc6cd05fce6d6da8d401e9f8a905bb5c0b2f27
prerequisite-patch-id: f14c099c87562069f25fb7aea6d9aae4086c49a8
Best regards,
--
Tamir Duberstein <tamird at gmail.com>
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 1/8] gpu: nova-core: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
drivers/gpu/nova-core/gpu.rs | 3 +--
drivers/gpu/nova-core/regs/macros.rs | 6 +++---
2 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs
index 60b86f370284..f1d72da7fddd 100644
--- a/drivers/gpu/nova-core/gpu.rs
+++ b/drivers/gpu/nova-core/gpu.rs
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: GPL-2.0
-use kernel::{device, devres::Devres, error::code::*, pci, prelude::*};
+use kernel::{device, devres::Devres, error::code::*, fmt, pci, prelude::*};
use crate::driver::Bar0;
use crate::firmware::{Firmware, FIRMWARE_VERSION};
use crate::regs;
use crate::util;
-use core::fmt;
macro_rules! define_chipset {
({ $($variant:ident = $value:expr),* $(,)* }) =>
diff --git a/drivers/gpu/nova-core/regs/macros.rs
b/drivers/gpu/nova-core/regs/macros.rs
index 6851af8b5885..759773a19480 100644
--- a/drivers/gpu/nova-core/regs/macros.rs
+++ b/drivers/gpu/nova-core/regs/macros.rs
@@ -106,10 +106,10 @@ macro_rules! register {
// TODO: display the raw hex value, then the value of all the fields.
This requires
// matching the fields, which will complexify the syntax
considerably...
- impl ::core::fmt::Debug for $name {
- fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>)
-> ::core::fmt::Result {
+ impl ::kernel::fmt::Debug for $name {
+ fn fmt(&self, f: &mut
::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result {
f.debug_tuple(stringify!($name))
- .field(&format_args!("0x{0:x}", &self.0))
+ .field(&::kernel::prelude::fmt!("0x{0:x}",
&self.0))
.finish()
}
}
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 2/8] rust: alloc: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
rust/kernel/alloc/kbox.rs | 2 +-
rust/kernel/alloc/kvec.rs | 2 +-
rust/kernel/alloc/kvec/errors.rs | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
index 856d05aa60f1..563187d09a8e 100644
--- a/rust/kernel/alloc/kbox.rs
+++ b/rust/kernel/alloc/kbox.rs
@@ -7,7 +7,6 @@
use super::{AllocError, Allocator, Flags};
use core::alloc::Layout;
use core::borrow::{Borrow, BorrowMut};
-use core::fmt;
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use core::mem::MaybeUninit;
@@ -17,6 +16,7 @@
use core::result::Result;
use crate::ffi::c_void;
+use crate::fmt;
use crate::init::InPlaceInit;
use crate::types::ForeignOwnable;
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 3c72e0bdddb8..4a1b2c26209a 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -7,9 +7,9 @@
layout::ArrayLayout,
AllocError, Allocator, Box, Flags,
};
+use crate::fmt;
use core::{
borrow::{Borrow, BorrowMut},
- fmt,
marker::PhantomData,
mem::{ManuallyDrop, MaybeUninit},
ops::Deref,
diff --git a/rust/kernel/alloc/kvec/errors.rs b/rust/kernel/alloc/kvec/errors.rs
index 348b8d27e102..21a920a4b09b 100644
--- a/rust/kernel/alloc/kvec/errors.rs
+++ b/rust/kernel/alloc/kvec/errors.rs
@@ -2,7 +2,7 @@
//! Errors for the [`Vec`] type.
-use core::fmt::{self, Debug, Formatter};
+use kernel::fmt::{self, Debug, Formatter};
use kernel::prelude::*;
/// Error type for [`Vec::push_within_capacity`].
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 3/8] rust: block: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Suggested-by: Alice Ryhl <aliceryhl at google.com>
Link:
https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Custom.20formatting/with/516476467
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
drivers/block/rnull.rs | 2 +-
rust/kernel/block/mq.rs | 2 +-
rust/kernel/block/mq/gen_disk.rs | 2 +-
rust/kernel/block/mq/raw_writer.rs | 3 +--
4 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/block/rnull.rs b/drivers/block/rnull.rs
index d07e76ae2c13..6366da12c5a5 100644
--- a/drivers/block/rnull.rs
+++ b/drivers/block/rnull.rs
@@ -51,7 +51,7 @@ fn init(_module: &'static ThisModule) -> impl
PinInit<Self, Error> {
.logical_block_size(4096)?
.physical_block_size(4096)?
.rotational(false)
- .build(format_args!("rnullb{}", 0), tagset)
+ .build(fmt!("rnullb{}", 0), tagset)
})();
try_pin_init!(Self {
diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs
index 831445d37181..61ea35bba7d5 100644
--- a/rust/kernel/block/mq.rs
+++ b/rust/kernel/block/mq.rs
@@ -82,7 +82,7 @@
//! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?;
//! let mut disk = gen_disk::GenDiskBuilder::new()
//! .capacity_sectors(4096)
-//! .build(format_args!("myblk"), tagset)?;
+//! .build(fmt!("myblk"), tagset)?;
//!
//! # Ok::<(), kernel::error::Error>(())
//! ```
diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs
index cd54cd64ea88..494c95623b97 100644
--- a/rust/kernel/block/mq/gen_disk.rs
+++ b/rust/kernel/block/mq/gen_disk.rs
@@ -6,9 +6,9 @@
//! C header: [`include/linux/blk_mq.h`](srctree/include/linux/blk_mq.h)
use crate::block::mq::{raw_writer::RawWriter, Operations, TagSet};
+use crate::fmt::{self, Write};
use crate::{bindings, error::from_err_ptr, error::Result, sync::Arc};
use crate::{error, static_lock_class};
-use core::fmt::{self, Write};
/// A builder for [`GenDisk`].
///
diff --git a/rust/kernel/block/mq/raw_writer.rs
b/rust/kernel/block/mq/raw_writer.rs
index 7e2159e4f6a6..d311e24e2595 100644
--- a/rust/kernel/block/mq/raw_writer.rs
+++ b/rust/kernel/block/mq/raw_writer.rs
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
-use core::fmt::{self, Write};
-
use crate::error::Result;
+use crate::fmt::{self, Write};
use crate::prelude::EINVAL;
/// A mutable reference to a byte buffer where a string can be written into.
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 4/8] rust: device: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
rust/kernel/device.rs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 5c946af3a4d5..9e9ecdb1beec 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -5,11 +5,11 @@
//! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
use crate::{
- bindings,
+ bindings, fmt,
str::CStr,
types::{ARef, Opaque},
};
-use core::{fmt, marker::PhantomData, ptr};
+use core::{marker::PhantomData, ptr};
#[cfg(CONFIG_PRINTK)]
use crate::c_str;
@@ -345,7 +345,7 @@ macro_rules! impl_device_context_into_aref {
macro_rules! dev_printk {
($method:ident, $dev:expr, $($f:tt)*) => {
{
- ($dev).$method(::core::format_args!($($f)*));
+ ($dev).$method($crate::prelude::fmt!($($f)*));
}
}
}
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 5/8] rust: file: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
rust/kernel/fs/file.rs | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs
index 35fd5db35c46..67a3654f0fd3 100644
--- a/rust/kernel/fs/file.rs
+++ b/rust/kernel/fs/file.rs
@@ -11,6 +11,7 @@
bindings,
cred::Credential,
error::{code::*, Error, Result},
+ fmt,
types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque},
};
use core::ptr;
@@ -460,8 +461,8 @@ fn from(_: BadFdError) -> Error {
}
}
-impl core::fmt::Debug for BadFdError {
- fn fmt(&self, f: &mut core::fmt::Formatter<'_>) ->
core::fmt::Result {
+impl fmt::Debug for BadFdError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) ->
fmt::Result {
f.pad("EBADF")
}
}
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 6/8] rust: kunit: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
rust/kernel/kunit.rs | 8 ++++----
scripts/rustdoc_test_gen.rs | 2 +-
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
index 099a61bbb8f4..42498cb93f33 100644
--- a/rust/kernel/kunit.rs
+++ b/rust/kernel/kunit.rs
@@ -6,8 +6,8 @@
//!
//! Reference: <https://docs.kernel.org/dev-tools/kunit/index.html>
+use crate::fmt;
use crate::prelude::*;
-use core::fmt;
#[cfg(CONFIG_PRINTK)]
use crate::c_str;
@@ -74,14 +74,14 @@ macro_rules! kunit_assert {
// mistake (it is hidden to prevent that).
//
// This mimics KUnit's failed assertion format.
- $crate::kunit::err(format_args!(
+ $crate::kunit::err($crate::prelude::fmt!(
" # {}: ASSERTION FAILED at {FILE}:{LINE}\n",
$name
));
- $crate::kunit::err(format_args!(
+ $crate::kunit::err($crate::prelude::fmt!(
" Expected {CONDITION} to be true, but is
false\n"
));
- $crate::kunit::err(format_args!(
+ $crate::kunit::err($crate::prelude::fmt!(
" Failure not reported to KUnit since this is a
non-KUnit task\n"
));
break 'out;
diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs
index 1ca253594d38..507d36875196 100644
--- a/scripts/rustdoc_test_gen.rs
+++ b/scripts/rustdoc_test_gen.rs
@@ -201,7 +201,7 @@ macro_rules! assert_eq {{
// This follows the syntax for declaring test metadata in the proposed KTAP
v2 spec, which may
// be used for the proposed KUnit test attributes API. Thus hopefully this
will make migration
// easier later on.
- ::kernel::kunit::info(format_args!(" # {kunit_name}.location:
{real_path}:{line}\n"));
+ ::kernel::kunit::info(fmt!(" # {kunit_name}.location:
{real_path}:{line}\n"));
/// The anchor where the test code body starts.
#[allow(unused)]
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 7/8] rust: seq_file: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
rust/kernel/seq_file.rs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/seq_file.rs b/rust/kernel/seq_file.rs
index 8f199b1a3bb1..59fbfc2473f8 100644
--- a/rust/kernel/seq_file.rs
+++ b/rust/kernel/seq_file.rs
@@ -4,7 +4,7 @@
//!
//! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
-use crate::{bindings, c_str, types::NotThreadSafe, types::Opaque};
+use crate::{bindings, c_str, fmt, types::NotThreadSafe, types::Opaque};
/// A utility for generating the contents of a seq file.
#[repr(transparent)]
@@ -31,7 +31,7 @@ pub unsafe fn from_raw<'a>(ptr: *mut
bindings::seq_file) -> &'a SeqFile {
/// Used by the [`seq_print`] macro.
#[inline]
- pub fn call_printf(&self, args: core::fmt::Arguments<'_>) {
+ pub fn call_printf(&self, args: fmt::Arguments<'_>) {
// SAFETY: Passing a void pointer to `Arguments` is valid for `%pA`.
unsafe {
bindings::seq_printf(
@@ -47,7 +47,7 @@ pub fn call_printf(&self, args:
core::fmt::Arguments<'_>) {
#[macro_export]
macro_rules! seq_print {
($m:expr, $($arg:tt)+) => (
- $m.call_printf(format_args!($($arg)+))
+ $m.call_printf($crate::prelude::fmt!($($arg)+))
);
}
pub use seq_print;
--
2.50.1
Tamir Duberstein
2025-Jul-19 22:42 UTC
[PATCH v2 8/8] rust: sync: use `kernel::{fmt,prelude::fmt!}`
Reduce coupling to implementation details of the formatting machinery by
avoiding direct use for `core`'s formatting traits and macros.
Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl at google.com>
Reviewed-by: Benno Lossin <lossin at kernel.org>
Acked-by: Danilo Krummrich <dakr at kernel.org>
Signed-off-by: Tamir Duberstein <tamird at gmail.com>
---
rust/kernel/sync/arc.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 63a66761d0c7..4f3c1c2ff05e 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -20,6 +20,7 @@
alloc::{AllocError, Flags, KBox},
bindings,
ffi::c_void,
+ fmt,
init::InPlaceInit,
try_init,
types::{ForeignOwnable, Opaque},
@@ -27,7 +28,6 @@
use core::{
alloc::Layout,
borrow::{Borrow, BorrowMut},
- fmt,
marker::PhantomData,
mem::{ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
--
2.50.1
Andreas Hindborg
2025-Aug-08 09:43 UTC
[PATCH v2 3/8] rust: block: use `kernel::{fmt,prelude::fmt!}`
"Tamir Duberstein" <tamird at gmail.com> writes:> Reduce coupling to implementation details of the formatting machinery by > avoiding direct use for `core`'s formatting traits and macros. > > Suggested-by: Alice Ryhl <aliceryhl at google.com> > Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Custom.20formatting/with/516476467 > Acked-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org> > Reviewed-by: Alice Ryhl <aliceryhl at google.com> > Reviewed-by: Benno Lossin <lossin at kernel.org> > Acked-by: Danilo Krummrich <dakr at kernel.org> > Signed-off-by: Tamir Duberstein <tamird at gmail.com>Acked-by: Andreas Hindborg <a.hindborg at kernel.org> Best regards, Andreas Hindborg