Skip to contentSkip to conversation

Binary Large Objects for Rust DebugFS

PATCH Superseded Danilo Krummrich 3 participants 32 messages Oct 3, 2025 – Oct 20, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

cover Binary Large Objects for Rust DebugFS

This series adds support for exposing binary large objects via Rust debugfs.

The first two patches extend UserSliceReader and UserSliceWriter with partial read/write helpers.

The series further introduces read_binary_file(), write_binary_file() and read_write_binary_file() methods for the Dir and ScopedDir types.

It also introduces the BinaryWriter and BinaryReader traits, which are used to read/write the implementing type's binary representation with the help of the backing file operations from/to debugfs.

Additional to some more generic blanked implementations for the BinaryWriter and BinaryReader traits it also provides implementations for common smart pointer types.

Both samples (file-based and scoped) are updated with corresponding examples.

A branch containing the patches can be found in [1].

[1] https://git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=debugfs_blobs

Danilo Krummrich (7):
rust: uaccess: add UserSliceReader::read_slice_partial()
rust: uaccess: add UserSliceWriter::write_slice_partial()
rust: debugfs: support for binary large objects
rust: debugfs: support blobs from smart pointers
samples: rust: debugfs: add example for blobs
rust: debugfs: support binary large objects for ScopedDir
samples: rust: debugfs_scoped: add example for blobs

 rust/kernel/debugfs.rs              | 112 ++++++++++++++++-
 rust/kernel/debugfs/file_ops.rs     | 144 ++++++++++++++++++++-
 rust/kernel/debugfs/traits.rs       | 186 +++++++++++++++++++++++++++-
 rust/kernel/uaccess.rs              |  29 +++++
 samples/rust/rust_debugfs.rs        |  13 ++
 samples/rust/rust_debugfs_scoped.rs |  14 ++-
 6 files changed, 487 insertions(+), 11 deletions(-)

base-commit: e406d57be7bd2a4e73ea512c1ae36a40a44e499e

Signature · 1 line
2.51.0
author maintainer
#1 lore raw

PATCH 1/7 rust: uaccess: add UserSliceReader::read_slice_partial()

The existing read_slice() method is a wrapper around copy_from_user() and expects the user buffer to be larger than the destination buffer.

However, userspace may split up writes in multiple partial operations providing an offset into the destination buffer and a smaller user buffer.

In order to support this common case, provide a helper for partial reads.

---
 rust/kernel/uaccess.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)
rust/kernel/uaccess.rs +13 -0
index a8fb4764185a..1b0b57e855c9 100644
@@ -287,6 +287,19 @@ pub fn read_slice(&mut self, out: &mut [u8]) -> Result {
         self.read_raw(out)
     }
 
+    /// Reads raw data from the user slice into a kernel buffer partially.
+    ///
+    /// This is the same as [`Self::read_slice`] but considers the given `offset` into `out` and
+    /// truncates the read to the boundaries of `self` and `out`.
+    ///
+    /// On success, returns the number of bytes read.
+    pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<usize> {
+        let end = offset.checked_add(self.len()).ok_or(EINVAL)?.min(out.len());
+
+        out.get_mut(offset..end)
+            .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len()))
+    }
+
     /// Reads a value of the specified type.
     ///
     /// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of
Signature · 1 line
2.51.0
reviewer
#2 lore raw

Re: [PATCH 1/7] rust: uaccess: add UserSliceReader::read_slice_partial()

Quoted 21 lines · Danilo Krummrich

The existing read_slice() method is a wrapper around copy_from_user() and expects the user buffer to be larger than the destination buffer.

However, userspace may split up writes in multiple partial operations providing an offset into the destination buffer and a smaller user buffer.

In order to support this common case, provide a helper for partial reads.

---
 rust/kernel/uaccess.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)
rust/kernel/uaccess.rs +0 -0
index a8fb4764185a..1b0b57e855c9 100644
@@ -287,6 +287,19 @@ pub fn read_slice(&mut self, out: &mut [u8]) -> Result {
         self.read_raw(out)

rust/kernel/uaccess.rs pub fn read_slice(&mut self, out: &mut [u8]) -> Result { · line 297

Quoted 10 lines
rust/kernel/uaccess.rs +8 -0
     }
 
+    /// Reads raw data from the user slice into a kernel buffer partially.
+    ///
+    /// This is the same as [`Self::read_slice`] but considers the given `offset` into `out` and
+    /// truncates the read to the boundaries of `self` and `out`.
+    ///
+    /// On success, returns the number of bytes read.
+    pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<usize> {
+        let end = offset.checked_add(self.len()).ok_or(EINVAL)?.min(out.len());

Should this be?
let end = offset.checked_add(self.len()).unwrap_or(out.len()).min(out.len());

rust/kernel/uaccess.rs pub fn read_slice(&mut self, out: &mut [u8]) -> Result { · line 300

Quoted 2 lines
rust/kernel/uaccess.rs +2 -0
+        out.get_mut(offset..end)
+            .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len()))

So if out.len() < offset, then we return Ok(0)?

Alice

author maintainer
#3 lore raw

Re: [PATCH 1/7] rust: uaccess: add UserSliceReader::read_slice_partial()

Quoted 24 lines · Alice Ryhl
Quoted 23 lines · Danilo Krummrich

The existing read_slice() method is a wrapper around copy_from_user() and expects the user buffer to be larger than the destination buffer.

However, userspace may split up writes in multiple partial operations providing an offset into the destination buffer and a smaller user buffer.

In order to support this common case, provide a helper for partial reads.

---
 rust/kernel/uaccess.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)
rust/kernel/uaccess.rs +0 -0
index a8fb4764185a..1b0b57e855c9 100644
@@ -287,6 +287,19 @@ pub fn read_slice(&mut self, out: &mut [u8]) -> Result {
         self.read_raw(out)
     }
 
Quoted 11 lines

rust/kernel/uaccess.rs pub fn read_slice(&mut self, out: &mut [u8]) -> Result { · line 297

Quoted 8 lines
rust/kernel/uaccess.rs +8 -0
+    /// Reads raw data from the user slice into a kernel buffer partially.
+    ///
+    /// This is the same as [`Self::read_slice`] but considers the given `offset` into `out` and
+    /// truncates the read to the boundaries of `self` and `out`.
+    ///
+    /// On success, returns the number of bytes read.
+    pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<usize> {
+        let end = offset.checked_add(self.len()).ok_or(EINVAL)?.min(out.len());

Should this be?
let end = offset.checked_add(self.len()).unwrap_or(out.len()).min(out.len());

Yes, that seems reasonable.

Quoted 4 lines

rust/kernel/uaccess.rs pub fn read_slice(&mut self, out: &mut [u8]) -> Result { · line 300

Quoted 2 lines
rust/kernel/uaccess.rs +2 -0
+        out.get_mut(offset..end)
+            .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len()))

So if out.len() < offset, then we return Ok(0)?

Yes, because it tells userspace that there are no more bytes left to read.

author maintainer
#4 lore raw

PATCH 2/7 rust: uaccess: add UserSliceWriter::write_slice_partial()

In reply to Danilo Krummrich

The existing write_slice() method is a wrapper around copy_to_user() and expects the user buffer to be larger than the source buffer.

However, userspace may split up reads in multiple partial operations providing an offset into the source buffer and a smaller user buffer.

In order to support this common case, provide a helper for partial writes.

---
 rust/kernel/uaccess.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
rust/kernel/uaccess.rs +16 -0
index 1b0b57e855c9..11bb8f3265dd 100644
@@ -451,6 +451,22 @@ pub fn write_slice(&mut self, data: &[u8]) -> Result {
         Ok(())
     }
 
+    /// Writes raw data to this user pointer from a kernel buffer partially.
+    ///
+    /// This is the same as [`Self::write_slice`] but considers the given `offset` into `data` and
+    /// truncates the write to the boundaries of `self` and `data`.
+    ///
+    /// On success, returns the number of bytes written.
+    pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usize> {
+        let end = offset
+            .checked_add(self.len())
+            .ok_or(EINVAL)?
+            .min(data.len());
+
+        data.get(offset..end)
+            .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len()))
+    }
+
     /// Writes the provided Rust value to this userspace pointer.
     ///
     /// Fails with [`EFAULT`] if the write happens on a bad address, or if the write goes out of
