From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 822E31632E7; Sun, 17 May 2026 00:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778976213; cv=none; b=smHm6wkNo9WwBpbyILxAxtedUSiFZuYyYr7wOOeam1tRw7DfxRoLsvA+kNRd2c+9TVyuT6mbPebtb2AtQTVjJBIDB7sj+kr9OUAJZf44MDeGhJcofPpxFVU7AjzfHO9c6x8+8wrJxRKe0A851Tsz+xZ2aCMdw9Ol0ygoC4XTYbM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778976213; c=relaxed/simple; bh=HnenYj+M4p4qXsQQTJ2FKMdGG+njf7P3a3XdXnokphw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RaCbXlUQnE0sbBXKVvvXlNMwoe6Gw4KaO4b0MHYjPjJy1CxxCRn+IqH04bmX/tYrNDGtxlMLzanJ9AlZWlsC5R/yL8yc6Td8eCT29RlWJx4Mx0gdoMg/vIBGxFwMW791OHy1qklU6BjMth+Hg3NyylcR1n2Ea+aB2tU6YpRa6wA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XtGwfL8D; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XtGwfL8D" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7B2EC19425; Sun, 17 May 2026 00:03:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778976213; bh=HnenYj+M4p4qXsQQTJ2FKMdGG+njf7P3a3XdXnokphw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XtGwfL8D4rLCRYovUzuxP1x5uDxfvnkOdhYR83Blktzf7gNSbzR5kg/9hD7Hoj4UK +YAxxW9ObPLquzvwzVo4eIVd0DcZg8wthiFopQE7VyUQDH1eGxftDrNKeGELyGRB4Z yB8mlahjHtAXeMEWPWyTjHZ23oUpmDuosyqplPYNygw0xqc+cBALz4hs+90LrNopgC 14F8OEyWvw9AyxGDCuWR3nqk2SJudogUKDQ211Jmmc8MV9n5l4tok3Xivp5bTlVQK7 yZzr+/CsbwJOV458v6qTggUZRg7a18vbv3utHHOgOT4eN4KgkF4RRiFv74m0TjRD8T ZNBXGTtB7H6VQ== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, acourbot@nvidia.com, aliceryhl@google.com, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, viresh.kumar@linaro.org, m.wilczynski@samsung.com, ukleinek@kernel.org, bhelgaas@google.com, kwilczynski@kernel.org, abdiel.janulgue@gmail.com, robin.murphy@arm.com, markus.probst@posteo.de, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu, igor.korotin@linux.dev, daniel.almeida@collabora.com, pcolberg@redhat.com Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-pm@vger.kernel.org, linux-pwm@vger.kernel.org, linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH v3 13/27] rust: usb: make Driver trait lifetime-parameterized Date: Sun, 17 May 2026 02:01:01 +0200 Message-ID: <20260517000149.3226762-14-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260517000149.3226762-1-dakr@kernel.org> References: <20260517000149.3226762-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: driver-core@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Signed-off-by: Danilo Krummrich --- 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 995221d327b8..ec67e4d2416f 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 driver::DriverLayout for Adapter { 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>`. - let data = unsafe { dev.drvdata_borrow::() }; + let data = unsafe { dev.drvdata_borrow::>() }; 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 = &USB_TABLE; /// -/// fn probe( -/// _interface: &usb::Interface, -/// _id: &usb::DeviceId, -/// _info: &Self::IdInfo, -/// ) -> impl PinInit { +/// fn probe<'bound>( +/// _interface: &'bound usb::Interface, +/// _id: &'bound usb::DeviceId, +/// _info: &'bound Self::IdInfo, +/// ) -> impl PinInit, Error> + 'bound { /// Err(ENODEV) /// } /// -/// fn disconnect(_interface: &usb::Interface, _data: Pin<&Self::Data>) {} +/// fn disconnect<'bound>( +/// _interface: &'bound usb::Interface, +/// _data: Pin<&'bound Self::Data<'bound>>, +/// ) { +/// } /// } ///``` pub trait Driver { @@ -306,7 +310,7 @@ pub trait Driver { type IdInfo: 'static; /// The type of the driver's bus device private data. - type Data; + type Data<'bound>: 'bound; /// The table of device ids supported by the driver. const ID_TABLE: IdTable; @@ -315,16 +319,19 @@ pub trait Driver { /// /// Called when a new USB interface is bound to this driver. /// Implementers should attempt to initialize the interface here. - fn probe( - interface: &Interface, - id: &DeviceId, - id_info: &Self::IdInfo, - ) -> impl PinInit; + fn probe<'bound>( + interface: &'bound Interface, + id: &'bound DeviceId, + id_info: &'bound Self::IdInfo, + ) -> impl PinInit, Error> + 'bound; /// USB driver disconnect. /// /// Called when the USB interface is about to be unbound from this driver. - fn disconnect(interface: &Interface, data: Pin<&Self::Data>); + fn disconnect<'bound>( + interface: &'bound Interface, + data: Pin<&'bound Self::Data<'bound>>, + ); } /// A USB interface. diff --git a/samples/rust/rust_driver_usb.rs b/samples/rust/rust_driver_usb.rs index 5942e4b01fd8..4403bd758db9 100644 --- a/samples/rust/rust_driver_usb.rs +++ b/samples/rust/rust_driver_usb.rs @@ -26,21 +26,21 @@ struct SampleDriver { impl usb::Driver for SampleDriver { type IdInfo = (); - type Data = Self; + type Data<'bound> = Self; const ID_TABLE: usb::IdTable = &USB_TABLE; - fn probe( - intf: &usb::Interface, - _id: &usb::DeviceId, - _info: &Self::IdInfo, - ) -> impl PinInit { + fn probe<'bound>( + intf: &'bound usb::Interface, + _id: &'bound usb::DeviceId, + _info: &'bound Self::IdInfo, + ) -> impl PinInit + 'bound { let dev: &device::Device = intf.as_ref(); dev_info!(dev, "Rust USB driver sample probed\n"); Ok(Self { _intf: intf.into() }) } - fn disconnect(intf: &usb::Interface, _data: Pin<&Self>) { + fn disconnect<'bound>(intf: &'bound usb::Interface, _data: Pin<&'bound Self>) { let dev: &device::Device = intf.as_ref(); dev_info!(dev, "Rust USB driver sample disconnected\n"); } -- 2.54.0