bitmap.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /*
  2. * lib/bitmap.c
  3. * Helper functions for bitmap.h.
  4. *
  5. * This source code is licensed under the GNU General Public License,
  6. * Version 2. See the file COPYING for more details.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/thread_info.h>
  10. #include <linux/ctype.h>
  11. #include <linux/errno.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/bitops.h>
  14. #include <linux/bug.h>
  15. #include <asm/page.h>
  16. #include <asm/uaccess.h>
  17. /*
  18. * bitmaps provide an array of bits, implemented using an an
  19. * array of unsigned longs. The number of valid bits in a
  20. * given bitmap does _not_ need to be an exact multiple of
  21. * BITS_PER_LONG.
  22. *
  23. * The possible unused bits in the last, partially used word
  24. * of a bitmap are 'don't care'. The implementation makes
  25. * no particular effort to keep them zero. It ensures that
  26. * their value will not affect the results of any operation.
  27. * The bitmap operations that return Boolean (bitmap_empty,
  28. * for example) or scalar (bitmap_weight, for example) results
  29. * carefully filter out these unused bits from impacting their
  30. * results.
  31. *
  32. * These operations actually hold to a slightly stronger rule:
  33. * if you don't input any bitmaps to these ops that have some
  34. * unused bits set, then they won't output any set unused bits
  35. * in output bitmaps.
  36. *
  37. * The byte ordering of bitmaps is more natural on little
  38. * endian architectures. See the big-endian headers
  39. * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
  40. * for the best explanations of this ordering.
  41. */
  42. int __bitmap_equal(const unsigned long *bitmap1,
  43. const unsigned long *bitmap2, unsigned int bits)
  44. {
  45. unsigned int k, lim = bits/BITS_PER_LONG;
  46. for (k = 0; k < lim; ++k)
  47. if (bitmap1[k] != bitmap2[k])
  48. return 0;
  49. if (bits % BITS_PER_LONG)
  50. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  51. return 0;
  52. return 1;
  53. }
  54. EXPORT_SYMBOL(__bitmap_equal);
  55. void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
  56. {
  57. unsigned int k, lim = bits/BITS_PER_LONG;
  58. for (k = 0; k < lim; ++k)
  59. dst[k] = ~src[k];
  60. if (bits % BITS_PER_LONG)
  61. dst[k] = ~src[k];
  62. }
  63. EXPORT_SYMBOL(__bitmap_complement);
  64. /**
  65. * __bitmap_shift_right - logical right shift of the bits in a bitmap
  66. * @dst : destination bitmap
  67. * @src : source bitmap
  68. * @shift : shift by this many bits
  69. * @nbits : bitmap size, in bits
  70. *
  71. * Shifting right (dividing) means moving bits in the MS -> LS bit
  72. * direction. Zeros are fed into the vacated MS positions and the
  73. * LS bits shifted off the bottom are lost.
  74. */
  75. void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  76. unsigned shift, unsigned nbits)
  77. {
  78. unsigned k, lim = BITS_TO_LONGS(nbits);
  79. unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  80. unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
  81. for (k = 0; off + k < lim; ++k) {
  82. unsigned long upper, lower;
  83. /*
  84. * If shift is not word aligned, take lower rem bits of
  85. * word above and make them the top rem bits of result.
  86. */
  87. if (!rem || off + k + 1 >= lim)
  88. upper = 0;
  89. else {
  90. upper = src[off + k + 1];
  91. if (off + k + 1 == lim - 1)
  92. upper &= mask;
  93. upper <<= (BITS_PER_LONG - rem);
  94. }
  95. lower = src[off + k];
  96. if (off + k == lim - 1)
  97. lower &= mask;
  98. lower >>= rem;
  99. dst[k] = lower | upper;
  100. }
  101. if (off)
  102. memset(&dst[lim - off], 0, off*sizeof(unsigned long));
  103. }
  104. EXPORT_SYMBOL(__bitmap_shift_right);
  105. /**
  106. * __bitmap_shift_left - logical left shift of the bits in a bitmap
  107. * @dst : destination bitmap
  108. * @src : source bitmap
  109. * @shift : shift by this many bits
  110. * @nbits : bitmap size, in bits
  111. *
  112. * Shifting left (multiplying) means moving bits in the LS -> MS
  113. * direction. Zeros are fed into the vacated LS bit positions
  114. * and those MS bits shifted off the top are lost.
  115. */
  116. void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  117. unsigned int shift, unsigned int nbits)
  118. {
  119. int k;
  120. unsigned int lim = BITS_TO_LONGS(nbits);
  121. unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
  122. for (k = lim - off - 1; k >= 0; --k) {
  123. unsigned long upper, lower;
  124. /*
  125. * If shift is not word aligned, take upper rem bits of
  126. * word below and make them the bottom rem bits of result.
  127. */
  128. if (rem && k > 0)
  129. lower = src[k - 1] >> (BITS_PER_LONG - rem);
  130. else
  131. lower = 0;
  132. upper = src[k] << rem;
  133. dst[k + off] = lower | upper;
  134. }
  135. if (off)
  136. memset(dst, 0, off*sizeof(unsigned long));
  137. }
  138. EXPORT_SYMBOL(__bitmap_shift_left);
  139. int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  140. const unsigned long *bitmap2, unsigned int bits)
  141. {
  142. unsigned int k;
  143. unsigned int lim = bits/BITS_PER_LONG;
  144. unsigned long result = 0;
  145. for (k = 0; k < lim; k++)
  146. result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  147. if (bits % BITS_PER_LONG)
  148. result |= (dst[k] = bitmap1[k] & bitmap2[k] &
  149. BITMAP_LAST_WORD_MASK(bits));
  150. return result != 0;
  151. }
  152. EXPORT_SYMBOL(__bitmap_and);
  153. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  154. const unsigned long *bitmap2, unsigned int bits)
  155. {
  156. unsigned int k;
  157. unsigned int nr = BITS_TO_LONGS(bits);
  158. for (k = 0; k < nr; k++)
  159. dst[k] = bitmap1[k] | bitmap2[k];
  160. }
  161. EXPORT_SYMBOL(__bitmap_or);
  162. void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  163. const unsigned long *bitmap2, unsigned int bits)
  164. {
  165. unsigned int k;
  166. unsigned int nr = BITS_TO_LONGS(bits);
  167. for (k = 0; k < nr; k++)
  168. dst[k] = bitmap1[k] ^ bitmap2[k];
  169. }
  170. EXPORT_SYMBOL(__bitmap_xor);
  171. int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  172. const unsigned long *bitmap2, unsigned int bits)
  173. {
  174. unsigned int k;
  175. unsigned int lim = bits/BITS_PER_LONG;
  176. unsigned long result = 0;
  177. for (k = 0; k < lim; k++)
  178. result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  179. if (bits % BITS_PER_LONG)
  180. result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
  181. BITMAP_LAST_WORD_MASK(bits));
  182. return result != 0;
  183. }
  184. EXPORT_SYMBOL(__bitmap_andnot);
  185. int __bitmap_intersects(const unsigned long *bitmap1,
  186. const unsigned long *bitmap2, unsigned int bits)
  187. {
  188. unsigned int k, lim = bits/BITS_PER_LONG;
  189. for (k = 0; k < lim; ++k)
  190. if (bitmap1[k] & bitmap2[k])
  191. return 1;
  192. if (bits % BITS_PER_LONG)
  193. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  194. return 1;
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(__bitmap_intersects);
  198. int __bitmap_subset(const unsigned long *bitmap1,
  199. const unsigned long *bitmap2, unsigned int bits)
  200. {
  201. unsigned int k, lim = bits/BITS_PER_LONG;
  202. for (k = 0; k < lim; ++k)
  203. if (bitmap1[k] & ~bitmap2[k])
  204. return 0;
  205. if (bits % BITS_PER_LONG)
  206. if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
  207. return 0;
  208. return 1;
  209. }
  210. EXPORT_SYMBOL(__bitmap_subset);
  211. int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
  212. {
  213. unsigned int k, lim = bits/BITS_PER_LONG;
  214. int w = 0;
  215. for (k = 0; k < lim; k++)
  216. w += hweight_long(bitmap[k]);
  217. if (bits % BITS_PER_LONG)
  218. w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
  219. return w;
  220. }
  221. EXPORT_SYMBOL(__bitmap_weight);
  222. void bitmap_set(unsigned long *map, unsigned int start, int len)
  223. {
  224. unsigned long *p = map + BIT_WORD(start);
  225. const unsigned int size = start + len;
  226. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  227. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  228. while (len - bits_to_set >= 0) {
  229. *p |= mask_to_set;
  230. len -= bits_to_set;
  231. bits_to_set = BITS_PER_LONG;
  232. mask_to_set = ~0UL;
  233. p++;
  234. }
  235. if (len) {
  236. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  237. *p |= mask_to_set;
  238. }
  239. }
  240. EXPORT_SYMBOL(bitmap_set);
  241. void bitmap_clear(unsigned long *map, unsigned int start, int len)
  242. {
  243. unsigned long *p = map + BIT_WORD(start);
  244. const unsigned int size = start + len;
  245. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  246. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  247. while (len - bits_to_clear >= 0) {
  248. *p &= ~mask_to_clear;
  249. len -= bits_to_clear;
  250. bits_to_clear = BITS_PER_LONG;
  251. mask_to_clear = ~0UL;
  252. p++;
  253. }
  254. if (len) {
  255. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  256. *p &= ~mask_to_clear;
  257. }
  258. }
  259. EXPORT_SYMBOL(bitmap_clear);
  260. /**
  261. * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
  262. * @map: The address to base the search on
  263. * @size: The bitmap size in bits
  264. * @start: The bitnumber to start searching at
  265. * @nr: The number of zeroed bits we're looking for
  266. * @align_mask: Alignment mask for zero area
  267. * @align_offset: Alignment offset for zero area.
  268. *
  269. * The @align_mask should be one less than a power of 2; the effect is that
  270. * the bit offset of all zero areas this function finds plus @align_offset
  271. * is multiple of that power of 2.
  272. */
  273. unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  274. unsigned long size,
  275. unsigned long start,
  276. unsigned int nr,
  277. unsigned long align_mask,
  278. unsigned long align_offset)
  279. {
  280. unsigned long index, end, i;
  281. again:
  282. index = find_next_zero_bit(map, size, start);
  283. /* Align allocation */
  284. index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
  285. end = index + nr;
  286. if (end > size)
  287. return end;
  288. i = find_next_bit(map, end, index);
  289. if (i < end) {
  290. start = i + 1;
  291. goto again;
  292. }
  293. return index;
  294. }
  295. EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
  296. /*
  297. * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
  298. * second version by Paul Jackson, third by Joe Korty.
  299. */
  300. #define CHUNKSZ 32
  301. #define nbits_to_hold_value(val) fls(val)
  302. #define BASEDEC 10 /* fancier cpuset lists input in decimal */
  303. /**
  304. * __bitmap_parse - convert an ASCII hex string into a bitmap.
  305. * @buf: pointer to buffer containing string.
  306. * @buflen: buffer size in bytes. If string is smaller than this
  307. * then it must be terminated with a \0.
  308. * @is_user: location of buffer, 0 indicates kernel space
  309. * @maskp: pointer to bitmap array that will contain result.
  310. * @nmaskbits: size of bitmap, in bits.
  311. *
  312. * Commas group hex digits into chunks. Each chunk defines exactly 32
  313. * bits of the resultant bitmask. No chunk may specify a value larger
  314. * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
  315. * then leading 0-bits are prepended. %-EINVAL is returned for illegal
  316. * characters and for grouping errors such as "1,,5", ",44", "," and "".
  317. * Leading and trailing whitespace accepted, but not embedded whitespace.
  318. */
  319. int __bitmap_parse(const char *buf, unsigned int buflen,
  320. int is_user, unsigned long *maskp,
  321. int nmaskbits)
  322. {
  323. int c, old_c, totaldigits, ndigits, nchunks, nbits;
  324. u32 chunk;
  325. const char __user __force *ubuf = (const char __user __force *)buf;
  326. bitmap_zero(maskp, nmaskbits);
  327. nchunks = nbits = totaldigits = c = 0;
  328. do {
  329. chunk = 0;
  330. ndigits = totaldigits;
  331. /* Get the next chunk of the bitmap */
  332. while (buflen) {
  333. old_c = c;
  334. if (is_user) {
  335. if (__get_user(c, ubuf++))
  336. return -EFAULT;
  337. }
  338. else
  339. c = *buf++;
  340. buflen--;
  341. if (isspace(c))
  342. continue;
  343. /*
  344. * If the last character was a space and the current
  345. * character isn't '\0', we've got embedded whitespace.
  346. * This is a no-no, so throw an error.
  347. */
  348. if (totaldigits && c && isspace(old_c))
  349. return -EINVAL;
  350. /* A '\0' or a ',' signal the end of the chunk */
  351. if (c == '\0' || c == ',')
  352. break;
  353. if (!isxdigit(c))
  354. return -EINVAL;
  355. /*
  356. * Make sure there are at least 4 free bits in 'chunk'.
  357. * If not, this hexdigit will overflow 'chunk', so
  358. * throw an error.
  359. */
  360. if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1))
  361. return -EOVERFLOW;
  362. chunk = (chunk << 4) | hex_to_bin(c);
  363. totaldigits++;
  364. }
  365. if (ndigits == totaldigits)
  366. return -EINVAL;
  367. if (nchunks == 0 && chunk == 0)
  368. continue;
  369. __bitmap_shift_left(maskp, maskp, CHUNKSZ, nmaskbits);
  370. *maskp |= chunk;
  371. nchunks++;
  372. nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ;
  373. if (nbits > nmaskbits)
  374. return -EOVERFLOW;
  375. } while (buflen && c == ',');
  376. return 0;
  377. }
  378. EXPORT_SYMBOL(__bitmap_parse);
  379. /**
  380. * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
  381. *
  382. * @ubuf: pointer to user buffer containing string.
  383. * @ulen: buffer size in bytes. If string is smaller than this
  384. * then it must be terminated with a \0.
  385. * @maskp: pointer to bitmap array that will contain result.
  386. * @nmaskbits: size of bitmap, in bits.
  387. *
  388. * Wrapper for __bitmap_parse(), providing it with user buffer.
  389. *
  390. * We cannot have this as an inline function in bitmap.h because it needs
  391. * linux/uaccess.h to get the access_ok() declaration and this causes
  392. * cyclic dependencies.
  393. */
  394. int bitmap_parse_user(const char __user *ubuf,
  395. unsigned int ulen, unsigned long *maskp,
  396. int nmaskbits)
  397. {
  398. if (!access_ok(VERIFY_READ, ubuf, ulen))
  399. return -EFAULT;
  400. return __bitmap_parse((const char __force *)ubuf,
  401. ulen, 1, maskp, nmaskbits);
  402. }
  403. EXPORT_SYMBOL(bitmap_parse_user);
  404. /**
  405. * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
  406. * @list: indicates whether the bitmap must be list
  407. * @buf: page aligned buffer into which string is placed
  408. * @maskp: pointer to bitmap to convert
  409. * @nmaskbits: size of bitmap, in bits
  410. *
  411. * Output format is a comma-separated list of decimal numbers and
  412. * ranges if list is specified or hex digits grouped into comma-separated
  413. * sets of 8 digits/set. Returns the number of characters written to buf.
  414. *
  415. * It is assumed that @buf is a pointer into a PAGE_SIZE area and that
  416. * sufficient storage remains at @buf to accommodate the
  417. * bitmap_print_to_pagebuf() output.
  418. */
  419. int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
  420. int nmaskbits)
  421. {
  422. ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
  423. int n = 0;
  424. if (len > 1)
  425. n = list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
  426. scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
  427. return n;
  428. }
  429. EXPORT_SYMBOL(bitmap_print_to_pagebuf);
  430. /**
  431. * __bitmap_parselist - convert list format ASCII string to bitmap
  432. * @buf: read nul-terminated user string from this buffer
  433. * @buflen: buffer size in bytes. If string is smaller than this
  434. * then it must be terminated with a \0.
  435. * @is_user: location of buffer, 0 indicates kernel space
  436. * @maskp: write resulting mask here
  437. * @nmaskbits: number of bits in mask to be written
  438. *
  439. * Input format is a comma-separated list of decimal numbers and
  440. * ranges. Consecutively set bits are shown as two hyphen-separated
  441. * decimal numbers, the smallest and largest bit numbers set in
  442. * the range.
  443. *
  444. * Returns 0 on success, -errno on invalid input strings.
  445. * Error values:
  446. * %-EINVAL: second number in range smaller than first
  447. * %-EINVAL: invalid character in string
  448. * %-ERANGE: bit number specified too large for mask
  449. */
  450. static int __bitmap_parselist(const char *buf, unsigned int buflen,
  451. int is_user, unsigned long *maskp,
  452. int nmaskbits)
  453. {
  454. unsigned a, b;
  455. int c, old_c, totaldigits, ndigits;
  456. const char __user __force *ubuf = (const char __user __force *)buf;
  457. int at_start, in_range;
  458. totaldigits = c = 0;
  459. bitmap_zero(maskp, nmaskbits);
  460. do {
  461. at_start = 1;
  462. in_range = 0;
  463. a = b = 0;
  464. ndigits = totaldigits;
  465. /* Get the next cpu# or a range of cpu#'s */
  466. while (buflen) {
  467. old_c = c;
  468. if (is_user) {
  469. if (__get_user(c, ubuf++))
  470. return -EFAULT;
  471. } else
  472. c = *buf++;
  473. buflen--;
  474. if (isspace(c))
  475. continue;
  476. /* A '\0' or a ',' signal the end of a cpu# or range */
  477. if (c == '\0' || c == ',')
  478. break;
  479. /*
  480. * whitespaces between digits are not allowed,
  481. * but it's ok if whitespaces are on head or tail.
  482. * when old_c is whilespace,
  483. * if totaldigits == ndigits, whitespace is on head.
  484. * if whitespace is on tail, it should not run here.
  485. * as c was ',' or '\0',
  486. * the last code line has broken the current loop.
  487. */
  488. if ((totaldigits != ndigits) && isspace(old_c))
  489. return -EINVAL;
  490. if (c == '-') {
  491. if (at_start || in_range)
  492. return -EINVAL;
  493. b = 0;
  494. in_range = 1;
  495. at_start = 1;
  496. continue;
  497. }
  498. if (!isdigit(c))
  499. return -EINVAL;
  500. b = b * 10 + (c - '0');
  501. if (!in_range)
  502. a = b;
  503. at_start = 0;
  504. totaldigits++;
  505. }
  506. if (ndigits == totaldigits)
  507. continue;
  508. /* if no digit is after '-', it's wrong*/
  509. if (at_start && in_range)
  510. return -EINVAL;
  511. if (!(a <= b))
  512. return -EINVAL;
  513. if (b >= nmaskbits)
  514. return -ERANGE;
  515. while (a <= b) {
  516. set_bit(a, maskp);
  517. a++;
  518. }
  519. } while (buflen && c == ',');
  520. return 0;
  521. }
  522. int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits)
  523. {
  524. char *nl = strchrnul(bp, '\n');
  525. int len = nl - bp;
  526. return __bitmap_parselist(bp, len, 0, maskp, nmaskbits);
  527. }
  528. EXPORT_SYMBOL(bitmap_parselist);
  529. /**
  530. * bitmap_parselist_user()
  531. *
  532. * @ubuf: pointer to user buffer containing string.
  533. * @ulen: buffer size in bytes. If string is smaller than this
  534. * then it must be terminated with a \0.
  535. * @maskp: pointer to bitmap array that will contain result.
  536. * @nmaskbits: size of bitmap, in bits.
  537. *
  538. * Wrapper for bitmap_parselist(), providing it with user buffer.
  539. *
  540. * We cannot have this as an inline function in bitmap.h because it needs
  541. * linux/uaccess.h to get the access_ok() declaration and this causes
  542. * cyclic dependencies.
  543. */
  544. int bitmap_parselist_user(const char __user *ubuf,
  545. unsigned int ulen, unsigned long *maskp,
  546. int nmaskbits)
  547. {
  548. if (!access_ok(VERIFY_READ, ubuf, ulen))
  549. return -EFAULT;
  550. return __bitmap_parselist((const char __force *)ubuf,
  551. ulen, 1, maskp, nmaskbits);
  552. }
  553. EXPORT_SYMBOL(bitmap_parselist_user);
  554. /**
  555. * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
  556. * @buf: pointer to a bitmap
  557. * @pos: a bit position in @buf (0 <= @pos < @nbits)
  558. * @nbits: number of valid bit positions in @buf
  559. *
  560. * Map the bit at position @pos in @buf (of length @nbits) to the
  561. * ordinal of which set bit it is. If it is not set or if @pos
  562. * is not a valid bit position, map to -1.
  563. *
  564. * If for example, just bits 4 through 7 are set in @buf, then @pos
  565. * values 4 through 7 will get mapped to 0 through 3, respectively,
  566. * and other @pos values will get mapped to -1. When @pos value 7
  567. * gets mapped to (returns) @ord value 3 in this example, that means
  568. * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
  569. *
  570. * The bit positions 0 through @bits are valid positions in @buf.
  571. */
  572. static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
  573. {
  574. if (pos >= nbits || !test_bit(pos, buf))
  575. return -1;
  576. return __bitmap_weight(buf, pos);
  577. }
  578. /**
  579. * bitmap_ord_to_pos - find position of n-th set bit in bitmap
  580. * @buf: pointer to bitmap
  581. * @ord: ordinal bit position (n-th set bit, n >= 0)
  582. * @nbits: number of valid bit positions in @buf
  583. *
  584. * Map the ordinal offset of bit @ord in @buf to its position in @buf.
  585. * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
  586. * >= weight(buf), returns @nbits.
  587. *
  588. * If for example, just bits 4 through 7 are set in @buf, then @ord
  589. * values 0 through 3 will get mapped to 4 through 7, respectively,
  590. * and all other @ord values returns @nbits. When @ord value 3
  591. * gets mapped to (returns) @pos value 7 in this example, that means
  592. * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
  593. *
  594. * The bit positions 0 through @nbits-1 are valid positions in @buf.
  595. */
  596. unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
  597. {
  598. unsigned int pos;
  599. for (pos = find_first_bit(buf, nbits);
  600. pos < nbits && ord;
  601. pos = find_next_bit(buf, nbits, pos + 1))
  602. ord--;
  603. return pos;
  604. }
  605. /**
  606. * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
  607. * @dst: remapped result
  608. * @src: subset to be remapped
  609. * @old: defines domain of map
  610. * @new: defines range of map
  611. * @nbits: number of bits in each of these bitmaps
  612. *
  613. * Let @old and @new define a mapping of bit positions, such that
  614. * whatever position is held by the n-th set bit in @old is mapped
  615. * to the n-th set bit in @new. In the more general case, allowing
  616. * for the possibility that the weight 'w' of @new is less than the
  617. * weight of @old, map the position of the n-th set bit in @old to
  618. * the position of the m-th set bit in @new, where m == n % w.
  619. *
  620. * If either of the @old and @new bitmaps are empty, or if @src and
  621. * @dst point to the same location, then this routine copies @src
  622. * to @dst.
  623. *
  624. * The positions of unset bits in @old are mapped to themselves
  625. * (the identify map).
  626. *
  627. * Apply the above specified mapping to @src, placing the result in
  628. * @dst, clearing any bits previously set in @dst.
  629. *
  630. * For example, lets say that @old has bits 4 through 7 set, and
  631. * @new has bits 12 through 15 set. This defines the mapping of bit
  632. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  633. * bit positions unchanged. So if say @src comes into this routine
  634. * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
  635. * 13 and 15 set.
  636. */
  637. void bitmap_remap(unsigned long *dst, const unsigned long *src,
  638. const unsigned long *old, const unsigned long *new,
  639. unsigned int nbits)
  640. {
  641. unsigned int oldbit, w;
  642. if (dst == src) /* following doesn't handle inplace remaps */
  643. return;
  644. bitmap_zero(dst, nbits);
  645. w = bitmap_weight(new, nbits);
  646. for_each_set_bit(oldbit, src, nbits) {
  647. int n = bitmap_pos_to_ord(old, oldbit, nbits);
  648. if (n < 0 || w == 0)
  649. set_bit(oldbit, dst); /* identity map */
  650. else
  651. set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
  652. }
  653. }
  654. EXPORT_SYMBOL(bitmap_remap);
  655. /**
  656. * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
  657. * @oldbit: bit position to be mapped
  658. * @old: defines domain of map
  659. * @new: defines range of map
  660. * @bits: number of bits in each of these bitmaps
  661. *
  662. * Let @old and @new define a mapping of bit positions, such that
  663. * whatever position is held by the n-th set bit in @old is mapped
  664. * to the n-th set bit in @new. In the more general case, allowing
  665. * for the possibility that the weight 'w' of @new is less than the
  666. * weight of @old, map the position of the n-th set bit in @old to
  667. * the position of the m-th set bit in @new, where m == n % w.
  668. *
  669. * The positions of unset bits in @old are mapped to themselves
  670. * (the identify map).
  671. *
  672. * Apply the above specified mapping to bit position @oldbit, returning
  673. * the new bit position.
  674. *
  675. * For example, lets say that @old has bits 4 through 7 set, and
  676. * @new has bits 12 through 15 set. This defines the mapping of bit
  677. * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
  678. * bit positions unchanged. So if say @oldbit is 5, then this routine
  679. * returns 13.
  680. */
  681. int bitmap_bitremap(int oldbit, const unsigned long *old,
  682. const unsigned long *new, int bits)
  683. {
  684. int w = bitmap_weight(new, bits);
  685. int n = bitmap_pos_to_ord(old, oldbit, bits);
  686. if (n < 0 || w == 0)
  687. return oldbit;
  688. else
  689. return bitmap_ord_to_pos(new, n % w, bits);
  690. }
  691. EXPORT_SYMBOL(bitmap_bitremap);
  692. /**
  693. * bitmap_onto - translate one bitmap relative to another
  694. * @dst: resulting translated bitmap
  695. * @orig: original untranslated bitmap
  696. * @relmap: bitmap relative to which translated
  697. * @bits: number of bits in each of these bitmaps
  698. *
  699. * Set the n-th bit of @dst iff there exists some m such that the
  700. * n-th bit of @relmap is set, the m-th bit of @orig is set, and
  701. * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
  702. * (If you understood the previous sentence the first time your
  703. * read it, you're overqualified for your current job.)
  704. *
  705. * In other words, @orig is mapped onto (surjectively) @dst,
  706. * using the map { <n, m> | the n-th bit of @relmap is the
  707. * m-th set bit of @relmap }.
  708. *
  709. * Any set bits in @orig above bit number W, where W is the
  710. * weight of (number of set bits in) @relmap are mapped nowhere.
  711. * In particular, if for all bits m set in @orig, m >= W, then
  712. * @dst will end up empty. In situations where the possibility
  713. * of such an empty result is not desired, one way to avoid it is
  714. * to use the bitmap_fold() operator, below, to first fold the
  715. * @orig bitmap over itself so that all its set bits x are in the
  716. * range 0 <= x < W. The bitmap_fold() operator does this by
  717. * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
  718. *
  719. * Example [1] for bitmap_onto():
  720. * Let's say @relmap has bits 30-39 set, and @orig has bits
  721. * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
  722. * @dst will have bits 31, 33, 35, 37 and 39 set.
  723. *
  724. * When bit 0 is set in @orig, it means turn on the bit in
  725. * @dst corresponding to whatever is the first bit (if any)
  726. * that is turned on in @relmap. Since bit 0 was off in the
  727. * above example, we leave off that bit (bit 30) in @dst.
  728. *
  729. * When bit 1 is set in @orig (as in the above example), it
  730. * means turn on the bit in @dst corresponding to whatever
  731. * is the second bit that is turned on in @relmap. The second
  732. * bit in @relmap that was turned on in the above example was
  733. * bit 31, so we turned on bit 31 in @dst.
  734. *
  735. * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
  736. * because they were the 4th, 6th, 8th and 10th set bits
  737. * set in @relmap, and the 4th, 6th, 8th and 10th bits of
  738. * @orig (i.e. bits 3, 5, 7 and 9) were also set.
  739. *
  740. * When bit 11 is set in @orig, it means turn on the bit in
  741. * @dst corresponding to whatever is the twelfth bit that is
  742. * turned on in @relmap. In the above example, there were
  743. * only ten bits turned on in @relmap (30..39), so that bit
  744. * 11 was set in @orig had no affect on @dst.
  745. *
  746. * Example [2] for bitmap_fold() + bitmap_onto():
  747. * Let's say @relmap has these ten bits set:
  748. * 40 41 42 43 45 48 53 61 74 95
  749. * (for the curious, that's 40 plus the first ten terms of the
  750. * Fibonacci sequence.)
  751. *
  752. * Further lets say we use the following code, invoking
  753. * bitmap_fold() then bitmap_onto, as suggested above to
  754. * avoid the possibility of an empty @dst result:
  755. *
  756. * unsigned long *tmp; // a temporary bitmap's bits
  757. *
  758. * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
  759. * bitmap_onto(dst, tmp, relmap, bits);
  760. *
  761. * Then this table shows what various values of @dst would be, for
  762. * various @orig's. I list the zero-based positions of each set bit.
  763. * The tmp column shows the intermediate result, as computed by
  764. * using bitmap_fold() to fold the @orig bitmap modulo ten
  765. * (the weight of @relmap).
  766. *
  767. * @orig tmp @dst
  768. * 0 0 40
  769. * 1 1 41
  770. * 9 9 95
  771. * 10 0 40 (*)
  772. * 1 3 5 7 1 3 5 7 41 43 48 61
  773. * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
  774. * 0 9 18 27 0 9 8 7 40 61 74 95
  775. * 0 10 20 30 0 40
  776. * 0 11 22 33 0 1 2 3 40 41 42 43
  777. * 0 12 24 36 0 2 4 6 40 42 45 53
  778. * 78 102 211 1 2 8 41 42 74 (*)
  779. *
  780. * (*) For these marked lines, if we hadn't first done bitmap_fold()
  781. * into tmp, then the @dst result would have been empty.
  782. *
  783. * If either of @orig or @relmap is empty (no set bits), then @dst
  784. * will be returned empty.
  785. *
  786. * If (as explained above) the only set bits in @orig are in positions
  787. * m where m >= W, (where W is the weight of @relmap) then @dst will
  788. * once again be returned empty.
  789. *
  790. * All bits in @dst not set by the above rule are cleared.
  791. */
  792. void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  793. const unsigned long *relmap, unsigned int bits)
  794. {
  795. unsigned int n, m; /* same meaning as in above comment */
  796. if (dst == orig) /* following doesn't handle inplace mappings */
  797. return;
  798. bitmap_zero(dst, bits);
  799. /*
  800. * The following code is a more efficient, but less
  801. * obvious, equivalent to the loop:
  802. * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
  803. * n = bitmap_ord_to_pos(orig, m, bits);
  804. * if (test_bit(m, orig))
  805. * set_bit(n, dst);
  806. * }
  807. */
  808. m = 0;
  809. for_each_set_bit(n, relmap, bits) {
  810. /* m == bitmap_pos_to_ord(relmap, n, bits) */
  811. if (test_bit(m, orig))
  812. set_bit(n, dst);
  813. m++;
  814. }
  815. }
  816. EXPORT_SYMBOL(bitmap_onto);
  817. /**
  818. * bitmap_fold - fold larger bitmap into smaller, modulo specified size
  819. * @dst: resulting smaller bitmap
  820. * @orig: original larger bitmap
  821. * @sz: specified size
  822. * @nbits: number of bits in each of these bitmaps
  823. *
  824. * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
  825. * Clear all other bits in @dst. See further the comment and
  826. * Example [2] for bitmap_onto() for why and how to use this.
  827. */
  828. void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  829. unsigned int sz, unsigned int nbits)
  830. {
  831. unsigned int oldbit;
  832. if (dst == orig) /* following doesn't handle inplace mappings */
  833. return;
  834. bitmap_zero(dst, nbits);
  835. for_each_set_bit(oldbit, orig, nbits)
  836. set_bit(oldbit % sz, dst);
  837. }
  838. EXPORT_SYMBOL(bitmap_fold);
  839. /*
  840. * Common code for bitmap_*_region() routines.
  841. * bitmap: array of unsigned longs corresponding to the bitmap
  842. * pos: the beginning of the region
  843. * order: region size (log base 2 of number of bits)
  844. * reg_op: operation(s) to perform on that region of bitmap
  845. *
  846. * Can set, verify and/or release a region of bits in a bitmap,
  847. * depending on which combination of REG_OP_* flag bits is set.
  848. *
  849. * A region of a bitmap is a sequence of bits in the bitmap, of
  850. * some size '1 << order' (a power of two), aligned to that same
  851. * '1 << order' power of two.
  852. *
  853. * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
  854. * Returns 0 in all other cases and reg_ops.
  855. */
  856. enum {
  857. REG_OP_ISFREE, /* true if region is all zero bits */
  858. REG_OP_ALLOC, /* set all bits in region */
  859. REG_OP_RELEASE, /* clear all bits in region */
  860. };
  861. static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
  862. {
  863. int nbits_reg; /* number of bits in region */
  864. int index; /* index first long of region in bitmap */
  865. int offset; /* bit offset region in bitmap[index] */
  866. int nlongs_reg; /* num longs spanned by region in bitmap */
  867. int nbitsinlong; /* num bits of region in each spanned long */
  868. unsigned long mask; /* bitmask for one long of region */
  869. int i; /* scans bitmap by longs */
  870. int ret = 0; /* return value */
  871. /*
  872. * Either nlongs_reg == 1 (for small orders that fit in one long)
  873. * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
  874. */
  875. nbits_reg = 1 << order;
  876. index = pos / BITS_PER_LONG;
  877. offset = pos - (index * BITS_PER_LONG);
  878. nlongs_reg = BITS_TO_LONGS(nbits_reg);
  879. nbitsinlong = min(nbits_reg, BITS_PER_LONG);
  880. /*
  881. * Can't do "mask = (1UL << nbitsinlong) - 1", as that
  882. * overflows if nbitsinlong == BITS_PER_LONG.
  883. */
  884. mask = (1UL << (nbitsinlong - 1));
  885. mask += mask - 1;
  886. mask <<= offset;
  887. switch (reg_op) {
  888. case REG_OP_ISFREE:
  889. for (i = 0; i < nlongs_reg; i++) {
  890. if (bitmap[index + i] & mask)
  891. goto done;
  892. }
  893. ret = 1; /* all bits in region free (zero) */
  894. break;
  895. case REG_OP_ALLOC:
  896. for (i = 0; i < nlongs_reg; i++)
  897. bitmap[index + i] |= mask;
  898. break;
  899. case REG_OP_RELEASE:
  900. for (i = 0; i < nlongs_reg; i++)
  901. bitmap[index + i] &= ~mask;
  902. break;
  903. }
  904. done:
  905. return ret;
  906. }
  907. /**
  908. * bitmap_find_free_region - find a contiguous aligned mem region
  909. * @bitmap: array of unsigned longs corresponding to the bitmap
  910. * @bits: number of bits in the bitmap
  911. * @order: region size (log base 2 of number of bits) to find
  912. *
  913. * Find a region of free (zero) bits in a @bitmap of @bits bits and
  914. * allocate them (set them to one). Only consider regions of length
  915. * a power (@order) of two, aligned to that power of two, which
  916. * makes the search algorithm much faster.
  917. *
  918. * Return the bit offset in bitmap of the allocated region,
  919. * or -errno on failure.
  920. */
  921. int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
  922. {
  923. unsigned int pos, end; /* scans bitmap by regions of size order */
  924. for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
  925. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  926. continue;
  927. __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  928. return pos;
  929. }
  930. return -ENOMEM;
  931. }
  932. EXPORT_SYMBOL(bitmap_find_free_region);
  933. /**
  934. * bitmap_release_region - release allocated bitmap region
  935. * @bitmap: array of unsigned longs corresponding to the bitmap
  936. * @pos: beginning of bit region to release
  937. * @order: region size (log base 2 of number of bits) to release
  938. *
  939. * This is the complement to __bitmap_find_free_region() and releases
  940. * the found region (by clearing it in the bitmap).
  941. *
  942. * No return value.
  943. */
  944. void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
  945. {
  946. __reg_op(bitmap, pos, order, REG_OP_RELEASE);
  947. }
  948. EXPORT_SYMBOL(bitmap_release_region);
  949. /**
  950. * bitmap_allocate_region - allocate bitmap region
  951. * @bitmap: array of unsigned longs corresponding to the bitmap
  952. * @pos: beginning of bit region to allocate
  953. * @order: region size (log base 2 of number of bits) to allocate
  954. *
  955. * Allocate (set bits in) a specified region of a bitmap.
  956. *
  957. * Return 0 on success, or %-EBUSY if specified region wasn't
  958. * free (not all bits were zero).
  959. */
  960. int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
  961. {
  962. if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
  963. return -EBUSY;
  964. return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
  965. }
  966. EXPORT_SYMBOL(bitmap_allocate_region);
  967. /**
  968. * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
  969. * @dst: destination buffer
  970. * @src: bitmap to copy
  971. * @nbits: number of bits in the bitmap
  972. *
  973. * Require nbits % BITS_PER_LONG == 0.
  974. */
  975. #ifdef __BIG_ENDIAN
  976. void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
  977. {
  978. unsigned int i;
  979. for (i = 0; i < nbits/BITS_PER_LONG; i++) {
  980. if (BITS_PER_LONG == 64)
  981. dst[i] = cpu_to_le64(src[i]);
  982. else
  983. dst[i] = cpu_to_le32(src[i]);
  984. }
  985. }
  986. EXPORT_SYMBOL(bitmap_copy_le);
  987. #endif