From: Wei Wang <wei.w.wang@intel.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Yury Norov <ynorov@caviumnetworks.com>
Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
corbet@lwn.net, dgilbert@redhat.com,
Andy Shevchenko <andy.shevchenko@gmail.com>
Subject: Re: [PATCH] linux/bitmap.h: fix BITMAP_LAST_WORD_MASK
Date: Tue, 07 Aug 2018 15:15:18 +0800 [thread overview]
Message-ID: <5B694706.9080404@intel.com> (raw)
In-Reply-To: <5B69445D.1000107@intel.com>
On 08/07/2018 03:03 PM, Wei Wang wrote:
> On 08/07/2018 07:30 AM, Rasmus Villemoes wrote:
>> On 2018-07-26 12:15, Wei Wang wrote:
>>> On 07/26/2018 05:37 PM, Yury Norov wrote:
>>>> On Thu, Jul 26, 2018 at 04:07:51PM +0800, Wei Wang wrote:
>>>>> The existing BITMAP_LAST_WORD_MASK macro returns 0xffffffff if
>>>>> nbits is
>>>>> 0. This patch changes the macro to return 0 when there is no bit
>>>>> needs to
>>>>> be masked.
>>>> I think this is intentional behavour. Previous version did return ~0UL
>>>> explicitly in this case. See patch 89c1e79eb3023 (linux/bitmap.h:
>>>> improve
>>>> BITMAP_{LAST,FIRST}_WORD_MASK) from Rasmus.
>>> Yes, I saw that. But it seems confusing for the corner case that
>>> nbits=0
>>> (no bits to mask), the macro returns with all the bits set.
>>>
>>>
>>>> Introducing conditional branch would affect performance. All existing
>>>> code checks nbits for 0 before handling last word where needed
>>>> explicitly. So I think we'd better change nothing here.
>>> I think that didn't save the conditional branch essentially, because
>>> it's just moved from inside this macro to the caller as you mentioned.
>>> If callers missed the check for some reason and passed 0 to the macro,
>>> they will get something unexpected.
>>>
>>> Current callers like __bitmap_weight, __bitmap_equal, and others,
>>> they have
>>>
>>> if (bits % BITS_PER_LONG)
>>> w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
>>>
>>> we could remove the "if" check by "w += hweight_long(bitmap[k] &
>>> BITMAP_LAST_WORD_MASK(bits % BITS_PER_LONG));" the branch is the same.
>> Absolutely not! That would access bitmap[lim] (the final value of the k
>> variable) despite that word not being part of the bitmap.
>
> Probably it's more clear to post the entire function here for a
> discussion:
>
> int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
> {
> unsigned int k, lim = bits/BITS_PER_LONG;
> int w = 0;
>
> for (k = 0; k < lim; k++)
> w += hweight_long(bitmap[k]);
>
> if (bits % BITS_PER_LONG)
> ==> w += hweight_long(bitmap[k] &
> BITMAP_LAST_WORD_MASK(bits));
>
> return w;
> }
>
> When the execution reaches "==>", isn't "k=lim"?
And accessing to bitmap[lim] which does not exist should be a case
considered by the caller rather than the macro. For example, with
"BITMAP_LAST_WORD_MASK(bits) & bitmap[k]", making
BITMAP_LAST_WORD_MASK(0) be 0 will not be a problem.
Anyway, my point is that we could make the macro itself robust.
Best,
Wei
next prev parent reply other threads:[~2018-08-07 7:10 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-26 8:07 [PATCH] linux/bitmap.h: fix BITMAP_LAST_WORD_MASK Wei Wang
2018-07-26 8:48 ` Andy Shevchenko
2018-07-26 10:08 ` Wei Wang
2018-07-26 14:00 ` Andy Shevchenko
2018-07-26 9:37 ` Yury Norov
2018-07-26 10:15 ` Wei Wang
2018-07-26 12:10 ` Yury Norov
2018-07-27 2:13 ` Wei Wang
2018-08-06 23:30 ` Rasmus Villemoes
2018-08-07 7:03 ` Wei Wang
2018-08-07 7:15 ` Wei Wang [this message]
2018-08-07 10:26 ` Andy Shevchenko
2018-08-07 11:22 ` Wei Wang
2018-08-14 12:46 ` Andy Shevchenko
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=5B694706.9080404@intel.com \
--to=wei.w.wang@intel.com \
--cc=akpm@linux-foundation.org \
--cc=andy.shevchenko@gmail.com \
--cc=corbet@lwn.net \
--cc=dgilbert@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=ynorov@caviumnetworks.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