[PATCH 02/22] introduce cpumask.cocci

From: Yury Norov
Date: Tue May 10 2022 - 11:58:00 EST


Using cpumask_weight() to compare weight of cpumask against a number
or expression is common but wrong pattern. The more efficient way is
to use cpumask_weight_{empty,full,gt,lt,ge,le,eq} as appropriate.

This patch adds cocci script to test sources for this.

CC: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
CC: Julia Lawall <Julia.Lawall@xxxxxxxx>
CC: Nicolas Palix <nicolas.palix@xxxxxxx>
CC: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
CC: linux-kernel@xxxxxxxxxxxxxxx
CC: cocci@xxxxxxxx
Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
---
MAINTAINERS | 1 +
scripts/coccinelle/api/cpumask.cocci | 51 ++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
create mode 100644 scripts/coccinelle/api/cpumask.cocci

diff --git a/MAINTAINERS b/MAINTAINERS
index 17fd10824636..ecd97fdd5d67 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3524,6 +3524,7 @@ F: lib/find_bit_benchmark.c
F: lib/nodemask.c
F: lib/test_bitmap.c
F: scripts/coccinelle/api/bitmap.cocci
+F: scripts/coccinelle/api/cpumask.cocci
F: tools/include/linux/bitmap.h
F: tools/include/linux/find.h
F: tools/lib/bitmap.c
diff --git a/scripts/coccinelle/api/cpumask.cocci b/scripts/coccinelle/api/cpumask.cocci
new file mode 100644
index 000000000000..2eb930dbe9c4
--- /dev/null
+++ b/scripts/coccinelle/api/cpumask.cocci
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Use cpumask_empty rather than cpumask_weight() == 0 etc
+///
+// Confidence: High
+// Copyright: (C) 2022 Yury Norov
+// URL: http://coccinelle.lip6.fr/
+// Comments:
+// Options: --no-includes --include-headers
+
+virtual org
+virtual report
+virtual context
+virtual patch
+
+@rempty1 depends on !patch@
+position p;
+statement S;
+@@
+
+if (cpumask_weight@p(...)) S
+
+@script:python depends on report@
+p << rempty1.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use !cpumask_empty()")
+
+@script:python depends on org@
+p << rempty1.p;
+@@
+
+@rcmp depends on !patch@
+expression exp;
+binary operator cmp = {>, <, >=, <=, ==, !=};
+position p;
+@@
+
+ cpumask_weight(...) cmp@p exp
+
+@script:python depends on report@
+p << rcmp.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0,
+ "ERROR: use cpumask_weight_{empty,full,gt,lt,ge,le,eq} as appropriate")
+
+@script:python depends on org@
+p << rcmp.p;
+@@
--
2.32.0