Skip to contentSkip to conversation

gcc-9: silence 'address-of-packed-member' warning

PATCH New Andy Shevchenko 3 participants 5 messages Nov 29, 2019 – Jan 10, 2020 2 messages not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gcc-9: silence 'address-of-packed-member' warning

GCC 9.x starts complaining about potential misalignment of the pointer to the array (in this case alignment=2) in the packed (alignment=1) structures.

Repeating Linus' Torvalds commit 6f303d60534c in the Linux kernel.

Original commit message:

We already did this for clang, but now gcc has that warning too. Yes, yes, the address may be unaligned. And that's kind of the point.

This in particular hides the warnings like

drivers/usb/gadget/composite.c:545:23: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
545 | collect_langs(sp, s->wData);

drivers/usb/gadget/composite.c:550:24: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
550 | collect_langs(sp, s->wData);

drivers/usb/gadget/composite.c:555:25: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
555 | collect_langs(sp, s->wData);

---
 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +2 -1
index 7485bc2594..a0469f6a9c 100644
@@ -672,11 +672,12 @@ endif
 endif
 
 KBUILD_CFLAGS += $(call cc-option,-Wno-format-nonliteral)
+KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
+
 ifeq ($(cc-name),clang)
 KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
-KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
 KBUILD_CFLAGS += $(call cc-option, -fcatch-undefined-behavior)
 endif
 
Signature · 1 line
2.24.0
#1 lore raw

PATCH 1/1 gcc-9: silence 'address-of-packed-member' warning

Quoted 33 lines · Andy Shevchenko

GCC 9.x starts complaining about potential misalignment of the pointer to the array (in this case alignment=2) in the packed (alignment=1) structures.

Repeating Linus' Torvalds commit 6f303d60534c in the Linux kernel.

Original commit message:

We already did this for clang, but now gcc has that warning too. Yes, yes, the address may be unaligned. And that's kind of the point.

This in particular hides the warnings like

drivers/usb/gadget/composite.c:545:23: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
545 | collect_langs(sp, s->wData);

drivers/usb/gadget/composite.c:550:24: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
550 | collect_langs(sp, s->wData);

drivers/usb/gadget/composite.c:555:25: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
555 | collect_langs(sp, s->wData);

---
 Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Makefile +0 -0
index 7485bc2594..a0469f6a9c 100644
@@ -672,11 +672,12 @@ endif
 endif
 

Makefile endif · line 682

Quoted 10 lines
Makefile +2 -1
 KBUILD_CFLAGS += $(call cc-option,-Wno-format-nonliteral)
+KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
+
 ifeq ($(cc-name),clang)
 KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
-KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
 KBUILD_CFLAGS += $(call cc-option, -fcatch-undefined-behavior)
 endif

Ooh, I wish I had thought to look at the kernel a while ago. I very much like this idea and need to run a test to see how much space we re-grain with this patch and reverting the handful of reworks that might not make as much long term sense to do. Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191129/00bd60fa/attachment.sig>
#4 lore raw

PATCH 1/1 gcc-9: silence 'address-of-packed-member' warning

In reply to a message not in the archive

[bringing this back to the list, seems like I accidentally hit the single reply button before]

Quoted 23 lines · Andy Shevchenko
Quoted 22 lines · Simon Goldschmidt

Andy Shevchenko <[email protected]> schrieb am Fr., 29. Nov. 2019, 18:48:

Quoted 18 lines

GCC 9.x starts complaining about potential misalignment of the pointer to the array (in this case alignment=2) in the packed (alignment=1) structures.

Repeating Linus' Torvalds commit 6f303d60534c in the Linux kernel.

Original commit message:

We already did this for clang, but now gcc has that warning too. Yes, yes, the address may be unaligned. And that's kind of the point.

This in particular hides the warnings like

drivers/usb/gadget/composite.c:545:23: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
545 | collect_langs(sp, s->wData);

Quoted 5 lines
Quoted 3 lines

Is it really ok to hide this warning? This address is used for an u16 pointer later, so it needs to be aligned. How do we ensure that wData is correctly aligned?

Why should it be?

Because this code is working on a packed struct, which may be unaligned. If it's not, why not remove packing on struct usb_string_descriptor instead of silencing this warning altogether?

Quoted 1 line

It worked before it will work after.

Who guarantees this struct is aligned/will stay aligned in the future?

Most of the platforms I know nowadays do work with unaligned access but are slow, so I think having the compiler warn here is good, no?

Quoted 7 lines
Quoted 5 lines

I took a different approach and fixed the called function to use byte access:

https://patchwork.ozlabs.org/patch/1199138/

So, that commit can be reverted then.

I admit this commit increases code size, but I'm not convinced that it's not necessary. If the access is always aligned, let's remove structure packing instead of reducing compiler warnings. (I still think most compiler warnings are good, not bad.)

Regards,
Simon

#5 lore raw

PATCH 1/1 gcc-9: silence 'address-of-packed-member' warning

Quoted 49 lines · Simon Goldschmidt

[bringing this back to the list, seems like I accidentally hit the single reply button before]

Quoted 23 lines · Andy Shevchenko
Quoted 22 lines · Simon Goldschmidt

Andy Shevchenko <[email protected]> schrieb am Fr., 29. Nov. 2019, 18:48:

Quoted 18 lines

GCC 9.x starts complaining about potential misalignment of the pointer to the array (in this case alignment=2) in the packed (alignment=1) structures.

Repeating Linus' Torvalds commit 6f303d60534c in the Linux kernel.

Original commit message:

We already did this for clang, but now gcc has that warning too. Yes, yes, the address may be unaligned. And that's kind of the point.

This in particular hides the warnings like

drivers/usb/gadget/composite.c:545:23: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
545 | collect_langs(sp, s->wData);

Quoted 5 lines
Quoted 3 lines

Is it really ok to hide this warning? This address is used for an u16 pointer later, so it needs to be aligned. How do we ensure that wData is correctly aligned?

Why should it be?

Because this code is working on a packed struct, which may be unaligned. If it's not, why not remove packing on struct usb_string_descriptor instead of silencing this warning altogether?

Quoted 1 line

It worked before it will work after.

Who guarantees this struct is aligned/will stay aligned in the future?

Most of the platforms I know nowadays do work with unaligned access but are slow, so I think having the compiler warn here is good, no?

Quoted 4 lines
Quoted 4 lines

I took a different approach and fixed the called function to use byte access:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

https://patchwork.ozlabs.org/patch/1199138/

So, that commit can be reverted then.

I admit this commit increases code size, but I'm not convinced that it's not necessary. If the access is always aligned, let's remove structure packing instead of reducing compiler warnings. (I still think most compiler warnings are good, not bad.)

In general terms, I agree that warnings can be helpful and good to know when they exist. Perhaps it's worth pursing this in the kernel community to move this from and always-disable to something enabled at a non-default W= number?

In more specific terms, we care so very much about binary size, especially when it's not clearly a user-visible performance hit. I do not disagree with "X is technically wrong and should be fixed, now that we see the warning showing it". I also don't disagree with "with some performance profiling we can see that unaligned access $here is a problem". But I do disagree with speculation on future CPUs not supporting unaligned access or that it may be a performance problem. We don't control the former and can investigate the latter.

I also don't disagree with "as custodian for X I'm going to fix this problem in my area.".

Thanks all!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191129/f1041ee8/attachment.sig>
#6 lore raw

PATCH 1/1 gcc-9: silence 'address-of-packed-member' warning

In reply to Andy Shevchenko
Quoted 15 lines · Andy Shevchenko

GCC 9.x starts complaining about potential misalignment of the pointer to the array (in this case alignment=2) in the packed (alignment=1) structures.

Repeating Linus' Torvalds commit 6f303d60534c in the Linux kernel.

Original commit message:

We already did this for clang, but now gcc has that warning too. Yes, yes, the address may be unaligned. And that's kind of the point.

This in particular hides the warnings like

drivers/usb/gadget/composite.c:545:23: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
545 | collect_langs(sp, s->wData);

Quoted 7 lines

drivers/usb/gadget/composite.c:550:24: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
550 | collect_langs(sp, s->wData);

drivers/usb/gadget/composite.c:555:25: warning: taking address of packed member of ‘struct usb_string_descriptor’ may result in an unaligned pointer value [-Waddress-of-packed-member]
555 | collect_langs(sp, s->wData);

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200110/2f6b62ec/attachment.sig>