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

From: Frederic Weisbecker
Date: Thu Feb 28 2019 - 12:13:15 EST


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@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Cc: Mauro Carvalho Chehab <mchehab+samsung@xxxxxxxxxx>
Cc: Joel Fernandes <joel@xxxxxxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Pavan Kondeti <pkondeti@xxxxxxxxxxxxxx>
Cc: Paul E . McKenney <paulmck@xxxxxxxxxxxxxxxxxx>
Cc: David S . Miller <davem@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
---
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