Skip to contentSkip to conversation

lib: simplify bitmap_from_u32array API

RFC PATCH In review Yury Norov 2 participants 4 messages Nov 15, 2017 – Nov 21, 2017

Subsystems (6)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

RFC PATCH 1/1 lib: simplify bitmap_from_u32array API

and make it looking like standard copy function:
bitmap_from_u32array(dst, src, size).

Currently bitmap_from_u32array() takes 4 arguments:
- unsigned long *bitmap, which is destination;
- unsigned int nbits, the length of destination bitmap, in bits;
- const u32 *buf, the source; and
- unsigned int nwords, the length of source buffer in ints.

In description to the function it is detailed like:
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
* bits between nword and nbits in @bitmap (if any) are cleared.

Having two size arguments looks unneeded and potentially dangerous.

It is unneeded because normally user of copy-like function should take care of the size of destination and make it big enough to fit source data.

And it is dangerous because function may hide possible error if user doesn't provide big enough bitmap, and data becomes silently dropped.

That's why all copy-like functions have 1 argument for size of copying data, and I don't see any reason to make bitmap_from_u32array() different.

One exception that comes in mind is strncpy() which also provides size of destination in arguments, but it's strongly argued by the possibility of taking broken strings in source, which is not the case of bitmap_from_u32array().

There is no many real users of bitmap_from_u32array(), and they all very clearly provide size of destination matched with the size of source, so additional functionality is not used. Like this:

        bitmap_from_u32array(to->link_modes.supported,
                            __ETHTOOL_LINK_MODE_MASK_NBITS,
                             link_usettings.link_modes.supported,
			     __ETHTOOL_LINK_MODE_MASK_NU32);
Where:
#define __ETHTOOL_LINK_MODE_MASK_NU32 \
        DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)

This patch demonstrates how the code will look with simplified API. If this simplification will be found useful, I'll send complete version that also reworks bitmap_to_u32array() and fixes corresponding test in lib/test_bitmap.c. I also expect that internal logic of functions will be simplified.

Boot-tested on arm64.

CC Andrew Morton <[email protected]>
CC Ben Hutchings <[email protected]>
CC David Decotigny <[email protected]>
CC David S. Miller <[email protected]>
CC Matthew Wilcox <[email protected]>
CC Rasmus Villemoes <[email protected]>
---
 arch/arm64/kernel/perf_event.c |  5 ++---
 include/linux/bitmap.h         |  7 +++----
 lib/bitmap.c                   | 19 +++++++------------
 net/core/ethtool.c             | 21 +++++++--------------
 4 files changed, 19 insertions(+), 33 deletions(-)
arch/arm64/kernel/perf_event.c +2 -3
index 9eaef51f83ff..70f4fd1952b6 100644
@@ -931,9 +931,8 @@ static void __armv8pmu_probe_pmu(void *info)
 	pmceid[0] = read_sysreg(pmceid0_el0);
 	pmceid[1] = read_sysreg(pmceid1_el0);
 
-	bitmap_from_u32array(cpu_pmu->pmceid_bitmap,
-			     ARMV8_PMUV3_MAX_COMMON_EVENTS, pmceid,
-			     ARRAY_SIZE(pmceid));
+	bitmap_from_u32array(cpu_pmu->pmceid_bitmap, pmceid,
+			     ARMV8_PMUV3_MAX_COMMON_EVENTS);
 }
 
 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
include/linux/bitmap.h +3 -4
index 19748a5b0e77..b3c4bc04a466 100644
@@ -60,7 +60,7 @@
  * 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_from_u32array(dst, buf, nbits)	*dst = *buf (nwords 32b words)
  * bitmap_to_u32array(buf, nwords, src, nbits)	*buf = *dst (nwords 32b words)
  */
 
@@ -165,10 +165,9 @@ 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 unsigned int bitmap_from_u32array(unsigned long *bitmap,
-					 unsigned int nbits,
+extern void *bitmap_from_u32array(unsigned long *bitmap,
 					 const u32 *buf,
-					 unsigned int nwords);
+					 unsigned int nbits);
 extern unsigned int bitmap_to_u32array(u32 *buf,
 				       unsigned int nwords,
 				       const unsigned long *bitmap,
lib/bitmap.c +7 -12
index c82c61b66e16..9d99f2f81281 100644
@@ -1108,22 +1108,17 @@ EXPORT_SYMBOL(bitmap_allocate_region);
  *	@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.
  *
- * Return the number of bits effectively copied.
+ * copy nbits bits from @buf to @bitmap. In last word of @bitmap, the bits
+ * beyond nbits (if any) are kept unchanged.
  */
-unsigned int
-bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
-		     const u32 *buf, unsigned int nwords)
+void *bitmap_from_u32array(unsigned long *bitmap, const u32 *buf,
+						unsigned int nbits)
 {
 	unsigned int dst_idx, src_idx;
+	unsigned int nwords = BITS_TO_LONGS(nbits);
 
-	for (src_idx = dst_idx = 0; dst_idx < BITS_TO_LONGS(nbits); ++dst_idx) {
+	for (src_idx = dst_idx = 0; dst_idx < nwords; ++dst_idx) {
 		unsigned long part = 0;
 
 		if (src_idx < nwords)
@@ -1144,7 +1139,7 @@ bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
 		}
 	}
 
-	return min_t(unsigned int, nbits, 32*nwords);
+	return bitmap;
 }
 EXPORT_SYMBOL(bitmap_from_u32array);
 
net/core/ethtool.c +7 -14
index 9a9a3d77e327..6ee127bb04e5 100644
@@ -600,17 +600,14 @@ static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
 
 	memcpy(&to->base, &link_usettings.base, sizeof(to->base));
 	bitmap_from_u32array(to->link_modes.supported,
-			     __ETHTOOL_LINK_MODE_MASK_NBITS,
 			     link_usettings.link_modes.supported,
-			     __ETHTOOL_LINK_MODE_MASK_NU32);
+			     __ETHTOOL_LINK_MODE_MASK_NBITS);
 	bitmap_from_u32array(to->link_modes.advertising,
-			     __ETHTOOL_LINK_MODE_MASK_NBITS,
 			     link_usettings.link_modes.advertising,
-			     __ETHTOOL_LINK_MODE_MASK_NU32);
+			     __ETHTOOL_LINK_MODE_MASK_NBITS);
 	bitmap_from_u32array(to->link_modes.lp_advertising,
-			     __ETHTOOL_LINK_MODE_MASK_NBITS,
 			     link_usettings.link_modes.lp_advertising,
-			     __ETHTOOL_LINK_MODE_MASK_NU32);
+			     __ETHTOOL_LINK_MODE_MASK_NBITS);
 
 	return 0;
 }
@@ -2343,10 +2340,8 @@ static int ethtool_get_per_queue_coalesce(struct net_device *dev,
 
 	useraddr += sizeof(*per_queue_opt);
 
-	bitmap_from_u32array(queue_mask,
-			     MAX_NUM_QUEUE,
-			     per_queue_opt->queue_mask,
-			     DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
+	bitmap_from_u32array(queue_mask, per_queue_opt->queue_mask,
+			     MAX_NUM_QUEUE);
 
 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
 		struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
@@ -2378,10 +2373,8 @@ static int ethtool_set_per_queue_coalesce(struct net_device *dev,
 
 	useraddr += sizeof(*per_queue_opt);
 
-	bitmap_from_u32array(queue_mask,
-			     MAX_NUM_QUEUE,
-			     per_queue_opt->queue_mask,
-			     DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
+	bitmap_from_u32array(queue_mask, per_queue_opt->queue_mask,
+			     MAX_NUM_QUEUE);
 	n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
 	tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
 	if (!backup)
Signature · 1 line
2.11.0
#1 lore raw

I certainly approve. The name sucks too 😉

Review line 65

Quoted 8 lines
(quoted diff) +1 -1
@@ -60,7 +60,7 @@
  * 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_from_u32array(dst, buf, nbits)	*dst = *buf (nwords 32b
 words)

I think this should read:
+ * bitmap_from_u32array(dst, buf, bits) Copy 'bits' from buf to dst

Also, on LE systems, shouldn't we just use memcpy() for the first bits/8 bytes?

author
#2 lore raw

Hi Matthew,

[+ Geert Uytterhoeven)

Quoted 1 line · Matthew Wilcox

I certainly approve. The name sucks too 😉

Yep. I changed it, didn't resist.

Quoted 11 lines

Review line 65

Quoted 8 lines
(quoted diff) +1 -1
@@ -60,7 +60,7 @@
  * 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_from_u32array(dst, buf, nbits)	*dst = *buf (nwords 32b
 words)

I think this should read:
+ * bitmap_from_u32array(dst, buf, bits) Copy 'bits' from buf to dst

Ack

Quoted 1 line

Also, on LE systems, shouldn't we just use memcpy() for the first bits/8 bytes?

Yes, but I did prefer to keep logic common for LE and BE arches. We can switch to memcpy 32-bit LE and BE versions, see my thoughts below.

Quoted 1 line

From 6ab70ff5ea32ecc5c9e77cc4b84e6dd3843e5d8f Mon Sep 17 00:00:00 2001

From: Yury Norov <[email protected]>
Date: Tue, 21 Nov 2017 22:42:57 +0300
Subject: [PATCH 1/2] bitmap: new bitmap_copy_safe and bitmap_{from,to}_arr32

'Safe' in bitmap_copy_safe stands for clearing tail bits in bitmap beyond last bit till the end of last word. It is useful for hardening API when bitmap is assumed to be exposed to userspace.

bitmap_{from,to}_arr32 functions are replacements for bitmap_{from,to}_u32array. They don't take unneeded nwords argument, and so simpler in implementation and understanding.

This patch suggests optimization for 32-bit systems - aliasing bitmap_{from,to}_arr32 to bitmap_copy_safe. Other possible optimization is to map 64-bit LE bitmap_{from,to}_arr32 to more generic function(s).

But I didn't end up with the function that would be helpful by itself, and can be used to alias 64-bit LE bitmap_{from,to}_arr32, like bitmap_copy_safe does. So I preferred to leave things as is.

Since this is RFC, I would like to ask people here for ideas. :)

The following patch switches kernel to new API and introduces new test.

---
 include/linux/bitmap.h | 31 +++++++++++++++++++++++++++++
 lib/bitmap.c           | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
include/linux/bitmap.h +31 -0
index 19748a5b0e77..1fa0de16b0a7 100644
@@ -62,6 +62,8 @@
  * 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)
+ * bitmap_from_arr32(dst, buf, nbits)		Copy 'nbits' from u32 buf to dst
+ * bitmap_to_arr32(buf, src, nbits)		Copy 'nbits' from buf to u32 dst
  */
 
 /*
@@ -219,6 +221,35 @@ static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
 	}
 }
 
+/*
+ * Copy bitmap and clear tail bits in last word.
+ */
+static inline void bitmap_copy_safe(unsigned long *dst,
+		const unsigned long *src, unsigned int nbits)
+{
+	bitmap_copy(dst, src, nbits);
+	if (nbits % BITS_PER_LONG)
+		dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
+}
+
+/*
+ * On 32-bit systems bitmaps are represented as u32 arrays internally, and
+ * therefore conversion is not needed when copying data from/to arrays of u32.
+ */
+#if BITS_PER_LONG == 64
+extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
+							unsigned int nbits);
+extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
+							unsigned int nbits);
+#else
+#define bitmap_from_arr32(bitmap, buf, nbits)		\
+	bitmap_copy_safe((unsigned long *) (bitmap),	\
+			(const unsigned long *) (buf), (nbits))
+#define bitmap_to_arr32(buf, bitmap, nbits)		\
+	bitmap_copy_safe((unsigned long *) (buf),	\
+			(const unsigned long *) (bitmap), (nbits))
+#endif
+
 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
 			const unsigned long *src2, unsigned int nbits)
 {
lib/bitmap.c +54 -0

57 lines not on this page · raw message

Signature · 1 line
2.11.0
author
#3 lore raw
Quoted 1 line

From 1a922d271d2ad05265871bbbd68d0414fa5ae49e Mon Sep 17 00:00:00 2001

From: Yury Norov <[email protected]>
Date: Tue, 21 Nov 2017 23:22:15 +0300
Subject: [PATCH 2/2] bitmap: replace bitmap_{from,to}_u32array

with bitmap_{from,to}_arr32 over the kernel. Additionally to it:
* __check_eq_bitmap() now takes single nbits argument. Is it even possible to compare bitmaps with different length?
* __check_eq_u32_array is not used in new test but may be used in future. So I don't remove it here, but annotate as __used.

---
 arch/arm64/kernel/perf_event.c |   5 +-
 include/linux/bitmap.h         |  11 +--
 lib/bitmap.c                   |  87 -----------------
 lib/test_bitmap.c              | 206 +++++++----------------------------------
 net/core/ethtool.c             |  53 +++++------
 5 files changed, 55 insertions(+), 307 deletions(-)
arch/arm64/kernel/perf_event.c +2 -3
index 9eaef51f83ff..b39622b4d25f 100644
@@ -931,9 +931,8 @@ static void __armv8pmu_probe_pmu(void *info)
 	pmceid[0] = read_sysreg(pmceid0_el0);
 	pmceid[1] = read_sysreg(pmceid1_el0);
 
-	bitmap_from_u32array(cpu_pmu->pmceid_bitmap,
-			     ARMV8_PMUV3_MAX_COMMON_EVENTS, pmceid,
-			     ARRAY_SIZE(pmceid));
+	bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
+			     pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
 }
 
 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
include/linux/bitmap.h +1 -10
index 1fa0de16b0a7..8611c386b997 100644
@@ -60,8 +60,6 @@
  * 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)
  * bitmap_from_arr32(dst, buf, nbits)		Copy 'nbits' from u32 buf to dst
  * bitmap_to_arr32(buf, src, nbits)		Copy 'nbits' from buf to u32 dst
  */
@@ -167,14 +165,7 @@ 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 unsigned int bitmap_from_u32array(unsigned long *bitmap,
-					 unsigned int nbits,
-					 const u32 *buf,
-					 unsigned int nwords);
-extern unsigned int 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
lib/bitmap.c +0 -87

93 lines not on this page · raw message

lib/test_bitmap.c +31 -175

241 lines not on this page · raw message

net/core/ethtool.c +21 -32

77 lines not on this page · raw message

Signature · 1 line
2.11.0