[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <f1d2bb84df85237f23ebf6e0f3dfecfd72d615b6.camel@perches.com>
Date: Fri, 21 Nov 2025 20:51:19 -0800
From: Joe Perches <joe@...ches.com>
To: Kees Cook <kees@...nel.org>, Vlastimil Babka <vbabka@...e.cz>
Cc: Andy Whitcroft <apw@...onical.com>, Dwaipayan Ray
<dwaipayanray1@...il.com>, Lukas Bulwahn <lukas.bulwahn@...il.com>, Randy
Dunlap <rdunlap@...radead.org>, Miguel Ojeda <ojeda@...nel.org>, Przemek
Kitszel <przemyslaw.kitszel@...el.com>, "Gustavo A. R. Silva"
<gustavoars@...nel.org>, Linus Torvalds <torvalds@...ux-foundation.org>,
Matthew Wilcox <willy@...radead.org>, Christoph Lameter <cl@...ux.com>,
Marco Elver <elver@...gle.com>, Vegard Nossum <vegard.nossum@...cle.com>,
Pekka Enberg <penberg@...nel.org>, David Rientjes <rientjes@...gle.com>,
Joonsoo Kim <iamjoonsoo.kim@....com>, Andrew Morton
<akpm@...ux-foundation.org>, Roman Gushchin <roman.gushchin@...ux.dev>,
Harry Yoo <harry.yoo@...cle.com>, Bill Wendling <morbo@...gle.com>, Justin
Stitt <justinstitt@...gle.com>, Jann Horn <jannh@...gle.com>, Greg
Kroah-Hartman <gregkh@...uxfoundation.org>, Sasha Levin
<sashal@...nel.org>, linux-mm@...ck.org, Nathan Chancellor
<nathan@...nel.org>, Peter Zijlstra <peterz@...radead.org>, Nick
Desaulniers <nick.desaulniers+lkml@...il.com>, Jonathan Corbet
<corbet@....net>, Jakub Kicinski <kuba@...nel.org>, Yafang Shao
<laoar.shao@...il.com>, Tony Ambardar <tony.ambardar@...il.com>, Alexander
Lobakin <aleksander.lobakin@...el.com>, Jan Hendrik Farr <kernel@...rr.cc>,
Alexander Potapenko <glider@...gle.com>, linux-kernel@...r.kernel.org,
linux-hardening@...r.kernel.org, linux-doc@...r.kernel.org,
llvm@...ts.linux.dev
Subject: Re: [PATCH v5 3/4] checkpatch: Suggest kmalloc_obj family for
sizeof allocations
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;
> }
> }
> }
Powered by blists - more mailing lists