aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
authorDanilo Krummrich <dakr@kernel.org>2026-05-25 22:21:11 +0200
committerDanilo Krummrich <dakr@kernel.org>2026-05-27 16:29:34 +0200
commitd18f3646184fc805d213fc049fc3b5d9fb9a6a27 (patch)
tree55ccc1b6409451e325a92193a9b4fcf95f40d62e /samples
parent4555291ddae9abe2c40a7eae192b1976b07a1fad (diff)
downloadlinux-next-history-d18f3646184fc805d213fc049fc3b5d9fb9a6a27.tar.gz
samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data
Make the Data struct lifetime-parameterized, storing a reference to the parent pci::Device<Bound>. This demonstrates that registration data can hold device resources tied to the parent driver's lifetime. In connect(), retrieve the parent PCI device from the registration data rather than casting through adev.parent(). Reviewed-by: Eliot Courtney <ecourtney@nvidia.com> Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://patch.msgid.link/20260525202921.124698-25-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'samples')
-rw-r--r--samples/rust/rust_driver_auxiliary.rs58
1 files changed, 35 insertions, 23 deletions
diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driver_auxiliary.rs
index e3e811a14110c..2c1351040e45e 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -51,16 +51,17 @@ impl auxiliary::Driver for AuxiliaryDriver {
}
}
-struct Data {
+struct Data<'bound> {
index: u32,
+ parent: &'bound pci::Device<Bound>,
}
struct ParentDriver;
#[allow(clippy::type_complexity)]
struct ParentData<'bound> {
- _reg0: auxiliary::Registration<'bound, ForLt!(Data)>,
- _reg1: auxiliary::Registration<'bound, ForLt!(Data)>,
+ _reg0: auxiliary::Registration<'bound, ForLt!(Data<'_>)>,
+ _reg1: auxiliary::Registration<'bound, ForLt!(Data<'_>)>,
}
kernel::pci_device_table!(
@@ -81,33 +82,44 @@ impl pci::Driver for ParentDriver {
_info: &'bound Self::IdInfo,
) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
Ok(ParentData {
- _reg0: auxiliary::Registration::new(
- pdev.as_ref(),
- AUXILIARY_NAME,
- 0,
- MODULE_NAME,
- Data { index: 0 },
- )?,
- _reg1: auxiliary::Registration::new(
- pdev.as_ref(),
- AUXILIARY_NAME,
- 1,
- MODULE_NAME,
- Data { index: 1 },
- )?,
+ // SAFETY: `ParentData` is the driver's private data, which is dropped when the
+ // device is unbound; i.e. `mem::forget()` is never called on it.
+ _reg0: unsafe {
+ auxiliary::Registration::new_with_lt(
+ pdev.as_ref(),
+ AUXILIARY_NAME,
+ 0,
+ MODULE_NAME,
+ Data {
+ index: 0,
+ parent: pdev,
+ },
+ )?
+ },
+ // SAFETY: See `_reg0` above.
+ _reg1: unsafe {
+ auxiliary::Registration::new_with_lt(
+ pdev.as_ref(),
+ AUXILIARY_NAME,
+ 1,
+ MODULE_NAME,
+ Data {
+ index: 1,
+ parent: pdev,
+ },
+ )?
+ },
})
}
}
impl ParentDriver {
fn connect(adev: &auxiliary::Device<Bound>) -> Result {
- let dev = adev.parent();
- let pdev: &pci::Device<Bound> = dev.try_into()?;
-
- let data = adev.registration_data::<ForLt!(Data)>()?;
+ let data = adev.registration_data::<ForLt!(Data<'_>)>()?;
+ let pdev = data.parent;
dev_info!(
- dev,
+ pdev,
"Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n",
adev.id(),
pdev.vendor_id(),
@@ -115,7 +127,7 @@ impl ParentDriver {
);
dev_info!(
- dev,
+ pdev,
"Connected to auxiliary device with index {}.\n",
data.index
);