From nobody Thu Feb 12 02:00:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72EBFEE0211 for ; Wed, 13 Sep 2023 20:38:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232654AbjIMUiH (ORCPT ); Wed, 13 Sep 2023 16:38:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232632AbjIMUiF (ORCPT ); Wed, 13 Sep 2023 16:38:05 -0400 Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A63261BC6 for ; Wed, 13 Sep 2023 13:38:01 -0700 (PDT) Received: from omf16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7DDED1A0DB3; Wed, 13 Sep 2023 20:38:00 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf16.hostedemail.com (Postfix) with ESMTPA id 668C42000E; Wed, 13 Sep 2023 20:37:58 +0000 (UTC) From: Joe Perches To: Andrew Morton , Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn Cc: Gustavo Silva , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] checkpatch: Simplify creating search strings Date: Wed, 13 Sep 2023 13:37:51 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 668C42000E X-Rspamd-Server: rspamout06 X-Stat-Signature: aig5jh5ba1bsssmfixgizfssewqsei5t X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX19uOctU2Ce8H1bSgmQLe4M/7y7V0x72Py4= X-HE-Tag: 1694637478-717840 X-HE-Meta: U2FsdGVkX1+8zdzFunbnL6OYE3TD/jnVW9qkuNeJy91kNora1rdp3TEvaPjQlUttQeEqUdrxLr2S5VEbtnwHGv7ozUcuQNZhBm2bXCOm+8vBY3V0ue70BrLDe5p0M5+av18oI319qtPXeVYgsC8nlHDms8wxEFDn5d0kfiYX56S/etsyCk1zRysKMp666vY9K36+SeHfNUbMKTLqI2Kqz943GVsHaXfSn/mn4X9fb7Wxhchf1eKD7oDoxsQ1Mih29BMkN8YGZGbQ/b1dNq6P0IGnrE5FEujbOdDLNgH4fq2dCpEW4fpPz9x3I67/zJkNJQfta3rcCqs= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Use join and map instead of loops. Signed-off-by: Joe Perches --- scripts/checkpatch.pl | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 7d16f863edf1c..617f9e53bacdf 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -625,18 +625,8 @@ our $signature_tags =3D qr{(?xi: our @link_tags =3D qw(Link Closes); =20 #Create a search and print patterns for all these strings to be used direc= tly below -our $link_tags_search =3D ""; -our $link_tags_print =3D ""; -foreach my $entry (@link_tags) { - if ($link_tags_search ne "") { - $link_tags_search .=3D '|'; - $link_tags_print .=3D ' or '; - } - $entry .=3D ':'; - $link_tags_search .=3D $entry; - $link_tags_print .=3D "'$entry'"; -} -$link_tags_search =3D "(?:${link_tags_search})"; +our $link_tags_search =3D '(?:' . join('|', @link_tags) . ')'; +our $link_tags_print =3D "'" . join("' or '", @link_tags) . "'"; =20 our $tracing_logging_tags =3D qr{(?xi: [=3D-]*> | @@ -819,15 +809,10 @@ our @mode_permission_funcs =3D ( ["__ATTR", 2], ); =20 -my $word_pattern =3D '\b[A-Z]?[a-z]{2,}\b'; - #Create a search pattern for all these functions to speed up a loop below -our $mode_perms_search =3D ""; -foreach my $entry (@mode_permission_funcs) { - $mode_perms_search .=3D '|' if ($mode_perms_search ne ""); - $mode_perms_search .=3D $entry->[0]; -} -$mode_perms_search =3D "(?:${mode_perms_search})"; +our $mode_perms_search =3D '(?:' . join('|', map{$_->[0]} @mode_permission= _funcs) . ')'; + +my $word_pattern =3D '\b[A-Z]?[a-z]{2,}\b'; =20 our %deprecated_apis =3D ( "synchronize_rcu_bh" =3D> "synchronize_rcu", @@ -847,12 +832,7 @@ our %deprecated_apis =3D ( ); =20 #Create a search pattern for all these strings to speed up a loop below -our $deprecated_apis_search =3D ""; -foreach my $entry (keys %deprecated_apis) { - $deprecated_apis_search .=3D '|' if ($deprecated_apis_search ne ""); - $deprecated_apis_search .=3D $entry; -} -$deprecated_apis_search =3D "(?:${deprecated_apis_search})"; +our $deprecated_apis_search =3D '(?:' . join('|', keys %deprecated_apis) .= ')'; =20 our $mode_perms_world_writable =3D qr{ S_IWUGO | @@ -887,7 +867,7 @@ foreach my $entry (keys %mode_permission_string_types) { $mode_perms_string_search .=3D '|' if ($mode_perms_string_search ne ""); $mode_perms_string_search .=3D $entry; } -our $single_mode_perms_string_search =3D "(?:${mode_perms_string_search})"; +our $single_mode_perms_string_search =3D '(?:' . join('|', keys %mode_perm= ission_string_types) . ')'; our $multi_mode_perms_string_search =3D qr{ ${single_mode_perms_string_search} (?:\s*\|\s*${single_mode_perms_string_search})* --=20 2.41.0 From nobody Thu Feb 12 02:00:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15276EE0203 for ; Thu, 14 Sep 2023 01:41:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233626AbjINBl6 (ORCPT ); Wed, 13 Sep 2023 21:41:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233345AbjINBl5 (ORCPT ); Wed, 13 Sep 2023 21:41:57 -0400 Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0831D1BD0 for ; Wed, 13 Sep 2023 18:41:52 -0700 (PDT) Received: from omf18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 700E5C0ED3; Thu, 14 Sep 2023 01:41:51 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf18.hostedemail.com (Postfix) with ESMTPA id 5D9EB2E; Thu, 14 Sep 2023 01:41:49 +0000 (UTC) From: Joe Perches To: Andrew Morton , Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn Cc: "Gustavo A . R . Silva" , linux-kernel@vger.kernel.org Subject: [PATCH V2 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check Date: Wed, 13 Sep 2023 18:41:47 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspamout03 X-Rspamd-Queue-Id: 5D9EB2E X-Stat-Signature: 64s9gkepqudbxw6q3mhphyrgas9f9n5t X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX18IvTadWSku2lXX3mRrqjmkVKInnXd5ObY= X-HE-Tag: 1694655709-754970 X-HE-Meta: U2FsdGVkX19X7SO++FS+W2npsmESX/N4ji5Hvf5FkffJk2a+F5sr6Seerxk2h/QrGsEp/dT0+m/PmAb5NJWRwzXCXepqEA5EU/0lwQ5vGHwm0EaxssmdkOAgluSsGKt3aQs1K8103KB1GsrGqv5+aqf/EZG47D2bBSEMtWRgT7eI7VB4NrCXeIMphtKkjixY/R7gwn5iO3Y0FTVMqhdOGPzOKZV5FlkwX/9F11R6Hw/zxmxUhl59h/phR7UyejZOdj7G/8jGBkv+uH3xsZRSVQGEna3ZUDg8V9KSAtoq0HDq798lGjM9h2XU3yMu4QA/Gfka0VIjz4vl7pG7MGXOJJt+Pi9W9b69DSNyiVMnKFOGiNERcVp2l5oXOmvtOYOiddOKVo8WLEosNjeWfuuQYx1nP8Mb2WX5 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly. Add alloc-with-multiplies checks for these new functions. Simplify the original codes repeated else to use a hash. Link: https://github.com/KSPP/linux/issues/342 Original-patch-by: Gustavo A. R. Silva Co-developed-by: Gustavo A. R. Silva Signed-off-by: Gustavo A. R. Silva Link: https://lore.kernel.org/lkml/ZQCaO+tYycDxVLy7@work/ Signed-off-by: Joe Perches --- v2: Fix search because vmalloc functions don't take a 3rd argument scripts/checkpatch.pl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 617f9e53bacdf..4cb248985eefc 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -834,6 +834,16 @@ our %deprecated_apis =3D ( #Create a search pattern for all these strings to speed up a loop below our $deprecated_apis_search =3D '(?:' . join('|', keys %deprecated_apis) .= ')'; =20 +our %alloc_with_multiply_apis =3D ( + "kmalloc" =3D> "kmalloc_array", + "kvmalloc" =3D> "kvmalloc_array", + "vmalloc" =3D> "vmalloc_array", + "kvzalloc" =3D> "kvcalloc", + "kzalloc" =3D> "kcalloc", + "vzalloc" =3D> "vcalloc", +); +our $alloc_with_multiply_search =3D '(?:' . join('|', keys %alloc_with_mul= tiply_apis) . ')'; + our $mode_perms_world_writable =3D qr{ S_IWUGO | S_IWOTH | @@ -7187,17 +7197,14 @@ sub process { "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); } =20 -# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kv= malloc_array/kvcalloc/kcalloc +# check for various allocs with multiplies that should use safer functions if ($perl_version_ok && defined $stat && - $stat =3D~ /^\+\s*($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*((?:kv|k)= [mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { + $stat =3D~ /^\+\s*($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*($alloc_w= ith_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) { my $oldfunc =3D $3; + my $newfunc =3D $alloc_with_multiply_apis{$oldfunc}; my $a1 =3D $4; my $a2 =3D $10; - my $newfunc =3D "kmalloc_array"; - $newfunc =3D "kvmalloc_array" if ($oldfunc eq "kvmalloc"); - $newfunc =3D "kvcalloc" if ($oldfunc eq "kvzalloc"); - $newfunc =3D "kcalloc" if ($oldfunc eq "kzalloc"); my $r1 =3D $a1; my $r2 =3D $a2; if ($a1 =3D~ /^sizeof\s*\S/) { @@ -7213,7 +7220,7 @@ sub process { "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && $cnt =3D=3D 1 && $fix) { - $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*= ((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' =3D ' . "$ne= wfunc(" . trim($r1) . ', ' . trim($r2)/e; + $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*= ($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' =3D ' . "$newfunc(" .= trim($r1) . ', ' . trim($r2)/e; } } } --=20 2.41.0 From nobody Thu Feb 12 02:00:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C463EE020D for ; Wed, 13 Sep 2023 20:38:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232691AbjIMUiO (ORCPT ); Wed, 13 Sep 2023 16:38:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232656AbjIMUiI (ORCPT ); Wed, 13 Sep 2023 16:38:08 -0400 Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6AFB21BCB for ; Wed, 13 Sep 2023 13:38:04 -0700 (PDT) Received: from omf16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 32609160494; Wed, 13 Sep 2023 20:38:03 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf16.hostedemail.com (Postfix) with ESMTPA id 0B01A2000E; Wed, 13 Sep 2023 20:38:00 +0000 (UTC) From: Joe Perches To: Andrew Morton , Andy Whitcroft , Dwaipayan Ray , Lukas Bulwahn Cc: Gustavo Silva , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] checkpatch: Add a couple new alloc functions to alloc with multiplies check Date: Wed, 13 Sep 2023 13:37:52 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 0B01A2000E X-Rspamd-Server: rspamout06 X-Stat-Signature: xjm5zefc7i1hfmefnuramsasuzznxpc6 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX18h5Wco8x/jTt4ifkNztuqruDZ97G4luyg= X-HE-Tag: 1694637480-576201 X-HE-Meta: U2FsdGVkX1/qvO+5ATy0oC1xdPj7yURbZiZ6p1jLPhVLbglW7mKrqeVfYdqE5XaGFecjXtS4WwLrkIDeavUySJSiZFdh2oQoEcyXe6nHZ5soQD5lUgr7HdLmHWo5l2ad26LgFg1Z9LQMFh5noewE80KKrBLPHlaPaSr+uDl/nL3GnCsOb2gt4y8A4TrD41kRFgxUEj4aVbr5125fztpf9Zmp/pcQz3VjzESUWVLoT8Vf8H0VNTdQav3wTvDjmVp/jDQj8jwi//bB5hO15srBsQ1DlxY6B0xwAx0He0eoCzVgYndVq2a9AOcmxJt7eZcUAFAAOXwHB0sC9obPoCama+Jkf7n7U6/kXwDLUdzRaPIMWuhgJmujoEHZYnyrJV1BA/QBLNh34rK6BBcizbS0CXAXHzO2WQo1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" vmalloc() and vzalloc() functions have now 2-factor multiplication argument forms vmalloc_array() and vcalloc(), correspondingly. Add alloc-with-multiplies checks for these new functions. Simplify the original codes repeated else to use a hash. Link: https://github.com/KSPP/linux/issues/342 Original-patch-by: Gustavo A. R. Silva Link: https://lore.kernel.org/lkml/ZQCaO+tYycDxVLy7@work/ Signed-off-by: Joe Perches --- scripts/checkpatch.pl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 617f9e53bacdf..4cb248985eefc 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -834,6 +834,16 @@ our %deprecated_apis =3D ( #Create a search pattern for all these strings to speed up a loop below our $deprecated_apis_search =3D '(?:' . join('|', keys %deprecated_apis) .= ')'; =20 +our %alloc_with_multiply_apis =3D ( + "kmalloc" =3D> "kmalloc_array", + "kvmalloc" =3D> "kvmalloc_array", + "vmalloc" =3D> "vmalloc_array", + "kvzalloc" =3D> "kvcalloc", + "kzalloc" =3D> "kcalloc", + "vzalloc" =3D> "vcalloc", +); +our $alloc_with_multiply_search =3D '(?:' . join('|', keys %alloc_with_mul= tiply_apis) . ')'; + our $mode_perms_world_writable =3D qr{ S_IWUGO | S_IWOTH | @@ -7187,17 +7197,14 @@ sub process { "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); } =20 -# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kv= malloc_array/kvcalloc/kcalloc +# check for various allocs with multiplies that should use safer functions if ($perl_version_ok && defined $stat && - $stat =3D~ /^\+\s*($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*((?:kv|k)= [mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { + $stat =3D~ /^\+\s*($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*($alloc_w= ith_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { my $oldfunc =3D $3; + my $newfunc =3D $alloc_with_multiply_apis{$oldfunc}; my $a1 =3D $4; my $a2 =3D $10; - my $newfunc =3D "kmalloc_array"; - $newfunc =3D "kvmalloc_array" if ($oldfunc eq "kvmalloc"); - $newfunc =3D "kvcalloc" if ($oldfunc eq "kvzalloc"); - $newfunc =3D "kcalloc" if ($oldfunc eq "kzalloc"); my $r1 =3D $a1; my $r2 =3D $a2; if ($a1 =3D~ /^sizeof\s*\S/) { @@ -7213,7 +7220,7 @@ sub process { "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && $cnt =3D=3D 1 && $fix) { - $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*= ((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' =3D ' . "$ne= wfunc(" . trim($r1) . ', ' . trim($r2)/e; + $fixed[$fixlinenr] =3D~ s/\b($Lval)\s*\=3D\s*(?:$balanced_parens)?\s*= ($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' =3D ' . "$newfunc(" .= trim($r1) . ', ' . trim($r2)/e; } } } --=20 2.41.0