bitmap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lib/bitmap.c
  4. * Helper functions for bitmap.h.
  5. */
  6. #include <linux/bitmap.h>
  7. #include <linux/bitops.h>
  8. #include <linux/ctype.h>
  9. #include <linux/device.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. /**
  13. * DOC: bitmap introduction
  14. *
  15. * bitmaps provide an array of bits, implemented using an
  16. * array of unsigned longs. The number of valid bits in a
  17. * given bitmap does _not_ need to be an exact multiple of
  18. * BITS_PER_LONG.
  19. *
  20. * The possible unused bits in the last, partially used word
  21. * of a bitmap are 'don't care'. The implementation makes
  22. * no particular effort to keep them zero. It ensures that
  23. * their value will not affect the results of any operation.
  24. * The bitmap operations that return Boolean (bitmap_empty,
  25. * for example) or scalar (bitmap_weight, for example) results
  26. * carefully filter out these unused bits from impacting their
  27. * results.
  28. *
  29. * The byte ordering of bitmaps is more natural on little
  30. * endian architectures. See the big-endian headers
  31. * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
  32. * for the best explanations of this ordering.
  33. */
  34. bool __bitmap_equal(const unsigned long *bitmap1,
  35. const unsigned long *bitmap2, unsigned int bits)
  36. {
  37. unsigned int k, lim = bits/BITS_PER_LONG;
  38. for (k = 0; k < lim; ++k)
  39. if (bitmap1[k] != bitmap2[k])
  40. return false;
  41. if (bits % BITS_PER_LONG)
  42. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  43. return false;
  44. return true;
  45. }
  46. EXPORT_SYMBOL(__bitmap_equal);
  47. bool __bitmap_or_equal(const unsigned long *bitmap1,
  48. const unsigned long *bitmap2,
  49. const unsigned long *bitmap3,
  50. unsigned int bits)
  51. {
  52. unsigned int k, lim = bits / BITS_PER_LONG;
  53. unsigned long tmp;
  54. for (k = 0; k < lim; ++k) {
  55. if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
  56. return false;
  57. }
  58. if (!(bits % BITS_PER_LONG))
  59. return true;
  60. tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
  61. return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
  62. }
  63. void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
  64. {
  65. unsigned int k, lim = BITS_TO_LONGS(bits);
  66. for (k = 0; k < lim; ++k)
  67. dst[k] = ~src[k];
  68. }
  69. EXPORT_SYMBOL(__bitmap_complement);
  70. /**
  71. * __bitmap_shift_right - logical right shift of the bits in a bitmap
  72. * @dst : destination bitmap
  73. * @src : source bitmap
  74. * @shift : shift by this many bits
  75. * @nbits : bitmap size, in bits
  76. *
  77. * Shifting right (dividing) means moving bits in the MS -> LS bit
  78. * direction. Zeros are fed into the vacated MS positions and the
  79. * LS bits shifted off the bottom are lost.
  80. */
  81. void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  82. unsigned shift, unsigned nbits)
  83. {
  84. unsigned k, lim = BITS_TO_LONGS(nbits);
  85. unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  86. unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
  87. for (k = 0; off + k < lim; ++k) {
  88. unsigned long upper, lower;
  89. /*
  90. * If shift is not word aligned, take lower rem bits of
  91. * word above and make them the top rem bits of result.
  92. */
  93. if (!rem || off + k + 1 >= lim)
  94. upper = 0;
  95. else {
  96. upper = src[off + k + 1];
  97. if (off + k + 1 == lim - 1)
  98. upper &= mask;
  99. upper <<= (BITS_PER_LONG - rem);
  100. }
  101. lower = src[off + k];
  102. if (off + k == lim - 1)
  103. lower &= mask;
  104. lower >>= rem;
  105. dst[k] = lower | upper;
  106. }
  107. if (off)
  108. memset(&dst[lim - off], 0, off*sizeof(unsigned long));
  109. }
  110. EXPORT_SYMBOL(__bitmap_shift_right);
  111. /**
  112. * __bitmap_shift_left - logical left shift of the bits in a bitmap
  113. * @dst : destination bitmap
  114. * @src : source bitmap
  115. * @shift : shift by this many bits
  116. * @nbits : bitmap size, in bits
  117. *
  118. * Shifting left (multiplying) means moving bits in the LS -> MS
  119. * direction. Zeros are fed into the vacated LS bit positions
  120. * and those MS bits shifted off the top are lost.
  121. */
  122. void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  123. unsigned int shift, unsigned int nbits)
  124. {
  125. int k;
  126. unsigned int lim = BITS_TO_LONGS(nbits);
  127. unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  128. for (k = lim - off - 1; k >= 0; --k) {
  129. unsigned long upper, lower;
  130. /*
  131. * If shift is not word aligned, take upper rem bits of
  132. * word below and make them the bottom rem bits of result.
  133. */
  134. if (rem && k > 0)
  135. lower = src[k - 1] >> (BITS_PER_LONG - rem);
  136. else
  137. lower = 0;
  138. upper = src[k] << rem;
  139. dst[k + off] = lower | upper;
  140. }
  141. if (off)
  142. memset(dst, 0, off*sizeof(unsigned long));
  143. }
  144. EXPORT_SYMBOL(__bitmap_shift_left);
  145. /**
  146. * bitmap_cut() - remove bit region from bitmap and right shift remaining bits
  147. * @dst: destination bitmap, might overlap with src
  148. * @src: source bitmap
  149. * @first: start bit of region to be removed
  150. * @cut: number of bits to remove
  151. * @nbits: bitmap size, in bits
  152. *
  153. * Set the n-th bit of @dst iff the n-th bit of @src is set and
  154. * n is less than @first, or the m-th bit of @src is set for any
  155. * m such that @first <= n < nbits, and m = n + @cut.
  156. *
  157. * In pictures, example for a big-endian 32-bit architecture:
  158. *
  159. * The @src bitmap is::
  160. *
  161. * 31 63
  162. * | |
  163. * 10000000 11000001 11110010 00010101 10000000 11000001 01110010 00010101
  164. * | | | |
  165. * 16 14 0 32
  166. *
  167. * if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is::
  168. *
  169. * 31 63
  170. * | |
  171. * 10110000 00011000 00110010 00010101 00010000 00011000 00101110 01000010
  172. * | | |
  173. * 14 (bit 17 0 32
  174. * from @src)
  175. *
  176. * Note that @dst and @src might overlap partially or entirely.
  177. *
  178. * This is implemented in the obvious way, with a shift and carry
  179. * step for each moved bit. Optimisation is left as an exercise
  180. * for the compiler.
  181. */
  182. void bitmap_cut(unsigned long *dst, const unsigned long *src,
  183. unsigned int first, unsigned int cut, unsigned int nbits)
  184. {
  185. unsigned int len = BITS_TO_LONGS(nbits);
  186. unsigned long keep = 0, carry;
  187. int i;
  188. if (first % BITS_PER_LONG) {
  189. keep = src[first / BITS_PER_LONG] &
  190. (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
  191. }
  192. memmove(dst, src, len * sizeof(*dst));
  193. while (cut--) {
  194. for (i = first / BITS_PER_LONG; i < len; i++) {
  195. if (i < len - 1)
  196. carry = dst[i + 1] & 1UL;
  197. else
  198. carry = 0;
  199. dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));
  200. }
  201. }
  202. dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);
  203. dst[first / BITS_PER_LONG] |= keep;
  204. }
  205. EXPORT_SYMBOL(bitmap_cut);
  206. bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  207. const unsigned long *bitmap2, unsigned int bits)
  208. {
  209. unsigned int k;
  210. unsigned int lim = bits/BITS_PER_LONG;
  211. unsigned long result = 0;
  212. for (k = 0; k < lim; k++)
  213. result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  214. if (bits % BITS_PER_LONG)
  215. result |= (dst[k] = bitmap1[k] & bitmap2[k] &
  216. BITMAP_LAST_WORD_MASK(bits));
  217. return result != 0;
  218. }
  219. EXPORT_SYMBOL(__bitmap_and);
  220. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  221. const unsigned long *bitmap2, unsigned int bits)
  222. {
  223. unsigned int k;
  224. unsigned int nr = BITS_TO_LONGS(bits);
  225. for (k = 0; k < nr; k++)
  226. dst[k] = bitmap1[k] | bitmap2[k];
  227. }
  228. EXPORT_SYMBOL(__bitmap_or);
  229. void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  230. const unsigned long *bitmap2, unsigned int bits)
  231. {
  232. unsigned int k;
  233. unsigned int nr = BITS_TO_LONGS(bits);
  234. for (k = 0; k < nr; k++)
  235. dst[k] = bitmap1[k] ^ bitmap2[k];
  236. }
  237. EXPORT_SYMBOL(__bitmap_xor);
  238. bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  239. const unsigned long *bitmap2, unsigned int bits)
  240. {
  241. unsigned int k;
  242. unsigned int lim = bits/BITS_PER_LONG;
  243. unsigned long result = 0;
  244. for (k = 0; k < lim; k++)
  245. result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  246. if (bits % BITS_PER_LONG)
  247. result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
  248. BITMAP_LAST_WORD_MASK(bits));
  249. return result != 0;
  250. }
  251. EXPORT_SYMBOL(__bitmap_andnot);
  252. void __bitmap_replace(unsigned long *dst,
  253. const unsigned long *old, const unsigned long *new,
  254. const unsigned long *mask, unsigned int nbits)
  255. {
  256. unsigned int k;
  257. unsigned int nr = BITS_TO_LONGS(nbits);
  258. for (k = 0; k < nr; k++)
  259. dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);
  260. }
  261. EXPORT_SYMBOL(__bitmap_replace);
  262. bool __bitmap_intersects(const unsigned long *bitmap1,
  263. const unsigned long *bitmap2, unsigned int bits)
  264. {
  265. unsigned int k, lim = bits/BITS_PER_LONG;
  266. for (k = 0; k < lim; ++k)
  267. if (bitmap1[k] & bitmap2[k])
  268. return true;
  269. if (bits % BITS_PER_LONG)
  270. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  271. return true;
  272. return false;
  273. }
  274. EXPORT_SYMBOL(__bitmap_intersects);
  275. bool __bitmap_subset(const unsigned long *bitmap1,
  276. const unsigned long *bitmap2, unsigned int bits)
  277. {
  278. unsigned int k, lim = bits/BITS_PER_LONG;
  279. for (k = 0; k < lim; ++k)
  280. if (bitmap1[k] & ~bitmap2[k])
  281. return false;
  282. if (bits % BITS_PER_LONG)
  283. if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  284. return false;
  285. return true;
  286. }
  287. EXPORT_SYMBOL(__bitmap_subset);
  288. #define BITMAP_WEIGHT(FETCH, bits) \
  289. ({ \
  290. unsigned int __bits = (bits), idx, w = 0; \
  291. \
  292. for (idx = 0; idx < __bits / BITS_PER_LONG; idx++) \
  293. w += hweight_long(FETCH); \
  294. \
  295. if (__bits % BITS_PER_LONG) \
  296. w += hweight_long((FETCH) & BITMAP_LAST_WORD_MASK(__bits)); \
  297. \
  298. w; \
  299. })
  300. unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
  301. {
  302. return BITMAP_WEIGHT(bitmap[idx], bits);
  303. }
  304. EXPORT_SYMBOL(__bitmap_weight);
  305. unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
  306. const unsigned long *bitmap2, unsigned int bits)
  307. {
  308. return BITMAP_WEIGHT(bitmap1[idx] & bitmap2[idx], bits);
  309. }
  310. EXPORT_SYMBOL(__bitmap_weight_and);
  311. unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
  312. const unsigned long *bitmap2, unsigned int bits)
  313. {
  314. return BITMAP_WEIGHT(bitmap1[idx] & ~bitmap2[idx], bits);
  315. }
  316. EXPORT_SYMBOL(__bitmap_weight_andnot);
  317. unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1,
  318. const unsigned long *bitmap2, unsigned int bits)
  319. {
  320. return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] | bitmap2[idx]; dst[idx]; }), bits);
  321. }
  322. void __bitmap_set(unsigned long *map, unsigned int start, int len)
  323. {
  324. unsigned long *p = map + BIT_WORD(start);
  325. const unsigned int size = start + len;
  326. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  327. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  328. while (len - bits_to_set >= 0) {
  329. *p |= mask_to_set;
  330. len -= bits_to_set;
  331. bits_to_set = BITS_PER_LONG;
  332. mask_to_set = ~0UL;
  333. p++;
  334. }
  335. if (len) {
  336. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  337. *p |= mask_to_set;
  338. }
  339. }
  340. EXPORT_SYMBOL(__bitmap_set);
  341. void __bitmap_clear(unsigned long *map, unsigned int start, int len)
  342. {
  343. unsigned long *p = map + BIT_WORD(start);
  344. const unsigned int size = start + len;
  345. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  346. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  347. while (len - bits_to_clear >= 0) {
  348. *p &= ~mask_to_clear;
  349. len -= bits_to_clear;
  350. bits_to_clear = BITS_PER_LONG;
  351. mask_to_clear = ~0UL;
  352. p++;
  353. }
  354. if (len) {
  355. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  356. *p &= ~mask_to_clear;
  357. }
  358. }
  359. EXPORT_SYMBOL(__bitmap_clear);
  360. /**
  361. * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
  362. * @map: The address to base the search on
  363. * @size: The bitmap size in bits
  364. * @start: The bitnumber to start searching at
  365. * @nr: The number of zeroed bits we're looking for
  366. * @align_mask: Alignment mask for zero area
  367. * @align_offset: Alignment offset for zero area.
  368. *
  369. * The @align_mask should be one less than a power of 2; the effect is that
  370. * the bit offset of all zero areas this function finds plus @align_offset
  371. * is multiple of that power of 2.
  372. */
  373. unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  374. unsigned long size,
  375. unsigned long start,
  376. unsigned int nr,
  377. unsigned long align_mask,
  378. unsigned long align_offset)
  379. {
  380. unsigned long index, end, i;
  381. again:
  382. index = find_next_zero_bit(map, size, start);
  383. /* Align allocation */
  384. index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
  385. end = index + nr;
  386. if (end > size)
  387. return end;
  388. i = find_next_bit(map, end, index);
  389. if (i < end) {
  390. start = i + 1;
  391. goto again;
  392. }
  393. return index;
  394. }
  395. EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
  396. /**
  397. * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
  398. * @buf: pointer to a bitmap
  399. * @pos: a bit position in @buf (0 <= @pos < @nbits)
  400. * @nbits: number of valid bit positions in @buf
  401. *
  402. * Map the bit at position @pos in @buf (of length @nbits) to the
  403. * ordinal of which set bit it is. If it is not set or if @pos
  404. * is not a valid bit position, map to -1.
  405. *
  406. * If for example, just bits 4 through 7 are set in @buf, then @pos
  407. * values 4 through 7 will get mapped to 0 through 3, respectively,
  408. * and other @pos values will get mapped to -1. When @pos value 7
  409. * gets mapped to (returns) @ord value 3 in this example, that means
  410. * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  411. *
  412. * The bit positions 0 through @bits are valid positions in @buf.
  413. */
  414. static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
  415. {
  416. if (pos >= nbits || !test_bit(pos, buf))
  417. return -1;
  418. return bitmap_weight(buf, pos);
  419. }
  420. /**
  421. * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  422. * @dst: remapped result
  423. * @src: subset to be remapped
  424. * @old: defines domain of map
  425. * @new: defines range of map
  426. * @nbits: number of bits in each of these bitmaps
  427. *
  428. * Let @old and @new define a mapping of bit positions, such that
  429. * whatever position is held by the n-th set bit in @old is mapped
  430. * to the n-th set bit in @new. In the more general case, allowing
  431. * for the possibility that the weight 'w' of @new is less than the
  432. * weight of @old, map the position of the n-th set bit in @old to
  433. * the position of the m-th set bit in @new, where m == n % w.
  434. *
  435. * If either of the @old and @new bitmaps are empty, or if @src and
  436. * @dst point to the same location, then this routine copies @src
  437. * to @dst.
  438. *
  439. * The positions of unset bits in @old are mapped to themselves
  440. * (the identity map).
  441. *
  442. * Apply the above specified mapping to @src, placing the result in
  443. * @dst, clearing any bits previously set in @dst.
  444. *
  445. * For example, lets say that @old has bits 4 through 7 set, and
  446. * @new has bits 12 through 15 set. This defines the mapping of bit
  447. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  448. * bit positions unchanged. So if say @src comes into this routine
  449. * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
  450. * 13 and 15 set.
  451. */
  452. void bitmap_remap(unsigned long *dst, const unsigned long *src,
  453. const unsigned long *old, const unsigned long *new,
  454. unsigned int nbits)
  455. {
  456. unsigned int oldbit, w;
  457. if (dst == src) /* following doesn't handle inplace remaps */
  458. return;
  459. bitmap_zero(dst, nbits);
  460. w = bitmap_weight(new, nbits);
  461. for_each_set_bit(oldbit, src, nbits) {
  462. int n = bitmap_pos_to_ord(old, oldbit, nbits);
  463. if (n < 0 || w == 0)
  464. set_bit(oldbit, dst); /* identity map */
  465. else
  466. set_bit(find_nth_bit(new, nbits, n % w), dst);
  467. }
  468. }
  469. EXPORT_SYMBOL(bitmap_remap);
  470. /**
  471. * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
  472. * @oldbit: bit position to be mapped
  473. * @old: defines domain of map
  474. * @new: defines range of map
  475. * @bits: number of bits in each of these bitmaps
  476. *
  477. * Let @old and @new define a mapping of bit positions, such that
  478. * whatever position is held by the n-th set bit in @old is mapped
  479. * to the n-th set bit in @new. In the more general case, allowing
  480. * for the possibility that the weight 'w' of @new is less than the
  481. * weight of @old, map the position of the n-th set bit in @old to
  482. * the position of the m-th set bit in @new, where m == n % w.
  483. *
  484. * The positions of unset bits in @old are mapped to themselves
  485. * (the identity map).
  486. *
  487. * Apply the above specified mapping to bit position @oldbit, returning
  488. * the new bit position.
  489. *
  490. * For example, lets say that @old has bits 4 through 7 set, and
  491. * @new has bits 12 through 15 set. This defines the mapping of bit
  492. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  493. * bit positions unchanged. So if say @oldbit is 5, then this routine
  494. * returns 13.
  495. */
  496. int bitmap_bitremap(int oldbit, const unsigned long *old,
  497. const unsigned long *new, int bits)
  498. {
  499. int w = bitmap_weight(new, bits);
  500. int n = bitmap_pos_to_ord(old, oldbit, bits);
  501. if (n < 0 || w == 0)
  502. return oldbit;
  503. else
  504. return find_nth_bit(new, bits, n % w);
  505. }
  506. EXPORT_SYMBOL(bitmap_bitremap);
  507. #ifdef CONFIG_NUMA
  508. /**
  509. * bitmap_onto - translate one bitmap relative to another
  510. * @dst: resulting translated bitmap
  511. * @orig: original untranslated bitmap
  512. * @relmap: bitmap relative to which translated
  513. * @bits: number of bits in each of these bitmaps
  514. *
  515. * Set the n-th bit of @dst iff there exists some m such that the
  516. * n-th bit of @relmap is set, the m-th bit of @orig is set, and
  517. * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
  518. * (If you understood the previous sentence the first time your
  519. * read it, you're overqualified for your current job.)
  520. *
  521. * In other words, @orig is mapped onto (surjectively) @dst,
  522. * using the map { <n, m> | the n-th bit of @relmap is the
  523. * m-th set bit of @relmap }.
  524. *
  525. * Any set bits in @orig above bit number W, where W is the
  526. * weight of (number of set bits in) @relmap are mapped nowhere.
  527. * In particular, if for all bits m set in @orig, m >= W, then
  528. * @dst will end up empty. In situations where the possibility
  529. * of such an empty result is not desired, one way to avoid it is
  530. * to use the bitmap_fold() operator, below, to first fold the
  531. * @orig bitmap over itself so that all its set bits x are in the
  532. * range 0 <= x < W. The bitmap_fold() operator does this by
  533. * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
  534. *
  535. * Example [1] for bitmap_onto():
  536. * Let's say @relmap has bits 30-39 set, and @orig has bits
  537. * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
  538. * @dst will have bits 31, 33, 35, 37 and 39 set.
  539. *
  540. * When bit 0 is set in @orig, it means turn on the bit in
  541. * @dst corresponding to whatever is the first bit (if any)
  542. * that is turned on in @relmap. Since bit 0 was off in the
  543. * above example, we leave off that bit (bit 30) in @dst.
  544. *
  545. * When bit 1 is set in @orig (as in the above example), it
  546. * means turn on the bit in @dst corresponding to whatever
  547. * is the second bit that is turned on in @relmap. The second
  548. * bit in @relmap that was turned on in the above example was
  549. * bit 31, so we turned on bit 31 in @dst.
  550. *
  551. * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
  552. * because they were the 4th, 6th, 8th and 10th set bits
  553. * set in @relmap, and the 4th, 6th, 8th and 10th bits of
  554. * @orig (i.e. bits 3, 5, 7 and 9) were also set.
  555. *
  556. * When bit 11 is set in @orig, it means turn on the bit in
  557. * @dst corresponding to whatever is the twelfth bit that is
  558. * turned on in @relmap. In the above example, there were
  559. * only ten bits turned on in @relmap (30..39), so that bit
  560. * 11 was set in @orig had no affect on @dst.
  561. *
  562. * Example [2] for bitmap_fold() + bitmap_onto():
  563. * Let's say @relmap has these ten bits set::
  564. *
  565. * 40 41 42 43 45 48 53 61 74 95
  566. *
  567. * (for the curious, that's 40 plus the first ten terms of the
  568. * Fibonacci sequence.)
  569. *
  570. * Further lets say we use the following code, invoking
  571. * bitmap_fold() then bitmap_onto, as suggested above to
  572. * avoid the possibility of an empty @dst result::
  573. *
  574. * unsigned long *tmp; // a temporary bitmap's bits
  575. *
  576. * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
  577. * bitmap_onto(dst, tmp, relmap, bits);
  578. *
  579. * Then this table shows what various values of @dst would be, for
  580. * various @orig's. I list the zero-based positions of each set bit.
  581. * The tmp column shows the intermediate result, as computed by
  582. * using bitmap_fold() to fold the @orig bitmap modulo ten
  583. * (the weight of @relmap):
  584. *
  585. * =============== ============== =================
  586. * @orig tmp @dst
  587. * 0 0 40
  588. * 1 1 41
  589. * 9 9 95
  590. * 10 0 40 [#f1]_
  591. * 1 3 5 7 1 3 5 7 41 43 48 61
  592. * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
  593. * 0 9 18 27 0 9 8 7 40 61 74 95
  594. * 0 10 20 30 0 40
  595. * 0 11 22 33 0 1 2 3 40 41 42 43
  596. * 0 12 24 36 0 2 4 6 40 42 45 53
  597. * 78 102 211 1 2 8 41 42 74 [#f1]_
  598. * =============== ============== =================
  599. *
  600. * .. [#f1]
  601. *
  602. * For these marked lines, if we hadn't first done bitmap_fold()
  603. * into tmp, then the @dst result would have been empty.
  604. *
  605. * If either of @orig or @relmap is empty (no set bits), then @dst
  606. * will be returned empty.
  607. *
  608. * If (as explained above) the only set bits in @orig are in positions
  609. * m where m >= W, (where W is the weight of @relmap) then @dst will
  610. * once again be returned empty.
  611. *
  612. * All bits in @dst not set by the above rule are cleared.
  613. */
  614. void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  615. const unsigned long *relmap, unsigned int bits)
  616. {
  617. unsigned int n, m; /* same meaning as in above comment */
  618. if (dst == orig) /* following doesn't handle inplace mappings */
  619. return;
  620. bitmap_zero(dst, bits);
  621. /*
  622. * The following code is a more efficient, but less
  623. * obvious, equivalent to the loop:
  624. * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
  625. * n = find_nth_bit(orig, bits, m);
  626. * if (test_bit(m, orig))
  627. * set_bit(n, dst);
  628. * }
  629. */
  630. m = 0;
  631. for_each_set_bit(n, relmap, bits) {
  632. /* m == bitmap_pos_to_ord(relmap, n, bits) */
  633. if (test_bit(m, orig))
  634. set_bit(n, dst);
  635. m++;
  636. }
  637. }
  638. /**
  639. * bitmap_fold - fold larger bitmap into smaller, modulo specified size
  640. * @dst: resulting smaller bitmap
  641. * @orig: original larger bitmap
  642. * @sz: specified size
  643. * @nbits: number of bits in each of these bitmaps
  644. *
  645. * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
  646. * Clear all other bits in @dst. See further the comment and
  647. * Example [2] for bitmap_onto() for why and how to use this.
  648. */
  649. void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  650. unsigned int sz, unsigned int nbits)
  651. {
  652. unsigned int oldbit;
  653. if (dst == orig) /* following doesn't handle inplace mappings */
  654. return;
  655. bitmap_zero(dst, nbits);
  656. for_each_set_bit(oldbit, orig, nbits)
  657. set_bit(oldbit % sz, dst);
  658. }
  659. #endif /* CONFIG_NUMA */
  660. unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
  661. {
  662. return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
  663. flags);
  664. }
  665. EXPORT_SYMBOL(bitmap_alloc);
  666. unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
  667. {
  668. return bitmap_alloc(nbits, flags | __GFP_ZERO);
  669. }
  670. EXPORT_SYMBOL(bitmap_zalloc);
  671. unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node)
  672. {
  673. return kmalloc_array_node(BITS_TO_LONGS(nbits), sizeof(unsigned long),
  674. flags, node);
  675. }
  676. EXPORT_SYMBOL(bitmap_alloc_node);
  677. unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node)
  678. {
  679. return bitmap_alloc_node(nbits, flags | __GFP_ZERO, node);
  680. }
  681. EXPORT_SYMBOL(bitmap_zalloc_node);
  682. void bitmap_free(const unsigned long *bitmap)
  683. {
  684. kfree(bitmap);
  685. }
  686. EXPORT_SYMBOL(bitmap_free);
  687. static void devm_bitmap_free(void *data)
  688. {
  689. unsigned long *bitmap = data;
  690. bitmap_free(bitmap);
  691. }
  692. unsigned long *devm_bitmap_alloc(struct device *dev,
  693. unsigned int nbits, gfp_t flags)
  694. {
  695. unsigned long *bitmap;
  696. int ret;
  697. bitmap = bitmap_alloc(nbits, flags);
  698. if (!bitmap)
  699. return NULL;
  700. ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
  701. if (ret)
  702. return NULL;
  703. return bitmap;
  704. }
  705. EXPORT_SYMBOL_GPL(devm_bitmap_alloc);
  706. unsigned long *devm_bitmap_zalloc(struct device *dev,
  707. unsigned int nbits, gfp_t flags)
  708. {
  709. return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
  710. }
  711. EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);
  712. #if BITS_PER_LONG == 64
  713. /**
  714. * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
  715. * @bitmap: array of unsigned longs, the destination bitmap
  716. * @buf: array of u32 (in host byte order), the source bitmap
  717. * @nbits: number of bits in @bitmap
  718. */
  719. void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
  720. {
  721. unsigned int i, halfwords;
  722. halfwords = DIV_ROUND_UP(nbits, 32);
  723. for (i = 0; i < halfwords; i++) {
  724. bitmap[i/2] = (unsigned long) buf[i];
  725. if (++i < halfwords)
  726. bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
  727. }
  728. /* Clear tail bits in last word beyond nbits. */
  729. if (nbits % BITS_PER_LONG)
  730. bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
  731. }
  732. EXPORT_SYMBOL(bitmap_from_arr32);
  733. /**
  734. * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
  735. * @buf: array of u32 (in host byte order), the dest bitmap
  736. * @bitmap: array of unsigned longs, the source bitmap
  737. * @nbits: number of bits in @bitmap
  738. */
  739. void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
  740. {
  741. unsigned int i, halfwords;
  742. halfwords = DIV_ROUND_UP(nbits, 32);
  743. for (i = 0; i < halfwords; i++) {
  744. buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
  745. if (++i < halfwords)
  746. buf[i] = (u32) (bitmap[i/2] >> 32);
  747. }
  748. /* Clear tail bits in last element of array beyond nbits. */
  749. if (nbits % BITS_PER_LONG)
  750. buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
  751. }
  752. EXPORT_SYMBOL(bitmap_to_arr32);
  753. #endif
  754. #if BITS_PER_LONG == 32
  755. /**
  756. * bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap
  757. * @bitmap: array of unsigned longs, the destination bitmap
  758. * @buf: array of u64 (in host byte order), the source bitmap
  759. * @nbits: number of bits in @bitmap
  760. */
  761. void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits)
  762. {
  763. int n;
  764. for (n = nbits; n > 0; n -= 64) {
  765. u64 val = *buf++;
  766. *bitmap++ = val;
  767. if (n > 32)
  768. *bitmap++ = val >> 32;
  769. }
  770. /*
  771. * Clear tail bits in the last word beyond nbits.
  772. *
  773. * Negative index is OK because here we point to the word next
  774. * to the last word of the bitmap, except for nbits == 0, which
  775. * is tested implicitly.
  776. */
  777. if (nbits % BITS_PER_LONG)
  778. bitmap[-1] &= BITMAP_LAST_WORD_MASK(nbits);
  779. }
  780. EXPORT_SYMBOL(bitmap_from_arr64);
  781. /**
  782. * bitmap_to_arr64 - copy the contents of bitmap to a u64 array of bits
  783. * @buf: array of u64 (in host byte order), the dest bitmap
  784. * @bitmap: array of unsigned longs, the source bitmap
  785. * @nbits: number of bits in @bitmap
  786. */
  787. void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits)
  788. {
  789. const unsigned long *end = bitmap + BITS_TO_LONGS(nbits);
  790. while (bitmap < end) {
  791. *buf = *bitmap++;
  792. if (bitmap < end)
  793. *buf |= (u64)(*bitmap++) << 32;
  794. buf++;
  795. }
  796. /* Clear tail bits in the last element of array beyond nbits. */
  797. if (nbits % 64)
  798. buf[-1] &= GENMASK_ULL((nbits - 1) % 64, 0);
  799. }
  800. EXPORT_SYMBOL(bitmap_to_arr64);
  801. #endif