diff mbox series

[v2] bitmap: fix BITMAP_LAST_WORD_MASK

Message ID 1533031278-5615-1-git-send-email-wei.w.wang@intel.com (mailing list archive)
State New, archived
Headers show
Series [v2] bitmap: fix BITMAP_LAST_WORD_MASK | expand

Commit Message

Wang, Wei W July 31, 2018, 10:01 a.m. UTC
When "nbits = 0", which means no bits to mask, this macro is expected to
return 0, instead of 0xffffffff. This patch changes the macro to return
0 when there is no bit needs to be masked.

Signed-off-by: Wei Wang <wei.w.wang@intel.com>
CC: Juan Quintela <quintela@redhat.com>
CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
CC: Peter Xu <peterx@redhat.com>
---
 include/qemu/bitmap.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

v1->v2 ChangeLog:
- fix the macro directly, instead of fixing the callers one by one.

Comments

Juan Quintela July 31, 2018, 2:44 p.m. UTC | #1
Wei Wang <wei.w.wang@intel.com> wrote:
> When "nbits = 0", which means no bits to mask, this macro is expected to
> return 0, instead of 0xffffffff. This patch changes the macro to return
> 0 when there is no bit needs to be masked.
>
> Signed-off-by: Wei Wang <wei.w.wang@intel.com>
> CC: Juan Quintela <quintela@redhat.com>
> CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
> CC: Peter Xu <peterx@redhat.com>

Reviewed-by: Juan Quintela <quintela@redhat.com>
Peter Xu Aug. 7, 2018, 7:39 a.m. UTC | #2
On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
> When "nbits = 0", which means no bits to mask, this macro is expected to
> return 0, instead of 0xffffffff. This patch changes the macro to return
> 0 when there is no bit needs to be masked.
> 
> Signed-off-by: Wei Wang <wei.w.wang@intel.com>
> CC: Juan Quintela <quintela@redhat.com>
> CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
> CC: Peter Xu <peterx@redhat.com>

Reviewed-by: Peter Xu <peterx@redhat.com>

Is there any existing path that can trigger this nbits==0?

> ---
>  include/qemu/bitmap.h | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> v1->v2 ChangeLog:
> - fix the macro directly, instead of fixing the callers one by one.
> 
> diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
> index 509eedd..9372423 100644
> --- a/include/qemu/bitmap.h
> +++ b/include/qemu/bitmap.h
> @@ -60,7 +60,10 @@
>   */
>  
>  #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
> -#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
> +#define BITMAP_LAST_WORD_MASK(nbits)                       \
> +(                                                          \
> +    nbits ? (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) : 0 \
> +)
>  
>  #define DECLARE_BITMAP(name,bits)                  \
>          unsigned long name[BITS_TO_LONGS(bits)]
> -- 
> 2.7.4
> 

Regards,
Wang, Wei W Aug. 7, 2018, 8:21 a.m. UTC | #3
On 08/07/2018 03:39 PM, Peter Xu wrote:
> On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
>> When "nbits = 0", which means no bits to mask, this macro is expected to
>> return 0, instead of 0xffffffff. This patch changes the macro to return
>> 0 when there is no bit needs to be masked.
>>
>> Signed-off-by: Wei Wang <wei.w.wang@intel.com>
>> CC: Juan Quintela <quintela@redhat.com>
>> CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
>> CC: Peter Xu <peterx@redhat.com>
> Reviewed-by: Peter Xu <peterx@redhat.com>
>
> Is there any existing path that can trigger this nbits==0?

Not sure about other bitmap APIs which call this macro. But it happens 
in the patches we are working on, which use bitmap_count_one.
It would be good to have the macro itself handle this corner case, so 
that callers won't need to worry about that.

