Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'bitmap-for-6.7' of https://github.com/norov/linux

Pull bitmap updates from Yury Norov:
"This includes the 'bitmap: cleanup bitmap_*_region() implementation'
series, and scattered cleanup patches"

* tag 'bitmap-for-6.7' of https://github.com/norov/linux:
buildid: reduce header file dependencies for module
bitmap: move bitmap_*_region() functions to bitmap.h
bitmap: drop _reg_op() function
bitmap: replace _reg_op(REG_OP_ISFREE) with find_next_bit()
bitmap: replace _reg_op(REG_OP_RELEASE) with bitmap_clear()
bitmap: replace _reg_op(REG_OP_ALLOC) with bitmap_set()
bitmap: fix opencoded bitmap_allocate_region()
bitmap: add test for bitmap_*_region() functions
bitmap: align __reg_op() wrappers with modern coding style
lib/bitmap: split-out string-related operations to a separate files
bitmap: Remove dead code, i.e. bitmap_copy_le()
bitmap: Fix a typo ("identify map")
cpumask: kernel-doc cleanups and additions

+700 -754
+2
MAINTAINERS
··· 3539 3539 R: Rasmus Villemoes <linux@rasmusvillemoes.dk> 3540 3540 S: Maintained 3541 3541 F: include/linux/bitfield.h 3542 + F: include/linux/bitmap-str.h 3542 3543 F: include/linux/bitmap.h 3543 3544 F: include/linux/bits.h 3544 3545 F: include/linux/cpumask.h 3545 3546 F: include/linux/find.h 3546 3547 F: include/linux/nodemask.h 3547 3548 F: include/vdso/bits.h 3549 + F: lib/bitmap-str.c 3548 3550 F: lib/bitmap.c 3549 3551 F: lib/cpumask.c 3550 3552 F: lib/cpumask_kunit.c
+16
include/linux/bitmap-str.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef __LINUX_BITMAP_STR_H 3 + #define __LINUX_BITMAP_STR_H 4 + 5 + int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, unsigned long *dst, int nbits); 6 + int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, int nmaskbits); 7 + extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp, 8 + int nmaskbits, loff_t off, size_t count); 9 + extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp, 10 + int nmaskbits, loff_t off, size_t count); 11 + int bitmap_parse(const char *buf, unsigned int buflen, unsigned long *dst, int nbits); 12 + int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits); 13 + int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, 14 + unsigned long *dst, int nbits); 15 + 16 + #endif /* __LINUX_BITMAP_STR_H */
+62 -25
include/linux/bitmap.h
··· 6 6 7 7 #include <linux/align.h> 8 8 #include <linux/bitops.h> 9 + #include <linux/errno.h> 9 10 #include <linux/find.h> 10 11 #include <linux/limits.h> 11 12 #include <linux/string.h> 12 13 #include <linux/types.h> 14 + #include <linux/bitmap-str.h> 13 15 14 16 struct device; 15 17 ··· 202 200 align_mask, 0); 203 201 } 204 202 205 - int bitmap_parse(const char *buf, unsigned int buflen, 206 - unsigned long *dst, int nbits); 207 - int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, 208 - unsigned long *dst, int nbits); 209 - int bitmap_parselist(const char *buf, unsigned long *maskp, 210 - int nmaskbits); 211 - int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, 212 - unsigned long *dst, int nbits); 213 203 void bitmap_remap(unsigned long *dst, const unsigned long *src, 214 204 const unsigned long *old, const unsigned long *new, unsigned int nbits); 215 205 int bitmap_bitremap(int oldbit, ··· 210 216 const unsigned long *relmap, unsigned int bits); 211 217 void bitmap_fold(unsigned long *dst, const unsigned long *orig, 212 218 unsigned int sz, unsigned int nbits); 213 - int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order); 214 - void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order); 215 - int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order); 216 - 217 - #ifdef __BIG_ENDIAN 218 - void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits); 219 - #else 220 - #define bitmap_copy_le bitmap_copy 221 - #endif 222 - int bitmap_print_to_pagebuf(bool list, char *buf, 223 - const unsigned long *maskp, int nmaskbits); 224 - 225 - extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp, 226 - int nmaskbits, loff_t off, size_t count); 227 - 228 - extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp, 229 - int nmaskbits, loff_t off, size_t count); 230 219 231 220 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) 232 221 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) ··· 493 516 { 494 517 *rs = find_next_bit(bitmap, end, *rs); 495 518 *re = find_next_zero_bit(bitmap, end, *rs + 1); 519 + } 520 + 521 + /** 522 + * bitmap_release_region - release allocated bitmap region 523 + * @bitmap: array of unsigned longs corresponding to the bitmap 524 + * @pos: beginning of bit region to release 525 + * @order: region size (log base 2 of number of bits) to release 526 + * 527 + * This is the complement to __bitmap_find_free_region() and releases 528 + * the found region (by clearing it in the bitmap). 529 + */ 530 + static inline void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order) 531 + { 532 + bitmap_clear(bitmap, pos, BIT(order)); 533 + } 534 + 535 + /** 536 + * bitmap_allocate_region - allocate bitmap region 537 + * @bitmap: array of unsigned longs corresponding to the bitmap 538 + * @pos: beginning of bit region to allocate 539 + * @order: region size (log base 2 of number of bits) to allocate 540 + * 541 + * Allocate (set bits in) a specified region of a bitmap. 542 + * 543 + * Returns: 0 on success, or %-EBUSY if specified region wasn't 544 + * free (not all bits were zero). 545 + */ 546 + static inline int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order) 547 + { 548 + unsigned int len = BIT(order); 549 + 550 + if (find_next_bit(bitmap, pos + len, pos) < pos + len) 551 + return -EBUSY; 552 + bitmap_set(bitmap, pos, len); 553 + return 0; 554 + } 555 + 556 + /** 557 + * bitmap_find_free_region - find a contiguous aligned mem region 558 + * @bitmap: array of unsigned longs corresponding to the bitmap 559 + * @bits: number of bits in the bitmap 560 + * @order: region size (log base 2 of number of bits) to find 561 + * 562 + * Find a region of free (zero) bits in a @bitmap of @bits bits and 563 + * allocate them (set them to one). Only consider regions of length 564 + * a power (@order) of two, aligned to that power of two, which 565 + * makes the search algorithm much faster. 566 + * 567 + * Returns: the bit offset in bitmap of the allocated region, 568 + * or -errno on failure. 569 + */ 570 + static inline int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order) 571 + { 572 + unsigned int pos, end; /* scans bitmap by regions of size order */ 573 + 574 + for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) { 575 + if (!bitmap_allocate_region(bitmap, pos, order)) 576 + return pos; 577 + } 578 + return -ENOMEM; 496 579 } 497 580 498 581 /**
+2 -1
include/linux/buildid.h
··· 2 2 #ifndef _LINUX_BUILDID_H 3 3 #define _LINUX_BUILDID_H 4 4 5 - #include <linux/mm_types.h> 5 + #include <linux/types.h> 6 6 7 7 #define BUILD_ID_SIZE_MAX 20 8 8 9 + struct vm_area_struct; 9 10 int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, 10 11 __u32 *size); 11 12 int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size);
+69 -44
include/linux/cpumask.h
··· 4 4 5 5 /* 6 6 * Cpumasks provide a bitmap suitable for representing the 7 - * set of CPU's in a system, one bit position per CPU number. In general, 7 + * set of CPUs in a system, one bit position per CPU number. In general, 8 8 * only nr_cpu_ids (<= NR_CPUS) bits are valid. 9 9 */ 10 10 #include <linux/kernel.h> ··· 97 97 * 98 98 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online. 99 99 * 100 - * The cpu_possible_mask is fixed at boot time, as the set of CPU id's 100 + * The cpu_possible_mask is fixed at boot time, as the set of CPU IDs 101 101 * that it is possible might ever be plugged in at anytime during the 102 102 * life of that system boot. The cpu_present_mask is dynamic(*), 103 103 * representing which CPUs are currently plugged in. And ··· 112 112 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot. 113 113 * 114 114 * Subtleties: 115 - * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode 115 + * 1) UP ARCHes (NR_CPUS == 1, CONFIG_SMP not defined) hardcode 116 116 * assumption that their single CPU is online. The UP 117 117 * cpu_{online,possible,present}_masks are placebos. Changing them 118 118 * will have no useful affect on the following num_*_cpus() ··· 155 155 * cpumask_first - get the first cpu in a cpumask 156 156 * @srcp: the cpumask pointer 157 157 * 158 - * Returns >= nr_cpu_ids if no cpus set. 158 + * Return: >= nr_cpu_ids if no cpus set. 159 159 */ 160 160 static inline unsigned int cpumask_first(const struct cpumask *srcp) 161 161 { ··· 166 166 * cpumask_first_zero - get the first unset cpu in a cpumask 167 167 * @srcp: the cpumask pointer 168 168 * 169 - * Returns >= nr_cpu_ids if all cpus are set. 169 + * Return: >= nr_cpu_ids if all cpus are set. 170 170 */ 171 171 static inline unsigned int cpumask_first_zero(const struct cpumask *srcp) 172 172 { ··· 178 178 * @srcp1: the first input 179 179 * @srcp2: the second input 180 180 * 181 - * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and(). 181 + * Return: >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and(). 182 182 */ 183 183 static inline 184 184 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2) ··· 190 190 * cpumask_last - get the last CPU in a cpumask 191 191 * @srcp: - the cpumask pointer 192 192 * 193 - * Returns >= nr_cpumask_bits if no CPUs set. 193 + * Return: >= nr_cpumask_bits if no CPUs set. 194 194 */ 195 195 static inline unsigned int cpumask_last(const struct cpumask *srcp) 196 196 { ··· 199 199 200 200 /** 201 201 * cpumask_next - get the next cpu in a cpumask 202 - * @n: the cpu prior to the place to search (ie. return will be > @n) 202 + * @n: the cpu prior to the place to search (i.e. return will be > @n) 203 203 * @srcp: the cpumask pointer 204 204 * 205 - * Returns >= nr_cpu_ids if no further cpus set. 205 + * Return: >= nr_cpu_ids if no further cpus set. 206 206 */ 207 207 static inline 208 208 unsigned int cpumask_next(int n, const struct cpumask *srcp) ··· 215 215 216 216 /** 217 217 * cpumask_next_zero - get the next unset cpu in a cpumask 218 - * @n: the cpu prior to the place to search (ie. return will be > @n) 218 + * @n: the cpu prior to the place to search (i.e. return will be > @n) 219 219 * @srcp: the cpumask pointer 220 220 * 221 - * Returns >= nr_cpu_ids if no further cpus unset. 221 + * Return: >= nr_cpu_ids if no further cpus unset. 222 222 */ 223 223 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp) 224 224 { ··· 254 254 255 255 /** 256 256 * cpumask_next_and - get the next cpu in *src1p & *src2p 257 - * @n: the cpu prior to the place to search (ie. return will be > @n) 257 + * @n: the cpu prior to the place to search (i.e. return will be > @n) 258 258 * @src1p: the first cpumask pointer 259 259 * @src2p: the second cpumask pointer 260 260 * 261 - * Returns >= nr_cpu_ids if no further cpus set in both. 261 + * Return: >= nr_cpu_ids if no further cpus set in both. 262 262 */ 263 263 static inline 264 264 unsigned int cpumask_next_and(int n, const struct cpumask *src1p, ··· 373 373 * @cpu: the cpu to ignore. 374 374 * 375 375 * Often used to find any cpu but smp_processor_id() in a mask. 376 - * Returns >= nr_cpu_ids if no cpus set. 376 + * Return: >= nr_cpu_ids if no cpus set. 377 377 */ 378 378 static inline 379 379 unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) ··· 388 388 } 389 389 390 390 /** 391 - * cpumask_nth - get the first cpu in a cpumask 391 + * cpumask_nth - get the Nth cpu in a cpumask 392 392 * @srcp: the cpumask pointer 393 - * @cpu: the N'th cpu to find, starting from 0 393 + * @cpu: the Nth cpu to find, starting from 0 394 394 * 395 - * Returns >= nr_cpu_ids if such cpu doesn't exist. 395 + * Return: >= nr_cpu_ids if such cpu doesn't exist. 396 396 */ 397 397 static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp) 398 398 { ··· 400 400 } 401 401 402 402 /** 403 - * cpumask_nth_and - get the first cpu in 2 cpumasks 403 + * cpumask_nth_and - get the Nth cpu in 2 cpumasks 404 404 * @srcp1: the cpumask pointer 405 405 * @srcp2: the cpumask pointer 406 - * @cpu: the N'th cpu to find, starting from 0 406 + * @cpu: the Nth cpu to find, starting from 0 407 407 * 408 - * Returns >= nr_cpu_ids if such cpu doesn't exist. 408 + * Return: >= nr_cpu_ids if such cpu doesn't exist. 409 409 */ 410 410 static inline 411 411 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1, ··· 416 416 } 417 417 418 418 /** 419 - * cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in 2nd. 419 + * cpumask_nth_andnot - get the Nth cpu set in 1st cpumask, and clear in 2nd. 420 420 * @srcp1: the cpumask pointer 421 421 * @srcp2: the cpumask pointer 422 - * @cpu: the N'th cpu to find, starting from 0 422 + * @cpu: the Nth cpu to find, starting from 0 423 423 * 424 - * Returns >= nr_cpu_ids if such cpu doesn't exist. 424 + * Return: >= nr_cpu_ids if such cpu doesn't exist. 425 425 */ 426 426 static inline 427 427 unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1, ··· 436 436 * @srcp1: the cpumask pointer 437 437 * @srcp2: the cpumask pointer 438 438 * @srcp3: the cpumask pointer 439 - * @cpu: the N'th cpu to find, starting from 0 439 + * @cpu: the Nth cpu to find, starting from 0 440 440 * 441 - * Returns >= nr_cpu_ids if such cpu doesn't exist. 441 + * Return: >= nr_cpu_ids if such cpu doesn't exist. 442 442 */ 443 443 static __always_inline 444 444 unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1, ··· 497 497 * @cpu: cpu number (< nr_cpu_ids) 498 498 * @cpumask: the cpumask pointer 499 499 * 500 - * Returns true if @cpu is set in @cpumask, else returns false 500 + * Return: true if @cpu is set in @cpumask, else returns false 501 501 */ 502 502 static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask) 503 503 { ··· 509 509 * @cpu: cpu number (< nr_cpu_ids) 510 510 * @cpumask: the cpumask pointer 511 511 * 512 - * Returns true if @cpu is set in old bitmap of @cpumask, else returns false 513 - * 514 512 * test_and_set_bit wrapper for cpumasks. 513 + * 514 + * Return: true if @cpu is set in old bitmap of @cpumask, else returns false 515 515 */ 516 516 static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask) 517 517 { ··· 523 523 * @cpu: cpu number (< nr_cpu_ids) 524 524 * @cpumask: the cpumask pointer 525 525 * 526 - * Returns true if @cpu is set in old bitmap of @cpumask, else returns false 527 - * 528 526 * test_and_clear_bit wrapper for cpumasks. 527 + * 528 + * Return: true if @cpu is set in old bitmap of @cpumask, else returns false 529 529 */ 530 530 static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask) 531 531 { ··· 560 560 * @src1p: the first input 561 561 * @src2p: the second input 562 562 * 563 - * If *@dstp is empty, returns false, else returns true 563 + * Return: false if *@dstp is empty, else returns true 564 564 */ 565 565 static inline bool cpumask_and(struct cpumask *dstp, 566 566 const struct cpumask *src1p, ··· 603 603 * @src1p: the first input 604 604 * @src2p: the second input 605 605 * 606 - * If *@dstp is empty, returns false, else returns true 606 + * Return: false if *@dstp is empty, else returns true 607 607 */ 608 608 static inline bool cpumask_andnot(struct cpumask *dstp, 609 609 const struct cpumask *src1p, ··· 617 617 * cpumask_equal - *src1p == *src2p 618 618 * @src1p: the first input 619 619 * @src2p: the second input 620 + * 621 + * Return: true if the cpumasks are equal, false if not 620 622 */ 621 623 static inline bool cpumask_equal(const struct cpumask *src1p, 622 624 const struct cpumask *src2p) ··· 632 630 * @src1p: the first input 633 631 * @src2p: the second input 634 632 * @src3p: the third input 633 + * 634 + * Return: true if first cpumask ORed with second cpumask == third cpumask, 635 + * otherwise false 635 636 */ 636 637 static inline bool cpumask_or_equal(const struct cpumask *src1p, 637 638 const struct cpumask *src2p, ··· 648 643 * cpumask_intersects - (*src1p & *src2p) != 0 649 644 * @src1p: the first input 650 645 * @src2p: the second input 646 + * 647 + * Return: true if first cpumask ANDed with second cpumask is non-empty, 648 + * otherwise false 651 649 */ 652 650 static inline bool cpumask_intersects(const struct cpumask *src1p, 653 651 const struct cpumask *src2p) ··· 664 656 * @src1p: the first input 665 657 * @src2p: the second input 666 658 * 667 - * Returns true if *@src1p is a subset of *@src2p, else returns false 659 + * Return: true if *@src1p is a subset of *@src2p, else returns false 668 660 */ 669 661 static inline bool cpumask_subset(const struct cpumask *src1p, 670 662 const struct cpumask *src2p) ··· 676 668 /** 677 669 * cpumask_empty - *srcp == 0 678 670 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear. 671 + * 672 + * Return: true if srcp is empty (has no bits set), else false 679 673 */ 680 674 static inline bool cpumask_empty(const struct cpumask *srcp) 681 675 { ··· 687 677 /** 688 678 * cpumask_full - *srcp == 0xFFFFFFFF... 689 679 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set. 680 + * 681 + * Return: true if srcp is full (has all bits set), else false 690 682 */ 691 683 static inline bool cpumask_full(const struct cpumask *srcp) 692 684 { ··· 698 686 /** 699 687 * cpumask_weight - Count of bits in *srcp 700 688 * @srcp: the cpumask to count bits (< nr_cpu_ids) in. 689 + * 690 + * Return: count of bits set in *srcp 701 691 */ 702 692 static inline unsigned int cpumask_weight(const struct cpumask *srcp) 703 693 { ··· 710 696 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2) 711 697 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in. 712 698 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in. 699 + * 700 + * Return: count of bits set in both *srcp1 and *srcp2 713 701 */ 714 702 static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1, 715 703 const struct cpumask *srcp2) ··· 760 744 * cpumask_any - pick a "random" cpu from *srcp 761 745 * @srcp: the input cpumask 762 746 * 763 - * Returns >= nr_cpu_ids if no cpus set. 747 + * Return: >= nr_cpu_ids if no cpus set. 764 748 */ 765 749 #define cpumask_any(srcp) cpumask_first(srcp) 766 750 ··· 769 753 * @mask1: the first input cpumask 770 754 * @mask2: the second input cpumask 771 755 * 772 - * Returns >= nr_cpu_ids if no cpus set. 756 + * Return: >= nr_cpu_ids if no cpus set. 773 757 */ 774 758 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2)) 775 759 ··· 785 769 * @len: the length of the buffer 786 770 * @dstp: the cpumask to set. 787 771 * 788 - * Returns -errno, or 0 for success. 772 + * Return: -errno, or 0 for success. 789 773 */ 790 774 static inline int cpumask_parse_user(const char __user *buf, int len, 791 775 struct cpumask *dstp) ··· 799 783 * @len: the length of the buffer 800 784 * @dstp: the cpumask to set. 801 785 * 802 - * Returns -errno, or 0 for success. 786 + * Return: -errno, or 0 for success. 803 787 */ 804 788 static inline int cpumask_parselist_user(const char __user *buf, int len, 805 789 struct cpumask *dstp) ··· 813 797 * @buf: the buffer to extract from 814 798 * @dstp: the cpumask to set. 815 799 * 816 - * Returns -errno, or 0 for success. 800 + * Return: -errno, or 0 for success. 817 801 */ 818 802 static inline int cpumask_parse(const char *buf, struct cpumask *dstp) 819 803 { ··· 825 809 * @buf: the buffer to extract from 826 810 * @dstp: the cpumask to set. 827 811 * 828 - * Returns -errno, or 0 for success. 812 + * Return: -errno, or 0 for success. 829 813 */ 830 814 static inline int cpulist_parse(const char *buf, struct cpumask *dstp) 831 815 { ··· 833 817 } 834 818 835 819 /** 836 - * cpumask_size - size to allocate for a 'struct cpumask' in bytes 820 + * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes 821 + * 822 + * Return: size to allocate for a &struct cpumask in bytes 837 823 */ 838 824 static inline unsigned int cpumask_size(void) 839 825 { ··· 849 831 * little more difficult, we typedef cpumask_var_t to an array or a 850 832 * pointer: doing &mask on an array is a noop, so it still works. 851 833 * 852 - * ie. 834 + * i.e. 853 835 * cpumask_var_t tmpmask; 854 836 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) 855 837 * return -ENOMEM; ··· 905 887 * a nop returning a constant 1 (in <linux/cpumask.h>). 906 888 * 907 889 * See alloc_cpumask_var_node. 890 + * 891 + * Return: %true if allocation succeeded, %false if not 908 892 */ 909 893 static inline 910 894 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) ··· 1045 1025 } 1046 1026 1047 1027 /** 1048 - * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask * 1028 + * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask * 1049 1029 * @bitmap: the bitmap 1050 1030 * 1051 1031 * There are a few places where cpumask_var_t isn't appropriate and ··· 1088 1068 * interface gives only a momentary snapshot and is not protected against 1089 1069 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held 1090 1070 * region. 1071 + * 1072 + * Return: momentary snapshot of the number of online CPUs 1091 1073 */ 1092 1074 static __always_inline unsigned int num_online_cpus(void) 1093 1075 { ··· 1182 1160 * @mask: the cpumask to copy 1183 1161 * @buf: the buffer to copy into 1184 1162 * 1185 - * Returns the length of the (null-terminated) @buf string, zero if 1163 + * Return: the length of the (null-terminated) @buf string, zero if 1186 1164 * nothing is copied. 1187 1165 */ 1188 1166 static inline ssize_t ··· 1205 1183 * cpumask; Typically used by bin_attribute to export cpumask bitmask 1206 1184 * ABI. 1207 1185 * 1208 - * Returns the length of how many bytes have been copied, excluding 1186 + * Return: the length of how many bytes have been copied, excluding 1209 1187 * terminating '\0'. 1210 1188 */ 1211 1189 static inline ssize_t ··· 1226 1204 * 1227 1205 * Everything is same with the above cpumap_print_bitmask_to_buf() 1228 1206 * except the print format. 1207 + * 1208 + * Return: the length of how many bytes have been copied, excluding 1209 + * terminating '\0'. 1229 1210 */ 1230 1211 static inline ssize_t 1231 1212 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
+1 -1
lib/Makefile
··· 48 48 bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \ 49 49 percpu-refcount.o rhashtable.o base64.o \ 50 50 once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \ 51 - generic-radix-tree.o 51 + generic-radix-tree.o bitmap-str.o 52 52 obj-$(CONFIG_STRING_SELFTEST) += test_string.o 53 53 obj-y += string_helpers.o 54 54 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
+510
lib/bitmap-str.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + #include <linux/bitmap.h> 4 + #include <linux/ctype.h> 5 + #include <linux/errno.h> 6 + #include <linux/err.h> 7 + #include <linux/export.h> 8 + #include <linux/hex.h> 9 + #include <linux/kernel.h> 10 + #include <linux/mm.h> 11 + #include <linux/string.h> 12 + 13 + #include "kstrtox.h" 14 + 15 + /** 16 + * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap 17 + * 18 + * @ubuf: pointer to user buffer containing string. 19 + * @ulen: buffer size in bytes. If string is smaller than this 20 + * then it must be terminated with a \0. 21 + * @maskp: pointer to bitmap array that will contain result. 22 + * @nmaskbits: size of bitmap, in bits. 23 + */ 24 + int bitmap_parse_user(const char __user *ubuf, 25 + unsigned int ulen, unsigned long *maskp, 26 + int nmaskbits) 27 + { 28 + char *buf; 29 + int ret; 30 + 31 + buf = memdup_user_nul(ubuf, ulen); 32 + if (IS_ERR(buf)) 33 + return PTR_ERR(buf); 34 + 35 + ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits); 36 + 37 + kfree(buf); 38 + return ret; 39 + } 40 + EXPORT_SYMBOL(bitmap_parse_user); 41 + 42 + /** 43 + * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string 44 + * @list: indicates whether the bitmap must be list 45 + * @buf: page aligned buffer into which string is placed 46 + * @maskp: pointer to bitmap to convert 47 + * @nmaskbits: size of bitmap, in bits 48 + * 49 + * Output format is a comma-separated list of decimal numbers and 50 + * ranges if list is specified or hex digits grouped into comma-separated 51 + * sets of 8 digits/set. Returns the number of characters written to buf. 52 + * 53 + * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned 54 + * area and that sufficient storage remains at @buf to accommodate the 55 + * bitmap_print_to_pagebuf() output. Returns the number of characters 56 + * actually printed to @buf, excluding terminating '\0'. 57 + */ 58 + int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, 59 + int nmaskbits) 60 + { 61 + ptrdiff_t len = PAGE_SIZE - offset_in_page(buf); 62 + 63 + return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) : 64 + scnprintf(buf, len, "%*pb\n", nmaskbits, maskp); 65 + } 66 + EXPORT_SYMBOL(bitmap_print_to_pagebuf); 67 + 68 + /** 69 + * bitmap_print_to_buf - convert bitmap to list or hex format ASCII string 70 + * @list: indicates whether the bitmap must be list 71 + * true: print in decimal list format 72 + * false: print in hexadecimal bitmask format 73 + * @buf: buffer into which string is placed 74 + * @maskp: pointer to bitmap to convert 75 + * @nmaskbits: size of bitmap, in bits 76 + * @off: in the string from which we are copying, We copy to @buf 77 + * @count: the maximum number of bytes to print 78 + */ 79 + static int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, 80 + int nmaskbits, loff_t off, size_t count) 81 + { 82 + const char *fmt = list ? "%*pbl\n" : "%*pb\n"; 83 + ssize_t size; 84 + void *data; 85 + 86 + data = kasprintf(GFP_KERNEL, fmt, nmaskbits, maskp); 87 + if (!data) 88 + return -ENOMEM; 89 + 90 + size = memory_read_from_buffer(buf, count, &off, data, strlen(data) + 1); 91 + kfree(data); 92 + 93 + return size; 94 + } 95 + 96 + /** 97 + * bitmap_print_bitmask_to_buf - convert bitmap to hex bitmask format ASCII string 98 + * @buf: buffer into which string is placed 99 + * @maskp: pointer to bitmap to convert 100 + * @nmaskbits: size of bitmap, in bits 101 + * @off: in the string from which we are copying, We copy to @buf 102 + * @count: the maximum number of bytes to print 103 + * 104 + * The bitmap_print_to_pagebuf() is used indirectly via its cpumap wrapper 105 + * cpumap_print_to_pagebuf() or directly by drivers to export hexadecimal 106 + * bitmask and decimal list to userspace by sysfs ABI. 107 + * Drivers might be using a normal attribute for this kind of ABIs. A 108 + * normal attribute typically has show entry as below:: 109 + * 110 + * static ssize_t example_attribute_show(struct device *dev, 111 + * struct device_attribute *attr, char *buf) 112 + * { 113 + * ... 114 + * return bitmap_print_to_pagebuf(true, buf, &mask, nr_trig_max); 115 + * } 116 + * 117 + * show entry of attribute has no offset and count parameters and this 118 + * means the file is limited to one page only. 119 + * bitmap_print_to_pagebuf() API works terribly well for this kind of 120 + * normal attribute with buf parameter and without offset, count:: 121 + * 122 + * bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, 123 + * int nmaskbits) 124 + * { 125 + * } 126 + * 127 + * The problem is once we have a large bitmap, we have a chance to get a 128 + * bitmask or list more than one page. Especially for list, it could be 129 + * as complex as 0,3,5,7,9,... We have no simple way to know it exact size. 130 + * It turns out bin_attribute is a way to break this limit. bin_attribute 131 + * has show entry as below:: 132 + * 133 + * static ssize_t 134 + * example_bin_attribute_show(struct file *filp, struct kobject *kobj, 135 + * struct bin_attribute *attr, char *buf, 136 + * loff_t offset, size_t count) 137 + * { 138 + * ... 139 + * } 140 + * 141 + * With the new offset and count parameters, this makes sysfs ABI be able 142 + * to support file size more than one page. For example, offset could be 143 + * >= 4096. 144 + * bitmap_print_bitmask_to_buf(), bitmap_print_list_to_buf() wit their 145 + * cpumap wrapper cpumap_print_bitmask_to_buf(), cpumap_print_list_to_buf() 146 + * make those drivers be able to support large bitmask and list after they 147 + * move to use bin_attribute. In result, we have to pass the corresponding 148 + * parameters such as off, count from bin_attribute show entry to this API. 149 + * 150 + * The role of cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf() 151 + * is similar with cpumap_print_to_pagebuf(), the difference is that 152 + * bitmap_print_to_pagebuf() mainly serves sysfs attribute with the assumption 153 + * the destination buffer is exactly one page and won't be more than one page. 154 + * cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf(), on the other 155 + * hand, mainly serves bin_attribute which doesn't work with exact one page, 156 + * and it can break the size limit of converted decimal list and hexadecimal 157 + * bitmask. 158 + * 159 + * WARNING! 160 + * 161 + * This function is not a replacement for sprintf() or bitmap_print_to_pagebuf(). 162 + * It is intended to workaround sysfs limitations discussed above and should be 163 + * used carefully in general case for the following reasons: 164 + * 165 + * - Time complexity is O(nbits^2/count), comparing to O(nbits) for snprintf(). 166 + * - Memory complexity is O(nbits), comparing to O(1) for snprintf(). 167 + * - @off and @count are NOT offset and number of bits to print. 168 + * - If printing part of bitmap as list, the resulting string is not a correct 169 + * list representation of bitmap. Particularly, some bits within or out of 170 + * related interval may be erroneously set or unset. The format of the string 171 + * may be broken, so bitmap_parselist-like parser may fail parsing it. 172 + * - If printing the whole bitmap as list by parts, user must ensure the order 173 + * of calls of the function such that the offset is incremented linearly. 174 + * - If printing the whole bitmap as list by parts, user must keep bitmap 175 + * unchanged between the very first and very last call. Otherwise concatenated 176 + * result may be incorrect, and format may be broken. 177 + * 178 + * Returns the number of characters actually printed to @buf 179 + */ 180 + int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp, 181 + int nmaskbits, loff_t off, size_t count) 182 + { 183 + return bitmap_print_to_buf(false, buf, maskp, nmaskbits, off, count); 184 + } 185 + EXPORT_SYMBOL(bitmap_print_bitmask_to_buf); 186 + 187 + /** 188 + * bitmap_print_list_to_buf - convert bitmap to decimal list format ASCII string 189 + * @buf: buffer into which string is placed 190 + * @maskp: pointer to bitmap to convert 191 + * @nmaskbits: size of bitmap, in bits 192 + * @off: in the string from which we are copying, We copy to @buf 193 + * @count: the maximum number of bytes to print 194 + * 195 + * Everything is same with the above bitmap_print_bitmask_to_buf() except 196 + * the print format. 197 + */ 198 + int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp, 199 + int nmaskbits, loff_t off, size_t count) 200 + { 201 + return bitmap_print_to_buf(true, buf, maskp, nmaskbits, off, count); 202 + } 203 + EXPORT_SYMBOL(bitmap_print_list_to_buf); 204 + 205 + /* 206 + * Region 9-38:4/10 describes the following bitmap structure: 207 + * 0 9 12 18 38 N 208 + * .........****......****......****.................. 209 + * ^ ^ ^ ^ ^ 210 + * start off group_len end nbits 211 + */ 212 + struct region { 213 + unsigned int start; 214 + unsigned int off; 215 + unsigned int group_len; 216 + unsigned int end; 217 + unsigned int nbits; 218 + }; 219 + 220 + static void bitmap_set_region(const struct region *r, unsigned long *bitmap) 221 + { 222 + unsigned int start; 223 + 224 + for (start = r->start; start <= r->end; start += r->group_len) 225 + bitmap_set(bitmap, start, min(r->end - start + 1, r->off)); 226 + } 227 + 228 + static int bitmap_check_region(const struct region *r) 229 + { 230 + if (r->start > r->end || r->group_len == 0 || r->off > r->group_len) 231 + return -EINVAL; 232 + 233 + if (r->end >= r->nbits) 234 + return -ERANGE; 235 + 236 + return 0; 237 + } 238 + 239 + static const char *bitmap_getnum(const char *str, unsigned int *num, 240 + unsigned int lastbit) 241 + { 242 + unsigned long long n; 243 + unsigned int len; 244 + 245 + if (str[0] == 'N') { 246 + *num = lastbit; 247 + return str + 1; 248 + } 249 + 250 + len = _parse_integer(str, 10, &n); 251 + if (!len) 252 + return ERR_PTR(-EINVAL); 253 + if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n) 254 + return ERR_PTR(-EOVERFLOW); 255 + 256 + *num = n; 257 + return str + len; 258 + } 259 + 260 + static inline bool end_of_str(char c) 261 + { 262 + return c == '\0' || c == '\n'; 263 + } 264 + 265 + static inline bool __end_of_region(char c) 266 + { 267 + return isspace(c) || c == ','; 268 + } 269 + 270 + static inline bool end_of_region(char c) 271 + { 272 + return __end_of_region(c) || end_of_str(c); 273 + } 274 + 275 + /* 276 + * The format allows commas and whitespaces at the beginning 277 + * of the region. 278 + */ 279 + static const char *bitmap_find_region(const char *str) 280 + { 281 + while (__end_of_region(*str)) 282 + str++; 283 + 284 + return end_of_str(*str) ? NULL : str; 285 + } 286 + 287 + static const char *bitmap_find_region_reverse(const char *start, const char *end) 288 + { 289 + while (start <= end && __end_of_region(*end)) 290 + end--; 291 + 292 + return end; 293 + } 294 + 295 + static const char *bitmap_parse_region(const char *str, struct region *r) 296 + { 297 + unsigned int lastbit = r->nbits - 1; 298 + 299 + if (!strncasecmp(str, "all", 3)) { 300 + r->start = 0; 301 + r->end = lastbit; 302 + str += 3; 303 + 304 + goto check_pattern; 305 + } 306 + 307 + str = bitmap_getnum(str, &r->start, lastbit); 308 + if (IS_ERR(str)) 309 + return str; 310 + 311 + if (end_of_region(*str)) 312 + goto no_end; 313 + 314 + if (*str != '-') 315 + return ERR_PTR(-EINVAL); 316 + 317 + str = bitmap_getnum(str + 1, &r->end, lastbit); 318 + if (IS_ERR(str)) 319 + return str; 320 + 321 + check_pattern: 322 + if (end_of_region(*str)) 323 + goto no_pattern; 324 + 325 + if (*str != ':') 326 + return ERR_PTR(-EINVAL); 327 + 328 + str = bitmap_getnum(str + 1, &r->off, lastbit); 329 + if (IS_ERR(str)) 330 + return str; 331 + 332 + if (*str != '/') 333 + return ERR_PTR(-EINVAL); 334 + 335 + return bitmap_getnum(str + 1, &r->group_len, lastbit); 336 + 337 + no_end: 338 + r->end = r->start; 339 + no_pattern: 340 + r->off = r->end + 1; 341 + r->group_len = r->end + 1; 342 + 343 + return end_of_str(*str) ? NULL : str; 344 + } 345 + 346 + /** 347 + * bitmap_parselist - convert list format ASCII string to bitmap 348 + * @buf: read user string from this buffer; must be terminated 349 + * with a \0 or \n. 350 + * @maskp: write resulting mask here 351 + * @nmaskbits: number of bits in mask to be written 352 + * 353 + * Input format is a comma-separated list of decimal numbers and 354 + * ranges. Consecutively set bits are shown as two hyphen-separated 355 + * decimal numbers, the smallest and largest bit numbers set in 356 + * the range. 357 + * Optionally each range can be postfixed to denote that only parts of it 358 + * should be set. The range will divided to groups of specific size. 359 + * From each group will be used only defined amount of bits. 360 + * Syntax: range:used_size/group_size 361 + * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769 362 + * The value 'N' can be used as a dynamically substituted token for the 363 + * maximum allowed value; i.e (nmaskbits - 1). Keep in mind that it is 364 + * dynamic, so if system changes cause the bitmap width to change, such 365 + * as more cores in a CPU list, then any ranges using N will also change. 366 + * 367 + * Returns: 0 on success, -errno on invalid input strings. Error values: 368 + * 369 + * - ``-EINVAL``: wrong region format 370 + * - ``-EINVAL``: invalid character in string 371 + * - ``-ERANGE``: bit number specified too large for mask 372 + * - ``-EOVERFLOW``: integer overflow in the input parameters 373 + */ 374 + int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits) 375 + { 376 + struct region r; 377 + long ret; 378 + 379 + r.nbits = nmaskbits; 380 + bitmap_zero(maskp, r.nbits); 381 + 382 + while (buf) { 383 + buf = bitmap_find_region(buf); 384 + if (buf == NULL) 385 + return 0; 386 + 387 + buf = bitmap_parse_region(buf, &r); 388 + if (IS_ERR(buf)) 389 + return PTR_ERR(buf); 390 + 391 + ret = bitmap_check_region(&r); 392 + if (ret) 393 + return ret; 394 + 395 + bitmap_set_region(&r, maskp); 396 + } 397 + 398 + return 0; 399 + } 400 + EXPORT_SYMBOL(bitmap_parselist); 401 + 402 + 403 + /** 404 + * bitmap_parselist_user() - convert user buffer's list format ASCII 405 + * string to bitmap 406 + * 407 + * @ubuf: pointer to user buffer containing string. 408 + * @ulen: buffer size in bytes. If string is smaller than this 409 + * then it must be terminated with a \0. 410 + * @maskp: pointer to bitmap array that will contain result. 411 + * @nmaskbits: size of bitmap, in bits. 412 + * 413 + * Wrapper for bitmap_parselist(), providing it with user buffer. 414 + */ 415 + int bitmap_parselist_user(const char __user *ubuf, 416 + unsigned int ulen, unsigned long *maskp, 417 + int nmaskbits) 418 + { 419 + char *buf; 420 + int ret; 421 + 422 + buf = memdup_user_nul(ubuf, ulen); 423 + if (IS_ERR(buf)) 424 + return PTR_ERR(buf); 425 + 426 + ret = bitmap_parselist(buf, maskp, nmaskbits); 427 + 428 + kfree(buf); 429 + return ret; 430 + } 431 + EXPORT_SYMBOL(bitmap_parselist_user); 432 + 433 + static const char *bitmap_get_x32_reverse(const char *start, 434 + const char *end, u32 *num) 435 + { 436 + u32 ret = 0; 437 + int c, i; 438 + 439 + for (i = 0; i < 32; i += 4) { 440 + c = hex_to_bin(*end--); 441 + if (c < 0) 442 + return ERR_PTR(-EINVAL); 443 + 444 + ret |= c << i; 445 + 446 + if (start > end || __end_of_region(*end)) 447 + goto out; 448 + } 449 + 450 + if (hex_to_bin(*end--) >= 0) 451 + return ERR_PTR(-EOVERFLOW); 452 + out: 453 + *num = ret; 454 + return end; 455 + } 456 + 457 + /** 458 + * bitmap_parse - convert an ASCII hex string into a bitmap. 459 + * @start: pointer to buffer containing string. 460 + * @buflen: buffer size in bytes. If string is smaller than this 461 + * then it must be terminated with a \0 or \n. In that case, 462 + * UINT_MAX may be provided instead of string length. 463 + * @maskp: pointer to bitmap array that will contain result. 464 + * @nmaskbits: size of bitmap, in bits. 465 + * 466 + * Commas group hex digits into chunks. Each chunk defines exactly 32 467 + * bits of the resultant bitmask. No chunk may specify a value larger 468 + * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value 469 + * then leading 0-bits are prepended. %-EINVAL is returned for illegal 470 + * characters. Grouping such as "1,,5", ",44", "," or "" is allowed. 471 + * Leading, embedded and trailing whitespace accepted. 472 + */ 473 + int bitmap_parse(const char *start, unsigned int buflen, 474 + unsigned long *maskp, int nmaskbits) 475 + { 476 + const char *end = strnchrnul(start, buflen, '\n') - 1; 477 + int chunks = BITS_TO_U32(nmaskbits); 478 + u32 *bitmap = (u32 *)maskp; 479 + int unset_bit; 480 + int chunk; 481 + 482 + for (chunk = 0; ; chunk++) { 483 + end = bitmap_find_region_reverse(start, end); 484 + if (start > end) 485 + break; 486 + 487 + if (!chunks--) 488 + return -EOVERFLOW; 489 + 490 + #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN) 491 + end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]); 492 + #else 493 + end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]); 494 + #endif 495 + if (IS_ERR(end)) 496 + return PTR_ERR(end); 497 + } 498 + 499 + unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32; 500 + if (unset_bit < nmaskbits) { 501 + bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit); 502 + return 0; 503 + } 504 + 505 + if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit) 506 + return -EOVERFLOW; 507 + 508 + return 0; 509 + } 510 + EXPORT_SYMBOL(bitmap_parse);
+2 -678
lib/bitmap.c
··· 6 6 7 7 #include <linux/bitmap.h> 8 8 #include <linux/bitops.h> 9 - #include <linux/bug.h> 10 9 #include <linux/ctype.h> 11 10 #include <linux/device.h> 12 - #include <linux/errno.h> 13 11 #include <linux/export.h> 14 - #include <linux/kernel.h> 15 - #include <linux/mm.h> 16 12 #include <linux/slab.h> 17 - #include <linux/string.h> 18 - #include <linux/thread_info.h> 19 - #include <linux/uaccess.h> 20 - 21 - #include <asm/page.h> 22 - 23 - #include "kstrtox.h" 24 13 25 14 /** 26 15 * DOC: bitmap introduction ··· 429 440 } 430 441 EXPORT_SYMBOL(bitmap_find_next_zero_area_off); 431 442 432 - /* 433 - * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers, 434 - * second version by Paul Jackson, third by Joe Korty. 435 - */ 436 - 437 - /** 438 - * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap 439 - * 440 - * @ubuf: pointer to user buffer containing string. 441 - * @ulen: buffer size in bytes. If string is smaller than this 442 - * then it must be terminated with a \0. 443 - * @maskp: pointer to bitmap array that will contain result. 444 - * @nmaskbits: size of bitmap, in bits. 445 - */ 446 - int bitmap_parse_user(const char __user *ubuf, 447 - unsigned int ulen, unsigned long *maskp, 448 - int nmaskbits) 449 - { 450 - char *buf; 451 - int ret; 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; 461 - } 462 - EXPORT_SYMBOL(bitmap_parse_user); 463 - 464 - /** 465 - * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string 466 - * @list: indicates whether the bitmap must be list 467 - * @buf: page aligned buffer into which string is placed 468 - * @maskp: pointer to bitmap to convert 469 - * @nmaskbits: size of bitmap, in bits 470 - * 471 - * Output format is a comma-separated list of decimal numbers and 472 - * ranges if list is specified or hex digits grouped into comma-separated 473 - * sets of 8 digits/set. Returns the number of characters written to buf. 474 - * 475 - * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned 476 - * area and that sufficient storage remains at @buf to accommodate the 477 - * bitmap_print_to_pagebuf() output. Returns the number of characters 478 - * actually printed to @buf, excluding terminating '\0'. 479 - */ 480 - int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, 481 - int nmaskbits) 482 - { 483 - ptrdiff_t len = PAGE_SIZE - offset_in_page(buf); 484 - 485 - return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) : 486 - scnprintf(buf, len, "%*pb\n", nmaskbits, maskp); 487 - } 488 - EXPORT_SYMBOL(bitmap_print_to_pagebuf); 489 - 490 - /** 491 - * bitmap_print_to_buf - convert bitmap to list or hex format ASCII string 492 - * @list: indicates whether the bitmap must be list 493 - * true: print in decimal list format 494 - * false: print in hexadecimal bitmask format 495 - * @buf: buffer into which string is placed 496 - * @maskp: pointer to bitmap to convert 497 - * @nmaskbits: size of bitmap, in bits 498 - * @off: in the string from which we are copying, We copy to @buf 499 - * @count: the maximum number of bytes to print 500 - */ 501 - static int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp, 502 - int nmaskbits, loff_t off, size_t count) 503 - { 504 - const char *fmt = list ? "%*pbl\n" : "%*pb\n"; 505 - ssize_t size; 506 - void *data; 507 - 508 - data = kasprintf(GFP_KERNEL, fmt, nmaskbits, maskp); 509 - if (!data) 510 - return -ENOMEM; 511 - 512 - size = memory_read_from_buffer(buf, count, &off, data, strlen(data) + 1); 513 - kfree(data); 514 - 515 - return size; 516 - } 517 - 518 - /** 519 - * bitmap_print_bitmask_to_buf - convert bitmap to hex bitmask format ASCII string 520 - * @buf: buffer into which string is placed 521 - * @maskp: pointer to bitmap to convert 522 - * @nmaskbits: size of bitmap, in bits 523 - * @off: in the string from which we are copying, We copy to @buf 524 - * @count: the maximum number of bytes to print 525 - * 526 - * The bitmap_print_to_pagebuf() is used indirectly via its cpumap wrapper 527 - * cpumap_print_to_pagebuf() or directly by drivers to export hexadecimal 528 - * bitmask and decimal list to userspace by sysfs ABI. 529 - * Drivers might be using a normal attribute for this kind of ABIs. A 530 - * normal attribute typically has show entry as below:: 531 - * 532 - * static ssize_t example_attribute_show(struct device *dev, 533 - * struct device_attribute *attr, char *buf) 534 - * { 535 - * ... 536 - * return bitmap_print_to_pagebuf(true, buf, &mask, nr_trig_max); 537 - * } 538 - * 539 - * show entry of attribute has no offset and count parameters and this 540 - * means the file is limited to one page only. 541 - * bitmap_print_to_pagebuf() API works terribly well for this kind of 542 - * normal attribute with buf parameter and without offset, count:: 543 - * 544 - * bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, 545 - * int nmaskbits) 546 - * { 547 - * } 548 - * 549 - * The problem is once we have a large bitmap, we have a chance to get a 550 - * bitmask or list more than one page. Especially for list, it could be 551 - * as complex as 0,3,5,7,9,... We have no simple way to know it exact size. 552 - * It turns out bin_attribute is a way to break this limit. bin_attribute 553 - * has show entry as below:: 554 - * 555 - * static ssize_t 556 - * example_bin_attribute_show(struct file *filp, struct kobject *kobj, 557 - * struct bin_attribute *attr, char *buf, 558 - * loff_t offset, size_t count) 559 - * { 560 - * ... 561 - * } 562 - * 563 - * With the new offset and count parameters, this makes sysfs ABI be able 564 - * to support file size more than one page. For example, offset could be 565 - * >= 4096. 566 - * bitmap_print_bitmask_to_buf(), bitmap_print_list_to_buf() wit their 567 - * cpumap wrapper cpumap_print_bitmask_to_buf(), cpumap_print_list_to_buf() 568 - * make those drivers be able to support large bitmask and list after they 569 - * move to use bin_attribute. In result, we have to pass the corresponding 570 - * parameters such as off, count from bin_attribute show entry to this API. 571 - * 572 - * The role of cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf() 573 - * is similar with cpumap_print_to_pagebuf(), the difference is that 574 - * bitmap_print_to_pagebuf() mainly serves sysfs attribute with the assumption 575 - * the destination buffer is exactly one page and won't be more than one page. 576 - * cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf(), on the other 577 - * hand, mainly serves bin_attribute which doesn't work with exact one page, 578 - * and it can break the size limit of converted decimal list and hexadecimal 579 - * bitmask. 580 - * 581 - * WARNING! 582 - * 583 - * This function is not a replacement for sprintf() or bitmap_print_to_pagebuf(). 584 - * It is intended to workaround sysfs limitations discussed above and should be 585 - * used carefully in general case for the following reasons: 586 - * 587 - * - Time complexity is O(nbits^2/count), comparing to O(nbits) for snprintf(). 588 - * - Memory complexity is O(nbits), comparing to O(1) for snprintf(). 589 - * - @off and @count are NOT offset and number of bits to print. 590 - * - If printing part of bitmap as list, the resulting string is not a correct 591 - * list representation of bitmap. Particularly, some bits within or out of 592 - * related interval may be erroneously set or unset. The format of the string 593 - * may be broken, so bitmap_parselist-like parser may fail parsing it. 594 - * - If printing the whole bitmap as list by parts, user must ensure the order 595 - * of calls of the function such that the offset is incremented linearly. 596 - * - If printing the whole bitmap as list by parts, user must keep bitmap 597 - * unchanged between the very first and very last call. Otherwise concatenated 598 - * result may be incorrect, and format may be broken. 599 - * 600 - * Returns the number of characters actually printed to @buf 601 - */ 602 - int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp, 603 - int nmaskbits, loff_t off, size_t count) 604 - { 605 - return bitmap_print_to_buf(false, buf, maskp, nmaskbits, off, count); 606 - } 607 - EXPORT_SYMBOL(bitmap_print_bitmask_to_buf); 608 - 609 - /** 610 - * bitmap_print_list_to_buf - convert bitmap to decimal list format ASCII string 611 - * @buf: buffer into which string is placed 612 - * @maskp: pointer to bitmap to convert 613 - * @nmaskbits: size of bitmap, in bits 614 - * @off: in the string from which we are copying, We copy to @buf 615 - * @count: the maximum number of bytes to print 616 - * 617 - * Everything is same with the above bitmap_print_bitmask_to_buf() except 618 - * the print format. 619 - */ 620 - int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp, 621 - int nmaskbits, loff_t off, size_t count) 622 - { 623 - return bitmap_print_to_buf(true, buf, maskp, nmaskbits, off, count); 624 - } 625 - EXPORT_SYMBOL(bitmap_print_list_to_buf); 626 - 627 - /* 628 - * Region 9-38:4/10 describes the following bitmap structure: 629 - * 0 9 12 18 38 N 630 - * .........****......****......****.................. 631 - * ^ ^ ^ ^ ^ 632 - * start off group_len end nbits 633 - */ 634 - struct region { 635 - unsigned int start; 636 - unsigned int off; 637 - unsigned int group_len; 638 - unsigned int end; 639 - unsigned int nbits; 640 - }; 641 - 642 - static void bitmap_set_region(const struct region *r, unsigned long *bitmap) 643 - { 644 - unsigned int start; 645 - 646 - for (start = r->start; start <= r->end; start += r->group_len) 647 - bitmap_set(bitmap, start, min(r->end - start + 1, r->off)); 648 - } 649 - 650 - static int bitmap_check_region(const struct region *r) 651 - { 652 - if (r->start > r->end || r->group_len == 0 || r->off > r->group_len) 653 - return -EINVAL; 654 - 655 - if (r->end >= r->nbits) 656 - return -ERANGE; 657 - 658 - return 0; 659 - } 660 - 661 - static const char *bitmap_getnum(const char *str, unsigned int *num, 662 - unsigned int lastbit) 663 - { 664 - unsigned long long n; 665 - unsigned int len; 666 - 667 - if (str[0] == 'N') { 668 - *num = lastbit; 669 - return str + 1; 670 - } 671 - 672 - len = _parse_integer(str, 10, &n); 673 - if (!len) 674 - return ERR_PTR(-EINVAL); 675 - if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n) 676 - return ERR_PTR(-EOVERFLOW); 677 - 678 - *num = n; 679 - return str + len; 680 - } 681 - 682 - static inline bool end_of_str(char c) 683 - { 684 - return c == '\0' || c == '\n'; 685 - } 686 - 687 - static inline bool __end_of_region(char c) 688 - { 689 - return isspace(c) || c == ','; 690 - } 691 - 692 - static inline bool end_of_region(char c) 693 - { 694 - return __end_of_region(c) || end_of_str(c); 695 - } 696 - 697 - /* 698 - * The format allows commas and whitespaces at the beginning 699 - * of the region. 700 - */ 701 - static const char *bitmap_find_region(const char *str) 702 - { 703 - while (__end_of_region(*str)) 704 - str++; 705 - 706 - return end_of_str(*str) ? NULL : str; 707 - } 708 - 709 - static const char *bitmap_find_region_reverse(const char *start, const char *end) 710 - { 711 - while (start <= end && __end_of_region(*end)) 712 - end--; 713 - 714 - return end; 715 - } 716 - 717 - static const char *bitmap_parse_region(const char *str, struct region *r) 718 - { 719 - unsigned int lastbit = r->nbits - 1; 720 - 721 - if (!strncasecmp(str, "all", 3)) { 722 - r->start = 0; 723 - r->end = lastbit; 724 - str += 3; 725 - 726 - goto check_pattern; 727 - } 728 - 729 - str = bitmap_getnum(str, &r->start, lastbit); 730 - if (IS_ERR(str)) 731 - return str; 732 - 733 - if (end_of_region(*str)) 734 - goto no_end; 735 - 736 - if (*str != '-') 737 - return ERR_PTR(-EINVAL); 738 - 739 - str = bitmap_getnum(str + 1, &r->end, lastbit); 740 - if (IS_ERR(str)) 741 - return str; 742 - 743 - check_pattern: 744 - if (end_of_region(*str)) 745 - goto no_pattern; 746 - 747 - if (*str != ':') 748 - return ERR_PTR(-EINVAL); 749 - 750 - str = bitmap_getnum(str + 1, &r->off, lastbit); 751 - if (IS_ERR(str)) 752 - return str; 753 - 754 - if (*str != '/') 755 - return ERR_PTR(-EINVAL); 756 - 757 - return bitmap_getnum(str + 1, &r->group_len, lastbit); 758 - 759 - no_end: 760 - r->end = r->start; 761 - no_pattern: 762 - r->off = r->end + 1; 763 - r->group_len = r->end + 1; 764 - 765 - return end_of_str(*str) ? NULL : str; 766 - } 767 - 768 - /** 769 - * bitmap_parselist - convert list format ASCII string to bitmap 770 - * @buf: read user string from this buffer; must be terminated 771 - * with a \0 or \n. 772 - * @maskp: write resulting mask here 773 - * @nmaskbits: number of bits in mask to be written 774 - * 775 - * Input format is a comma-separated list of decimal numbers and 776 - * ranges. Consecutively set bits are shown as two hyphen-separated 777 - * decimal numbers, the smallest and largest bit numbers set in 778 - * the range. 779 - * Optionally each range can be postfixed to denote that only parts of it 780 - * should be set. The range will divided to groups of specific size. 781 - * From each group will be used only defined amount of bits. 782 - * Syntax: range:used_size/group_size 783 - * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769 784 - * The value 'N' can be used as a dynamically substituted token for the 785 - * maximum allowed value; i.e (nmaskbits - 1). Keep in mind that it is 786 - * dynamic, so if system changes cause the bitmap width to change, such 787 - * as more cores in a CPU list, then any ranges using N will also change. 788 - * 789 - * Returns: 0 on success, -errno on invalid input strings. Error values: 790 - * 791 - * - ``-EINVAL``: wrong region format 792 - * - ``-EINVAL``: invalid character in string 793 - * - ``-ERANGE``: bit number specified too large for mask 794 - * - ``-EOVERFLOW``: integer overflow in the input parameters 795 - */ 796 - int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits) 797 - { 798 - struct region r; 799 - long ret; 800 - 801 - r.nbits = nmaskbits; 802 - bitmap_zero(maskp, r.nbits); 803 - 804 - while (buf) { 805 - buf = bitmap_find_region(buf); 806 - if (buf == NULL) 807 - return 0; 808 - 809 - buf = bitmap_parse_region(buf, &r); 810 - if (IS_ERR(buf)) 811 - return PTR_ERR(buf); 812 - 813 - ret = bitmap_check_region(&r); 814 - if (ret) 815 - return ret; 816 - 817 - bitmap_set_region(&r, maskp); 818 - } 819 - 820 - return 0; 821 - } 822 - EXPORT_SYMBOL(bitmap_parselist); 823 - 824 - 825 - /** 826 - * bitmap_parselist_user() - convert user buffer's list format ASCII 827 - * string to bitmap 828 - * 829 - * @ubuf: pointer to user buffer containing string. 830 - * @ulen: buffer size in bytes. If string is smaller than this 831 - * then it must be terminated with a \0. 832 - * @maskp: pointer to bitmap array that will contain result. 833 - * @nmaskbits: size of bitmap, in bits. 834 - * 835 - * Wrapper for bitmap_parselist(), providing it with user buffer. 836 - */ 837 - int bitmap_parselist_user(const char __user *ubuf, 838 - unsigned int ulen, unsigned long *maskp, 839 - int nmaskbits) 840 - { 841 - char *buf; 842 - int ret; 843 - 844 - buf = memdup_user_nul(ubuf, ulen); 845 - if (IS_ERR(buf)) 846 - return PTR_ERR(buf); 847 - 848 - ret = bitmap_parselist(buf, maskp, nmaskbits); 849 - 850 - kfree(buf); 851 - return ret; 852 - } 853 - EXPORT_SYMBOL(bitmap_parselist_user); 854 - 855 - static const char *bitmap_get_x32_reverse(const char *start, 856 - const char *end, u32 *num) 857 - { 858 - u32 ret = 0; 859 - int c, i; 860 - 861 - for (i = 0; i < 32; i += 4) { 862 - c = hex_to_bin(*end--); 863 - if (c < 0) 864 - return ERR_PTR(-EINVAL); 865 - 866 - ret |= c << i; 867 - 868 - if (start > end || __end_of_region(*end)) 869 - goto out; 870 - } 871 - 872 - if (hex_to_bin(*end--) >= 0) 873 - return ERR_PTR(-EOVERFLOW); 874 - out: 875 - *num = ret; 876 - return end; 877 - } 878 - 879 - /** 880 - * bitmap_parse - convert an ASCII hex string into a bitmap. 881 - * @start: pointer to buffer containing string. 882 - * @buflen: buffer size in bytes. If string is smaller than this 883 - * then it must be terminated with a \0 or \n. In that case, 884 - * UINT_MAX may be provided instead of string length. 885 - * @maskp: pointer to bitmap array that will contain result. 886 - * @nmaskbits: size of bitmap, in bits. 887 - * 888 - * Commas group hex digits into chunks. Each chunk defines exactly 32 889 - * bits of the resultant bitmask. No chunk may specify a value larger 890 - * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value 891 - * then leading 0-bits are prepended. %-EINVAL is returned for illegal 892 - * characters. Grouping such as "1,,5", ",44", "," or "" is allowed. 893 - * Leading, embedded and trailing whitespace accepted. 894 - */ 895 - int bitmap_parse(const char *start, unsigned int buflen, 896 - unsigned long *maskp, int nmaskbits) 897 - { 898 - const char *end = strnchrnul(start, buflen, '\n') - 1; 899 - int chunks = BITS_TO_U32(nmaskbits); 900 - u32 *bitmap = (u32 *)maskp; 901 - int unset_bit; 902 - int chunk; 903 - 904 - for (chunk = 0; ; chunk++) { 905 - end = bitmap_find_region_reverse(start, end); 906 - if (start > end) 907 - break; 908 - 909 - if (!chunks--) 910 - return -EOVERFLOW; 911 - 912 - #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN) 913 - end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]); 914 - #else 915 - end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]); 916 - #endif 917 - if (IS_ERR(end)) 918 - return PTR_ERR(end); 919 - } 920 - 921 - unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32; 922 - if (unset_bit < nmaskbits) { 923 - bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit); 924 - return 0; 925 - } 926 - 927 - if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit) 928 - return -EOVERFLOW; 929 - 930 - return 0; 931 - } 932 - EXPORT_SYMBOL(bitmap_parse); 933 - 934 443 /** 935 444 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap 936 445 * @buf: pointer to a bitmap ··· 475 988 * to @dst. 476 989 * 477 990 * The positions of unset bits in @old are mapped to themselves 478 - * (the identify map). 991 + * (the identity map). 479 992 * 480 993 * Apply the above specified mapping to @src, placing the result in 481 994 * @dst, clearing any bits previously set in @dst. ··· 524 1037 * the position of the m-th set bit in @new, where m == n % w. 525 1038 * 526 1039 * The positions of unset bits in @old are mapped to themselves 527 - * (the identify map). 1040 + * (the identity map). 528 1041 * 529 1042 * Apply the above specified mapping to bit position @oldbit, returning 530 1043 * the new bit position. ··· 706 1219 set_bit(oldbit % sz, dst); 707 1220 } 708 1221 #endif /* CONFIG_NUMA */ 709 - 710 - /* 711 - * Common code for bitmap_*_region() routines. 712 - * bitmap: array of unsigned longs corresponding to the bitmap 713 - * pos: the beginning of the region 714 - * order: region size (log base 2 of number of bits) 715 - * reg_op: operation(s) to perform on that region of bitmap 716 - * 717 - * Can set, verify and/or release a region of bits in a bitmap, 718 - * depending on which combination of REG_OP_* flag bits is set. 719 - * 720 - * A region of a bitmap is a sequence of bits in the bitmap, of 721 - * some size '1 << order' (a power of two), aligned to that same 722 - * '1 << order' power of two. 723 - * 724 - * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits). 725 - * Returns 0 in all other cases and reg_ops. 726 - */ 727 - 728 - enum { 729 - REG_OP_ISFREE, /* true if region is all zero bits */ 730 - REG_OP_ALLOC, /* set all bits in region */ 731 - REG_OP_RELEASE, /* clear all bits in region */ 732 - }; 733 - 734 - static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op) 735 - { 736 - int nbits_reg; /* number of bits in region */ 737 - int index; /* index first long of region in bitmap */ 738 - int offset; /* bit offset region in bitmap[index] */ 739 - int nlongs_reg; /* num longs spanned by region in bitmap */ 740 - int nbitsinlong; /* num bits of region in each spanned long */ 741 - unsigned long mask; /* bitmask for one long of region */ 742 - int i; /* scans bitmap by longs */ 743 - int ret = 0; /* return value */ 744 - 745 - /* 746 - * Either nlongs_reg == 1 (for small orders that fit in one long) 747 - * or (offset == 0 && mask == ~0UL) (for larger multiword orders.) 748 - */ 749 - nbits_reg = 1 << order; 750 - index = pos / BITS_PER_LONG; 751 - offset = pos - (index * BITS_PER_LONG); 752 - nlongs_reg = BITS_TO_LONGS(nbits_reg); 753 - nbitsinlong = min(nbits_reg, BITS_PER_LONG); 754 - 755 - /* 756 - * Can't do "mask = (1UL << nbitsinlong) - 1", as that 757 - * overflows if nbitsinlong == BITS_PER_LONG. 758 - */ 759 - mask = (1UL << (nbitsinlong - 1)); 760 - mask += mask - 1; 761 - mask <<= offset; 762 - 763 - switch (reg_op) { 764 - case REG_OP_ISFREE: 765 - for (i = 0; i < nlongs_reg; i++) { 766 - if (bitmap[index + i] & mask) 767 - goto done; 768 - } 769 - ret = 1; /* all bits in region free (zero) */ 770 - break; 771 - 772 - case REG_OP_ALLOC: 773 - for (i = 0; i < nlongs_reg; i++) 774 - bitmap[index + i] |= mask; 775 - break; 776 - 777 - case REG_OP_RELEASE: 778 - for (i = 0; i < nlongs_reg; i++) 779 - bitmap[index + i] &= ~mask; 780 - break; 781 - } 782 - done: 783 - return ret; 784 - } 785 - 786 - /** 787 - * bitmap_find_free_region - find a contiguous aligned mem region 788 - * @bitmap: array of unsigned longs corresponding to the bitmap 789 - * @bits: number of bits in the bitmap 790 - * @order: region size (log base 2 of number of bits) to find 791 - * 792 - * Find a region of free (zero) bits in a @bitmap of @bits bits and 793 - * allocate them (set them to one). Only consider regions of length 794 - * a power (@order) of two, aligned to that power of two, which 795 - * makes the search algorithm much faster. 796 - * 797 - * Return the bit offset in bitmap of the allocated region, 798 - * or -errno on failure. 799 - */ 800 - int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order) 801 - { 802 - unsigned int pos, end; /* scans bitmap by regions of size order */ 803 - 804 - for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) { 805 - if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) 806 - continue; 807 - __reg_op(bitmap, pos, order, REG_OP_ALLOC); 808 - return pos; 809 - } 810 - return -ENOMEM; 811 - } 812 - EXPORT_SYMBOL(bitmap_find_free_region); 813 - 814 - /** 815 - * bitmap_release_region - release allocated bitmap region 816 - * @bitmap: array of unsigned longs corresponding to the bitmap 817 - * @pos: beginning of bit region to release 818 - * @order: region size (log base 2 of number of bits) to release 819 - * 820 - * This is the complement to __bitmap_find_free_region() and releases 821 - * the found region (by clearing it in the bitmap). 822 - * 823 - * No return value. 824 - */ 825 - void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order) 826 - { 827 - __reg_op(bitmap, pos, order, REG_OP_RELEASE); 828 - } 829 - EXPORT_SYMBOL(bitmap_release_region); 830 - 831 - /** 832 - * bitmap_allocate_region - allocate bitmap region 833 - * @bitmap: array of unsigned longs corresponding to the bitmap 834 - * @pos: beginning of bit region to allocate 835 - * @order: region size (log base 2 of number of bits) to allocate 836 - * 837 - * Allocate (set bits in) a specified region of a bitmap. 838 - * 839 - * Return 0 on success, or %-EBUSY if specified region wasn't 840 - * free (not all bits were zero). 841 - */ 842 - int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order) 843 - { 844 - if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) 845 - return -EBUSY; 846 - return __reg_op(bitmap, pos, order, REG_OP_ALLOC); 847 - } 848 - EXPORT_SYMBOL(bitmap_allocate_region); 849 - 850 - /** 851 - * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order. 852 - * @dst: destination buffer 853 - * @src: bitmap to copy 854 - * @nbits: number of bits in the bitmap 855 - * 856 - * Require nbits % BITS_PER_LONG == 0. 857 - */ 858 - #ifdef __BIG_ENDIAN 859 - void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits) 860 - { 861 - unsigned int i; 862 - 863 - for (i = 0; i < nbits/BITS_PER_LONG; i++) { 864 - if (BITS_PER_LONG == 64) 865 - dst[i] = cpu_to_le64(src[i]); 866 - else 867 - dst[i] = cpu_to_le32(src[i]); 868 - } 869 - } 870 - EXPORT_SYMBOL(bitmap_copy_le); 871 - #endif 872 1222 873 1223 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags) 874 1224 {
+12 -5
lib/cpumask.c
··· 14 14 * @start: the start point of the iteration 15 15 * @wrap: assume @n crossing @start terminates the iteration 16 16 * 17 - * Returns >= nr_cpu_ids on completion 17 + * Return: >= nr_cpu_ids on completion 18 18 * 19 19 * Note: the @wrap argument is required for the start condition when 20 20 * we cannot assume @start is set in @mask. ··· 48 48 * @node: memory node from which to allocate or %NUMA_NO_NODE 49 49 * 50 50 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 51 - * a nop returning a constant 1 (in <linux/cpumask.h>) 52 - * Returns TRUE if memory allocation succeeded, FALSE otherwise. 51 + * a nop returning a constant 1 (in <linux/cpumask.h>). 52 + * 53 + * Return: TRUE if memory allocation succeeded, FALSE otherwise. 53 54 * 54 55 * In addition, mask will be NULL if this fails. Note that gcc is 55 56 * usually smart enough to know that mask can never be NULL if ··· 116 115 * @i: index number 117 116 * @node: local numa_node 118 117 * 119 - * Returns online CPU according to a numa aware policy; local cpus are returned 118 + * Return: online CPU according to a numa aware policy; local cpus are returned 120 119 * first, followed by non-local ones, then it wraps around. 121 120 * 122 121 * For those who wants to enumerate all CPUs based on their NUMA distances, ··· 164 163 * Iterated calls using the same srcp1 and srcp2 will be distributed within 165 164 * their intersection. 166 165 * 167 - * Returns >= nr_cpu_ids if the intersection is empty. 166 + * Return: >= nr_cpu_ids if the intersection is empty. 168 167 */ 169 168 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p, 170 169 const struct cpumask *src2p) ··· 183 182 } 184 183 EXPORT_SYMBOL(cpumask_any_and_distribute); 185 184 185 + /** 186 + * cpumask_any_distribute - Return an arbitrary cpu from srcp 187 + * @srcp: &cpumask for selection 188 + * 189 + * Return: >= nr_cpu_ids if the intersection is empty. 190 + */ 186 191 unsigned int cpumask_any_distribute(const struct cpumask *srcp) 187 192 { 188 193 unsigned int next, prev;
+24
lib/test_bitmap.c
··· 330 330 expect_eq_pbl("0-108,128-1023", bmap2, 1024); 331 331 } 332 332 333 + static void __init test_bitmap_region(void) 334 + { 335 + int pos, order; 336 + 337 + DECLARE_BITMAP(bmap, 1000); 338 + 339 + bitmap_zero(bmap, 1000); 340 + 341 + for (order = 0; order < 10; order++) { 342 + pos = bitmap_find_free_region(bmap, 1000, order); 343 + if (order == 0) 344 + expect_eq_uint(pos, 0); 345 + else 346 + expect_eq_uint(pos, order < 9 ? BIT(order) : -ENOMEM); 347 + } 348 + 349 + bitmap_release_region(bmap, 0, 0); 350 + for (order = 1; order < 9; order++) 351 + bitmap_release_region(bmap, BIT(order), order); 352 + 353 + expect_eq_uint(bitmap_weight(bmap, 1000), 0); 354 + } 355 + 333 356 #define EXP2_IN_BITS (sizeof(exp2) * 8) 334 357 335 358 static void __init test_replace(void) ··· 1250 1227 test_zero_clear(); 1251 1228 test_fill_set(); 1252 1229 test_copy(); 1230 + test_bitmap_region(); 1253 1231 test_replace(); 1254 1232 test_bitmap_arr32(); 1255 1233 test_bitmap_arr64();