From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A808C4332F for ; Tue, 8 Mar 2022 14:59:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347696AbiCHPA3 (ORCPT ); Tue, 8 Mar 2022 10:00:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244592AbiCHPA1 (ORCPT ); Tue, 8 Mar 2022 10:00:27 -0500 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 121133917F; Tue, 8 Mar 2022 06:59:29 -0800 (PST) Received: from sslproxy01.your-server.de ([78.46.139.224]) by www62.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1nRbIu-0007PU-Fm; Tue, 08 Mar 2022 15:59:12 +0100 Received: from [85.1.206.226] (helo=linux.home) by sslproxy01.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nRbIu-000R4h-0o; Tue, 08 Mar 2022 15:59:12 +0100 Subject: Re: [PATCH] selftests/bpf: fix array_size.cocci warning To: Guo Zhengkui , Shuah Khan , Alexei Starovoitov , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Dave Marchevsky , Yucong Sun , Christy Lee , Delyan Kratunov , =?UTF-8?Q?Toke_H=c3=b8iland-J=c3=b8rgense?= =?UTF-8?Q?n?= , "open list:KERNEL SELFTEST FRAMEWORK" , "open list:BPF (Safe dynamic programs and tools)" , "open list:BPF (Safe dynamic programs and tools)" , open list Cc: zhengkui_guo@outlook.com References: <20220308091813.28574-1-guozhengkui@vivo.com> From: Daniel Borkmann Message-ID: Date: Tue, 8 Mar 2022 15:59:11 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20220308091813.28574-1-guozhengkui@vivo.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.5/26475/Tue Mar 8 10:31:43 2022) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 `, > 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 [...] > 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 > #include <../../../tools/include/linux/filter.h> > #include > +#include > > 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 > #include > +#include > #include > > 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