From nobody Mon Oct 6 01:23:45 2025 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 4CB0321B91D for ; Sun, 27 Jul 2025 15:04:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628667; cv=none; b=b/zdK4vPOc0Kav95XF6mSNRDyYLXsEovvIcXhDDwRwQlx/4CePksngPyDY2YiUPfxYo7/oZvLwvXesPRFi0vQHqxXfG07glkGqHE7mD3mP1d1rG3TSenHmLGAouF5DN1xVWLEbfKI3cGufq5LFqLP2nFvlDKgNk6KaetOiewFAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628667; c=relaxed/simple; bh=H4aHIJOV1TJAq6ZuusFg9Zqr2Br14ng8uZtfr3ysksQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fTU9JQSceKixD2DIuBvkXdprPAXhdFJ2QyC7wEmC6Oiz9BkCQ7oU7hVfGeAoGwyoDFLNPk8K3pCxzBg05TFWnRNa1/VdnLxDlBnD4oUodu3DSz0bMIHJxOKzq6JfBj1PdU5ImpOyaw7k3EjdVGq/S64qDgSjMfDPNvVvdviOKHQ= 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=C+w1gprh; arc=none smtp.client-ip=209.85.214.173 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="C+w1gprh" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2402774851fso1522585ad.1 for ; Sun, 27 Jul 2025 08:04:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=furiosa.ai; s=google; t=1753628665; x=1754233465; 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=TkIl0Ng87tj2Fj8IQfRrJkmK5nZP9FIei480gavcNNA=; b=C+w1gprhE1LSZUr3AKY+NKfeXsbGRczUf3vBzxMHuzhfEgcoMyLlOpvVz6bg3VUJvR T070zJP8V33fQDhZLl/585EIYF8b/UIYhXfMoN0RnOTc0yJt4OA8tZ+9lMizxJPAazPY AO1taE7AGjFBN9DwXCqPOP34ZnMYaEUl9raJk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753628665; x=1754233465; 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=TkIl0Ng87tj2Fj8IQfRrJkmK5nZP9FIei480gavcNNA=; b=gHu3nxWuIji5EG+0PUVcfCmVCH7U7Te/PTYY1JrcdndVzRPzAlnKWMTTzE4ZpTaA2D x+GtKD5bwGNKdUhzDTBzsSlR3O2j+huIivmgyoJeWTUKtISC6/tQC0fXOiU7RSRUN4nK d54F07StKuv03k5uVLOPbvfz5yEyD7F5Iap5e+xV5KofPE74hq6049FoRPFPkynZnycZ WwvO5buPee00Hobjm6CTaPlrqAPg0HzKXGtOuTRTSFd4hyGHDnLPoGe4MaJ1hGCflwHP I+/Df2BGeOUBPBAIxCdnO02vmmdyK8+NU9NTGLYLzndQ6CdeadZixZdlNyrCN1MmRsk1 PR6g== X-Forwarded-Encrypted: i=1; AJvYcCUqfGb8zh3rf5nQAgr+++fObG0c9zBDHPPXboAMPljL9AMt063zOdBtfXYNtuOKZ8YjMgHUHZ2ndpGFDhs=@vger.kernel.org X-Gm-Message-State: AOJu0YwJr9+omye4VFWFCvpDUmeU8w5Lulf8SjaX0pUAGuYJaD7EhIZp tAcun9Oc2ETrd8hEbHnjjQfgP/dgC/Rv5UTqGY2snm6nyFbZALtkH22/jjnHbx200MA= X-Gm-Gg: ASbGncuKx577l3ldvkesROcLxsgAU926HLMhX7fckeBc5/N2QlsmWDo9ktZaoDrlG2l 9zR+OtPAV4fpAg9r0s+B1Njilb4xAXAjVUUuHCcw7EQci2zaRvXG0esoP/DTW68dtTh56CdpHMv jHsuoQuBGp1eiqoNo+Q1v4WRQO6hGAjox3MSlIY0ApFVaUw+lpT4wC+Yf+c9rPrl6qCR9zYVfK5 t3YVq6xT49tiJswsv2W2UPF5DnJhcmQSl2Ixwdg7T8h/XfX5LJAtlCibNjgFdkvo5hryuM3XUW+ cIHC2mghm2UsOYlePneros2FMddmdfsUzQDcgs+Md0+EiJRPPa2pNV/io5KrvPQXFwL7McG6N7o u+FP4kxSq+30rpfYrF/dRJLDS3oz83g+LH1q6dxPv3kssf2FfucRQWmmuX8uvWA== X-Google-Smtp-Source: AGHT+IH3T9NAHqsD3owCYZXUMl0VQjeiye3RBk2SMd/XJ4g10HNgS5Tm3Wd75o21SRnae8bador9kg== X-Received: by 2002:a17:903:ace:b0:240:1953:f9a with SMTP id d9443c01a7336-24019531719mr20105335ad.2.1753628665479; Sun, 27 Jul 2025 08:04:25 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Jul 2025 08:04:25 -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 1/4] rust: bindings: add io_uring headers in bindings_helper.h Date: Sun, 27 Jul 2025 15:03:26 +0000 Message-ID: <20250727150329.27433-2-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 adds two headers io_uring.h io_uring/cmd.h in bindings_helper for implementing rust io_uring abstraction. Signed-off-by: Sidong Yang --- rust/bindings/bindings_helper.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 8cbb660e2ec2..a41205e2b8b8 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -72,6 +72,8 @@ #include #include #include +#include +#include #include =20 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) --=20 2.43.0 From nobody Mon Oct 6 01:23:45 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 From nobody Mon Oct 6 01:23:45 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 2C31921CC55 for ; Sun, 27 Jul 2025 15:04:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628675; cv=none; b=AjYlcp7oQR4N0u4d+SEx7wuSi1OVQ+WTkgFLlCy8gxzmLpMwaUYF9jjlb0OaiMiNeHc8d2IqFFZKk6mtig0fE2aeVLQcOZ7+YoR1U9YlZnS7pd+WfPt4m7R+zwHnsuM5IbSt0CZxl7YjkYjEnKy9YP0nhYmTkFUUslRdSpQIG3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628675; c=relaxed/simple; bh=LYz4BJjEcxej4a2hfZQjwwKXuLLfa6KFrpaj5RCmjU0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GxhOEwBmcfJ894r6K3/DA0g0Vo1VMUDoP0PWTBSG/5X59vHODtlwrKdDfh7BiA8drPNfO09jqLHHzqWEK05lgv/mypuXNrzr2w8g/mmYuC2TKPiLvNufz2m0JSwq9+aL2H3fSMGJnIQjj1XebIsvttRv9qh/tLOFfatGgBCDqLQ= 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=oYvpybBo; arc=none smtp.client-ip=209.85.214.171 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="oYvpybBo" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-24014cd385bso3901085ad.0 for ; Sun, 27 Jul 2025 08:04:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=furiosa.ai; s=google; t=1753628672; x=1754233472; 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=PF8dNyFHhR5n7ctm1h4dvcd/FHv92TBiu95SBZTfIhs=; b=oYvpybBozPeNGcX9sJTU7BAvYgyef8KzlEzqS72ym5+zHa0LXdxdWXDMjZExT3q3Ht wN4kArx/GmqEUXs6VdgDze9FWxp4bLm2t9lobECX1fV6XdG0nGRwzhmRnJevvuX+McHH Unjpe6rXvWlPPAMUGlhntd4Iu6bxmslgI9NZ0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753628672; x=1754233472; 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=PF8dNyFHhR5n7ctm1h4dvcd/FHv92TBiu95SBZTfIhs=; b=xBitl0dZ2RecuhI2v2kgGiBpvY/+rnmmHEaKCWdYPf8YgjIra4M0iVNZZxoqDN3NDG vU/PQpn3o/JVO+nUuwajgfkZFMk4Hz6S5Y1WIjJYZGf4U53KcScnlBZv2tkBmnPY95tO i3dxhZuvcxNvUnZuogiahTY6gMaxqeaPku3EDH7DlriwL9vSMsQyIs80+59C1DKgQOd6 F+Gt/xGBkN40xH2VTv0ygsWoB8hC5vFVpsw7pG20GzZpbrcRjSOsOKHBpXZksSo0m31h mjzB3Nx83/9G+JLC/aze1n9EjKe/69oHg3VcZZlhFH+pU2AEo+ZILfAsnTrZFitd7FJI P1EA== X-Forwarded-Encrypted: i=1; AJvYcCUoMQj7CK6CN1DRgA+9RuugQx8JNxXEqlsKvN4AqtCqE0t7LXZN6ftmIDi+xVi1JPVt0t6W3UQ+yBVob1E=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/9Z1/klp6kolipp2mhNtRWMZz13gaRGxNAc4UJMfiND3j7Wky 0zuEpCR11BLpkLs0NqobFlwVoiAc5VdhHKTX+8w3z6qeNmYxravPmu/jn28ihQfmZXDJg9Jg1fq vRaRO X-Gm-Gg: ASbGncv4j/VKIQa/EeCsYs4NXPbe9mtQd//AM38U59oH7qLf3DQ0pFGomDQ7/7S7PwO lrsID4uIRXpAdBdK89FwNdXIU5hBzW2f4gGdhAJRpbdP+FOJkwm8dtdHuHInlpMQD5EbGWsVQwf 1W8dn7UnuBJxi1qXN7lDe/sMYL1pj8m20GUksF3FbYkRScQCjJq6/DUzz163gF2u/e/mhjXe80q vOZTNTPlkO4/xKnUWA6AyYF6oXiMQ0pkkrKoSuVbjBmg39iWHEoib8ftpFy8eq06Z41zNucpNpw AwBDZw7zuG9cVmiwpHYDiKuqVlDog1NuMHPaBssteGVdn1rj0q+6I1yVZo6Yb8J9n7UWvogNYMV ageXHTdCaIWqK6mfU7PqFxxB4R52x7vMItnjA4zpToFW7OVjHLcMYCBMsBiWYBT1Y3bOmYKm/ X-Google-Smtp-Source: AGHT+IFB6M/iK5b+VX6qsLhQzXTdgj2DRxrKstCfZsGXDnZr07n3sQDXtrRVVzTp4bMKr/agcoYYRA== X-Received: by 2002:a17:902:ccc9:b0:240:2eb6:d5cc with SMTP id d9443c01a7336-2402eb6d9e9mr6477695ad.17.1753628672438; Sun, 27 Jul 2025 08:04:32 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Jul 2025 08:04:32 -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 3/4] rust: miscdevice: add uring_cmd() for MiscDevice trait Date: Sun, 27 Jul 2025 15:03:28 +0000 Message-ID: <20250727150329.27433-4-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 adds uring_cmd() function for MiscDevice trait and its callback implementation. It uses IoUringCmd that io_uring_cmd rust abstraction. Signed-off-by: Sidong Yang --- rust/kernel/miscdevice.rs | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 288f40e79906..54be866ea7ff 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -14,6 +14,7 @@ error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, ffi::{c_int, c_long, c_uint, c_ulong}, fs::File, + io_uring::IoUringCmd, mm::virt::VmaNew, prelude::*, seq_file::SeqFile, @@ -175,6 +176,19 @@ fn show_fdinfo( ) { build_error!(VTABLE_DEFAULT_ERROR) } + + /// Handler for uring_cmd. + /// + /// This function is invoked when userspace process submits the uring_= cmd op + /// on io_uring submission queue. The `io_uring_cmd` would be used for= get + /// arguments cmd_op, sqe, cmd_data. + fn uring_cmd( + _device: ::Borrowed<'_>, + _io_uring_cmd: Pin<&mut IoUringCmd>, + _issue_flags: u32, + ) -> Result { + build_error!(VTABLE_DEFAULT_ERROR) + } } =20 /// A vtable for the file operations of a Rust miscdevice. @@ -332,6 +346,28 @@ impl MiscdeviceVTable { T::show_fdinfo(device, m, file); } =20 + /// # Safety + /// + /// `ioucmd` is not null and points to a valid `bindings::io_uring_cmd= `. + unsafe extern "C" fn uring_cmd( + ioucmd: *mut bindings::io_uring_cmd, + issue_flags: ffi::c_uint, + ) -> ffi::c_int { + // SAFETY: The file is valid for the duration of this call. + let ioucmd =3D unsafe { IoUringCmd::from_raw(ioucmd) }; + let file =3D ioucmd.file(); + + // SAFETY: The file is valid for the duration of this call. + let private =3D unsafe { (*file.as_ptr()).private_data }.cast(); + // SAFETY: uring_cmd calls can borrow the private data of the file. + let device =3D unsafe { ::borrow(private= ) }; + + match T::uring_cmd(device, ioucmd, issue_flags) { + Ok(ret) =3D> ret as ffi::c_int, + Err(err) =3D> err.to_errno() as ffi::c_int, + } + } + const VTABLE: bindings::file_operations =3D bindings::file_operations { open: Some(Self::open), release: Some(Self::release), @@ -354,6 +390,11 @@ impl MiscdeviceVTable { } else { None }, + uring_cmd: if T::HAS_URING_CMD { + Some(Self::uring_cmd) + } else { + None + }, // SAFETY: All zeros is a valid value for `bindings::file_operatio= ns`. ..unsafe { MaybeUninit::zeroed().assume_init() } }; --=20 2.43.0 From nobody Mon Oct 6 01:23:45 2025 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 8DA9B21D583 for ; Sun, 27 Jul 2025 15:04:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628677; cv=none; b=NYLLgf8ltARApOAcZcgri3biRiMXBgD6xyDApzXVvP3WJRsTY0WxqlQ0uZA5g3Em3pD7upv+52VOyxGKcLgd0+klpg7mugp5SvkSBp0nsvXmML+ckU7gEldcg1dB/Zo/sFmWc4lSuwp1YizT1+FCxF3+CBvZou09QbJ7CYjyZ48= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753628677; c=relaxed/simple; bh=jNgn+CIvun+R8VfNUxSXEoyphazpmnbHYWrUF62SfyI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Tv+OBfd+6i+hN/Bd1PNkGsLe7cy0yn1vwKnU/yRxbBTg287UA/bUb38LVSGd9Ids0N1UbGWXXYNFeR5W7uqTLqdn8rHqtDAxcS/GXXn1fQFhlpeNU1LVxAXPPFKAd8hTcG8y7e1bqwAGWO3JAmkEzZ6qammzzLpSlyECn24LA1A= 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=ir2XY/E5; arc=none smtp.client-ip=209.85.214.170 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="ir2XY/E5" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-23aeac7d77aso33109995ad.3 for ; Sun, 27 Jul 2025 08:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=furiosa.ai; s=google; t=1753628676; x=1754233476; 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=BeNP/PP8zrdwI/UScK/RcuZJZ4ARpjDlW5AwHswNcR4=; b=ir2XY/E5gcnilqBBkpD/V/9/SfBhZ7t9ruUs6al5TzEx9aAlEQVjHbnT7fZMytQ3B7 ThmxRCNLpXgdlsxhk3Lv2VBKvY9YL0bD40MMMOh2YDgEPrQ0/TsJIiOpiF7H1aEVSXnQ tTnl/W6Ite76U7giSmcpHIo5y2CdiWO8cA6pU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753628676; x=1754233476; 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=BeNP/PP8zrdwI/UScK/RcuZJZ4ARpjDlW5AwHswNcR4=; b=TAzWHt09B5DIMJQD7811kWn8KoZpuqpTozspL8YHY3ezMihtos87JqxIS8FX8arXHJ nA/YivK89kckFhorQI+Czl5i6Gs5nc0Lis2g0qarNQedrhUKYwJI6xSdilbtrfbb1872 uzTc8gPAL8iW7yx4NsEFlV8OmijL5w1OgDCaAD1hBBvCOcW9OUhTErVcZfaZQbuG7KuT oSHTfoVYZJrckM2Zewcap4xKf0FGeNRVac+XbKGeQ7bRRI/4A/Sx1Q4/L9fPsB8snG6b Z1C/JiCiF5RVYRiceXe/SKKz8xMsvmNVE+NNacKTLiKT18LvIzu3BHv9eDKlsNRKW5y6 Ob3w== X-Forwarded-Encrypted: i=1; AJvYcCWXONubsGO4cIiqWC/b7smBw7H3PdzEWmhbzSKhXIgBJsnlAz4JuDhKVwPzGmQEGpvqhMSsT8hcZL4LQrM=@vger.kernel.org X-Gm-Message-State: AOJu0YwC97gJI0rCXYtND1narJgRnVyWPLeJoPsYLWE6OUyhs+0jQQhS rkYit+0FzCr+6tuA8LWW/v3r+uh0Nhpb3vIzqCfOc92xgbd4jFJ497dmFUN6KLzaweo= X-Gm-Gg: ASbGnctFYbOvHjNoUjfzaBwvIREsyawtqBALmFf7sDi37euZkfU2FSeIwpTugmt2W5L C+fx4nSWl2cDfFVvaRTSSR5CGRe4z5FRg5X8BaKCXvxBREIYFFvKA9kKpz/X2P110/9/vViQ573 PBVQMPRZq5cif88bPQZazNLK7hpCsHsgbQ7GgZZrBkfpaveIJPx41DeJAytnKJCl4R5sh3xWcQi yIXu/bFMeP5T/lhbU97oHFnPtdBx/L2tKwnni6OWHMOjMdVe5pBEdgyIYDGf0DdcmWpkXX9LEN2 /XmDLKsRBuJ55sLYIz+rwzdeg3IMsU3Ia0VadyOX/0vGgvfQAD3Yx5stcXLPv72K0wlz5OWF+Cf ESyuXSszi7WImuIC6F5ztghRjde//j5t6yhKyeBhIRE61vGEJvfYVQwDVxF9Iug== X-Google-Smtp-Source: AGHT+IEcwUGTvIwSkq572UO1/rKsozpV5VbkWfGTO1wB9NAisfgejoLG5RhSTngcNuD/suDu9l8M6g== X-Received: by 2002:a17:903:3c4d:b0:234:c8f6:1afb with SMTP id d9443c01a7336-23fb2ee8a67mr118373005ad.0.1753628675847; Sun, 27 Jul 2025 08:04:35 -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.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Jul 2025 08:04:35 -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 4/4] samples: rust: rust_misc_device: add uring_cmd example Date: Sun, 27 Jul 2025 15:03:29 +0000 Message-ID: <20250727150329.27433-5-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 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 --- 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_devi= ce.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 =3D _IOR::('|' as u32, 0x81); const RUST_MISC_DEV_SET_VALUE: u32 =3D _IOW::('|' as u32, 0x82); =20 +const RUST_MISC_DEV_URING_CMD_SET_VALUE: u32 =3D _IOR::('|' as u32, 0= x83); +const RUST_MISC_DEV_URING_CMD_GET_VALUE: u32 =3D _IOW::('|' as u32, 0= x84); + module! { type: RustMiscDeviceModule, name: "rust_misc_device", @@ -190,6 +194,36 @@ fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: = u32, arg: usize) -> Result =20 Ok(0) } + + fn uring_cmd( + me: Pin<&RustMiscDevice>, + io_uring_cmd: Pin<&mut IoUringCmd>, + _issue_flags: u32, + ) -> Result { + dev_info!(me.dev, "UringCmd Rust Misc Device Sample\n"); + + let cmd =3D io_uring_cmd.cmd_op(); + let cmd_data =3D io_uring_cmd.sqe().cmd_data().as_ptr() as *const = usize; + + // SAFETY: `cmd_data` is guaranteed to be a valid pointer to the c= ommand data + // within the SQE structure. + // FIXME: switch to read_once() when it's available. + let addr =3D unsafe { core::ptr::read_volatile(cmd_data) }; + + match cmd { + RUST_MISC_DEV_URING_CMD_SET_VALUE =3D> { + me.set_value(UserSlice::new(addr, 8).reader())?; + } + RUST_MISC_DEV_URING_CMD_GET_VALUE =3D> { + me.get_value(UserSlice::new(addr, 8).writer())?; + } + _ =3D> { + dev_err!(me.dev, "-> uring_cmd not recognised: {}\n", cmd); + return Err(ENOTTY); + } + } + Ok(0) + } } =20 #[pinned_drop] --=20 2.43.0