Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1961795pxa; Mon, 3 Aug 2020 03:59:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwe2J6J7hj0C4dLpUHPM+2Y7Yrf/1G5IUF8Os7rvv/NHzv3qPCWh51IxjwxnkQofEaTEu6t X-Received: by 2002:a17:907:444c:: with SMTP id on20mr16900148ejb.77.1596452371483; Mon, 03 Aug 2020 03:59:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596452371; cv=none; d=google.com; s=arc-20160816; b=Hnj7t50uz5qm1lZd1eysLtEjBR17kBB6nLWymu+H3QaaBzG4krHmCVoWpSwHI3aKqz Hoadgh4qhYE2DPM7ZIessFUYM3bv4E2V3ImZ6Ow6moOSW4/AWjBnwUxadA4X42RI/uOF Cwar84ujDdbPcXLZH+yJfd4BuXTCRLPlp3P0cdGmAVUkBaQicjJcfq7wdRwevVZfo8fs y9j63pJQVcDcNnOlnt910N4YhZm0mRR3eJQb0kA6vjm+nLZMVj9cOdYmMqIBEIbCEuwx CTqTSWnFv2pbgKu962AMqG2Ouq1BCCbeHdeM5xo4+v361keSCUy+E45ZGJTJiERUBYhf 9afQ== 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 :message-id:date:subject:cc:to:from; bh=MMWR59ppgwvDkLer5o1rKBFEg1vToLpYYmzQrSMwZDA=; b=F4BaAs+WCxpzPtlhmIo4U4vnjFTa7JlEWJB4KclgA/3UbUTcP0RvyHRBWbust8RYgk dbiVcuQ5Hw9vPZdtQmq4H36wPW2CDQ8OCGp4dJNJjNENjyMa48dTVEnzfDzpUvTQQm/R 8kHyYprJZbKOWc4G8z5fyVR3JYTkB/iIhgYNFy0csg825Cac7I9aolxmjRFTcLFexi0z 31769T9TfgKb5rnZDzFy324Le19diWiZqqINOiEkxuWE123m8Hg2fkGUC9OI6wsEmGUs qeWoDxkGveMCCq1T92ewVPlIntEFFmLdJ7Sgc926K4gLl4woNpYJy0+K/oUCbH8TAvJ3 zbRQ== 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 d2si5693426edq.1.2020.08.03.03.59.08; Mon, 03 Aug 2020 03:59:31 -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 S1726394AbgHCK4O (ORCPT + 99 others); Mon, 3 Aug 2020 06:56:14 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:43105 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725948AbgHCK4N (ORCPT ); Mon, 3 Aug 2020 06:56:13 -0400 Received: by mail-lf1-f65.google.com with SMTP id b11so20230652lfe.10 for ; Mon, 03 Aug 2020 03:56:12 -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:mime-version :content-transfer-encoding; bh=MMWR59ppgwvDkLer5o1rKBFEg1vToLpYYmzQrSMwZDA=; b=eRUqCCTmW7PQ7kVVunyZzd6xAsS5J3Al6NeLdWLug8J+Fh2n85scALuOAOheJy0jMD lAR2w6Nsdu6jywVV6yoVzn88pTjecDkUe0YPKWlXAL716ftN07DwnpIgprotqR1WLoxJ CqgysDTzxc3iDcFVq/R4pVN3bzwuAZ1VtaeIdj25dOARmr4lTaZgS/Gxg7YFiwgAIQ4h lD/zcBtGk172quYUulROuf8dCZGuT8qET7zVe5Rk8JnPSO5tqpLBLcVtC9kpSNRHj4Ts YQ0Tgf+JH9zW0vlF3H15maFYmC3whBmJJ3H0WTjqCtzxeYcnT8fVyiyxkLOpOYwa0CQH +XWQ== X-Gm-Message-State: AOAM530WtdiHwFy18zC0sALaPoJpj+D2x3vcy10NsWM6c0GwnIjXZWh0 gmq/c2SqnByi0Ef9fLEQgp0IGTAi X-Received: by 2002:a19:c206:: with SMTP id l6mr8177709lfc.152.1596452171263; Mon, 03 Aug 2020 03:56:11 -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 d5sm878932ljc.80.2020.08.03.03.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2020 03:56:10 -0700 (PDT) From: Denis Efremov To: Julia Lawall Cc: Denis Efremov , cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [PATCH] coccinelle: api: add kvmalloc script Date: Mon, 3 Aug 2020 13:56:01 +0300 Message-Id: <20200803105601.12162-1-efremov@linux.com> X-Mailer: git-send-email 2.26.2 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 --- If coccinelle fails with "Segmentation fault" during analysis, then one needs to increase stack limit, e.g. ulimit -s 32767. Current, I've sent only one patch for this rule and will send the rest after the merge window. https://lkml.org/lkml/2020/7/31/986 scripts/coccinelle/api/kvmalloc.cocci | 127 ++++++++++++++++++++++++++ 1 file changed, 127 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..76d6aeab7c09 --- /dev/null +++ b/scripts/coccinelle/api/kvmalloc.cocci @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Find conditions in code for 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; +position p; +@@ + +( +* if (\(size <= E1\|size < E1\|size == E1\|size > 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\|E == NULL\))@p { + ... +* E = \(vmalloc\|vzalloc\|vmalloc_node\|vzalloc_node\)(..., size, ...) + ... + } +) + +@depends on patch@ +expression E, E1, flags, size, node; +identifier x; +type T; +@@ + +( +- if (\(size <= E1\|size < E1\|size == E1\|size > E1\)) +- E = kmalloc(size, flags); +- else +- E = vmalloc(size); ++ E = kvmalloc(size, flags); +| +- E = kmalloc(size, flags | __GFP_NOWARN); +- if (\(!E\|E == NULL\)) +- E = vmalloc(size); ++ E = kvmalloc(size, flags); +| +- T x = kmalloc(size, flags | __GFP_NOWARN); +- if (\(!x\|x == NULL\)) +- x = vmalloc(size); ++ T x = kvmalloc(size, flags); +| +- if (\(size <= E1\|size < E1\|size == E1\|size > E1\)) +- E = kzalloc(size, flags); +- else +- E = vzalloc(size); ++ E = kvzalloc(size, flags); +| +- E = kzalloc(size, flags | __GFP_NOWARN); +- if (\(!E\|E == NULL\)) +- E = vzalloc(size); ++ E = kvzalloc(size, flags); +| +- T x = kzalloc(size, flags | __GFP_NOWARN); +- if (\(!x\|x == NULL\)) +- x = vzalloc(size); ++ T x = kvzalloc(size, flags); +| +- if (\(size <= E1\|size < E1\|size == E1\|size > 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\|E == NULL\)) +- E = vmalloc_node(size, node); ++ E = kvmalloc_node(size, flags, node); +| +- T x = kmalloc_node(size, flags | __GFP_NOWARN, node); +- if (\(!x\|x == NULL\)) +- x = vmalloc_node(size, node); ++ T x = kvmalloc_node(size, flags, node); +| +- if (\(size <= E1\|size < E1\|size == E1\|size > 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\|E == NULL\)) +- E = vzalloc_node(size, node); ++ E = kvzalloc_node(size, flags, node); +| +- T x = kvzalloc_node(size, flags | __GFP_NOWARN, node); +- if (\(!x\|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