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;
+
 #[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 };
     }
 }
 
-- 
2.52.0

Reply via email to