Re: [PATCH v2 11/15] gpu: nova-core: bound a GSP wait by a single deadline
From: Alexandre Courbot
Date: Mon Aug 31 2026 - 02:05:18 EST
On Sat Aug 29, 2026 at 10:33 AM JST, John Hubbard wrote:
<...>
> @@ -600,17 +610,43 @@ pub(crate) fn send_command_no_wait<M>(&self, bar: Bar0<'_>, command: M) -> Resul
> self.inner.lock().send_command(bar, command).map(|_| ())
> }
>
> - /// Receive a message from the GSP.
> + /// Receive a message from the GSP, matching on the function code alone.
> ///
> - /// Matches on the function code alone, for a caller awaiting an unsolicited GSP event rather
> - /// than a reply to a command. See [`CmdqInner::receive_msg`].
> - pub(crate) fn receive_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
> + /// Returns `ERANGE` if the message that arrives is not of type `M`. See
> + /// [`CmdqInner::receive_msg`].
> + fn receive_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
> where
> // This allows all error types, including `Infallible`, to be used for `M::InitError`.
> Error: From<M::InitError>,
> {
> self.inner.lock().receive_msg(timeout, None)
> }
> +
> + /// Waits for an unsolicited GSP event of type `M`, dispatching any other event that arrives
> + /// first.
> + ///
> + /// # Errors
> + ///
> + /// - `ETIMEDOUT` if the event does not arrive within [`Self::RECEIVE_TIMEOUT`] of the call,
> + /// however many other events are dispatched while waiting.
We also return the errors from `receive_msg`, this should be documented.
> + pub(crate) fn await_msg<M: MessageFromGsp>(&self) -> Result<M>
> + where
> + // This allows all error types, including `Infallible`, to be used for `M::InitError`.
> + Error: From<M::InitError>,
> + {
> + let deadline = Instant::<Monotonic>::now() + Self::RECEIVE_TIMEOUT;
> + loop {
> + let remaining = deadline - Instant::<Monotonic>::now();
> + if remaining.is_negative() {
> + break Err(ETIMEDOUT);
> + }
> + match self.receive_msg::<M>(remaining) {
> + Ok(msg) => break Ok(msg),
> + Err(ERANGE) => continue,
> + Err(e) => break Err(e),
> + }
> + }
> + }
Note that this will release the lock between calls to `receive_msg`. If
a `send_command` comes in from another thread at the wrong time, that
one *does* keep the lock until it has received the reply it expects - so
our reply will be consumed silently and we will timeout.
I guess you can just remove `Cmdq::receive_msg` here: acquire the lock
at the beginning of `await_msg`, and call `inner.receive_msg` from it
directly. The GSP queue is synchronous by design anyway, so that's what
we want even if it looks strange to hold a lock for potentially 5
seconds.