From patchwork Wed Sep 13 20:37:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 139084 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:a8d:b0:3f2:4152:657d with SMTP id gr13csp79923vqb; Wed, 13 Sep 2023 13:53:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0KrUFugsugcq13fS2FNr+fwpgophVBAXvo/WEVFkrYhH8S3u/B/w8Rw3HGcy4o0VM8Pos X-Received: by 2002:a17:902:e5cc:b0:1c3:d07f:39f7 with SMTP id u12-20020a170902e5cc00b001c3d07f39f7mr4890701plf.62.1694638391654; Wed, 13 Sep 2023 13:53:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694638391; cv=none; d=google.com; s=arc-20160816; b=s7ngzdvlhfaPxaJvI4GdTjJ6y+jgwuVRPbHW53BTxOPAFBkKXEsXaq7mX0EeMaN2ew pvgLYcrkE8TwQOgefEmvX0fEEJKn6TKttoAGTsV9x/3IdNlG1d48atOFwc0i35vXCYze g5WL8+P1ntOgOQFPUryEzBFYPFH8o3LLgxUo9ZPK7d6W7JS6NpvJ7q7qGhbSN1Zqk5SN 3p9FbToI2f9hbnSYf6a43qMttHW6RCVpYTBSCvQDblpIqgA+N9K8zmqpiz7IxZKoPImu ahqVWU98zTgIR4EMClw68AWrFnyTlQwzQ1Gb3eBJ7dt2z7EIBweDWXqHyPfKcf95SI9S Bk5A== 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=uzlREZxElhzueAbjbw+1BtuziLOgZvYayFmIdjPcTAE=; fh=bqPn+niz5aQZsD6V/8OOajUf9N3p5m0PF4Bpg+1BhpQ=; b=GV41aBGhiuD2o5rJt6CjFELR/1VApKm5pYLQo88zKPj9/GYjZPUcOMQOe7tUxD1qKN +gzJs71zrng4ZjrCKXhY7ATJ1lVcB5sQdutGfFwspnx+YemHVanO1QKTiDa/Ro3ltJOs 7WvxATvVcTbpxZLzuP2z20IwClTa/9LiY/b0hEfLrCpUP8EZOAcIfrXxodPyDCCMsjPx q95pxzU8Hdi2+chdvD7xlpmmzafqneymt/CQxY3ZMGBZms04BJwqv0wntxW5jPhM4eez +MExi4aVHMNW83FtZkV99CKoNb8+5PEkWegdOOyMX42MldQmAYCDmIwRbSRPhVruwgjD kHww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id s3-20020a170902c64300b001bc434b9d95si101682pls.156.2023.09.13.13.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 13:53:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 790D6848314B; Wed, 13 Sep 2023 13:38:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232665AbjIMUiO (ORCPT + 34 others); 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 X-Rspamd-Queue-Id: 0B01A2000E X-Spam-Status: No, score=-0.10 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 13:38:15 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776957146258679573 X-GMAIL-MSGID: 1776957146258679573 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 = ( #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)\s*,/) { 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; } } }