Skip to contentSkip to conversation

UAPI: net/sched: Avoid -Wflex-array-member-not-at-end warning

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Aug 9, 2024 – Aug 13, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 UAPI: net/sched: Avoid -Wflex-array-member-not-at-end warning

-Wflex-array-member-not-at-end has been introduced in GCC-14, and we are getting ready to enable it, globally.

So, in order to avoid ending up with a flexible-array member in the middle of multiple other structs, we use the __struct_group() helper to create a new tagged struct tc_u32_sel_hdr. This structure groups together all the members of the flexible struct tc_u32_sel except the flexible array.

As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct member currently causing trouble from struct tc_u32_sel to struct tc_u32_sel_hdr.

This approach avoids having to implement struct tc_u32_sel_hdr as a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future.

So, with these changes, fix the following warning:
drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h:245:27: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

---
Changes in v2:
- Update subject line. (Jakub)

 .../chelsio/cxgb4/cxgb4_tc_u32_parse.h        |  2 +-
 include/uapi/linux/pkt_cls.h                  | 23 +++++++++++--------
 2 files changed, 14 insertions(+), 11 deletions(-)
drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h +1 -1
index 9050568a034c..64663112cad8 100644
@@ -242,7 +242,7 @@ struct cxgb4_next_header {
 	 * field's value to jump to next header such as IHL field
 	 * in IPv4 header.
 	 */
-	struct tc_u32_sel sel;
+	struct tc_u32_sel_hdr sel;
 	struct tc_u32_key key;
 	/* location of jump to make */
 	const struct cxgb4_match_field *jump;
include/uapi/linux/pkt_cls.h +13 -10
index d36d9cdf0c00..2c32080416b5 100644
@@ -246,16 +246,19 @@ struct tc_u32_key {
 };
 
 struct tc_u32_sel {
-	unsigned char		flags;
-	unsigned char		offshift;
-	unsigned char		nkeys;
-
-	__be16			offmask;
-	__u16			off;
-	short			offoff;
-
-	short			hoff;
-	__be32			hmask;
+	/* New members MUST be added within the __struct_group() macro below. */
+	__struct_group(tc_u32_sel_hdr, hdr, /* no attrs */,
+		unsigned char		flags;
+		unsigned char		offshift;
+		unsigned char		nkeys;
+
+		__be16			offmask;
+		__u16			off;
+		short			offoff;
+
+		short			hoff;
+		__be32			hmask;
+	);
 	struct tc_u32_key	keys[];
 };
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 19 lines · On Fri, 9 Aug 2024 09:27:51 -0600 "Gustavo A. R. Silva"…

-Wflex-array-member-not-at-end has been introduced in GCC-14, and we are getting ready to enable it, globally.

So, in order to avoid ending up with a flexible-array member in the middle of multiple other structs, we use the __struct_group() helper to create a new tagged struct tc_u32_sel_hdr. This structure groups together all the members of the flexible struct tc_u32_sel except the flexible array.

As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct member currently causing trouble from struct tc_u32_sel to struct tc_u32_sel_hdr.

This approach avoids having to implement struct tc_u32_sel_hdr as a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future.

Quoted 4 lines

So, with these changes, fix the following warning:
drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h:245:27: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

Tested this with iproute2-next and clang and no problem.

The patch might be better split into two parts?

maintainer
#2 lore raw
Quoted 3 lines · On Fri, 9 Aug 2024 09:42:03 -0700 Stephen Hemminger wrote:

Tested this with iproute2-next and clang and no problem.

The patch might be better split into two parts?

Fair point, a small two-patch series would be ideal.

Signature · 1 line
pw-bot: cr