Skip to contentSkip to conversation

lib: unload lib/bitmap.c

PATCH In review Yury Norov 2 participants 8 messages Oct 7, 2023 – Oct 16, 2023

Subsystems (3)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

cover lib: unload lib/bitmap.c

The file is intended to hold functions to operate on bit arrays, but this days, more than 1/3 of bitmap.c is helpers for bitmap-to-string converters, plus some wrappers for device.h.

So move those out of lib/bitmap.c in sake of readability and maintainability.

Functionally, this series is a no-op.

Yury Norov (2):
lib/bitmap: move bitmap allocators for device to linux/device.h lib/bitmap: split-out string-related operations to a separate files

 MAINTAINERS                |   2 +
 include/linux/bitmap-str.h |  16 ++
 include/linux/bitmap.h     |  25 +-
 include/linux/device.h     |  30 ++
 lib/Makefile               |   2 +-
 lib/bitmap-str.c           | 510 ++++++++++++++++++++++++++++++++++
 lib/bitmap.c               | 545 -------------------------------------
 7 files changed, 560 insertions(+), 570 deletions(-)
 create mode 100644 include/linux/bitmap-str.h
 create mode 100644 lib/bitmap-str.c
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH 1/2 lib/bitmap: move bitmap allocators for device to linux/device.h

The allocators are simple wrappers around bitmap_{alloc,free}(). So move them from bitmap to device sources.

Similarly to other device wrappers, turn them to static inlines and place in header.

---
 include/linux/bitmap.h |  8 --------
 include/linux/device.h | 30 ++++++++++++++++++++++++++++++
 lib/bitmap.c           | 33 ---------------------------------
 3 files changed, 30 insertions(+), 41 deletions(-)
include/linux/bitmap.h +0 -8
index 03644237e1ef..ce8fcd8736f1 100644
@@ -11,8 +11,6 @@
 #include <linux/string.h>
 #include <linux/types.h>
 
-struct device;
-
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
  * longs.  The bitmap interface and available operations are listed
@@ -125,12 +123,6 @@ unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
 void bitmap_free(const unsigned long *bitmap);
 
-/* Managed variants of the above. */
-unsigned long *devm_bitmap_alloc(struct device *dev,
-				 unsigned int nbits, gfp_t flags);
-unsigned long *devm_bitmap_zalloc(struct device *dev,
-				  unsigned int nbits, gfp_t flags);
-
 /*
  * lib/bitmap.c provides these functions:
  */
include/linux/device.h +30 -0
index 56d93a1ffb7b..01b8161b283a 100644
@@ -435,6 +435,36 @@ struct device_dma_parameters {
 	unsigned long segment_boundary_mask;
 };
 
+static inline void devm_bitmap_free(void *data)
+{
+	unsigned long *bitmap = data;
+
+	bitmap_free(bitmap);
+}
+
+static inline unsigned long *devm_bitmap_alloc(struct device *dev,
+				 unsigned int nbits, gfp_t flags)
+{
+	unsigned long *bitmap;
+	int ret;
+
+	bitmap = bitmap_alloc(nbits, flags);
+	if (!bitmap)
+		return NULL;
+
+	ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
+	if (ret)
+		return NULL;
+
+	return bitmap;
+}
+
+static inline unsigned long *devm_bitmap_zalloc(struct device *dev,
+				  unsigned int nbits, gfp_t flags)
+{
+	return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
+}
+
 /**
  * enum device_link_state - Device link states.
  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
lib/bitmap.c +0 -33
index ddb31015e38a..41f32fa3a80f 100644
@@ -8,7 +8,6 @@
 #include <linux/bitops.h>
 #include <linux/bug.h>
 #include <linux/ctype.h>
-#include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/export.h>
 #include <linux/kernel.h>
@@ -1415,38 +1414,6 @@ void bitmap_free(const unsigned long *bitmap)
 }
 EXPORT_SYMBOL(bitmap_free);
 
-static void devm_bitmap_free(void *data)
-{
-	unsigned long *bitmap = data;
-
-	bitmap_free(bitmap);
-}
-
-unsigned long *devm_bitmap_alloc(struct device *dev,
-				 unsigned int nbits, gfp_t flags)
-{
-	unsigned long *bitmap;
-	int ret;
-
-	bitmap = bitmap_alloc(nbits, flags);
-	if (!bitmap)
-		return NULL;
-
-	ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
-	if (ret)
-		return NULL;
-
-	return bitmap;
-}
-EXPORT_SYMBOL_GPL(devm_bitmap_alloc);
-
-unsigned long *devm_bitmap_zalloc(struct device *dev,
-				  unsigned int nbits, gfp_t flags)
-{
-	return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
-}
-EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);
-
 #if BITS_PER_LONG == 64
 /**
  * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 1/2] lib/bitmap: move bitmap allocators for device to linux/device.h

Quoted 2 lines · Yury Norov

The allocators are simple wrappers around bitmap_{alloc,free}(). So move them from bitmap to device sources.

No, they belong in the bitmap.h file, as they are devm_* versions of the same functions in this file. They don't belong in the device.h file.

Quoted 2 lines

Similarly to other device wrappers, turn them to static inlines and place in header.

Why do these need to be inline functions?

thanks,

greg k-h

author maintainer
#3 lore raw

Re: [PATCH 1/2] lib/bitmap: move bitmap allocators for device to linux/device.h

Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Yury Norov

The allocators are simple wrappers around bitmap_{alloc,free}(). So move them from bitmap to device sources.

No, they belong in the bitmap.h file, as they are devm_* versions of the same functions in this file. They don't belong in the device.h file.

OK then. I don't thing that the functions are anything wrong, and don't want to 'get rid of them' in any way.

But could you please elaborate? I'm not too familiar to devm_* things, and to me devm_alloc/free() look similar to e.g. vfio_dma_bitmap_alloc_all() or iova_bitmap_alloc(), which allocate memory for bitmap + do some other initialization things.

And they all reside in corresponding subsystems. Why devm differs?

Quoted 4 lines
Quoted 2 lines

Similarly to other device wrappers, turn them to static inlines and place in header.

Why do these need to be inline functions?

Because they are small. devm_bitmap_free() and devm_bitmap_zalloc() are pure one-line wrappers, and devm_bimap_alloc() is a 2 function calls followed by conditionals, which is similar to __devm_add_action_or_reset() or devm_kmalloc_array() in the same file, and much less than some other inliners in the source tree.

In my plans, I want to move bitmap_{z,}alloc/free() to linux/bitmap.h, and that way devm_bitmap_alloc() together with other users would be propagated __kmalloc_array() by compiler without generating pretty useless call/ret's, and benefit from compile-time optimizations if __builtin_constant_p() hits.

Maybe it's worth to do in this series.

Thanks,
Yury

#4 lore raw

Re: [PATCH 1/2] lib/bitmap: move bitmap allocators for device to linux/device.h

Quoted 11 lines · Yury Norov
Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines · Yury Norov

The allocators are simple wrappers around bitmap_{alloc,free}(). So move them from bitmap to device sources.

No, they belong in the bitmap.h file, as they are devm_* versions of the same functions in this file. They don't belong in the device.h file.

OK then. I don't thing that the functions are anything wrong, and don't want to 'get rid of them' in any way.

Quoted 6 lines

But could you please elaborate? I'm not too familiar to devm_* things, and to me devm_alloc/free() look similar to e.g. vfio_dma_bitmap_alloc_all() or iova_bitmap_alloc(), which allocate memory for bitmap + do some other initialization things.

And they all reside in corresponding subsystems. Why devm differs?

They are just "devm" versions of the normal functions, so they belong next to those normal functions as well.

Quoted 10 lines
Quoted 4 lines
Quoted 2 lines

Similarly to other device wrappers, turn them to static inlines and place in header.

Why do these need to be inline functions?

Because they are small. devm_bitmap_free() and devm_bitmap_zalloc() are pure one-line wrappers, and devm_bimap_alloc() is a 2 function calls followed by conditionals, which is similar to __devm_add_action_or_reset() or devm_kmalloc_array() in the same file, and much less than some other inliners in the source tree.

Are you sure this works properly? the _free functions for devm_* calls are set as function pointers and you just passed in a function pointer to an inline function in your patch. How is that going to work? Will you get even more versions than the original one had (hint, I think you will, one per file it is called in...)

Quoted 5 lines

In my plans, I want to move bitmap_{z,}alloc/free() to linux/bitmap.h, and that way devm_bitmap_alloc() together with other users would be propagated __kmalloc_array() by compiler without generating pretty useless call/ret's, and benefit from compile-time optimizations if __builtin_constant_p() hits.

Does that really matter for _alloc() calls? These should not be on a fast path (or at least the devm_*() ones should not be. What workload has this as being a bottleneck?

And remember, in modern systems with retbleed mitigations enabled, there are no 'ret' calls in the kernel at all!

thanks,

greg k-h

author maintainer
#5 lore raw

PATCH 2/2 lib/bitmap: split-out string-related operations to a separate files

In reply to Yury Norov

lib/bitmap.c and corresponding include/linux/bitmap.h are intended to hold functions related to operations on bitmaps, like bitmap_shift or bitmap_set. Historically, some string-related operations like bitmap_parse are also reside in lib/bitmap.c.

Now that the subsystem evolves, string-related bitmap operations became a significant part of the file. Because they are quite different from the other bitmap functions by nature, it's worth to split them to a separate source/header files.

---
 MAINTAINERS                |   2 +
 include/linux/bitmap-str.h |  16 ++
 include/linux/bitmap.h     |  17 +-
 lib/Makefile               |   2 +-
 lib/bitmap-str.c           | 510 ++++++++++++++++++++++++++++++++++++
 lib/bitmap.c               | 512 -------------------------------------
 6 files changed, 530 insertions(+), 529 deletions(-)
 create mode 100644 include/linux/bitmap-str.h
 create mode 100644 lib/bitmap-str.c
MAINTAINERS +2 -0
index 81d5fc0bba68..d507dd2582f2 100644
@@ -3506,12 +3506,14 @@ R:	Andy Shevchenko <[email protected]>
 R:	Rasmus Villemoes <[email protected]>
 S:	Maintained
 F:	include/linux/bitfield.h
+F:	include/linux/bitmap-str.h
 F:	include/linux/bitmap.h
 F:	include/linux/bits.h
 F:	include/linux/cpumask.h
 F:	include/linux/find.h
 F:	include/linux/nodemask.h
 F:	include/vdso/bits.h
+F:	lib/bitmap-str.c
 F:	lib/bitmap.c
 F:	lib/cpumask.c
 F:	lib/cpumask_kunit.c
include/linux/bitmap-str.h new file +16 -0
new file mode 100644
index 000000000000..17caeca94cab
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_BITMAP_STR_H
+#define __LINUX_BITMAP_STR_H
+
+int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, unsigned long *dst, int nbits);
+int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, int nmaskbits);
+extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
+					int nmaskbits, loff_t off, size_t count);
+extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
+					int nmaskbits, loff_t off, size_t count);
+int bitmap_parse(const char *buf, unsigned int buflen, unsigned long *dst, int nbits);
+int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits);
+int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
+					unsigned long *dst, int nbits);
+
+#endif /* __LINUX_BITMAP_STR_H */
include/linux/bitmap.h +1 -16
index ce8fcd8736f1..67d47e35d285 100644
@@ -10,6 +10,7 @@
 #include <linux/limits.h>
 #include <linux/string.h>
 #include <linux/types.h>
+#include <linux/bitmap-str.h>
 
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
@@ -192,14 +193,6 @@ bitmap_find_next_zero_area(unsigned long *map,
 					      align_mask, 0);
 }
 
-int bitmap_parse(const char *buf, unsigned int buflen,
-			unsigned long *dst, int nbits);
-int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
-			unsigned long *dst, int nbits);
-int bitmap_parselist(const char *buf, unsigned long *maskp,
-			int nmaskbits);
-int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
-			unsigned long *dst, int nbits);
 void bitmap_remap(unsigned long *dst, const unsigned long *src,
 		const unsigned long *old, const unsigned long *new, unsigned int nbits);
 int bitmap_bitremap(int oldbit,
@@ -217,14 +210,6 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n
 #else
 #define bitmap_copy_le bitmap_copy
 #endif
-int bitmap_print_to_pagebuf(bool list, char *buf,
-				   const unsigned long *maskp, int nmaskbits);
-
-extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
-				      int nmaskbits, loff_t off, size_t count);
-
-extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
-				      int nmaskbits, loff_t off, size_t count);
 
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
lib/Makefile +1 -1
index 740109b6e2c8..9e8f9f6dd3b2 100644
@@ -48,7 +48,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bsearch.o find_bit.o llist.o memweight.o kfifo.o \
 	 percpu-refcount.o rhashtable.o base64.o \
 	 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
-	 generic-radix-tree.o
+	 generic-radix-tree.o bitmap-str.o
 obj-$(CONFIG_STRING_SELFTEST) += test_string.o
 obj-y += string_helpers.o
 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
lib/bitmap-str.c new file +510 -0

510 lines not on this page · raw message

lib/bitmap.c +0 -512

528 lines not on this page · raw message

Signature · 1 line
2.39.2
author maintainer
#6 lore raw
In reply to Yury Norov
Quoted 4 lines · Yury Norov

The file is intended to hold functions to operate on bit arrays, but this days, more than 1/3 of bitmap.c is helpers for bitmap-to-string converters, plus some wrappers for device.h.

Quoted 8 lines

So move those out of lib/bitmap.c in sake of readability and maintainability.

Functionally, this series is a no-op.

Yury Norov (2):
lib/bitmap: move bitmap allocators for device to linux/device.h lib/bitmap: split-out string-related operations to a separate files

OK, then if no objections, I drop #1 as a controversial, and pull #2 into bitmap-for-next.

#7 lore raw
Quoted 8 lines · Yury Norov
Quoted 7 lines · Yury Norov

The file is intended to hold functions to operate on bit arrays, but this days, more than 1/3 of bitmap.c is helpers for bitmap-to-string converters, plus some wrappers for device.h.

So move those out of lib/bitmap.c in sake of readability and maintainability.

Quoted 8 lines
Quoted 5 lines

Functionally, this series is a no-op.

Yury Norov (2):
lib/bitmap: move bitmap allocators for device to linux/device.h lib/bitmap: split-out string-related operations to a separate files

OK, then if no objections, I drop #1 as a controversial, and pull #2 into bitmap-for-next.

That's fine with me, thanks.

greg k-h