mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexandre Courbot <acourbot@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	 David Airlie <airlied@gmail.com>,
	Simona Vetter <simona@ffwll.ch>,
	 Benno Lossin <lossin@kernel.org>, Gary Guo <gary@garyguo.net>
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>,
	 linux-kernel@vger.kernel.org, nova-gpu@lists.linux.dev,
	 rust-for-linux@vger.kernel.org,
	Alexandre Courbot <acourbot@nvidia.com>
Subject: [PATCH v2 4/6] gpu: nova-core: gsp: cmdq: move message trimming into its own function
Date: Fri, 06 Nov 2026 16:06:12 +0900	[thread overview]
Message-ID: <20261106-r000_rebase_on_cmdq-rpc-v2-4-e481ea2168ef@nvidia.com> (raw)
In-Reply-To: <20261106-r000_rebase_on_cmdq-rpc-v2-0-e481ea2168ef@nvidia.com>

Trimming the two driver-owned slices of the command queue to the
advertised length of a message is going to be used by both RPC and GMC
messages, so factor that part out into a private function.

The parsing itself can be improved by leveraging `split_at_checked` and
`get`, so use them.

No functional change intended.

Suggested-by: Eliot Courtney <ecourtney@nvidia.com>
Link: https://lore.kernel.org/all/DMN62G2CDG3N.ETT4GRFXWKKU@nvidia.com/
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 drivers/gpu/nova-core/gsp/cmdq.rs     | 21 +++++++++++++++++++++
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs | 20 +-------------------
 2 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gsp/cmdq.rs
index 9dd11ac8952b..f3151aa8ea26 100644
--- a/drivers/gpu/nova-core/gsp/cmdq.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq.rs
@@ -719,3 +719,24 @@ fn drain(&mut self) -> Result {
         }
     }
 }
+
+/// Helper function that trims the concatenation of `slice_1` and `slice_2` to `length`.
+///
+/// Returns the truncated `(slice_1, slice_2)` pair, or `EIO` if `length > slice_1.len() +
+/// slice_2.len()`.
+fn trim_message_slice<'a>(
+    (slice_1, slice_2): (&'a [u8], &'a [u8]),
+    length: usize,
+) -> Result<(&'a [u8], &'a [u8])> {
+    // See if `slice_1` covers the whole message, take it entirely if it doesn't.
+    let slice_1 = slice_1
+        .split_at_checked(length)
+        .map(|split| split.0)
+        .unwrap_or(slice_1);
+    // `slice_2` is the remainder of the message not covered by `slice_1`.
+    let slice_2 = slice_2
+        .get(..length.saturating_sub(slice_1.len()))
+        .ok_or(EIO)?;
+
+    Ok((slice_1, slice_2))
+}
diff --git a/drivers/gpu/nova-core/gsp/cmdq/rpc.rs b/drivers/gpu/nova-core/gsp/cmdq/rpc.rs
index 36cba7c28d3e..6ee28e93586b 100644
--- a/drivers/gpu/nova-core/gsp/cmdq/rpc.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq/rpc.rs
@@ -214,27 +214,9 @@ pub(super) fn parse(dev: &device::Device, element: GspMessage<'a>) -> Result<Rpc
             payload_length,
         );
 
-        // Check that the driver read area is large enough for the message.
-        if slice_1.len() + slice_2.len() < payload_length {
-            return Err(EIO);
-        }
-
-        // Cut the message slices down to the actual length of the message.
-        let (slice_1, slice_2) = if slice_1.len() > payload_length {
-            // PANIC: we checked above that `slice_1` is at least as long as `payload_length`.
-            (slice_1.split_at(payload_length).0, &slice_2[0..0])
-        } else {
-            (
-                slice_1,
-                // PANIC: we checked above that `slice_1.len() + slice_2.len()` is at least as
-                // large as `payload_length`.
-                slice_2.split_at(payload_length - slice_1.len()).0,
-            )
-        };
-
         Ok(RpcMessage {
             header: rpc_header,
-            contents: (slice_1, slice_2),
+            contents: super::trim_message_slice((slice_1, slice_2), payload_length)?,
         })
     }
 

-- 
2.56.0


  parent reply	other threads:[~2026-11-06  7:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06  7:06 [PATCH v2 0/6] gpu: nova-core: gsp: add support for GMC messages Alexandre Courbot
2026-11-06  7:06 ` [PATCH v2 1/6] gpu: nova-core: gsp: fw: add the r000 bindings for the GMC API header Alexandre Courbot
2026-11-06  7:06 ` [PATCH v2 2/6] gpu: nova-core: gsp: add " Alexandre Courbot
2026-11-06  7:06 ` [PATCH v2 3/6] gpu: nova-core: gsp: cmdq: add the GMC send path Alexandre Courbot
2026-11-06  7:06 ` Alexandre Courbot [this message]
2026-11-06  9:08   ` [PATCH v2 4/6] gpu: nova-core: gsp: cmdq: move message trimming into its own function Eliot Courtney
2026-11-06  7:06 ` [PATCH v2 5/6] gpu: nova-core: gsp: cmdq: do not use ENOMSG to skip messages Alexandre Courbot
2026-11-06  9:09   ` Eliot Courtney
2026-11-06  7:06 ` [PATCH v2 6/6] gpu: nova-core: gsp: cmdq: add the GMC receive path Alexandre Courbot
2026-11-06  9:38   ` Eliot Courtney

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=20261106-r000_rebase_on_cmdq-rpc-v2-4-e481ea2168ef@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=ecourtney@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=ttabi@nvidia.com \
    --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

all inboxes | Powered by JetHome®