linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpf: fix incorrect initialization of bpf_ctx_convert_map
@ 2020-10-26 21:03 Arnd Bergmann
  2020-10-26 22:32 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: Arnd Bergmann @ 2020-10-26 21:03 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Jakub Sitnicki,
	Arnd Bergmann, Martin KaFai Lau
  Cc: Marek Majkowski, Song Liu, Yonghong Song, Andrii Nakryiko,
	John Fastabend, KP Singh, Jiri Olsa, Alan Maguire, Hao Luo,
	netdev, bpf, linux-kernel

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(-)

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] bpf: fix incorrect initialization of bpf_ctx_convert_map
  2020-10-26 21:03 [PATCH] bpf: fix incorrect initialization of bpf_ctx_convert_map Arnd Bergmann
@ 2020-10-26 22:32 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2020-10-26 22:32 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Alexei Starovoitov, Daniel Borkmann, Jakub Sitnicki,
	Arnd Bergmann, Martin KaFai Lau, Marek Majkowski, Song Liu,
	Yonghong Song, Andrii Nakryiko, John Fastabend, KP Singh,
	Jiri Olsa, Alan Maguire, Hao Luo, Networking, bpf, open list

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
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-26 22:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-26 21:03 [PATCH] bpf: fix incorrect initialization of bpf_ctx_convert_map Arnd Bergmann
2020-10-26 22:32 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).