1
2
3
4
5
6#include <linux/export.h>
7#include <linux/thread_info.h>
8#include <linux/ctype.h>
9#include <linux/errno.h>
10#include <linux/bitmap.h>
11#include <linux/bitops.h>
12#include <linux/bug.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
16#include <linux/string.h>
17#include <linux/uaccess.h>
18
19#include <asm/page.h>
20
21#include "kstrtox.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46int __bitmap_equal(const unsigned long *bitmap1,
47 const unsigned long *bitmap2, unsigned int bits)
48{
49 unsigned int k, lim = bits/BITS_PER_LONG;
50 for (k = 0; k < lim; ++k)
51 if (bitmap1[k] != bitmap2[k])
52 return 0;
53
54 if (bits % BITS_PER_LONG)
55 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
56 return 0;
57
58 return 1;
59}
60EXPORT_SYMBOL(__bitmap_equal);
61
62bool __bitmap_or_equal(const unsigned long *bitmap1,
63 const unsigned long *bitmap2,
64 const unsigned long *bitmap3,
65 unsigned int bits)
66{
67 unsigned int k, lim = bits / BITS_PER_LONG;
68 unsigned long tmp;
69
70 for (k = 0; k < lim; ++k) {
71 if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
72 return false;
73 }
74
75 if (!(bits % BITS_PER_LONG))
76 return true;
77
78 tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
79 return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
80}
81
82void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
83{
84 unsigned int k, lim = BITS_TO_LONGS(bits);
85 for (k = 0; k < lim; ++k)
86 dst[k] = ~src[k];
87}
88EXPORT_SYMBOL(__bitmap_complement);
89
90
91
92
93
94
95
96
97
98
99
100
101void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
102 unsigned shift, unsigned nbits)
103{
104 unsigned k, lim = BITS_TO_LONGS(nbits);
105 unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
106 unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
107 for (k = 0; off + k < lim; ++k) {
108 unsigned long upper, lower;
109
110
111
112
113
114 if (!rem || off + k + 1 >= lim)
115 upper = 0;
116 else {
117 upper = src[off + k + 1];
118 if (off + k + 1 == lim - 1)
119 upper &= mask;
120 upper <<= (BITS_PER_LONG - rem);
121 }
122 lower = src[off + k];
123 if (off + k == lim - 1)
124 lower &= mask;
125 lower >>= rem;
126 dst[k] = lower | upper;
127 }
128 if (off)
129 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
130}
131EXPORT_SYMBOL(__bitmap_shift_right);
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
147 unsigned int shift, unsigned int nbits)
148{
149 int k;
150 unsigned int lim = BITS_TO_LONGS(nbits);
151 unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
152 for (k = lim - off - 1; k >= 0; --k) {
153 unsigned long upper, lower;
154
155
156
157
158
159 if (rem && k > 0)
160 lower = src[k - 1] >> (BITS_PER_LONG - rem);
161 else
162 lower = 0;
163 upper = src[k] << rem;
164 dst[k + off] = lower | upper;
165 }
166 if (off)
167 memset(dst, 0, off*sizeof(unsigned long));
168}
169EXPORT_SYMBOL(__bitmap_shift_left);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208void bitmap_cut(unsigned long *dst, const unsigned long *src,
209 unsigned int first, unsigned int cut, unsigned int nbits)
210{
211 unsigned int len = BITS_TO_LONGS(nbits);
212 unsigned long keep = 0, carry;
213 int i;
214
215 if (first % BITS_PER_LONG) {
216 keep = src[first / BITS_PER_LONG] &
217 (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
218 }
219
220 memmove(dst, src, len * sizeof(*dst));
221
222 while (cut--) {
223 for (i = first / BITS_PER_LONG; i < len; i++) {
224 if (i < len - 1)
225 carry = dst[i + 1] & 1UL;
226 else
227 carry = 0;
228
229 dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));
230 }
231 }
232
233 dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);
234 dst[first / BITS_PER_LONG] |= keep;
235}
236EXPORT_SYMBOL(bitmap_cut);
237
238int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
239 const unsigned long *bitmap2, unsigned int bits)
240{
241 unsigned int k;
242 unsigned int lim = bits/BITS_PER_LONG;
243 unsigned long result = 0;
244
245 for (k = 0; k < lim; k++)
246 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
247 if (bits % BITS_PER_LONG)
248 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
249 BITMAP_LAST_WORD_MASK(bits));
250 return result != 0;
251}
252EXPORT_SYMBOL(__bitmap_and);
253
254void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
255 const unsigned long *bitmap2, unsigned int bits)
256{
257 unsigned int k;
258 unsigned int nr = BITS_TO_LONGS(bits);
259
260 for (k = 0; k < nr; k++)
261 dst[k] = bitmap1[k] | bitmap2[k];
262}
263EXPORT_SYMBOL(__bitmap_or);
264
265void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
266 const unsigned long *bitmap2, unsigned int bits)
267{
268 unsigned int k;
269 unsigned int nr = BITS_TO_LONGS(bits);
270
271 for (k = 0; k < nr; k++)
272 dst[k] = bitmap1[k] ^ bitmap2[k];
273}
274EXPORT_SYMBOL(__bitmap_xor);
275
276int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
277 const unsigned long *bitmap2, unsigned int bits)
278{
279 unsigned int k;
280 unsigned int lim = bits/BITS_PER_LONG;
281 unsigned long result = 0;
282
283 for (k = 0; k < lim; k++)
284 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
285 if (bits % BITS_PER_LONG)
286 result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
287 BITMAP_LAST_WORD_MASK(bits));
288 return result != 0;
289}
290EXPORT_SYMBOL(__bitmap_andnot);
291
292void __bitmap_replace(unsigned long *dst,
293 const unsigned long *old, const unsigned long *new,
294 const unsigned long *mask, unsigned int nbits)
295{
296 unsigned int k;
297 unsigned int nr = BITS_TO_LONGS(nbits);
298
299 for (k = 0; k < nr; k++)
300 dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);
301}
302EXPORT_SYMBOL(__bitmap_replace);
303
304int __bitmap_intersects(const unsigned long *bitmap1,
305 const unsigned long *bitmap2, unsigned int bits)
306{
307 unsigned int k, lim = bits/BITS_PER_LONG;
308 for (k = 0; k < lim; ++k)
309 if (bitmap1[k] & bitmap2[k])
310 return 1;
311
312 if (bits % BITS_PER_LONG)
313 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
314 return 1;
315 return 0;
316}
317EXPORT_SYMBOL(__bitmap_intersects);
318
319int __bitmap_subset(const unsigned long *bitmap1,
320 const unsigned long *bitmap2, unsigned int bits)
321{
322 unsigned int k, lim = bits/BITS_PER_LONG;
323 for (k = 0; k < lim; ++k)
324 if (bitmap1[k] & ~bitmap2[k])
325 return 0;
326
327 if (bits % BITS_PER_LONG)
328 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
329 return 0;
330 return 1;
331}
332EXPORT_SYMBOL(__bitmap_subset);
333
334int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
335{
336 unsigned int k, lim = bits/BITS_PER_LONG;
337 int w = 0;
338
339 for (k = 0; k < lim; k++)
340 w += hweight_long(bitmap[k]);
341
342 if (bits % BITS_PER_LONG)
343 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
344
345 return w;
346}
347EXPORT_SYMBOL(__bitmap_weight);
348
349void __bitmap_set(unsigned long *map, unsigned int start, int len)
350{
351 unsigned long *p = map + BIT_WORD(start);
352 const unsigned int size = start + len;
353 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
354 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
355
356 while (len - bits_to_set >= 0) {
357 *p |= mask_to_set;
358 len -= bits_to_set;
359 bits_to_set = BITS_PER_LONG;
360 mask_to_set = ~0UL;
361 p++;
362 }
363 if (len) {
364 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
365 *p |= mask_to_set;
366 }
367}
368EXPORT_SYMBOL(__bitmap_set);
369
370void __bitmap_clear(unsigned long *map, unsigned int start, int len)
371{
372 unsigned long *p = map + BIT_WORD(start);
373 const unsigned int size = start + len;
374 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
375 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
376
377 while (len - bits_to_clear >= 0) {
378 *p &= ~mask_to_clear;
379 len -= bits_to_clear;
380 bits_to_clear = BITS_PER_LONG;
381 mask_to_clear = ~0UL;
382 p++;
383 }
384 if (len) {
385 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
386 *p &= ~mask_to_clear;
387 }
388}
389EXPORT_SYMBOL(__bitmap_clear);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
405 unsigned long size,
406 unsigned long start,
407 unsigned int nr,
408 unsigned long align_mask,
409 unsigned long align_offset)
410{
411 unsigned long index, end, i;
412again:
413 index = find_next_zero_bit(map, size, start);
414
415
416 index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
417
418 end = index + nr;
419 if (end > size)
420 return end;
421 i = find_next_bit(map, end, index);
422 if (i < end) {
423 start = i + 1;
424 goto again;
425 }
426 return index;
427}
428EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444int bitmap_parse_user(const char __user *ubuf,
445 unsigned int ulen, unsigned long *maskp,
446 int nmaskbits)
447{
448 char *buf;
449 int ret;
450
451 buf = memdup_user_nul(ubuf, ulen);
452 if (IS_ERR(buf))
453 return PTR_ERR(buf);
454
455 ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits);
456
457 kfree(buf);
458 return ret;
459}
460EXPORT_SYMBOL(bitmap_parse_user);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
479 int nmaskbits)
480{
481 ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
482
483 return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
484 scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
485}
486EXPORT_SYMBOL(bitmap_print_to_pagebuf);
487
488
489
490
491
492
493
494
495struct region {
496 unsigned int start;
497 unsigned int off;
498 unsigned int group_len;
499 unsigned int end;
500};
501
502static int bitmap_set_region(const struct region *r,
503 unsigned long *bitmap, int nbits)
504{
505 unsigned int start;
506
507 if (r->end >= nbits)
508 return -ERANGE;
509
510 for (start = r->start; start <= r->end; start += r->group_len)
511 bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
512
513 return 0;
514}
515
516static int bitmap_check_region(const struct region *r)
517{
518 if (r->start > r->end || r->group_len == 0 || r->off > r->group_len)
519 return -EINVAL;
520
521 return 0;
522}
523
524static const char *bitmap_getnum(const char *str, unsigned int *num)
525{
526 unsigned long long n;
527 unsigned int len;
528
529 len = _parse_integer(str, 10, &n);
530 if (!len)
531 return ERR_PTR(-EINVAL);
532 if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n)
533 return ERR_PTR(-EOVERFLOW);
534
535 *num = n;
536 return str + len;
537}
538
539static inline bool end_of_str(char c)
540{
541 return c == '\0' || c == '\n';
542}
543
544static inline bool __end_of_region(char c)
545{
546 return isspace(c) || c == ',';
547}
548
549static inline bool end_of_region(char c)
550{
551 return __end_of_region(c) || end_of_str(c);
552}
553
554
555
556
557
558static const char *bitmap_find_region(const char *str)
559{
560 while (__end_of_region(*str))
561 str++;
562
563 return end_of_str(*str) ? NULL : str;
564}
565
566static const char *bitmap_find_region_reverse(const char *start, const char *end)
567{
568 while (start <= end && __end_of_region(*end))
569 end--;
570
571 return end;
572}
573
574static const char *bitmap_parse_region(const char *str, struct region *r)
575{
576 str = bitmap_getnum(str, &r->start);
577 if (IS_ERR(str))
578 return str;
579
580 if (end_of_region(*str))
581 goto no_end;
582
583 if (*str != '-')
584 return ERR_PTR(-EINVAL);
585
586 str = bitmap_getnum(str + 1, &r->end);
587 if (IS_ERR(str))
588 return str;
589
590 if (end_of_region(*str))
591 goto no_pattern;
592
593 if (*str != ':')
594 return ERR_PTR(-EINVAL);
595
596 str = bitmap_getnum(str + 1, &r->off);
597 if (IS_ERR(str))
598 return str;
599
600 if (*str != '/')
601 return ERR_PTR(-EINVAL);
602
603 return bitmap_getnum(str + 1, &r->group_len);
604
605no_end:
606 r->end = r->start;
607no_pattern:
608 r->off = r->end + 1;
609 r->group_len = r->end + 1;
610
611 return end_of_str(*str) ? NULL : str;
612}
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits)
639{
640 struct region r;
641 long ret;
642
643 bitmap_zero(maskp, nmaskbits);
644
645 while (buf) {
646 buf = bitmap_find_region(buf);
647 if (buf == NULL)
648 return 0;
649
650 buf = bitmap_parse_region(buf, &r);
651 if (IS_ERR(buf))
652 return PTR_ERR(buf);
653
654 ret = bitmap_check_region(&r);
655 if (ret)
656 return ret;
657
658 ret = bitmap_set_region(&r, maskp, nmaskbits);
659 if (ret)
660 return ret;
661 }
662
663 return 0;
664}
665EXPORT_SYMBOL(bitmap_parselist);
666
667
668
669
670
671
672
673
674
675
676
677
678
679int bitmap_parselist_user(const char __user *ubuf,
680 unsigned int ulen, unsigned long *maskp,
681 int nmaskbits)
682{
683 char *buf;
684 int ret;
685
686 buf = memdup_user_nul(ubuf, ulen);
687 if (IS_ERR(buf))
688 return PTR_ERR(buf);
689
690 ret = bitmap_parselist(buf, maskp, nmaskbits);
691
692 kfree(buf);
693 return ret;
694}
695EXPORT_SYMBOL(bitmap_parselist_user);
696
697static const char *bitmap_get_x32_reverse(const char *start,
698 const char *end, u32 *num)
699{
700 u32 ret = 0;
701 int c, i;
702
703 for (i = 0; i < 32; i += 4) {
704 c = hex_to_bin(*end--);
705 if (c < 0)
706 return ERR_PTR(-EINVAL);
707
708 ret |= c << i;
709
710 if (start > end || __end_of_region(*end))
711 goto out;
712 }
713
714 if (hex_to_bin(*end--) >= 0)
715 return ERR_PTR(-EOVERFLOW);
716out:
717 *num = ret;
718 return end;
719}
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737int bitmap_parse(const char *start, unsigned int buflen,
738 unsigned long *maskp, int nmaskbits)
739{
740 const char *end = strnchrnul(start, buflen, '\n') - 1;
741 int chunks = BITS_TO_U32(nmaskbits);
742 u32 *bitmap = (u32 *)maskp;
743 int unset_bit;
744 int chunk;
745
746 for (chunk = 0; ; chunk++) {
747 end = bitmap_find_region_reverse(start, end);
748 if (start > end)
749 break;
750
751 if (!chunks--)
752 return -EOVERFLOW;
753
754#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
755 end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]);
756#else
757 end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]);
758#endif
759 if (IS_ERR(end))
760 return PTR_ERR(end);
761 }
762
763 unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32;
764 if (unset_bit < nmaskbits) {
765 bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit);
766 return 0;
767 }
768
769 if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit)
770 return -EOVERFLOW;
771
772 return 0;
773}
774EXPORT_SYMBOL(bitmap_parse);
775
776
777#ifdef CONFIG_NUMA
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
797{
798 if (pos >= nbits || !test_bit(pos, buf))
799 return -1;
800
801 return __bitmap_weight(buf, pos);
802}
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
823{
824 unsigned int pos;
825
826 for (pos = find_first_bit(buf, nbits);
827 pos < nbits && ord;
828 pos = find_next_bit(buf, nbits, pos + 1))
829 ord--;
830
831 return pos;
832}
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866void bitmap_remap(unsigned long *dst, const unsigned long *src,
867 const unsigned long *old, const unsigned long *new,
868 unsigned int nbits)
869{
870 unsigned int oldbit, w;
871
872 if (dst == src)
873 return;
874 bitmap_zero(dst, nbits);
875
876 w = bitmap_weight(new, nbits);
877 for_each_set_bit(oldbit, src, nbits) {
878 int n = bitmap_pos_to_ord(old, oldbit, nbits);
879
880 if (n < 0 || w == 0)
881 set_bit(oldbit, dst);
882 else
883 set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
884 }
885}
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913int bitmap_bitremap(int oldbit, const unsigned long *old,
914 const unsigned long *new, int bits)
915{
916 int w = bitmap_weight(new, bits);
917 int n = bitmap_pos_to_ord(old, oldbit, bits);
918 if (n < 0 || w == 0)
919 return oldbit;
920 else
921 return bitmap_ord_to_pos(new, n % w, bits);
922}
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030void bitmap_onto(unsigned long *dst, const unsigned long *orig,
1031 const unsigned long *relmap, unsigned int bits)
1032{
1033 unsigned int n, m;
1034
1035 if (dst == orig)
1036 return;
1037 bitmap_zero(dst, bits);
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049 m = 0;
1050 for_each_set_bit(n, relmap, bits) {
1051
1052 if (test_bit(m, orig))
1053 set_bit(n, dst);
1054 m++;
1055 }
1056}
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069void bitmap_fold(unsigned long *dst, const unsigned long *orig,
1070 unsigned int sz, unsigned int nbits)
1071{
1072 unsigned int oldbit;
1073
1074 if (dst == orig)
1075 return;
1076 bitmap_zero(dst, nbits);
1077
1078 for_each_set_bit(oldbit, orig, nbits)
1079 set_bit(oldbit % sz, dst);
1080}
1081#endif
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101enum {
1102 REG_OP_ISFREE,
1103 REG_OP_ALLOC,
1104 REG_OP_RELEASE,
1105};
1106
1107static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
1108{
1109 int nbits_reg;
1110 int index;
1111 int offset;
1112 int nlongs_reg;
1113 int nbitsinlong;
1114 unsigned long mask;
1115 int i;
1116 int ret = 0;
1117
1118
1119
1120
1121
1122 nbits_reg = 1 << order;
1123 index = pos / BITS_PER_LONG;
1124 offset = pos - (index * BITS_PER_LONG);
1125 nlongs_reg = BITS_TO_LONGS(nbits_reg);
1126 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
1127
1128
1129
1130
1131
1132 mask = (1UL << (nbitsinlong - 1));
1133 mask += mask - 1;
1134 mask <<= offset;
1135
1136 switch (reg_op) {
1137 case REG_OP_ISFREE:
1138 for (i = 0; i < nlongs_reg; i++) {
1139 if (bitmap[index + i] & mask)
1140 goto done;
1141 }
1142 ret = 1;
1143 break;
1144
1145 case REG_OP_ALLOC:
1146 for (i = 0; i < nlongs_reg; i++)
1147 bitmap[index + i] |= mask;
1148 break;
1149
1150 case REG_OP_RELEASE:
1151 for (i = 0; i < nlongs_reg; i++)
1152 bitmap[index + i] &= ~mask;
1153 break;
1154 }
1155done:
1156 return ret;
1157}
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
1174{
1175 unsigned int pos, end;
1176
1177 for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
1178 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1179 continue;
1180 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1181 return pos;
1182 }
1183 return -ENOMEM;
1184}
1185EXPORT_SYMBOL(bitmap_find_free_region);
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
1199{
1200 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
1201}
1202EXPORT_SYMBOL(bitmap_release_region);
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
1216{
1217 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1218 return -EBUSY;
1219 return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1220}
1221EXPORT_SYMBOL(bitmap_allocate_region);
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231#ifdef __BIG_ENDIAN
1232void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
1233{
1234 unsigned int i;
1235
1236 for (i = 0; i < nbits/BITS_PER_LONG; i++) {
1237 if (BITS_PER_LONG == 64)
1238 dst[i] = cpu_to_le64(src[i]);
1239 else
1240 dst[i] = cpu_to_le32(src[i]);
1241 }
1242}
1243EXPORT_SYMBOL(bitmap_copy_le);
1244#endif
1245
1246unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
1247{
1248 return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
1249 flags);
1250}
1251EXPORT_SYMBOL(bitmap_alloc);
1252
1253unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
1254{
1255 return bitmap_alloc(nbits, flags | __GFP_ZERO);
1256}
1257EXPORT_SYMBOL(bitmap_zalloc);
1258
1259void bitmap_free(const unsigned long *bitmap)
1260{
1261 kfree(bitmap);
1262}
1263EXPORT_SYMBOL(bitmap_free);
1264
1265#if BITS_PER_LONG == 64
1266
1267
1268
1269
1270
1271
1272void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
1273{
1274 unsigned int i, halfwords;
1275
1276 halfwords = DIV_ROUND_UP(nbits, 32);
1277 for (i = 0; i < halfwords; i++) {
1278 bitmap[i/2] = (unsigned long) buf[i];
1279 if (++i < halfwords)
1280 bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
1281 }
1282
1283
1284 if (nbits % BITS_PER_LONG)
1285 bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
1286}
1287EXPORT_SYMBOL(bitmap_from_arr32);
1288
1289
1290
1291
1292
1293
1294
1295void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
1296{
1297 unsigned int i, halfwords;
1298
1299 halfwords = DIV_ROUND_UP(nbits, 32);
1300 for (i = 0; i < halfwords; i++) {
1301 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
1302 if (++i < halfwords)
1303 buf[i] = (u32) (bitmap[i/2] >> 32);
1304 }
1305
1306
1307 if (nbits % BITS_PER_LONG)
1308 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
1309}
1310EXPORT_SYMBOL(bitmap_to_arr32);
1311
1312#endif
1313