| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * lib/bitmap.c |
|---|
| 3 | 4 | * 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 | 5 | */ |
|---|
| 8 | | -#include <linux/export.h> |
|---|
| 9 | | -#include <linux/thread_info.h> |
|---|
| 10 | | -#include <linux/ctype.h> |
|---|
| 11 | | -#include <linux/errno.h> |
|---|
| 6 | + |
|---|
| 12 | 7 | #include <linux/bitmap.h> |
|---|
| 13 | 8 | #include <linux/bitops.h> |
|---|
| 14 | 9 | #include <linux/bug.h> |
|---|
| 10 | +#include <linux/ctype.h> |
|---|
| 11 | +#include <linux/device.h> |
|---|
| 12 | +#include <linux/errno.h> |
|---|
| 13 | +#include <linux/export.h> |
|---|
| 15 | 14 | #include <linux/kernel.h> |
|---|
| 16 | 15 | #include <linux/mm.h> |
|---|
| 17 | 16 | #include <linux/slab.h> |
|---|
| 18 | 17 | #include <linux/string.h> |
|---|
| 18 | +#include <linux/thread_info.h> |
|---|
| 19 | 19 | #include <linux/uaccess.h> |
|---|
| 20 | 20 | |
|---|
| 21 | 21 | #include <asm/page.h> |
|---|
| 22 | 22 | |
|---|
| 23 | +#include "kstrtox.h" |
|---|
| 24 | + |
|---|
| 23 | 25 | /** |
|---|
| 24 | 26 | * DOC: bitmap introduction |
|---|
| 25 | 27 | * |
|---|
| 26 | | - * bitmaps provide an array of bits, implemented using an an |
|---|
| 28 | + * bitmaps provide an array of bits, implemented using an |
|---|
| 27 | 29 | * array of unsigned longs. The number of valid bits in a |
|---|
| 28 | 30 | * given bitmap does _not_ need to be an exact multiple of |
|---|
| 29 | 31 | * BITS_PER_LONG. |
|---|
| .. | .. |
|---|
| 36 | 38 | * for example) or scalar (bitmap_weight, for example) results |
|---|
| 37 | 39 | * carefully filter out these unused bits from impacting their |
|---|
| 38 | 40 | * results. |
|---|
| 39 | | - * |
|---|
| 40 | | - * These operations actually hold to a slightly stronger rule: |
|---|
| 41 | | - * if you don't input any bitmaps to these ops that have some |
|---|
| 42 | | - * unused bits set, then they won't output any set unused bits |
|---|
| 43 | | - * in output bitmaps. |
|---|
| 44 | 41 | * |
|---|
| 45 | 42 | * The byte ordering of bitmaps is more natural on little |
|---|
| 46 | 43 | * endian architectures. See the big-endian headers |
|---|
| .. | .. |
|---|
| 63 | 60 | return 1; |
|---|
| 64 | 61 | } |
|---|
| 65 | 62 | EXPORT_SYMBOL(__bitmap_equal); |
|---|
| 63 | + |
|---|
| 64 | +bool __bitmap_or_equal(const unsigned long *bitmap1, |
|---|
| 65 | + const unsigned long *bitmap2, |
|---|
| 66 | + const unsigned long *bitmap3, |
|---|
| 67 | + unsigned int bits) |
|---|
| 68 | +{ |
|---|
| 69 | + unsigned int k, lim = bits / BITS_PER_LONG; |
|---|
| 70 | + unsigned long tmp; |
|---|
| 71 | + |
|---|
| 72 | + for (k = 0; k < lim; ++k) { |
|---|
| 73 | + if ((bitmap1[k] | bitmap2[k]) != bitmap3[k]) |
|---|
| 74 | + return false; |
|---|
| 75 | + } |
|---|
| 76 | + |
|---|
| 77 | + if (!(bits % BITS_PER_LONG)) |
|---|
| 78 | + return true; |
|---|
| 79 | + |
|---|
| 80 | + tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k]; |
|---|
| 81 | + return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0; |
|---|
| 82 | +} |
|---|
| 66 | 83 | |
|---|
| 67 | 84 | void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits) |
|---|
| 68 | 85 | { |
|---|
| .. | .. |
|---|
| 153 | 170 | } |
|---|
| 154 | 171 | EXPORT_SYMBOL(__bitmap_shift_left); |
|---|
| 155 | 172 | |
|---|
| 173 | +/** |
|---|
| 174 | + * bitmap_cut() - remove bit region from bitmap and right shift remaining bits |
|---|
| 175 | + * @dst: destination bitmap, might overlap with src |
|---|
| 176 | + * @src: source bitmap |
|---|
| 177 | + * @first: start bit of region to be removed |
|---|
| 178 | + * @cut: number of bits to remove |
|---|
| 179 | + * @nbits: bitmap size, in bits |
|---|
| 180 | + * |
|---|
| 181 | + * Set the n-th bit of @dst iff the n-th bit of @src is set and |
|---|
| 182 | + * n is less than @first, or the m-th bit of @src is set for any |
|---|
| 183 | + * m such that @first <= n < nbits, and m = n + @cut. |
|---|
| 184 | + * |
|---|
| 185 | + * In pictures, example for a big-endian 32-bit architecture: |
|---|
| 186 | + * |
|---|
| 187 | + * The @src bitmap is:: |
|---|
| 188 | + * |
|---|
| 189 | + * 31 63 |
|---|
| 190 | + * | | |
|---|
| 191 | + * 10000000 11000001 11110010 00010101 10000000 11000001 01110010 00010101 |
|---|
| 192 | + * | | | | |
|---|
| 193 | + * 16 14 0 32 |
|---|
| 194 | + * |
|---|
| 195 | + * if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is:: |
|---|
| 196 | + * |
|---|
| 197 | + * 31 63 |
|---|
| 198 | + * | | |
|---|
| 199 | + * 10110000 00011000 00110010 00010101 00010000 00011000 00101110 01000010 |
|---|
| 200 | + * | | | |
|---|
| 201 | + * 14 (bit 17 0 32 |
|---|
| 202 | + * from @src) |
|---|
| 203 | + * |
|---|
| 204 | + * Note that @dst and @src might overlap partially or entirely. |
|---|
| 205 | + * |
|---|
| 206 | + * This is implemented in the obvious way, with a shift and carry |
|---|
| 207 | + * step for each moved bit. Optimisation is left as an exercise |
|---|
| 208 | + * for the compiler. |
|---|
| 209 | + */ |
|---|
| 210 | +void bitmap_cut(unsigned long *dst, const unsigned long *src, |
|---|
| 211 | + unsigned int first, unsigned int cut, unsigned int nbits) |
|---|
| 212 | +{ |
|---|
| 213 | + unsigned int len = BITS_TO_LONGS(nbits); |
|---|
| 214 | + unsigned long keep = 0, carry; |
|---|
| 215 | + int i; |
|---|
| 216 | + |
|---|
| 217 | + if (first % BITS_PER_LONG) { |
|---|
| 218 | + keep = src[first / BITS_PER_LONG] & |
|---|
| 219 | + (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG)); |
|---|
| 220 | + } |
|---|
| 221 | + |
|---|
| 222 | + memmove(dst, src, len * sizeof(*dst)); |
|---|
| 223 | + |
|---|
| 224 | + while (cut--) { |
|---|
| 225 | + for (i = first / BITS_PER_LONG; i < len; i++) { |
|---|
| 226 | + if (i < len - 1) |
|---|
| 227 | + carry = dst[i + 1] & 1UL; |
|---|
| 228 | + else |
|---|
| 229 | + carry = 0; |
|---|
| 230 | + |
|---|
| 231 | + dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1)); |
|---|
| 232 | + } |
|---|
| 233 | + } |
|---|
| 234 | + |
|---|
| 235 | + dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG); |
|---|
| 236 | + dst[first / BITS_PER_LONG] |= keep; |
|---|
| 237 | +} |
|---|
| 238 | +EXPORT_SYMBOL(bitmap_cut); |
|---|
| 239 | + |
|---|
| 156 | 240 | int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
|---|
| 157 | 241 | const unsigned long *bitmap2, unsigned int bits) |
|---|
| 158 | 242 | { |
|---|
| .. | .. |
|---|
| 206 | 290 | return result != 0; |
|---|
| 207 | 291 | } |
|---|
| 208 | 292 | EXPORT_SYMBOL(__bitmap_andnot); |
|---|
| 293 | + |
|---|
| 294 | +void __bitmap_replace(unsigned long *dst, |
|---|
| 295 | + const unsigned long *old, const unsigned long *new, |
|---|
| 296 | + const unsigned long *mask, unsigned int nbits) |
|---|
| 297 | +{ |
|---|
| 298 | + unsigned int k; |
|---|
| 299 | + unsigned int nr = BITS_TO_LONGS(nbits); |
|---|
| 300 | + |
|---|
| 301 | + for (k = 0; k < nr; k++) |
|---|
| 302 | + dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]); |
|---|
| 303 | +} |
|---|
| 304 | +EXPORT_SYMBOL(__bitmap_replace); |
|---|
| 209 | 305 | |
|---|
| 210 | 306 | int __bitmap_intersects(const unsigned long *bitmap1, |
|---|
| 211 | 307 | const unsigned long *bitmap2, unsigned int bits) |
|---|
| .. | .. |
|---|
| 338 | 434 | * second version by Paul Jackson, third by Joe Korty. |
|---|
| 339 | 435 | */ |
|---|
| 340 | 436 | |
|---|
| 341 | | -#define CHUNKSZ 32 |
|---|
| 342 | | -#define nbits_to_hold_value(val) fls(val) |
|---|
| 343 | | -#define BASEDEC 10 /* fancier cpuset lists input in decimal */ |
|---|
| 344 | | - |
|---|
| 345 | | -/** |
|---|
| 346 | | - * __bitmap_parse - convert an ASCII hex string into a bitmap. |
|---|
| 347 | | - * @buf: pointer to buffer containing string. |
|---|
| 348 | | - * @buflen: buffer size in bytes. If string is smaller than this |
|---|
| 349 | | - * then it must be terminated with a \0. |
|---|
| 350 | | - * @is_user: location of buffer, 0 indicates kernel space |
|---|
| 351 | | - * @maskp: pointer to bitmap array that will contain result. |
|---|
| 352 | | - * @nmaskbits: size of bitmap, in bits. |
|---|
| 353 | | - * |
|---|
| 354 | | - * Commas group hex digits into chunks. Each chunk defines exactly 32 |
|---|
| 355 | | - * bits of the resultant bitmask. No chunk may specify a value larger |
|---|
| 356 | | - * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value |
|---|
| 357 | | - * then leading 0-bits are prepended. %-EINVAL is returned for illegal |
|---|
| 358 | | - * characters and for grouping errors such as "1,,5", ",44", "," and "". |
|---|
| 359 | | - * Leading and trailing whitespace accepted, but not embedded whitespace. |
|---|
| 360 | | - */ |
|---|
| 361 | | -int __bitmap_parse(const char *buf, unsigned int buflen, |
|---|
| 362 | | - int is_user, unsigned long *maskp, |
|---|
| 363 | | - int nmaskbits) |
|---|
| 364 | | -{ |
|---|
| 365 | | - int c, old_c, totaldigits, ndigits, nchunks, nbits; |
|---|
| 366 | | - u32 chunk; |
|---|
| 367 | | - const char __user __force *ubuf = (const char __user __force *)buf; |
|---|
| 368 | | - |
|---|
| 369 | | - bitmap_zero(maskp, nmaskbits); |
|---|
| 370 | | - |
|---|
| 371 | | - nchunks = nbits = totaldigits = c = 0; |
|---|
| 372 | | - do { |
|---|
| 373 | | - chunk = 0; |
|---|
| 374 | | - ndigits = totaldigits; |
|---|
| 375 | | - |
|---|
| 376 | | - /* Get the next chunk of the bitmap */ |
|---|
| 377 | | - while (buflen) { |
|---|
| 378 | | - old_c = c; |
|---|
| 379 | | - if (is_user) { |
|---|
| 380 | | - if (__get_user(c, ubuf++)) |
|---|
| 381 | | - return -EFAULT; |
|---|
| 382 | | - } |
|---|
| 383 | | - else |
|---|
| 384 | | - c = *buf++; |
|---|
| 385 | | - buflen--; |
|---|
| 386 | | - if (isspace(c)) |
|---|
| 387 | | - continue; |
|---|
| 388 | | - |
|---|
| 389 | | - /* |
|---|
| 390 | | - * If the last character was a space and the current |
|---|
| 391 | | - * character isn't '\0', we've got embedded whitespace. |
|---|
| 392 | | - * This is a no-no, so throw an error. |
|---|
| 393 | | - */ |
|---|
| 394 | | - if (totaldigits && c && isspace(old_c)) |
|---|
| 395 | | - return -EINVAL; |
|---|
| 396 | | - |
|---|
| 397 | | - /* A '\0' or a ',' signal the end of the chunk */ |
|---|
| 398 | | - if (c == '\0' || c == ',') |
|---|
| 399 | | - break; |
|---|
| 400 | | - |
|---|
| 401 | | - if (!isxdigit(c)) |
|---|
| 402 | | - return -EINVAL; |
|---|
| 403 | | - |
|---|
| 404 | | - /* |
|---|
| 405 | | - * Make sure there are at least 4 free bits in 'chunk'. |
|---|
| 406 | | - * If not, this hexdigit will overflow 'chunk', so |
|---|
| 407 | | - * throw an error. |
|---|
| 408 | | - */ |
|---|
| 409 | | - if (chunk & ~((1UL << (CHUNKSZ - 4)) - 1)) |
|---|
| 410 | | - return -EOVERFLOW; |
|---|
| 411 | | - |
|---|
| 412 | | - chunk = (chunk << 4) | hex_to_bin(c); |
|---|
| 413 | | - totaldigits++; |
|---|
| 414 | | - } |
|---|
| 415 | | - if (ndigits == totaldigits) |
|---|
| 416 | | - return -EINVAL; |
|---|
| 417 | | - if (nchunks == 0 && chunk == 0) |
|---|
| 418 | | - continue; |
|---|
| 419 | | - |
|---|
| 420 | | - __bitmap_shift_left(maskp, maskp, CHUNKSZ, nmaskbits); |
|---|
| 421 | | - *maskp |= chunk; |
|---|
| 422 | | - nchunks++; |
|---|
| 423 | | - nbits += (nchunks == 1) ? nbits_to_hold_value(chunk) : CHUNKSZ; |
|---|
| 424 | | - if (nbits > nmaskbits) |
|---|
| 425 | | - return -EOVERFLOW; |
|---|
| 426 | | - } while (buflen && c == ','); |
|---|
| 427 | | - |
|---|
| 428 | | - return 0; |
|---|
| 429 | | -} |
|---|
| 430 | | -EXPORT_SYMBOL(__bitmap_parse); |
|---|
| 431 | | - |
|---|
| 432 | 437 | /** |
|---|
| 433 | 438 | * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap |
|---|
| 434 | 439 | * |
|---|
| .. | .. |
|---|
| 437 | 442 | * then it must be terminated with a \0. |
|---|
| 438 | 443 | * @maskp: pointer to bitmap array that will contain result. |
|---|
| 439 | 444 | * @nmaskbits: size of bitmap, in bits. |
|---|
| 440 | | - * |
|---|
| 441 | | - * Wrapper for __bitmap_parse(), providing it with user buffer. |
|---|
| 442 | | - * |
|---|
| 443 | | - * We cannot have this as an inline function in bitmap.h because it needs |
|---|
| 444 | | - * linux/uaccess.h to get the access_ok() declaration and this causes |
|---|
| 445 | | - * cyclic dependencies. |
|---|
| 446 | 445 | */ |
|---|
| 447 | 446 | int bitmap_parse_user(const char __user *ubuf, |
|---|
| 448 | 447 | unsigned int ulen, unsigned long *maskp, |
|---|
| 449 | 448 | int nmaskbits) |
|---|
| 450 | 449 | { |
|---|
| 451 | | - if (!access_ok(VERIFY_READ, ubuf, ulen)) |
|---|
| 452 | | - return -EFAULT; |
|---|
| 453 | | - return __bitmap_parse((const char __force *)ubuf, |
|---|
| 454 | | - ulen, 1, maskp, nmaskbits); |
|---|
| 450 | + char *buf; |
|---|
| 451 | + int ret; |
|---|
| 455 | 452 | |
|---|
| 453 | + buf = memdup_user_nul(ubuf, ulen); |
|---|
| 454 | + if (IS_ERR(buf)) |
|---|
| 455 | + return PTR_ERR(buf); |
|---|
| 456 | + |
|---|
| 457 | + ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits); |
|---|
| 458 | + |
|---|
| 459 | + kfree(buf); |
|---|
| 460 | + return ret; |
|---|
| 456 | 461 | } |
|---|
| 457 | 462 | EXPORT_SYMBOL(bitmap_parse_user); |
|---|
| 458 | 463 | |
|---|
| .. | .. |
|---|
| 476 | 481 | int nmaskbits) |
|---|
| 477 | 482 | { |
|---|
| 478 | 483 | ptrdiff_t len = PAGE_SIZE - offset_in_page(buf); |
|---|
| 479 | | - int n = 0; |
|---|
| 480 | 484 | |
|---|
| 481 | | - if (len > 1) |
|---|
| 482 | | - n = list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) : |
|---|
| 483 | | - scnprintf(buf, len, "%*pb\n", nmaskbits, maskp); |
|---|
| 484 | | - return n; |
|---|
| 485 | + return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) : |
|---|
| 486 | + scnprintf(buf, len, "%*pb\n", nmaskbits, maskp); |
|---|
| 485 | 487 | } |
|---|
| 486 | 488 | EXPORT_SYMBOL(bitmap_print_to_pagebuf); |
|---|
| 487 | 489 | |
|---|
| 490 | +/* |
|---|
| 491 | + * Region 9-38:4/10 describes the following bitmap structure: |
|---|
| 492 | + * 0 9 12 18 38 |
|---|
| 493 | + * .........****......****......****...... |
|---|
| 494 | + * ^ ^ ^ ^ |
|---|
| 495 | + * start off group_len end |
|---|
| 496 | + */ |
|---|
| 497 | +struct region { |
|---|
| 498 | + unsigned int start; |
|---|
| 499 | + unsigned int off; |
|---|
| 500 | + unsigned int group_len; |
|---|
| 501 | + unsigned int end; |
|---|
| 502 | +}; |
|---|
| 503 | + |
|---|
| 504 | +static int bitmap_set_region(const struct region *r, |
|---|
| 505 | + unsigned long *bitmap, int nbits) |
|---|
| 506 | +{ |
|---|
| 507 | + unsigned int start; |
|---|
| 508 | + |
|---|
| 509 | + if (r->end >= nbits) |
|---|
| 510 | + return -ERANGE; |
|---|
| 511 | + |
|---|
| 512 | + for (start = r->start; start <= r->end; start += r->group_len) |
|---|
| 513 | + bitmap_set(bitmap, start, min(r->end - start + 1, r->off)); |
|---|
| 514 | + |
|---|
| 515 | + return 0; |
|---|
| 516 | +} |
|---|
| 517 | + |
|---|
| 518 | +static int bitmap_check_region(const struct region *r) |
|---|
| 519 | +{ |
|---|
| 520 | + if (r->start > r->end || r->group_len == 0 || r->off > r->group_len) |
|---|
| 521 | + return -EINVAL; |
|---|
| 522 | + |
|---|
| 523 | + return 0; |
|---|
| 524 | +} |
|---|
| 525 | + |
|---|
| 526 | +static const char *bitmap_getnum(const char *str, unsigned int *num) |
|---|
| 527 | +{ |
|---|
| 528 | + unsigned long long n; |
|---|
| 529 | + unsigned int len; |
|---|
| 530 | + |
|---|
| 531 | + len = _parse_integer(str, 10, &n); |
|---|
| 532 | + if (!len) |
|---|
| 533 | + return ERR_PTR(-EINVAL); |
|---|
| 534 | + if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n) |
|---|
| 535 | + return ERR_PTR(-EOVERFLOW); |
|---|
| 536 | + |
|---|
| 537 | + *num = n; |
|---|
| 538 | + return str + len; |
|---|
| 539 | +} |
|---|
| 540 | + |
|---|
| 541 | +static inline bool end_of_str(char c) |
|---|
| 542 | +{ |
|---|
| 543 | + return c == '\0' || c == '\n'; |
|---|
| 544 | +} |
|---|
| 545 | + |
|---|
| 546 | +static inline bool __end_of_region(char c) |
|---|
| 547 | +{ |
|---|
| 548 | + return isspace(c) || c == ','; |
|---|
| 549 | +} |
|---|
| 550 | + |
|---|
| 551 | +static inline bool end_of_region(char c) |
|---|
| 552 | +{ |
|---|
| 553 | + return __end_of_region(c) || end_of_str(c); |
|---|
| 554 | +} |
|---|
| 555 | + |
|---|
| 556 | +/* |
|---|
| 557 | + * The format allows commas and whitespaces at the beginning |
|---|
| 558 | + * of the region. |
|---|
| 559 | + */ |
|---|
| 560 | +static const char *bitmap_find_region(const char *str) |
|---|
| 561 | +{ |
|---|
| 562 | + while (__end_of_region(*str)) |
|---|
| 563 | + str++; |
|---|
| 564 | + |
|---|
| 565 | + return end_of_str(*str) ? NULL : str; |
|---|
| 566 | +} |
|---|
| 567 | + |
|---|
| 568 | +static const char *bitmap_find_region_reverse(const char *start, const char *end) |
|---|
| 569 | +{ |
|---|
| 570 | + while (start <= end && __end_of_region(*end)) |
|---|
| 571 | + end--; |
|---|
| 572 | + |
|---|
| 573 | + return end; |
|---|
| 574 | +} |
|---|
| 575 | + |
|---|
| 576 | +static const char *bitmap_parse_region(const char *str, struct region *r) |
|---|
| 577 | +{ |
|---|
| 578 | + str = bitmap_getnum(str, &r->start); |
|---|
| 579 | + if (IS_ERR(str)) |
|---|
| 580 | + return str; |
|---|
| 581 | + |
|---|
| 582 | + if (end_of_region(*str)) |
|---|
| 583 | + goto no_end; |
|---|
| 584 | + |
|---|
| 585 | + if (*str != '-') |
|---|
| 586 | + return ERR_PTR(-EINVAL); |
|---|
| 587 | + |
|---|
| 588 | + str = bitmap_getnum(str + 1, &r->end); |
|---|
| 589 | + if (IS_ERR(str)) |
|---|
| 590 | + return str; |
|---|
| 591 | + |
|---|
| 592 | + if (end_of_region(*str)) |
|---|
| 593 | + goto no_pattern; |
|---|
| 594 | + |
|---|
| 595 | + if (*str != ':') |
|---|
| 596 | + return ERR_PTR(-EINVAL); |
|---|
| 597 | + |
|---|
| 598 | + str = bitmap_getnum(str + 1, &r->off); |
|---|
| 599 | + if (IS_ERR(str)) |
|---|
| 600 | + return str; |
|---|
| 601 | + |
|---|
| 602 | + if (*str != '/') |
|---|
| 603 | + return ERR_PTR(-EINVAL); |
|---|
| 604 | + |
|---|
| 605 | + return bitmap_getnum(str + 1, &r->group_len); |
|---|
| 606 | + |
|---|
| 607 | +no_end: |
|---|
| 608 | + r->end = r->start; |
|---|
| 609 | +no_pattern: |
|---|
| 610 | + r->off = r->end + 1; |
|---|
| 611 | + r->group_len = r->end + 1; |
|---|
| 612 | + |
|---|
| 613 | + return end_of_str(*str) ? NULL : str; |
|---|
| 614 | +} |
|---|
| 615 | + |
|---|
| 488 | 616 | /** |
|---|
| 489 | | - * __bitmap_parselist - convert list format ASCII string to bitmap |
|---|
| 490 | | - * @buf: read nul-terminated user string from this buffer |
|---|
| 491 | | - * @buflen: buffer size in bytes. If string is smaller than this |
|---|
| 492 | | - * then it must be terminated with a \0. |
|---|
| 493 | | - * @is_user: location of buffer, 0 indicates kernel space |
|---|
| 617 | + * bitmap_parselist - convert list format ASCII string to bitmap |
|---|
| 618 | + * @buf: read user string from this buffer; must be terminated |
|---|
| 619 | + * with a \0 or \n. |
|---|
| 494 | 620 | * @maskp: write resulting mask here |
|---|
| 495 | 621 | * @nmaskbits: number of bits in mask to be written |
|---|
| 496 | 622 | * |
|---|
| .. | .. |
|---|
| 506 | 632 | * |
|---|
| 507 | 633 | * Returns: 0 on success, -errno on invalid input strings. Error values: |
|---|
| 508 | 634 | * |
|---|
| 509 | | - * - ``-EINVAL``: second number in range smaller than first |
|---|
| 635 | + * - ``-EINVAL``: wrong region format |
|---|
| 510 | 636 | * - ``-EINVAL``: invalid character in string |
|---|
| 511 | 637 | * - ``-ERANGE``: bit number specified too large for mask |
|---|
| 638 | + * - ``-EOVERFLOW``: integer overflow in the input parameters |
|---|
| 512 | 639 | */ |
|---|
| 513 | | -static int __bitmap_parselist(const char *buf, unsigned int buflen, |
|---|
| 514 | | - int is_user, unsigned long *maskp, |
|---|
| 515 | | - int nmaskbits) |
|---|
| 640 | +int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits) |
|---|
| 516 | 641 | { |
|---|
| 517 | | - unsigned int a, b, old_a, old_b; |
|---|
| 518 | | - unsigned int group_size, used_size, off; |
|---|
| 519 | | - int c, old_c, totaldigits, ndigits; |
|---|
| 520 | | - const char __user __force *ubuf = (const char __user __force *)buf; |
|---|
| 521 | | - int at_start, in_range, in_partial_range; |
|---|
| 642 | + struct region r; |
|---|
| 643 | + long ret; |
|---|
| 522 | 644 | |
|---|
| 523 | | - totaldigits = c = 0; |
|---|
| 524 | | - old_a = old_b = 0; |
|---|
| 525 | | - group_size = used_size = 0; |
|---|
| 526 | 645 | bitmap_zero(maskp, nmaskbits); |
|---|
| 527 | | - do { |
|---|
| 528 | | - at_start = 1; |
|---|
| 529 | | - in_range = 0; |
|---|
| 530 | | - in_partial_range = 0; |
|---|
| 531 | | - a = b = 0; |
|---|
| 532 | | - ndigits = totaldigits; |
|---|
| 533 | 646 | |
|---|
| 534 | | - /* Get the next cpu# or a range of cpu#'s */ |
|---|
| 535 | | - while (buflen) { |
|---|
| 536 | | - old_c = c; |
|---|
| 537 | | - if (is_user) { |
|---|
| 538 | | - if (__get_user(c, ubuf++)) |
|---|
| 539 | | - return -EFAULT; |
|---|
| 540 | | - } else |
|---|
| 541 | | - c = *buf++; |
|---|
| 542 | | - buflen--; |
|---|
| 543 | | - if (isspace(c)) |
|---|
| 544 | | - continue; |
|---|
| 647 | + while (buf) { |
|---|
| 648 | + buf = bitmap_find_region(buf); |
|---|
| 649 | + if (buf == NULL) |
|---|
| 650 | + return 0; |
|---|
| 545 | 651 | |
|---|
| 546 | | - /* A '\0' or a ',' signal the end of a cpu# or range */ |
|---|
| 547 | | - if (c == '\0' || c == ',') |
|---|
| 548 | | - break; |
|---|
| 549 | | - /* |
|---|
| 550 | | - * whitespaces between digits are not allowed, |
|---|
| 551 | | - * but it's ok if whitespaces are on head or tail. |
|---|
| 552 | | - * when old_c is whilespace, |
|---|
| 553 | | - * if totaldigits == ndigits, whitespace is on head. |
|---|
| 554 | | - * if whitespace is on tail, it should not run here. |
|---|
| 555 | | - * as c was ',' or '\0', |
|---|
| 556 | | - * the last code line has broken the current loop. |
|---|
| 557 | | - */ |
|---|
| 558 | | - if ((totaldigits != ndigits) && isspace(old_c)) |
|---|
| 559 | | - return -EINVAL; |
|---|
| 652 | + buf = bitmap_parse_region(buf, &r); |
|---|
| 653 | + if (IS_ERR(buf)) |
|---|
| 654 | + return PTR_ERR(buf); |
|---|
| 560 | 655 | |
|---|
| 561 | | - if (c == '/') { |
|---|
| 562 | | - used_size = a; |
|---|
| 563 | | - at_start = 1; |
|---|
| 564 | | - in_range = 0; |
|---|
| 565 | | - a = b = 0; |
|---|
| 566 | | - continue; |
|---|
| 567 | | - } |
|---|
| 656 | + ret = bitmap_check_region(&r); |
|---|
| 657 | + if (ret) |
|---|
| 658 | + return ret; |
|---|
| 568 | 659 | |
|---|
| 569 | | - if (c == ':') { |
|---|
| 570 | | - old_a = a; |
|---|
| 571 | | - old_b = b; |
|---|
| 572 | | - at_start = 1; |
|---|
| 573 | | - in_range = 0; |
|---|
| 574 | | - in_partial_range = 1; |
|---|
| 575 | | - a = b = 0; |
|---|
| 576 | | - continue; |
|---|
| 577 | | - } |
|---|
| 660 | + ret = bitmap_set_region(&r, maskp, nmaskbits); |
|---|
| 661 | + if (ret) |
|---|
| 662 | + return ret; |
|---|
| 663 | + } |
|---|
| 578 | 664 | |
|---|
| 579 | | - if (c == '-') { |
|---|
| 580 | | - if (at_start || in_range) |
|---|
| 581 | | - return -EINVAL; |
|---|
| 582 | | - b = 0; |
|---|
| 583 | | - in_range = 1; |
|---|
| 584 | | - at_start = 1; |
|---|
| 585 | | - continue; |
|---|
| 586 | | - } |
|---|
| 587 | | - |
|---|
| 588 | | - if (!isdigit(c)) |
|---|
| 589 | | - return -EINVAL; |
|---|
| 590 | | - |
|---|
| 591 | | - b = b * 10 + (c - '0'); |
|---|
| 592 | | - if (!in_range) |
|---|
| 593 | | - a = b; |
|---|
| 594 | | - at_start = 0; |
|---|
| 595 | | - totaldigits++; |
|---|
| 596 | | - } |
|---|
| 597 | | - if (ndigits == totaldigits) |
|---|
| 598 | | - continue; |
|---|
| 599 | | - if (in_partial_range) { |
|---|
| 600 | | - group_size = a; |
|---|
| 601 | | - a = old_a; |
|---|
| 602 | | - b = old_b; |
|---|
| 603 | | - old_a = old_b = 0; |
|---|
| 604 | | - } else { |
|---|
| 605 | | - used_size = group_size = b - a + 1; |
|---|
| 606 | | - } |
|---|
| 607 | | - /* if no digit is after '-', it's wrong*/ |
|---|
| 608 | | - if (at_start && in_range) |
|---|
| 609 | | - return -EINVAL; |
|---|
| 610 | | - if (!(a <= b) || group_size == 0 || !(used_size <= group_size)) |
|---|
| 611 | | - return -EINVAL; |
|---|
| 612 | | - if (b >= nmaskbits) |
|---|
| 613 | | - return -ERANGE; |
|---|
| 614 | | - while (a <= b) { |
|---|
| 615 | | - off = min(b - a + 1, used_size); |
|---|
| 616 | | - bitmap_set(maskp, a, off); |
|---|
| 617 | | - a += group_size; |
|---|
| 618 | | - } |
|---|
| 619 | | - } while (buflen && c == ','); |
|---|
| 620 | 665 | return 0; |
|---|
| 621 | | -} |
|---|
| 622 | | - |
|---|
| 623 | | -int bitmap_parselist(const char *bp, unsigned long *maskp, int nmaskbits) |
|---|
| 624 | | -{ |
|---|
| 625 | | - char *nl = strchrnul(bp, '\n'); |
|---|
| 626 | | - int len = nl - bp; |
|---|
| 627 | | - |
|---|
| 628 | | - return __bitmap_parselist(bp, len, 0, maskp, nmaskbits); |
|---|
| 629 | 666 | } |
|---|
| 630 | 667 | EXPORT_SYMBOL(bitmap_parselist); |
|---|
| 631 | 668 | |
|---|
| .. | .. |
|---|
| 640 | 677 | * @nmaskbits: size of bitmap, in bits. |
|---|
| 641 | 678 | * |
|---|
| 642 | 679 | * Wrapper for bitmap_parselist(), providing it with user buffer. |
|---|
| 643 | | - * |
|---|
| 644 | | - * We cannot have this as an inline function in bitmap.h because it needs |
|---|
| 645 | | - * linux/uaccess.h to get the access_ok() declaration and this causes |
|---|
| 646 | | - * cyclic dependencies. |
|---|
| 647 | 680 | */ |
|---|
| 648 | 681 | int bitmap_parselist_user(const char __user *ubuf, |
|---|
| 649 | 682 | unsigned int ulen, unsigned long *maskp, |
|---|
| 650 | 683 | int nmaskbits) |
|---|
| 651 | 684 | { |
|---|
| 652 | | - if (!access_ok(VERIFY_READ, ubuf, ulen)) |
|---|
| 653 | | - return -EFAULT; |
|---|
| 654 | | - return __bitmap_parselist((const char __force *)ubuf, |
|---|
| 655 | | - ulen, 1, maskp, nmaskbits); |
|---|
| 685 | + char *buf; |
|---|
| 686 | + int ret; |
|---|
| 687 | + |
|---|
| 688 | + buf = memdup_user_nul(ubuf, ulen); |
|---|
| 689 | + if (IS_ERR(buf)) |
|---|
| 690 | + return PTR_ERR(buf); |
|---|
| 691 | + |
|---|
| 692 | + ret = bitmap_parselist(buf, maskp, nmaskbits); |
|---|
| 693 | + |
|---|
| 694 | + kfree(buf); |
|---|
| 695 | + return ret; |
|---|
| 656 | 696 | } |
|---|
| 657 | 697 | EXPORT_SYMBOL(bitmap_parselist_user); |
|---|
| 658 | 698 | |
|---|
| 699 | +static const char *bitmap_get_x32_reverse(const char *start, |
|---|
| 700 | + const char *end, u32 *num) |
|---|
| 701 | +{ |
|---|
| 702 | + u32 ret = 0; |
|---|
| 703 | + int c, i; |
|---|
| 659 | 704 | |
|---|
| 705 | + for (i = 0; i < 32; i += 4) { |
|---|
| 706 | + c = hex_to_bin(*end--); |
|---|
| 707 | + if (c < 0) |
|---|
| 708 | + return ERR_PTR(-EINVAL); |
|---|
| 709 | + |
|---|
| 710 | + ret |= c << i; |
|---|
| 711 | + |
|---|
| 712 | + if (start > end || __end_of_region(*end)) |
|---|
| 713 | + goto out; |
|---|
| 714 | + } |
|---|
| 715 | + |
|---|
| 716 | + if (hex_to_bin(*end--) >= 0) |
|---|
| 717 | + return ERR_PTR(-EOVERFLOW); |
|---|
| 718 | +out: |
|---|
| 719 | + *num = ret; |
|---|
| 720 | + return end; |
|---|
| 721 | +} |
|---|
| 722 | + |
|---|
| 723 | +/** |
|---|
| 724 | + * bitmap_parse - convert an ASCII hex string into a bitmap. |
|---|
| 725 | + * @start: pointer to buffer containing string. |
|---|
| 726 | + * @buflen: buffer size in bytes. If string is smaller than this |
|---|
| 727 | + * then it must be terminated with a \0 or \n. In that case, |
|---|
| 728 | + * UINT_MAX may be provided instead of string length. |
|---|
| 729 | + * @maskp: pointer to bitmap array that will contain result. |
|---|
| 730 | + * @nmaskbits: size of bitmap, in bits. |
|---|
| 731 | + * |
|---|
| 732 | + * Commas group hex digits into chunks. Each chunk defines exactly 32 |
|---|
| 733 | + * bits of the resultant bitmask. No chunk may specify a value larger |
|---|
| 734 | + * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value |
|---|
| 735 | + * then leading 0-bits are prepended. %-EINVAL is returned for illegal |
|---|
| 736 | + * characters. Grouping such as "1,,5", ",44", "," or "" is allowed. |
|---|
| 737 | + * Leading, embedded and trailing whitespace accepted. |
|---|
| 738 | + */ |
|---|
| 739 | +int bitmap_parse(const char *start, unsigned int buflen, |
|---|
| 740 | + unsigned long *maskp, int nmaskbits) |
|---|
| 741 | +{ |
|---|
| 742 | + const char *end = strnchrnul(start, buflen, '\n') - 1; |
|---|
| 743 | + int chunks = BITS_TO_U32(nmaskbits); |
|---|
| 744 | + u32 *bitmap = (u32 *)maskp; |
|---|
| 745 | + int unset_bit; |
|---|
| 746 | + int chunk; |
|---|
| 747 | + |
|---|
| 748 | + for (chunk = 0; ; chunk++) { |
|---|
| 749 | + end = bitmap_find_region_reverse(start, end); |
|---|
| 750 | + if (start > end) |
|---|
| 751 | + break; |
|---|
| 752 | + |
|---|
| 753 | + if (!chunks--) |
|---|
| 754 | + return -EOVERFLOW; |
|---|
| 755 | + |
|---|
| 756 | +#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN) |
|---|
| 757 | + end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]); |
|---|
| 758 | +#else |
|---|
| 759 | + end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]); |
|---|
| 760 | +#endif |
|---|
| 761 | + if (IS_ERR(end)) |
|---|
| 762 | + return PTR_ERR(end); |
|---|
| 763 | + } |
|---|
| 764 | + |
|---|
| 765 | + unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32; |
|---|
| 766 | + if (unset_bit < nmaskbits) { |
|---|
| 767 | + bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit); |
|---|
| 768 | + return 0; |
|---|
| 769 | + } |
|---|
| 770 | + |
|---|
| 771 | + if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit) |
|---|
| 772 | + return -EOVERFLOW; |
|---|
| 773 | + |
|---|
| 774 | + return 0; |
|---|
| 775 | +} |
|---|
| 776 | +EXPORT_SYMBOL(bitmap_parse); |
|---|
| 777 | + |
|---|
| 778 | + |
|---|
| 779 | +#ifdef CONFIG_NUMA |
|---|
| 660 | 780 | /** |
|---|
| 661 | 781 | * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap |
|---|
| 662 | 782 | * @buf: pointer to a bitmap |
|---|
| .. | .. |
|---|
| 765 | 885 | set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst); |
|---|
| 766 | 886 | } |
|---|
| 767 | 887 | } |
|---|
| 768 | | -EXPORT_SYMBOL(bitmap_remap); |
|---|
| 769 | 888 | |
|---|
| 770 | 889 | /** |
|---|
| 771 | 890 | * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit |
|---|
| .. | .. |
|---|
| 803 | 922 | else |
|---|
| 804 | 923 | return bitmap_ord_to_pos(new, n % w, bits); |
|---|
| 805 | 924 | } |
|---|
| 806 | | -EXPORT_SYMBOL(bitmap_bitremap); |
|---|
| 807 | 925 | |
|---|
| 808 | 926 | /** |
|---|
| 809 | 927 | * bitmap_onto - translate one bitmap relative to another |
|---|
| .. | .. |
|---|
| 938 | 1056 | m++; |
|---|
| 939 | 1057 | } |
|---|
| 940 | 1058 | } |
|---|
| 941 | | -EXPORT_SYMBOL(bitmap_onto); |
|---|
| 942 | 1059 | |
|---|
| 943 | 1060 | /** |
|---|
| 944 | 1061 | * bitmap_fold - fold larger bitmap into smaller, modulo specified size |
|---|
| .. | .. |
|---|
| 963 | 1080 | for_each_set_bit(oldbit, orig, nbits) |
|---|
| 964 | 1081 | set_bit(oldbit % sz, dst); |
|---|
| 965 | 1082 | } |
|---|
| 966 | | -EXPORT_SYMBOL(bitmap_fold); |
|---|
| 1083 | +#endif /* CONFIG_NUMA */ |
|---|
| 967 | 1084 | |
|---|
| 968 | 1085 | /* |
|---|
| 969 | 1086 | * Common code for bitmap_*_region() routines. |
|---|
| .. | .. |
|---|
| 1147 | 1264 | } |
|---|
| 1148 | 1265 | EXPORT_SYMBOL(bitmap_free); |
|---|
| 1149 | 1266 | |
|---|
| 1267 | +static void devm_bitmap_free(void *data) |
|---|
| 1268 | +{ |
|---|
| 1269 | + unsigned long *bitmap = data; |
|---|
| 1270 | + |
|---|
| 1271 | + bitmap_free(bitmap); |
|---|
| 1272 | +} |
|---|
| 1273 | + |
|---|
| 1274 | +unsigned long *devm_bitmap_alloc(struct device *dev, |
|---|
| 1275 | + unsigned int nbits, gfp_t flags) |
|---|
| 1276 | +{ |
|---|
| 1277 | + unsigned long *bitmap; |
|---|
| 1278 | + int ret; |
|---|
| 1279 | + |
|---|
| 1280 | + bitmap = bitmap_alloc(nbits, flags); |
|---|
| 1281 | + if (!bitmap) |
|---|
| 1282 | + return NULL; |
|---|
| 1283 | + |
|---|
| 1284 | + ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap); |
|---|
| 1285 | + if (ret) |
|---|
| 1286 | + return NULL; |
|---|
| 1287 | + |
|---|
| 1288 | + return bitmap; |
|---|
| 1289 | +} |
|---|
| 1290 | +EXPORT_SYMBOL_GPL(devm_bitmap_alloc); |
|---|
| 1291 | + |
|---|
| 1292 | +unsigned long *devm_bitmap_zalloc(struct device *dev, |
|---|
| 1293 | + unsigned int nbits, gfp_t flags) |
|---|
| 1294 | +{ |
|---|
| 1295 | + return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO); |
|---|
| 1296 | +} |
|---|
| 1297 | +EXPORT_SYMBOL_GPL(devm_bitmap_zalloc); |
|---|
| 1298 | + |
|---|
| 1150 | 1299 | #if BITS_PER_LONG == 64 |
|---|
| 1151 | 1300 | /** |
|---|
| 1152 | 1301 | * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap |
|---|