Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp793497rwa; Sat, 20 Aug 2022 15:00:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR4JGzuUmv/ZApPrxo4VQNQk5bRUORJ3l5Ur63+/D6nZOTBfs6bPFPneLiriybamNg1I9P++ X-Received: by 2002:a17:907:6d13:b0:731:3be2:9eb with SMTP id sa19-20020a1709076d1300b007313be209ebmr8758177ejc.163.1661032835887; Sat, 20 Aug 2022 15:00:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661032835; cv=none; d=google.com; s=arc-20160816; b=IGSf1sWTMjpd9TL2nqwPAfWcv+o7hcsn8Lzp/9A7xPe7yaeSTfUgTos5SuuOtSBZ6t vv/gxbIbu/0G46LUn6nqNhQC3Hm9vHkN2vLlIeSou7CtF3d6/jEIZvUcE852kaHdXpRA iye0d9nzcrrVRGiegijpDzQfhoxn/Xz+1FKGeJsMwEfvrIlRivuHQ0anDUteHAY49pDo UOJGioVEz4RAErKgCkseyHeISuG0R4p2oxSAcULPFmK34vkLyhfVyLv9q4manXZGW2MR K34YqnPE8dKwQWwBOTJVQoNqTIJtUWnm5gQrgORPj9LBk6ARUTbFX59R+/Qr2q15W5J/ ZBTQ== 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 :message-id:date:subject:cc:to:from; bh=elgiJnY7f9bc8iVOR5qOD9dahLsl/ZFlZW7lhITf2Ps=; b=UM0qdA8H4+gEe9p8AkyKf428q2AkvhVcQkRNzVYi4OlRsycIKUEWTKNUZXmTe8uSyg i6/RiDEP4lCblyfWZ5lmEglJahP48hx2dl73+mpfqyidKJTAs2yfsnO+ETADQlXLGtXJ B5Ifc7tA88/IToUiym11VtzQeiHvzjpfl8EMThOhweNKS7UDsr5LcF/US5qs+IxO3it0 YjqyrPpiVFpyMVIGXjUugsbMDQm0mJWnSdbxktnQ69B2tVvJND1A0RVE6zxFSFijmjnZ ZexsNbQiNItlKo30jJMyyEqBVDbEBhaR+TxsHNI+p0o6p108pcI2lJxH9fd5jVSEfmKV uuEA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=uw.edu Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q23-20020aa7da97000000b0043e564e423csi1175458eds.19.2022.08.20.14.59.58; Sat, 20 Aug 2022 15:00:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=uw.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233550AbiHTVlx (ORCPT + 99 others); Sat, 20 Aug 2022 17:41:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbiHTVlv (ORCPT ); Sat, 20 Aug 2022 17:41:51 -0400 Received: from mx5.cs.washington.edu (mx5.cs.washington.edu [IPv6:2607:4000:200:11::6a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22B2227FEA for ; Sat, 20 Aug 2022 14:41:50 -0700 (PDT) Received: from mx5.cs.washington.edu (localhost [IPv6:0:0:0:0:0:0:0:1]) by mx5.cs.washington.edu (8.17.1/8.17.1/1.26) with ESMTP id 27KLfdHh1089230; Sat, 20 Aug 2022 14:41:39 -0700 Received: from attu4.cs.washington.edu (attu4.cs.washington.edu [IPv6:2607:4000:200:10:0:0:0:8c]) (authenticated bits=128) by mx5.cs.washington.edu (8.17.1/8.17.1/1.26) with ESMTPSA id 27KLfcRO1089226 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=OK); Sat, 20 Aug 2022 14:41:38 -0700 Received: from attu4.cs.washington.edu (localhost [127.0.0.1]) by attu4.cs.washington.edu (8.15.2/8.15.2/1.23) with ESMTP id 27KLfcHB498192; Sat, 20 Aug 2022 14:41:38 -0700 Received: (from klee33@localhost) by attu4.cs.washington.edu (8.15.2/8.15.2/Submit/1.2) id 27KLfcr6498191; Sat, 20 Aug 2022 14:41:38 -0700 From: Kenneth Lee To: apw@canonical.com, joe@perches.com, dwaipayanray1@gmail.com, lukas.bulwahn@gmail.com Cc: linux-kernel@vger.kernel.org, Kenneth Lee Subject: [PATCH] checkpatch: add ALLOC_UNNECESSARY_ARRAY test Date: Sat, 20 Aug 2022 14:41:20 -0700 Message-Id: <20220820214120.497971-1-klee33@uw.edu> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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