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
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 14/24] rust: auxiliary: generalize Registration over ForLt
Date: Tue, 28 Apr 2026 00:11:12 +0200 [thread overview]
Message-ID: <20260427221155.2144848-15-dakr@kernel.org> (raw)
In-Reply-To: <20260427221155.2144848-1-dakr@kernel.org>
Generalize Registration<T> to Registration<F: ForLt> and
Device::registration_data<F: ForLt>() to return Pin<&F::Of<'_>>.
The stored 'static lifetime is shortened to the borrow lifetime of &self
via ForLt::cast_ref; ForLt's covariance guarantee makes this sound.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
drivers/gpu/nova-core/driver.rs | 4 +-
rust/kernel/auxiliary.rs | 80 ++++++++++++++++++---------
samples/rust/rust_driver_auxiliary.rs | 7 ++-
3 files changed, 61 insertions(+), 30 deletions(-)
diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs
index 815489dd92d0..2a17fc99d9b6 100644
--- a/drivers/gpu/nova-core/driver.rs
+++ b/drivers/gpu/nova-core/driver.rs
@@ -21,6 +21,7 @@
},
Arc,
},
+ types::ForLt,
};
use crate::gpu::Gpu;
@@ -32,7 +33,8 @@
pub(crate) struct NovaCore {
#[pin]
pub(crate) gpu: Gpu,
- _reg: Devres<auxiliary::Registration<()>>,
+ #[allow(clippy::type_complexity)]
+ _reg: Devres<auxiliary::Registration<ForLt!(())>>,
}
const BAR0_SIZE: usize = SZ_16M;
diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
index f593a21a16be..e27de4be4d87 100644
--- a/rust/kernel/auxiliary.rs
+++ b/rust/kernel/auxiliary.rs
@@ -287,12 +287,19 @@ pub fn parent(&self) -> &device::Device<device::Bound> {
/// Returns a pinned reference to the registration data set by the registering (parent) driver.
///
- /// Returns [`EINVAL`] if `T` does not match the type used by the parent driver when calling
+ /// `F` is the [`ForLt`](trait@ForLt) encoding of the data type. The returned
+ /// reference has its lifetime shortened from `'static` to `&self`'s borrow lifetime via
+ /// [`ForLt::cast_ref`].
+ ///
+ /// Returns [`EINVAL`] if `F` does not match the type used by the parent driver when calling
/// [`Registration::new()`].
///
/// Returns [`ENOENT`] if no registration data has been set, e.g. when the device was
/// registered by a C driver.
- pub fn registration_data<T: 'static>(&self) -> Result<Pin<&T>> {
+ pub fn registration_data<F: ForLt>(&self) -> Result<Pin<&F::Of<'_>>>
+ where
+ F::Of<'static>: 'static,
+ {
// SAFETY: By the type invariant, `self.as_raw()` is a valid `struct auxiliary_device`.
let ptr = unsafe { (*self.as_raw()).registration_data_rust };
if ptr.is_null() {
@@ -305,18 +312,23 @@ pub fn registration_data<T: 'static>(&self) -> Result<Pin<&T>> {
// SAFETY: `ptr` is non-null and was set via `into_foreign()` in `Registration::new()`;
// `RegistrationData` is `#[repr(C)]` with `type_id` at offset 0, so reading a `TypeId`
- // at the start of the allocation is valid regardless of `T`.
+ // at the start of the allocation is valid regardless of `F`.
let type_id = unsafe { ptr.cast::<TypeId>().read() };
- if type_id != TypeId::of::<T>() {
+ if type_id != TypeId::of::<F::Of<'static>>() {
return Err(EINVAL);
}
- // SAFETY: The `TypeId` check above confirms that the stored type is `T`; `ptr` remains
- // valid until `Registration::drop()` calls `from_foreign()`.
- let wrapper = unsafe { Pin::<KBox<RegistrationData<T>>>::borrow(ptr) };
+ // SAFETY: The `TypeId` check above confirms that the stored type matches
+ // `F::Of<'static>`; `ptr` remains valid until `Registration::drop()` calls
+ // `from_foreign()`.
+ let wrapper = unsafe { Pin::<KBox<RegistrationData<F::Of<'static>>>>::borrow(ptr) };
// SAFETY: `data` is a structurally pinned field of `RegistrationData`.
- Ok(unsafe { wrapper.map_unchecked(|w| &w.data) })
+ let pinned: Pin<&F::Of<'static>> = unsafe { wrapper.map_unchecked(|w| &w.data) };
+
+ // SAFETY: ForLt guarantees covariance, making it sound to shorten 'static to &self's
+ // lifetime via cast_ref.
+ Ok(unsafe { Pin::new_unchecked(F::cast_ref(pinned.get_ref())) })
}
}
@@ -405,43 +417,54 @@ struct RegistrationData<T> {
/// This type represents the registration of a [`struct auxiliary_device`]. When its parent device
/// is unbound, the corresponding auxiliary device will be unregistered from the system.
///
-/// The type parameter `T` is the type of the registration data owned by the registering (parent)
-/// driver. It can be accessed by the auxiliary driver through
-/// [`Device::registration_data()`].
+/// The type parameter `F` is a [`ForLt`](trait@ForLt) encoding of the registration
+/// data type. For non-lifetime-parameterized types, use [`ForLt!(T)`](macro@ForLt).
+/// The data can be accessed by the auxiliary driver through [`Device::registration_data()`].
///
/// # Invariants
///
/// `self.adev` always holds a valid pointer to an initialized and registered
/// [`struct auxiliary_device`], and `registration_data` points to a valid
-/// `Pin<KBox<RegistrationData<T>>>`.
-pub struct Registration<T: 'static> {
+/// `Pin<KBox<RegistrationData<F::Of<'static>>>>`.
+pub struct Registration<F: ForLt>
+where
+ F::Of<'static>: 'static,
+{
adev: NonNull<bindings::auxiliary_device>,
- _data: PhantomData<T>,
+ _data: PhantomData<F>,
}
-impl<T: Send + 'static> Registration<T> {
+impl<F: ForLt> Registration<F>
+where
+ F::Of<'static>: Send + 'static,
+{
/// Create and register a new auxiliary device with the given registration data.
///
/// The `data` is owned by the registration and can be accessed through the auxiliary device
/// via [`Device::registration_data()`].
- pub fn new<E>(
- parent: &device::Device<device::Bound>,
+ pub fn new<'a, E>(
+ parent: &'a device::Device<device::Bound>,
name: &CStr,
id: u32,
modname: &CStr,
- data: impl PinInit<T, E>,
+ data: impl PinInit<F::Of<'a>, E>,
) -> Result<Devres<Self>>
where
Error: From<E>,
{
let data = KBox::pin_init::<Error>(
try_pin_init!(RegistrationData {
- type_id: TypeId::of::<T>(),
+ type_id: TypeId::of::<F::Of<'static>>(),
data <- data,
}),
GFP_KERNEL,
)?;
+ // SAFETY: Lifetimes are erased and do not affect layout, so RegistrationData<F::Of<'a>>
+ // and RegistrationData<F::Of<'static>> have identical representation.
+ let data: Pin<KBox<RegistrationData<F::Of<'static>>>> =
+ unsafe { core::mem::transmute(data) };
+
let boxed = KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)?;
let adev = boxed.get();
@@ -470,9 +493,11 @@ pub fn new<E>(
let ret = unsafe { bindings::__auxiliary_device_add(adev, modname.as_char_ptr()) };
if ret != 0 {
// SAFETY: `registration_data` was set above via `into_foreign()`.
- let _ = unsafe {
- Pin::<KBox<RegistrationData<T>>>::from_foreign((*adev).registration_data_rust)
- };
+ drop(unsafe {
+ Pin::<KBox<RegistrationData<F::Of<'static>>>>::from_foreign(
+ (*adev).registration_data_rust,
+ )
+ });
// SAFETY: `adev` is guaranteed to be a valid pointer to a
// `struct auxiliary_device`, which has been initialized.
@@ -494,7 +519,10 @@ pub fn new<E>(
}
}
-impl<T: 'static> Drop for Registration<T> {
+impl<F: ForLt> Drop for Registration<F>
+where
+ F::Of<'static>: 'static,
+{
fn drop(&mut self) {
// SAFETY: By the type invariant of `Self`, `self.adev.as_ptr()` is a valid registered
// `struct auxiliary_device`.
@@ -502,7 +530,7 @@ fn drop(&mut self) {
// SAFETY: `registration_data` was set in `new()` via `into_foreign()`.
drop(unsafe {
- Pin::<KBox<RegistrationData<T>>>::from_foreign(
+ Pin::<KBox<RegistrationData<F::Of<'static>>>>::from_foreign(
(*self.adev.as_ptr()).registration_data_rust,
)
});
@@ -516,7 +544,7 @@ fn drop(&mut self) {
}
// SAFETY: A `Registration` of a `struct auxiliary_device` can be released from any thread.
-unsafe impl<T: Send> Send for Registration<T> {}
+unsafe impl<F: ForLt> Send for Registration<F> where F::Of<'static>: Send {}
// SAFETY: `Registration` does not expose any methods or fields that need synchronization.
-unsafe impl<T: Send> Sync for Registration<T> {}
+unsafe impl<F: ForLt> Sync for Registration<F> where F::Of<'static>: Send {}
diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driver_auxiliary.rs
index d35963ac7fa4..4ad619c5731e 100644
--- a/samples/rust/rust_driver_auxiliary.rs
+++ b/samples/rust/rust_driver_auxiliary.rs
@@ -55,9 +55,10 @@ struct Data {
index: u32,
}
+#[allow(clippy::type_complexity)]
struct ParentDriver {
- _reg0: Devres<auxiliary::Registration<Data>>,
- _reg1: Devres<auxiliary::Registration<Data>>,
+ _reg0: Devres<auxiliary::Registration<ForLt!(Data)>>,
+ _reg1: Devres<auxiliary::Registration<ForLt!(Data)>>,
}
kernel::pci_device_table!(
@@ -100,7 +101,7 @@ fn connect(adev: &auxiliary::Device<Bound>) -> Result {
let dev = adev.parent();
let pdev: &pci::Device<Bound> = dev.try_into()?;
- let data = adev.registration_data::<Data>()?;
+ let data = adev.registration_data::<ForLt!(Data)>()?;
dev_info!(
dev,
--
2.54.0
next prev parent reply other threads:[~2026-04-27 22:13 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-27 22:10 [PATCH 00/24] rust: device: Higher-Ranked Lifetime Types for device drivers Danilo Krummrich
2026-04-27 22:10 ` [PATCH 01/24] rust: driver core: drop drvdata before devres release Danilo Krummrich
2026-04-27 22:11 ` [PATCH 02/24] rust: types: add `ForLt` trait for higher-ranked lifetime support Danilo Krummrich
2026-04-27 22:16 ` Danilo Krummrich
2026-04-27 22:11 ` [PATCH 03/24] rust: devres: add ForLt support to Devres Danilo Krummrich
2026-04-27 22:11 ` [PATCH 04/24] rust: device: generalize drvdata methods over ForLt Danilo Krummrich
2026-04-27 22:11 ` [PATCH 05/24] rust: driver: make Adapter trait lifetime-parameterized Danilo Krummrich
2026-04-27 22:11 ` [PATCH 06/24] rust: pci: implement Sync for Device<Bound> Danilo Krummrich
2026-04-27 23:52 ` Gary Guo
2026-04-27 22:11 ` [PATCH 07/24] rust: platform: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH 08/24] rust: auxiliary: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH 09/24] rust: usb: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH 10/24] rust: device: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH 11/24] rust: pci: make Driver trait lifetime-parameterized Danilo Krummrich
2026-04-27 22:11 ` [PATCH 12/24] rust: platform: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH 13/24] rust: auxiliary: " Danilo Krummrich
2026-04-27 22:11 ` Danilo Krummrich [this message]
2026-04-27 22:11 ` [PATCH 15/24] samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data Danilo Krummrich
2026-04-27 22:11 ` [PATCH 16/24] rust: usb: make Driver trait lifetime-parameterized Danilo Krummrich
2026-04-27 22:11 ` [PATCH 17/24] rust: i2c: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH 18/24] rust: pci: make Bar lifetime-parameterized Danilo Krummrich
2026-04-27 22:11 ` [PATCH 19/24] rust: io: make IoMem and ExclusiveIoMem lifetime-parameterized Danilo Krummrich
2026-04-27 22:11 ` [PATCH 20/24] samples: rust: rust_driver_pci: use HRT lifetime for Bar Danilo Krummrich
2026-04-27 22:11 ` [PATCH REF 21/24] gpu: nova-core: " Danilo Krummrich
2026-04-27 22:11 ` [PATCH REF 22/24] gpu: nova-core: unregister sysmem flush page from Drop Danilo Krummrich
2026-04-27 22:11 ` [PATCH REF 23/24] gpu: nova-core: replace ARef<Device> with &'a Device in SysmemFlush Danilo Krummrich
2026-04-27 22:11 ` [PATCH REF 24/24] gpu: drm: tyr: use HRT lifetime for IoMem Danilo Krummrich
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=20260427221155.2144848-15-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=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=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