Signature · 1 line
2.51.0
author maintainer
#5 lore raw

PATCH 3/7 rust: debugfs: support for binary large objects

In reply to Danilo Krummrich

Introduce support for read-only, write-only, and read-write binary files in Rust debugfs. This adds:

- BinaryWriter and BinaryReader traits for writing to and reading from user slices in binary form.
- New Dir methods: read_binary_file(), write_binary_file(), read_write_binary_file.
- Corresponding FileOps implementations: BinaryReadFile, BinaryWriteFile, BinaryReadWriteFile.

This allows kernel modules to expose arbitrary binary data through debugfs, with proper support for offsets and partial reads/writes.

---
 rust/kernel/debugfs.rs          |  67 ++++++++++++++-
 rust/kernel/debugfs/file_ops.rs | 144 +++++++++++++++++++++++++++++++-
 rust/kernel/debugfs/traits.rs   |  45 +++++++++-
 3 files changed, 249 insertions(+), 7 deletions(-)
rust/kernel/debugfs.rs +65 -2
index 381c23b3dd83..b1a3adca7fd4 100644
@@ -21,12 +21,15 @@
 use core::ops::Deref;
 
 mod traits;
-pub use traits::{Reader, Writer};
+pub use traits::{BinaryReader, BinaryWriter, Reader, Writer};
 
 mod callback_adapters;
 use callback_adapters::{FormatAdapter, NoWriter, WritableAdapter};
 mod file_ops;
-use file_ops::{FileOps, ReadFile, ReadWriteFile, WriteFile};
+use file_ops::{
+    BinaryReadFile, BinaryReadWriteFile, BinaryWriteFile, FileOps, ReadFile, ReadWriteFile,
+    WriteFile,
+};
 #[cfg(CONFIG_DEBUG_FS)]
 mod entry;
 #[cfg(CONFIG_DEBUG_FS)]
@@ -150,6 +153,33 @@ pub fn read_only_file<'a, T, E: 'a>(
         self.create_file(name, data, file_ops)
     }
 
+    /// Creates a read-only binary file in this directory.
+    ///
+    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`] on the value
+    /// initialized by `data`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use kernel::c_str;
+    /// # use kernel::debugfs::Dir;
+    /// # use kernel::prelude::*;
+    /// # let dir = Dir::new(c_str!("my_debugfs_dir"));
+    /// let file = KBox::pin_init(dir.read_binary_file(c_str!("foo"), [0x1, 0x2]), GFP_KERNEL)?;
+    /// # Ok::<(), Error>(())
+    /// ```
+    pub fn read_binary_file<'a, T, E: 'a>(
+        &'a self,
+        name: &'a CStr,
+        data: impl PinInit<T, E> + 'a,
+    ) -> impl PinInit<File<T>, E> + 'a
+    where
+        T: BinaryWriter + Send + Sync + 'static,
+    {
+        let file_ops = &<T as BinaryReadFile<_>>::FILE_OPS;
+        self.create_file(name, data, file_ops)
+    }
+
     /// Creates a read-only file in this directory, with contents from a callback.
     ///
     /// `f` must be a function item or a non-capturing closure.
@@ -206,6 +236,22 @@ pub fn read_write_file<'a, T, E: 'a>(
         self.create_file(name, data, file_ops)
     }
 
+    /// Creates a read-write binary file in this directory.
+    ///
+    /// Reading the file uses the [`BinaryWriter`] implementation.
+    /// Writing to the file uses the [`BinaryReader`] implementation.
+    pub fn read_write_binary_file<'a, T, E: 'a>(
+        &'a self,
+        name: &'a CStr,
+        data: impl PinInit<T, E> + 'a,
+    ) -> impl PinInit<File<T>, E> + 'a
+    where
+        T: BinaryWriter + BinaryReader + Send + Sync + 'static,
+    {
+        let file_ops = &<T as BinaryReadWriteFile<_>>::FILE_OPS;
+        self.create_file(name, data, file_ops)
+    }
+
     /// Creates a read-write file in this directory, with logic from callbacks.
     ///
     /// Reading from the file is handled by `f`. Writing to the file is handled by `w`.
@@ -248,6 +294,23 @@ pub fn write_only_file<'a, T, E: 'a>(
         self.create_file(name, data, &T::FILE_OPS)
     }
 
+    /// Creates a write-only binary file in this directory.
+    ///
+    /// The file owns its backing data. Writing to the file uses the [`BinaryReader`]
+    /// implementation.
+    ///
+    /// The file is removed when the returned [`File`] is dropped.
+    pub fn write_binary_file<'a, T, E: 'a>(
+        &'a self,
+        name: &'a CStr,
+        data: impl PinInit<T, E> + 'a,
+    ) -> impl PinInit<File<T>, E> + 'a
+    where
+        T: BinaryReader + Send + Sync + 'static,
+    {
+        self.create_file(name, data, &T::FILE_OPS)
+    }
+
     /// Creates a write-only file in this directory, with write logic from a callback.
     ///
     /// `w` must be a function item or a non-capturing closure.
rust/kernel/debugfs/file_ops.rs +140 -4

164 lines not on this page · raw message

rust/kernel/debugfs/traits.rs +44 -1

63 lines not on this page · raw message

Signature · 1 line
2.51.0
reviewer
#6 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 23 lines · Danilo Krummrich

Introduce support for read-only, write-only, and read-write binary files in Rust debugfs. This adds:

- BinaryWriter and BinaryReader traits for writing to and reading from user slices in binary form.
- New Dir methods: read_binary_file(), write_binary_file(), read_write_binary_file.
- Corresponding FileOps implementations: BinaryReadFile, BinaryWriteFile, BinaryReadWriteFile.

This allows kernel modules to expose arbitrary binary data through debugfs, with proper support for offsets and partial reads/writes.

rust/kernel/debugfs/file_ops.rs +8 -0
+extern "C" fn blob_write<T: BinaryReader>(
+    file: *mut bindings::file,
+    buf: *const c_char,
+    count: usize,
+    ppos: *mut bindings::loff_t,
+) -> isize {
+    // SAFETY:
+    // - `file` is a valid pointer to a `struct file`.

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 321

Quoted 10 lines
rust/kernel/debugfs/file_ops.rs +10 -0
+    // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
+    let this = unsafe { &*((*file).private_data.cast::<T>()) };
+
+    // SAFETY: `ppos` is a valid `loff_t` pointer.
+    let pos = unsafe { &mut *ppos };
+
+    let mut reader = UserSlice::new(UserPtr::from_ptr(buf.cast_mut().cast()), count).reader();
+
+    let ret = || -> Result<isize> {
+        let offset = (*pos).try_into()?;

So offsets larger than the buffer result in Ok(0) unless the offset doesn't fit in an usize, in which case it's an error instead? I think we should treat offsets that are too large in the same manner no matter how large they are.

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 324

Quoted 2 lines
rust/kernel/debugfs/file_ops.rs +2 -0
+        let read = this.read_from_slice(&mut reader, offset)?;
+        *pos += bindings::loff_t::try_from(read)?;

This addition could overflow and panic the kernel.

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 337

Quoted 12 lines
rust/kernel/debugfs/file_ops.rs +12 -0
+        Ok(read.try_into()?)
+    }();
+
+    match ret {
+        Ok(n) => n,
+        Err(e) => e.to_errno() as isize,
+    }
+}
+
+pub(crate) trait BinaryWriteFile<T> {
+    const FILE_OPS: FileOps<T>;
+}

