Skip to content

Commit

Permalink
checkpatch: Add a couple new alloc functions to alloc with multiplies…
Browse files Browse the repository at this point in the history
… check

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.

Link: KSPP#342

Original-patch-by: Gustavo A. R. Silva <[email protected]>
Link: https://lore.kernel.org/lkml/ZQCaO+tYycDxVLy7@work/
Signed-off-by: Joe Perches <[email protected]>
  • Loading branch information
JoePerches authored and intel-lab-lkp committed Sep 13, 2023
1 parent c53c591 commit f4107fe
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,16 @@ sub find_standard_signature {
#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 |
Expand Down Expand Up @@ -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/) {
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit f4107fe

Please sign in to comment.