Skip to contentSkip to conversation

bitmap: some fixes

PATCH In review Wei Wang 2 participants 6 messages Jul 30, 2018 – Jul 31, 2018

lore
Share & tools
lore

Wei Wang

author
#0 lore raw

cover bitmap: some fixes

"nbits = 0" is not applicable to BITMAP_LAST_WORD_MASK, callers should avoid that. Fix bitmap_count_one to avoid passing 0 to the macro.

Wei Wang (2):
bitmap.h: add comments to BITMAP_LAST_WORD_MASK
bitmap: fix bitmap_count_one

 include/qemu/bitmap.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/2 bitmap.h: add comments to BITMAP_LAST_WORD_MASK

This macro was ported from Linux and we've reached an aggreement there that the corner case "nbits = 0" is not applicable to this macro, because when "nbits = 0", which means no bits to mask, this macro is expected to return 0, instead of 0xffffffff. This patch simply adds a comment above the macro as a note to users about the corner case.

CC Dr. David Alan Gilbert <[email protected]>
CC Juan Quintela <[email protected]>
CC Peter Xu <[email protected]>
---
 include/qemu/bitmap.h | 1 +
 1 file changed, 1 insertion(+)
include/qemu/bitmap.h +1 -0
index 509eedd..f53c640 100644
@@ -60,6 +60,7 @@
  */
 
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
+/* "nbits = 0" is not applicable to this macro. Callers should avoid that. */
 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
 
 #define DECLARE_BITMAP(name,bits)                  \
Signature · 1 line
2.7.4
author
#5 lore raw

PATCH 2/2 bitmap: fix bitmap_count_one

In reply to Wei Wang

Since "nbits = 0" is not applicable to the BITMAP_LAST_WORD_MASK macro, callers need to avoid passing "nbits = 0" to this macro, which generates incorrect results.

CC Dr. David Alan Gilbert <[email protected]>
CC Juan Quintela <[email protected]>
CC Peter Xu <[email protected]>
---
 include/qemu/bitmap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/bitmap.h +1 -1
index f53c640..e45e9c0 100644
@@ -223,7 +223,7 @@ static inline int bitmap_intersects(const unsigned long *src1,
 static inline long bitmap_count_one(const unsigned long *bitmap, long nbits)
 {
     if (small_nbits(nbits)) {
-        return ctpopl(*bitmap & BITMAP_LAST_WORD_MASK(nbits));
+        return nbits ? ctpopl(*bitmap & BITMAP_LAST_WORD_MASK(nbits)) : 0;
     } else {
         return slow_bitmap_count_one(bitmap, nbits);
     }
Signature · 1 line
2.7.4
author
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] bitmap.h: add comments to BITMAP_LAST_WORD_MASK

In reply to Wei Wang
Quoted 19 lines · Wei Wang

This macro was ported from Linux and we've reached an aggreement there that the corner case "nbits = 0" is not applicable to this macro, because when "nbits = 0", which means no bits to mask, this macro is expected to return 0, instead of 0xffffffff. This patch simply adds a comment above the macro as a note to users about the corner case.

CC Dr. David Alan Gilbert <[email protected]>
CC Juan Quintela <[email protected]>
CC Peter Xu <[email protected]>
---
  include/qemu/bitmap.h | 1 +
  1 file changed, 1 insertion(+)
include/qemu/bitmap.h +0 -0
index 509eedd..f53c640 100644

include/qemu/bitmap.h line 66

Quoted 7 lines
include/qemu/bitmap.h +1 -0
@@ -60,6 +60,7 @@
   */
 
  #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
+/* "nbits = 0" is not applicable to this macro. Callers should avoid that. */
  #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
 
  #define DECLARE_BITMAP(name,bits)                  \

A better fix would be to directly change the macro to: nbits ? (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) : 0, so that we don't need to fix other callers like bitmap_full, bitmap_intersects.

So just post this out for a discussion whether it's preferred to just adding note comments as we did for linux or fixing the macro directly.

Best,
Wei

#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] bitmap.h: add comments to BITMAP_LAST_WORD_MASK

Quoted 29 lines · Wei Wang
Quoted 19 lines · Wei Wang

This macro was ported from Linux and we've reached an aggreement there that the corner case "nbits = 0" is not applicable to this macro, because when "nbits = 0", which means no bits to mask, this macro is expected to return 0, instead of 0xffffffff. This patch simply adds a comment above the macro as a note to users about the corner case.

CC Dr. David Alan Gilbert <[email protected]>
CC Juan Quintela <[email protected]>
CC Peter Xu <[email protected]>
---
  include/qemu/bitmap.h | 1 +
  1 file changed, 1 insertion(+)
include/qemu/bitmap.h +0 -0
index 509eedd..f53c640 100644

include/qemu/bitmap.h line 65

Quoted 6 lines
include/qemu/bitmap.h +1 -0
@@ -60,6 +60,7 @@
   */
    #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) &
 (BITS_PER_LONG - 1)))
