bitmap.h 16.7 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
#ifndef __LINUX_BITMAP_H
#define __LINUX_BITMAP_H

#ifndef __ASSEMBLY__

#include <linux/types.h>
#include <linux/bitops.h>
#include <linux/string.h>
J
Jiri Slaby 已提交
10
#include <linux/kernel.h>
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24

/*
 * bitmaps provide bit arrays that consume one or more unsigned
 * longs.  The bitmap interface and available operations are listed
 * here, in bitmap.h
 *
 * Function implementations generic to all architectures are in
 * lib/bitmap.c.  Functions implementations that are architecture
 * specific are in various include/asm-<arch>/bitops.h headers
 * and other arch/<arch> specific files.
 *
 * See lib/bitmap.c for more details.
 */

25 26 27
/**
 * DOC: bitmap overview
 *
L
Linus Torvalds 已提交
28 29 30
 * The available bitmap operations and their rough meaning in the
 * case that the bitmap is a single unsigned long are thus:
 *
31 32 33
 * Note that nbits should be always a compile time evaluable constant.
 * Otherwise many inlines will generate horrible code.
 *
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * ::
 *
 *  bitmap_zero(dst, nbits)                     *dst = 0UL
 *  bitmap_fill(dst, nbits)                     *dst = ~0UL
 *  bitmap_copy(dst, src, nbits)                *dst = *src
 *  bitmap_and(dst, src1, src2, nbits)          *dst = *src1 & *src2
 *  bitmap_or(dst, src1, src2, nbits)           *dst = *src1 | *src2
 *  bitmap_xor(dst, src1, src2, nbits)          *dst = *src1 ^ *src2
 *  bitmap_andnot(dst, src1, src2, nbits)       *dst = *src1 & ~(*src2)
 *  bitmap_complement(dst, src, nbits)          *dst = ~(*src)
 *  bitmap_equal(src1, src2, nbits)             Are *src1 and *src2 equal?
 *  bitmap_intersects(src1, src2, nbits)        Do *src1 and *src2 overlap?
 *  bitmap_subset(src1, src2, nbits)            Is *src1 a subset of *src2?
 *  bitmap_empty(src, nbits)                    Are all bits zero in *src?
 *  bitmap_full(src, nbits)                     Are all bits set in *src?
 *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
 *  bitmap_set(dst, pos, nbits)                 Set specified bit area
 *  bitmap_clear(dst, pos, nbits)               Clear specified bit area
 *  bitmap_find_next_zero_area(buf, len, pos, n, mask)  Find bit free area
 *  bitmap_find_next_zero_area_off(buf, len, pos, n, mask)  as above
 *  bitmap_shift_right(dst, src, n, nbits)      *dst = *src >> n
 *  bitmap_shift_left(dst, src, n, nbits)       *dst = *src << n
 *  bitmap_remap(dst, src, old, new, nbits)     *dst = map(old, new)(src)
 *  bitmap_bitremap(oldbit, old, new, nbits)    newbit = map(old, new)(oldbit)
 *  bitmap_onto(dst, orig, relmap, nbits)       *dst = orig relative to relmap
 *  bitmap_fold(dst, orig, sz, nbits)           dst bits = orig bits mod sz
 *  bitmap_parse(buf, buflen, dst, nbits)       Parse bitmap dst from kernel buf
 *  bitmap_parse_user(ubuf, ulen, dst, nbits)   Parse bitmap dst from user buf
 *  bitmap_parselist(buf, dst, nbits)           Parse bitmap dst from kernel buf
 *  bitmap_parselist_user(buf, dst, nbits)      Parse bitmap dst from user buf
 *  bitmap_find_free_region(bitmap, bits, order)  Find and allocate bit region
 *  bitmap_release_region(bitmap, pos, order)   Free specified bit region
 *  bitmap_allocate_region(bitmap, pos, order)  Allocate specified bit region
67 68
 *  bitmap_from_arr32(dst, buf, nbits)          Copy nbits from u32[] buf to dst
 *  bitmap_to_arr32(buf, src, nbits)            Copy nbits from buf to u32[] dst
69
 *
70 71 72 73 74
 * Note, bitmap_zero() and bitmap_fill() operate over the region of
 * unsigned longs, that is, bits behind bitmap till the unsigned long
 * boundary will be zeroed or filled as well. Consider to use
 * bitmap_clear() or bitmap_set() to make explicit zeroing or filling
 * respectively.
L
Linus Torvalds 已提交
75 76
 */

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * DOC: bitmap bitops
 *
 * Also the following operations in asm/bitops.h apply to bitmaps.::
 *
 *  set_bit(bit, addr)                  *addr |= bit
 *  clear_bit(bit, addr)                *addr &= ~bit
 *  change_bit(bit, addr)               *addr ^= bit
 *  test_bit(bit, addr)                 Is bit set in *addr?
 *  test_and_set_bit(bit, addr)         Set bit and return old value
 *  test_and_clear_bit(bit, addr)       Clear bit and return old value
 *  test_and_change_bit(bit, addr)      Change bit and return old value
 *  find_first_zero_bit(addr, nbits)    Position first zero bit in *addr
 *  find_first_bit(addr, nbits)         Position first set bit in *addr
 *  find_next_zero_bit(addr, nbits, bit)  Position next zero bit in *addr >= bit
 *  find_next_bit(addr, nbits, bit)     Position next set bit in *addr >= bit
