Skip to content

Commit 3aa5688

Browse files
norovtorvalds
authored andcommitted
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. * __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. Tested on arm64 and 32-bit BE mips. [arnd@arndb.de: perf: arm_dsu_pmu: convert to bitmap_from_arr32] Link: http://lkml.kernel.org/r/20180201172508.5739-2-ynorov@caviumnetworks.com [ynorov@caviumnetworks.com: fix net/core/ethtool.c] Link: http://lkml.kernel.org/r/20180205071747.4ekxtsbgxkj5b2fz@yury-thinkpad Link: http://lkml.kernel.org/r/20171228150019.27953-2-ynorov@caviumnetworks.com Signed-off-by: Yury Norov <ynorov@caviumnetworks.com> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Cc: Ben Hutchings <ben@decadent.org.uk> Cc: David Decotigny <decot@googlers.com>, Cc: David S. Miller <davem@davemloft.net>, Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Matthew Wilcox <mawilcox@microsoft.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: Heiner Kallweit <hkallweit1@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c724f19 commit 3aa5688

6 files changed

Lines changed: 57 additions & 311 deletions

File tree

‎arch/arm64/kernel/perf_event.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,8 @@ static void __armv8pmu_probe_pmu(void *info)
925925
pmceid[0] = read_sysreg(pmceid0_el0);
926926
pmceid[1] = read_sysreg(pmceid1_el0);
927927

928-
bitmap_from_u32array(cpu_pmu->pmceid_bitmap,
929-
ARMV8_PMUV3_MAX_COMMON_EVENTS, pmceid,
930-
ARRAY_SIZE(pmceid));
928+
bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
929+
pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
931930
}
932931

933932
static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)

‎drivers/perf/arm_dsu_pmu.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,8 @@ static void dsu_pmu_probe_pmu(struct dsu_pmu *dsu_pmu)
658658
return;
659659
cpmceid[0] = __dsu_pmu_read_pmceid(0);
660660
cpmceid[1] = __dsu_pmu_read_pmceid(1);
661-
bitmap_from_u32array(dsu_pmu->cpmceid_bitmap,
662-
DSU_PMU_MAX_COMMON_EVENTS,
663-
cpmceid,
664-
ARRAY_SIZE(cpmceid));
661+
bitmap_from_arr32(dsu_pmu->cpmceid_bitmap, cpmceid,
662+
DSU_PMU_MAX_COMMON_EVENTS);
665663
}
666664

667665
static void dsu_pmu_set_active_cpu(int cpu, struct dsu_pmu *dsu_pmu)

‎include/linux/bitmap.h‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
* bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
6565
* bitmap_release_region(bitmap, pos, order) Free specified bit region
6666
* bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
67-
* bitmap_from_u32array(dst, nbits, buf, nwords) *dst = *buf (nwords 32b words)
68-
* bitmap_to_u32array(buf, nwords, src, nbits) *buf = *dst (nwords 32b words)
6967
* bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
7068
* bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
7169
*
@@ -176,14 +174,7 @@ extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
176174
extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
177175
extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
178176
extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
179-
extern unsigned int bitmap_from_u32array(unsigned long *bitmap,
180-
unsigned int nbits,
181-
const u32 *buf,
182-
unsigned int nwords);
183-
extern unsigned int bitmap_to_u32array(u32 *buf,
184-
unsigned int nwords,
185-
const unsigned long *bitmap,
186-
unsigned int nbits);
177+
187178
#ifdef __BIG_ENDIAN
188179
extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
189180
#else

‎lib/bitmap.c‎

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,93 +1105,6 @@ int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
11051105
}
11061106
EXPORT_SYMBOL(bitmap_allocate_region);
11071107

