diff mbox series

[next] net/smc: Use static_assert() to check struct sizes

Message ID ZrVBuiqFHAORpFxE@cute (mailing list archive)
State Accepted
Commit 0a3e6939d4b33d68bce89477b9db01d68b744749
Delegated to: Netdev Maintainers
Headers show
Series [next] net/smc: Use static_assert() to check struct sizes | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 29 this patch: 29
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 29 this patch: 29
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 29 this patch: 29
netdev/checkpatch warning CHECK: Please use a blank line after function/struct/union/enum declarations WARNING: line length of 108 exceeds 80 columns WARNING: line length of 116 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-08-10--18-00 (tests: 707)

Commit Message

Gustavo A. R. Silva Aug. 8, 2024, 10:07 p.m. UTC
Commit 9748dbc9f265 ("net/smc: Avoid -Wflex-array-member-not-at-end
warnings") introduced tagged `struct smc_clc_v2_extension_fixed` and
`struct smc_clc_smcd_v2_extension_fixed`. We want to ensure that when
new members need to be added to the flexible structures, they are
always included within these tagged structs.

So, we use `static_assert()` to ensure that the memory layout for
both the flexible structure and the tagged struct is the same after
any changes.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 net/smc/smc_clc.h | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Jan Karcher Aug. 11, 2024, 6:12 a.m. UTC | #1
On 09/08/2024 00:07, Gustavo A. R. Silva wrote:
> Commit 9748dbc9f265 ("net/smc: Avoid -Wflex-array-member-not-at-end
> warnings") introduced tagged `struct smc_clc_v2_extension_fixed` and
> `struct smc_clc_smcd_v2_extension_fixed`. We want to ensure that when
> new members need to be added to the flexible structures, they are
> always included within these tagged structs.
> 
> So, we use `static_assert()` to ensure that the memory layout for
> both the flexible structure and the tagged struct is the same after
> any changes.

Hi Gustavo,

good catch. By reviewing it, it makes sense to me. Please let me give it 
a run with our test-suite before adding a r-b.

Thanks
- J

> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>   net/smc/smc_clc.h | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/net/smc/smc_clc.h b/net/smc/smc_clc.h
> index 467effb50cd6..5625fda2960b 100644
> --- a/net/smc/smc_clc.h
> +++ b/net/smc/smc_clc.h
> @@ -145,6 +145,8 @@ struct smc_clc_v2_extension {
>   	);
>   	u8 user_eids[][SMC_MAX_EID_LEN];
>   };
> +static_assert(offsetof(struct smc_clc_v2_extension, user_eids) == sizeof(struct smc_clc_v2_extension_fixed),
> +	      "struct member likely outside of struct_group_tagged()");
>   
>   struct smc_clc_msg_proposal_prefix {	/* prefix part of clc proposal message*/
>   	__be32 outgoing_subnet;	/* subnet mask */
> @@ -169,6 +171,8 @@ struct smc_clc_smcd_v2_extension {
>   	);
>   	struct smc_clc_smcd_gid_chid gidchid[];
>   };
> +static_assert(offsetof(struct smc_clc_smcd_v2_extension, gidchid) == sizeof(struct smc_clc_smcd_v2_extension_fixed),
> +	      "struct member likely outside of struct_group_tagged()");
>   
>   struct smc_clc_msg_proposal {	/* clc proposal message sent by Linux */
>   	struct smc_clc_msg_hdr hdr;
Jan Karcher Aug. 12, 2024, 6:29 a.m. UTC | #2
On 09/08/2024 00:07, Gustavo A. R. Silva wrote:
> Commit 9748dbc9f265 ("net/smc: Avoid -Wflex-array-member-not-at-end
> warnings") introduced tagged `struct smc_clc_v2_extension_fixed` and
> `struct smc_clc_smcd_v2_extension_fixed`. We want to ensure that when
> new members need to be added to the flexible structures, they are
> always included within these tagged structs.
> 
> So, we use `static_assert()` to ensure that the memory layout for
> both the flexible structure and the tagged struct is the same after
> any changes.

Read up what the macro does. I like it.
Compile tested on s390.

Reviewed-by: Jan Karcher <jaka@linux.ibm.com>

> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>   net/smc/smc_clc.h | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/net/smc/smc_clc.h b/net/smc/smc_clc.h
> index 467effb50cd6..5625fda2960b 100644
> --- a/net/smc/smc_clc.h
> +++ b/net/smc/smc_clc.h
> @@ -145,6 +145,8 @@ struct smc_clc_v2_extension {
>   	);
>   	u8 user_eids[][SMC_MAX_EID_LEN];
>   };
> +static_assert(offsetof(struct smc_clc_v2_extension, user_eids) == sizeof(struct smc_clc_v2_extension_fixed),
> +	      "struct member likely outside of struct_group_tagged()");
>   
>   struct smc_clc_msg_proposal_prefix {	/* prefix part of clc proposal message*/
>   	__be32 outgoing_subnet;	/* subnet mask */
> @@ -169,6 +171,8 @@ struct smc_clc_smcd_v2_extension {
>   	);
>   	struct smc_clc_smcd_gid_chid gidchid[];
>   };
> +static_assert(offsetof(struct smc_clc_smcd_v2_extension, gidchid) == sizeof(struct smc_clc_smcd_v2_extension_fixed),
> +	      "struct member likely outside of struct_group_tagged()");
>   
>   struct smc_clc_msg_proposal {	/* clc proposal message sent by Linux */
>   	struct smc_clc_msg_hdr hdr;
patchwork-bot+netdevbpf@kernel.org Aug. 13, 2024, 2:10 a.m. UTC | #3
Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Thu, 8 Aug 2024 16:07:54 -0600 you wrote:
> Commit 9748dbc9f265 ("net/smc: Avoid -Wflex-array-member-not-at-end
> warnings") introduced tagged `struct smc_clc_v2_extension_fixed` and
> `struct smc_clc_smcd_v2_extension_fixed`. We want to ensure that when
> new members need to be added to the flexible structures, they are
> always included within these tagged structs.
> 
> So, we use `static_assert()` to ensure that the memory layout for
> both the flexible structure and the tagged struct is the same after
> any changes.
> 
> [...]

Here is the summary with links:
  - [next] net/smc: Use static_assert() to check struct sizes
    https://git.kernel.org/netdev/net-next/c/0a3e6939d4b3

You are awesome, thank you!
diff mbox series

Patch

diff --git a/net/smc/smc_clc.h b/net/smc/smc_clc.h
index 467effb50cd6..5625fda2960b 100644
--- a/net/smc/smc_clc.h
+++ b/net/smc/smc_clc.h
@@ -145,6 +145,8 @@  struct smc_clc_v2_extension {
 	);
 	u8 user_eids[][SMC_MAX_EID_LEN];
 };
+static_assert(offsetof(struct smc_clc_v2_extension, user_eids) == sizeof(struct smc_clc_v2_extension_fixed),
+	      "struct member likely outside of struct_group_tagged()");
 
 struct smc_clc_msg_proposal_prefix {	/* prefix part of clc proposal message*/
 	__be32 outgoing_subnet;	/* subnet mask */
@@ -169,6 +171,8 @@  struct smc_clc_smcd_v2_extension {
 	);
 	struct smc_clc_smcd_gid_chid gidchid[];
 };
+static_assert(offsetof(struct smc_clc_smcd_v2_extension, gidchid) == sizeof(struct smc_clc_smcd_v2_extension_fixed),
+	      "struct member likely outside of struct_group_tagged()");
 
 struct smc_clc_msg_proposal {	/* clc proposal message sent by Linux */
 	struct smc_clc_msg_hdr hdr;