From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Timur Tabi" <ttabi@nvidia.com>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
"gary@garyguo.net" <gary@garyguo.net>,
"Eliot Courtney" <ecourtney@nvidia.com>,
"nova-gpu@lists.linux.dev" <nova-gpu@lists.linux.dev>,
"Zhi Wang" <zhiw@nvidia.com>,
"mcgrof@kernel.org" <mcgrof@kernel.org>,
"dakr@kernel.org" <dakr@kernel.org>,
"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>,
"driver-core@lists.linux.dev" <driver-core@lists.linux.dev>,
"ojeda@kernel.org" <ojeda@kernel.org>,
"russ.weight@linux.dev" <russ.weight@linux.dev>
Subject: Re: [PATCH v3 2/7] gpu: nova-core: add TLV parser for firmware files
Date: Thu, 09 Jul 2026 12:43:28 +0900 [thread overview]
Message-ID: <DJTQAZCXYPN7.3Q0B4NH8EA8BO@nvidia.com> (raw)
In-Reply-To: <e913d3a53361cd6b8b076f43d038cfd029cda473.camel@nvidia.com>
On Thu Jul 9, 2026 at 7:48 AM JST, Timur Tabi wrote:
> On Tue, 2026-07-07 at 14:13 +0900, Alexandre Courbot wrote:
>> > Returning error on length==0 simplifies a lot of code on the caller side.
>>
>> Can you give an example of such a simplification? I'm fine if there is a
>> benefit to doing so, but would like to understand what we gain.
>
> Well, maybe not "a lot" of code, but in general, it eliminates the need to test for empty slices
> every time. If get_bytes() returns success, you know that you actually do have some bytes.
>
> For example:
>
> let sig_bytes = self.get_bytes(b"SIGN")?;
>
> // Ensure that sig_bytes can be divided evenly into chunks.
> if sig_bytes.len() % num_sigs != 0 {
> return Err(EINVAL);
> }
>
> // num_sigs cannot be 0, and sig_bytes cannot be empty, so this cannot panic.
> let sig_size = sig_bytes.len() / num_sigs;
>
> sig_bytes.len() cannot be 0, so we know that sig_size cannot be 0, and therefore
>
> sig_bytes.chunks_exact(sig_size).nth(index).ok_or(EINVAL)
>
> chunks_exact() cannot fail.
Basically we want a guarantee that `sig_size` is not zero. I'd prefer if
we could enforce that using the type system rather than relying on a
guarantee that `get_bytes` doesn't return an empty slice, because even
that guarantee alone is not enough - we also need `num_sigs > 0` and
`sig_bytes.len() % num_sigs == 0` for that.
All these conditions are spread around in the code, so I think it is a
good opportunity to gather them together, and use `NonZero` to hold the
guarantee we want.
Something like this:
let num_sigs = usize::from_safe_cast(tlv.get_u32(b"NSIG")?);
// Size of one signature - Booter is always signed
let sig_size = if (1..=15).contains(&num_sigs) && sig_bytes.len() % num_sigs == 0 {
NonZero::new(sig_bytes.len() / num_sigs)
} else {
None
}
.ok_or_else(|| {
dev_err!(dev, "invalid signature count {}\n", num_sigs);
EINVAL
})?;
...
// PANIC: `sig_size` is `NonZero`, so `chunks_exact` cannot panic.
let sig_chunk = sig_bytes.chunks_exact(sig_size.get()).nth(index).ok_or(EINVAL)?;
This is more sound as the guarantee we need is carried by the type of
`sig_size`, and with that there is no need to make `get_bytes` return an
error on empty slices.
next prev parent reply other threads:[~2026-07-09 3:43 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-02 19:27 [PATCH v3 0/7] Transition Nova Core to TLV firmare images Timur Tabi
2026-07-02 19:27 ` [PATCH v3 1/7] rust: firmware: add request_into_buf() Timur Tabi
2026-07-03 2:51 ` Alvin Sun
2026-07-03 3:06 ` Timur Tabi
2026-07-03 13:51 ` Danilo Krummrich
2026-07-06 6:30 ` Alexandre Courbot
2026-07-06 10:50 ` Danilo Krummrich
2026-07-07 2:54 ` Timur Tabi
2026-07-07 5:10 ` Alexandre Courbot
2026-07-07 13:32 ` Danilo Krummrich
2026-07-02 19:27 ` [PATCH v3 2/7] gpu: nova-core: add TLV parser for firmware files Timur Tabi
2026-07-02 19:45 ` Timur Tabi
2026-07-06 6:31 ` Alexandre Courbot
2026-07-07 2:56 ` Timur Tabi
2026-07-07 5:13 ` Alexandre Courbot
2026-07-08 22:48 ` Timur Tabi
2026-07-09 3:43 ` Alexandre Courbot [this message]
2026-07-06 6:31 ` Alexandre Courbot
2026-07-08 20:12 ` Timur Tabi
2026-07-09 1:55 ` Alexandre Courbot
2026-07-02 19:27 ` [PATCH v3 3/7] gpu: nova-core: transition booter_load to TLV images Timur Tabi
2026-07-06 6:31 ` Alexandre Courbot
2026-07-08 20:46 ` Timur Tabi
2026-07-09 3:51 ` Alexandre Courbot
2026-07-02 19:27 ` [PATCH v3 4/7] gpu: nova-core: transition gsp " Timur Tabi
2026-07-02 19:27 ` [PATCH v3 5/7] gpu: nova-core: transition gen_bootloader " Timur Tabi
2026-07-06 6:31 ` Alexandre Courbot
2026-07-08 22:26 ` Timur Tabi
2026-07-09 2:24 ` Alexandre Courbot
2026-07-02 19:27 ` [PATCH v3 6/7] gpu: nova-core: transition fsp " Timur Tabi
2026-07-02 19:27 ` [PATCH v3 7/7] gpu: nova-core: update firmware module info for " Timur Tabi
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=DJTQAZCXYPN7.3Q0B4NH8EA8BO@nvidia.com \
--to=acourbot@nvidia.com \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=ecourtney@nvidia.com \
--cc=gary@garyguo.net \
--cc=jhubbard@nvidia.com \
--cc=mcgrof@kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=russ.weight@linux.dev \
--cc=rust-for-linux@vger.kernel.org \
--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