rust: auxiliary: unregister on parent device unbind

Guarantee that an auxiliary driver will be unbound before its parent is
unbound; there is no point in operating an auxiliary device whose parent
has been unbound.

In practice, this guarantee allows us to assume that for a bound
auxiliary device, also the parent device is bound.

This is useful when an auxiliary driver calls into its parent, since it
allows the parent to directly access device resources and its device
private data due to the guaranteed bound device context.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Danilo Krummrich
2025-10-21 00:34:26 +02:00
parent 589b061975
commit e4e679c860
3 changed files with 63 additions and 45 deletions

View File

@@ -5,7 +5,8 @@
//! To make this driver probe, QEMU must be run with `-device pci-testdev`.
use kernel::{
auxiliary, c_str, device::Core, driver, error::Error, pci, prelude::*, InPlaceModule,
auxiliary, c_str, device::Core, devres::Devres, driver, error::Error, pci, prelude::*,
InPlaceModule,
};
use pin_init::PinInit;
@@ -40,8 +41,12 @@ impl auxiliary::Driver for AuxiliaryDriver {
}
}
#[pin_data]
struct ParentDriver {
_reg: [auxiliary::Registration; 2],
#[pin]
_reg0: Devres<auxiliary::Registration>,
#[pin]
_reg1: Devres<auxiliary::Registration>,
}
kernel::pci_device_table!(
@@ -57,11 +62,9 @@ impl pci::Driver for ParentDriver {
const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> impl PinInit<Self, Error> {
Ok(Self {
_reg: [
auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 0, MODULE_NAME)?,
auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 1, MODULE_NAME)?,
],
try_pin_init!(Self {
_reg0 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 0, MODULE_NAME),
_reg1 <- auxiliary::Registration::new(pdev.as_ref(), AUXILIARY_NAME, 1, MODULE_NAME),
})
}
}