bpf: Avoid thousands of -Wflex-array-members-not-at-end warnings
PATCH 1/1 bpf: Avoid thousands of -Wflex-array-members-not-at-end warnings
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally.
struct bpf_prog_array is a flexible structure, this is a structure that contains a flexible-array member (struct bpf_prog_array_item items[];).
We create the new struct bpf_prog_array_hdr type, and use it to replace the object type causing trouble in struct bpf_empty_prog_array, namely struct bpf_prog_array hdr;
Also, once -fms-extensions is enabled, we can use transparent struct members in struct bpf_prog_array.
Notice that the newly created type does not contain the flex-array member items, which is the object causing the -Wfamnae warnings in struct bpf_empty_prog_array.
With these changes, fix the following warnings:
7659 ./include/linux/bpf.h:2369:31: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
--- include/linux/bpf-cgroup.h | 2 +- include/linux/bpf.h | 8 ++++++-- kernel/bpf/core.c | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-)
include/linux/bpf-cgroup.h +1 -1
@@ -184,7 +184,7 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,struct bpf_prog_array *array; array =rcu_access_pointer (cgrp->bpf.effective[type]); -return array != &bpf_empty_prog_array.hdr; +return (void *)array != (void *)&bpf_empty_prog_array.hdr; }/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
include/linux/bpf.h +6 -2
@@ -2360,13 +2360,17 @@ struct bpf_prog_array_item { }; }; -struct bpf_prog_array { +struct bpf_prog_array_hdr {struct rcu_head rcu; +}; + +struct bpf_prog_array { +struct bpf_prog_array_hdr ;struct bpf_prog_array_item items[]; };struct bpf_empty_prog_array { -struct bpf_prog_array hdr; +struct bpf_prog_array_hdr hdr;struct bpf_prog *null_prog; };
kernel/bpf/core.c +3 -3
@@ -2598,14 +2598,14 @@ struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)if (prog_cnt) p =kzalloc_flex (*p, items, prog_cnt +1 , flags);else - p = &bpf_empty_prog_array.hdr; + p = (void *)&bpf_empty_prog_array.hdr;return p; }void bpf_prog_array_free (struct bpf_prog_array *progs) { -if (!progs || progs == &bpf_empty_prog_array.hdr) +if (!progs || (void *)progs == (void *)&bpf_empty_prog_array.hdr)return ;kfree_rcu (progs, rcu); }
@@ -2626,7 +2626,7 @@ static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu)void bpf_prog_array_free_sleepable (struct bpf_prog_array *progs) { -if (!progs || progs == &bpf_empty_prog_array.hdr) +if (!progs || (void *)progs == (void *)&bpf_empty_prog_array.hdr)return ;call_rcu_tasks_trace (&progs->rcu, __bpf_prog_array_free_sleepable_cb); }
Signature · 1 line
2.43.0
Quoted 11 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.
struct
bpf_prog_arrayis a flexible structure, this is a structure that contains a flexible-array member (structbpf_prog_array_itemitems[];).We create the new struct
bpf_prog_array_hdrtype, and use it to replace the object type causing trouble in structbpf_empty_prog_array, namely structbpf_prog_arrayhdr;Also, once -fms-extensions is enabled, we can use transparent struct
Typo: "since" instead of "once".
Quoted 43 lines
members in struct
bpf_prog_array.Notice that the newly created type does not contain the flex-array member
items, which is the object causing the -Wfamnae warnings in structbpf_empty_prog_array.With these changes, fix the following warnings:
7659
./include/linux/bpf.h:2369:31: 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]>---
include/linux/bpf-cgroup.h | 2 +- include/linux/bpf.h | 8 ++++++-- kernel/bpf/core.c | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-)include/linux/bpf-cgroup.h +1
-1@@ -184,7 +184,7 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,struct bpf_prog_array *array; array =rcu_access_pointer (cgrp->bpf.effective[type]); -return array != &bpf_empty_prog_array.hdr; +return (void *)array != (void *)&bpf_empty_prog_array.hdr; }/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */ include/linux/bpf.h +3
-1@@ -2360,13 +2360,17 @@ struct bpf_prog_array_item { }; }; -struct bpf_prog_array { +struct bpf_prog_array_hdr {struct rcu_head rcu; +}; +
include/linux/bpf.h struct bpf_prog_array_item { · line 2375
Quoted 10 lines
include/linux/bpf.h +3
-1+struct bpf_prog_array { +struct bpf_prog_array_hdr ;struct bpf_prog_array_item items[]; };struct bpf_empty_prog_array { -struct bpf_prog_array hdr; +struct bpf_prog_array_hdr hdr;struct bpf_prog *null_prog; };
AFAICT, this struct exists entirely to populate a single element of "items" in a global variable. (I only see "null_prog" used during the initializer.) None of this is needed; globals will be correctly sized with an array initializer of a FAM. Totally untested:
struct bpf_prog_array bpf_empty_prog_array = {
.items = { NULL, },
};kernel/bpf/core.c struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags) · line 2601
Quoted 11 lines
kernel/bpf/core.c +1
-1@@ -2598,14 +2598,14 @@ struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)if (prog_cnt) p =kzalloc_flex (*p, items, prog_cnt +1 , flags);else - p = &bpf_empty_prog_array.hdr; + p = (void *)&bpf_empty_prog_array.hdr;
Then there also shouldn't be any need for the casting (or the "hdr" addressing):
p = &bpf_empty_prog_array;
Etc
-Kees
Quoted 20 lines
kernel/bpf/core.c +2
-2return p; }void bpf_prog_array_free (struct bpf_prog_array *progs) { -if (!progs || progs == &bpf_empty_prog_array.hdr) +if (!progs || (void *)progs == (void *)&bpf_empty_prog_array.hdr)return ;kfree_rcu (progs, rcu); }@@ -2626,7 +2626,7 @@ static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu)void bpf_prog_array_free_sleepable (struct bpf_prog_array *progs) { -if (!progs || progs == &bpf_empty_prog_array.hdr) +if (!progs || (void *)progs == (void *)&bpf_empty_prog_array.hdr)return ;call_rcu_tasks_trace (&progs->rcu, __bpf_prog_array_free_sleepable_cb); }
Signature · 1 line
Kees Cook
Quoted 9 lines · Kees Cook
Quoted 6 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.
struct
bpf_prog_arrayis a flexible structure, this is a structure that contains a flexible-array member (structbpf_prog_array_itemitems[];).
Quoted 7 lines
Quoted 5 lines
We create the new struct
bpf_prog_array_hdrtype, and use it to replace the object type causing trouble in structbpf_empty_prog_array, namely structbpf_prog_arrayhdr;Also, once -fms-extensions is enabled, we can use transparent struct
Typo: "since" instead of "once".
Ah yes, thanks!
Quoted 57 lines
Quoted 44 lines
members in struct
bpf_prog_array.Notice that the newly created type does not contain the flex-array member
items, which is the object causing the -Wfamnae warnings in structbpf_empty_prog_array.With these changes, fix the following warnings:
7659
./include/linux/bpf.h:2369:31: 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]>---
include/linux/bpf-cgroup.h | 2 +- include/linux/bpf.h | 8 ++++++-- kernel/bpf/core.c | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-)include/linux/bpf-cgroup.h +1
-1@@ -184,7 +184,7 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,struct bpf_prog_array *array; array =rcu_access_pointer (cgrp->bpf.effective[type]); -return array != &bpf_empty_prog_array.hdr; +return (void *)array != (void *)&bpf_empty_prog_array.hdr; }/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */ include/linux/bpf.h +3
-1@@ -2360,13 +2360,17 @@ struct bpf_prog_array_item { }; }; -struct bpf_prog_array { +struct bpf_prog_array_hdr {struct rcu_head rcu; +}; +include/linux/bpf.h struct bpf_prog_array_item { · line 2375
Quoted 10 lines
include/linux/bpf.h +3
-1+struct bpf_prog_array { +struct bpf_prog_array_hdr ;struct bpf_prog_array_item items[]; };struct bpf_empty_prog_array { -struct bpf_prog_array hdr; +struct bpf_prog_array_hdr hdr;struct bpf_prog *null_prog; };AFAICT, this struct exists entirely to populate a single element of "items" in a global variable. (I only see "
null_prog" used during the initializer.) None of this is needed; globals will be correctly sized with an array initializer of a FAM. Totally untested:
Quoted 3 lines
struct bpf_prog_array bpf_empty_prog_array = {
.items = { NULL, },
};Okay, in this case the patch would look as follows:
include/linux/bpf-cgroup.h +1 -1
@@ -184,7 +184,7 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,struct bpf_prog_array *array; array =rcu_access_pointer (cgrp->bpf.effective[type]); -return array != &bpf_empty_prog_array.hdr; +return array != &bpf_empty_prog_array; }/* Wrappers for __cgroup_bpf_run_filter_skb() guarded by cgroup_bpf_enabled. */
include/linux/bpf.h +1 -6
@@ -2365,18 +2365,13 @@ struct bpf_prog_array {struct bpf_prog_array_item items[]; }; -struct bpf_empty_prog_array { -struct bpf_prog_array hdr; -struct bpf_prog *null_prog; -}; -/* to avoid allocating empty bpf_prog_array for cgroups that * don't have bpf program attached use one global 'bpf_empty_prog_array' * It will not be modified the caller of bpf_prog_array_alloc() * (since caller requested prog_cnt == 0) * that pointer should be 'freed' by bpf_prog_array_free() */ -extern struct bpf_empty_prog_array bpf_empty_prog_array; +extern struct bpf_prog_array bpf_empty_prog_array;struct bpf_prog_array *bpf_prog_array_alloc (u32 prog_cnt,gfp_t flags);void bpf_prog_array_free (struct bpf_prog_array *progs);
kernel/bpf/core.c +7 -5
@@ -2613,8 +2613,10 @@ static struct bpf_prog_dummy { }, }; -struct bpf_empty_prog_array bpf_empty_prog_array = { - .null_prog =NULL , +struct bpf_prog_array bpf_empty_prog_array = { + .items = { + { .prog =NULL }, + }, };EXPORT_SYMBOL (bpf_empty_prog_array);
@@ -2625,14 +2627,14 @@ struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags)if (prog_cnt) p =kzalloc_flex (*p, items, prog_cnt +1 , flags);else - p = &bpf_empty_prog_array.hdr; + p = &bpf_empty_prog_array;return p; }void bpf_prog_array_free (struct bpf_prog_array *progs) { -if (!progs || progs == &bpf_empty_prog_array.hdr) +if (!progs || progs == &bpf_empty_prog_array)return ;kfree_rcu (progs, rcu); }
@@ -2653,7 +2655,7 @@ static void __bpf_prog_array_free_sleepable_cb(struct rcu_head *rcu)void bpf_prog_array_free_sleepable (struct bpf_prog_array *progs) { -if (!progs || progs == &bpf_empty_prog_array.hdr) +if (!progs || progs == &bpf_empty_prog_array)return ;call_rcu_tasks_trace (&progs->rcu, __bpf_prog_array_free_sleepable_cb); }
Could maintainers give us feedback or comment on this, please? :)
Thanks!
-Gustavo