diff mbox series

bpf, btf: Make if test explicit to fix Coccinelle error

Message ID 20240624195426.176827-2-thorsten.blum@toblux.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf, btf: Make if test explicit to fix Coccinelle error | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 844 this patch: 844
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 849 this patch: 849
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 853 this patch: 853
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc

Commit Message

Thorsten Blum June 24, 2024, 7:54 p.m. UTC
Explicitly test the iterator variable i > 0 to fix the following
Coccinelle/coccicheck error reported by itnull.cocci:

	ERROR: iterator variable bound on line 4688 cannot be NULL

Compile-tested only.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Alexei Starovoitov June 24, 2024, 8:15 p.m. UTC | #1
On Mon, Jun 24, 2024 at 12:56 PM Thorsten Blum <thorsten.blum@toblux.com> wrote:
>
> Explicitly test the iterator variable i > 0 to fix the following
> Coccinelle/coccicheck error reported by itnull.cocci:
>
>         ERROR: iterator variable bound on line 4688 cannot be NULL
>
> Compile-tested only.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> ---
>  kernel/bpf/btf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 821063660d9f..7720f8967814 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -4687,7 +4687,7 @@ static void btf_datasec_show(const struct btf *btf,
>                             __btf_name_by_offset(btf, t->name_off));
>         for_each_vsi(i, t, vsi) {
>                 var = btf_type_by_id(btf, vsi->type);
> -               if (i)
> +               if (i > 0)
>                         btf_show(show, ",");

Sorry, I don't think this is a sustainable approach.
We cannot fix such things all over the kernel.
Pls make cocci smarter instead.

pw-bot: cr
Eduard Zingerman June 24, 2024, 8:16 p.m. UTC | #2
On Mon, 2024-06-24 at 21:54 +0200, Thorsten Blum wrote:
> Explicitly test the iterator variable i > 0 to fix the following
> Coccinelle/coccicheck error reported by itnull.cocci:
> 
> 	ERROR: iterator variable bound on line 4688 cannot be NULL
> 
> Compile-tested only.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> ---
>  kernel/bpf/btf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 821063660d9f..7720f8967814 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -4687,7 +4687,7 @@ static void btf_datasec_show(const struct btf *btf,
>  			    __btf_name_by_offset(btf, t->name_off));
>  	for_each_vsi(i, t, vsi) {
>  		var = btf_type_by_id(btf, vsi->type);
> -		if (i)
> +		if (i > 0)
>  			btf_show(show, ",");
>  		btf_type_ops(var)->show(btf, var, vsi->type,
>  					data + vsi->offset, bits_offset, show);

Could you please elaborate a bit?
Here is for_each_vsi is defined:

#define for_each_vsi(i, datasec_type, member)			\
	for (i = 0, member = btf_type_var_secinfo(datasec_type);	\
	     i < btf_type_vlen(datasec_type);			\
	     i++, member++)

Here it sets 'i' to zero for the first iteration.
Why would the tool report that 'i' can't be zero?
Thorsten Blum June 24, 2024, 11:08 p.m. UTC | #3
On 24. Jun 2024, at 13:16, Eduard Zingerman <eddyz87@gmail.com> wrote:
> On Mon, 2024-06-24 at 21:54 +0200, Thorsten Blum wrote:
>> Explicitly test the iterator variable i > 0 to fix the following
>> Coccinelle/coccicheck error reported by itnull.cocci:
>> 
>> ERROR: iterator variable bound on line 4688 cannot be NULL
>> 
>> Compile-tested only.
>> 
>> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
>> ---
>> kernel/bpf/btf.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
>> index 821063660d9f..7720f8967814 100644
>> --- a/kernel/bpf/btf.c
>> +++ b/kernel/bpf/btf.c
>> @@ -4687,7 +4687,7 @@ static void btf_datasec_show(const struct btf *btf,
>>    __btf_name_by_offset(btf, t->name_off));
>> for_each_vsi(i, t, vsi) {
>> var = btf_type_by_id(btf, vsi->type);
>> - if (i)
>> + if (i > 0)
>> btf_show(show, ",");
>> btf_type_ops(var)->show(btf, var, vsi->type,
>> data + vsi->offset, bits_offset, show);
> 
> Could you please elaborate a bit?
> Here is for_each_vsi is defined:
> 
> #define for_each_vsi(i, datasec_type, member) \
> for (i = 0, member = btf_type_var_secinfo(datasec_type); \
>     i < btf_type_vlen(datasec_type); \
>     i++, member++)
> 
> Here it sets 'i' to zero for the first iteration.
> Why would the tool report that 'i' can't be zero?

Coccinelle thinks i can't be a NULL pointer (not the number zero). It's
essentially a false-positive warning, but since there are only 4 such
warnings under kernel/, I thought it would be worthwhile to remove some
of them by making the tests explicit.
Eduard Zingerman June 24, 2024, 11:27 p.m. UTC | #4
On Mon, 2024-06-24 at 16:08 -0700, Thorsten Blum wrote:
> On 24. Jun 2024, at 13:16, Eduard Zingerman <eddyz87@gmail.com> wrote:
> > On Mon, 2024-06-24 at 21:54 +0200, Thorsten Blum wrote:
> > > Explicitly test the iterator variable i > 0 to fix the following
> > > Coccinelle/coccicheck error reported by itnull.cocci:
> > > 
> > > ERROR: iterator variable bound on line 4688 cannot be NULL

[...]

> > #define for_each_vsi(i, datasec_type, member) \
> > for (i = 0, member = btf_type_var_secinfo(datasec_type); \
> >     i < btf_type_vlen(datasec_type); \
> >     i++, member++)
> > 
> > Here it sets 'i' to zero for the first iteration.
> > Why would the tool report that 'i' can't be zero?
> 
> Coccinelle thinks i can't be a NULL pointer (not the number zero). It's
> essentially a false-positive warning, but since there are only 4 such
> warnings under kernel/, I thought it would be worthwhile to remove some
> of them by making the tests explicit.

Sorry, not really familiar with the tool, but it seems like the
following part of the itnull.cocci fires the warning:

  @r depends on !patch exists@
  iterator I;
  expression x,E;
  position p1,p2;
  @@
  
  *I@p1(x,...)
  { ... when != x = E
  (
  *  x@p2 == NULL
  |
  *  x@p2 != NULL
  )
    ... when any
  }
  
  @script:python depends on org@
  p1 << r.p1;
  p2 << r.p2;
  @@
  
  cocci.print_main("iterator-bound variable",p1)
  cocci.print_secs("useless NULL test",p2)
  
  @script:python depends on report@
  p1 << r.p1;
  p2 << r.p2;
  @@
  
  msg = "ERROR: iterator variable bound on line %s cannot be NULL" % (p1[0].line)
  coccilib.report.print_report(p2[0], msg)

Is there a way to add a constraint here, requiring 'x' to have a pointer type?
(So that the rule does not match, as it clearly shouldn't).
diff mbox series

Patch

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 821063660d9f..7720f8967814 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -4687,7 +4687,7 @@  static void btf_datasec_show(const struct btf *btf,
 			    __btf_name_by_offset(btf, t->name_off));
 	for_each_vsi(i, t, vsi) {
 		var = btf_type_by_id(btf, vsi->type);
-		if (i)
+		if (i > 0)
 			btf_show(show, ",");
 		btf_type_ops(var)->show(btf, var, vsi->type,
 					data + vsi->offset, bits_offset, show);