6 Commits
Author SHA1 Message Date
Ke Sun 643a7c306b rust: fmt: route {:p} through HashedPtr to prevent address leaks
Define a custom `kernel::fmt::Pointer` trait and `HashedPtr` wrapper
so that `{:p}` formatting uses the kernel's `%p` hashed format instead
of printing raw pointer values, preventing kernel address space leaks.

Signed-off-by: Ke Sun <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Link: https://patch.msgid.link/[email protected]
[ Fixed KUnit failure when the CRNG is not ready. Then, as suggested,
  replaced the `scnprintf` comment (with v16's), changed width to 100,
  replaced cast with `without_provenance`. - Miguel ]
Signed-off-by: Miguel Ojeda <[email protected]>
2026-08-12 11:58:50 +02:00
Ke Sun fb7d645176 rust: fmt: fix {:p} printing stack addresses
The `impl_fmt_adapter_forward!` macro forwards `Pointer` for
`Adapter<T>` by destructuring `self` into a local `t`, causing `{:p}`
to print the address of that temporary stack variable rather than the
actual pointer.

Remove `Pointer` from the macro and provide a manual impl for
`Adapter<&T>` that passes `self.0` directly.

Signed-off-by: Ke Sun <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Tested-by: Link Mauve <[email protected]>
Cc: [email protected]
Fixes: c5cf01ba8d ("rust: support formatting of foreign types")
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
2026-08-12 11:55:52 +02:00
Ke Sun fc1ce3afa2 rust: fmt: use vertical import style
Switch single-line `use` imports in fmt.rs to vertical style for
better readability and easier maintenance.

Signed-off-by: Ke Sun <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
2026-05-20 00:47:24 +02:00
Dirk Behme 1e4e2a847f rust: fmt: Fix grammar in Adapter description
Add a missing `and` in the description of the `Adapter`.

Fixes: c5cf01ba8d ("rust: support formatting of foreign types")
Signed-off-by: Dirk Behme <[email protected]>
Acked-by: Tamir Duberstein <[email protected]>
Link: https://patch.msgid.link/[email protected]
[ Reworded for typo. - Miguel ]
Signed-off-by: Miguel Ojeda <[email protected]>
2026-01-04 23:51:35 +01:00
Tamir Duberstein c5cf01ba8d rust: support formatting of foreign types
Introduce a `fmt!` macro which wraps all arguments in
`kernel::fmt::Adapter` and a `kernel::fmt::Display` trait. This enables
formatting of foreign types (like `core::ffi::CStr`) that do not
implement `core::fmt::Display` due to concerns around lossy conversions
which do not apply in the kernel.

Suggested-by: Alice Ryhl <[email protected]>
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Custom.20formatting/with/516476467
Acked-by: Greg Kroah-Hartman <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Acked-by: Danilo Krummrich <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Signed-off-by: Tamir Duberstein <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
2025-10-22 07:15:31 +02:00
Tamir Duberstein bda947d613 rust: kernel: add fmt module
`kernel::fmt` is a facade over `core::fmt` that can be used downstream,
allowing future changes to the formatting machinery to be contained
within the kernel crate without downstream code needing to be modified.

Signed-off-by: Tamir Duberstein <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
2025-07-21 01:16:35 +02:00