Skip to contentSkip to conversation

checkpatch: do not apply "initialise globals to 0" check to BPF progs

PATCH Superseded Song Liu 2 participants 4 messages Feb 8, 2021 – Feb 9, 2021

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Song Liu

author
#0 lore raw

PATCH 1/1 checkpatch: do not apply "initialise globals to 0" check to BPF progs

BPF programs explicitly initialise global variables to 0 to make sure clang (v10 or older) do not put the variables in the common section. Skip "initialise globals to 0" check for BPF programs to elimiate error messages like:

ERROR: do not initialise globals to 0
#19: FILE: samples/bpf/tracex1_kern.c:21:

Cc Andy Whitcroft <[email protected]>
Cc Joe Perches <[email protected]>
---
 scripts/checkpatch.pl | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +5 -1
index 1afe3af1cc097..24d1856187651 100755
@@ -4323,7 +4323,11 @@ sub process {
 		}
 
 # check for global initialisers.
-		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
+# Do not apply to BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
+		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
+		    $realfile !~ /^tools\/testing\/selftests\/bpf\/progs\/.*\.c/ &&
+		    $realfile !~ /^samples\/bpf\/.*_kern.c/ &&
+		    $realfile !~ /.bpf.c$/) {
 			if (ERROR("GLOBAL_INITIALISERS",
 				  "do not initialise globals to $1\n" . $herecurr) &&
 			    $fix) {
Signature · 1 line
2.24.1
maintainer
#1 lore raw
Quoted 7 lines · Song Liu

BPF programs explicitly initialise global variables to 0 to make sure clang (v10 or older) do not put the variables in the common section. Skip "initialise globals to 0" check for BPF programs to elimiate error messages like:

    ERROR: do not initialise globals to 0
    #19: FILE: samples/bpf/tracex1_kern.c:21:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 4330

Quoted 11 lines
scripts/checkpatch.pl +0 -0
@@ -4323,7 +4323,11 @@ sub process {
  		}

 # check for global initialisers.
scripts/checkpatch.pl +5 -1
-		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
+# Do not apply to BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
+		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
+		    $realfile !~ /^tools\/testing\/selftests\/bpf\/progs\/.*\.c/ &&
+		    $realfile !~ /^samples\/bpf\/.*_kern.c/ &&
+		    $realfile !~ /.bpf.c$/) {

probably better to make this a function so when additional files are added it'd be easier to update this and it will not look as complex.

		if ($line =~ /.../ &&
		    !exclude_global_initialisers($realfile))
Quoted 3 lines

  if (ERROR("GLOBAL_INITIALISERS",
  "do not initialise globals to $1\n" . $herecurr) && $fix) {

author
#2 lore raw
Quoted 30 lines · Joe Perches
Quoted 7 lines · Song Liu

BPF programs explicitly initialise global variables to 0 to make sure clang (v10 or older) do not put the variables in the common section. Skip "initialise globals to 0" check for BPF programs to elimiate error messages like:

ERROR: do not initialise globals to 0
#19: FILE: samples/bpf/tracex1_kern.c:21:

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 4331

Quoted 11 lines
scripts/checkpatch.pl +5 -1
@@ -4323,7 +4323,11 @@ sub process {
 		}
 
 
 # check for global initialisers.
-		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
+# Do not apply to BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
+		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
+		    $realfile !~ /^tools\/testing\/selftests\/bpf\/progs\/.*\.c/ &&
+		    $realfile !~ /^samples\/bpf\/.*_kern.c/ &&
+		    $realfile !~ /.bpf.c$/) {

probably better to make this a function so when additional files are added it'd be easier to update this and it will not look as complex.

Quoted 2 lines
		if ($line =~ /.../ &&
		    !exclude_global_initialisers($realfile))

Good point! I will make this a function in v2.

--ignore is not ideal, because it is common for a BPF test/sample patch to have both BPF code and user space code. Adding --ignore will skip the check for user space code.

Thanks,
Song

maintainer
#3 lore raw
In reply to Song Liu
Quoted 7 lines · Song Liu

BPF programs explicitly initialise global variables to 0 to make sure clang (v10 or older) do not put the variables in the common section. Skip "initialise globals to 0" check for BPF programs to elimiate error messages like:

    ERROR: do not initialise globals to 0
    #19: FILE: samples/bpf/tracex1_kern.c:21:

Another possible option is simply to add --ignore=GLOBAL_INITIALISERS to the checkpatch command line for these files.

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 14 lines
scripts/checkpatch.pl +0 -0
@@ -4323,7 +4323,11 @@ sub process {
  		}

 # check for global initialisers.
scripts/checkpatch.pl +5 -1
-		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
+# Do not apply to BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
+		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
+		    $realfile !~ /^tools\/testing\/selftests\/bpf\/progs\/.*\.c/ &&
+		    $realfile !~ /^samples\/bpf\/.*_kern.c/ &&
+		    $realfile !~ /.bpf.c$/) {

  if (ERROR("GLOBAL_INITIALISERS",
  "do not initialise globals to $1\n" . $herecurr) && $fix) {