]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - lib/bitmap.c
bitmap: Add bitmap_alloc(), bitmap_zalloc() and bitmap_free()
[mirror_ubuntu-bionic-kernel.git] / lib / bitmap.c
index d8f0c094b18eba130571a3e405796040750d9646..f9495f29d900c78d9b1706865758b98f9008d031 100644 (file)
@@ -13,6 +13,8 @@
 #include <linux/bitops.h>
 #include <linux/bug.h>
 #include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
 
@@ -468,14 +470,15 @@ EXPORT_SYMBOL(bitmap_parse_user);
  * ranges if list is specified or hex digits grouped into comma-separated
  * sets of 8 digits/set. Returns the number of characters written to buf.
  *
- * It is assumed that @buf is a pointer into a PAGE_SIZE area and that
- * sufficient storage remains at @buf to accommodate the
- * bitmap_print_to_pagebuf() output.
+ * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
+ * area and that sufficient storage remains at @buf to accommodate the
+ * bitmap_print_to_pagebuf() output. Returns the number of characters
+ * actually printed to @buf, excluding terminating '\0'.
  */
 int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
                            int nmaskbits)
 {
-       ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
+       ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
        int n = 0;
 
        if (len > 1)
@@ -607,7 +610,7 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen,
                /* if no digit is after '-', it's wrong*/
                if (at_start && in_range)
                        return -EINVAL;
-               if (!(a <= b) || !(used_size <= group_size))
+               if (!(a <= b) || group_size == 0 || !(used_size <= group_size))
                        return -EINVAL;
                if (b >= nmaskbits)
                        return -ERANGE;
@@ -1214,3 +1217,22 @@ void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int n
 }
 EXPORT_SYMBOL(bitmap_copy_le);
 #endif
+
+unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
+{
+       return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
+                            flags);
+}
+EXPORT_SYMBOL(bitmap_alloc);
+
+unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
+{
+       return bitmap_alloc(nbits, flags | __GFP_ZERO);
+}
+EXPORT_SYMBOL(bitmap_zalloc);
+
+void bitmap_free(const unsigned long *bitmap)
+{
+       kfree(bitmap);
+}
+EXPORT_SYMBOL(bitmap_free);