[PATCH 05/37] locking/lockdep: Introduce lock usage mask iterator

Frederic Weisbecker
2019-02-28T17:13:14Z

Introduce a mask iterator that can be used to get the bit numbers from a
lock usage mask.

We don't want the overhead of the bitmap library for a fixed small 64
bit mask. So we just want a simple loop relying on an ffs() like
function. Yet we want to be careful not to shift further 64 bits at once
as it has unpredictable behaviour, depending on architecture backend.

Therefore the shift on each iteration is cut in two parts:

1) Shift by the first set bit number (can't exceed 63)
2) Shift again by 1 because __ffs64() counts from zero

Inspired-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Cc: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Cc: Joel Fernandes <joel@joelfernandes.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Pavan Kondeti <pkondeti@codeaurora.org>
Cc: Paul E . McKenney <paulmck@linux.vnet.ibm.com>
Cc: David S . Miller <davem@davemloft.net>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
---
kernel/locking/lockdep.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index 004278969afc..1a335176cb61 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -463,6 +463,23 @@ const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
}

+static u64 mask_iter(u64 *mask, int *bit)
+{
+ u64 old_mask = *mask;
+
+ if (old_mask) {
+ long fs = __ffs64(old_mask);
+ *bit += fs;
+ *mask >>= fs;
+ *mask >>= 1;
+ }
+
+ return old_mask;
+}
+
+#define for_each_bit_nr(mask, bit) \
+ for (bit = 0; mask_iter(&mask, &bit); bit++)
+
static inline u64 lock_flag(enum lock_usage_bit bit)
{
return BIT_ULL(bit);
--
2.21.0