* [PATCH][next] scsi: fc: Avoid -Wflex-array-member-not-at-end warnings
@ 2025-08-12 14:13 Gustavo A. R. Silva
2025-08-26 1:53 ` Martin K. Petersen
0 siblings, 1 reply; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-08-12 14:13 UTC (permalink / raw)
To: James Smart, Dick Kennedy, James E.J. Bottomley,
Martin K. Petersen, Hannes Reinecke
Cc: linux-scsi, linux-kernel, Gustavo A. R. Silva, linux-hardening
-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 fc_df_desc_fpin_reg_hdr`.
This structure groups together all the members of the flexible
`struct fc_df_desc_fpin_reg` 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 members currently causing
trouble from `struct fc_df_desc_fpin_reg` to `struct
fc_df_desc_fpin_reg_hdr`.
We also want to ensure that in case new members need to be added to the
flexible structure, they are always included within the newly created
tagged struct. For this, we use `_Static_assert()`. This ensures that
the memory layout for both the flexible structure and the new tagged
struct is the same after any changes.
This approach avoids having to implement `struct fc_df_desc_fpin_reg_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.
The above is also done for flexible `struct fc_els_rdf`.
So, with these changes, fix the following warnings:
drivers/scsi/lpfc/lpfc_hw4.h:4936:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
drivers/scsi/lpfc/lpfc_hw4.h:4942:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
drivers/scsi/lpfc/lpfc_hw4.h:4947:41: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
drivers/scsi/lpfc/lpfc_hw4.h | 4 ++--
include/uapi/scsi/fc/fc_els.h | 40 ++++++++++++++++++++++++-----------
2 files changed, 30 insertions(+), 14 deletions(-)
diff --git a/drivers/scsi/lpfc/lpfc_hw4.h b/drivers/scsi/lpfc/lpfc_hw4.h
index dcd7204d4eec..e319858c88ba 100644
--- a/drivers/scsi/lpfc/lpfc_hw4.h
+++ b/drivers/scsi/lpfc/lpfc_hw4.h
@@ -4909,13 +4909,13 @@ struct send_frame_wqe {
#define ELS_RDF_REG_TAG_CNT 4
struct lpfc_els_rdf_reg_desc {
- struct fc_df_desc_fpin_reg reg_desc; /* descriptor header */
+ struct fc_df_desc_fpin_reg_hdr reg_desc; /* descriptor header */
__be32 desc_tags[ELS_RDF_REG_TAG_CNT];
/* tags in reg_desc */
};
struct lpfc_els_rdf_req {
- struct fc_els_rdf rdf; /* hdr up to descriptors */
+ struct fc_els_rdf_hdr rdf; /* hdr up to descriptors */
struct lpfc_els_rdf_reg_desc reg_d1; /* 1st descriptor */
};
diff --git a/include/uapi/scsi/fc/fc_els.h b/include/uapi/scsi/fc/fc_els.h
index 16782c360de3..81b9f87943f4 100644
--- a/include/uapi/scsi/fc/fc_els.h
+++ b/include/uapi/scsi/fc/fc_els.h
@@ -11,6 +11,12 @@
#include <linux/types.h>
#include <asm/byteorder.h>
+#ifdef __KERNEL__
+#include <linux/stddef.h> /* for offsetof */
+#else
+#include <stddef.h> /* for offsetof */
+#endif
+
/*
* Fibre Channel Switch - Enhanced Link Services definitions.
* From T11 FC-LS Rev 1.2 June 7, 2005.
@@ -1109,12 +1115,15 @@ struct fc_els_fpin {
/* Diagnostic Function Descriptor - FPIN Registration */
struct fc_df_desc_fpin_reg {
- __be32 desc_tag; /* FPIN Registration (0x00030001) */
- __be32 desc_len; /* Length of Descriptor (in bytes).
- * Size of descriptor excluding
- * desc_tag and desc_len fields.
- */
- __be32 count; /* Number of desc_tags elements */
+ /* New members MUST be added within the __struct_group() macro below. */
+ __struct_group(fc_df_desc_fpin_reg_hdr, hdr, /* no attrs */,
+ __be32 desc_tag; /* FPIN Registration (0x00030001) */
+ __be32 desc_len; /* Length of Descriptor (in bytes).
+ * Size of descriptor excluding
+ * desc_tag and desc_len fields.
+ */
+ __be32 count; /* Number of desc_tags elements */
+ );
__be32 desc_tags[]; /* Array of Descriptor Tags.
* Each tag indicates a function
* supported by the N_Port (request)
@@ -1124,19 +1133,26 @@ struct fc_df_desc_fpin_reg {
* See ELS_FN_DTAG_xxx for tag values.
*/
};
+_Static_assert(offsetof(struct fc_df_desc_fpin_reg, desc_tags) == sizeof(struct fc_df_desc_fpin_reg_hdr),
+ "struct member likely outside of __struct_group()");
/*
* ELS_RDF - Register Diagnostic Functions
*/
struct fc_els_rdf {
- __u8 fpin_cmd; /* command (0x19) */
- __u8 fpin_zero[3]; /* specified as zero - part of cmd */
- __be32 desc_len; /* Length of Descriptor List (in bytes).
- * Size of ELS excluding fpin_cmd,
- * fpin_zero and desc_len fields.
- */
+ /* New members MUST be added within the __struct_group() macro below. */
+ __struct_group(fc_els_rdf_hdr, hdr, /* no attrs */,
+ __u8 fpin_cmd; /* command (0x19) */
+ __u8 fpin_zero[3]; /* specified as zero - part of cmd */
+ __be32 desc_len; /* Length of Descriptor List (in bytes).
+ * Size of ELS excluding fpin_cmd,
+ * fpin_zero and desc_len fields.
+ */
+ );
struct fc_tlv_desc desc[]; /* Descriptor list */
};
+_Static_assert(offsetof(struct fc_els_rdf, desc) == sizeof(struct fc_els_rdf_hdr),
+ "struct member likely outside of __struct_group()");
/*
* ELS RDF LS_ACC Response.
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH][next] scsi: fc: Avoid -Wflex-array-member-not-at-end warnings
2025-08-12 14:13 [PATCH][next] scsi: fc: Avoid -Wflex-array-member-not-at-end warnings Gustavo A. R. Silva
@ 2025-08-26 1:53 ` Martin K. Petersen
2025-08-26 23:13 ` Justin Tee
0 siblings, 1 reply; 4+ messages in thread
From: Martin K. Petersen @ 2025-08-26 1:53 UTC (permalink / raw)
To: James Smart, Justin Tee
Cc: Gustavo A. R. Silva, Hannes Reinecke, linux-scsi, linux-kernel,
linux-hardening
James & Justin,
Please help review Gustavo's patch.
I also noticed that Justin is not listed as lpfc maintainer. Please
submit patch. Thanks!
> -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 fc_df_desc_fpin_reg_hdr`.
> This structure groups together all the members of the flexible
> `struct fc_df_desc_fpin_reg` 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 members currently causing
> trouble from `struct fc_df_desc_fpin_reg` to `struct
> fc_df_desc_fpin_reg_hdr`.
>
> We also want to ensure that in case new members need to be added to the
> flexible structure, they are always included within the newly created
> tagged struct. For this, we use `_Static_assert()`. This ensures that
> the memory layout for both the flexible structure and the new tagged
> struct is the same after any changes.
>
> This approach avoids having to implement `struct fc_df_desc_fpin_reg_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.
>
> The above is also done for flexible `struct fc_els_rdf`.
>
> So, with these changes, fix the following warnings:
> drivers/scsi/lpfc/lpfc_hw4.h:4936:41: warning: structure containing a
> flexible array member is not at the end of another structure
> [-Wflex-array-member-not-at-end]
> drivers/scsi/lpfc/lpfc_hw4.h:4942:41: warning: structure containing a
> flexible array member is not at the end of another structure
> [-Wflex-array-member-not-at-end]
> drivers/scsi/lpfc/lpfc_hw4.h:4947:41: warning: structure containing a
> flexible array member is not at the end of another structure
> [-Wflex-array-member-not-at-end]
>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
> drivers/scsi/lpfc/lpfc_hw4.h | 4 ++--
> include/uapi/scsi/fc/fc_els.h | 40 ++++++++++++++++++++++++-----------
> 2 files changed, 30 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/scsi/lpfc/lpfc_hw4.h b/drivers/scsi/lpfc/lpfc_hw4.h
> index dcd7204d4eec..e319858c88ba 100644
> --- a/drivers/scsi/lpfc/lpfc_hw4.h
> +++ b/drivers/scsi/lpfc/lpfc_hw4.h
> @@ -4909,13 +4909,13 @@ struct send_frame_wqe {
>
> #define ELS_RDF_REG_TAG_CNT 4
> struct lpfc_els_rdf_reg_desc {
> - struct fc_df_desc_fpin_reg reg_desc; /* descriptor header */
> + struct fc_df_desc_fpin_reg_hdr reg_desc; /* descriptor header */
> __be32 desc_tags[ELS_RDF_REG_TAG_CNT];
> /* tags in reg_desc */
> };
>
> struct lpfc_els_rdf_req {
> - struct fc_els_rdf rdf; /* hdr up to descriptors */
> + struct fc_els_rdf_hdr rdf; /* hdr up to descriptors */
> struct lpfc_els_rdf_reg_desc reg_d1; /* 1st descriptor */
> };
>
> diff --git a/include/uapi/scsi/fc/fc_els.h b/include/uapi/scsi/fc/fc_els.h
> index 16782c360de3..81b9f87943f4 100644
> --- a/include/uapi/scsi/fc/fc_els.h
> +++ b/include/uapi/scsi/fc/fc_els.h
> @@ -11,6 +11,12 @@
> #include <linux/types.h>
> #include <asm/byteorder.h>
>
> +#ifdef __KERNEL__
> +#include <linux/stddef.h> /* for offsetof */
> +#else
> +#include <stddef.h> /* for offsetof */
> +#endif
> +
> /*
> * Fibre Channel Switch - Enhanced Link Services definitions.
> * From T11 FC-LS Rev 1.2 June 7, 2005.
> @@ -1109,12 +1115,15 @@ struct fc_els_fpin {
>
> /* Diagnostic Function Descriptor - FPIN Registration */
> struct fc_df_desc_fpin_reg {
> - __be32 desc_tag; /* FPIN Registration (0x00030001) */
> - __be32 desc_len; /* Length of Descriptor (in bytes).
> - * Size of descriptor excluding
> - * desc_tag and desc_len fields.
> - */
> - __be32 count; /* Number of desc_tags elements */
> + /* New members MUST be added within the __struct_group() macro below. */
> + __struct_group(fc_df_desc_fpin_reg_hdr, hdr, /* no attrs */,
> + __be32 desc_tag; /* FPIN Registration (0x00030001) */
> + __be32 desc_len; /* Length of Descriptor (in bytes).
> + * Size of descriptor excluding
> + * desc_tag and desc_len fields.
> + */
> + __be32 count; /* Number of desc_tags elements */
> + );
> __be32 desc_tags[]; /* Array of Descriptor Tags.
> * Each tag indicates a function
> * supported by the N_Port (request)
> @@ -1124,19 +1133,26 @@ struct fc_df_desc_fpin_reg {
> * See ELS_FN_DTAG_xxx for tag values.
> */
> };
> +_Static_assert(offsetof(struct fc_df_desc_fpin_reg, desc_tags) == sizeof(struct fc_df_desc_fpin_reg_hdr),
> + "struct member likely outside of __struct_group()");
>
> /*
> * ELS_RDF - Register Diagnostic Functions
> */
> struct fc_els_rdf {
> - __u8 fpin_cmd; /* command (0x19) */
> - __u8 fpin_zero[3]; /* specified as zero - part of cmd */
> - __be32 desc_len; /* Length of Descriptor List (in bytes).
> - * Size of ELS excluding fpin_cmd,
> - * fpin_zero and desc_len fields.
> - */
> + /* New members MUST be added within the __struct_group() macro below. */
> + __struct_group(fc_els_rdf_hdr, hdr, /* no attrs */,
> + __u8 fpin_cmd; /* command (0x19) */
> + __u8 fpin_zero[3]; /* specified as zero - part of cmd */
> + __be32 desc_len; /* Length of Descriptor List (in bytes).
> + * Size of ELS excluding fpin_cmd,
> + * fpin_zero and desc_len fields.
> + */
> + );
> struct fc_tlv_desc desc[]; /* Descriptor list */
> };
> +_Static_assert(offsetof(struct fc_els_rdf, desc) == sizeof(struct fc_els_rdf_hdr),
> + "struct member likely outside of __struct_group()");
>
> /*
> * ELS RDF LS_ACC Response.
--
Martin K. Petersen
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH][next] scsi: fc: Avoid -Wflex-array-member-not-at-end warnings
2025-08-26 1:53 ` Martin K. Petersen
@ 2025-08-26 23:13 ` Justin Tee
2025-08-27 5:54 ` Gustavo A. R. Silva
0 siblings, 1 reply; 4+ messages in thread
From: Justin Tee @ 2025-08-26 23:13 UTC (permalink / raw)
To: Martin K. Petersen, Gustavo A. R. Silva
Cc: James Smart, Justin Tee, Hannes Reinecke, linux-scsi,
linux-kernel, linux-hardening
Hi Martin and Gustavo,
Regarding the maintainers file, yes Broadcom will push a patch to update soon.
Regarding Gustavo’s patch, I think we should also update the
assignment of rdf.desc_len in lpfc_els.c like below.
diff --git a/drivers/scsi/lpfc/lpfc_els.c b/drivers/scsi/lpfc/lpfc_els.c
index fca81e0c7c2e..432761fb49de 100644
--- a/drivers/scsi/lpfc/lpfc_els.c
+++ b/drivers/scsi/lpfc/lpfc_els.c
@@ -3762,7 +3762,7 @@ lpfc_issue_els_rdf(struct lpfc_vport *vport,
uint8_t retry)
memset(prdf, 0, cmdsize);
prdf->rdf.fpin_cmd = ELS_RDF;
prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
- sizeof(struct fc_els_rdf));
+ sizeof(struct fc_els_rdf_hdr));
prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
Thanks,
Justin Tee
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH][next] scsi: fc: Avoid -Wflex-array-member-not-at-end warnings
2025-08-26 23:13 ` Justin Tee
@ 2025-08-27 5:54 ` Gustavo A. R. Silva
0 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2025-08-27 5:54 UTC (permalink / raw)
To: Justin Tee, Martin K. Petersen, Gustavo A. R. Silva
Cc: James Smart, Justin Tee, Hannes Reinecke, linux-scsi,
linux-kernel, linux-hardening
On 27/08/25 01:13, Justin Tee wrote:
> Hi Martin and Gustavo,
>
> Regarding the maintainers file, yes Broadcom will push a patch to update soon.
>
> Regarding Gustavo’s patch, I think we should also update the
> assignment of rdf.desc_len in lpfc_els.c like below.
>
> diff --git a/drivers/scsi/lpfc/lpfc_els.c b/drivers/scsi/lpfc/lpfc_els.c
> index fca81e0c7c2e..432761fb49de 100644
> --- a/drivers/scsi/lpfc/lpfc_els.c
> +++ b/drivers/scsi/lpfc/lpfc_els.c
> @@ -3762,7 +3762,7 @@ lpfc_issue_els_rdf(struct lpfc_vport *vport,
> uint8_t retry)
> memset(prdf, 0, cmdsize);
> prdf->rdf.fpin_cmd = ELS_RDF;
> prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
> - sizeof(struct fc_els_rdf));
> + sizeof(struct fc_els_rdf_hdr));
Good catch! :)
Thanks
-Gustavo
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-08-27 5:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-12 14:13 [PATCH][next] scsi: fc: Avoid -Wflex-array-member-not-at-end warnings Gustavo A. R. Silva
2025-08-26 1:53 ` Martin K. Petersen
2025-08-26 23:13 ` Justin Tee
2025-08-27 5:54 ` Gustavo A. R. Silva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).