mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Manuel Ebner <manuelebner@mailbox.org>
To: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
	open list <linux-kernel@vger.kernel.org>,
	Kees Cook <kees@kernel.org>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Lukas Bulwahn <lukas.bulwahn@gmail.com>
Subject: [RFC] scripts: checkpatch.pl: type aware alloc: add suggestions to script output
Date: Sat, 02 May 2026 16:43:42 +0200	[thread overview]
Message-ID: <a56939f2f2f832349e47aaa511da7c21cefadc6d.camel@mailbox.org> (raw)

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,/;

                 reply	other threads:[~2026-05-02 14:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a56939f2f2f832349e47aaa511da7c21cefadc6d.camel@mailbox.org \
    --to=manuelebner@mailbox.org \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®