Re: [PATCH 3/8] rust: debugfs: Add scoped builder interface

Greg Kroah-Hartman
2025-04-30T12:06:15Z

On Tue, Apr 29, 2025 at 11:15:57PM +0000, Matthew Maurer wrote:
> This adds an interface which allows access to references which may not
> live indefinitely by forcing them to live at least as long as the
> DebugFS directory itself.

They can ONLY live as long as the debugfs directory lives, is that what
you mean here?

>
> Signed-off-by: Matthew Maurer <mmaurer@google.com>
> ---
> rust/kernel/debugfs.rs | 163 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 163 insertions(+)
>
> diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
> index b20df5fce692b3047c804f7ad5af90fc4248979b..f6240fd056f8598d5ef06bdaf61c5c33eab5b734 100644
> --- a/rust/kernel/debugfs.rs
> +++ b/rust/kernel/debugfs.rs
> @@ -12,7 +12,12 @@
> use crate::str::CStr;
> use crate::types::{ARef, AlwaysRefCounted, Opaque};
> use core::fmt::Display;
> +use core::marker::{PhantomData, PhantomPinned};
> +use core::mem::ManuallyDrop;
> +use core::ops::Deref;
> +use core::pin::Pin;
> use core::ptr::NonNull;
> +use pin_init::{pin_data, pinned_drop, PinInit};
>
> /// Handle to a DebugFS directory.
> pub struct Dir {
> @@ -117,6 +122,22 @@ pub fn display_file<T: Display + Sized>(
> &self,
> name: &CStr,
> data: &'static T,
> + ) -> Result<ARef<Self>> {
> + // SAFETY: As `data` lives for the static lifetime, it outlives the file.
> + unsafe { self.display_file_raw(name, data) }
> + }
> +
> + /// Creates a DebugFS file backed by the display implementation of the provided pointer.
> + ///
> + /// # Safety
> + /// The pointee of `data` must outlive the accessibility of the `Dir` returned by this function.
> + /// This means that before `data` may become invalid, either:
> + /// * The refcount must go to zero
> + /// * The file must be rendered inaccessible, e.g. via `debugfs_remove`

Perhaps the refcount should be the thing driving debugfs_remove()? That
might save you lots of mess overall I think.

thanks,

greg k-h