dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment
@ 2025-07-14 11:02 Rhys Lloyd
  2025-07-14 11:28 ` Danilo Krummrich
  2025-07-14 14:48 ` Joel Fernandes
  0 siblings, 2 replies; 6+ messages in thread
From: Rhys Lloyd @ 2025-07-14 11:02 UTC (permalink / raw)
  To: acourbot, dakr
  Cc: Rhys Lloyd, rust-for-linux, airlied, simona, nouveau, dri-devel,
	linux-kernel

Instead of the data field containing a u32 and changing the alignment,
change data to [u8; 4] and convert to u32 with a helper function.
Removes another magic number by making the struct the same size as
the data it needs to read, allowing the use of
`size_of::<PmuLookupTableEntry>()`

Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
---
Changes in v2:
- get_data helper function renamed to data

---
 drivers/gpu/nova-core/vbios.rs | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
index 5b5d9f38cbb3..339c66e63c7e 100644
--- a/drivers/gpu/nova-core/vbios.rs
+++ b/drivers/gpu/nova-core/vbios.rs
@@ -896,21 +896,26 @@ fn try_from(base: BiosImageBase) -> Result<Self> {
 struct PmuLookupTableEntry {
     application_id: u8,
     target_id: u8,
-    data: u32,
+    data: [u8; 4],
 }
 
 impl PmuLookupTableEntry {
     fn new(data: &[u8]) -> Result<Self> {
-        if data.len() < 6 {
+        if data.len() < core::mem::size_of::<Self>() {
             return Err(EINVAL);
         }
 
         Ok(PmuLookupTableEntry {
             application_id: data[0],
             target_id: data[1],
-            data: u32::from_le_bytes(data[2..6].try_into().map_err(|_| EINVAL)?),
+            data: [data[2], data[3], data[4], data[5]],
         })
     }
+
+    /// Construct a u32 from `self.data`.
+    fn data(&self) -> u32 {
+        u32::from_le_bytes(self.data)
+    }
 }
 
 /// The [`PmuLookupTableEntry`] structure is used to find the [`PmuLookupTableEntry`] for a given
@@ -1037,7 +1042,7 @@ fn setup_falcon_data(
             .find_entry_by_type(FALCON_UCODE_ENTRY_APPID_FWSEC_PROD)
         {
             Ok(entry) => {
-                let mut ucode_offset = entry.data as usize;
+                let mut ucode_offset = entry.data() as usize;
                 ucode_offset -= pci_at_image.base.data.len();
                 if ucode_offset < first_fwsec.base.data.len() {
                     dev_err!(pdev.as_ref(), "Falcon Ucode offset not in second Fwsec.\n");

base-commit: 215a3f91713383a3c0d2da82d223a608a3c17ac1
prerequisite-patch-id: d80f92d314a0693d4c89ffb7810d9ab6990336fa
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment
  2025-07-14 11:02 [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment Rhys Lloyd
@ 2025-07-14 11:28 ` Danilo Krummrich
  2025-07-14 14:48 ` Joel Fernandes
  1 sibling, 0 replies; 6+ messages in thread
From: Danilo Krummrich @ 2025-07-14 11:28 UTC (permalink / raw)
  To: Rhys Lloyd
  Cc: acourbot, Joel Fernandes, rust-for-linux, airlied, simona,
	nouveau, dri-devel, linux-kernel

(Cc: Joel)

On Mon Jul 14, 2025 at 1:02 PM CEST, Rhys Lloyd wrote:
> Instead of the data field containing a u32 and changing the alignment,
> change data to [u8; 4] and convert to u32 with a helper function.
> Removes another magic number by making the struct the same size as
> the data it needs to read, allowing the use of
> `size_of::<PmuLookupTableEntry>()`
>
> Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
> ---
> Changes in v2:
> - get_data helper function renamed to data
>
> ---
>  drivers/gpu/nova-core/vbios.rs | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
> index 5b5d9f38cbb3..339c66e63c7e 100644
> --- a/drivers/gpu/nova-core/vbios.rs
> +++ b/drivers/gpu/nova-core/vbios.rs
> @@ -896,21 +896,26 @@ fn try_from(base: BiosImageBase) -> Result<Self> {
>  struct PmuLookupTableEntry {
>      application_id: u8,
>      target_id: u8,
> -    data: u32,
> +    data: [u8; 4],
>  }
>  
>  impl PmuLookupTableEntry {
>      fn new(data: &[u8]) -> Result<Self> {
> -        if data.len() < 6 {
> +        if data.len() < core::mem::size_of::<Self>() {
>              return Err(EINVAL);
>          }
>  
>          Ok(PmuLookupTableEntry {
>              application_id: data[0],
>              target_id: data[1],
> -            data: u32::from_le_bytes(data[2..6].try_into().map_err(|_| EINVAL)?),
> +            data: [data[2], data[3], data[4], data[5]],
>          })
>      }
> +
> +    /// Construct a u32 from `self.data`.
> +    fn data(&self) -> u32 {
> +        u32::from_le_bytes(self.data)
> +    }
>  }
>  
>  /// The [`PmuLookupTableEntry`] structure is used to find the [`PmuLookupTableEntry`] for a given
> @@ -1037,7 +1042,7 @@ fn setup_falcon_data(
>              .find_entry_by_type(FALCON_UCODE_ENTRY_APPID_FWSEC_PROD)
>          {
>              Ok(entry) => {
> -                let mut ucode_offset = entry.data as usize;
> +                let mut ucode_offset = entry.data() as usize;
>                  ucode_offset -= pci_at_image.base.data.len();
>                  if ucode_offset < first_fwsec.base.data.len() {
>                      dev_err!(pdev.as_ref(), "Falcon Ucode offset not in second Fwsec.\n");
>
> base-commit: 215a3f91713383a3c0d2da82d223a608a3c17ac1
> prerequisite-patch-id: d80f92d314a0693d4c89ffb7810d9ab6990336fa


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment
  2025-07-14 11:02 [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment Rhys Lloyd
  2025-07-14 11:28 ` Danilo Krummrich
@ 2025-07-14 14:48 ` Joel Fernandes
  2025-07-14 14:53   ` Alice Ryhl
  1 sibling, 1 reply; 6+ messages in thread
From: Joel Fernandes @ 2025-07-14 14:48 UTC (permalink / raw)
  To: Rhys Lloyd
  Cc: acourbot, dakr, rust-for-linux, airlied, simona, nouveau,
	dri-devel, linux-kernel

Hello, Rhys,

On Mon, Jul 14, 2025 at 04:02:23AM -0700, Rhys Lloyd wrote:
> Instead of the data field containing a u32 and changing the alignment,
> change data to [u8; 4] and convert to u32 with a helper function.
> Removes another magic number by making the struct the same size as
> the data it needs to read, allowing the use of
> `size_of::<PmuLookupTableEntry>()`
> 
> Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
> ---
> Changes in v2:
> - get_data helper function renamed to data
> 
> ---
>  drivers/gpu/nova-core/vbios.rs | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
> index 5b5d9f38cbb3..339c66e63c7e 100644
> --- a/drivers/gpu/nova-core/vbios.rs
> +++ b/drivers/gpu/nova-core/vbios.rs
> @@ -896,21 +896,26 @@ fn try_from(base: BiosImageBase) -> Result<Self> {
>  struct PmuLookupTableEntry {
>      application_id: u8,
>      target_id: u8,
> -    data: u32,
> +    data: [u8; 4],

Instead of this, could we make the struct as #repr[(C, packed)] or does that
not work for some reason?

>  }
>  
>  impl PmuLookupTableEntry {
>      fn new(data: &[u8]) -> Result<Self> {
> -        if data.len() < 6 {
> +        if data.len() < core::mem::size_of::<Self>() {

This part looks good, and thanks for the fix. Alex beat me to the review but
for the actual fix [1], FWIW:
Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com>

[1] https://lore.kernel.org/all/DBBG2S0ZQAMI.2KK26Z7U58DI8@nvidia.com/#t

thanks,

 - Joel

>              return Err(EINVAL);
>          }
>  
>          Ok(PmuLookupTableEntry {
>              application_id: data[0],
>              target_id: data[1],
> -            data: u32::from_le_bytes(data[2..6].try_into().map_err(|_| EINVAL)?),
> +            data: [data[2], data[3], data[4], data[5]],
>          })
>      }
> +
> +    /// Construct a u32 from `self.data`.
> +    fn data(&self) -> u32 {
> +        u32::from_le_bytes(self.data)
> +    }
>  }
>  
>  /// The [`PmuLookupTableEntry`] structure is used to find the [`PmuLookupTableEntry`] for a given
> @@ -1037,7 +1042,7 @@ fn setup_falcon_data(
>              .find_entry_by_type(FALCON_UCODE_ENTRY_APPID_FWSEC_PROD)
>          {
>              Ok(entry) => {
> -                let mut ucode_offset = entry.data as usize;
> +                let mut ucode_offset = entry.data() as usize;
>                  ucode_offset -= pci_at_image.base.data.len();
>                  if ucode_offset < first_fwsec.base.data.len() {
>                      dev_err!(pdev.as_ref(), "Falcon Ucode offset not in second Fwsec.\n");
> 
> base-commit: 215a3f91713383a3c0d2da82d223a608a3c17ac1
> prerequisite-patch-id: d80f92d314a0693d4c89ffb7810d9ab6990336fa
> -- 
> 2.50.1
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment
  2025-07-14 14:48 ` Joel Fernandes
@ 2025-07-14 14:53   ` Alice Ryhl
  2025-07-14 15:22     ` Joel Fernandes
  0 siblings, 1 reply; 6+ messages in thread
From: Alice Ryhl @ 2025-07-14 14:53 UTC (permalink / raw)
  To: Joel Fernandes
  Cc: Rhys Lloyd, acourbot, dakr, rust-for-linux, airlied, simona,
	nouveau, dri-devel, linux-kernel

On Mon, Jul 14, 2025 at 4:49 PM Joel Fernandes <joelagnelf@nvidia.com> wrote:
>
> Hello, Rhys,
>
> On Mon, Jul 14, 2025 at 04:02:23AM -0700, Rhys Lloyd wrote:
> > Instead of the data field containing a u32 and changing the alignment,
> > change data to [u8; 4] and convert to u32 with a helper function.
> > Removes another magic number by making the struct the same size as
> > the data it needs to read, allowing the use of
> > `size_of::<PmuLookupTableEntry>()`
> >
> > Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
> > ---
> > Changes in v2:
> > - get_data helper function renamed to data
> >
> > ---
> >  drivers/gpu/nova-core/vbios.rs | 13 +++++++++----
> >  1 file changed, 9 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
> > index 5b5d9f38cbb3..339c66e63c7e 100644
> > --- a/drivers/gpu/nova-core/vbios.rs
> > +++ b/drivers/gpu/nova-core/vbios.rs
> > @@ -896,21 +896,26 @@ fn try_from(base: BiosImageBase) -> Result<Self> {
> >  struct PmuLookupTableEntry {
> >      application_id: u8,
> >      target_id: u8,
> > -    data: u32,
> > +    data: [u8; 4],
>
> Instead of this, could we make the struct as #repr[(C, packed)] or does that
> not work for some reason?

It would probably, but packed structs aren't very nice to work with
because Rust has to be really careful to never generate a reference to
unaligned fields.

Alice

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment
  2025-07-14 14:53   ` Alice Ryhl
@ 2025-07-14 15:22     ` Joel Fernandes
  2025-07-16  1:28       ` Alexandre Courbot
  0 siblings, 1 reply; 6+ messages in thread
From: Joel Fernandes @ 2025-07-14 15:22 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Rhys Lloyd, acourbot, dakr, rust-for-linux, airlied, simona,
	nouveau, dri-devel, linux-kernel



On 7/14/2025 10:53 AM, Alice Ryhl wrote:
> On Mon, Jul 14, 2025 at 4:49 PM Joel Fernandes <joelagnelf@nvidia.com> wrote:
>>
>> Hello, Rhys,
>>
>> On Mon, Jul 14, 2025 at 04:02:23AM -0700, Rhys Lloyd wrote:
>>> Instead of the data field containing a u32 and changing the alignment,
>>> change data to [u8; 4] and convert to u32 with a helper function.
>>> Removes another magic number by making the struct the same size as
>>> the data it needs to read, allowing the use of
>>> `size_of::<PmuLookupTableEntry>()`
>>>
>>> Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
>>> ---
>>> Changes in v2:
>>> - get_data helper function renamed to data
>>>
>>> ---
>>>  drivers/gpu/nova-core/vbios.rs | 13 +++++++++----
>>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
>>> index 5b5d9f38cbb3..339c66e63c7e 100644
>>> --- a/drivers/gpu/nova-core/vbios.rs
>>> +++ b/drivers/gpu/nova-core/vbios.rs
>>> @@ -896,21 +896,26 @@ fn try_from(base: BiosImageBase) -> Result<Self> {
>>>  struct PmuLookupTableEntry {
>>>      application_id: u8,
>>>      target_id: u8,
>>> -    data: u32,
>>> +    data: [u8; 4],
>>
>> Instead of this, could we make the struct as #repr[(C, packed)] or does that
>> not work for some reason?
> 
> It would probably, but packed structs aren't very nice to work with
> because Rust has to be really careful to never generate a reference to
> unaligned fields.
Oh, interesting. I am Ok with the [u8; 4] then. Btw, we do have several
#[repr(C, packed)] in vbios.rs already.

 - Joel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment
  2025-07-14 15:22     ` Joel Fernandes
@ 2025-07-16  1:28       ` Alexandre Courbot
  0 siblings, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2025-07-16  1:28 UTC (permalink / raw)
  To: Joel Fernandes, Alice Ryhl
  Cc: Rhys Lloyd, dakr, rust-for-linux, airlied, simona, nouveau,
	dri-devel, linux-kernel

On Tue Jul 15, 2025 at 12:22 AM JST, Joel Fernandes wrote:
>
>
> On 7/14/2025 10:53 AM, Alice Ryhl wrote:
>> On Mon, Jul 14, 2025 at 4:49 PM Joel Fernandes <joelagnelf@nvidia.com> wrote:
>>>
>>> Hello, Rhys,
>>>
>>> On Mon, Jul 14, 2025 at 04:02:23AM -0700, Rhys Lloyd wrote:
>>>> Instead of the data field containing a u32 and changing the alignment,
>>>> change data to [u8; 4] and convert to u32 with a helper function.
>>>> Removes another magic number by making the struct the same size as
>>>> the data it needs to read, allowing the use of
>>>> `size_of::<PmuLookupTableEntry>()`
>>>>
>>>> Signed-off-by: Rhys Lloyd <krakow20@gmail.com>
>>>> ---
>>>> Changes in v2:
>>>> - get_data helper function renamed to data
>>>>
>>>> ---
>>>>  drivers/gpu/nova-core/vbios.rs | 13 +++++++++----
>>>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
>>>> index 5b5d9f38cbb3..339c66e63c7e 100644
>>>> --- a/drivers/gpu/nova-core/vbios.rs
>>>> +++ b/drivers/gpu/nova-core/vbios.rs
>>>> @@ -896,21 +896,26 @@ fn try_from(base: BiosImageBase) -> Result<Self> {
>>>>  struct PmuLookupTableEntry {
>>>>      application_id: u8,
>>>>      target_id: u8,
>>>> -    data: u32,
>>>> +    data: [u8; 4],
>>>
>>> Instead of this, could we make the struct as #repr[(C, packed)] or does that
>>> not work for some reason?
>> 
>> It would probably, but packed structs aren't very nice to work with
>> because Rust has to be really careful to never generate a reference to
>> unaligned fields.
> Oh, interesting. I am Ok with the [u8; 4] then. Btw, we do have several
> #[repr(C, packed)] in vbios.rs already.

Yeah, in this particular case this is a module-local struct for which
(AFAICT) we don't need to generate references to, so unless there are
other issues I think making it packed and storing the properly-ordered
u32 at construction time is both simpler and safer.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-07-16  1:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-14 11:02 [PATCH v2] gpu: nova-core: vbios: change PmuLookupTableEntry to relax alignment Rhys Lloyd
2025-07-14 11:28 ` Danilo Krummrich
2025-07-14 14:48 ` Joel Fernandes
2025-07-14 14:53   ` Alice Ryhl
2025-07-14 15:22     ` Joel Fernandes
2025-07-16  1:28       ` Alexandre Courbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).