mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kenneth Lee <klee33@uw.edu>
To: apw@canonical.com, joe@perches.com, dwaipayanray1@gmail.com,
	lukas.bulwahn@gmail.com
Cc: linux-kernel@vger.kernel.org, Kenneth Lee <klee33@uw.edu>
Subject: [PATCH] checkpatch: add ALLOC_UNNECESSARY_ARRAY test
Date: Sat, 20 Aug 2022 14:41:20 -0700	[thread overview]
Message-ID: <20220820214120.497971-1-klee33@uw.edu> (raw)

Using calloc|malloc_array(1, ...) can be simplified to zalloc|malloc(...)
and improves semantics. This is because we are only allocating one element
so there is no need to use the array version of the methods.

This can be applied to kvmalloc_array, kvcalloc, kcalloc, devm_kcalloc,
and devm_kmalloc_array. Note that there is no devm_kvmalloc_array or
devm_kvcalloc, so the regex does not check for this.

Signed-off-by: Kenneth Lee <klee33@uw.edu>
---
This is my first patch that is not a trivial cleanup, so please let me
know if I am approaching something wrong. Also unsure if the warning
name should be something else besides ALLOC_UNNECESSARY_ARRAY
---
 scripts/checkpatch.pl | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 79e759aac543..83d3a9f308ea 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7079,6 +7079,19 @@ sub process {
 			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
 		}
 
+# check for use of unnecessary array alloc methods
+# calloc|malloc_array(1, ...) should be zalloc|malloc(...)
+		if ($line =~ /(\b(?:devm_(?:kcalloc|kmalloc_array))|\b(?:kv|k)(?:calloc|malloc_array))\s*\(\s*1\s*,/) {
+			my $newfunc = "kmalloc";
+			$newfunc = "kvmalloc" if ($1 eq "kvmalloc_array");
+			$newfunc = "kvzalloc" if ($1 eq "kvcalloc");
+			$newfunc = "kzalloc" if ($1 eq "kcalloc");
+			$newfunc = "devm_kzalloc" if ($1 eq "devm_kcalloc");
+			$newfunc = "devm_kmalloc" if ($1 eq "devm_kmalloc_array");
+			WARN("ALLOC_UNNECESSARY_ARRAY",
+			     "$1(1, ...) can be simplified to $newfunc(...)\n" . $herecurr);
+		}
+
 # check for multiple semicolons
 		if ($line =~ /;\s*;\s*$/) {
 			if (WARN("ONE_SEMICOLON",
-- 
2.31.1


             reply	other threads:[~2022-08-20 21:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-20 21:41 Kenneth Lee [this message]
2022-08-21  0:47 ` Joe Perches

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=20220820214120.497971-1-klee33@uw.edu \
    --to=klee33@uw.edu \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --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®