From patchwork Thu Sep 14 01:41:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 139224 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp49286vqi; Wed, 13 Sep 2023 18:42:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZ4Oq4MHmRUIuxtXt87Zs73gzDa7B87jgQhxvFonD2hSDWa7njrdltzaUJrHwEKqylt9hk X-Received: by 2002:a05:6808:1141:b0:3a7:1bd8:4eb6 with SMTP id u1-20020a056808114100b003a71bd84eb6mr5371647oiu.44.1694655744529; Wed, 13 Sep 2023 18:42:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694655744; cv=none; d=google.com; s=arc-20160816; b=GpUeCkfnaSB9JT1Tk4TuAFUrs2idadaIVPdfq+aoi55AC4FBO1WPrVhrkKflKi8pSi xigD1BRWKG8ajl8feYxCg4JxgdGG8fCE8NZqK+ASDX1RkmBK31HaGfdIV3OE5twi0cu8 iduTll5ZCOEzKg+m7JbsYa+t5RdPS1HO5JGldobdHTYiNrOSSrryGxZriT/IGcoNeALb Psi4YR4uFSBaTSxqw7gLIxwkKotr03A4lctKccDmEDfiBLjBYXkgZveuAznQ2XsQTVj/ e9uV4WB94cuCS8azJsMzDpqIsZfBmEtvz7+z4qGNkDUHY/5sQjkJkLJMMDLmSYD9EDLH tvxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=OfBGYlNXgJnxP/IxbVw0xSrgseSCo8GCgYQmjYSxn7Q=; fh=sImqnVp5HXWJzeGR9OVtHHlkDZ2s2l11nAxa+/tYIJc=; b=TZqHP6oJJcesaMaYPDIHWj2MnP1EgZA67tCfFycg0pKsOtw5fL+qhEo78ldZqVYOqT WDokEbjIdh7Tyu8Q3alOEe8KZqFF8kmZFZjIgWlcCuHV+p7ovYTXgS43AZZ/2NwUvync sQ/c2mD0UJnqSJNpmuw1z9oU6hWwel/yFwRTsVuQxyKDKpdC7g2USK7+1e8pXWlOCV2h ENGd7a03/vO7MUDOaZN8HfsOEOsI4QUQv4rV8bhMbTZjAzAtAIfoyCwTe0pFjBKfg0i6 c1fJ/BBnE98cefUHmHw0l5ihy/damG2JJiGcunQYjKU9CFNW6kOFwxax2EYTogZHcPej lozA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id s6-20020a656446000000b00573fc1f288csi378893pgv.569.2023.09.13.18.42.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 18:42:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 583A68318050; Wed, 13 Sep 2023 18:42:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233598AbjINBl6 (ORCPT + 35 others); 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 X-Rspamd-Server: rspamout03 X-Rspamd-Queue-Id: 5D9EB2E X-Stat-Signature: 64s9gkepqudbxw6q3mhphyrgas9f9n5t X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 13 Sep 2023 18:42:13 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776957146258679573 X-GMAIL-MSGID: 1776975341497398854 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 = ( #Create a search pattern for all these strings to speed up a loop below our $deprecated_apis_search = '(?:' . join('|', keys %deprecated_apis) . ')'; +our %alloc_with_multiply_apis = ( + "kmalloc" => "kmalloc_array", + "kvmalloc" => "kvmalloc_array", + "vmalloc" => "vmalloc_array", + "kvzalloc" => "kvcalloc", + "kzalloc" => "kcalloc", + "vzalloc" => "vcalloc", +); +our $alloc_with_multiply_search = '(?:' . join('|', keys %alloc_with_multiply_apis) . ')'; + our $mode_perms_world_writable = qr{ S_IWUGO | S_IWOTH | @@ -7187,17 +7197,14 @@ sub process { "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); } -# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc +# check for various allocs with multiplies that should use safer functions if ($perl_version_ok && defined $stat && - $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { + $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*($alloc_with_multiply_search)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/) { my $oldfunc = $3; + my $newfunc = $alloc_with_multiply_apis{$oldfunc}; my $a1 = $4; my $a2 = $10; - my $newfunc = "kmalloc_array"; - $newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc"); - $newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc"); - $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); my $r1 = $a1; my $r2 = $a2; if ($a1 =~ /^sizeof\s*\S/) { @@ -7213,7 +7220,7 @@ sub process { "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) && $cnt == 1 && $fix) { - $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; + $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*($oldfunc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; } } }