From: Yury Norov <yury.norov@gmail.com>
To: "Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"David Laight" <David.Laight@ACULAB.COM>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Joe Perches" <joe@perches.com>,
"Julia Lawall" <Julia.Lawall@inria.fr>,
"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
"Nicholas Piggin" <npiggin@gmail.com>,
"Nicolas Palix" <nicolas.palix@imag.fr>,
"Peter Zijlstra" <peterz@infradead.org>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
"Matti Vaittinen" <Matti.Vaittinen@fi.rohmeurope.com>,
linux-kernel@vger.kernel.org
Cc: Yury Norov <yury.norov@gmail.com>, cocci@inria.fr
Subject: [PATCH 01/22] introduce bitmap.cocci
Date: Tue, 10 May 2022 08:47:29 -0700 [thread overview]
Message-ID: <20220510154750.212913-2-yury.norov@gmail.com> (raw)
In-Reply-To: <20220510154750.212913-1-yury.norov@gmail.com>
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@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
CC: Julia Lawall <Julia.Lawall@inria.fr>
CC: Nicolas Palix <nicolas.palix@imag.fr>
CC: linux-kernel@vger.kernel.org
CC: cocci@inria.fr
CC: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Yury Norov <yury.norov@gmail.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
+
+@rfull depends on !patch@
+position p;
+expression E1, E2;
+binary operator cmp = {==, !=, <};
+@@
+
+ bitmap_weight(E1,E2) cmp@p E2
+
+@script:python depends on report@
+p << rfull.p;
+@@
+
+coccilib.report.print_report(p[0], "ERROR: use bitmap_full()")
+
+@script:python depends on org@
+p << rfull.p;
+@@
+
+@rempty1 depends on !patch@
+position p;
+statement S;
+@@
+
+ if (bitmap_weight@p(...)) S
+
+@script:python depends on report@
+p << rempty1.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use !bitmap_empty()")
+
+@script:python depends on org@
+p << rempty1.p;
+@@
+
+@rempty depends on !patch@
+position p;
+@@
+
+ bitmap_weight@p(...) == 0
+
+@script:python depends on report@
+p << rempty.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use bitmap_empty()")
+
+@script:python depends on org@
+p << rempty.p;
+@@
+
+@not_rempty depends on !patch@
+position p;
+@@
+
+ bitmap_weight(...) @p> 0
+
+@script:python depends on report@
+p << not_rempty.p;
+@@
+
+for p0 in p:
+ coccilib.report.print_report(p0, "ERROR: use \"!bitmap_empty()\"")
+
+@script:python depends on org@
+p << not_rempty.p;
+@@
+
+
+@rcmp depends on !patch@
+expression exp;
+binary operator cmp = {>, <, >=, <=, ==, !=};
+position p;
+@@
+
+ bitmap_weight(...) cmp@p exp
+
+@script: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")
+
+@script:python depends on org@
+p << rcmp.p;
+@@
--
2.32.0
next prev parent reply other threads:[~2022-05-10 15:58 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-10 15:47 [PATCH 00/21] add coccinelle scripts for {bitmap,cpumask,nodes}_empty() Yury Norov
2022-05-10 15:47 ` Yury Norov [this message]
2022-05-10 15:47 ` [PATCH 02/22] introduce cpumask.cocci Yury Norov
2022-05-10 15:47 ` [PATCH 03/22] introduce nodemask.cocci Yury Norov
2022-05-10 15:47 ` [PATCH 04/22] ice: use bitmap_empty() in ice_vf_has_no_qs_ena() Yury Norov
2022-05-10 15:47 ` [PATCH 05/22] iio: replace bitmap_weight with bitmap_weitght_{eq,le} where appropriate Yury Norov
2022-05-14 15:53 ` Jonathan Cameron
2022-05-14 16:31 ` Joe Perches
2022-05-15 16:40 ` Jonathan Cameron
2022-05-10 15:47 ` [PATCH 06/22] octeontx2: use bitmap_empty() instead of bitmap_weight() Yury Norov
2022-05-10 21:31 ` Jakub Kicinski
2022-05-10 15:47 ` [PATCH 07/22] risc-v: replace bitmap_weight with bitmap_empty in riscv_fill_hwcap() Yury Norov
2022-05-10 16:31 ` Anup Patel
2022-05-10 15:47 ` [PATCH 08/22] bitops: introduce MANY_BITS() macro Yury Norov
2022-05-10 16:50 ` Alexei Starovoitov
2022-05-10 17:54 ` David Laight
2022-05-10 19:11 ` Yury Norov
2022-05-11 10:59 ` Rasmus Villemoes
2022-05-10 17:57 ` Max Filippov
2022-05-10 19:16 ` Yury Norov
2022-05-11 4:55 ` Max Filippov
2022-05-10 15:47 ` [PATCH 09/22] qed: replace bitmap_weight() with MANY_BITS() Yury Norov
2022-05-10 15:47 ` [PATCH 10/22] net/mlx5e: simplify mlx5e_set_fecparam() Yury Norov
2022-05-10 15:47 ` [PATCH 11/22] KVM: x86: hyper-v: replace bitmap_weight() with hweight64() Yury Norov
2022-05-16 13:08 ` Vitaly Kuznetsov
2022-05-22 14:53 ` Guenter Roeck
2022-05-22 17:39 ` Yury Norov
2022-05-10 15:47 ` [PATCH 12/22] ia64: cleanup remove_siblinginfo() Yury Norov
2022-05-10 22:33 ` Andrew Morton
2022-05-10 15:47 ` [PATCH 13/22] x86: smp: move cpumask_weight() out of for-loop in remove_siblinginfo Yury Norov
2022-05-11 9:47 ` Thomas Gleixner
2022-05-10 15:47 ` [PATCH 14/22] x86: smp: use cpumask_weight_eq() " Yury Norov
2022-05-10 15:47 ` [PATCH 15/22] net/mlx5: use cpumask_weight_gt() in irq_pool_request_irq() Yury Norov
2022-05-10 15:47 ` [PATCH 16/22] x86/tsc: use cpumask_weight_gt() in loop_timeout() Yury Norov
2022-05-10 15:47 ` [PATCH 17/22] sched/core: fix opencoded cpumask_any_but() Yury Norov
2022-05-10 16:37 ` Peter Zijlstra
2022-05-10 17:21 ` Valentin Schneider
2022-05-10 15:47 ` [PATCH 18/22] sched/core: remove unneeded cpumask_weight() in sched_core_cpu_{starting,deactivate} Yury Norov
2022-05-10 15:47 ` [PATCH 19/22] sched/core: replace cpumask_weight() with cpumask_weight_eq() where appropriate Yury Norov
2022-05-10 15:47 ` [PATCH 20/22] sched/topology: " Yury Norov
2022-05-10 15:47 ` [PATCH 21/22] cpufreq: use cpumask_weight_gt() in policy_is_shared() Yury Norov
2022-05-11 3:16 ` Viresh Kumar
2022-05-10 15:47 ` [PATCH 22/22] clockevents: use cpumask_weight_eq() in tick_cleanup_dead_cpu() Yury Norov
2022-05-11 8:18 ` Thomas Gleixner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220510154750.212913-2-yury.norov@gmail.com \
--to=yury.norov@gmail.com \
--cc=David.Laight@ACULAB.COM \
--cc=Julia.Lawall@inria.fr \
--cc=Matti.Vaittinen@fi.rohmeurope.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=cocci@inria.fr \
--cc=gregkh@linuxfoundation.org \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mirq-linux@rere.qmqm.pl \
--cc=nicolas.palix@imag.fr \
--cc=npiggin@gmail.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome