xref: /linux-6.15/include/linux/bitmap.h (revision e00a844a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_BITMAP_H
3 #define __LINUX_BITMAP_H
4 
5 #ifndef __ASSEMBLY__
6 
7 #include <linux/types.h>
8 #include <linux/bitops.h>
9 #include <linux/string.h>
10 #include <linux/kernel.h>
11 
12 /*
13  * bitmaps provide bit arrays that consume one or more unsigned
14  * longs.  The bitmap interface and available operations are listed
15  * here, in bitmap.h
16  *
17  * Function implementations generic to all architectures are in
18  * lib/bitmap.c.  Functions implementations that are architecture
19  * specific are in various include/asm-<arch>/bitops.h headers
20  * and other arch/<arch> specific files.
21  *
22  * See lib/bitmap.c for more details.
23  */
24 
25 /**
26  * DOC: bitmap overview
27  *
28  * The available bitmap operations and their rough meaning in the
29  * case that the bitmap is a single unsigned long are thus:
30  *
31  * Note that nbits should be always a compile time evaluable constant.
32  * Otherwise many inlines will generate horrible code.
33  *
34  * ::
35  *
36  *  bitmap_zero(dst, nbits)                     *dst = 0UL
37  *  bitmap_fill(dst, nbits)                     *dst = ~0UL
38  *  bitmap_copy(dst, src, nbits)                *dst = *src
39  *  bitmap_and(dst, src1, src2, nbits)          *dst = *src1 & *src2
40  *  bitmap_or(dst, src1, src2, nbits)           *dst = *src1 | *src2
41  *  bitmap_xor(dst, src1, src2, nbits)          *dst = *src1 ^ *src2
42  *  bitmap_andnot(dst, src1, src2, nbits)       *dst = *src1 & ~(*src2)
43  *  bitmap_complement(dst, src, nbits)          *dst = ~(*src)
44  *  bitmap_equal(src1, src2, nbits)             Are *src1 and *src2 equal?
45  *  bitmap_intersects(src1, src2, nbits)        Do *src1 and *src2 overlap?
46  *  bitmap_subset(src1, src2, nbits)            Is *src1 a subset of *src2?
47  *  bitmap_empty(src, nbits)                    Are all bits zero in *src?
48  *  bitmap_full(src, nbits)                     Are all bits set in *src?
49  *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
50  *  bitmap_set(dst, pos, nbits)                 Set specified bit area
51  *  bitmap_clear(dst, pos, nbits)               Clear specified bit area
52  *  bitmap_find_next_zero_area(buf, len, pos, n, mask)  Find bit free area
53  *  bitmap_find_next_zero_area_off(buf, len, pos, n, mask)  as above
54  *  bitmap_shift_right(dst, src, n, nbits)      *dst = *src >> n
55  *  bitmap_shift_left(dst, src, n, nbits)       *dst = *src << n
56  *  bitmap_remap(dst, src, old, new, nbits)     *dst = map(old, new)(src)
57  *  bitmap_bitremap(oldbit, old, new, nbits)    newbit = map(old, new)(oldbit)
58  *  bitmap_onto(dst, orig, relmap, nbits)       *dst = orig relative to relmap
59  *  bitmap_fold(dst, orig, sz, nbits)           dst bits = orig bits mod sz
60  *  bitmap_parse(buf, buflen, dst, nbits)       Parse bitmap dst from kernel buf
61  *  bitmap_parse_user(ubuf, ulen, dst, nbits)   Parse bitmap dst from user buf
62  *  bitmap_parselist(buf, dst, nbits)           Parse bitmap dst from kernel buf
63  *  bitmap_parselist_user(buf, dst, nbits)      Parse bitmap dst from user buf
64  *  bitmap_find_free_region(bitmap, bits, order)  Find and allocate bit region
65  *  bitmap_release_region(bitmap, pos, order)   Free specified bit region
66  *  bitmap_allocate_region(bitmap, pos, order)  Allocate specified bit region
67  *  bitmap_from_u32array(dst, nbits, buf, nwords)  *dst = *buf (nwords 32b words)
68  *  bitmap_to_u32array(buf, nwords, src, nbits) *buf = *dst (nwords 32b words)
69  *
70  */
71 
72 /**
73  * DOC: bitmap bitops
74  *
75  * Also the following operations in asm/bitops.h apply to bitmaps.::
76  *
77  *  set_bit(bit, addr)                  *addr |= bit
78  *  clear_bit(bit, addr)                *addr &= ~bit
79  *  change_bit(bit, addr)               *addr ^= bit
80  *  test_bit(bit, addr)                 Is bit set in *addr?
81  *  test_and_set_bit(bit, addr)         Set bit and return old value
82  *  test_and_clear_bit(bit, addr)       Clear bit and return old value
83  *  test_and_change_bit(bit, addr)      Change bit and return old value
84  *  find_first_zero_bit(addr, nbits)    Position first zero bit in *addr
85  *  find_first_bit(addr, nbits)         Position first set bit in *addr
86  *  find_next_zero_bit(addr, nbits, bit)  Position next zero bit in *addr >= bit
87  *  find_next_bit(addr, nbits, bit)     Position next set bit in *addr >= bit
88  *
89  */
90 
91 /**
92  * DOC: declare bitmap
93  * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
94  * to declare an array named 'name' of just enough unsigned longs to
95  * contain all bit positions from 0 to 'bits' - 1.
96  */
97 
98 /*
99  * lib/bitmap.c provides these functions:
100  */
101 
102 extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
103 extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
104 extern int __bitmap_equal(const unsigned long *bitmap1,
105 			  const unsigned long *bitmap2, unsigned int nbits);
106 extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
107 			unsigned int nbits);
108 extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
109 				unsigned int shift, unsigned int nbits);
110 extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
111 				unsigned int shift, unsigned int nbits);
112 extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
113 			const unsigned long *bitmap2, unsigned int nbits);
114 extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
115 			const unsigned long *bitmap2, unsigned int nbits);
116 extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
117 			const unsigned long *bitmap2, unsigned int nbits);
118 extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
119 			const unsigned long *bitmap2, unsigned int nbits);
120 extern int __bitmap_intersects(const unsigned long *bitmap1,
121 			const unsigned long *bitmap2, unsigned int nbits);
122 extern int __bitmap_subset(const unsigned long *bitmap1,
123 			const unsigned long *bitmap2, unsigned int nbits);
124 extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
125 extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
126 extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
127 
128 extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
129 						    unsigned long size,
130 						    unsigned long start,
131 						    unsigned int nr,
132 						    unsigned long align_mask,
133 						    unsigned long align_offset);
134 
135 /**
136  * bitmap_find_next_zero_area - find a contiguous aligned zero area
137  * @map: The address to base the search on
138  * @size: The bitmap size in bits
139  * @start: The bitnumber to start searching at
140  * @nr: The number of zeroed bits we're looking for
141  * @align_mask: Alignment mask for zero area
142  *
143  * The @align_mask should be one less than a power of 2; the effect is that
144  * the bit offset of all zero areas this function finds is multiples of that
145  * power of 2. A @align_mask of 0 means no alignment is required.
146  */
147 static inline unsigned long
148 bitmap_find_next_zero_area(unsigned long *map,
149 			   unsigned long size,
150 			   unsigned long start,
151 			   unsigned int nr,
152 			   unsigned long align_mask)
153 {
154 	return bitmap_find_next_zero_area_off(map, size, start, nr,
155 					      align_mask, 0);
156 }
157 
158 extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
159 			unsigned long *dst, int nbits);
160 extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
161 			unsigned long *dst, int nbits);
162 extern int bitmap_parselist(const char *buf, unsigned long *maskp,
163 			int nmaskbits);
164 extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
165 			unsigned long *dst, int nbits);
166 extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
167 		const unsigned long *old, const unsigned long *new, unsigned int nbits);
168 extern int bitmap_bitremap(int oldbit,
169 		const unsigned long *old, const unsigned long *new, int bits);
170 extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
171 		const unsigned long *relmap, unsigned int bits);
172 extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
173 		unsigned int sz, unsigned int nbits);
174 extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
175 extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
176 extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
177 extern unsigned int bitmap_from_u32array(unsigned long *bitmap,
178 					 unsigned int nbits,
179 					 const u32 *buf,
180 					 unsigned int nwords);
181 extern unsigned int bitmap_to_u32array(u32 *buf,
182 				       unsigned int nwords,
183 				       const unsigned long *bitmap,
184 				       unsigned int nbits);
185 #ifdef __BIG_ENDIAN
186 extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
187 #else
188 #define bitmap_copy_le bitmap_copy
189 #endif
190 extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
191 extern int bitmap_print_to_pagebuf(bool list, char *buf,
192 				   const unsigned long *maskp, int nmaskbits);
193 
194 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
195 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
196 
197 #define small_const_nbits(nbits) \
198 	(__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
199 
200 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
201 {
202 	if (small_const_nbits(nbits))
203 		*dst = 0UL;
204 	else {
205 		unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
206 		memset(dst, 0, len);
207 	}
208 }
209 
210 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
211 {
212 	unsigned int nlongs = BITS_TO_LONGS(nbits);
213 	if (!small_const_nbits(nbits)) {
214 		unsigned int len = (nlongs - 1) * sizeof(unsigned long);
215 		memset(dst, 0xff,  len);
216 	}
217 	dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
218 }
219 
220 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
221 			unsigned int nbits)
222 {
223 	if (small_const_nbits(nbits))
224 		*dst = *src;
225 	else {
226 		unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
227 		memcpy(dst, src, len);
228 	}
229 }
230 
231 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
232 			const unsigned long *src2, unsigned int nbits)
233 {
234 	if (small_const_nbits(nbits))
235 		return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
236 	return __bitmap_and(dst, src1, src2, nbits);
237 }
238 
239 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
240 			const unsigned long *src2, unsigned int nbits)
241 {
242 	if (small_const_nbits(nbits))
243 		*dst = *src1 | *src2;
244 	else
245 		__bitmap_or(dst, src1, src2, nbits);
246 }
247 
248 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
249 			const unsigned long *src2, unsigned int nbits)
250 {
251 	if (small_const_nbits(nbits))
252 		*dst = *src1 ^ *src2;
253 	else
254 		__bitmap_xor(dst, src1, src2, nbits);
255 }
256 
257 static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
258 			const unsigned long *src2, unsigned int nbits)
259 {
260 	if (small_const_nbits(nbits))
261 		return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
262 	return __bitmap_andnot(dst, src1, src2, nbits);
263 }
264 
265 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
266 			unsigned int nbits)
267 {
268 	if (small_const_nbits(nbits))
269 		*dst = ~(*src);
270 	else
271 		__bitmap_complement(dst, src, nbits);
272 }
273 
274 static inline int bitmap_equal(const unsigned long *src1,
275 			const unsigned long *src2, unsigned int nbits)
276 {
277 	if (small_const_nbits(nbits))
278 		return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
279 	if (__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
280 		return !memcmp(src1, src2, nbits / 8);
281 	return __bitmap_equal(src1, src2, nbits);
282 }
283 
284 static inline int bitmap_intersects(const unsigned long *src1,
285 			const unsigned long *src2, unsigned int nbits)
286 {
287 	if (small_const_nbits(nbits))
288 		return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
289 	else
290 		return __bitmap_intersects(src1, src2, nbits);
291 }
292 
293 static inline int bitmap_subset(const unsigned long *src1,
294 			const unsigned long *src2, unsigned int nbits)
295 {
296 	if (small_const_nbits(nbits))
297 		return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
298 	else
299 		return __bitmap_subset(src1, src2, nbits);
300 }
301 
302 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
303 {
304 	if (small_const_nbits(nbits))
305 		return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
306 
307 	return find_first_bit(src, nbits) == nbits;
308 }
309 
310 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
311 {
312 	if (small_const_nbits(nbits))
313 		return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
314 
315 	return find_first_zero_bit(src, nbits) == nbits;
316 }
317 
318 static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
319 {
320 	if (small_const_nbits(nbits))
321 		return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
322 	return __bitmap_weight(src, nbits);
323 }
324 
325 static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
326 		unsigned int nbits)
327 {
328 	if (__builtin_constant_p(nbits) && nbits == 1)
329 		__set_bit(start, map);
330 	else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
331 		 __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
332 		memset((char *)map + start / 8, 0xff, nbits / 8);
333 	else
334 		__bitmap_set(map, start, nbits);
335 }
336 
337 static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
338 		unsigned int nbits)
339 {
340 	if (__builtin_constant_p(nbits) && nbits == 1)
341 		__clear_bit(start, map);
342 	else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
343 		 __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
344 		memset((char *)map + start / 8, 0, nbits / 8);
345 	else
346 		__bitmap_clear(map, start, nbits);
347 }
348 
349 static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
350 				unsigned int shift, int nbits)
351 {
352 	if (small_const_nbits(nbits))
353 		*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
354 	else
355 		__bitmap_shift_right(dst, src, shift, nbits);
356 }
357 
358 static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
359 				unsigned int shift, unsigned int nbits)
360 {
361 	if (small_const_nbits(nbits))
362 		*dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
363 	else
364 		__bitmap_shift_left(dst, src, shift, nbits);
365 }
366 
367 static inline int bitmap_parse(const char *buf, unsigned int buflen,
368 			unsigned long *maskp, int nmaskbits)
369 {
370 	return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
371 }
372 
373 /**
374  * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
375  * @n: u64 value
376  *
377  * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
378  * integers in 32-bit environment, and 64-bit integers in 64-bit one.
379  *
380  * There are four combinations of endianness and length of the word in linux
381  * ABIs: LE64, BE64, LE32 and BE32.
382  *
383  * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
384  * bitmaps and therefore don't require any special handling.
385  *
386  * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
387  * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
388  * other hand is represented as an array of 32-bit words and the position of
389  * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
390  * word.  For example, bit #42 is located at 10th position of 2nd word.
391  * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
392  * values in memory as it usually does. But for BE we need to swap hi and lo
393  * words manually.
394  *
395  * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
396  * lo parts of u64.  For LE32 it does nothing, and for BE environment it swaps
397  * hi and lo words, as is expected by bitmap.
398  */
399 #if __BITS_PER_LONG == 64
400 #define BITMAP_FROM_U64(n) (n)
401 #else
402 #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
403 				((unsigned long) ((u64)(n) >> 32))
404 #endif
405 
406 /**
407  * bitmap_from_u64 - Check and swap words within u64.
408  *  @mask: source bitmap
409  *  @dst:  destination bitmap
410  *
411  * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
412  * to read u64 mask, we will get the wrong word.
413  * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
414  * but we expect the lower 32-bits of u64.
415  */
416 static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
417 {
418 	dst[0] = mask & ULONG_MAX;
419 
420 	if (sizeof(mask) > sizeof(unsigned long))
421 		dst[1] = mask >> 32;
422 }
423 
424 #endif /* __ASSEMBLY__ */
425 
426 #endif /* __LINUX_BITMAP_H */
427