Hmm ... this is inconsistent with how we do vtables in other parts of kernel. Normally a struct is used instead of a trait (see e.g. miscdevice or block). But the inconsistency is already present.

Alice

author maintainer
#7 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 31 lines · Alice Ryhl
Quoted 30 lines · Danilo Krummrich

Introduce support for read-only, write-only, and read-write binary files in Rust debugfs. This adds:

- BinaryWriter and BinaryReader traits for writing to and reading from user slices in binary form.
- New Dir methods: read_binary_file(), write_binary_file(), read_write_binary_file.
- Corresponding FileOps implementations: BinaryReadFile, BinaryWriteFile, BinaryReadWriteFile.

This allows kernel modules to expose arbitrary binary data through debugfs, with proper support for offsets and partial reads/writes.

rust/kernel/debugfs/file_ops.rs +15 -0
+extern "C" fn blob_write<T: BinaryReader>(
+    file: *mut bindings::file,
+    buf: *const c_char,
+    count: usize,
+    ppos: *mut bindings::loff_t,
+) -> isize {
+    // SAFETY:
+    // - `file` is a valid pointer to a `struct file`.
+    // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
+    let this = unsafe { &*((*file).private_data.cast::<T>()) };
+
+    // SAFETY: `ppos` is a valid `loff_t` pointer.
+    let pos = unsafe { &mut *ppos };
+
+    let mut reader = UserSlice::new(UserPtr::from_ptr(buf.cast_mut().cast()), count).reader();
Quoted 8 lines

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 266

Quoted 3 lines
rust/kernel/debugfs/file_ops.rs +3 -0
+
+    let ret = || -> Result<isize> {
+        let offset = (*pos).try_into()?;

So offsets larger than the buffer result in Ok(0) unless the offset doesn't fit in an usize, in which case it's an error instead? I think we should treat offsets that are too large in the same manner no matter how large they are.

The offset being larger than thhe buffer is fine, userspace has to try to read until the kernel indicates that there are no more bytes left to read by returning zero.

But if the offset is larger than a usize there isn't a chance this can ever be successful in the first place, hence I'd consider this an error.

Quoted 4 lines

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 324

Quoted 2 lines
rust/kernel/debugfs/file_ops.rs +2 -0
+        let read = this.read_from_slice(&mut reader, offset)?;
+        *pos += bindings::loff_t::try_from(read)?;

This addition could overflow and panic the kernel.

I don't see a real scenario where this could overflow when read_from_slice() was successful, but I (also) think this should be checked_add() instead.

Quoted 8 lines
Quoted 8 lines
rust/kernel/debugfs/file_ops.rs +8 -0
+        Ok(read.try_into()?)
+    }();
+
+    match ret {
+        Ok(n) => n,
+        Err(e) => e.to_errno() as isize,
+    }
+}
Quoted 8 lines

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 337

Quoted 4 lines
rust/kernel/debugfs/file_ops.rs +4 -0
+
+pub(crate) trait BinaryWriteFile<T> {
+    const FILE_OPS: FileOps<T>;
+}

Hmm ... this is inconsistent with how we do vtables in other parts of kernel. Normally a struct is used instead of a trait (see e.g. miscdevice or block). But the inconsistency is already present.

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

author maintainer
#8 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

reviewer
#9 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Quoted 7 lines

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

You can still use a struct:

struct BinaryWriterVtable<T: BinaryWriter + Sync>;

impl<T: BinaryWriter + Sync> BinaryWriterVtable<T> {
    const VTABLE: bindings::foo = ...;
}
author maintainer
#10 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 15 lines · Alice Ryhl
Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Quoted 7 lines

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

Quoted 7 lines

You can still use a struct:

struct BinaryWriterVtable<T: BinaryWriter + Sync>;

impl<T: BinaryWriter + Sync> BinaryWriterVtable<T> {
    const VTABLE: bindings::foo = ...;
}

Yeah, but do we get something for adding yet another type in this case?

Another point to consider is if we want a more generic fops abstraction type.

In any case, I'd like to add this as good first issue for the whole file to be changed accordingly.

reviewer
#11 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 24 lines · Danilo Krummrich
Quoted 15 lines · Alice Ryhl
Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Quoted 7 lines

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

Quoted 7 lines

You can still use a struct:

struct BinaryWriterVtable<T: BinaryWriter + Sync>;

impl<T: BinaryWriter + Sync> BinaryWriterVtable<T> {
    const VTABLE: bindings::foo = ...;
}
Quoted 6 lines

Yeah, but do we get something for adding yet another type in this case?

Another point to consider is if we want a more generic fops abstraction type.

In any case, I'd like to add this as good first issue for the whole file to be changed accordingly.

Yes, keep it as-is for consistency with the rest of the file, even if the file is inconsistent with the rest of kernel. Please go ahead and file a good-first-issue for this.

Alice

author maintainer
#12 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 27 lines · Alice Ryhl
Quoted 26 lines · Danilo Krummrich
Quoted 15 lines · Alice Ryhl
Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Quoted 7 lines

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

Quoted 7 lines

You can still use a struct:

struct BinaryWriterVtable<T: BinaryWriter + Sync>;

impl<T: BinaryWriter + Sync> BinaryWriterVtable<T> {
    const VTABLE: bindings::foo = ...;
}

Yeah, but do we get something for adding yet another type in this case?

Quoted 8 lines
Quoted 4 lines

Another point to consider is if we want a more generic fops abstraction type.

In any case, I'd like to add this as good first issue for the whole file to be changed accordingly.

Yes, keep it as-is for consistency with the rest of the file, even if the file is inconsistent with the rest of kernel. Please go ahead and file a good-first-issue for this.

Before doing so, can you please answer the question above? While I'm all for consistency, in this specific case it seems we'd need another indirection for that. And I'm not convinced that's an improvement.

reviewer
#13 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 34 lines · Danilo Krummrich
Quoted 32 lines · Alice Ryhl
Quoted 30 lines · Danilo Krummrich
Quoted 15 lines · Alice Ryhl
Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Quoted 7 lines

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T
impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

Quoted 7 lines

You can still use a struct:

struct BinaryWriterVtable<T: BinaryWriter + Sync>;

impl<T: BinaryWriter + Sync> BinaryWriterVtable<T> {
    const VTABLE: bindings::foo = ...;
}

Yeah, but do we get something for adding yet another type in this case?

Another point to consider is if we want a more generic fops abstraction type.

In any case, I'd like to add this as good first issue for the whole file to be changed accordingly.

Quoted 7 lines
Quoted 3 lines

Yes, keep it as-is for consistency with the rest of the file, even if the file is inconsistent with the rest of kernel. Please go ahead and file a good-first-issue for this.

Before doing so, can you please answer the question above? While I'm all for consistency, in this specific case it seems we'd need another indirection for that. And I'm not convinced that's an improvement.

The choice is between adding a new type or a new trait. There's no intrinsic advantage to choosing either one, but the rest of kernel chose "new type" over "new trait", so it makes sense to be consistent.

Alice

author maintainer
#14 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 39 lines · Alice Ryhl
Quoted 38 lines · Danilo Krummrich
Quoted 35 lines · Alice Ryhl
Quoted 24 lines · Danilo Krummrich
Quoted 15 lines · Alice Ryhl
Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Danilo Krummrich

The reason I went with a trait is because that's consistent within the file.

Otherwise, I don't mind one or the other. If we always want to use a struct, I'm fine with that. :)

Quoted 7 lines

Actually, there's another reason I forgot about since I sent the series. :)

We need it because we derive it from blanket implementations:

impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T
impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T

Quoted 7 lines

You can still use a struct:

struct BinaryWriterVtable<T: BinaryWriter + Sync>;

impl<T: BinaryWriter + Sync> BinaryWriterVtable<T> {
    const VTABLE: bindings::foo = ...;
}
Quoted 6 lines

Yeah, but do we get something for adding yet another type in this case?

Another point to consider is if we want a more generic fops abstraction type.

In any case, I'd like to add this as good first issue for the whole file to be changed accordingly.

Yes, keep it as-is for consistency with the rest of the file, even if the file is inconsistent with the rest of kernel. Please go ahead and file a good-first-issue for this.

Quoted 7 lines
Quoted 3 lines

Before doing so, can you please answer the question above? While I'm all for consistency, in this specific case it seems we'd need another indirection for that. And I'm not convinced that's an improvement.

The choice is between adding a new type or a new trait. There's no intrinsic advantage to choosing either one, but the rest of kernel chose "new type" over "new trait", so it makes sense to be consistent.

My hesitation came from the assumption that we'd need another type (additional to the existing trait). But we can indeed replace it, so that's fine.

reviewer
#15 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

In reply to Danilo Krummrich
Quoted 41 lines · Danilo Krummrich
Quoted 31 lines · Alice Ryhl
Quoted 30 lines · Danilo Krummrich

Introduce support for read-only, write-only, and read-write binary files in Rust debugfs. This adds:

- BinaryWriter and BinaryReader traits for writing to and reading from user slices in binary form.
- New Dir methods: read_binary_file(), write_binary_file(), read_write_binary_file.
- Corresponding FileOps implementations: BinaryReadFile, BinaryWriteFile, BinaryReadWriteFile.

This allows kernel modules to expose arbitrary binary data through debugfs, with proper support for offsets and partial reads/writes.

rust/kernel/debugfs/file_ops.rs +15 -0
+extern "C" fn blob_write<T: BinaryReader>(
+    file: *mut bindings::file,
+    buf: *const c_char,
+    count: usize,
+    ppos: *mut bindings::loff_t,
+) -> isize {
+    // SAFETY:
+    // - `file` is a valid pointer to a `struct file`.
+    // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
+    let this = unsafe { &*((*file).private_data.cast::<T>()) };
+
+    // SAFETY: `ppos` is a valid `loff_t` pointer.
+    let pos = unsafe { &mut *ppos };
+
+    let mut reader = UserSlice::new(UserPtr::from_ptr(buf.cast_mut().cast()), count).reader();
Quoted 8 lines

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 266

Quoted 3 lines
rust/kernel/debugfs/file_ops.rs +3 -0
+
+    let ret = || -> Result<isize> {
+        let offset = (*pos).try_into()?;

So offsets larger than the buffer result in Ok(0) unless the offset doesn't fit in an usize, in which case it's an error instead? I think we should treat offsets that are too large in the same manner no matter how large they are.

Quoted 6 lines

The offset being larger than thhe buffer is fine, userspace has to try to read until the kernel indicates that there are no more bytes left to read by returning zero.

But if the offset is larger than a usize there isn't a chance this can ever be successful in the first place, hence I'd consider this an error.

I don't really agree with this. Obviously we have to return Ok(0) if the position is equal to the buffer size. But for positions strictly larger than the buffer size I think it's reasonable to choose between Ok(0) or an error. But please, let's be consistent about whether we return Ok(0) or errors for positions larger than the buffer size.

Besides, it could succeed. Imagine a debugfs file whose contents aren't stored in memory as a big byte array, but can be fetched / computed on the fly based on any offset. Such a file could be larger than 4GB on a 32-bit machine no problem.

Alice

author maintainer
#16 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 46 lines · Alice Ryhl
Quoted 45 lines · Danilo Krummrich
Quoted 31 lines · Alice Ryhl
Quoted 30 lines · Danilo Krummrich

Introduce support for read-only, write-only, and read-write binary files in Rust debugfs. This adds:

- BinaryWriter and BinaryReader traits for writing to and reading from user slices in binary form.
- New Dir methods: read_binary_file(), write_binary_file(), read_write_binary_file.
- Corresponding FileOps implementations: BinaryReadFile, BinaryWriteFile, BinaryReadWriteFile.

This allows kernel modules to expose arbitrary binary data through debugfs, with proper support for offsets and partial reads/writes.

rust/kernel/debugfs/file_ops.rs +15 -0
+extern "C" fn blob_write<T: BinaryReader>(
+    file: *mut bindings::file,
+    buf: *const c_char,
+    count: usize,
+    ppos: *mut bindings::loff_t,
+) -> isize {
+    // SAFETY:
+    // - `file` is a valid pointer to a `struct file`.
+    // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
+    let this = unsafe { &*((*file).private_data.cast::<T>()) };
+
+    // SAFETY: `ppos` is a valid `loff_t` pointer.
+    let pos = unsafe { &mut *ppos };
+
+    let mut reader = UserSlice::new(UserPtr::from_ptr(buf.cast_mut().cast()), count).reader();
Quoted 8 lines

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 266

Quoted 3 lines
rust/kernel/debugfs/file_ops.rs +3 -0
+
+    let ret = || -> Result<isize> {
+        let offset = (*pos).try_into()?;

So offsets larger than the buffer result in Ok(0) unless the offset doesn't fit in an usize, in which case it's an error instead? I think we should treat offsets that are too large in the same manner no matter how large they are.

The offset being larger than thhe buffer is fine, userspace has to try to read until the kernel indicates that there are no more bytes left to read by returning zero.

Quoted 8 lines
Quoted 2 lines

But if the offset is larger than a usize there isn't a chance this can ever be successful in the first place, hence I'd consider this an error.

I don't really agree with this. Obviously we have to return Ok(0) if the position is equal to the buffer size. But for positions strictly larger than the buffer size I think it's reasonable to choose between Ok(0) or an error. But please, let's be consistent about whether we return Ok(0) or errors for positions larger than the buffer size.

There's not really a choice, it has to be Ok(0), otherwise we break userspace.

However, you do have a point with how the offset conversion to usize should be handled. We shouldn't try to convert it to usize in the first place, but rather pass it through as it is and make it a common offset-larger-buffer case.

We probably also want a type alias for bindings::loff_t.

reviewer
#17 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 56 lines · Danilo Krummrich
Quoted 46 lines · Alice Ryhl
Quoted 45 lines · Danilo Krummrich
Quoted 31 lines · Alice Ryhl
Quoted 30 lines · Danilo Krummrich

Introduce support for read-only, write-only, and read-write binary files in Rust debugfs. This adds:

- BinaryWriter and BinaryReader traits for writing to and reading from user slices in binary form.
- New Dir methods: read_binary_file(), write_binary_file(), read_write_binary_file.
- Corresponding FileOps implementations: BinaryReadFile, BinaryWriteFile, BinaryReadWriteFile.

This allows kernel modules to expose arbitrary binary data through debugfs, with proper support for offsets and partial reads/writes.

rust/kernel/debugfs/file_ops.rs +15 -0
+extern "C" fn blob_write<T: BinaryReader>(
+    file: *mut bindings::file,
+    buf: *const c_char,
+    count: usize,
+    ppos: *mut bindings::loff_t,
+) -> isize {
+    // SAFETY:
+    // - `file` is a valid pointer to a `struct file`.
+    // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
+    let this = unsafe { &*((*file).private_data.cast::<T>()) };
+
+    // SAFETY: `ppos` is a valid `loff_t` pointer.
+    let pos = unsafe { &mut *ppos };
+
+    let mut reader = UserSlice::new(UserPtr::from_ptr(buf.cast_mut().cast()), count).reader();
Quoted 8 lines

rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 266

Quoted 3 lines
rust/kernel/debugfs/file_ops.rs +3 -0
+
+    let ret = || -> Result<isize> {
+        let offset = (*pos).try_into()?;

So offsets larger than the buffer result in Ok(0) unless the offset doesn't fit in an usize, in which case it's an error instead? I think we should treat offsets that are too large in the same manner no matter how large they are.

The offset being larger than thhe buffer is fine, userspace has to try to read until the kernel indicates that there are no more bytes left to read by returning zero.

Quoted 8 lines
Quoted 2 lines

But if the offset is larger than a usize there isn't a chance this can ever be successful in the first place, hence I'd consider this an error.

I don't really agree with this. Obviously we have to return Ok(0) if the position is equal to the buffer size. But for positions strictly larger than the buffer size I think it's reasonable to choose between Ok(0) or an error. But please, let's be consistent about whether we return Ok(0) or errors for positions larger than the buffer size.

Quoted 5 lines

There's not really a choice, it has to be Ok(0), otherwise we break userspace.

However, you do have a point with how the offset conversion to usize should be handled. We shouldn't try to convert it to usize in the first place, but rather pass it through as it is and make it a common offset-larger-buffer case.

SGTM.

Quoted 1 line

We probably also want a type alias for bindings::loff_t.

I ended up using i64 for simple_read_from_buffer in iov.rs instead of loff_t. But if they can differ, then yeah let's introduce a loff_t type alias.

Alice

author maintainer
#18 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 3 lines · Alice Ryhl

I ended up using i64 for simple_read_from_buffer in iov.rs instead of loff_t. But if they can differ, then yeah let's introduce a loff_t type alias.

No, I don't think they can differ (I used i64 in earlier version that didn't make it to the list as well), but I think it could still make sense to indicate the relationship with loff_t. When I see an i64, an offset into a buffer is not the first thing that comes to my mind.

What about uaccess::Offset?

reviewer
#19 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 11 lines · Danilo Krummrich
Quoted 3 lines · Alice Ryhl

I ended up using i64 for simple_read_from_buffer in iov.rs instead of loff_t. But if they can differ, then yeah let's introduce a loff_t type alias.

No, I don't think they can differ (I used i64 in earlier version that didn't make it to the list as well), but I think it could still make sense to indicate the relationship with loff_t. When I see an i64, an offset into a buffer is not the first thing that comes to my mind.

What about uaccess::Offset?

Hmm. That seems wrong. loff_t is a *file position*, so it should go in kernel::fs, right? We're only using it in uaccess/iov because they happen to have utility methods to help with implementing fops entries. None of the "base" uaccess/iov functions use loff_t for anything since they deal with sizes in the address space, for which usize is the correct type.

Alice

author maintainer
#20 lore raw

Re: [PATCH 3/7] rust: debugfs: support for binary large objects

Quoted 11 lines · Alice Ryhl
Quoted 10 lines · Danilo Krummrich
Quoted 3 lines · Alice Ryhl

I ended up using i64 for simple_read_from_buffer in iov.rs instead of loff_t. But if they can differ, then yeah let's introduce a loff_t type alias.

No, I don't think they can differ (I used i64 in earlier version that didn't make it to the list as well), but I think it could still make sense to indicate the relationship with loff_t. When I see an i64, an offset into a buffer is not the first thing that comes to my mind.

Quoted 8 lines
Quoted 1 line

What about uaccess::Offset?

Hmm. That seems wrong. loff_t is a *file position*, so it should go in kernel::fs, right? We're only using it in uaccess/iov because they happen to have utility methods to help with implementing fops entries. None of the "base" uaccess/iov functions use loff_t for anything since they deal with sizes in the address space, for which usize is the correct type.

Indeed, fair enough.

author maintainer
#21 lore raw

PATCH 4/7 rust: debugfs: support blobs from smart pointers

In reply to Danilo Krummrich

Extend Rust debugfs binary support to allow exposing data stored in common smart pointers and heap-allocated collections.

- Implement BinaryWriter for Box<T>, Pin<Box<T>>, Arc<T>, and Vec<T>.
- Introduce BinaryReaderMut for mutable binary access with outer locks.
- Implement BinaryReaderMut for Box<T>, Vec<T>, and base types.
- Update BinaryReader to delegate to BinaryReaderMut for Mutex<T>, Box<T>, Pin<Box<T>> and Arc<T>.

This enables debugfs files to directly expose or update data stored inside heap-allocated, reference-counted, or lock-protected containers without manual dereferencing or locking.

---
 rust/kernel/debugfs.rs        |   2 +-
 rust/kernel/debugfs/traits.rs | 145 +++++++++++++++++++++++++++++++++-
 2 files changed, 144 insertions(+), 3 deletions(-)
rust/kernel/debugfs.rs +1 -1
index b1a3adca7fd4..3c3bbcc126ef 100644
@@ -21,7 +21,7 @@
 use core::ops::Deref;
 
 mod traits;
-pub use traits::{BinaryReader, BinaryWriter, Reader, Writer};
+pub use traits::{BinaryReader, BinaryReaderMut, BinaryWriter, Reader, Writer};
 
 mod callback_adapters;
 use callback_adapters::{FormatAdapter, NoWriter, WritableAdapter};
rust/kernel/debugfs/traits.rs +143 -2

189 lines not on this page · raw message

Signature · 1 line
2.51.0
#22 lore raw

Re: [PATCH 4/7] rust: debugfs: support blobs from smart pointers

Quoted 92 lines · Danilo Krummrich

Extend Rust debugfs binary support to allow exposing data stored in common smart pointers and heap-allocated collections.

- Implement BinaryWriter for Box<T>, Pin<Box<T>>, Arc<T>, and Vec<T>.
- Introduce BinaryReaderMut for mutable binary access with outer locks.
- Implement BinaryReaderMut for Box<T>, Vec<T>, and base types.
- Update BinaryReader to delegate to BinaryReaderMut for Mutex<T>, Box<T>, Pin<Box<T>> and Arc<T>.

This enables debugfs files to directly expose or update data stored inside heap-allocated, reference-counted, or lock-protected containers without manual dereferencing or locking.

---
 rust/kernel/debugfs.rs        |   2 +-
 rust/kernel/debugfs/traits.rs | 145 +++++++++++++++++++++++++++++++++-
 2 files changed, 144 insertions(+), 3 deletions(-)
rust/kernel/debugfs.rs +1 -1
index b1a3adca7fd4..3c3bbcc126ef 100644
@@ -21,7 +21,7 @@
 use core::ops::Deref;
 
 mod traits;
-pub use traits::{BinaryReader, BinaryWriter, Reader, Writer};
+pub use traits::{BinaryReader, BinaryReaderMut, BinaryWriter, Reader, Writer};
 
 mod callback_adapters;
 use callback_adapters::{FormatAdapter, NoWriter, WritableAdapter};
rust/kernel/debugfs/traits.rs +26 -0
index 60a6ee6c6b58..bcd0a9db3cc9 100644
@@ -3,11 +3,14 @@
 
 //! Traits for rendering or updating values exported to DebugFS.
 
+use crate::alloc::Allocator;
 use crate::prelude::*;
+use crate::sync::Arc;
 use crate::sync::Mutex;
 use crate::transmute::{AsBytes, FromBytes};
 use crate::uaccess::{UserSliceReader, UserSliceWriter};
 use core::fmt::{self, Debug, Formatter};
+use core::ops::{Deref, DerefMut};
 use core::str::FromStr;
 use core::sync::atomic::{
     AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU64,
@@ -50,12 +53,14 @@ pub trait BinaryWriter {
     fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize>;
 }
 
+// Base implementation for any `T: AsBytes`.
 impl<T: AsBytes> BinaryWriter for T {
     fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
         writer.write_slice_partial(self.as_bytes(), offset)
     }
 }
 
+// Delegate for `Mutex<T>`: Support a `T` with an outer mutex.
 impl<T: BinaryWriter> BinaryWriter for Mutex<T> {
     fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
         let guard = self.lock();
@@ -64,6 +69,56 @@ fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<
     }
 }
 
+// Delegate for `Box<T, A>`: Support a `Box<T, A>` with no lock or an inner lock.
+impl<T, A> BinaryWriter for Box<T, A>
+where
+    T: BinaryWriter,
+    A: Allocator,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        self.deref().write_to_slice(writer, offset)
+    }
+}
+
+// Delegate for `Pin<Box<T, A>>`: Support a `Pin<Box<T, A>>` with no lock or an inner lock.
+impl<T, A> BinaryWriter for Pin<Box<T, A>>
+where
+    T: BinaryWriter,
+    A: Allocator,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        self.deref().write_to_slice(writer, offset)
+    }
+}

rust/kernel/debugfs/traits.rs fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result< · line 102

Quoted 10 lines
rust/kernel/debugfs/traits.rs +10 -0
+
+// Delegate for `Arc<T>`: Support a `Arc<T>` with no lock or an inner lock.
+impl<T> BinaryWriter for Arc<T>
+where
+    T: BinaryWriter,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        self.deref().write_to_slice(writer, offset)
+    }
+}

For Box, Pin<Box<, and Arc, where the only operation being performed is to deref, is there a reason that we couldn't have the File object be *inside* the object, thus avoiding any need for these? I can't see them causing trouble, but

```
Box<File<T>>
Pin<Box<File<T>>>
Arc<File<T>>
```

seem like they'd usually be fine. The one caveat I can think of is that if you had other functions that wanted to take an &Arc<T> for operations on the Arc, then having an Arc<File<T>> would be suboptimal. Am I missing something?

Depending on the use case I'm missing, would a blanket implementation for T: Deref in this case and DerefMut later on make more sense? That should contract these into a single definition and generalize to e.g. ListArc without further code.

Quoted 65 lines
rust/kernel/debugfs/traits.rs +58 -0
+
+// Delegate for `Vec<T, A>`.
+impl<T, A> BinaryWriter for Vec<T, A>
+where
+    T: AsBytes,
+    A: Allocator,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        let slice = self.as_slice();
+
+        // SAFETY: `T: AsBytes` 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_partial(buffer, offset)
+    }
+}
+
 /// A trait for types that can be updated from a user slice.
 ///
 /// This works similarly to `FromStr`, but operates on a `UserSliceReader` rather than a &str.
@@ -92,6 +147,70 @@ fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result {
 }
 
 /// Trait for types that can be constructed from a binary representation.
+///
+/// See also [`BinaryReader`] for interior mutability.
+pub trait BinaryReaderMut {
+    /// Reads the binary form of `self` from `reader`.
+    ///
+    /// Same as [`BinaryReader::read_from_slice`], but takes a mutable reference.
+    ///
+    /// `offset` is the requested offset into the binary representation of `self`.
+    ///
+    /// On success, returns the number of bytes read from `reader`.
+    fn read_from_slice_mut(&mut self, reader: &mut UserSliceReader, offset: usize)
+        -> Result<usize>;
+}
+
+// Base implementation for any `T: AsBytes + FromBytes`.
+impl<T: AsBytes + FromBytes> BinaryReaderMut for T {
+    fn read_from_slice_mut(
+        &mut self,
+        reader: &mut UserSliceReader,
+        offset: usize,
+    ) -> Result<usize> {
+        reader.read_slice_partial(self.as_bytes_mut(), offset)
+    }
+}
+
+// Delegate for `Box<T, A>`: Support a `Box<T, A>` with an outer lock.
+impl<T: ?Sized + BinaryReaderMut, A: Allocator> BinaryReaderMut for Box<T, A> {
+    fn read_from_slice_mut(
+        &mut self,
+        reader: &mut UserSliceReader,
+        offset: usize,
+    ) -> Result<usize> {
+        self.deref_mut().read_from_slice_mut(reader, offset)
+    }
+}
+
+// Delegate for `Vec<T, A>`: Support a `Vec<T, A>` with an outer lock.
+impl<T, A> BinaryReaderMut for Vec<T, A>
+where

rust/kernel/debugfs/traits.rs fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result { · line 198

Quoted 10 lines
rust/kernel/debugfs/traits.rs +10 -0
+    T: AsBytes + FromBytes,
+    A: Allocator,
+{
+    fn read_from_slice_mut(
+        &mut self,
+        reader: &mut UserSliceReader,
+        offset: usize,
+    ) -> Result<usize> {
+        let slice = self.as_mut_slice();
+

Nit: This is safe, but it also requires FromBytes, and is &mut, &mut

Quoted 57 lines
rust/kernel/debugfs/traits.rs +39 -3
+        // SAFETY: `T: AsBytes` allows us to treat `&[T]` as `&[u8]`.
+        let buffer = unsafe {
+            core::slice::from_raw_parts_mut(
+                slice.as_mut_ptr().cast(),
+                core::mem::size_of_val(slice),
+            )
+        };
+
+        reader.read_slice_partial(buffer, offset)
+    }
+}
+
+/// Trait for types that can be constructed from a binary representation.
+///
+/// See also [`BinaryReaderMut`] for the mutable version.
 pub trait BinaryReader {
     /// Reads the binary form of `self` from `reader`.
     ///
@@ -101,11 +220,33 @@ pub trait BinaryReader {
     fn read_from_slice(&self, reader: &mut UserSliceReader, offset: usize) -> Result<usize>;
 }
 
-impl<T: AsBytes + FromBytes> BinaryReader for Mutex<T> {
+// Delegate for `Mutex<T>`: Support a `T` with an outer `Mutex`.
+impl<T: BinaryReaderMut> BinaryReader for Mutex<T> {
     fn read_from_slice(&self, reader: &mut UserSliceReader, offset: usize) -> Result<usize> {
         let mut this = self.lock();
 
-        reader.read_slice_partial(this.as_bytes_mut(), offset)
+        this.read_from_slice_mut(reader, offset)
+    }
+}
+
+// Delegate for `Box<T, A>`: Support a `Box<T, A>` with an inner lock.
+impl<T: ?Sized + BinaryReader, A: Allocator> BinaryReader for Box<T, A> {
+    fn read_from_slice(&self, reader: &mut UserSliceReader, offset: usize) -> Result<usize> {
+        self.deref().read_from_slice(reader, offset)
+    }
+}
+
+// Delegate for `Pin<Box<T, A>>`: Support a `Pin<Box<T, A>>` with an inner lock.
+impl<T: ?Sized + BinaryReader, A: Allocator> BinaryReader for Pin<Box<T, A>> {
+    fn read_from_slice(&self, reader: &mut UserSliceReader, offset: usize) -> Result<usize> {
+        self.deref().read_from_slice(reader, offset)
+    }
+}
+
+// Delegate for `Arc<T>`: Support an `Arc<T>` with an inner lock.
+impl<T: ?Sized + BinaryReader> BinaryReader for Arc<T> {
+    fn read_from_slice(&self, reader: &mut UserSliceReader, offset: usize) -> Result<usize> {
+        self.deref().read_from_slice(reader, offset)
     }
 }
 
--

2.51.0

author maintainer
#23 lore raw

Re: [PATCH 4/7] rust: debugfs: support blobs from smart pointers

Quoted 59 lines · Matthew Maurer
Quoted 36 lines · Danilo Krummrich
rust/kernel/debugfs/traits.rs +23 -0
+// Base implementation for any `T: AsBytes`.
 impl<T: AsBytes> BinaryWriter for T {
     fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
         writer.write_slice_partial(self.as_bytes(), offset)
     }
 }
 
+// Delegate for `Mutex<T>`: Support a `T` with an outer mutex.
 impl<T: BinaryWriter> BinaryWriter for Mutex<T> {
     fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
         let guard = self.lock();
@@ -64,6 +69,56 @@ fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<
     }
 }
 
+// Delegate for `Box<T, A>`: Support a `Box<T, A>` with no lock or an inner lock.
+impl<T, A> BinaryWriter for Box<T, A>
+where
+    T: BinaryWriter,
+    A: Allocator,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        self.deref().write_to_slice(writer, offset)
+    }
+}
+
+// Delegate for `Pin<Box<T, A>>`: Support a `Pin<Box<T, A>>` with no lock or an inner lock.
+impl<T, A> BinaryWriter for Pin<Box<T, A>>
+where
+    T: BinaryWriter,
+    A: Allocator,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        self.deref().write_to_slice(writer, offset)
+    }
+}

rust/kernel/debugfs/traits.rs fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result< · line 102

Quoted 10 lines
rust/kernel/debugfs/traits.rs +10 -0
+
+// Delegate for `Arc<T>`: Support a `Arc<T>` with no lock or an inner lock.
+impl<T> BinaryWriter for Arc<T>
+where
+    T: BinaryWriter,
+{
+    fn write_to_slice(&self, writer: &mut UserSliceWriter, offset: usize) -> Result<usize> {
+        self.deref().write_to_slice(writer, offset)
+    }
+}

For Box, Pin<Box<, and Arc, where the only operation being performed is to deref, is there a reason that we couldn't have the File object be *inside* the object, thus avoiding any need for these? I can't see them causing trouble, but

```
Box<File<T>>
Pin<Box<File<T>>>
Arc<File<T>>
```
Quoted 4 lines

seem like they'd usually be fine. The one caveat I can think of is that if you had other functions that wanted to take an &Arc<T> for operations on the Arc, then having an Arc<File<T>> would be suboptimal. Am I missing something?

I think this way around is not compatible with scoped API.

Besides that, there is a semantical difference between File<Arc<T>> and Arc<File<T>>, i.e. the file itself would become reference counted.

Quoted 4 lines

Depending on the use case I'm missing, would a blanket implementation for T: Deref in this case and DerefMut later on make more sense? That should contract these into a single definition and generalize to e.g. ListArc without further code.

It was also my first thought to generalize over T: Deref, but unfortunately this does lead to conflicting implementations of BinaryWriter.

