From nobody Mon Oct 6 10:14:37 2025 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.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 7090F155CB3 for ; Tue, 22 Jul 2025 12:33:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753187638; cv=none; b=KkSCHkSVOLBm5x3NcEUFRGq8xiHrKx7zZG9XJzOBGFiEVt4Shh3wDaAUS4coThScBqlCFwk4/zp3dOV7Um4ZBm3V//qvmom/YsngXWOQQPA/EVG8rA+yVqiSIHAPXx1NkK9Uz3kvlOKD3YipTSkkn1exq8/7hRgQEZrCBZ/INvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753187638; c=relaxed/simple; bh=5lA2uHvtW9tJXOxdNCu5CQZgwm/fgXKEZp7l+Kz99ok=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qnD5eDVv+9w3Nf0bCDQesE5q37PlfrD6klltwqmKEsHyryk4K154LP6NIKnWCPRGGa744UFgXuy5m82oHbiYk6SInfMnDbv6nbAko+y5OM0/Sza+x3l0EDkjMXFyjuPjFCU0jrCAwz8oz6gW8OlUK2dw3onVFj5u/SeLD91uids= 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=pfbgAyi8; arc=none smtp.client-ip=209.85.221.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="pfbgAyi8" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3a4f7f1b932so3675988f8f.2 for ; Tue, 22 Jul 2025 05:33:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753187635; x=1753792435; 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=V1wE547TIUAeDYCvaz6VzLnWtvtjxwWPkj/pXRVyxz4=; b=pfbgAyi8k7rAW+g/LYmSDQs3YnWvEZZFj7LXt3lgMiwPs1k+vflD7212eR1XwtooKi WjtgUMP42fB7HkjgAKyf+F5vTAqAlaCkzq4q2SEI4blwcQkkYinBqrXXC2y+PjBooA1q 3ep61+X+bPHsN2Zv8wzTrl9pX4DvyMw9oSEOGXrUi3QkgF3bLWbfVd5j5bz6ued2eLJu q0XZCBI8wdGmwpP4EV4+BLRYHyPqJQZthZIcNd1yB2JDDCl2llaB0HRciwckqheC3YSY zyq0UU2spcf/6pNspHKLd26EowYybhM4TM5mB9Q72Hd6xOS6JUsF/ShF7TK1l+kH6Rq6 Wqdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753187635; x=1753792435; 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=V1wE547TIUAeDYCvaz6VzLnWtvtjxwWPkj/pXRVyxz4=; b=A5aphvxW2fuCcahkqmtpKnimpxvz/ESA9aVpTOntdT0xRlY9PU6+wMx0rfQie8Enri 8ayuMI6gtrlQvshvFICkT1wVfIunWztEj7OSiud6yG/r2kLZQk8Sy97f/GJdPc8bqut5 Jn9bwLaI/peKRm5jBiL/XNhjuXLiyk3xR50/SJrE3EDGRXxKpGluLnjQGROyzd5VeaV/ cELE4rEpt0Sbn9Ol6vw9Vu/FfABS1DYyaJ0KUPQi4ZzuF3MbMCun2dOWObEIGDfh2tV2 H9YLQUP+OtHgcmdrs0cDXFutDQIJFLbPzDgYCq4EqsrrydCKYznBQUJmoys4IsqbsGa9 XHaQ== X-Forwarded-Encrypted: i=1; AJvYcCUOeGn9IzPK75Wsim1gKM0WscGQ3yj9r+osKYw0HjuAGpiUXdYqMWEuDlK4Dd84s8CKoNoaDd/h+fgjqZU=@vger.kernel.org X-Gm-Message-State: AOJu0Ywjw+6Q9ScUCvs7KvYCjPcFTPFV/UFyFpbM3YWFeCbxP73eTSTK NlVKxx9A9lo4EkjVuS/TlfDk8J4b0A5BoZucYq9sw1g3APjXfJvE03PBlya1OGXdjfjzdybfdcc vGVlyjEx+5C7XRqqM9Q== X-Google-Smtp-Source: AGHT+IGOxLEETJULhnec/XmaByK7scAjLrn1ejXnv3+Oewq0MJ+ilG56wjy6xJIQ0+9UFZ48l5QIfgeEFo4c6UQ= X-Received: from wrna17.prod.google.com ([2002:adf:e5d1:0:b0:3a8:2eeb:874]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2485:b0:3a4:eb92:39b6 with SMTP id ffacd0b85a97d-3b60e5188a9mr19190697f8f.54.1753187634757; Tue, 22 Jul 2025 05:33:54 -0700 (PDT) Date: Tue, 22 Jul 2025 12:33:09 +0000 In-Reply-To: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8317; i=aliceryhl@google.com; h=from:subject:message-id; bh=5lA2uHvtW9tJXOxdNCu5CQZgwm/fgXKEZp7l+Kz99ok=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBof4UvfDi6rwVoVP1MQGMum0Fmodzvgp+Yoy8B2 CTXWNBHOSCJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaH+FLwAKCRAEWL7uWMY5 RvtyD/9NRrsGLba8fLEarrHTSaPAd75YSfsUiV9E/52u3O48Rv9maqRWdHQHKG04OaVr7GERuwI wDa6ddQEdvrGH1uagYc/OAefLXy2V8LKBFZgrJkS+IfdZAoZhDGSnOTuNJr7bdFQh86HL1VvoqA 6Lede8I2dtqYwIsn2dw5Q5D1LL5zYakz8futTpPoDByi2yFEV4K+3DAuSiB0pdk2CKABattUnsb pZ8oBO/ULs4RLSLjeWkP0Fh1AOWtQu09lYt/5D/LblCsedpda4Uv0sE33SFi/JlNi8KVN7mTs0D lGiy1tybQXbOtFlJqgBMcNQ0sJzl5aTwzeIq8SZ4ANRR2bvheKeLqpF7yK6Cjn7H6PTbmilScvJ BD3agXX5A+O1+FTfoau0Kgz5ffFN5rxA+sfVeqOstDbg00jcBGtFfCEX5JUoa11pEEZtBjQqBso a0pK2BG1Vvki64qYI8RxHNwgWPw5pRn6qSdliX2M+GPkBaYREbOQPxd77vIrG+LDRn0VnJLVb8q XzjqgaJ3IvTcUfzZ2PYw+qzn+p8sbDGwJ05QB082Cyy/bRqIiopWOHTCl6dqxGGdGp9AVes1pcP 3gpGGYUqfF3xgTXm5dBO1HdTg5q4WrwumJpSonaQQOehKjguW2va518EUJPBUS+5x9L2hj14RyB 05LO4IQYywvUfvQ== X-Mailer: b4 0.14.2 Message-ID: <20250722-iov-iter-v3-1-3efc9c2c2893@google.com> Subject: [PATCH v3 1/4] 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, 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. Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- rust/kernel/iov.rs | 167 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/lib.rs | 1 + 2 files changed, 168 insertions(+) diff --git a/rust/kernel/iov.rs b/rust/kernel/iov.rs new file mode 100644 index 0000000000000000000000000000000000000000..a92fa22c856a506f836a15c74a2= 9e82dc90a4721 --- /dev/null +++ b/rust/kernel/iov.rs @@ -0,0 +1,167 @@ +// 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 struct invariants for the right = durations, 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 struct invariants, `self.iov` is a valid IO vect= or. + 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 struct invariants, `self.iov` is a valid IO vect= or, and `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 = deinitialize any bytes in + // the provided buffer, so `out` is still a valid `u8` slice 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: `Self::copy_from_iter_raw` guarantees that the first `l= en` bytes 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 deinitialize any bytes in 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 deinitialize = the provided buffer. + // SAFETY: + // * By the struct invariants, it is still valid to read from this= IO 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 e88bc4b27d6e367f0296381c8d6b22de21d69f54..8ee29bd340027be855bcab9c3b3= 15fa2b89ca9f3 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -83,6 +83,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.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 10:14:37 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 CF3082E9743 for ; Tue, 22 Jul 2025 12:33:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753187639; cv=none; b=qEZA8KPelH+p8tq8ogz9GT+2wlxGV1bsyU3bj3wklIgJIZ6NNJLC5XgREussl916blNB9wvushmyf3Fa3Ks8W1Q5PMY6OYPDoQzZEtsTUOhnkUG0zlJaOQ1r99YjT7Qd/+iaDcDCDNeFObXzEZiVeuswnwXrOsPrMbNYTKe0Qck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753187639; c=relaxed/simple; bh=bN+xS9l/IoVlBsDrl9dzTtgs+eX0mH/u4d0cvmwE7sM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jq1JogjQwVTI7jFvgna9rJTuPaVwqSGhYwXUWJbjuGm8pJ5uyDpAKn6uCta3CzjqJubeLBlV1XhvWjzEdpqq8b0tbcteWVHkbAi+RiBB1cjmTGMD6k3f40KUTbXA89M8n+pGbfi/0otE1zp6VrbybvA5Rek584a1xI9VjhNKqus= 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=kCch1fbR; arc=none smtp.client-ip=209.85.128.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="kCch1fbR" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4560a30a793so22488095e9.3 for ; Tue, 22 Jul 2025 05:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753187636; x=1753792436; 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=vIys7Zrgy3fi/vh2doa1crdb0goric/E95Ha5k97fXg=; b=kCch1fbRuhI1MizkfMzzreyfhAzN1iQbbc0ZkzVKbwlfHJr3Fq4G/39FDe7DY26fkC PBDIW6oNDvc0YYaBoN7zRhug/KOjnzIXCs7A/EyHe08L5EVjfIIb7bSmAN5QAwewzJ98 YkYDkD8fd6qbIYL0E27LppPlHXECwOZVyxlE0FVzVEzPz3Xfgr3EjXGUC3akgdZzMil/ KvJ+eQ2AMap2gk0xS1gpAoRXgZb81F2bK6zs6BAw6QA/omlVXEgV600967VZN+A0aZYH fnWUg7OcwQCjQLhjX8s0WjqW3x/4c5/DkckaolN3foVJrlbb+JXVL0jaIuMIfBpVZ+yV 6tzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753187636; x=1753792436; 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=vIys7Zrgy3fi/vh2doa1crdb0goric/E95Ha5k97fXg=; b=Suw9UAGZNDGcPwGT6opns/6dL4hdhi159zjSlpxKeEwLoPce/0QOqrGwpZcXTcWiEg 6RceZ+RfOGMYv+luyZv5xyaC2RgV9arXmEfh6sjBoQmuNgTfkTs+JwtQW/Viv+6Eukc6 NZAa1BZAgiPb1Cfx+an93R1fZN/mA7K+EdyhlkS8XPfVdOSqNzF9th+1GewpfuUtItss UNmWuoIO/6Kwb45HIIeEppCbaXB5IXaVJ8jV92MSqnjSU7LdBs7HKAPu6xp/c14Sbtq5 2/oAmQDlo480NG6RS85FZkFQctOpOq/G2ywWHrWFnEpiIgbnxbu9yq12B1F0+/Z92rxg kPYQ== X-Forwarded-Encrypted: i=1; AJvYcCWyn9XyIGCINNJ6jnqx/IofMO8NFFYKhIs+bLB+nB3i4Rpsnelf3B9iwXJi1824e+pZ0sa3pS+aTa6vaYI=@vger.kernel.org X-Gm-Message-State: AOJu0YxxGuAvEptqqv2MpFDidOMbJwNu+aY3wFSGywX55/FNAdxk9TIa jC2Zmx7wQSHmoHHGfGfBHL+XJOVcJx5iSAqMdKK6/ZalSndvu2ifi6oAT+WVWpdjl+S2aGpk+Tz ZwPu4QwzdeVd6ZBE0HA== X-Google-Smtp-Source: AGHT+IHLmr8kfvxdQeovMqJvDH4dC+bxCl4yRhEA65mLowxn+aFNuomZDh0NdY/mR5eXodkHtdwQA/9jowq000M= X-Received: from wmbfp9.prod.google.com ([2002:a05:600c:6989:b0:456:3cf:1e95]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e03:b0:456:161c:3d77 with SMTP id 5b1f17b1804b1-4562ec35d4dmr263377375e9.16.1753187636266; Tue, 22 Jul 2025 05:33:56 -0700 (PDT) Date: Tue, 22 Jul 2025 12:33:10 +0000 In-Reply-To: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7454; i=aliceryhl@google.com; h=from:subject:message-id; bh=bN+xS9l/IoVlBsDrl9dzTtgs+eX0mH/u4d0cvmwE7sM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBof4Uvl12Pj3ASutOliRdEuJdpOMuA5XJ6h8iwX HkcHLisWBqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaH+FLwAKCRAEWL7uWMY5 RoWrEACccsy66C6M1q5PkZ4R/XPyBzO9mCYnWPyqidHvzcsvZOsxrXtOIQs0yTk+/9MfAC/VgOq oJZX90V5CqUi8hxGrYO5lCeavnvqmgZhFcjIlKtrOGW2Ks3uihVTLz9p0OIz603IGb3S2zZw33/ Z1D547wUKz4gy2b02pokECHbyGnpaiURyoJWdZCbAIQJwXC0BkPZ5J2IiFxes7Vy6Hz6muOaVRY LMNWPAqrzkpz8Wg4BtTAooYbI++1MsDheh7Jb3R6cLtPbbWEy+Zl+ZN5C7EnfDwErqIr4zOKi5v i1BB31Oynu9jGAURtHsbTr8y97Z9Eu7Z1MgwwB46gl5qY5S6I33dHqbliHnNbZGA3IHy52eBghB 47TZdZP+BrRS0O+mLsvZN2TC9FchSyGJ8SKeWiOBT4ej1C0FxKG82sdOW2bRoXt7IMCSdVIc71Q qd8feD3vwFPuOcdtVdXQ5R5pB80wlm0pBzgrqQkkCb3zaj2fU7Z7aq4HdWHlT3CNMBMOoozov8s SBWm5xfc+XNB5IApXGfkx4FbdQ3LshLuIvHp8WHf3djGUyUmpafInnhbU/YYWBaw4HGOWktndq7 MeKpY4XuaC8wL7boQNwpx6Kq39Z7zYXxlFsZOikZC/Mi8W0qPbeDwgwOCxx1UC1dvimqW0RF09W uqHVCyljoaBinMg== X-Mailer: b4 0.14.2 Message-ID: <20250722-iov-iter-v3-2-3efc9c2c2893@google.com> Subject: [PATCH v3 2/4] 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, 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. Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- rust/kernel/iov.rs | 143 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 143 insertions(+) diff --git a/rust/kernel/iov.rs b/rust/kernel/iov.rs index a92fa22c856a506f836a15c74a29e82dc90a4721..e31a7947677c0603764648c0641= 8697b2be2af18 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. /// @@ -165,3 +174,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 struct invariants for the right = durations, 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 struct invariants, `self.iov` is a valid IO vect= or. + 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 struct invariants, `self.iov` is a valid IO vect= or, and `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 struct 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.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 10:14:37 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 4787F2E9EAF for ; Tue, 22 Jul 2025 12:33:59 +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=1753187641; cv=none; b=ikcSVm3WSmMUZM2Vt5daTYeWPGZGAoWWRI+OQJ13etiLJnlInyrwWCD1e+Iy0SBLr+06u4GIYNZFTr6ngA7ZGYV2h6sRRHYSoWcbi6J0jdRirRJSq0E3aPB3EktLFRLvYtcMnZG0ckCrGnwi+RqtSkW2tcN3BYhl4ky2JouFxxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753187641; c=relaxed/simple; bh=RHSf1djWQ5ziuqFNfIcXAngCpz/As9JffyIUYTaCsAQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u28nAcOtz8H26lztX+zZuDb1LAKbX4mujfFH7aP/mk7sxlPTNHol6kWtGoOsHXTUyqaJAJZ4ExXLgdgWi68w84bQ75jSxr1GDwqm6KzygFbYfhKBikq66WH+iVP2vXujoBLay7JRWQqRRJwAWxzjOtYb48Y47SG7MyZKzTzeBmo= 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=l6DODV5E; 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="l6DODV5E" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3a4fabcafecso2503921f8f.0 for ; Tue, 22 Jul 2025 05:33:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753187637; x=1753792437; 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=RHuFNlg3XjE6xiR6wmMnLidoCY7UAyUub3aLnrvtSCI=; b=l6DODV5ERtVhs5iYBvuSZRVb4T8kD3SY/lRc+ObnQ36CG064Z13r+zCXzwUW5FVWTt c12Ht5oIFjbSNGHRBAm5GTtWAecyX3zWA7F3CUP4FcC6XMIcEiwTXOXqiUGhdsLtHEt9 0vmpS2JvkWu0MAFwc2ggCScjkRYTWwSUQdbA3R7ls7ltZW/qyryDljlD27Gx6oY/qg5D gAso0PSTgRMHfGuL+WKsbRs1JvRN4+F0RdO7vI7dD1idABH3qpv2y74sz3fEgOu/Yi+I +O0OURUNKul6sygmgp/LTPFWTJFb6AOuvPa4GoA1lkKCn3nAU/L9B4bAQyfTaajZlIDF WrTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753187637; x=1753792437; 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=RHuFNlg3XjE6xiR6wmMnLidoCY7UAyUub3aLnrvtSCI=; b=DOIAHJznAF8KIQBo2Yo5oIoob+zAEe5PQqSWrhApzIDf+ahFLcLouSVkvEhqXsRz3G nCxyzRE3FiFKs0sacBpR8Z/9OUZnDZMXHtgEsWYqZYfT8QQm5QuTYDjm7EW8sjxO9dxL DJyHVb7Sad2IrBmkMjSIMcsMB7RGjSuqpX4bgIsuBFH4V3nfpXPR/V4r8mfiYn+qgh6j BO3hZN/DqN4ggFDc57mrtmOLp30uAPZlb5Ib+YUsNM35JLdAx5hUDzJGQnWSc4o3VLhu 49RKn6UoSRgSPz0KBvvqDPbNtJsTtBwIKGVz6bWlTRDsnnB6CbCiXSMIF4oXH2hAaRL9 X8kQ== X-Forwarded-Encrypted: i=1; AJvYcCVw6onnc87CVZ/KWzTjhIIgwci1yHEAMGh7ytE5ftpI1tsoKPlW4Ka4/Uxi5Gm6XwZUM6di/wHTuxwayec=@vger.kernel.org X-Gm-Message-State: AOJu0YzhPDeZeOqcy2r1p9NKMqcr/LemT1XNcQFFEGCSBZ1iKEQwZGQj WJ+4NktjC3zFBmZCQ/xnlP8g0H0R3BFVsa0f3esIMjlcBWuPK/BcM4cSHl/zlecDlghmKpPyaFa ezB6kPS9OOx+2HVdd2g== X-Google-Smtp-Source: AGHT+IGCiOpv5N70smSx4M9mF1xjWR2XagRQxcYjCsEDgG6w3ESOsRvYxhb36yCsFqyKuqaSd52odsvNBTbgqJU= X-Received: from wrmr13.prod.google.com ([2002:adf:e68d:0:b0:3a4:e608:d34b]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:250c:b0:3b5:e6c0:1241 with SMTP id ffacd0b85a97d-3b60dd65266mr23484990f8f.9.1753187637394; Tue, 22 Jul 2025 05:33:57 -0700 (PDT) Date: Tue, 22 Jul 2025 12:33:11 +0000 In-Reply-To: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=7067; i=aliceryhl@google.com; h=from:subject:message-id; bh=RHSf1djWQ5ziuqFNfIcXAngCpz/As9JffyIUYTaCsAQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBof4UwSU9RtvHAUUnUlpX4wTNHJ4T948ieWfHyh Az1EfYuqEmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaH+FMAAKCRAEWL7uWMY5 RiiDD/0arJRqkglkqv8O2LNCN5M8Y/lNP6cBHo2WXsuQsthaCvatn6fQuh5ygLbkFOEID275jw1 X32NCjHXSAmR9nyhVFAGUGBQvgBTq+N4zK/ptCcgr1KU2AaZZYEIPimvck2d2aZsZNoC92oTabB VgaUsQHH1D2+LieOsicE92gY++iDiolQ8ySK/T1XHIt72fZtNygiZBiDQHxCid8F0cEPe23ie5q W8L1zRPUQX0VZWOF7OsNu1Dkc8XYJXKGPF3pSX+E8UqRBIkej5Wt6MAzMoFI8nwvbZbRMAiifTS OCbW+o1Lb8gvM6iudP40KKeXHa9QMA0vKxBZMdnHwCeYMahu1dwFA96GLRoj+TFT7f5q3rZtkYq CRLMrRKa5Mlqu5f81nCpS4MBUccUTFlHfvNBDXK1BJrIzwXJh7evFWhhs45AXNZ3MF2L+uTN/W0 1QUlkNJyHyzVb/t/e9O5hlHX1CzV9IQLa05COxeaYjyu0rNDeQSCUOODh5E8jXCZXgHhxkQBNGz zn+6U8R/ToLlK1ZFXwDTd6YvELahZnKZWMsKVCQZGFTksgGZS8HKcY0mUENl235Clqz+JpeT15z fJ6DKShQHdW5045N8FNfya54XuYrGyD7H6meZHYFt4rrkid8wSq4ubN4Ze8w3MpI6A7549BEskD lsH9sLZsjY5xcHQ== X-Mailer: b4 0.14.2 Message-ID: <20250722-iov-iter-v3-3-3efc9c2c2893@google.com> Subject: [PATCH v3 3/4] 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, Alice Ryhl , Benno Lossin , Christian Brauner 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. Cc: Christian Brauner Co-developed-by: Lee Jones Signed-off-by: Lee Jones Signed-off-by: Alice Ryhl --- rust/kernel/fs.rs | 3 +++ rust/kernel/fs/kiocb.rs | 67 +++++++++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/miscdevice.rs | 63 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) 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..837f4be7cb8fbca6e3f9aeff74d= 1c904df3ff7ff --- /dev/null +++ b/rust/kernel/fs/kiocb.rs @@ -0,0 +1,67 @@ +// 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 private data of the underlying 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 Rust data stored in the private data of the file. + pub fn file(&self) -> ::Borrowed<'a> { + // SAFETY: The `kiocb` lets us access the 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: The `kiocb` can access `ki_pos`. + 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: The `kiocb` can access `ki_pos`. + unsafe { &mut (*self.as_raw()).ki_pos } + } +} diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index ad51ffc549b85da9b71ed866bf0afd4c4cb76f07..eaa626277cc39d84761a2228f5a= ee05640323094 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, @@ -136,6 +137,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`]. @@ -240,6 +251,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`. @@ -336,6 +387,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.50.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 10:14:37 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 ACB142E9ED5 for ; Tue, 22 Jul 2025 12:34:00 +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=1753187642; cv=none; b=C5k+o5DMg8lmBtLHGrKnwfWsBii9sro8b475/6EK64HCbCz9lEpFqVqrCcccfhYof75lA1Q1jNmgvc12g8ud9WkBej/FXeGs81MeDrFWfFS1/6tbpboieHH2Fh5EXcN4CSL96/ZvybWeCJSMSTmFth48fMcYTN7Pdhvu9bPkpS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753187642; c=relaxed/simple; bh=FG99A8uKS0EE4jq/yYyxx3nPSnCQfslijrkZLCnqBt8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Z4M5K8r55YMuKuYAXrKM4sbAMPQpRv1jUIVtqeXqLjTPer86WYpGFuJKSh8IGp0g/xn1dhe6oZSsmztwsIsn5NqVc4aNbYr5SWjM32kxld5TAKWZt9iP58NuKaPlkEZKLaNHN+K25rVnJZQFC6K85iVvaue5fqVDpoIun1Pi7U0= 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=2AXn9v2C; 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="2AXn9v2C" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45639e6a320so27573575e9.3 for ; Tue, 22 Jul 2025 05:34:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753187639; x=1753792439; 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=QPTngAVTQIfkddyJSfXZqoosOR6+zBrLPpEZoe8PuFU=; b=2AXn9v2CZymy/HmSziNh6om8JigbDX5WxoaYBCbd9IemTGFV9MmNYqNGgdRYL6FNL+ nFsFpmYeblo4wuAWOE7nbgY+4bGsdAhcpIt76v3j1M74uQGFYY0DSXDHti8BuBqmd0DH nTDxi22ICOjYSLS1WmJQ137vNccsLob6vwLl+Qef5zvzcXcOobwdl5vJnSrY+Q55jEzd 4f0jIBfBrGeLBoMZBpxsHKXlZURMJ3ZjNr/11XMsfwTuXE7hCN6lGZpNoS+eFJVeEXn8 9fT+LMHvpJ4zT7IppM2o4dibVWZz8z2RRQFHvmRXkwggwAMPtDkT16wgh/j1rWD6aRrs flkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753187639; x=1753792439; 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=QPTngAVTQIfkddyJSfXZqoosOR6+zBrLPpEZoe8PuFU=; b=FseThMabUrLWsBVE76TKGFx2zR62CK6jUTG6KVzbAUBMdEKZCOLYJp+75vHWaEdNeu l3qyr1N9DFv++IUmzr8M4I7qgXJjY/EX+2N86Mi2zw+14a+mYwL8sHWrhTZRHTD3VgDA eVAKJPCHluS1w64mBEwHVY+xacpJCxkg7oVF39pCKkJC+S4HwJdAc/lq/cvalU8EVCP+ gTRsD0mSaj/vKmfke+qWw2jikmvYzM+hntdlzK6GACptM27OaVZ+hKfJ34cpqdr+KqHo GmUxPrcdGedJ8xe7IxvPab9K0ySCaTyL6j16GInnqMM/5r7Sw3gXbf3tF2F9APguzgpa oqpA== X-Forwarded-Encrypted: i=1; AJvYcCWSRLTxJ897h01hEZSwzCWI/9LzngSGZvtMp2/bbj4t0SR03xmjwYOGJ2Qf9fSTfVsCxez4Yop/CsNR2NY=@vger.kernel.org X-Gm-Message-State: AOJu0YwsufTMd1FGTUzXiRtRXMG9SZRkfNiL3V1nKW/N5cZtJOXkoSBO Xiaev5aA9lxfTVUMJ+6irHIln14Nt66Tz/D2YxUoUjAaImiennq6OiLVAZZ5FO+5hYlJC4UpKGM R5uylid+TkhwT6qzpEg== X-Google-Smtp-Source: AGHT+IFXjGJcAEeFdu7nCu1d3cAti7GtSz1mDB/R5nXZkei19JW0FzweFSjBngPkIB+aR+qo5Q6rT1Owp4+hWy8= X-Received: from wmbhc5.prod.google.com ([2002:a05:600c:8705:b0:456:302:6de0]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:64ce:b0:456:133f:a02d with SMTP id 5b1f17b1804b1-4562e373d8emr252182475e9.17.1753187639039; Tue, 22 Jul 2025 05:33:59 -0700 (PDT) Date: Tue, 22 Jul 2025 12:33:12 +0000 In-Reply-To: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250722-iov-iter-v3-0-3efc9c2c2893@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3150; i=aliceryhl@google.com; h=from:subject:message-id; bh=vIurSXFz5k4sNYySp9jntD8+LsMhT8UnySSZFixYeks=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBof4UwHa+JoiPxu/gVzVFNUGRHnO+trTQnz90Nh OMduI9/IiKJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaH+FMAAKCRAEWL7uWMY5 Rk0fEAChnUhKY4xTDqQJb7E2xrWr4MRDPfVt6PsD0vqz0rt+dr5aWhfmkfRO4ms2XN7X/WkG3lE 6F83T5W9Bcj8036QqK/0tXR4l5ayYNKaHojafT5kmNx3VZjcC6kqG3BHIPjDtdS6DZ32dOms/0T EPrGWRPeW6L7YY/qlSqNQuwSDvnoatVEQxWIFb8N0QHJas/6wUULuBsNbCA5gUDrg3c/ZIV80d0 BIjEitlJG5dT6tksa3dTLZAO5WYsrVyX2tEwesfP9hz3PgIJf8wtIZ75xL/Vda+zuEFDZfmE5ZG lJ1m2Rs5q4vZzl67kCT4kbhZkNKYjAvdW5DBs9g5T+TUgsVbxg+Yox5GEea2RSqet+QgAbHZNTM NtWVVFM08DYr08zhK52/Et8AuSWNKTD+T/N75J4+zf908P4bOCuGGSfjn06DeAo8HB4VE7YidzY 0dSm3/yk+xZ8398A/jS4DEvdgEy0peb2o+AHaXat2v1gzXmxOjXYkanD0AngnPQDv10g14wt2BH JtOkls5OA3NlUeuVnvuyJVX76nCMGojQwU7KC6KW39akC2ymktFFyE7ABGtzsVpLHiN7s8C98nE XdMAyv+rGnshUGy3tx9+ejJ7O/fAxGet2lpqfKzqEp75TiiKr4K0yF9cqqTKd9EeI450C6iAvrY Jh87Cas0nEP3IeA== X-Mailer: b4 0.14.2 Message-ID: <20250722-iov-iter-v3-4-3efc9c2c2893@google.com> Subject: [PATCH v3 4/4] 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, 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.50.0.727.gbf7dc18ff4-goog