Best,
Wei
Dr. David Alan Gilbert Aug. 7, 2018, 9:53 a.m. UTC | #4
* Wei Wang (wei.w.wang@intel.com) wrote:
> On 08/07/2018 03:39 PM, Peter Xu wrote:
> > On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
> > > When "nbits = 0", which means no bits to mask, this macro is expected to
> > > return 0, instead of 0xffffffff. This patch changes the macro to return
> > > 0 when there is no bit needs to be masked.
> > > 
> > > Signed-off-by: Wei Wang <wei.w.wang@intel.com>
> > > CC: Juan Quintela <quintela@redhat.com>
> > > CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > > CC: Peter Xu <peterx@redhat.com>
> > Reviewed-by: Peter Xu <peterx@redhat.com>
> > 
> > Is there any existing path that can trigger this nbits==0?
> 
> Not sure about other bitmap APIs which call this macro. But it happens in
> the patches we are working on, which use bitmap_count_one.
> It would be good to have the macro itself handle this corner case, so that
> callers won't need to worry about that.

Given that I see you're having a similar discussion on the kernel list
we should see how that pans out before making qemu changes.

Dave

> Best,
> Wei
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Peter Xu Aug. 7, 2018, 12:17 p.m. UTC | #5
On Tue, Aug 07, 2018 at 04:21:15PM +0800, Wei Wang wrote:
> On 08/07/2018 03:39 PM, Peter Xu wrote:
> > On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
> > > When "nbits = 0", which means no bits to mask, this macro is expected to
> > > return 0, instead of 0xffffffff. This patch changes the macro to return
> > > 0 when there is no bit needs to be masked.
> > > 
> > > Signed-off-by: Wei Wang <wei.w.wang@intel.com>
> > > CC: Juan Quintela <quintela@redhat.com>
> > > CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > > CC: Peter Xu <peterx@redhat.com>
> > Reviewed-by: Peter Xu <peterx@redhat.com>
> > 
> > Is there any existing path that can trigger this nbits==0?
> 
> Not sure about other bitmap APIs which call this macro. But it happens in
> the patches we are working on, which use bitmap_count_one.
> It would be good to have the macro itself handle this corner case, so that
> callers won't need to worry about that.

Yeah that makes sense.  Asked since that would matter on whether it's
3.0 material, then it's possibly not.

Regards,
Wang, Wei W Aug. 8, 2018, 1:30 a.m. UTC | #6
On 08/07/2018 08:17 PM, Peter Xu wrote:
> On Tue, Aug 07, 2018 at 04:21:15PM +0800, Wei Wang wrote:
>> On 08/07/2018 03:39 PM, Peter Xu wrote:
>>> On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
>>>> When "nbits = 0", which means no bits to mask, this macro is expected to
>>>> return 0, instead of 0xffffffff. This patch changes the macro to return
>>>> 0 when there is no bit needs to be masked.
>>>>
>>>> Signed-off-by: Wei Wang <wei.w.wang@intel.com>
>>>> CC: Juan Quintela <quintela@redhat.com>
>>>> CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
>>>> CC: Peter Xu <peterx@redhat.com>
>>> Reviewed-by: Peter Xu <peterx@redhat.com>
>>>
>>> Is there any existing path that can trigger this nbits==0?
>> Not sure about other bitmap APIs which call this macro. But it happens in
>> the patches we are working on, which use bitmap_count_one.
>> It would be good to have the macro itself handle this corner case, so that
>> callers won't need to worry about that.
> Yeah that makes sense.  Asked since that would matter on whether it's
> 3.0 material, then it's possibly not.
>

OK, thanks for checking.

