From: Thomas Gleixner <tglx@linutronix.de>
To: "Yury Norov" <yury.norov@gmail.com>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Fernando Fernandez Mancera" <ffmancera@riseup.net>,
"Xin Li (Intel)" <xin@zytor.com>,
x86@kernel.org, linux-kernel@vger.kernel.org
Cc: Yury Norov <yury.norov@gmail.com>
Subject: Re: [PATCH 1/2] bitmap: add bitmap_weight_from()
Date: Sun, 20 Jul 2025 12:44:19 +0200 [thread overview]
Message-ID: <87cy9vuoos.ffs@tglx> (raw)
In-Reply-To: <20250720014146.432316-2-yury.norov@gmail.com>
On Sat, Jul 19 2025 at 21:41, Yury Norov wrote:
>
> +#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.
> + for (++idx; idx < __bits / BITS_PER_LONG; idx++) \
> + w += hweight_long(FETCH); \
Evaluates to false
> + if (__bits % BITS_PER_LONG) \
Evaluates to true.
> + 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
next prev parent reply other threads:[~2025-07-20 10:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 10:44 ` Thomas Gleixner [this message]
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
-- strict thread matches above, loose matches on Subject: below --
2025-12-14 23:54 [PATCH 0/2] x86/topology: add bitmap_weight_from() and use it in topo_unit_count() Yury Norov (NVIDIA)
2025-12-14 23:54 ` [PATCH 1/2] bitmap: add bitmap_weight_from() Yury Norov (NVIDIA)
2025-12-15 6:59 ` Ingo Molnar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87cy9vuoos.ffs@tglx \
--to=tglx@linutronix.de \
--cc=akpm@linux-foundation.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=ffmancera@riseup.net \
--cc=hpa@zytor.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox