mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] checkpatch tweaks
@ 2023-08-08  3:30 Jim Cromie
  2023-08-08  3:30 ` [PATCH 1/2] checkpatch: special case extern struct in .c Jim Cromie
  2023-08-08  3:30 ` [PATCH 2/2] checkpatch: reword long-line warning about commit-msg Jim Cromie
  0 siblings, 2 replies; 5+ messages in thread
From: Jim Cromie @ 2023-08-08  3:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: akpm, Jim Cromie

2 small changes:
  . silence extern in .c warning if sym is present in vmlinux.lds.h
  . warn about >75 chars 1st, since thats the actual test.

Jim Cromie (2):
  checkpatch: special case extern struct in .c
  checkpatch: reword long-line warning about commit-msg

 scripts/checkpatch.pl | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

-- 
2.41.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] checkpatch: special case extern struct in .c
  2023-08-08  3:30 [PATCH 0/2] checkpatch tweaks Jim Cromie
@ 2023-08-08  3:30 ` Jim Cromie
  2023-08-08  3:30 ` [PATCH 2/2] checkpatch: reword long-line warning about commit-msg Jim Cromie
  1 sibling, 0 replies; 5+ messages in thread
From: Jim Cromie @ 2023-08-08  3:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: akpm, Jim Cromie, apw, joe

"externs should be avoided in .c files" needs an exception for linker
symbols, like those that mark the start, stop of many kernel sections.

Since checkpatch already checks REALNAME to avoid looking at fragments
changing vmlinux.lds.h, add a new else-if block to look at them
instead.  As a simple heuristic, treat all words (in the patch-line)
as possible symbols, to screen later warnings.

For my test case, the possible-symbols included BOUNDED_BY (a macro),
which is extra, but not troublesome - these are just to screen
WARNINGS that might be issued on later fragments (changing .c files)

Where the WARN is issued, precede it with an else-if block to catch
one common extern-in-c use case: "extern struct foo bar[]".  Here we
can at least issue a softer warning, after checking for a match with a
maybe-linker-symbol parsed earlier from the patch.

Though heuristic, it worked for my test-case, allowing both start__,
stop__ $symbol's (wo the prefixes specifically named).  I've coded it
narrowly, it can be expanded later to cover any other expressions.

It does require that the externs in .c's have the additions to
vmlinux.lds.h in the same patch.  And requires vmlinux.lds.h before .c
fragments.

Cc: apw@canonical.com
Cc: joe@perches.com
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 scripts/checkpatch.pl | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 880fde13d9b8..6aabcc1f66c1 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -74,6 +74,8 @@ my $git_command ='export LANGUAGE=en_US.UTF-8; git';
 my $tabsize = 8;
 my ${CONFIG_} = "CONFIG_";
 
+my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
+
 sub help {
 	my ($exitcode) = @_;
 
@@ -6051,6 +6053,9 @@ sub process {
 
 # check for line continuations outside of #defines, preprocessor #, and asm
 
+		} elsif ($realfile =~ m@/vmlinux.lds.h$@) {
+		    $line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge;
+		    #print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol;
 		} else {
 			if ($prevline !~ /^..*\\$/ &&
 			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
@@ -7119,6 +7124,21 @@ sub process {
 				     "arguments for function declarations should follow identifier\n" . $herecurr);
 			}
 
+		} elsif ($realfile =~ /\.c$/ && defined $stat &&
+		    $stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/)
+		{
+			my ($st_type, $st_name) = ($1, $2);
+
+			for my $s (keys %maybe_linker_symbol) {
+			    #print "Linker symbol? $st_name : $s\n";
+			    goto LIKELY_LINKER_SYMBOL
+				if $st_name =~ /$s/;
+			}
+			WARN("AVOID_EXTERNS",
+			     "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
+			     . "is this a linker symbol ?\n" . $herecurr);
+		  LIKELY_LINKER_SYMBOL:
+
 		} elsif ($realfile =~ /\.c$/ && defined $stat &&
 		    $stat =~ /^.\s*extern\s+/)
 		{
-- 
2.41.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] checkpatch: reword long-line warning about commit-msg
  2023-08-08  3:30 [PATCH 0/2] checkpatch tweaks Jim Cromie
  2023-08-08  3:30 ` [PATCH 1/2] checkpatch: special case extern struct in .c Jim Cromie
@ 2023-08-08  3:30 ` Jim Cromie
  1 sibling, 0 replies; 5+ messages in thread
From: Jim Cromie @ 2023-08-08  3:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: akpm, Jim Cromie, apw, joe

Reword the warning to complain about line length 1st, since thats
whats actually tested.

Cc: apw@canonical.com
Cc: joe@perches.com
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 6aabcc1f66c1..6e789dc07420 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3272,7 +3272,7 @@ sub process {
 					# A Fixes:, link or signature tag line
 		      $commit_log_possible_stack_dump)) {
 			WARN("COMMIT_LOG_LONG_LINE",
-			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
+			     "Prefer a maximum 75 chars per line (possible unwrapped commit description?)\n" . $herecurr);
 			$commit_log_long_line = 1;
 		}
 
-- 
2.41.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] checkpatch: special case extern struct in .c
  2023-09-07 17:44 [PATCH 1/2] checkpatch: special case extern struct in .c Jim Cromie
@ 2023-09-07 18:18 ` Joe Perches
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Perches @ 2023-09-07 18:18 UTC (permalink / raw)
  To: Jim Cromie, linux-kernel; +Cc: akpm, apw, Kees Cook

On Thu, 2023-09-07 at 11:44 -0600, Jim Cromie wrote:
> The warning "externs should be avoided in .c files" wants an exception
> for linker symbols (named in vmlinux.lds.h etc), like those that mark
> the __start, __stop/__end symbols delimiting many kernel sections.
> 
> Since checkpatch already checks REALNAME to avoid looking at patch
> chunks changing vmlinux.lds.h, add a new else-if block to look at them
> instead.  As a simple heuristic, treat all words (in the + patch-lines)
> as candidate symbols, to screen later warnings about the same symbols
> being found in following chunks that change *.c files.
> 
> Where the "# check for new externs in .c files." is done, precede it
> with a new else-if block to isolate one common extern-in-c use case:
> "extern struct foo bar[]".  For this case, we can issue a more
> informative warning:
> 
>   WARN("AVOID_EXTERNS",
>      "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
>      . "is this a linker symbol ?\n" . $herecurr);
> 
> NOTE: The "screening" is a regex match, not an exact match.  This
> accepts __start_foo and __stop_foo symbols found in a *.c file, if
> "foo" was found previously in a vmlinux.lds.h chunk.
> 
> It does require that the patch adding "externs in .c's" also have the
> additions to vmlinux.lds.h.  And it requires vmlinux.lds.h chunks
> before .c chunks.
> 
> Cc: apw@canonical.com
> Cc: joe@perches.com
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
> ---
>  scripts/checkpatch.pl | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 880fde13d9b8..6aabcc1f66c1 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -74,6 +74,8 @@ my $git_command ='export LANGUAGE=en_US.UTF-8; git';
>  my $tabsize = 8;
>  my ${CONFIG_} = "CONFIG_";
>  
> +my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
> +
>  sub help {
>  	my ($exitcode) = @_;
>  
> @@ -6051,6 +6053,9 @@ sub process {
>  
>  # check for line continuations outside of #defines, preprocessor #, and asm
>  
> +		} elsif ($realfile =~ m@/vmlinux.lds.h$@) {
> +		    $line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge;
> +		    #print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol;
>  		} else {
>  			if ($prevline !~ /^..*\\$/ &&
>  			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
> @@ -7119,6 +7124,21 @@ sub process {
>  				     "arguments for function declarations should follow identifier\n" . $herecurr);
>  			}
>  
> +		} elsif ($realfile =~ /\.c$/ && defined $stat &&
> +		    $stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/)

Use the proper \s+ instead of ' '
And why use $stat instead of $sline?
Are you expecting these externs to be on multiple lines?

		} elsif ($realfile =~ /\.c$/ &&
			 $sline =~ /^\+\s*extern\s+struct\s+(\w+)\s+(\w+)\s*\[\s*\]\s*;/


> +		{
> +			my ($st_type, $st_name) = ($1, $2);
> +
> +			for my $s (keys %maybe_linker_symbol) {
> +			    #print "Linker symbol? $st_name : $s\n";
> +			    goto LIKELY_LINKER_SYMBOL

yuck.  no gotos please

Just use last

> +				if $st_name =~ /$s/;
> +			}
> +			WARN("AVOID_EXTERNS",
> +			     "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
> +			     . "is this a linker symbol ?\n" . $herecurr);

Single line output required then $herecurr
Using "in .c file" is also unnecessary.

> +		  LIKELY_LINKER_SYMBOL:
> +
>  		} elsif ($realfile =~ /\.c$/ && defined $stat &&
>  		    $stat =~ /^.\s*extern\s+/)
>  		{


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] checkpatch: special case extern struct in .c
@ 2023-09-07 17:44 Jim Cromie
  2023-09-07 18:18 ` Joe Perches
  0 siblings, 1 reply; 5+ messages in thread
From: Jim Cromie @ 2023-09-07 17:44 UTC (permalink / raw)
  To: linux-kernel; +Cc: akpm, Jim Cromie, apw, joe, Kees Cook

The warning "externs should be avoided in .c files" wants an exception
for linker symbols (named in vmlinux.lds.h etc), like those that mark
the __start, __stop/__end symbols delimiting many kernel sections.

Since checkpatch already checks REALNAME to avoid looking at patch
chunks changing vmlinux.lds.h, add a new else-if block to look at them
instead.  As a simple heuristic, treat all words (in the + patch-lines)
as candidate symbols, to screen later warnings about the same symbols
being found in following chunks that change *.c files.

Where the "# check for new externs in .c files." is done, precede it
with a new else-if block to isolate one common extern-in-c use case:
"extern struct foo bar[]".  For this case, we can issue a more
informative warning:

  WARN("AVOID_EXTERNS",
     "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
     . "is this a linker symbol ?\n" . $herecurr);

NOTE: The "screening" is a regex match, not an exact match.  This
accepts __start_foo and __stop_foo symbols found in a *.c file, if
"foo" was found previously in a vmlinux.lds.h chunk.

It does require that the patch adding "externs in .c's" also have the
additions to vmlinux.lds.h.  And it requires vmlinux.lds.h chunks
before .c chunks.

Cc: apw@canonical.com
Cc: joe@perches.com
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 scripts/checkpatch.pl | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 880fde13d9b8..6aabcc1f66c1 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -74,6 +74,8 @@ my $git_command ='export LANGUAGE=en_US.UTF-8; git';
 my $tabsize = 8;
 my ${CONFIG_} = "CONFIG_";
 
+my %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
+
 sub help {
 	my ($exitcode) = @_;
 
@@ -6051,6 +6053,9 @@ sub process {
 
 # check for line continuations outside of #defines, preprocessor #, and asm
 
+		} elsif ($realfile =~ m@/vmlinux.lds.h$@) {
+		    $line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge;
+		    #print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol;
 		} else {
 			if ($prevline !~ /^..*\\$/ &&
 			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
@@ -7119,6 +7124,21 @@ sub process {
 				     "arguments for function declarations should follow identifier\n" . $herecurr);
 			}
 
+		} elsif ($realfile =~ /\.c$/ && defined $stat &&
+		    $stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/)
+		{
+			my ($st_type, $st_name) = ($1, $2);
+
+			for my $s (keys %maybe_linker_symbol) {
+			    #print "Linker symbol? $st_name : $s\n";
+			    goto LIKELY_LINKER_SYMBOL
+				if $st_name =~ /$s/;
+			}
+			WARN("AVOID_EXTERNS",
+			     "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
+			     . "is this a linker symbol ?\n" . $herecurr);
+		  LIKELY_LINKER_SYMBOL:
+
 		} elsif ($realfile =~ /\.c$/ && defined $stat &&
 		    $stat =~ /^.\s*extern\s+/)
 		{
-- 
2.41.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-09-07 18:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-08  3:30 [PATCH 0/2] checkpatch tweaks Jim Cromie
2023-08-08  3:30 ` [PATCH 1/2] checkpatch: special case extern struct in .c Jim Cromie
2023-08-08  3:30 ` [PATCH 2/2] checkpatch: reword long-line warning about commit-msg Jim Cromie
2023-09-07 17:44 [PATCH 1/2] checkpatch: special case extern struct in .c Jim Cromie
2023-09-07 18:18 ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®