Best,
Wei
Wang, Wei W Aug. 8, 2018, 1:34 a.m. UTC | #7
On 08/07/2018 05:53 PM, Dr. David Alan Gilbert wrote:
> * Wei Wang (wei.w.wang@intel.com) wrote:
>> On 08/07/2018 03:39 PM, Peter Xu wrote:
>>> On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
>>>> When "nbits = 0", which means no bits to mask, this macro is expected to
>>>> return 0, instead of 0xffffffff. This patch changes the macro to return
>>>> 0 when there is no bit needs to be masked.
>>>>
>>>> Signed-off-by: Wei Wang <wei.w.wang@intel.com>
>>>> CC: Juan Quintela <quintela@redhat.com>
>>>> CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
>>>> CC: Peter Xu <peterx@redhat.com>
>>> Reviewed-by: Peter Xu <peterx@redhat.com>
>>>
>>> Is there any existing path that can trigger this nbits==0?
>> Not sure about other bitmap APIs which call this macro. But it happens in
>> the patches we are working on, which use bitmap_count_one.
>> It would be good to have the macro itself handle this corner case, so that
>> callers won't need to worry about that.
> Given that I see you're having a similar discussion on the kernel list
> we should see how that pans out before making qemu changes.

OK.
The situation is a little different in Linux, because all the callers 
there have already taken the responsibilities to avoid the "nbits=0" 
corner case, that's also the reason that they want to stick with the old 
way. Here in QEMU, most callers (e.g. bitmap_count_one, bitmap_fill, 
bitmap_intersects) haven't checked that, so fixing the macro itself 
might be a better choice here.

Best,
Wei
Dr. David Alan Gilbert Aug. 8, 2018, 8:29 a.m. UTC | #8
* Wei Wang (wei.w.wang@intel.com) wrote:
> On 08/07/2018 05:53 PM, Dr. David Alan Gilbert wrote:
> > * Wei Wang (wei.w.wang@intel.com) wrote:
> > > On 08/07/2018 03:39 PM, Peter Xu wrote:
> > > > On Tue, Jul 31, 2018 at 06:01:18PM +0800, Wei Wang wrote:
> > > > > When "nbits = 0", which means no bits to mask, this macro is expected to
> > > > > return 0, instead of 0xffffffff. This patch changes the macro to return
> > > > > 0 when there is no bit needs to be masked.
> > > > > 
> > > > > Signed-off-by: Wei Wang <wei.w.wang@intel.com>
> > > > > CC: Juan Quintela <quintela@redhat.com>
> > > > > CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
> > > > > CC: Peter Xu <peterx@redhat.com>
> > > > Reviewed-by: Peter Xu <peterx@redhat.com>
> > > > 
> > > > Is there any existing path that can trigger this nbits==0?
> > > Not sure about other bitmap APIs which call this macro. But it happens in
> > > the patches we are working on, which use bitmap_count_one.
> > > It would be good to have the macro itself handle this corner case, so that
> > > callers won't need to worry about that.
> > Given that I see you're having a similar discussion on the kernel list
> > we should see how that pans out before making qemu changes.
> 
> OK.
> The situation is a little different in Linux, because all the callers there
> have already taken the responsibilities to avoid the "nbits=0" corner case,
> that's also the reason that they want to stick with the old way. Here in
> QEMU, most callers (e.g. bitmap_count_one, bitmap_fill, bitmap_intersects)
> haven't checked that, so fixing the macro itself might be a better choice
> here.

Where we have macros or functions that have the same name as the kernel
then we should keep them consistent with the kernel unless we have a
VERY good reason to make them differ;  that's especially true if the
difference is a small subtle difference like this;  otherwise it would
be too easy for someone used to QEMU or the kernel to introduce a bad
mistake in the other one because they think they're using the same
thing.

Dave


> Best,
> Wei
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
diff mbox series

Patch

diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
index 509eedd..9372423 100644
--- a/include/qemu/bitmap.h
+++ b/include/qemu/bitmap.h
@@ -60,7 +60,10 @@ 
  */
 
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
-#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
+#define BITMAP_LAST_WORD_MASK(nbits)                       \
+(                                                          \
+    nbits ? (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) : 0 \
+)
 
 #define DECLARE_BITMAP(name,bits)                  \
         unsigned long name[BITS_TO_LONGS(bits)]