[RFC PATCH v2 05/10] samples: rust: dma: configure DMA through dma::Setup

From: Matteo Kloiber

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


Replace the unsafe dma::Device setter with dma::Setup::configure() and
use the returned dma::Handle for the coherent allocation and the SG
table. Both APIs still take a device, so hand them dma.device() for
now.

Set the maximum segment size to 64 KiB explicitly. This is the value
the PCI core sets for every device, and the sample relied on it
implicitly.

Signed-off-by: Matteo Kloiber <kernel@xxxxxxxxxxx>
---
samples/rust/rust_dma.rs | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
index b6a36d10bf74..c84107e5b2c3 100644
--- a/samples/rust/rust_dma.rs
+++ b/samples/rust/rust_dma.rs
@@ -4,15 +4,17 @@
//!
//! To make this driver probe, QEMU must be run with `-device pci-testdev`.

+use core::num::NonZeroU32;
+
use kernel::{
device::{
Bound,
Core, //
},
dma::{
+ self,
Coherent,
DataDirection,
- Device,
DmaMask, //
},
io::{
@@ -27,6 +29,7 @@
Owned,
SGTable, //
}, //
+ sizes::SizeConstants,
};

struct DmaSampleDriver;
@@ -77,19 +80,23 @@ impl pci::Driver for DmaSampleDriver {

fn probe<'bound>(
pdev: &'bound pci::Device<Core<'_>>,
- _tokens: pci::InitTokens<'bound>,
+ tokens: pci::InitTokens<'bound>,
_info: Option<&'bound Self::IdInfo>,
) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
+ let pci::InitTokens { dma: setup, .. } = tokens;
pin_init::pin_init_scope(move || {
dev_info!(pdev, "Probe DMA test driver.\n");

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)? };
+ let dma = setup.configure(dma::Params {
+ streaming_mask: Some(mask),
+ coherent_mask: Some(mask),
+ max_seg_size: Some(const { NonZeroU32::new(u32::SZ_64K).unwrap() }),
+ })?;

let ca: Coherent<'_, [MyStruct]> =
- Coherent::zeroed_slice(pdev.as_ref(), TEST_VALUES.len(), GFP_KERNEL)?;
+ Coherent::zeroed_slice(dma.device(), TEST_VALUES.len(), GFP_KERNEL)?;

for (i, value) in TEST_VALUES.into_iter().enumerate() {
io_project!(ca, [panic: i]).copy_write(MyStruct::new(value.0, value.1));
@@ -98,7 +105,7 @@ fn probe<'bound>(
let size = 4 * page::PAGE_SIZE;
let pages = VVec::with_capacity(size, GFP_KERNEL)?;

- let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL);
+ let sgt = SGTable::new(dma.device(), pages, DataDirection::ToDevice, GFP_KERNEL);

Ok(try_pin_init!(Self::Data {
pdev,
--
2.51.2