bitmap: introduce generic optimized bitmap_size()
authorAlexander Lobakin <aleksander.lobakin@intel.com>
Wed, 27 Mar 2024 15:23:49 +0000 (16:23 +0100)
committerDavid S. Miller <davem@davemloft.net>
Mon, 1 Apr 2024 09:49:28 +0000 (10:49 +0100)
The number of times yet another open coded
`BITS_TO_LONGS(nbits) * sizeof(long)` can be spotted is huge.
Some generic helper is long overdue.

Add one, bitmap_size(), but with one detail.
BITS_TO_LONGS() uses DIV_ROUND_UP(). The latter works well when both
divident and divisor are compile-time constants or when the divisor
is not a pow-of-2. When it is however, the compilers sometimes tend
to generate suboptimal code (GCC 13):

48 83 c0 3f           add    $0x3f,%rax
48 c1 e8 06           shr    $0x6,%rax
48 8d 14 c5 00 00 00 00 lea    0x0(,%rax,8),%rdx

%BITS_PER_LONG is always a pow-2 (either 32 or 64), but GCC still does
full division of `nbits + 63` by it and then multiplication by 8.
Instead of BITS_TO_LONGS(), use ALIGN() and then divide by 8. GCC:

8d 50 3f              lea    0x3f(%rax),%edx
c1 ea 03              shr    $0x3,%edx
81 e2 f8 ff ff 1f     and    $0x1ffffff8,%edx

Now it shifts `nbits + 63` by 3 positions (IOW performs fast division
by 8) and then masks bits[2:0]. bloat-o-meter:

add/remove: 0/0 grow/shrink: 20/133 up/down: 156/-773 (-617)

Clang does it better and generates the same code before/after starting
from -O1, except that with the ALIGN() approach it uses %edx and thus
still saves some bytes:

add/remove: 0/0 grow/shrink: 9/133 up/down: 18/-538 (-520)

Note that we can't expand DIV_ROUND_UP() by adding a check and using
this approach there, as it's used in array declarations where
expressions are not allowed.
Add this helper to tools/ as well.

Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Acked-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/md/dm-clone-metadata.c
drivers/s390/cio/idset.c
include/linux/bitmap.h
include/linux/cpumask.h
lib/math/prime_numbers.c
tools/include/linux/bitmap.h

index c43d55672bce035b50549f070eb2a5535b4447d4..47c1fa7aad8b5b993af919b9ae98cdaa0f3f0559 100644 (file)
@@ -465,11 +465,6 @@ static void __destroy_persistent_data_structures(struct dm_clone_metadata *cmd)
 
 /*---------------------------------------------------------------------------*/
 
-static size_t bitmap_size(unsigned long nr_bits)
-{
-       return BITS_TO_LONGS(nr_bits) * sizeof(long);
-}
-
 static int __dirty_map_init(struct dirty_map *dmap, unsigned long nr_words,
                            unsigned long nr_regions)
 {
index 0a1105a483bfe0da0ce111e2c23aa2af59ef6ca5..e5f28370a9039d71e9b353908632aba61c9fa307 100644 (file)
@@ -18,7 +18,7 @@ struct idset {
 
 static inline unsigned long idset_bitmap_size(int num_ssid, int num_id)
 {
-       return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long);
+       return bitmap_size(size_mul(num_ssid, num_id));
 }
 
 static struct idset *idset_new(int num_ssid, int num_id)
index 914c23e96f26a45975ce97ebb355ef33d6d533fa..363e0b184a453d45af72e6624c65dbbbc47056da 100644 (file)
@@ -226,9 +226,11 @@ void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 #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_size(nbits)     (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE)
+
 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
 {
-       unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
+       unsigned int len = bitmap_size(nbits);
 
        if (small_const_nbits(nbits))
                *dst = 0;
@@ -238,7 +240,7 @@ static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
 
 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
 {
-       unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
+       unsigned int len = bitmap_size(nbits);
 
        if (small_const_nbits(nbits))
                *dst = ~0UL;
@@ -249,7 +251,7 @@ static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
                        unsigned int nbits)
 {
-       unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
+       unsigned int len = bitmap_size(nbits);
 
        if (small_const_nbits(nbits))
                *dst = *src;
index 1c29947db84899cd72b00a8c77685972dc187b80..6519f9c7770951222d138d175f958bc36eb20b0e 100644 (file)
@@ -853,7 +853,7 @@ static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
  */
 static inline unsigned int cpumask_size(void)
 {
-       return BITS_TO_LONGS(large_cpumask_bits) * sizeof(long);
+       return bitmap_size(large_cpumask_bits);
 }
 
 /*
index d42cebf7407fc45cc698c406e2d38846b248ae12..d3b64b10da1c5e3459de5124cf699b57c026c232 100644 (file)
@@ -6,8 +6,6 @@
 #include <linux/prime_numbers.h>
 #include <linux/slab.h>
 
-#define bitmap_size(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long))
-
 struct primes {
        struct rcu_head rcu;
        unsigned long last, sz;
index 8c6852dba04f25074ebb0edcc2b1fb33f962701c..210c13b1b8570b46aab703a3383ef5947e8fa1f5 100644 (file)
@@ -26,13 +26,14 @@ bool __bitmap_intersects(const unsigned long *bitmap1,
 #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_size(nbits)     (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE)
+
 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
 {
        if (small_const_nbits(nbits))
                *dst = 0UL;
        else {
-               int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
-               memset(dst, 0, len);
+               memset(dst, 0, bitmap_size(nbits));
        }
 }
 
@@ -84,7 +85,7 @@ static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  */
 static inline unsigned long *bitmap_zalloc(int nbits)
 {
-       return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
+       return calloc(1, bitmap_size(nbits));
 }
 
 /*