From: Sidong Yang <sidong.yang@furiosa.ai>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: Caleb Sander Mateos <csander@purestorage.com>,
Benno Lossin <lossin@kernel.org>, Miguel Ojeda <ojeda@kernel.org>,
Arnd Bergmann <arnd@arndb.de>, Jens Axboe <axboe@kernel.dk>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
io-uring@vger.kernel.org
Subject: Re: [RFC PATCH v2 4/4] samples: rust: rust_misc_device: add uring_cmd example
Date: Thu, 7 Aug 2025 15:30:25 +0900 [thread overview]
Message-ID: <aJRIAUwlIJkJ9jxV@sidongui-MacBookPro.local> (raw)
In-Reply-To: <B650673C-E2C8-4382-A86D-CD44840F5B21@collabora.com>
On Fri, Aug 01, 2025 at 11:11:44AM -0300, Daniel Almeida wrote:
> Hi Sidong,
>
> > On 27 Jul 2025, at 12:03, Sidong Yang <sidong.yang@furiosa.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@furiosa.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" ?
ioctl() uses the args name with "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:
> o src must be valid for reads.
> o src must be properly aligned.
> o src must point to a properly initialized value of type T.
>
> You must prove that the pre-conditions above are fulfilled here.
Okay, I would describe pre-conditions.
>
> > + // 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.
I don't know it's the best way to use &[u8] `cmd_data` in driver. The driver should
cast the `cmd_data` to user structure i32 in this time. is there any safe way
to provide safe interface for user driver?
>
> > +
> > + 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?
This function would be called in io_uring subsystem. In detail, there is caller
io_uring_cmd() in io_uring/uring_cmd.c. When io_uring subsystem noticed that
submission queue entry which has uring cmd op pushed to submission queue, it
would be submitted and actually calls this function.
>
> > #[pinned_drop]
> > --
> > 2.43.0
> >
> >
>
> - Daniel
>
next prev parent reply other threads:[~2025-08-07 6:30 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-27 15:03 [RFC PATCH v2 0/4] rust: miscdevice: abstraction for uring-cmd Sidong Yang
2025-07-27 15:03 ` [RFC PATCH v2 1/4] rust: bindings: add io_uring headers in bindings_helper.h Sidong Yang
2025-07-27 15:03 ` [RFC PATCH v2 2/4] rust: io_uring: introduce rust abstraction for io-uring cmd Sidong Yang
2025-08-01 13:48 ` Daniel Almeida
2025-08-02 10:52 ` Benno Lossin
2025-08-06 12:38 ` Daniel Almeida
2025-08-06 13:38 ` Benno Lossin
2025-08-08 6:56 ` Sidong Yang
2025-08-08 8:49 ` Benno Lossin
2025-08-08 9:43 ` Sidong Yang
2025-08-09 10:18 ` Benno Lossin
2025-08-09 12:51 ` Sidong Yang
2025-08-09 20:22 ` Benno Lossin
2025-08-10 13:50 ` Sidong Yang
2025-08-10 14:27 ` Daniel Almeida
2025-08-10 14:46 ` Sidong Yang
2025-08-10 20:06 ` Benno Lossin
2025-08-11 12:34 ` Sidong Yang
2025-08-11 12:44 ` Daniel Almeida
2025-08-11 14:50 ` Sidong Yang
2025-08-12 8:34 ` Benno Lossin
2025-08-12 12:19 ` Sidong Yang
2025-08-12 12:43 ` Daniel Almeida
2025-08-12 13:56 ` Sidong Yang
2025-08-12 13:59 ` Daniel Almeida
2025-08-12 14:38 ` Daniel Almeida
2025-08-13 0:54 ` Sidong Yang
2025-08-08 13:55 ` Caleb Sander Mateos
2025-08-09 12:53 ` Sidong Yang
2025-08-05 3:39 ` Sidong Yang
2025-08-05 13:02 ` Daniel Almeida
2025-08-06 9:11 ` Sidong Yang
2025-07-27 15:03 ` [RFC PATCH v2 3/4] rust: miscdevice: add uring_cmd() for MiscDevice trait Sidong Yang
2025-08-01 14:04 ` Daniel Almeida
2025-08-07 7:46 ` Sidong Yang
2025-07-27 15:03 ` [RFC PATCH v2 4/4] samples: rust: rust_misc_device: add uring_cmd example Sidong Yang
2025-08-01 14:11 ` Daniel Almeida
2025-08-07 6:30 ` Sidong Yang [this message]
2025-07-27 17:17 ` [RFC PATCH v2 0/4] rust: miscdevice: abstraction for uring-cmd Daniel Almeida
2025-08-01 14:13 ` Daniel Almeida
2025-08-07 6:17 ` Sidong Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aJRIAUwlIJkJ9jxV@sidongui-MacBookPro.local \
--to=sidong.yang@furiosa.ai \
--cc=arnd@arndb.de \
--cc=axboe@kernel.dk \
--cc=csander@purestorage.com \
--cc=daniel.almeida@collabora.com \
--cc=gregkh@linuxfoundation.org \
--cc=io-uring@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).