Skip to contentSkip to conversation

gpu: nova-core: retain the GSP-RM log buffers

PATCH Superseded Vladislav Zaharov 3 participants 7 messages Sep 13 – Sep 14

Subsystems (6)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover gpu: nova-core: retain the GSP-RM log buffers

The GSP-RM log buffers are exposed through debugfs, but the entries are owned by the Gpu that probe() builds, and the buffers themselves are DMA allocations that cannot outlive the device. They are therefore gone as soon as the GPU is unbound, and in particular as soon as probe() fails - which is the case todo.rst singled out, and the one where a GSP log is worth having.

Patch 1 gives the module data a DebugfsData that owns the debugfs root, replacing the static that held it. Patch 2 adds a gsp_keep_logs module
parameter: when it is set, whatever the GSP wrote is copied into that same DebugfsData and exposed under a "retained" directory until the module is unloaded. Patch 3 drops the now completed task from todo.rst.

Changes since v3:
- keep the copies in the module data rather than in a second global. The debugfs root moves there too, so one pointer now replaces both statics, and the global lock and its unsafe initialization are gone (Gary Guo)
- with that, the module data owns the root and is dropped after the registration, so a module init that fails can no longer leave the directory behind. v3 fixed that by ordering the fields of the module data; it now follows from where the root lives, and the guard type is gone
- keep a copy of the device name with the retained copies rather than a reference to the device, so that a GPU that is gone does not stay allocated until the module is unloaded
- drop a re-check of the retained directory that could not fire

The pointer is published from inside the initializer of DebugfsData, through the &this in form, and it is a raw pointer rather than a
reference: pin-init does not allow references to fields to be created inside an initializer. Readers turn it back into a reference in one place.

Testing was done on top of drm-rust-next with the TLV firmware images installed. On a GB203:

- with gsp_keep_logs unset, no "retained" directory is created and the

    entries disappear on unbind, as before;
  - with gsp_keep_logs=1, retained/<BDF>/{loginit,logintr,logrm} hold
    the contents the live entries had, all 64 KiB of each readable;

- binding the GPU again recreates the live entries without disturbing the copies, and unbinding it a second time replaces them, leaving exactly one set behind;
- with a failure injected after the GSP has booted, probe() fails with -EINVAL, the driver stays unbound, and the logs of that attempt are still readable;
- with a failure injected into the Registration instead, the module fails to load, leaves no debugfs directory behind, and the next load comes up with its directory intact;
- the copies are released on module unload, and three load/unload cycles leave nothing behind.

The parameter does need a value: the Rust bool param ops do not set KERNEL_PARAM_OPS_FL_NOARG, so a bare gsp_keep_logs is refused, where the C bool would have taken it.

No warnings, oopses or refcount complaints in dmesg throughout. Built and checked with CLIPPY=1 and rustfmtcheck. checkpatch --strict is clean apart from the MAINTAINERS note for the new file, which is already covered by the existing "F: drivers/gpu/nova-core/" pattern.

John Hubbard's r000 series adds three more log buffers in gsp.rs. Whichever of the two lands second needs a small rebase; the retained copies extend to the new buffers by adding them to RetainedLogBuffers.

v3: https://lore.kernel.org/nova-gpu/[email protected]/
v2: https://lore.kernel.org/nova-gpu/[email protected]/

Vladislav Zaharov (3):
gpu: nova-core: move the debugfs root into the module data
gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind
Documentation: nova: remove completed GSP log buffer task

 Documentation/gpu/nova/core/todo.rst   |  12 --
 drivers/gpu/nova-core/gsp.rs           | 100 ++--------
 drivers/gpu/nova-core/gsp/logbuffer.rs | 253 +++++++++++++++++++++++++
 drivers/gpu/nova-core/nova_core.rs     | 109 +++++++++--
 4 files changed, 362 insertions(+), 112 deletions(-)
 create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs

base-commit: 66a2c223b620d844fe26c6bd4844d2a6a8c9dffc

Signature · 1 line
2.55.0
#1 lore raw

PATCH v4 1/3 gpu: nova-core: move the debugfs root into the module data

The debugfs root lives in a static that init() fills in and a guard field of the module data clears again. That costs a static mut, an unsafe write on each side and a guard type whose only job is to undo the write.

It also leaks. try_pin_init! drops only the fields it has already built, and the guard is written after the Registration, so a registration that fails leaves the guard unbuilt and the static set. Statics are never dropped, and the module is unloaded right away, so the directory outlives everything that could remove it. The next load then finds the name taken: debugfs_create_dir() returns -EEXIST, which Entry keeps as it would any other pointer, and the driver comes up with no debugfs at all until the machine is rebooted.

Have the module data own a DebugfsData instead, built before the registration and dropped after it, and keep only a pointer to it in the static, for devices that have no other way to reach the data of their module. What is left is one unsafe read for the users and one write on each side, with no guard type. A registration that fails now drops the data that was built before it, and the directory goes with it.

Assisted-by Claude:claude-opus-5
---
 drivers/gpu/nova-core/gsp.rs       | 15 +++---
 drivers/gpu/nova-core/nova_core.rs | 82 +++++++++++++++++++++++-------
 2 files changed, 69 insertions(+), 28 deletions(-)
drivers/gpu/nova-core/gsp.rs +6 -9
index 25ea43f1cbe9..f29e601e6753 100644
@@ -196,15 +196,12 @@ pub(crate) fn new(pdev: &'gsp pci::Device<device::Bound>) -> impl PinInit<Self,
                         logrm,
                     };
 
-                    #[allow(static_mut_refs)]
-                    // SAFETY: `DEBUGFS_ROOT` is created before driver registration and cleared
-                    // after driver unregistration, so no probe() can race with its modification.
-                    //
-                    // PANIC: `DEBUGFS_ROOT` cannot be `None` here.  It is set before driver
-                    // registration and cleared after driver unregistration, so it is always
-                    // `Some` for the entire lifetime that probe() can be called.
-                    let log_parent: &debugfs::Dir = unsafe { crate::DEBUGFS_ROOT.as_ref() }
-                        .expect("DEBUGFS_ROOT not initialized");
+                    // PANIC: The module data cannot be gone here. It is published before the
+                    // driver is registered and taken down after it is unregistered, so it is
+                    // there for as long as probe() can be called.
+                    let log_parent: &debugfs::Dir = crate::debugfs_data()
+                        .expect("module data not initialized")
+                        .root();
 
                     log_parent.scope(log_buffers, dev.name(), |logs, dir| {
                         dir.read_binary_file(c"loginit", &logs.loginit.0);
drivers/gpu/nova-core/nova_core.rs +63 -19

103 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v4 1/3] gpu: nova-core: move the debugfs root into the module data

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing invariant comment for `DebugfsData` construction --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing invariant comment for DebugfsData construction
--

commit f5ca13df2d92dfa80fcdd81e199934e5ce48a7c2
Author: Vladislav Zaharov <[email protected]>

gpu: nova-core: move the debugfs root into the module data

This commit moves the debugfs root directory into a new DebugfsData structure owned by the module, instead of using a static mut with a guard type. This prevents leaks where a failing driver registration would leave the directory lingering.

Quoted 3 lines
drivers/gpu/nova-core/nova_core.rs +0 -0

[ ... ]

drivers/gpu/nova-core/nova_core.rs line 63

Quoted 13 lines
drivers/gpu/nova-core/nova_core.rs +13 -0
+impl DebugfsData {
+    /// Creates the shared data and publishes it, so that [`debugfs_data()`] can hand it out.
+    fn new() -> impl PinInit<Self> {
+        pin_init!(&this in Self {
+            root: debugfs::Dir::new(c"nova-core"),
+            _: {
+                // SAFETY: Module initialization runs once and before the driver is registered, so
+                // nothing can be reading `DEBUGFS_DATA` while it is written here. `this` is where
+                // the data is being built, and it stays there: the module data never moves.
+                unsafe { DEBUGFS_DATA = this.as_ptr() };
+            },
+        })
+    }

[Severity: Low]
Is an // INVARIANT: comment missing here? DebugfsData has a documented invariant, and is being constructed here via pin_init!, but there is no comment explaining how the invariants are satisfied.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v4 1/3] gpu: nova-core: move the debugfs root into the module data

In reply to Vladislav Zaharov
Quoted 48 lines · Vladislav Zaharov

The debugfs root lives in a static that init() fills in and a guard field of the module data clears again. That costs a static mut, an unsafe write on each side and a guard type whose only job is to undo the write.

It also leaks. try_pin_init! drops only the fields it has already built, and the guard is written after the Registration, so a registration that fails leaves the guard unbuilt and the static set. Statics are never dropped, and the module is unloaded right away, so the directory outlives everything that could remove it. The next load then finds the name taken: debugfs_create_dir() returns -EEXIST, which Entry keeps as it would any other pointer, and the driver comes up with no debugfs at all until the machine is rebooted.

Have the module data own a DebugfsData instead, built before the registration and dropped after it, and keep only a pointer to it in the static, for devices that have no other way to reach the data of their module. What is left is one unsafe read for the users and one write on each side, with no guard type. A registration that fails now drops the data that was built before it, and the directory goes with it.

Assisted-by Claude:claude-opus-5
---
 drivers/gpu/nova-core/gsp.rs       | 15 +++---
 drivers/gpu/nova-core/nova_core.rs | 82 +++++++++++++++++++++++-------
 2 files changed, 69 insertions(+), 28 deletions(-)
drivers/gpu/nova-core/nova_core.rs +5 -4
index 1133c6ce5c55..0f8501c26e05 100644
@@ -30,40 +30,84 @@
 
 pub(crate) const MODULE_NAME: &core::ffi::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
 
-// TODO: Move this into per-module data once that exists.
-static mut DEBUGFS_ROOT: Option<debugfs::Dir> = None;
+/// Pointer to the [`DebugfsData`] the module owns.
+///
+/// A device has no way to reach the data of its module, so probe() goes through here instead.
+// TODO: Drop this once devices can reach the data of their module.
+static mut DEBUGFS_DATA: *const DebugfsData = core::ptr::null();
 
[snip]
 
-impl Drop for DebugfsRootGuard {
-    fn drop(&mut self) {

drivers/gpu/nova-core/nova_core.rs line 77

Quoted 10 lines
drivers/gpu/nova-core/nova_core.rs +7 -3
-        // SAFETY: This guard is dropped after `_driver` (due to field order),
-        // so the driver is unregistered and no probe() can be running.
-        unsafe { DEBUGFS_ROOT = None };
+#[pinned_drop]
+impl PinnedDrop for DebugfsData {
+    fn drop(self: Pin<&mut Self>) {
+        // SAFETY: This runs after the registration is dropped, as the fields of `NovaCoreModule`
+        // are dropped in declaration order, so the driver is unregistered and neither a probe()
+        // nor the teardown of a device can be reading `DEBUGFS_DATA`.
+        unsafe { DEBUGFS_DATA = core::ptr::null() };

I think we can drop this. If we're still accessing it after registration is dropped and just before module unload, we have a bigger problem.

Dropping this would allow this pointer to be completely uncoupled of the struct itself (it really is a module-level mechanism and not coupled to this type).

Quoted 6 lines
     }

}

drivers/gpu/nova-core/nova_core.rs +3 -0
+/// Returns the data the module shares with its devices, or [`None`] if there is none yet.
+///
+/// Only ever call this while the driver is registered, which is to say from probe() or from the

drivers/gpu/nova-core/nova_core.rs line 93

Quoted 10 lines
drivers/gpu/nova-core/nova_core.rs +10 -0
+/// teardown of a device that is bound: the data is built before the registration and dropped
+/// after it, and nothing else keeps what is returned here alive.
+pub(crate) fn debugfs_data() -> Option<&'static DebugfsData> {
+    // SAFETY: `DEBUGFS_DATA` is written while the module data is initialized, before the driver
+    // is registered, and again when that data is dropped, after the driver is unregistered. Both
+    // happen with no device bound, so a caller in probe() or in the teardown of a device cannot
+    // race with either, and by the type invariant what it gets points at live data that outlives
+    // the device it is used from.
+    unsafe { DEBUGFS_DATA.as_ref() }
+}

The 'static signature would be lying here. And also it is not ideal that this returns a Option; the user would be always unwrapping it.

Instead, you can do this:

    pub(crate) fn debugfs_data<'a>(dev: &'a Device<Bound>) -> &'a DebugfsData {
        // SAFETY: `_debugfs` field of module data is dropped after
        // registration. So it must be alive while a device is bound.
        unsafe { &*DEBUGFS_DATA }
    }

See? This way the = null() becomes truly redundant because we use lifetime to restrict how long that data can be accessed.

Quoted 15 lines
drivers/gpu/nova-core/nova_core.rs +6 -3
+
 #[pin_data]
 struct NovaCoreModule {
-    // Fields are dropped in declaration order, so `_driver` is dropped first,
-    // then `_debugfs_guard` clears `DEBUGFS_ROOT`.
+    // Fields are dropped in declaration order, so the registration goes first and no probe() can
+    // still be running once the shared data is torn down. `init()` builds them the other way
+    // round, as the data has to be there before the first probe() reaches for it.
     #[pin]
     _driver: Registration<pci::Adapter<driver::NovaCoreDriver>>,
-    _debugfs_guard: DebugfsRootGuard,
+    #[pin]
+    _debugfs: DebugfsData,
 }
 

drivers/gpu/nova-core/nova_core.rs line 109

Quoted 10 lines
drivers/gpu/nova-core/nova_core.rs +1 -6
 impl InPlaceModule for NovaCoreModule {
     fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, Error> {
-        let dir = debugfs::Dir::new(c"nova-core");
-
-        // SAFETY: We are the only driver code running during init, so there
-        // cannot be any concurrent access to `DEBUGFS_ROOT`.
-        unsafe { DEBUGFS_ROOT = Some(dir) };
-
         try_pin_init!(Self {
+            _debugfs <- DebugfsData::new(),

The pointer should be set here instead, not part of DebugfsData. pin-init gives you initialized pointer of _debugfs. So something like (untested):

    _: {
        unsafe { DEBUGFS_DATA = _debugfs.get_ref(); }
    },

should work.

Best,
Gary

Quoted 5 lines

_driver <- Registration::new(MODULE_NAME, module),
- _debugfs_guard: DebugfsRootGuard,

         })
     }

}

#4 lore raw

PATCH v4 2/3 gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind

In reply to Vladislav Zaharov

The GSP-RM log buffers are exposed through debugfs, but the Scope that owns them lives in Gsp, inside GspResources, inside the Gpu built by probe(). They are DMA allocations of the device and cannot outlive it, so the entries go away as soon as the GPU is unbound - and, more to the point, as soon as probe() fails, which is exactly when the log of a GSP that did not come up is the thing one wants to read.

Add a gsp_keep_logs module parameter. When it is set, dropping the log buffers copies whatever the GSP wrote into memory owned by the module and exposes the copies until the module is unloaded. A buffer whose "put" pointer is still zero was never written to and is skipped.

The GSP has normally been stopped by the time the buffers are dropped, but a boot that timed out can leave it still appending, so a DMA read barrier orders the read of the "put" pointer before the copy.

The copies belong to the module data next to the debugfs root, and live in a "retained" directory created during module init rather than on first use, which keeps the teardown path of a device from having to create anything. Keeping them out of the directory used by bound GPUs also means a device coming back does not find its debugfs name taken by its own history; nouveau, which recreates the entries under the name of the GPU that just went away, has that problem.

While at it, move the log buffer code out of gsp.rs into gsp/logbuffer.rs.

Assisted-by Claude:claude-opus-5
---
 drivers/gpu/nova-core/gsp.rs           |  97 ++--------
 drivers/gpu/nova-core/gsp/logbuffer.rs | 253 +++++++++++++++++++++++++
 drivers/gpu/nova-core/nova_core.rs     |  29 ++-
 3 files changed, 300 insertions(+), 79 deletions(-)
 create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs
drivers/gpu/nova-core/gsp.rs +19 -78
index f29e601e6753..1a1eb7f37075 100644
@@ -12,11 +12,7 @@
         CoherentView,
         DmaAddress, //
     },
-    io::{
-        io_project,
-        io_write,
-        Io, //
-    },
+    io::io_write,
     pci,
     prelude::*, //
 };
@@ -24,9 +20,13 @@
 pub(crate) mod cmdq;
 pub(crate) mod commands;
 mod fw;
+mod logbuffer;
 mod regs;
 mod sequencer;
 
+use logbuffer::LogBuffers;
+pub(crate) use logbuffer::RetainedLogs;
+
 pub(crate) use fw::{
     GspFmcBootParams,
     GspFwWprMeta,
@@ -77,10 +77,6 @@ pub(crate) fn dev(&self) -> &'gpu device::Device<device::Bound> {
     }
 }
 
-/// Number of GSP pages to use in a RM log buffer.
-const RM_LOG_BUFFER_NUM_PAGES: usize = 0x10;
-const LOG_BUFFER_SIZE: usize = RM_LOG_BUFFER_NUM_PAGES * GSP_PAGE_SIZE;
-
 /// Array of page table entries, as understood by the GSP bootloader.
 #[repr(C)]
 #[derive(FromBytes, IntoBytes)]
@@ -101,49 +97,6 @@ fn init(view: CoherentView<'_, Self>, start: DmaAddress) -> Result<()> {
     }
 }
 
-/// The logging buffers are byte queues that contain encoded printf-like
-/// messages from GSP-RM.  They need to be decoded by a special application
-/// that can parse the buffers.
-///
-/// The 'loginit' buffer contains logs from early GSP-RM init and
-/// exception dumps.  The 'logrm' buffer contains the subsequent logs. Both are
-/// written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE.
-///
-/// The physical address map for the log buffer is stored in the buffer
-/// itself, starting with offset 1. Offset 0 contains the "put" pointer (pp).
-/// Initially, pp is equal to 0. If the buffer has valid logging data in it,
-/// then pp points to index into the buffer where the next logging entry will
-/// be written. Therefore, the logging data is valid if:
-///   1 <= pp < sizeof(buffer)/sizeof(u64)
-struct LogBuffer<'a>(Coherent<'a, [u8; LOG_BUFFER_SIZE]>);
-
-impl<'a> LogBuffer<'a> {
-    /// Creates a new `LogBuffer` mapped on `dev`.
-    fn new(dev: &'a device::Device<device::Bound>) -> Result<Self> {
-        let obj = Self(Coherent::zeroed(dev, GFP_KERNEL)?);
-
-        let start_addr = obj.0.dma_address();
-
-        let pte_view = io_project!(
-            obj.0,
-            [build: size_of::<u64>()..][build: ..RM_LOG_BUFFER_NUM_PAGES * size_of::<u64>()]
-        )
-        .try_cast::<PteArray<RM_LOG_BUFFER_NUM_PAGES>>()?;
-        PteArray::init(pte_view, start_addr)?;
-
-        Ok(obj)
-    }
-}
-
-struct LogBuffers<'a> {
-    /// Init log buffer.
-    loginit: LogBuffer<'a>,
-    /// Interrupts log buffer.
-    logintr: LogBuffer<'a>,
-    /// RM log buffer.
-    logrm: LogBuffer<'a>,
-}
-
 /// GSP runtime data.
 #[pin_data]
 pub(crate) struct Gsp<'gsp> {
@@ -165,9 +118,7 @@ pub(crate) fn new(pdev: &'gsp pci::Device<device::Bound>) -> impl PinInit<Self,
         pin_init::pin_init_scope(move || {
             let dev = pdev.as_ref();
 
-            let loginit = LogBuffer::new(dev)?;
-            let logintr = LogBuffer::new(dev)?;
-            let logrm = LogBuffer::new(dev)?;
+            let log_buffers = LogBuffers::new(dev)?;
 
             // Initialise the logging structures. The OpenRM equivalents are in:
             // _kgspInitLibosLoggingStructures (allocates memory for buffers)
@@ -182,33 +133,23 @@ pub(crate) fn new(pdev: &'gsp pci::Device<device::Bound>) -> impl PinInit<Self,
                         GFP_KERNEL,
                     )?;
 
-                    libos.init_at(0, LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0))?;
-                    libos.init_at(1, LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0))?;
-                    libos.init_at(2, LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?;
+                    libos.init_at(
+                        0,
+                        LibosMemoryRegionInitArgument::new("LOGINIT", &log_buffers.loginit.0),
+                    )?;
+                    libos.init_at(
+                        1,
+                        LibosMemoryRegionInitArgument::new("LOGINTR", &log_buffers.logintr.0),
+                    )?;
+                    libos.init_at(
+                        2,
+                        LibosMemoryRegionInitArgument::new("LOGRM", &log_buffers.logrm.0),
+                    )?;
                     libos.init_at(3, LibosMemoryRegionInitArgument::new("RMARGS", rmargs))?;
 
                     libos.into()
                 },
-                logs <- {
-                    let log_buffers = LogBuffers {
-                        loginit,
-                        logintr,
-                        logrm,
-                    };
-
-                    // PANIC: The module data cannot be gone here. It is published before the
-                    // driver is registered and taken down after it is unregistered, so it is
-                    // there for as long as probe() can be called.
-                    let log_parent: &debugfs::Dir = crate::debugfs_data()
-                        .expect("module data not initialized")
-                        .root();
-
-                    log_parent.scope(log_buffers, dev.name(), |logs, dir| {
-                        dir.read_binary_file(c"loginit", &logs.loginit.0);
-                        dir.read_binary_file(c"logintr", &logs.logintr.0);
-                        dir.read_binary_file(c"logrm", &logs.logrm.0);
-                    })
-                },
+                logs <- log_buffers.scope(),
             }))
         })
     }
