1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * lib/bitmap.c
4 * Helper functions for bitmap.h.
5 */
6
7 #include <linux/bitmap.h>
8 #include <linux/bitops.h>
9 #include <linux/ctype.h>
10 #include <linux/device.h>
11 #include <linux/export.h>
12 #include <linux/slab.h>
13
14 /**
15 * DOC: bitmap introduction
16 *
17 * bitmaps provide an array of bits, implemented using an
18 * array of unsigned longs. The number of valid bits in a
19 * given bitmap does _not_ need to be an exact multiple of
20 * BITS_PER_LONG.
21 *
22 * The possible unused bits in the last, partially used word
23 * of a bitmap are 'don't care'. The implementation makes
24 * no particular effort to keep them zero. It ensures that
25 * their value will not affect the results of any operation.
26 * The bitmap operations that return Boolean (bitmap_empty,
27 * for example) or scalar (bitmap_weight, for example) results
28 * carefully filter out these unused bits from impacting their
29 * results.
30 *
31 * The byte ordering of bitmaps is more natural on little
32 * endian architectures. See the big-endian headers
33 * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
34 * for the best explanations of this ordering.
35 */
36
__bitmap_equal(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)37 bool __bitmap_equal(const unsigned long *bitmap1,
38 const unsigned long *bitmap2, unsigned int bits)
39 {
40 unsigned int k, lim = bits/BITS_PER_LONG;
41 for (k = 0; k < lim; ++k)
42 if (bitmap1[k] != bitmap2[k])
43 return false;
44
45 if (bits % BITS_PER_LONG)
46 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
47 return false;
48
49 return true;
50 }
51 EXPORT_SYMBOL(__bitmap_equal);
52
__bitmap_or_equal(const unsigned long * bitmap1,const unsigned long * bitmap2,const unsigned long * bitmap3,unsigned int bits)53 bool __bitmap_or_equal(const unsigned long *bitmap1,
54 const unsigned long *bitmap2,
55 const unsigned long *bitmap3,
56 unsigned int bits)
57 {
58 unsigned int k, lim = bits / BITS_PER_LONG;
59 unsigned long tmp;
60
61 for (k = 0; k < lim; ++k) {
62 if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
63 return false;
64 }
65
66 if (!(bits % BITS_PER_LONG))
67 return true;
68
69 tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
70 return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
71 }
72 EXPORT_SYMBOL(__bitmap_or_equal);
73
__bitmap_complement(unsigned long * dst,const unsigned long * src,unsigned int bits)74 void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
75 {
76 unsigned int k, lim = BITS_TO_LONGS(bits);
77 for (k = 0; k < lim; ++k)
78 dst[k] = ~src[k];
79 }
80 EXPORT_SYMBOL(__bitmap_complement);
81
82 /**
83 * __bitmap_shift_right - logical right shift of the bits in a bitmap
84 * @dst : destination bitmap
85 * @src : source bitmap
86 * @shift : shift by this many bits
87 * @nbits : bitmap size, in bits
88 *
89 * Shifting right (dividing) means moving bits in the MS -> LS bit
90 * direction. Zeros are fed into the vacated MS positions and the
91 * LS bits shifted off the bottom are lost.
92 */
__bitmap_shift_right(unsigned long * dst,const unsigned long * src,unsigned shift,unsigned nbits)93 void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
94 unsigned shift, unsigned nbits)
95 {
96 unsigned k, lim = BITS_TO_LONGS(nbits);
97 unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
98 unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
99 for (k = 0; off + k < lim; ++k) {
100 unsigned long upper, lower;
101
102 /*
103 * If shift is not word aligned, take lower rem bits of
104 * word above and make them the top rem bits of result.
105 */
106 if (!rem || off + k + 1 >= lim)
107 upper = 0;
108 else {
109 upper = src[off + k + 1];
110 if (off + k + 1 == lim - 1)
111 upper &= mask;
112 upper <<= (BITS_PER_LONG - rem);
113 }
114 lower = src[off + k];
115 if (off + k == lim - 1)
116 lower &= mask;
117 lower >>= rem;
118 dst[k] = lower | upper;
119 }
120 if (off)
121 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
122 }
123 EXPORT_SYMBOL(__bitmap_shift_right);
124
125
126 /**
127 * __bitmap_shift_left - logical left shift of the bits in a bitmap
128 * @dst : destination bitmap
129 * @src : source bitmap
130 * @shift : shift by this many bits
131 * @nbits : bitmap size, in bits
132 *
133 * Shifting left (multiplying) means moving bits in the LS -> MS
134 * direction. Zeros are fed into the vacated LS bit positions
135 * and those MS bits shifted off the top are lost.
136 */
137
__bitmap_shift_left(unsigned long * dst,const unsigned long * src,unsigned int shift,unsigned int nbits)138 void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
139 unsigned int shift, unsigned int nbits)
140 {
141 int k;
142 unsigned int lim = BITS_TO_LONGS(nbits);
143 unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
144 for (k = lim - off - 1; k >= 0; --k) {
145 unsigned long upper, lower;
146
147 /*
148 * If shift is not word aligned, take upper rem bits of
149 * word below and make them the bottom rem bits of result.
150 */
151 if (rem && k > 0)
152 lower = src[k - 1] >> (BITS_PER_LONG - rem);
153 else
154 lower = 0;
155 upper = src[k] << rem;
156 dst[k + off] = lower | upper;
157 }
158 if (off)
159 memset(dst, 0, off*sizeof(unsigned long));
160 }
161 EXPORT_SYMBOL(__bitmap_shift_left);
162
163 /**
164 * bitmap_cut() - remove bit region from bitmap and right shift remaining bits
165 * @dst: destination bitmap, might overlap with src
166 * @src: source bitmap
167 * @first: start bit of region to be removed
168 * @cut: number of bits to remove
169 * @nbits: bitmap size, in bits
170 *
171 * Set the n-th bit of @dst iff the n-th bit of @src is set and
172 * n is less than @first, or the m-th bit of @src is set for any
173 * m such that @first <= n < nbits, and m = n + @cut.
174 *
175 * In pictures, example for a big-endian 32-bit architecture:
176 *
177 * The @src bitmap is::
178 *
179 * 31 63
180 * | |
181 * 10000000 11000001 11110010 00010101 10000000 11000001 01110010 00010101
182 * | | | |
183 * 16 14 0 32
184 *
185 * if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is::
186 *
187 * 31 63
188 * | |
189 * 10110000 00011000 00110010 00010101 00010000 00011000 00101110 01000010
190 * | | |
191 * 14 (bit 17 0 32
192 * from @src)
193 *
194 * Note that @dst and @src might overlap partially or entirely.
195 *
196 * This is implemented in the obvious way, with a shift and carry
197 * step for each moved bit. Optimisation is left as an exercise
198 * for the compiler.
199 */
bitmap_cut(unsigned long * dst,const unsigned long * src,unsigned int first,unsigned int cut,unsigned int nbits)200 void bitmap_cut(unsigned long *dst, const unsigned long *src,
201 unsigned int first, unsigned int cut, unsigned int nbits)
202 {
203 unsigned int len = BITS_TO_LONGS(nbits);
204 unsigned long keep = 0, carry;
205 int i;
206
207 if (first % BITS_PER_LONG) {
208 keep = src[first / BITS_PER_LONG] &
209 (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
210 }
211
212 memmove(dst, src, len * sizeof(*dst));
213
214 while (cut--) {
215 for (i = first / BITS_PER_LONG; i < len; i++) {
216 if (i < len - 1)
217 carry = dst[i + 1] & 1UL;
218 else
219 carry = 0;
220
221 dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));
222 }
223 }
224
225 dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);
226 dst[first / BITS_PER_LONG] |= keep;
227 }
228 EXPORT_SYMBOL(bitmap_cut);
229
__bitmap_and(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)230 bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
231 const unsigned long *bitmap2, unsigned int bits)
232 {
233 unsigned int k;
234 unsigned int lim = bits/BITS_PER_LONG;
235 unsigned long result = 0;
236
237 for (k = 0; k < lim; k++)
238 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
239 if (bits % BITS_PER_LONG)
240 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
241 BITMAP_LAST_WORD_MASK(bits));
242 return result != 0;
243 }
244 EXPORT_SYMBOL(__bitmap_and);
245
__bitmap_or(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)246 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
247 const unsigned long *bitmap2, unsigned int bits)
248 {
249 unsigned int k;
250 unsigned int nr = BITS_TO_LONGS(bits);
251
252 for (k = 0; k < nr; k++)
253 dst[k] = bitmap1[k] | bitmap2[k];
254 }
255 EXPORT_SYMBOL(__bitmap_or);
256
__bitmap_xor(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)257 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
258 const unsigned long *bitmap2, unsigned int bits)
259 {
260 unsigned int k;
261 unsigned int nr = BITS_TO_LONGS(bits);
262
263 for (k = 0; k < nr; k++)
264 dst[k] = bitmap1[k] ^ bitmap2[k];
265 }
266 EXPORT_SYMBOL(__bitmap_xor);
267
__bitmap_andnot(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)268 bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
269 const unsigned long *bitmap2, unsigned int bits)
270 {
271 unsigned int k;
272 unsigned int lim = bits/BITS_PER_LONG;
273 unsigned long result = 0;
274
275 for (k = 0; k < lim; k++)
276 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
277 if (bits % BITS_PER_LONG)
278 result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
279 BITMAP_LAST_WORD_MASK(bits));
280 return result != 0;
281 }
282 EXPORT_SYMBOL(__bitmap_andnot);
283
__bitmap_replace(unsigned long * dst,const unsigned long * old,const unsigned long * new,const unsigned long * mask,unsigned int nbits)284 void __bitmap_replace(unsigned long *dst,
285 const unsigned long *old, const unsigned long *new,
286 const unsigned long *mask, unsigned int nbits)
287 {
288 unsigned int k;
289 unsigned int nr = BITS_TO_LONGS(nbits);
290
291 for (k = 0; k < nr; k++)
292 dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);
293 }
294 EXPORT_SYMBOL(__bitmap_replace);
295
__bitmap_intersects(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)296 bool __bitmap_intersects(const unsigned long *bitmap1,
297 const unsigned long *bitmap2, unsigned int bits)
298 {
299 unsigned int k, lim = bits/BITS_PER_LONG;
300 for (k = 0; k < lim; ++k)
301 if (bitmap1[k] & bitmap2[k])
302 return true;
303
304 if (bits % BITS_PER_LONG)
305 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
306 return true;
307 return false;
308 }
309 EXPORT_SYMBOL(__bitmap_intersects);
310
__bitmap_intersects_and(const unsigned long * bitmap1,const unsigned long * bitmap2,const unsigned long * bitmap3,unsigned int bits)311 bool __bitmap_intersects_and(const unsigned long *bitmap1,
312 const unsigned long *bitmap2,
313 const unsigned long *bitmap3, unsigned int bits)
314 {
315 unsigned int k, lim = bits / BITS_PER_LONG;
316
317 for (k = 0; k < lim; ++k)
318 if (bitmap1[k] & bitmap2[k] & bitmap3[k])
319 return true;
320
321 if (bits % BITS_PER_LONG)
322 if ((bitmap1[k] & bitmap2[k] & bitmap3[k]) & BITMAP_LAST_WORD_MASK(bits))
323 return true;
324 return false;
325 }
326 EXPORT_SYMBOL(__bitmap_intersects_and);
327
__bitmap_subset(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)328 bool __bitmap_subset(const unsigned long *bitmap1,
329 const unsigned long *bitmap2, unsigned int bits)
330 {
331 unsigned int k, lim = bits/BITS_PER_LONG;
332 for (k = 0; k < lim; ++k)
333 if (bitmap1[k] & ~bitmap2[k])
334 return false;
335
336 if (bits % BITS_PER_LONG)
337 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
338 return false;
339 return true;
340 }
341 EXPORT_SYMBOL(__bitmap_subset);
342
343 #define BITMAP_WEIGHT(FETCH, bits) \
344 ({ \
345 unsigned int __bits = (bits), idx, w = 0; \
346 \
347 for (idx = 0; idx < __bits / BITS_PER_LONG; idx++) \
348 w += hweight_long(FETCH); \
349 \
350 if (__bits % BITS_PER_LONG) \
351 w += hweight_long((FETCH) & BITMAP_LAST_WORD_MASK(__bits)); \
352 \
353 w; \
354 })
355
__bitmap_weight(const unsigned long * bitmap,unsigned int bits)356 unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
357 {
358 return BITMAP_WEIGHT(bitmap[idx], bits);
359 }
360 EXPORT_SYMBOL(__bitmap_weight);
361
__bitmap_weight_and(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)362 unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
363 const unsigned long *bitmap2, unsigned int bits)
364 {
365 return BITMAP_WEIGHT(bitmap1[idx] & bitmap2[idx], bits);
366 }
367 EXPORT_SYMBOL(__bitmap_weight_and);
368
__bitmap_weight_andnot(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)369 unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
370 const unsigned long *bitmap2, unsigned int bits)
371 {
372 return BITMAP_WEIGHT(bitmap1[idx] & ~bitmap2[idx], bits);
373 }
374 EXPORT_SYMBOL(__bitmap_weight_andnot);
375
__bitmap_weighted_or(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)376 unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1,
377 const unsigned long *bitmap2, unsigned int bits)
378 {
379 return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] | bitmap2[idx]; dst[idx]; }), bits);
380 }
381 EXPORT_SYMBOL(__bitmap_weighted_or);
382
__bitmap_weighted_xor(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)383 unsigned int __bitmap_weighted_xor(unsigned long *dst, const unsigned long *bitmap1,
384 const unsigned long *bitmap2, unsigned int bits)
385 {
386 return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] ^ bitmap2[idx]; dst[idx]; }), bits);
387 }
388 EXPORT_SYMBOL(__bitmap_weighted_xor);
389
__bitmap_set(unsigned long * map,unsigned int start,int len)390 void __bitmap_set(unsigned long *map, unsigned int start, int len)
391 {
392 unsigned long *p = map + BIT_WORD(start);
393 const unsigned int size = start + len;
394 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
395 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
396
397 while (len - bits_to_set >= 0) {
398 *p |= mask_to_set;
399 len -= bits_to_set;
400 bits_to_set = BITS_PER_LONG;
401 mask_to_set = ~0UL;
402 p++;
403 }
404 if (len) {
405 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
406 *p |= mask_to_set;
407 }
408 }
409 EXPORT_SYMBOL(__bitmap_set);
410
__bitmap_clear(unsigned long * map,unsigned int start,int len)411 void __bitmap_clear(unsigned long *map, unsigned int start, int len)
412 {
413 unsigned long *p = map + BIT_WORD(start);
414 const unsigned int size = start + len;
415 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
416 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
417
418 while (len - bits_to_clear >= 0) {
419 *p &= ~mask_to_clear;
420 len -= bits_to_clear;
421 bits_to_clear = BITS_PER_LONG;
422 mask_to_clear = ~0UL;
423 p++;
424 }
425 if (len) {
426 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
427 *p &= ~mask_to_clear;
428 }
429 }
430 EXPORT_SYMBOL(__bitmap_clear);
431
432 /**
433 * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
434 * @map: The address to base the search on
435 * @size: The bitmap size in bits
436 * @start: The bitnumber to start searching at
437 * @nr: The number of zeroed bits we're looking for
438 * @align_mask: Alignment mask for zero area
439 * @align_offset: Alignment offset for zero area.
440 *
441 * The @align_mask should be one less than a power of 2; the effect is that
442 * the bit offset of all zero areas this function finds plus @align_offset
443 * is multiple of that power of 2.
444 *
445 * Return: The bit offset of the found area or a value greater than or equal
446 * to @size if no area is found.
447 */
bitmap_find_next_zero_area_off(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,unsigned long align_mask,unsigned long align_offset)448 unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
449 unsigned long size,
450 unsigned long start,
451 unsigned int nr,
452 unsigned long align_mask,
453 unsigned long align_offset)
454 {
455 unsigned long end, i, off;
456
457 for_each_clear_bit_from(start, map, size) {
458 start = __ALIGN_MASK(start + align_offset, align_mask) - align_offset;
459 end = start + nr;
460 if (end > size)
461 break;
462
463 off = round_down(start, BITS_PER_LONG);
464 i = find_last_bit(map + start / BITS_PER_LONG, end - off) + off;
465 if (i >= end || i < start)
466 return start;
467
468 start = i;
469 }
470
471 return size;
472 }
473 EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
474
475 /**
476 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
477 * @buf: pointer to a bitmap
478 * @pos: a bit position in @buf (0 <= @pos < @nbits)
479 * @nbits: number of valid bit positions in @buf
480 *
481 * Map the bit at position @pos in @buf (of length @nbits) to the
482 * ordinal of which set bit it is. If it is not set or if @pos
483 * is not a valid bit position, map to -1.
484 *
485 * If for example, just bits 4 through 7 are set in @buf, then @pos
486 * values 4 through 7 will get mapped to 0 through 3, respectively,
487 * and other @pos values will get mapped to -1. When @pos value 7
488 * gets mapped to (returns) @ord value 3 in this example, that means
489 * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
490 *
491 * The bit positions 0 through @bits are valid positions in @buf.
492 */
bitmap_pos_to_ord(const unsigned long * buf,unsigned int pos,unsigned int nbits)493 static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
494 {
495 if (pos >= nbits || !test_bit(pos, buf))
496 return -1;
497
498 return bitmap_weight(buf, pos);
499 }
500
501 /**
502 * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
503 * @dst: remapped result
504 * @src: subset to be remapped
505 * @old: defines domain of map
506 * @new: defines range of map
507 * @nbits: number of bits in each of these bitmaps
508 *
509 * Let @old and @new define a mapping of bit positions, such that
510 * whatever position is held by the n-th set bit in @old is mapped
511 * to the n-th set bit in @new. In the more general case, allowing
512 * for the possibility that the weight 'w' of @new is less than the
513 * weight of @old, map the position of the n-th set bit in @old to
514 * the position of the m-th set bit in @new, where m == n % w.
515 *
516 * If either of the @old and @new bitmaps are empty, or if @src and
517 * @dst point to the same location, then this routine copies @src
518 * to @dst.
519 *
520 * The positions of unset bits in @old are mapped to themselves
521 * (the identity map).
522 *
523 * Apply the above specified mapping to @src, placing the result in
524 * @dst, clearing any bits previously set in @dst.
525 *
526 * For example, lets say that @old has bits 4 through 7 set, and
527 * @new has bits 12 through 15 set. This defines the mapping of bit
528 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
529 * bit positions unchanged. So if say @src comes into this routine
530 * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
531 * 13 and 15 set.
532 */
bitmap_remap(unsigned long * dst,const unsigned long * src,const unsigned long * old,const unsigned long * new,unsigned int nbits)533 void bitmap_remap(unsigned long *dst, const unsigned long *src,
534 const unsigned long *old, const unsigned long *new,
535 unsigned int nbits)
536 {
537 unsigned int oldbit, w;
538
539 if (dst == src) /* following doesn't handle inplace remaps */
540 return;
541 bitmap_zero(dst, nbits);
542
543 w = bitmap_weight(new, nbits);
544 for_each_set_bit(oldbit, src, nbits) {
545 int n = bitmap_pos_to_ord(old, oldbit, nbits);
546
547 if (n < 0 || w == 0)
548 set_bit(oldbit, dst); /* identity map */
549 else
550 set_bit(find_nth_bit(new, nbits, n % w), dst);
551 }
552 }
553 EXPORT_SYMBOL(bitmap_remap);
554
555 /**
556 * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
557 * @oldbit: bit position to be mapped
558 * @old: defines domain of map
559 * @new: defines range of map
560 * @bits: number of bits in each of these bitmaps
561 *
562 * Let @old and @new define a mapping of bit positions, such that
563 * whatever position is held by the n-th set bit in @old is mapped
564 * to the n-th set bit in @new. In the more general case, allowing
565 * for the possibility that the weight 'w' of @new is less than the
566 * weight of @old, map the position of the n-th set bit in @old to
567 * the position of the m-th set bit in @new, where m == n % w.
568 *
569 * The positions of unset bits in @old are mapped to themselves
570 * (the identity map).
571 *
572 * Apply the above specified mapping to bit position @oldbit, returning
573 * the new bit position.
574 *
575 * For example, lets say that @old has bits 4 through 7 set, and
576 * @new has bits 12 through 15 set. This defines the mapping of bit
577 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
578 * bit positions unchanged. So if say @oldbit is 5, then this routine
579 * returns 13.
580 */
bitmap_bitremap(int oldbit,const unsigned long * old,const unsigned long * new,int bits)581 int bitmap_bitremap(int oldbit, const unsigned long *old,
582 const unsigned long *new, int bits)
583 {
584 int w = bitmap_weight(new, bits);
585 int n = bitmap_pos_to_ord(old, oldbit, bits);
586 if (n < 0 || w == 0)
587 return oldbit;
588 else
589 return find_nth_bit(new, bits, n % w);
590 }
591 EXPORT_SYMBOL(bitmap_bitremap);
592
593 #ifdef CONFIG_NUMA
594 /**
595 * bitmap_onto - translate one bitmap relative to another
596 * @dst: resulting translated bitmap
597 * @orig: original untranslated bitmap
598 * @relmap: bitmap relative to which translated
599 * @bits: number of bits in each of these bitmaps
600 *
601 * Set the n-th bit of @dst iff there exists some m such that the
602 * n-th bit of @relmap is set, the m-th bit of @orig is set, and
603 * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
604 * (If you understood the previous sentence the first time your
605 * read it, you're overqualified for your current job.)
606 *
607 * In other words, @orig is mapped onto (surjectively) @dst,
608 * using the map { <n, m> | the n-th bit of @relmap is the
609 * m-th set bit of @relmap }.
610 *
611 * Any set bits in @orig above bit number W, where W is the
612 * weight of (number of set bits in) @relmap are mapped nowhere.
613 * In particular, if for all bits m set in @orig, m >= W, then
614 * @dst will end up empty. In situations where the possibility
615 * of such an empty result is not desired, one way to avoid it is
616 * to use the bitmap_fold() operator, below, to first fold the
617 * @orig bitmap over itself so that all its set bits x are in the
618 * range 0 <= x < W. The bitmap_fold() operator does this by
619 * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
620 *
621 * Example [1] for bitmap_onto():
622 * Let's say @relmap has bits 30-39 set, and @orig has bits
623 * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
624 * @dst will have bits 31, 33, 35, 37 and 39 set.
625 *
626 * When bit 0 is set in @orig, it means turn on the bit in
627 * @dst corresponding to whatever is the first bit (if any)
628 * that is turned on in @relmap. Since bit 0 was off in the
629 * above example, we leave off that bit (bit 30) in @dst.
630 *
631 * When bit 1 is set in @orig (as in the above example), it
632 * means turn on the bit in @dst corresponding to whatever
633 * is the second bit that is turned on in @relmap. The second
634 * bit in @relmap that was turned on in the above example was
635 * bit 31, so we turned on bit 31 in @dst.
636 *
637 * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
638 * because they were the 4th, 6th, 8th and 10th set bits
639 * set in @relmap, and the 4th, 6th, 8th and 10th bits of
640 * @orig (i.e. bits 3, 5, 7 and 9) were also set.
641 *
642 * When bit 11 is set in @orig, it means turn on the bit in
643 * @dst corresponding to whatever is the twelfth bit that is
644 * turned on in @relmap. In the above example, there were
645 * only ten bits turned on in @relmap (30..39), so that bit
646 * 11 was set in @orig had no affect on @dst.
647 *
648 * Example [2] for bitmap_fold() + bitmap_onto():
649 * Let's say @relmap has these ten bits set::
650 *
651 * 40 41 42 43 45 48 53 61 74 95
652 *
653 * (for the curious, that's 40 plus the first ten terms of the
654 * Fibonacci sequence.)
655 *
656 * Further lets say we use the following code, invoking
657 * bitmap_fold() then bitmap_onto, as suggested above to
658 * avoid the possibility of an empty @dst result::
659 *
660 * unsigned long *tmp; // a temporary bitmap's bits
661 *
662 * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
663 * bitmap_onto(dst, tmp, relmap, bits);
664 *
665 * Then this table shows what various values of @dst would be, for
666 * various @orig's. I list the zero-based positions of each set bit.
667 * The tmp column shows the intermediate result, as computed by
668 * using bitmap_fold() to fold the @orig bitmap modulo ten
669 * (the weight of @relmap):
670 *
671 * =============== ============== =================
672 * @orig tmp @dst
673 * 0 0 40
674 * 1 1 41
675 * 9 9 95
676 * 10 0 40 [#f1]_
677 * 1 3 5 7 1 3 5 7 41 43 48 61
678 * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
679 * 0 9 18 27 0 9 8 7 40 61 74 95
680 * 0 10 20 30 0 40
681 * 0 11 22 33 0 1 2 3 40 41 42 43
682 * 0 12 24 36 0 2 4 6 40 42 45 53
683 * 78 102 211 1 2 8 41 42 74 [#f1]_
684 * =============== ============== =================
685 *
686 * .. [#f1]
687 *
688 * For these marked lines, if we hadn't first done bitmap_fold()
689 * into tmp, then the @dst result would have been empty.
690 *
691 * If either of @orig or @relmap is empty (no set bits), then @dst
692 * will be returned empty.
693 *
694 * If (as explained above) the only set bits in @orig are in positions
695 * m where m >= W, (where W is the weight of @relmap) then @dst will
696 * once again be returned empty.
697 *
698 * All bits in @dst not set by the above rule are cleared.
699 */
bitmap_onto(unsigned long * dst,const unsigned long * orig,const unsigned long * relmap,unsigned int bits)700 void bitmap_onto(unsigned long *dst, const unsigned long *orig,
701 const unsigned long *relmap, unsigned int bits)
702 {
703 unsigned int n, m; /* same meaning as in above comment */
704
705 if (dst == orig) /* following doesn't handle inplace mappings */
706 return;
707 bitmap_zero(dst, bits);
708
709 /*
710 * The following code is a more efficient, but less
711 * obvious, equivalent to the loop:
712 * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
713 * n = find_nth_bit(orig, bits, m);
714 * if (test_bit(m, orig))
715 * set_bit(n, dst);
716 * }
717 */
718
719 m = 0;
720 for_each_set_bit(n, relmap, bits) {
721 /* m == bitmap_pos_to_ord(relmap, n, bits) */
722 if (test_bit(m, orig))
723 set_bit(n, dst);
724 m++;
725 }
726 }
727
728 /**
729 * bitmap_fold - fold larger bitmap into smaller, modulo specified size
730 * @dst: resulting smaller bitmap
731 * @orig: original larger bitmap
732 * @sz: specified size
733 * @nbits: number of bits in each of these bitmaps
734 *
735 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
736 * Clear all other bits in @dst. See further the comment and
737 * Example [2] for bitmap_onto() for why and how to use this.
738 */
bitmap_fold(unsigned long * dst,const unsigned long * orig,unsigned int sz,unsigned int nbits)739 void bitmap_fold(unsigned long *dst, const unsigned long *orig,
740 unsigned int sz, unsigned int nbits)
741 {
742 unsigned int oldbit;
743
744 if (dst == orig) /* following doesn't handle inplace mappings */
745 return;
746 bitmap_zero(dst, nbits);
747
748 for_each_set_bit(oldbit, orig, nbits)
749 set_bit(oldbit % sz, dst);
750 }
751 #endif /* CONFIG_NUMA */
752
bitmap_alloc(unsigned int nbits,gfp_t flags)753 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
754 {
755 return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
756 flags);
757 }
758 EXPORT_SYMBOL(bitmap_alloc);
759
bitmap_zalloc(unsigned int nbits,gfp_t flags)760 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
761 {
762 return bitmap_alloc(nbits, flags | __GFP_ZERO);
763 }
764 EXPORT_SYMBOL(bitmap_zalloc);
765
bitmap_alloc_node(unsigned int nbits,gfp_t flags,int node)766 unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node)
767 {
768 return kmalloc_array_node(BITS_TO_LONGS(nbits), sizeof(unsigned long),
769 flags, node);
770 }
771 EXPORT_SYMBOL(bitmap_alloc_node);
772
bitmap_zalloc_node(unsigned int nbits,gfp_t flags,int node)773 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node)
774 {
775 return bitmap_alloc_node(nbits, flags | __GFP_ZERO, node);
776 }
777 EXPORT_SYMBOL(bitmap_zalloc_node);
778
bitmap_free(const unsigned long * bitmap)779 void bitmap_free(const unsigned long *bitmap)
780 {
781 kfree(bitmap);
782 }
783 EXPORT_SYMBOL(bitmap_free);
784
devm_bitmap_free(void * data)785 static void devm_bitmap_free(void *data)
786 {
787 unsigned long *bitmap = data;
788
789 bitmap_free(bitmap);
790 }
791
devm_bitmap_alloc(struct device * dev,unsigned int nbits,gfp_t flags)792 unsigned long *devm_bitmap_alloc(struct device *dev,
793 unsigned int nbits, gfp_t flags)
794 {
795 unsigned long *bitmap;
796 int ret;
797
798 bitmap = bitmap_alloc(nbits, flags);
799 if (!bitmap)
800 return NULL;
801
802 ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
803 if (ret)
804 return NULL;
805
806 return bitmap;
807 }
808 EXPORT_SYMBOL_GPL(devm_bitmap_alloc);
809
devm_bitmap_zalloc(struct device * dev,unsigned int nbits,gfp_t flags)810 unsigned long *devm_bitmap_zalloc(struct device *dev,
811 unsigned int nbits, gfp_t flags)
812 {
813 return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
814 }
815 EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);
816
817 #if BITS_PER_LONG == 64
818 /**
819 * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
820 * @bitmap: array of unsigned longs, the destination bitmap
821 * @buf: array of u32 (in host byte order), the source bitmap
822 * @nbits: number of bits in @bitmap
823 */
bitmap_from_arr32(unsigned long * bitmap,const u32 * buf,unsigned int nbits)824 void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
825 {
826 unsigned int i, halfwords;
827
828 halfwords = DIV_ROUND_UP(nbits, 32);
829 for (i = 0; i < halfwords; i++) {
830 bitmap[i/2] = (unsigned long) buf[i];
831 if (++i < halfwords)
832 bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
833 }
834
835 /* Clear tail bits in last word beyond nbits. */
836 if (nbits % BITS_PER_LONG)
837 bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
838 }
839 EXPORT_SYMBOL(bitmap_from_arr32);
840
841 /**
842 * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
843 * @buf: array of u32 (in host byte order), the dest bitmap
844 * @bitmap: array of unsigned longs, the source bitmap
845 * @nbits: number of bits in @bitmap
846 */
bitmap_to_arr32(u32 * buf,const unsigned long * bitmap,unsigned int nbits)847 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
848 {
849 unsigned int i, halfwords;
850
851 halfwords = DIV_ROUND_UP(nbits, 32);
852 for (i = 0; i < halfwords; i++) {
853 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
854 if (++i < halfwords)
855 buf[i] = (u32) (bitmap[i/2] >> 32);
856 }
857
858 /* Clear tail bits in last element of array beyond nbits. */
859 if (nbits % BITS_PER_LONG)
860 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
861 }
862 EXPORT_SYMBOL(bitmap_to_arr32);
863 #endif
864
865 #if BITS_PER_LONG == 32
866 /**
867 * bitmap_from_arr64 - copy the contents of u64 array of bits to bitmap
868 * @bitmap: array of unsigned longs, the destination bitmap
869 * @buf: array of u64 (in host byte order), the source bitmap
870 * @nbits: number of bits in @bitmap
871 */
bitmap_from_arr64(unsigned long * bitmap,const u64 * buf,unsigned int nbits)872 void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits)
873 {
874 int n;
875
876 for (n = nbits; n > 0; n -= 64) {
877 u64 val = *buf++;
878
879 *bitmap++ = val;
880 if (n > 32)
881 *bitmap++ = val >> 32;
882 }
883
884 /*
885 * Clear tail bits in the last word beyond nbits.
886 *
887 * Negative index is OK because here we point to the word next
888 * to the last word of the bitmap, except for nbits == 0, which
889 * is tested implicitly.
890 */
891 if (nbits % BITS_PER_LONG)
892 bitmap[-1] &= BITMAP_LAST_WORD_MASK(nbits);
893 }
894 EXPORT_SYMBOL(bitmap_from_arr64);
895
896 /**
897 * bitmap_to_arr64 - copy the contents of bitmap to a u64 array of bits
898 * @buf: array of u64 (in host byte order), the dest bitmap
899 * @bitmap: array of unsigned longs, the source bitmap
900 * @nbits: number of bits in @bitmap
901 */
bitmap_to_arr64(u64 * buf,const unsigned long * bitmap,unsigned int nbits)902 void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits)
903 {
904 const unsigned long *end = bitmap + BITS_TO_LONGS(nbits);
905
906 while (bitmap < end) {
907 *buf = *bitmap++;
908 if (bitmap < end)
909 *buf |= (u64)(*bitmap++) << 32;
910 buf++;
911 }
912
913 /* Clear tail bits in the last element of array beyond nbits. */
914 if (nbits % 64)
915 buf[-1] &= GENMASK_ULL((nbits - 1) % 64, 0);
916 }
917 EXPORT_SYMBOL(bitmap_to_arr64);
918 #endif
919