Skip to content

Commit 070580b

Browse files
committed
checkpatch: Suggest kmalloc_obj family for sizeof allocations
To support shifting away from sized allocation towards typed allocations, suggest the kmalloc_obj family of macros when a sizeof() is present in the argument lists. Link: https://patch.msgid.link/20251203233036.3212363-2-kees@kernel.org Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 2932ba8 commit 070580b

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

scripts/checkpatch.pl

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7258,17 +7258,42 @@ sub process {
72587258
"Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
72597259
}
72607260

7261-
# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
7261+
# check for (kv|k)[mz]alloc that could be kmalloc_obj/kvmalloc_obj/kzalloc_obj/kvzalloc_obj
7262+
if ($perl_version_ok &&
7263+
defined $stat &&
7264+
$stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/) {
7265+
my $oldfunc = $3;
7266+
my $a1 = $4;
7267+
my $newfunc = "kmalloc_obj";
7268+
$newfunc = "kvmalloc_obj" if ($oldfunc eq "kvmalloc");
7269+
$newfunc = "kvzalloc_obj" if ($oldfunc eq "kvzalloc");
7270+
$newfunc = "kzalloc_obj" if ($oldfunc eq "kzalloc");
7271+
7272+
if ($a1 =~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/) {
7273+
my $cnt = statement_rawlines($stat);
7274+
my $herectx = get_stat_here($linenr, $cnt, $here);
7275+
7276+
if (WARN("ALLOC_WITH_SIZEOF",
7277+
"Prefer $newfunc over $oldfunc with sizeof\n" . $herectx) &&
7278+
$cnt == 1 &&
7279+
$fix) {
7280+
$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/$1 = $newfunc($a1,/;
7281+
}
7282+
}
7283+
}
7284+
7285+
7286+
# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_objs/kvmalloc_objs/kzalloc_objs/kvzalloc_objs
72627287
if ($perl_version_ok &&
72637288
defined $stat &&
72647289
$stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
72657290
my $oldfunc = $3;
72667291
my $a1 = $4;
72677292
my $a2 = $10;
7268-
my $newfunc = "kmalloc_array";
7269-
$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
7270-
$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
7271-
$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
7293+
my $newfunc = "kmalloc_objs";
7294+
$newfunc = "kvmalloc_objs" if ($oldfunc eq "kvmalloc");
7295+
$newfunc = "kvzalloc_objs" if ($oldfunc eq "kvzalloc");
7296+
$newfunc = "kzalloc_objs" if ($oldfunc eq "kzalloc");
72727297
my $r1 = $a1;
72737298
my $r2 = $a2;
72747299
if ($a1 =~ /^sizeof\s*\S/) {
@@ -7284,7 +7309,9 @@ sub process {
72847309
"Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
72857310
$cnt == 1 &&
72867311
$fix) {
7287-
$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;
7312+
my $sized = trim($r2);
7313+
$sized =~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/;
7314+
$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . $sized . ', ' . trim($r1)/e;
72887315
}
72897316
}
72907317
}

0 commit comments

Comments
 (0)