NVIDIA GPU driver infrastructure
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>, "Alok Kumar" <alkumar@nvidia.com>,
	nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH 12/15] gpu: nova-core: switch to the r000 GSP firmware
Date: Thu, 03 Dec 2026 13:23:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-247851@kernel.org> (raw)
In-Reply-To: <20261203-b4-r000-switch-v1-12-b1463ec6dae2@nvidia.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] nova-core: missing element_len check in wait_for_msg() causes
  panic
- [Medium] gpu: nova-core: 1us BAR0 MMIO busy-wait polling storm in cmdq
- [Medium] nova-core: wait_for_msg() does not bound element_len()
  against readable area
- [Medium] gpu: nova-core: unfulfilled expect(dead_code) in
  nvkv/encode.rs
- [Low] gpu: nova-core: dma_mb(Full) used instead of mb(Full) for MMIO
  ordering
--

commit b84d5985668cf177648c64b977c82247b207c333
Author: John Hubbard <jhubbard@nvidia.com>

gpu: nova-core: switch to the r000 GSP firmware

This commit switches nova-core's GSP bindings, message queue transport,
boot sequence, and shutdown path from the r570 firmware interface to r000,
moving queue pointers to BAR0 registers and replacing the r570 boot and
shutdown RPC commands with GMC commands.

> diff --git a/drivers/gpu/nova-core/gsp/boot.rs b/drivers/gpu/nova-core/gsp/boot.rs
> index fb474e5984a80..763dbe1f8fdb4 100644
> --- a/drivers/gpu/nova-core/gsp/boot.rs
> +++ b/drivers/gpu/nova-core/gsp/boot.rs

[ ... ]

> @@ -82,14 +85,19 @@ pub(crate) fn boot(
>  
>          dev_dbg!(pdev, "RISC-V active? {}\n", gsp_falcon.is_riscv_active(),);
>  
> -        hal.post_boot(&self, ctx, &gsp_fw)?;
> -
> -        // Wait until GSP is fully initialized.
> -        commands::rpc::wait_gsp_init_done(&self.cmdq)?;
> -
> +        // GSP-RM answers `GSP_INIT` once it has finished starting, which includes the
> +        // load-and-execute events that it raises along the way.
> +        let libos_dma_address = self.libos.dma_address();
> +        let bootloader_app_version = gsp_fw.bootloader.app_version;
>          let static_info = self
>              .cmdq
> -            .send_rpc_command(commands::rpc::GetGspStaticInfo)?;
> +            .send_gmc_command(GspInit::new(pdev, chipset, ctx.vgpu.state())?, |event| {
> +                ctx.handle_load_exec_event(libos_dma_address, bootloader_app_version, event)
> +            })?

[Severity: Medium]
Does calling GspInit::new() here make all non-test items in
drivers/gpu/nova-core/gsp/nvkv/encode.rs live, leaving its module-level
#![cfg_attr(not(CONFIG_KUNIT = "y"), expect(dead_code))] attribute
unfulfilled?

When CONFIG_KUNIT is n or m (including allmodconfig), rustc emits an
unfulfilled_lint_expectations warning that fails the build under
CONFIG_WERROR=y.  Removing that attribute from nvkv/encode.rs resolves it.

> diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gsp/cmdq.rs
> index 10f6ddcf3eb52..f6bede6a77e7a 100644
> --- a/drivers/gpu/nova-core/gsp/cmdq.rs
> +++ b/drivers/gpu/nova-core/gsp/cmdq.rs

[ ... ]

> @@ -328,76 +332,73 @@ fn allocate_command(&mut self, size: usize, timeout: Delta) -> Result<GspCommand
>          })
>      }
>  
> -    // Returns the index of the memory page the GSP will write the next message to.
> -    //
> -    // # Invariants
> -    //
> -    // - The returned value is within `0..MSGQ_NUM_PAGES`.
> +    /// Returns the GSP's count of posted messages.
>      fn gsp_write_ptr(&self) -> u32 {
> -        let ptr = MsgqTxHeader::write_ptr(io_project!(self.mem, .gspq.tx)) % MSGQ_NUM_PAGES;
> +        // ORDERING: `readl` ends in a read barrier, which orders this read before the data reads
> +        // that follow it.
> +        let ptr = u32::from(self.bar.read(regs::NV_PGSP_MSGQ_HEAD::at(0)).val());
>  
> -        // ORDERING: LOAD->LOAD ordering needed to order `gsp_write_ptr` read before data read.
> -        dma_mb(Read);
> +        // Ignore out-of-range reads (e.g. a register zeroed while GSP-RM is suspended).
> +        if ptr.wrapping_sub(self.cpu_read_ptr) > MSGQ_NUM_PAGES {
> +            return self.last_gsp_write_ptr.get();
> +        }
>  
> +        self.last_gsp_write_ptr.set(ptr);
>          ptr
>      }

[Severity: Medium]
Does moving gsp_write_ptr() and gsp_read_ptr() from coherent RAM to BAR0
MMIO reads turn the 1-microsecond read_poll_timeout() loops in
wait_for_msg() and allocate_command() into a non-sleeping MMIO polling
storm?

fsleep() uses udelay() for intervals up to 10 microseconds, so
Delta::from_micros(1) busy-spins without yielding while issuing non-posted
BAR0 readl() transactions for up to 15 seconds during GspInit boot and 5
seconds on other waits.  On PREEMPT_NONE or PREEMPT_VOLUNTARY kernels this
pegs the calling CPU at 100% while flooding the GPU BAR0 interface.

In addition, allocate_command() calls driver_write_area() immediately
after read_poll_timeout() succeeds on driver_write_area_size(), issuing a
redundant BAR0 read and barrier on every command send.

> +    /// Returns the GSP's count of consumed commands.
>      fn gsp_read_ptr(&self) -> u32 {
> -        let ptr = MsgqRxHeader::read_ptr(io_project!(self.mem, .gspq.rx)) % MSGQ_NUM_PAGES;
> +        let ptr = u32::from(self.bar.read(regs::NV_PGSP_QUEUE_TAIL::at(0)).address());
>  
> -        // ORDERING: LOAD->STORE ordering needed to order `gsp_read_ptr` read before data write.
> +        // ORDERING: the data writes that follow must not pass this read. `readl` orders only the
> +        // reads that follow it, so the writes need a full barrier.
>          dma_mb(Full);

[ ... ]

> +    /// Releases `elem_count` more elements of the message queue to the GSP.
>      fn advance_cpu_read_ptr(&mut self, elem_count: u32) {
> -        // ORDERING: LOAD->STORE ordering needed to order `cpu_read_ptr` write after data read.
> -        dma_mb(Full);
> +        self.cpu_read_ptr = self.cpu_read_ptr.wrapping_add(elem_count);
>  
> -        let rx = io_project!(self.mem, .cpuq.rx);
> -        let rptr = MsgqRxHeader::read_ptr(rx).wrapping_add(elem_count) % MSGQ_NUM_PAGES;
> -        MsgqRxHeader::set_read_ptr(rx, rptr)
> -    }
> +        // ORDERING: the data reads must complete before this write releases the slots to the GSP.
> +        // `writel` orders only the writes before it, so the reads need a full barrier.
> +        dma_mb(Full);
>  
> -    // Returns the index of the memory page the CPU can write the next command to.
> -    //
> -    // # Invariants
> -    //
> -    // - The returned value is within `0..MSGQ_NUM_PAGES`.
> -    fn cpu_write_ptr(&self) -> u32 {
> -        MsgqTxHeader::write_ptr(io_project!(self.mem, .cpuq.tx)) % MSGQ_NUM_PAGES
> +        self.bar.write(
> +            Array::at(0),
> +            regs::NV_PGSP_MSGQ_TAIL::zeroed().with_val(self.cpu_read_ptr),
> +        );
>      }

[Severity: Low]
Should gsp_read_ptr() and advance_cpu_read_ptr() use mb(Full) instead of
dma_mb(Full) now that the pointers live in BAR0 MMIO registers?

Per Documentation/memory-barriers.txt, dma_*() barriers do not guarantee
ordering with MMIO accesses.  While dma_mb() currently falls back to mb()
or an equivalent barrier on supported architectures, using mb(Full)
matches the kernel barrier contract.

[ ... ]

> @@ -643,23 +618,23 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
>          }
>          .map(|(slice_1, slice_2)| (slice_1.as_flattened(), slice_2.as_flattened()))?;
>  
> -        // Extract the `GspMsgElement`.
> -        let (header, slice_1) = GspMsgElement::from_bytes_prefix(slice_1).ok_or(EIO)?;
> -
> -        // Validate checksum after truncating the message to its exact length.
> -        if Cmdq::calculate_checksum(
> -            SBufferIter::new_reader([header.as_bytes(), slice_1, slice_2])
> -                .take(header.msg_length()),
> -        ) != 0
> -        {
> -            dev_err!(&self.dev, "GSP receive: bad checksum\n");
> -            return Err(EIO);
> -        }
> +        // Extract the `QueueElementHeader`.
> +        let (header, slice_1) = QueueElementHeader::ref_from_prefix(slice_1).map_err(|_| EIO)?;
>  
> -        Ok(GspMessage {
> -            header,
> -            contents: (slice_1, slice_2),
> -        })
> +        header.validate().inspect_err(|_| {
> +            dev_err!(&self.dev, "GSP receive: bad queue element header\n");
> +        })?;
> +
> +        // Cut the message slices down to the length that the queue element header declares.
> +        let contents =
> +            trim_message_slice((slice_1, slice_2), header.message_len()).inspect_err(|_| {
> +                dev_err!(
> +                    &self.dev,
> +                    "GSP receive: read area shorter than the message\n"
> +                );
> +            })?;
> +
> +        Ok(GspMessage { header, contents })
>      }

