Skip to contentSkip to conversation

bitmap: introduce for_each_set_bitrange()

PATCH In review Yury Norov 3 participants 8 messages Jul 9, 2021 – Jul 16, 2021

Subsystems (3)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

cover bitmap: introduce for_each_set_bitrange()

Introduce for_each_set_bitrange() and improve bitmap_list_string().

On top of:
https://lore.kernel.org/lkml/YNp3extAkTY8Aocd@yury-ThinkPad/T/ and
https://lore.kernel.org/lkml/YNirnaYw1GSxg1jK@yury-ThinkPad/T/

The full series is here:
https://github.com/norov/linux/commits/bm-f3

Yury Norov (2):
lib: bitmap: add performance test for bitmap_print_to_pagebuf
bitmap: introduce for_each_set_bitrange{_from}

 include/linux/find.h |  7 +++++++
 lib/test_bitmap.c    | 37 +++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c       | 40 ++++++++++++++++------------------------
 3 files changed, 60 insertions(+), 24 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/2 lib: bitmap: add performance test for bitmap_print_to_pagebuf

This patch adds performance test for a fully set bitmap. Functional tests are in lib/test_printf.c.

---
 lib/test_bitmap.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
lib/test_bitmap.c +37 -0
index 4ea73f5aed41..452d525007da 100644
@@ -430,6 +430,42 @@ static void __init test_bitmap_parselist(void)
 	}
 }
 
+static void __init test_bitmap_printlist(void)
+{
+	unsigned long *bmap = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	char expected[256];
+	int ret, slen;
+	ktime_t time;
+
+	if (!buf || !bmap)
+		goto out;
+
+	memset(bmap, -1, PAGE_SIZE);
+	slen = snprintf(expected, 256, "0-%ld", PAGE_SIZE * 8 - 1);
+	if (slen < 0)
+		goto out;
+
+	time = ktime_get();
+	ret = bitmap_print_to_pagebuf(true, buf, bmap, PAGE_SIZE * 8);
+	time = ktime_get() - time;
+
+	if (ret != slen + 1) {
+		pr_err("bitmap_print_to_pagebuf: result is %d, expected %d\n", ret, slen);
+		goto out;
+	}
+
+	if (strncmp(buf, expected, slen)) {
+		pr_err("bitmap_print_to_pagebuf: result is %s, expected %s\n", buf, expected);
+		goto out;
+	}
+
+	pr_err("bitmap_print_to_pagebuf: input is '%s', Time: %llu\n", buf, time);
+out:
+	kfree(buf);
+	kfree(bmap);
+}
+
 static const unsigned long parse_test[] __initconst = {
 	BITMAP_FROM_U64(0),
 	BITMAP_FROM_U64(1),
@@ -669,6 +705,7 @@ static void __init selftest(void)
 	test_bitmap_arr32();
 	test_bitmap_parse();
 	test_bitmap_parselist();
+	test_bitmap_printlist();
 	test_mem_optimisations();
 	test_for_each_set_clump8();
 	test_bitmap_cut();
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/2 bitmap: introduce for_each_set_bitrange

In reply to Yury Norov

bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit. We can do better by detecting ranges of set bits.

This patch introduces a macro for_each_set_bitrange and uses it in bitmap_list_string(). In my environment, before/after is 943008/31008 ns.

---
 include/linux/find.h |  7 +++++++
 lib/vsprintf.c       | 40 ++++++++++++++++------------------------
 2 files changed, 23 insertions(+), 24 deletions(-)
include/linux/find.h +7 -0
index ae9ed52b52b8..1a5ed45dc81b 100644
@@ -301,6 +301,13 @@ unsigned long find_next_bit_le(const void *addr, unsigned
 	     (bit) < (size);					\
 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
 
+#define for_each_set_bitrange(b, e, addr, size)			\
+	for ((b) = find_next_bit((addr), (size), 0),		\
+	     (e) = find_next_zero_bit((addr), (size), (b) + 1);	\
+	     (b) < (size);					\
+	     (b) = find_next_bit((addr), (size), (e) + 1),	\
+	     (e) = find_next_zero_bit((addr), (size), (b) + 1))
+
 /**
  * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
  * @start: bit offset to start search and to store the current iteration offset
lib/vsprintf.c +16 -24
index 87acf66f0e4c..1ee54dace71e 100644
@@ -1240,38 +1240,30 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
 			 struct printf_spec spec, const char *fmt)
 {
 	int nr_bits = max_t(int, spec.field_width, 0);
-	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
-	int cur, rbot, rtop;
-	bool first = true;
+	char *start = buf;
+	int b, e;
 
 	if (check_pointer(&buf, end, bitmap, spec))
 		return buf;
 
-	rbot = cur = find_first_bit(bitmap, nr_bits);
-	while (cur < nr_bits) {
-		rtop = cur;
-		cur = find_next_bit(bitmap, nr_bits, cur + 1);
-		if (cur < nr_bits && cur <= rtop + 1)
-			continue;
+	for_each_set_bitrange(b, e, bitmap, nr_bits) {
+		buf = number(buf, end, b, default_dec_spec);
+		if (e == b + 1)
+			goto put_comma;
 
-		if (!first) {
-			if (buf < end)
-				*buf = ',';
-			buf++;
-		}
-		first = false;
+		if (buf < end)
+			*buf = '-';
 
-		buf = number(buf, end, rbot, default_dec_spec);
-		if (rbot < rtop) {
-			if (buf < end)
-				*buf = '-';
-			buf++;
+		buf = number(++buf, end, e - 1, default_dec_spec);
+put_comma:
+		if (buf < end)
+			*buf = ',';
+		buf++;
+	}
 
-			buf = number(buf, end, rtop, default_dec_spec);
-		}
+	if (buf > start)
+		buf--;
 
-		rbot = cur;
-	}
 	return buf;
 }
 
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

Re: [PATCH 2/2] bitmap: introduce for_each_set_bitrange

Quoted 6 lines · Yury Norov

bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit. We can do better by detecting ranges of set bits.

This patch introduces a macro for_each_set_bitrange and uses it in bitmap_list_string(). In my environment, before/after is 943008/31008 ns.

Ah, it seems we already have bitmap_for_each_{set,clear}_region() with the same functionality. I'll check everything again and submit v2 soon.

maintainer
#4 lore raw

Re: [PATCH 2/2] bitmap: introduce for_each_set_bitrange

In reply to Yury Norov
Quoted 18 lines · On Thu, 8 Jul 2021 20:45:19 -0700 Yury Norov <[email protected]> wrote:

bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit. We can do better by detecting ranges of set bits.

This patch introduces a macro for_each_set_bitrange and uses it in bitmap_list_string(). In my environment, before/after is 943008/31008 ns.

---
 include/linux/find.h |  7 +++++++
 lib/vsprintf.c       | 40 ++++++++++++++++------------------------
 2 files changed, 23 insertions(+), 24 deletions(-)
include/linux/find.h +0 -0
index ae9ed52b52b8..1a5ed45dc81b 100644

include/linux/find.h unsigned long find_next_bit_le(const void *addr, unsigned · line 304

Quoted 4 lines
include/linux/find.h +1 -0
@@ -301,6 +301,13 @@ unsigned long find_next_bit_le(const void *addr, unsigned
 	     (bit) < (size);					\
 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
 
+#define for_each_set_bitrange(b, e, addr, size)			\

The above needs a kerneldoc header.

Quoted 26 lines
include/linux/find.h +6 -0
+	for ((b) = find_next_bit((addr), (size), 0),		\
+	     (e) = find_next_zero_bit((addr), (size), (b) + 1);	\
+	     (b) < (size);					\
+	     (b) = find_next_bit((addr), (size), (e) + 1),	\
+	     (e) = find_next_zero_bit((addr), (size), (b) + 1))
+
 /**
  * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
  * @start: bit offset to start search and to store the current iteration offset
lib/vsprintf.c +2 -3
index 87acf66f0e4c..1ee54dace71e 100644
@@ -1240,38 +1240,30 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
 			 struct printf_spec spec, const char *fmt)
 {
 	int nr_bits = max_t(int, spec.field_width, 0);
-	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
-	int cur, rbot, rtop;
-	bool first = true;
+	char *start = buf;
+	int b, e;
 
 	if (check_pointer(&buf, end, bitmap, spec))
 		return buf;
 

lib/vsprintf.c char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, · line 1252

Quoted 10 lines
lib/vsprintf.c +4 -6
-	rbot = cur = find_first_bit(bitmap, nr_bits);
-	while (cur < nr_bits) {
-		rtop = cur;
-		cur = find_next_bit(bitmap, nr_bits, cur + 1);
-		if (cur < nr_bits && cur <= rtop + 1)
-			continue;
+	for_each_set_bitrange(b, e, bitmap, nr_bits) {
+		buf = number(buf, end, b, default_dec_spec);
+		if (e == b + 1)
+			goto put_comma;

Using a goto to skip a few lines instead of just having the reverse conditional is rather sloppy IMO.

		if (e != b + 1) {
			if (buf < end)
				*buf = '-';
			buf++;
			buf = number(buf, end, e - 1, default_dec_spec);
		}

Is much clearer.

Quoted 16 lines
lib/vsprintf.c +3 -11
-		if (!first) {
-			if (buf < end)
-				*buf = ',';
-			buf++;
-		}
-		first = false;
+		if (buf < end)
+			*buf = '-';
 
-		buf = number(buf, end, rbot, default_dec_spec);
-		if (rbot < rtop) {
-			if (buf < end)
-				*buf = '-';
-			buf++;
+		buf = number(++buf, end, e - 1, default_dec_spec);

lib/vsprintf.c char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, · line 1265

Quoted 10 lines
lib/vsprintf.c +7 -2
+put_comma:
+		if (buf < end)
+			*buf = ',';
+		buf++;
+	}
 
-			buf = number(buf, end, rtop, default_dec_spec);
-		}
+	if (buf > start)
+		buf--;

If the above is to undo the last comma, please put back the first logic.

-- Steve

Quoted 6 lines
lib/vsprintf.c +0 -2
-		rbot = cur;
-	}
 	return buf;
 }
 
author maintainer
#5 lore raw

Re: [PATCH 2/2] bitmap: introduce for_each_set_bitrange

Quoted 24 lines · Steven Rostedt
Quoted 21 lines · On Thu, 8 Jul 2021 20:45:19 -0700 Yury Norov <[email protected]> wrote:

bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit. We can do better by detecting ranges of set bits.

This patch introduces a macro for_each_set_bitrange and uses it in bitmap_list_string(). In my environment, before/after is 943008/31008 ns.

---
 include/linux/find.h |  7 +++++++
 lib/vsprintf.c       | 40 ++++++++++++++++------------------------
 2 files changed, 23 insertions(+), 24 deletions(-)
include/linux/find.h +0 -0
index ae9ed52b52b8..1a5ed45dc81b 100644
@@ -301,6 +301,13 @@ unsigned long find_next_bit_le(const void *addr, unsigned
 	     (bit) < (size);					\
 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
 
Quoted 3 lines

include/linux/find.h unsigned long find_next_bit_le(const void *addr, unsigned · line 304

Quoted 1 line
include/linux/find.h +1 -0
+#define for_each_set_bitrange(b, e, addr, size)			\

The above needs a kerneldoc header.

OK.

Quoted 41 lines
Quoted 27 lines
include/linux/find.h +6 -0
+	for ((b) = find_next_bit((addr), (size), 0),		\
+	     (e) = find_next_zero_bit((addr), (size), (b) + 1);	\
+	     (b) < (size);					\
+	     (b) = find_next_bit((addr), (size), (e) + 1),	\
+	     (e) = find_next_zero_bit((addr), (size), (b) + 1))
+
 /**
  * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
  * @start: bit offset to start search and to store the current iteration offset
lib/vsprintf.c +2 -3
index 87acf66f0e4c..1ee54dace71e 100644
@@ -1240,38 +1240,30 @@ char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
 			 struct printf_spec spec, const char *fmt)
 {
 	int nr_bits = max_t(int, spec.field_width, 0);
-	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
-	int cur, rbot, rtop;
-	bool first = true;
+	char *start = buf;
+	int b, e;
 
 	if (check_pointer(&buf, end, bitmap, spec))
 		return buf;
 

lib/vsprintf.c char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, · line 1252

Quoted 10 lines
lib/vsprintf.c +4 -6
-	rbot = cur = find_first_bit(bitmap, nr_bits);
-	while (cur < nr_bits) {
-		rtop = cur;
-		cur = find_next_bit(bitmap, nr_bits, cur + 1);
-		if (cur < nr_bits && cur <= rtop + 1)
-			continue;
+	for_each_set_bitrange(b, e, bitmap, nr_bits) {
+		buf = number(buf, end, b, default_dec_spec);
+		if (e == b + 1)
+			goto put_comma;

Using a goto to skip a few lines instead of just having the reverse conditional is rather sloppy IMO.

Quoted 8 lines
		if (e != b + 1) {
			if (buf < end)
				*buf = '-';
			buf++;
			buf = number(buf, end, e - 1, default_dec_spec);
		}

Is much clearer.

I don't think it's clearer, but as you wish.

Quoted 22 lines
Quoted 22 lines
lib/vsprintf.c +8 -11
-		if (!first) {
-			if (buf < end)
-				*buf = ',';
-			buf++;
-		}
-		first = false;
+		if (buf < end)
+			*buf = '-';
 
-		buf = number(buf, end, rbot, default_dec_spec);
-		if (rbot < rtop) {
-			if (buf < end)
-				*buf = '-';
-			buf++;
+		buf = number(++buf, end, e - 1, default_dec_spec);
+put_comma:
+		if (buf < end)
+			*buf = ',';
+		buf++;
+	}
 
Quoted 8 lines

lib/vsprintf.c char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, · line 1265

Quoted 4 lines
lib/vsprintf.c +2 -2
-			buf = number(buf, end, rtop, default_dec_spec);
-		}
+	if (buf > start)
+		buf--;

If the above is to undo the last comma, please put back the first logic.

-- Steve

You're asking me to move part of the logic inside the loop which generally should be avoided. Is there any particular reason to do this?

Quoted 7 lines
Quoted 7 lines
lib/vsprintf.c +0 -2
-		rbot = cur;
-	}
 	return buf;
 }
 
maintainer
#6 lore raw

Re: [PATCH 2/2] bitmap: introduce for_each_set_bitrange

Quoted 19 lines · Yury Norov
Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Thu, 8 Jul 2021 20:45:19 -0700 Yury Norov <[email protected]> wrote:

bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit. We can do better by detecting ranges of set bits.

lib/vsprintf.c +0 -0
index 87acf66f0e4c..1ee54dace71e 100644
Quoted 6 lines

lib/vsprintf.c char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, · line 1265

Quoted 4 lines
lib/vsprintf.c +2 -2
-			buf = number(buf, end, rtop, default_dec_spec);
-		}
+	if (buf > start)
+		buf--;

If the above is to undo the last comma, please put back the first logic.

Quoted 2 lines

You're asking me to move part of the logic inside the loop which generally should be avoided. Is there any particular reason to do this?

vsprintf() should write what is needed and keep the rest of the given buffer intact. There is even a test for this in the test_printf module.

I think that test_printf does not complain here because only a single character is used and it is later replaced by the trailing '\0'.

By other words, undoing the last comma does not cause visible problems in the end. But from vsprintf() point of view, it is a hack that does not trigger the warning only by chance. And it is better to avoid it.

Best Regards,
Petr

author maintainer
#7 lore raw

Re: [PATCH 2/2] bitmap: introduce for_each_set_bitrange

Quoted 29 lines · Petr Mladek
Quoted 19 lines · Yury Norov
Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Thu, 8 Jul 2021 20:45:19 -0700 Yury Norov <[email protected]> wrote:

bitmap_list_string() is very ineffective when printing bitmaps with long ranges of set bits because it calls find_next_bit for each bit. We can do better by detecting ranges of set bits.

lib/vsprintf.c +0 -0
index 87acf66f0e4c..1ee54dace71e 100644
Quoted 6 lines

lib/vsprintf.c char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, · line 1265

Quoted 4 lines
lib/vsprintf.c +2 -2
-			buf = number(buf, end, rtop, default_dec_spec);
-		}
+	if (buf > start)
+		buf--;

If the above is to undo the last comma, please put back the first logic.

Quoted 2 lines

You're asking me to move part of the logic inside the loop which generally should be avoided. Is there any particular reason to do this?

vsprintf() should write what is needed and keep the rest of the given buffer intact. There is even a test for this in the test_printf module.

I think that test_printf does not complain here because only a single character is used and it is later replaced by the trailing '\0'.

Quoted 6 lines

By other words, undoing the last comma does not cause visible problems in the end. But from vsprintf() point of view, it is a hack that does not trigger the warning only by chance. And it is better to avoid it.

Best Regards,
Petr

Ah, OK. Thanks Petr.