Skip to contentSkip to conversation

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

PATCH Applied Vladislav Zaharov 3 participants 7 messages Nov 14 – Nov 15

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

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

The GSP-RM log buffers are exposed through debugfs, but they go away when the GPU is unbound, including when probe() fails, which is when they are most useful.

Patch 1 moves the debugfs root into the module data, which also fixes the root being leaked when the driver registration fails. Patch 2 moves the log buffer code into its own file. Patch 3 adds a gsp_keep_logs module parameter that keeps a copy of the buffers after unbind. Patch 4 drops the completed item from todo.rst.

Changes since v7:
- patch 3: check only gsp_keep_logs, not CONFIG_DEBUG_FS, drop the reserve() before replacing the copies, add RetainedLogs::push() and mark retained_logs() #[inline] (Danilo)
- collect Alexandre's Reviewed-by for patches 1 and 2

The code and the commit messages were drafted with an LLM coding assistant and reviewed by me. Each patch builds on its own, and the series was tested again on a GB203: the copies are kept after unbind and after a failed probe, replaced by the live buffers on the next bind, and nothing is kept without gsp_keep_logs.

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

Vladislav Zaharov (4):
gpu: nova-core: move the debugfs root into the module data
gpu: nova-core: gsp: move the log buffers into their own file
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/debugfs.rs       |  45 ++++++
 drivers/gpu/nova-core/gsp.rs           | 100 +++---------
 drivers/gpu/nova-core/gsp/logbuffer.rs | 205 +++++++++++++++++++++++++
 drivers/gpu/nova-core/nova_core.rs     |  68 +++++---
 5 files changed, 313 insertions(+), 117 deletions(-)
 create mode 100644 drivers/gpu/nova-core/debugfs.rs
 create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs

base-commit: 0ff9e5b9ff6736c37593cd7db89f4593ec4f8e86

Signature · 1 line
2.56.0
#1 lore raw

PATCH v8 1/4 gpu: nova-core: move the debugfs root into the module data

The debugfs root lives in a static that is cleared by a guard field of the module data. The guard is built after the driver registration, so if the registration fails, the root is never removed and the next module load finds the name taken.

Move the root into a DebugfsData owned by the module data, and keep only a pointer to it in the static, for devices that cannot reach the module data yet. If the registration fails, the root is dropped with the rest of the module data.

Fixes 09691f5d8070 ("gpu: nova-core: create debugfs root in module init")
---
 drivers/gpu/nova-core/debugfs.rs   | 29 ++++++++++++++
 drivers/gpu/nova-core/gsp.rs       | 11 +-----
 drivers/gpu/nova-core/nova_core.rs | 62 ++++++++++++++++++------------
 3 files changed, 69 insertions(+), 33 deletions(-)
 create mode 100644 drivers/gpu/nova-core/debugfs.rs
drivers/gpu/nova-core/debugfs.rs new file +29 -0
new file mode 100644
index 000000000000..6c6ed129f0da
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Debugfs data shared by all GPUs of the driver.
+
+use kernel::{
+    debugfs,
+    prelude::*, //
+};
+
+/// Debugfs data the module shares with every GPU it drives.
+#[pin_data]
+pub(crate) struct DebugfsData {
+    /// Root directory of the driver in debugfs.
+    root: debugfs::Dir,
+}
+
+impl DebugfsData {
+    /// Creates the debugfs data of the module.
+    pub(crate) fn new() -> impl PinInit<Self> {
+        pin_init!(Self {
+            root: debugfs::Dir::new(c"nova-core"),
+        })
+    }
+
+    /// Returns the root directory of the driver.
+    pub(crate) fn root(&self) -> &debugfs::Dir {
+        &self.root
+    }
+}
drivers/gpu/nova-core/gsp.rs +2 -9
index a4b832f6aec1..79ee74a0a16f 100644
@@ -200,15 +200,8 @@ pub(crate) fn new(
                         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");
+                    let log_parent: &debugfs::Dir =
+                        crate::NovaCoreModule::debugfs_data(dev).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 +38 -24

93 lines not on this page · raw message

Signature · 1 line
2.56.0
#2 lore raw

PATCH v8 2/4 gpu: nova-core: gsp: move the log buffers into their own file

In reply to Vladislav Zaharov

Move the GSP-RM log buffer code from gsp.rs into gsp/logbuffer.rs, ahead of extending it. No functional change.

---
 drivers/gpu/nova-core/gsp.rs           | 59 +++--------------------
 drivers/gpu/nova-core/gsp/logbuffer.rs | 65 ++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 52 deletions(-)
 create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs
drivers/gpu/nova-core/gsp.rs +7 -52
index 79ee74a0a16f..43df95406d35 100644
@@ -12,11 +12,7 @@
         CoherentView,
         DmaAddress, //
     },
-    io::{
-        io_project,
-        io_write,
-        Io, //
-    },
+    io::io_write,
     pci,
     prelude::*, //
 };
@@ -24,10 +20,16 @@
 pub(crate) mod cmdq;
 pub(crate) mod commands;
 mod fw;
+mod logbuffer;
 mod nvkv;
 mod regs;
 mod sequencer;
 
+use logbuffer::{
+    LogBuffer,
+    LogBuffers, //
+};
+
 pub(crate) use fw::{
     GspFmcBootParams,
     GspFwWprMeta,
@@ -78,10 +80,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)]
@@ -102,49 +100,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> {
drivers/gpu/nova-core/gsp/logbuffer.rs new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.56.0
#3 lore raw

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

In reply to Vladislav Zaharov

The GSP-RM log buffers are DMA allocations of the device, so their debugfs files go away when the GPU is unbound, including when probe() fails. That is when the logs are most needed.

Add a gsp_keep_logs module parameter. When it is set, the buffers are copied into module memory when they are dropped, and the copies are exposed in the same files until the GPU is bound again or the module is unloaded.

---
 drivers/gpu/nova-core/debugfs.rs       |  18 ++-
 drivers/gpu/nova-core/gsp.rs           |  42 +++----
 drivers/gpu/nova-core/gsp/logbuffer.rs | 150 ++++++++++++++++++++++++-
 drivers/gpu/nova-core/nova_core.rs     |   6 +
 4 files changed, 184 insertions(+), 32 deletions(-)
drivers/gpu/nova-core/debugfs.rs +17 -1
index 6c6ed129f0da..79e8fab945a3 100644
@@ -4,12 +4,21 @@
 
 use kernel::{
     debugfs,
-    prelude::*, //
+    new_mutex,
+    prelude::*,
+    sync::Mutex, //
 };
 
+use crate::gsp::RetainedLogs;
+
 /// Debugfs data the module shares with every GPU it drives.
 #[pin_data]
 pub(crate) struct DebugfsData {
+    /// Copies of the GSP-RM log buffers of GPUs that are no longer bound.
+    ///
+    /// Declared before `root`, as the copies live below it.
+    #[pin]
+    retained_logs: Mutex<RetainedLogs>,
     /// Root directory of the driver in debugfs.
     root: debugfs::Dir,
 }
@@ -18,6 +27,7 @@ impl DebugfsData {
     /// Creates the debugfs data of the module.
     pub(crate) fn new() -> impl PinInit<Self> {
         pin_init!(Self {
+            retained_logs <- new_mutex!(RetainedLogs::new()),
             root: debugfs::Dir::new(c"nova-core"),
         })
     }
@@ -26,4 +36,10 @@ pub(crate) fn new() -> impl PinInit<Self> {
     pub(crate) fn root(&self) -> &debugfs::Dir {
         &self.root
     }
+
+    /// Returns the copies of the GSP-RM log buffers of GPUs that are no longer bound.
+    #[inline]
+    pub(crate) fn retained_logs(&self) -> &Mutex<RetainedLogs> {
+        &self.retained_logs
+    }
 }
drivers/gpu/nova-core/gsp.rs +16 -26

64 lines not on this page · raw message

drivers/gpu/nova-core/gsp/logbuffer.rs +145 -5

183 lines not on this page · raw message

drivers/gpu/nova-core/nova_core.rs +6 -0
index f5d08de8a212..1671e260dfb4 100644
@@ -91,6 +91,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 unbound",
+        },
+    },
 }
 
 kernel::module_firmware!(firmware::ModInfoBuilder);
