On Fri, 12 Dec 2025 14:49:48 -0600
Timur Tabi <[email protected]> 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 | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/nova-core/nova_core.rs 
> b/drivers/gpu/nova-core/nova_core.rs
> index 7d7b75650b04..591edede4376 100644
> --- a/drivers/gpu/nova-core/nova_core.rs
> +++ b/drivers/gpu/nova-core/nova_core.rs
> @@ -2,7 +2,7 @@
>  
>  //! Nova Core GPU Driver
>  
> -use kernel::{error::Error, pci, prelude::*, InPlaceModule};
> +use kernel::{error::Error, pci, prelude::*, InPlaceModule, debugfs::Dir};
>  use pin_init::{PinInit, pinned_drop};
>  
>  #[macro_use]
> @@ -24,6 +24,8 @@
>  
>  pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as 
> kernel::ModuleMetadata>::NAME;
>  
> +static mut DEBUGFS_ROOT: Option<Dir> = None;

Please don't use `static mut`. If you need anything associcated with the
lifetime of the driver, please just put it into the module.

> +
>  #[pin_data(PinnedDrop)]
>  struct NovaCoreModule {
>      #[pin]
> @@ -33,6 +35,13 @@ struct NovaCoreModule {
>  impl InPlaceModule for NovaCoreModule {
>      fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, 
> Error> {
>          pr_info!("NovaCore GPU driver loaded\n");
> +
> +        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),
>          })
> @@ -42,6 +51,9 @@ fn init(module: &'static kernel::ThisModule) -> impl 
> PinInit<Self, Error> {
>  #[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`.
> +        unsafe { DEBUGFS_ROOT = None };

Sorry, I missed this from my previous email. But if you put this struct
inside module itself, the dtor will automatically be run on module unload.

Best,
Gary

>      }
>  }
>  

Reply via email to