Quoted 7 lines
Quoted 7 lines
rust/kernel/debugfs/traits.rs +7 -0
+// 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,
+    A: Allocator,
+{
+    fn read_from_slice_mut(
Quoted 8 lines

rust/kernel/debugfs/traits.rs fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result { · line 198

Quoted 6 lines
rust/kernel/debugfs/traits.rs +6 -0
+        &mut self,
+        reader: &mut UserSliceReader,
+        offset: usize,
+    ) -> Result<usize> {
+        let slice = self.as_mut_slice();
+

Nit: This is safe, but it also requires FromBytes, and is &mut, &mut

It already requires T: AsBytes + FromBytes above. Or do you mean something else?

#24 lore raw

Re: [PATCH 4/7] rust: debugfs: support blobs from smart pointers

Quoted 3 lines

I think this way around is not compatible with scoped API.

I thought you'd just be able to deref inside the callback, but perhaps I'm wrong. I'll try to find a chance to pull down the patch and check.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
rust/kernel/debugfs/traits.rs +7 -0
+// 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,
+    A: Allocator,
+{
+    fn read_from_slice_mut(
Quoted 8 lines

rust/kernel/debugfs/traits.rs fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result { · line 198

Quoted 6 lines
rust/kernel/debugfs/traits.rs +6 -0
+        &mut self,
+        reader: &mut UserSliceReader,
+        offset: usize,
+    ) -> Result<usize> {
+        let slice = self.as_mut_slice();
+

Nit: This is safe, but it also requires FromBytes, and is &mut, &mut

Quoted 1 line

It already requires T: AsBytes + FromBytes above. Or do you mean something else?

I mean the safety comment. Your code is right, the comment needs to be updated.

author maintainer
#25 lore raw

PATCH 5/7 samples: rust: debugfs: add example for blobs

In reply to Danilo Krummrich

Extend the Rust debugfs sample to demonstrate usage of binary file support. The example now shows how to expose both fixed-size arrays and dynamically sized vectors as binary blobs in debugfs.

---
 samples/rust/rust_debugfs.rs | 13 +++++++++++++
 1 file changed, 13 insertions(+)
samples/rust/rust_debugfs.rs +13 -0
index 82b61a15a34b..75ceb95276fa 100644
@@ -38,6 +38,7 @@
 use kernel::debugfs::{Dir, File};
 use kernel::new_mutex;
 use kernel::prelude::*;
+use kernel::sizes::*;
 use kernel::sync::Mutex;
 
 use kernel::{acpi, device::Core, of, platform, str::CString, types::ARef};
@@ -62,6 +63,10 @@ struct RustDebugFs {
     counter: File<AtomicUsize>,
     #[pin]
     inner: File<Mutex<Inner>>,
+    #[pin]
+    array_blob: File<Mutex<[u8; 4]>>,
+    #[pin]
+    vector_blob: File<Mutex<KVec<u8>>>,
 }
 
 #[derive(Debug)]
@@ -143,6 +148,14 @@ fn new(pdev: &platform::Device<Core>) -> impl PinInit<Self, Error> + '_ {
                 ),
                 counter <- Self::build_counter(&debugfs),
                 inner <- Self::build_inner(&debugfs),
+                array_blob <- debugfs.read_write_binary_file(
+                    c_str!("array_blob"),
+                    new_mutex!([0x62, 0x6c, 0x6f, 0x62]),
+                ),
+                vector_blob <- debugfs.read_write_binary_file(
+                    c_str!("vector_blob"),
+                    new_mutex!(kernel::kvec!(0x42; SZ_4K)?),
+                ),
                 _debugfs: debugfs,
                 pdev: pdev.into(),
             }
Signature · 1 line
2.51.0
reviewer Reviewed-by on v1 5/7
#26 lore raw

Re: [PATCH 5/7] samples: rust: debugfs: add example for blobs

Quoted 5 lines · Danilo Krummrich

Extend the Rust debugfs sample to demonstrate usage of binary file support. The example now shows how to expose both fixed-size arrays and dynamically sized vectors as binary blobs in debugfs.

author maintainer
#27 lore raw

PATCH 6/7 rust: debugfs: support binary large objects for ScopedDir

In reply to Danilo Krummrich

Add support for creating binary debugfs files via ScopedDir. This mirrors the existing functionality for Dir, but without producing an owning handle -- files are automatically removed when the associated Scope is dropped.

---
 rust/kernel/debugfs.rs | 45 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
rust/kernel/debugfs.rs +45 -0
index 3c3bbcc126ef..0eb1719e4953 100644
@@ -531,6 +531,20 @@ pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat
         self.create_file(name, data, &T::FILE_OPS)
     }
 
+    /// Creates a read-only binary file in this directory.
+    ///
+    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`].
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        self.create_file(name, data, &T::FILE_OPS)
+    }
+
     /// Creates a read-only file in this directory, with contents from a callback.
     ///
     /// The file contents are generated by calling `f` with `data`.
@@ -568,6 +582,22 @@ pub fn read_write_file<T: Writer + Reader + Send + Sync + 'static>(
         self.create_file(name, data, vtable)
     }
 
+    /// Creates a read-write binary file in this directory.
+    ///
+    /// Reading the file uses the [`BinaryWriter`] implementation on `data`. Writing to the file
+    /// uses the [`BinaryReader`] implementation on `data`.
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn read_write_binary_file<T: BinaryWriter + BinaryReader + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        let vtable = &<T as BinaryReadWriteFile<_>>::FILE_OPS;
+        self.create_file(name, data, vtable)
+    }
+
     /// Creates a read-write file in this directory, with logic from callbacks.
     ///
     /// Reading from the file is handled by `f`. Writing to the file is handled by `w`.
@@ -607,6 +637,21 @@ pub fn write_only_file<T: Reader + Send + Sync + 'static>(&self, name: &CStr, da
         self.create_file(name, data, vtable)
     }
 
+    /// Creates a write-only binary file in this directory.
+    ///
+    /// Writing to the file uses the [`BinaryReader`] implementation on `data`.
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn write_binary_file<T: BinaryReader + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        let vtable = &<T as BinaryWriteFile<_>>::FILE_OPS;
+        self.create_file(name, data, vtable)
+    }
+
     /// Creates a write-only file in this directory, with write logic from a callback.
     ///
     /// Writing to the file is handled by `w`.
Signature · 1 line
2.51.0
reviewer
#28 lore raw

Re: [PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir

Quoted 20 lines · Danilo Krummrich

Add support for creating binary debugfs files via ScopedDir. This mirrors the existing functionality for Dir, but without producing an owning handle -- files are automatically removed when the associated Scope is dropped.

---
 rust/kernel/debugfs.rs | 45 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
rust/kernel/debugfs.rs +2 -0
index 3c3bbcc126ef..0eb1719e4953 100644
@@ -531,6 +531,20 @@ pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat
         self.create_file(name, data, &T::FILE_OPS)
     }
 
+    /// Creates a read-only binary file in this directory.
+    ///

rust/kernel/debugfs.rs pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat · line 545

Quoted 10 lines
rust/kernel/debugfs.rs +10 -0
+    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`].
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        self.create_file(name, data, &T::FILE_OPS)

Why isn't <T as MyTrait> need here when it's needed for the other methods?

Alice

author maintainer
#29 lore raw

Re: [PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir

Quoted 19 lines · Alice Ryhl
Quoted 18 lines · Danilo Krummrich

Add support for creating binary debugfs files via ScopedDir. This mirrors the existing functionality for Dir, but without producing an owning handle -- files are automatically removed when the associated Scope is dropped.

---
 rust/kernel/debugfs.rs | 45 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
rust/kernel/debugfs.rs +0 -0
index 3c3bbcc126ef..0eb1719e4953 100644
@@ -531,6 +531,20 @@ pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat
         self.create_file(name, data, &T::FILE_OPS)
     }
 
Quoted 15 lines

rust/kernel/debugfs.rs pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat · line 545

Quoted 12 lines
rust/kernel/debugfs.rs +12 -0
+    /// Creates a read-only binary file in this directory.
+    ///
+    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`].
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        self.create_file(name, data, &T::FILE_OPS)

Why isn't <T as MyTrait> need here when it's needed for the other methods?

It's not needed for write_binary_file() I think, but read_write_binary_file() needs it because:

fn read_write_binary_file<T: BinaryWriter + BinaryReader + Send + Sync + 'static>()

So, just &T::FILE_OPS is ambiguous, because it implements BinaryReadFile, BinaryWriteFile and BinaryReadWriteFile.

author maintainer
#30 lore raw

PATCH 7/7 samples: rust: debugfs_scoped: add example for blobs

In reply to Danilo Krummrich

Extend the rust_debugfs_scoped sample to demonstrate how to export a large binary object through a ScopedDir.

---
 samples/rust/rust_debugfs_scoped.rs | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
samples/rust/rust_debugfs_scoped.rs +10 -4
index b0c4e76b123e..c80312cf168d 100644
@@ -9,6 +9,7 @@
 use core::sync::atomic::AtomicUsize;
 use kernel::debugfs::{Dir, Scope};
 use kernel::prelude::*;
+use kernel::sizes::*;
 use kernel::sync::Mutex;
 use kernel::{c_str, new_mutex, str::CString};
 
@@ -66,18 +67,22 @@ fn create_file_write(
             GFP_KERNEL,
         )?;
     }
+    let blob = KBox::pin_init(new_mutex!([0x42; SZ_4K]), GFP_KERNEL)?;
 
     let scope = KBox::pin_init(
-        mod_data
-            .device_dir
-            .scope(DeviceData { name, nums }, &file_name, |dev_data, dir| {
+        mod_data.device_dir.scope(
+            DeviceData { name, nums, blob },
+            &file_name,
+            |dev_data, dir| {
                 for (idx, val) in dev_data.nums.iter().enumerate() {
                     let Ok(name) = CString::try_from_fmt(fmt!("{idx}")) else {
                         return;
                     };
                     dir.read_write_file(&name, val);
                 }
-            }),
+                dir.read_write_binary_file(c_str!("blob"), &dev_data.blob);
+            },
+        ),
         GFP_KERNEL,
     )?;
     (*mod_data.devices.lock()).push(scope, GFP_KERNEL)?;
@@ -110,6 +115,7 @@ fn init(device_dir: Dir) -> impl PinInit<Self> {
 struct DeviceData {
     name: CString,
     nums: KVec<AtomicUsize>,
+    blob: Pin<KBox<Mutex<[u8; SZ_4K]>>>,
 }
 
 fn init_control(base_dir: &Dir, dyn_dirs: Dir) -> impl PinInit<Scope<ModuleData>> + '_ {
Signature · 1 line
2.51.0