linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: "Guo Zhengkui" <guozhengkui@vivo.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"Dave Marchevsky" <davemarchevsky@fb.com>,
	"Yucong Sun" <sunyucong@gmail.com>,
	"Christy Lee" <christylee@fb.com>,
	"Delyan Kratunov" <delyank@fb.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"open list:KERNEL SELFTEST FRAMEWORK"
	<linux-kselftest@vger.kernel.org>,
	"open list:BPF (Safe dynamic programs and tools)"
	<netdev@vger.kernel.org>,
	"open list:BPF (Safe dynamic programs and tools)"
	<bpf@vger.kernel.org>, "open list" <linux-kernel@vger.kernel.org>
Cc: zhengkui_guo@outlook.com
Subject: Re: [PATCH] selftests/bpf: fix array_size.cocci warning
Date: Tue, 8 Mar 2022 15:59:11 +0100	[thread overview]
Message-ID: <cc8cffe8-58ed-27f3-0865-4ac3b2313866@iogearbox.net> (raw)
In-Reply-To: <20220308091813.28574-1-guozhengkui@vivo.com>

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@vivo.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

  reply	other threads:[~2022-03-08 14:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-08  9:17 [PATCH] selftests/bpf: fix array_size.cocci warning Guo Zhengkui
2022-03-08 14:59 ` Daniel Borkmann [this message]
2022-03-08 15:00   ` Daniel Borkmann
2022-03-09  3:22     ` [PATCH v2] " Guo Zhengkui
2022-03-09  3:27       ` Guo Zhengkui
2022-03-09  3:35     ` Guo Zhengkui
2022-03-10 22:08       ` Daniel Borkmann
2022-03-15 13:01         ` [PATCH v3] " Guo Zhengkui
2022-03-15 16:20           ` patchwork-bot+netdevbpf
2022-03-11 18:38       ` [PATCH v2] " Andrii Nakryiko
2022-03-15 11:48         ` Guo Zhengkui
  -- strict thread matches above, loose matches on Subject: below --
2021-11-17 22:42 [PATCH] selftests/bpf: fix array_size.cocci warning: Daniel Borkmann
2021-11-18  7:11 ` [PATCH] selftests/bpf: fix array_size.cocci warning Guo Zhengkui

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cc8cffe8-58ed-27f3-0865-4ac3b2313866@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=christylee@fb.com \
    --cc=davemarchevsky@fb.com \
    --cc=delyank@fb.com \
    --cc=guozhengkui@vivo.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=songliubraving@fb.com \
    --cc=sunyucong@gmail.com \
    --cc=toke@redhat.com \
    --cc=yhs@fb.com \
    --cc=zhengkui_guo@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).