Skip to contentSkip to conversation

checkpatch: refactor and add new alloc w/ multiply tests

PATCH In review Joe Perches 2 participants 10 messages Sep 13, 2023 – Sep 14, 2023 Checkpatch

lore
Share & tools
lore

Joe Perches

author maintainer
#0 lore raw

cover checkpatch: refactor and add new alloc w/ multiply tests

Original alloc function patch by Gustavo Silva

Joe Perches (2):
checkpatch: Simplify creating search strings
checkpatch: Add a couple new alloc functions to alloc with multiplies check

 scripts/checkpatch.pl | 55 +++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 34 deletions(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/2 checkpatch: Simplify creating search strings

Use join and map instead of loops.

---
 scripts/checkpatch.pl | 34 +++++++---------------------------
 1 file changed, 7 insertions(+), 27 deletions(-)
scripts/checkpatch.pl +7 -27
index 7d16f863edf1c..617f9e53bacdf 100755
@@ -625,18 +625,8 @@ our $signature_tags = qr{(?xi:
 our @link_tags = qw(Link Closes);
 
 #Create a search and print patterns for all these strings to be used directly below
-our $link_tags_search = "";
-our $link_tags_print = "";
-foreach my $entry (@link_tags) {
-	if ($link_tags_search ne "") {
-		$link_tags_search .= '|';
-		$link_tags_print .= ' or ';
-	}
-	$entry .= ':';
-	$link_tags_search .= $entry;
-	$link_tags_print .= "'$entry'";
-}
-$link_tags_search = "(?:${link_tags_search})";
+our $link_tags_search = '(?:' . join('|', @link_tags) . ')';
+our $link_tags_print = "'" . join("' or '", @link_tags) . "'";
 
 our $tracing_logging_tags = qr{(?xi:
 	[=-]*> |
@@ -819,15 +809,10 @@ our @mode_permission_funcs = (
 	["__ATTR", 2],
 );
 
-my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
-
 #Create a search pattern for all these functions to speed up a loop below
-our $mode_perms_search = "";
-foreach my $entry (@mode_permission_funcs) {
-	$mode_perms_search .= '|' if ($mode_perms_search ne "");
-	$mode_perms_search .= $entry->[0];
-}
-$mode_perms_search = "(?:${mode_perms_search})";
+our $mode_perms_search = '(?:' . join('|', map{$_->[0]} @mode_permission_funcs) . ')';
+
+my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
 
 our %deprecated_apis = (
 	"synchronize_rcu_bh"			=> "synchronize_rcu",
@@ -847,12 +832,7 @@ our %deprecated_apis = (
 );
 
 #Create a search pattern for all these strings to speed up a loop below
-our $deprecated_apis_search = "";
-foreach my $entry (keys %deprecated_apis) {
-	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
-	$deprecated_apis_search .= $entry;
-}
-$deprecated_apis_search = "(?:${deprecated_apis_search})";
+our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')';
 
 our $mode_perms_world_writable = qr{
 	S_IWUGO		|
@@ -887,7 +867,7 @@ foreach my $entry (keys %mode_permission_string_types) {
 	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
 	$mode_perms_string_search .= $entry;
 }
-our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
+our $single_mode_perms_string_search = '(?:' . join('|', keys %mode_permission_string_types) . ')';
 our $multi_mode_perms_string_search = qr{
 	${single_mode_perms_string_search}
 	(?:\s*\|\s*${single_mode_perms_string_search})*
Signature · 1 line
2.41.0
author maintainer
#2 lore raw

PATCH 2/2 checkpatch: Add a couple new alloc functions to alloc with multiplies check

In reply to Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

---
 scripts/checkpatch.pl | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
scripts/checkpatch.pl +14 -7
index 617f9e53bacdf..4cb248985eefc 100755
@@ -834,6 +834,16 @@ our %deprecated_apis = (
 #Create a search pattern for all these strings to speed up a loop below
 our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')';
 
+our %alloc_with_multiply_apis = (
+	"kmalloc"		=> "kmalloc_array",
+	"kvmalloc"		=> "kvmalloc_array",
+	"vmalloc"		=> "vmalloc_array",
+	"kvzalloc"		=> "kvcalloc",
+	"kzalloc"		=> "kcalloc",
+	"vzalloc"		=> "vcalloc",
+);
+our $alloc_with_multiply_search = '(?:' . join('|', keys %alloc_with_multiply_apis) . ')';
+
 our $mode_perms_world_writable = qr{
 	S_IWUGO		|
 	S_IWOTH		|
@@ -7187,17 +7197,14 @@ sub process {
 			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
 		}
 
-# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
+# check for various allocs with multiplies that should use safer functions
 		if ($perl_version_ok &&
 		    defined $stat &&
-		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
+		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*($alloc_with_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
 			my $oldfunc = $3;
+			my $newfunc = $alloc_with_multiply_apis{$oldfunc};
 			my $a1 = $4;
 			my $a2 = $10;
-			my $newfunc = "kmalloc_array";
-			$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
-			$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
-			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
 			my $r1 = $a1;
 			my $r2 = $a2;
 			if ($a1 =~ /^sizeof\s*\S/) {
@@ -7213,7 +7220,7 @@ sub process {
 					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
 				    $cnt == 1 &&
 				    $fix) {
-					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
+					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
 				}
 			}
 		}
Signature · 1 line
2.41.0
#3 lore raw

Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check

Quoted 10 lines · Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

Why don't you wait for a response or a v2 from the original submitter?

--
Gustavo

author maintainer
#4 lore raw

Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check

Quoted 7 lines · Gustavo A. R. Silva
Quoted 5 lines · Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Quoted 8 lines
Quoted 5 lines

Simplify the original codes repeated else to use a hash.

Why don't you wait for a response or a v2 from the original submitter?

Because there really is no need to wait.

#5 lore raw

Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check

Quoted 10 lines · Joe Perches
Quoted 9 lines · Gustavo A. R. Silva
Quoted 7 lines · Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Why don't you wait for a response or a v2 from the original submitter?

Because there really is no need to wait.

By the way, did you test it?

--
Gustavo

author maintainer
#6 lore raw

Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check

Quoted 14 lines · Gustavo A. R. Silva
Quoted 12 lines · Joe Perches
Quoted 11 lines · Gustavo A. R. Silva
Quoted 9 lines · Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why don't you wait for a response or a v2 from the original submitter?

Because there really is no need to wait.

By the way, did you test it?

Yes, against arch/s390/include/asm/idals.h

Did you? If so, you could also add your own tested-by...

#7 lore raw

Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check

Quoted 17 lines · Joe Perches
Quoted 16 lines · Gustavo A. R. Silva
Quoted 14 lines · Joe Perches
Quoted 13 lines · Gustavo A. R. Silva
Quoted 10 lines · Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Why don't you wait for a response or a v2 from the original submitter?

Because there really is no need to wait.

By the way, did you test it?

Yes, against arch/s390/include/asm/idals.h

I don't see any instances of vmalloc() or vzalloc() in that file.

--
Gustavo

author maintainer
#8 lore raw

Re: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check

Quoted 22 lines · Gustavo A. R. Silva
Quoted 20 lines · Joe Perches
Quoted 19 lines · Gustavo A. R. Silva
Quoted 17 lines · Joe Perches
Quoted 15 lines · Gustavo A. R. Silva
Quoted 10 lines · Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

Why don't you wait for a response or a v2 from the original submitter?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Because there really is no need to wait.

By the way, did you test it?

Yes, against arch/s390/include/asm/idals.h

I don't see any instances of vmalloc() or vzalloc() in that file.

<snort> Cute.

Missing a ? in the original search '\s*,?'
or just its removal...

Likely the removal would be better.

thanks, cheers, Joe

author maintainer
#9 lore raw

PATCH v2 2/2 checkpatch: Add a couple new alloc functions to alloc with multiplies check

In reply to Joe Perches

vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly.

Add alloc-with-multiplies checks for these new functions.

Simplify the original codes repeated else to use a hash.

---

v2: Fix search because vmalloc functions don't take a 3rd argument

 scripts/checkpatch.pl | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
scripts/checkpatch.pl +14 -7
index 617f9e53bacdf..4cb248985eefc 100755
@@ -834,6 +834,16 @@ our %deprecated_apis = (
 #Create a search pattern for all these strings to speed up a loop below
 our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')';
 
+our %alloc_with_multiply_apis = (
+	"kmalloc"		=> "kmalloc_array",
+	"kvmalloc"		=> "kvmalloc_array",
+	"vmalloc"		=> "vmalloc_array",
+	"kvzalloc"		=> "kvcalloc",
+	"kzalloc"		=> "kcalloc",
+	"vzalloc"		=> "vcalloc",
+);
+our $alloc_with_multiply_search = '(?:' . join('|', keys %alloc_with_multiply_apis) . ')';
+
 our $mode_perms_world_writable = qr{
 	S_IWUGO		|
 	S_IWOTH		|
@@ -7187,17 +7197,14 @@ sub process {
 			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
 		}
 
-# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
+# check for various allocs with multiplies that should use safer functions
 		if ($perl_version_ok &&
 		    defined $stat &&
-		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
+		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*($alloc_with_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) {
 			my $oldfunc = $3;
+			my $newfunc = $alloc_with_multiply_apis{$oldfunc};
 			my $a1 = $4;
 			my $a2 = $10;
-			my $newfunc = "kmalloc_array";
-			$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
-			$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
-			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
 			my $r1 = $a1;
 			my $r2 = $a2;
 			if ($a1 =~ /^sizeof\s*\S/) {
@@ -7213,7 +7220,7 @@ sub process {
 					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
 				    $cnt == 1 &&
 				    $fix) {
-					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
+					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
 				}
 			}
 		}
Signature · 1 line
2.41.0