On Fri Jan 16, 2026 at 10:49 PM CET, Timur Tabi wrote:
> Create the 'nova_core' root debugfs entry when the driver loads.
>
> Normally, non-const global variables need to be protected by a
> mutex.  Instead, we use unsafe code, as we know the entry is never
> modified after the driver is loaded.  This solves the lifetime
> issue of the mutex guard, which would otherwise have required the
> use of `pin_init_scope`.
>
> Signed-off-by: Timur Tabi <[email protected]>
> ---
>  drivers/gpu/nova-core/nova_core.rs | 20 +++++++++++++++++++-
>  1 file changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/nova-core/nova_core.rs 
> b/drivers/gpu/nova-core/nova_core.rs
> index d0df8db3693d..eceefa41cddc 100644
> --- a/drivers/gpu/nova-core/nova_core.rs
> +++ b/drivers/gpu/nova-core/nova_core.rs
> @@ -3,6 +3,7 @@
>  //! Nova Core GPU Driver
>  
>  use kernel::{
> +    debugfs::Dir,
>      error::Error,
>      pci,
>      prelude::*,
> @@ -27,7 +28,9 @@
>  
>  pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as 
> kernel::ModuleMetadata>::NAME;
>  
> -#[pin_data]
> +static mut DEBUGFS_ROOT: Option<Dir> = None;

Please add the FIXME comment that we talked about in the last revision and use
debugfs::Dir rather than just Dir.

> +
> +#[pin_data(PinnedDrop)]
>  struct NovaCoreModule {
>      #[pin]
>      _driver: kernel::driver::Registration<pci::Adapter<driver::NovaCore>>,
> @@ -35,12 +38,27 @@ struct NovaCoreModule {
>  
>  impl InPlaceModule for NovaCoreModule {
>      fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, 
> Error> {
> +        let dir = Dir::new(kernel::c_str!("nova_core"));
> +
> +        // SAFETY: we are the only driver code running, so there cannot be 
> any concurrent access to
> +        // `DEBUGFS_ROOT`.
> +        unsafe { DEBUGFS_ROOT = Some(dir) };
> +
>          try_pin_init!(Self {
>              _driver <- kernel::driver::Registration::new(MODULE_NAME, 
> module),
>          })
>      }
>  }
>  
> +#[pinned_drop]
> +impl PinnedDrop for NovaCoreModule {
> +    fn drop(self: Pin<&mut Self>) {
> +        // SAFETY: we are the only driver code running, so there cannot be 
> any concurrent access to
> +        // `DEBUGFS_ROOT`.

The driver::Registration is not yet dropped at this point, so there might be
other driver code running. You have to justify that this does not race with
module_init() (which module_exit() never does) and that it does not race with
Gsp::new(), i.e. probe().

Note that probe() may run async, so you have to drop the driver registration
manually before dropping the debugfs dir.

> +        unsafe { DEBUGFS_ROOT = None };

You also have to drop the debugfs::Dir.

> +    }
> +}
> +
>  module! {
>      type: NovaCoreModule,
>      name: "NovaCore",
> -- 
> 2.52.0

Reply via email to