L
Linus Torvalds 已提交
93 94 95
 *
 */

96 97
/**
 * DOC: declare bitmap
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106
 * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
 * to declare an array named 'name' of just enough unsigned longs to
 * contain all bit positions from 0 to 'bits' - 1.
 */

/*
 * lib/bitmap.c provides these functions:
 */

107
extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
108
extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
L
Linus Torvalds 已提交
109
extern int __bitmap_equal(const unsigned long *bitmap1,
110
			  const unsigned long *bitmap2, unsigned int nbits);
L
Linus Torvalds 已提交
111
extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
112
			unsigned int nbits);
113 114
extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
				unsigned int shift, unsigned int nbits);
115 116
extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
				unsigned int shift, unsigned int nbits);
117
extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
118
			const unsigned long *bitmap2, unsigned int nbits);
L
Linus Torvalds 已提交
119
extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
120
			const unsigned long *bitmap2, unsigned int nbits);
L
Linus Torvalds 已提交
121
extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
122
			const unsigned long *bitmap2, unsigned int nbits);
123
extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
124
			const unsigned long *bitmap2, unsigned int nbits);
L
Linus Torvalds 已提交
125
extern int __bitmap_intersects(const unsigned long *bitmap1,
126
			const unsigned long *bitmap2, unsigned int nbits);
L
Linus Torvalds 已提交
127
extern int __bitmap_subset(const unsigned long *bitmap1,
128
			const unsigned long *bitmap2, unsigned int nbits);
129
extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
130 131
extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

extern unsigned long 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);

/**
 * bitmap_find_next_zero_area - find a contiguous aligned zero area
 * @map: The address to base the search on
 * @size: The bitmap size in bits
 * @start: The bitnumber to start searching at
 * @nr: The number of zeroed bits we're looking for
 * @align_mask: Alignment mask for zero area
 *
 * The @align_mask should be one less than a power of 2; the effect is that
 * the bit offset of all zero areas this function finds is multiples of that
 * power of 2. A @align_mask of 0 means no alignment is required.
 */
static inline unsigned long
bitmap_find_next_zero_area(unsigned long *map,
			   unsigned long size,
			   unsigned long start,
			   unsigned int nr,
			   unsigned long align_mask)
{
	return bitmap_find_next_zero_area_off(map, size, start, nr,
					      align_mask, 0);
}
162

163 164 165
extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
			unsigned long *dst, int nbits);
extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
L
Linus Torvalds 已提交
166 167 168
			unsigned long *dst, int nbits);
extern int bitmap_parselist(const char *buf, unsigned long *maskp,
			int nmaskbits);
169 170
extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
			unsigned long *dst, int nbits);
171
extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
172
		const unsigned long *old, const unsigned long *new, unsigned int nbits);
173 174
extern int bitmap_bitremap(int oldbit,
		const unsigned long *old, const unsigned long *new, int bits);
175
extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
176
		const unsigned long *relmap, unsigned int bits);
177
extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
178
		unsigned int sz, unsigned int nbits);
179 180 181
extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
182

183
#ifdef __BIG_ENDIAN
184
extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
185 186 187
#else
#define bitmap_copy_le bitmap_copy
#endif
188
extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
189 190
extern int bitmap_print_to_pagebuf(bool list, char *buf,
				   const unsigned long *maskp, int nmaskbits);
L
Linus Torvalds 已提交
191

192 193
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
L
Linus Torvalds 已提交
194

195 196 197
#define small_const_nbits(nbits) \
	(__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)

198
static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
L
Linus Torvalds 已提交
199
{
200
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
201 202
		*dst = 0UL;
	else {
203
		unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
L
Linus Torvalds 已提交
204 205 206 207
		memset(dst, 0, len);
	}
}

208
static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
L
Linus Torvalds 已提交
209
{
210 211 212 213 214
	if (small_const_nbits(nbits))
		*dst = ~0UL;
	else {
		unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
		memset(dst, 0xff, len);
L
Linus Torvalds 已提交
215 216 217 218
	}
}

static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
219
			unsigned int nbits)
L
Linus Torvalds 已提交
220
{
221
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
222 223
		*dst = *src;
	else {
224
		unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
L
Linus Torvalds 已提交
225 226 227 228
		memcpy(dst, src, len);
	}
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/*
 * Copy bitmap and clear tail bits in last word.
 */
static inline void bitmap_copy_clear_tail(unsigned long *dst,
		const unsigned long *src, unsigned int nbits)
{
	bitmap_copy(dst, src, nbits);
	if (nbits % BITS_PER_LONG)
		dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
}

/*
 * On 32-bit systems bitmaps are represented as u32 arrays internally, and
 * therefore conversion is not needed when copying data from/to arrays of u32.
 */
#if BITS_PER_LONG == 64
extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
							unsigned int nbits);
extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
							unsigned int nbits);
#else
#define bitmap_from_arr32(bitmap, buf, nbits)			\
	bitmap_copy_clear_tail((unsigned long *) (bitmap),	\
			(const unsigned long *) (buf), (nbits))
#define bitmap_to_arr32(buf, bitmap, nbits)			\
	bitmap_copy_clear_tail((unsigned long *) (buf),		\
			(const unsigned long *) (bitmap), (nbits))
#endif

258
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
259
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
260
{
261
	if (small_const_nbits(nbits))
262
		return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
263
	return __bitmap_and(dst, src1, src2, nbits);
L
Linus Torvalds 已提交
264 265 266
}

static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
267
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
268
{
269
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
270 271 272 273 274 275
		*dst = *src1 | *src2;
	else
		__bitmap_or(dst, src1, src2, nbits);
}

static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
276
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
277
{
278
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
279 280 281 282 283
		*dst = *src1 ^ *src2;
	else
		__bitmap_xor(dst, src1, src2, nbits);
}

284
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
285
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
286
{
287
	if (small_const_nbits(nbits))
288
		return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
289
	return __bitmap_andnot(dst, src1, src2, nbits);
L
Linus Torvalds 已提交
290 291 292
}

static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
293
			unsigned int nbits)
L
Linus Torvalds 已提交
294
{
295
	if (small_const_nbits(nbits))
296
		*dst = ~(*src);
L
Linus Torvalds 已提交
297 298 299 300 301
	else
		__bitmap_complement(dst, src, nbits);
}

static inline int bitmap_equal(const unsigned long *src1,
302
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
303
{
304
	if (small_const_nbits(nbits))
A
Andrew Morton 已提交
305
		return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
306
	if (__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
307
		return !memcmp(src1, src2, nbits / 8);
A
Andrew Morton 已提交
308
	return __bitmap_equal(src1, src2, nbits);
L
Linus Torvalds 已提交
309 310 311
}

static inline int bitmap_intersects(const unsigned long *src1,
312
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
313
{
314
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
315 316 317 318 319 320
		return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
	else
		return __bitmap_intersects(src1, src2, nbits);
}

static inline int bitmap_subset(const unsigned long *src1,
321
			const unsigned long *src2, unsigned int nbits)
L
Linus Torvalds 已提交
322
{
323
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
324 325 326 327 328
		return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
	else
		return __bitmap_subset(src1, src2, nbits);
}

329
static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
L
Linus Torvalds 已提交
330
{
331
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
332
		return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
333 334

	return find_first_bit(src, nbits) == nbits;
L
Linus Torvalds 已提交
335 336
}

337
static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
L
Linus Torvalds 已提交
338
{
339
	if (small_const_nbits(nbits))
L
Linus Torvalds 已提交
340
		return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
341 342

	return find_first_zero_bit(src, nbits) == nbits;
L
Linus Torvalds 已提交
343 344
}

345
static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
L
Linus Torvalds 已提交
346
{
347
	if (small_const_nbits(nbits))
348
		return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
L
Linus Torvalds 已提交
349 350 351
	return __bitmap_weight(src, nbits);
}

352 353 354 355 356
static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
		unsigned int nbits)
{
	if (__builtin_constant_p(nbits) && nbits == 1)
		__set_bit(start, map);
357 358 359
	else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
		 __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
		memset((char *)map + start / 8, 0xff, nbits / 8);
360 361 362 363 364 365 366 367 368
	else
		__bitmap_set(map, start, nbits);
}

static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
		unsigned int nbits)
{
	if (__builtin_constant_p(nbits) && nbits == 1)
		__clear_bit(start, map);
369 370 371
	else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
		 __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
		memset((char *)map + start / 8, 0, nbits / 8);
372 373 374 375
	else
		__bitmap_clear(map, start, nbits);
}

376 377
static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
				unsigned int shift, int nbits)
L
Linus Torvalds 已提交
378
{
379
	if (small_const_nbits(nbits))
380
		*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
L
Linus Torvalds 已提交
381
	else
382
		__bitmap_shift_right(dst, src, shift, nbits);
L
Linus Torvalds 已提交
383 384
}

385 386
static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
				unsigned int shift, unsigned int nbits)
L
Linus Torvalds 已提交
387
{
388
	if (small_const_nbits(nbits))
389
		*dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
L
Linus Torvalds 已提交
390
	else
391
		__bitmap_shift_left(dst, src, shift, nbits);
L
Linus Torvalds 已提交
392 393
}

394 395 396 397 398 399
static inline int bitmap_parse(const char *buf, unsigned int buflen,
			unsigned long *maskp, int nmaskbits)
{
	return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
}

400
/**
Y
Yury Norov 已提交
401
 * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
402
 * @n: u64 value
Y
Yury Norov 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
 *
 * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
 * integers in 32-bit environment, and 64-bit integers in 64-bit one.
 *
 * There are four combinations of endianness and length of the word in linux
 * ABIs: LE64, BE64, LE32 and BE32.
 *
 * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
 * bitmaps and therefore don't require any special handling.
 *
 * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
 * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
 * other hand is represented as an array of 32-bit words and the position of
 * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
 * word.  For example, bit #42 is located at 10th position of 2nd word.
 * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
 * values in memory as it usually does. But for BE we need to swap hi and lo
 * words manually.
 *
 * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
 * lo parts of u64.  For LE32 it does nothing, and for BE environment it swaps
 * hi and lo words, as is expected by bitmap.
 */
#if __BITS_PER_LONG == 64
#define BITMAP_FROM_U64(n) (n)
#else
#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
				((unsigned long) ((u64)(n) >> 32))
#endif

433
/**
434 435 436 437
 * bitmap_from_u64 - Check and swap words within u64.
 *  @mask: source bitmap
 *  @dst:  destination bitmap
 *
438
 * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
439
 * to read u64 mask, we will get the wrong word.
440
 * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
441 442 443 444 445 446 447 448 449 450
 * but we expect the lower 32-bits of u64.
 */
static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
{
	dst[0] = mask & ULONG_MAX;

	if (sizeof(mask) > sizeof(unsigned long))
		dst[1] = mask >> 32;
}

L
Linus Torvalds 已提交
451 452 453
#endif /* __ASSEMBLY__ */

#endif /* __LINUX_BITMAP_H */