[Severity: High]
Can a GSP queue element whose mctp_payload_size spans more slots than are
currently readable panic the kernel in driver_read_area()?

QueueElementHeader::validate() allows element_len() to exceed
size_of::<QueueElementHeader>() + message_len() up to 16 pages, and
wait_for_msg() only bounds message_len() via trim_message_slice().  If 1
slot is readable (NV_PGSP_MSGQ_HEAD == 1) and a message has
nvdm_payload_size == 64 with mctp_payload_size == 8192 (element_count() ==
2), consume_element() advances cpu_read_ptr to 2 past both
NV_PGSP_MSGQ_HEAD and last_gsp_write_ptr.

On the next driver_read_area() call, gsp_write_ptr() sees the wrapped
difference exceed MSGQ_NUM_PAGES and falls back to last_gsp_write_ptr (1),
so gspq_readable_slots() underflows to u32::MAX and
io_project!(self.mem, .gspq.msgq.data[panic: ..before_len]) panics out of
bounds.  Verifying that header.element_len() fits in the readable area in
wait_for_msg() prevents this.

[ ... ]

> @@ -683,14 +659,25 @@ fn consume_element<R>(
>          f: impl FnOnce(GspParsedMessage<'_>) -> R,
>      ) -> Result<R> {
>          let message = self.wait_for_msg(timeout)?;
> -        let elem_count = u32::try_from(message.header.msg_length().div_ceil(GSP_PAGE_SIZE))
> -            .map_err(|_| {
> -                dev_err!(&self.dev, "GSP receive: message length overflow\n");
> -                EIO
> -            })?;
> +        let elem_count = message.header.element_count();

[Severity: Medium]
Can advancing cpu_read_ptr by element_count() here without first checking
element_len() against the readable slots underflow gspq_readable_slots()
and panic in driver_read_area() instead of returning EIO?

Because wait_for_msg() only validates message_len() against the readable
slices and QueueElementHeader is borrowed in place from coherent DMA
memory via ref_from_prefix() across validate(), message_len(),
element_count(), and nvdm_type(), a misframed or concurrently modified
header can cause advance_cpu_read_ptr() to overtake last_gsp_write_ptr.
Copying the 24-byte QueueElementHeader by value, validating element_len()
against the readable area, and clamping gspq_readable_slots() to
MSGQ_NUM_PAGES eliminates this panic path.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-b4-r000-switch-v1-0-b1463ec6dae2@nvidia.com?part=12

  reply	other threads:[~2026-12-03 13:23 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 12:09 [PATCH 00/15] gpu: nova-core: switch to the r000 GSP firmware Alexandre Courbot
2026-12-03 12:09 ` [PATCH 01/15] gpu: nova-core: gsp: nvkv: add const constructor for Key Alexandre Courbot
2026-12-03 12:09 ` [PATCH 02/15] gpu: nova-core: gsp: add the GSP_INIT command and its reply Alexandre Courbot
2026-12-03 12:26   ` sashiko-bot
2026-12-03 12:09 ` [PATCH 03/15] gpu: nova-core: gsp: populate VF information in GSP_INIT Alexandre Courbot
2026-12-03 14:03   ` Maxime Ripard
2026-12-03 14:32     ` Miguel Ojeda
2026-12-03 14:38       ` Maxime Ripard
2026-12-03 12:09 ` [PATCH 04/15] gpu: nova-core: gsp: re-export PowerStateLevel from gsp::commands Alexandre Courbot
2026-12-03 12:09 ` [PATCH 05/15] gpu: nova-core: gsp: add the GSP_SUSPEND request Alexandre Courbot
2026-12-03 12:09 ` [PATCH 06/15] gpu: nova-core: firmware: add the ucodes firmware loader Alexandre Courbot
2026-12-03 12:09 ` [PATCH 07/15] gpu: nova-core: gsp: define GSP_MSG_QUEUE_ELEMENT_SIZE_MAX without bindings Alexandre Courbot
2026-12-03 12:09 ` [PATCH 08/15] gpu: nova-core: fb: increase Blackwell reserved memory sizes for r000 Alexandre Courbot
2026-12-03 12:09 ` [PATCH 09/15] gpu: nova-core: gsp: drain the message queue while waiting for GSP suspend Alexandre Courbot
2026-12-03 12:56   ` sashiko-bot
2026-12-03 12:09 ` [PATCH 10/15] gpu: nova-core: gsp: zero-initialize FMC boot parameter structs Alexandre Courbot
2026-12-03 12:09 ` [PATCH 11/15] gpu: nova-core: firmware: add r000 GSP bindings Alexandre Courbot
2026-12-03 12:09 ` [PATCH 12/15] gpu: nova-core: switch to the r000 GSP firmware Alexandre Courbot
2026-12-03 13:23   ` sashiko-bot [this message]
2026-12-03 12:09 ` [PATCH 13/15] gpu: nova-core: gsp: delete the GSP sequencer Alexandre Courbot
2026-12-03 12:09 ` [PATCH 14/15] gpu: nova-core: gsp: delete the r570 RPC commands Alexandre Courbot
2026-12-03 12:09 ` [PATCH 15/15] gpu: nova-core: firmware: delete the r570 bindings Alexandre Courbot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247851@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=alkumar@nvidia.com \
    --cc=apopple@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ecourtney@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=lossin@kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=simona@ffwll.ch \
    --cc=skhan@linuxfoundation.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=tzimmermann@suse.de \
    --cc=work@onurozkan.dev \
    --cc=zhiw@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox