From nobody Mon Oct 6 03:13:53 2025 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E549421CA02 for ; Sun, 27 Jul 2025 15:04:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628671; cv=none; b=aOZ1VQ6Y7hHj+eqWAP3aVTlnT5tk61sfcDtbIXkLWtiSwF4sC6gytqmI2J9XuUqWsjjVSrG2o2nFRxL45xXjmz2a4LQo9f93gXhkA91wSlPRmSe15dOuLYopE/4qo77UxnLjx1rf4rReP18JwKZEeYNy+6gFQ5GbjD10+NeaOu4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628671; c=relaxed/simple; bh=cK0C6CMwUFq5DnEbyxxAxzphNNePE5niB42IjceE7Vg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ly77gmGYORpUN30Cshb8y/mc4icsLZm11+mzx/kBfnVI4iGZIbS85f49lYjlYEdcCDfrQsp7fOnKdwY3ZiHsC77C4P/lELLOcfU3u8yftHPdMq/sqLvcolLytEHi4keUNQncuTOTKCfIW5PW3M1NVjJQXtm7F1RgBgwrAOo1wb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=furiosa.ai; spf=none smtp.mailfrom=furiosa.ai; dkim=pass (1024-bit key) header.d=furiosa.ai header.i=@furiosa.ai header.b=WbS2hieZ; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=furiosa.ai Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=furiosa.ai Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=furiosa.ai header.i=@furiosa.ai header.b="WbS2hieZ" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-313bb9b2f5bso3308814a91.3 for ; Sun, 27 Jul 2025 08:04:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=furiosa.ai; s=google; t=1753628669; x=1754233469; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IJ11FFDjzx+V6NkfUN6iB7gcMGoXSPCw6j8ek1pZA3I=; b=WbS2hieZqce/Ywj/m8nhIZMQ7Mj2+83CSmSjrJzVQiudsZH5bqhp3D4+eA/+DqN9qZ zPfLETjxQnhsmAjGJcTV0/D4wSfw5CQLQ0K0GEMwZhKk//MU/ytbCHb/YVzzC0Bg9hyH IpHu8BO2sx2hsm2dhKn5wNEgRjxXEGyGq27M0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753628669; x=1754233469; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IJ11FFDjzx+V6NkfUN6iB7gcMGoXSPCw6j8ek1pZA3I=; b=kNP6Kz7LoTQS0NItkmBLJMKz5tuvWWWYojcbxq4CqxDSYddgoq5/mkrBD9Uc/668K0 aHkg93gnw/w5wJh5M4q16MIKucw9hRpYkDpueAHJWt06PL4B2d06V6sxx1iB8wbtGD38 xziUbaWWSq8YNAGkkycu42uNn6ilUP2/s/CVC/pC/B9LvAAX58a9CI1+/rIu4Exg3UuJ /kFRCFlIYZnkOrKvOuXrfv36mOnDUQgTXGOPs4xiDXihceMTIpkdXVhuPMEfaiekACJf qZJPKXpcX0GCvcBBEdMPakFlvEi2eX6Bzw4Wc1i//dnT0+FEo9CfsDbLja1qw7jhP28I 4MuA== X-Forwarded-Encrypted: i=1; AJvYcCW24eI26ndQsyewRBaEAIBd/mLB9kocR+0k3u4lVFrNBqxs3z1nDG8Tb+d5bRsoVpFgFZjkf6HC0w8zNMk=@vger.kernel.org X-Gm-Message-State: AOJu0YxadI8OMKHkgwcKFqappA71p78fFmkyPRKWfnTuxXqtjPMpGVdC YKxjnsB9GS7BynrClt451ByAQBIW7ICX6NBrMcx7AjliEJiE5HU7cTYtwBS78W5BjQM= X-Gm-Gg: ASbGncu29BH63hoAqH9pNbVQnpzzszmjYeVxN7Y/6m+oHdyb7DFoaiFfW5M+NLVG4F7 IWSGanD5/glGorsy0pt/ZncF5QLQ4ZondsXvpw8OTnvk/ONwOIlnxeU1AqrkpDXdS9pmskOrzgx 6YoYKFckJpQ83kaXN4NR94NGALa2hBCr5SnHrSrn/P9LlkrOTo/ikek5K8FFDHb/bTlYUrt+k3M pgLHwyv+13Ob/3aeyq2yKN19F2mM4hzq7c3KJTL8Iu5lc2BQAxsjk/ylc6pWA936NjchD69GGQD cI8JbcGZu7nH7Rt2XErINlOgMS185ZsXhve3q/0ZIqua5eSiGrecf0S1TIgFU3AzLSLwdjsCF0d 6zW3eZyNJYYjWxYVoqqY0kdJerNYcycEXvZvve8FNmGaBIO5UY0hpQAGQuEIJGw== X-Google-Smtp-Source: AGHT+IH0f6GIgi9C1wsJukW9qT+wBKrNk/MtPTNq8t53L3BzWGIVFMNGwdiF9Ph0f4hFJBwPzZjydA== X-Received: by 2002:a17:90b:4c86:b0:313:17e3:7ae0 with SMTP id 98e67ed59e1d1-31e77a26fcbmr10353854a91.34.1753628668983; Sun, 27 Jul 2025 08:04:28 -0700 (PDT) Received: from sidong.sidong.yang.office.furiosa.vpn ([175.195.128.78]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23ffdec96aesm15381965ad.165.2025.07.27.08.04.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Jul 2025 08:04:28 -0700 (PDT) From: Sidong Yang To: Caleb Sander Mateos , Benno Lossin Cc: Miguel Ojeda , Arnd Bergmann , Jens Axboe , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, io-uring@vger.kernel.org, Sidong Yang Subject: [RFC PATCH v2 2/4] rust: io_uring: introduce rust abstraction for io-uring cmd Date: Sun, 27 Jul 2025 15:03:27 +0000 Message-ID: <20250727150329.27433-3-sidong.yang@furiosa.ai> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250727150329.27433-1-sidong.yang@furiosa.ai> References: <20250727150329.27433-1-sidong.yang@furiosa.ai> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch introduces rust abstraction for io-uring sqe, cmd. IoUringSqe abstracts io_uring_sqe and it has cmd_data(). and IoUringCmd is abstraction for io_uring_cmd. From this, user can get cmd_op, flags, pdu and also sqe. Signed-off-by: Sidong Yang --- rust/kernel/io_uring.rs | 183 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 2 files changed, 184 insertions(+) create mode 100644 rust/kernel/io_uring.rs diff --git a/rust/kernel/io_uring.rs b/rust/kernel/io_uring.rs new file mode 100644 index 000000000000..0acdf3878247 --- /dev/null +++ b/rust/kernel/io_uring.rs @@ -0,0 +1,183 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Furiosa AI. + +//! IoUring command and submission queue entry abstractions. +//! +//! C headers: [`include/linux/io_uring/cmd.h`](srctree/include/linux/io_u= ring/cmd.h) and +//! [`include/linux/io_uring/io_uring.h`](srctree/include/linux/io_uring/i= o_uring.h) + +use core::{mem::MaybeUninit, pin::Pin, ptr::addr_of_mut}; + +use crate::{fs::File, types::Opaque}; + +/// A Rust abstraction for the Linux kernel's `io_uring_cmd` structure. +/// +/// This structure is a safe, opaque wrapper around the raw C `io_uring_cm= d` +/// binding from the Linux kernel. It represents a command structure used +/// in io_uring operations within the kernel. +/// +/// # Type Safety +/// +/// The `#[repr(transparent)]` attribute ensures that this wrapper has +/// the same memory layout as the underlying `io_uring_cmd` structure, +/// allowing it to be safely transmuted between the two representations. +/// +/// # Fields +/// +/// * `inner` - An opaque wrapper containing the actual `io_uring_cmd` dat= a. +/// The `Opaque` type prevents direct access to the internal +/// structure fields, ensuring memory safety and encapsulation. +/// +/// # Usage +/// +/// This type is used internally by the io_uring subsystem to manage +/// asynchronous I/O commands. It is typically accessed through a pinned +/// mutable reference: `Pin<&mut IoUringCmd>`. The pinning ensures that +/// the structure remains at a fixed memory location, which is required +/// for safe interaction with the kernel's io_uring infrastructure. +/// +/// Users typically receive this type as an argument in the `file_operatio= ns::uring_cmd()` +/// callback function, where they can access and manipulate the io_uring c= ommand +/// data for custom file operations. +/// +/// This type should not be constructed or manipulated directly by +/// kernel module developers. +#[repr(transparent)] +pub struct IoUringCmd { + inner: Opaque, +} + +impl IoUringCmd { + /// Returns the cmd_op with associated with the io_uring_cmd. + #[inline] + pub fn cmd_op(&self) -> u32 { + // SAFETY: The call guarantees that `self.inner` is not dangling a= nd stays valid + unsafe { (*self.inner.get()).cmd_op } + } + + /// Returns the flags with associated with the io_uring_cmd. + #[inline] + pub fn flags(&self) -> u32 { + // SAFETY: The call guarantees that `self.inner` is not dangling a= nd stays valid + unsafe { (*self.inner.get()).flags } + } + + /// Returns the ref pdu for free use. + #[inline] + pub fn pdu(&mut self) -> &mut MaybeUninit<[u8; 32]> { + // SAFETY: The call guarantees that `self.inner` is not dangling a= nd stays valid + let inner =3D unsafe { &mut *self.inner.get() }; + let ptr =3D addr_of_mut!(inner.pdu) as *mut MaybeUninit<[u8; 32]>; + + // SAFETY: The call guarantees that `self.inner` is not dangling a= nd stays valid + unsafe { &mut *ptr } + } + + /// Constructs a new `IoUringCmd` from a raw `io_uring_cmd` + /// + /// # Safety + /// + /// The caller must guarantee that: + /// - The pointer `ptr` is not null and points to a valid `bindings::i= o_uring_cmd`. + /// - The memory pointed to by `ptr` remains valid for the duration of= the returned reference's lifetime `'a`. + /// - The memory will not be moved or freed while the returned `Pin<&m= ut IoUringCmd>` is alive. + #[inline] + pub unsafe fn from_raw<'a>(ptr: *mut bindings::io_uring_cmd) -> Pin<&'= a mut IoUringCmd> { + // SAFETY: The caller guarantees that the pointer is not dangling = and stays valid for the + // duration of 'a. The cast is okay because `IoUringCmd` is `repr(= transparent)` and has the + // same memory layout as `bindings::io_uring_cmd`. The returned `P= in` ensures that the object + // cannot be moved, which is required because the kernel may hold = pointers to this memory + // location and moving it would invalidate those pointers. + unsafe { Pin::new_unchecked(&mut *ptr.cast()) } + } + + /// Returns the file that referenced by uring cmd self. + #[inline] + pub fn file(&self) -> &File { + // SAFETY: The call guarantees that the `self.inner` is not dangli= ng and stays valid + let file =3D unsafe { (*self.inner.get()).file }; + // SAFETY: The call guarantees that `file` points valid file. + unsafe { File::from_raw_file(file) } + } + + /// Returns a reference to the uring cmd's SQE. + #[inline] + pub fn sqe(&self) -> &IoUringSqe { + // SAFETY: The call guarantees that the `self.inner` is not dangli= ng and stays valid + let sqe =3D unsafe { (*self.inner.get()).sqe }; + // SAFETY: The call guarantees that the `sqe` points valid io_urin= g_sqe. + unsafe { IoUringSqe::from_raw(sqe) } + } + + /// Called by consumers of io_uring_cmd, if they originally returned -= EIOCBQUEUED upon receiving the command + #[inline] + pub fn done(self: Pin<&mut IoUringCmd>, ret: isize, res2: u64, issue_f= lags: u32) { + // SAFETY: The call guarantees that `self.inner` is not dangling a= nd stays valid + unsafe { + bindings::io_uring_cmd_done(self.inner.get(), ret, res2, issue= _flags); + } + } +} + +/// A Rust abstraction for the Linux kernel's `io_uring_sqe` structure. +/// +/// This structure is a safe, opaque wrapper around the raw C `io_uring_sq= e` +/// binding from the Linux kernel. It represents a Submission Queue Entry +/// used in io_uring operations within the kernel. +/// +/// # Type Safety +/// +/// The `#[repr(transparent)]` attribute ensures that this wrapper has +/// the same memory layout as the underlying `io_uring_sqe` structure, +/// allowing it to be safely transmuted between the two representations. +/// +/// # Fields +/// +/// * `inner` - An opaque wrapper containing the actual `io_uring_sqe` dat= a. +/// The `Opaque` type prevents direct access to the internal +/// structure fields, ensuring memory safety and encapsulation. +/// +/// # Usage +/// +/// This type represents a submission queue entry that describes an I/O +/// operation to be executed by the io_uring subsystem. It contains +/// information such as the operation type, file descriptor, buffer +/// pointers, and other operation-specific data. +/// +/// Users can obtain this type from `IoUringCmd::sqe()` method, which +/// extracts the submission queue entry associated with a command. +/// +/// This type should not be constructed or manipulated directly by +/// kernel module developers. +#[repr(transparent)] +pub struct IoUringSqe { + inner: Opaque, +} + +impl<'a> IoUringSqe { + /// Returns the cmd_data with associated with the io_uring_sqe. + /// This function returns 16 byte array. We don't support IORING_SETUP= _SQE128 for now. + pub fn cmd_data(&'a self) -> &'a [Opaque] { + // SAFETY: The call guarantees that `self.inner` is not dangling a= nd stays valid + let cmd =3D unsafe { (*self.inner.get()).__bindgen_anon_6.cmd.as_r= ef() }; + + // SAFETY: The call guarantees that `cmd` is not dangling and stay= s valid + unsafe { core::slice::from_raw_parts(cmd.as_ptr() as *const Opaque= , 16) } + } + + /// Constructs a new `IoUringSqe` from a raw `io_uring_sqe` + /// + /// # Safety + /// + /// The caller must guarantee that: + /// - The pointer `ptr` is not null and points to a valid `bindings::i= o_uring_sqe`. + /// - The memory pointed to by `ptr` remains valid for the duration of= the returned reference's lifetime `'a`. + #[inline] + pub unsafe fn from_raw(ptr: *const bindings::io_uring_sqe) -> &'a IoUr= ingSqe { + // SAFETY: The caller guarantees that the pointer is not dangling = and stays valid for the + // duration of 'a. The cast is okay because `IoUringSqe` is `repr(= transparent)` and has the + // same memory layout as `bindings::io_uring_sqe`. + unsafe { &*ptr.cast() } + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 6b4774b2b1c3..fb310e78d51d 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -80,6 +80,7 @@ pub mod fs; pub mod init; pub mod io; +pub mod io_uring; pub mod ioctl; pub mod jump_label; #[cfg(CONFIG_KUNIT)] --=20 2.43.0