On Fri, 2025-11-21 at 17:42 -0800, Kees Cook wrote:
> 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.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -7258,17 +7258,42 @@ 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 (kv|k)[mz]alloc that could be
> kmalloc_obj/kvmalloc_obj/kzalloc_obj/kvzalloc_obj
There are _way_ too many of these existing uses to suggest this change
in existing files so please add '&& !$file' to these tests
> + if ($perl_version_ok &&
> + defined $stat &&
> + $stat =~
> /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/)
> {
> + my $oldfunc = $3;
> + my $a1 = $4;
> + my $newfunc = "kmalloc_obj";
> + $newfunc = "kvmalloc_obj" if ($oldfunc eq "kvmalloc");
> + $newfunc = "kvzalloc_obj" if ($oldfunc eq "kvzalloc");
> + $newfunc = "kzalloc_obj" if ($oldfunc eq "kzalloc");
> +
> + if ($a1 =~ s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/) {
> + my $cnt = statement_rawlines($stat);
> + my $herectx = get_stat_here($linenr, $cnt,
> $here);
> +
> + if (WARN("ALLOC_WITH_SIZEOF",
> + "Prefer $newfunc over $oldfunc with
> sizeof\n" . $herectx) &&
> + $cnt == 1 &&
> + $fix) {
> + $fixed[$fixlinenr] =~
> s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/$1
> = $newfunc($a1,/;
> + }
> + }
> + }
> +
> +
> +# check for (kv|k)[mz]alloc with multiplies that could be
> kmalloc_objs/kvmalloc_objs/kzalloc_objs/kvzalloc_objs
> 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*,/)
> {
> my $oldfunc = $3;
> 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 $newfunc = "kmalloc_objs";
> + $newfunc = "kvmalloc_objs" if ($oldfunc eq "kvmalloc");
> + $newfunc = "kvzalloc_objs" if ($oldfunc eq "kvzalloc");
> + $newfunc = "kzalloc_objs" if ($oldfunc eq "kzalloc");
> my $r1 = $a1;
> my $r2 = $a2;
> if ($a1 =~ /^sizeof\s*\S/) {
> @@ -7284,7 +7309,9 @@ 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;
> + my $sized = trim($r2);
> + $sized =~
> s/^sizeof\s*\S\(?([^\)]*)\)?$/$1/;
> + $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;
> }
> }
> }