diff mbox series

[02/16] lib/bitmap: don't call bitmap_set() with len == 0

Message ID 20220718192844.1805158-3-yury.norov@gmail.com (mailing list archive)
State New
Headers show
Series Introduce DEBUG_BITMAP config option and bitmap_check_params() | expand

Commit Message

Yury Norov July 18, 2022, 7:28 p.m. UTC
bitmap_parselist() format allows passing 0-length regions, but because
len == 0 is not covered by small_const_nbits() macro, we have to call
__bitmap_set() and do some prologue calculations just for nothing.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/bitmap.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Andy Shevchenko July 18, 2022, 9:06 p.m. UTC | #1
On Mon, Jul 18, 2022 at 12:28:30PM -0700, Yury Norov wrote:
> bitmap_parselist() format allows passing 0-length regions, but because
> len == 0 is not covered by small_const_nbits() macro, we have to call
> __bitmap_set() and do some prologue calculations just for nothing.

...

>  static void bitmap_set_region(const struct region *r, unsigned long *bitmap)
>  {
> -	unsigned int start;
> +	unsigned int start, len;
>  
> -	for (start = r->start; start <= r->end; start += r->group_len)
> -		bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
> +	for (start = r->start; start <= r->end; start += r->group_len) {
> +		len = min(r->end - start + 1, r->off);
> +		if (len > 0)
> +			bitmap_set(bitmap, start, len);

Maybe I'm missing something, but how small_const_nbits() can possible be useful
here? I mean in which cases the compiler would be able to prove that len is a
const with < sizeof(unsigned long) bits?

> +	}
>  }
diff mbox series

Patch

diff --git a/lib/bitmap.c b/lib/bitmap.c
index cd4dd848ea6a..790df2ea02df 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -712,10 +712,13 @@  struct region {
 
 static void bitmap_set_region(const struct region *r, unsigned long *bitmap)
 {
-	unsigned int start;
+	unsigned int start, len;
 
-	for (start = r->start; start <= r->end; start += r->group_len)
-		bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
+	for (start = r->start; start <= r->end; start += r->group_len) {
+		len = min(r->end - start + 1, r->off);
+		if (len > 0)
+			bitmap_set(bitmap, start, len);
+	}
 }
 
 static int bitmap_check_region(const struct region *r)