[PATCH] rust: dma: allow drivers to tune max segment size

Beata Michalska posted 1 patch 2 weeks ago
There is a newer version of this series
rust/helpers/dma.c |  5 +++++
rust/kernel/dma.rs | 17 +++++++++++++++++
2 files changed, 22 insertions(+)
[PATCH] rust: dma: allow drivers to tune max segment size
Posted by Beata Michalska 2 weeks ago
Make dma_set_max_seg_size() available to Rust so drivers can perform
standard DMA setup steps.

Signed-off-by: Beata Michalska <beata.michalska@arm.com>
---
 rust/helpers/dma.c |  5 +++++
 rust/kernel/dma.rs | 17 +++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c
index 2afa32c21c94..220b9308830f 100644
--- a/rust/helpers/dma.c
+++ b/rust/helpers/dma.c
@@ -40,3 +40,8 @@ size_t rust_helper_dma_max_mapping_size(struct device *dev)
 {
 	return dma_max_mapping_size(dev);
 }
+
+void rust_helper_dma_set_max_seg_size(struct device *dev, unsigned int size)
+{
+	dma_set_max_seg_size(dev, size);
+}
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index acc65b1e0f24..badcf6b5705a 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -85,6 +85,23 @@ unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
             bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), mask.value())
         })
     }
+
+    /// Set the maximum size of a single DMA segment the device may request.
+    ///
+    /// This method is usually called once from `probe()` as soon as the device capabilities are
+    /// known.
+    ///
+    /// # Safety
+    ///
+    /// This method must not be called if there are any in-flight DMA allocation
+    /// or mapping operations.
+    unsafe fn dma_set_max_seg_size(&self, size: u32) {
+        // SAFETY:
+        // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
+        // - The safety requirement of this function guarantees that there are no concurrent calls
+        //   to DMA allocation and mapping operations relying on this parameter.
+        unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) }
+    }
 }
 
 /// A DMA mask that holds a bitmask with the lowest `n` bits set.
-- 
2.25.1
Re: [PATCH] rust: dma: allow drivers to tune max segment size
Posted by Danilo Krummrich 2 weeks ago
On Fri Jan 23, 2026 at 11:18 AM CET, Beata Michalska wrote:
> +    /// Set the maximum size of a single DMA segment the device may request.
> +    ///
> +    /// This method is usually called once from `probe()` as soon as the device capabilities are
> +    /// known.
> +    ///
> +    /// # Safety
> +    ///
> +    /// This method must not be called if there are any in-flight DMA allocation
> +    /// or mapping operations.

Yes, unfortunately it has the same requirements as the DMA mask setters.

For consistency, can you please use the exact same safety requirement text as
the other methods?

> +    unsafe fn dma_set_max_seg_size(&self, size: u32) {
> +        // SAFETY:
> +        // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
> +        // - The safety requirement of this function guarantees that there are no concurrent calls
> +        //   to DMA allocation and mapping operations relying on this parameter.
> +        unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) }
> +    }
>  }
>  
>  /// A DMA mask that holds a bitmask with the lowest `n` bits set.
> -- 
> 2.25.1
Re: [PATCH] rust: dma: allow drivers to tune max segment size
Posted by Beata Michalska 2 weeks ago
On Fri, Jan 23, 2026 at 11:40:47AM +0100, Danilo Krummrich wrote:
> On Fri Jan 23, 2026 at 11:18 AM CET, Beata Michalska wrote:
> > +    /// Set the maximum size of a single DMA segment the device may request.
> > +    ///
> > +    /// This method is usually called once from `probe()` as soon as the device capabilities are
> > +    /// known.
> > +    ///
> > +    /// # Safety
> > +    ///
> > +    /// This method must not be called if there are any in-flight DMA allocation
> > +    /// or mapping operations.
> 
> Yes, unfortunately it has the same requirements as the DMA mask setters.
> 
> For consistency, can you please use the exact same safety requirement text as
> the other methods?

Done:
https://lore.kernel.org/rust-for-linux/20260123131308.3218421-1-beata.michalska@arm.com/T/#u

---
BR
Beata
> 
> > +    unsafe fn dma_set_max_seg_size(&self, size: u32) {
> > +        // SAFETY:
> > +        // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid.
> > +        // - The safety requirement of this function guarantees that there are no concurrent calls
> > +        //   to DMA allocation and mapping operations relying on this parameter.
> > +        unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) }
> > +    }
> >  }
> >  
> >  /// A DMA mask that holds a bitmask with the lowest `n` bits set.
> > -- 
> > 2.25.1
>