Rust for Linux List
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alexandre Courbot" <acourbot@nvidia.com>
Cc: "Timur Tabi" <ttabi@nvidia.com>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"nova-gpu@lists.linux.dev" <nova-gpu@lists.linux.dev>,
	"Zhi Wang" <zhiw@nvidia.com>,
	"gary@garyguo.net" <gary@garyguo.net>,
	"russ.weight@linux.dev" <russ.weight@linux.dev>,
	"mcgrof@kernel.org" <mcgrof@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>
Subject: Re: [PATCH v3 1/7] rust: firmware: add request_into_buf()
Date: Tue, 07 Jul 2026 15:32:54 +0200	[thread overview]
Message-ID: <DJSDL78562EB.1RQP5C4UX55TL@kernel.org> (raw)
In-Reply-To: <DJS2WBOSQ1U7.AQQZA227X9MK@nvidia.com>

On Tue Jul 7, 2026 at 7:10 AM CEST, Alexandre Courbot wrote:
> On Tue Jul 7, 2026 at 11:54 AM JST, Timur Tabi wrote:
>> On Mon, 2026-07-06 at 12:50 +0200, Danilo Krummrich wrote:
>>> On Mon Jul 6, 2026 at 8:30 AM CEST, Alexandre Courbot wrote:
>>> > Sashiko is correct to point out that this doesn't return the number of
>>> > bytes actually written into `buf`. We might not use that information in
>>> > nova-core, but this is a kernel-wide API.
>>> 
>>> Returning the size only doesn't carry a lot of value, please see [1].
>>> 
>>> > Returning a `Result<&[u8]>` would take care of this and cover the
>>> > general use-case nicely.
>>> 
>>> I'm fine with either this or just Result, as we don't have a user for the
>>> former.
>>> 
>>> [1] https://lore.kernel.org/nova-gpu/DJ6JV2QQY0QD.1ZK1PRRBN5BRP@kernel.org/
>>
>> Guys, please come to a consensus.  I had it return a size originally, but then Danilo said don't
>> bother, and now Alex says I should return a size.

I said that both are fine for now, just Result or the slice, but only the size
doesn't serve a purpose (see [1] above or the reasoning below).

> Incorrect; I said you should return a slice. :)
>
> What makes me a bit nervous with this API is that it is not sound
> against incorrect size information or race conditions (e.g. if the file
> on disk changes after we determined its size). But that's true of my
> proposal as well anyway.

There's nothing we can do about this, the file could also be corrupted, etc. In
the end there are only two cases, the hardware can't deal with garbage, then the
size-sanity check is not sufficient and we need proper validation (e.g. through
the returned slice), or the hardware can deal with garbage, then the size-sanity
check isn't necessary.

> Another inefficiency is that the buffer must be initialized when passed,
> only for its content to be immediately overwritten. I'd like to make
> this function accept a `MaybeUninit`, but that will require more generic
> code.

This is true, with the current approach we need something like

	let buf = VBox::<[u8; SIZE]>::zeroed(GFP_KERNEL)?;

which isn't the end of the world, especially in the firmware loading path, but I
agree it is a bit unfortunate.

We could indeed get rid of the initialization requirement entirely, but in this
case just returning a slice isn't sufficient, as it leaves the caller with e.g.
a VBox<MaybeUninit<[u8; SIZE]>> for storage, which is annoying if you want to
access the buffer subsequently, e.g. to parse headers, etc.

So, for this to properly work, you'd need to take the container (Vec, Box,
CoherentBox) by value and return the initialized one on success and the
uninitialized one wrapped into a custom error type on failure. But this is
probably a bit overengineered.

A simpler option would be to just use an initializer, such that
request_into_buf() becomes something like this:

	pub fn request_firmware_init<'a>(
	    name: &'a CStr,
	    dev: &'a Device,
	) -> impl Init<[u8], Error> + 'a {
	    unsafe {
	        init_from_closure(move |slot: *mut [u8]| {
	            // call request_firmware_into_buf()
	            Ok(())
	        })
	    }
	}

And then allocate the buffer with

	let init = request_firmware_init(...);
	let fw = VVec::init_with(len, init, GFP_KERNEL)?;

In any case, the current approach is fine for this series, this can be a
follow-up if needed.

The only thing that'd be nice to replace would be

	VVec::from_elem(0u8, size, GFP_KERNEL)

with

	VVec::<u8>::zeroed(size, GFP_KERNEL)

analogous to Box::zeroed().

  reply	other threads:[~2026-07-07 13:32 UTC|newest]

Thread overview: 23+ 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 [this message]
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-06  6:31   ` 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-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-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=DJSDL78562EB.1RQP5C4UX55TL@kernel.org \
    --to=dakr@kernel.org \
    --cc=acourbot@nvidia.com \
    --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