diff mbox series

[RFC,1/7] kernel: backport devm_bitmap_{alloc,zalloc}

Message ID 3594a12991ee4fc6d9b9cec25b1acc83e30a624c.1657998467.git.sander@svanheule.net
State Superseded
Delegated to: Sander Vanheule
Headers show
Series realtek: MFD for switch core | expand

Commit Message

Sander Vanheule July 16, 2022, 7:09 p.m. UTC
devm_* helpers are useful for driver resource management. Backport these
helpers for bitmap allocations from v5.13.

Signed-off-by: Sander Vanheule <sander@svanheule.net>
---
 ...de-devm_bitmap_alloc-and-devm_bitmap.patch | 88 +++++++++++++++++++
 1 file changed, 88 insertions(+)
 create mode 100644 target/linux/generic/backport-5.10/201-lib-bitmap-provide-devm_bitmap_alloc-and-devm_bitmap.patch
diff mbox series

Patch

diff --git a/target/linux/generic/backport-5.10/201-lib-bitmap-provide-devm_bitmap_alloc-and-devm_bitmap.patch b/target/linux/generic/backport-5.10/201-lib-bitmap-provide-devm_bitmap_alloc-and-devm_bitmap.patch
new file mode 100644
index 000000000000..515ee68749c4
--- /dev/null
+++ b/target/linux/generic/backport-5.10/201-lib-bitmap-provide-devm_bitmap_alloc-and-devm_bitmap.patch
@@ -0,0 +1,88 @@ 
+From e829c2e4744850bab4d8f8ffebd00df10b4c6c2b Mon Sep 17 00:00:00 2001
+From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
+Date: Mon, 15 Mar 2021 10:13:56 +0100
+Subject: [PATCH] lib: bitmap: provide devm_bitmap_alloc() and
+ devm_bitmap_zalloc()
+
+Provide managed variants of bitmap_alloc() and bitmap_zalloc().
+
+Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
+Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
+---
+ include/linux/bitmap.h |  8 ++++++++
+ lib/bitmap.c           | 33 +++++++++++++++++++++++++++++++++
+ 2 files changed, 41 insertions(+)
+
+--- a/include/linux/bitmap.h
++++ b/include/linux/bitmap.h
+@@ -9,6 +9,8 @@
+ #include <linux/string.h>
+ #include <linux/kernel.h>
+ 
++struct device;
++
+ /*
+  * bitmaps provide bit arrays that consume one or more unsigned
+  * longs.  The bitmap interface and available operations are listed
+@@ -122,6 +124,12 @@ extern unsigned long *bitmap_alloc(unsig
+ extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
+ extern 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:
+  */
+--- a/lib/bitmap.c
++++ b/lib/bitmap.c
+@@ -6,6 +6,7 @@
+ #include <linux/export.h>
+ #include <linux/thread_info.h>
+ #include <linux/ctype.h>
++#include <linux/device.h>
+ #include <linux/errno.h>
+ #include <linux/bitmap.h>
+ #include <linux/bitops.h>
+@@ -1262,6 +1263,38 @@ void bitmap_free(const unsigned long *bi
+ }
+ 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