NVIDIA GPU driver infrastructure
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Gary Guo" <gary@garyguo.net>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Michal Wilczynski" <m.wilczynski@samsung.com>,
	"Uwe Kleine-König" <ukleinek@kernel.org>,
	"Danilo Krummrich" <dakr@kernel.org>,
	driver-core@lists.linux.dev, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
	nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
	linux-pwm@vger.kernel.org
Subject: Re: [PATCH v5 19/20] rust: io: add copying methods
Date: Sun, 05 Jul 2026 22:58:33 +0900	[thread overview]
Message-ID: <DJQOVQU813BE.YEOLP9RLAFK@nvidia.com> (raw)
In-Reply-To: <20260626-io_projection-v5-19-d0961471ae50@garyguo.net>

On Fri Jun 26, 2026 at 11:45 PM JST, Gary Guo wrote:
<...>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index aa82736253ac..b5ac3ac86bbd 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -5,7 +5,8 @@
>  //! C header: [`include/asm-generic/io.h`](srctree/include/asm-generic/io.h)
>  
>  use core::{
> -    marker::PhantomData, //
> +    marker::PhantomData,
> +    mem::MaybeUninit, //
>  };
>  
>  use crate::{
> @@ -271,6 +272,61 @@ pub trait IoCapable<T>: IoBackend {
>      fn io_write<'a>(view: Self::View<'a, T>, value: T);
>  }
>  
> +/// Trait indicating that an I/O backend supports memory copy operations.
> +pub trait IoCopyable: IoBackend {
> +    /// Copy contents of `view` to `buffer`.
> +    ///
> +    /// # Safety
> +    ///
> +    /// - `buffer` is valid for volatile write for `view.size()` bytes.
> +    unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8);

Should we also define whether overlapping regions are allowed or not? I
guess the shape of the API assumes that the regions don't overlap, but
it might be useful to state it clearly.

> +
> +    /// Copy `size` bytes from `buffer` to `address`.

There are no `size` or `address` parameters.

> +    ///
> +    /// # Safety
> +    ///
> +    /// - `buffer` is valid for volatile read for `view.size()` bytes.
> +    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8);
> +
> +    /// Copy from `view` and return the value.
> +    #[inline]
> +    fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
> +        // Project `self` to `[u8]`.
> +        let ptr = Self::as_ptr(view);
> +        // SAFETY: This is a identity projection.
> +        let slice_view = unsafe {
> +            Self::project_view(
> +                view,
> +                core::ptr::slice_from_raw_parts_mut::<u8>(ptr.cast(), size_of::<T>()),
> +            )
> +        };
> +
> +        let mut buf = MaybeUninit::<T>::uninit();
> +        // SAFETY: `buf.as_mut_ptr()` is valid for write for `size_of::<T>()` bytes.
> +        unsafe { Self::copy_from_io(slice_view, buf.as_mut_ptr().cast()) };
> +        // SAFETY: T: FromBytes` guarantee that all bit patterns are valid.

Missing `.

> +        unsafe { buf.assume_init() }
> +    }
> +
> +    /// Copy `value` to `view`.
> +    #[inline]
> +    fn copy_write<T: IntoBytes>(view: Self::View<'_, T>, value: T) {
> +        // Project `self` to `[u8]`.
> +        let ptr = Self::as_ptr(view);
> +        // SAFETY: This is a identity projection.
> +        let slice_view = unsafe {
> +            Self::project_view(
> +                view,
> +                core::ptr::slice_from_raw_parts_mut::<u8>(ptr.cast(), size_of::<T>()),
> +            )
> +        };
> +
> +        // SAFETY: `&raw const value` is valid for read for `size_of::<T>()` bytes.
> +        unsafe { Self::copy_to_io(slice_view, (&raw const value).cast()) };
> +        core::mem::forget(value);

Maybe we should mention in the doccomment that the destructor of `value`
is not run, neither by the end of this method nor at any point in the
future IIUC, because the `View` doesn't own the object that has been
copied into it. Or we can sidestep the problem by taking a `value: &T`.

> +    }
> +}
> +
>  /// Describes a given I/O location: its offset, width, and type to convert the raw value from and
>  /// into.
>  ///
> @@ -350,6 +406,24 @@ fn size(self) -> usize {
>          KnownSize::size(Self::Backend::as_ptr(self.as_view()))
>      }
>  
> +    /// Returns the length of the slice in number of elements.
> +    #[inline]
> +    fn len<T>(self) -> usize
> +    where
> +        Self: Io<'a, Target = [T]>,
> +    {
> +        Self::Backend::as_ptr(self.as_view()).len()
> +    }
> +
> +    /// Returns `true` if the slice has a length of 0.
> +    #[inline]
> +    fn is_empty<T>(self) -> bool
> +    where
> +        Self: Io<'a, Target = [T]>,
> +    {
> +        self.len() == 0
> +    }

nit: these two could have been introduced alongside `size` in patch 12,
or all 3 in the present patch. It's a bit inconsistent that `size` stays
unused all this time, while these two related ones are introduced right
when they become useful.

> +
>      /// Try to convert 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
> @@ -437,6 +511,115 @@ fn write_val(self, value: Self::Target)
>          Self::Backend::io_write(self.as_view(), value)
>      }
>  
> +    /// Copy-read from I/O memory.
> +    ///
> +    /// This is equivalent to reading from the I/O memory with byte-wise copy, although the actual
> +    /// implementation might be more efficient. There is no atomicity guarantee. Note that for some
> +    /// backends (e.g. `Mmio`), this can read different value compared to [`read_val`] as
> +    /// byte-swapping is not performed.
> +    ///
> +    /// [`read_val`]: Io::read_val
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```no_run
> +    /// # use kernel::io::*;
> +    /// # fn test_copy_read(mmio: Mmio<'_, [u8; 6]>) {
> +    /// // let mmio: Mmio<'_, [u8; 6]>;
> +    /// let val: [u8; 6] = mmio.copy_read();
> +    /// # }
> +    /// ```
> +    #[inline]
> +    fn copy_read(self) -> Self::Target
> +    where
> +        Self::Backend: IoCopyable,
> +        Self::Target: Sized + FromBytes,
> +    {
> +        Self::Backend::copy_read(self.as_view())
> +    }
> +
> +    /// Copy-write to I/O memory.
> +    ///
> +    /// This is equivalent to writing to the I/O memory with byte-wise copy, although the actual
> +    /// implementation might be more efficient. There is no atomicity guarantee. Note that for some
> +    /// backends (e.g. `Mmio`), this can read different value compared to [`write_val`] as

"this can write" I presume (copy/paste omission?)

> +    /// byte-swapping is not performed.
> +    ///
> +    /// [`write_val`]: Io::write_val
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```no_run
> +    /// # use kernel::io::*;
> +    /// # fn test_copy_write(mmio: Mmio<'_, [u8; 6]>) {
> +    /// // let mmio: Mmio<'_, [u8; 6]>;
> +    /// mmio.copy_write([0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]);
> +    /// # }
> +    /// ```
> +    #[inline]
> +    fn copy_write(self, value: Self::Target)
> +    where
> +        Self::Backend: IoCopyable,
> +        Self::Target: Sized + IntoBytes,
> +    {
> +        Self::Backend::copy_write(self.as_view(), value);
> +    }
> +
> +    /// Copy bytes from slice to I/O memory.

"from `data`" maybe.

> +    ///
> +    /// The length of `self` must be the same as `data`, similar to [`[u8]::copy_from_slice`].
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```no_run
> +    /// # use kernel::io::*;
> +    /// # fn test_copy_write(mmio: Mmio<'_, [u8]>) {
> +    /// // let mmio: Mmio<'_, [u8]>;
> +    /// mmio.copy_from_slice(&[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]);
> +    /// # }
> +    /// ```
> +    #[inline]
> +    fn copy_from_slice(self, data: &[u8])
> +    where
> +        Self::Backend: IoCopyable,
> +        Self: Io<'a, Target = [u8]>,
> +    {
> +        assert_eq!(self.len(), data.len());

I think this one deserves an entry in a `# Panics` section of the
doccomment (same for all other asserts).

> +
> +        // SAFETY: `data.as_ptr()` is valid for read for `self.size()` bytes.
> +        unsafe {
> +            Self::Backend::copy_to_io(self.as_view(), data.as_ptr());
> +        }
> +    }
> +
> +    /// Copy bytes from I/O memory to slice.

"to `data`".

> +    ///
> +    /// The length of `self` must be the same as `data`, similar to [`[u8]::copy_from_slice`].
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```no_run
> +    /// # use kernel::io::*;
> +    /// # fn test_copy_write(mmio: Mmio<'_, [u8]>) {
> +    /// // let mmio: Mmio<'_, [u8]>;
> +    /// let mut buf = [0; 6];
> +    /// mmio.copy_to_slice(&mut buf);
> +    /// # }
> +    /// ```
> +    #[inline]
> +    fn copy_to_slice(self, data: &mut [u8])
> +    where
> +        Self::Backend: IoCopyable,
> +        Self: Io<'a, Target = [u8]>,
> +    {
> +        assert_eq!(self.len(), data.len());
> +
> +        // SAFETY: `data.as_ptr()` is valid for write for `self.size()` bytes.

Should be `data.as_mut_ptr()`.

  reply	other threads:[~2026-07-05 13:58 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26 14:45 [PATCH v5 00/20] rust: I/O type generalization and projection Gary Guo
2026-06-26 14:45 ` [PATCH v5 01/20] rust: io: add dynamically-sized `Region` type Gary Guo
2026-07-03  3:16   ` Alexandre Courbot
2026-07-03 12:38     ` Gary Guo
2026-06-26 14:45 ` [PATCH v5 02/20] rust: io: add missing safety requirement in `IoCapable` methods Gary Guo
2026-07-03  2:57   ` Alexandre Courbot
2026-07-03 12:35     ` Gary Guo
2026-07-03 13:09       ` Alexandre Courbot
2026-07-03 13:49         ` Gary Guo
2026-07-05 13:59           ` Alexandre Courbot
2026-07-05 16:45             ` Gary Guo
2026-06-26 14:45 ` [PATCH v5 03/20] rust: io: restrict untyped IO access and `register!` to `Region` Gary Guo
2026-06-26 14:45 ` [PATCH v5 04/20] rust: io: implement `Io` on reference types instead Gary Guo
2026-06-26 14:45 ` [PATCH v5 05/20] rust: io: generalize `MmioRaw` to pointer to arbitrary type Gary Guo
2026-06-26 14:45 ` [PATCH v5 06/20] rust: io: rename `Mmio` to `MmioOwned` Gary Guo
2026-06-26 14:45 ` [PATCH v5 07/20] rust: io: implement `Mmio` as view type Gary Guo
2026-07-03  3:30   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 08/20] rust: pci: io: make `ConfigSpace` a view Gary Guo
2026-06-26 14:45 ` [PATCH v5 09/20] rust: io: use view types instead of addresses for `Io` Gary Guo
2026-07-03  5:03   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 10/20] pwm: th1520: remove unnecessary `deref` Gary Guo
2026-07-03  5:04   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 11/20] rust: io: remove `MmioOwned` Gary Guo
2026-07-03  5:58   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 12/20] rust: io: move `Io` methods to extension trait Gary Guo
2026-07-03  6:19   ` Alexandre Courbot
2026-07-03 12:43     ` Gary Guo
2026-06-26 14:45 ` [PATCH v5 13/20] rust: io: add projection macro and methods Gary Guo
2026-07-03  6:39   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 14/20] rust: io: add I/O backend for system memory with volatile access Gary Guo
2026-07-03 11:37   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 15/20] rust: io: implement a view type for `Coherent` Gary Guo
2026-07-03 13:14   ` Alexandre Courbot
2026-07-03 13:50     ` Gary Guo
2026-07-03 14:39       ` Alexandre Courbot
2026-07-03 14:44         ` Gary Guo
2026-06-26 14:45 ` [PATCH v5 16/20] rust: io: add `read_val` and `write_val` functions on `Io` Gary Guo
2026-07-03 14:43   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 17/20] gpu: nova-core: use I/O projection for cleaner encapsulation Gary Guo
2026-07-03 14:46   ` Alexandre Courbot
2026-07-03 15:13     ` Gary Guo
2026-06-26 14:45 ` [PATCH v5 18/20] rust: dma: drop `dma_read!` and `dma_write!` API Gary Guo
2026-07-03 14:47   ` Alexandre Courbot
2026-06-26 14:45 ` [PATCH v5 19/20] rust: io: add copying methods Gary Guo
2026-07-05 13:58   ` Alexandre Courbot [this message]
2026-06-26 14:45 ` [PATCH v5 20/20] rust: io: implement `IoSysMap` Gary Guo
2026-07-06  1:02   ` Alexandre Courbot
2026-06-28 23:37 ` [PATCH v5 00/20] rust: I/O type generalization and projection Danilo Krummrich
2026-06-29 14:15   ` Gary Guo
2026-06-30 13:02     ` Miguel Ojeda
2026-06-30 13:07       ` Gary Guo

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=DJQOVQU813BE.YEOLP9RLAFK@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.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=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=m.wilczynski@samsung.com \
    --cc=nova-gpu@lists.linux.dev \
    --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=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ukleinek@kernel.org \
    --cc=work@onurozkan.dev \
    /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