1108-
/**
1109-
* bitmap_from_u32array - copy the contents of a u32 array of bits to bitmap
1110-
* @bitmap: array of unsigned longs, the destination bitmap, non NULL
1111-
* @nbits: number of bits in @bitmap
1112-
* @buf: array of u32 (in host byte order), the source bitmap, non NULL
1113-
* @nwords: number of u32 words in @buf
1114-
*
1115-
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
1116-
* bits between nword and nbits in @bitmap (if any) are cleared. In
1117-
* last word of @bitmap, the bits beyond nbits (if any) are kept
1118-
* unchanged.
1119-
*
1120-
* Return the number of bits effectively copied.
1121-
*/
1122-
unsigned int
1123-
bitmap_from_u32array(unsigned long *bitmap, unsigned int nbits,
1124-
const u32 *buf, unsigned int nwords)
1125-
{
1126-
unsigned int dst_idx, src_idx;
1127-
1128-
for (src_idx = dst_idx = 0; dst_idx < BITS_TO_LONGS(nbits); ++dst_idx) {
1129-
unsigned long part = 0;
1130-
1131-
if (src_idx < nwords)
1132-
part = buf[src_idx++];
1133-
1134-
#if BITS_PER_LONG == 64
1135-
if (src_idx < nwords)
1136-
part |= ((unsigned long) buf[src_idx++]) << 32;
1137-
#endif
1138-
1139-
if (dst_idx < nbits/BITS_PER_LONG)
1140-
bitmap[dst_idx] = part;
1141-
else {
1142-
unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
1143-
1144-
bitmap[dst_idx] = (bitmap[dst_idx] & ~mask)
1145-
| (part & mask);
1146-
}
1147-
}
1148-
1149-
return min_t(unsigned int, nbits, 32*nwords);
1150-
}
1151-
EXPORT_SYMBOL(bitmap_from_u32array);
1152-
1153-
/**
1154-
* bitmap_to_u32array - copy the contents of bitmap to a u32 array of bits
1155-
* @buf: array of u32 (in host byte order), the dest bitmap, non NULL
1156-
* @nwords: number of u32 words in @buf
1157-
* @bitmap: array of unsigned longs, the source bitmap, non NULL
1158-
* @nbits: number of bits in @bitmap
1159-
*
1160-
* copy min(nbits, 32*nwords) bits from @bitmap to @buf. Remaining
1161-
* bits after nbits in @buf (if any) are cleared.
1162-
*
1163-
* Return the number of bits effectively copied.
1164-
*/
1165-
unsigned int
1166-
bitmap_to_u32array(u32 *buf, unsigned int nwords,
1167-
const unsigned long *bitmap, unsigned int nbits)
1168-
{
1169-
unsigned int dst_idx = 0, src_idx = 0;
1170-
1171-
while (dst_idx < nwords) {
1172-
unsigned long part = 0;
1173-
1174-
if (src_idx < BITS_TO_LONGS(nbits)) {
1175-
part = bitmap[src_idx];
1176-
if (src_idx >= nbits/BITS_PER_LONG)
1177-
part &= BITMAP_LAST_WORD_MASK(nbits);
1178-
src_idx++;
1179-
}
1180-
1181-
buf[dst_idx++] = part & 0xffffffffUL;
1182-
1183-
#if BITS_PER_LONG == 64
1184-
if (dst_idx < nwords) {
1185-
part >>= 32;
1186-
buf[dst_idx++] = part & 0xffffffffUL;
1187-
}
1188-
#endif
1189-
}
1190-
1191-
return min_t(unsigned int, nbits, 32*nwords);
1192-
}
1193-
EXPORT_SYMBOL(bitmap_to_u32array);
1194-
11951108
/**
11961109
* bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
11971110
* @dst: destination buffer

‎lib/test_bitmap.c‎

Lines changed: 31 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __check_eq_uint(const char *srcfile, unsigned int line,
2323
const unsigned int exp_uint, unsigned int x)
2424
{
2525
if (exp_uint != x) {
26-
pr_warn("[%s:%u] expected %u, got %u\n",
26+
pr_err("[%s:%u] expected %u, got %u\n",
2727
srcfile, line, exp_uint, x);
2828
return false;
2929
}
@@ -33,19 +33,13 @@ __check_eq_uint(const char *srcfile, unsigned int line,
3333

3434
static bool __init
3535
__check_eq_bitmap(const char *srcfile, unsigned int line,
36-
const unsigned long *exp_bmap, unsigned int exp_nbits,
37-
const unsigned long *bmap, unsigned int nbits)
36+
const unsigned long *exp_bmap, const unsigned long *bmap,
37+
unsigned int nbits)
3838
{
39-
if (exp_nbits != nbits) {
40-
pr_warn("[%s:%u] bitmap length mismatch: expected %u, got %u\n",
41-
srcfile, line, exp_nbits, nbits);
42-
return false;
43-
}
44-
4539
if (!bitmap_equal(exp_bmap, bmap, nbits)) {
4640
pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
4741
srcfile, line,
48-
exp_nbits, exp_bmap, nbits, bmap);
42+
nbits, exp_bmap, nbits, bmap);
4943
return false;
5044
}
5145
return true;
@@ -66,6 +60,10 @@ __check_eq_pbl(const char *srcfile, unsigned int line,
6660
return true;
6761
}
6862

63+
static bool __init
64+
__check_eq_u32_array(const char *srcfile, unsigned int line,
65+
const u32 *exp_arr, unsigned int exp_len,
66+
const u32 *arr, unsigned int len) __used;
6967
static bool __init
7068
__check_eq_u32_array(const char *srcfile, unsigned int line,
7169
const u32 *exp_arr, unsigned int exp_len,
@@ -255,171 +253,29 @@ static void __init test_bitmap_parselist(void)
255253
}
256254
}
257255

258-
static void __init test_bitmap_u32_array_conversions(void)
256+
static void __init test_bitmap_arr32(void)
259257
{
260-
DECLARE_BITMAP(bmap1, 1024);
261-
DECLARE_BITMAP(bmap2, 1024);
262-
u32 exp_arr[32], arr[32];
263-
unsigned nbits;
264-
265-
for (nbits = 0 ; nbits < 257 ; ++nbits) {
266-
const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
267-
unsigned int i, rv;
268-
269-
bitmap_zero(bmap1, nbits);
270-
bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
271-
272-
memset(arr, 0xff, sizeof(arr));
273-
rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
274-
expect_eq_uint(nbits, rv);
275-
276-
memset(exp_arr, 0xff, sizeof(exp_arr));
277-
memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
278-
expect_eq_u32_array(exp_arr, 32, arr, 32);
279-
280-
bitmap_fill(bmap2, 1024);
281-
rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
282-
expect_eq_uint(nbits, rv);
283-
expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
284-
285-
for (i = 0 ; i < nbits ; ++i) {
286-
/*
287-
* test conversion bitmap -> u32[]
288-
*/
289-
290-
bitmap_zero(bmap1, 1024);
291-
__set_bit(i, bmap1);
292-
bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */
293-
294-
memset(arr, 0xff, sizeof(arr));
295-
rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
296-
expect_eq_uint(nbits, rv);
297-
298-
/* 1st used u32 words contain expected bit set, the
299-
* remaining words are left unchanged (0xff)
300-
*/
301-
memset(exp_arr, 0xff, sizeof(exp_arr));
302-
memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
303-
exp_arr[i/32] = (1U<<(i%32));
304-
expect_eq_u32_array(exp_arr, 32, arr, 32);
305-
306-
307-
/* same, with longer array to fill
308-
*/
309-
memset(arr, 0xff, sizeof(arr));
310-
rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
311-
expect_eq_uint(nbits, rv);
312-
313-
/* 1st used u32 words contain expected bit set, the
314-
* remaining words are all 0s
315-
*/
316-
memset(exp_arr, 0, sizeof(exp_arr));
317-
exp_arr[i/32] = (1U<<(i%32));
318-
expect_eq_u32_array(exp_arr, 32, arr, 32);
319-
320-
/*
321-
* test conversion u32[] -> bitmap
322-
*/
323-
324-
/* the 1st nbits of bmap2 are identical to
325-
* bmap1, the remaining bits of bmap2 are left
326-
* unchanged (all 1s)
327-
*/
328-
bitmap_fill(bmap2, 1024);
329-
rv = bitmap_from_u32array(bmap2, nbits,
330-
exp_arr, used_u32s);
331-
expect_eq_uint(nbits, rv);
332-
333-
expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
334-
335-
/* same, with more bits to fill
336-
*/
337-
memset(arr, 0xff, sizeof(arr)); /* garbage */
338-
memset(arr, 0, used_u32s*sizeof(u32));
339-
arr[i/32] = (1U<<(i%32));
340-
341-
bitmap_fill(bmap2, 1024);
342-
rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
343-
expect_eq_uint(used_u32s*32, rv);
344-
345-
/* the 1st nbits of bmap2 are identical to
346-
* bmap1, the remaining bits of bmap2 are cleared
347-
*/
348-
bitmap_zero(bmap1, 1024);
349-
__set_bit(i, bmap1);
350-
expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
351-
352-
353-
/*
354-
* test short conversion bitmap -> u32[] (1
355-
* word too short)
356-
*/
357-
if (used_u32s > 1) {
358-
bitmap_zero(bmap1, 1024);
359-
__set_bit(i, bmap1);
360-
bitmap_set(bmap1, nbits,
361-
1024 - nbits); /* garbage */
362-
memset(arr, 0xff, sizeof(arr));
363-
364-
rv = bitmap_to_u32array(arr, used_u32s - 1,
365-
bmap1, nbits);
366-
expect_eq_uint((used_u32s - 1)*32, rv);
367-
368-
/* 1st used u32 words contain expected
369-
* bit set, the remaining words are
370-
* left unchanged (0xff)
371-
*/
372-
memset(exp_arr, 0xff, sizeof(exp_arr));
373-
memset(exp_arr, 0,
374-
(used_u32s-1)*sizeof(*exp_arr));
375-
if ((i/32) < (used_u32s - 1))
376-
exp_arr[i/32] = (1U<<(i%32));
377-
expect_eq_u32_array(exp_arr, 32, arr, 32);
378-
}
379-
380-
/*
381-
* test short conversion u32[] -> bitmap (3
382-
* bits too short)
383-
*/
384-
if (nbits > 3) {
385-
memset(arr, 0xff, sizeof(arr)); /* garbage */
386-
memset(arr, 0, used_u32s*sizeof(*arr));
387-
arr[i/32] = (1U<<(i%32));
388-
389-
bitmap_zero(bmap1, 1024);
390-
rv = bitmap_from_u32array(bmap1, nbits - 3,
391-
arr, used_u32s);
392-
expect_eq_uint(nbits - 3, rv);
393-
394-
/* we are expecting the bit < nbits -
395-
* 3 (none otherwise), and the rest of
396-
* bmap1 unchanged (0-filled)
397-
*/
398-
bitmap_zero(bmap2, 1024);
399-
if (i < nbits - 3)
400-
__set_bit(i, bmap2);
401-
expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
402-
403-
/* do the same with bmap1 initially
404-
* 1-filled
405-
*/
406-
407-
bitmap_fill(bmap1, 1024);
408-
rv = bitmap_from_u32array(bmap1, nbits - 3,
409-
arr, used_u32s);
410-
expect_eq_uint(nbits - 3, rv);
411-
412-
/* we are expecting the bit < nbits -
413-
* 3 (none otherwise), and the rest of
414-
* bmap1 unchanged (1-filled)
415-
*/
416-
bitmap_zero(bmap2, 1024);
417-
if (i < nbits - 3)
418-
__set_bit(i, bmap2);
419-
bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
420-
expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
421-
}
422-
}
258+
unsigned int nbits, next_bit, len = sizeof(exp) * 8;
259+
u32 arr[sizeof(exp) / 4];
260+
DECLARE_BITMAP(bmap2, len);
261+
262+
memset(arr, 0xa5, sizeof(arr));
263+
264+
for (nbits = 0; nbits < len; ++nbits) {
265+
bitmap_to_arr32(arr, exp, nbits);
266+
bitmap_from_arr32(bmap2, arr, nbits);
267+
expect_eq_bitmap(bmap2, exp, nbits);
268+
269+
next_bit = find_next_bit(bmap2,
270+
round_up(nbits, BITS_PER_LONG), nbits);
271+
if (next_bit < round_up(nbits, BITS_PER_LONG))
272+
pr_err("bitmap_copy_arr32(nbits == %d:"
273+
" tail is not safely cleared: %d\n",
274+
nbits, next_bit);
275+
276+
if (nbits < len - 32)
277+
expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
278+
0xa5a5a5a5);
423279
}
424280
}
425281

@@ -454,7 +310,7 @@ static void noinline __init test_mem_optimisations(void)
454310
static int __init test_bitmap_init(void)
455311
{
456312
test_zero_fill_copy();
457-
test_bitmap_u32_array_conversions();
313+
test_bitmap_arr32();
458314
test_bitmap_parselist();
459315
test_mem_optimisations();
460316

0 commit comments

Comments
 (0)