[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220510154750.212913-2-yury.norov@gmail.com>
Date: Tue, 10 May 2022 08:47:29 -0700
From: Yury Norov <yury.norov@...il.com>
To: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
David Laight <David.Laight@...LAB.COM>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Joe Perches <joe@...ches.com>,
Julia Lawall <Julia.Lawall@...ia.fr>,
Michał Mirosław <mirq-linux@...e.qmqm.pl>,
Nicholas Piggin <npiggin@...il.com>,
Nicolas Palix <nicolas.palix@...g.fr>,
Peter Zijlstra <peterz@...radead.org>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Matti Vaittinen <Matti.Vaittinen@...rohmeurope.com>,
linux-kernel@...r.kernel.org
Cc: Yury Norov <yury.norov@...il.com>, cocci@...ia.fr
Subject: [PATCH 01/22] introduce bitmap.cocci
Using bitmap_weight() to compare weight of bitmap against a number or
expression is common but wrong pattern. The more efficient way is to
use bitmap_weight_{empty,full,gt,lt,ge,le,eq} as appropriate.
This patch adds cocci script to test sources for this.
Because for short bitmaps there's no performance adwantage of conditional
bitmap_weight over regular one, and because readability of latter
may be considered better, bitmap_weight_{gt,lt,ge,le,eq} cases are
marked with WARNING.
CC: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
CC: Rasmus Villemoes <linux@...musvillemoes.dk>
CC: Julia Lawall <Julia.Lawall@...ia.fr>
CC: Nicolas Palix <nicolas.palix@...g.fr>
CC: linux-kernel@...r.kernel.org
CC: cocci@...ia.fr
CC: Yury Norov <yury.norov@...il.com>
Signed-off-by: Yury Norov <yury.norov@...il.com>
---
MAINTAINERS | 1 +
scripts/coccinelle/api/bitmap.cocci | 104 ++++++++++++++++++++++++++++
2 files changed, 105 insertions(+)
create mode 100644 scripts/coccinelle/api/bitmap.cocci
diff --git a/MAINTAINERS b/MAINTAINERS
index f57e6d38a542..17fd10824636 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3523,6 +3523,7 @@ F: lib/find_bit.c
F: lib/find_bit_benchmark.c
F: lib/nodemask.c
F: lib/test_bitmap.c
+F: scripts/coccinelle/api/bitmap.cocci
F: tools/include/linux/bitmap.h
F: tools/include/linux/find.h
F: tools/lib/bitmap.c
diff --git a/scripts/coccinelle/api/bitmap.cocci b/scripts/coccinelle/api/bitmap.cocci
new file mode 100644
index 000000000000..24ff1809ba1f
--- /dev/null
+++ b/scripts/coccinelle/api/bitmap.cocci
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Use bitmap_empty rather than bitmap_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
+
+@...ll depends on !patch@
+position p;
+expression E1, E2;
+binary operator cmp = {==, !=, <};
+@@
+
+ bitmap_weight(E1,E2) cmp@p E2
+
+@...ipt:python depends on report@
+p << rfull.p;
+@@
+
+coccilib.report.print_report(p[0], "ERROR: use bitmap_full()")
+
+@...ipt:python depends on org@
+p << rfull.p;
+@@
+
+@...pty1 depends on !patch@
+position p;
+statement S;
+@@
+
+ if (bitmap_weight@p(...)) S
+
+@...ipt:python depends on report@
+p << rempty1.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use !bitmap_empty()")
+
+@...ipt:python depends on org@
+p << rempty1.p;
+@@
+
+@...pty depends on !patch@
+position p;
+@@
+
+ bitmap_weight@p(...) == 0
+
+@...ipt:python depends on report@
+p << rempty.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use bitmap_empty()")
+
+@...ipt:python depends on org@
+p << rempty.p;
+@@
+
+@..._rempty depends on !patch@
+position p;
+@@
+
+ bitmap_weight(...) @p> 0
+
+@...ipt:python depends on report@
+p << not_rempty.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use \"!bitmap_empty()\"")
+
+@...ipt:python depends on org@
+p << not_rempty.p;
+@@
+
+
+@...p depends on !patch@
+expression exp;
+binary operator cmp = {>, <, >=, <=, ==, !=};
+position p;
+@@
+
+ bitmap_weight(...) cmp@p exp
+
+@...ipt:python depends on report@
+p << rcmp.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0,
+ "WARNING: use bitmap_weight_{gt,lt,ge,le,eq} as appropriate")
+
+@...ipt:python depends on org@
+p << rcmp.p;
+@@
--
2.32.0
Powered by blists - more mailing lists