Re: [PATCH v2 3/9] rust: debugfs: add Entry::from_raw and ScopedDir::from_dentry

"Danilo Krummrich" <[email protected]> Mon, 03 Aug 2026 19:55:27 +0200
Newsgroups dev.linux.lists.driver-core,org.freedesktop.lists.dri-devel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Thu Jul 30, 2026 at 7:05 PM CEST, Alvin Sun wrote:
> +    /// Creates a [`ScopedDir`] wrapping an existing debugfs dentry.
> +    ///
> +    /// Files created under this directory are not automatically removed=
 on drop;
> +    /// their lifetime is tied to the dentry owner.
> +    ///
> +    /// # Safety
> +    ///
> +    /// The caller must ensure the dentry remains valid for the lifetime=
 of the
> +    /// returned `ScopedDir`.
> +    pub unsafe fn from_dentry(dentry: *mut bindings::dentry) -> Self {
> +        let _ =3D dentry;
> +        ScopedDir {
> +            #[cfg(CONFIG_DEBUG_FS)]
> +            // SAFETY: The caller guarantees the dentry is valid and out=
lives this `ScopedDir`.
> +            entry: ManuallyDrop::new(unsafe { Entry::from_raw(dentry) })=
,
> +            _phantom: PhantomData,
> +        }
> +    }

I think what you actually want to express is that the DRM debugfs root dir
represents a view into an existing scope, where the data is T::Registration=
Data.

Additionally, we want to be able to derive new debugfs::Scopes from this th=
at
are shorter lived, so we can create new debugfs::Scopes e.g. in
drm::DriverFile<'a>, which I currently work on.

So, what I'm thinking of is a type like:

	pub struct ScopeRef<'a, T> {
	    #[cfg(CONFIG_DEBUG_FS)]
	    dentry: Option<NonNull<bindings::dentry>>,
	    data: &'a T,
	}

which has a corresponding unsafe constructor and provides methods to e.g. c=
reate
a new directory, returning a new ScopeRef<'a, T>, which can be stored in
lifetime scoped data.

Additionally, but that's somewhat orthogonal, we want debugfs::Scope to sup=
port
lifetime data, so a scope can participate in the driver lifecycle; the
corresponding synchronization is already in place with full proxy fops.