+/* "nbits = 0" is not applicable to this macro. Callers should avoid that. */
  #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
    #define DECLARE_BITMAP(name,bits)                  \

A better fix would be to directly change the macro to: nbits ? (~0UL

include/qemu/bitmap.h line 66

Quoted 1 line
include/qemu/bitmap.h +0 -0
 > (-(nbits) & (BITS_PER_LONG - 1))) : 0,
Quoted 8 lines

so that we don't need to fix other callers like bitmap_full, bitmap_intersects.

So just post this out for a discussion whether it's preferred to just adding note comments as we did for linux or fixing the macro directly.

Best,
Wei

On one hand:
a - we have copied it form linux, we don't want to diverge On the other hand:
b - it is much easier to use if we change the macro

So, it is a tought one.

I slightly preffer b), but will not object to a either. As you are the one doing the patch, your choice.

Later, Juan.

author
#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] bitmap.h: add comments to BITMAP_LAST_WORD_MASK

Quoted 42 lines · Juan Quintela
Quoted 28 lines · Wei Wang
Quoted 19 lines · Wei Wang

This macro was ported from Linux and we've reached an aggreement there that the corner case "nbits = 0" is not applicable to this macro, because when "nbits = 0", which means no bits to mask, this macro is expected to return 0, instead of 0xffffffff. This patch simply adds a comment above the macro as a note to users about the corner case.

CC Dr. David Alan Gilbert <[email protected]>
CC Juan Quintela <[email protected]>
CC Peter Xu <[email protected]>
---
   include/qemu/bitmap.h | 1 +
   1 file changed, 1 insertion(+)
include/qemu/bitmap.h +0 -0
index 509eedd..f53c640 100644

include/qemu/bitmap.h line 65

Quoted 6 lines
include/qemu/bitmap.h +1 -0
@@ -60,6 +60,7 @@
    */
     #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) &
 (BITS_PER_LONG - 1)))
+/* "nbits = 0" is not applicable to this macro. Callers should avoid that. */
   #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
     #define DECLARE_BITMAP(name,bits)                  \

A better fix would be to directly change the macro to: nbits ? (~0UL

include/qemu/bitmap.h line 66

Quoted 1 line
include/qemu/bitmap.h +0 -0
 > (-(nbits) & (BITS_PER_LONG - 1))) : 0,
Quoted 8 lines

so that we don't need to fix other callers like bitmap_full, bitmap_intersects.

So just post this out for a discussion whether it's preferred to just adding note comments as we did for linux or fixing the macro directly.

Best,
Wei

On one hand:
a - we have copied it form linux, we don't want to diverge On the other hand:
b - it is much easier to use if we change the macro

Quoted 4 lines

So, it is a tought one.

I slightly preffer b), but will not object to a either. As you are the one doing the patch, your choice.

Thanks Juan. I plan to choose b - fixing the macro directly.

Best,
Wei