Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/find_bit: optimize find_next_bit() functions

Over the past couple years, the function _find_next_bit() was extended
with parameters that modify its behavior to implement and- zero- and le-
flavors. The parameters are passed at compile time, but current design
prevents a compiler from optimizing out the conditionals.

As find_next_bit() API grows, I expect that more parameters will be added.
Current design would require more conditional code in _find_next_bit(),
which would bloat the helper even more and make it barely readable.

This patch replaces _find_next_bit() with a macro FIND_NEXT_BIT, and adds
a set of wrappers, so that the compile-time optimizations become possible.

The common logic is moved to the new macro, and all flavors may be
generated by providing a FETCH macro parameter, like in this example:

#define FIND_NEXT_BIT(FETCH, MUNGE, size, start) ...

find_next_xornot_and_bit(addr1, addr2, addr3, size, start)
{
return FIND_NEXT_BIT(addr1[idx] ^ ~addr2[idx] & addr3[idx],
/* nop */, size, start);
}

The FETCH may be of any complexity, as soon as it only refers the bitmap(s)
and an iterator idx.

MUNGE is here to support _le code generation for BE builds. May be
empty.

I ran find_bit_benchmark 16 times on top of 6.0-rc2 and 16 times on top
of 6.0-rc2 + this series. The results for kvm/x86_64 are:

v6.0-rc2 Optimized Difference Z-score
Random dense bitmap ns ns ns %
find_next_bit: 787735 670546 117189 14.9 3.97
find_next_zero_bit: 777492 664208 113284 14.6 10.51
find_last_bit: 830925 687573 143352 17.3 2.35
find_first_bit: 3874366 3306635 567731 14.7 1.84
find_first_and_bit: 40677125 37739887 2937238 7.2 1.36
find_next_and_bit: 347865 304456 43409 12.5 1.35

Random sparse bitmap
find_next_bit: 19816 14021 5795 29.2 6.10
find_next_zero_bit: 1318901 1223794 95107 7.2 1.41
find_last_bit: 14573 13514 1059 7.3 6.92
find_first_bit: 1313321 1249024 64297 4.9 1.53
find_first_and_bit: 8921 8098 823 9.2 4.56
find_next_and_bit: 9796 7176 2620 26.7 5.39

Where the statistics is significant (z-score > 3), the improvement
is ~15%.

According to the bloat-o-meter, the Image size is 10-11K less:

x86_64/defconfig:
add/remove: 32/14 grow/shrink: 61/782 up/down: 6344/-16521 (-10177)

arm64/defconfig:
add/remove: 3/2 grow/shrink: 50/714 up/down: 608/-11556 (-10948)

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Yury Norov <yury.norov@gmail.com>

