Skip to contentSkip to conversation

bitmap_*() should cope with zero size bitmaps

PATCH In review Jan Beulich 2 participants 4 messages Jun 3, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitmap_*() should cope with zero size bitmaps

... to match expectations set by memset()/memcpy().

Similarly for find_{first,next}_{,zero_}_bit() on x86.

__bitmap_shift_{left,right}() would also need fixing (they more generally can't cope with the shift count being larger than the bitmap size, and they perform undefined operations by possibly shifting an unsigned long value by BITS_PER_LONG bits), but since these functions aren't really used anywhere I wonder if we wouldn't better simply get rid of them.

xen/include/asm-x86/bitops.h +38 -16
@@ -327,10 +327,7 @@ static inline unsigned int __scanbit(uns
  * Returns the bit-number of the first set bit, not the number of the byte
  * containing a bit.
  */
-#define find_first_bit(addr,size)                               \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?       \
-  (__scanbit(*(const unsigned long *)addr, size)) :             \
-  __find_first_bit(addr,size)))
+#define find_first_bit(addr, size) find_next_bit(addr, size, 0)
 
 /**
  * find_next_bit - find the first set bit in a memory region
@@ -338,10 +335,24 @@ static inline unsigned int __scanbit(uns
  * @offset: The bitnumber to start searching at
  * @size: The maximum size to search
  */
-#define find_next_bit(addr,size,off)                                     \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?                \
-  ((off) + (__scanbit((*(const unsigned long *)addr) >> (off), size))) : \
-  __find_next_bit(addr,size,off)))
+#define find_next_bit(addr, size, off) ({ \
+    unsigned int r__ = (size); \
+    unsigned int o__ = (off); \
+    switch ( -!__builtin_constant_p(size) | r__ ) \
+    { \
+    case 0: (void)(addr); break; \
+    case 1 ... BITS_PER_LONG: \
+        r__ = o__ + __scanbit(*(const unsigned long *)(addr) >> o__, r__); \
+        break; \
+    default: \
+        if ( __builtin_constant_p(off) && !o__ ) \
+            r__ = __find_first_bit(addr, r__); \
+        else \
+            r__ = __find_next_bit(addr, r__, o__); \
+        break; \
+    } \
+    r__; \
+})
 
 /**
  * find_first_zero_bit - find the first zero bit in a memory region
@@ -351,10 +362,7 @@ static inline unsigned int __scanbit(uns
  * Returns the bit-number of the first zero bit, not the number of the byte
  * containing a bit.
  */
-#define find_first_zero_bit(addr,size)                          \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?       \
-  (__scanbit(~*(const unsigned long *)addr, size)) :            \
-  __find_first_zero_bit(addr,size)))
+#define find_first_zero_bit(addr, size) find_next_zero_bit(addr, size, 0)
 
 /**
  * find_next_zero_bit - find the first zero bit in a memory region
@@ -362,10 +370,24 @@ static inline unsigned int __scanbit(uns
  * @offset: The bitnumber to start searching at
  * @size: The maximum size to search
  */
-#define find_next_zero_bit(addr,size,off)                                   \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?                   \
-  ((off)+(__scanbit(~(((*(const unsigned long *)addr)) >> (off)), size))) : \
-  __find_next_zero_bit(addr,size,off)))
+#define find_next_zero_bit(addr, size, off) ({ \
+    unsigned int r__ = (size); \
+    unsigned int o__ = (off); \
+    switch ( -!__builtin_constant_p(size) | r__ ) \
+    { \
+    case 0: (void)(addr); break; \
+    case 1 ... BITS_PER_LONG: \
+        r__ = o__ + __scanbit(~*(const unsigned long *)(addr) >> o__, r__); \
+        break; \
+    default: \
+        if ( __builtin_constant_p(off) && !o__ ) \
+            r__ = __find_first_zero_bit(addr, r__); \
+        else \
+            r__ = __find_next_zero_bit(addr, r__, o__); \
+        break; \
+    } \
+    r__; \
+})
 
 /**
  * find_first_set_bit - find the first set bit in @word
xen/include/xen/bitmap.h +64 -64

208 lines not on this page · raw message

bitmap_*() should cope with zero size bitmaps

... to match expectations set by memset()/memcpy().

Similarly for find_{first,next}_{,zero_}_bit() on x86.

__bitmap_shift_{left,right}() would also need fixing (they more generally can't cope with the shift count being larger than the bitmap size, and they perform undefined operations by possibly shifting an unsigned long value by BITS_PER_LONG bits), but since these functions aren't really used anywhere I wonder if we wouldn't better simply get rid of them.

xen/include/asm-x86/bitops.h +38 -16

78 lines not on this page · raw message

xen/include/xen/bitmap.h +64 -64

208 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 29 lines · Jan Beulich

... to match expectations set by memset()/memcpy().

Similarly for find_{first,next}_{,zero_}_bit() on x86.

__bitmap_shift_{left,right}() would also need fixing (they more generally can't cope with the shift count being larger than the bitmap size, and they perform undefined operations by possibly shifting an unsigned long value by BITS_PER_LONG bits), but since these functions aren't really used anywhere I wonder if we wouldn't better simply get rid of them.

xen/include/asm-x86/bitops.h +1 -4
@@ -327,10 +327,7 @@ static inline unsigned int __scanbit(uns
  * Returns the bit-number of the first set bit, not the number of the byte
  * containing a bit.
  */
