fanotify: Avoid a couple of -Wflex-array-member-not-at-end warnings
Subsystems (3)
PATCH 1/1 fanotify: Avoid a couple of -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Modify FANOTIFY_INLINE_FH() macro, which defines a struct containing a flexible-array member in the middle (struct fanotify_fh::buf), to use struct_size_t() to pre-allocate space for both struct fanotify_fh and its flexible-array member. Replace the struct with a union and relocate the flexible structure (struct fanotify_fh) to the end.
See the memory layout of struct fanotify_fid_event before and after changes below.
pahole -C fanotify_fid_event fs/notify/fanotify/fanotify.o
BEFORE:
struct fanotify_fid_event {
struct fanotify_event fae; /* 0 48 */
__kernel_fsid_t fsid; /* 48 8 */
struct {
struct fanotify_fh object_fh; /* 56 4 */
unsigned char _inline_fh_buf[12]; /* 60 12 */
}; /* 56 16 */
/* size: 72, cachelines: 2, members: 3 */
/* last cacheline: 8 bytes */
};AFTER:
struct fanotify_fid_event {
struct fanotify_event fae; /* 0 48 */
__kernel_fsid_t fsid; /* 48 8 */
union {
unsigned char _inline_fh_buf[16]; /* 56 16 */
struct fanotify_fh object_fh __attribute__((__aligned__(1))); /* 56 4 */
} __attribute__((__aligned__(1))); /* 56 16 */
/* size: 72, cachelines: 2, members: 3 */
/* forced alignments: 1 */
/* last cacheline: 8 bytes */
} __attribute__((__aligned__(8)));So, with these changes, fix the following warnings:
fs/notify/fanotify/fanotify.h:317:28: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] fs/notify/fanotify/fanotify.h:289:28: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
--- fs/notify/fanotify/fanotify.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/notify/fanotify/fanotify.h +6 -6
@@ -275,12 +275,12 @@ static inline void fanotify_init_event(struct fanotify_event *event, event->pid =NULL ; } -#define FANOTIFY_INLINE_FH (name, size) \ -struct { \ - struct fanotify_fh name; \ -/* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \ -unsigned char _inline_fh_buf[size]; \ -} +#define FANOTIFY_INLINE_FH(name, size) \ +union { \ + /* Space for object_fh and object_fh.buf[] - access with fanotify_fh_buf() */ \ + unsigned char _inline_fh_buf[struct_size_t(struct fanotify_fh, buf, size)]; \ + struct fanotify_fh name; \ +} __packedstruct fanotify_fid_event {struct fanotify_event fae;
Signature · 1 line
2.43.0
Amir Goldstein
reviewerQuoted 28 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Modify
FANOTIFY_INLINE_FH()macro, which defines a struct containing a flexible-array member in the middle (structfanotify_fh::buf), to usestruct_size_t()to pre-allocate space for both structfanotify_fhand its flexible-array member. Replace the struct with a union and relocate the flexible structure (structfanotify_fh) to the end.See the memory layout of struct
fanotify_fid_eventbefore and after changes below.pahole -C
fanotify_fid_eventfs/notify/fanotify/fanotify.oBEFORE:
struct fanotify_fid_event { struct fanotify_event fae; /* 0 48 */ __kernel_fsid_t fsid; /* 48 8 */ struct { struct fanotify_fh object_fh; /* 56 4 */ unsigned char _inline_fh_buf[12]; /* 60 12 */ }; /* 56 16 */ /* size: 72, cachelines: 2, members: 3 */ /* last cacheline: 8 bytes */ };
Quoted 7 lines
AFTER:
struct fanotify_fid_event { struct fanotify_event fae; /* 0 48 */ __kernel_fsid_t fsid; /* 48 8 */ union { unsigned char _inline_fh_buf[16]; /* 56 16 */ struct fanotify_fh object_fh __attribute__((__aligned__(1))); /* 56 4 */
I'm not that familiar with pahole, but I find it surprising to see this member aligned(1), when struct fanotify_fh is defined as __aligned(4).
Quoted 25 lines
} __attribute__((__aligned__(1))); /* 56 16 */ /* size: 72, cachelines: 2, members: 3 */ /* forced alignments: 1 */ /* last cacheline: 8 bytes */ } __attribute__((__aligned__(8)));So, with these changes, fix the following warnings:
fs/notify/fanotify/fanotify.h:317:28: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]fs/notify/fanotify/fanotify.h:289:28: 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 <[email protected]>--- fs/notify/fanotify/fanotify.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)fs/notify/fanotify/fanotify.h +0
-0@@ -275,12 +275,12 @@ static inline void fanotify_init_event(struct fanotify_event *event, event->pid =NULL ; }
fs/notify/fanotify/fanotify.h static inline void fanotify_init_event(struct fanotify_event *event, · line 281
Quoted 10 lines
fs/notify/fanotify/fanotify.h +4
-6-#define FANOTIFY_INLINE_FH (name, size) \ -struct { \ - struct fanotify_fh name; \ -/* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \ -unsigned char _inline_fh_buf[size]; \ -} +#define FANOTIFY_INLINE_FH(name, size) \ +union { \ + /* Space for object_fh and object_fh.buf[] - access with fanotify_fh_buf() */ \ + unsigned char _inline_fh_buf[struct_size_t(struct fanotify_fh, buf, size)]; \
The name _inline_fh_buf is confusing in this setting better use bytes[] as in DEFINE_FLEX() or maybe even consider a generic helper DEFINE_FLEX_MEMBER() to use instead of FANOTIFY_INLINE_FH(), because this is not fanotify specific, except maybe for alignment (see below).
fs/notify/fanotify/fanotify.h static inline void fanotify_init_event(struct fanotify_event *event, · line 283
Quoted 2 lines
fs/notify/fanotify/fanotify.h +2
-0+struct fanotify_fh name; \ +} __packed
Why added __packed?
The fact that struct fanotify_fh is 4 bytes aligned could end up with less bytes reserved for the inline buffer if the union is not also 4 bytes aligned.
So maybe something like this:
#define FANOTIFY_INLINE_FH(name, size) \
DEFINE_FLEX_MEMBER(struct fanotify_fh, name, size) __aligned(4)
Thanks,
Amir.
Jan Kara
maintainerQuoted 38 lines · Amir Goldstein
Quoted 28 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Modify
FANOTIFY_INLINE_FH()macro, which defines a struct containing a flexible-array member in the middle (structfanotify_fh::buf), to usestruct_size_t()to pre-allocate space for both structfanotify_fhand its flexible-array member. Replace the struct with a union and relocate the flexible structure (structfanotify_fh) to the end.See the memory layout of struct
fanotify_fid_eventbefore and after changes below.pahole -C
fanotify_fid_eventfs/notify/fanotify/fanotify.oBEFORE:
struct fanotify_fid_event { struct fanotify_event fae; /* 0 48 */ __kernel_fsid_t fsid; /* 48 8 */ struct { struct fanotify_fh object_fh; /* 56 4 */ unsigned char _inline_fh_buf[12]; /* 60 12 */ }; /* 56 16 */ /* size: 72, cachelines: 2, members: 3 */ /* last cacheline: 8 bytes */ };Quoted 7 lines
AFTER:
struct fanotify_fid_event { struct fanotify_event fae; /* 0 48 */ __kernel_fsid_t fsid; /* 48 8 */ union { unsigned char _inline_fh_buf[16]; /* 56 16 */ struct fanotify_fh object_fh __attribute__((__aligned__(1))); /* 56 4 */
Quoted 2 lines
I'm not that familiar with pahole, but I find it surprising to see this member aligned(1), when struct
fanotify_fhis defined as__aligned(4).
Yeah.
Quoted 25 lines
Quoted 25 lines
} __attribute__((__aligned__(1))); /* 56 16 */ /* size: 72, cachelines: 2, members: 3 */ /* forced alignments: 1 */ /* last cacheline: 8 bytes */ } __attribute__((__aligned__(8)));So, with these changes, fix the following warnings:
fs/notify/fanotify/fanotify.h:317:28: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]fs/notify/fanotify/fanotify.h:289:28: 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 <[email protected]>--- fs/notify/fanotify/fanotify.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)fs/notify/fanotify/fanotify.h +0
-0@@ -275,12 +275,12 @@ static inline void fanotify_init_event(struct fanotify_event *event, event->pid =NULL ; }
Quoted 16 lines
fs/notify/fanotify/fanotify.h static inline void fanotify_init_event(struct fanotify_event *event, · line 281
Quoted 10 lines
fs/notify/fanotify/fanotify.h +4
-6-#define FANOTIFY_INLINE_FH (name, size) \ -struct { \ - struct fanotify_fh name; \ -/* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \ -unsigned char _inline_fh_buf[size]; \ -} +#define FANOTIFY_INLINE_FH(name, size) \ +union { \ + /* Space for object_fh and object_fh.buf[] - access with fanotify_fh_buf() */ \ + unsigned char _inline_fh_buf[struct_size_t(struct fanotify_fh, buf, size)]; \The name
_inline_fh_bufis confusing in this setting better use bytes[] as inDEFINE_FLEX()or maybe even consider a generic helperDEFINE_FLEX_MEMBER()to use instead ofFANOTIFY_INLINE_FH(), because this is not fanotify specific, except maybe for alignment (see below).
Yes, I guess a generic helper for this would be nice but if fanotify is the only place that plays these tricks, we can keep it specific for now. I agree naming the "space-buffer" field "bytes" would be less confusing.
Quoted 6 lines
fs/notify/fanotify/fanotify.h static inline void fanotify_init_event(struct fanotify_event *event, · line 283
Quoted 3 lines
fs/notify/fanotify/fanotify.h +2
-0+struct fanotify_fh name; \ +} __packedWhy added
__packed?
Quoted 7 lines
The fact that struct
fanotify_fhis 4 bytes aligned could end up with less bytes reserved for the inline buffer if the union is not also 4 bytes aligned.So maybe something like this:
#define
FANOTIFY_INLINE_FH(name, size)\
DEFINE_FLEX_MEMBER(struct fanotify_fh, name, size)__aligned(4)
I guess you need to provide the "member" information to DEFINE_FLEX_MEMBER() somewhere as well.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 39 lines · Jan Kara
Quoted 38 lines · Amir Goldstein
Quoted 35 lines · Gustavo A. R. Silva
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
Modify
FANOTIFY_INLINE_FH()macro, which defines a struct containing a flexible-array member in the middle (structfanotify_fh::buf), to usestruct_size_t()to pre-allocate space for both structfanotify_fhand its flexible-array member. Replace the struct with a union and relocate the flexible structure (structfanotify_fh) to the end.See the memory layout of struct
fanotify_fid_eventbefore and after changes below.pahole -C
fanotify_fid_eventfs/notify/fanotify/fanotify.oBEFORE:
struct fanotify_fid_event { struct fanotify_event fae; /* 0 48 */ __kernel_fsid_t fsid; /* 48 8 */ struct { struct fanotify_fh object_fh; /* 56 4 */ unsigned char _inline_fh_buf[12]; /* 60 12 */ }; /* 56 16 */ /* size: 72, cachelines: 2, members: 3 */ /* last cacheline: 8 bytes */ };AFTER:
struct fanotify_fid_event { struct fanotify_event fae; /* 0 48 */ __kernel_fsid_t fsid; /* 48 8 */ union { unsigned char _inline_fh_buf[16]; /* 56 16 */ struct fanotify_fh object_fh __attribute__((__aligned__(1))); /* 56 4 */
Quoted 4 lines
Quoted 2 lines
I'm not that familiar with pahole, but I find it surprising to see this member aligned(1), when struct
fanotify_fhis defined as__aligned(4).Yeah.
Yep, gotcha.
Quoted 43 lines
Quoted 26 lines
Quoted 26 lines
} __attribute__((__aligned__(1))); /* 56 16 */ /* size: 72, cachelines: 2, members: 3 */ /* forced alignments: 1 */ /* last cacheline: 8 bytes */ } __attribute__((__aligned__(8)));So, with these changes, fix the following warnings:
fs/notify/fanotify/fanotify.h:317:28: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]fs/notify/fanotify/fanotify.h:289:28: 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 <[email protected]>--- fs/notify/fanotify/fanotify.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)fs/notify/fanotify/fanotify.h +0
-0@@ -275,12 +275,12 @@ static inline void fanotify_init_event(struct fanotify_event *event, event->pid =NULL ; }Quoted 16 lines
fs/notify/fanotify/fanotify.h static inline void fanotify_init_event(struct fanotify_event *event, · line 281
Quoted 10 lines
fs/notify/fanotify/fanotify.h +4
-6-#define FANOTIFY_INLINE_FH (name, size) \ -struct { \ - struct fanotify_fh name; \ -/* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \ -unsigned char _inline_fh_buf[size]; \ -} +#define FANOTIFY_INLINE_FH(name, size) \ +union { \ + /* Space for object_fh and object_fh.buf[] - access with fanotify_fh_buf() */ \ + unsigned char _inline_fh_buf[struct_size_t(struct fanotify_fh, buf, size)]; \The name
_inline_fh_bufis confusing in this setting better use bytes[] as inDEFINE_FLEX()or maybe even consider a generic helperDEFINE_FLEX_MEMBER()to use instead ofFANOTIFY_INLINE_FH(), because this is not fanotify specific, except maybe for alignment (see below).
Quoted 3 lines
Yes, I guess a generic helper for this would be nice but if fanotify is the only place that plays these tricks, we can keep it specific for now. I agree naming the "space-buffer" field "bytes" would be less confusing.
I can send v2 with this change.
Quoted 10 lines
Quoted 10 lines
fs/notify/fanotify/fanotify.h static inline void fanotify_init_event(struct fanotify_event *event, · line 283
Quoted 4 lines
fs/notify/fanotify/fanotify.h +2
-0+struct fanotify_fh name; \ +} __packedWhy added
__packed?The fact that struct
fanotify_fhis 4 bytes aligned could end up with less bytes reserved for the inline buffer if the union is not also 4 bytes aligned.
Quoted 7 lines
Quoted 4 lines
So maybe something like this:
#define
FANOTIFY_INLINE_FH(name, size)\
DEFINE_FLEX_MEMBER(struct fanotify_fh, name, size)__aligned(4)I guess you need to provide the "member" information to
DEFINE_FLEX_MEMBER()somewhere as well.
Yeah, I can write something like that as well.
I'll come back with v2, shortly.
Thanks for the feedback, folks! :)
--
Gustavo
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Gustavo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on jack-fs/fsnotify]
[also build test WARNING on linus/master v6.15-rc5 next-20250507] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Gustavo-A-R-Silva/fanotify-Avoid-a-couple-of-Wflex-array-member-not-at-end-warnings/20250507-074110 base: https://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git fsnotify patch link: https://lore.kernel.org/r/aBqdlxlBtb9s7ydc%40kspp patch subject: [PATCH][next] fanotify: Avoid a couple of -Wflex-array-member-not-at-end warnings config: x86_64-defconfig (https://download.01.org/0day-ci/archive/20250508/[email protected]/config) compiler: gcc-11 (Debian 11.3.0-12) 11.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250508/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from fs/notify/fdinfo.c:17:
>> fs/notify/fanotify/fanotify.h:280:16: warning: alignment 1 of 'union <anonymous>' is less than 4 [-Wpacked-not-aligned]
280 | struct fanotify_fh name; \
| ^~~~~~~~~~~
fs/notify/fanotify/fanotify.h:287:9: note: in expansion of macro 'FANOTIFY_INLINE_FH'
287 | FANOTIFY_INLINE_FH(object_fh, FANOTIFY_INLINE_FH_LEN);
| ^~~~~~~~~~~~~~~~~~
>> fs/notify/fanotify/fanotify.h:280:16: warning: alignment 1 of 'union <anonymous>' is less than 4 [-Wpacked-not-aligned]
280 | struct fanotify_fh name; \
| ^~~~~~~~~~~
fs/notify/fanotify/fanotify.h:315:9: note: in expansion of macro 'FANOTIFY_INLINE_FH'
315 | FANOTIFY_INLINE_FH(object_fh, MAX_HANDLE_SZ);
| ^~~~~~~~~~~~~~~~~~vim +280 fs/notify/fanotify/fanotify.h
b8a6c3a2f0ae4d Amir Goldstein 2020-07-08 275
2c5069433a3adc Gabriel Krisman Bertazi 2021-10-25 276 #define FANOTIFY_INLINE_FH(name, size) \
e3725b8a2ecdf6 Gustavo A. R. Silva 2025-05-06 277 union { \
e3725b8a2ecdf6 Gustavo A. R. Silva 2025-05-06 278 /* Space for object_fh and object_fh.buf[] - access with fanotify_fh_buf() */ \
e3725b8a2ecdf6 Gustavo A. R. Silva 2025-05-06 279 unsigned char _inline_fh_buf[struct_size_t(struct fanotify_fh, buf, size)]; \
1758cd2e95d31b Alexey Dobriyan 2023-10-10 @280 struct fanotify_fh name; \
e3725b8a2ecdf6 Gustavo A. R. Silva 2025-05-06 281 } __packed2c5069433a3adc Gabriel Krisman Bertazi 2021-10-25 282
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki