Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'bitmap-for-v7.1' of https://github.com/norov/linux

Pull bitmap updates from Yury Norov:

- new API: bitmap_weight_from() and bitmap_weighted_xor() (Yury)

- drop unused __find_nth_andnot_bit() (Yury)

- new tests and test improvements (Andy, Akinobu, Yury)

- fixes for count_zeroes API (Yury)

- cleanup bitmap_print_to_pagebuf() mess (Yury)

- documentation updates (Andy, Kai, Kit).

* tag 'bitmap-for-v7.1' of https://github.com/norov/linux: (24 commits)
bitops: Update kernel-doc for sign_extendXX()
powerpc/xive: simplify xive_spapr_debug_show()
thermal: intel: switch cpumask_get() to using cpumask_print_to_pagebuf()
coresight: don't use bitmap_print_to_pagebuf()
lib/prime_numbers: drop temporary buffer in dump_primes()
drm/xe: switch xe_pagefault_queue_init() to using bitmap_weighted_or()
ice: use bitmap_empty() in ice_vf_has_no_qs_ena
ice: use bitmap_weighted_xor() in ice_find_free_recp_res_idx()
bitmap: introduce bitmap_weighted_xor()
bitmap: add test_zero_nbits()
bitmap: exclude nbits == 0 cases from bitmap test
bitmap: test bitmap_weight() for more
asm-generic/bitops: Fix a comment typo in instrumented-atomic.h
bitops: fix kernel-doc parameter name for parity8()
lib: count_zeros: unify count_{leading,trailing}_zeros()
lib: count_zeros: fix 32/64-bit inconsistency in count_trailing_zeros()
lib: crypto: fix comments for count_leading_zeros()
x86/topology: use bitmap_weight_from()
bitmap: add bitmap_weight_from()
lib/find_bit_benchmark: avoid clearing randomly filled bitmap in test_find_first_bit()
...

+230 -97
+2 -2
arch/loongarch/kvm/intc/eiointc.c
··· 16 16 ipnum = (s->ipmap >> (irq / 32 * 8)) & 0xff; 17 17 if (!(s->status & BIT(EIOINTC_ENABLE_INT_ENCODE))) { 18 18 ipnum = count_trailing_zeros(ipnum); 19 - ipnum = (ipnum >= 0 && ipnum < 4) ? ipnum : 0; 19 + ipnum = ipnum < 4 ? ipnum : 0; 20 20 } 21 21 22 22 cpuid = ((u8 *)s->coremap)[irq]; ··· 41 41 ipnum = (s->ipmap >> (irq / 32 * 8)) & 0xff; 42 42 if (!(s->status & BIT(EIOINTC_ENABLE_INT_ENCODE))) { 43 43 ipnum = count_trailing_zeros(ipnum); 44 - ipnum = (ipnum >= 0 && ipnum < 4) ? ipnum : 0; 44 + ipnum = ipnum < 4 ? ipnum : 0; 45 45 } 46 46 47 47 cpu = s->sw_coremap[irq];
+2 -10
arch/powerpc/sysdev/xive/spapr.c
··· 667 667 static int xive_spapr_debug_show(struct seq_file *m, void *private) 668 668 { 669 669 struct xive_irq_bitmap *xibm; 670 - char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 671 670 672 - if (!buf) 673 - return -ENOMEM; 674 - 675 - list_for_each_entry(xibm, &xive_irq_bitmaps, list) { 676 - memset(buf, 0, PAGE_SIZE); 677 - bitmap_print_to_pagebuf(true, buf, xibm->bitmap, xibm->count); 678 - seq_printf(m, "bitmap #%d: %s", xibm->count, buf); 679 - } 680 - kfree(buf); 671 + list_for_each_entry(xibm, &xive_irq_bitmaps, list) 672 + seq_printf(m, "bitmap #%d: %*pbl\n", xibm->count, xibm->count, xibm->bitmap); 681 673 682 674 return 0; 683 675 }
+2 -6
arch/x86/kernel/cpu/topology.c
··· 204 204 static unsigned int topo_unit_count(u32 lvlid, enum x86_topology_domains at_level, 205 205 unsigned long *map) 206 206 { 207 - unsigned int id, end, cnt = 0; 207 + unsigned int end; 208 208 209 209 /* Calculate the exclusive end */ 210 210 end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]); 211 - 212 - /* Unfortunately there is no bitmap_weight_range() */ 213 - for (id = find_next_bit(map, end, lvlid); id < end; id = find_next_bit(map, end, ++id)) 214 - cnt++; 215 - return cnt; 211 + return bitmap_weight_from(map, lvlid, end); 216 212 } 217 213 218 214 static __init void topo_register_apic(u32 apic_id, u32 acpi_id, bool present)
+1 -2
drivers/gpu/drm/xe/xe_pagefault.c
··· 291 291 xe_dss_mask_t all_dss; 292 292 int num_dss, num_eus; 293 293 294 - bitmap_or(all_dss, gt->fuse_topo.g_dss_mask, 294 + num_dss = bitmap_weighted_or(all_dss, gt->fuse_topo.g_dss_mask, 295 295 gt->fuse_topo.c_dss_mask, XE_MAX_DSS_FUSE_BITS); 296 296 297 - num_dss = bitmap_weight(all_dss, XE_MAX_DSS_FUSE_BITS); 298 297 num_eus = bitmap_weight(gt->fuse_topo.eu_mask_per_dss, 299 298 XE_MAX_EU_FUSE_BITS) * num_dss; 300 299
+14 -18
drivers/hwtracing/coresight/coresight-cti-sysfs.c
··· 606 606 struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); 607 607 struct cti_config *cfg = &drvdata->config; 608 608 unsigned long ctigate_bitmask = cfg->ctigate; 609 - int size = 0; 610 609 611 610 if (cfg->ctigate == 0) 612 - size = sprintf(buf, "\n"); 613 - else 614 - size = bitmap_print_to_pagebuf(true, buf, &ctigate_bitmask, 615 - cfg->nr_ctm_channels); 616 - return size; 611 + return sprintf(buf, "\n"); 612 + 613 + return sysfs_emit(buf, "%*pbl\n", cfg->nr_ctm_channels, &ctigate_bitmask); 617 614 } 618 615 static DEVICE_ATTR_RW(chan_gate_enable); 619 616 ··· 707 710 { 708 711 struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); 709 712 struct cti_config *cfg = &drvdata->config; 710 - int size = 0, nr_trig_max = cfg->nr_trig_max; 713 + int nr_trig_max = cfg->nr_trig_max; 711 714 unsigned long mask = cfg->trig_out_filter; 712 715 713 - if (mask) 714 - size = bitmap_print_to_pagebuf(true, buf, &mask, nr_trig_max); 715 - return size; 716 + if (mask == 0) 717 + return 0; 718 + 719 + return sysfs_emit(buf, "%*pbl\n", nr_trig_max, &mask); 716 720 } 717 721 static DEVICE_ATTR_RO(trigout_filtered); 718 722 ··· 832 834 { 833 835 struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); 834 836 struct cti_config *config = &drvdata->config; 835 - int size, i; 837 + int i; 836 838 unsigned long inuse_bits = 0, chan_mask; 837 839 838 840 /* scan regs to get bitmap of channels in use. */ ··· 850 852 /* list of channels, or 'none' */ 851 853 chan_mask = GENMASK(config->nr_ctm_channels - 1, 0); 852 854 if (inuse_bits & chan_mask) 853 - size = bitmap_print_to_pagebuf(true, buf, &inuse_bits, 854 - config->nr_ctm_channels); 855 - else 856 - size = sprintf(buf, "\n"); 857 - return size; 855 + return sysfs_emit(buf, "%*pbl\n", config->nr_ctm_channels, &inuse_bits); 856 + 857 + return sprintf(buf, "\n"); 858 858 } 859 859 860 860 static ssize_t chan_inuse_show(struct device *dev, ··· 924 928 struct cti_config *cfg = &drvdata->config; 925 929 unsigned long mask = con->con_in->used_mask; 926 930 927 - return bitmap_print_to_pagebuf(true, buf, &mask, cfg->nr_trig_max); 931 + return sysfs_emit(buf, "%*pbl\n", cfg->nr_trig_max, &mask); 928 932 } 929 933 930 934 static ssize_t trigout_sig_show(struct device *dev, ··· 938 942 struct cti_config *cfg = &drvdata->config; 939 943 unsigned long mask = con->con_out->used_mask; 940 944 941 - return bitmap_print_to_pagebuf(true, buf, &mask, cfg->nr_trig_max); 945 + return sysfs_emit(buf, "%*pbl\n", cfg->nr_trig_max, &mask); 942 946 } 943 947 944 948 /* convert a sig type id to a name */
+1 -3
drivers/net/ethernet/intel/ice/ice_switch.c
··· 4984 4984 hw->switch_info->recp_list[bit].res_idxs, 4985 4985 ICE_MAX_FV_WORDS); 4986 4986 4987 - bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS); 4988 - 4989 4987 /* return number of free indexes */ 4990 - return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS); 4988 + return (u16)bitmap_weighted_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS); 4991 4989 } 4992 4990 4993 4991 /**
+2 -2
drivers/net/ethernet/intel/ice/ice_vf_lib.c
··· 1210 1210 */ 1211 1211 bool ice_vf_has_no_qs_ena(struct ice_vf *vf) 1212 1212 { 1213 - return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) && 1214 - !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF)); 1213 + return bitmap_empty(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) && 1214 + bitmap_empty(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF); 1215 1215 } 1216 1216 1217 1217 /**
+1 -2
drivers/thermal/intel/intel_powerclamp.c
··· 200 200 if (!cpumask_available(idle_injection_cpu_mask)) 201 201 return -ENODEV; 202 202 203 - return bitmap_print_to_pagebuf(false, buf, cpumask_bits(idle_injection_cpu_mask), 204 - nr_cpumask_bits); 203 + return cpumap_print_to_pagebuf(false, buf, idle_injection_cpu_mask); 205 204 } 206 205 207 206 static const struct kernel_param_ops cpumask_ops = {
+1 -1
include/asm-generic/bitops/instrumented-atomic.h
··· 100 100 return arch_test_and_change_bit(nr, addr); 101 101 } 102 102 103 - #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H */ 103 + #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_ATOMIC_H */
+48
include/linux/bitmap.h
··· 46 46 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2 47 47 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2 48 48 * bitmap_weighted_or(dst, src1, src2, nbits) *dst = *src1 | *src2. Returns Hamming Weight of dst 49 + * bitmap_weighted_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2. Returns Hamming Weight of dst 49 50 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2 50 51 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2) 51 52 * bitmap_complement(dst, src, nbits) *dst = ~(*src) ··· 58 57 * bitmap_weight(src, nbits) Hamming Weight: number set bits 59 58 * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap 60 59 * bitmap_weight_andnot(src1, src2, nbits) Hamming Weight of andnot'ed bitmap 60 + * bitmap_weight_from(src, start, end) Hamming Weight starting from @start 61 61 * bitmap_set(dst, pos, nbits) Set specified bit area 62 62 * bitmap_clear(dst, pos, nbits) Clear specified bit area 63 63 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area ··· 169 167 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, 170 168 const unsigned long *bitmap2, unsigned int nbits); 171 169 unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1, 170 + const unsigned long *bitmap2, unsigned int nbits); 171 + unsigned int __bitmap_weighted_xor(unsigned long *dst, const unsigned long *bitmap1, 172 172 const unsigned long *bitmap2, unsigned int nbits); 173 173 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, 174 174 const unsigned long *bitmap2, unsigned int nbits); ··· 357 353 } 358 354 359 355 static __always_inline 356 + unsigned int bitmap_weighted_xor(unsigned long *dst, const unsigned long *src1, 357 + const unsigned long *src2, unsigned int nbits) 358 + { 359 + if (small_const_nbits(nbits)) { 360 + *dst = *src1 ^ *src2; 361 + return hweight_long(*dst & BITMAP_LAST_WORD_MASK(nbits)); 362 + } else { 363 + return __bitmap_weighted_xor(dst, src1, src2, nbits); 364 + } 365 + } 366 + 367 + static __always_inline 360 368 void bitmap_xor(unsigned long *dst, const unsigned long *src1, 361 369 const unsigned long *src2, unsigned int nbits) 362 370 { ··· 493 477 if (small_const_nbits(nbits)) 494 478 return hweight_long(*src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)); 495 479 return __bitmap_weight_andnot(src1, src2, nbits); 480 + } 481 + 482 + /** 483 + * bitmap_weight_from - Hamming weight for a memory region 484 + * @bitmap: The base address 485 + * @start: The bitnumber to starts weighting 486 + * @end: the bitmap size in bits 487 + * 488 + * Returns the number of set bits in the region. If @start >= @end, 489 + * return >= end. 490 + */ 491 + static __always_inline 492 + unsigned long bitmap_weight_from(const unsigned long *bitmap, 493 + unsigned int start, unsigned int end) 494 + { 495 + unsigned long w; 496 + 497 + if (unlikely(start >= end)) 498 + return end; 499 + 500 + if (small_const_nbits(end)) 501 + return hweight_long(*bitmap & GENMASK(end - 1, start)); 502 + 503 + bitmap += start / BITS_PER_LONG; 504 + /* Opencode round_down() to not include math.h */ 505 + end -= start & ~(BITS_PER_LONG - 1); 506 + start %= BITS_PER_LONG; 507 + w = bitmap_weight(bitmap, end); 508 + if (start) 509 + w -= hweight_long(*bitmap & BITMAP_LAST_WORD_MASK(start)); 510 + 511 + return w; 496 512 } 497 513 498 514 static __always_inline
+9 -3
include/linux/bitops.h
··· 179 179 /** 180 180 * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit 181 181 * @value: value to sign extend 182 - * @index: 0 based bit index (0<=index<32) to sign bit 182 + * @index: 0 based bit index (0 <= index < 32) to sign bit 183 183 * 184 184 * This is safe to use for 16- and 8-bit types as well. 185 + * 186 + * Return: 32-bit sign extended value 185 187 */ 186 188 static __always_inline __s32 sign_extend32(__u32 value, int index) 187 189 { ··· 194 192 /** 195 193 * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit 196 194 * @value: value to sign extend 197 - * @index: 0 based bit index (0<=index<64) to sign bit 195 + * @index: 0 based bit index (0 <= index < 64) to sign bit 196 + * 197 + * This is safe to use for 32-, 16- and 8-bit types as well. 198 + * 199 + * Return: 64-bit sign extended value 198 200 */ 199 201 static __always_inline __s64 sign_extend64(__u64 value, int index) 200 202 { ··· 236 230 237 231 /** 238 232 * parity8 - get the parity of an u8 value 239 - * @value: the value to be examined 233 + * @val: the value to be examined 240 234 * 241 235 * Determine the parity of the u8 argument. 242 236 *
+3 -10
include/linux/count_zeros.h
··· 18 18 * 19 19 * If the MSB of @x is set, the result is 0. 20 20 * If only the LSB of @x is set, then the result is BITS_PER_LONG-1. 21 - * If @x is 0 then the result is COUNT_LEADING_ZEROS_0. 21 + * If @x is 0 then the result is BITS_PER_LONG. 22 22 */ 23 23 static inline int count_leading_zeros(unsigned long x) 24 24 { ··· 28 28 return BITS_PER_LONG - fls64(x); 29 29 } 30 30 31 - #define COUNT_LEADING_ZEROS_0 BITS_PER_LONG 32 - 33 31 /** 34 32 * count_trailing_zeros - Count the number of zeros from the LSB forwards 35 33 * @x: The value ··· 36 38 * 37 39 * If the LSB of @x is set, the result is 0. 38 40 * If only the MSB of @x is set, then the result is BITS_PER_LONG-1. 39 - * If @x is 0 then the result is COUNT_TRAILING_ZEROS_0. 41 + * If @x is 0 then the result is BITS_PER_LONG. 40 42 */ 41 43 static inline int count_trailing_zeros(unsigned long x) 42 44 { 43 - #define COUNT_TRAILING_ZEROS_0 (-1) 44 - 45 - if (sizeof(x) == 4) 46 - return ffs(x); 47 - else 48 - return (x != 0) ? __ffs(x) : COUNT_TRAILING_ZEROS_0; 45 + return x ? __ffs(x) : BITS_PER_LONG; 49 46 } 50 47 51 48 #endif /* _LINUX_BITOPS_COUNT_ZEROS_H_ */
-2
include/linux/find.h
··· 22 22 unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n); 23 23 unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2, 24 24 unsigned long size, unsigned long n); 25 - unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 26 - unsigned long size, unsigned long n); 27 25 unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 28 26 const unsigned long *addr3, unsigned long size, 29 27 unsigned long n);
+9
lib/bitmap.c
··· 69 69 tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k]; 70 70 return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0; 71 71 } 72 + EXPORT_SYMBOL(__bitmap_or_equal); 72 73 73 74 void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits) 74 75 { ··· 361 360 { 362 361 return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] | bitmap2[idx]; dst[idx]; }), bits); 363 362 } 363 + EXPORT_SYMBOL(__bitmap_weighted_or); 364 + 365 + unsigned int __bitmap_weighted_xor(unsigned long *dst, const unsigned long *bitmap1, 366 + const unsigned long *bitmap2, unsigned int bits) 367 + { 368 + return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] ^ bitmap2[idx]; dst[idx]; }), bits); 369 + } 370 + EXPORT_SYMBOL(__bitmap_weighted_xor); 364 371 365 372 void __bitmap_set(unsigned long *map, unsigned int start, int len) 366 373 {
+4 -4
lib/crypto/mpi/longlong.h
··· 66 66 * denominator). Like udiv_qrnnd but the numbers are signed. The quotient 67 67 * is rounded towards 0. 68 68 * 69 - * 5) count_leading_zeros(count, x) counts the number of zero-bits from the 69 + * 5) count_leading_zeros(x) counts the number of zero-bits from the 70 70 * msb to the first non-zero bit in the UWtype X. This is the number of 71 - * steps X needs to be shifted left to set the msb. Undefined for X == 0, 72 - * unless the symbol COUNT_LEADING_ZEROS_0 is defined to some value. 71 + * steps X needs to be shifted left to set the msb. 72 + * count_leading_zeros(0) == BITS_PER_LONG 73 73 * 74 - * 6) count_trailing_zeros(count, x) like count_leading_zeros, but counts 74 + * 6) count_trailing_zeros() like count_leading_zeros(), but counts 75 75 * from the least significant end. 76 76 * 77 77 * 7) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
-7
lib/find_bit.c
··· 172 172 } 173 173 EXPORT_SYMBOL(__find_nth_and_bit); 174 174 175 - unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 176 - unsigned long size, unsigned long n) 177 - { 178 - return FIND_NTH_BIT(addr1[idx] & ~addr2[idx], size, n); 179 - } 180 - EXPORT_SYMBOL(__find_nth_andnot_bit); 181 - 182 175 unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, 183 176 const unsigned long *addr2, 184 177 const unsigned long *addr3,
+9 -6
lib/find_bit_benchmark.c
··· 30 30 static DECLARE_BITMAP(bitmap2, BITMAP_LEN) __initdata; 31 31 32 32 /* 33 - * This is Schlemiel the Painter's algorithm. It should be called after 34 - * all other tests for the same bitmap because it sets all bits of bitmap to 1. 33 + * This is Schlemiel the Painter's algorithm. 35 34 */ 36 - static int __init test_find_first_bit(void *bitmap, unsigned long len) 35 + static int __init test_find_first_bit(const void *bitmap, unsigned long len) 37 36 { 37 + static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata; 38 38 unsigned long i, cnt; 39 39 ktime_t time; 40 40 41 + bitmap_copy(cp, bitmap, BITMAP_LEN); 42 + 41 43 time = ktime_get(); 42 44 for (cnt = i = 0; i < len; cnt++) { 43 - i = find_first_bit(bitmap, len); 44 - __clear_bit(i, bitmap); 45 + i = find_first_bit(cp, len); 46 + __clear_bit(i, cp); 45 47 } 46 48 time = ktime_get() - time; 47 49 pr_err("find_first_bit: %18llu ns, %6ld iterations\n", time, cnt); ··· 51 49 return 0; 52 50 } 53 51 54 - static int __init test_find_first_and_bit(void *bitmap, const void *bitmap2, unsigned long len) 52 + static int __init test_find_first_and_bit(const void *bitmap, const void *bitmap2, 53 + unsigned long len) 55 54 { 56 55 static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata; 57 56 unsigned long i, cnt;
+2 -4
lib/math/tests/prime_numbers_kunit.c
··· 8 8 9 9 static void dump_primes(void *ctx, const struct primes *p) 10 10 { 11 - static char buf[PAGE_SIZE]; 12 11 struct kunit_suite *suite = ctx; 13 12 14 - bitmap_print_to_pagebuf(true, buf, p->primes, p->sz); 15 - kunit_info(suite, "primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %s", 16 - p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], buf); 13 + kunit_info(suite, "primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %*pbl", 14 + p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], (int)p->sz, p->primes); 17 15 } 18 16 19 17 static void prime_numbers_test(struct kunit *test)
+120 -15
lib/test_bitmap.c
··· 354 354 355 355 static const unsigned long sg_mask[] __initconst = { 356 356 BITMAP_FROM_U64(0x000000000000035aULL), 357 + BITMAP_FROM_U64(0x0000000000000000ULL), 357 358 }; 358 359 359 360 static const unsigned long sg_src[] __initconst = { 360 361 BITMAP_FROM_U64(0x0000000000000667ULL), 362 + BITMAP_FROM_U64(0x0000000000000000ULL), 361 363 }; 362 364 363 365 static const unsigned long sg_gather_exp[] __initconst = { 364 366 BITMAP_FROM_U64(0x0000000000000029ULL), 367 + BITMAP_FROM_U64(0x0000000000000000ULL), 365 368 }; 366 369 367 370 static const unsigned long sg_scatter_exp[] __initconst = { 368 371 BITMAP_FROM_U64(0x000000000000021aULL), 372 + BITMAP_FROM_U64(0x0000000000000000ULL), 369 373 }; 370 374 371 375 static void __init test_bitmap_sg(void) ··· 383 379 /* Simple gather call */ 384 380 bitmap_zero(bmap_gather, 100); 385 381 bitmap_gather(bmap_gather, sg_src, sg_mask, nbits); 386 - expect_eq_bitmap(sg_gather_exp, bmap_gather, nbits); 382 + expect_eq_bitmap(sg_gather_exp, bmap_gather, 100); 387 383 388 384 /* Simple scatter call */ 389 385 bitmap_zero(bmap_scatter, 100); 390 386 bitmap_scatter(bmap_scatter, sg_src, sg_mask, nbits); 391 - expect_eq_bitmap(sg_scatter_exp, bmap_scatter, nbits); 387 + expect_eq_bitmap(sg_scatter_exp, bmap_scatter, 100); 392 388 393 389 /* Scatter/gather relationship */ 394 390 bitmap_zero(bmap_tmp, 100); 395 391 bitmap_gather(bmap_tmp, bmap_scatter, sg_mask, nbits); 396 392 bitmap_scatter(bmap_res, bmap_tmp, sg_mask, nbits); 397 - expect_eq_bitmap(bmap_scatter, bmap_res, nbits); 393 + expect_eq_bitmap(bmap_scatter, bmap_res, 100); 398 394 } 399 395 400 396 #define PARSE_TIME 0x1 ··· 524 520 } 525 521 526 522 if (ptest.flags & PARSE_TIME) 527 - pr_info("parselist: %d: input is '%s' OK, Time: %llu\n", 528 - i, ptest.in, time); 523 + pr_info("parselist('%s'):\t%llu\n", ptest.in, time); 529 524 530 525 #undef ptest 531 526 } ··· 547 544 goto out; 548 545 549 546 time = ktime_get(); 550 - ret = bitmap_print_to_pagebuf(true, buf, bmap, PAGE_SIZE * 8); 547 + ret = scnprintf(buf, PAGE_SIZE, "%*pbl", (int)PAGE_SIZE * 8, bmap); 551 548 time = ktime_get() - time; 552 549 553 - if (ret != slen + 1) { 554 - pr_err("bitmap_print_to_pagebuf: result is %d, expected %d\n", ret, slen); 550 + if (ret != slen) { 551 + pr_err("scnprintf(\"%%*pbl\"): result is %d, expected %d\n", ret, slen); 555 552 failed_tests++; 556 553 goto out; 557 554 } 558 555 559 556 if (strncmp(buf, expected, slen)) { 560 - pr_err("bitmap_print_to_pagebuf: result is %s, expected %s\n", buf, expected); 557 + pr_err("scnprintf(\"%%*pbl\"): result is %s, expected %s\n", buf, expected); 561 558 failed_tests++; 562 559 goto out; 563 560 } 564 561 565 - pr_info("bitmap_print_to_pagebuf: input is '%s', Time: %llu\n", buf, time); 562 + pr_info("scnprintf(\"%%*pbl\", '%s'):\t%llu\n", buf, time); 566 563 out: 567 564 kfree(buf); 568 565 kfree(bmap); ··· 653 650 654 651 memset(arr, 0xa5, sizeof(arr)); 655 652 656 - for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) { 653 + for (nbits = 1; nbits < EXP1_IN_BITS; ++nbits) { 657 654 bitmap_to_arr32(arr, exp1, nbits); 658 655 bitmap_from_arr32(bmap2, arr, nbits); 659 656 expect_eq_bitmap(bmap2, exp1, nbits); ··· 681 678 682 679 memset(arr, 0xa5, sizeof(arr)); 683 680 684 - for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) { 681 + for (nbits = 1; nbits < EXP1_IN_BITS; ++nbits) { 685 682 memset(bmap2, 0xff, sizeof(arr)); 686 683 bitmap_to_arr64(arr, exp1, nbits); 687 684 bitmap_from_arr64(bmap2, arr, nbits); ··· 714 711 unsigned int start, nbits; 715 712 716 713 for (start = 0; start < 1024; start += 8) { 717 - for (nbits = 0; nbits < 1024 - start; nbits += 8) { 714 + for (nbits = 1; nbits < 1024 - start; nbits += 8) { 718 715 memset(bmap1, 0x5a, sizeof(bmap1)); 719 716 memset(bmap2, 0x5a, sizeof(bmap2)); 720 717 ··· 852 849 bitmap_clear(tmp, 0, wr); 853 850 expect_eq_bitmap(tmp, copy, 500); 854 851 } 852 + } 853 + 854 + static void __init test_bitmap_weight(void) 855 + { 856 + unsigned int bit, w1, w2, w; 857 + DECLARE_BITMAP(b, 30); 858 + DECLARE_BITMAP(b1, 128); 859 + 860 + bitmap_parselist("all:1/2", b, 30); 861 + 862 + /* Test inline implementation */ 863 + w = bitmap_weight(b, 30); 864 + w1 = bitmap_weight(b, 15); 865 + w2 = bitmap_weight_from(b, 15, 30); 866 + 867 + expect_eq_uint(15, w); 868 + expect_eq_uint(8, w1); 869 + expect_eq_uint(7, w2); 870 + 871 + /* Test outline implementation */ 872 + w = bitmap_weight(exp1, EXP1_IN_BITS); 873 + for (bit = 1; bit < EXP1_IN_BITS; bit++) { 874 + w1 = bitmap_weight(exp1, bit); 875 + w2 = bitmap_weight_from(exp1, bit, EXP1_IN_BITS); 876 + expect_eq_uint(w1 + w2, w); 877 + } 878 + 879 + /* Test out-of-range */ 880 + w = bitmap_weight_from(b, 31, 30); 881 + expect_eq_uint(0, !!(w < 30)); 882 + 883 + /* 884 + * Test bitmap_weight() for correctness in case of some bits set between 885 + * nbits and end of the last word. 886 + */ 887 + bitmap_fill(b1, 128); 888 + 889 + /* Inline */ 890 + expect_eq_uint(30, bitmap_weight(b1, 30)); 891 + expect_eq_uint(100, bitmap_weight(b1, 100)); 892 + 893 + /* Outline */ 894 + for (int i = 1; i < 128; i++) 895 + expect_eq_uint(i, bitmap_weight(b1, i)); 855 896 } 856 897 857 898 static void __init test_for_each_clear_bit(void) ··· 1442 1395 } 1443 1396 } 1444 1397 time = ktime_get() - time; 1445 - pr_info("Time spent in %s:\t%llu\n", __func__, time); 1398 + pr_info("%s:\t\t%llu\n", __func__, time); 1446 1399 } 1447 1400 1448 1401 static void __init test_bitmap_write_perf(void) ··· 1464 1417 } 1465 1418 } 1466 1419 time = ktime_get() - time; 1467 - pr_info("Time spent in %s:\t%llu\n", __func__, time); 1420 + pr_info("%s:\t\t%llu\n", __func__, time); 1421 + } 1422 + 1423 + /* 1424 + * nbits == 0 is most commonly not a valid case. Bitmap users should revisit 1425 + * the caller logic. Bitmap API doesn't provide any guarantees on returned 1426 + * value. The pointers are not dereferenced. The return value is intentionally 1427 + * ignored. 1428 + */ 1429 + static void __init test_zero_nbits(void) 1430 + { 1431 + static volatile __always_used unsigned long ret __initdata; 1432 + 1433 + bitmap_clear(NULL, 0, 0); 1434 + bitmap_complement(NULL, NULL, 0); 1435 + bitmap_copy(NULL, NULL, 0); 1436 + bitmap_copy_clear_tail(NULL, NULL, 0); 1437 + bitmap_fill(NULL, 0); 1438 + bitmap_from_arr32(NULL, NULL, 0); 1439 + bitmap_from_arr64(NULL, NULL, 0); 1440 + bitmap_or(NULL, NULL, NULL, 0); 1441 + bitmap_set(NULL, 0, 0); 1442 + bitmap_shift_left(NULL, NULL, 0, 0); 1443 + bitmap_shift_right(NULL, NULL, 0, 0); 1444 + bitmap_to_arr32(NULL, NULL, 0); 1445 + bitmap_to_arr64(NULL, NULL, 0); 1446 + bitmap_write(NULL, 0, 0, 0); 1447 + bitmap_xor(NULL, NULL, NULL, 0); 1448 + bitmap_zero(NULL, 0); 1449 + 1450 + ret = bitmap_and(NULL, NULL, NULL, 0); 1451 + ret = bitmap_empty(NULL, 0); 1452 + ret = bitmap_equal(NULL, NULL, 0); 1453 + ret = bitmap_full(NULL, 0); 1454 + ret = bitmap_or_equal(NULL, NULL, NULL, 0); 1455 + ret = bitmap_read(NULL, 0, 0); 1456 + ret = bitmap_subset(NULL, NULL, 0); 1457 + ret = bitmap_weight(NULL, 0); 1458 + ret = bitmap_weight_and(NULL, NULL, 0); 1459 + ret = bitmap_weight_andnot(NULL, NULL, 0); 1460 + ret = bitmap_weight_from(NULL, 0, 0); 1461 + ret = bitmap_weighted_or(NULL, NULL, NULL, 0); 1462 + 1463 + ret = find_first_and_and_bit(NULL, NULL, NULL, 0); 1464 + ret = find_first_and_bit(NULL, NULL, 0); 1465 + ret = find_first_andnot_bit(NULL, NULL, 0); 1466 + ret = find_first_bit(NULL, 0); 1467 + ret = find_first_zero_bit(NULL, 0); 1468 + ret = find_last_bit(NULL, 0); 1469 + ret = find_next_and_bit(NULL, NULL, 0, 0); 1470 + ret = find_next_andnot_bit(NULL, NULL, 0, 0); 1471 + ret = find_next_bit(NULL, 0, 0); 1472 + ret = find_next_clump8(NULL, NULL, 0, 0); 1473 + ret = find_next_zero_bit(NULL, 0, 0); 1474 + ret = find_nth_and_bit(NULL, NULL, 0, 0); 1475 + ret = find_nth_bit(NULL, 0, 0); 1476 + ret = find_random_bit(NULL, 0); 1468 1477 } 1469 1478 1470 1479 #undef TEST_BIT_LEN ··· 1544 1441 test_bitmap_const_eval(); 1545 1442 test_bitmap_read_write(); 1546 1443 test_bitmap_read_perf(); 1444 + test_bitmap_weight(); 1547 1445 test_bitmap_write_perf(); 1446 + test_zero_nbits(); 1548 1447 1549 1448 test_find_nth_bit(); 1550 1449 test_for_each_set_bit();