This adds support for creating a DebugFS directory which is aware that
it is bound to a device. As a result, callbacks under that directory
have access to a bound device which gives them efficient access to other
Devres, ability to use dev_err! and friends, etc.
Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
rust/kernel/debugfs.rs | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
index d7b8014a6474698235203f2b7d8fec96f2bb43f8..ac614d693fa73929d095b669e9ba61958bec609e 100644
--- a/rust/kernel/debugfs.rs
+++ b/rust/kernel/debugfs.rs
@@ -11,6 +11,11 @@
#[cfg(CONFIG_DEBUG_FS)]
use crate::sync::Arc;
use crate::{
+ device::{
+ Bound,
+ Device, //
+ },
+ devres::Devres,
fmt,
prelude::*,
str::CStr,
@@ -722,3 +727,38 @@ fn new(name: &CStr) -> ScopedDir<'data, 'static> {
}
}
}
+
+impl<'a, T: 'a + Send> Devres<Scope<T>> {
+ /// Creates a new scope, which is a directory at the root of the debugfs filesystem,
+ /// associated with some data `T`, enclosed in a [`Devres`] for the provided device.
+ ///
+ /// The `init` closure is called to populate the directory with files and subdirectories. These
+ /// files can reference the data stored in the scope. Because it is stored inside a `Devres`,
+ /// the init method is granted access to a `&Device<Bound>`.
+ ///
+ /// This can be used for cheaply accessing device-protected data inside DebugFS methods or
+ /// accessing device-specific methods (e.g. [`dev_err!`]).
+ ///
+ /// The entire directory tree created within the scope will be removed when the returned
+ /// `Scope` handle is dropped.
+ pub fn dir<E: 'a, F>(
+ dev: &'a Device<Bound>,
+ data: impl PinInit<T, E> + 'a,
+ name: &'a CStr,
+ init: F,
+ ) -> impl PinInit<Self, Error> + 'a
+ where
+ F: for<'data, 'dir> FnOnce(&'data T, &'data Device<Bound>, &'dir ScopedDir<'data, 'dir>)
+ + 'a,
+ Error: From<E>,
+ {
+ Devres::new(
+ dev,
+ Scope::new(data, |data| {
+ let scoped = ScopedDir::new(name);
+ init(data, dev, &scoped);
+ scoped.into_entry()
+ }),
+ )
+ }
+}
--
2.53.0.rc2.204.g2597b5adb4-goog
On Tue Feb 3, 2026 at 3:46 PM GMT, Matthew Maurer wrote:
> This adds support for creating a DebugFS directory which is aware that
> it is bound to a device. As a result, callbacks under that directory
> have access to a bound device which gives them efficient access to other
> Devres, ability to use dev_err! and friends, etc.
>
> Signed-off-by: Matthew Maurer <mmaurer@google.com>
> ---
> rust/kernel/debugfs.rs | 40 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 40 insertions(+)
>
> diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
> index d7b8014a6474698235203f2b7d8fec96f2bb43f8..ac614d693fa73929d095b669e9ba61958bec609e 100644
> --- a/rust/kernel/debugfs.rs
> +++ b/rust/kernel/debugfs.rs
> @@ -11,6 +11,11 @@
> #[cfg(CONFIG_DEBUG_FS)]
> use crate::sync::Arc;
> use crate::{
> + device::{
> + Bound,
> + Device, //
> + },
> + devres::Devres,
> fmt,
> prelude::*,
> str::CStr,
> @@ -722,3 +727,38 @@ fn new(name: &CStr) -> ScopedDir<'data, 'static> {
> }
> }
> }
> +
> +impl<'a, T: 'a + Send> Devres<Scope<T>> {
> + /// Creates a new scope, which is a directory at the root of the debugfs filesystem,
> + /// associated with some data `T`, enclosed in a [`Devres`] for the provided device.
> + ///
> + /// The `init` closure is called to populate the directory with files and subdirectories. These
> + /// files can reference the data stored in the scope. Because it is stored inside a `Devres`,
> + /// the init method is granted access to a `&Device<Bound>`.
> + ///
> + /// This can be used for cheaply accessing device-protected data inside DebugFS methods or
> + /// accessing device-specific methods (e.g. [`dev_err!`]).
> + ///
> + /// The entire directory tree created within the scope will be removed when the returned
> + /// `Scope` handle is dropped.
> + pub fn dir<E: 'a, F>(
> + dev: &'a Device<Bound>,
> + data: impl PinInit<T, E> + 'a,
> + name: &'a CStr,
> + init: F,
> + ) -> impl PinInit<Self, Error> + 'a
> + where
> + F: for<'data, 'dir> FnOnce(&'data T, &'data Device<Bound>, &'dir ScopedDir<'data, 'dir>)
> + + 'a,
> + Error: From<E>,
> + {
> + Devres::new(
> + dev,
> + Scope::new(data, |data| {
> + let scoped = ScopedDir::new(name);
> + init(data, dev, &scoped);
> + scoped.into_entry()
> + }),
> + )
> + }
> +}
I think it is a big strange to have this on `Devres` (in patch v6 it has `Devres::dir` doesn't make
too much sense). I would suggest that we domsomething like
impl<'a, T: 'a + Send> Scope<T> {
pub fn devres_dir(
...
) -> impl PinInit<Devres<Self>, Error> + 'a;
}
To me `Devres` is just a generic container type, just like `Arc` and `ARef`, so
the assoc functions should be defined on the concrete type.
Also: is there a reason that this needs a special API, and by
Devres::new(device, Scope::dir(data, c"name", |data| {
// use data and device
});
?
Best,
Gary
On Tue, Feb 3, 2026 at 8:48 AM Gary Guo <gary@garyguo.net> wrote:
>
> On Tue Feb 3, 2026 at 3:46 PM GMT, Matthew Maurer wrote:
> > This adds support for creating a DebugFS directory which is aware that
> > it is bound to a device. As a result, callbacks under that directory
> > have access to a bound device which gives them efficient access to other
> > Devres, ability to use dev_err! and friends, etc.
> >
> > Signed-off-by: Matthew Maurer <mmaurer@google.com>
> > ---
> > rust/kernel/debugfs.rs | 40 ++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 40 insertions(+)
> >
> > diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
> > index d7b8014a6474698235203f2b7d8fec96f2bb43f8..ac614d693fa73929d095b669e9ba61958bec609e 100644
> > --- a/rust/kernel/debugfs.rs
> > +++ b/rust/kernel/debugfs.rs
> > @@ -11,6 +11,11 @@
> > #[cfg(CONFIG_DEBUG_FS)]
> > use crate::sync::Arc;
> > use crate::{
> > + device::{
> > + Bound,
> > + Device, //
> > + },
> > + devres::Devres,
> > fmt,
> > prelude::*,
> > str::CStr,
> > @@ -722,3 +727,38 @@ fn new(name: &CStr) -> ScopedDir<'data, 'static> {
> > }
> > }
> > }
> > +
> > +impl<'a, T: 'a + Send> Devres<Scope<T>> {
> > + /// Creates a new scope, which is a directory at the root of the debugfs filesystem,
> > + /// associated with some data `T`, enclosed in a [`Devres`] for the provided device.
> > + ///
> > + /// The `init` closure is called to populate the directory with files and subdirectories. These
> > + /// files can reference the data stored in the scope. Because it is stored inside a `Devres`,
> > + /// the init method is granted access to a `&Device<Bound>`.
> > + ///
> > + /// This can be used for cheaply accessing device-protected data inside DebugFS methods or
> > + /// accessing device-specific methods (e.g. [`dev_err!`]).
> > + ///
> > + /// The entire directory tree created within the scope will be removed when the returned
> > + /// `Scope` handle is dropped.
> > + pub fn dir<E: 'a, F>(
> > + dev: &'a Device<Bound>,
> > + data: impl PinInit<T, E> + 'a,
> > + name: &'a CStr,
> > + init: F,
> > + ) -> impl PinInit<Self, Error> + 'a
> > + where
> > + F: for<'data, 'dir> FnOnce(&'data T, &'data Device<Bound>, &'dir ScopedDir<'data, 'dir>)
> > + + 'a,
> > + Error: From<E>,
> > + {
> > + Devres::new(
> > + dev,
> > + Scope::new(data, |data| {
> > + let scoped = ScopedDir::new(name);
> > + init(data, dev, &scoped);
> > + scoped.into_entry()
> > + }),
> > + )
> > + }
> > +}
>
> I think it is a big strange to have this on `Devres` (in patch v6 it has `Devres::dir` doesn't make
> too much sense). I would suggest that we domsomething like
>
> impl<'a, T: 'a + Send> Scope<T> {
> pub fn devres_dir(
> ...
> ) -> impl PinInit<Devres<Self>, Error> + 'a;
> }
>
> To me `Devres` is just a generic container type, just like `Arc` and `ARef`, so
> the assoc functions should be defined on the concrete type.
>
> Also: is there a reason that this needs a special API, and by
>
> Devres::new(device, Scope::dir(data, c"name", |data| {
> // use data and device
> });
>
> ?
Yes - that won't work, because the function being provided to
`Scope::dir` is `for<'data, 'dir> FnOnce(&'data T, &'dir
ScopedDir<'data, 'dir>)` - this means that *intentionally*, if you
capture any non-static-lifetime variable from outside the closure, you
won't be able to use it with the methods on `ScopedDir`, because the
`'data` lifetime bound should stop you. In the general case, we
wouldn't want a reference with the same lifetime as `device` in that
example to be usable inside the debugfs callbacks. The device of a
Devres wrapped scope is a special case because we know that it will
outlive it.
>
> Best,
> Gary
>
>
On Tue Feb 3, 2026 at 5:47 PM CET, Gary Guo wrote:
> I think it is a big strange to have this on `Devres` (in patch v6 it has `Devres::dir` doesn't make
> too much sense). I would suggest that we domsomething like
>
> impl<'a, T: 'a + Send> Scope<T> {
> pub fn devres_dir(
> ...
> ) -> impl PinInit<Devres<Self>, Error> + 'a;
> }
Good catch, I did not notice that this is implemented on Devres, rather than
debugfs. This should not be implemented on Devres.
> To me `Devres` is just a generic container type, just like `Arc` and `ARef`, so
> the assoc functions should be defined on the concrete type.
Indded.
On Tue Feb 3, 2026 at 4:46 PM CET, Matthew Maurer wrote: > This adds support for creating a DebugFS directory which is aware that > it is bound to a device. As a result, callbacks under that directory > have access to a bound device which gives them efficient access to other > Devres, ability to use dev_err! and friends, etc. > Suggested-by: Danilo Krummrich <dakr@kernel.org> > Signed-off-by: Matthew Maurer <mmaurer@google.com> Again, Acked-by: Danilo Krummrich <dakr@kernel.org> if this should go through another tree, but I can also pick it up.
© 2016 - 2026 Red Hat, Inc.