* [PATCH 1/2] bitmap: add bitmap_weight_from()
2025-07-20 1:41 [PATCH 0/2] bitmap: introduce bitmap_weight_from() Yury Norov
@ 2025-07-20 1:41 ` Yury Norov
2025-07-20 10:44 ` Thomas Gleixner
2025-07-20 1:41 ` [PATCH 2/2] x86: topology: simplify topo_unit_count() Yury Norov
2025-07-21 15:30 ` [PATCH 0/2] bitmap: introduce bitmap_weight_from() Dave Hansen
2 siblings, 1 reply; 5+ messages in thread
From: Yury Norov @ 2025-07-20 1:41 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
H. Peter Anvin, Rasmus Villemoes, Andrew Morton,
Peter Zijlstra (Intel),
Ilpo Järvinen, Fernando Fernandez Mancera, Xin Li (Intel),
x86, linux-kernel
Cc: Yury Norov
From: Yury Norov (NVIDIA) <yury.norov@gmail.com>
bitmap_weight_from is useful in topo_unit_count() and potentially
more spots.
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
include/linux/bitmap.h | 11 +++++++++++
lib/bitmap.c | 28 ++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 595217b7a6e7..3cde3bd766b7 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -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)
diff --git a/lib/bitmap.c b/lib/bitmap.c
index b97692854966..eb9905071e3b 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -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)
{
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 2/2] x86: topology: simplify topo_unit_count()
2025-07-20 1:41 [PATCH 0/2] bitmap: introduce bitmap_weight_from() Yury Norov
2025-07-20 1:41 ` [PATCH 1/2] bitmap: add bitmap_weight_from() Yury Norov
@ 2025-07-20 1:41 ` Yury Norov
2025-07-21 15:30 ` [PATCH 0/2] bitmap: introduce bitmap_weight_from() Dave Hansen
2 siblings, 0 replies; 5+ messages in thread
From: Yury Norov @ 2025-07-20 1:41 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
H. Peter Anvin, Rasmus Villemoes, Andrew Morton,
Peter Zijlstra (Intel),
Ilpo Järvinen, Fernando Fernandez Mancera, Xin Li (Intel),
x86, linux-kernel
Cc: Yury Norov
From: Yury Norov (NVIDIA) <yury.norov@gmail.com>
Switch the function to using bitmap_weight_from(), and make it (almost)
a one-liner.
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
arch/x86/kernel/cpu/topology.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/arch/x86/kernel/cpu/topology.c b/arch/x86/kernel/cpu/topology.c
index e35ccdc84910..db19c5384f92 100644
--- a/arch/x86/kernel/cpu/topology.c
+++ b/arch/x86/kernel/cpu/topology.c
@@ -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)
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 0/2] bitmap: introduce bitmap_weight_from()
2025-07-20 1:41 [PATCH 0/2] bitmap: introduce bitmap_weight_from() Yury Norov
2025-07-20 1:41 ` [PATCH 1/2] bitmap: add bitmap_weight_from() Yury Norov
2025-07-20 1:41 ` [PATCH 2/2] x86: topology: simplify topo_unit_count() Yury Norov
@ 2025-07-21 15:30 ` Dave Hansen
2 siblings, 0 replies; 5+ messages in thread
From: Dave Hansen @ 2025-07-21 15:30 UTC (permalink / raw)
To: Yury Norov, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin, Rasmus Villemoes, Andrew Morton,
Peter Zijlstra (Intel),
Ilpo Järvinen, Fernando Fernandez Mancera, Xin Li (Intel),
x86, linux-kernel
On 7/19/25 18:41, Yury Norov wrote:
> 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.
^ permalink raw reply [flat|nested] 5+ messages in thread