Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: Gary Guo <gary@garyguo.net>
Cc: "Alice Ryhl" <aliceryhl@google.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>,
	"Alexandre Courbot" <acourbot@nvidia.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 v6 18/20] rust: dma: drop `dma_read!` and `dma_write!` API
Date: Fri, 10 Jul 2026 23:04:32 -0300	[thread overview]
Message-ID: <4990B3CF-C19E-4282-B749-D02ABECD59DC@collabora.com> (raw)
In-Reply-To: <20260706-io_projection-v6-18-72cd5d055d54@garyguo.net>



> On 6 Jul 2026, at 09:44, Gary Guo <gary@garyguo.net> wrote:
> 
> The primitive read/write use case is covered by the `io_read!` and
> `io_write!` macro. The non-primitive use case was finicky; they should
> either be achieved using `CoherentBox` or `as_ref()/as_mut()` to assert the
> lack of concurrent access, or should be using memcpy-like APIs to express
> the non-atomic and tearable nature.
> 
> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> rust/kernel/dma.rs       | 128 -----------------------------------------------
> samples/rust/rust_dma.rs |  11 ++--
> 2 files changed, 8 insertions(+), 131 deletions(-)
> 
> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
> index 1535bc6eec64..6e7ea3b72f2f 100644
> --- a/rust/kernel/dma.rs
> +++ b/rust/kernel/dma.rs
> @@ -661,52 +661,6 @@ pub unsafe fn as_mut(&self) -> &mut T {
>         // SAFETY: per safety requirement.
>         unsafe { &mut *self.as_mut_ptr() }
>     }
> -
> -    /// Reads the value of `field` and ensures that its type is [`FromBytes`].
> -    ///
> -    /// # Safety
> -    ///
> -    /// This must be called from the [`dma_read`] macro which ensures that the `field` pointer is
> -    /// validated beforehand.
> -    ///
> -    /// Public but hidden since it should only be used from [`dma_read`] macro.
> -    #[doc(hidden)]
> -    pub unsafe fn field_read<F: FromBytes>(&self, field: *const F) -> F {
> -        // SAFETY:
> -        // - By the safety requirements field is valid.
> -        // - Using read_volatile() here is not sound as per the usual rules, the usage here is
> -        // a special exception with the following notes in place. When dealing with a potential
> -        // race from a hardware or code outside kernel (e.g. user-space program), we need that
> -        // read on a valid memory is not UB. Currently read_volatile() is used for this, and the
> -        // rationale behind is that it should generate the same code as READ_ONCE() which the
> -        // kernel already relies on to avoid UB on data races. Note that the usage of
> -        // read_volatile() is limited to this particular case, it cannot be used to prevent
> -        // the UB caused by racing between two kernel functions nor do they provide atomicity.
> -        unsafe { field.read_volatile() }
> -    }
> -
> -    /// Writes a value to `field` and ensures that its type is [`AsBytes`].
> -    ///
> -    /// # Safety
> -    ///
> -    /// This must be called from the [`dma_write`] macro which ensures that the `field` pointer is
> -    /// validated beforehand.
> -    ///
> -    /// Public but hidden since it should only be used from [`dma_write`] macro.
> -    #[doc(hidden)]
> -    pub unsafe fn field_write<F: AsBytes>(&self, field: *mut F, val: F) {
> -        // SAFETY:
> -        // - By the safety requirements field is valid.
> -        // - Using write_volatile() here is not sound as per the usual rules, the usage here is
> -        // a special exception with the following notes in place. When dealing with a potential
> -        // race from a hardware or code outside kernel (e.g. user-space program), we need that
> -        // write on a valid memory is not UB. Currently write_volatile() is used for this, and the
> -        // rationale behind is that it should generate the same code as WRITE_ONCE() which the
> -        // kernel already relies on to avoid UB on data races. Note that the usage of
> -        // write_volatile() is limited to this particular case, it cannot be used to prevent
> -        // the UB caused by racing between two kernel functions nor do they provide atomicity.
> -        unsafe { field.write_volatile(val) }
> -    }
> }
> 
> impl<T: AsBytes + FromBytes> Coherent<T> {
> @@ -1266,85 +1220,3 @@ fn as_view(self) -> CoherentView<'a, Self::Target> {
>         }
>     }
> }
> -
> -/// Reads a field of an item from an allocated region of structs.
> -///
> -/// The syntax is of the form `kernel::dma_read!(dma, proj)` where `dma` is an expression evaluating
> -/// to a [`Coherent`] and `proj` is a [projection specification](kernel::ptr::project!).
> -///
> -/// # Examples
> -///
> -/// ```
> -/// use kernel::device::Device;
> -/// use kernel::dma::{attrs::*, Coherent};
> -///
> -/// 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(alloc: &kernel::dma::Coherent<[MyStruct]>) -> Result {
> -/// let whole = kernel::dma_read!(alloc, [try: 2]);
> -/// let field = kernel::dma_read!(alloc, [panic: 1].field);
> -/// # Ok::<(), Error>(()) }
> -/// ```
> -#[macro_export]
> -macro_rules! dma_read {
> -    ($dma:expr, $($proj:tt)*) => {{
> -        let dma = &$dma;
> -        let ptr = $crate::ptr::project!(
> -            $crate::dma::Coherent::as_ptr(dma), $($proj)*
> -        );
> -        // SAFETY: The pointer created by the projection is within the DMA region.
> -        unsafe { $crate::dma::Coherent::field_read(dma, ptr) }
> -    }};
> -}
> -
> -/// Writes to a field of an item from an allocated region of structs.
> -///
> -/// The syntax is of the form `kernel::dma_write!(dma, proj, val)` where `dma` is an expression
> -/// evaluating to a [`Coherent`], `proj` is a
> -/// [projection specification](kernel::ptr::project!), and `val` is the value to be written to the
> -/// projected location.
> -///
> -/// # Examples
> -///
> -/// ```
> -/// use kernel::device::Device;
> -/// use kernel::dma::{attrs::*, Coherent};
> -///
> -/// struct MyStruct { member: 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(alloc: &kernel::dma::Coherent<[MyStruct]>) -> Result {
> -/// kernel::dma_write!(alloc, [try: 2].member, 0xf);
> -/// kernel::dma_write!(alloc, [panic: 1], MyStruct { member: 0xf });
> -/// # Ok::<(), Error>(()) }
> -/// ```
> -#[macro_export]
> -macro_rules! dma_write {
> -    (@parse [$dma:expr] [$($proj:tt)*] [, $val:expr]) => {{
> -        let dma = &$dma;
> -        let ptr = $crate::ptr::project!(
> -            mut $crate::dma::Coherent::as_mut_ptr(dma), $($proj)*
> -        );
> -        let val = $val;
> -        // SAFETY: The pointer created by the projection is within the DMA region.
> -        unsafe { $crate::dma::Coherent::field_write(dma, ptr, val) }
> -    }};
> -    (@parse [$dma:expr] [$($proj:tt)*] [.$field:tt $($rest:tt)*]) => {
> -        $crate::dma_write!(@parse [$dma] [$($proj)* .$field] [$($rest)*])
> -    };
> -    (@parse [$dma:expr] [$($proj:tt)*] [[$flavor:ident: $index:expr] $($rest:tt)*]) => {
> -        $crate::dma_write!(@parse [$dma] [$($proj)* [$flavor: $index]] [$($rest)*])
> -    };
> -    ($dma:expr, $($rest:tt)*) => {
> -        $crate::dma_write!(@parse [$dma] [] [$($rest)*])
> -    };
> -}
> diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
> index 5046b4628d0e..4af46e99d2dd 100644
> --- a/samples/rust/rust_dma.rs
> +++ b/samples/rust/rust_dma.rs
> @@ -12,6 +12,10 @@
>         Device,
>         DmaMask, //
>     },
> +    io::{
> +        io_project,
> +        io_read, //
> +    },
>     page, pci,
>     prelude::*,
>     scatterlist::{Owned, SGTable},
> @@ -77,7 +81,8 @@ fn probe<'bound>(
>                 Coherent::zeroed_slice(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
> 
>             for (i, value) in TEST_VALUES.into_iter().enumerate() {
> -                kernel::dma_write!(ca, [try: i], MyStruct::new(value.0, value.1));
> +                // SAFETY: `ca` is not yet shared with device or other threads.
> +                unsafe { *io_project!(ca, [panic: i]).as_mut() = MyStruct::new(value.0, value.1) };
>             }
> 
>             let size = 4 * page::PAGE_SIZE;
> @@ -97,8 +102,8 @@ fn probe<'bound>(
> impl DmaSampleDriver {
>     fn check_dma(&self) {
>         for (i, value) in TEST_VALUES.into_iter().enumerate() {
> -            let val0 = kernel::dma_read!(self.ca, [panic: i].h);
> -            let val1 = kernel::dma_read!(self.ca, [panic: i].b);
> +            let val0 = io_read!(self.ca, [panic: i].h);
> +            let val1 = io_read!(self.ca, [panic: i].b);
> 
>             assert_eq!(val0, value.0);
>             assert_eq!(val1, value.1);
> 
> -- 
> 2.54.0
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>


  reply	other threads:[~2026-07-11  2:05 UTC|newest]

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

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=4990B3CF-C19E-4282-B749-D02ABECD59DC@collabora.com \
    --to=daniel.almeida@collabora.com \
    --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=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