gpu: nova-core: retain the GSP-RM log buffers
Subsystems (6)
v5 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 v4:
- no longer reset the pointer to the module data; debugfs_data() takes the bound device and returns a reference tied to it, rather than an Option<&'static> (Gary Guo)
- set the pointer from the module initializer, through the binding pin-init makes for the initialized _debugfs field (Gary Guo). v4 said pin-init gives no access to a pinned field there, which was wrong
- rebase onto current drm-rust-next
- reword comments that no longer matched the code, and use the current Assisted-by format
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 repeated 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. Each patch builds on its own; 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.
v4: https://lore.kernel.org/nova-gpu/[email protected]/
v3: 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 | 247 +++++++++++++++++++++++++ drivers/gpu/nova-core/nova_core.rs | 100 ++++++++-- 4 files changed, 347 insertions(+), 112 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs
base-commit: e1a9f41b896f0d57eb1165dd8a4c7446be6af638
Signature · 1 line
2.55.0
PATCH v5 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 a single write when the data is built, with no guard type. A registration that fails now drops the data that was built before it, and the directory goes with it.
--- drivers/gpu/nova-core/gsp.rs | 10 +--- drivers/gpu/nova-core/nova_core.rs | 73 ++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 28 deletions(-)
drivers/gpu/nova-core/gsp.rs +1 -9
@@ -199,15 +199,7 @@ 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 ::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 +54 -19
100 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v5 2/3 gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind
In reply to Vladislav ZaharovThe 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.
--- drivers/gpu/nova-core/gsp.rs | 92 ++------- drivers/gpu/nova-core/gsp/logbuffer.rs | 247 +++++++++++++++++++++++++ drivers/gpu/nova-core/nova_core.rs | 29 ++- 3 files changed, 294 insertions(+), 74 deletions(-) create mode 100644 drivers/gpu/nova-core/gsp/logbuffer.rs
drivers/gpu/nova-core/gsp.rs +19 -73
@@ -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> {
@@ -168,9 +121,7 @@ pub(crate) fn new( 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)
@@ -185,28 +136,23 @@ pub(crate) fn new( 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, - }; - -let log_parent: &debugfs::Dir =crate ::debugfs_data (dev).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 +247 -0
247 lines not on this page · raw message
drivers/gpu/nova-core/nova_core.rs +28 -1
@@ -8,6 +8,7 @@ driver::Registration, pci, prelude::*, + sync::Mutex, InPlaceModule, // };
@@ -46,6 +47,11 @@/// Reached from a device through [`debugfs_data()`]. #[pin_data] 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 , }
@@ -53,8 +59,18 @@ pub(crate) struct DebugfsData {impl DebugfsData {/// Creates the shared data. 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! (Self { - root: debugfs::Dir ::new (c"nova-core" ), + retained_logs <- kernel::new_mutex!(retained_logs), + root, }) }
@@ -62,6 +78,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 + } }/// Returns the data the module shares with its devices.
@@ -110,6 +131,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
[email protected]
AI reviewRe: [PATCH v5 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] gpu: nova-core: unordered put pointer in log buffer snapshot --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] gpu: nova-core: unordered put pointer in log buffer snapshot
--
commit e3a18fe9cea338c661ee3d185d004b01a5ff3d7d
Author: Vladislav Zaharov <[email protected]>
gpu: nova-core: gsp: retain the GSP-RM log buffers after unbind
This commit adds a gsp_keep_logs module parameter to retain GSP-RM log buffers after GPU unbind or probe failure. It creates copies of the buffers in memory owned by the module and exposes them through debugfs, moving the buffer logic to a separate logbuffer.rs file.
Quoted 5 lines
drivers/gpu/nova-core/gsp/logbuffer.rs new file +0
-0
[ ... ]
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]
Since copy_to_slice() reads the entire buffer, including the put pointer at offset 0, and has no internal read ordering guarantees, can the read of the put pointer inside copy_to_slice() be reordered after the data reads?
If the GSP is still appending data concurrently (e.g. after a boot timeout), the snapshot might capture a newer put pointer but stale log data. This could cause the log parser to read garbage or fail, defeating the purpose of the explicit dma_mb(Read) barrier.
Quoted 4 lines
drivers/gpu/nova-core/gsp/logbuffer.rs new file +4
-0+ +Ok (snapshot) + } +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v5 3/3 Documentation: nova: remove completed GSP log buffer task
In reply to Vladislav ZaharovExposing 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.
--- Documentation/gpu/nova/core/todo.rst | 12 ------------ 1 file changed, 12 deletions(-)
Documentation/gpu/nova/core/todo.rst +0 -12
@@ -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