Skip to contentSkip to conversation

hw/usb: Use __attribute__((packed)) vs __packed

PATCH In review Roque Arcudia Hernandez 5 participants 6 messages Nov 1, 2024 – Nov 21, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/usb: Use __attribute__((packed)) vs __packed

__packed is non standard and is not present in clang-cl. __attribute__((packed)) has the same semantics.

---
 include/hw/usb/dwc2-regs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/usb/dwc2-regs.h +1 -1
index 523b112c5e..b8b4266543 100644
@@ -838,7 +838,7 @@
 struct dwc2_dma_desc {
         uint32_t status;
         uint32_t buf;
-} __packed;
+} QEMU_PACKED;
 
 /* Host Mode DMA descriptor status quadlet */
 
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Roque Arcudia Hernandez

__packed is non standard and is not present in clang-cl. __attribute__((packed)) has the same semantics.

---
  include/hw/usb/dwc2-regs.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/usb/dwc2-regs.h +0 -0
index 523b112c5e..b8b4266543 100644

include/hw/usb/dwc2-regs.h line 844

Quoted 8 lines
include/hw/usb/dwc2-regs.h +1 -1
@@ -838,7 +838,7 @@
  struct dwc2_dma_desc {
          uint32_t status;
          uint32_t buf;
-} __packed;
+} QEMU_PACKED;
 
  /* Host Mode DMA descriptor status quadlet */
 
Reviewed-by on v1 1/1
#2 lore raw
In reply to Roque Arcudia Hernandez
Quoted 14 lines · Roque Arcudia Hernandez

__packed is non standard and is not present in clang-cl. __attribute__((packed)) has the same semantics.

---
  include/hw/usb/dwc2-regs.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/usb/dwc2-regs.h +0 -0
index 523b112c5e..b8b4266543 100644

include/hw/usb/dwc2-regs.h line 841

Quoted 5 lines
include/hw/usb/dwc2-regs.h +1 -1
@@ -838,7 +838,7 @@
  struct dwc2_dma_desc {
          uint32_t status;
          uint32_t buf;
-} __packed;
+} QEMU_PACKED;

Actually, the struct only consists of two 32-bit values, so I doubt that the "packed" is needed here at all. Maybe we could even simply remove it?

Thomas

#3 lore raw
Quoted 23 lines · Thomas Huth
Quoted 14 lines · Roque Arcudia Hernandez

__packed is non standard and is not present in clang-cl. __attribute__((packed)) has the same semantics.

---
  include/hw/usb/dwc2-regs.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/usb/dwc2-regs.h +0 -0
index 523b112c5e..b8b4266543 100644

include/hw/usb/dwc2-regs.h line 841

Quoted 5 lines
include/hw/usb/dwc2-regs.h +1 -1
@@ -838,7 +838,7 @@
   struct dwc2_dma_desc {
           uint32_t status;
           uint32_t buf;
-} __packed;
+} QEMU_PACKED;
Quoted 1 line

Actually, the struct only consists of two 32-bit values, so I doubt that the "packed" is needed here at all. Maybe we could even simply remove it?

To me it is important to mark structures as packed if it is important for them to have strict layout like in this case, even if de-facto it does not change the actual layout. It's just like an annotation saying this structure can be used on wire or somesuch.

/mjt

#4 lore raw
Quoted 24 lines · Michael Tokarev
Quoted 23 lines · Thomas Huth
Quoted 14 lines · Roque Arcudia Hernandez

__packed is non standard and is not present in clang-cl. __attribute__((packed)) has the same semantics.

---
  include/hw/usb/dwc2-regs.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/usb/dwc2-regs.h +0 -0
index 523b112c5e..b8b4266543 100644

include/hw/usb/dwc2-regs.h line 841

Quoted 5 lines
include/hw/usb/dwc2-regs.h +1 -1
@@ -838,7 +838,7 @@
   struct dwc2_dma_desc {
           uint32_t status;
           uint32_t buf;
-} __packed;
+} QEMU_PACKED;
Quoted 6 lines
Quoted 1 line

Actually, the struct only consists of two 32-bit values, so I doubt that the "packed" is needed here at all. Maybe we could even simply remove it?

To me it is important to mark structures as packed if it is important for them to have strict layout like in this case, even if de-facto it does not change the actual layout.  It's just like an annotation saying this structure can be used on wire or somesuch.

Well, it can have an impact on your binary, too. On architectures that don't support unaligned memory accesses, the compiler has to generate code that reads the values of packed structures in a more cumbersome way. So if you used "packed" though it's not really necessary (i.e. everything is naturally aligned, and you can be sure that the starting address is also properly aligned), the compiler generates worse code than necessary in that case.

Thomas

#5 lore raw
Quoted 25 lines · Thomas Huth
Quoted 24 lines · Michael Tokarev
Quoted 23 lines · Thomas Huth
Quoted 14 lines · Roque Arcudia Hernandez

__packed is non standard and is not present in clang-cl. __attribute__((packed)) has the same semantics.

---
  include/hw/usb/dwc2-regs.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/usb/dwc2-regs.h +0 -0
index 523b112c5e..b8b4266543 100644

include/hw/usb/dwc2-regs.h line 841

Quoted 5 lines
include/hw/usb/dwc2-regs.h +1 -1
@@ -838,7 +838,7 @@
   struct dwc2_dma_desc {
           uint32_t status;
           uint32_t buf;
-} __packed;
+} QEMU_PACKED;
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Actually, the struct only consists of two 32-bit values, so I doubt that the "packed" is needed here at all. Maybe we could even simply remove it?

To me it is important to mark structures as packed if it is important for them to have strict layout like in this case, even if de-facto it does not change the actual layout.  It's just like an annotation saying this structure can be used on wire or somesuch.

Well, it can have an impact on your binary, too. On architectures that don't support unaligned memory accesses, the compiler has to generate code that reads the values of packed structures in a more cumbersome way. So if you used "packed" though it's not really necessary (i.e. everything is naturally aligned, and you can be sure that the starting address is also properly aligned), the compiler generates worse code than necessary in that case.

Well, in this case the struct is downright unused. :) But we can't know if it is naturally aligned or not in guest memory, so it is advisable to keep the QEMU_PACKED.

Paolo