[PATCH 2/5] gpu: nova-core: gsp: match GSP RPC replies by sequence, not just function

From: Alexandre Courbot

Date: Thu Dec 03 2026 - 23:16:59 EST


From: John Hubbard <jhubbard@xxxxxxxxxx>

Since r000, GSP-RM copies a command's RPC sequence number into its
reply, alongside the function code. r570 was leaving that field at zero,
meaning nova-core matches a reply on the function code alone and never
sets the sequence in a command, so a reply to a command that has already
timed out can satisfy a later command with the same function code.

Write the sequence number into every RPC command, and require both the
function code and the sequence to match before accepting a reply. A
message with the expected function code and a stale sequence is a late
reply rather than an event, so it is logged and dropped.

Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>
Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
---
Documentation/gpu/nova/core/interrupts.rst | 5 +++-
drivers/gpu/nova-core/gsp/cmdq/rpc.rs | 46 ++++++++++++++++++++----------
drivers/gpu/nova-core/gsp/fw.rs | 8 +++++-
3 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/Documentation/gpu/nova/core/interrupts.rst b/Documentation/gpu/nova/core/interrupts.rst
index 27fa21f4b10f..a155c0532cfc 100644
--- a/Documentation/gpu/nova/core/interrupts.rst
+++ b/Documentation/gpu/nova/core/interrupts.rst
@@ -583,7 +583,10 @@ GMC replies match on command ID and sequence number, and GMC events arriving
during a wait go to the caller's handler; unclaimed replies and unknown command
IDs are logged at warning level, and other GMC events are dropped.

-The RPC sequence number takes no part in the match.
+A command's reply must carry the RPC sequence number that nova-core wrote into
+the command, as well as its function code to be considered a reply to that
+command. An unsolicited event answers no command, so a caller awaiting one
+matches on the function code alone.

The read pointer advances past every valid element, whether it matched or was
an event. An element that fails framing or message-header validation, or
diff --git a/drivers/gpu/nova-core/gsp/cmdq/rpc.rs b/drivers/gpu/nova-core/gsp/cmdq/rpc.rs
index ffe8b950c1d9..fb7397012804 100644
--- a/drivers/gpu/nova-core/gsp/cmdq/rpc.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq/rpc.rs
@@ -127,7 +127,7 @@ fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Res
let (rpc_header, contents) =
RpcMessageHeader::from_bytes_mut_prefix(dst.contents.0).ok_or(EIO)?;
let (cmd, payload_1) = M::Command::from_bytes_mut_prefix(contents).ok_or(EIO)?;
- let rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
+ let rpc_header_init = RpcMessageHeader::init(seq, size_in_bytes, M::FUNCTION);
// SAFETY: `rpc_header` is a valid reference, and is not touched if the initializer fails.
unsafe {
pin_init::raw_try_init(core::ptr::from_mut(rpc_header), rpc_header_init)?;
@@ -278,9 +278,9 @@ pub(crate) fn send_rpc_command<M>(&self, command: M) -> Result<M::Reply>
Error: From<<M::Reply as MessageFromGsp>::InitError>,
{
self.with_inner_locked(|inner| {
- inner.send_rpc_command(command)?;
+ let seq = inner.send_rpc_command(command)?;

- inner.await_rpc_msg()
+ inner.await_rpc_msg(Some(seq))
})
}

@@ -298,12 +298,14 @@ pub(crate) fn send_rpc_command_no_wait<M>(&self, command: M) -> Result
M: CommandToGsp<Reply = NoReply>,
Error: From<M::InitError>,
{
- self.with_inner_locked(|inner| inner.send_rpc_command(command))
+ self.with_inner_locked(|inner| inner.send_rpc_command(command).map(|_| ()))
}

/// Waits for an unsolicited GSP event of type `M`. Events that arrive before it are logged and
/// consumed.
///
+ /// Events answer no command and are matched on function code alone.
+ ///
/// The queue mutex is held for the whole wait, up to [`Self::RECEIVE_TIMEOUT`], so no other
/// caller can send a command or consume an event meanwhile.
///
@@ -319,14 +321,15 @@ pub(crate) fn await_rpc_msg<M: MessageFromGsp>(&self) -> Result<M>
// This allows all error types, including `Infallible`, to be used for `M::InitError`.
Error: From<M::InitError>,
{
- self.with_inner_locked(|inner| inner.await_rpc_msg())
+ self.with_inner_locked(|inner| inner.await_rpc_msg(None))
}
}

impl CmdqInner<'_> {
- /// Sends `command` to the GSP.
+ /// Sends `command` to the GSP, and returns the sequence number of its first element.
///
- /// The command may be split into multiple messages if it is large.
+ /// The command may be split into multiple messages if it is large. Each message carries its
+ /// own sequence number, and GSP-RM copies the one of the first message into its reply.
///
/// # Errors
///
@@ -335,29 +338,29 @@ impl CmdqInner<'_> {
/// written to by its [`CommandToGsp::init_variable_payload`] method.
///
/// Error codes returned by the command initializers are propagated as-is.
- fn send_rpc_command<M>(&mut self, command: M) -> Result
+ fn send_rpc_command<M>(&mut self, command: M) -> Result<u32>
where
M: CommandToGsp,
Error: From<M::InitError>,
{
match SplitState::new(command)? {
- SplitState::Single(command) => self
- .send_command_element(RpcCommandElement(command))
- .map(|_| ()),
+ SplitState::Single(command) => self.send_command_element(RpcCommandElement(command)),
SplitState::Split(command, mut continuations) => {
- self.send_command_element(RpcCommandElement(command))?;
+ let seq = self.send_command_element(RpcCommandElement(command))?;

while let Some(continuation) = continuations.next() {
self.send_command_element(RpcCommandElement(continuation))?;
}

- Ok(())
+ Ok(seq)
}
}
}

/// Receives a message of type `M`, waiting up to [`Cmdq::RECEIVE_TIMEOUT`] from the call.
///
+ /// If `expected_seq` is set, then the message must carry that sequence number too.
+ ///
/// Any other message that arrives first is logged as an event and does not extend the
/// deadline.
///
@@ -368,7 +371,7 @@ fn send_rpc_command<M>(&mut self, command: M) -> Result
/// - `EIO` if a message fails framing validation.
///
/// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
- fn await_rpc_msg<M: MessageFromGsp>(&mut self) -> Result<M>
+ fn await_rpc_msg<M: MessageFromGsp>(&mut self, expected_seq: Option<u32>) -> Result<M>
where
// This allows all error types, including `Infallible`, to be used for `M::InitError`.
Error: From<M::InitError>,
@@ -382,7 +385,10 @@ fn await_rpc_msg<M: MessageFromGsp>(&mut self) -> Result<M>
}
};
let function = rpc_message.header.function();
- if matches!(function, Ok(f) if f == M::FUNCTION) {
+ let seq = rpc_message.header.sequence();
+ let func_matches = matches!(function, Ok(f) if f == M::FUNCTION);
+ let seq_matches = expected_seq.is_none_or(|expected| seq == expected);
+ if func_matches && seq_matches {
let (cmd, contents_1) =
M::Message::from_bytes_prefix(rpc_message.contents.0).ok_or(EIO)?;
let mut sbuffer = SBufferIter::new_reader([contents_1, rpc_message.contents.1]);
@@ -399,6 +405,16 @@ fn await_rpc_msg<M: MessageFromGsp>(&mut self) -> Result<M>
}
})
.map(Some)
+ } else if func_matches {
+ dev_warn!(
+ self.dev,
+ "GSP RPC: dropping stale {:?} reply (seq {}, awaiting {:?})\n",
+ M::FUNCTION,
+ seq,
+ expected_seq,
+ );
+
+ Ok(None)
} else {
rpc_message.log(self.dev);

diff --git a/drivers/gpu/nova-core/gsp/fw.rs b/drivers/gpu/nova-core/gsp/fw.rs
index 4fb150dfba8b..a576fc86708c 100644
--- a/drivers/gpu/nova-core/gsp/fw.rs
+++ b/drivers/gpu/nova-core/gsp/fw.rs
@@ -478,13 +478,19 @@ pub(crate) struct RpcMessageHeader {
impl RpcMessageHeader {
/// Creates a new RPC header.
///
+ /// `sequence` is the sequence number of the message, which GSP-RM copies into its reply.
/// `cmd_size` is the size in bytes of the payload. `function` is the RPC function of the
/// message.
- pub(crate) fn init(cmd_size: usize, function: MsgFunction) -> impl Init<Self, Error> {
+ pub(crate) fn init(
+ sequence: u32,
+ cmd_size: usize,
+ function: MsgFunction,
+ ) -> impl Init<Self, Error> {
let init_inner = try_init!(bindings::rpc_message_header_v {
header_version: MsgHeaderVersion::new().into(),
signature: bindings::NV_VGPU_MSG_SIGNATURE_VALID,
function: function.into(),
+ sequence,
length: size_of::<Self>()
.checked_add(cmd_size)
.ok_or(EOVERFLOW)

--
2.56.1