Skip to contentSkip to conversation

bpf: fix incorrect initialization of bpf_ctx_convert_map

PATCH In review Arnd Bergmann 2 participants 2 messages Oct 26, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix incorrect initialization of bpf_ctx_convert_map

From: Arnd Bergmann <[email protected]>

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")
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
index ed7d02e8bc93..2a4a4aeeaac1 100644
@@ -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
Signature · 1 line
2.27.0
#1 lore raw
Quoted 8 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

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:

Quoted 7 lines

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

Quoted 28 lines
Fixes e9ddbb7707ff ("bpf: Introduce SK_LOOKUP program type with a dedicated attach point")
Fixes 4c80c7bc583a ("bpf: Fix build in minimal configurations, again")
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -2
index ed7d02e8bc93..2a4a4aeeaac1 100644
@@ -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