diff mbox series

bpf: fix incorrect initialization of bpf_ctx_convert_map

Message ID 20201026210355.3885283-1-arnd@kernel.org (mailing list archive)
State New, archived
Delegated to: BPF
Headers show
Series bpf: fix incorrect initialization of bpf_ctx_convert_map | expand

Commit Message

Arnd Bergmann Oct. 26, 2020, 9:03 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de>

gcc -Wextra points out that a field may get overridden in some
configurations such as x86 allmodconfig, when the next index after the one
that has been assigned last already had a value, in this case for index
BPF_PROG_TYPE_SK_LOOKUP, which comes after BPF_PROG_TYPE_LSM in the list:

kernel/bpf/btf.c:4225:2: warning: initialized field overwritten [-Woverride-init]
 4225 |  0, /* avoid empty array */
      |  ^
kernel/bpf/btf.c:4225:2: note: (near initialization for 'bpf_ctx_convert_map[30]')

Move the zero-initializer first instead. This avoids the warning since
nothing else uses index 0, and the last element does not have to be zero.

Fixes: e9ddbb7707ff ("bpf: Introduce SK_LOOKUP program type with a dedicated attach point")
Fixes: 4c80c7bc583a ("bpf: Fix build in minimal configurations, again")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andrii Nakryiko Oct. 26, 2020, 10:32 p.m. UTC | #1
On Mon, Oct 26, 2020 at 2:04 PM Arnd Bergmann <arnd@kernel.org> wrote:
>
> From: Arnd Bergmann <arnd@arndb.de>
>
> gcc -Wextra points out that a field may get overridden in some
> configurations such as x86 allmodconfig, when the next index after the one
> that has been assigned last already had a value, in this case for index
> BPF_PROG_TYPE_SK_LOOKUP, which comes after BPF_PROG_TYPE_LSM in the list:
>
> kernel/bpf/btf.c:4225:2: warning: initialized field overwritten [-Woverride-init]
>  4225 |  0, /* avoid empty array */
>       |  ^
> kernel/bpf/btf.c:4225:2: note: (near initialization for 'bpf_ctx_convert_map[30]')
>
> Move the zero-initializer first instead. This avoids the warning since
> nothing else uses index 0, and the last element does not have to be zero.

Wouldn't it be cleaner and more explicit to add __MAX_BPF_PROG_TYPE to
enum bpf_prog_type in include/uapi/linux/bpf.h, similarly to how we do
it with enum bpf_attach_type? Then just specify the size of the array
here explicitly? Unless we are trying to save a few bytes for more
minimal configurations where some BPF program types are not used (but
still defined in an enum)?


>
> Fixes: e9ddbb7707ff ("bpf: Introduce SK_LOOKUP program type with a dedicated attach point")
> Fixes: 4c80c7bc583a ("bpf: Fix build in minimal configurations, again")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  kernel/bpf/btf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index ed7d02e8bc93..2a4a4aeeaac1 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -4218,11 +4218,11 @@ enum {
>         __ctx_convert_unused, /* to avoid empty enum in extreme .config */
>  };
>  static u8 bpf_ctx_convert_map[] = {
> +       [0] = 0, /* avoid empty array */
>  #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
>         [_id] = __ctx_convert##_id,
>  #include <linux/bpf_types.h>
>  #undef BPF_PROG_TYPE
> -       0, /* avoid empty array */
>  };
>  #undef BPF_MAP_TYPE
>  #undef BPF_LINK_TYPE
> --
> 2.27.0
>
diff mbox series

Patch

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index ed7d02e8bc93..2a4a4aeeaac1 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -4218,11 +4218,11 @@  enum {
 	__ctx_convert_unused, /* to avoid empty enum in extreme .config */
 };
 static u8 bpf_ctx_convert_map[] = {
+	[0] = 0, /* avoid empty array */
 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
 	[_id] = __ctx_convert##_id,
 #include <linux/bpf_types.h>
 #undef BPF_PROG_TYPE
-	0, /* avoid empty array */
 };
 #undef BPF_MAP_TYPE
 #undef BPF_LINK_TYPE