drivers/gpu/nova-core/gsp/logbuffer.rs new file +253 -0

253 lines not on this page · raw message

drivers/gpu/nova-core/nova_core.rs +28 -1
index 0f8501c26e05..1ea61a2ebf4f 100644
@@ -7,6 +7,7 @@
     driver::Registration,
     pci,
     prelude::*,
+    sync::Mutex,
     InPlaceModule, //
 };
 
@@ -44,6 +45,11 @@
 /// driver registration.
 #[pin_data(PinnedDrop)]
 pub(crate) struct DebugfsData {
+    /// Copies of the log buffers of GPUs that are gone.
+    ///
+    /// Declared before `root`, as the copies live below it.
+    #[pin]
+    retained_logs: Mutex<gsp::RetainedLogs>,
     /// Root directory of the driver in debugfs.
     root: debugfs::Dir,
 }
@@ -51,8 +57,18 @@ pub(crate) struct DebugfsData {
 impl DebugfsData {
     /// Creates the shared data and publishes it, so that [`debugfs_data()`] can hand it out.
     fn new() -> impl PinInit<Self> {
+        let root = debugfs::Dir::new(c"nova-core");
+
+        // Deciding here, rather than when the first GPU goes away, keeps the teardown path of a
+        // device from having to create anything.
+        let mut retained_logs = gsp::RetainedLogs::new();
+        if module_parameters::gsp_keep_logs.value() {
+            retained_logs.enable(&root);
+        }
+
         pin_init!(&this in Self {
-            root: debugfs::Dir::new(c"nova-core"),
+            retained_logs <- kernel::new_mutex!(retained_logs),
+            root,
             _: {
                 // SAFETY: Module initialization runs once and before the driver is registered, so
                 // nothing can be reading `DEBUGFS_DATA` while it is written here. `this` is where
@@ -66,6 +82,11 @@ fn new() -> impl PinInit<Self> {
     pub(crate) fn root(&self) -> &debugfs::Dir {
         &self.root
     }
+
+    /// Returns the copies of the log buffers of GPUs that are gone.
+    pub(crate) fn retained_logs(&self) -> &Mutex<gsp::RetainedLogs> {
+        &self.retained_logs
+    }
 }
 
 #[pinned_drop]
@@ -119,6 +140,12 @@ fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, Error> {
     description: "Nova Core GPU driver",
     license: "GPL v2",
     firmware: [],
+    params: {
+        gsp_keep_logs: bool {
+            default: false,
+            description: "Keep the GSP-RM log buffers in debugfs after their GPU is gone",
+        },
+    },
 }
 
 kernel::module_firmware!(firmware::ModInfoBuilder);
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH v4 2/3] gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `dma_mb(Read)` memory barrier does not protect against data races because the snapshot…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The dma_mb(Read) memory barrier does not protect against data races because the snapshot relies on the put pointer re-read by the unordered copy_to_slice(). --

commit cb68e0aae9b03cb04a10d2043892acfc8a1fe581
Author: Vladislav Zaharov <[email protected]>

gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind

Adds a module parameter to retain GSP-RM log buffers after unbind, making them available through debugfs. This copies the contents into module-owned memory since the original buffers are DMA allocations that do not outlive the device.

Quoted 5 lines
drivers/gpu/nova-core/gsp/logbuffer.rs new file +0 -0
new file mode 100644
index 0000000000000..890aa2f9e38e7

[ ... ]

Quoted 6 lines
drivers/gpu/nova-core/gsp/logbuffer.rs new file +6 -0
+    fn snapshot(&self) -> Result<VVec<u8>> {
+        // Offset 0 holds the "put" pointer, which the GSP advances as it appends entries. It is
+        // still zero if nothing was ever logged, which is all that is tested here: a buffer that
+        // was written to is copied whole, and making sense of "put" is left to the decoder.
+        let put = io_project!(self.0, [build: ..size_of::<u64>()]).try_cast::<u64>()?;
+        if put.read_val() == 0 {

drivers/gpu/nova-core/gsp/logbuffer.rs line 83

Quoted 10 lines
drivers/gpu/nova-core/gsp/logbuffer.rs new file +10 -0
+            return Ok(VVec::new());
+        }
+
+        // ORDERING: LOAD->LOAD ordering needed to order the "put" read before the data read. The
+        // GSP has normally been stopped by the time this runs, but a boot that timed out can leave
+        // it still appending.
+        dma_mb(Read);
+
+        let mut snapshot = VVec::zeroed(LOG_BUFFER_SIZE, GFP_KERNEL)?;
+        io_project!(self.0, [build: ..]).copy_to_slice(&mut snapshot);

[Severity: Medium]
Does the unordered copy_to_slice() at the end of this function negate the effect of dma_mb(Read)?

If a boot times out, the GSP may still be appending logs when LogBuffer::snapshot() is called. The safely ordered "put" value is dropped here, and the snapshot relies on the "put" pointer being re-read by the unordered copy_to_slice() operation.

This means copy_to_slice() can read the log payload before reading the "put" pointer at offset 0. If the GSP concurrently writes new logs and updates the "put" pointer, the snapshot could capture the updated "put" pointer but stale log payload data, causing the user-space decoder to parse garbage data.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

PATCH v4 3/3 Documentation: nova: remove completed GSP log buffer task

In reply to Vladislav Zaharov

Exposing the GSP-RM log buffers through debugfs is implemented, and with the gsp_keep_logs module parameter they now also survive a failed probe, which was the part of the task that was still missing.

Assisted-by Claude:claude-opus-5
---
 Documentation/gpu/nova/core/todo.rst | 12 ------------
 1 file changed, 12 deletions(-)
Documentation/gpu/nova/core/todo.rst +0 -12
index a01c362b1be0..28fc7329d56b 100644
@@ -141,18 +141,6 @@ Implement support for instmem (bar2) used to store page tables.
 GPU System Processor (GSP)
 ==========================
 
-Export GSP log buffers
-----------------------
-
-Recent patches from Timur Tabi [1] added support to expose GSP-RM log buffers
-(even after failure to probe the driver) through debugfs.
-
-This is also an interesting feature for nova-core, especially in the early days.
-
-| Link: https://lore.kernel.org/nouveau/[email protected]/ [1]
-| Reference: Debugfs abstractions
-| Complexity: Intermediate
-
 GSP firmware abstraction
 ------------------------
 
Signature · 1 line
2.55.0