[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <cc8cffe8-58ed-27f3-0865-4ac3b2313866@iogearbox.net>
Date: Tue, 8 Mar 2022 15:59:11 +0100
From: Daniel Borkmann <daniel@...earbox.net>
To: Guo Zhengkui <guozhengkui@...o.com>, Shuah Khan <shuah@...nel.org>,
Alexei Starovoitov <ast@...nel.org>,
Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <kafai@...com>,
Song Liu <songliubraving@...com>, Yonghong Song <yhs@...com>,
John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>,
Dave Marchevsky <davemarchevsky@...com>,
Yucong Sun <sunyucong@...il.com>,
Christy Lee <christylee@...com>,
Delyan Kratunov <delyank@...com>,
Toke Høiland-Jørgensen
<toke@...hat.com>,
"open list:KERNEL SELFTEST FRAMEWORK"
<linux-kselftest@...r.kernel.org>,
"open list:BPF (Safe dynamic programs and tools)"
<netdev@...r.kernel.org>,
"open list:BPF (Safe dynamic programs and tools)"
<bpf@...r.kernel.org>, open list <linux-kernel@...r.kernel.org>
Cc: zhengkui_guo@...look.com
Subject: Re: [PATCH] selftests/bpf: fix array_size.cocci warning
On 3/8/22 10:17 AM, Guo Zhengkui wrote:
> Fix the array_size.cocci warning in tools/testing/selftests/bpf/
>
> Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.
>
> syscall.c and test_rdonly_maps.c don't contain header files which
> implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`,
> in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also
> checks that `arr` is really an array (using __must_be_array(arr)).
>
> Signed-off-by: Guo Zhengkui <guozhengkui@...o.com>
[...]
> diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
> index e550f728962d..85c6e7849463 100644
> --- a/tools/testing/selftests/bpf/progs/syscall.c
> +++ b/tools/testing/selftests/bpf/progs/syscall.c
> @@ -6,6 +6,7 @@
> #include <bpf/bpf_tracing.h>
> #include <../../../tools/include/linux/filter.h>
> #include <linux/btf.h>
> +#include <linux/kernel.h>
>
> char _license[] SEC("license") = "GPL";
>
> @@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
> static __u64 value = 34;
> static union bpf_attr prog_load_attr = {
> .prog_type = BPF_PROG_TYPE_XDP,
> - .insn_cnt = sizeof(insns) / sizeof(insns[0]),
> + .insn_cnt = ARRAY_SIZE(insns),
> };
> int ret;
>
> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> index fc8e8a34a3db..ca75aac745a4 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 <linux/kernel.h>
> #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;
>
There's bpf_util.h with ARRAY_SIZE definition which is used in selftests, pls change to
reuse that one.
Thanks,
Daniel
Powered by blists - more mailing lists