Signature · 1 line
2.56.0
#4 lore raw

PATCH v8 4/4 Documentation: nova: remove completed GSP log buffer task

In reply to Vladislav Zaharov

The GSP-RM log buffers are exposed in debugfs, and with gsp_keep_logs they also survive a failed probe.

---
 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.56.0
Tested-by on the v8 series
#5 lore raw
In reply to Vladislav Zaharov
Quoted 10 lines · Vladislav Zaharov

The GSP-RM log buffers are exposed through debugfs, but they go away when the GPU is unbound, including when probe() fails, which is when they are most useful.

Patch 1 moves the debugfs root into the module data, which also fixes the root being leaked when the driver registration fails. Patch 2 moves the log buffer code into its own file. Patch 3 adds a gsp_keep_logs module parameter that keeps a copy of the buffers after unbind. Patch 4 drops the completed item from todo.rst.

Quoted 5 lines

Changes since v7:
- patch 3: check only gsp_keep_logs, not CONFIG_DEBUG_FS, drop the reserve() before replacing the copies, add RetainedLogs::push() and mark retained_logs() #[inline] (Danilo)
- collect Alexandre's Reviewed-by for patches 1 and 2

Hi Vlad,

This is working nicely for me, the logs stay in the same place and I can retrieve them even when I inject an error in the driver to fail the probe:

root@redforge:/sys/kernel/debug/nova-core# dmesg -t |grep nova-core|grep failed
nova-core 0000:c1:00.0: probe with driver nova-core failed with error -22
nova-core 0000:c2:00.0: probe with driver nova-core failed with error -22
nova-core 0000:01:00.0: probe with driver nova-core failed with error -22
root@redforge:/sys/kernel/debug/nova-core# tree
.
├── 0000:01:00.0
│   ├── loginit
│   ├── logintr
│   └── logrm
├── 0000:c1:00.0
│   ├── loginit
│   └── logrm
└── 0000:c2:00.0
    ├── loginit
    ├── logintr
    └── logrm

So for the series,

thanks,

Signature · 1 line
John Hubbard
Quoted 26 lines

The code and the commit messages were drafted with an LLM coding assistant and reviewed by me. Each patch builds on its own, and the series was tested again on a GB203: the copies are kept after unbind and after a failed probe, replaced by the live buffers on the next bind, and nothing is kept without gsp_keep_logs.

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

Vladislav Zaharov (4):
gpu: nova-core: move the debugfs root into the module data
gpu: nova-core: gsp: move the log buffers into their own file
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/debugfs.rs       |  45 ++++++
 drivers/gpu/nova-core/gsp.rs           | 100 +++---------
 drivers/gpu/nova-core/gsp/logbuffer.rs | 205 +++++++++++++++++++++++++
 drivers/gpu/nova-core/nova_core.rs     |  68 +++++---
 5 files changed, 313 insertions(+), 117 deletions(-)
 create mode 100644 drivers/gpu/nova-core/debugfs.rs
 create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs

base-commit: 0ff9e5b9ff6736c37593cd7db89f4593ec4f8e86

maintainer Applied
#6 lore raw
In reply to Vladislav Zaharov
Quoted 1 line · Vladislav Zaharov

[PATCH v8 0/4] gpu: nova-core: retain the GSP-RM log buffers

Applied, thanks!

Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git

[1/4] gpu: nova-core: move the debugfs root into the module data

      commit: b9a34b6ac2f7
[2/4] gpu: nova-core: gsp: move the log buffers into their own file
      commit: 35d780c3c9c7

[3/4] gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind
commit: 27b10da20729

[ s/scope()/into_debugfs_scope()/; drop unnecessary explicit Infallible
from into_debugfs_scope()'s return type. - Danilo ]

[4/4] Documentation: nova: remove completed GSP log buffer task

      commit: 8045d0e7d208

The patches will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patches are queued up for the upcoming merge window for the next major kernel release.