[PATCH 20/24] samples: rust: rust_driver_pci: use HRT lifetime for Bar

Danilo Krummrich
2026-04-27T22:14:14Z

Convert the sample driver to SampleDriver<'a>, taking advantage of the
lifetime-parameterized Driver trait.

The driver struct holds &'a pci::Device directly instead of
ARef<pci::Device>, and pci::Bar<'a> directly instead of
Devres<pci::Bar>. This removes PinnedDrop, pin_init_scope, and runtime
revocation checks on BAR access.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
samples/rust/rust_driver_pci.rs | 89 +++++++++++++++------------------
1 file changed, 39 insertions(+), 50 deletions(-)

diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
index 38d639731229..7f763f43a38d 100644
--- a/samples/rust/rust_driver_pci.rs
+++ b/samples/rust/rust_driver_pci.rs
@@ -9,7 +9,6 @@
Bound,
Core, //
},
- devres::Devres,
io::{
register,
register::Array,
@@ -17,8 +16,7 @@
},
num::Bounded,
pci,
- prelude::*,
- sync::aref::ARef, //
+ prelude::*, //
};

mod regs {
@@ -45,7 +43,7 @@ mod regs {
pub(super) const END: usize = 0x10;
}

-type Bar0 = pci::Bar<'static, { regs::END }>;
+type Bar0<'a> = pci::Bar<'a, { regs::END }>;

#[derive(Copy, Clone, Debug)]
struct TestIndex(u8);
@@ -66,26 +64,24 @@ impl TestIndex {
const NO_EVENTFD: Self = Self(0);
}

-#[pin_data(PinnedDrop)]
-struct SampleDriver {
- pdev: ARef<pci::Device>,
- #[pin]
- bar: Devres<Bar0>,
+struct SampleDriver<'a> {
+ pdev: &'a pci::Device,
+ bar: Bar0<'a>,
index: TestIndex,
}

kernel::pci_device_table!(
PCI_TABLE,
MODULE_PCI_TABLE,
- <SampleDriver as pci::Driver<'_>>::IdInfo,
+ <SampleDriver<'_> as pci::Driver<'_>>::IdInfo,
[(
pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5),
TestIndex::NO_EVENTFD
)]
);

-impl SampleDriver {
- fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
+impl SampleDriver<'_> {
+ fn testdev(index: &TestIndex, bar: &Bar0<'_>) -> Result<u32> {
// Select the test.
bar.write_reg(regs::TEST::zeroed().with_index(*index));

@@ -138,7 +134,7 @@ fn config_space(pdev: &pci::Device<Bound>) {
}
}

-impl<'a> pci::Driver<'a> for SampleDriver {
+impl<'a> pci::Driver<'a> for SampleDriver<'a> {
type IdInfo = TestIndex;

const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
@@ -147,54 +143,47 @@ fn probe(
pdev: &'a pci::Device<Core>,
info: &'a Self::IdInfo,
) -> impl PinInit<Self, Error> + 'a {
- pin_init::pin_init_scope(move || {
- let vendor = pdev.vendor_id();
- dev_dbg!(
- pdev,
- "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
- vendor,
- pdev.device_id()
- );
-
- pdev.enable_device_mem()?;
- pdev.set_master();
-
- Ok(try_pin_init!(Self {
- bar: pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci")?
- .into_devres()?,
- index: *info,
- _: {
- let bar = bar.access(pdev.as_ref())?;
-
- dev_info!(
- pdev,
- "pci-testdev data-match count: {}\n",
- Self::testdev(info, bar)?
- );
- Self::config_space(pdev);
- },
- pdev: pdev.into(),
- }))
+ let vendor = pdev.vendor_id();
+ dev_dbg!(
+ pdev,
+ "Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n",
+ vendor,
+ pdev.device_id()
+ );
+
+ pdev.enable_device_mem()?;
+ pdev.set_master();
+
+ let bar = pdev.iomap_region_sized::<{ regs::END }>(0, c"rust_driver_pci")?;
+
+ dev_info!(
+ pdev,
+ "pci-testdev data-match count: {}\n",
+ Self::testdev(info, &bar)?
+ );
+ Self::config_space(pdev);
+
+ Ok(Self {
+ pdev,
+ bar,
+ index: *info,
})
}

- fn unbind(pdev: &'a pci::Device<Core>, this: Pin<&'a Self>) {
- if let Ok(bar) = this.bar.access(pdev.as_ref()) {
- // Reset pci-testdev by writing a new test index.
- bar.write_reg(regs::TEST::zeroed().with_index(this.index));
- }
+ fn unbind(_pdev: &'a pci::Device<Core>, this: Pin<&'a Self>) {
+ this.bar
+ .write_reg(regs::TEST::zeroed().with_index(this.index));
}
}

-#[pinned_drop]
-impl PinnedDrop for SampleDriver {
- fn drop(self: Pin<&mut Self>) {
+impl Drop for SampleDriver<'_> {
+ fn drop(&mut self) {
dev_dbg!(self.pdev, "Remove Rust PCI driver sample.\n");
}
}

kernel::module_pci_driver! {
- type: SampleDriver,
+ type: SampleDriver<'_>,
name: "rust_driver_pci",
authors: ["Danilo Krummrich"],
description: "Rust PCI driver",
--
2.54.0