public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation
@ 2023-08-11  0:57 Yury Norov
  2023-08-11  0:57 ` [PATCH v2 1/6] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
                   ` (5 more replies)
  0 siblings, 6 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

bitmap_{allocate,find_free,release}_region() functions are implemented
on top of _reg_op() machinery. It duplicates existing generic functionality
with no benefits. In fact, generic alternatives may work even better
because they optimized for small_const_nbits() case and overall very well
optimized for performance and code generation.

This series drops _reg_op() entirely.

v2:
 - fix declaration order (patch6);
 - fix kernel-doc varnings;
 - don't use 'drop' wording where (patches 2-4);

Yury Norov (6):
  bitmap: fix opencoded bitmap_allocate_region()
  bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear()
  bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit()
  bitmap: drop _reg_op() function
  bitmap: move bitmap_*_region functions to bitmap.h

 include/linux/bitmap.h |  65 ++++++++++++++++++-
 lib/bitmap.c           | 140 -----------------------------------------
 2 files changed, 62 insertions(+), 143 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2 1/6] bitmap: fix opencoded bitmap_allocate_region()
  2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
@ 2023-08-11  0:57 ` Yury Norov
  2023-08-11  0:57 ` [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

bitmap_find_region() opencodes bitmap_allocate_region(). Fix it.

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

diff --git a/lib/bitmap.c b/lib/bitmap.c
index ddb31015e38a..3a589016f5e0 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1315,10 +1315,8 @@ int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
 	unsigned int pos, end;		/* scans bitmap by regions of size order */
 
 	for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
-		if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
-			continue;
-		__reg_op(bitmap, pos, order, REG_OP_ALLOC);
-		return pos;
+		if (!bitmap_allocate_region(bitmap, pos, order))
+			return pos;
 	}
 	return -ENOMEM;
 }
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
  2023-08-11  0:57 ` [PATCH v2 1/6] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
@ 2023-08-11  0:57 ` Yury Norov
  2023-08-11  6:21   ` Rasmus Villemoes
  2023-08-11  9:25   ` Andy Shevchenko
  2023-08-11  0:57 ` [PATCH v2 3/6] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear() Yury Norov
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

_reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.

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

diff --git a/lib/bitmap.c b/lib/bitmap.c
index 3a589016f5e0..c9afe704fe4b 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1352,9 +1352,12 @@ EXPORT_SYMBOL(bitmap_release_region);
  */
 int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
 {
+	unsigned int nbits = pos + BIT(order);
+
 	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
 		return -EBUSY;
-	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
+	bitmap_set(bitmap, pos, nbits);
+	return 0;
 }
 EXPORT_SYMBOL(bitmap_allocate_region);
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 3/6] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear()
  2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
  2023-08-11  0:57 ` [PATCH v2 1/6] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
  2023-08-11  0:57 ` [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
@ 2023-08-11  0:57 ` Yury Norov
  2023-08-11  0:57 ` [PATCH v2 4/6] bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit() Yury Norov
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

_reg_op(REG_OP_RELEASE) duplicates bitmap_clear(). Fix it.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/bitmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index c9afe704fe4b..0ffafc41cd56 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1335,7 +1335,7 @@ EXPORT_SYMBOL(bitmap_find_free_region);
  */
 void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
 {
-	__reg_op(bitmap, pos, order, REG_OP_RELEASE);
+	bitmap_clear(bitmap, pos, pos + BIT(order));
 }
 EXPORT_SYMBOL(bitmap_release_region);
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 4/6] bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit()
  2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
                   ` (2 preceding siblings ...)
  2023-08-11  0:57 ` [PATCH v2 3/6] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear() Yury Norov
@ 2023-08-11  0:57 ` Yury Norov
  2023-08-11  0:57 ` [PATCH v2 5/6] bitmap: drop _reg_op() function Yury Norov
  2023-08-11  0:57 ` [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h Yury Norov
  5 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

_reg_op(REG_OP_ISFREE) can be trivially replaced with find_next_bit().
Doing that opens room for potential small_const_nbits()
optimization.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/bitmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index 0ffafc41cd56..d4560acbe077 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1354,7 +1354,7 @@ int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
 {
 	unsigned int nbits = pos + BIT(order);
 
-	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
+	if (find_next_bit(bitmap, pos, nbits) < nbits)
 		return -EBUSY;
 	bitmap_set(bitmap, pos, nbits);
 	return 0;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 5/6] bitmap: drop _reg_op() function
  2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
                   ` (3 preceding siblings ...)
  2023-08-11  0:57 ` [PATCH v2 4/6] bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit() Yury Norov
@ 2023-08-11  0:57 ` Yury Norov
  2023-08-11  0:57 ` [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h Yury Norov
  5 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

Now that all _reg_op() users are switched alternative functions, _reg_op()
machinery is not needed anymore.

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

diff --git a/lib/bitmap.c b/lib/bitmap.c
index d4560acbe077..ae619523c3ec 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1220,82 +1220,6 @@ void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 }
 #endif /* CONFIG_NUMA */
 
-/*
- * Common code for bitmap_*_region() routines.
- *	bitmap: array of unsigned longs corresponding to the bitmap
- *	pos: the beginning of the region
- *	order: region size (log base 2 of number of bits)
- *	reg_op: operation(s) to perform on that region of bitmap
- *
- * Can set, verify and/or release a region of bits in a bitmap,
- * depending on which combination of REG_OP_* flag bits is set.
- *
- * A region of a bitmap is a sequence of bits in the bitmap, of
- * some size '1 << order' (a power of two), aligned to that same
- * '1 << order' power of two.
- *
- * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
- * Returns 0 in all other cases and reg_ops.
- */
-
-enum {
-	REG_OP_ISFREE,		/* true if region is all zero bits */
-	REG_OP_ALLOC,		/* set all bits in region */
-	REG_OP_RELEASE,		/* clear all bits in region */
-};
-
-static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
-{
-	int nbits_reg;		/* number of bits in region */
-	int index;		/* index first long of region in bitmap */
-	int offset;		/* bit offset region in bitmap[index] */
-	int nlongs_reg;		/* num longs spanned by region in bitmap */
-	int nbitsinlong;	/* num bits of region in each spanned long */
-	unsigned long mask;	/* bitmask for one long of region */
-	int i;			/* scans bitmap by longs */
-	int ret = 0;		/* return value */
-
-	/*
-	 * Either nlongs_reg == 1 (for small orders that fit in one long)
-	 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
-	 */
-	nbits_reg = 1 << order;
-	index = pos / BITS_PER_LONG;
-	offset = pos - (index * BITS_PER_LONG);
-	nlongs_reg = BITS_TO_LONGS(nbits_reg);
-	nbitsinlong = min(nbits_reg,  BITS_PER_LONG);
-
-	/*
-	 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
-	 * overflows if nbitsinlong == BITS_PER_LONG.
-	 */
-	mask = (1UL << (nbitsinlong - 1));
-	mask += mask - 1;
-	mask <<= offset;
-
-	switch (reg_op) {
-	case REG_OP_ISFREE:
-		for (i = 0; i < nlongs_reg; i++) {
-			if (bitmap[index + i] & mask)
-				goto done;
-		}
-		ret = 1;	/* all bits in region free (zero) */
-		break;
-
-	case REG_OP_ALLOC:
-		for (i = 0; i < nlongs_reg; i++)
-			bitmap[index + i] |= mask;
-		break;
-
-	case REG_OP_RELEASE:
-		for (i = 0; i < nlongs_reg; i++)
-			bitmap[index + i] &= ~mask;
-		break;
-	}
-done:
-	return ret;
-}
-
 /**
  * bitmap_find_free_region - find a contiguous aligned mem region
  *	@bitmap: array of unsigned longs corresponding to the bitmap
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h
  2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
                   ` (4 preceding siblings ...)
  2023-08-11  0:57 ` [PATCH v2 5/6] bitmap: drop _reg_op() function Yury Norov
@ 2023-08-11  0:57 ` Yury Norov
  2023-08-11  9:27   ` Andy Shevchenko
  5 siblings, 1 reply; 15+ messages in thread
From: Yury Norov @ 2023-08-11  0:57 UTC (permalink / raw)
  To: linux-kernel, Andy Shevchenko, Rasmus Villemoes; +Cc: Yury Norov

Now that bitmap_*_region() functions are implemented as thin wrappers
around others, it's worth to move them to the header, as it opens room
for compile-time optimizations.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 include/linux/bitmap.h | 65 ++++++++++++++++++++++++++++++++++++++++--
 lib/bitmap.c           | 65 ------------------------------------------
 2 files changed, 62 insertions(+), 68 deletions(-)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 03644237e1ef..aa33fc290619 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -216,9 +216,6 @@ void bitmap_onto(unsigned long *dst, const unsigned long *orig,
 		const unsigned long *relmap, unsigned int bits);
 void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 		unsigned int sz, unsigned int nbits);
-int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
-void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
-int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
 
 #ifdef __BIG_ENDIAN
 void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
@@ -599,6 +596,68 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
 	map[index] |= value << offset;
 }
 
+/**
+ * bitmap_allocate_region - allocate bitmap region
+ *	@bitmap: array of unsigned longs corresponding to the bitmap
+ *	@pos: beginning of bit region to allocate
+ *	@order: region size (log base 2 of number of bits) to allocate
+ *
+ * Allocate (set bits in) a specified region of a bitmap.
+ *
+ * Return 0 on success, or %-EBUSY if specified region wasn't
+ * free (not all bits were zero).
+ */
+static inline int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
+{
+	unsigned int nbits = pos + BIT(order);
+
+	if (find_next_bit(bitmap, pos, nbits) < nbits)
+		return -EBUSY;
+	bitmap_set(bitmap, pos, nbits);
+	return 0;
+}
+
+/**
+ * bitmap_find_free_region - find a contiguous aligned mem region
+ *	@bitmap: array of unsigned longs corresponding to the bitmap
+ *	@bits: number of bits in the bitmap
+ *	@order: region size (log base 2 of number of bits) to find
+ *
+ * Find a region of free (zero) bits in a @bitmap of @bits bits and
+ * allocate them (set them to one).  Only consider regions of length
+ * a power (@order) of two, aligned to that power of two, which
+ * makes the search algorithm much faster.
+ *
+ * Return the bit offset in bitmap of the allocated region,
+ * or -errno on failure.
+ */
+static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
+{
+	unsigned int pos, end;		/* scans bitmap by regions of size order */
+
+	for (pos = 0 ; (end = pos + BIT(order)) <= bits; pos = end)
+		if (!bitmap_allocate_region(bitmap, pos, order))
+			return pos;
+
+	return -ENOMEM;
+}
+
+/**
+ * bitmap_release_region - release allocated bitmap region
+ *	@bitmap: array of unsigned longs corresponding to the bitmap
+ *	@pos: beginning of bit region to release
+ *	@order: region size (log base 2 of number of bits) to release
+ *
+ * This is the complement to __bitmap_find_free_region() and releases
+ * the found region (by clearing it in the bitmap).
+ *
+ * No return value.
+ */
+static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
+{
+	bitmap_clear(bitmap, pos, pos + BIT(order));
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* __LINUX_BITMAP_H */
diff --git a/lib/bitmap.c b/lib/bitmap.c
index ae619523c3ec..1c5d1f5d2071 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1220,71 +1220,6 @@ void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 }
 #endif /* CONFIG_NUMA */
 
-/**
- * bitmap_find_free_region - find a contiguous aligned mem region
- *	@bitmap: array of unsigned longs corresponding to the bitmap
- *	@bits: number of bits in the bitmap
- *	@order: region size (log base 2 of number of bits) to find
- *
- * Find a region of free (zero) bits in a @bitmap of @bits bits and
- * allocate them (set them to one).  Only consider regions of length
- * a power (@order) of two, aligned to that power of two, which
- * makes the search algorithm much faster.
- *
- * Return the bit offset in bitmap of the allocated region,
- * or -errno on failure.
- */
-int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
-{
-	unsigned int pos, end;		/* scans bitmap by regions of size order */
-
-	for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
-		if (!bitmap_allocate_region(bitmap, pos, order))
-			return pos;
-	}
-	return -ENOMEM;
-}
-EXPORT_SYMBOL(bitmap_find_free_region);
-
-/**
- * bitmap_release_region - release allocated bitmap region
- *	@bitmap: array of unsigned longs corresponding to the bitmap
- *	@pos: beginning of bit region to release
- *	@order: region size (log base 2 of number of bits) to release
- *
- * This is the complement to __bitmap_find_free_region() and releases
- * the found region (by clearing it in the bitmap).
- *
- * No return value.
- */
-void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
-{
-	bitmap_clear(bitmap, pos, pos + BIT(order));
-}
-EXPORT_SYMBOL(bitmap_release_region);
-
-/**
- * bitmap_allocate_region - allocate bitmap region
- *	@bitmap: array of unsigned longs corresponding to the bitmap
- *	@pos: beginning of bit region to allocate
- *	@order: region size (log base 2 of number of bits) to allocate
- *
- * Allocate (set bits in) a specified region of a bitmap.
- *
- * Return 0 on success, or %-EBUSY if specified region wasn't
- * free (not all bits were zero).
- */
-int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
-{
-	unsigned int nbits = pos + BIT(order);
-
-	if (find_next_bit(bitmap, pos, nbits) < nbits)
-		return -EBUSY;
-	bitmap_set(bitmap, pos, nbits);
-	return 0;
-}
-EXPORT_SYMBOL(bitmap_allocate_region);
-
 /**
  * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
  * @dst:   destination buffer
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11  0:57 ` [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
@ 2023-08-11  6:21   ` Rasmus Villemoes
  2023-08-11 12:56     ` Yury Norov
  2023-08-11  9:25   ` Andy Shevchenko
  1 sibling, 1 reply; 15+ messages in thread
From: Rasmus Villemoes @ 2023-08-11  6:21 UTC (permalink / raw)
  To: Yury Norov, linux-kernel, Andy Shevchenko

On 11/08/2023 02.57, Yury Norov wrote:
> _reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.
> 
> Signed-off-by: Yury Norov <yury.norov@gmail.com>
> ---
>  lib/bitmap.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/bitmap.c b/lib/bitmap.c
> index 3a589016f5e0..c9afe704fe4b 100644
> --- a/lib/bitmap.c
> +++ b/lib/bitmap.c
> @@ -1352,9 +1352,12 @@ EXPORT_SYMBOL(bitmap_release_region);
>   */
>  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
>  {
> +	unsigned int nbits = pos + BIT(order);
> +

That really doesn't sound right. Have you added self-tests for these
functions first and then used those to catch regressions?

Rasmus


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11  0:57 ` [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
  2023-08-11  6:21   ` Rasmus Villemoes
@ 2023-08-11  9:25   ` Andy Shevchenko
  2023-08-11 13:01     ` Yury Norov
  1 sibling, 1 reply; 15+ messages in thread
From: Andy Shevchenko @ 2023-08-11  9:25 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Rasmus Villemoes

On Thu, Aug 10, 2023 at 05:57:28PM -0700, Yury Norov wrote:
> _reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.

...

>  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
>  {
> +	unsigned int nbits = pos + BIT(order);

Shouldn't this be unsigned long?
As the prototype of the used later unsigned long find_next_bit().

>  	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
>  		return -EBUSY;
> -	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
> +	bitmap_set(bitmap, pos, nbits);
> +	return 0;
>  }

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h
  2023-08-11  0:57 ` [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h Yury Norov
@ 2023-08-11  9:27   ` Andy Shevchenko
  2023-08-11 13:05     ` Yury Norov
  0 siblings, 1 reply; 15+ messages in thread
From: Andy Shevchenko @ 2023-08-11  9:27 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Rasmus Villemoes

On Thu, Aug 10, 2023 at 05:57:32PM -0700, Yury Norov wrote:
> Now that bitmap_*_region() functions are implemented as thin wrappers
> around others, it's worth to move them to the header, as it opens room
> for compile-time optimizations.

...

> + * Return 0 on success, or %-EBUSY if specified region wasn't
> + * free (not all bits were zero).

Run

	scripts/kernel-doc -v -none -Wall

against this

...

> + * Return the bit offset in bitmap of the allocated region,
> + * or -errno on failure.

Ditto.

...

> +static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
> +{
> +	unsigned int pos, end;		/* scans bitmap by regions of size order */
> +
> +	for (pos = 0 ; (end = pos + BIT(order)) <= bits; pos = end)

Extra space.

> +		if (!bitmap_allocate_region(bitmap, pos, order))
> +			return pos;
> +
> +	return -ENOMEM;
> +}

...

> +/**
> + * bitmap_release_region - release allocated bitmap region
> + *	@bitmap: array of unsigned longs corresponding to the bitmap
> + *	@pos: beginning of bit region to release
> + *	@order: region size (log base 2 of number of bits) to release
> + *
> + * This is the complement to __bitmap_find_free_region() and releases
> + * the found region (by clearing it in the bitmap).

> + * No return value.

Useless line.

> + */

...

Seems like the original code has all these, perhaps update in a separate patch?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11  6:21   ` Rasmus Villemoes
@ 2023-08-11 12:56     ` Yury Norov
  2023-08-11 13:31       ` Rasmus Villemoes
  0 siblings, 1 reply; 15+ messages in thread
From: Yury Norov @ 2023-08-11 12:56 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: linux-kernel, Andy Shevchenko

On Fri, Aug 11, 2023 at 08:21:33AM +0200, Rasmus Villemoes wrote:
> On 11/08/2023 02.57, Yury Norov wrote:
> > _reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.
> > 
> > Signed-off-by: Yury Norov <yury.norov@gmail.com>
> > ---
> >  lib/bitmap.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/lib/bitmap.c b/lib/bitmap.c
> > index 3a589016f5e0..c9afe704fe4b 100644
> > --- a/lib/bitmap.c
> > +++ b/lib/bitmap.c
> > @@ -1352,9 +1352,12 @@ EXPORT_SYMBOL(bitmap_release_region);
> >   */
> >  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
> >  {
> > +	unsigned int nbits = pos + BIT(order);
> > +
> 
> That really doesn't sound right. Have you added self-tests for these
> functions first and then used those to catch regressions?

When bitmap_allocate_region() is broken, almost every arch build fails
to boot. Can you explain what exactly looks wrong to you?

Thanks,
Yury

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11  9:25   ` Andy Shevchenko
@ 2023-08-11 13:01     ` Yury Norov
  0 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11 13:01 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-kernel, Rasmus Villemoes

On Fri, Aug 11, 2023 at 12:25:02PM +0300, Andy Shevchenko wrote:
> On Thu, Aug 10, 2023 at 05:57:28PM -0700, Yury Norov wrote:
> > _reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.
> 
> ...
> 
> >  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
> >  {
> > +	unsigned int nbits = pos + BIT(order);
> 
> Shouldn't this be unsigned long?
> As the prototype of the used later unsigned long find_next_bit().

Linux doesn't support bitmaps longer than 2^32 bits. I really doubt
such huge bitmaps would ever exist, but if that will happen, too many
bitmap functions will have to be revisited.

For the reference, check the code of __reg_op(), which I remove in the
series - all indexes and offsets are int's there.

Thanks,
Yury
 
> >  	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
> >  		return -EBUSY;
> > -	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
> > +	bitmap_set(bitmap, pos, nbits);
> > +	return 0;
> >  }
> 
> -- 
> With Best Regards,
> Andy Shevchenko
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h
  2023-08-11  9:27   ` Andy Shevchenko
@ 2023-08-11 13:05     ` Yury Norov
  0 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-11 13:05 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-kernel, Rasmus Villemoes

On Fri, Aug 11, 2023 at 12:27:51PM +0300, Andy Shevchenko wrote:
> On Thu, Aug 10, 2023 at 05:57:32PM -0700, Yury Norov wrote:
> > Now that bitmap_*_region() functions are implemented as thin wrappers
> > around others, it's worth to move them to the header, as it opens room
> > for compile-time optimizations.
> 
> ...
> 
> > + * Return 0 on success, or %-EBUSY if specified region wasn't
> > + * free (not all bits were zero).
> 
> Run
> 
> 	scripts/kernel-doc -v -none -Wall
> ...
> 
> Seems like the original code has all these, perhaps update in a separate patch?
 
Yes. This patch is named _move_, which means - I don't touch the code.
I already replaced (1U << order) to BIT(order), and I'm not happy with
that. We need to do that in a separate patch.

Thanks,
Yury

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11 12:56     ` Yury Norov
@ 2023-08-11 13:31       ` Rasmus Villemoes
  2023-08-14  0:39         ` Yury Norov
  0 siblings, 1 reply; 15+ messages in thread
From: Rasmus Villemoes @ 2023-08-11 13:31 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Andy Shevchenko

On 11/08/2023 14.56, Yury Norov wrote:
> On Fri, Aug 11, 2023 at 08:21:33AM +0200, Rasmus Villemoes wrote:
>> On 11/08/2023 02.57, Yury Norov wrote:
>>> _reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.
>>>
>>> Signed-off-by: Yury Norov <yury.norov@gmail.com>
>>> ---
>>>  lib/bitmap.c | 5 ++++-
>>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/lib/bitmap.c b/lib/bitmap.c
>>> index 3a589016f5e0..c9afe704fe4b 100644
>>> --- a/lib/bitmap.c
>>> +++ b/lib/bitmap.c
>>> @@ -1352,9 +1352,12 @@ EXPORT_SYMBOL(bitmap_release_region);
>>>   */
>>>  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
>>>  {
>>> +	unsigned int nbits = pos + BIT(order);
>>> +
>>
>> That really doesn't sound right. Have you added self-tests for these
>> functions first and then used those to catch regressions?
> 
> When bitmap_allocate_region() is broken, almost every arch build fails
> to boot. Can you explain what exactly looks wrong to you?

The number of bits we are about to set should not be [position in bitmap
to start from] + [2^order]. The second half of that patch was

-	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
+	bitmap_set(bitmap, pos, nbits);
+	return 0;

so instead of setting 1<<nbits starting at pos, you're now setting
pos+(1<<nbits) starting at pos. How is that correct?

Rasmus


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-11 13:31       ` Rasmus Villemoes
@ 2023-08-14  0:39         ` Yury Norov
  0 siblings, 0 replies; 15+ messages in thread
From: Yury Norov @ 2023-08-14  0:39 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: linux-kernel, Andy Shevchenko

On Fri, Aug 11, 2023 at 03:31:01PM +0200, Rasmus Villemoes wrote:
> On 11/08/2023 14.56, Yury Norov wrote:
> > On Fri, Aug 11, 2023 at 08:21:33AM +0200, Rasmus Villemoes wrote:
> >> On 11/08/2023 02.57, Yury Norov wrote:
> >>> _reg_op(REG_OP_ALLOC) duplicates bitmap_set(). Fix it.
> >>>
> >>> Signed-off-by: Yury Norov <yury.norov@gmail.com>
> >>> ---
> >>>  lib/bitmap.c | 5 ++++-
> >>>  1 file changed, 4 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/lib/bitmap.c b/lib/bitmap.c
> >>> index 3a589016f5e0..c9afe704fe4b 100644
> >>> --- a/lib/bitmap.c
> >>> +++ b/lib/bitmap.c
> >>> @@ -1352,9 +1352,12 @@ EXPORT_SYMBOL(bitmap_release_region);
> >>>   */
> >>>  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
> >>>  {
> >>> +	unsigned int nbits = pos + BIT(order);
> >>> +
> >>
> >> That really doesn't sound right. Have you added self-tests for these
> >> functions first and then used those to catch regressions?
> > 
> > When bitmap_allocate_region() is broken, almost every arch build fails
> > to boot. Can you explain what exactly looks wrong to you?
> 
> The number of bits we are about to set should not be [position in bitmap
> to start from] + [2^order]. The second half of that patch was
> 
> -	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
> +	bitmap_set(bitmap, pos, nbits);
> +	return 0;
> 
> so instead of setting 1<<nbits starting at pos, you're now setting
> pos+(1<<nbits) starting at pos. How is that correct?

You're right. Bad on me. :/

I'll send v3 with all the discussed cleared shortly.

Thanks,
YUry

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2023-08-14  0:40 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-11  0:57 [PATCH v2 0/6] bitmap: cleanup bitmap_*_region() implementation Yury Norov
2023-08-11  0:57 ` [PATCH v2 1/6] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
2023-08-11  0:57 ` [PATCH v2 2/6] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
2023-08-11  6:21   ` Rasmus Villemoes
2023-08-11 12:56     ` Yury Norov
2023-08-11 13:31       ` Rasmus Villemoes
2023-08-14  0:39         ` Yury Norov
2023-08-11  9:25   ` Andy Shevchenko
2023-08-11 13:01     ` Yury Norov
2023-08-11  0:57 ` [PATCH v2 3/6] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear() Yury Norov
2023-08-11  0:57 ` [PATCH v2 4/6] bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit() Yury Norov
2023-08-11  0:57 ` [PATCH v2 5/6] bitmap: drop _reg_op() function Yury Norov
2023-08-11  0:57 ` [PATCH v2 6/6] bitmap: move bitmap_*_region functions to bitmap.h Yury Norov
2023-08-11  9:27   ` Andy Shevchenko
2023-08-11 13:05     ` Yury Norov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox