* [PATCH v2] efi_loader: Fix warnings for unaligned accesses
@ 2023-05-11 16:40 Ilias Apalodimas
2023-05-11 16:55 ` Heinrich Schuchardt
0 siblings, 1 reply; 5+ messages in thread
From: Ilias Apalodimas @ 2023-05-11 16:40 UTC (permalink / raw)
To: u-boot
Cc: trini, takahiro.akashi, Ilias Apalodimas, Heinrich Schuchardt,
Heinrich Schuchardt
Tom reports that when building with clang we see this warning:
field guid within 'struct efi_hii_keyboard_layout' is less aligned than 'efi_guid_t' and is usually due to 'struct efi_hii_keyboard_layout' being packed, which can lead to unaligned accesses [-Wunaligned-access]
This happens because 'struct efi_hii_keyboard_layout' is defined as
packed and thus has 1-byte alignment but efi_guid_t is a type that
requires greater alignment than that.
However the EFI spec describes the EFI_GUID as
"128-bit buffer containing a unique identifier value.
Unless otherwise specified"
So convert the efi_guid_t -> u8 b[16] here and skip the alignment
requirements. Since the struct is packed to begin with, it makes no
difference on the final memory layout.
Suggested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
Changes since v1:
- Adjust the commit message and add a comment on why this happens
include/efi_api.h | 28 +++++++++++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git a/include/efi_api.h b/include/efi_api.h
index 2fd0221c1c77..55a4c989fc7c 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -1170,7 +1170,33 @@ struct efi_key_descriptor {
struct efi_hii_keyboard_layout {
u16 layout_length;
- efi_guid_t guid;
+ /*
+ * The EFI spec defines this as efi_guid_t.
+ * clang and gcc both report alignment problems here.
+ * clang with -Wunaligned-access
+ * warning: field guid within 'struct efi_hii_keyboard_layout' is less
+ * aligned than 'efi_guid_t' and is usually due to
+ * 'struct efi_hii_keyboard_layout' being packed, which can lead to
+ * unaligned accesses
+ *
+ * GCC with -Wpacked-not-aligned -Waddress-of-packed-member
+ * 'efi_guid_t' offset 2 in 'struct efi_hii_keyboard_layout'
+ * isn't aligned to 4
+ *
+ * Removing the alignment from efi_guid_t is not an option, since
+ * it is also used in non-packed structs and that would break
+ * calculations with offsetof
+ *
+ * This is the only place we get a report for. That happens because
+ * all other declarations of efi_guid_t within a packed struct happens
+ * to be 4-byte aligned. i.e a u32, a u64 a 2 * u16 or any combination
+ * that ends up landing efi_guid_t on a 4byte boundary precedes.
+ *
+ * Replace this with a 1-byte aligned counterpart of b[16]. This is a
+ * packed struct so the memory placement of efi_guid_t should not change
+ *
+ */
+ u8 guid[16];
u32 layout_descriptor_string_offset;
u8 descriptor_count;
/* struct efi_key_descriptor descriptors[]; follows here */
--
2.39.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] efi_loader: Fix warnings for unaligned accesses
2023-05-11 16:40 [PATCH v2] efi_loader: Fix warnings for unaligned accesses Ilias Apalodimas
@ 2023-05-11 16:55 ` Heinrich Schuchardt
2023-05-11 17:00 ` Ilias Apalodimas
0 siblings, 1 reply; 5+ messages in thread
From: Heinrich Schuchardt @ 2023-05-11 16:55 UTC (permalink / raw)
To: Ilias Apalodimas; +Cc: trini, takahiro.akashi, u-boot
On 5/11/23 18:40, Ilias Apalodimas wrote:
> Tom reports that when building with clang we see this warning:
> field guid within 'struct efi_hii_keyboard_layout' is less aligned than 'efi_guid_t' and is usually due to 'struct efi_hii_keyboard_layout' being packed, which can lead to unaligned accesses [-Wunaligned-access]
>
> This happens because 'struct efi_hii_keyboard_layout' is defined as
> packed and thus has 1-byte alignment but efi_guid_t is a type that
> requires greater alignment than that.
>
> However the EFI spec describes the EFI_GUID as
> "128-bit buffer containing a unique identifier value.
> Unless otherwise specified"
>
> So convert the efi_guid_t -> u8 b[16] here and skip the alignment
> requirements. Since the struct is packed to begin with, it makes no
> difference on the final memory layout.
>
> Suggested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> Reported-by: Tom Rini <trini@konsulko.com>
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> ---
> Changes since v1:
> - Adjust the commit message and add a comment on why this happens
>
> include/efi_api.h | 28 +++++++++++++++++++++++++++-
> 1 file changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/include/efi_api.h b/include/efi_api.h
> index 2fd0221c1c77..55a4c989fc7c 100644
> --- a/include/efi_api.h
> +++ b/include/efi_api.h
> @@ -1170,7 +1170,33 @@ struct efi_key_descriptor {
>
> struct efi_hii_keyboard_layout {
> u16 layout_length;
> - efi_guid_t guid;
> + /*
> + * The EFI spec defines this as efi_guid_t.
> + * clang and gcc both report alignment problems here.
> + * clang with -Wunaligned-access
> + * warning: field guid within 'struct efi_hii_keyboard_layout' is less
> + * aligned than 'efi_guid_t' and is usually due to
> + * 'struct efi_hii_keyboard_layout' being packed, which can lead to
> + * unaligned accesses
> + *
> + * GCC with -Wpacked-not-aligned -Waddress-of-packed-member
> + * 'efi_guid_t' offset 2 in 'struct efi_hii_keyboard_layout'
> + * isn't aligned to 4
> + *
> + * Removing the alignment from efi_guid_t is not an option, since
> + * it is also used in non-packed structs and that would break
> + * calculations with offsetof
> + *
> + * This is the only place we get a report for. That happens because
> + * all other declarations of efi_guid_t within a packed struct happens
> + * to be 4-byte aligned. i.e a u32, a u64 a 2 * u16 or any combination
> + * that ends up landing efi_guid_t on a 4byte boundary precedes.
> + *
> + * Replace this with a 1-byte aligned counterpart of b[16]. This is a
> + * packed struct so the memory placement of efi_guid_t should not change
> + *
> + */
> + u8 guid[16];
> u32 layout_descriptor_string_offset;
> u8 descriptor_count;
> /* struct efi_key_descriptor descriptors[]; follows here */
> --
> 2.39.2
>
Thank you for investigating this in depth.
Commit messages should preferably limited to 75 characters per line, see
scripts/checkpatch.pl. No need to resubmit.
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] efi_loader: Fix warnings for unaligned accesses
2023-05-11 16:55 ` Heinrich Schuchardt
@ 2023-05-11 17:00 ` Ilias Apalodimas
2023-05-12 0:05 ` AKASHI Takahiro
0 siblings, 1 reply; 5+ messages in thread
From: Ilias Apalodimas @ 2023-05-11 17:00 UTC (permalink / raw)
To: Heinrich Schuchardt; +Cc: trini, takahiro.akashi, u-boot
Hi Heinrich,
On Thu, 11 May 2023 at 19:56, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 5/11/23 18:40, Ilias Apalodimas wrote:
> > Tom reports that when building with clang we see this warning:
> > field guid within 'struct efi_hii_keyboard_layout' is less aligned than 'efi_guid_t' and is usually due to 'struct efi_hii_keyboard_layout' being packed, which can lead to unaligned accesses [-Wunaligned-access]
> >
> > This happens because 'struct efi_hii_keyboard_layout' is defined as
> > packed and thus has 1-byte alignment but efi_guid_t is a type that
> > requires greater alignment than that.
> >
> > However the EFI spec describes the EFI_GUID as
> > "128-bit buffer containing a unique identifier value.
> > Unless otherwise specified"
> >
> > So convert the efi_guid_t -> u8 b[16] here and skip the alignment
> > requirements. Since the struct is packed to begin with, it makes no
> > difference on the final memory layout.
> >
> > Suggested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> > Reported-by: Tom Rini <trini@konsulko.com>
> > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > ---
> > Changes since v1:
> > - Adjust the commit message and add a comment on why this happens
> >
> > include/efi_api.h | 28 +++++++++++++++++++++++++++-
> > 1 file changed, 27 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/efi_api.h b/include/efi_api.h
> > index 2fd0221c1c77..55a4c989fc7c 100644
> > --- a/include/efi_api.h
> > +++ b/include/efi_api.h
> > @@ -1170,7 +1170,33 @@ struct efi_key_descriptor {
> >
> > struct efi_hii_keyboard_layout {
> > u16 layout_length;
> > - efi_guid_t guid;
> > + /*
> > + * The EFI spec defines this as efi_guid_t.
> > + * clang and gcc both report alignment problems here.
> > + * clang with -Wunaligned-access
> > + * warning: field guid within 'struct efi_hii_keyboard_layout' is less
> > + * aligned than 'efi_guid_t' and is usually due to
> > + * 'struct efi_hii_keyboard_layout' being packed, which can lead to
> > + * unaligned accesses
> > + *
> > + * GCC with -Wpacked-not-aligned -Waddress-of-packed-member
> > + * 'efi_guid_t' offset 2 in 'struct efi_hii_keyboard_layout'
> > + * isn't aligned to 4
> > + *
> > + * Removing the alignment from efi_guid_t is not an option, since
> > + * it is also used in non-packed structs and that would break
> > + * calculations with offsetof
> > + *
> > + * This is the only place we get a report for. That happens because
> > + * all other declarations of efi_guid_t within a packed struct happens
> > + * to be 4-byte aligned. i.e a u32, a u64 a 2 * u16 or any combination
> > + * that ends up landing efi_guid_t on a 4byte boundary precedes.
> > + *
> > + * Replace this with a 1-byte aligned counterpart of b[16]. This is a
> > + * packed struct so the memory placement of efi_guid_t should not change
> > + *
> > + */
> > + u8 guid[16];
> > u32 layout_descriptor_string_offset;
> > u8 descriptor_count;
> > /* struct efi_key_descriptor descriptors[]; follows here */
> > --
> > 2.39.2
> >
>
> Thank you for investigating this in depth.
yw :)
>
> Commit messages should preferably limited to 75 characters per line, see
> scripts/checkpatch.pl. No need to resubmit.
Yea I know, I just prefered to keep the clang warning intact in the
commit message
Thanks
/Ilias
>
> Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] efi_loader: Fix warnings for unaligned accesses
2023-05-11 17:00 ` Ilias Apalodimas
@ 2023-05-12 0:05 ` AKASHI Takahiro
2023-05-12 5:28 ` Ilias Apalodimas
0 siblings, 1 reply; 5+ messages in thread
From: AKASHI Takahiro @ 2023-05-12 0:05 UTC (permalink / raw)
To: Ilias Apalodimas; +Cc: Heinrich Schuchardt, trini, u-boot
Hi Ilias,
On Thu, May 11, 2023 at 08:00:32PM +0300, Ilias Apalodimas wrote:
> Hi Heinrich,
>
> On Thu, 11 May 2023 at 19:56, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> >
> > On 5/11/23 18:40, Ilias Apalodimas wrote:
> > > Tom reports that when building with clang we see this warning:
> > > field guid within 'struct efi_hii_keyboard_layout' is less aligned than 'efi_guid_t' and is usually due to 'struct efi_hii_keyboard_layout' being packed, which can lead to unaligned accesses [-Wunaligned-access]
> > >
> > > This happens because 'struct efi_hii_keyboard_layout' is defined as
> > > packed and thus has 1-byte alignment but efi_guid_t is a type that
> > > requires greater alignment than that.
> > >
> > > However the EFI spec describes the EFI_GUID as
> > > "128-bit buffer containing a unique identifier value.
> > > Unless otherwise specified"
> > >
> > > So convert the efi_guid_t -> u8 b[16] here and skip the alignment
> > > requirements. Since the struct is packed to begin with, it makes no
> > > difference on the final memory layout.
> > >
> > > Suggested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> > > Reported-by: Tom Rini <trini@konsulko.com>
> > > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > > ---
> > > Changes since v1:
> > > - Adjust the commit message and add a comment on why this happens
> > >
> > > include/efi_api.h | 28 +++++++++++++++++++++++++++-
> > > 1 file changed, 27 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/include/efi_api.h b/include/efi_api.h
> > > index 2fd0221c1c77..55a4c989fc7c 100644
> > > --- a/include/efi_api.h
> > > +++ b/include/efi_api.h
> > > @@ -1170,7 +1170,33 @@ struct efi_key_descriptor {
> > >
> > > struct efi_hii_keyboard_layout {
> > > u16 layout_length;
> > > - efi_guid_t guid;
> > > + /*
> > > + * The EFI spec defines this as efi_guid_t.
> > > + * clang and gcc both report alignment problems here.
> > > + * clang with -Wunaligned-access
> > > + * warning: field guid within 'struct efi_hii_keyboard_layout' is less
> > > + * aligned than 'efi_guid_t' and is usually due to
> > > + * 'struct efi_hii_keyboard_layout' being packed, which can lead to
> > > + * unaligned accesses
> > > + *
> > > + * GCC with -Wpacked-not-aligned -Waddress-of-packed-member
> > > + * 'efi_guid_t' offset 2 in 'struct efi_hii_keyboard_layout'
> > > + * isn't aligned to 4
> > > + *
> > > + * Removing the alignment from efi_guid_t is not an option, since
> > > + * it is also used in non-packed structs and that would break
> > > + * calculations with offsetof
> > > + *
> > > + * This is the only place we get a report for. That happens because
> > > + * all other declarations of efi_guid_t within a packed struct happens
> > > + * to be 4-byte aligned. i.e a u32, a u64 a 2 * u16 or any combination
> > > + * that ends up landing efi_guid_t on a 4byte boundary precedes.
> > > + *
> > > + * Replace this with a 1-byte aligned counterpart of b[16]. This is a
> > > + * packed struct so the memory placement of efi_guid_t should not change
> > > + *
> > > + */
> > > + u8 guid[16];
I thought that you have agreed with my comment, saying keep "efi_guid_t" here.
https://lists.denx.de/pipermail/u-boot/2023-April/515831.html
-Takahiro Akashi
> > > u32 layout_descriptor_string_offset;
> > > u8 descriptor_count;
> > > /* struct efi_key_descriptor descriptors[]; follows here */
> > > --
> > > 2.39.2
> > >
> >
> > Thank you for investigating this in depth.
>
> yw :)
>
> >
> > Commit messages should preferably limited to 75 characters per line, see
> > scripts/checkpatch.pl. No need to resubmit.
>
> Yea I know, I just prefered to keep the clang warning intact in the
> commit message
>
> Thanks
> /Ilias
> >
> > Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] efi_loader: Fix warnings for unaligned accesses
2023-05-12 0:05 ` AKASHI Takahiro
@ 2023-05-12 5:28 ` Ilias Apalodimas
0 siblings, 0 replies; 5+ messages in thread
From: Ilias Apalodimas @ 2023-05-12 5:28 UTC (permalink / raw)
To: AKASHI Takahiro, Heinrich Schuchardt, trini, u-boot
Akashi-san,
On Fri, May 12, 2023 at 09:05:05AM +0900, AKASHI Takahiro wrote:
> Hi Ilias,
>
> On Thu, May 11, 2023 at 08:00:32PM +0300, Ilias Apalodimas wrote:
> > Hi Heinrich,
> >
> > On Thu, 11 May 2023 at 19:56, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
> > >
> > > On 5/11/23 18:40, Ilias Apalodimas wrote:
> > > > Tom reports that when building with clang we see this warning:
> > > > field guid within 'struct efi_hii_keyboard_layout' is less aligned than 'efi_guid_t' and is usually due to 'struct efi_hii_keyboard_layout' being packed, which can lead to unaligned accesses [-Wunaligned-access]
> > > >
> > > > This happens because 'struct efi_hii_keyboard_layout' is defined as
> > > > packed and thus has 1-byte alignment but efi_guid_t is a type that
> > > > requires greater alignment than that.
> > > >
> > > > However the EFI spec describes the EFI_GUID as
> > > > "128-bit buffer containing a unique identifier value.
> > > > Unless otherwise specified"
> > > >
> > > > So convert the efi_guid_t -> u8 b[16] here and skip the alignment
> > > > requirements. Since the struct is packed to begin with, it makes no
> > > > difference on the final memory layout.
> > > >
> > > > Suggested-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> > > > Reported-by: Tom Rini <trini@konsulko.com>
> > > > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > > > ---
> > > > Changes since v1:
> > > > - Adjust the commit message and add a comment on why this happens
> > > >
> > > > include/efi_api.h | 28 +++++++++++++++++++++++++++-
> > > > 1 file changed, 27 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/include/efi_api.h b/include/efi_api.h
> > > > index 2fd0221c1c77..55a4c989fc7c 100644
> > > > --- a/include/efi_api.h
> > > > +++ b/include/efi_api.h
> > > > @@ -1170,7 +1170,33 @@ struct efi_key_descriptor {
> > > >
> > > > struct efi_hii_keyboard_layout {
> > > > u16 layout_length;
> > > > - efi_guid_t guid;
> > > > + /*
> > > > + * The EFI spec defines this as efi_guid_t.
> > > > + * clang and gcc both report alignment problems here.
> > > > + * clang with -Wunaligned-access
> > > > + * warning: field guid within 'struct efi_hii_keyboard_layout' is less
> > > > + * aligned than 'efi_guid_t' and is usually due to
> > > > + * 'struct efi_hii_keyboard_layout' being packed, which can lead to
> > > > + * unaligned accesses
> > > > + *
> > > > + * GCC with -Wpacked-not-aligned -Waddress-of-packed-member
> > > > + * 'efi_guid_t' offset 2 in 'struct efi_hii_keyboard_layout'
> > > > + * isn't aligned to 4
> > > > + *
> > > > + * Removing the alignment from efi_guid_t is not an option, since
> > > > + * it is also used in non-packed structs and that would break
> > > > + * calculations with offsetof
> > > > + *
> > > > + * This is the only place we get a report for. That happens because
> > > > + * all other declarations of efi_guid_t within a packed struct happens
> > > > + * to be 4-byte aligned. i.e a u32, a u64 a 2 * u16 or any combination
> > > > + * that ends up landing efi_guid_t on a 4byte boundary precedes.
> > > > + *
> > > > + * Replace this with a 1-byte aligned counterpart of b[16]. This is a
> > > > + * packed struct so the memory placement of efi_guid_t should not change
> > > > + *
> > > > + */
> > > > + u8 guid[16];
>
> I thought that you have agreed with my comment, saying keep "efi_guid_t" here.
> https://lists.denx.de/pipermail/u-boot/2023-April/515831.html
I changed my mind since then since keeping efi_guid_t is indeed a problem,
so i won't silence that. The compiler might emit code with memory instructions
that don't tolerate misalignment
What we could do if we get more than one structs is add a efi_guid_ua_t
which wont have the alignment requirements and use that.
Regards
/Ilias
>
> -Takahiro Akashi
>
>
> > > > u32 layout_descriptor_string_offset;
> > > > u8 descriptor_count;
> > > > /* struct efi_key_descriptor descriptors[]; follows here */
> > > > --
> > > > 2.39.2
> > > >
> > >
> > > Thank you for investigating this in depth.
> >
> > yw :)
> >
> > >
> > > Commit messages should preferably limited to 75 characters per line, see
> > > scripts/checkpatch.pl. No need to resubmit.
> >
> > Yea I know, I just prefered to keep the clang warning intact in the
> > commit message
> >
> > Thanks
> > /Ilias
> > >
> > > Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-05-12 5:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-11 16:40 [PATCH v2] efi_loader: Fix warnings for unaligned accesses Ilias Apalodimas
2023-05-11 16:55 ` Heinrich Schuchardt
2023-05-11 17:00 ` Ilias Apalodimas
2023-05-12 0:05 ` AKASHI Takahiro
2023-05-12 5:28 ` Ilias Apalodimas
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox