bitmap.c 33 KB

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