Binary Large Objects for Rust DebugFS
Subsystems (3)
Danilo Krummrich
maintainerv3 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
Changes in v3:
- Add UserSliceReader::read_slice_file() and UserSliceWriter::write_slice_file() taking an &mut file::Offset to adjust it internally and make use of them.
- Add a new type file::Offset, rather than a type alias.
- Move affected delegate comments to previous patch.
- Add a brief comment for BinaryReadFile, BinaryWriteFile and BinaryReadWriteFile.
Changes in v2:
- Add file::Offset type alias.
- uaccess:
- Saturate at buffer length on offset overflow.
- Use file::Offset instead of usize.
- debugfs:
- Use file::Offset instead of usize.
- Handle potential overflow when updating ppos.
- Use &T::FILE_OPS directly if possible.
- Fix safety comment in BinaryReaderMut::read_from_slice_mut().
Danilo Krummrich (10):
rust: fs: add new type file::Offset
rust: uaccess: add UserSliceReader::read_slice_partial()
rust: uaccess: add UserSliceReader::read_slice_file()
rust: uaccess: add UserSliceWriter::write_slice_partial()
rust: uaccess: add UserSliceWriter::write_slice_file()
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 | 110 ++++++++++++- rust/kernel/debugfs/file_ops.rs | 146 ++++++++++++++++- rust/kernel/debugfs/traits.rs | 238 +++++++++++++++++++++++++++- rust/kernel/fs/file.rs | 142 ++++++++++++++++- rust/kernel/uaccess.rs | 81 ++++++++++ samples/rust/rust_debugfs.rs | 13 ++ samples/rust/rust_debugfs_scoped.rs | 14 +- 7 files changed, 732 insertions(+), 12 deletions(-)
base-commit: e6901808a3b28d8bdabfa98a618b2eab6f8798e8
Signature · 1 line
2.51.0
Danilo Krummrich
maintainerPATCH v3 1/10 rust: fs: add new type file::Offset
Add a new type for file offsets, i.e. bindings::loff_t. Trying to avoid using raw bindings types, this seems to be the better alternative compared to just using i64.
--- rust/kernel/fs/file.rs | 142 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-)
rust/kernel/fs/file.rs +141 -1
@@ -15,7 +15,147 @@ sync::aref::{ARef, AlwaysRefCounted}, types::{NotThreadSafe, Opaque}, }; -use core::ptr; +use core::{num::TryFromIntError , ptr}; + +/// Representation of an offset within a [`File`]. +/// +/// Transparent wrapper around `bindings::loff_t`. +#[repr(transparent)] +#[derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Default )] +pub struct Offset (bindings::loff_t ); + +impl Offset { +/// The largest value that can be represented by this type. +pub const MAX :Self =Self (bindings::loff_t::MAX ); + +/// The smallest value that can be represented by this type. +pub const MIN :Self =Self (bindings::loff_t::MIN ); + +/// Create a mutable [`Offset`] reference from the raw `*mut bindings::loff_t`. +/// +/// # Safety +/// +/// - `offset` must be a valid pointer to a `bindings::loff_t`. +/// - The caller must guarantee exclusive access to `offset`. +#[inline] +pub const unsafe fn from_raw <'a>(offset: *mut bindings::loff_t ) -> &'amut Self { +// SAFETY: By the safety requirements of this function +// - `offset` is a valid pointer to a `bindings::loff_t`, +// - we have exclusive access to `offset`. +unsafe { &mut *offset.cast () } + } + +/// Returns `true` if the [`Offset`] is negative. +/// +/// # Examples +/// +/// ``` +/// use kernel::fs::file::Offset; +/// +/// let offset = Offset::from(1); +/// assert!(!offset.is_negative()); +/// +/// let offset = Offset::from(-1); +/// assert!(offset.is_negative()); +/// ``` +#[inline] +pub const fn is_negative (self) ->bool { + self.0 .is_negative () + } + +/// Saturating addition with another [`Offset`]. +#[inline] +pub fn saturating_add (self, rhs:Offset ) ->Offset { +Self (self.0 .saturating_add (rhs.0 )) + } + +/// Saturating addition with a [`usize`]. +/// +/// If the [`usize`] fits in `bindings::loff_t` it is converted and added; otherwise the result +/// saturates to [`Offset::MAX`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::fs::file::Offset; +/// +/// let offset = Offset::from(40); +/// +/// let offset = offset.saturating_add_usize(2); +/// assert_eq!(offset, Offset::from(42)); +/// +/// let offset = Offset::MAX.saturating_sub_usize(1); +/// let offset = offset.saturating_add_usize(usize::MAX); +/// assert_eq!(offset, Offset::MAX); +/// ``` +pub fn saturating_add_usize (self, rhs:usize ) ->Offset { +match bindings::loff_t::try_from (rhs) { +Ok (rhs_loff) =>Self (self.0 .saturating_add (rhs_loff)), +Err (_) =>Self ::MAX , + } + } + +/// Saturating subtraction with another [`Offset`]. +#[inline] +pub fn saturating_sub (self, rhs:Offset ) ->Offset { +Offset (self.0 .saturating_sub (rhs.0 )) + } + +/// Saturating subtraction with a [`usize`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::fs::file::Offset; +/// +/// let offset = Offset::from(100); +/// let offset = offset.saturating_sub_usize(58); +/// assert_eq!(offset, Offset::from(42)); +/// +/// let offset = Offset::MIN.saturating_add_usize(1); +/// let offset = offset.saturating_sub_usize(usize::MAX); +/// assert_eq!(offset, Offset::MIN); +/// ``` +#[inline] +pub fn saturating_sub_usize (self, rhs:usize ) ->Offset { +match bindings::loff_t::try_from (rhs) { +Ok (rhs_loff) =>Offset (self.0 .saturating_sub (rhs_loff)), +Err (_) =>Self ::MIN , + } + } +} + +impl From <bindings::loff_t >for Offset { +#[inline] +fn from (v: bindings::loff_t ) ->Self { +Self (v) + } +} + +impl From <Offset >for bindings::loff_t { +#[inline] +fn from (offset:Offset ) ->Self { + offset.0 + } +} + +impl TryFrom <usize >for Offset { +type Error =TryFromIntError ; + +#[inline] +fn try_from (u:usize ) ->Result <Self ,Self ::Error > { +Ok (Self (bindings::loff_t::try_from (u)?)) + } +} + +impl TryFrom <Offset >for usize { +type Error =TryFromIntError ; + +#[inline] +fn try_from (offset:Offset ) ->Result <Self ,Self ::Error > { + usize::try_from (offset.0 ) + } +}/// Flags associated with a [`File`]. pub mod flags {
Signature · 1 line
2.51.0
Danilo Krummrich
maintainerPATCH v3 2/10 rust: uaccess: add UserSliceReader::read_slice_partial()
In reply to Danilo KrummrichThe 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 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
rust/kernel/uaccess.rs +16 -0
@@ -287,6 +287,22 @@ 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 ()) + .unwrap_or (out.len ()) + .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
Danilo Krummrich
maintainerPATCH v3 3/10 rust: uaccess: add UserSliceReader::read_slice_file()
In reply to Danilo KrummrichAdd UserSliceReader::read_slice_file(), which is the same as UserSliceReader::read_slice_partial() but updates the given file::Offset by the number of bytes read.
This is equivalent to C's simple_write_to_buffer() and useful when dealing with file offsets from file operations.
--- rust/kernel/uaccess.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
rust/kernel/uaccess.rs +25 -0
@@ -9,6 +9,7 @@ bindings, error::Result, ffi::{c_char, c_void}, + fs::file, prelude::*, transmute::{AsBytes, FromBytes}, };
@@ -303,6 +304,30 @@ pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<us .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len())) } +/// Reads raw data from the user slice into a kernel buffer partially. +/// +/// This is the same as [`Self::read_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes read. +/// +/// This is equivalent to C's `simple_write_to_buffer()`. +/// +/// On success, returns the number of bytes read. +pub fn read_slice_file (&mut self, out: &mut [u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else { +return Ok (0 ); + }; + +let read = self.read_slice_partial (out, offset_index)?; + + *offset = offset.saturating_add_usize (read); + +Ok (read) + } +/// 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
Danilo Krummrich
maintainerPATCH v3 4/10 rust: uaccess: add UserSliceWriter::write_slice_partial()
In reply to Danilo KrummrichThe 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
@@ -479,6 +479,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 ()) + .unwrap_or (data.len ()) + .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
Danilo Krummrich
maintainerPATCH v3 5/10 rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Danilo KrummrichAdd UserSliceWriter::write_slice_file(), which is the same as UserSliceWriter::write_slice_partial() but updates the given file::Offset by the number of bytes written.
This is equivalent to C's simple_read_from_buffer() and useful when dealing with file offsets from file operations.
--- rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
rust/kernel/uaccess.rs +24 -0
@@ -495,6 +495,30 @@ pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len())) } +/// Writes raw data to this user pointer from a kernel buffer partially. +/// +/// This is the same as [`Self::write_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes written. +/// +/// This is equivalent to C's `simple_read_from_buffer()`. +/// +/// On success, returns the number of bytes written. +pub fn write_slice_file (&mut self, data: &[u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else { +return Ok (0 ); + }; + +let written = self.write_slice_partial (data, offset_index)?; + + *offset = offset.saturating_add_usize (written); + +Ok (written) + } +/// 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
Danilo Krummrich
maintainerPATCH v3 6/10 rust: debugfs: support for binary large objects
In reply to Danilo KrummrichIntroduce 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 | 66 ++++++++++++++- rust/kernel/debugfs/file_ops.rs | 146 +++++++++++++++++++++++++++++++- rust/kernel/debugfs/traits.rs | 68 ++++++++++++++- 3 files changed, 273 insertions(+), 7 deletions(-)
rust/kernel/debugfs.rs +64 -2
@@ -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,32 @@ 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: &'aCStr , + data:impl PinInit <T ,E > + 'a, + ) ->impl PinInit <File <T >,E > + 'a +where +T :BinaryWriter +Send +Sync + 'static, + { + self.create_file (name, data, &T ::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 +235,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: &'aCStr , + 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 +293,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: &'aCStr , + 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 +142 -4
166 lines not on this page · raw message
rust/kernel/debugfs/traits.rs +67 -1
88 lines not on this page · raw message
Signature · 1 line
2.51.0
Danilo Krummrich
maintainerPATCH v3 7/10 rust: debugfs: support blobs from smart pointers
In reply to Danilo KrummrichExtend 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 | 174 +++++++++++++++++++++++++++++++++- 2 files changed, 173 insertions(+), 3 deletions(-)
rust/kernel/debugfs.rs +1 -1
@@ -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 +172 -2
210 lines not on this page · raw message
Signature · 1 line
2.51.0
Danilo Krummrich
maintainerPATCH v3 8/10 samples: rust: debugfs: add example for blobs
In reply to Danilo KrummrichExtend 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
@@ -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
Danilo Krummrich
maintainerPATCH v3 9/10 rust: debugfs: support binary large objects for ScopedDir
In reply to Danilo KrummrichAdd 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 | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
rust/kernel/debugfs.rs +44 -0
@@ -530,6 +530,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: &'dataT , + ) { + 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`.
@@ -567,6 +581,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: &'dataT , + ) { +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`.
@@ -606,6 +636,20 @@ 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: &'dataT , + ) { + self.create_file (name, data, &T ::FILE_OPS ) + } +/// 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
Danilo Krummrich
maintainerPATCH v3 10/10 samples: rust: debugfs_scoped: add example for blobs
In reply to Danilo KrummrichExtend 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
@@ -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
Re: [PATCH v3 01/10] rust: fs: add new type file::Offset
In reply to Danilo KrummrichQuoted 9 lines · Danilo Krummrich
Add a new type for file offsets, i.e.
bindings::loff_t. Trying to avoid using raw bindings types, this seems to be the better alternative compared to just using i64.Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Thanks a lot for this!
Cheers,
Miguel
Re: [PATCH v3 09/10] rust: debugfs: support binary large objects for ScopedDir
In reply to Danilo KrummrichQuoted 8 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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Re: [PATCH v3 07/10] rust: debugfs: support blobs from smart pointers
In reply to Danilo KrummrichQuoted 9 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>.
Quoted 7 lines
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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Alice Ryhl
reviewerRe: [PATCH v3 06/10] rust: debugfs: support for binary large objects
In reply to Danilo KrummrichQuoted 29 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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>rust/kernel/debugfs/file_ops.rs +12
-0+extern "C" fn blob_read <T :BinaryWriter >( + file: *mut bindings::file , + buf: *mut 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:
rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 271
Quoted 10 lines
rust/kernel/debugfs/file_ops.rs +10
-0+// `ppos` is a valid `file::Offset` pointer. +// We have exclusive access to `ppos`. +let pos =unsafe { file::Offset ::from_raw (ppos) }; + +let mut writer =UserSlice ::new (UserPtr ::from_ptr (buf.cast ()), count).writer (); + +let ret = || ->Result <isize > { +let written = this.write_to_slice (&mut writer, pos)?; + +Ok (written.try_into ()?)
Hmm ... a conversion? Sounds like write_to_slice() has the wrong return type.
Alice
Alice Ryhl
reviewerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Danilo KrummrichQuoted 30 lines · Danilo Krummrich
Add
UserSliceWriter::write_slice_file(), which is the same asUserSliceWriter::write_slice_partial()but updates the givenfile::Offsetby the number of bytes written.This is equivalent to C's
simple_read_from_buffer()and useful when dealing with file offsets from file operations.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)rust/kernel/uaccess.rs +10
-0@@ -495,6 +495,30 @@ pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len())) } +/// Writes raw data to this user pointer from a kernel buffer partially. +/// +/// This is the same as [`Self::write_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes written. +/// +/// This is equivalent to C's `simple_read_from_buffer()`. +/// +/// On success, returns the number of bytes written. +pub fn write_slice_file (&mut self, data: &[u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () {
rust/kernel/uaccess.rs pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz · line 517
Quoted 10 lines
rust/kernel/uaccess.rs +10
-0+return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else { +return Ok (0 ); + }; + +let written = self.write_slice_partial (data, offset_index)?; + + *offset = offset.saturating_add_usize (written);
This addition should never overflow:
offset + written <= data.len() <= isize::MAX <= Offset::MAX
I can't help but think that maybe this should be a + operation instead?
Alice
Re: [PATCH v3 04/10] rust: uaccess: add UserSliceWriter::write_slice_partial()
In reply to Danilo KrummrichQuoted 6 lines · Danilo Krummrich
The existing
write_slice()method is a wrapper aroundcopy_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.
Quoted 6 lines
In order to support this common case, provide a helper for partial writes.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
This code is ok
but:
Quoted 4 lines
rust/kernel/uaccess.rs +4
-0+/// 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`.
rust/kernel/uaccess.rs pub fn write_slice(&mut self, data: &[u8]) -> Result { · line 495
Quoted 10 lines
rust/kernel/uaccess.rs +10
-0+/// +/// 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 ()) + .unwrap_or (data.len ()) + .min (data.len ()); + + data.get (offset..end) + .map_or (Ok (0 ), |src| self.write_slice (src).map (|()| src.len ()))
Isn't it more readable to write it like this?
let Some(src) = data.get(offset..end) else {
return Ok(0);
};
self.write_slice(src)?;
Ok(src.len())Alice
Danilo Krummrich
maintainerRe: [PATCH v3 06/10] rust: debugfs: support for binary large objects
In reply to Alice RyhlQuoted 35 lines · Alice Ryhl
Quoted 34 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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>rust/kernel/debugfs/file_ops.rs +17
-0+extern "C" fn blob_read <T :BinaryWriter >( + file: *mut bindings::file , + buf: *mut 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 `file::Offset` pointer. +// We have exclusive access to `ppos`. +let pos =unsafe { file::Offset ::from_raw (ppos) }; + +let mut writer =UserSlice ::new (UserPtr ::from_ptr (buf.cast ()), count).writer ();
Quoted 8 lines
rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 271
Quoted 5 lines
rust/kernel/debugfs/file_ops.rs +5
-0+ +let ret = || ->Result <isize > { +let written = this.write_to_slice (&mut writer, pos)?; + +Ok (written.try_into ()?)Hmm ... a conversion? Sounds like
write_to_slice()has the wrong return type.
write_to_slice() returns the number of bytes written as usize, which seems correct, no?
Alice Ryhl
reviewerRe: [PATCH v3 06/10] rust: debugfs: support for binary large objects
Quoted 46 lines · Danilo Krummrich
Quoted 35 lines · Alice Ryhl
Quoted 34 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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>rust/kernel/debugfs/file_ops.rs +17
-0+extern "C" fn blob_read <T :BinaryWriter >( + file: *mut bindings::file , + buf: *mut 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 `file::Offset` pointer. +// We have exclusive access to `ppos`. +let pos =unsafe { file::Offset ::from_raw (ppos) }; + +let mut writer =UserSlice ::new (UserPtr ::from_ptr (buf.cast ()), count).writer ();Quoted 8 lines
rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 271
Quoted 5 lines
rust/kernel/debugfs/file_ops.rs +5
-0+ +let ret = || ->Result <isize > { +let written = this.write_to_slice (&mut writer, pos)?; + +Ok (written.try_into ()?)Hmm ... a conversion? Sounds like
write_to_slice()has the wrong return type.
Quoted 2 lines
write_to_slice()returns the number of bytes written as usize, which seems correct, no?
Yes, you're right, I think usize is the right value. The cast is unfortunate, but it can't really be avoided. In practice it should never fail because slice lengths always fit in an isize, but isize isn't the right type.
Alice
Danilo Krummrich
maintainerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Alice RyhlQuoted 35 lines · Alice Ryhl
Quoted 34 lines · Danilo Krummrich
Add
UserSliceWriter::write_slice_file(), which is the same asUserSliceWriter::write_slice_partial()but updates the givenfile::Offsetby the number of bytes written.This is equivalent to C's
simple_read_from_buffer()and useful when dealing with file offsets from file operations.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)rust/kernel/uaccess.rs +14
-0@@ -495,6 +495,30 @@ pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len())) } +/// Writes raw data to this user pointer from a kernel buffer partially. +/// +/// This is the same as [`Self::write_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes written. +/// +/// This is equivalent to C's `simple_read_from_buffer()`. +/// +/// On success, returns the number of bytes written. +pub fn write_slice_file (&mut self, data: &[u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else {
Quoted 8 lines
rust/kernel/uaccess.rs pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz · line 517
Quoted 6 lines
rust/kernel/uaccess.rs +6
-0+return Ok (0 ); + }; + +let written = self.write_slice_partial (data, offset_index)?; + + *offset = offset.saturating_add_usize (written);This addition should never overflow:
It probably never will (which is why this was a + operation in v1).
Quoted 1 line
offset + written <=
data.len()<=isize::MAX<=Offset::MAX
However, this would rely on implementation details you listed, i.e. the invariant that a slice length should be at most isize::MAX and what's the maximum size of file::Offset::MAX.
Even though I don't expect any of the above to change any time soon saturating_add_usize() seems reasonable to me.
Quoted 1 line
I can't help but think that maybe this should be a + operation instead?
Alice Ryhl
reviewerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
Quoted 46 lines · Danilo Krummrich
Quoted 35 lines · Alice Ryhl
Quoted 34 lines · Danilo Krummrich
Add
UserSliceWriter::write_slice_file(), which is the same asUserSliceWriter::write_slice_partial()but updates the givenfile::Offsetby the number of bytes written.This is equivalent to C's
simple_read_from_buffer()and useful when dealing with file offsets from file operations.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)rust/kernel/uaccess.rs +14
-0@@ -495,6 +495,30 @@ pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len())) } +/// Writes raw data to this user pointer from a kernel buffer partially. +/// +/// This is the same as [`Self::write_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes written. +/// +/// This is equivalent to C's `simple_read_from_buffer()`. +/// +/// On success, returns the number of bytes written. +pub fn write_slice_file (&mut self, data: &[u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else {Quoted 8 lines
rust/kernel/uaccess.rs pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz · line 517
Quoted 6 lines
rust/kernel/uaccess.rs +6
-0+return Ok (0 ); + }; + +let written = self.write_slice_partial (data, offset_index)?; + + *offset = offset.saturating_add_usize (written);This addition should never overflow:
Quoted 7 lines
It probably never will (which is why this was a + operation in v1).
Quoted 1 line
offset + written <=
data.len()<=isize::MAX<=Offset::MAXHowever, this would rely on implementation details you listed, i.e. the invariant that a slice length should be at most
isize::MAXand what's the maximum size offile::Offset::MAX.
It's not an implementation detail. All Rust allocations are guaranteed to fit in isize::MAX bytes:
https://doc.rust-lang.org/stable/std/ptr/index.html#allocation
Alice
Danilo Krummrich
maintainerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
Quoted 51 lines · Alice Ryhl
Quoted 50 lines · Danilo Krummrich
Quoted 35 lines · Alice Ryhl
Quoted 34 lines · Danilo Krummrich
Add
UserSliceWriter::write_slice_file(), which is the same asUserSliceWriter::write_slice_partial()but updates the givenfile::Offsetby the number of bytes written.This is equivalent to C's
simple_read_from_buffer()and useful when dealing with file offsets from file operations.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)rust/kernel/uaccess.rs +14
-0@@ -495,6 +495,30 @@ pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len())) } +/// Writes raw data to this user pointer from a kernel buffer partially. +/// +/// This is the same as [`Self::write_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes written. +/// +/// This is equivalent to C's `simple_read_from_buffer()`. +/// +/// On success, returns the number of bytes written. +pub fn write_slice_file (&mut self, data: &[u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else {Quoted 8 lines
rust/kernel/uaccess.rs pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz · line 517
Quoted 6 lines
rust/kernel/uaccess.rs +6
-0+return Ok (0 ); + }; + +let written = self.write_slice_partial (data, offset_index)?; + + *offset = offset.saturating_add_usize (written);This addition should never overflow:
It probably never will (which is why this was a + operation in v1).
Quoted 1 line
offset + written <=
data.len()<=isize::MAX<=Offset::MAX
Quoted 7 lines
Quoted 3 lines
However, this would rely on implementation details you listed, i.e. the invariant that a slice length should be at most
isize::MAXand what's the maximum size offile::Offset::MAX.It's not an implementation detail. All Rust allocations are guaranteed to fit in
isize::MAXbytes:
https://doc.rust-lang.org/stable/std/ptr/index.html#allocation
Yeah, I'm aware -- I expressed this badly.
What I meant is that for the kernel we obviously know that there's no architecture where isize::MAX > file::Offset::MAX.
However, in the core API the conversion from usize to u128 is considered fallible. So, applying the assumption that isize::MAX <= file::Offset::MAX is at least inconsistent.
Alice Ryhl
reviewerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
Quoted 60 lines · Danilo Krummrich
Quoted 51 lines · Alice Ryhl
Quoted 50 lines · Danilo Krummrich
Quoted 35 lines · Alice Ryhl
Quoted 34 lines · Danilo Krummrich
Add
UserSliceWriter::write_slice_file(), which is the same asUserSliceWriter::write_slice_partial()but updates the givenfile::Offsetby the number of bytes written.This is equivalent to C's
simple_read_from_buffer()and useful when dealing with file offsets from file operations.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)rust/kernel/uaccess.rs +14
-0@@ -495,6 +495,30 @@ pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz .map_or(Ok(0), |src| self.write_slice(src).map(|()| src.len())) } +/// Writes raw data to this user pointer from a kernel buffer partially. +/// +/// This is the same as [`Self::write_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes written. +/// +/// This is equivalent to C's `simple_read_from_buffer()`. +/// +/// On success, returns the number of bytes written. +pub fn write_slice_file (&mut self, data: &[u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + } + +let Ok (offset_index) = (*offset).try_into ()else {Quoted 8 lines
rust/kernel/uaccess.rs pub fn write_slice_partial(&mut self, data: &[u8], offset: usize) -> Result<usiz · line 517
Quoted 6 lines
rust/kernel/uaccess.rs +6
-0+return Ok (0 ); + }; + +let written = self.write_slice_partial (data, offset_index)?; + + *offset = offset.saturating_add_usize (written);This addition should never overflow:
It probably never will (which is why this was a + operation in v1).
Quoted 1 line
offset + written <=
data.len()<=isize::MAX<=Offset::MAXQuoted 7 lines
Quoted 3 lines
However, this would rely on implementation details you listed, i.e. the invariant that a slice length should be at most
isize::MAXand what's the maximum size offile::Offset::MAX.It's not an implementation detail. All Rust allocations are guaranteed to fit in
isize::MAXbytes:
https://doc.rust-lang.org/stable/std/ptr/index.html#allocation
Quoted 8 lines
Yeah, I'm aware -- I expressed this badly.
What I meant is that for the kernel we obviously know that there's no architecture where
isize::MAX>file::Offset::MAX.However, in the core API the conversion from usize to u128 is considered fallible. So, applying the assumption that
isize::MAX<=file::Offset::MAXis at least inconsistent.
I would love to have infallible conversions from usize to u64 (and u32 to usize), but we can't really modify the stdlib to add them.
But even if we had them, it wouldn't help here since the target type is i64, not u64. And there are usize values that don't fit in i64 - it's just that in this case the usize fits in isize.
Alice
Danilo Krummrich
maintainerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
Quoted 2 lines · Alice Ryhl
I would love to have infallible conversions from usize to u64 (and u32 to usize), but we can't really modify the stdlib to add them.
We can (and probably should) implement a kernel specific infallible one.
I think we also want a helper for slice::len() as isize.
Quoted 3 lines
But even if we had them, it wouldn't help here since the target type is i64, not u64. And there are usize values that don't fit in i64 - it's just that in this case the usize fits in isize.
Sure, it doesn't change the code required for this case. Yet, I think that if we agree on having a kernel specific infallible conversions for usize -> u64 and isize -> i64, it makes this + operation formally more consistent.
Here's the diff I'd apply:
rust/kernel/fs/file.rs +16 -0
@@ -125,6 +125,22 @@ pub fn saturating_sub_usize(self, rhs: usize) -> Offset { } } +impl core::ops::Add <isize >for Offset { +type Output =Offset ; + +#[inline] +fn add (self, rhs:isize ) ->Offset { +Offset (self.0 + rhsas bindings::loff_t ) + } +} + +impl core::ops::AddAssign <isize >for Offset { +#[inline] +fn add_assign (&mut self, rhs:isize ) { + self.0 += rhsas bindings::loff_t ; + } +} +impl From <bindings::loff_t >for Offset {#[inline] fn from (v: bindings::loff_t ) ->Self {
rust/kernel/uaccess.rs +2 -1
@@ -514,7 +514,8 @@ pub fn write_slice_file(&mut self, data: &[u8], offset: &mut file::Offset) -> Relet written = self.write_slice_partial (data, offset_index)?; - *offset = offset.saturating_add_usize (written); +// OVERFLOW: `offset + written <= data.len() <= isize::MAX <= Offset::MAX` + *offset += writtenas isize ;Ok (written) }
Re: [PATCH v3 06/10] rust: debugfs: support for binary large objects
In reply to Alice RyhlQuoted 50 lines · Alice Ryhl
Quoted 46 lines · Danilo Krummrich
Quoted 35 lines · Alice Ryhl
Quoted 34 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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>rust/kernel/debugfs/file_ops.rs +17
-0+extern "C" fn blob_read <T :BinaryWriter >( + file: *mut bindings::file , + buf: *mut 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 `file::Offset` pointer. +// We have exclusive access to `ppos`. +let pos =unsafe { file::Offset ::from_raw (ppos) }; + +let mut writer =UserSlice ::new (UserPtr ::from_ptr (buf.cast ()), count).writer ();Quoted 8 lines
rust/kernel/debugfs/file_ops.rs impl<T: Reader + Sync> WriteFile<T> for T { · line 271
Quoted 5 lines
rust/kernel/debugfs/file_ops.rs +5
-0+ +let ret = || ->Result <isize > { +let written = this.write_to_slice (&mut writer, pos)?; + +Ok (written.try_into ()?)Hmm ... a conversion? Sounds like
write_to_slice()has the wrong return type.Quoted 2 lines
write_to_slice()returns the number of bytes written as usize, which seems correct, no?
Quoted 6 lines
Yes, you're right, I think usize is the right value. The cast is unfortunate, but it can't really be avoided. In practice it should never fail because slice lengths always fit in an isize, but isize isn't the right type.
Alice
Re: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Danilo KrummrichQuoted 51 lines · Danilo Krummrich
Quoted 2 lines · Alice Ryhl
I would love to have infallible conversions from usize to u64 (and u32 to usize), but we can't really modify the stdlib to add them.
We can (and probably should) implement a kernel specific infallible one.
I think we also want a helper for
slice::len() as isize.Quoted 3 lines
But even if we had them, it wouldn't help here since the target type is i64, not u64. And there are usize values that don't fit in i64 - it's just that in this case the usize fits in isize.
Sure, it doesn't change the code required for this case. Yet, I think that if we agree on having a kernel specific infallible conversions for usize -> u64 and isize -> i64, it makes this + operation formally more consistent.
Here's the diff I'd apply:
rust/kernel/fs/file.rs +16
-0@@ -125,6 +125,22 @@ pub fn saturating_sub_usize(self, rhs: usize) -> Offset { } } +impl core::ops::Add <isize >for Offset { +type Output =Offset ; + +#[inline] +fn add (self, rhs:isize ) ->Offset { +Offset (self.0 + rhsas bindings::loff_t ) + } +} + +impl core::ops::AddAssign <isize >for Offset { +#[inline] +fn add_assign (&mut self, rhs:isize ) { + self.0 += rhsas bindings::loff_t ; + } +} +impl From <bindings::loff_t >for Offset {#[inline] fn from (v: bindings::loff_t ) ->Self {rust/kernel/uaccess.rs +0
-0
rust/kernel/uaccess.rs pub fn write_slice_file(&mut self, data: &[u8], offset: &mut file::Offset) -> Re · line 521
Quoted 9 lines
rust/kernel/uaccess.rs +2
-1@@ -514,7 +514,8 @@ pub fn write_slice_file(&mut self, data: &[u8], offset: &mut file::Offset) -> Relet written = self.write_slice_partial (data, offset_index)?; - *offset = offset.saturating_add_usize (written); +// OVERFLOW: `offset + written <= data.len() <= isize::MAX <= Offset::MAX` + *offset += writtenas isize ;Ok (written) }
This LGTM.
Re: [PATCH v3 02/10] rust: uaccess: add UserSliceReader::read_slice_partial()
In reply to Danilo KrummrichQuoted 29 lines · Danilo Krummrich
The existing
read_slice()method is a wrapper aroundcopy_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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)rust/kernel/uaccess.rs +4
-0@@ -287,6 +287,22 @@ 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`.
rust/kernel/uaccess.rs pub fn read_slice(&mut self, out: &mut [u8]) -> Result { · line 303
Quoted 10 lines
rust/kernel/uaccess.rs +10
-0+/// +/// 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 ()) + .unwrap_or (out.len ()) + .min (out.len ()); + + out.get_mut (offset..end) + .map_or (Ok (0 ), |dst| self.read_slice (dst).map (|()| dst.len ()))
Same comment applies as write_slice_partial().
Re: [PATCH v3 01/10] rust: fs: add new type file::Offset
In reply to Danilo KrummrichQuoted 19 lines · Danilo Krummrich
Add a new type for file offsets, i.e.
bindings::loff_t. Trying to avoid using raw bindings types, this seems to be the better alternative compared to just using i64.Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/fs/file.rs | 142 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-)rust/kernel/fs/file.rs +0
-0@@ -15,7 +15,147 @@ sync::aref::{ARef, AlwaysRefCounted}, types::{NotThreadSafe, Opaque},
rust/kernel/fs/file.rs line 25
Quoted 10 lines
rust/kernel/fs/file.rs +8
-1}; -use core::ptr; +use core::{num::TryFromIntError , ptr}; + +/// Representation of an offset within a [`File`]. +/// +/// Transparent wrapper around `bindings::loff_t`. +#[repr(transparent)] +#[derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Default )] +pub struct Offset (bindings::loff_t );
There is no invariant on this type, so the field can be public.
pub struct Offset(pub bindings::loff_t);
Otherwise LGTM:
Alice
Re: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Alice RyhlQuoted 3 lines · Alice Ryhl
I would love to have infallible conversions from usize to u64 (and u32 to usize), but we can't really modify the stdlib to add them.
I don't have a link at hand now, but we already (recently too, I think) discussed having these "we know it is infallible in the kernel" conversions. Maybe there was a patch posted even.
Otherwise, I will create a good first issue.
Cheers,
Miguel
Danilo Krummrich
maintainerRe: [PATCH v3 01/10] rust: fs: add new type file::Offset
In reply to Danilo KrummrichQuoted 8 lines · Danilo Krummrich
Add a new type for file offsets, i.e.
bindings::loff_t. Trying to avoid using raw bindings types, this seems to be the better alternative compared to just using i64.Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Christian, Al: If you agree with the patch, can you please provide an ACK, so I can take it through the driver-core tree?
Thanks,
Danilo
Quoted 5 lines · Danilo Krummrich
rust: uaccess: add
UserSliceReader::read_slice_partial()
rust: uaccess: addUserSliceReader::read_slice_file()
rust: uaccess: addUserSliceWriter::write_slice_partial()
rust: uaccess: addUserSliceWriter::write_slice_file()
Cheers,
Miguel
Re: [PATCH v3 04/10] rust: uaccess: add UserSliceWriter::write_slice_partial()
In reply to Alice RyhlQuoted 9 lines · Alice Ryhl
Isn't it more readable to write it like this?
let Some(src) = data.get(offset..end) else { return Ok(0); }; self.write_slice(src)?; Ok(src.len())
Yeah, I also tend to prefer that style for things like this, because the "main" operation (like forwarding to write_slice()) is at the top-level. rather than deep in a closure. Plus early returns look closer to C.
Cheers,
Miguel
Re: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Danilo KrummrichQuoted 2 lines · Danilo Krummrich
+ /// Writes raw data to this user pointer from a kernel buffer partially.
We use "user pointer" in a few places (including another of the added ones), while "user slice" in others -- shouldn't we just use "user slice"?
By the way, for new APIs with several arguments and potential paths due to the offset, it wouldn't hurt to have an example (even if build-only) with a couple asserts to show how the API behaves. But I can create a good first issue for that if you prefer.
Similarly (another potential good first issue), should we use e.g.
#[doc(alias = "simple_read_from_buffer")]`
?
Thanks!
Cheers,
Miguel
Re: [PATCH v3 01/10] rust: fs: add new type file::Offset
In reply to Danilo KrummrichQuoted 58 lines · Danilo Krummrich
Add a new type for file offsets, i.e.
bindings::loff_t. Trying to avoid using raw bindings types, this seems to be the better alternative compared to just using i64.Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Cc Jan Kara <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/fs/file.rs | 142 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-)rust/kernel/fs/file.rs +37
-1@@ -15,7 +15,147 @@ sync::aref::{ARef, AlwaysRefCounted}, types::{NotThreadSafe, Opaque}, }; -use core::ptr; +use core::{num::TryFromIntError , ptr}; + +/// Representation of an offset within a [`File`]. +/// +/// Transparent wrapper around `bindings::loff_t`. +#[repr(transparent)] +#[derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Default )] +pub struct Offset (bindings::loff_t ); + +impl Offset { +/// The largest value that can be represented by this type. +pub const MAX :Self =Self (bindings::loff_t::MAX ); + +/// The smallest value that can be represented by this type. +pub const MIN :Self =Self (bindings::loff_t::MIN ); + +/// Create a mutable [`Offset`] reference from the raw `*mut bindings::loff_t`. +/// +/// # Safety +/// +/// - `offset` must be a valid pointer to a `bindings::loff_t`. +/// - The caller must guarantee exclusive access to `offset`. +#[inline] +pub const unsafe fn from_raw <'a>(offset: *mut bindings::loff_t ) -> &'amut Self { +// SAFETY: By the safety requirements of this function +// - `offset` is a valid pointer to a `bindings::loff_t`, +// - we have exclusive access to `offset`. +unsafe { &mut *offset.cast () } + } + +/// Returns `true` if the [`Offset`] is negative. +/// +/// # Examples +/// +/// ``` +/// use kernel::fs::file::Offset; +///
rust/kernel/fs/file.rs line 64
Quoted 10 lines
rust/kernel/fs/file.rs +10
-0+/// let offset = Offset::from(1); +/// assert!(!offset.is_negative()); +/// +/// let offset = Offset::from(-1); +/// assert!(offset.is_negative()); +/// ``` +#[inline] +pub const fn is_negative (self) ->bool { + self.0 .is_negative () + }
For symmetry with Rust's primitive types from which this method is borrowed, do we want to also add is_positive?
Re: [PATCH v3 02/10] rust: uaccess: add UserSliceReader::read_slice_partial()
In reply to Danilo KrummrichQuoted 26 lines · Danilo Krummrich
The existing
read_slice()method is a wrapper aroundcopy_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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)rust/kernel/uaccess.rs +1
-0@@ -287,6 +287,22 @@ 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.
rust/kernel/uaccess.rs pub fn read_slice(&mut self, out: &mut [u8]) -> Result { · line 300
Quoted 10 lines
rust/kernel/uaccess.rs +10
-0+/// +/// 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 ()) + .unwrap_or (out.len ()) + .min (out.len ());
IIUC you should be able to replace the checked_add().unwrap_or() with saturating_add() and end up with the same result.
Re: [PATCH v3 03/10] rust: uaccess: add UserSliceReader::read_slice_file()
In reply to Danilo KrummrichQuoted 40 lines · Danilo Krummrich
Add
UserSliceReader::read_slice_file(), which is the same asUserSliceReader::read_slice_partial()but updates the givenfile::Offsetby the number of bytes read.This is equivalent to C's
simple_write_to_buffer()and useful when dealing with file offsets from file operations.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)rust/kernel/uaccess.rs +13
-0@@ -9,6 +9,7 @@ bindings, error::Result, ffi::{c_char, c_void}, + fs::file, prelude::*, transmute::{AsBytes, FromBytes}, };@@ -303,6 +304,30 @@ pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<us .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len())) } +/// Reads raw data from the user slice into a kernel buffer partially. +/// +/// This is the same as [`Self::read_slice_partial`] but updates the given [`file::Offset`] by +/// the number of bytes read. +/// +/// This is equivalent to C's `simple_write_to_buffer()`. +/// +/// On success, returns the number of bytes read. +pub fn read_slice_file (&mut self, out: &mut [u8 ], offset: &mut file::Offset ) ->Result <usize > { +if offset.is_negative () { +return Err (EINVAL ); + }
rust/kernel/uaccess.rs pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<us · line 328
Quoted 10 lines
rust/kernel/uaccess.rs +10
-0+ +let Ok (offset_index) = (*offset).try_into ()else { +return Ok (0 ); + }; + +let read = self.read_slice_partial (out, offset_index)?; + + *offset = offset.saturating_add_usize (read); + +Ok (read)
Not sure whether this would be better, but you can avoid the read local variable with the following:
self.read_slice_partial(out, offset_index)
.inspect(|&read| *offset = offset.saturating_add_usize(read))
In any case,
Re: [PATCH v3 04/10] rust: uaccess: add UserSliceWriter::write_slice_partial()
In reply to Danilo KrummrichQuoted 25 lines · Danilo Krummrich
The existing
write_slice()method is a wrapper aroundcopy_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.
Reviewed-by Greg Kroah-Hartman <[email protected]>Reviewed-by Matthew Maurer <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/uaccess.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)rust/kernel/uaccess.rs +1
-0@@ -479,6 +479,22 @@ pub fn write_slice(&mut self, data: &[u8]) -> Result {Ok (()) } +/// Writes raw data to this user pointer from a kernel buffer partially.
rust/kernel/uaccess.rs pub fn write_slice(&mut self, data: &[u8]) -> Result { · line 492
Quoted 10 lines
rust/kernel/uaccess.rs +10
-0+/// +/// 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 ()) + .unwrap_or (data.len ()) + .min (data.len ());
Same suggestion as the read counterpart about saturating_add.
Alexandre Courbot
reviewerRe: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
In reply to Miguel OjedaQuoted 10 lines · Miguel Ojeda
Quoted 3 lines · Alice Ryhl
I would love to have infallible conversions from usize to u64 (and u32 to usize), but we can't really modify the stdlib to add them.
I don't have a link at hand now, but we already (recently too, I think) discussed having these "we know it is infallible in the kernel" conversions. Maybe there was a patch posted even.
Otherwise, I will create a good first issue.
Are you referring to this discussion?
https://lore.kernel.org/rust-for-linux/[email protected]/
If so, I have posted something along those lines:
https://lore.kernel.org/rust-for-linux/[email protected]/
We planned to have it stew in Nova for a bit, but I don't mind moving this to the core code if you think that looks good enough.
Re: [PATCH v3 05/10] rust: uaccess: add UserSliceWriter::write_slice_file()
Quoted 4 lines · Alexandre Courbot
Are you referring to this discussion?
I saw that one and the patches -- perhaps it was in meetings, but dealing with guarantees that are only true in the kernel (assumptions, conversions) has come up before several times over the years.
Cheers,
Miguel
Danilo Krummrich
maintainerOn Wed Oct 22, 2025 at 4:30 PM CEST, Danilo Krummrich wrote:
Applied to driver-core-testing, thanks!
Quoted 2 lines
Danilo Krummrich (10):
rust: fs: add new typefile::Offset
I picked up the version from v2 [1] for now and send a follow-up series for the new type approach [2].
Quoted 1 line
rust: uaccess: add
UserSliceReader::read_slice_partial()
[ Replace map_or() with let-else; use saturating_add(). - Danilo ]
Quoted 1 line
rust: uaccess: add
UserSliceReader::read_slice_file()
[ Replace saturating_add() with the raw operator and a corresponding
OVERFLOW comment. - Danilo ]
Quoted 1 line
rust: uaccess: add
UserSliceWriter::write_slice_partial()
[ Replace map_or() with let-else; use saturating_add(). - Danilo ]
Quoted 1 line
rust: uaccess: add
UserSliceWriter::write_slice_file()
[ Replace saturating_add() with the raw operator and a corresponding
OVERFLOW comment. - Danilo ]
Quoted 5 lines
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
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/