bitmap.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
  64. * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
  65. *
  66. * Note, bitmap_zero() and bitmap_fill() operate over the region of
  67. * unsigned longs, that is, bits behind bitmap till the unsigned long
  68. * boundary will be zeroed or filled as well. Consider to use
  69. * bitmap_clear() or bitmap_set() to make explicit zeroing or filling
  70. * respectively.
  71. */
  72. /**
  73. * DOC: bitmap bitops
  74. *
  75. * Also the following operations in asm/bitops.h apply to bitmaps.::
  76. *
  77. * set_bit(bit, addr) *addr |= bit
  78. * clear_bit(bit, addr) *addr &= ~bit
  79. * change_bit(bit, addr) *addr ^= bit
  80. * test_bit(bit, addr) Is bit set in *addr?
  81. * test_and_set_bit(bit, addr) Set bit and return old value
  82. * test_and_clear_bit(bit, addr) Clear bit and return old value
  83. * test_and_change_bit(bit, addr) Change bit and return old value
  84. * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
  85. * find_first_bit(addr, nbits) Position first set bit in *addr
  86. * find_next_zero_bit(addr, nbits, bit)
  87. * Position next zero bit in *addr >= bit
  88. * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
  89. * find_next_and_bit(addr1, addr2, nbits, bit)
  90. * Same as find_next_bit, but in
  91. * (*addr1 & *addr2)
  92. *
  93. */
  94. /**
  95. * DOC: declare bitmap
  96. * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
  97. * to declare an array named 'name' of just enough unsigned longs to
  98. * contain all bit positions from 0 to 'bits' - 1.
  99. */
  100. /*
  101. * Allocation and deallocation of bitmap.
  102. * Provided in lib/bitmap.c to avoid circular dependency.
  103. */
  104. extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
  105. extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
  106. extern void bitmap_free(const unsigned long *bitmap);
  107. /*
  108. * lib/bitmap.c provides these functions:
  109. */
  110. extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
  111. extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
  112. extern int __bitmap_equal(const unsigned long *bitmap1,
  113. const unsigned long *bitmap2, unsigned int nbits);
  114. extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
  115. unsigned int nbits);
  116. extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  117. unsigned int shift, unsigned int nbits);
  118. extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  119. unsigned int shift, unsigned int nbits);
  120. extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  121. const unsigned long *bitmap2, unsigned int nbits);
  122. extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  123. const unsigned long *bitmap2, unsigned int nbits);
  124. extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  125. const unsigned long *bitmap2, unsigned int nbits);
  126. extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  127. const unsigned long *bitmap2, unsigned int nbits);
  128. extern int __bitmap_intersects(const unsigned long *bitmap1,
  129. const unsigned long *bitmap2, unsigned int nbits);
  130. extern int __bitmap_subset(const unsigned long *bitmap1,
  131. const unsigned long *bitmap2, unsigned int nbits);
  132. extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
  133. extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
  134. extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
  135. extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  136. unsigned long size,
  137. unsigned long start,
  138. unsigned int nr,
  139. unsigned long align_mask,
  140. unsigned long align_offset);
  141. /**
  142. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  143. * @map: The address to base the search on
  144. * @size: The bitmap size in bits
  145. * @start: The bitnumber to start searching at
  146. * @nr: The number of zeroed bits we're looking for
  147. * @align_mask: Alignment mask for zero area
  148. *
  149. * The @align_mask should be one less than a power of 2; the effect is that
  150. * the bit offset of all zero areas this function finds is multiples of that
  151. * power of 2. A @align_mask of 0 means no alignment is required.
  152. */
  153. static inline unsigned long
  154. bitmap_find_next_zero_area(unsigned long *map,
  155. unsigned long size,
  156. unsigned long start,
  157. unsigned int nr,
  158. unsigned long align_mask)
  159. {
  160. return bitmap_find_next_zero_area_off(map, size, start, nr,
  161. align_mask, 0);
  162. }
  163. extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
  164. unsigned long *dst, int nbits);
  165. extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
  166. unsigned long *dst, int nbits);
  167. extern int bitmap_parselist(const char *buf, unsigned long *maskp,
  168. int nmaskbits);
  169. extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
  170. unsigned long *dst, int nbits);
  171. extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
  172. const unsigned long *old, const unsigned long *new, unsigned int nbits);
  173. extern int bitmap_bitremap(int oldbit,
  174. const unsigned long *old, const unsigned long *new, int bits);
  175. extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  176. const unsigned long *relmap, unsigned int bits);
  177. extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  178. unsigned int sz, unsigned int nbits);
  179. extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
  180. extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
  181. extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
  182. #ifdef __BIG_ENDIAN
  183. extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
  184. #else
  185. #define bitmap_copy_le bitmap_copy
  186. #endif
  187. extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
  188. extern int bitmap_print_to_pagebuf(bool list, char *buf,
  189. const unsigned long *maskp, int nmaskbits);
  190. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  191. #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
  192. /*
  193. * The static inlines below do not handle constant nbits==0 correctly,
  194. * so make such users (should any ever turn up) call the out-of-line
  195. * versions.
  196. */
  197. #define small_const_nbits(nbits) \
  198. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG && (nbits) > 0)
  199. static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
  200. {
  201. if (small_const_nbits(nbits))
  202. *dst = 0UL;
  203. else {
  204. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  205. memset(dst, 0, len);
  206. }
  207. }
  208. static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
  209. {
  210. if (small_const_nbits(nbits))
  211. *dst = ~0UL;
  212. else {
  213. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  214. memset(dst, 0xff, len);
  215. }
  216. }
  217. static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
  218. unsigned int nbits)
  219. {
  220. if (small_const_nbits(nbits))
  221. *dst = *src;
  222. else {
  223. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  224. memcpy(dst, src, len);
  225. }
  226. }
  227. /*
  228. * Copy bitmap and clear tail bits in last word.
  229. */
  230. static inline void bitmap_copy_clear_tail(unsigned long *dst,
  231. const unsigned long *src, unsigned int nbits)
  232. {
  233. bitmap_copy(dst, src, nbits);
  234. if (nbits % BITS_PER_LONG)
  235. dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
  236. }
  237. /*
  238. * On 32-bit systems bitmaps are represented as u32 arrays internally, and
  239. * therefore conversion is not needed when copying data from/to arrays of u32.
  240. */
  241. #if BITS_PER_LONG == 64
  242. extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
  243. unsigned int nbits);
  244. extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
  245. unsigned int nbits);
  246. #else
  247. #define bitmap_from_arr32(bitmap, buf, nbits) \
  248. bitmap_copy_clear_tail((unsigned long *) (bitmap), \
  249. (const unsigned long *) (buf), (nbits))
  250. #define bitmap_to_arr32(buf, bitmap, nbits) \
  251. bitmap_copy_clear_tail((unsigned long *) (buf), \
  252. (const unsigned long *) (bitmap), (nbits))
  253. #endif
  254. static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
  255. const unsigned long *src2, unsigned int nbits)
  256. {
  257. if (small_const_nbits(nbits))
  258. return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  259. return __bitmap_and(dst, src1, src2, nbits);
  260. }
  261. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  262. const unsigned long *src2, unsigned int nbits)
  263. {
  264. if (small_const_nbits(nbits))
  265. *dst = *src1 | *src2;
  266. else
  267. __bitmap_or(dst, src1, src2, nbits);
  268. }
  269. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  270. const unsigned long *src2, unsigned int nbits)
  271. {
  272. if (small_const_nbits(nbits))
  273. *dst = *src1 ^ *src2;
  274. else
  275. __bitmap_xor(dst, src1, src2, nbits);
  276. }
  277. static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
  278. const unsigned long *src2, unsigned int nbits)
  279. {
  280. if (small_const_nbits(nbits))
  281. return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  282. return __bitmap_andnot(dst, src1, src2, nbits);
  283. }
  284. static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
  285. unsigned int nbits)
  286. {
  287. if (small_const_nbits(nbits))
  288. *dst = ~(*src);
  289. else
  290. __bitmap_complement(dst, src, nbits);
  291. }
  292. #ifdef __LITTLE_ENDIAN
  293. #define BITMAP_MEM_ALIGNMENT 8
  294. #else
  295. #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
  296. #endif
  297. #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
  298. static inline int bitmap_equal(const unsigned long *src1,
  299. const unsigned long *src2, unsigned int nbits)
  300. {
  301. if (small_const_nbits(nbits))
  302. return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
  303. if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
  304. IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
  305. return !memcmp(src1, src2, nbits / 8);
  306. return __bitmap_equal(src1, src2, nbits);
  307. }
  308. static inline int bitmap_intersects(const unsigned long *src1,
  309. const unsigned long *src2, unsigned int nbits)
  310. {
  311. if (small_const_nbits(nbits))
  312. return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  313. else
  314. return __bitmap_intersects(src1, src2, nbits);
  315. }
  316. static inline int bitmap_subset(const unsigned long *src1,
  317. const unsigned long *src2, unsigned int nbits)
  318. {
  319. if (small_const_nbits(nbits))
  320. return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
  321. else
  322. return __bitmap_subset(src1, src2, nbits);
  323. }
  324. static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
  325. {
  326. if (small_const_nbits(nbits))
  327. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  328. return find_first_bit(src, nbits) == nbits;
  329. }
  330. static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
  331. {
  332. if (small_const_nbits(nbits))
  333. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  334. return find_first_zero_bit(src, nbits) == nbits;
  335. }
  336. static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
  337. {
  338. if (small_const_nbits(nbits))
  339. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  340. return __bitmap_weight(src, nbits);
  341. }
  342. static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
  343. unsigned int nbits)
  344. {
  345. if (__builtin_constant_p(nbits) && nbits == 1)
  346. __set_bit(start, map);
  347. else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
  348. IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
  349. __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
  350. IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
  351. memset((char *)map + start / 8, 0xff, nbits / 8);
  352. else
  353. __bitmap_set(map, start, nbits);
  354. }
  355. static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
  356. unsigned int nbits)
  357. {
  358. if (__builtin_constant_p(nbits) && nbits == 1)
  359. __clear_bit(start, map);
  360. else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
  361. IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
  362. __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
  363. IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
  364. memset((char *)map + start / 8, 0, nbits / 8);
  365. else
  366. __bitmap_clear(map, start, nbits);
  367. }
  368. static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  369. unsigned int shift, unsigned int nbits)
  370. {
  371. if (small_const_nbits(nbits))
  372. *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
  373. else
  374. __bitmap_shift_right(dst, src, shift, nbits);
  375. }
  376. static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  377. unsigned int shift, unsigned int nbits)
  378. {
  379. if (small_const_nbits(nbits))
  380. *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
  381. else
  382. __bitmap_shift_left(dst, src, shift, nbits);
  383. }
  384. static inline int bitmap_parse(const char *buf, unsigned int buflen,
  385. unsigned long *maskp, int nmaskbits)
  386. {
  387. return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
  388. }
  389. /**
  390. * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
  391. * @n: u64 value
  392. *
  393. * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
  394. * integers in 32-bit environment, and 64-bit integers in 64-bit one.
  395. *
  396. * There are four combinations of endianness and length of the word in linux
  397. * ABIs: LE64, BE64, LE32 and BE32.
  398. *
  399. * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
  400. * bitmaps and therefore don't require any special handling.
  401. *
  402. * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
  403. * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
  404. * other hand is represented as an array of 32-bit words and the position of
  405. * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
  406. * word. For example, bit #42 is located at 10th position of 2nd word.
  407. * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
  408. * values in memory as it usually does. But for BE we need to swap hi and lo
  409. * words manually.
  410. *
  411. * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
  412. * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
  413. * hi and lo words, as is expected by bitmap.
  414. */
  415. #if __BITS_PER_LONG == 64
  416. #define BITMAP_FROM_U64(n) (n)
  417. #else
  418. #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
  419. ((unsigned long) ((u64)(n) >> 32))
  420. #endif
  421. /**
  422. * bitmap_from_u64 - Check and swap words within u64.
  423. * @mask: source bitmap
  424. * @dst: destination bitmap
  425. *
  426. * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
  427. * to read u64 mask, we will get the wrong word.
  428. * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
  429. * but we expect the lower 32-bits of u64.
  430. */
  431. static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
  432. {
  433. dst[0] = mask & ULONG_MAX;
  434. if (sizeof(mask) > sizeof(unsigned long))
  435. dst[1] = mask >> 32;
  436. }
  437. #endif /* __ASSEMBLY__ */
  438. #endif /* __LINUX_BITMAP_H */