bitmap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_BITMAP_H
  3. #define __LINUX_BITMAP_H
  4. #ifndef __ASSEMBLY__
  5. #include <linux/types.h>
  6. #include <linux/bitops.h>
  7. #include <linux/string.h>
  8. #include <linux/kernel.h>
  9. /*
  10. * bitmaps provide bit arrays that consume one or more unsigned
  11. * longs. The bitmap interface and available operations are listed
  12. * here, in bitmap.h
  13. *
  14. * Function implementations generic to all architectures are in
  15. * lib/bitmap.c. Functions implementations that are architecture
  16. * specific are in various include/asm-<arch>/bitops.h headers
  17. * and other arch/<arch> specific files.
  18. *
  19. * See lib/bitmap.c for more details.
  20. */
  21. /**
  22. * DOC: bitmap overview
  23. *
  24. * The available bitmap operations and their rough meaning in the
  25. * case that the bitmap is a single unsigned long are thus:
  26. *
  27. * Note that nbits should be always a compile time evaluable constant.
  28. * Otherwise many inlines will generate horrible code.
  29. *
  30. * ::
  31. *
  32. * bitmap_zero(dst, nbits) *dst = 0UL
  33. * bitmap_fill(dst, nbits) *dst = ~0UL
  34. * bitmap_copy(dst, src, nbits) *dst = *src
  35. * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
  36. * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
  37. * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
  38. * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
  39. * bitmap_complement(dst, src, nbits) *dst = ~(*src)
  40. * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
  41. * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
  42. * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
  43. * bitmap_empty(src, nbits) Are all bits zero in *src?
  44. * bitmap_full(src, nbits) Are all bits set in *src?
  45. * bitmap_weight(src, nbits) Hamming Weight: number set bits
  46. * bitmap_set(dst, pos, nbits) Set specified bit area
  47. * bitmap_clear(dst, pos, nbits) Clear specified bit area
  48. * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
  49. * bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above
  50. * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
  51. * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
  52. * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
  53. * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
  54. * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
  55. * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
  56. * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
  57. * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
  58. * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
  59. * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
  60. * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
  61. * bitmap_release_region(bitmap, pos, order) Free specified bit region
  62. * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
  63. * bitmap_from_u32array(dst, nbits, buf, nwords) *dst = *buf (nwords 32b words)
  64. * bitmap_to_u32array(buf, nwords, src, nbits) *buf = *dst (nwords 32b words)
  65. *
  66. */
  67. /**
  68. * DOC: bitmap bitops
  69. *
  70. * Also the following operations in asm/bitops.h apply to bitmaps.::
  71. *
  72. * set_bit(bit, addr) *addr |= bit
  73. * clear_bit(bit, addr) *addr &= ~bit
  74. * change_bit(bit, addr) *addr ^= bit
  75. * test_bit(bit, addr) Is bit set in *addr?
  76. * test_and_set_bit(bit, addr) Set bit and return old value
  77. * test_and_clear_bit(bit, addr) Clear bit and return old value
  78. * test_and_change_bit(bit, addr) Change bit and return old value
  79. * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
  80. * find_first_bit(addr, nbits) Position first set bit in *addr
  81. * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
  82. * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
  83. *
  84. */
  85. /**
  86. * DOC: declare bitmap
  87. * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
  88. * to declare an array named 'name' of just enough unsigned longs to
  89. * contain all bit positions from 0 to 'bits' - 1.
  90. */
  91. /*
  92. * lib/bitmap.c provides these functions:
  93. */
  94. extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
  95. extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
  96. extern int __bitmap_equal(const unsigned long *bitmap1,
  97. const unsigned long *bitmap2, unsigned int nbits);
  98. extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
  99. unsigned int nbits);
  100. extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  101. unsigned int shift, unsigned int nbits);
  102. extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  103. unsigned int shift, unsigned int nbits);
  104. extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  105. const unsigned long *bitmap2, unsigned int nbits);
  106. extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  107. const unsigned long *bitmap2, unsigned int nbits);
  108. extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  109. const unsigned long *bitmap2, unsigned int nbits);
  110. extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  111. const unsigned long *bitmap2, unsigned int nbits);
  112. extern int __bitmap_intersects(const unsigned long *bitmap1,
  113. const unsigned long *bitmap2, unsigned int nbits);
  114. extern int __bitmap_subset(const unsigned long *bitmap1,
  115. const unsigned long *bitmap2, unsigned int nbits);
  116. extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
  117. extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
  118. extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
  119. extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  120. unsigned long size,
  121. unsigned long start,
  122. unsigned int nr,
  123. unsigned long align_mask,
  124. unsigned long align_offset);
  125. /**
  126. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  127. * @map: The address to base the search on
  128. * @size: The bitmap size in bits
  129. * @start: The bitnumber to start searching at
  130. * @nr: The number of zeroed bits we're looking for
  131. * @align_mask: Alignment mask for zero area
  132. *
  133. * The @align_mask should be one less than a power of 2; the effect is that
  134. * the bit offset of all zero areas this function finds is multiples of that
  135. * power of 2. A @align_mask of 0 means no alignment is required.
  136. */
  137. static inline unsigned long
  138. bitmap_find_next_zero_area(unsigned long *map,
  139. unsigned long size,
  140. unsigned long start,
  141. unsigned int nr,
  142. unsigned long align_mask)
  143. {
  144. return bitmap_find_next_zero_area_off(map, size, start, nr,
  145. align_mask, 0);
  146. }
  147. extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
  148. unsigned long *dst, int nbits);
  149. extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
  150. unsigned long *dst, int nbits);
  151. extern int bitmap_parselist(const char *buf, unsigned long *maskp,
  152. int nmaskbits);
  153. extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
  154. unsigned long *dst, int nbits);
  155. extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
  156. const unsigned long *old, const unsigned long *new, unsigned int nbits);
  157. extern int bitmap_bitremap(int oldbit,
  158. const unsigned long *old, const unsigned long *new, int bits);
  159. extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  160. const unsigned long *relmap, unsigned int bits);
  161. extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  162. unsigned int sz, unsigned int nbits);
  163. extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
  164. extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
  165. extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
  166. extern unsigned int bitmap_from_u32array(unsigned long *bitmap,
  167. unsigned int nbits,
  168. const u32 *buf,
  169. unsigned int nwords);
  170. extern unsigned int bitmap_to_u32array(u32 *buf,
  171. unsigned int nwords,
  172. const unsigned long *bitmap,
  173. unsigned int nbits);
  174. #ifdef __BIG_ENDIAN
  175. extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
  176. #else
  177. #define bitmap_copy_le bitmap_copy
  178. #endif
  179. extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
  180. extern int bitmap_print_to_pagebuf(bool list, char *buf,
  181. const unsigned long *maskp, int nmaskbits);
  182. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  183. #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
  184. #define small_const_nbits(nbits) \
  185. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  186. static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
  187. {
  188. if (small_const_nbits(nbits))
  189. *dst = 0UL;
  190. else {
  191. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  192. memset(dst, 0, len);
  193. }
  194. }
  195. static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
  196. {
  197. unsigned int nlongs = BITS_TO_LONGS(nbits);
  198. if (!small_const_nbits(nbits)) {
  199. unsigned int len = (nlongs - 1) * sizeof(unsigned long);
  200. memset(dst, 0xff, len);
  201. }
  202. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  203. }
  204. static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
  205. unsigned int nbits)
  206. {
  207. if (small_const_nbits(nbits))
  208. *dst = *src;
  209. else {
  210. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  211. memcpy(dst, src, len);
  212. }
  213. }
  214. static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
  215. const unsigned long *src2, unsigned int nbits)
  216. {
  217. if (small_const_nbits(nbits))
  218. return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  219. return __bitmap_and(dst, src1, src2, nbits);
  220. }
  221. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  222. const unsigned long *src2, unsigned int nbits)
  223. {
  224. if (small_const_nbits(nbits))
  225. *dst = *src1 | *src2;
  226. else
  227. __bitmap_or(dst, src1, src2, nbits);
  228. }
  229. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  230. const unsigned long *src2, unsigned int nbits)
  231. {
  232. if (small_const_nbits(nbits))
  233. *dst = *src1 ^ *src2;
  234. else
  235. __bitmap_xor(dst, src1, src2, nbits);
  236. }
  237. static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
  238. const unsigned long *src2, unsigned int nbits)
  239. {
  240. if (small_const_nbits(nbits))
  241. return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  242. return __bitmap_andnot(dst, src1, src2, nbits);
  243. }
  244. static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
  245. unsigned int nbits)
  246. {
  247. if (small_const_nbits(nbits))
  248. *dst = ~(*src);
  249. else
  250. __bitmap_complement(dst, src, nbits);
  251. }
  252. static inline int bitmap_equal(const unsigned long *src1,
  253. const unsigned long *src2, unsigned int nbits)
  254. {
  255. if (small_const_nbits(nbits))
  256. return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
  257. if (__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
  258. return !memcmp(src1, src2, nbits / 8);
  259. return __bitmap_equal(src1, src2, nbits);
  260. }
  261. static inline int bitmap_intersects(const unsigned long *src1,
  262. const unsigned long *src2, unsigned int nbits)
  263. {
  264. if (small_const_nbits(nbits))
  265. return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  266. else
  267. return __bitmap_intersects(src1, src2, nbits);
  268. }
  269. static inline int bitmap_subset(const unsigned long *src1,
  270. const unsigned long *src2, unsigned int nbits)
  271. {
  272. if (small_const_nbits(nbits))
  273. return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
  274. else
  275. return __bitmap_subset(src1, src2, nbits);
  276. }
  277. static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
  278. {
  279. if (small_const_nbits(nbits))
  280. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  281. return find_first_bit(src, nbits) == nbits;
  282. }
  283. static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
  284. {
  285. if (small_const_nbits(nbits))
  286. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  287. return find_first_zero_bit(src, nbits) == nbits;
  288. }
  289. static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
  290. {
  291. if (small_const_nbits(nbits))
  292. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  293. return __bitmap_weight(src, nbits);
  294. }
  295. static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
  296. unsigned int nbits)
  297. {
  298. if (__builtin_constant_p(nbits) && nbits == 1)
  299. __set_bit(start, map);
  300. else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
  301. __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
  302. memset((char *)map + start / 8, 0xff, nbits / 8);
  303. else
  304. __bitmap_set(map, start, nbits);
  305. }
  306. static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
  307. unsigned int nbits)
  308. {
  309. if (__builtin_constant_p(nbits) && nbits == 1)
  310. __clear_bit(start, map);
  311. else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
  312. __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
  313. memset((char *)map + start / 8, 0, nbits / 8);
  314. else
  315. __bitmap_clear(map, start, nbits);
  316. }
  317. static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  318. unsigned int shift, int nbits)
  319. {
  320. if (small_const_nbits(nbits))
  321. *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
  322. else
  323. __bitmap_shift_right(dst, src, shift, nbits);
  324. }
  325. static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  326. unsigned int shift, unsigned int nbits)
  327. {
  328. if (small_const_nbits(nbits))
  329. *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
  330. else
  331. __bitmap_shift_left(dst, src, shift, nbits);
  332. }
  333. static inline int bitmap_parse(const char *buf, unsigned int buflen,
  334. unsigned long *maskp, int nmaskbits)
  335. {
  336. return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
  337. }
  338. /**
  339. * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
  340. * @n: u64 value
  341. *
  342. * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
  343. * integers in 32-bit environment, and 64-bit integers in 64-bit one.
  344. *
  345. * There are four combinations of endianness and length of the word in linux
  346. * ABIs: LE64, BE64, LE32 and BE32.
  347. *
  348. * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
  349. * bitmaps and therefore don't require any special handling.
  350. *
  351. * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
  352. * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
  353. * other hand is represented as an array of 32-bit words and the position of
  354. * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
  355. * word. For example, bit #42 is located at 10th position of 2nd word.
  356. * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
  357. * values in memory as it usually does. But for BE we need to swap hi and lo
  358. * words manually.
  359. *
  360. * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
  361. * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
  362. * hi and lo words, as is expected by bitmap.
  363. */
  364. #if __BITS_PER_LONG == 64
  365. #define BITMAP_FROM_U64(n) (n)
  366. #else
  367. #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
  368. ((unsigned long) ((u64)(n) >> 32))
  369. #endif
  370. /**
  371. * bitmap_from_u64 - Check and swap words within u64.
  372. * @mask: source bitmap
  373. * @dst: destination bitmap
  374. *
  375. * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
  376. * to read u64 mask, we will get the wrong word.
  377. * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
  378. * but we expect the lower 32-bits of u64.
  379. */
  380. static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
  381. {
  382. dst[0] = mask & ULONG_MAX;
  383. if (sizeof(mask) > sizeof(unsigned long))
  384. dst[1] = mask >> 32;
  385. }
  386. #endif /* __ASSEMBLY__ */
  387. #endif /* __LINUX_BITMAP_H */