Skip to contentSkip to conversation

bitmap: fix BITMAP_LAST_WORD_MASK

PATCH In review Wei Wang 4 participants 9 messages Jul 31, 2018 – Aug 8, 2018

lore
Share & tools
lore

Wei Wang

author
#0 lore raw

PATCH v2 1/1 bitmap: fix BITMAP_LAST_WORD_MASK

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>
---
 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.

include/qemu/bitmap.h +4 -1
index 509eedd..9372423 100644
@@ -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)]
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#2 lore raw
In reply to Wei Wang
Quoted 8 lines · Wei Wang

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>

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

Quoted 26 lines
---
 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.

include/qemu/bitmap.h +4 -1
index 509eedd..9372423 100644
@@ -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)]
Signature · 1 line
2.7.4

Regards,

Signature · 1 line
Peter Xu
author
#3 lore raw
Quoted 5 lines · Peter Xu
Quoted 4 lines · Wei Wang

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.

Quoted 7 lines
Quoted 4 lines
CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>

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

#4 lore raw
Quoted 10 lines · * Wei Wang ([email protected]) wrote:
Quoted 9 lines · Peter Xu
Quoted 8 lines · Wei Wang

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>
Quoted 8 lines
Quoted 3 lines

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

Quoted 2 lines

Best,
Wei

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

author
#5 lore raw
Quoted 13 lines · Dr. David Alan Gilbert
Quoted 12 lines · * Wei Wang ([email protected]) wrote:
Quoted 11 lines · Peter Xu
Quoted 8 lines · Wei Wang

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

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

#6 lore raw
Quoted 22 lines · * Wei Wang ([email protected]) wrote:
Quoted 13 lines · Dr. David Alan Gilbert
Quoted 12 lines · * Wei Wang ([email protected]) wrote:
Quoted 11 lines · Peter Xu
Quoted 8 lines · Wei Wang

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

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.

Quoted 7 lines

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

Quoted 2 lines

Best,
Wei

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#7 lore raw
In reply to Wei Wang
Quoted 10 lines · Wei Wang
Quoted 9 lines · Peter Xu
Quoted 8 lines · Wei Wang

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>
Quoted 8 lines
Quoted 3 lines

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,

Signature · 1 line
Peter Xu
author
#8 lore raw
Quoted 13 lines · Peter Xu
Quoted 12 lines · Wei Wang
Quoted 11 lines · Peter Xu
Quoted 8 lines · Wei Wang

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.

CC Juan Quintela <[email protected]>
CC Dr. David Alan Gilbert <[email protected]>
CC Peter Xu <[email protected]>
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

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