Many new generic allocation functions like the
kvmalloc family have been added recently to the kernel.

The allocation functions test now includes:

o kvmalloc and variants
o kstrdup_const
o kmemdup_nul
o dma_alloc_coherent
o alloc_skb and variants

Add a separate $allocFunctions variable to help make
the allocation functions test a bit more readable.

Miscellanea:

o Use $allocFunctions in the unnecessary OOM message test and
  add exclude uses with __GFP_NOWARN
o Use $allocFunctions in the unnecessary cast test
o Add the kvmalloc family to the preferred sizeof alloc style
  foo = kvmalloc(sizeof(*foo), ...)

Signed-off-by: Joe Perches <j...@perches.com>
---
 scripts/checkpatch.pl | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 155fa9305166..551da83cb00f 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -466,6 +466,16 @@ our $logFunctions = qr{(?x:
        seq_vprintf|seq_printf|seq_puts
 )};
 
+our $allocFunctions = qr{(?x:
+       (?:(?:devm_)?
+               (?:kv|k|v)[czm]alloc(?:_node|_array)? |
+               kstrdup(?:_const)? |
+               kmemdup(?:_nul)?) |
+       (?:\w+)?alloc_skb(?:ip_align)? |
+                               # dev_alloc_skb/netdev_alloc_skb, et al
+       dma_alloc_coherent
+)};
+
 our $signature_tags = qr{(?xi:
        Signed-off-by:|
        Co-developed-by:|
@@ -5547,7 +5557,8 @@ sub process {
                        my ($s, $c) = ctx_statement_block($linenr - 3, 
$realcnt, 0);
 #                      print("line: <$line>\nprevline: <$prevline>\ns: 
<$s>\nc: <$c>\n\n\n");
 
-                       if ($s =~ /(?:^|\n)[ 
\+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/)
 {
+                       if ($s =~ /(?:^|\n)[ 
\+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/
 &&
+                           $s !~ /\b__GFP_NOWARN\b/ ) {
                                WARN("OOM_MESSAGE",
                                     "Possible unnecessary 'out of memory' 
message\n" . $hereprev);
                        }
@@ -6198,8 +6209,8 @@ sub process {
                        }
                }
 
-# check for pointless casting of kmalloc return
-               if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
+# check for pointless casting of alloc functions
+               if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
                        WARN("UNNECESSARY_CASTS",
                             "unnecessary cast may hide bugs, see 
http://c-faq.com/malloc/mallocnocast.html\n"; . $herecurr);
                }
@@ -6207,7 +6218,7 @@ sub process {
 # alloc style
 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
                if ($perl_version_ok &&
-                   $line =~ 
/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/)
 {
+                   $line =~ 
/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/)
 {
                        CHK("ALLOC_SIZEOF_STRUCT",
                            "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . 
$herecurr);
                }



Reply via email to