From nobody Mon Oct 6 17:09:07 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 C386428850C for ; Sat, 19 Jul 2025 14:34:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752935656; cv=none; b=hY4Ts0nGRC1n1vD+FWECaN5dJkVtyBrck4imU04yI9GxLsmdRUu1yYU3IXH5aLalnE4qyO/wuGPX8WtkRAjSDtAPTc+Gm8XBH6bRKQywqtvAMF1n2n6EaqUH5/8Js1Yc/y8WH5hUIP84aMXUSNUs/WAbGaWcTPTyVlBXqArkakM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752935656; c=relaxed/simple; bh=hiYfT7g7dEoDoCFfoRNmanSXnUrnNYBQJyWlQ2T6V+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a6FFr/a3+2DsCQUNoOczq307UT/ZjzN+qyN6RWk0ZAjgzF5Ju9B0N/ROB733WC9CLBhizaIAZZ9BgORrDokETciUq1O6UX4czI+bP9dG4o/hVeqTy1g8NJUzLzXmyLr3SAl6Vez0dtYSalOJ/dFBpoRoHeetZJKd7uFgRKWLO8w= 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=UHA2Vjl0; arc=none smtp.client-ip=209.85.214.180 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="UHA2Vjl0" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-235d6de331fso34309855ad.3 for ; Sat, 19 Jul 2025 07:34:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=furiosa.ai; s=google; t=1752935654; x=1753540454; 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=QoZ5l7uX723ZF26lE5z2JZL2KcJPe7FMlPZI7PwJSRw=; b=UHA2Vjl0HVSaJ5MUNvqQs9kjuKTho+MVWNouiA0AgG98jP63Fet7xfUtuOeSSO5JXx xq5i4nfJxk542Y+Gzm7Q+ny5knN4q4MLnSQwB4aIEne3FPgnREnnZvul597C0q1bAFNt 4pkkCBjs19vvD8HJEq1fUSTYuDxuD1anp5x7E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752935654; x=1753540454; 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=QoZ5l7uX723ZF26lE5z2JZL2KcJPe7FMlPZI7PwJSRw=; b=XRclkY2zyRhy5arI0OpbiygCMU3OhRtqjwm1PizsW6BQdLfVWvC8W/pnJwUuJVeyTP uuruog6yZI9xwCSFsm37X35psGiqVawTRNHrrPtkE1ISLpm5CWK0www2P+OuWKW83TeG cVaguuNN7+QeFDRPAiO5hnxoMwZ2AexyMq82ZG8nsUFqdbJE+hlEP/qfZ5bV3npSV6/Z 0xak7MEUc9qqJqcYxxUGuaPt5b0fkYnoZJvj1J/OrPOPXAarJjMIt8+A34POhLquWXdt PuzEFqGyWVIu8jmgfHRpX0c5fK8So6J3SViRLa5+FBcdGTO40aSkW6PUbktlPQCyAI5o m85A== X-Forwarded-Encrypted: i=1; AJvYcCWoDsSjZ1JHPfHfu6H351KG6Zb5/ZbeGjrFVohDJElL3Sqg5vGrhQALMcYMCklcJfKqOYXB6YNsxGmLabg=@vger.kernel.org X-Gm-Message-State: AOJu0YxQNfCDBOLEQjnXx30QajSbqNkO+lex87HojgLRvsPZr1IOUVa/ ujjkOsDkxCfPVXWCXWSpMdTW3NwctZsCWpG8WoNl7DBxDiZPsHmG+s5+eT9ABWYhtuMZxooOx2H 8u5Qj X-Gm-Gg: ASbGncs/eY8Z4EtrVoDF90iiyji6jyNPgmWMCLLpp+MoisHqMO6DbiyvmFHQ6zGvncV Bppe6xQYSIhZtZHHsNi9NaJ4B+5g7+MHbT86nBP9Imcwi6q8TRNvR/o1zr7JD2a5oCvsRdBSn0k LdB2qXB1Hj5VY5LUBXzKAC9dbE7QvREhHtuLPxEYFeEe1QVNGb1/QOWDcYTUjhpSAJoQeE8+kg5 BeQ0y3T9FkiO+OveqYp3r0sAufkwsDNazuGHDA7I2ubIGoQb02RtDOWzMb4zQl1NUt5WIVomX4D GaK9mIwyqPxSNnhd7kwmQwHEdCJrmJF+1Mm140fs6CYlDoKiRSMO2TWjCCRJxnl71Xd/ecGKNj5 RBpLNkwryIW1uoTOnB/k1GE/7j129z6Hk5IMXoYI5VH0tBrxXmJHCAf7OGZ7oLwO2aXT3MQ== X-Google-Smtp-Source: AGHT+IFnM/CiV+7xt++m6hPsUY32KdWuau4dLfx8UwlhOPQ9sEoM5NIlxtAMRGhEYpHMy1yGgtgf5g== X-Received: by 2002:a17:902:ef08:b0:23d:d9ae:3b56 with SMTP id d9443c01a7336-23e256b7467mr228306365ad.22.1752935654049; Sat, 19 Jul 2025 07:34:14 -0700 (PDT) Received: from sidong.sidong.yang.office.furiosa.vpn ([61.83.209.48]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23e3b5e3d4esm30017525ad.23.2025.07.19.07.34.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Jul 2025 07:34:13 -0700 (PDT) From: Sidong Yang To: Miguel Ojeda , Arnd Bergmann , Jens Axboe Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, io-uring@vger.kernel.org, Sidong Yang Subject: [PATCH 2/4] rust: io_uring: introduce rust abstraction for io-uring cmd Date: Sat, 19 Jul 2025 14:33:56 +0000 Message-ID: <20250719143358.22363-3-sidong.yang@furiosa.ai> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250719143358.22363-1-sidong.yang@furiosa.ai> References: <20250719143358.22363-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 | 114 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 2 files changed, 115 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..7843effbedb4 --- /dev/null +++ b/rust/kernel/io_uring.rs @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Furiosa AI. + +//! Files and file descriptors. +//! +//! C headers: [`include/linux/io_uring/cmd.h`](srctree/include/linux/io_u= ring/cmd.h) and +//! [`include/linux/file.h`](srctree/include/linux/file.h) + +use core::mem::MaybeUninit; + +use crate::{fs::File, types::Opaque}; + +pub mod flags { + pub const COMPLETE_DEFER: i32 =3D bindings::io_uring_cmd_flags_IO_URIN= G_F_COMPLETE_DEFER; + pub const UNLOCKED: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_UN= LOCKED; + + pub const MULTISHOT: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_M= ULTISHOT; + pub const IOWQ: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_IOWQ; + pub const NONBLOCK: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_NO= NBLOCK; + + pub const SQE128: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_SQE1= 28; + pub const CQE32: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_CQE32; + pub const IOPOLL: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_IOPO= LL; + + pub const CANCEL: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_CANC= EL; + pub const COMPAT: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_COMP= AT; + pub const TASK_DEAD: i32 =3D bindings::io_uring_cmd_flags_IO_URING_F_T= ASK_DEAD; +} + +#[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 the pointer is not dangling an= d 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 the pointer is not dangling an= d stays valid + unsafe { (*self.inner.get()).flags } + } + + /// Returns the ref pdu for free use. + #[inline] + pub fn pdu(&mut self) -> MaybeUninit<&mut [u8; 32]> { + // SAFETY: The call guarantees that the pointer is not dangling an= d stays valid + unsafe { MaybeUninit::new(&mut (*self.inner.get()).pdu) } + } + + /// Constructs a new `struct io_uring_cmd` wrapper from a file descrip= tor. + #[inline] + pub unsafe fn from_raw<'a>(ptr: *const bindings::io_uring_cmd) -> &'a = IoUringCmd { + // SAFETY: The caller guarantees that the pointer is not dangling = and stays valid for the + // duration of 'a. The cast is okay because `File` is `repr(transp= arent)`. + unsafe { &*ptr.cast() } + } + + // Returns the file that referenced by uring cmd self. + #[inline] + pub fn file<'a>(&'a self) -> &'a File { + // SAFETY: The call guarantees that the pointer is not dangling an= d stays valid + let file =3D unsafe { (*self.inner.get()).file }; + unsafe { File::from_raw_file(file) } + } + + // Returns the sqe that referenced by uring cmd self. + #[inline] + pub fn sqe(&self) -> &IoUringSqe { + // SAFETY: The call guarantees that the pointer is not dangling an= d stays valid + let ptr =3D unsafe { (*self.inner.get()).sqe }; + unsafe { IoUringSqe::from_raw(ptr) } + } + + // Called by consumers of io_uring_cmd, if they originally returned -E= IOCBQUEUED upon receiving the command + #[inline] + pub fn done(self, ret: isize, res2: u64, issue_flags: u32) { + // SAFETY: The call guarantees that the pointer is not dangling an= d stays valid + unsafe { + bindings::io_uring_cmd_done(self.inner.get(), ret, res2, issue= _flags); + } + } +} + +#[repr(transparent)] +pub struct IoUringSqe { + inner: Opaque, +} + +impl<'a> IoUringSqe { + pub fn cmd_data(&'a self) -> &'a [Opaque] { + // SAFETY: The call guarantees that the pointer is not dangling an= d stays valid + unsafe { + let cmd =3D (*self.inner.get()).__bindgen_anon_6.cmd.as_ref(); + core::slice::from_raw_parts(cmd.as_ptr() as *const Opaque,= 8) + } + } + + #[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 `File` is `repr(transp= arent)`. + // + // INVARIANT: The caller guarantees that there are no problematic = `fdget_pos` calls. + 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