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

Andreas Hindborg
2025-03-19T19:27:01Z

"Alice Ryhl" <aliceryhl@google.com> 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@kernel.org>
> Signed-off-by: Lee Jones <lee@kernel.org>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> 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 fa9ecc42602a477328a25b5d357db90b59dc72ae..8daafdc7f3e47aef3c90507082d35ad6819598eb 100644
> --- a/rust/kernel/miscdevice.rs
> +++ b/rust/kernel/miscdevice.rs
> @@ -14,12 +14,13 @@
> error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},
> ffi::{c_int, c_long, c_uint, c_ulong},
> fs::File,
> + iov::{IovIterDest, IovIterSource},
> 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)]
> @@ -119,6 +120,16 @@ fn release(device: Self::Ptr, _file: &File) {
> drop(device);
> }
>
> + /// 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 utilties in [`kernel::ioctl`].
> @@ -160,6 +171,36 @@ fn show_fdinfo(
> }
> }
>
> +/// Wrapper for the kernel's `struct kiocb`.

Could you give more context? Please describe the purpose for the type
and intended use. Perhaps give an example that can be compile tested.

> +///
> +/// The type `T` represents the private data of the file.
> +pub struct Kiocb<'a, T> {
> + inner: NonNull<bindings::kiocb>,
> + _phantom: PhantomData<&'a T>,
> +}

An abstraction for `kiocb` does not belong here. It should go into kernel/fs.

Best regards,
Andreas Hindborg