[RFC] scripts: checkpatch.pl: type aware alloc: add suggestions to script output
From: Manuel Ebner
Date: Sat May 02 2026 - 10:45:00 EST
hello,
i think i'm far from a patch here, maybe i'll get there.
my change messes up $herectx, but i don't know why.
@ Kees Cook
after comparing the functions of you patch (2932ba8d9c99) with the functions
in deprecated.rst i found the issue. there's a missing ')' on line 391. I'll
fix this in my patchseries "[PATCH v5 3/3] Documentation: deprecated.rst:
kmalloc-family: mark argument as optional"
Just to make sure. I'll look for following vars with Regex:
ptr, *prt, gfp, count and flex_member
ptr = kmalloc(sizeof(*ptr), gfp);
-> ptr = kmalloc_obj(*ptr [, gfp] );
ptr = kzalloc(sizeof(*ptr), gfp);
-> ptr = kzalloc_obj(*ptr [, gfp] );
ptr = kmalloc_array(count, sizeof(*ptr), gfp);
-> ptr = kmalloc_objs(*ptr, count [, gfp] );
ptr = kcalloc(count, sizeof(*ptr), gfp);
-> ptr = kzalloc_objs(*ptr, count [, gfp] );
keywords are:
*alloc_obj(s)
__auto_type, struct, kmalloc_flex.
thoose will be as is in the suggestion
ptr = kmalloc(struct_size(ptr, flex_member, count), gfp);
-> ptr = kmalloc_flex(*ptr, flex_member, count [, gfp] );
ptr = kmalloc(sizeof(struct foo), gfp);
-> __auto_type ptr = kmalloc_obj(struct foo [, gfp] );
Thanks
Manuel
changelog:
---------
[PATCH] scripts: checkpatch.pl: type aware alloc: add suggestions
to script output
goal is to lower the hurdle of implementing type aware alloc by
suggesting the required function.
old output:
WARNING: Prefer kzalloc_obj over kzalloc with sizeof
#42: FILE: deprecated.c:42:
+ ptr = kzalloc(sizeof(*ptr), gfp);
new output:
WARNING: Prefer kmalloc_obj over kmalloc with sizeof
#42: FILE: deprecated.c:42:
+ ptr = kzalloc(sizeof(*ptr), gfp);
Suggestion
+ ptr = kmalloc_obj(*ptr, gfp);
---
scripts/checkpatch.pl | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 0492d6afc9a1..b8e5fbf2c19a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7300,10 +7300,11 @@ sub process {
# check for (kv|k)[mz]alloc that could be
kmalloc_obj/kvmalloc_obj/kzalloc_obj/kvzalloc_obj
if ($perl_version_ok &&
defined $stat &&
- $stat =~
/^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/)
{
+ $stat =~ /(\+?[
\t]*)(\w*)\s=\s((?:kv|k)[mz]alloc)\((sizeof\()?\*(\w*)\),\s*(\w*)\);/) {
my $oldfunc = $3;
my $a1 = $4;
my $newfunc = "kmalloc_obj";
+ my $suggestfunc = "Suggestion -> $1$2 = $3\_obj(\*$5, $6\);";
$newfunc = "kvmalloc_obj" if ($oldfunc eq "kvmalloc");
$newfunc = "kvzalloc_obj" if ($oldfunc eq "kvzalloc");
$newfunc = "kzalloc_obj" if ($oldfunc eq "kzalloc");
@@ -7313,7 +7314,7 @@ sub process {
my $herectx = get_stat_here($linenr, $cnt, $here);
if (WARN("ALLOC_WITH_SIZEOF",
- "Prefer $newfunc over $oldfunc with sizeof\n" .
$herectx) &&
+ "Prefer $newfunc over $oldfunc with sizeof\n" .
$suggestfunc . "\n") &&
$cnt == 1 &&
$fix) {
$fixed[$fixlinenr] =~
s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*,/$1 =
$newfunc($a1,/;