Re: [PATCH v2 3/4] rust: miscdevice: Provide additional abstractions for iov_iter and kiocb structures

From: Alice Ryhl
Date: Wed Jul 09 2025 - 07:17:33 EST


On Tue, Jul 08, 2025 at 04:53:23PM +0200, Andreas Hindborg wrote:
> "Alice Ryhl" <aliceryhl@xxxxxxxxxx> writes:
>
> > These will be used for the read_iter() and write_iter() callbacks, which
> > are now the preferred back-ends for when a user operates on a char device
> > with read() and write() respectively.
> >
> > Co-developed-by: Lee Jones <lee@xxxxxxxxxx>
> > Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
> > Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > ---
> > rust/kernel/miscdevice.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++-
> > 1 file changed, 96 insertions(+), 1 deletion(-)
> >
> > diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
> > index 22f291211636f66efca6b33b675833236332719e..a49954c9b0d14117645be8139db792f1fd22589d 100644
> > --- a/rust/kernel/miscdevice.rs
> > +++ b/rust/kernel/miscdevice.rs
> > @@ -14,13 +14,14 @@
> > error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},
> > ffi::{c_int, c_long, c_uint, c_ulong},
> > fs::File,
> > + iov::{IovIterDest, IovIterSource},
> > mm::virt::VmaNew,
> > prelude::*,
> > seq_file::SeqFile,
> > str::CStr,
> > types::{ForeignOwnable, Opaque},
> > };
> > -use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin};
> > +use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin, ptr::NonNull};
> >
> > /// Options for creating a misc device.
> > #[derive(Copy, Clone)]
> > @@ -136,6 +137,16 @@ fn mmap(
> > build_error!(VTABLE_DEFAULT_ERROR)
> > }
> >
> > + /// Read from this miscdevice.
> > + fn read_iter(_kiocb: Kiocb<'_, Self::Ptr>, _iov: &mut IovIterDest<'_>) -> Result<usize> {
> > + build_error!(VTABLE_DEFAULT_ERROR)
> > + }
> > +
> > + /// Write to this miscdevice.
> > + fn write_iter(_kiocb: Kiocb<'_, Self::Ptr>, _iov: &mut IovIterSource<'_>) -> Result<usize> {
> > + build_error!(VTABLE_DEFAULT_ERROR)
> > + }
> > +
> > /// Handler for ioctls.
> > ///
> > /// The `cmd` argument is usually manipulated using the utilities in [`kernel::ioctl`].
> > @@ -177,6 +188,36 @@ fn show_fdinfo(
> > }
> > }
> >
> > +/// Wrapper for the kernel's `struct kiocb`.
> > +///
> > +/// The type `T` represents the private data of the file.
> > +pub struct Kiocb<'a, T> {
> > + inner: NonNull<bindings::kiocb>,
> > + _phantom: PhantomData<&'a T>,
> > +}
>
> Also, `kiocb` is not miscdevice specific. It should probably not live here.

I can place it in rust/kernel/fs.rs, but this is an instance of the more
general fact that miscdevice defines many things from `file_operations`
that we should probably generalize in the future.

Alice