2021-02-08 23:44:43

by Song Liu

[permalink] [raw]
Subject: [PATCH] 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]>
Signed-off-by: Song Liu <[email protected]>
---
scripts/checkpatch.pl | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 1afe3af1cc097..24d1856187651 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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) {
--
2.24.1


2021-02-09 06:34:55

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: do not apply "initialise globals to 0" check to BPF progs

On Mon, 2021-02-08 at 15:40 -0800, Song Liu wrote:
> 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:
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -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.

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


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


2021-02-09 06:36:41

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: do not apply "initialise globals to 0" check to BPF progs

On Mon, 2021-02-08 at 15:40 -0800, Song Liu wrote:
> 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.

> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -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) {


2021-02-09 16:52:22

by Song Liu

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: do not apply "initialise globals to 0" check to BPF progs



> On Feb 8, 2021, at 10:29 PM, Joe Perches <[email protected]> wrote:
>
> On Mon, 2021-02-08 at 15:40 -0800, Song Liu wrote:
>> 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:
> []
>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
>> @@ -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.
>
> 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