-#define find_first_bit(addr,size)                               \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?       \
-  (__scanbit(*(const unsigned long *)addr, size)) :             \
-  __find_first_bit(addr,size)))
+#define find_first_bit(addr, size) find_next_bit(addr, size, 0)
 
 /**
  * find_next_bit - find the first set bit in a memory region
@@ -338,10 +335,24 @@ static inline unsigned int __scanbit(uns
  * @offset: The bitnumber to start searching at

xen/include/asm-x86/bitops.h static inline unsigned int __scanbit(uns · line 341

Quoted 10 lines
xen/include/asm-x86/bitops.h +4 -4
  * @size: The maximum size to search
  */
-#define find_next_bit(addr,size,off)                                     \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?                \
-  ((off) + (__scanbit((*(const unsigned long *)addr) >> (off), size))) : \
-  __find_next_bit(addr,size,off)))
+#define find_next_bit(addr, size, off) ({ \
+    unsigned int r__ = (size); \
+    unsigned int o__ = (off); \
+    switch ( -!__builtin_constant_p(size) | r__ ) \

Mmmm, binary trickery. :) But doesn't leave us with the old behaviour in cases where 'size' is a non-compile-time-constant zero?

Tim.

Quoted 14 lines
xen/include/asm-x86/bitops.h +14 -0
+    { \
+    case 0: (void)(addr); break; \
+    case 1 ... BITS_PER_LONG: \
+        r__ = o__ + __scanbit(*(const unsigned long *)(addr) >> o__, r__); \
+        break; \
+    default: \
+        if ( __builtin_constant_p(off) && !o__ ) \
+            r__ = __find_first_bit(addr, r__); \
+        else \
+            r__ = __find_next_bit(addr, r__, o__); \
+        break; \
+    } \
+    r__; \
+})
#2 lore raw
Quoted 36 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 34 lines · Jan Beulich

... to match expectations set by memset()/memcpy().

Similarly for find_{first,next}_{,zero_}_bit() on x86.

__bitmap_shift_{left,right}() would also need fixing (they more generally can't cope with the shift count being larger than the bitmap size, and they perform undefined operations by possibly shifting an unsigned long value by BITS_PER_LONG bits), but since these functions aren't really used anywhere I wonder if we wouldn't better simply get rid of them.

xen/include/asm-x86/bitops.h +1 -7
@@ -327,10 +327,7 @@ static inline unsigned int __scanbit(uns
  * Returns the bit-number of the first set bit, not the number of the byte
  * containing a bit.
  */
-#define find_first_bit(addr,size)                               \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?       \
-  (__scanbit(*(const unsigned long *)addr, size)) :             \
-  __find_first_bit(addr,size)))
+#define find_first_bit(addr, size) find_next_bit(addr, size, 0)
 
 /**
  * find_next_bit - find the first set bit in a memory region
@@ -338,10 +335,24 @@ static inline unsigned int __scanbit(uns
  * @offset: The bitnumber to start searching at
  * @size: The maximum size to search
  */
-#define find_next_bit(addr,size,off)                                     \
-((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ?                \
-  ((off) + (__scanbit((*(const unsigned long *)addr) >> (off), size))) : \
Quoted 8 lines

xen/include/asm-x86/bitops.h static inline unsigned int __scanbit(uns · line 341

Quoted 5 lines
xen/include/asm-x86/bitops.h +4 -1
-  __find_next_bit(addr,size,off)))
+#define find_next_bit(addr, size, off) ({ \
+    unsigned int r__ = (size); \
+    unsigned int o__ = (off); \
+    switch ( -!__builtin_constant_p(size) | r__ ) \

Mmmm, binary trickery. :) But doesn't leave us with the old behaviour in cases where 'size' is a non-compile-time-constant zero?

Intentionally (and not just for zero - the compile time constant zero case was what was broken after all) - I could restore this, but I think the real purpose of optimizing here ought to be when either of the two execution branches can be entirely optimized out (which is only for the compile time constant case).

Jan

#3 lore raw
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 4 lines · Jan Beulich
xen/include/asm-x86/bitops.h +4 -0
+#define find_next_bit(addr, size, off) ({ \
+    unsigned int r__ = (size); \
+    unsigned int o__ = (off); \
+    switch ( -!__builtin_constant_p(size) | r__ ) \
Quoted 5 lines
Quoted 2 lines

Mmmm, binary trickery. :) But doesn't leave us with the old behaviour in cases where 'size' is a non-compile-time-constant zero?

Intentionally (and not just for zero - the compile time constant zero case was what was broken after all)

Ah, I see -- the non-constant case was already OK. Sorry for the noise.

Tim.