public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: "Gustavo A. R. Silva" <gustavoars@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org, Kees Cook <kees@kernel.org>
Subject: Re: [PATCH v2][next] bpf: Avoid thousands of -Wflex-array-members-not-at-end warnings
Date: Tue, 31 Mar 2026 12:07:48 +0100	[thread overview]
Message-ID: <37169136-2fa2-4756-b3cc-6049766c8319@gmail.com> (raw)
In-Reply-To: <acr7Whmn0br3xeBP@kspp>

On 3/30/26 11:38 PM, Gustavo A. R. Silva wrote:
> Apparently, struct bpf_empty_prog_array exists entirely to populate a
> single element of "items" in a global variable. "null_prog" is only
> used during the initializer.
> 
> None of this is needed; globals will be correctly sized with an array
> initializer of a flexible-array member.
> 
> So, remove struct bpf_empty_prog_array and adjust the rest of the code,
> accordingly.
> 
> 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 <gustavoars@kernel.org>
> ---
Great cleanup! It was quite confusing how we used null_prog to fill the 
fist element of the flexible array. I wonder why was it done like that.

Acked-by: Mykyta Yatsenko <yatsenko@meta.com>
> Changes in v2:
>   - Remove struct bpf_empty_prog_array. (Kees)
> 
> v1:
>   -Link: https://lore.kernel.org/linux-hardening/aaZr2A1UPJq33127@kspp/
> 
>   include/linux/bpf-cgroup.h |  2 +-
>   include/linux/bpf.h        |  7 +------
>   kernel/bpf/core.c          | 12 +++++++-----
>   3 files changed, 9 insertions(+), 12 deletions(-)
> 
> diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> index 2f535331f926..b2e79c2b41d5 100644
> --- a/include/linux/bpf-cgroup.h
> +++ b/include/linux/bpf-cgroup.h
> @@ -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. */
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 05b34a6355b0..4f5b9e85a20c 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -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);
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 67eb12b637a5..ca39d2e690b9 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -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);
>   }


  reply	other threads:[~2026-03-31 11:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-30 22:38 [PATCH v2][next] bpf: Avoid thousands of -Wflex-array-members-not-at-end warnings Gustavo A. R. Silva
2026-03-31 11:07 ` Mykyta Yatsenko [this message]
2026-04-06  2:44 ` Alexei Starovoitov
2026-04-06  3:27   ` Kees Cook
2026-04-06 23:24     ` Alexei Starovoitov
2026-04-07  0:22       ` Gustavo A. R. Silva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=37169136-2fa2-4756-b3cc-6049766c8319@gmail.com \
    --to=mykyta.yatsenko5@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=gustavoars@kernel.org \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox