[PATCH][next] ext4: Avoid -Wflex-array-member-not-at-end warning

Gustavo A. R. Silva posted 1 patch 8 months, 3 weeks ago
fs/ext4/mballoc.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
[PATCH][next] ext4: Avoid -Wflex-array-member-not-at-end warning
Posted by Gustavo A. R. Silva 8 months, 3 weeks ago
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are
getting ready to enable it, globally.

Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
a flexible structure where the size of the flexible-array member
is known at compile-time, and refactor the rest of the code,
accordingly.

So, with these changes, fix the following warning:

fs/ext4/mballoc.c:3041:40: 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>
---
 fs/ext4/mballoc.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 0d523e9fb3d5..f88424c28194 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -3037,10 +3037,8 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
 	unsigned char blocksize_bits = min_t(unsigned char,
 					     sb->s_blocksize_bits,
 					     EXT4_MAX_BLOCK_LOG_SIZE);
-	struct sg {
-		struct ext4_group_info info;
-		ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
-	} sg;
+	DEFINE_RAW_FLEX(struct ext4_group_info, sg, bb_counters,
+			EXT4_MAX_BLOCK_LOG_SIZE + 2);
 
 	group--;
 	if (group == 0)
@@ -3048,7 +3046,7 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
 			      " 2^0   2^1   2^2   2^3   2^4   2^5   2^6  "
 			      " 2^7   2^8   2^9   2^10  2^11  2^12  2^13  ]\n");
 
-	i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
+	i = (blocksize_bits + 2) * sizeof(sg->bb_counters[0]) +
 		sizeof(struct ext4_group_info);
 
 	grinfo = ext4_get_group_info(sb, group);
@@ -3068,14 +3066,14 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
 	 * We care only about free space counters in the group info and
 	 * these are safe to access even after the buddy has been unloaded
 	 */
-	memcpy(&sg, grinfo, i);
-	seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
-			sg.info.bb_fragments, sg.info.bb_first_free);
+	memcpy(sg, grinfo, i);
+	seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg->bb_free,
+			sg->bb_fragments, sg->bb_first_free);
 	for (i = 0; i <= 13; i++)
 		seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
-				sg.info.bb_counters[i] : 0);
+				sg->bb_counters[i] : 0);
 	seq_puts(seq, " ]");
-	if (EXT4_MB_GRP_BBITMAP_CORRUPT(&sg.info))
+	if (EXT4_MB_GRP_BBITMAP_CORRUPT(sg))
 		seq_puts(seq, " Block bitmap corrupted!");
 	seq_putc(seq, '\n');
 	return 0;
-- 
2.43.0
Re: [PATCH][next] ext4: Avoid -Wflex-array-member-not-at-end warning
Posted by Theodore Ts'o 7 months, 3 weeks ago
On Wed, 26 Mar 2025 16:55:51 -0600, Gustavo A. R. Silva wrote:
> -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
> getting ready to enable it, globally.
> 
> Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
> a flexible structure where the size of the flexible-array member
> is known at compile-time, and refactor the rest of the code,
> accordingly.
> 
> [...]

Applied, thanks!

[1/1] ext4: Avoid -Wflex-array-member-not-at-end warning
      commit: 7e50bbb134aba1df0854f171b596b3a42d35605a

(Apologies for sending this late; I've been dealing with a family
medical emergency.  In any case, the patch landed in v6.16-rc2.)

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>
Re: [PATCH][next] ext4: Avoid -Wflex-array-member-not-at-end warning
Posted by Kees Cook 8 months, 1 week ago
On Wed, Mar 26, 2025 at 04:55:51PM -0600, Gustavo A. R. Silva wrote:
> -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
> getting ready to enable it, globally.
> 
> Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
> a flexible structure where the size of the flexible-array member
> is known at compile-time, and refactor the rest of the code,
> accordingly.
> 
> So, with these changes, fix the following warning:
> 
> fs/ext4/mballoc.c:3041:40: 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>
> ---
>  fs/ext4/mballoc.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 0d523e9fb3d5..f88424c28194 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -3037,10 +3037,8 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
>  	unsigned char blocksize_bits = min_t(unsigned char,
>  					     sb->s_blocksize_bits,
>  					     EXT4_MAX_BLOCK_LOG_SIZE);
> -	struct sg {
> -		struct ext4_group_info info;
> -		ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
> -	} sg;
> +	DEFINE_RAW_FLEX(struct ext4_group_info, sg, bb_counters,
> +			EXT4_MAX_BLOCK_LOG_SIZE + 2);

Yup, struct ext4_group_info::bb_counters is ext4_grpblk_t, so everything
matches up.

>  
>  	group--;
>  	if (group == 0)
> @@ -3048,7 +3046,7 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
>  			      " 2^0   2^1   2^2   2^3   2^4   2^5   2^6  "
>  			      " 2^7   2^8   2^9   2^10  2^11  2^12  2^13  ]\n");
>  
> -	i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
> +	i = (blocksize_bits + 2) * sizeof(sg->bb_counters[0]) +
>  		sizeof(struct ext4_group_info);
>  
>  	grinfo = ext4_get_group_info(sb, group);
> @@ -3068,14 +3066,14 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
>  	 * We care only about free space counters in the group info and
>  	 * these are safe to access even after the buddy has been unloaded
>  	 */
> -	memcpy(&sg, grinfo, i);
> -	seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
> -			sg.info.bb_fragments, sg.info.bb_first_free);
> +	memcpy(sg, grinfo, i);
> +	seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg->bb_free,
> +			sg->bb_fragments, sg->bb_first_free);
>  	for (i = 0; i <= 13; i++)
>  		seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
> -				sg.info.bb_counters[i] : 0);
> +				sg->bb_counters[i] : 0);
>  	seq_puts(seq, " ]");
> -	if (EXT4_MB_GRP_BBITMAP_CORRUPT(&sg.info))
> +	if (EXT4_MB_GRP_BBITMAP_CORRUPT(sg))
>  		seq_puts(seq, " Block bitmap corrupted!");
>  	seq_putc(seq, '\n');
>  	return 0;

Replacements looks good.

Reviewed-by: Kees Cook <kees@kernel.org>

-- 
Kees Cook