Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy
@ 2026-07-28 17:31 Josef Ippisch via B4 Relay
  2026-07-28 17:31 ` [PATCH v2 1/2] " Josef Ippisch via B4 Relay
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Josef Ippisch via B4 Relay @ 2026-07-28 17:31 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: driver-core, rust-for-linux, linux-kernel, Joshua Liebow-Feeser,
	Miguel Ojeda, Josef Ippisch

Migrate `BinaryWriter` and `BinaryReaderMut`'s default implementation's
requirements on T from `kernel::transmute` traits to `zerocopy` traits.

This is part of a bigger migration from rust/kernel/transmute.rs to the
zerocopy crate, see also [1] and [2]

[1] https://github.com/Rust-for-Linux/linux/issues/975
[2] https://github.com/Rust-for-Linux/linux/issues/1241

The crate zerocopy was introduced with

c37398010a05 rust: zerocopy: import crate

This introduced among other things the `FromBytes` and `IntoBytes` traits
that, when derived, do better checks on the actual requirements of the
types they are implemented on than the existing
`kernel::transmute::FromBytes` and `kernel::transmute::AsBytes` traits,
respectively.

This is my first kernel patch so please let me know if I do anything wrong
process wise or regarding some other aspect!

I noticed that we could probably get rid of the `BinaryWriter` and
`BinaryReaderMut` implementations for `Vec` altogether if we implemented
`FromBytes`, `Immutable` and `IntoBytes` for `Vec`.  These are already
implemented for [T] after all. What do you think?

Suggested-by: Joshua Liebow-Feeser <joshlf@google.com>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/975
Link: https://github.com/Rust-for-Linux/linux/issues/1241
Signed-off-by: Josef Ippisch <josef.ippisch.dev@mailbox.org>
---
Changes in v2:
- Addresses Sashiko AI review's comment about also migrating
  `BinaryReaderMut` to zerocopy to not bifurcate the API requirements of
  debugfs/traits.rs.
- Do not `use zerocopy::{FromBytes, IntoBytes}` as they are part of
  `use kernel::prelude::*`.
- For some reason sending v2 via SMTP server did not reach the mailing
  list.  I apologize if some of you receive this twice now.  I did not
  mark this RESEND because of the email never reaching the mailing list.
- Link to v1: https://patch.msgid.link/20260719-migrate-binarywriter-to-zerocopy-intobytes-v1-0-27ed30512e75@mailbox.org

---
Josef Ippisch (2):
      rust: debugfs: migrate debugfs traits requirements to zerocopy
      rust: debugfs: remove unsafe blocks from traits impl for Vec

 rust/kernel/debugfs/traits.rs | 41 +++++++++++------------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)
---
base-commit: 7059bdf4f04a3e14f4fafb3ac35fdca913e3e21a
change-id: 20260719-migrate-binarywriter-to-zerocopy-intobytes-1e8f4ef93a60

Best regards,
--  
Josef Ippisch <josef.ippisch.dev@mailbox.org>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] rust: debugfs: migrate debugfs traits requirements to zerocopy
  2026-07-28 17:31 [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Josef Ippisch via B4 Relay
@ 2026-07-28 17:31 ` Josef Ippisch via B4 Relay
  2026-07-28 17:31 ` [PATCH v2 2/2] rust: debugfs: remove unsafe blocks from traits impl for Vec Josef Ippisch via B4 Relay
  2026-07-31  9:09 ` [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Josef Ippisch via B4 Relay @ 2026-07-28 17:31 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: driver-core, rust-for-linux, linux-kernel, Joshua Liebow-Feeser,
	Miguel Ojeda, Josef Ippisch

From: Josef Ippisch <josef.ippisch.dev@mailbox.org>

Migrate `BinaryWriter` and `BinaryReaderMut`'s default implementation's
requirements on T from `kernel::transmute` traits to `zerocopy` traits.

The additional `zerocopy::Immutable` requirement on `BinaryWriter` does not
further restrict the types in practice but is rather a more explicit
requirement (that the type does not have interior mutability) and is
required by zerocopy for the `as_bytes()` function.

Suggested-by: Joshua Liebow-Feeser <joshlf@google.com>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/975
Link: https://github.com/Rust-for-Linux/linux/issues/1241
Signed-off-by: Josef Ippisch <josef.ippisch.dev@mailbox.org>
---
 rust/kernel/debugfs/traits.rs | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs
index 8c39524b6a99..b794f7dd7e14 100644
--- a/rust/kernel/debugfs/traits.rs
+++ b/rust/kernel/debugfs/traits.rs
@@ -18,10 +18,6 @@
         Arc,
         Mutex, //
     },
-    transmute::{
-        AsBytes,
-        FromBytes, //
-    },
     uaccess::{
         UserSliceReader,
         UserSliceWriter, //
@@ -36,6 +32,8 @@
     str::FromStr,
 };
 
+use zerocopy::Immutable;
+
 /// A trait for types that can be written into a string.
 ///
 /// This works very similarly to `Debug`, and is automatically implemented if `Debug` is
@@ -76,8 +74,8 @@ fn write_to_slice(
     ) -> Result<usize>;
 }
 
-// Base implementation for any `T: AsBytes`.
-impl<T: AsBytes> BinaryWriter for T {
+// Base implementation for any `T: Immutable + IntoBytes`.
+impl<T: Immutable + IntoBytes> BinaryWriter for T {
     fn write_to_slice(
         &self,
         writer: &mut UserSliceWriter,
@@ -147,7 +145,7 @@ fn write_to_slice(
 // Delegate for `Vec<T, A>`.
 impl<T, A> BinaryWriter for Vec<T, A>
 where
-    T: AsBytes,
+    T: Immutable + IntoBytes,
     A: Allocator,
 {
     fn write_to_slice(
@@ -157,7 +155,7 @@ fn write_to_slice(
     ) -> Result<usize> {
         let slice = self.as_slice();
 
-        // SAFETY: `T: AsBytes` allows us to treat `&[T]` as `&[u8]`.
+        // SAFETY: `T: Immutable + IntoBytes` allows us to treat `&[T]` as `&[u8]`.
         let buffer = unsafe {
             core::slice::from_raw_parts(slice.as_ptr().cast(), core::mem::size_of_val(slice))
         };
@@ -230,14 +228,14 @@ fn read_from_slice_mut(
     ) -> Result<usize>;
 }
 
-// Base implementation for any `T: AsBytes + FromBytes`.
-impl<T: AsBytes + FromBytes> BinaryReaderMut for T {
+// Base implementation for any `T: FromBytes + IntoBytes`.
+impl<T: FromBytes + IntoBytes> BinaryReaderMut for T {
     fn read_from_slice_mut(
         &mut self,
         reader: &mut UserSliceReader,
         offset: &mut file::Offset,
     ) -> Result<usize> {
-        reader.read_slice_file(self.as_bytes_mut(), offset)
+        reader.read_slice_file(self.as_mut_bytes(), offset)
     }
 }
 
@@ -255,7 +253,7 @@ fn read_from_slice_mut(
 // Delegate for `Vec<T, A>`: Support a `Vec<T, A>` with an outer lock.
 impl<T, A> BinaryReaderMut for Vec<T, A>
 where
-    T: AsBytes + FromBytes,
+    T: FromBytes + IntoBytes,
     A: Allocator,
 {
     fn read_from_slice_mut(
@@ -265,7 +263,7 @@ fn read_from_slice_mut(
     ) -> Result<usize> {
         let slice = self.as_mut_slice();
 
-        // SAFETY: `T: AsBytes + FromBytes` allows us to treat `&mut [T]` as `&mut [u8]`.
+        // SAFETY: `T: FromBytes + IntoBytes` allow us to treat `&mut [T]` as `&mut [u8]`.
         let buffer = unsafe {
             core::slice::from_raw_parts_mut(
                 slice.as_mut_ptr().cast(),

-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] rust: debugfs: remove unsafe blocks from traits impl for Vec
  2026-07-28 17:31 [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Josef Ippisch via B4 Relay
  2026-07-28 17:31 ` [PATCH v2 1/2] " Josef Ippisch via B4 Relay
@ 2026-07-28 17:31 ` Josef Ippisch via B4 Relay
  2026-07-31  9:09 ` [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Josef Ippisch via B4 Relay @ 2026-07-28 17:31 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: driver-core, rust-for-linux, linux-kernel, Joshua Liebow-Feeser,
	Miguel Ojeda, Josef Ippisch

From: Josef Ippisch <josef.ippisch.dev@mailbox.org>

The previous implementation used an `unsafe` block to manually cast Vec's
slice to a &[u8] using `core::slice::from_raw_parts`.  Instead, the
implementation can be implemented in safe rust using zerocopy's trait
functions `as_bytes()` and `as_mut_bytes()`, respectively, and making use
of deref coercion to implicitly cast Vec to &[T] as `FromBytes` and
`IntoBytes` automatically are implemented on [T] when they are implemented
on T.

Signed-off-by: Josef Ippisch <josef.ippisch.dev@mailbox.org>
---
 rust/kernel/debugfs/traits.rs | 21 ++-------------------
 1 file changed, 2 insertions(+), 19 deletions(-)

diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs
index b794f7dd7e14..b295f8420abd 100644
--- a/rust/kernel/debugfs/traits.rs
+++ b/rust/kernel/debugfs/traits.rs
@@ -153,14 +153,7 @@ fn write_to_slice(
         writer: &mut UserSliceWriter,
         offset: &mut file::Offset,
     ) -> Result<usize> {
-        let slice = self.as_slice();
-
-        // SAFETY: `T: Immutable + IntoBytes` allows us to treat `&[T]` as `&[u8]`.
-        let buffer = unsafe {
-            core::slice::from_raw_parts(slice.as_ptr().cast(), core::mem::size_of_val(slice))
-        };
-
-        writer.write_slice_file(buffer, offset)
+        writer.write_slice_file(self.as_bytes(), offset)
     }
 }
 
@@ -261,17 +254,7 @@ fn read_from_slice_mut(
         reader: &mut UserSliceReader,
         offset: &mut file::Offset,
     ) -> Result<usize> {
-        let slice = self.as_mut_slice();
-
-        // SAFETY: `T: FromBytes + IntoBytes` allow us to treat `&mut [T]` as `&mut [u8]`.
-        let buffer = unsafe {
-            core::slice::from_raw_parts_mut(
-                slice.as_mut_ptr().cast(),
-                core::mem::size_of_val(slice),
-            )
-        };
-
-        reader.read_slice_file(buffer, offset)
+        reader.read_slice_file(self.as_mut_bytes(), offset)
     }
 }
 

-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy
  2026-07-28 17:31 [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Josef Ippisch via B4 Relay
  2026-07-28 17:31 ` [PATCH v2 1/2] " Josef Ippisch via B4 Relay
  2026-07-28 17:31 ` [PATCH v2 2/2] rust: debugfs: remove unsafe blocks from traits impl for Vec Josef Ippisch via B4 Relay
@ 2026-07-31  9:09 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-07-31  9:09 UTC (permalink / raw)
  To: josef.ippisch.dev
  Cc: Rafael J. Wysocki, Danilo Krummrich, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, driver-core, rust-for-linux, linux-kernel,
	Joshua Liebow-Feeser, Miguel Ojeda

On Tue, Jul 28, 2026 at 07:31:18PM +0200, Josef Ippisch via B4 Relay wrote:
> Migrate `BinaryWriter` and `BinaryReaderMut`'s default implementation's
> requirements on T from `kernel::transmute` traits to `zerocopy` traits.
> 
> This is part of a bigger migration from rust/kernel/transmute.rs to the
> zerocopy crate, see also [1] and [2]
> 
> [1] https://github.com/Rust-for-Linux/linux/issues/975
> [2] https://github.com/Rust-for-Linux/linux/issues/1241
> 
> The crate zerocopy was introduced with
> 
> c37398010a05 rust: zerocopy: import crate
> 
> This introduced among other things the `FromBytes` and `IntoBytes` traits
> that, when derived, do better checks on the actual requirements of the
> types they are implemented on than the existing
> `kernel::transmute::FromBytes` and `kernel::transmute::AsBytes` traits,
> respectively.
> 
> This is my first kernel patch so please let me know if I do anything wrong
> process wise or regarding some other aspect!
> 
> I noticed that we could probably get rid of the `BinaryWriter` and
> `BinaryReaderMut` implementations for `Vec` altogether if we implemented
> `FromBytes`, `Immutable` and `IntoBytes` for `Vec`.  These are already
> implemented for [T] after all. What do you think?
> 
> Suggested-by: Joshua Liebow-Feeser <joshlf@google.com>
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/975
> Link: https://github.com/Rust-for-Linux/linux/issues/1241
> Signed-off-by: Josef Ippisch <josef.ippisch.dev@mailbox.org>
> ---
> Changes in v2:
> - Addresses Sashiko AI review's comment about also migrating
>   `BinaryReaderMut` to zerocopy to not bifurcate the API requirements of
>   debugfs/traits.rs.
> - Do not `use zerocopy::{FromBytes, IntoBytes}` as they are part of
>   `use kernel::prelude::*`.
> - For some reason sending v2 via SMTP server did not reach the mailing
>   list.  I apologize if some of you receive this twice now.  I did not
>   mark this RESEND because of the email never reaching the mailing list.
> - Link to v1: https://patch.msgid.link/20260719-migrate-binarywriter-to-zerocopy-intobytes-v1-0-27ed30512e75@mailbox.org
> 
> ---
> Josef Ippisch (2):
>       rust: debugfs: migrate debugfs traits requirements to zerocopy
>       rust: debugfs: remove unsafe blocks from traits impl for Vec
> 
>  rust/kernel/debugfs/traits.rs | 41 +++++++++++------------------------------
>  1 file changed, 11 insertions(+), 30 deletions(-)

Always nice to delete code!

now queued up, thanks.

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-31  9:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-28 17:31 [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Josef Ippisch via B4 Relay
2026-07-28 17:31 ` [PATCH v2 1/2] " Josef Ippisch via B4 Relay
2026-07-28 17:31 ` [PATCH v2 2/2] rust: debugfs: remove unsafe blocks from traits impl for Vec Josef Ippisch via B4 Relay
2026-07-31  9:09 ` [PATCH v2 0/2] rust: debugfs: migrate debugfs traits requirements to zerocopy Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox