[v2,6/6] bitmap: move bitmap_*_region functions to bitmap.h
Commit Message
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(-)
Comments
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?
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
@@ -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 */
@@ -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