public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count()
@ 2025-12-22 19:11 Yury Norov (NVIDIA)
  2025-12-22 19:11 ` [PATCH v2 1/2] bitmap: add bitmap_weight_from() Yury Norov (NVIDIA)
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yury Norov (NVIDIA) @ 2025-12-22 19:11 UTC (permalink / raw)
  To: Ingo Molnar, Thomas Gleixner, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Yury Norov, Rasmus Villemoes, Andrew Morton,
	Peter Zijlstra (Intel), Ilpo Järvinen, Tony Luck,
	Xin Li (Intel), Chang S. Bae, x86, linux-kernel

Currently the function open-codes the missing bitmap_weight_from() with
a for-loop, which is known to be less effective. Add the helper and
switch topo_unit_count() to using it.

v1: https://lore.kernel.org/all/20251214235437.244125-1-yury.norov@gmail.com/
v2:
 - rename 'nbits' to 'end' (Ingo);
 - leverage bitmap_weight() in inline implementation;
 - drop outline implementation;
 - don't handle 'start >= end' case explititly, leave it to user.

Yury Norov (NVIDIA) (2):
  bitmap: add bitmap_weight_from()
  x86/topology: use bitmap_weight_from()

 arch/x86/kernel/cpu/topology.c |  8 ++------
 include/linux/bitmap.h         | 30 ++++++++++++++++++++++++++++++
 lib/test_bitmap.c              | 26 ++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 6 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] bitmap: add bitmap_weight_from()
  2025-12-22 19:11 [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov (NVIDIA)
@ 2025-12-22 19:11 ` Yury Norov (NVIDIA)
  2025-12-22 19:11 ` [PATCH v2 2/2] x86/topology: use bitmap_weight_from() Yury Norov (NVIDIA)
  2026-02-22 17:49 ` [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov
  2 siblings, 0 replies; 5+ messages in thread
From: Yury Norov (NVIDIA) @ 2025-12-22 19:11 UTC (permalink / raw)
  To: Ingo Molnar, Thomas Gleixner, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Yury Norov, Rasmus Villemoes, Andrew Morton,
	Peter Zijlstra (Intel), Ilpo Järvinen, Tony Luck,
	Xin Li (Intel), Chang S. Bae, x86, linux-kernel

The function calculates a Hamming weight of a bitmap starting from an
arbitrary bit.

Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
 include/linux/bitmap.h | 30 ++++++++++++++++++++++++++++++
 lib/test_bitmap.c      | 26 ++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index b0395e4ccf90..fdff6e87512b 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -57,6 +57,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, end)         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
@@ -479,6 +480,35 @@ unsigned long bitmap_weight_andnot(const unsigned long *src1,
 	return __bitmap_weight_andnot(src1, src2, nbits);
 }
 
+/**
+ * bitmap_weight_from - Hamming weight for a memory region
+ * @bitmap: The base address
+ * @start: The bitnumber to starts weighting
+ * @end: the bitmap size in bits
+ *
+ * Returns the number of set bits in the region. If @start >= @end,
+ * the result is undefined.
+ */
+static __always_inline
+unsigned long bitmap_weight_from(const unsigned long *bitmap,
+				   unsigned int start, unsigned int end)
+{
+	unsigned long w;
+
+	if (small_const_nbits(end))
+		return hweight_long(*bitmap & GENMASK(end - 1, start));
+
+	bitmap += start / BITS_PER_LONG;
+	/* Opencode round_down() to not include math.h */
+	end -= start & ~(BITS_PER_LONG - 1);
+	start %= BITS_PER_LONG;
+	w = bitmap_weight(bitmap, end);
+	if (start)
+		w -= hweight_long(*bitmap & BITMAP_LAST_WORD_MASK(start));
+
+	return w;
+}
+
 static __always_inline
 void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits)
 {
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index c83829ef557f..eff0b1eba8e4 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -851,6 +851,31 @@ static void __init test_for_each_set_bit_from(void)
 	}
 }
 
