On Sun Jun 28, 2026 at 11:53 PM JST, Danilo Krummrich wrote:
<...>
> diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs
> index 5e8b75dab0a6..7a3a0e21e955 100644
> --- a/rust/kernel/drm/device.rs
> +++ b/rust/kernel/drm/device.rs
> @@ -32,6 +32,7 @@
> };
> use core::{
> alloc::Layout,
> + cell::UnsafeCell,
> marker::PhantomData,
> mem,
> ops::Deref,
> @@ -247,6 +248,9 @@ pub fn new(
> // SAFETY: `drm_dev` is still private to this function.
> unsafe { (*drm_dev).driver = const { &Self::VTABLE } };
>
> + // SAFETY: `raw_drm` is valid; no concurrent access before
> registration.
> + unsafe { (*raw_drm.as_ptr()).registration_data =
> UnsafeCell::new(NonNull::dangling()) };
> +
> // SAFETY: The reference count is one, and now we take ownership of
> that reference as a
> // `drm::Device`.
> // INVARIANT: We just created the device above, but have yet to call
> `drm_dev_register`.
> @@ -270,6 +274,7 @@ pub fn new(
> pub struct Device<T: drm::Driver, C: DeviceContext = Normal> {
> dev: Opaque<bindings::drm_device>,
> data: T::Data,
> + pub(super) registration_data:
> UnsafeCell<NonNull<T::RegistrationData<'static>>>,
> _ctx: PhantomData<C>,
> }
>
> @@ -278,6 +283,28 @@ pub(crate) fn as_raw(&self) -> *mut bindings::drm_device
> {
> self.dev.get()
> }
>
> + /// Returns a reference to the registration data with lifetime shortened
> from `'static`.
> + ///
> + /// # Safety
> + ///
> + /// The caller must ensure that:
> + ///
> + /// - The parent bus device is bound (e.g. by holding an active
> `drm_dev_enter()` critical
> + /// section via [`RegistrationGuard`]).
In practice, `registration_data_unchecked` is only ever called from a
`Registered` context, so can't we move its definition to an `impl<T>
Device<T, Registered>` block so this invariant is enforced by the type
system?