public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation
@ 2023-08-29  2:39 Yury Norov
  2023-08-29  2:39 ` [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style Yury Norov
                   ` (7 more replies)
  0 siblings, 8 replies; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

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: https://lore.kernel.org/lkml/20230811005732.107718-2-yury.norov@gmail.com/T/
v3: https://lore.kernel.org/lkml/20230815233628.45016-2-yury.norov@gmail.com/T/
v4: address nits for v3.

Yury Norov (8):
  bitmap: align __reg_op() wrappers with modern coding style
  bitmap: add test for bitmap_*_regoon() functions
  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 |  63 ++++++++++++++++++-
 lib/bitmap.c           | 140 -----------------------------------------
 lib/test_bitmap.c      |  24 +++++++
 3 files changed, 84 insertions(+), 143 deletions(-)

-- 
2.39.2


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

* [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style
  2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
@ 2023-08-29  2:39 ` Yury Norov
  2023-08-29 15:26   ` Andy Shevchenko
  2023-08-29  2:39 ` [PATCH v4 2/8] bitmap: add test for bitmap_*_region() functions Yury Norov
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

Fix comments so that scripts/kernel-doc doesn't warn, and fix for-loop
stype in bitmap_find_free_region().

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/bitmap.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/lib/bitmap.c b/lib/bitmap.c
index 935e0f96e785..250715260d95 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1234,8 +1234,8 @@ void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  * 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.
+ * Return: 1 if REG_OP_ISFREE succeeds (region is all zero bits).
+ *	   0 in all other cases and reg_ops.
  */
 
 enum {
@@ -1307,14 +1307,14 @@ static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_
  * 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,
+ * 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) {
+	for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) {
 		if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
 			continue;
 		__reg_op(bitmap, pos, order, REG_OP_ALLOC);
@@ -1332,8 +1332,6 @@ EXPORT_SYMBOL(bitmap_find_free_region);
  *
  * 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)
 {
@@ -1349,7 +1347,7 @@ EXPORT_SYMBOL(bitmap_release_region);
  *
  * Allocate (set bits in) a specified region of a bitmap.
  *
- * Return 0 on success, or %-EBUSY if specified region wasn't
+ * 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)
-- 
2.39.2


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

* [PATCH v4 2/8] bitmap: add test for bitmap_*_region() functions
  2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
  2023-08-29  2:39 ` [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style Yury Norov
@ 2023-08-29  2:39 ` Yury Norov
  2023-08-29 15:29   ` Andy Shevchenko
  2023-08-29  2:39 ` [PATCH v4 3/8] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
                   ` (5 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

Test basic functionality of bitmap_{allocate,release,find_free}_region()
functions.

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 lib/test_bitmap.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index f2ea9f30c7c5..65f22c2578b0 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -330,6 +330,29 @@ static void __init test_copy(void)
 	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
 }
 
+static void __init test_bitmap_region(void)
+{
+	int pos, order;
+
+	DECLARE_BITMAP(bmap, 1000);
+
+	bitmap_zero(bmap, 1000);
+
+	for (order = 0; order < 10; order++) {
+		pos = bitmap_find_free_region(bmap, 1000, order);
+		if (order == 0)
+			expect_eq_uint(pos, 0);
+		else
+			expect_eq_uint(pos, order < 9 ? BIT(order) : -ENOMEM);
+	}
+
+	bitmap_release_region(bmap, 0, 0);
+	for (order = 1; order < 9; order++)
+		bitmap_release_region(bmap, BIT(order), order);
+
+	expect_eq_uint(bitmap_weight(bmap, 1000), 0);
+}
+
 #define EXP2_IN_BITS	(sizeof(exp2) * 8)
 
 static void __init test_replace(void)
@@ -1227,6 +1250,7 @@ static void __init selftest(void)
 	test_zero_clear();
 	test_fill_set();
 	test_copy();
+	test_bitmap_region();
 	test_replace();
 	test_bitmap_arr32();
 	test_bitmap_arr64();
-- 
2.39.2


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

* [PATCH v4 3/8] bitmap: fix opencoded bitmap_allocate_region()
  2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
  2023-08-29  2:39 ` [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style Yury Norov
  2023-08-29  2:39 ` [PATCH v4 2/8] bitmap: add test for bitmap_*_region() functions Yury Norov
@ 2023-08-29  2:39 ` Yury Norov
  2023-08-29 15:30   ` Andy Shevchenko
  2023-08-29  2:39 ` [PATCH v4 4/8] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

bitmap_find_region() opencodes bitmap_allocate_region(). Fix it.

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
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 250715260d95..3c069944dd83 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 + BIT(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] 14+ messages in thread

* [PATCH v4 4/8] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
  2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
                   ` (2 preceding siblings ...)
  2023-08-29  2:39 ` [PATCH v4 3/8] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
@ 2023-08-29  2:39 ` Yury Norov
  2023-08-29  2:39 ` [PATCH v4 5/8] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear() Yury Norov
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

_reg_op(REG_OP_ALLOC) duplicates bitmap_set().

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
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 3c069944dd83..0fabcb0e4628 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1350,9 +1350,12 @@ EXPORT_SYMBOL(bitmap_release_region);
  */
 int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
 {
+	unsigned int len = 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, len);
+	return 0;
 }
 EXPORT_SYMBOL(bitmap_allocate_region);
 
-- 
2.39.2


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

* [PATCH v4 5/8] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear()
  2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
                   ` (3 preceding siblings ...)
  2023-08-29  2:39 ` [PATCH v4 4/8] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
@ 2023-08-29  2:39 ` Yury Norov
  2023-08-29 15:32   ` Andy Shevchenko
  2023-08-29  2:39 ` [PATCH v4 6/8] bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit() Yury Norov
                   ` (2 subsequent siblings)
  7 siblings, 1 reply; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

_reg_op(REG_OP_RELEASE) duplicates bitmap_clear().

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
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 0fabcb0e4628..56595ce513f5 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1333,7 +1333,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] 14+ messages in thread

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

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

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
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 56595ce513f5..c94340c4ba86 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1352,7 +1352,7 @@ int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
 {
 	unsigned int len = BIT(order);
 
-	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
+	if (find_next_bit(bitmap, pos + len, pos) < pos + len)
 		return -EBUSY;
 	bitmap_set(bitmap, pos, len);
 	return 0;
-- 
2.39.2


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

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

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

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
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 c94340c4ba86..ab730ce52d21 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.
- *
- * Return: 1 if REG_OP_ISFREE succeeds (region is all zero bits).
- *	   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] 14+ messages in thread

* [PATCH v4 8/8] bitmap: move bitmap_*_region() functions to bitmap.h
  2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
                   ` (6 preceding siblings ...)
  2023-08-29  2:39 ` [PATCH v4 7/8] bitmap: drop _reg_op() function Yury Norov
@ 2023-08-29  2:39 ` Yury Norov
  2023-08-29 10:07   ` Yury Norov
  7 siblings, 1 reply; 14+ messages in thread
From: Yury Norov @ 2023-08-29  2:39 UTC (permalink / raw)
  To: linux-kernel; +Cc: Yury Norov, Andy Shevchenko, Rasmus Villemoes

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.

CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
CC: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 include/linux/bitmap.h | 63 ++++++++++++++++++++++++++++++++++++++++--
 lib/bitmap.c           | 63 ------------------------------------------
 2 files changed, 60 insertions(+), 66 deletions(-)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 1516ff979315..973ae5b11c70 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);
 
 int bitmap_print_to_pagebuf(bool list, char *buf,
 				   const unsigned long *maskp, int nmaskbits);
@@ -594,6 +591,66 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
 	map[index] |= value << offset;
 }
 
+/**
+ * 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).
+ */
+static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
+{
+	bitmap_clear(bitmap, pos, pos + BIT(order));
+}
+
+/**
+ * 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 len = BIT(order);
+
+	if (find_next_bit(bitmap, pos + len, pos) < pos + len)
+		return -EBUSY;
+	bitmap_set(bitmap, pos, len);
+	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;
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* __LINUX_BITMAP_H */
diff --git a/lib/bitmap.c b/lib/bitmap.c
index ab730ce52d21..65a6d5d47d81 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -1220,69 +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 + BIT(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).
- */
-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 len = BIT(order);
-
-	if (find_next_bit(bitmap, pos + len, pos) < pos + len)
-		return -EBUSY;
-	bitmap_set(bitmap, pos, len);
-	return 0;
-}
-EXPORT_SYMBOL(bitmap_allocate_region);
-
 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
 {
 	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
-- 
2.39.2


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

* Re: [PATCH v4 8/8] bitmap: move bitmap_*_region() functions to bitmap.h
  2023-08-29  2:39 ` [PATCH v4 8/8] bitmap: move bitmap_*_region() functions to bitmap.h Yury Norov
@ 2023-08-29 10:07   ` Yury Norov
  0 siblings, 0 replies; 14+ messages in thread
From: Yury Norov @ 2023-08-29 10:07 UTC (permalink / raw)
  To: linux-kernel; +Cc: Andy Shevchenko, Rasmus Villemoes

> +/**
> + * 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).
> + */
> +static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
> +{
> +	bitmap_clear(bitmap, pos, pos + BIT(order));
> +}

This is a leftover after wrong rebase from previous buggy version. It
should be:

        bitmap_clear(bitmap, pos, BIT(order));

Please igonre the patch, I'll resend it shourtly.

Sorry for noise.

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

* Re: [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style
  2023-08-29  2:39 ` [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style Yury Norov
@ 2023-08-29 15:26   ` Andy Shevchenko
  0 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2023-08-29 15:26 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Rasmus Villemoes

On Mon, Aug 28, 2023 at 07:39:04PM -0700, Yury Norov wrote:
> Fix comments so that scripts/kernel-doc doesn't warn, and fix for-loop
> stype in bitmap_find_free_region().

...

> CC: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

> Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

When you have a tag, no need to have a separate Cc.
An in general, explicit Cc in the commit message make a lot of noise
and makes git index of the project fatter, you can use --cc parameter
when preparing / sending patches.

Other than that
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v4 2/8] bitmap: add test for bitmap_*_region() functions
  2023-08-29  2:39 ` [PATCH v4 2/8] bitmap: add test for bitmap_*_region() functions Yury Norov
@ 2023-08-29 15:29   ` Andy Shevchenko
  0 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2023-08-29 15:29 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Rasmus Villemoes

On Mon, Aug 28, 2023 at 07:39:05PM -0700, Yury Norov wrote:
> Test basic functionality of bitmap_{allocate,release,find_free}_region()
> functions.

I'm always fan of new test cases,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v4 3/8] bitmap: fix opencoded bitmap_allocate_region()
  2023-08-29  2:39 ` [PATCH v4 3/8] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
@ 2023-08-29 15:30   ` Andy Shevchenko
  0 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2023-08-29 15:30 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Rasmus Villemoes

On Mon, Aug 28, 2023 at 07:39:06PM -0700, Yury Norov wrote:
> bitmap_find_region() opencodes bitmap_allocate_region(). Fix it.

I don't think you "fix" anything here. You just refactor / replace the old code
with new one. I guess I made the same remark against v3.

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH v4 5/8] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear()
  2023-08-29  2:39 ` [PATCH v4 5/8] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear() Yury Norov
@ 2023-08-29 15:32   ` Andy Shevchenko
  0 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2023-08-29 15:32 UTC (permalink / raw)
  To: Yury Norov; +Cc: linux-kernel, Rasmus Villemoes

On Mon, Aug 28, 2023 at 07:39:08PM -0700, Yury Norov wrote:
> _reg_op(REG_OP_RELEASE) duplicates bitmap_clear().

...

> -	__reg_op(bitmap, pos, order, REG_OP_RELEASE);
> +	bitmap_clear(bitmap, pos, pos + BIT(order));

Is it still buggy?

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2023-08-29 15:45 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-29  2:39 [PATCH v4 0/8] bitmap: cleanup bitmap_*_region() implementation Yury Norov
2023-08-29  2:39 ` [PATCH v4 1/8] bitmap: align __reg_op() wrappers with modern coding style Yury Norov
2023-08-29 15:26   ` Andy Shevchenko
2023-08-29  2:39 ` [PATCH v4 2/8] bitmap: add test for bitmap_*_region() functions Yury Norov
2023-08-29 15:29   ` Andy Shevchenko
2023-08-29  2:39 ` [PATCH v4 3/8] bitmap: fix opencoded bitmap_allocate_region() Yury Norov
2023-08-29 15:30   ` Andy Shevchenko
2023-08-29  2:39 ` [PATCH v4 4/8] bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set() Yury Norov
2023-08-29  2:39 ` [PATCH v4 5/8] bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear() Yury Norov
2023-08-29 15:32   ` Andy Shevchenko
2023-08-29  2:39 ` [PATCH v4 6/8] bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit() Yury Norov
2023-08-29  2:39 ` [PATCH v4 7/8] bitmap: drop _reg_op() function Yury Norov
2023-08-29  2:39 ` [PATCH v4 8/8] bitmap: move bitmap_*_region() functions to bitmap.h Yury Norov
2023-08-29 10:07   ` Yury Norov

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