2022-03-11 22:28:27

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH v2] selftests/bpf: fix array_size.cocci warning

On Tue, Mar 8, 2022 at 7:36 PM Guo Zhengkui <[email protected]> wrote:
>
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
>
> Use `ARRAY_SIZE(arr)` in bpf_util.h instead of forms like
> `sizeof(arr)/sizeof(arr[0])`.
>
> Signed-off-by: Guo Zhengkui <[email protected]>
> ---
> .../selftests/bpf/prog_tests/cgroup_attach_autodetach.c | 2 +-
> .../testing/selftests/bpf/prog_tests/cgroup_attach_multi.c | 2 +-
> .../selftests/bpf/prog_tests/cgroup_attach_override.c | 2 +-
> tools/testing/selftests/bpf/prog_tests/global_data.c | 6 +++---
> tools/testing/selftests/bpf/prog_tests/obj_name.c | 2 +-
> tools/testing/selftests/bpf/progs/syscall.c | 3 ++-
> tools/testing/selftests/bpf/progs/test_rdonly_maps.c | 3 ++-
> tools/testing/selftests/bpf/test_cgroup_storage.c | 2 +-
> tools/testing/selftests/bpf/test_lru_map.c | 4 ++--
> tools/testing/selftests/bpf/test_sock_addr.c | 6 +++---
> tools/testing/selftests/bpf/test_sockmap.c | 4 ++--
> 11 files changed, 19 insertions(+), 17 deletions(-)
>

[...]

> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> index fc8e8a34a3db..a500f2c15970 100644
> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> @@ -3,6 +3,7 @@
>
> #include <linux/ptrace.h>
> #include <linux/bpf.h>
> +#include <bpf_util.h>

bpf_util.h isn't supposed to be included from BPF source code side. Is
this ARRAY_SIZE() use so important for BPF programs? Maybe just leave
existing code under progs/*.c as is?

> #include <bpf/bpf_helpers.h>
>
> const struct {
> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
> {
> /* prevent compiler to optimize everything out */
> unsigned * volatile p = (void *)&rdonly_values.a;
> - int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
> + int i = ARRAY_SIZE(rdonly_values.a);
> unsigned iters = 0, sum = 0;
>
> /* validate verifier can allow full loop as well */

[...]