Skip to contentSkip to conversation

stddef: Introduce struct_group_tagged_attr() helper macro

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Mar 18, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 stddef: Introduce struct_group_tagged_attr() helper macro

We need a new struct_group() helper that allows for both having the struct be tagged, and specifying struct attributes like __packed or __align(x).

This new helper will initially be used to address -Wflex-array-member-not-at-end warnings, where a tagged struct is used to separate the flexible-array member from the rest of the members in the flexible structure [1]. There are some scenarios in which those members need to be packed, as well.

So, struct_group_tagged_attr() is introduced for this.

---
 include/linux/stddef.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/linux/stddef.h +20 -0
index 929d67710cc5..919df9453257 100644
@@ -80,6 +80,26 @@ enum {
 #define struct_group_tagged(TAG, NAME, MEMBERS...) \
 	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
 
+/**
+ * struct_group_tagged_attr() - Create a struct_group with a reusable
+ * tag and trailing attributes.
+ *
+ * @TAG: The tag name for the named sub-struct
+ * @NAME: The identifier name of the mirrored sub-struct
+ * @ATTRS: Any struct attributes to apply
+ * @MEMBERS: The member declarations for the mirrored structs
+ *
+ * Used to create an anonymous union of two structs with identical
+ * layout and size: one anonymous and one named. The former can be
+ * used normally without sub-struct naming, and the latter can be
+ * used to reason about the start, end, and size of the group of
+ * struct members. Includes struct tag argument for the named copy,
+ * so the specified layout can be reused later. Also includes
+ * structure attributes argument.
+ */
+#define struct_group_tagged_attr(TAG, NAME, ATTRS, MEMBERS...) \
+	__struct_group(TAG, NAME, ATTRS, MEMBERS)
+
 /**
  * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
  *
Signature · 1 line
2.34.1
#1 lore raw
Quoted 35 lines · Gustavo A. R. Silva

We need a new struct_group() helper that allows for both having the struct be tagged, and specifying struct attributes like __packed or __align(x).

This new helper will initially be used to address -Wflex-array-member-not-at-end warnings, where a tagged struct is used to separate the flexible-array member from the rest of the members in the flexible structure [1]. There are some scenarios in which those members need to be packed, as well.

So, struct_group_tagged_attr() is introduced for this.

---
 include/linux/stddef.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/linux/stddef.h +9 -0
index 929d67710cc5..919df9453257 100644
@@ -80,6 +80,26 @@ enum {
 #define struct_group_tagged(TAG, NAME, MEMBERS...) \
 	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
 
+/**
+ * struct_group_tagged_attr() - Create a struct_group with a reusable
+ * tag and trailing attributes.
+ *
+ * @TAG: The tag name for the named sub-struct
+ * @NAME: The identifier name of the mirrored sub-struct
+ * @ATTRS: Any struct attributes to apply
+ * @MEMBERS: The member declarations for the mirrored structs
+ *

include/linux/stddef.h enum { · line 101

Quoted 10 lines
include/linux/stddef.h +10 -0
+ * Used to create an anonymous union of two structs with identical
+ * layout and size: one anonymous and one named. The former can be
+ * used normally without sub-struct naming, and the latter can be
+ * used to reason about the start, end, and size of the group of
+ * struct members. Includes struct tag argument for the named copy,
+ * so the specified layout can be reused later. Also includes
+ * structure attributes argument.
+ */
+#define struct_group_tagged_attr(TAG, NAME, ATTRS, MEMBERS...) \
+	__struct_group(TAG, NAME, ATTRS, MEMBERS)

This is the same as __struct_group() only with a longer name? Why not just use __struct_group() directly?

Signature · 1 line
Kees Cook
#2 lore raw
Quoted 41 lines · Kees Cook
Quoted 40 lines · Gustavo A. R. Silva

We need a new struct_group() helper that allows for both having the struct be tagged, and specifying struct attributes like __packed or __align(x).

This new helper will initially be used to address -Wflex-array-member-not-at-end warnings, where a tagged struct is used to separate the flexible-array member from the rest of the members in the flexible structure [1]. There are some scenarios in which those members need to be packed, as well.

So, struct_group_tagged_attr() is introduced for this.

---
  include/linux/stddef.h | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
include/linux/stddef.h +14 -0
index 929d67710cc5..919df9453257 100644
@@ -80,6 +80,26 @@ enum {
  #define struct_group_tagged(TAG, NAME, MEMBERS...) \
  	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
 
+/**
+ * struct_group_tagged_attr() - Create a struct_group with a reusable
+ * tag and trailing attributes.
+ *
+ * @TAG: The tag name for the named sub-struct
+ * @NAME: The identifier name of the mirrored sub-struct
+ * @ATTRS: Any struct attributes to apply
+ * @MEMBERS: The member declarations for the mirrored structs
+ *
+ * Used to create an anonymous union of two structs with identical
+ * layout and size: one anonymous and one named. The former can be
+ * used normally without sub-struct naming, and the latter can be
+ * used to reason about the start, end, and size of the group of
+ * struct members. Includes struct tag argument for the named copy,
Quoted 9 lines

include/linux/stddef.h enum { · line 101

Quoted 5 lines
include/linux/stddef.h +5 -0
+ * so the specified layout can be reused later. Also includes
+ * structure attributes argument.
+ */
+#define struct_group_tagged_attr(TAG, NAME, ATTRS, MEMBERS...) \
+	__struct_group(TAG, NAME, ATTRS, MEMBERS)

This is the same as __struct_group() only with a longer name? Why not just use __struct_group() directly?

Mmmh, the rest of the helpers in the struct_group() family fall in this same category, can we use __struct_group() indistinctly?

--
Gustavo