+85 -57
+15 -8
include/linux/find.h
··· 8 8 9 9 #include <linux/bitops.h> 10 10 11 - extern unsigned long _find_next_bit(const unsigned long *addr1, 12 - const unsigned long *addr2, unsigned long nbits, 13 - unsigned long start, unsigned long invert, unsigned long le); 11 + unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits, 12 + unsigned long start); 13 + unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, 14 + unsigned long nbits, unsigned long start); 15 + unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits, 16 + unsigned long start); 14 17 extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); 15 18 extern unsigned long _find_first_and_bit(const unsigned long *addr1, 16 19 const unsigned long *addr2, unsigned long size); ··· 22 19 23 20 #ifdef __BIG_ENDIAN 24 21 unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size); 22 + unsigned long _find_next_zero_bit_le(const unsigned long *addr, unsigned 23 + long size, unsigned long offset); 24 + unsigned long _find_next_bit_le(const unsigned long *addr, unsigned 25 + long size, unsigned long offset); 25 26 #endif 26 27 27 28 #ifndef find_next_bit ··· 52 45 return val ? __ffs(val) : size; 53 46 } 54 47 55 - return _find_next_bit(addr, NULL, size, offset, 0UL, 0); 48 + return _find_next_bit(addr, size, offset); 56 49 } 57 50 #endif 58 51 ··· 82 75 return val ? __ffs(val) : size; 83 76 } 84 77 85 - return _find_next_bit(addr1, addr2, size, offset, 0UL, 0); 78 + return _find_next_and_bit(addr1, addr2, size, offset); 86 79 } 87 80 #endif 88 81 ··· 110 103 return val == ~0UL ? size : ffz(val); 111 104 } 112 105 113 - return _find_next_bit(addr, NULL, size, offset, ~0UL, 0); 106 + return _find_next_zero_bit(addr, size, offset); 114 107 } 115 108 #endif 116 109 ··· 258 251 return val == ~0UL ? size : ffz(val); 259 252 } 260 253 261 - return _find_next_bit(addr, NULL, size, offset, ~0UL, 1); 254 + return _find_next_zero_bit_le(addr, size, offset); 262 255 } 263 256 #endif 264 257 ··· 291 284 return val ? __ffs(val) : size; 292 285 } 293 286 294 - return _find_next_bit(addr, NULL, size, offset, 0UL, 1); 287 + return _find_next_bit_le(addr, size, offset); 295 288 } 296 289 #endif 297 290
+70 -49
lib/find_bit.c
··· 40 40 sz; \ 41 41 }) 42 42 43 - #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ 44 - !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \ 45 - !defined(find_next_and_bit) 46 43 /* 47 - * This is a common helper function for find_next_bit, find_next_zero_bit, and 48 - * find_next_and_bit. The differences are: 49 - * - The "invert" argument, which is XORed with each fetched word before 50 - * searching it for one bits. 51 - * - The optional "addr2", which is anded with "addr1" if present. 44 + * Common helper for find_next_bit() function family 45 + * @FETCH: The expression that fetches and pre-processes each word of bitmap(s) 46 + * @MUNGE: The expression that post-processes a word containing found bit (may be empty) 47 + * @size: The bitmap size in bits 48 + * @start: The bitnumber to start searching at 52 49 */ 53 - unsigned long _find_next_bit(const unsigned long *addr1, 54 - const unsigned long *addr2, unsigned long nbits, 55 - unsigned long start, unsigned long invert, unsigned long le) 56 - { 57 - unsigned long tmp, mask; 58 - 59 - if (unlikely(start >= nbits)) 60 - return nbits; 61 - 62 - tmp = addr1[start / BITS_PER_LONG]; 63 - if (addr2) 64 - tmp &= addr2[start / BITS_PER_LONG]; 65 - tmp ^= invert; 66 - 67 - /* Handle 1st word. */ 68 - mask = BITMAP_FIRST_WORD_MASK(start); 69 - if (le) 70 - mask = swab(mask); 71 - 72 - tmp &= mask; 73 - 74 - start = round_down(start, BITS_PER_LONG); 75 - 76 - while (!tmp) { 77 - start += BITS_PER_LONG; 78 - if (start >= nbits) 79 - return nbits; 80 - 81 - tmp = addr1[start / BITS_PER_LONG]; 82 - if (addr2) 83 - tmp &= addr2[start / BITS_PER_LONG]; 84 - tmp ^= invert; 85 - } 86 - 87 - if (le) 88 - tmp = swab(tmp); 89 - 90 - return min(start + __ffs(tmp), nbits); 91 - } 92 - EXPORT_SYMBOL(_find_next_bit); 93 - #endif 50 + #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \ 51 + ({ \ 52 + unsigned long mask, idx, tmp, sz = (size), __start = (start); \ 53 + \ 54 + if (unlikely(__start >= sz)) \ 55 + goto out; \ 56 + \ 57 + mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \ 58 + idx = __start / BITS_PER_LONG; \ 59 + \ 60 + for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \ 61 + if ((idx + 1) * BITS_PER_LONG >= sz) \ 62 + goto out; \ 63 + idx++; \ 64 + } \ 65 + \ 66 + sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \ 67 + out: \ 68 + sz; \ 69 + }) 94 70 95 71 #ifndef find_first_bit 96 72 /* ··· 101 125 return FIND_FIRST_BIT(~addr[idx], /* nop */, size); 102 126 } 103 127 EXPORT_SYMBOL(_find_first_zero_bit); 128 + #endif 129 + 130 + #ifndef find_next_bit 131 + unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start) 132 + { 133 + return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start); 134 + } 135 + EXPORT_SYMBOL(_find_next_bit); 136 + #endif 137 + 138 + #ifndef find_next_and_bit 139 + unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, 140 + unsigned long nbits, unsigned long start) 141 + { 142 + return FIND_NEXT_BIT(addr1[idx] & addr2[idx], /* nop */, nbits, start); 143 + } 144 + EXPORT_SYMBOL(_find_next_and_bit); 145 + #endif 146 + 147 + #ifndef find_next_zero_bit 148 + unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits, 149 + unsigned long start) 150 + { 151 + return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start); 152 + } 153 + EXPORT_SYMBOL(_find_next_zero_bit); 104 154 #endif 105 155 106 156 #ifndef find_last_bit ··· 174 172 return FIND_FIRST_BIT(~addr[idx], swab, size); 175 173 } 176 174 EXPORT_SYMBOL(_find_first_zero_bit_le); 175 + 176 + #endif 177 + 178 + #ifndef find_next_zero_bit_le 179 + unsigned long _find_next_zero_bit_le(const unsigned long *addr, 180 + unsigned long size, unsigned long offset) 181 + { 182 + return FIND_NEXT_BIT(~addr[idx], swab, size, offset); 183 + } 184 + EXPORT_SYMBOL(_find_next_zero_bit_le); 185 + #endif 186 + 187 + #ifndef find_next_bit_le 188 + unsigned long _find_next_bit_le(const unsigned long *addr, 189 + unsigned long size, unsigned long offset) 190 + { 191 + return FIND_NEXT_BIT(addr[idx], swab, size, offset); 192 + } 193 + EXPORT_SYMBOL(_find_next_bit_le); 177 194 178 195 #endif 179 196