Re: [PATCH v4 14/27] rust: usb: make Driver trait lifetime-parameterized

From: Eliot Courtney

Date: Mon May 25 2026 - 00:32:51 EST


On Fri May 22, 2026 at 8:34 AM JST, Danilo Krummrich wrote:
> Add a 'bound lifetime to the associated Data, changing type Data to type
> Data<'bound>.
>
> This allows the driver's bus device private data to capture the device /
> driver bound lifetime; device resources can be stored directly by
> reference rather than requiring Devres.
>
> The probe() and unbind() callbacks thus gain a 'bound lifetime parameter
> on the methods themselves; avoiding a global lifetime on the trait impl.
>
> Existing drivers set type Data<'bound> = Self, preserving the current
> behavior.
>
> Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> ---
> rust/kernel/usb.rs | 39 +++++++++++++++++++--------------
> samples/rust/rust_driver_usb.rs | 14 ++++++------
> 2 files changed, 30 insertions(+), 23 deletions(-)
>
> diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
> index 1dbb8387b463..616e22e34c6f 100644
> --- a/rust/kernel/usb.rs
> +++ b/rust/kernel/usb.rs
> @@ -41,7 +41,7 @@
> // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
> unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
> type DriverType = bindings::usb_driver;
> - type DriverData<'bound> = T::Data;
> + type DriverData<'bound> = T::Data<'bound>;
> const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
> }
>
> @@ -110,7 +110,7 @@ extern "C" fn disconnect_callback(intf: *mut bindings::usb_interface) {
> // SAFETY: `disconnect_callback` is only ever called after a successful call to
> // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
> // and stored a `Pin<KBox<T::Data>>`.
> - let data = unsafe { dev.drvdata_borrow::<T::Data>() };
> + let data = unsafe { dev.drvdata_borrow::<T::Data<'_>>() };
>
> T::disconnect(intf, data);
> }
> @@ -287,18 +287,22 @@ macro_rules! usb_device_table {
> ///
> /// impl usb::Driver for MyDriver {
> /// type IdInfo = ();
> -/// type Data = Self;
> +/// type Data<'bound> = Self;
> /// const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
> ///
> -/// fn probe(
> -/// _interface: &usb::Interface<Core<'_>>,
> -/// _id: &usb::DeviceId,
> -/// _info: &Self::IdInfo,
> -/// ) -> impl PinInit<Self::Data, Error> {
> +/// fn probe<'bound>(
> +/// _interface: &'bound usb::Interface<Core<'_>>,
> +/// _id: &'bound usb::DeviceId,
> +/// _info: &'bound Self::IdInfo,
> +/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {

Are we sure that usb::DeviceId can have its lifetime extended to more
than the callback lifetime?

Reviewed-by: Eliot Courtney <ecourtney@xxxxxxxxxx>

if it's guaranteed safe to extend the lifetime, or if not, with removing
the 'bound lifetime.