[RFC PATCH v2 10/10] rust: dma: remove the Device trait and its setters

From: Matteo Kloiber

Date: Sun Nov 29 2026 - 17:25:20 EST


All drivers configure DMA through dma::Setup now, and nothing calls the
unsafe dma::Device setters anymore. Keeping them would allow changing
the DMA parameters behind an existing dma::Handle.

Remove the trait, its PCI and platform implementations, and the
dma_set_mask_and_coherent() helper, which only the trait used.

With the setters gone, a driver can only change the DMA parameters
through its dma::Setup, and all Rust DMA APIs require the dma::Handle
that configure() returns. Update the safety comments of the PCI and
platform probe callbacks accordingly.

Signed-off-by: Matteo Kloiber <kernel@xxxxxxxxxxx>
---
rust/helpers/dma.c | 6 ---
rust/kernel/dma.rs | 83 +----------------------------------------
rust/kernel/pci.rs | 5 +--
rust/kernel/platform.rs | 5 +--
4 files changed, 5 insertions(+), 94 deletions(-)

diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c
index ff8f24dae9df..443360237606 100644
--- a/rust/helpers/dma.c
+++ b/rust/helpers/dma.c
@@ -17,12 +17,6 @@ __rust_helper void rust_helper_dma_free_attrs(struct device *dev, size_t size,
dma_free_attrs(dev, size, cpu_addr, dma_handle, attrs);
}

-__rust_helper int rust_helper_dma_set_mask_and_coherent(struct device *dev,
- u64 mask)
-{
- return dma_set_mask_and_coherent(dev, mask);
-}
-
__rust_helper int rust_helper_dma_set_mask(struct device *dev, u64 mask)
{
return dma_set_mask(dev, mask);
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 226d00397f56..6b06f57bbdd2 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -9,8 +9,7 @@
debugfs,
device::{
self,
- Bound,
- Core, //
+ Bound, //
},
error::to_result,
fs::file,
@@ -178,86 +177,6 @@ pub(crate) fn new_for_test(dev: &'bound device::Device<Bound>) -> Self {
}
}

-/// Trait to be implemented by DMA capable bus devices.
-///
-/// The [`dma::Device`](Device) trait should be implemented by bus specific device representations,
-/// where the underlying bus is DMA capable, such as:
-#[cfg_attr(CONFIG_PCI, doc = "* [`pci::Device`](kernel::pci::Device)")]
-/// * [`platform::Device`](::kernel::platform::Device)
-pub trait Device<'a>: AsRef<device::Device<Core<'a>>> {
- /// Set up the device's DMA streaming addressing capabilities.
- ///
- /// This method is usually called once from `probe()` as soon as the device capabilities are
- /// known.
- ///
- /// # Safety
- ///
- /// This method must not be called concurrently with any DMA allocation or mapping primitives,
- /// such as [`Coherent::zeroed`].
- unsafe fn dma_set_mask(&self, mask: DmaMask) -> Result {
- // 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 primitives using this mask.
- to_result(unsafe { bindings::dma_set_mask(self.as_ref().as_raw(), mask.value()) })
- }
-
- /// Set up the device's DMA coherent addressing capabilities.
- ///
- /// This method is usually called once from `probe()` as soon as the device capabilities are
- /// known.
- ///
- /// # Safety
- ///
- /// This method must not be called concurrently with any DMA allocation or mapping primitives,
- /// such as [`Coherent::zeroed`].
- unsafe fn dma_set_coherent_mask(&self, mask: DmaMask) -> Result {
- // 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 primitives using this mask.
- to_result(unsafe { bindings::dma_set_coherent_mask(self.as_ref().as_raw(), mask.value()) })
- }
-
- /// Set up the device's DMA addressing capabilities.
- ///
- /// This is a combination of [`Device::dma_set_mask`] and [`Device::dma_set_coherent_mask`].
- ///
- /// This method is usually called once from `probe()` as soon as the device capabilities are
- /// known.
- ///
- /// # Safety
- ///
- /// This method must not be called concurrently with any DMA allocation or mapping primitives,
- /// such as [`Coherent::zeroed`].
- unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result {
- // 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 primitives using this mask.
- to_result(unsafe {
- 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 concurrently with any DMA allocation or mapping primitives,
- /// such as [`Coherent::zeroed`].
- 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 primitives using 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.
///
/// Use [`DmaMask::new`] or [`DmaMask::try_new`] to construct a value. Values
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 10b00b641dea..a98fbc252cd2 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -150,7 +150,8 @@ extern "C" fn probe_callback(
from_result(|| {
// SAFETY: The bus initializes the DMA parameters before probing. This is the only
// setup token issued for this binding, and no driver code has run for the binding
- // yet.
+ // yet. The driver cannot use the Rust DMA allocation or mapping APIs until it
+ // consumes the token, and has no other way to change the DMA parameters.
let dma = unsafe { dma::Setup::new(pdev.as_ref()) };
let data = T::probe(pdev, InitTokens { dma }, info);

@@ -839,8 +840,6 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx>
kernel::impl_device_context_deref!(unsafe { Device });
kernel::impl_device_context_into_aref!(Device);

-impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}
-
// SAFETY: Instances of `Device` are always reference-counted.
unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
#[inline]
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index bdb2c5c09b21..a24114c212bf 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -107,7 +107,8 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff
from_result(|| {
// SAFETY: The bus initializes the DMA parameters before probing. This is the only
// setup token issued for this binding, and no driver code has run for the binding
- // yet.
+ // yet. The driver cannot use the Rust DMA allocation or mapping APIs until it
+ // consumes the token, and has no other way to change the DMA parameters.
let dma = unsafe { dma::Setup::new(pdev.as_ref()) };
let data = T::probe(pdev, InitTokens { dma }, info);

@@ -547,8 +548,6 @@ pub fn optional_irq_by_name(&self, name: &CStr) -> Result<IrqRequest<'_>> {
kernel::impl_device_context_deref!(unsafe { Device });
kernel::impl_device_context_into_aref!(Device);

-impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}
-
// SAFETY: Instances of `Device` are always reference-counted.
unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
fn inc_ref(&self) {
--
2.51.2