+static void __init test_bitmap_weight(void)
+{
+	unsigned int bit, w1, w2, w;
+	DECLARE_BITMAP(b, 30);
+
+	bitmap_parselist("all:1/2", b, 30);
+
+	/* Test inline implementation */
+	w = bitmap_weight(b, 30);
+	w1 = bitmap_weight(b, 15);
+	w2 = bitmap_weight_from(b, 15, 30);
+
+	expect_eq_uint(15, w);
+	expect_eq_uint(8, w1);
+	expect_eq_uint(7, w2);
+
+	/* Test outline implementation */
+	w = bitmap_weight(exp1, EXP1_IN_BITS);
+	for (bit = 0; bit < EXP1_IN_BITS; bit++) {
+		w1 = bitmap_weight(exp1, bit);
+		w2 = bitmap_weight_from(exp1, bit, EXP1_IN_BITS);
+		expect_eq_uint(w1 + w2, w);
+	}
+}
+
 static void __init test_for_each_clear_bit(void)
 {
 	DECLARE_BITMAP(orig, 500);
@@ -1441,6 +1466,7 @@ static void __init selftest(void)
 	test_bitmap_const_eval();
 	test_bitmap_read_write();
 	test_bitmap_read_perf();
+	test_bitmap_weight();
 	test_bitmap_write_perf();
 
 	test_find_nth_bit();
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] x86/topology: use bitmap_weight_from()
  2025-12-22 19:11 [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov (NVIDIA)
  2025-12-22 19:11 ` [PATCH v2 1/2] bitmap: add bitmap_weight_from() Yury Norov (NVIDIA)
@ 2025-12-22 19:11 ` Yury Norov (NVIDIA)
  2026-02-22 17:49 ` [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov
  2 siblings, 0 replies; 5+ messages in thread
From: Yury Norov (NVIDIA) @ 2025-12-22 19:11 UTC (permalink / raw)
  To: Ingo Molnar, Thomas Gleixner, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Yury Norov, Rasmus Villemoes, Andrew Morton,
	Peter Zijlstra (Intel), Ilpo Järvinen, Tony Luck,
	Xin Li (Intel), Chang S. Bae, x86, linux-kernel

Switch topo_unit_count() to use bitmap_weight_from().

Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
 arch/x86/kernel/cpu/topology.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/cpu/topology.c b/arch/x86/kernel/cpu/topology.c
index f55ea3cdbf88..ba7142f417ad 100644
--- a/arch/x86/kernel/cpu/topology.c
+++ b/arch/x86/kernel/cpu/topology.c
@@ -204,15 +204,11 @@ 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;
+	unsigned int end;
 
 	/* Calculate the exclusive end */
 	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 related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count()
  2025-12-22 19:11 [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov (NVIDIA)
  2025-12-22 19:11 ` [PATCH v2 1/2] bitmap: add bitmap_weight_from() Yury Norov (NVIDIA)
  2025-12-22 19:11 ` [PATCH v2 2/2] x86/topology: use bitmap_weight_from() Yury Norov (NVIDIA)
@ 2026-02-22 17:49 ` Yury Norov
  2026-03-13 22:42   ` Yury Norov
  2 siblings, 1 reply; 5+ messages in thread
From: Yury Norov @ 2026-02-22 17:49 UTC (permalink / raw)
  To: Yury Norov (NVIDIA)
  Cc: Ingo Molnar, Thomas Gleixner, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Rasmus Villemoes, Andrew Morton,
	Peter Zijlstra (Intel), Ilpo Järvinen, Tony Luck,
	Xin Li (Intel), Chang S. Bae, x86, linux-kernel

Ping?

On Mon, Dec 22, 2025 at 02:11:36PM -0500, Yury Norov (NVIDIA) wrote:
> Currently the function open-codes the missing bitmap_weight_from() with
> a for-loop, which is known to be less effective. Add the helper and
> switch topo_unit_count() to using it.
> 
> v1: https://lore.kernel.org/all/20251214235437.244125-1-yury.norov@gmail.com/
> v2:
>  - rename 'nbits' to 'end' (Ingo);
>  - leverage bitmap_weight() in inline implementation;
>  - drop outline implementation;
>  - don't handle 'start >= end' case explititly, leave it to user.
> 
> Yury Norov (NVIDIA) (2):
>   bitmap: add bitmap_weight_from()
>   x86/topology: use bitmap_weight_from()
> 
>  arch/x86/kernel/cpu/topology.c |  8 ++------
>  include/linux/bitmap.h         | 30 ++++++++++++++++++++++++++++++
>  lib/test_bitmap.c              | 26 ++++++++++++++++++++++++++
>  3 files changed, 58 insertions(+), 6 deletions(-)
> 
> -- 
> 2.43.0

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count()
  2026-02-22 17:49 ` [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov
@ 2026-03-13 22:42   ` Yury Norov
  0 siblings, 0 replies; 5+ messages in thread
From: Yury Norov @ 2026-03-13 22:42 UTC (permalink / raw)
  To: Yury Norov (NVIDIA)
  Cc: Ingo Molnar, Thomas Gleixner, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Rasmus Villemoes, Andrew Morton,
	Peter Zijlstra (Intel), Ilpo Järvinen, Tony Luck,
	Xin Li (Intel), Chang S. Bae, x86, linux-kernel

On Sun, Feb 22, 2026 at 12:49:51PM -0500, Yury Norov wrote:
> Ping?

OK, if no objections, I'm taking it in bitmap-for-next.

Thanks,
Yury
 
> On Mon, Dec 22, 2025 at 02:11:36PM -0500, Yury Norov (NVIDIA) wrote:
> > Currently the function open-codes the missing bitmap_weight_from() with
> > a for-loop, which is known to be less effective. Add the helper and
> > switch topo_unit_count() to using it.
> > 
> > v1: https://lore.kernel.org/all/20251214235437.244125-1-yury.norov@gmail.com/
> > v2:
> >  - rename 'nbits' to 'end' (Ingo);
> >  - leverage bitmap_weight() in inline implementation;
> >  - drop outline implementation;
> >  - don't handle 'start >= end' case explititly, leave it to user.
> > 
> > Yury Norov (NVIDIA) (2):
> >   bitmap: add bitmap_weight_from()
> >   x86/topology: use bitmap_weight_from()
> > 
> >  arch/x86/kernel/cpu/topology.c |  8 ++------
> >  include/linux/bitmap.h         | 30 ++++++++++++++++++++++++++++++
> >  lib/test_bitmap.c              | 26 ++++++++++++++++++++++++++
> >  3 files changed, 58 insertions(+), 6 deletions(-)
> > 
> > -- 
> > 2.43.0

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-13 22:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-22 19:11 [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov (NVIDIA)
2025-12-22 19:11 ` [PATCH v2 1/2] bitmap: add bitmap_weight_from() Yury Norov (NVIDIA)
2025-12-22 19:11 ` [PATCH v2 2/2] x86/topology: use bitmap_weight_from() Yury Norov (NVIDIA)
2026-02-22 17:49 ` [PATCH v2 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov
2026-03-13 22:42   ` Yury Norov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox