Re: [PATCH v2 4/4] rust: samples: add EDU PCI driver sample
From: Danilo Krummrich
Date: Fri Jul 03 2026 - 17:12:46 EST
On Sun Jun 21, 2026 at 2:27 PM CEST, Danilo Krummrich wrote:
> I'm going to send a patch to make the irq::Registration compatible with the
> device driver lifetime rework soon. Please rebase onto that once it's sent, so
> this sample can land as idiomatic as possible.
Please find the patch in [1]; purely mechanical conversion of the EDU driver for
testing purposes in [2].
[1] https://lore.kernel.org/driver-core/20260703210936.1128698-1-dakr@xxxxxxxxxx/
[2]
diff --git a/samples/rust/rust_driver_edu.rs b/samples/rust/rust_driver_edu.rs
index 5f4efd514032..301b780773cb 100644
--- a/samples/rust/rust_driver_edu.rs
+++ b/samples/rust/rust_driver_edu.rs
@@ -6,7 +6,6 @@
use kernel::{
device::Bound,
- devres::Devres,
dma::{Coherent, Device, DmaMask},
io::{
poll::read_poll_timeout,
@@ -16,7 +15,7 @@
irq::{self, Flags},
pci::{self, IrqTypes},
prelude::*,
- sync::{aref::ARef, Arc, Completion},
+ sync::Completion,
time::Delta, //
};
@@ -66,20 +65,21 @@ mod regs {
pub(super) const END: usize = 0xA0;
}
-type Bar0 = pci::Bar<'static, { regs::END }>;
+type Bar0<'a> = pci::Bar<'a, { regs::END }>;
+
+struct EduDriver;
#[pin_data(PinnedDrop)]
-struct EduDriver {
- pdev: ARef<pci::Device>,
- data: Arc<EduDriverData>,
+struct EduDriverData<'bound> {
+ pdev: &'bound pci::Device,
#[pin]
- irq_handler: irq::Registration<Arc<EduDriverData>>,
+ irq_handler: irq::Registration<'bound, IrqHandler<'bound>>,
}
#[pin_data]
-struct EduDriverData {
- #[pin]
- bar: Devres<Bar0>,
+struct IrqHandler<'a> {
+ pdev: &'a pci::Device,
+ bar: Bar0<'a>,
#[pin]
irq_test_completion: Completion,
#[pin]
@@ -88,16 +88,16 @@ struct EduDriverData {
}
impl EduDriver {
- fn init(pdev: &pci::Device<Bound>, bar: &Bar0, data: &EduDriverData) -> Result {
+ fn init(pdev: &pci::Device<Bound>, bar: &Bar0<'_>, handler: &IrqHandler<'_>) -> Result {
Self::magic(pdev, bar)?;
Self::liveness_check(pdev, bar)?;
Self::factorial(pdev, bar)?;
- Self::test_irq(pdev, bar, data)?;
- Self::test_dma(pdev, bar, data)?;
+ Self::test_irq(pdev, handler)?;
+ Self::test_dma(pdev, handler)?;
Ok(())
}
- fn magic(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
+ fn magic(pdev: &pci::Device<Bound>, bar: &Bar0<'_>) -> Result {
let identification = bar.read(regs::IDENTIFICATION);
let magic: u8 = identification.magic().into();
@@ -121,7 +121,7 @@ fn magic(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
Ok(())
}
- fn liveness_check(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
+ fn liveness_check(pdev: &pci::Device<Bound>, bar: &Bar0<'_>) -> Result {
let test_value = 0xabcd;
bar.write(regs::LIVENESS_CHECK, test_value.into());
@@ -142,7 +142,7 @@ fn liveness_check(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
Ok(())
}
- fn factorial(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
+ fn factorial(pdev: &pci::Device<Bound>, bar: &Bar0<'_>) -> Result {
Self::wait_until_compute_has_finished(pdev, bar)?;
bar.write(regs::FACTORIAL, 4.into());
@@ -167,28 +167,30 @@ fn factorial(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
Ok(())
}
- fn test_irq(pdev: &pci::Device<Bound>, bar: &Bar0, data: &EduDriverData) -> Result {
+ fn test_irq(pdev: &pci::Device<Bound>, handler: &IrqHandler<'_>) -> Result {
dev_dbg!(pdev, "raising irq\n");
- bar.write(regs::IRQ_RAISE, IRQ_MAGIC_VALUE.into());
+ handler.bar.write(regs::IRQ_RAISE, IRQ_MAGIC_VALUE.into());
- data.irq_test_completion.wait_for_completion();
+ handler.irq_test_completion.wait_for_completion();
Ok(())
}
- fn test_dma(pdev: &pci::Device<Bound>, bar: &Bar0, data: &EduDriverData) -> Result {
+ fn test_dma(pdev: &pci::Device<Bound>, handler: &IrqHandler<'_>) -> Result {
dev_dbg!(pdev, "testing dma\n");
- let dma = &data.dma;
+ let dma = &handler.dma;
const DMA_VALUE: u64 = 42;
kernel::dma_write!(dma, , DMA_VALUE);
- bar.write(regs::DMA_SRC, dma.dma_handle().into());
- bar.write(regs::DMA_DST, QEMU_DMA_BASE.into());
- bar.write(regs::DMA_COUNT, (dma.size() as u64).into());
- bar.write(
+ handler.bar.write(regs::DMA_SRC, dma.dma_handle().into());
+ handler.bar.write(regs::DMA_DST, QEMU_DMA_BASE.into());
+ handler
+ .bar
+ .write(regs::DMA_COUNT, (dma.size() as u64).into());
+ handler.bar.write(
regs::DMA_COMMAND,
regs::DMA_COMMAND::zeroed()
.with_start_transfer(true)
@@ -196,15 +198,17 @@ fn test_dma(pdev: &pci::Device<Bound>, bar: &Bar0, data: &EduDriverData) -> Resu
.with_raise_irq(true),
);
- data.irq_dma_completion.wait_for_completion();
+ handler.irq_dma_completion.wait_for_completion();
// Destroy previous value to test roundtrip
kernel::dma_write!(dma, , 0);
- bar.write(regs::DMA_SRC, QEMU_DMA_BASE.into());
- bar.write(regs::DMA_DST, dma.dma_handle().into());
- bar.write(regs::DMA_COUNT, (dma.size() as u64).into());
- bar.write(
+ handler.bar.write(regs::DMA_SRC, QEMU_DMA_BASE.into());
+ handler.bar.write(regs::DMA_DST, dma.dma_handle().into());
+ handler
+ .bar
+ .write(regs::DMA_COUNT, (dma.size() as u64).into());
+ handler.bar.write(
regs::DMA_COMMAND,
regs::DMA_COMMAND::zeroed()
.with_start_transfer(true)
@@ -212,7 +216,7 @@ fn test_dma(pdev: &pci::Device<Bound>, bar: &Bar0, data: &EduDriverData) -> Resu
.with_raise_irq(true),
);
- data.irq_dma_completion.wait_for_completion();
+ handler.irq_dma_completion.wait_for_completion();
let result = kernel::dma_read!(dma,);
@@ -230,7 +234,7 @@ fn test_dma(pdev: &pci::Device<Bound>, bar: &Bar0, data: &EduDriverData) -> Resu
Ok(())
}
- fn wait_until_compute_has_finished(pdev: &pci::Device<Bound>, bar: &Bar0) -> Result {
+ fn wait_until_compute_has_finished(pdev: &pci::Device<Bound>, bar: &Bar0<'_>) -> Result {
read_poll_timeout(
|| Ok(bar.read(regs::STATUS)),
|status| status.computing() == 0,
@@ -244,7 +248,7 @@ fn wait_until_compute_has_finished(pdev: &pci::Device<Bound>, bar: &Bar0) -> Res
impl pci::Driver for EduDriver {
type IdInfo = ();
- type Data<'bound> = Self;
+ type Data<'bound> = EduDriverData<'bound>;
const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
@@ -275,76 +279,61 @@ fn probe<'bound>(
.alloc_irq_vectors(1, 1, IrqTypes::default().with(pci::IrqType::Msi))
.inspect_err(|e| dev_err!(pdev, "alloc_irq_vectors failed: {:?}\n", e))?;
- // State shared with the IRQ handler (the BAR and the completion the
- // handler signals) lives in an `Arc<EduDriverData>`. `EduDriverData`
- // itself implements `irq::Handler`, and the registration takes an
- // `Arc<T>` via the `impl Handler for Arc<T>` blanket impl. This keeps
- // the handler's state out of `EduDriver` and avoids a self-reference.
- let data = Arc::pin_init(
- try_pin_init!(EduDriverData {
- bar <- pdev
- .iomap_region_sized(0, c"rust_driver_edu")
- .and_then(|bar| bar.into_devres()),
- irq_test_completion <- Completion::new(),
- irq_dma_completion <- Completion::new(),
- dma: ca,
- }),
- GFP_KERNEL,
- )?;
-
- let req = irq::Registration::new(
- (*irq.start()).try_into()?,
- Flags::TRIGGER_NONE,
- c"rust_edu_irq",
- Ok(data.clone()),
- );
+ let bar = pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_edu")?;
- // Ordering matters: the handler is registered (`irq_handler <- req`)
- // *before* the `_:` block runs the self-tests, one of which raises an
- // interrupt and waits for the handler. Raising before the handler is
- // registered would hang (the completion is never signalled).
- Ok(try_pin_init!(Self {
+ // SAFETY: The Registration is not leaked.
+ let req = unsafe {
+ irq::Registration::new(
+ (*irq.start()).try_into()?,
+ Flags::TRIGGER_NONE,
+ c"rust_edu_irq",
+ try_pin_init!(IrqHandler {
+ pdev,
+ bar,
+ irq_test_completion <- Completion::new(),
+ irq_dma_completion <- Completion::new(),
+ dma: ca,
+ }? Error),
+ )
+ };
+
+ Ok(try_pin_init!(EduDriverData {
irq_handler <- req,
- // Side-effect block: run the staged self-tests against the mapped
- // BAR now that the handler is live. A failure here aborts probe.
+ // Ordering matters: the handler is registered (`irq_handler <- req`)
+ // *before* the `_:` block runs the self-tests, one of which raises an
+ // interrupt and waits for the handler. Raising before the handler is
+ // registered would hang (the completion is never signalled).
_: {
- let bar = data.bar.access(pdev.as_ref())?;
- EduDriver::init(pdev, bar, &data)?;
+ let handler = irq_handler.handler();
+ EduDriver::init(pdev, &handler.bar, handler)?;
dev_info!(
pdev,
"rust_driver_edu successfully initialized\n",
);
},
- data,
- pdev: pdev.into()
+ pdev,
}))
})
}
}
-impl irq::Handler for EduDriverData {
- fn handle(&self, pdev: &kernel::device::Device<Bound>) -> irq::IrqReturn {
- dev_dbg!(pdev, "irq handler called\n");
- // `access()` only fails on device mismatch, so this branch is
- // structurally unreachable here, but it must be handled.
- let Ok(bar) = self.bar.access(pdev.as_ref()) else {
- dev_err!(pdev, "cannot access bar register inside irq handler\n");
- return irq::IrqReturn::None;
- };
- let status: u32 = bar.read(regs::IRQ_STATUS).into();
+impl irq::Handler for IrqHandler<'_> {
+ fn handle(&self) -> irq::IrqReturn {
+ dev_dbg!(self.pdev, "irq handler called\n");
+ let status: u32 = self.bar.read(regs::IRQ_STATUS).into();
// DMA_IRQ
if status & DMA_IRQ != 0 {
- dev_dbg!(pdev, "handling dma completion in irq\n");
- bar.write(regs::IRQ_ACK, DMA_IRQ.into());
+ dev_dbg!(self.pdev, "handling dma completion in irq\n");
+ self.bar.write(regs::IRQ_ACK, DMA_IRQ.into());
self.irq_dma_completion.complete();
}
// TEST_IRQ
let magic = status & !DMA_IRQ;
if magic == IRQ_MAGIC_VALUE {
- dev_dbg!(pdev, "handling test completion in irq\n");
- bar.write(regs::IRQ_ACK, magic.into());
+ dev_dbg!(self.pdev, "handling test completion in irq\n");
+ self.bar.write(regs::IRQ_ACK, magic.into());
self.irq_test_completion.complete();
}
@@ -353,7 +342,7 @@ fn handle(&self, pdev: &kernel::device::Device<Bound>) -> irq::IrqReturn {
}
#[pinned_drop]
-impl PinnedDrop for EduDriver {
+impl PinnedDrop for EduDriverData<'_> {
fn drop(self: Pin<&mut Self>) {
dev_dbg!(self.pdev, "Remove Rust EDU driver sample.\n");
}