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 <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]> Cc: Mauro Carvalho Chehab <[email protected]> Cc: Joel Fernandes <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Pavan Kondeti <[email protected]> Cc: Paul E . McKenney <[email protected]> Cc: David S . Miller <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Sebastian Andrzej Siewior <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> --- 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

