[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <B650673C-E2C8-4382-A86D-CD44840F5B21@collabora.com>
Date: Fri, 1 Aug 2025 11:11:44 -0300
From: Daniel Almeida <daniel.almeida@...labora.com>
To: Sidong Yang <sidong.yang@...iosa.ai>
Cc: Caleb Sander Mateos <csander@...estorage.com>,
Benno Lossin <lossin@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Arnd Bergmann <arnd@...db.de>,
Jens Axboe <axboe@...nel.dk>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
io-uring@...r.kernel.org
Subject: Re: [RFC PATCH v2 4/4] samples: rust: rust_misc_device: add uring_cmd
example
Hi Sidong,
> On 27 Jul 2025, at 12:03, Sidong Yang <sidong.yang@...iosa.ai> wrote:
>
> This patch makes rust_misc_device handle uring_cmd. Command ops are like
> ioctl that set or get values in simple way.
>
> Signed-off-by: Sidong Yang <sidong.yang@...iosa.ai>
> ---
> samples/rust/rust_misc_device.rs | 34 ++++++++++++++++++++++++++++++++
> 1 file changed, 34 insertions(+)
>
> diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
> index c881fd6dbd08..1044bde86e8d 100644
> --- a/samples/rust/rust_misc_device.rs
> +++ b/samples/rust/rust_misc_device.rs
> @@ -101,6 +101,7 @@
> c_str,
> device::Device,
> fs::File,
> + io_uring::IoUringCmd,
> ioctl::{_IO, _IOC_SIZE, _IOR, _IOW},
> miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},
> new_mutex,
> @@ -114,6 +115,9 @@
> const RUST_MISC_DEV_GET_VALUE: u32 = _IOR::<i32>('|' as u32, 0x81);
> const RUST_MISC_DEV_SET_VALUE: u32 = _IOW::<i32>('|' as u32, 0x82);
>
> +const RUST_MISC_DEV_URING_CMD_SET_VALUE: u32 = _IOR::<i32>('|' as u32, 0x83);
> +const RUST_MISC_DEV_URING_CMD_GET_VALUE: u32 = _IOW::<i32>('|' as u32, 0x84);
> +
> module! {
> type: RustMiscDeviceModule,
> name: "rust_misc_device",
> @@ -190,6 +194,36 @@ fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result
>
> Ok(0)
> }
> +
> + fn uring_cmd(
> + me: Pin<&RustMiscDevice>,
“me” ?
> + io_uring_cmd: Pin<&mut IoUringCmd>,
> + _issue_flags: u32,
> + ) -> Result<i32> {
> + dev_info!(me.dev, "UringCmd Rust Misc Device Sample\n");
> +
> + let cmd = io_uring_cmd.cmd_op();
> + let cmd_data = io_uring_cmd.sqe().cmd_data().as_ptr() as *const usize;
> +
> + // SAFETY: `cmd_data` is guaranteed to be a valid pointer to the command data
> + // within the SQE structure.
This is what core::ptr::read_volatile says:
Safety
Behavior is undefined if any of the following conditions are violated:
• src must be valid for reads.
• src must be properly aligned.
• src must point to a properly initialized value of type T.
You must prove that the pre-conditions above are fulfilled here.
> + // FIXME: switch to read_once() when it's available.
> + let addr = unsafe { core::ptr::read_volatile(cmd_data) };
So drivers have to write “unsafe” directly? It isn’t forbidden, but
we should try our best to avoid it.
> +
> + match cmd {
> + RUST_MISC_DEV_URING_CMD_SET_VALUE => {
> + me.set_value(UserSlice::new(addr, 8).reader())?;
> + }
> + RUST_MISC_DEV_URING_CMD_GET_VALUE => {
> + me.get_value(UserSlice::new(addr, 8).writer())?;
> + }
> + _ => {
> + dev_err!(me.dev, "-> uring_cmd not recognised: {}\n", cmd);
> + return Err(ENOTTY);
> + }
> + }
> + Ok(0)
> + }
> }
>
Who calls this function?
> #[pinned_drop]
> --
> 2.43.0
>
>
— Daniel
Powered by blists - more mailing lists