Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2280078pxa; Mon, 3 Aug 2020 11:59:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzIFKIsA9PsgPQMDQqeXHp57dC/UiMwBT8X6JLo9soQAFO/hCg+L13DuWdBtXlmsnzZw4mO X-Received: by 2002:a17:906:6083:: with SMTP id t3mr17114238ejj.531.1596481178190; Mon, 03 Aug 2020 11:59:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596481178; cv=none; d=google.com; s=arc-20160816; b=R6cKrtRroLYtQSfruENTTziJaTy33fa/UDyc19uFJsgP+znc8RHhAJsKW1UxGNavD6 COCIrUaCn24tPlvEvrL8/xdI/0VDCvmYVDTqwn8o96NEAw82iY4oa4LMXisXks8Ubpss N4QzDms1pheY3DsH5gzruLxTZI7Gktf4Wy8Vn1IXPetdq+rCxX05FO/g2QNeUjh+JqKR ddrHaN9rY4PF7pld7h0gD3SSoHYEK2ytfc55y7BbMU3BO0pinrZv9aAcuAho/Q1Qxg7I Ra3o6uYUMVmHuq98PMd9sRJ30YB7XrDZzcwfavJXgsWh3pNhUXRnyBQWxU3xmrWH0m/W U0kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1QdS0fy0Eqxo2sy6ZpyXla/1BB0SZsD0LwltC3usfSw=; b=WGDu6Lq2rTochPMTNxCd0evqmrka7rsg69sAxJvpWIv2a24XiVIz8MrSj/zDkHgdpe lq88uhjOuxTqOxRZq154v8RNpL3CbObyuFxUejv4bIQJCZCF5+U6w/jfTusMEZaaL7kO bOpk47y0IN8UJOW5PkJS9L0EezKiJ/FMLOuY2NrhdPzY2/KCfYBys3NuTEPrcI5IPc+O XpgDGzXoRVFbniB1233sDwxYea3ogL83a0CRlMlZQ6z1dm3TgpS//PG1qcl1FeFioDTn /zI/csmFDF9FYYIbHMNAVhQJWF+WzCPcsancEuBVDll92LobA553YI8MxsYarKkF/FtP q8aQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bv8si10549134ejb.476.2020.08.03.11.59.15; Mon, 03 Aug 2020 11:59:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727066AbgHCS54 (ORCPT + 99 others); Mon, 3 Aug 2020 14:57:56 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:33993 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726130AbgHCS5z (ORCPT ); Mon, 3 Aug 2020 14:57:55 -0400 Received: by mail-lj1-f196.google.com with SMTP id z14so3240074ljm.1 for ; Mon, 03 Aug 2020 11:57:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1QdS0fy0Eqxo2sy6ZpyXla/1BB0SZsD0LwltC3usfSw=; b=QTzsLSomQVybgBr4LcldoQ6b/RqYdfHQUOLgV8KwkyY1CSSUCJLYWfmAPGCimdp82s r41cYdaXFnLoplGmmIovxeVn6kplXTv6rUrdvIzIHW1WQmwPGWDOTILmM0yyAuhEdgYn gtf6M1CzpGi/uPgCOHl5Za+J783KjDJ0W2dG5w5+/EmBJ+x6VnctfYWlAZ8pw5pkPtix UdQGIlUN/Gnoq070BvTWKlxNcomYoIIgUAuN4GOvCuK2qZfkJCammJoVAghNBb6nzxmJ h5oa7O40lPhV+TEzqO7DCXbMrNtyTOwtIHncMgSLyEWGnRK/9LhhDaIxMQOJguLvXEU4 FB1Q== X-Gm-Message-State: AOAM532bNQ9ehndCVbe4CUycULG9N53qPsJgZ+/M+o7fZ1RuDSKo4lyQ 4dTXo8wnlnEoDhhfHYQ1jkc= X-Received: by 2002:a2e:804b:: with SMTP id p11mr9084289ljg.221.1596481073216; Mon, 03 Aug 2020 11:57:53 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-38-130.ip.moscow.rt.ru. [37.110.38.130]) by smtp.googlemail.com with ESMTPSA id r8sm3042645lfm.42.2020.08.03.11.57.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2020 11:57:52 -0700 (PDT) From: Denis Efremov To: Julia Lawall Cc: Denis Efremov , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH v2] coccinelle: api: add kvmalloc script Date: Mon, 3 Aug 2020 21:57:49 +0300 Message-Id: <20200803185749.39034-1-efremov@linux.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200803105601.12162-1-efremov@linux.com> References: <20200803105601.12162-1-efremov@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Suggest kvmalloc instead of opencoded kmalloc && vmalloc condition. Signed-off-by: Denis Efremov --- Changes in v2: - binary operator cmp added - NULL comparisions simplified - "T x" case added to !patch mode scripts/coccinelle/api/kvmalloc.cocci | 142 ++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 scripts/coccinelle/api/kvmalloc.cocci diff --git a/scripts/coccinelle/api/kvmalloc.cocci b/scripts/coccinelle/api/kvmalloc.cocci new file mode 100644 index 000000000000..20b22e3d0f74 --- /dev/null +++ b/scripts/coccinelle/api/kvmalloc.cocci @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Find if/else condition with kmalloc/vmalloc calls. +/// Suggest to use kvmalloc instead. +/// +// Confidence: High +// Copyright: (C) 2020 Denis Efremov ISPRAS +// Options: --no-includes --include-headers +// + +virtual patch +virtual report +virtual org +virtual context + +@opportunity depends on !patch@ +expression E, E1, size; +binary operator cmp = {<=, <, ==, >, >=}; +identifier x; +type T; +position p; +@@ + +( +* if (size cmp E1 || ...)@p { + ... +* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\| +* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...) + ... + } else { + ... +* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...) + ... + } +| +* E = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\| +* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...) + ... when != E = E1 + when != size = E1 + when any +* if (E == NULL)@p { + ... +* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...) + ... + } +| +* T x = \(kmalloc\|kzalloc\|kcalloc\|kmalloc_node\|kzalloc_node\| +* kmalloc_array\|kmalloc_array_node\|kcalloc_node\)(..., size, ...); + ... when != x = E1 + when != size = E1 + when any +* if (x == NULL)@p { + ... +* x = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...) + ... + } +) + +@depends on patch@ +expression E, E1, flags, size, node; +binary operator cmp = {<=, <, ==, >, >=}; +identifier x; +type T; +@@ + +( +- if (size cmp E1) +- E = kmalloc(size, flags); +- else +- E = vmalloc(size); ++ E = kvmalloc(size, flags); +| +- E = kmalloc(size, flags | __GFP_NOWARN); +- if (E == NULL) +- E = vmalloc(size); ++ E = kvmalloc(size, flags); +| +- T x = kmalloc(size, flags | __GFP_NOWARN); +- if (x == NULL) +- x = vmalloc(size); ++ T x = kvmalloc(size, flags); +| +- if (size cmp E1) +- E = kzalloc(size, flags); +- else +- E = vzalloc(size); ++ E = kvzalloc(size, flags); +| +- E = kzalloc(size, flags | __GFP_NOWARN); +- if (E == NULL) +- E = vzalloc(size); ++ E = kvzalloc(size, flags); +| +- T x = kzalloc(size, flags | __GFP_NOWARN); +- if (x == NULL) +- x = vzalloc(size); ++ T x = kvzalloc(size, flags); +| +- if (size cmp E1) +- E = kmalloc_node(size, flags, node); +- else +- E = vmalloc_node(size, node); ++ E = kvmalloc_node(size, flags, node); +| +- E = kmalloc_node(size, flags | __GFP_NOWARN, node); +- if (E == NULL) +- E = vmalloc_node(size, node); ++ E = kvmalloc_node(size, flags, node); +| +- T x = kmalloc_node(size, flags | __GFP_NOWARN, node); +- if (x == NULL) +- x = vmalloc_node(size, node); ++ T x = kvmalloc_node(size, flags, node); +| +- if (size cmp E1) +- E = kvzalloc_node(size, flags, node); +- else +- E = vzalloc_node(size, node); ++ E = kvzalloc_node(size, flags, node); +| +- E = kvzalloc_node(size, flags | __GFP_NOWARN, node); +- if (E == NULL) +- E = vzalloc_node(size, node); ++ E = kvzalloc_node(size, flags, node); +| +- T x = kvzalloc_node(size, flags | __GFP_NOWARN, node); +- if (x == NULL) +- x = vzalloc_node(size, node); ++ T x = kvzalloc_node(size, flags, node); +) + +@script: python depends on report@ +p << opportunity.p; +@@ + +coccilib.report.print_report(p[0], "WARNING: opportunity for kvmalloc") + +@script: python depends on org@ +p << opportunity.p; +@@ + +coccilib.org.print_todo(p[0], "WARNING: opportunity for kvmalloc") -- 2.26.2