mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Andreas Hindborg" <a.hindborg@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>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Joel Fernandes" <joelagnelf@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>, "Zhi Wang" <zhiw@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	<driver-core@lists.linux.dev>, <rust-for-linux@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/7] rust: dma: add from-slice constructors for Coherent and CoherentBox
Date: Mon, 23 Mar 2026 16:55:37 +0000	[thread overview]
Message-ID: <DHABINRM1861.3HQ3J4IZ150F4@garyguo.net> (raw)
In-Reply-To: <20260321-b4-nova-dma-removal-v1-1-5cf18a75ff64@nvidia.com>

On Sat Mar 21, 2026 at 1:36 PM GMT, Alexandre Courbot wrote:
> A very common pattern is to create a block of coherent memory with the
> content of an already-existing slice of bytes (e.g. a loaded firmware
> blob).
>
> `CoherentBox` makes this easier, but still implies a potentially
> panicking operation with `copy_from_slice` that requires a `PANIC`
> comment.
>
> Add `from_slice_with_attrs` and `from_slice` methods to both `Coherent`
> and `CoherentBox` to turn this into a trivial one-step operation.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/dma.rs | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 102 insertions(+)
>
> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
> index 6d2bec52806b..a5cc993c919e 100644
> --- a/rust/kernel/dma.rs
> +++ b/rust/kernel/dma.rs
> @@ -453,6 +453,62 @@ pub fn init_at<E>(&mut self, i: usize, init: impl Init<T, E>) -> Result
>  
>          Ok(())
>      }
> +
> +    /// Allocates a region of coherent memory of the same size as `data` and initializes it with a
> +    /// copy of its contents.
> +    ///
> +    /// This is the [`CoherentBox`] variant of [`Coherent::from_slice_with_attrs`].
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use core::ops::Deref;
> +    ///
> +    /// # use kernel::device::{Bound, Device};
> +    /// use kernel::dma::{
> +    ///     attrs::*,
> +    ///     CoherentBox
> +    /// };
> +    ///
> +    /// # fn test(dev: &Device<Bound>) -> Result {
> +    /// let data = [0u8, 1u8, 2u8, 3u8];
> +    /// let c: CoherentBox<[u8]> =
> +    ///     CoherentBox::from_slice_with_attrs(dev, &data, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
> +    ///
> +    /// assert_eq!(c.deref(), &data);
> +    /// # Ok::<(), Error>(()) }
> +    /// ```
> +    pub fn from_slice_with_attrs(
> +        dev: &device::Device<Bound>,
> +        data: &[T],
> +        gfp_flags: kernel::alloc::Flags,
> +        dma_attrs: Attrs,
> +    ) -> Result<Self>
> +    where
> +        T: Copy,
> +    {
> +        Coherent::<T>::alloc_slice_with_attrs(dev, data.len(), gfp_flags, dma_attrs)
> +            .map(Self)

I'd rather just use `?` and not use map.

> +            .map(|mut slice| {
> +                // PANIC: `slice` was created with length `data.len()`.
> +                slice.copy_from_slice(data);
> +                slice
> +            })
> +    }
> +
> +    /// Performs the same functionality as [`CoherentBox::from_slice_with_attrs`], except the
> +    /// `dma_attrs` is 0 by default.
> +    #[inline]
> +    pub fn from_slice(
> +        dev: &device::Device<Bound>,
> +        data: &[T],
> +        gfp_flags: kernel::alloc::Flags,
> +    ) -> Result<Self>
> +    where
> +        T: Copy,
> +    {
> +        Self::from_slice_with_attrs(dev, data, gfp_flags, Attrs(0))
> +    }
>  }
>  
>  impl<T: AsBytes + FromBytes> CoherentBox<T> {
> @@ -827,6 +883,52 @@ pub fn zeroed_slice(
>      ) -> Result<Coherent<[T]>> {
>          Self::zeroed_slice_with_attrs(dev, len, gfp_flags, Attrs(0))
>      }
> +
> +    /// Allocates a region of coherent memory of the same size as `data` and initializes it with a
> +    /// copy of its contents.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// # use kernel::device::{Bound, Device};
> +    /// use kernel::dma::{
> +    ///     attrs::*,
> +    ///     Coherent
> +    /// };
> +    ///
> +    /// # fn test(dev: &Device<Bound>) -> Result {
> +    /// let data = [0u8, 1u8, 2u8, 3u8];
> +    /// // `c` has the same content as `data`.
> +    /// let c: Coherent<[u8]> =
> +    ///     Coherent::from_slice_with_attrs(dev, &data, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
> +    ///
> +    /// # Ok::<(), Error>(()) }
> +    /// ```
> +    pub fn from_slice_with_attrs(
> +        dev: &device::Device<Bound>,
> +        data: &[T],
> +        gfp_flags: kernel::alloc::Flags,
> +        dma_attrs: Attrs,
> +    ) -> Result<Coherent<[T]>>
> +    where
> +        T: Copy,
> +    {
> +        CoherentBox::from_slice_with_attrs(dev, data, gfp_flags, dma_attrs).map(Into::into)

This function can be inline as it's just wrapping another.

Best,
Gary

> +    }
> +
> +    /// Performs the same functionality as [`Coherent::from_slice_with_attrs`], except the
> +    /// `dma_attrs` is 0 by default.
> +    #[inline]
> +    pub fn from_slice(
> +        dev: &device::Device<Bound>,
> +        data: &[T],
> +        gfp_flags: kernel::alloc::Flags,
> +    ) -> Result<Coherent<[T]>>
> +    where
> +        T: Copy,
> +    {
> +        Self::from_slice_with_attrs(dev, data, gfp_flags, Attrs(0))
> +    }
>  }
>  
>  impl<T> Coherent<[T]> {


  reply	other threads:[~2026-03-23 16:55 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-21 13:36 [PATCH 0/7] rust: dma: add from-slice constructors and use them in nova-core Alexandre Courbot
2026-03-21 13:36 ` [PATCH 1/7] rust: dma: add from-slice constructors for Coherent and CoherentBox Alexandre Courbot
2026-03-23 16:55   ` Gary Guo [this message]
2026-03-26 14:59     ` Alexandre Courbot
2026-03-26 15:02       ` Danilo Krummrich
2026-03-27 10:39       ` Miguel Ojeda
2026-03-24 14:29   ` Andreas Hindborg
2026-03-21 13:36 ` [PATCH 2/7] gpu: nova-core: firmware: riscv: use dma::Coherent Alexandre Courbot
2026-03-21 14:58   ` Gary Guo
2026-03-23  6:15     ` Alexandre Courbot
2026-03-23 13:05       ` Gary Guo
2026-03-23 14:33         ` Alexandre Courbot
2026-03-21 13:36 ` [PATCH 3/7] gpu: nova-core: firmware: fwsec: " Alexandre Courbot
2026-03-21 13:36 ` [PATCH 4/7] gpu: nova-core: falcon: " Alexandre Courbot
2026-03-25  2:14   ` Eliot Courtney
2026-03-26 15:04     ` Alexandre Courbot
2026-03-26 15:35       ` Gary Guo
2026-03-28 13:03         ` Alexandre Courbot
2026-03-21 13:36 ` [PATCH 5/7] gpu: nova-core: fb: " Alexandre Courbot
2026-03-21 13:36 ` [PATCH 6/7] gpu: nova-core: firmware: gsp: use dma::Coherent for signatures Alexandre Courbot
2026-03-21 13:36 ` [PATCH 7/7] gpu: nova-core: firmware: gsp: use dma::Coherent for level0 table Alexandre Courbot
2026-03-23 17:01 ` [PATCH 0/7] rust: dma: add from-slice constructors and use them in nova-core 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=DHABINRM1861.3HQ3J4IZ150F4@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=apopple@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=ecourtney@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=zhiw@nvidia.com \
    /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

all inboxes | Powered by JetHome®