[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <442B2871-4512-4C5B-A394-C9CA14FF4C3C@collabora.com>
Date: Wed, 16 Jul 2025 19:19:49 -0300
From: Daniel Almeida <daniel.almeida@...labora.com>
To: Danilo Krummrich <dakr@...nel.org>
Cc: abdiel.janulgue@...il.com,
robin.murphy@....com,
a.hindborg@...nel.org,
ojeda@...nel.org,
alex.gaynor@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
lossin@...nel.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
bhelgaas@...gle.com,
kwilczynski@...nel.org,
gregkh@...uxfoundation.org,
rafael@...nel.org,
rust-for-linux@...r.kernel.org,
linux-pci@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 2/5] rust: dma: add DMA addressing capabilities
For some reason this diff did not apply on top of your latest commit (corrupt
patch on line 6)
>
> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
> index afd3ba538e3c..ad69ef316295 100644
> --- a/rust/kernel/dma.rs
> +++ b/rust/kernel/dma.rs
> @@ -81,25 +81,6 @@ unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
> /// never exceed the bit width of `u64`.
> ///
> /// This is the Rust equivalent of the C macro `DMA_BIT_MASK()`.
> -///
> -/// # Examples
> -///
> -/// ```
> -/// use kernel::dma::DmaMask;
> -///
> -/// let mask0 = DmaMask::new(0)?;
> -/// assert_eq!(mask0.value(), 0);
> -///
> -/// let mask1 = DmaMask::new(1)?;
> -/// assert_eq!(mask1.value(), 0b1);
> -///
> -/// let mask64 = DmaMask::new(64)?;
> -/// assert_eq!(mask64.value(), u64::MAX);
> -///
> -/// let mask_overflow = DmaMask::new(100);
> -/// assert!(mask_overflow.is_err());
> -/// # Ok::<(), Error>(())
> -/// ```
> #[derive(Debug, Clone, Copy, PartialEq, Eq)]
> pub struct DmaMask(u64);
>
> @@ -108,8 +89,27 @@ impl DmaMask {
> ///
> /// For `n <= 64`, sets exactly the lowest `n` bits.
> /// For `n > 64`, returns [`EINVAL`].
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::dma::DmaMask;
> + ///
> + /// let mask0 = DmaMask::try_new(0)?;
> + /// assert_eq!(mask0.value(), 0);
> + ///
> + /// let mask1 = DmaMask::try_new(1)?;
> + /// assert_eq!(mask1.value(), 0b1);
> + ///
> + /// let mask64 = DmaMask::try_new(64)?;
> + /// assert_eq!(mask64.value(), u64::MAX);
> + ///
> + /// let mask_overflow = DmaMask::try_new(100);
> + /// assert!(mask_overflow.is_err());
> + /// # Ok::<(), Error>(())
> + /// ```
> #[inline]
> - pub const fn new(n: usize) -> Result<Self> {
> + pub const fn try_new(n: u32) -> Result<Self> {
> Ok(Self(match n {
> 0 => 0,
> 1..=64 => u64::MAX >> (64 - n),
> @@ -117,6 +117,38 @@ pub const fn new(n: usize) -> Result<Self> {
> }))
> }
>
> + /// Constructs a `DmaMask` with the lowest `n` bits set to `1`.
> + ///
> + /// For `n <= 64`, sets exactly the lowest `n` bits.
> + /// For `n > 64`, results in a build error.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::dma::DmaMask;
> + /// use kernel::bits::genmask_u64;
There is also this, which is not upstream (nor used in the patch)
> + ///
> + /// let mask0 = DmaMask::new::<0>();
> + /// assert_eq!(mask0.value(), 0);
> + ///
> + /// let mask1 = DmaMask::new::<1>();
> + /// assert_eq!(mask1.value(), 0b1);
> + ///
> + /// let mask64 = DmaMask::new::<64>();
> + /// assert_eq!(mask64.value(), u64::MAX);
> + ///
> + /// // Build failure.
> + /// // let mask_overflow = DmaMask::new::<100>();
> + /// ```
> + #[inline(always)]
> + pub const fn new<const N: u32>() -> Self {
> + let Ok(mask) = Self::try_new(N) else {
> + build_error!("Invalid DMA Mask.");
> + };
> +
> + mask
> + }
> +
> /// Returns the underlying `u64` bitmask value.
> #[inline]
> pub const fn value(&self) -> u64 {
> diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
> index 9422ac68c139..c5e7cce68654 100644
> --- a/samples/rust/rust_dma.rs
> +++ b/samples/rust/rust_dma.rs
> @@ -58,7 +58,7 @@ impl pci::Driver for DmaSampleDriver {
> fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self>>> {
> dev_info!(pdev.as_ref(), "Probe DMA test driver.\n");
>
> - let mask = DmaMask::new(64)?;
> + let mask = DmaMask::new::<64>();
>
> // SAFETY: There are no concurrent calls to DMA allocation and mapping primitives.
> unsafe { pdev.dma_set_mask_and_coherent(mask)? };
>
>
I hand-applied this and fixed the issue above. All doctests pass FYI.
— Daniel
Powered by blists - more mailing lists