From nobody Fri Oct 3 23:04:07 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 086FA2EBBB1 for ; Fri, 22 Aug 2025 08:42:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852172; cv=none; b=n9iQ6bC0c5YCS1Hns6nJqnoFKh5hDC3j1HkAQ2VMh6LAN8jlPeKYdZ9rBBEziMD+lLAUvENUCZSIWrXGf5weWdB1jU0/jox+j4GKrppRwQzh3JMB6WUIwWQSM/x4XO93NqX0TSSh1tDO3rPCai0a6DlDnFcQ0/+i822Nw55OdR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852172; c=relaxed/simple; bh=in5mD66dF6MWhJFhTOWar9Vd/EkPs7Wos8Q4bTZR/Uw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Vvr3E96d0cF5R6XpzAdR+WREwP9ChtHV9jtGU+CrOiP78zQDDGL+gaPIt0DqMQxSOs3TVyW/VCCY7eqcDiNR8Ng9rJfYrtQBRQWawqQqZ+/hAQ1os3TWkxlhTst7Gpp1o3OLElwBAnO8ej02OwdxjNDttTWM28ByV8oW+IXNi1U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rbJ3fk7E; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rbJ3fk7E" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45a1b0d221aso8918155e9.3 for ; Fri, 22 Aug 2025 01:42:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755852168; x=1756456968; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DDkpJcAPlE9LIu6Ir4cHL/5Mh0mGhXKda2sSeyuNZGU=; b=rbJ3fk7EEesZMyWmDdBwsuMINSteB4sysS9TqU6xmDCjVFkr6vX9Z2wdOllC50AddP +z87TwKSixwzuRSl4kWZWlwTQM+TpjyAx6d4IAqKWSv2De0zbEKiIYtN/hHWyIMbejOh HtV2vHcuIUyO8it0vAvUH5Jmd8VurKWmzz6HPvzoM3JU65aTbVbJdgN+wQBQEVK/3fXS LBsNNFEU6iGtg5hsXI6SHX6Ttz8h3ziQ9Irkq15zQiWUqqG5ow5D9KNTEStRrKQ2kg2f mpN01qEOzwkE+ut1oURpor0L9nYD/OCFazKAK6pU4T5qMwlUDiAAGltcjVOrrQ5J1P/m +c8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755852168; x=1756456968; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DDkpJcAPlE9LIu6Ir4cHL/5Mh0mGhXKda2sSeyuNZGU=; b=H8B20TPESShw+pCxVA7MmtiI+V2gPlA6MdFYgLD4oQK2Lq0CTzNT/fIdrrIkOUzIO5 nNaLbmNxeLFUAr+ib6iMymIgLqeATF8Y0hp2wEx3nLfrYyz4xx+j5N5BeHDfq0rHmXoJ ScY8W+hvum0NJG3gW3gt6BAg0YZH6YEaKr6dppr9DYu/669Sw+uqSBXNMgIcSeK1+bdb e/1eAySERzr9rK4nTL7lnKCeco+1wN7gJKdiQwSNmofj3Unj7cLhJTPitDapYSTuwjJQ F7vy088xh2mqZkvnujTiW/QzBXIuswMFRg/mmiycEhhYsJxLbjLMZMgbTBXcc3FIf4Ck Ln3g== X-Forwarded-Encrypted: i=1; AJvYcCWDT6K4s1THgRZX59EywkJDBDWGGWotLIQ/xANhtwQPR1nU6qTYh2skZUGQnyYcPIqQPMdr8JVDZ0cO9H8=@vger.kernel.org X-Gm-Message-State: AOJu0Yy7iNb1eGadlNyAKA5SR+HmTrWx2tCScyjmnlERAuuIRJJ5vdJ9 nsm9S630wAN2Qes/Na23O8O2nqgurN+PyLHDynfUwS5vaUQFN8v3u3a59jOPRBMq8Exd6xqomf8 P5ZSgPMxw/sjNmEMwdw== X-Google-Smtp-Source: AGHT+IHieDBnpYGPhS67rf2immtvBi64zMak8Dg1tvpotMZwHEiKosyrEMG2JuFJ23cpjKzqAfYVaIKwsvo9eWg= X-Received: from wmti4.prod.google.com ([2002:a05:600c:8b84:b0:459:8c48:6c34]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fcd:b0:456:133f:a02d with SMTP id 5b1f17b1804b1-45b517cfe71mr20458965e9.17.1755852168517; Fri, 22 Aug 2025 01:42:48 -0700 (PDT) Date: Fri, 22 Aug 2025 08:42:32 +0000 In-Reply-To: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8638; i=aliceryhl@google.com; h=from:subject:message-id; bh=in5mD66dF6MWhJFhTOWar9Vd/EkPs7Wos8Q4bTZR/Uw=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoqC2F22YR29+Ubnecs5fZRxU0+A+AJdwXcwKsQ eZ0iB4pWlmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaKgthQAKCRAEWL7uWMY5 RlGdD/kBn5yX8w7kpqATUgaD9ysh5C16EdeBS8XFLIF8wArSA0+fmcK/8Y22neUFqc1llH/H0nX 7yp3G7ZkR/tMxusJEUoinP+wnr+zOX1dxi+hvcpwwE9agyX1DWQOrNotk/UFeZaXjE8Bt/ZTO8N xxRu49PsgOjfj+I3SS/V8DXOkmC//03VLf5RqPaouORGQ58PaoWzB+2ZDGDi9S9H46goqXYTqHE jsZGpJHc67/VZdX1/+IWxt9iGRgXQGeTId8vtzj7LfdeCkbmvZDCqF34pNTaesvxAHWKl6PlQ6m 5Jk/+/8jxp7yI2kqO6vBEVJefeGaxW2F1/M/PbiTPthCBKLI5EtJcsg/BUFgv236pWNW2V15x09 jrFX0gqqkFMDIULTHGTEAcGKkRbEWEr2PF3vZHmYitP7TWABpEpDgSJDaswL+I0Gu4abj8tIi39 xfG61HCKxcTZeYsVnunAIXQV0rlgBEjrgtvu3odywpdN0vFRuY2YAgu/tmJDxYzTVbV4qxfDWuR KUKJHl6cVouQgY43rp/Y/VDtthw87yu69hYoJ2ngXCDSTPIXO+cKOGHCCwBEguIuWv1DveiJB3D WYb5aVhf4cM0gnFJZbKkvNhC70nk5ZMVul7MS0PnzpHPXmuKJMaWvYs2CAPwFMsapP/r4BxSvE3 3vkAJGdDn1lXmiQ== X-Mailer: b4 0.14.2 Message-ID: <20250822-iov-iter-v5-1-6ce4819c2977@google.com> Subject: [PATCH v5 1/5] rust: iov: add iov_iter abstractions for ITER_SOURCE From: Alice Ryhl To: Greg Kroah-Hartman , Alexander Viro , Arnd Bergmann , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Matthew Maurer , Lee Jones , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Benno Lossin Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds abstractions for the iov_iter type in the case where data_source is ITER_SOURCE. This will make Rust implementations of fops->write_iter possible. This series only has support for using existing IO vectors created by C code. Additional abstractions will be needed to support the creation of IO vectors in Rust code. These abstractions make the assumption that `struct iov_iter` does not have internal self-references, which implies that it is valid to move it between different local variables. Reviewed-by: Andreas Hindborg Reviewed-by: Danilo Krummrich Signed-off-by: Alice Ryhl --- rust/kernel/iov.rs | 171 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/lib.rs | 1 + 2 files changed, 172 insertions(+) diff --git a/rust/kernel/iov.rs b/rust/kernel/iov.rs new file mode 100644 index 0000000000000000000000000000000000000000..01f4b90ff8b494f0089cb756d6f= 64d34966c4b7d --- /dev/null +++ b/rust/kernel/iov.rs @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Google LLC. + +//! IO vectors. +//! +//! C headers: [`include/linux/iov_iter.h`](srctree/include/linux/iov_iter= .h), +//! [`include/linux/uio.h`](srctree/include/linux/uio.h) + +use crate::{ + alloc::{Allocator, Flags}, + bindings, + prelude::*, + types::Opaque, +}; +use core::{marker::PhantomData, mem::MaybeUninit, ptr, slice}; + +const ITER_SOURCE: bool =3D bindings::ITER_SOURCE !=3D 0; + +/// An IO vector that acts as a source of data. +/// +/// The data may come from many different sources. This includes both thin= gs in kernel-space and +/// reading from userspace. It's not necessarily the case that the data so= urce is immutable, so +/// rewinding the IO vector to read the same data twice is not guaranteed = to result in the same +/// bytes. It's also possible that the data source is mapped in a thread-l= ocal manner using e.g. +/// `kmap_local_page()`, so this type is not `Send` to ensure that the map= ping is read from the +/// right context in that scenario. +/// +/// # Invariants +/// +/// Must hold a valid `struct iov_iter` with `data_source` set to `ITER_SO= URCE`. For the duration +/// of `'data`, it must be safe to read from this IO vector using the stan= dard C methods for this +/// purpose. +#[repr(transparent)] +pub struct IovIterSource<'data> { + iov: Opaque, + /// Represent to the type system that this value contains a pointer to= readable data it does + /// not own. + _source: PhantomData<&'data [u8]>, +} + +impl<'data> IovIterSource<'data> { + /// Obtain an `IovIterSource` from a raw pointer. + /// + /// # Safety + /// + /// * The referenced `struct iov_iter` must be valid and must only be = accessed through the + /// returned reference for the duration of `'iov`. + /// * The referenced `struct iov_iter` must have `data_source` set to = `ITER_SOURCE`. + /// * For the duration of `'data`, it must be safe to read from this I= O vector using the + /// standard C methods for this purpose. + #[track_caller] + #[inline] + pub unsafe fn from_raw<'iov>(ptr: *mut bindings::iov_iter) -> &'iov mu= t IovIterSource<'data> { + // SAFETY: The caller ensures that `ptr` is valid. + let data_source =3D unsafe { (*ptr).data_source }; + assert_eq!(data_source, ITER_SOURCE); + + // SAFETY: The caller ensures the type invariants for the right du= rations, and + // `IovIterSource` is layout compatible with `struct iov_iter`. + unsafe { &mut *ptr.cast::>() } + } + + /// Access this as a raw `struct iov_iter`. + #[inline] + pub fn as_raw(&mut self) -> *mut bindings::iov_iter { + self.iov.get() + } + + /// Returns the number of bytes available in this IO vector. + /// + /// Note that this may overestimate the number of bytes. For example, = reading from userspace + /// memory could fail with `EFAULT`, which will be treated as the end = of the IO vector. + #[inline] + pub fn len(&self) -> usize { + // SAFETY: We have shared access to this IO vector, so we can read= its `count` field. + unsafe { + (*self.iov.get()) + .__bindgen_anon_1 + .__bindgen_anon_1 + .as_ref() + .count + } + } + + /// Returns whether there are any bytes left in this IO vector. + /// + /// This may return `true` even if there are no more bytes available. = For example, reading from + /// userspace memory could fail with `EFAULT`, which will be treated a= s the end of the IO vector. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() =3D=3D 0 + } + + /// Advance this IO vector by `bytes` bytes. + /// + /// If `bytes` is larger than the size of this IO vector, it is advanc= ed to the end. + #[inline] + pub fn advance(&mut self, bytes: usize) { + // SAFETY: By the type invariants, `self.iov` is a valid IO vector. + unsafe { bindings::iov_iter_advance(self.as_raw(), bytes) }; + } + + /// Advance this IO vector backwards by `bytes` bytes. + /// + /// # Safety + /// + /// The IO vector must not be reverted to before its beginning. + #[inline] + pub unsafe fn revert(&mut self, bytes: usize) { + // SAFETY: By the type invariants, `self.iov` is a valid IO vector= , and the caller + // ensures that `bytes` is in bounds. + unsafe { bindings::iov_iter_revert(self.as_raw(), bytes) }; + } + + /// Read data from this IO vector. + /// + /// Returns the number of bytes that have been copied. + #[inline] + pub fn copy_from_iter(&mut self, out: &mut [u8]) -> usize { + // SAFETY: `Self::copy_from_iter_raw` guarantees that it will not = write any uninitialized + // bytes in the provided buffer, so `out` is still a valid `u8` sl= ice after this call. + let out =3D unsafe { &mut *(ptr::from_mut(out) as *mut [MaybeUnini= t]) }; + + self.copy_from_iter_raw(out).len() + } + + /// Read data from this IO vector and append it to a vector. + /// + /// Returns the number of bytes that have been copied. + #[inline] + pub fn copy_from_iter_vec( + &mut self, + out: &mut Vec, + flags: Flags, + ) -> Result { + out.reserve(self.len(), flags)?; + let len =3D self.copy_from_iter_raw(out.spare_capacity_mut()).len(= ); + // SAFETY: + // - `len` is the length of a subslice of the spare capacity, so `= len` is at most the + // length of the spare capacity. + // - `Self::copy_from_iter_raw` guarantees that the first `len` by= tes of the spare capacity + // have been initialized. + unsafe { out.inc_len(len) }; + Ok(len) + } + + /// Read data from this IO vector into potentially uninitialized memor= y. + /// + /// Returns the sub-slice of the output that has been initialized. If = the returned slice is + /// shorter than the input buffer, then the entire IO vector has been = read. + /// + /// This will never write uninitialized bytes to the provided buffer. + #[inline] + pub fn copy_from_iter_raw(&mut self, out: &mut [MaybeUninit]) -> &= mut [u8] { + let capacity =3D out.len(); + let out =3D out.as_mut_ptr().cast::(); + + // GUARANTEES: The C API guarantees that it does not write uniniti= alized bytes to the + // provided buffer. + // SAFETY: + // * By the type invariants, it is still valid to read from this I= O vector. + // * `out` is valid for writing for `capacity` bytes because it co= mes from a slice of + // that length. + let len =3D unsafe { bindings::_copy_from_iter(out.cast(), capacit= y, self.as_raw()) }; + + // SAFETY: The underlying C api guarantees that initialized bytes = have been written to the + // first `len` bytes of the spare capacity. + unsafe { slice::from_raw_parts_mut(out, len) } + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index ed53169e795c0badf548025a57f946fa18bc73e3..99dbb7b2812e018ac4598948781= 6ce020f38aa61 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -92,6 +92,7 @@ pub mod init; pub mod io; pub mod ioctl; +pub mod iov; pub mod jump_label; #[cfg(CONFIG_KUNIT)] pub mod kunit; --=20 2.51.0.rc2.233.g662b1ed5c5-goog From nobody Fri Oct 3 23:04:07 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 3C8AA2EBDF8 for ; Fri, 22 Aug 2025 08:42:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852173; cv=none; b=plelp8I4tgAYCWesJFG9L3SseMFiasBzHYVW+IvGYTz6Kjekx55r6N5fqv3GXhSoughzgQVfr/o3gjFjGib2LJgp6hA6PqFmea5w8zEoZtTbEWr9zgjq25yF1e1ffCKqbfMxQ2Js6FqpDyM5gHO5H0Bz3DQ3mK47M8aYFmcb2lA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852173; c=relaxed/simple; bh=ODBIitTzxUXBO94d3vvMB/FAJjjO9QEhgsDUttvZtao=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZheWMl+Qa1C8Y5hgViOKp6UNxYGktl9Q5zlO0ns0QzR1EJkGKeqHGCbxobV5XNqQFncwB/HIpWWyEOMOPtxrpA+unfraeyBcFI4fjBihLMeqwjLL4xQnHC0nY4duAiI/64YGGbbHlOYeKTrAUsbW+D/cwKQYoapEMNIPsfeeEFY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=js8R/EZl; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="js8R/EZl" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45a1b05a59cso13126555e9.1 for ; Fri, 22 Aug 2025 01:42:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755852169; x=1756456969; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cW8wBssRteKh9TtGxIze/nGUMED/N5ojfLHAnK9eV2g=; b=js8R/EZliGbfWbuPVaYf98qy56baqjD+sBbr61lwHBjhjdTjQTJ9BLMNRjuOHbOAp9 jO00455bM3ED8wywsHp8WN5M4bIr8j4iAa61JFS5/BPJQ2bmSneG3cKUw7JLskHk3yh0 dzc/tpM0Dwv8TNsC88BFd2kIOt0RtMZ6eS/55x8xanlAa9AcTzLeTs2cPSTjgicaUNvy mo3IoJR3vs3ewn6bymZof1u99TSBPsDyL2OKiAyMttEzy/aiVOzU6/wjSA9XwKxHGDqx 8IKhEGt88OE5GOPvM/VfIRdo+BdidhWu3pAXBw7e1B8LAokOnfvgEBCFulX08Ibuw/eu QSRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755852169; x=1756456969; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cW8wBssRteKh9TtGxIze/nGUMED/N5ojfLHAnK9eV2g=; b=lwKpfD8KcGrcC5M1ehExv48u+GHhEIKJ3kDf2z4agsmLBVA5ZIT+siTAy24uHRm2Xi K7ILmNHn3VQnZO5mfRMWxKGEQ3yWCPzni3e+fvbV8JnNHjgWmREORO/Si8cYaY41QMTQ 0MzA1hwareyhA4N1PjFj2nmaVJ+jl641fQOjjmA0NtflIjJwJVTeuum1gzk5ZWqygNwE BctD/JEuKh+DRjht/qSstAjCmpgbUHP9SHLwzjU8bksa5SWV984tIgligeqRVvHoor/m PEoL2gDLgQxkmpruW3bqtOH4mBisdR6MGfrecw4avQCUlKfqlIJEvhQ36AOol9ZDSKjo s50A== X-Forwarded-Encrypted: i=1; AJvYcCWPH/XNaCiOrHYB6CObejK5gzOjg7ntm3ftv+93/q3eDBC4TZ8kKDqeF+a9+GbIqOtvw/cdy0m2B5zrp7g=@vger.kernel.org X-Gm-Message-State: AOJu0Yzh/7MSy5wEvfdJ8WfULFjnxAgOSE4A0iK6t3kvMFFBYC7tKqZP LPQa4dDsM6yGRcBoHZGKM6x/ZOTxIlyJEV2kBJhMXXPpvK6b+MhFXh4EfuAWv+OtVvPn5XclK5s 4Nx8HzxzmN7f6f2zG8A== X-Google-Smtp-Source: AGHT+IFrQckRHYDsKo694J6+s5vHo+fM7l4unH3LDECydbGtTUfVArVEqGHPJsJKikDK/LWckHfj2vsMKg5fvGY= X-Received: from wmbhj9.prod.google.com ([2002:a05:600c:5289:b0:45a:29a8:a9e1]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4705:b0:458:bf0a:6061 with SMTP id 5b1f17b1804b1-45b517d49d4mr17113005e9.24.1755852169617; Fri, 22 Aug 2025 01:42:49 -0700 (PDT) Date: Fri, 22 Aug 2025 08:42:33 +0000 In-Reply-To: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7578; i=aliceryhl@google.com; h=from:subject:message-id; bh=ODBIitTzxUXBO94d3vvMB/FAJjjO9QEhgsDUttvZtao=; b=kA0DAAoBBFi+7ljGOUYByyZiAGioLYWia6Humescx8Z7qQ5tXLsVR836qZwZTqB1jNauY2fUh YkCMwQAAQoAHRYhBIOSopRNZAcK8Ui9sgRYvu5YxjlGBQJoqC2FAAoJEARYvu5YxjlGHagP/2pM SHC18QWiyU1W0CnnbeOhCkbLq7Dc8kmWg9tVOqMP2/LROT6a98BkkiSOA1vgnZQf1PpP1eoNnrA ADtCA2rR7WeWHWORokmsnvwm4M4heTbTmAvYUgmRmiR1Fvv7hmsLmT93aIZ0HIpF+u5tQ7wy02Z 6Tq3QiS4NK1t1SipWlY4P2XhS2o4dMW2JHdsG7znI56Mm0pBr2WJ1z1G6Ft95H9V0om/XgQAwfv ZKFCogeNAm+fB2O8uJLT2nwjYn704DETlLjcauE6xGHM6xTdJ7khdTZRo9t8EjjbMtHngf8+Tnc WAbJiBky43aJnPlI8fiaDrySQVmUsxpkRdF1efRUD4mauXTvL05+E6URO/6Asd3HU+HK+A5E7Vu jGdTLmcvp9hU43iISum5k4GlzaExNvmEQP7TQoC8jGHzXfdKsH41THXGKODhRkPRBqYK4hBTf9s kg2b3KCN62qAC/J053jjLwr25JtpbXQd6vOUzPQHtnL7fH+IO6jz5JebtbLkSCr1xEBS9/c6+Ki a+s0YHcg03w6ix74g5ch3rIY2GeKxNsA9AMz9nZhS8Qsye28SwuM1PAM+ZesqAHDQDB3BRjKr2e zRbTlxF8XaFgs5iOAP002Z8YACwEVmSz/d1ujPi/Soz3TtuOi9Rl6pShBrRZlK3lPiZHiWt47Ro DwH2F X-Mailer: b4 0.14.2 Message-ID: <20250822-iov-iter-v5-2-6ce4819c2977@google.com> Subject: [PATCH v5 2/5] rust: iov: add iov_iter abstractions for ITER_DEST From: Alice Ryhl To: Greg Kroah-Hartman , Alexander Viro , Arnd Bergmann , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Matthew Maurer , Lee Jones , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Benno Lossin Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds abstractions for the iov_iter type in the case where data_source is ITER_DEST. This will make Rust implementations of fops->read_iter possible. This series only has support for using existing IO vectors created by C code. Additional abstractions will be needed to support the creation of IO vectors in Rust code. These abstractions make the assumption that `struct iov_iter` does not have internal self-references, which implies that it is valid to move it between different local variables. This patch adds an IovIterDest struct that is very similar to the IovIterSource from the previous patch. However, as the methods on the two structs have very little overlap (just getting the length and advance/revert), I do not think it is worth it to try and deduplicate this logic. Reviewed-by: Andreas Hindborg Reviewed-by: Danilo Krummrich Signed-off-by: Alice Ryhl --- rust/kernel/iov.rs | 143 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 143 insertions(+) diff --git a/rust/kernel/iov.rs b/rust/kernel/iov.rs index 01f4b90ff8b494f0089cb756d6f64d34966c4b7d..43bae8923c4611927d3bfe747fd= ef69a1f73529c 100644 --- a/rust/kernel/iov.rs +++ b/rust/kernel/iov.rs @@ -16,6 +16,15 @@ use core::{marker::PhantomData, mem::MaybeUninit, ptr, slice}; =20 const ITER_SOURCE: bool =3D bindings::ITER_SOURCE !=3D 0; +const ITER_DEST: bool =3D bindings::ITER_DEST !=3D 0; + +// Compile-time assertion for the above constants. +const _: () =3D { + build_assert!( + ITER_SOURCE !=3D ITER_DEST, + "ITER_DEST and ITER_SOURCE should be different." + ); +}; =20 /// An IO vector that acts as a source of data. /// @@ -169,3 +178,137 @@ pub fn copy_from_iter_raw(&mut self, out: &mut [Maybe= Uninit]) -> &mut [u8] { unsafe { slice::from_raw_parts_mut(out, len) } } } + +/// An IO vector that acts as a destination for data. +/// +/// IO vectors support many different types of destinations. This includes= both buffers in +/// kernel-space and writing to userspace. It's possible that the destinat= ion buffer is mapped in a +/// thread-local manner using e.g. `kmap_local_page()`, so this type is no= t `Send` to ensure that +/// the mapping is written to the right context in that scenario. +/// +/// # Invariants +/// +/// Must hold a valid `struct iov_iter` with `data_source` set to `ITER_DE= ST`. For the duration of +/// `'data`, it must be safe to write to this IO vector using the standard= C methods for this +/// purpose. +#[repr(transparent)] +pub struct IovIterDest<'data> { + iov: Opaque, + /// Represent to the type system that this value contains a pointer to= writable data it does + /// not own. + _source: PhantomData<&'data mut [u8]>, +} + +impl<'data> IovIterDest<'data> { + /// Obtain an `IovIterDest` from a raw pointer. + /// + /// # Safety + /// + /// * The referenced `struct iov_iter` must be valid and must only be = accessed through the + /// returned reference for the duration of `'iov`. + /// * The referenced `struct iov_iter` must have `data_source` set to = `ITER_DEST`. + /// * For the duration of `'data`, it must be safe to write to this IO= vector using the + /// standard C methods for this purpose. + #[track_caller] + #[inline] + pub unsafe fn from_raw<'iov>(ptr: *mut bindings::iov_iter) -> &'iov mu= t IovIterDest<'data> { + // SAFETY: The caller ensures that `ptr` is valid. + let data_source =3D unsafe { (*ptr).data_source }; + assert_eq!(data_source, ITER_DEST); + + // SAFETY: The caller ensures the type invariants for the right du= rations, and + // `IovIterSource` is layout compatible with `struct iov_iter`. + unsafe { &mut *ptr.cast::>() } + } + + /// Access this as a raw `struct iov_iter`. + #[inline] + pub fn as_raw(&mut self) -> *mut bindings::iov_iter { + self.iov.get() + } + + /// Returns the number of bytes available in this IO vector. + /// + /// Note that this may overestimate the number of bytes. For example, = reading from userspace + /// memory could fail with EFAULT, which will be treated as the end of= the IO vector. + #[inline] + pub fn len(&self) -> usize { + // SAFETY: We have shared access to this IO vector, so we can read= its `count` field. + unsafe { + (*self.iov.get()) + .__bindgen_anon_1 + .__bindgen_anon_1 + .as_ref() + .count + } + } + + /// Returns whether there are any bytes left in this IO vector. + /// + /// This may return `true` even if there are no more bytes available. = For example, reading from + /// userspace memory could fail with EFAULT, which will be treated as = the end of the IO vector. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() =3D=3D 0 + } + + /// Advance this IO vector by `bytes` bytes. + /// + /// If `bytes` is larger than the size of this IO vector, it is advanc= ed to the end. + #[inline] + pub fn advance(&mut self, bytes: usize) { + // SAFETY: By the type invariants, `self.iov` is a valid IO vector. + unsafe { bindings::iov_iter_advance(self.as_raw(), bytes) }; + } + + /// Advance this IO vector backwards by `bytes` bytes. + /// + /// # Safety + /// + /// The IO vector must not be reverted to before its beginning. + #[inline] + pub unsafe fn revert(&mut self, bytes: usize) { + // SAFETY: By the type invariants, `self.iov` is a valid IO vector= , and the caller + // ensures that `bytes` is in bounds. + unsafe { bindings::iov_iter_revert(self.as_raw(), bytes) }; + } + + /// Write data to this IO vector. + /// + /// Returns the number of bytes that were written. If this is shorter = than the provided slice, + /// then no more bytes can be written. + #[inline] + pub fn copy_to_iter(&mut self, input: &[u8]) -> usize { + // SAFETY: + // * By the type invariants, it is still valid to write to this IO= vector. + // * `input` is valid for `input.len()` bytes. + unsafe { bindings::_copy_to_iter(input.as_ptr().cast(), input.len(= ), self.as_raw()) } + } + + /// Utility for implementing `read_iter` given the full contents of th= e file. + /// + /// The full contents of the file being read from is represented by `c= ontents`. This call will + /// write the appropriate sub-slice of `contents` and update the file = position in `ppos` so + /// that the file will appear to contain `contents` even if takes mult= iple reads to read the + /// entire file. + #[inline] + pub fn simple_read_from_buffer(&mut self, ppos: &mut i64, contents: &[= u8]) -> Result { + if *ppos < 0 { + return Err(EINVAL); + } + let Ok(pos) =3D usize::try_from(*ppos) else { + return Ok(0); + }; + if pos >=3D contents.len() { + return Ok(0); + } + + // BOUNDS: We just checked that `pos < contents.len()` above. + let num_written =3D self.copy_to_iter(&contents[pos..]); + + // OVERFLOW: `pos+num_written <=3D contents.len() <=3D isize::MAX = <=3D i64::MAX`. + *ppos =3D (pos + num_written) as i64; + + Ok(num_written) + } +} --=20 2.51.0.rc2.233.g662b1ed5c5-goog From nobody Fri Oct 3 23:04:07 2025 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 78B612ECD38 for ; Fri, 22 Aug 2025 08:42:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852175; cv=none; b=TSwX/h/18IGsdqBQJ6pozBLDmTSJTaxZc9gcGgKGWcEAt77eIHAuve8LTB7a4ZKorYS9/g1+Z20/xfW0rQMoJeVxaf900ckQWGUb08mr5wwmTv0MiKEIAy44uKAESEyzoKnfEcLuq3QgTqxPvTFpw39J/igaz30zzo1/kbDze60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852175; c=relaxed/simple; bh=pEFhG0haAU3GTGryN2WieVcKPQ715Oa917yCHNmiuvA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uv81ABKyrXXlPJwH7KFDbIt8kgXIzJaMqmIkJN/v8rXZMO8NALxLtGHLffD+02MncdguRrKtCAt/hBPUCQ13Oqj9fipgx3FcpgF0BdM934fhwm3pDr3BAcjvC4Kveh3FcTZsQrAWsgYDAwLtY7OqdeHDa4tFHgYAXjcf4TFxWkA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WHGEX1jO; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WHGEX1jO" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45a1b0ccb6cso9725595e9.3 for ; Fri, 22 Aug 2025 01:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755852172; x=1756456972; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Qjj30volqfs6w7L9vkmy/27FL2MqFj9qU1x6ao4EmPI=; b=WHGEX1jOZxZtkELQiJrt61F8xZKWglR60cTGY4ggHiamOVjhPRLuZqkdrrEYA3T6hq DVSduN8arok6ZTD0XqFmQqFHOSV7YL7gxVbknBRzSOf276ljOBceW76E/tYeEk4Pm/av Iz5eJXOVxDfihWHp2R71udM2Z7lVou3ofIpRjF8QaeoTy8cbFIQv2A4jt2olRCgN4z23 xMSpPa4TXaEr5tpVfGjqBqlYUFEaTIAqJtTh/6DmNHDzlUQpSJiuH1eHmqNaWT0RZrFM XSz/4p0Te+ZCirM2s1xudmkm9xNROdI+7B1Xf3vaHqjo9QcezAvWPqHFfYAs9qLJaNWq f9IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755852172; x=1756456972; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Qjj30volqfs6w7L9vkmy/27FL2MqFj9qU1x6ao4EmPI=; b=UY/LhnCMJfbf8bZTCzGPL8YlaFnVLB5yuEUfUBBB476RBbKCZmlU6zqzQGMdkyFHFD /cxq93eZyPMdbuf7GgNMYgzmcPDptLXMAdKuZuor8V1KoV6X1wk17h5EIo0NaVyMn0MI pnPuwIYN8+lwBTfs/XYfzgcGgPQ11vva9YBufRkRyHpLH6HM8OHIS26SPk0P6PWPvs0p cjzvQ8AuQrA3jg4Wn5gaYO8FpZbR4oYSAzZJ5WkyBWv0bYNJgg8Nb8hUOKeEY3oH4jQC 7bbBVZNMq1GFoc7B8DxM+HGYp18lHWW6FlviR3jMchiaRW60psyEenz8FIBrb+EXSRkR 3TWg== X-Forwarded-Encrypted: i=1; AJvYcCVBnw6GPtgl2YgzbwbbYwHFuTF1olsyDArt09YSlX7uJ1Z6dUYOscXpFBBYV01N45yUgwHLi7W4UtVwNzY=@vger.kernel.org X-Gm-Message-State: AOJu0YwLxXXLqKwGHDj7Ihcf5eJR0+YQiThbLoA8932GFbloW31ZyWjR vPZcc8pX+VjBIQa8SoEd2H+L+bid1wG8PX/dRuv5zKn6aTFIgVPSvrdkYyHx64DRpHcqZ/ZMasX AYO/AR3DUqX4GFy+VRA== X-Google-Smtp-Source: AGHT+IHxHQFqWDULQYicIlutHzD9qOw+A69Wxr2UrhdnZx18qXPqIyxmWSW/+I752DN8ZrWOOX9rLabeqfTz+2s= X-Received: from wmox22.prod.google.com ([2002:a05:600c:1796:b0:459:dbaa:93aa]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:314c:b0:45b:43cc:e55a with SMTP id 5b1f17b1804b1-45b517cca69mr11960315e9.37.1755852171849; Fri, 22 Aug 2025 01:42:51 -0700 (PDT) Date: Fri, 22 Aug 2025 08:42:34 +0000 In-Reply-To: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3559; i=aliceryhl@google.com; h=from:subject:message-id; bh=pEFhG0haAU3GTGryN2WieVcKPQ715Oa917yCHNmiuvA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoqC2FrtlThqcOSRNsIBpt3SBxjJlChXDoE5MX/ Z0qjh7A8iOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaKgthQAKCRAEWL7uWMY5 Rr9XD/4hIWjTh4NRA9TG7NZx0jJFcqPCUg2D2dCla9YcILf6jp8Oja4o3k/O1mTB5QXzgxlZCxC HGM0Uw0WLbwtciMXD+Z/yJBCN7WutdGdsw7xMlW2TYa3vmLuE0jEM20LcnGg5OrICQW8JvlQ4GG I6sBoNu/D9G8X/QW9c7+U6Js4iG8MwVcSEjb2vJ9fOFuHh/+4gnIL9gA4VtOftDZg5X1K+XqH83 GSL6kfTiDC1o5ZkJlOfyQ59s9vj73Yy0oXpmstIX5CRkY8xrQZStLyeV/dORliKUBcgzk4h1f/8 TNe+m+sUUoHfpFXRv6SfzmqqRZsaT48FJZn94mNQ335CC+Pnz2pE7ypb7JM+L+JSOiOdpc2VE1D 1sgpNcX0OClBZ54/f7f0sSlymGL4nZYi8OFWCZRhBNTov3610Yk22mjG/dDqE7lJiFLZ2JcTTr1 DgmkF4c1GVvDJERN29KIXZkStlX5budcF3y9aG06O5jjtK4hE3EDqnpLbU3lwhmtagdCslqGFDT GSHidoSQFxp02+DiBbXiK2wsefcDSFNxT5k2+n0AomBQ8h1KJY4YYl6/OTs9x0/7YsCbbELIByJ dvjgp8LhBgS33XJsx2IBZx4n4GicXbBiwqxQSRLZB2T1Vyvu0Xig8v+GOE7i39/4hUiP3PUlJly sKR+4GFmCOO/NhA== X-Mailer: b4 0.14.2 Message-ID: <20250822-iov-iter-v5-3-6ce4819c2977@google.com> Subject: [PATCH v5 3/5] rust: fs: add Kiocb struct From: Alice Ryhl To: Greg Kroah-Hartman , Alexander Viro , Arnd Bergmann , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Matthew Maurer , Lee Jones , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Benno Lossin , Christian Brauner Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This adds a very simple Kiocb struct that lets you access the inner file's private data and the file position. For now, nothing else is supported. Cc: Christian Brauner Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl --- rust/kernel/fs.rs | 3 +++ rust/kernel/fs/kiocb.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 71 insertions(+) diff --git a/rust/kernel/fs.rs b/rust/kernel/fs.rs index 0121b38c59e63d01a89f22c8ef6983ef5c3234de..6ba6bdf143cb991c6e78215178e= b585260215da0 100644 --- a/rust/kernel/fs.rs +++ b/rust/kernel/fs.rs @@ -6,3 +6,6 @@ =20 pub mod file; pub use self::file::{File, LocalFile}; + +mod kiocb; +pub use self::kiocb::Kiocb; diff --git a/rust/kernel/fs/kiocb.rs b/rust/kernel/fs/kiocb.rs new file mode 100644 index 0000000000000000000000000000000000000000..84c936cd69b0e9b490d54c87d8c= 7279b27d4476a --- /dev/null +++ b/rust/kernel/fs/kiocb.rs @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Kernel IO callbacks. +//! +//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h) + +use core::marker::PhantomData; +use core::ptr::NonNull; +use kernel::types::ForeignOwnable; + +/// Wrapper for the kernel's `struct kiocb`. +/// +/// Currently this abstractions is incomplete and is essentially just a tu= ple containing a +/// reference to a file and a file position. +/// +/// The type `T` represents the filesystem or driver specific data associa= ted with the file. +/// +/// # Invariants +/// +/// `inner` points at a valid `struct kiocb` whose file has the type `T` a= s its private data. +pub struct Kiocb<'a, T> { + inner: NonNull, + _phantom: PhantomData<&'a T>, +} + +impl<'a, T: ForeignOwnable> Kiocb<'a, T> { + /// Create a `Kiocb` from a raw pointer. + /// + /// # Safety + /// + /// The pointer must reference a valid `struct kiocb` for the duration= of `'a`. The private + /// data of the file must be `T`. + pub unsafe fn from_raw(kiocb: *mut bindings::kiocb) -> Self { + Self { + // SAFETY: If a pointer is valid it is not null. + inner: unsafe { NonNull::new_unchecked(kiocb) }, + _phantom: PhantomData, + } + } + + /// Access the underlying `struct kiocb` directly. + pub fn as_raw(&self) -> *mut bindings::kiocb { + self.inner.as_ptr() + } + + /// Get the filesystem or driver specific data associated with the fil= e. + pub fn file(&self) -> ::Borrowed<'a> { + // SAFETY: We have shared access to this kiocb and hence the under= lying file, so we can + // read the file's private data. + let private =3D unsafe { (*(*self.as_raw()).ki_filp).private_data = }; + // SAFETY: The kiocb has shared access to the private data. + unsafe { ::borrow(private) } + } + + /// Gets the current value of `ki_pos`. + pub fn ki_pos(&self) -> i64 { + // SAFETY: We have shared access to the kiocb, so we can read its = `ki_pos` field. + unsafe { (*self.as_raw()).ki_pos } + } + + /// Gets a mutable reference to the `ki_pos` field. + pub fn ki_pos_mut(&mut self) -> &mut i64 { + // SAFETY: We have exclusive access to the kiocb, so we can write = to `ki_pos`. + unsafe { &mut (*self.as_raw()).ki_pos } + } +} --=20 2.51.0.rc2.233.g662b1ed5c5-goog From nobody Fri Oct 3 23:04:07 2025 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) (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 E6CF92ECEBE for ; Fri, 22 Aug 2025 08:42:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852177; cv=none; b=dXvabPdrJFLYD0WYZ4wX96ZmL3F1eoo1Xu3yuWvBDW6oShRr3wz4g0qt877kyl9RGDRIAw9ncDZWC+NoG9PkF7jSz7o/hAu5FDN7p7OxkM+VaRy2pMXkWGgGY9u14ihila6ZFnGunu+B92tOF8hpW2LOCrU4Zusw6oah7PPszn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852177; c=relaxed/simple; bh=cBnYlOvskfx5sm6Y4IKbX2kYZoyJZJkrtZoMqj5Kt9I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uXv0e1IH7+akMtwhroYadOIrxldiknHmS2wJghaG5fizbIaHt6bt/Z45xs9qVI2bs/qyuQQHmB96OsmfOhUq6oLRFzh3CCCAAT8pgyDeN5GSB+axib/wB8G8Q04t72TRGkFXO6sm2qSdeUgB9n1JXBtYUg1es3yBFIItzIN/cM4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=d3XocRdy; arc=none smtp.client-ip=209.85.218.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="d3XocRdy" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-afcb7a4132eso134539666b.3 for ; Fri, 22 Aug 2025 01:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755852173; x=1756456973; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KLgm1xonn2W8CnafnexlfU+hOmm/BVh9s7sKWTQeHB0=; b=d3XocRdyfysA142wqX+x4RzOQNCCAvBxDhACULcY3JryRCb6Fs0ZmeERgWRCvCuYNf 2kQZ8ubY2dBaP/ZtzNXOr3QdbSoISG7i3kc3RbvHvl84ID3ONId1hLTtnxLFrNFARWBW vpARBOsaF4ZBiate3xzE8m7QZM8VbWvC3bGD9Te3TtA6t6xsstGS7VVZyTUe7Mr47JUv vu0itrx+ZlA0sO0kAFYohak3RG7qHhlRbjdh+9P319EujEIefo/wp0rjg4x+EWsQEldk Ru/HWpsHjuDgDlxHsIObZ2p6qFt6btqgP/Ugio7u9hqBJTsYOYp3sesOkRs2mVXSotl2 qqSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755852173; x=1756456973; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KLgm1xonn2W8CnafnexlfU+hOmm/BVh9s7sKWTQeHB0=; b=uUY71p2R7JYxDOosFa5WHPQIogoYtBdKwL1USNmLVwemtYo8inX4qKKciXTIofYYT6 XTsFvkCH6ZxMLccbBPvjOf/lElAHE0UH9AmWWlpWGuEKH+qQaoPQEZMLy6eouW2GdyyJ /loH1R6PcC1Ma5daGsV8eS490pqZtEG+JEmLCnSOABM5YQ0i8aXswaiX2bmsbkdpsBtC lTCSCd0CBQbLJQ102PToJymBGiec66x3ToVJLbJEjaCa9fP3VwLEu7UYrearC0YNag8s RRCvyDSJF2LS84HvfGiFb57fKkhN1OQQhXkfqAXqvgNdE0G67bJ3LUeAT1jWFhvT9nEu mizg== X-Forwarded-Encrypted: i=1; AJvYcCXXOX+kN8Uiqf5TGV7RqcEcf8iLXcc0dDSvM3SViHC+3VDJRFREuXOeIWt5c7S091Eq3tbfkcuoZFppjb0=@vger.kernel.org X-Gm-Message-State: AOJu0YzCcXvjQ61DFWtAbY2d/tOUXPkK8unK/0goE+W1Yfa/vGysqh3O zdHqhqhxz+ls70mwmD2tsOj+J3Sn/PTdU9WEHFz2Rk/wVwWtYmaKBw+qrGWYFFypjoLNNIMvQ2u PvS0BD+Wy87WuSBxB1Q== X-Google-Smtp-Source: AGHT+IGiC9Ykz4abp/5AXBZeDzha8KmgpOY98sijHceaASfXiYptrI7RlujdHFTa8jXCsWPvXzoudFEja3KmhkM= X-Received: from ejctq7.prod.google.com ([2002:a17:907:c507:b0:af9:2a5f:c194]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:3f17:b0:ad8:85df:865b with SMTP id a640c23a62f3a-afe290443fcmr188578566b.33.1755852173287; Fri, 22 Aug 2025 01:42:53 -0700 (PDT) Date: Fri, 22 Aug 2025 08:42:35 +0000 In-Reply-To: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4089; i=aliceryhl@google.com; h=from:subject:message-id; bh=cBnYlOvskfx5sm6Y4IKbX2kYZoyJZJkrtZoMqj5Kt9I=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoqC2F43Y5g8DOun815uNXvdJQldub+TxdA26Ti Z1UU1EB/z6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaKgthQAKCRAEWL7uWMY5 Rq2QEACoDE1bUPaRQCRZz8fPX5A/0CfEqNAo3Cd5E+dEODUDamxYbUnnhYuGxV+lChHpfJb2es+ UD63WK+l+iidAoCPao57Dx1md35hCRKSVCWv1Tpxh5ITTBn87FKBS1qgpPnUW9hIpXOi+HLyKVW lEkdGtYNw9FO1jcEZ6YM30mGIcmdBGceCf5jTiur5Kt4k+FkU9Ig8gPsKmqdtNtvTPGnHrA1Xji ZFW4zJCnKt4VXUdynwZ0lRpudg/Q6ZVWrBppcp3YkZOa6OaezTvScAYKs+stTKk2zrhZpxMU39n aa+0N+oiyXuAtLCCZW9owpBq1KmKNviASSqxLO8B0SxzxxJEIp+DkyiJpYUuPIiOEt0MUZ1i3YQ n22JtGWLTau9VKXeqzEhpP2OImGS5A8aOhTIxzrEmR1xvuBY6moxYSi/XIhijmAGuwChpWGGEX7 /goEjvuPfCIsaMUMI48w/TcEwC4i2UpbS3OUxGcYXBYexPVjlQVJNPEFZyOKo2TCQgOTkxxgxdJ fC/wm5/u6aFttwkZ1m4Ki9yb+I2nQhKjQERu6JNfB8qXzhExLkUbNEljwfco9K4EpqtUcbPvD97 4adUhaE0HMJivOd9skc5zxWFovAXx7kppIWHPu/hfG+dCTDAz0O9DM/vdibZfpFBn4ypsWwbc04 S5deTcWMDKgBuHg== X-Mailer: b4 0.14.2 Message-ID: <20250822-iov-iter-v5-4-6ce4819c2977@google.com> Subject: [PATCH v5 4/5] rust: miscdevice: Provide additional abstractions for iov_iter and kiocb structures From: Alice Ryhl To: Greg Kroah-Hartman , Alexander Viro , Arnd Bergmann , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Matthew Maurer , Lee Jones , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Benno Lossin Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable These will be used for the read_iter() and write_iter() callbacks, which are now the preferred back-ends for when a user operates on a char device with read() and write() respectively. Co-developed-by: Lee Jones Signed-off-by: Lee Jones Reviewed-by: Andreas Hindborg Signed-off-by: Alice Ryhl --- rust/kernel/miscdevice.rs | 63 +++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 6373fe183b2748162988b45d7ed8f563045d3b4d..35630fc638751c416d009fcf0c0= ffdf0a4da1218 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -13,7 +13,8 @@ device::Device, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, ffi::{c_int, c_long, c_uint, c_ulong}, - fs::File, + fs::{File, Kiocb}, + iov::{IovIterDest, IovIterSource}, mm::virt::VmaNew, prelude::*, seq_file::SeqFile, @@ -141,6 +142,16 @@ fn mmap( build_error!(VTABLE_DEFAULT_ERROR) } =20 + /// Read from this miscdevice. + fn read_iter(_kiocb: Kiocb<'_, Self::Ptr>, _iov: &mut IovIterDest<'_>)= -> Result { + build_error!(VTABLE_DEFAULT_ERROR) + } + + /// Write to this miscdevice. + fn write_iter(_kiocb: Kiocb<'_, Self::Ptr>, _iov: &mut IovIterSource<'= _>) -> Result { + build_error!(VTABLE_DEFAULT_ERROR) + } + /// Handler for ioctls. /// /// The `cmd` argument is usually manipulated using the utilities in [= `kernel::ioctl`]. @@ -245,6 +256,46 @@ impl MiscdeviceVTable { 0 } =20 + /// # Safety + /// + /// `kiocb` must be correspond to a valid file that is associated with= a + /// `MiscDeviceRegistration`. `iter` must be a valid `struct iov_it= er` for writing. + unsafe extern "C" fn read_iter( + kiocb: *mut bindings::kiocb, + iter: *mut bindings::iov_iter, + ) -> isize { + // SAFETY: The caller provides a valid `struct kiocb` associated w= ith a + // `MiscDeviceRegistration` file. + let kiocb =3D unsafe { Kiocb::from_raw(kiocb) }; + // SAFETY: This is a valid `struct iov_iter` for writing. + let iov =3D unsafe { IovIterDest::from_raw(iter) }; + + match T::read_iter(kiocb, iov) { + Ok(res) =3D> res as isize, + Err(err) =3D> err.to_errno() as isize, + } + } + + /// # Safety + /// + /// `kiocb` must be correspond to a valid file that is associated with= a + /// `MiscDeviceRegistration`. `iter` must be a valid `struct iov_it= er` for writing. + unsafe extern "C" fn write_iter( + kiocb: *mut bindings::kiocb, + iter: *mut bindings::iov_iter, + ) -> isize { + // SAFETY: The caller provides a valid `struct kiocb` associated w= ith a + // `MiscDeviceRegistration` file. + let kiocb =3D unsafe { Kiocb::from_raw(kiocb) }; + // SAFETY: This is a valid `struct iov_iter` for reading. + let iov =3D unsafe { IovIterSource::from_raw(iter) }; + + match T::write_iter(kiocb, iov) { + Ok(res) =3D> res as isize, + Err(err) =3D> err.to_errno() as isize, + } + } + /// # Safety /// /// `file` must be a valid file that is associated with a `MiscDeviceR= egistration`. @@ -341,6 +392,16 @@ impl MiscdeviceVTable { open: Some(Self::open), release: Some(Self::release), mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None }, + read_iter: if T::HAS_READ_ITER { + Some(Self::read_iter) + } else { + None + }, + write_iter: if T::HAS_WRITE_ITER { + Some(Self::write_iter) + } else { + None + }, unlocked_ioctl: if T::HAS_IOCTL { Some(Self::ioctl) } else { --=20 2.51.0.rc2.233.g662b1ed5c5-goog From nobody Fri Oct 3 23:04:07 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 B69472ED845 for ; Fri, 22 Aug 2025 08:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852178; cv=none; b=AJJrH/klaSfu4Z1LRe+CryRjXjK7v36KctEUwXPA4Ec3Ojpz3tK5+QwG9I0wcasRPFOQST1nvtqzccWpWRUW0zhPyX2QrlHiW2QhAzHF0f38H+ya3Xg4BLYzPa7MlC/oy/nPV1PP1YFM3hhA29AC0QXBDMeLLPoPO6QC2VJn38U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755852178; c=relaxed/simple; bh=rB/mZF8xBrMCzSbeYYPQ0DaqjXVp8y/1e3Ssslw3iIU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y0yXlIPRa4sJdCY28Dhtj/EAB8o1p/hGKQTmLZ3nRXrrtFs2fvpxOZhQeBdIFfQImkmJEN3efrvRh8+nzvAIO4nbZyEqCxqVzdkKinAw6y5gjqOafE/zip4YsNfZ8P+el+JXz3baEGn7YDfRoimTbCoqF6ssY+BG6+fnLTHbDiE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HlNMgTLq; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HlNMgTLq" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3c68ee43c76so75972f8f.0 for ; Fri, 22 Aug 2025 01:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755852175; x=1756456975; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=f/u4XDf1WVNqmU+JT0FI8dpt88Rxm4l0HCKQQVKeyjc=; b=HlNMgTLqL5Z8G2mmlmwDCkK9KBRyXjStXsfOA81JQVHsuXnNiXFOilnINQr2rZe8hz UqT0d73lzb0L5AjenW2SzNsScJtfylK7YlCUoKc0FlqIpl1c8rerRcvqr7IELurE8tLx YGMbhhy1FI3YxRbqElX6eFcvefbd3enwsM+K1TZr9u0ubPUSIAF6qLk+mk4Ij3cWto+a QHLQkk6T6O3rGlYCJakB2ydG+IiHofAYWYAu3QG9bcE5F4uPd2eDJLsDoJK2hXXA0WGU MVzTVNjo1GN2PtLuomY3iVg4UDOWKubiuejn+QUr15hQ3kJogJEP/ACSQU8o/YO61kQP waeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755852175; x=1756456975; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=f/u4XDf1WVNqmU+JT0FI8dpt88Rxm4l0HCKQQVKeyjc=; b=vkNH5utEoE2Ged/vJrkGiqSxHZmxBcy5k+62Ac813LvNaoa/tqMVHwo3N2dip8NYDz 9HKaRBaj0/e4sCS+cZsQUjwUtDbV5J+IGx4IZd1ldT7wDql2mgMjj3yWEMpENJ7Q+KWH c9Ujsr62jTwBmC7TMcVyc2Yz4TriHye9SJQ81NcoBDFuMd+l649D5oVGDBJw60YkH8vV P/D+ywxT8PTfNiY5b2KbQJosCjWMDcAxD3wtYryVnMYgAaJ2eswark9prZ8T0wnbCRaH 2Emexa91F79sTnwuSEcxlTy9WqVhogt4po833jX1kVJNP+/MvRHWYX9EJOGs3wqx/AQu v6SA== X-Forwarded-Encrypted: i=1; AJvYcCUozsGBDEpNbkbvunqRleE6Tw4IMPLo9/f8SRL+bXsNUpFx742m/tDcnuOuuhole7SQUdD2IySM9KQXetw=@vger.kernel.org X-Gm-Message-State: AOJu0YyKEFDSzYJQ46B+7bTXUm4a4UeOl6wyEC3f3fndtKEcbYaRYiaG Z92uOcwAA1xVqyMLAbKNhoSwTUcsY8DRFD1dQNKxz6qxkwMlQcTH0huNrK0WfYkg+oU+C/Wg5nA ifD5/6ph4N8oNMX8JpA== X-Google-Smtp-Source: AGHT+IFsvYdHV4/lLRj0b7l8g/4es08yFobDgfpUGx3RVdmcuZme238QYK2UJaceJcGeVU7iLpwV8UZBNenTMvo= X-Received: from wmbes27.prod.google.com ([2002:a05:600c:811b:b0:459:db77:fc89]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2485:b0:3b7:664a:8416 with SMTP id ffacd0b85a97d-3c5dae06637mr1497131f8f.23.1755852174560; Fri, 22 Aug 2025 01:42:54 -0700 (PDT) Date: Fri, 22 Aug 2025 08:42:36 +0000 In-Reply-To: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250822-iov-iter-v5-0-6ce4819c2977@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3154; i=aliceryhl@google.com; h=from:subject:message-id; bh=bBziUhAZe2zo8uvOtUQGB4HG8Cb4inbaTy33bNyXEno=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBoqC2GKQ0nWwuudCr7dtkC0pIcAouFTgw+Q0UhT L0+6NKp0XqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaKgthgAKCRAEWL7uWMY5 RqF6D/95Phco70OrE20deLCWG0QdG/eE5+5DqPpoBcT+MJq3+9eOOi4AARuy+Cic2AEwQxy2I4Y Xts0hr+n8ywXlGL5VNdWQ0CcHYyu3GZQsHZ9jYav1NGxUSiPSey9c4CFOMYGXXLL3I9ZNMj89jn CJPsl4w8nNaVgTiELlk90Jv/LCIg6XVCNUl5pvzRQbm3wpMlDheeo4xAGpXuLK5tM2J4C4Sotqz N7bZNG/QPWooMk25aGm7V1gYkeAEVrHy4tzXmsPaQesB9a8UGp0sgaiUf0b+NTqhM6k3fX/G17g vZqmtEgi9nzO2+v2oBatMrDlwbtXpZg5Vqs7rv2l9yMMbe9XwjPR4I/IIUVSrVZWnVZcBeK3cIg PkgUTXqPFM2njh1sNUKnUS6TA6cjWBdnQQDs8rmo4FKcZ6n6LhMnu8Lqy/CcxMZ8m6ZNXAQDpls JPO/28dOrkJLNeG76JnIKPRtumnC/oMbjsBe3sRSlY6ycw8XBo8m/XYGCaAYAsGb2y8auRPH6s0 /aEdeYeeXxPJb1/PVS6zTaBtpAKwxbKJlw1y2j7l/hj5YfU0FqYvY25xSZ1Ni1jEaVMtcN2UgBS m4wguwcAqSpBOrARfDb8DtsBsSl36cfz5qw2iv5bqkw4gR6J1RGveIEW87ST/NYTrFtkfLC6pxl 5ESxG4XpnZ2Dh0Q== X-Mailer: b4 0.14.2 Message-ID: <20250822-iov-iter-v5-5-6ce4819c2977@google.com> Subject: [PATCH v5 5/5] samples: rust_misc_device: Expand the sample to support read()ing from userspace From: Alice Ryhl To: Greg Kroah-Hartman , Alexander Viro , Arnd Bergmann , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Matthew Maurer , Lee Jones , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alice Ryhl , Benno Lossin Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Lee Jones A userland application can now operate on the char device with read() in order to consume a locally held buffer. Memory for the buffer is to be provisioned and the buffer populated in its subsequently provided write() counterpart. Signed-off-by: Lee Jones Reviewed-by: Andreas Hindborg Co-developed-by: Alice Ryhl Signed-off-by: Alice Ryhl --- samples/rust/rust_misc_device.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs index e7ab77448f754906615b6f89d72b51fa268f6c41..9e4005e337969af764e57a937ae= 5481d7710cfc9 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -100,8 +100,9 @@ use kernel::{ c_str, device::Device, - fs::File, + fs::{File, Kiocb}, ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, + iov::{IovIterDest, IovIterSource}, miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, new_mutex, prelude::*, @@ -144,6 +145,7 @@ fn init(_module: &'static ThisModule) -> impl PinInit { =20 struct Inner { value: i32, + buffer: KVVec, } =20 #[pin_data(PinnedDrop)] @@ -165,7 +167,10 @@ fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result) -> Result, iov: &mut IovIterDest<'_= >) -> Result { + let me =3D kiocb.file(); + dev_info!(me.dev, "Reading from Rust Misc Device Sample\n"); + + let inner =3D me.inner.lock(); + // Read the buffer contents, taking the file position into account. + let read =3D iov.simple_read_from_buffer(kiocb.ki_pos_mut(), &inne= r.buffer)?; + + Ok(read) + } + + fn write_iter(mut kiocb: Kiocb<'_, Self::Ptr>, iov: &mut IovIterSource= <'_>) -> Result { + let me =3D kiocb.file(); + dev_info!(me.dev, "Writing to Rust Misc Device Sample\n"); + + let mut inner =3D me.inner.lock(); + + // Replace buffer contents. + inner.buffer.clear(); + let len =3D iov.copy_from_iter_vec(&mut inner.buffer, GFP_KERNEL)?; + + // Set position to zero so that future `read` calls will see the n= ew contents. + *kiocb.ki_pos_mut() =3D 0; + + Ok(len) + } + fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize)= -> Result { dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n"); =20 --=20 2.51.0.rc2.233.g662b1ed5c5-goog