From: Gary Guo <gary@garyguo.net>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Miguel Ojeda <ojeda@kernel.org>, Boqun Feng <boqun@kernel.org>,
Gary Guo <gary@garyguo.net>,
Björn Roy Baron <bjorn3_gh@protonmail.com>,
Benno Lossin <lossin@kernel.org>,
Andreas Hindborg <a.hindborg@kernel.org>,
Alice Ryhl <aliceryhl@google.com>,
Trevor Gross <tmgross@umich.edu>,
Daniel Almeida <daniel.almeida@collabora.com>,
Bjorn Helgaas <bhelgaas@google.com>,
Krzysztof Wilczyński <kwilczynski@kernel.org>,
Abdiel Janulgue <abdiel.janulgue@gmail.com>,
Robin Murphy <robin.murphy@arm.com>,
Alexandre Courbot <acourbot@nvidia.com>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>
Cc: driver-core@lists.linux.dev, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: [PATCH v2 06/11] rust: io: add view type
Date: Tue, 21 Apr 2026 15:56:17 +0100 [thread overview]
Message-ID: <20260421-io_projection-v2-6-4c251c692ef4@garyguo.net> (raw)
In-Reply-To: <20260421-io_projection-v2-0-4c251c692ef4@garyguo.net>
The view may be created statically via I/O projection using `io_project!()`
macro to perform compile-time checks, or created by type-casting an
existing view type with `try_cast()` function, where the size and alignment
checks are performed at runtime.
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Signed-off-by: Gary Guo <gary@garyguo.net>
---
rust/kernel/io.rs | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 146 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index a13be8c5fd2d..869071d47a13 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -7,7 +7,11 @@
use crate::{
bindings,
prelude::*,
- ptr::KnownSize, //
+ ptr::KnownSize,
+ transmute::{
+ AsBytes,
+ FromBytes, //
+ }, //
};
pub mod mem;
@@ -297,6 +301,13 @@ pub trait Io {
/// Type of this I/O region. For untyped I/O regions, [`Region`] type can be used.
type Type: ?Sized + KnownSize;
+ /// Get a [`View`] covering the entire region.
+ #[inline]
+ fn as_view(&self) -> View<'_, Self, Self::Type> {
+ // SAFETY: This is an empty projection, so it trivially satisfies the invariant.
+ unsafe { View::new_unchecked(self, self.as_ptr()) }
+ }
+
/// Returns the base pointer of this mapping.
///
/// This is a pointer to capture metadata. The specific meaning of the pointer depends on
@@ -912,3 +923,137 @@ pub fn relaxed(&self) -> &RelaxedMmio<T> {
readq_relaxed,
writeq_relaxed
);
+
+/// A view into an I/O region.
+///
+/// # Invariants
+///
+/// - `ptr` is aligned for `T`
+/// - `ptr` has same provenance as `io.as_ptr()`
+/// - `ptr.byte_offset_from(io.as_ptr())` is between 0 to
+/// `KnownSize::size(io.as_ptr()) - KnownSize::size(ptr)`.
+///
+/// These invariants are trivially satisfied if the pointer is created via pointer projection.
+pub struct View<'a, IO: ?Sized, T: ?Sized> {
+ io: &'a IO,
+ ptr: *mut T,
+}
+
+impl<'a, IO: ?Sized, T: ?Sized> View<'a, IO, T> {
+ // For `io_project!` macro use only.
+ #[doc(hidden)]
+ #[inline]
+ pub fn as_view(&self) -> Self {
+ *self
+ }
+
+ /// Create a view of a provided I/O region.
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must satisfy the invariants of the view type.
+ #[inline]
+ pub unsafe fn new_unchecked(io: &'a IO, ptr: *mut T) -> Self {
+ // INVARIANT: Per function safety requirement.
+ Self { io, ptr }
+ }
+
+ /// Obtain the underlying I/O region.
+ #[inline]
+ pub fn io(self) -> &'a IO {
+ self.io
+ }
+
+ /// Obtain a pointer to the subview.
+ ///
+ /// The interpretation of the pointer depends on the underlying I/O region.
+ #[inline]
+ pub fn as_ptr(self) -> *mut T {
+ self.ptr
+ }
+}
+
+impl<IO: ?Sized, T: ?Sized> Clone for View<'_, IO, T> {
+ #[inline]
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
+impl<IO: ?Sized, T: ?Sized> Copy for View<'_, IO, T> {}
+
+impl<'a, IO: ?Sized, T: ?Sized> View<'a, IO, T> {
+ /// Try to convert this view into a different typed I/O view.
+ ///
+ /// The target type must be of same or smaller size to current type, and the current view must
+ /// be properly aligned for the target type.
+ #[inline]
+ pub fn try_cast<U>(self) -> Result<View<'a, IO, U>>
+ where
+ T: KnownSize + FromBytes + AsBytes,
+ U: FromBytes + AsBytes,
+ {
+ if size_of::<U>() > KnownSize::size(self.ptr) {
+ return Err(EINVAL);
+ }
+
+ if self.ptr.addr() % align_of::<U>() != 0 {
+ return Err(EINVAL);
+ }
+
+ // INVARIANT: We have checked bounds and alignment.
+ Ok(View {
+ io: self.io,
+ ptr: self.ptr.cast(),
+ })
+ }
+}
+
+/// Project an I/O type to a subview of it.
+///
+/// The syntax is of form `io_project!(io, proj)` where `io` is an expression to a type that
+/// implements [`Io`] and `proj` is a [projection specification](kernel::ptr::project!).
+///
+/// In addition to projecting from [`Io`], you may also project from a [`View`] of an [`Io`].
+///
+/// # Examples
+///
+/// ```
+/// use kernel::io::{
+/// io_project,
+/// Mmio,
+/// View,
+/// };
+/// struct MyStruct { field: u32, }
+///
+/// // SAFETY: All bit patterns are acceptable values for `MyStruct`.
+/// unsafe impl kernel::transmute::FromBytes for MyStruct{};
+/// // SAFETY: Instances of `MyStruct` have no uninitialized portions.
+/// unsafe impl kernel::transmute::AsBytes for MyStruct{};
+///
+/// # fn test(mmio: &Mmio<[MyStruct]>) -> Result {
+/// // let mmio: Mmio<[MyStruct]>;
+/// let field: View<'_, _, u32> = io_project!(mmio, [try: 1].field);
+/// let whole: View<'_, _, MyStruct> = io_project!(mmio, [try: 2]);
+/// let nested: View<'_, Mmio<_>, u32> = io_project!(whole, .field);
+/// # Ok::<(), Error>(()) }
+#[macro_export]
+#[doc(hidden)]
+macro_rules! io_project {
+ ($io:expr, $($proj:tt)*) => {{
+ // Bring `as_view` to scope.
+ use $crate::io::Io as _;
+
+ // Convert IO to view for unified handling.
+ // This also takes advantage to deref coercion.
+ let view: $crate::io::View<'_, _, _> = $io.as_view();
+ let ptr = $crate::ptr::project!(
+ mut view.as_ptr(), $($proj)*
+ );
+ // SAFETY: projection of a projection is still a valid projection.
+ unsafe { $crate::io::View::new_unchecked(view.io(), ptr) }
+ }};
+}
+
+#[doc(inline)]
+pub use crate::io_project;
--
2.51.2
next prev parent reply other threads:[~2026-04-21 14:56 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-21 14:56 [PATCH v2 00/11] rust: I/O type generalization and projection Gary Guo
2026-04-21 14:56 ` [PATCH v2 01/11] rust: io: generalize `MmioRaw` to pointer to arbitrary type Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 02/11] rust: io: generalize `Mmio` " Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 03/11] rust: io: use pointer types instead of address Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 04/11] rust: io: add missing safety requirement in `IoCapable` methods Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 05/11] rust: io: restrict untyped IO access and `register!` to `Region` Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` Gary Guo [this message]
2026-04-22 22:25 ` Claude review: rust: io: add view type Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 07/11] rust: dma: add methods to unsafely create reference from subview Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 08/11] rust: io: add `read_val` and `write_val` function on I/O view Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 09/11] gpu: nova-core: use I/O projection for cleaner encapsulation Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 10/11] rust: dma: drop `dma_read!` and `dma_write!` API Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-21 14:56 ` [PATCH v2 11/11] rust: io: add copying methods Gary Guo
2026-04-22 22:25 ` Claude review: " Claude Code Review Bot
2026-04-22 22:25 ` Claude review: rust: I/O type generalization and projection Claude Code Review Bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260421-io_projection-v2-6-4c251c692ef4@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nouveau@lists.freedesktop.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox