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; + +#[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`. + unsafe { DEBUGFS_ROOT = None }; + } +} + module! { type: NovaCoreModule, name: "NovaCore", -- 2.52.0
