bitmap: introduce bitmap_weight_from()
Subsystems (3)
Yury Norov
maintainercover bitmap: introduce bitmap_weight_from()
From: Yury Norov (NVIDIA) <[email protected]>
Introduce bitmap_weight_from() and use it in topo_unit_count()
Yury Norov (2):
bitmap: introduce bitmap_weight_from()
x86: topology: simplify topo_unit_count()
arch/x86/kernel/cpu/topology.c | 9 ++------- include/linux/bitmap.h | 11 +++++++++++ lib/bitmap.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
Yury Norov
maintainerPATCH 1/2 bitmap: add bitmap_weight_from()
From: Yury Norov (NVIDIA) <[email protected]>
bitmap_weight_from is useful in topo_unit_count() and potentially more spots.
--- include/linux/bitmap.h | 11 +++++++++++ lib/bitmap.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
include/linux/bitmap.h +11 -0
@@ -56,6 +56,7 @@ struct device;* bitmap_weight(src, nbits) Hamming Weight: number set bits * bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap * bitmap_weight_andnot(src1, src2, nbits) Hamming Weight of andnot'ed bitmap +* bitmap_weight_from(src, start, nbits) Hamming Weight starting from @start * bitmap_set(dst, pos, nbits) Set specified bit area * bitmap_clear(dst, pos, nbits) Clear specified bit area * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
@@ -181,6 +182,8 @@ unsigned int __bitmap_weight_and(const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits);unsigned int __bitmap_weight_andnot (const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int nbits); +unsigned int __bitmap_weight_from (const unsigned long *bitmap, +unsigned int start,unsigned int nbits);void __bitmap_set (unsigned long *map,unsigned int start,int len);void __bitmap_clear (unsigned long *map,unsigned int start,int len);
@@ -446,6 +449,14 @@ unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)return __bitmap_weight (src, nbits); } +static __always_inline +unsigned int bitmap_weight_from(const unsigned long *src, unsigned int start, unsigned int nbits) +{ +if (small_const_nbits (start + nbits -1 )) +return hweight_long (*src &GENMASK (start + nbits -1 , start)); +return __bitmap_weight_from (src, start, nbits); +} +static __always_inline unsigned long bitmap_weight_and (const unsigned long *src1,const unsigned long *src2,unsigned int nbits)
lib/bitmap.c +28 -0
@@ -335,12 +335,40 @@ EXPORT_SYMBOL(__bitmap_subset); w; \ }) +#define BITMAP_WEIGHT_FROM (FETCH , start, bits) \ +({ \ + unsigned long __start = (start), __bits = (bits); \ + unsigned int idx, w = 0; \ + \ + if (unlikely(__start >= bits)) \ + goto out; \ + \ + idx = __start / BITS_PER_LONG; \ + w = (FETCH) & BITMAP_FIRST_WORD_MASK(__start); \ + \ + for (++idx; idx < __bits / BITS_PER_LONG; idx++) \ + w += hweight_long(FETCH); \ + \ + if (__bits % BITS_PER_LONG) \ + w += hweight_long((FETCH) & BITMAP_LAST_WORD_MASK(__bits)); \ + \ +out: \ + w; \ +}) +unsigned int __bitmap_weight (const unsigned long *bitmap,unsigned int bits) {return BITMAP_WEIGHT (bitmap[idx], bits); }EXPORT_SYMBOL (__bitmap_weight); +unsigned int __bitmap_weight_from (const unsigned long *bitmap, +unsigned int start,unsigned int bits) +{ +return BITMAP_WEIGHT_FROM (bitmap[idx], start, bits); +} +EXPORT_SYMBOL (__bitmap_weight_from); +unsigned int __bitmap_weight_and (const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int bits) {
Signature · 1 line
2.43.0
Re: [PATCH 1/2] bitmap: add bitmap_weight_from()
lib/bitmap.c EXPORT_SYMBOL(__bitmap_subset); · line 347
Quoted 11 lines · Yury Norov
lib/bitmap.c +10
-0+#define BITMAP_WEIGHT_FROM (FETCH , start, bits) \ +({ \ + unsigned long __start = (start), __bits = (bits); \ + unsigned int idx, w = 0; \ + \ + if (unlikely(__start >= bits)) \ + goto out; \ + \ + idx = __start / BITS_PER_LONG; \ + w = (FETCH) & BITMAP_FIRST_WORD_MASK(__start); \
So this expands to
w = bitmap[idx] & (~0UL << ((start) & (BITS_PER_LONG - 1)));
Which means @w contains the content of the first bitmap word except for the masked off bits. Let's assume @start is 0 and @bits is 32. Therefore @idx is 0.
Assume further bitmap[idx] is all ones, which means 64bits set on a 64bit system. That results in
w = bitmap[0] & (~0UL << ((0) & (BITS_PER_LONG - 1)));
--> w = 0xFFFFFFFFFFFFFFFF & (0xFFFFFFFFFFFFFFFF << (0 & 0x3F));
--> w = 0xFFFFFFFFFFFFFFFF;
which is obviously bogus.
lib/bitmap.c EXPORT_SYMBOL(__bitmap_subset); · line 350
Quoted 2 lines
lib/bitmap.c +2
-0+for (++idx; idx < __bits /BITS_PER_LONG ; idx++) \ + w +=hweight_long (FETCH ); \
Evaluates to false
lib/bitmap.c EXPORT_SYMBOL(__bitmap_subset); · line 352
Quoted 1 line
lib/bitmap.c +1
-0+if (__bits %BITS_PER_LONG ) \
Evaluates to true.
lib/bitmap.c EXPORT_SYMBOL(__bitmap_subset); · line 353
Quoted 1 line
lib/bitmap.c +1
-0+ w +=hweight_long ((FETCH ) &BITMAP_LAST_WORD_MASK (__bits)); \
So this is executed and evaluates to:
w += hweight_long(bitmap[1] & (~0UL >> (-(32UL) & (BITS_PER_LONG - 1))));
Let's assume the second word contains all ones as well.
--> w += hweight_long(0xFFFFFFFFFFFFFFFF & (0xFFFFFFFFFFFFFFFF >> (0xFFFFFFFFFFFFFFE0 & 0x3F))); --> w += hweight_long(0xFFFFFFFFFFFFFFFF & (0xFFFFFFFFFFFFFFFF >> (0x20))); --> w += hweight_long(0xFFFFFFFFFFFFFFFF & 0xFFFFFFFF);
--> w += 32;
Due to the wraparound of the addition it results in
w = 31
which is not making the bogosity above more correct. And no, you can't just fix up the initial assignment to @w:
w = hweight_long((FETCH) & BITMAP_FIRST_WORD_MASK(__start);
because then the result is 32 + 32 == 64 as the final clause is unconditionally executed.
Something like this should work:
unsigned int idx, maxidx, w = 0;
idx = start / BITS_PER_LONG;
w = hweight_long((FETCH) & BITMAP_FIRST_WORD_MASK((unsigned long)start));
maxidx = bits / BITS_PER_LONG;
for (idx++; idx < maxidx; idx++)
w += hweight_long((FETCH));
if (maxidx * BITS_PER_LONG < bits)
w += hweight_long((FETCH) & BITMAP_LAST_WORD_MASK((unsigned long)bits));No?
Thanks,
tglx
Yury Norov
maintainerPATCH 2/2 x86: topology: simplify topo_unit_count()
In reply to Yury NorovFrom: Yury Norov (NVIDIA) <[email protected]>
Switch the function to using bitmap_weight_from(), and make it (almost) a one-liner.
--- arch/x86/kernel/cpu/topology.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/topology.c +2 -7
@@ -208,15 +208,10 @@ static __init bool check_for_real_bsp(u32 apic_id)static unsigned int topo_unit_count (u32 lvlid,enum x86_topology_domains at_level,unsigned long *map) { -unsigned int id, end, cnt =0 ; -/* Calculate the exclusive end */ - end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]); +unsigned int end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]); -/* Unfortunately there is no bitmap_weight_range() */ -for (id =find_next_bit (map, end, lvlid); id < end; id =find_next_bit (map, end, ++id)) - cnt++; -return cnt; +return bitmap_weight_from (map, lvlid, end); }static __init void topo_register_apic (u32 apic_id,u32 acpi_id,bool present)
Signature · 1 line
2.43.0
Quoted 4 lines · Yury Norov
arch/x86/kernel/cpu/topology.c | 9 ++------- include/linux/bitmap.h | 11 +++++++++++ lib/bitmap.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-)
I think we need to hear more about the motivation for doing this, including if there are other sites that might benefit from this helper.