Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

gpu: nova-core: create debugfs root in module init

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 <ttabi@nvidia.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Tested-by: John Hubbard <jhubbard@nvidia.com>
Tested-by: Eliot Courtney <ecourtney@nvidia.com>
Link: https://patch.msgid.link/20260319212658.2541610-6-ttabi@nvidia.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

authored by

Timur Tabi and committed by
Danilo Krummrich
09691f5d ea0c8380

+25
+25
drivers/gpu/nova-core/nova_core.rs
··· 3 3 //! Nova Core GPU Driver 4 4 5 5 use kernel::{ 6 + debugfs, 6 7 driver::Registration, 7 8 pci, 8 9 prelude::*, ··· 28 27 29 28 pub(crate) const MODULE_NAME: &core::ffi::CStr = <LocalModule as kernel::ModuleMetadata>::NAME; 30 29 30 + // TODO: Move this into per-module data once that exists. 31 + static mut DEBUGFS_ROOT: Option<debugfs::Dir> = None; 32 + 33 + /// Guard that clears `DEBUGFS_ROOT` when dropped. 34 + struct DebugfsRootGuard; 35 + 36 + impl Drop for DebugfsRootGuard { 37 + fn drop(&mut self) { 38 + // SAFETY: This guard is dropped after `_driver` (due to field order), 39 + // so the driver is unregistered and no probe() can be running. 40 + unsafe { DEBUGFS_ROOT = None }; 41 + } 42 + } 43 + 31 44 #[pin_data] 32 45 struct NovaCoreModule { 46 + // Fields are dropped in declaration order, so `_driver` is dropped first, 47 + // then `_debugfs_guard` clears `DEBUGFS_ROOT`. 33 48 #[pin] 34 49 _driver: Registration<pci::Adapter<driver::NovaCore>>, 50 + _debugfs_guard: DebugfsRootGuard, 35 51 } 36 52 37 53 impl InPlaceModule for NovaCoreModule { 38 54 fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, Error> { 55 + let dir = debugfs::Dir::new(kernel::c_str!("nova_core")); 56 + 57 + // SAFETY: We are the only driver code running during init, so there 58 + // cannot be any concurrent access to `DEBUGFS_ROOT`. 59 + unsafe { DEBUGFS_ROOT = Some(dir) }; 60 + 39 61 try_pin_init!(Self { 40 62 _driver <- Registration::new(MODULE_NAME, module), 63 + _debugfs_guard: DebugfsRootGuard, 41 64 }) 42 65 } 43 66 }