Danilo Krummrich
2025-May-20 15:36 UTC
[PATCH v3 16/19] nova-core: Add support for VBIOS ucode extraction for boot
On Tue, May 20, 2025 at 11:11:12AM -0400, Joel Fernandes wrote:> On 5/20/2025 11:01 AM, Danilo Krummrich wrote: > > I made this change and it LGTM. Thanks! I did not do the '.0' though since I > want to keep the readability, lets see in the next revision if that looks good.I think readability, is just as good with `.0`, but I'm fine with either.> >>> In general, I feel like a lot of those Option come from a programming pattern > >>> that is very common in C, i.e. allocate a structure (stack or heap) and then > >>> initialize its fields. > >>> > >>> In Rust you should aim to initialize all the fields of a structure when you > >>> create the instance. Option as a return type of a function is common, but it's > >>> always a bit suspicious when there is an Option field in a struct. > >> > >> I looked into it, I could not git rid of those ones because we need to > >> initialize in the "impl TryFrom<BiosImageBase> for BiosImage {" > >> > >> 0xE0 => Ok(BiosImage::FwSec(FwSecBiosImage { > >> base, > >> falcon_data_offset: None, > >> pmu_lookup_table: None, > >> falcon_ucode_offset: None, > >> })), > >> > >> And these fields will not be determined until much later, because as is the case > >> with the earlier example, these fields cannot be determined until all the images > >> are parsed. > > > > You should not use TryFrom, but instead use a normal constructor, such as > > > > BiosImage::new(base_bios_image) > > > > and do the parsing within this constructor. > > > > If you want a helper type with Options while parsing that's totally fine, but > > the final result can clearly be without Options. For instance: > > > > struct Data { > > image: KVec<u8>, > > } > > > > impl Data { > > fn new() -> Result<Self> { > > let parser = DataParser::new(); > > > > Self { image: parser.parse()? } > > } > > > > fn load_image(&self) { > > ... > > } > > } > > > > struct DataParser { > > // Only some images have a checksum. > > checksum: Option<u64>, > > // Some images have an extra offset. > > offset: Option<u64>, > > // Some images need to be patched. > > patch: Option<KVec<u8>>, > > image: KVec<u8>, > > } > > > > impl DataParser { > > fn new() -> Self { > > Self { > > checksum: None, > > offset: None, > > patch: None, > > bytes: KVec::new(), > > } > > } > > > > fn parse(self) -> Result<KVec<u8>> { > > // Fetch all the required data. > > self.fetch_checksum()?; > > self.fetch_offset()?; > > self.fetch_patch()?; > > self.fetch_byes()?; > > > > // Doesn't do anything if `checksum == None`. > > self.validate_checksum()?; > > > > // Doesn't do anything if `offset == None`. > > self.apply_offset()?; > > > > // Doesn't do anything if `patch == None`. > > self.apply_patch()?; > > > > // Return the final image. > > self.image > > } > > } > > > > I think the pattern here is the same, but in this example you keep working with > > the DataParser, instead of a new instance of Data. > > I think this would be a fundamental rewrite of the patch. I am Ok with looking > into it as a future item, but right now I am not sure if it justifies not using > Option for these few. There's a lot of immediate work we have to do for boot, > lets please not block the patch on just this if that's Ok with you. If you want, > I could add a TODO here.Honestly, I don't think it'd be too bad to fix this up. It's "just" a bit of juggling fields and moving code around. The actual code should not change much. Having Option<T> where the corresponding value T isn't actually optional is extremely confusing and makes it hard for everyone, but especially new contributors, to understand the code and can easily trick people into taking wrong assumptions. Making the code reasonably accessible for (new) contributors is one of the objectives of nova and one of the learnings from nouveau. Hence, let's get this right from the get-go please.
Joel Fernandes
2025-May-20 16:02 UTC
[PATCH v3 16/19] nova-core: Add support for VBIOS ucode extraction for boot
> On May 20, 2025, at 11:37?AM, Danilo Krummrich <dakr at kernel.org> wrote: > > ?On Tue, May 20, 2025 at 11:11:12AM -0400, Joel Fernandes wrote: >> On 5/20/2025 11:01 AM, Danilo Krummrich wrote: >> >> I made this change and it LGTM. Thanks! I did not do the '.0' though since I >> want to keep the readability, lets see in the next revision if that looks good. > > I think readability, is just as good with `.0`, but I'm fine with either.Cool.> >>>>> In general, I feel like a lot of those Option come from a programming pattern >>>>> that is very common in C, i.e. allocate a structure (stack or heap) and then >>>>> initialize its fields. >>>>> >>>>> In Rust you should aim to initialize all the fields of a structure when you >>>>> create the instance. Option as a return type of a function is common, but it's >>>>> always a bit suspicious when there is an Option field in a struct. >>>> >>>> I looked into it, I could not git rid of those ones because we need to >>>> initialize in the "impl TryFrom<BiosImageBase> for BiosImage {" >>>> >>>> 0xE0 => Ok(BiosImage::FwSec(FwSecBiosImage { >>>> base, >>>> falcon_data_offset: None, >>>> pmu_lookup_table: None, >>>> falcon_ucode_offset: None, >>>> })), >>>> >>>> And these fields will not be determined until much later, because as is the case >>>> with the earlier example, these fields cannot be determined until all the images >>>> are parsed. >>> >>> You should not use TryFrom, but instead use a normal constructor, such as >>> >>> BiosImage::new(base_bios_image) >>> >>> and do the parsing within this constructor. >>> >>> If you want a helper type with Options while parsing that's totally fine, but >>> the final result can clearly be without Options. For instance: >>> >>> struct Data { >>> image: KVec<u8>, >>> } >>> >>> impl Data { >>> fn new() -> Result<Self> { >>> let parser = DataParser::new(); >>> >>> Self { image: parser.parse()? } >>> } >>> >>> fn load_image(&self) { >>> ... >>> } >>> } >>> >>> struct DataParser { >>> // Only some images have a checksum. >>> checksum: Option<u64>, >>> // Some images have an extra offset. >>> offset: Option<u64>, >>> // Some images need to be patched. >>> patch: Option<KVec<u8>>, >>> image: KVec<u8>, >>> } >>> >>> impl DataParser { >>> fn new() -> Self { >>> Self { >>> checksum: None, >>> offset: None, >>> patch: None, >>> bytes: KVec::new(), >>> } >>> } >>> >>> fn parse(self) -> Result<KVec<u8>> { >>> // Fetch all the required data. >>> self.fetch_checksum()?; >>> self.fetch_offset()?; >>> self.fetch_patch()?; >>> self.fetch_byes()?; >>> >>> // Doesn't do anything if `checksum == None`. >>> self.validate_checksum()?; >>> >>> // Doesn't do anything if `offset == None`. >>> self.apply_offset()?; >>> >>> // Doesn't do anything if `patch == None`. >>> self.apply_patch()?; >>> >>> // Return the final image. >>> self.image >>> } >>> } >>> >>> I think the pattern here is the same, but in this example you keep working with >>> the DataParser, instead of a new instance of Data. >> >> I think this would be a fundamental rewrite of the patch. I am Ok with looking >> into it as a future item, but right now I am not sure if it justifies not using >> Option for these few. There's a lot of immediate work we have to do for boot, >> lets please not block the patch on just this if that's Ok with you. If you want, >> I could add a TODO here. > > Honestly, I don't think it'd be too bad to fix this up. It's "just" a bit of > juggling fields and moving code around. The actual code should not change much. > > Having Option<T> where the corresponding value T isn't actually optional is > extremely confusing and makes it hard for everyone, but especially new > contributors, to understand the code and can easily trick people into taking > wrong assumptions. > > Making the code reasonably accessible for (new) contributors is one of the > objectives of nova and one of the learnings from nouveau. > > Hence, let's get this right from the get-go please.Ok, I will look into making this change. :-) thanks, - Joel
Joel Fernandes
2025-May-20 18:13 UTC
[PATCH v3 16/19] nova-core: Add support for VBIOS ucode extraction for boot
On 5/20/2025 11:36 AM, Danilo Krummrich wrote:>>> If you want a helper type with Options while parsing that's totally fine, but >>> the final result can clearly be without Options. For instance: >>> >>> struct Data { >>> image: KVec<u8>, >>> } >>> >>> impl Data { >>> fn new() -> Result<Self> { >>> let parser = DataParser::new(); >>> >>> Self { image: parser.parse()? } >>> } >>> >>> fn load_image(&self) { >>> ... >>> } >>> } >>> >>> struct DataParser { >>> // Only some images have a checksum. >>> checksum: Option<u64>, >>> // Some images have an extra offset. >>> offset: Option<u64>, >>> // Some images need to be patched. >>> patch: Option<KVec<u8>>, >>> image: KVec<u8>, >>> } >>> >>> impl DataParser { >>> fn new() -> Self { >>> Self { >>> checksum: None, >>> offset: None, >>> patch: None, >>> bytes: KVec::new(), >>> } >>> } >>> >>> fn parse(self) -> Result<KVec<u8>> { >>> // Fetch all the required data. >>> self.fetch_checksum()?; >>> self.fetch_offset()?; >>> self.fetch_patch()?; >>> self.fetch_byes()?; >>> >>> // Doesn't do anything if `checksum == None`. >>> self.validate_checksum()?; >>> >>> // Doesn't do anything if `offset == None`. >>> self.apply_offset()?; >>> >>> // Doesn't do anything if `patch == None`. >>> self.apply_patch()?; >>> >>> // Return the final image. >>> self.image >>> } >>> } >>> >>> I think the pattern here is the same, but in this example you keep working with >>> the DataParser, instead of a new instance of Data. >> I think this would be a fundamental rewrite of the patch. I am Ok with looking >> into it as a future item, but right now I am not sure if it justifies not using >> Option for these few. There's a lot of immediate work we have to do for boot, >> lets please not block the patch on just this if that's Ok with you. If you want, >> I could add a TODO here. > > Honestly, I don't think it'd be too bad to fix this up. It's "just" a bit of > juggling fields and moving code around. The actual code should not change much. > > Having Option<T> where the corresponding value T isn't actually optional is > extremely confusing and makes it hard for everyone, but especially new > contributors, to understand the code and can easily trick people into taking > wrong assumptions. > > Making the code reasonably accessible for (new) contributors is one of the > objectives of nova and one of the learnings from nouveau.I implemented the Data parsing pattern like the following, the final FwSecBiosImage will not have optional fields as you suggested. It does get rid of 2 additional fields as well which are not needed after vbios parsing completes. https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git/commit/?h=nova/vbios&id=8cc852fe5573890596a91a2a935b3af24dcb9f04 Hope that looks Ok now! I am open to naming FwSecBiosPartial as FwSecBiosData if that's better. The full file after the change: https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git/tree/drivers/gpu/nova-core/vbios.rs?h=nova/vbios&id=8cc852fe5573890596a91a2a935b3af24dcb9f04 thanks, - Joel
Dave Airlie
2025-May-20 21:32 UTC
[PATCH v3 16/19] nova-core: Add support for VBIOS ucode extraction for boot
On Wed, 21 May 2025 at 04:13, Joel Fernandes <joelagnelf at nvidia.com> wrote:> > > > On 5/20/2025 11:36 AM, Danilo Krummrich wrote: > >>> If you want a helper type with Options while parsing that's totally fine, but > >>> the final result can clearly be without Options. For instance: > >>> > >>> struct Data { > >>> image: KVec<u8>, > >>> } > >>> > >>> impl Data { > >>> fn new() -> Result<Self> { > >>> let parser = DataParser::new(); > >>> > >>> Self { image: parser.parse()? } > >>> } > >>> > >>> fn load_image(&self) { > >>> ... > >>> } > >>> } > >>> > >>> struct DataParser { > >>> // Only some images have a checksum. > >>> checksum: Option<u64>, > >>> // Some images have an extra offset. > >>> offset: Option<u64>, > >>> // Some images need to be patched. > >>> patch: Option<KVec<u8>>, > >>> image: KVec<u8>, > >>> } > >>> > >>> impl DataParser { > >>> fn new() -> Self { > >>> Self { > >>> checksum: None, > >>> offset: None, > >>> patch: None, > >>> bytes: KVec::new(), > >>> } > >>> } > >>> > >>> fn parse(self) -> Result<KVec<u8>> { > >>> // Fetch all the required data. > >>> self.fetch_checksum()?; > >>> self.fetch_offset()?; > >>> self.fetch_patch()?; > >>> self.fetch_byes()?; > >>> > >>> // Doesn't do anything if `checksum == None`. > >>> self.validate_checksum()?; > >>> > >>> // Doesn't do anything if `offset == None`. > >>> self.apply_offset()?; > >>> > >>> // Doesn't do anything if `patch == None`. > >>> self.apply_patch()?; > >>> > >>> // Return the final image. > >>> self.image > >>> } > >>> } > >>> > >>> I think the pattern here is the same, but in this example you keep working with > >>> the DataParser, instead of a new instance of Data. > >> I think this would be a fundamental rewrite of the patch. I am Ok with looking > >> into it as a future item, but right now I am not sure if it justifies not using > >> Option for these few. There's a lot of immediate work we have to do for boot, > >> lets please not block the patch on just this if that's Ok with you. If you want, > >> I could add a TODO here. > > > > Honestly, I don't think it'd be too bad to fix this up. It's "just" a bit of > > juggling fields and moving code around. The actual code should not change much. > > > > Having Option<T> where the corresponding value T isn't actually optional is > > extremely confusing and makes it hard for everyone, but especially new > > contributors, to understand the code and can easily trick people into taking > > wrong assumptions. > > > > Making the code reasonably accessible for (new) contributors is one of the > > objectives of nova and one of the learnings from nouveau.I just want to back Danilo up on this concept as well. When I did the experiments code, I faced the not fully constructed object problem a lot, and I tried to resist the C pattern of Option<> all the things, it's a very C based thing where we create an object then initialise it as we go, and it's not a great pattern to have for rust code. I'm not a huge fan of constructor/builder objects either if they can be avoided, please do, and I tried to also avoid proliferating them, but I think for most things we can build the pieces and then the final object as we go, it just requires doing so from the start, and not giving into the Option<> pattern. Dave.