1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_BITMAP_H
3 #define __LINUX_BITMAP_H
7 #include <linux/align.h>
8 #include <linux/bitops.h>
9 #include <linux/find.h>
10 #include <linux/limits.h>
11 #include <linux/string.h>
12 #include <linux/types.h>
17 * bitmaps provide bit arrays that consume one or more unsigned
18 * longs. The bitmap interface and available operations are listed
21 * Function implementations generic to all architectures are in
22 * lib/bitmap.c. Functions implementations that are architecture
23 * specific are in various include/asm-<arch>/bitops.h headers
24 * and other arch/<arch> specific files.
26 * See lib/bitmap.c for more details.
30 * DOC: bitmap overview
32 * The available bitmap operations and their rough meaning in the
33 * case that the bitmap is a single unsigned long are thus:
35 * The generated code is more efficient when nbits is known at
36 * compile-time and at most BITS_PER_LONG.
40 * bitmap_zero(dst, nbits) *dst = 0UL
41 * bitmap_fill(dst, nbits) *dst = ~0UL
42 * bitmap_copy(dst, src, nbits) *dst = *src
43 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
44 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
45 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
46 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
47 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
48 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
49 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
50 * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
51 * bitmap_empty(src, nbits) Are all bits zero in *src?
52 * bitmap_full(src, nbits) Are all bits set in *src?
53 * bitmap_weight(src, nbits) Hamming Weight: number set bits
54 * bitmap_set(dst, pos, nbits) Set specified bit area
55 * bitmap_clear(dst, pos, nbits) Clear specified bit area
56 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
57 * bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
58 * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
59 * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
60 * bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
61 * bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
62 * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
63 * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
64 * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
65 * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
66 * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
67 * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
68 * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
69 * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
70 * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
71 * bitmap_release_region(bitmap, pos, order) Free specified bit region
72 * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
73 * bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
74 * bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
75 * bitmap_get_value8(map, start) Get 8bit value from map at start
76 * bitmap_set_value8(map, value, start) Set 8bit value to map at start
78 * Note, bitmap_zero() and bitmap_fill() operate over the region of
79 * unsigned longs, that is, bits behind bitmap till the unsigned long
80 * boundary will be zeroed or filled as well. Consider to use
81 * bitmap_clear() or bitmap_set() to make explicit zeroing or filling
88 * Also the following operations in asm/bitops.h apply to bitmaps.::
90 * set_bit(bit, addr) *addr |= bit
91 * clear_bit(bit, addr) *addr &= ~bit
92 * change_bit(bit, addr) *addr ^= bit
93 * test_bit(bit, addr) Is bit set in *addr?
94 * test_and_set_bit(bit, addr) Set bit and return old value
95 * test_and_clear_bit(bit, addr) Clear bit and return old value
96 * test_and_change_bit(bit, addr) Change bit and return old value
97 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
98 * find_first_bit(addr, nbits) Position first set bit in *addr
99 * find_next_zero_bit(addr, nbits, bit)
100 * Position next zero bit in *addr >= bit
101 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
102 * find_next_and_bit(addr1, addr2, nbits, bit)
103 * Same as find_next_bit, but in
109 * DOC: declare bitmap
110 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
111 * to declare an array named 'name' of just enough unsigned longs to
112 * contain all bit positions from 0 to 'bits' - 1.
116 * Allocation and deallocation of bitmap.
117 * Provided in lib/bitmap.c to avoid circular dependency.
119 unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
120 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
121 unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
122 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
123 void bitmap_free(const unsigned long *bitmap);
125 /* Managed variants of the above. */
126 unsigned long *devm_bitmap_alloc(struct device *dev,
127 unsigned int nbits, gfp_t flags);
128 unsigned long *devm_bitmap_zalloc(struct device *dev,
129 unsigned int nbits, gfp_t flags);
132 * lib/bitmap.c provides these functions:
135 int __bitmap_equal(const unsigned long *bitmap1,
136 const unsigned long *bitmap2, unsigned int nbits);
137 bool __pure __bitmap_or_equal(const unsigned long *src1,
138 const unsigned long *src2,
139 const unsigned long *src3,
141 void __bitmap_complement(unsigned long *dst, const unsigned long *src,
143 void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
144 unsigned int shift, unsigned int nbits);
145 void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
146 unsigned int shift, unsigned int nbits);
147 void bitmap_cut(unsigned long *dst, const unsigned long *src,
148 unsigned int first, unsigned int cut, unsigned int nbits);
149 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
150 const unsigned long *bitmap2, unsigned int nbits);
151 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
152 const unsigned long *bitmap2, unsigned int nbits);
153 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
154 const unsigned long *bitmap2, unsigned int nbits);
155 int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
156 const unsigned long *bitmap2, unsigned int nbits);
157 void __bitmap_replace(unsigned long *dst,
158 const unsigned long *old, const unsigned long *new,
159 const unsigned long *mask, unsigned int nbits);
160 int __bitmap_intersects(const unsigned long *bitmap1,
161 const unsigned long *bitmap2, unsigned int nbits);
162 int __bitmap_subset(const unsigned long *bitmap1,
163 const unsigned long *bitmap2, unsigned int nbits);
164 int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
165 void __bitmap_set(unsigned long *map, unsigned int start, int len);
166 void __bitmap_clear(unsigned long *map, unsigned int start, int len);
168 unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
172 unsigned long align_mask,
173 unsigned long align_offset);
176 * bitmap_find_next_zero_area - find a contiguous aligned zero area
177 * @map: The address to base the search on
178 * @size: The bitmap size in bits
179 * @start: The bitnumber to start searching at
180 * @nr: The number of zeroed bits we're looking for
181 * @align_mask: Alignment mask for zero area
183 * The @align_mask should be one less than a power of 2; the effect is that
184 * the bit offset of all zero areas this function finds is multiples of that
185 * power of 2. A @align_mask of 0 means no alignment is required.
187 static inline unsigned long
188 bitmap_find_next_zero_area(unsigned long *map,
192 unsigned long align_mask)
194 return bitmap_find_next_zero_area_off(map, size, start, nr,
198 int bitmap_parse(const char *buf, unsigned int buflen,
199 unsigned long *dst, int nbits);
200 int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
201 unsigned long *dst, int nbits);
202 int bitmap_parselist(const char *buf, unsigned long *maskp,
204 int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
205 unsigned long *dst, int nbits);
206 void bitmap_remap(unsigned long *dst, const unsigned long *src,
207 const unsigned long *old, const unsigned long *new, unsigned int nbits);
208 int bitmap_bitremap(int oldbit,
209 const unsigned long *old, const unsigned long *new, int bits);
210 void bitmap_onto(unsigned long *dst, const unsigned long *orig,
211 const unsigned long *relmap, unsigned int bits);
212 void bitmap_fold(unsigned long *dst, const unsigned long *orig,
213 unsigned int sz, unsigned int nbits);
214 int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
215 void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
216 int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
219 void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
221 #define bitmap_copy_le bitmap_copy
223 unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
224 int bitmap_print_to_pagebuf(bool list, char *buf,
225 const unsigned long *maskp, int nmaskbits);
227 extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
228 int nmaskbits, loff_t off, size_t count);
230 extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
231 int nmaskbits, loff_t off, size_t count);
233 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
234 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
236 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
238 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
242 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
244 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
245 memset(dst, 0xff, len);
248 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
251 unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
252 memcpy(dst, src, len);
256 * Copy bitmap and clear tail bits in last word.
258 static inline void bitmap_copy_clear_tail(unsigned long *dst,
259 const unsigned long *src, unsigned int nbits)
261 bitmap_copy(dst, src, nbits);
262 if (nbits % BITS_PER_LONG)
263 dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
267 * On 32-bit systems bitmaps are represented as u32 arrays internally, and
268 * therefore conversion is not needed when copying data from/to arrays of u32.
270 #if BITS_PER_LONG == 64
271 void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
273 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
276 #define bitmap_from_arr32(bitmap, buf, nbits) \
277 bitmap_copy_clear_tail((unsigned long *) (bitmap), \
278 (const unsigned long *) (buf), (nbits))
279 #define bitmap_to_arr32(buf, bitmap, nbits) \
280 bitmap_copy_clear_tail((unsigned long *) (buf), \
281 (const unsigned long *) (bitmap), (nbits))
284 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
285 const unsigned long *src2, unsigned int nbits)
287 if (small_const_nbits(nbits))
288 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
289 return __bitmap_and(dst, src1, src2, nbits);
292 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
293 const unsigned long *src2, unsigned int nbits)
295 if (small_const_nbits(nbits))
296 *dst = *src1 | *src2;
298 __bitmap_or(dst, src1, src2, nbits);
301 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
302 const unsigned long *src2, unsigned int nbits)
304 if (small_const_nbits(nbits))
305 *dst = *src1 ^ *src2;
307 __bitmap_xor(dst, src1, src2, nbits);
310 static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
311 const unsigned long *src2, unsigned int nbits)
313 if (small_const_nbits(nbits))
314 return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
315 return __bitmap_andnot(dst, src1, src2, nbits);
318 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
321 if (small_const_nbits(nbits))
324 __bitmap_complement(dst, src, nbits);
327 #ifdef __LITTLE_ENDIAN
328 #define BITMAP_MEM_ALIGNMENT 8
330 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
332 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
334 static inline int bitmap_equal(const unsigned long *src1,
335 const unsigned long *src2, unsigned int nbits)
337 if (small_const_nbits(nbits))
338 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
339 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
340 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
341 return !memcmp(src1, src2, nbits / 8);
342 return __bitmap_equal(src1, src2, nbits);
346 * bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
347 * @src1: Pointer to bitmap 1
348 * @src2: Pointer to bitmap 2 will be or'ed with bitmap 1
349 * @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
350 * @nbits: number of bits in each of these bitmaps
352 * Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
354 static inline bool bitmap_or_equal(const unsigned long *src1,
355 const unsigned long *src2,
356 const unsigned long *src3,
359 if (!small_const_nbits(nbits))
360 return __bitmap_or_equal(src1, src2, src3, nbits);
362 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
365 static inline int bitmap_intersects(const unsigned long *src1,
366 const unsigned long *src2, unsigned int nbits)
368 if (small_const_nbits(nbits))
369 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
371 return __bitmap_intersects(src1, src2, nbits);
374 static inline int bitmap_subset(const unsigned long *src1,
375 const unsigned long *src2, unsigned int nbits)
377 if (small_const_nbits(nbits))
378 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
380 return __bitmap_subset(src1, src2, nbits);
383 static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
385 if (small_const_nbits(nbits))
386 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
388 return find_first_bit(src, nbits) == nbits;
391 static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
393 if (small_const_nbits(nbits))
394 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
396 return find_first_zero_bit(src, nbits) == nbits;
399 static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
401 if (small_const_nbits(nbits))
402 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
403 return __bitmap_weight(src, nbits);
406 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
409 if (__builtin_constant_p(nbits) && nbits == 1)
410 __set_bit(start, map);
411 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
412 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
413 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
414 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
415 memset((char *)map + start / 8, 0xff, nbits / 8);
417 __bitmap_set(map, start, nbits);
420 static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
423 if (__builtin_constant_p(nbits) && nbits == 1)
424 __clear_bit(start, map);
425 else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
426 IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
427 __builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
428 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
429 memset((char *)map + start / 8, 0, nbits / 8);
431 __bitmap_clear(map, start, nbits);
434 static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
435 unsigned int shift, unsigned int nbits)
437 if (small_const_nbits(nbits))
438 *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
440 __bitmap_shift_right(dst, src, shift, nbits);
443 static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
444 unsigned int shift, unsigned int nbits)
446 if (small_const_nbits(nbits))
447 *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
449 __bitmap_shift_left(dst, src, shift, nbits);
452 static inline void bitmap_replace(unsigned long *dst,
453 const unsigned long *old,
454 const unsigned long *new,
455 const unsigned long *mask,
458 if (small_const_nbits(nbits))
459 *dst = (*old & ~(*mask)) | (*new & *mask);
461 __bitmap_replace(dst, old, new, mask, nbits);
464 static inline void bitmap_next_set_region(unsigned long *bitmap,
465 unsigned int *rs, unsigned int *re,
468 *rs = find_next_bit(bitmap, end, *rs);
469 *re = find_next_zero_bit(bitmap, end, *rs + 1);
473 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
476 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
477 * integers in 32-bit environment, and 64-bit integers in 64-bit one.
479 * There are four combinations of endianness and length of the word in linux
480 * ABIs: LE64, BE64, LE32 and BE32.
482 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
483 * bitmaps and therefore don't require any special handling.
485 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
486 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
487 * other hand is represented as an array of 32-bit words and the position of
488 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
489 * word. For example, bit #42 is located at 10th position of 2nd word.
490 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
491 * values in memory as it usually does. But for BE we need to swap hi and lo
494 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
495 * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
496 * hi and lo words, as is expected by bitmap.
498 #if __BITS_PER_LONG == 64
499 #define BITMAP_FROM_U64(n) (n)
501 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
502 ((unsigned long) ((u64)(n) >> 32))
506 * bitmap_from_u64 - Check and swap words within u64.
507 * @mask: source bitmap
508 * @dst: destination bitmap
510 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
511 * to read u64 mask, we will get the wrong word.
512 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
513 * but we expect the lower 32-bits of u64.
515 static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
517 dst[0] = mask & ULONG_MAX;
519 if (sizeof(mask) > sizeof(unsigned long))
524 * bitmap_get_value8 - get an 8-bit value within a memory region
525 * @map: address to the bitmap memory region
526 * @start: bit offset of the 8-bit value; must be a multiple of 8
528 * Returns the 8-bit value located at the @start bit offset within the @src
531 static inline unsigned long bitmap_get_value8(const unsigned long *map,
534 const size_t index = BIT_WORD(start);
535 const unsigned long offset = start % BITS_PER_LONG;
537 return (map[index] >> offset) & 0xFF;
541 * bitmap_set_value8 - set an 8-bit value within a memory region
542 * @map: address to the bitmap memory region
543 * @value: the 8-bit value; values wider than 8 bits may clobber bitmap
544 * @start: bit offset of the 8-bit value; must be a multiple of 8
546 static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
549 const size_t index = BIT_WORD(start);
550 const unsigned long offset = start % BITS_PER_LONG;
552 map[index] &= ~(0xFFUL << offset);
553 map[index] |= value << offset;
556 #endif /* __ASSEMBLY__ */
558 #endif /* __LINUX_BITMAP_H */