From: Danilo Krummrich <dakr@kernel.org>
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
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 <dakr@kernel.org>
Subject: [PATCH v2 16/25] rust: i2c: make Driver trait lifetime-parameterized
Date: Wed, 6 May 2026 23:50:52 +0200 [thread overview]
Message-ID: <20260506215113.851360-17-dakr@kernel.org> (raw)
In-Reply-To: <20260506215113.851360-1-dakr@kernel.org>
Make i2c::Driver take a lifetime parameter 'bound that ties device
resources to the binding scope.
Internally, Adapter<T: Driver> becomes Adapter<F: ForLt> with a bound
for<'bound> F::Of<'bound>: Driver<'bound>; module_i2c_driver! wraps the
driver type in ForLt!() so drivers don't have to.
Acked-by: Igor Korotin <igor.korotin@linux.dev>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/i2c.rs | 119 +++++++++++++++++++-------------
samples/rust/rust_driver_i2c.rs | 18 ++---
2 files changed, 81 insertions(+), 56 deletions(-)
diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index 208c73aa3ce3..2216b49604b2 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -92,43 +92,58 @@ macro_rules! i2c_device_table {
}
/// An adapter for the registration of I2C drivers.
-pub struct Adapter<T: Driver>(T);
+///
+/// `F` is a [`ForLt`](trait@ForLt) type that maps lifetimes to the driver's device
+/// private data type, i.e. `F::Of<'bound>` is the driver struct
+/// parameterized by `'bound`. The macro `module_i2c_driver!` generates
+/// this automatically via `ForLt!()`.
+pub struct Adapter<F>(PhantomData<F>);
// SAFETY:
// - `bindings::i2c_driver` is a C type declared as `repr(C)`.
-// - `T` is the type of the driver's device private data.
+// - `F::Of<'static>` is the stored type of the driver's device private data.
// - `struct i2c_driver` embeds a `struct device_driver`.
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
-unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
+unsafe impl<F> driver::DriverLayout for Adapter<F>
+where
+ F: ForLt + 'static,
+ for<'bound> F::Of<'bound>: Driver<'bound>,
+{
type DriverType = bindings::i2c_driver;
- type DriverData = ForLt!(T);
+ type DriverData = F;
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
}
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
// a preceding call to `register` has been successful.
-unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
+unsafe impl<F> driver::RegistrationOps for Adapter<F>
+where
+ F: ForLt + 'static,
+ for<'bound> F::Of<'bound>: Driver<'bound>,
+{
unsafe fn register(
idrv: &Opaque<Self::DriverType>,
name: &'static CStr,
module: &'static ThisModule,
) -> Result {
build_assert!(
- T::ACPI_ID_TABLE.is_some() || T::OF_ID_TABLE.is_some() || T::I2C_ID_TABLE.is_some(),
+ <F::Of<'static> as Driver<'static>>::ACPI_ID_TABLE.is_some()
+ || <F::Of<'static> as Driver<'static>>::OF_ID_TABLE.is_some()
+ || <F::Of<'static> as Driver<'static>>::I2C_ID_TABLE.is_some(),
"At least one of ACPI/OF/Legacy tables must be present when registering an i2c driver"
);
- let i2c_table = match T::I2C_ID_TABLE {
+ let i2c_table = match <F::Of<'static> as Driver<'static>>::I2C_ID_TABLE {
Some(table) => table.as_ptr(),
None => core::ptr::null(),
};
- let of_table = match T::OF_ID_TABLE {
+ let of_table = match <F::Of<'static> as Driver<'static>>::OF_ID_TABLE {
Some(table) => table.as_ptr(),
None => core::ptr::null(),
};
- let acpi_table = match T::ACPI_ID_TABLE {
+ let acpi_table = match <F::Of<'static> as Driver<'static>>::ACPI_ID_TABLE {
Some(table) => table.as_ptr(),
None => core::ptr::null(),
};
@@ -154,7 +169,11 @@ unsafe fn unregister(idrv: &Opaque<Self::DriverType>) {
}
}
-impl<T: Driver + 'static> Adapter<T> {
+impl<F> Adapter<F>
+where
+ F: ForLt + 'static,
+ for<'bound> F::Of<'bound>: Driver<'bound>,
+{
extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_int {
// SAFETY: The I2C bus only ever calls the probe callback with a valid pointer to a
// `struct i2c_client`.
@@ -162,13 +181,12 @@ extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_
// INVARIANT: `idev` is valid for the duration of `probe_callback()`.
let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
- let info = Self::i2c_id_info(idev)
- .or_else(|| <Self as driver::Adapter<'_>>::id_info(idev.as_ref()));
-
from_result(|| {
- let data = T::probe(idev, info);
+ let info = Self::i2c_id_info(idev)
+ .or_else(|| <Self as driver::Adapter<'_>>::id_info(idev.as_ref()));
+ let data = <F::Of<'_> as Driver<'_>>::probe(idev, info);
- idev.as_ref().set_drvdata::<ForLt!(T)>(data)?;
+ idev.as_ref().set_drvdata::<F>(data)?;
Ok(0)
})
}
@@ -178,11 +196,10 @@ extern "C" fn remove_callback(idev: *mut bindings::i2c_client) {
let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
// SAFETY: `remove_callback` is only ever called after a successful call to
- // `probe_callback`, hence it's guaranteed that `I2cClient::set_drvdata()` has been called
- // and stored a `Pin<KBox<T>>`.
- let data = unsafe { idev.as_ref().drvdata_borrow::<ForLt!(T)>() };
+ // `probe_callback`, hence it's guaranteed that drvdata has been set.
+ let data = unsafe { idev.as_ref().drvdata_borrow::<F>() };
- T::unbind(idev, data);
+ <F::Of<'_> as Driver<'_>>::unbind(idev, data);
}
extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
@@ -190,23 +207,24 @@ extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal>>() };
// SAFETY: `shutdown_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>>`.
- let data = unsafe { idev.as_ref().drvdata_borrow::<ForLt!(T)>() };
+ // `probe_callback`, hence it's guaranteed that drvdata has been set.
+ let data = unsafe { idev.as_ref().drvdata_borrow::<F>() };
- T::shutdown(idev, data);
+ <F::Of<'_> as Driver<'_>>::shutdown(idev, data);
}
/// The [`i2c::IdTable`] of the corresponding driver.
- fn i2c_id_table() -> Option<IdTable<<Self as driver::Adapter<'static>>::IdInfo>> {
- T::I2C_ID_TABLE
+ fn i2c_id_table<'bound>() -> Option<IdTable<<F::Of<'bound> as Driver<'bound>>::IdInfo>> {
+ <F::Of<'bound> as Driver<'bound>>::I2C_ID_TABLE
}
/// Returns the driver's private data from the matching entry in the [`i2c::IdTable`], if any.
///
/// If this returns `None`, it means there is no match with an entry in the [`i2c::IdTable`].
- fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter<'static>>::IdInfo> {
- let table = Self::i2c_id_table()?;
+ fn i2c_id_info<'bound>(
+ dev: &I2cClient,
+ ) -> Option<&'bound <F::Of<'bound> as Driver<'bound>>::IdInfo> {
+ let table = Self::i2c_id_table::<'bound>()?;
// SAFETY:
// - `table` has static lifetime, hence it's valid for reads
@@ -225,15 +243,19 @@ fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter<'sta
}
}
-impl<'bound, T: Driver + 'static> driver::Adapter<'bound> for Adapter<T> {
- type IdInfo = T::IdInfo;
+impl<'bound, F> driver::Adapter<'bound> for Adapter<F>
+where
+ F: ForLt + 'static,
+ F::Of<'bound>: Driver<'bound>,
+{
+ type IdInfo = <F::Of<'bound> as Driver<'bound>>::IdInfo;
fn of_id_table() -> Option<of::IdTable<Self::IdInfo>> {
- T::OF_ID_TABLE
+ <F::Of<'bound> as Driver<'bound>>::OF_ID_TABLE
}
fn acpi_id_table() -> Option<acpi::IdTable<Self::IdInfo>> {
- T::ACPI_ID_TABLE
+ <F::Of<'bound> as Driver<'bound>>::ACPI_ID_TABLE
}
}
@@ -252,8 +274,11 @@ fn acpi_id_table() -> Option<acpi::IdTable<Self::IdInfo>> {
/// ```
#[macro_export]
macro_rules! module_i2c_driver {
- ($($f:tt)*) => {
- $crate::module_driver!(<T>, $crate::i2c::Adapter<T>, { $($f)* });
+ (type: $type:ty, $($rest:tt)*) => {
+ $crate::module_driver!(<T>, $crate::i2c::Adapter<T>, {
+ type: $crate::types::ForLt!($type),
+ $($rest)*
+ });
};
}
@@ -271,7 +296,7 @@ macro_rules! module_i2c_driver {
/// kernel::acpi_device_table!(
/// ACPI_TABLE,
/// MODULE_ACPI_TABLE,
-/// <MyDriver as i2c::Driver>::IdInfo,
+/// <MyDriver as i2c::Driver<'_>>::IdInfo,
/// [
/// (acpi::DeviceId::new(c"LNUXBEEF"), ())
/// ]
@@ -280,7 +305,7 @@ macro_rules! module_i2c_driver {
/// kernel::i2c_device_table!(
/// I2C_TABLE,
/// MODULE_I2C_TABLE,
-/// <MyDriver as i2c::Driver>::IdInfo,
+/// <MyDriver as i2c::Driver<'_>>::IdInfo,
/// [
/// (i2c::DeviceId::new(c"rust_driver_i2c"), ())
/// ]
@@ -289,30 +314,30 @@ macro_rules! module_i2c_driver {
/// kernel::of_device_table!(
/// OF_TABLE,
/// MODULE_OF_TABLE,
-/// <MyDriver as i2c::Driver>::IdInfo,
+/// <MyDriver as i2c::Driver<'_>>::IdInfo,
/// [
/// (of::DeviceId::new(c"test,device"), ())
/// ]
/// );
///
-/// impl i2c::Driver for MyDriver {
+/// impl<'bound> i2c::Driver<'bound> for MyDriver {
/// type IdInfo = ();
/// const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
/// const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
/// const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
///
/// fn probe(
-/// _idev: &i2c::I2cClient<Core>,
-/// _id_info: Option<&Self::IdInfo>,
-/// ) -> impl PinInit<Self, Error> {
+/// _idev: &'bound i2c::I2cClient<Core>,
+/// _id_info: Option<&'bound Self::IdInfo>,
+/// ) -> impl PinInit<Self, Error> + 'bound {
/// Err(ENODEV)
/// }
///
-/// fn shutdown(_idev: &i2c::I2cClient<Core>, this: Pin<&Self>) {
+/// fn shutdown(_idev: &'bound i2c::I2cClient<Core>, _this: Pin<&'bound Self>) {
/// }
/// }
///```
-pub trait Driver: Send {
+pub trait Driver<'bound>: Send {
/// The type holding information about each device id supported by the driver.
// TODO: Use `associated_type_defaults` once stabilized:
//
@@ -335,9 +360,9 @@ pub trait Driver: Send {
/// Called when a new i2c client is added or discovered.
/// Implementers should attempt to initialize the client here.
fn probe(
- dev: &I2cClient<device::Core>,
- id_info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error>;
+ dev: &'bound I2cClient<device::Core>,
+ id_info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound;
/// I2C driver shutdown.
///
@@ -350,7 +375,7 @@ fn probe(
/// This callback is distinct from final resource cleanup, as the driver instance remains valid
/// after it returns. Any deallocation or teardown of driver-owned resources should instead be
/// handled in `Self::drop`.
- fn shutdown(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
+ fn shutdown(dev: &'bound I2cClient<device::Core>, this: Pin<&'bound Self>) {
let _ = (dev, this);
}
@@ -364,7 +389,7 @@ fn shutdown(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
/// operations to gracefully tear down the device.
///
/// Otherwise, release operations for driver resources should be performed in `Self::drop`.
- fn unbind(dev: &I2cClient<device::Core>, this: Pin<&Self>) {
+ fn unbind(dev: &'bound I2cClient<device::Core>, this: Pin<&'bound Self>) {
let _ = (dev, this);
}
}
diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c.rs
index 6be79f9e9fb5..042a18305817 100644
--- a/samples/rust/rust_driver_i2c.rs
+++ b/samples/rust/rust_driver_i2c.rs
@@ -15,25 +15,25 @@
kernel::acpi_device_table! {
ACPI_TABLE,
MODULE_ACPI_TABLE,
- <SampleDriver as i2c::Driver>::IdInfo,
+ <SampleDriver as i2c::Driver<'_>>::IdInfo,
[(acpi::DeviceId::new(c"LNUXBEEF"), 0)]
}
kernel::i2c_device_table! {
I2C_TABLE,
MODULE_I2C_TABLE,
- <SampleDriver as i2c::Driver>::IdInfo,
+ <SampleDriver as i2c::Driver<'_>>::IdInfo,
[(i2c::DeviceId::new(c"rust_driver_i2c"), 0)]
}
kernel::of_device_table! {
OF_TABLE,
MODULE_OF_TABLE,
- <SampleDriver as i2c::Driver>::IdInfo,
+ <SampleDriver as i2c::Driver<'_>>::IdInfo,
[(of::DeviceId::new(c"test,rust_driver_i2c"), 0)]
}
-impl i2c::Driver for SampleDriver {
+impl<'bound> i2c::Driver<'bound> for SampleDriver {
type IdInfo = u32;
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
@@ -41,9 +41,9 @@ impl i2c::Driver for SampleDriver {
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
fn probe(
- idev: &i2c::I2cClient<Core>,
- info: Option<&Self::IdInfo>,
- ) -> impl PinInit<Self, Error> {
+ idev: &'bound i2c::I2cClient<Core>,
+ info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
let dev = idev.as_ref();
dev_info!(dev, "Probe Rust I2C driver sample.\n");
@@ -55,11 +55,11 @@ fn probe(
Ok(Self)
}
- fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
+ fn shutdown(idev: &'bound i2c::I2cClient<Core>, _this: Pin<&'bound Self>) {
dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n");
}
- fn unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
+ fn unbind(idev: &'bound i2c::I2cClient<Core>, _this: Pin<&'bound Self>) {
dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n");
}
}
--
2.54.0
next prev parent reply other threads:[~2026-05-06 21:53 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-06 21:50 [PATCH v2 00/25] rust: device: Higher-Ranked Lifetime Types for device drivers Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 01/25] rust: driver core: drop drvdata before devres release Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 02/25] rust: types: add `ForLt` trait for higher-ranked lifetime support Danilo Krummrich
2026-05-06 22:14 ` Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 03/25] rust: device: generalize drvdata methods over ForLt Danilo Krummrich
2026-05-07 12:50 ` Alice Ryhl
2026-05-06 21:50 ` [PATCH v2 04/25] rust: driver: make Adapter trait lifetime-parameterized Danilo Krummrich
2026-05-07 12:24 ` Gary Guo
2026-05-06 21:50 ` [PATCH v2 05/25] rust: pci: implement Sync for Device<Bound> Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 06/25] rust: platform: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 07/25] rust: auxiliary: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 08/25] rust: usb: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 09/25] rust: device: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 10/25] rust: pci: make Driver trait lifetime-parameterized Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 11/25] rust: platform: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 12/25] rust: auxiliary: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 13/25] rust: auxiliary: generalize Registration over ForLt Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 14/25] samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 15/25] rust: usb: make Driver trait lifetime-parameterized Danilo Krummrich
2026-05-06 21:50 ` Danilo Krummrich [this message]
2026-05-06 21:50 ` [PATCH v2 17/25] rust: pci: make Bar lifetime-parameterized Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 18/25] rust: io: make IoMem and ExclusiveIoMem lifetime-parameterized Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 19/25] samples: rust: rust_driver_pci: use HRT lifetime for Bar Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 20/25] rust: driver-core: rename 'a lifetime to 'bound Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 21/25] gpu: nova-core: " Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 22/25] gpu: nova-core: use HRT lifetime for Bar Danilo Krummrich
2026-05-06 21:50 ` [PATCH v2 23/25] gpu: nova-core: unregister sysmem flush page from Drop Danilo Krummrich
2026-05-06 21:51 ` [PATCH v2 24/25] gpu: nova-core: replace ARef<Device> with &'bound Device in SysmemFlush Danilo Krummrich
2026-05-06 21:51 ` [PATCH v2 25/25] gpu: drm: tyr: use HRT lifetime for IoMem Danilo Krummrich
2026-05-07 12:14 ` [PATCH v2 00/25] rust: device: Higher-Ranked Lifetime Types for device drivers Dirk Behme
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260506215113.851360-17-dakr@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david.m.ertman@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=igor.korotin@linux.dev \
--cc=ira.weiny@intel.com \
--cc=kwilczynski@kernel.org \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=m.wilczynski@samsung.com \
--cc=markus.probst@posteo.de \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=ukleinek@kernel.org \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox