diff mbox

[net-next,v7,01/19] lib/bitmap.c: conversion routines to/from u32 array

Message ID 1454893743-6285-2-git-send-email-ddecotig@gmail.com
State Superseded, archived
Delegated to: David Miller
Headers show

Commit Message

david decotigny Feb. 8, 2016, 1:08 a.m. UTC
From: David Decotigny <decot@googlers.com>

Aimed at transferring bitmaps to/from user-space in a 32/64-bit agnostic
way.

Tested:
  unit tests (next patch) on qemu i386, x86_64, ppc, ppc64 BE and LE,
  ARM.

Signed-off-by: David Decotigny <decot@googlers.com>
---
 include/linux/bitmap.h |  6 ++++
 lib/bitmap.c           | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)

Comments

Andrew Morton Feb. 8, 2016, 11:36 p.m. UTC | #1
On Sun,  7 Feb 2016 17:08:45 -0800 David Decotigny <ddecotig@gmail.com> wrote:

> From: David Decotigny <decot@googlers.com>
> 
> Aimed at transferring bitmaps to/from user-space in a 32/64-bit agnostic
> way.
> 
> Tested:
>   unit tests (next patch) on qemu i386, x86_64, ppc, ppc64 BE and LE,
>   ARM.
> 
> @@ -1060,6 +1062,90 @@ int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
>  EXPORT_SYMBOL(bitmap_allocate_region);
>  
>  /**
> + * bitmap_from_u32array - copy the contents of a u32 array of bits to bitmap
> + *	@bitmap: array of unsigned longs, the destination bitmap, non NULL
> + *	@nbits: number of bits in @bitmap
> + *	@buf: array of u32 (in host byte order), the source bitmap, non NULL
> + *	@nwords: number of u32 words in @buf
> + *
> + * copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
> + * bits between nword and nbits in @bitmap (if any) are cleared. In
> + * last word of @bitmap, the bits beyond nbits (if any) are kept
> + * unchanged.
> + */

This will leave the caller not knowing how many valid bits are actually
present in the resulting bitmap.  To determine that, the caller will
need to perform (duplicated) math on `nbits' and `nwords'.

> +void bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
> +			  const u32 *buf, unsigned int nwords)

So how about we make this return the number of valid bits in *bitmap?

> +/**
> + * bitmap_to_u32array - copy the contents of bitmap to a u32 array of bits
> + *	@buf: array of u32 (in host byte order), the dest bitmap, non NULL
> + *	@nwords: number of u32 words in @buf
> + *	@bitmap: array of unsigned longs, the source bitmap, non NULL
> + *	@nbits: number of bits in @bitmap
> + *
> + * copy min(nbits, 32*nwords) bits from @bitmap to @buf. Remaining
> + * bits after nbits in @buf (if any) are cleared.
> + */
> +void bitmap_to_u32array(u32 *buf, unsigned int nwords,
> +			const unsigned long *bitmap, unsigned int nbits)

Ditto.
david decotigny Feb. 10, 2016, 12:30 a.m. UTC | #2
thanks. done in v8 I am sending right now.

On Mon, Feb 8, 2016 at 3:36 PM, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Sun,  7 Feb 2016 17:08:45 -0800 David Decotigny <ddecotig@gmail.com> wrote:
>
>> From: David Decotigny <decot@googlers.com>
>>
>> Aimed at transferring bitmaps to/from user-space in a 32/64-bit agnostic
>> way.
>>
>> Tested:
>>   unit tests (next patch) on qemu i386, x86_64, ppc, ppc64 BE and LE,
>>   ARM.
>>
>> @@ -1060,6 +1062,90 @@ int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
>>  EXPORT_SYMBOL(bitmap_allocate_region);
>>
>>  /**
>> + * bitmap_from_u32array - copy the contents of a u32 array of bits to bitmap
>> + *   @bitmap: array of unsigned longs, the destination bitmap, non NULL
>> + *   @nbits: number of bits in @bitmap
>> + *   @buf: array of u32 (in host byte order), the source bitmap, non NULL
>> + *   @nwords: number of u32 words in @buf
>> + *
>> + * copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
>> + * bits between nword and nbits in @bitmap (if any) are cleared. In
>> + * last word of @bitmap, the bits beyond nbits (if any) are kept
>> + * unchanged.
>> + */
>
> This will leave the caller not knowing how many valid bits are actually
> present in the resulting bitmap.  To determine that, the caller will
> need to perform (duplicated) math on `nbits' and `nwords'.
>
>> +void bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
>> +                       const u32 *buf, unsigned int nwords)
>
> So how about we make this return the number of valid bits in *bitmap?
>
>> +/**
>> + * bitmap_to_u32array - copy the contents of bitmap to a u32 array of bits
>> + *   @buf: array of u32 (in host byte order), the dest bitmap, non NULL
>> + *   @nwords: number of u32 words in @buf
>> + *   @bitmap: array of unsigned longs, the source bitmap, non NULL
>> + *   @nbits: number of bits in @bitmap
>> + *
>> + * copy min(nbits, 32*nwords) bits from @bitmap to @buf. Remaining
>> + * bits after nbits in @buf (if any) are cleared.
>> + */
>> +void bitmap_to_u32array(u32 *buf, unsigned int nwords,
>> +                     const unsigned long *bitmap, unsigned int nbits)
>
> Ditto.
>
>
diff mbox

Patch

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 9653fdb..f7dc158 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -59,6 +59,8 @@ 
  * bitmap_find_free_region(bitmap, bits, order)	Find and allocate bit region
  * bitmap_release_region(bitmap, pos, order)	Free specified bit region
  * bitmap_allocate_region(bitmap, pos, order)	Allocate specified bit region
+ * bitmap_from_u32array(dst, nbits, buf, nwords) *dst = *buf (nwords 32b words)
+ * bitmap_to_u32array(buf, nwords, src, nbits)	*buf = *dst (nwords 32b words)
  */
 
 /*
@@ -163,6 +165,10 @@  extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
 extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
 extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
+extern void bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
+				 const u32 *buf, unsigned int nwords);
+extern void bitmap_to_u32array(u32 *buf, unsigned int nwords,
+			       const unsigned long *bitmap, unsigned int nbits);
 #ifdef __BIG_ENDIAN
 extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
 #else
diff --git a/lib/bitmap.c b/lib/bitmap.c
index 8148143..e1cc648 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -12,6 +12,8 @@ 
 #include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/bug.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
 
 #include <asm/page.h>
 #include <asm/uaccess.h>
@@ -1060,6 +1062,90 @@  int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
 EXPORT_SYMBOL(bitmap_allocate_region);
 
 /**
+ * bitmap_from_u32array - copy the contents of a u32 array of bits to bitmap
+ *	@bitmap: array of unsigned longs, the destination bitmap, non NULL
+ *	@nbits: number of bits in @bitmap
+ *	@buf: array of u32 (in host byte order), the source bitmap, non NULL
+ *	@nwords: number of u32 words in @buf
+ *
+ * copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
+ * bits between nword and nbits in @bitmap (if any) are cleared. In
+ * last word of @bitmap, the bits beyond nbits (if any) are kept
+ * unchanged.
+ */
+void bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
+			  const u32 *buf, unsigned int nwords)
+{
+	unsigned int k;
+	const u32 *src = buf;
+
+	for (k = 0; k < BITS_TO_LONGS(nbits); ++k) {
+		unsigned long part = 0;
+
+		if (nwords) {
+			part = *src++;
+			nwords--;
+		}
+
+#if BITS_PER_LONG == 64
+		if (nwords) {
+			part |= ((unsigned long) *src++) << 32;
+			nwords--;
+		}
+#endif
+
+		if (k < nbits/BITS_PER_LONG)
+			bitmap[k] = part;
+		else {
+			unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
+
+			bitmap[k] = (bitmap[k] & ~mask) | (part & mask);
+		}
+	}
+}
+EXPORT_SYMBOL(bitmap_from_u32array);
+
+/**
+ * bitmap_to_u32array - copy the contents of bitmap to a u32 array of bits
+ *	@buf: array of u32 (in host byte order), the dest bitmap, non NULL
+ *	@nwords: number of u32 words in @buf
+ *	@bitmap: array of unsigned longs, the source bitmap, non NULL
+ *	@nbits: number of bits in @bitmap
+ *
+ * copy min(nbits, 32*nwords) bits from @bitmap to @buf. Remaining
+ * bits after nbits in @buf (if any) are cleared.
+ */
+void bitmap_to_u32array(u32 *buf, unsigned int nwords,
+			const unsigned long *bitmap, unsigned int nbits)
+{
+	unsigned int k = 0;
+	u32 *dst = buf;
+
+	while (nwords) {
+		unsigned long part = 0;
+
+		if (k < BITS_TO_LONGS(nbits)) {
+			part = bitmap[k];
+			if (k >= nbits/BITS_PER_LONG)
+				part &= BITMAP_LAST_WORD_MASK(nbits);
+			k++;
+		}
+
+		*dst++ = part & 0xffffffffUL;
+		nwords--;
+
+#if BITS_PER_LONG == 64
+		if (nwords) {
+			part >>= 32;
+			*dst++ = part & 0xffffffffUL;
+			nwords--;
+		}
+#endif
+	}
+}
+EXPORT_SYMBOL(bitmap_to_u32array);
+
+/**
  * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
  * @dst:   destination buffer
  * @src:   bitmap to copy