Skip to contentSkip to conversation

lib/find: add find_nth_bit()

PATCH In review Yury Norov 1 participant 9 messages Sep 18, 2022 – Sep 26, 2022

Subsystems (4)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

v3 cover lib/find: add find_nth_bit()

Kernel lacks for a function that searches for Nth bit in a bitmap. Usually people do it like this:
for_each_set_bit(bit, mask, size)

          if (n-- == 0)
                  return bit;

Which is not so elegant and very slow.

This series adds fast routines for this task, and applies them where appropriate.

v1: https://lore.kernel.org/lkml/[email protected]/T/
v2: https://lore.kernel.org/lkml/CAAH8bW_RYG_Tbpip=BkSFAymDm2y3jJBqTi0PJWA=H-a-L_3tg@mail.gmail.com/T/
v3:
- add bitmap_weight_and() and use it in cpumask_local_spread();
- rework find_nth_bit() family: similarly to [1], introduce
FIND_NTH_BIT(), and optimize the function to return earlier when
it's known that the rest of bitmap can't meet the requirement.
- patch "lib/nodemask: inline next_node_in() and node_random()" has
been merged, so drop it from this series.

On top of:
[1] https://lore.kernel.org/lkml/[email protected]/T/

Yury Norov (6):
lib/bitmap: don't call __bitmap_weight() in kernel code lib/bitmap: add bitmap_weight_and()
lib: add find_nth(,and,andnot)_bit() lib/bitmap: add tests for find_nth_bit() lib/bitmap: remove bitmap_ord_to_pos
cpumask: add cpumask_nth_{,and,andnot}

 fs/ntfs3/bitmap.c        |  4 +-
 include/linux/bitmap.h   | 13 +++++-
 include/linux/bitops.h   | 19 +++++++++
 include/linux/cpumask.h  | 55 +++++++++++++++++++++++++
 include/linux/find.h     | 86 ++++++++++++++++++++++++++++++++++++++++
 include/linux/nodemask.h |  3 +-
 lib/bitmap.c             | 68 ++++++++++++-------------------
 lib/cpumask.c            | 28 ++++++-------
 lib/find_bit.c           | 44 ++++++++++++++++++++
 lib/find_bit_benchmark.c | 18 +++++++++
 lib/test_bitmap.c        | 47 +++++++++++++++++++++-
 11 files changed, 320 insertions(+), 65 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/6 lib/bitmap: don't call __bitmap_weight() in kernel code

__bitmap_weight() is not to be used directly in the kernel code because it's a helper for bitmap_weight(). Switch everything to bitmap_weight().

---
 fs/ntfs3/bitmap.c | 4 ++--
 lib/bitmap.c      | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/ntfs3/bitmap.c +2 -2
index 5d44ceac855b..e92bbd754365 100644
@@ -560,7 +560,7 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
 
 		buf = (ulong *)bh->b_data;
 
-		used = __bitmap_weight(buf, wbits);
+		used = bitmap_weight(buf, wbits);
 		if (used < wbits) {
 			frb = wbits - used;
 			wnd->free_bits[iw] = frb;
@@ -1364,7 +1364,7 @@ int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits)
 		buf = (ulong *)bh->b_data;
 
 		__bitmap_clear(buf, b0, blocksize * 8 - b0);
-		frb = wbits - __bitmap_weight(buf, wbits);
+		frb = wbits - bitmap_weight(buf, wbits);
 		wnd->total_zeroes += frb - wnd->free_bits[iw];
 		wnd->free_bits[iw] = frb;
 
lib/bitmap.c +1 -1
index 488e6c3e5acc..d56e275db73e 100644
@@ -953,7 +953,7 @@ static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigne
 	if (pos >= nbits || !test_bit(pos, buf))
 		return -1;
 
-	return __bitmap_weight(buf, pos);
+	return bitmap_weight(buf, pos);
 }
 
 /**
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v3 2/6 lib/bitmap: add bitmap_weight_and()

In reply to Yury Norov

The function calculates Hamming weight of (bitmap1 & bitmap2). Now we have to do like this:

	tmp = bitmap_alloc(nbits);
	bitmap_and(tmp, map1, map2, nbits);
	weight = bitmap_weight(tmp, nbits);
	bitmap_free(tmp);

This requires additional memory, adds pressure on alloc subsystem, and way less cache-friendly than just:

	weight = bitmap_weight_and(map1, map2, nbits);

The following patches apply it for cpumask functions.

---
 include/linux/bitmap.h  | 12 ++++++++++++
 include/linux/cpumask.h | 11 +++++++++++
 lib/bitmap.c            | 30 +++++++++++++++++++++---------
 3 files changed, 44 insertions(+), 9 deletions(-)
include/linux/bitmap.h +12 -0
index f65410a49fda..b2aef45af0db 100644
@@ -51,6 +51,7 @@ struct device;
  *  bitmap_empty(src, nbits)                    Are all bits zero in *src?
  *  bitmap_full(src, nbits)                     Are all bits set in *src?
  *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
+ *  bitmap_weight_and(src1, src2, nbits)        Hamming Weight of and'ed bitmap
  *  bitmap_set(dst, pos, nbits)                 Set specified bit area
  *  bitmap_clear(dst, pos, nbits)               Clear specified bit area
  *  bitmap_find_next_zero_area(buf, len, pos, n, mask)  Find bit free area
@@ -164,6 +165,8 @@ bool __bitmap_intersects(const unsigned long *bitmap1,
 bool __bitmap_subset(const unsigned long *bitmap1,
 		     const unsigned long *bitmap2, unsigned int nbits);
 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
+unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
+				 const unsigned long *bitmap2, unsigned int nbits);
 void __bitmap_set(unsigned long *map, unsigned int start, int len);
 void __bitmap_clear(unsigned long *map, unsigned int start, int len);
 
@@ -439,6 +442,15 @@ unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
 	return __bitmap_weight(src, nbits);
 }
 
+static __always_inline
+unsigned long bitmap_weight_and(const unsigned long *src1,
+				const unsigned long *src2, unsigned int nbits)
+{
+	if (small_const_nbits(nbits))
+		return hweight_long(*src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits));
+	return __bitmap_weight_and(src1, src2, nbits);
+}
+
 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
 		unsigned int nbits)
 {
include/linux/cpumask.h +11 -0
index e8ad12b5b9d2..31bf08c786e3 100644
@@ -586,6 +586,17 @@ static inline unsigned int cpumask_weight(const struct cpumask *srcp)
 	return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
 }
 
+/**
+ * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
+ * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
+ * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
+ */
+static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1,
+						const struct cpumask *srcp2)
+{
+	return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), nr_cpumask_bits);
+}
+
 /**
  * cpumask_shift_right - *dstp = *srcp >> n
  * @dstp: the cpumask result
lib/bitmap.c +21 -9
index d56e275db73e..3fc2e338ec30 100644
@@ -333,20 +333,32 @@ bool __bitmap_subset(const unsigned long *bitmap1,
 }
 EXPORT_SYMBOL(__bitmap_subset);
 
+#define BITMAP_WEIGHT(FETCH, bits)	\
+({										\
+	unsigned int __bits = (bits), idx, w = 0;				\
+										\
+	for (idx = 0; idx < __bits / BITS_PER_LONG; idx++)			\
+		w += hweight_long(FETCH);					\
+										\
+	if (__bits % BITS_PER_LONG)						\
+		w += hweight_long((FETCH) & BITMAP_LAST_WORD_MASK(__bits));	\
+										\
+	w;									\
+})
+
 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
 {
-	unsigned int k, lim = bits/BITS_PER_LONG, w = 0;
-
-	for (k = 0; k < lim; k++)
-		w += hweight_long(bitmap[k]);
-
-	if (bits % BITS_PER_LONG)
-		w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
-
-	return w;
+	return BITMAP_WEIGHT(bitmap[idx], bits);
 }
 EXPORT_SYMBOL(__bitmap_weight);
 
+unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
+				const unsigned long *bitmap2, unsigned int bits)
+{
+	return BITMAP_WEIGHT(bitmap1[idx] & bitmap2[idx], bits);
+}
+EXPORT_SYMBOL(__bitmap_weight_and);
+
 void __bitmap_set(unsigned long *map, unsigned int start, int len)
 {
 	unsigned long *p = map + BIT_WORD(start);
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH v3 3/6 lib: add find_nth{,_and,_andnot}_bit()

In reply to Yury Norov

Kernel lacks for a function that searches for Nth bit in a bitmap. Usually people do it like this:

	for_each_set_bit(bit, mask, size)
		if (n-- == 0)
			return bit;

We can do it more efficiently, if we:
1. find a word containing Nth bit, using hweight(); and
2. find the bit, using a helper fns(), that works similarly to __ffs() and ffz().

fns() is implemented as a simple loop. For x86_64, there's PDEP instruction to do that: ret = clz(pdep(1 << idx, num)). However, for large bitmaps the most of improvement comes from using hweight(), so I kept fns() simple.

New find_nth_bit() is ~70 times faster on x86_64/kvm in find_bit benchmark:

find_nth_bit:                  7154190 ns,  16411 iterations
for_each_bit:                505493126 ns,  16315 iterations

With all that, a family of 3 new functions is added, and used where appropriate in the following patches.

---
Comparing to v2, it adds a path that allows quit earlier if the condition can't be met:

	for (idx = 0; (idx + 1) * BITS_PER_LONG <= sz; idx++) {	

+ if (idx * BITS_PER_LONG + nr >= sz)
+ goto out;
...

 include/linux/bitops.h | 19 ++++++++++
 include/linux/find.h   | 86 ++++++++++++++++++++++++++++++++++++++++++
 lib/find_bit.c         | 44 +++++++++++++++++++++
 3 files changed, 149 insertions(+)
include/linux/bitops.h +19 -0
index 3b89c64bcfd8..d7dd83fafeba 100644
@@ -247,6 +247,25 @@ static inline unsigned long __ffs64(u64 word)
 	return __ffs((unsigned long)word);
 }
 
+/**
+ * fns - find N'th set bit in a word
+ * @word: The word to search
+ * @n: Bit to find
+ */
+static inline unsigned long fns(unsigned long word, unsigned int n)
+{
+	unsigned int bit;
+
+	while (word) {
+		bit = __ffs(word);
+		if (n-- == 0)
+			return bit;
+		__clear_bit(bit, &word);
+	}
+
+	return BITS_PER_LONG;
+}
+
 /**
  * assign_bit - Assign value to a bit in memory
  * @nr: the bit to set
include/linux/find.h +86 -0

98 lines not on this page · raw message

lib/find_bit.c +44 -0

56 lines not on this page · raw message

Signature · 1 line
2.34.1
author maintainer
#4 lore raw

PATCH v3 4/6 lib/bitmap: add tests for find_nth_bit()

In reply to Yury Norov

Add functional and performance tests for find_nth_bit().

---
 lib/find_bit_benchmark.c | 18 +++++++++++++++
 lib/test_bitmap.c        | 47 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 63 insertions(+), 2 deletions(-)
lib/find_bit_benchmark.c +18 -0
index db904b57d4b8..10754586403b 100644
@@ -115,6 +115,22 @@ static int __init test_find_last_bit(const void *bitmap, unsigned long len)
 	return 0;
 }
 
+static int __init test_find_nth_bit(const unsigned long *bitmap, unsigned long len)
+{
+	unsigned long l, n, w = bitmap_weight(bitmap, len);
+	ktime_t time;
+
+	time = ktime_get();
+	for (n = 0; n < w; n++) {
+		l = find_nth_bit(bitmap, len, n);
+		WARN_ON(l >= len);
+	}
+	time = ktime_get() - time;
+	pr_err("find_nth_bit:       %18llu ns, %6ld iterations\n", time, w);
+
+	return 0;
+}
+
 static int __init test_find_next_and_bit(const void *bitmap,
 		const void *bitmap2, unsigned long len)
 {
@@ -142,6 +158,7 @@ static int __init find_bit_test(void)
 	test_find_next_bit(bitmap, BITMAP_LEN);
 	test_find_next_zero_bit(bitmap, BITMAP_LEN);
 	test_find_last_bit(bitmap, BITMAP_LEN);
+	test_find_nth_bit(bitmap, BITMAP_LEN / 10);
 
 	/*
 	 * test_find_first_bit() may take some time, so
@@ -164,6 +181,7 @@ static int __init find_bit_test(void)
 	test_find_next_bit(bitmap, BITMAP_LEN);
 	test_find_next_zero_bit(bitmap, BITMAP_LEN);
 	test_find_last_bit(bitmap, BITMAP_LEN);
+	test_find_nth_bit(bitmap, BITMAP_LEN);
 	test_find_first_bit(bitmap, BITMAP_LEN);
 	test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN);
 	test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
lib/test_bitmap.c +45 -2

71 lines not on this page · raw message

Signature · 1 line
2.34.1
author maintainer
#5 lore raw

PATCH v3 5/6 lib/bitmap: remove bitmap_ord_to_pos

In reply to Yury Norov

Now that we have find_nth_bit(), we can drop bitmap_ord_to_pos().

---
 include/linux/bitmap.h   |  1 -
 include/linux/nodemask.h |  3 +--
 lib/bitmap.c             | 36 +++---------------------------------
 3 files changed, 4 insertions(+), 36 deletions(-)
include/linux/bitmap.h +0 -1
index b2aef45af0db..7d6d73b78147 100644
@@ -225,7 +225,6 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n
 #else
 #define bitmap_copy_le bitmap_copy
 #endif
-unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
 int bitmap_print_to_pagebuf(bool list, char *buf,
 				   const unsigned long *maskp, int nmaskbits);
 
include/linux/nodemask.h +1 -2
index 4b71a96190a8..0c45fb066caa 100644
@@ -508,8 +508,7 @@ static inline int node_random(const nodemask_t *maskp)
 
 	w = nodes_weight(*maskp);
 	if (w)
-		bit = bitmap_ord_to_pos(maskp->bits,
-			get_random_int() % w, MAX_NUMNODES);
+		bit = find_nth_bit(maskp->bits, MAX_NUMNODES, get_random_int() % w);
 	return bit;
 #else
 	return 0;
lib/bitmap.c +3 -33
index 3fc2e338ec30..1c81413c51f8 100644
@@ -968,36 +968,6 @@ static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigne
 	return bitmap_weight(buf, pos);
 }
 
-/**
- * bitmap_ord_to_pos - find position of n-th set bit in bitmap
- *	@buf: pointer to bitmap
- *	@ord: ordinal bit position (n-th set bit, n >= 0)
- *	@nbits: number of valid bit positions in @buf
- *
- * Map the ordinal offset of bit @ord in @buf to its position in @buf.
- * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
- * >= weight(buf), returns @nbits.
- *
- * If for example, just bits 4 through 7 are set in @buf, then @ord
- * values 0 through 3 will get mapped to 4 through 7, respectively,
- * and all other @ord values returns @nbits.  When @ord value 3
- * gets mapped to (returns) @pos value 7 in this example, that means
- * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
- *
- * The bit positions 0 through @nbits-1 are valid positions in @buf.
- */
-unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
-{
-	unsigned int pos;
-
-	for (pos = find_first_bit(buf, nbits);
-	     pos < nbits && ord;
-	     pos = find_next_bit(buf, nbits, pos + 1))
-		ord--;
-
-	return pos;
-}
-
 /**
  * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  *	@dst: remapped result
@@ -1047,7 +1017,7 @@ void bitmap_remap(unsigned long *dst, const unsigned long *src,
 		if (n < 0 || w == 0)
 			set_bit(oldbit, dst);	/* identity map */
 		else
-			set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
+			set_bit(find_nth_bit(new, nbits, n % w), dst);
 	}
 }
 EXPORT_SYMBOL(bitmap_remap);
@@ -1086,7 +1056,7 @@ int bitmap_bitremap(int oldbit, const unsigned long *old,
 	if (n < 0 || w == 0)
 		return oldbit;
 	else
-		return bitmap_ord_to_pos(new, n % w, bits);
+		return find_nth_bit(new, bits, n % w);
 }
 EXPORT_SYMBOL(bitmap_bitremap);
 
@@ -1210,7 +1180,7 @@ void bitmap_onto(unsigned long *dst, const unsigned long *orig,
 	 * The following code is a more efficient, but less
 	 * obvious, equivalent to the loop:
 	 *	for (m = 0; m < bitmap_weight(relmap, bits); m++) {
-	 *		n = bitmap_ord_to_pos(orig, m, bits);
+	 *		n = find_nth_bit(orig, bits, m);
 	 *		if (test_bit(m, orig))
 	 *			set_bit(n, dst);
 	 *	}
Signature · 1 line
2.34.1
author maintainer
#6 lore raw

PATCH v3 6/6 cpumask: add cpumask_nth_{,and,andnot}

In reply to Yury Norov

Add cpumask_nth_{,and,andnot} as wrappers around corresponding find functions, and use it in cpumask_local_spread().

---
 include/linux/cpumask.h | 44 +++++++++++++++++++++++++++++++++++++++++
 lib/cpumask.c           | 28 ++++++++++++--------------
 2 files changed, 57 insertions(+), 15 deletions(-)
include/linux/cpumask.h +44 -0
index 31bf08c786e3..621b59820561 100644
@@ -337,6 +337,50 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
 	return i;
 }
 
+/**
+ * cpumask_nth - get the first cpu in a cpumask
+ * @srcp: the cpumask pointer
+ * @cpu: the N'th cpu to find, starting from 0
+ *
+ * Returns >= nr_cpu_ids if such cpu doesn't exist.
+ */
+static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
+{
+	return find_nth_bit(cpumask_bits(srcp), nr_cpumask_bits, cpumask_check(cpu));
+}
+
+/**
+ * cpumask_nth_and - get the first cpu in 2 cpumasks
+ * @srcp1: the cpumask pointer
+ * @srcp2: the cpumask pointer
+ * @cpu: the N'th cpu to find, starting from 0
+ *
+ * Returns >= nr_cpu_ids if such cpu doesn't exist.
+ */
+static inline
+unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
+							const struct cpumask *srcp2)
+{
+	return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
+				nr_cpumask_bits, cpumask_check(cpu));
+}
+
+/**
+ * cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in 2nd.
+ * @srcp1: the cpumask pointer
+ * @srcp2: the cpumask pointer
+ * @cpu: the N'th cpu to find, starting from 0
+ *
+ * Returns >= nr_cpu_ids if such cpu doesn't exist.
+ */
+static inline
+unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
+							const struct cpumask *srcp2)
+{
+	return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
+				nr_cpumask_bits, cpumask_check(cpu));
+}
+
 #define CPU_BITS_NONE						\
 {								\
 	[0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL			\
lib/cpumask.c +13 -15
index f0ae119be8c4..2c4a63b6f03f 100644
@@ -128,23 +128,21 @@ unsigned int cpumask_local_spread(unsigned int i, int node)
 	i %= num_online_cpus();
 
 	if (node == NUMA_NO_NODE) {
-		for_each_cpu(cpu, cpu_online_mask)
-			if (i-- == 0)
-				return cpu;
+		cpu = cpumask_nth(i, cpu_online_mask);
+		if (cpu < nr_cpu_ids)
+			return cpu;
 	} else {
 		/* NUMA first. */
-		for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
-			if (i-- == 0)
-				return cpu;
-
-		for_each_cpu(cpu, cpu_online_mask) {
-			/* Skip NUMA nodes, done above. */
-			if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
-				continue;
-
-			if (i-- == 0)
-				return cpu;
-		}
+		cpu = cpumask_nth_and(i, cpu_online_mask, cpumask_of_node(node));
+		if (cpu < nr_cpu_ids)
+			return cpu;
+
+		i -= cpumask_weight_and(cpu_online_mask, cpumask_of_node(node));
+
+		/* Skip NUMA nodes, done above. */
+		cpu = cpumask_nth_andnot(i, cpu_online_mask, cpumask_of_node(node));
+		if (cpu < nr_cpu_ids)
+			return cpu;
 	}
 	BUG();
 }
Signature · 1 line
2.34.1
author maintainer
#7 lore raw
In reply to Yury Norov

Ping?

Quoted 47 lines · Yury Norov

Kernel lacks for a function that searches for Nth bit in a bitmap. Usually people do it like this:
for_each_set_bit(bit, mask, size)

          if (n-- == 0)
                  return bit;

Which is not so elegant and very slow.

This series adds fast routines for this task, and applies them where appropriate.

v1: https://lore.kernel.org/lkml/[email protected]/T/
v2: https://lore.kernel.org/lkml/CAAH8bW_RYG_Tbpip=BkSFAymDm2y3jJBqTi0PJWA=H-a-L_3tg@mail.gmail.com/T/
v3:
- add bitmap_weight_and() and use it in cpumask_local_spread();
- rework find_nth_bit() family: similarly to [1], introduce
FIND_NTH_BIT(), and optimize the function to return earlier when
it's known that the rest of bitmap can't meet the requirement.
- patch "lib/nodemask: inline next_node_in() and node_random()" has
been merged, so drop it from this series.

On top of:
[1] https://lore.kernel.org/lkml/[email protected]/T/

Yury Norov (6):
lib/bitmap: don't call __bitmap_weight() in kernel code lib/bitmap: add bitmap_weight_and()
lib: add find_nth(,and,andnot)_bit() lib/bitmap: add tests for find_nth_bit() lib/bitmap: remove bitmap_ord_to_pos
cpumask: add cpumask_nth_{,and,andnot}

 fs/ntfs3/bitmap.c        |  4 +-
 include/linux/bitmap.h   | 13 +++++-
 include/linux/bitops.h   | 19 +++++++++
 include/linux/cpumask.h  | 55 +++++++++++++++++++++++++
 include/linux/find.h     | 86 ++++++++++++++++++++++++++++++++++++++++
 include/linux/nodemask.h |  3 +-
 lib/bitmap.c             | 68 ++++++++++++-------------------
 lib/cpumask.c            | 28 ++++++-------
 lib/find_bit.c           | 44 ++++++++++++++++++++
 lib/find_bit_benchmark.c | 18 +++++++++
 lib/test_bitmap.c        | 47 +++++++++++++++++++++-
 11 files changed, 320 insertions(+), 65 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#8 lore raw

OK, take in bitmap-for-next.

Quoted 50 lines · Yury Norov

Ping?

Quoted 47 lines · Yury Norov

Kernel lacks for a function that searches for Nth bit in a bitmap. Usually people do it like this:
for_each_set_bit(bit, mask, size)

          if (n-- == 0)
                  return bit;

Which is not so elegant and very slow.

This series adds fast routines for this task, and applies them where appropriate.

v1: https://lore.kernel.org/lkml/[email protected]/T/
v2: https://lore.kernel.org/lkml/CAAH8bW_RYG_Tbpip=BkSFAymDm2y3jJBqTi0PJWA=H-a-L_3tg@mail.gmail.com/T/
v3:
- add bitmap_weight_and() and use it in cpumask_local_spread();
- rework find_nth_bit() family: similarly to [1], introduce
FIND_NTH_BIT(), and optimize the function to return earlier when
it's known that the rest of bitmap can't meet the requirement.
- patch "lib/nodemask: inline next_node_in() and node_random()" has
been merged, so drop it from this series.

On top of:
[1] https://lore.kernel.org/lkml/[email protected]/T/

Yury Norov (6):
lib/bitmap: don't call __bitmap_weight() in kernel code lib/bitmap: add bitmap_weight_and()
lib: add find_nth(,and,andnot)_bit() lib/bitmap: add tests for find_nth_bit() lib/bitmap: remove bitmap_ord_to_pos
cpumask: add cpumask_nth_{,and,andnot}

 fs/ntfs3/bitmap.c        |  4 +-
 include/linux/bitmap.h   | 13 +++++-
 include/linux/bitops.h   | 19 +++++++++
 include/linux/cpumask.h  | 55 +++++++++++++++++++++++++
 include/linux/find.h     | 86 ++++++++++++++++++++++++++++++++++++++++
 include/linux/nodemask.h |  3 +-
 lib/bitmap.c             | 68 ++++++++++++-------------------
 lib/cpumask.c            | 28 ++++++-------
 lib/find_bit.c           | 44 ++++++++++++++++++++
 lib/find_bit_benchmark.c | 18 +++++++++
 lib/test_bitmap.c        | 47 +++++++++++++++++++++-
 11 files changed, 320 insertions(+), 65 deletions(-)
Signature · 1 line
2.34.1