Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] gpu: nova-core: unaligned stack slice in
  PmuLogBuffer::capture_event()
- [Medium] gpu: nova-core: RETAINED_LOGS KVec memory leak in
  DebugfsRootGuard::drop()
--

commit de98d34f69a2abc89f1f11708b2ef5d8075d83df
Author: Agustín Pereira <[email protected]>

gpu: nova-core: gsp: retain debugfs logs on teardown and capture logpmu

This commit moves GSP-RM logging into a dedicated gsp::logs module, captures
PMU UCODE_LIBOS_PRINT events into a debugfs logpmu buffer, and adds a
keep_gsp_logging module parameter to snapshot and retain non-empty log
buffers in debugfs across probe failures or device unbind.

> diff --git a/drivers/gpu/nova-core/gsp/logs.rs 
> b/drivers/gpu/nova-core/gsp/logs.rs
> new file mode 100644
> index 0000000000000..9044315f18db9
> --- /dev/null
> +++ b/drivers/gpu/nova-core/gsp/logs.rs
> @@ -0,0 +1,450 @@

[ ... ]

> +    pub(crate) fn capture_event(&self, contents: (&[u8], &[u8])) -> 
> Result<usize> {
> +        let total_len = contents.0.len().saturating_add(contents.1.len());
> +        let header_len = size_of::<UcodeLibOsPrintHeader>();
> +        if total_len < header_len {
> +            return Err(EINVAL);
> +        }
> +
> +        let mut sbuf = SBufferIter::new_reader([contents.0, contents.1]);
> +        let mut hdr_bytes = [0u8; size_of::<UcodeLibOsPrintHeader>()];
> +        sbuf.read_exact(&mut hdr_bytes)?;
> +
> +        let hdr = UcodeLibOsPrintHeader::from_bytes(&hdr_bytes).ok_or(EIO)?;

[Severity: Medium]
Can UcodeLibOsPrintHeader::from_bytes(&hdr_bytes) fail with Err(EIO) on
valid events because hdr_bytes is only 1-byte aligned?

UcodeLibOsPrintHeader wraps rpc_ucode_libos_print_v1e_08, which contains
u32 fields and requires 4-byte alignment, while FromBytes::from_bytes()
returns None when slice_ptr.is_aligned() is false.  Whenever the compiler
places hdr_bytes on an address not divisible by 4, capture_event() rejects
valid PMU print events and fails the KUnit tests.  Using
UcodeLibOsPrintHeader::from_bytes_copy(&hdr_bytes) avoids the alignment
requirement.

> diff --git a/drivers/gpu/nova-core/nova_core.rs 
> b/drivers/gpu/nova-core/nova_core.rs
> index a8f6dc22057be..028e368f1b831 100644
> --- a/drivers/gpu/nova-core/nova_core.rs
> +++ b/drivers/gpu/nova-core/nova_core.rs
> @@ -35,11 +35,52 @@
>  // TODO: Move this into per-module data once that exists.
>  static mut DEBUGFS_ROOT: Option<debugfs::Dir> = None;
>  
> -/// Guard that clears `DEBUGFS_ROOT` when dropped.
> +kernel::sync::global_lock! {
> +    // SAFETY: Initialized once in `NovaCoreModule::init` before driver 
> registration
> +    // and cleared in `DebugfsRootGuard::drop` after driver unregistration.
> +    unsafe(uninit) static RETAINED_LOGS: 
> Mutex<KVec<gsp::logs::RetainedLogEntry>> = KVec::new();
> +}

[ ... ]

> +/// Guard that clears retained logs and `DEBUGFS_ROOT` when dropped.
>  struct DebugfsRootGuard;
>  
>  impl Drop for DebugfsRootGuard {
>      fn drop(&mut self) {
> +        // Drop retained `debugfs` scopes before removing `DEBUGFS_ROOT`.
> +        RETAINED_LOGS.lock().clear();
> +
>          // 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 };

[Severity: Medium]
Does RETAINED_LOGS.lock().clear() leak the KVec backing buffer on module
unload?

KVec::clear() truncates the vector to length 0 and drops its elements in
place, but keeps the Kmalloc capacity allocation.  Because RETAINED_LOGS is
a static item, its Drop implementation never runs when the module is
unloaded.  In addition, because NovaCoreModule drops _driver before
_debugfs_guard, unloading the module with keep_gsp_logging=true unbinds all
active devices and snapshots their logs into RETAINED_LOGS right before
DebugfsRootGuard::drop() clears them and leaks the vector buffer.
Replacing the vector with *RETAINED_LOGS.lock() = KVec::new() will drop the
old KVec and free its backing memory.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to