blob: 78f70d9007adc74cb460fac397465088cab77413 [file] [log] [blame]
Thomas Gleixner40b0b3f2019-06-03 07:44:46 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * lib/bitmap.c
4 * Helper functions for bitmap.h.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Bartosz Golaszewski1238da52021-03-15 10:13:55 +01006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/bitmap.h>
8#include <linux/bitops.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -05009#include <linux/bug.h>
Bartosz Golaszewski1238da52021-03-15 10:13:55 +010010#include <linux/ctype.h>
11#include <linux/errno.h>
12#include <linux/export.h>
David Decotignye52bc7c2016-02-19 09:23:59 -050013#include <linux/kernel.h>
Rasmus Villemoesce1091d2018-10-30 15:05:14 -070014#include <linux/mm.h>
Andy Shevchenkoc42b65e2018-08-01 15:42:56 -070015#include <linux/slab.h>
David Decotignye52bc7c2016-02-19 09:23:59 -050016#include <linux/string.h>
Bartosz Golaszewski1238da52021-03-15 10:13:55 +010017#include <linux/thread_info.h>
Andy Lutomirski13d4ea02016-07-14 13:22:57 -070018#include <linux/uaccess.h>
Sudeep Holla5aaba362014-09-30 14:48:22 +010019
20#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Yury Norove371c482019-05-14 15:43:14 -070022#include "kstrtox.h"
23
Randy Dunlap7d7363e2017-10-16 16:32:51 -070024/**
25 * DOC: bitmap introduction
26 *
Randy Dunlap197d6c12020-10-15 20:10:45 -070027 * bitmaps provide an array of bits, implemented using an
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * array of unsigned longs. The number of valid bits in a
29 * given bitmap does _not_ need to be an exact multiple of
30 * BITS_PER_LONG.
31 *
32 * The possible unused bits in the last, partially used word
33 * of a bitmap are 'don't care'. The implementation makes
34 * no particular effort to keep them zero. It ensures that
35 * their value will not affect the results of any operation.
36 * The bitmap operations that return Boolean (bitmap_empty,
37 * for example) or scalar (bitmap_weight, for example) results
38 * carefully filter out these unused bits from impacting their
39 * results.
40 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * The byte ordering of bitmaps is more natural on little
42 * endian architectures. See the big-endian headers
43 * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
44 * for the best explanations of this ordering.
45 */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047int __bitmap_equal(const unsigned long *bitmap1,
Rasmus Villemoes5e0680692014-08-06 16:09:53 -070048 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Rasmus Villemoes5e0680692014-08-06 16:09:53 -070050 unsigned int k, lim = bits/BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 for (k = 0; k < lim; ++k)
52 if (bitmap1[k] != bitmap2[k])
53 return 0;
54
55 if (bits % BITS_PER_LONG)
56 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
57 return 0;
58
59 return 1;
60}
61EXPORT_SYMBOL(__bitmap_equal);
62
Thomas Gleixnerb9fa6442019-07-22 20:47:24 +020063bool __bitmap_or_equal(const unsigned long *bitmap1,
64 const unsigned long *bitmap2,
65 const unsigned long *bitmap3,
66 unsigned int bits)
67{
68 unsigned int k, lim = bits / BITS_PER_LONG;
69 unsigned long tmp;
70
71 for (k = 0; k < lim; ++k) {
72 if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
73 return false;
74 }
75
76 if (!(bits % BITS_PER_LONG))
77 return true;
78
79 tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
80 return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
81}
82
Rasmus Villemoes3d6684f2014-08-06 16:09:55 -070083void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Yury Norovca1250b2018-06-07 17:10:41 -070085 unsigned int k, lim = BITS_TO_LONGS(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 for (k = 0; k < lim; ++k)
87 dst[k] = ~src[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89EXPORT_SYMBOL(__bitmap_complement);
90
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080091/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * __bitmap_shift_right - logical right shift of the bits in a bitmap
Randy Dunlap05fb6bf2007-02-28 20:12:13 -080093 * @dst : destination bitmap
94 * @src : source bitmap
95 * @shift : shift by this many bits
Rasmus Villemoes2fbad292015-02-13 14:36:02 -080096 * @nbits : bitmap size, in bits
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * Shifting right (dividing) means moving bits in the MS -> LS bit
99 * direction. Zeros are fed into the vacated MS positions and the
100 * LS bits shifted off the bottom are lost.
101 */
Rasmus Villemoes2fbad292015-02-13 14:36:02 -0800102void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
103 unsigned shift, unsigned nbits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Rasmus Villemoescfac1d02015-02-13 14:36:10 -0800105 unsigned k, lim = BITS_TO_LONGS(nbits);
Rasmus Villemoes2fbad292015-02-13 14:36:02 -0800106 unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
Rasmus Villemoescfac1d02015-02-13 14:36:10 -0800107 unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 for (k = 0; off + k < lim; ++k) {
109 unsigned long upper, lower;
110
111 /*
112 * If shift is not word aligned, take lower rem bits of
113 * word above and make them the top rem bits of result.
114 */
115 if (!rem || off + k + 1 >= lim)
116 upper = 0;
117 else {
118 upper = src[off + k + 1];
Rasmus Villemoescfac1d02015-02-13 14:36:10 -0800119 if (off + k + 1 == lim - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 upper &= mask;
Rasmus Villemoes9d8a6b22015-02-13 14:36:05 -0800121 upper <<= (BITS_PER_LONG - rem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123 lower = src[off + k];
Rasmus Villemoescfac1d02015-02-13 14:36:10 -0800124 if (off + k == lim - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 lower &= mask;
Rasmus Villemoes9d8a6b22015-02-13 14:36:05 -0800126 lower >>= rem;
127 dst[k] = lower | upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 if (off)
130 memset(&dst[lim - off], 0, off*sizeof(unsigned long));
131}
132EXPORT_SYMBOL(__bitmap_shift_right);
133
134
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800135/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * __bitmap_shift_left - logical left shift of the bits in a bitmap
Randy Dunlap05fb6bf2007-02-28 20:12:13 -0800137 * @dst : destination bitmap
138 * @src : source bitmap
139 * @shift : shift by this many bits
Rasmus Villemoesdba94c22015-02-13 14:36:13 -0800140 * @nbits : bitmap size, in bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
142 * Shifting left (multiplying) means moving bits in the LS -> MS
143 * direction. Zeros are fed into the vacated LS bit positions
144 * and those MS bits shifted off the top are lost.
145 */
146
Rasmus Villemoesdba94c22015-02-13 14:36:13 -0800147void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
148 unsigned int shift, unsigned int nbits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Rasmus Villemoesdba94c22015-02-13 14:36:13 -0800150 int k;
Rasmus Villemoes7f590652015-02-13 14:36:19 -0800151 unsigned int lim = BITS_TO_LONGS(nbits);
Rasmus Villemoesdba94c22015-02-13 14:36:13 -0800152 unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 for (k = lim - off - 1; k >= 0; --k) {
154 unsigned long upper, lower;
155
156 /*
157 * If shift is not word aligned, take upper rem bits of
158 * word below and make them the bottom rem bits of result.
159 */
160 if (rem && k > 0)
Rasmus Villemoes6d874ec2015-02-13 14:36:16 -0800161 lower = src[k - 1] >> (BITS_PER_LONG - rem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 else
163 lower = 0;
Rasmus Villemoes7f590652015-02-13 14:36:19 -0800164 upper = src[k] << rem;
Rasmus Villemoes6d874ec2015-02-13 14:36:16 -0800165 dst[k + off] = lower | upper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 if (off)
168 memset(dst, 0, off*sizeof(unsigned long));
169}
170EXPORT_SYMBOL(__bitmap_shift_left);
171
Stefano Brivio20927672020-01-22 00:17:54 +0100172/**
173 * bitmap_cut() - remove bit region from bitmap and right shift remaining bits
174 * @dst: destination bitmap, might overlap with src
175 * @src: source bitmap
176 * @first: start bit of region to be removed
177 * @cut: number of bits to remove
178 * @nbits: bitmap size, in bits
179 *
180 * Set the n-th bit of @dst iff the n-th bit of @src is set and
181 * n is less than @first, or the m-th bit of @src is set for any
182 * m such that @first <= n < nbits, and m = n + @cut.
183 *
184 * In pictures, example for a big-endian 32-bit architecture:
185 *
Mauro Carvalho Chehab46422892020-04-14 18:48:59 +0200186 * The @src bitmap is::
Stefano Brivio20927672020-01-22 00:17:54 +0100187 *
Mauro Carvalho Chehab46422892020-04-14 18:48:59 +0200188 * 31 63
189 * | |
190 * 10000000 11000001 11110010 00010101 10000000 11000001 01110010 00010101
191 * | | | |
192 * 16 14 0 32
Stefano Brivio20927672020-01-22 00:17:54 +0100193 *
Mauro Carvalho Chehab46422892020-04-14 18:48:59 +0200194 * if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is::
195 *
196 * 31 63
197 * | |
198 * 10110000 00011000 00110010 00010101 00010000 00011000 00101110 01000010
199 * | | |
200 * 14 (bit 17 0 32
201 * from @src)
Stefano Brivio20927672020-01-22 00:17:54 +0100202 *
203 * Note that @dst and @src might overlap partially or entirely.
204 *
205 * This is implemented in the obvious way, with a shift and carry
206 * step for each moved bit. Optimisation is left as an exercise
207 * for the compiler.
208 */
209void bitmap_cut(unsigned long *dst, const unsigned long *src,
210 unsigned int first, unsigned int cut, unsigned int nbits)
211{
212 unsigned int len = BITS_TO_LONGS(nbits);
213 unsigned long keep = 0, carry;
214 int i;
215
Stefano Brivio20927672020-01-22 00:17:54 +0100216 if (first % BITS_PER_LONG) {
217 keep = src[first / BITS_PER_LONG] &
218 (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
219 }
220
Stefano Brivio5959f822020-08-11 18:34:29 -0700221 memmove(dst, src, len * sizeof(*dst));
222
Stefano Brivio20927672020-01-22 00:17:54 +0100223 while (cut--) {
224 for (i = first / BITS_PER_LONG; i < len; i++) {
225 if (i < len - 1)
226 carry = dst[i + 1] & 1UL;
227 else
228 carry = 0;
229
230 dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));
231 }
232 }
233
234 dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);
235 dst[first / BITS_PER_LONG] |= keep;
236}
237EXPORT_SYMBOL(bitmap_cut);
238
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700239int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700240 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700242 unsigned int k;
Rasmus Villemoes7e5f97d2014-08-06 16:10:22 -0700243 unsigned int lim = bits/BITS_PER_LONG;
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700244 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Rasmus Villemoes7e5f97d2014-08-06 16:10:22 -0700246 for (k = 0; k < lim; k++)
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700247 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
Rasmus Villemoes7e5f97d2014-08-06 16:10:22 -0700248 if (bits % BITS_PER_LONG)
249 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
250 BITMAP_LAST_WORD_MASK(bits));
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700251 return result != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253EXPORT_SYMBOL(__bitmap_and);
254
255void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700256 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700258 unsigned int k;
259 unsigned int nr = BITS_TO_LONGS(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 for (k = 0; k < nr; k++)
262 dst[k] = bitmap1[k] | bitmap2[k];
263}
264EXPORT_SYMBOL(__bitmap_or);
265
266void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700267 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700269 unsigned int k;
270 unsigned int nr = BITS_TO_LONGS(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 for (k = 0; k < nr; k++)
273 dst[k] = bitmap1[k] ^ bitmap2[k];
274}
275EXPORT_SYMBOL(__bitmap_xor);
276
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700277int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700278 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Rasmus Villemoes2f9305e2014-08-06 16:09:59 -0700280 unsigned int k;
Rasmus Villemoes74e76532014-08-06 16:10:24 -0700281 unsigned int lim = bits/BITS_PER_LONG;
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700282 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Rasmus Villemoes74e76532014-08-06 16:10:24 -0700284 for (k = 0; k < lim; k++)
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700285 result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
Rasmus Villemoes74e76532014-08-06 16:10:24 -0700286 if (bits % BITS_PER_LONG)
287 result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
288 BITMAP_LAST_WORD_MASK(bits));
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700289 return result != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291EXPORT_SYMBOL(__bitmap_andnot);
292
Andy Shevchenko30544ed2019-12-04 16:53:26 -0800293void __bitmap_replace(unsigned long *dst,
294 const unsigned long *old, const unsigned long *new,
295 const unsigned long *mask, unsigned int nbits)
296{
297 unsigned int k;
298 unsigned int nr = BITS_TO_LONGS(nbits);
299
300 for (k = 0; k < nr; k++)
301 dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);
302}
303EXPORT_SYMBOL(__bitmap_replace);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305int __bitmap_intersects(const unsigned long *bitmap1,
Rasmus Villemoes6dfe9792014-08-06 16:10:01 -0700306 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Rasmus Villemoes6dfe9792014-08-06 16:10:01 -0700308 unsigned int k, lim = bits/BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 for (k = 0; k < lim; ++k)
310 if (bitmap1[k] & bitmap2[k])
311 return 1;
312
313 if (bits % BITS_PER_LONG)
314 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
315 return 1;
316 return 0;
317}
318EXPORT_SYMBOL(__bitmap_intersects);
319
320int __bitmap_subset(const unsigned long *bitmap1,
Rasmus Villemoes5be20212014-08-06 16:10:03 -0700321 const unsigned long *bitmap2, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Rasmus Villemoes5be20212014-08-06 16:10:03 -0700323 unsigned int k, lim = bits/BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 for (k = 0; k < lim; ++k)
325 if (bitmap1[k] & ~bitmap2[k])
326 return 0;
327
328 if (bits % BITS_PER_LONG)
329 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
330 return 0;
331 return 1;
332}
333EXPORT_SYMBOL(__bitmap_subset);
334
Rasmus Villemoes877d9f32014-08-06 16:10:05 -0700335int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Rasmus Villemoes877d9f32014-08-06 16:10:05 -0700337 unsigned int k, lim = bits/BITS_PER_LONG;
338 int w = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 for (k = 0; k < lim; k++)
Akinobu Mita37d54112006-03-26 01:39:56 -0800341 w += hweight_long(bitmap[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (bits % BITS_PER_LONG)
Akinobu Mita37d54112006-03-26 01:39:56 -0800344 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return w;
347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348EXPORT_SYMBOL(__bitmap_weight);
349
Matthew Wilcoxe5af3232017-07-10 15:51:29 -0700350void __bitmap_set(unsigned long *map, unsigned int start, int len)
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800351{
352 unsigned long *p = map + BIT_WORD(start);
Rasmus Villemoesfb5ac542014-08-06 16:10:07 -0700353 const unsigned int size = start + len;
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800354 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
355 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
356
Rasmus Villemoesfb5ac542014-08-06 16:10:07 -0700357 while (len - bits_to_set >= 0) {
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800358 *p |= mask_to_set;
Rasmus Villemoesfb5ac542014-08-06 16:10:07 -0700359 len -= bits_to_set;
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800360 bits_to_set = BITS_PER_LONG;
361 mask_to_set = ~0UL;
362 p++;
363 }
Rasmus Villemoesfb5ac542014-08-06 16:10:07 -0700364 if (len) {
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800365 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
366 *p |= mask_to_set;
367 }
368}
Matthew Wilcoxe5af3232017-07-10 15:51:29 -0700369EXPORT_SYMBOL(__bitmap_set);
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800370
Matthew Wilcoxe5af3232017-07-10 15:51:29 -0700371void __bitmap_clear(unsigned long *map, unsigned int start, int len)
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800372{
373 unsigned long *p = map + BIT_WORD(start);
Rasmus Villemoes154f5e32014-08-06 16:10:10 -0700374 const unsigned int size = start + len;
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800375 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
376 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
377
Rasmus Villemoes154f5e32014-08-06 16:10:10 -0700378 while (len - bits_to_clear >= 0) {
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800379 *p &= ~mask_to_clear;
Rasmus Villemoes154f5e32014-08-06 16:10:10 -0700380 len -= bits_to_clear;
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800381 bits_to_clear = BITS_PER_LONG;
382 mask_to_clear = ~0UL;
383 p++;
384 }
Rasmus Villemoes154f5e32014-08-06 16:10:10 -0700385 if (len) {
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800386 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
387 *p &= ~mask_to_clear;
388 }
389}
Matthew Wilcoxe5af3232017-07-10 15:51:29 -0700390EXPORT_SYMBOL(__bitmap_clear);
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800391
Michal Nazarewicz5e19b012014-12-12 16:54:45 -0800392/**
393 * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800394 * @map: The address to base the search on
395 * @size: The bitmap size in bits
396 * @start: The bitnumber to start searching at
397 * @nr: The number of zeroed bits we're looking for
398 * @align_mask: Alignment mask for zero area
Michal Nazarewicz5e19b012014-12-12 16:54:45 -0800399 * @align_offset: Alignment offset for zero area.
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800400 *
401 * The @align_mask should be one less than a power of 2; the effect is that
Michal Nazarewicz5e19b012014-12-12 16:54:45 -0800402 * the bit offset of all zero areas this function finds plus @align_offset
403 * is multiple of that power of 2.
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800404 */
Michal Nazarewicz5e19b012014-12-12 16:54:45 -0800405unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
406 unsigned long size,
407 unsigned long start,
408 unsigned int nr,
409 unsigned long align_mask,
410 unsigned long align_offset)
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800411{
412 unsigned long index, end, i;
413again:
414 index = find_next_zero_bit(map, size, start);
415
416 /* Align allocation */
Michal Nazarewicz5e19b012014-12-12 16:54:45 -0800417 index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800418
419 end = index + nr;
420 if (end > size)
421 return end;
422 i = find_next_bit(map, end, index);
423 if (i < end) {
424 start = i + 1;
425 goto again;
426 }
427 return index;
428}
Michal Nazarewicz5e19b012014-12-12 16:54:45 -0800429EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
Akinobu Mitac1a2a962009-12-15 16:48:25 -0800430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +0100432 * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * second version by Paul Jackson, third by Joe Korty.
434 */
435
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700436/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800437 * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700438 *
439 * @ubuf: pointer to user buffer containing string.
440 * @ulen: buffer size in bytes. If string is smaller than this
441 * then it must be terminated with a \0.
442 * @maskp: pointer to bitmap array that will contain result.
443 * @nmaskbits: size of bitmap, in bits.
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700444 */
445int bitmap_parse_user(const char __user *ubuf,
446 unsigned int ulen, unsigned long *maskp,
447 int nmaskbits)
448{
Yury Norove66eda02020-02-03 17:37:31 -0800449 char *buf;
450 int ret;
H Hartley Sweetenb9c321f2011-10-31 17:12:32 -0700451
Yury Norove66eda02020-02-03 17:37:31 -0800452 buf = memdup_user_nul(ubuf, ulen);
453 if (IS_ERR(buf))
454 return PTR_ERR(buf);
455
Yury Norov2d626152020-02-03 17:37:34 -0800456 ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits);
Yury Norove66eda02020-02-03 17:37:31 -0800457
458 kfree(buf);
459 return ret;
Reinette Chatre01a3ee22006-10-11 01:21:55 -0700460}
461EXPORT_SYMBOL(bitmap_parse_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/**
Sudeep Holla5aaba362014-09-30 14:48:22 +0100464 * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
465 * @list: indicates whether the bitmap must be list
466 * @buf: page aligned buffer into which string is placed
467 * @maskp: pointer to bitmap to convert
468 * @nmaskbits: size of bitmap, in bits
469 *
470 * Output format is a comma-separated list of decimal numbers and
471 * ranges if list is specified or hex digits grouped into comma-separated
472 * sets of 8 digits/set. Returns the number of characters written to buf.
Sudeep Holla9cf79d12015-06-25 15:02:17 -0700473 *
Rasmus Villemoesce1091d2018-10-30 15:05:14 -0700474 * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
475 * area and that sufficient storage remains at @buf to accommodate the
476 * bitmap_print_to_pagebuf() output. Returns the number of characters
477 * actually printed to @buf, excluding terminating '\0'.
Sudeep Holla5aaba362014-09-30 14:48:22 +0100478 */
479int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
480 int nmaskbits)
481{
Rasmus Villemoesce1091d2018-10-30 15:05:14 -0700482 ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
Sudeep Holla5aaba362014-09-30 14:48:22 +0100483
Rasmus Villemoes8ec3d7682018-10-30 15:05:18 -0700484 return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
485 scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
Sudeep Holla5aaba362014-09-30 14:48:22 +0100486}
487EXPORT_SYMBOL(bitmap_print_to_pagebuf);
488
Yury Norove371c482019-05-14 15:43:14 -0700489/*
490 * Region 9-38:4/10 describes the following bitmap structure:
491 * 0 9 12 18 38
492 * .........****......****......****......
493 * ^ ^ ^ ^
494 * start off group_len end
495 */
496struct region {
497 unsigned int start;
498 unsigned int off;
499 unsigned int group_len;
500 unsigned int end;
501};
502
503static int bitmap_set_region(const struct region *r,
504 unsigned long *bitmap, int nbits)
505{
506 unsigned int start;
507
508 if (r->end >= nbits)
509 return -ERANGE;
510
511 for (start = r->start; start <= r->end; start += r->group_len)
512 bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
513
514 return 0;
515}
516
517static int bitmap_check_region(const struct region *r)
518{
519 if (r->start > r->end || r->group_len == 0 || r->off > r->group_len)
520 return -EINVAL;
521
522 return 0;
523}
524
525static const char *bitmap_getnum(const char *str, unsigned int *num)
526{
527 unsigned long long n;
528 unsigned int len;
529
530 len = _parse_integer(str, 10, &n);
531 if (!len)
532 return ERR_PTR(-EINVAL);
533 if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n)
534 return ERR_PTR(-EOVERFLOW);
535
536 *num = n;
537 return str + len;
538}
539
540static inline bool end_of_str(char c)
541{
542 return c == '\0' || c == '\n';
543}
544
545static inline bool __end_of_region(char c)
546{
547 return isspace(c) || c == ',';
548}
549
550static inline bool end_of_region(char c)
551{
552 return __end_of_region(c) || end_of_str(c);
553}
554
555/*
Randy Dunlap20607432020-03-30 17:22:11 -0700556 * The format allows commas and whitespaces at the beginning
Yury Norove371c482019-05-14 15:43:14 -0700557 * of the region.
558 */
559static const char *bitmap_find_region(const char *str)
560{
561 while (__end_of_region(*str))
562 str++;
563
564 return end_of_str(*str) ? NULL : str;
565}
566
Yury Norov2d626152020-02-03 17:37:34 -0800567static const char *bitmap_find_region_reverse(const char *start, const char *end)
568{
569 while (start <= end && __end_of_region(*end))
570 end--;
571
572 return end;
573}
574
Yury Norove371c482019-05-14 15:43:14 -0700575static const char *bitmap_parse_region(const char *str, struct region *r)
576{
577 str = bitmap_getnum(str, &r->start);
578 if (IS_ERR(str))
579 return str;
580
581 if (end_of_region(*str))
582 goto no_end;
583
584 if (*str != '-')
585 return ERR_PTR(-EINVAL);
586
587 str = bitmap_getnum(str + 1, &r->end);
588 if (IS_ERR(str))
589 return str;
590
591 if (end_of_region(*str))
592 goto no_pattern;
593
594 if (*str != ':')
595 return ERR_PTR(-EINVAL);
596
597 str = bitmap_getnum(str + 1, &r->off);
598 if (IS_ERR(str))
599 return str;
600
601 if (*str != '/')
602 return ERR_PTR(-EINVAL);
603
604 return bitmap_getnum(str + 1, &r->group_len);
605
606no_end:
607 r->end = r->start;
608no_pattern:
609 r->off = r->end + 1;
610 r->group_len = r->end + 1;
611
612 return end_of_str(*str) ? NULL : str;
613}
614
Sudeep Holla5aaba362014-09-30 14:48:22 +0100615/**
Yury Norove371c482019-05-14 15:43:14 -0700616 * bitmap_parselist - convert list format ASCII string to bitmap
617 * @buf: read user string from this buffer; must be terminated
618 * with a \0 or \n.
Randy Dunlap6e1907ff2006-06-25 05:48:57 -0700619 * @maskp: write resulting mask here
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * @nmaskbits: number of bits in mask to be written
621 *
622 * Input format is a comma-separated list of decimal numbers and
623 * ranges. Consecutively set bits are shown as two hyphen-separated
624 * decimal numbers, the smallest and largest bit numbers set in
625 * the range.
Noam Camus2d13e6c2016-10-11 13:51:35 -0700626 * Optionally each range can be postfixed to denote that only parts of it
627 * should be set. The range will divided to groups of specific size.
628 * From each group will be used only defined amount of bits.
629 * Syntax: range:used_size/group_size
630 * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -0300632 * Returns: 0 on success, -errno on invalid input strings. Error values:
633 *
Yury Norove371c482019-05-14 15:43:14 -0700634 * - ``-EINVAL``: wrong region format
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -0300635 * - ``-EINVAL``: invalid character in string
636 * - ``-ERANGE``: bit number specified too large for mask
Yury Norove371c482019-05-14 15:43:14 -0700637 * - ``-EOVERFLOW``: integer overflow in the input parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Yury Norove371c482019-05-14 15:43:14 -0700639int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Yury Norove371c482019-05-14 15:43:14 -0700641 struct region r;
642 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 bitmap_zero(maskp, nmaskbits);
Mike Travis4b0604202011-05-24 17:13:12 -0700645
Yury Norove371c482019-05-14 15:43:14 -0700646 while (buf) {
647 buf = bitmap_find_region(buf);
648 if (buf == NULL)
649 return 0;
Mike Travis4b0604202011-05-24 17:13:12 -0700650
Yury Norove371c482019-05-14 15:43:14 -0700651 buf = bitmap_parse_region(buf, &r);
652 if (IS_ERR(buf))
653 return PTR_ERR(buf);
Mike Travis4b0604202011-05-24 17:13:12 -0700654
Yury Norove371c482019-05-14 15:43:14 -0700655 ret = bitmap_check_region(&r);
656 if (ret)
657 return ret;
Noam Camus2d13e6c2016-10-11 13:51:35 -0700658
Yury Norove371c482019-05-14 15:43:14 -0700659 ret = bitmap_set_region(&r, maskp, nmaskbits);
660 if (ret)
661 return ret;
662 }
Noam Camus2d13e6c2016-10-11 13:51:35 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
666EXPORT_SYMBOL(bitmap_parselist);
667
Mike Travis4b0604202011-05-24 17:13:12 -0700668
669/**
670 * bitmap_parselist_user()
671 *
672 * @ubuf: pointer to user buffer containing string.
673 * @ulen: buffer size in bytes. If string is smaller than this
674 * then it must be terminated with a \0.
675 * @maskp: pointer to bitmap array that will contain result.
676 * @nmaskbits: size of bitmap, in bits.
677 *
678 * Wrapper for bitmap_parselist(), providing it with user buffer.
Mike Travis4b0604202011-05-24 17:13:12 -0700679 */
680int bitmap_parselist_user(const char __user *ubuf,
681 unsigned int ulen, unsigned long *maskp,
682 int nmaskbits)
683{
Yury Norov281327c2019-05-14 15:43:11 -0700684 char *buf;
685 int ret;
686
687 buf = memdup_user_nul(ubuf, ulen);
688 if (IS_ERR(buf))
689 return PTR_ERR(buf);
690
691 ret = bitmap_parselist(buf, maskp, nmaskbits);
692
693 kfree(buf);
694 return ret;
Mike Travis4b0604202011-05-24 17:13:12 -0700695}
696EXPORT_SYMBOL(bitmap_parselist_user);
697
Yury Norov2d626152020-02-03 17:37:34 -0800698static const char *bitmap_get_x32_reverse(const char *start,
699 const char *end, u32 *num)
700{
701 u32 ret = 0;
702 int c, i;
703
704 for (i = 0; i < 32; i += 4) {
705 c = hex_to_bin(*end--);
706 if (c < 0)
707 return ERR_PTR(-EINVAL);
708
709 ret |= c << i;
710
711 if (start > end || __end_of_region(*end))
712 goto out;
713 }
714
715 if (hex_to_bin(*end--) >= 0)
716 return ERR_PTR(-EOVERFLOW);
717out:
718 *num = ret;
719 return end;
720}
721
722/**
723 * bitmap_parse - convert an ASCII hex string into a bitmap.
724 * @start: pointer to buffer containing string.
725 * @buflen: buffer size in bytes. If string is smaller than this
726 * then it must be terminated with a \0 or \n. In that case,
727 * UINT_MAX may be provided instead of string length.
728 * @maskp: pointer to bitmap array that will contain result.
729 * @nmaskbits: size of bitmap, in bits.
730 *
731 * Commas group hex digits into chunks. Each chunk defines exactly 32
732 * bits of the resultant bitmask. No chunk may specify a value larger
733 * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
734 * then leading 0-bits are prepended. %-EINVAL is returned for illegal
735 * characters. Grouping such as "1,,5", ",44", "," or "" is allowed.
736 * Leading, embedded and trailing whitespace accepted.
737 */
738int bitmap_parse(const char *start, unsigned int buflen,
739 unsigned long *maskp, int nmaskbits)
740{
741 const char *end = strnchrnul(start, buflen, '\n') - 1;
742 int chunks = BITS_TO_U32(nmaskbits);
743 u32 *bitmap = (u32 *)maskp;
744 int unset_bit;
Alexander Gordeev81c4f4d2020-06-10 18:41:41 -0700745 int chunk;
Yury Norov2d626152020-02-03 17:37:34 -0800746
Alexander Gordeev81c4f4d2020-06-10 18:41:41 -0700747 for (chunk = 0; ; chunk++) {
Yury Norov2d626152020-02-03 17:37:34 -0800748 end = bitmap_find_region_reverse(start, end);
749 if (start > end)
750 break;
751
752 if (!chunks--)
753 return -EOVERFLOW;
754
Alexander Gordeev81c4f4d2020-06-10 18:41:41 -0700755#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
756 end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]);
757#else
758 end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]);
759#endif
Yury Norov2d626152020-02-03 17:37:34 -0800760 if (IS_ERR(end))
761 return PTR_ERR(end);
762 }
763
764 unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32;
765 if (unset_bit < nmaskbits) {
766 bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit);
767 return 0;
768 }
769
770 if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit)
771 return -EOVERFLOW;
772
773 return 0;
774}
775EXPORT_SYMBOL(bitmap_parse);
776
Mike Travis4b0604202011-05-24 17:13:12 -0700777
Rasmus Villemoescdc90a12019-05-14 15:42:43 -0700778#ifdef CONFIG_NUMA
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800779/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800780 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800781 * @buf: pointer to a bitmap
Rasmus Villemoesdf1d80a92015-02-12 15:02:07 -0800782 * @pos: a bit position in @buf (0 <= @pos < @nbits)
783 * @nbits: number of valid bit positions in @buf
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800784 *
Rasmus Villemoesdf1d80a92015-02-12 15:02:07 -0800785 * Map the bit at position @pos in @buf (of length @nbits) to the
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800786 * ordinal of which set bit it is. If it is not set or if @pos
Paul Jackson96b7f342006-01-08 01:01:46 -0800787 * is not a valid bit position, map to -1.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800788 *
789 * If for example, just bits 4 through 7 are set in @buf, then @pos
790 * values 4 through 7 will get mapped to 0 through 3, respectively,
Rasmus Villemoesa8551742014-08-06 16:10:14 -0700791 * and other @pos values will get mapped to -1. When @pos value 7
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800792 * gets mapped to (returns) @ord value 3 in this example, that means
793 * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
794 *
795 * The bit positions 0 through @bits are valid positions in @buf.
796 */
Rasmus Villemoesdf1d80a92015-02-12 15:02:07 -0800797static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800798{
Rasmus Villemoesdf1d80a92015-02-12 15:02:07 -0800799 if (pos >= nbits || !test_bit(pos, buf))
Paul Jackson96b7f342006-01-08 01:01:46 -0800800 return -1;
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800801
Rasmus Villemoesdf1d80a92015-02-12 15:02:07 -0800802 return __bitmap_weight(buf, pos);
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800803}
804
805/**
Ben Hutchings9a86e2b2010-03-05 13:43:17 -0800806 * bitmap_ord_to_pos - find position of n-th set bit in bitmap
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800807 * @buf: pointer to bitmap
808 * @ord: ordinal bit position (n-th set bit, n >= 0)
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800809 * @nbits: number of valid bit positions in @buf
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800810 *
811 * Map the ordinal offset of bit @ord in @buf to its position in @buf.
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800812 * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
813 * >= weight(buf), returns @nbits.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800814 *
815 * If for example, just bits 4 through 7 are set in @buf, then @ord
816 * values 0 through 3 will get mapped to 4 through 7, respectively,
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800817 * and all other @ord values returns @nbits. When @ord value 3
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800818 * gets mapped to (returns) @pos value 7 in this example, that means
819 * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
820 *
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800821 * The bit positions 0 through @nbits-1 are valid positions in @buf.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800822 */
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800823unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800824{
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800825 unsigned int pos;
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800826
Rasmus Villemoesf6a1f5d2015-02-12 15:02:10 -0800827 for (pos = find_first_bit(buf, nbits);
828 pos < nbits && ord;
829 pos = find_next_bit(buf, nbits, pos + 1))
830 ord--;
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800831
832 return pos;
833}
834
835/**
836 * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800837 * @dst: remapped result
Paul Jackson96b7f342006-01-08 01:01:46 -0800838 * @src: subset to be remapped
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800839 * @old: defines domain of map
840 * @new: defines range of map
Rasmus Villemoes9814ec12015-02-12 15:02:13 -0800841 * @nbits: number of bits in each of these bitmaps
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800842 *
843 * Let @old and @new define a mapping of bit positions, such that
844 * whatever position is held by the n-th set bit in @old is mapped
845 * to the n-th set bit in @new. In the more general case, allowing
846 * for the possibility that the weight 'w' of @new is less than the
847 * weight of @old, map the position of the n-th set bit in @old to
848 * the position of the m-th set bit in @new, where m == n % w.
849 *
Paul Jackson96b7f342006-01-08 01:01:46 -0800850 * If either of the @old and @new bitmaps are empty, or if @src and
851 * @dst point to the same location, then this routine copies @src
852 * to @dst.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800853 *
Paul Jackson96b7f342006-01-08 01:01:46 -0800854 * The positions of unset bits in @old are mapped to themselves
855 * (the identify map).
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800856 *
857 * Apply the above specified mapping to @src, placing the result in
858 * @dst, clearing any bits previously set in @dst.
859 *
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800860 * For example, lets say that @old has bits 4 through 7 set, and
861 * @new has bits 12 through 15 set. This defines the mapping of bit
862 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
Paul Jackson96b7f342006-01-08 01:01:46 -0800863 * bit positions unchanged. So if say @src comes into this routine
864 * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
865 * 13 and 15 set.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800866 */
867void bitmap_remap(unsigned long *dst, const unsigned long *src,
868 const unsigned long *old, const unsigned long *new,
Rasmus Villemoes9814ec12015-02-12 15:02:13 -0800869 unsigned int nbits)
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800870{
Rasmus Villemoes9814ec12015-02-12 15:02:13 -0800871 unsigned int oldbit, w;
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800872
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800873 if (dst == src) /* following doesn't handle inplace remaps */
874 return;
Rasmus Villemoes9814ec12015-02-12 15:02:13 -0800875 bitmap_zero(dst, nbits);
Paul Jackson96b7f342006-01-08 01:01:46 -0800876
Rasmus Villemoes9814ec12015-02-12 15:02:13 -0800877 w = bitmap_weight(new, nbits);
878 for_each_set_bit(oldbit, src, nbits) {
879 int n = bitmap_pos_to_ord(old, oldbit, nbits);
Akinobu Mita08564fb2010-03-05 13:43:18 -0800880
Paul Jackson96b7f342006-01-08 01:01:46 -0800881 if (n < 0 || w == 0)
882 set_bit(oldbit, dst); /* identity map */
883 else
Rasmus Villemoes9814ec12015-02-12 15:02:13 -0800884 set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800885 }
886}
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800887
888/**
889 * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
Randy Dunlap6e1907ff2006-06-25 05:48:57 -0700890 * @oldbit: bit position to be mapped
891 * @old: defines domain of map
892 * @new: defines range of map
893 * @bits: number of bits in each of these bitmaps
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800894 *
895 * Let @old and @new define a mapping of bit positions, such that
896 * whatever position is held by the n-th set bit in @old is mapped
897 * to the n-th set bit in @new. In the more general case, allowing
898 * for the possibility that the weight 'w' of @new is less than the
899 * weight of @old, map the position of the n-th set bit in @old to
900 * the position of the m-th set bit in @new, where m == n % w.
901 *
Paul Jackson96b7f342006-01-08 01:01:46 -0800902 * The positions of unset bits in @old are mapped to themselves
903 * (the identify map).
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800904 *
905 * Apply the above specified mapping to bit position @oldbit, returning
906 * the new bit position.
907 *
908 * For example, lets say that @old has bits 4 through 7 set, and
909 * @new has bits 12 through 15 set. This defines the mapping of bit
910 * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
Paul Jackson96b7f342006-01-08 01:01:46 -0800911 * bit positions unchanged. So if say @oldbit is 5, then this routine
912 * returns 13.
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800913 */
914int bitmap_bitremap(int oldbit, const unsigned long *old,
915 const unsigned long *new, int bits)
916{
Paul Jackson96b7f342006-01-08 01:01:46 -0800917 int w = bitmap_weight(new, bits);
918 int n = bitmap_pos_to_ord(old, oldbit, bits);
919 if (n < 0 || w == 0)
920 return oldbit;
921 else
922 return bitmap_ord_to_pos(new, n % w, bits);
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800923}
Paul Jacksonfb5eeee2005-10-30 15:02:33 -0800924
Paul Jackson7ea931c2008-04-28 02:12:29 -0700925/**
926 * bitmap_onto - translate one bitmap relative to another
927 * @dst: resulting translated bitmap
928 * @orig: original untranslated bitmap
929 * @relmap: bitmap relative to which translated
930 * @bits: number of bits in each of these bitmaps
931 *
932 * Set the n-th bit of @dst iff there exists some m such that the
933 * n-th bit of @relmap is set, the m-th bit of @orig is set, and
934 * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
935 * (If you understood the previous sentence the first time your
936 * read it, you're overqualified for your current job.)
937 *
938 * In other words, @orig is mapped onto (surjectively) @dst,
Masanari Iidada3dae52014-09-09 01:27:23 +0900939 * using the map { <n, m> | the n-th bit of @relmap is the
Paul Jackson7ea931c2008-04-28 02:12:29 -0700940 * m-th set bit of @relmap }.
941 *
942 * Any set bits in @orig above bit number W, where W is the
943 * weight of (number of set bits in) @relmap are mapped nowhere.
944 * In particular, if for all bits m set in @orig, m >= W, then
945 * @dst will end up empty. In situations where the possibility
946 * of such an empty result is not desired, one way to avoid it is
947 * to use the bitmap_fold() operator, below, to first fold the
948 * @orig bitmap over itself so that all its set bits x are in the
949 * range 0 <= x < W. The bitmap_fold() operator does this by
950 * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
951 *
952 * Example [1] for bitmap_onto():
953 * Let's say @relmap has bits 30-39 set, and @orig has bits
954 * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine,
955 * @dst will have bits 31, 33, 35, 37 and 39 set.
956 *
957 * When bit 0 is set in @orig, it means turn on the bit in
958 * @dst corresponding to whatever is the first bit (if any)
959 * that is turned on in @relmap. Since bit 0 was off in the
960 * above example, we leave off that bit (bit 30) in @dst.
961 *
962 * When bit 1 is set in @orig (as in the above example), it
963 * means turn on the bit in @dst corresponding to whatever
964 * is the second bit that is turned on in @relmap. The second
965 * bit in @relmap that was turned on in the above example was
966 * bit 31, so we turned on bit 31 in @dst.
967 *
968 * Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
969 * because they were the 4th, 6th, 8th and 10th set bits
970 * set in @relmap, and the 4th, 6th, 8th and 10th bits of
971 * @orig (i.e. bits 3, 5, 7 and 9) were also set.
972 *
973 * When bit 11 is set in @orig, it means turn on the bit in
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300974 * @dst corresponding to whatever is the twelfth bit that is
Paul Jackson7ea931c2008-04-28 02:12:29 -0700975 * turned on in @relmap. In the above example, there were
976 * only ten bits turned on in @relmap (30..39), so that bit
977 * 11 was set in @orig had no affect on @dst.
978 *
979 * Example [2] for bitmap_fold() + bitmap_onto():
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -0300980 * Let's say @relmap has these ten bits set::
981 *
Paul Jackson7ea931c2008-04-28 02:12:29 -0700982 * 40 41 42 43 45 48 53 61 74 95
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -0300983 *
Paul Jackson7ea931c2008-04-28 02:12:29 -0700984 * (for the curious, that's 40 plus the first ten terms of the
985 * Fibonacci sequence.)
986 *
987 * Further lets say we use the following code, invoking
988 * bitmap_fold() then bitmap_onto, as suggested above to
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -0300989 * avoid the possibility of an empty @dst result::
Paul Jackson7ea931c2008-04-28 02:12:29 -0700990 *
991 * unsigned long *tmp; // a temporary bitmap's bits
992 *
993 * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
994 * bitmap_onto(dst, tmp, relmap, bits);
995 *
996 * Then this table shows what various values of @dst would be, for
997 * various @orig's. I list the zero-based positions of each set bit.
998 * The tmp column shows the intermediate result, as computed by
999 * using bitmap_fold() to fold the @orig bitmap modulo ten
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -03001000 * (the weight of @relmap):
Paul Jackson7ea931c2008-04-28 02:12:29 -07001001 *
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -03001002 * =============== ============== =================
Paul Jackson7ea931c2008-04-28 02:12:29 -07001003 * @orig tmp @dst
1004 * 0 0 40
1005 * 1 1 41
1006 * 9 9 95
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -03001007 * 10 0 40 [#f1]_
Paul Jackson7ea931c2008-04-28 02:12:29 -07001008 * 1 3 5 7 1 3 5 7 41 43 48 61
1009 * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45
1010 * 0 9 18 27 0 9 8 7 40 61 74 95
1011 * 0 10 20 30 0 40
1012 * 0 11 22 33 0 1 2 3 40 41 42 43
1013 * 0 12 24 36 0 2 4 6 40 42 45 53
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -03001014 * 78 102 211 1 2 8 41 42 74 [#f1]_
1015 * =============== ============== =================
Paul Jackson7ea931c2008-04-28 02:12:29 -07001016 *
mchehab@s-opensource.com40bf19a2017-03-30 17:11:35 -03001017 * .. [#f1]
1018 *
1019 * For these marked lines, if we hadn't first done bitmap_fold()
Paul Jackson7ea931c2008-04-28 02:12:29 -07001020 * into tmp, then the @dst result would have been empty.
1021 *
1022 * If either of @orig or @relmap is empty (no set bits), then @dst
1023 * will be returned empty.
1024 *
1025 * If (as explained above) the only set bits in @orig are in positions
1026 * m where m >= W, (where W is the weight of @relmap) then @dst will
1027 * once again be returned empty.
1028 *
1029 * All bits in @dst not set by the above rule are cleared.
1030 */
1031void bitmap_onto(unsigned long *dst, const unsigned long *orig,
Rasmus Villemoeseb569882015-02-12 15:02:01 -08001032 const unsigned long *relmap, unsigned int bits)
Paul Jackson7ea931c2008-04-28 02:12:29 -07001033{
Rasmus Villemoeseb569882015-02-12 15:02:01 -08001034 unsigned int n, m; /* same meaning as in above comment */
Paul Jackson7ea931c2008-04-28 02:12:29 -07001035
1036 if (dst == orig) /* following doesn't handle inplace mappings */
1037 return;
1038 bitmap_zero(dst, bits);
1039
1040 /*
1041 * The following code is a more efficient, but less
1042 * obvious, equivalent to the loop:
1043 * for (m = 0; m < bitmap_weight(relmap, bits); m++) {
1044 * n = bitmap_ord_to_pos(orig, m, bits);
1045 * if (test_bit(m, orig))
1046 * set_bit(n, dst);
1047 * }
1048 */
1049
1050 m = 0;
Akinobu Mita08564fb2010-03-05 13:43:18 -08001051 for_each_set_bit(n, relmap, bits) {
Paul Jackson7ea931c2008-04-28 02:12:29 -07001052 /* m == bitmap_pos_to_ord(relmap, n, bits) */
1053 if (test_bit(m, orig))
1054 set_bit(n, dst);
1055 m++;
1056 }
1057}
Paul Jackson7ea931c2008-04-28 02:12:29 -07001058
1059/**
1060 * bitmap_fold - fold larger bitmap into smaller, modulo specified size
1061 * @dst: resulting smaller bitmap
1062 * @orig: original larger bitmap
1063 * @sz: specified size
Rasmus Villemoesb26ad582015-02-12 15:02:04 -08001064 * @nbits: number of bits in each of these bitmaps
Paul Jackson7ea931c2008-04-28 02:12:29 -07001065 *
1066 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
1067 * Clear all other bits in @dst. See further the comment and
1068 * Example [2] for bitmap_onto() for why and how to use this.
1069 */
1070void bitmap_fold(unsigned long *dst, const unsigned long *orig,
Rasmus Villemoesb26ad582015-02-12 15:02:04 -08001071 unsigned int sz, unsigned int nbits)
Paul Jackson7ea931c2008-04-28 02:12:29 -07001072{
Rasmus Villemoesb26ad582015-02-12 15:02:04 -08001073 unsigned int oldbit;
Paul Jackson7ea931c2008-04-28 02:12:29 -07001074
1075 if (dst == orig) /* following doesn't handle inplace mappings */
1076 return;
Rasmus Villemoesb26ad582015-02-12 15:02:04 -08001077 bitmap_zero(dst, nbits);
Paul Jackson7ea931c2008-04-28 02:12:29 -07001078
Rasmus Villemoesb26ad582015-02-12 15:02:04 -08001079 for_each_set_bit(oldbit, orig, nbits)
Paul Jackson7ea931c2008-04-28 02:12:29 -07001080 set_bit(oldbit % sz, dst);
1081}
Rasmus Villemoescdc90a12019-05-14 15:42:43 -07001082#endif /* CONFIG_NUMA */
Paul Jackson7ea931c2008-04-28 02:12:29 -07001083
Paul Jackson3cf64b92006-03-24 03:15:46 -08001084/*
1085 * Common code for bitmap_*_region() routines.
1086 * bitmap: array of unsigned longs corresponding to the bitmap
1087 * pos: the beginning of the region
1088 * order: region size (log base 2 of number of bits)
1089 * reg_op: operation(s) to perform on that region of bitmap
1090 *
1091 * Can set, verify and/or release a region of bits in a bitmap,
1092 * depending on which combination of REG_OP_* flag bits is set.
1093 *
1094 * A region of a bitmap is a sequence of bits in the bitmap, of
1095 * some size '1 << order' (a power of two), aligned to that same
1096 * '1 << order' power of two.
1097 *
1098 * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
1099 * Returns 0 in all other cases and reg_ops.
1100 */
1101
1102enum {
1103 REG_OP_ISFREE, /* true if region is all zero bits */
1104 REG_OP_ALLOC, /* set all bits in region */
1105 REG_OP_RELEASE, /* clear all bits in region */
1106};
1107
Rasmus Villemoes9279d322014-08-06 16:10:16 -07001108static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
Paul Jackson3cf64b92006-03-24 03:15:46 -08001109{
1110 int nbits_reg; /* number of bits in region */
1111 int index; /* index first long of region in bitmap */
1112 int offset; /* bit offset region in bitmap[index] */
1113 int nlongs_reg; /* num longs spanned by region in bitmap */
1114 int nbitsinlong; /* num bits of region in each spanned long */
1115 unsigned long mask; /* bitmask for one long of region */
1116 int i; /* scans bitmap by longs */
1117 int ret = 0; /* return value */
1118
1119 /*
1120 * Either nlongs_reg == 1 (for small orders that fit in one long)
1121 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
1122 */
1123 nbits_reg = 1 << order;
1124 index = pos / BITS_PER_LONG;
1125 offset = pos - (index * BITS_PER_LONG);
1126 nlongs_reg = BITS_TO_LONGS(nbits_reg);
1127 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
1128
1129 /*
1130 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
1131 * overflows if nbitsinlong == BITS_PER_LONG.
1132 */
1133 mask = (1UL << (nbitsinlong - 1));
1134 mask += mask - 1;
1135 mask <<= offset;
1136
1137 switch (reg_op) {
1138 case REG_OP_ISFREE:
1139 for (i = 0; i < nlongs_reg; i++) {
1140 if (bitmap[index + i] & mask)
1141 goto done;
1142 }
1143 ret = 1; /* all bits in region free (zero) */
1144 break;
1145
1146 case REG_OP_ALLOC:
1147 for (i = 0; i < nlongs_reg; i++)
1148 bitmap[index + i] |= mask;
1149 break;
1150
1151 case REG_OP_RELEASE:
1152 for (i = 0; i < nlongs_reg; i++)
1153 bitmap[index + i] &= ~mask;
1154 break;
1155 }
1156done:
1157 return ret;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/**
Paul Jackson87e24802006-03-24 03:15:44 -08001161 * bitmap_find_free_region - find a contiguous aligned mem region
Paul Jackson3cf64b92006-03-24 03:15:46 -08001162 * @bitmap: array of unsigned longs corresponding to the bitmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * @bits: number of bits in the bitmap
Paul Jackson3cf64b92006-03-24 03:15:46 -08001164 * @order: region size (log base 2 of number of bits) to find
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *
Paul Jackson3cf64b92006-03-24 03:15:46 -08001166 * Find a region of free (zero) bits in a @bitmap of @bits bits and
1167 * allocate them (set them to one). Only consider regions of length
1168 * a power (@order) of two, aligned to that power of two, which
Paul Jackson87e24802006-03-24 03:15:44 -08001169 * makes the search algorithm much faster.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 *
Paul Jackson3cf64b92006-03-24 03:15:46 -08001171 * Return the bit offset in bitmap of the allocated region,
Paul Jackson87e24802006-03-24 03:15:44 -08001172 * or -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
Rasmus Villemoes9279d322014-08-06 16:10:16 -07001174int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Rasmus Villemoes9279d322014-08-06 16:10:16 -07001176 unsigned int pos, end; /* scans bitmap by regions of size order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Rasmus Villemoes9279d322014-08-06 16:10:16 -07001178 for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
Linus Torvaldsaa8e4fc2009-03-12 19:32:51 -07001179 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1180 continue;
1181 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1182 return pos;
1183 }
1184 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186EXPORT_SYMBOL(bitmap_find_free_region);
1187
1188/**
Paul Jackson87e24802006-03-24 03:15:44 -08001189 * bitmap_release_region - release allocated bitmap region
Paul Jackson3cf64b92006-03-24 03:15:46 -08001190 * @bitmap: array of unsigned longs corresponding to the bitmap
1191 * @pos: beginning of bit region to release
1192 * @order: region size (log base 2 of number of bits) to release
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001194 * This is the complement to __bitmap_find_free_region() and releases
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * the found region (by clearing it in the bitmap).
Paul Jackson3cf64b92006-03-24 03:15:46 -08001196 *
1197 * No return value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Rasmus Villemoes9279d322014-08-06 16:10:16 -07001199void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Paul Jackson3cf64b92006-03-24 03:15:46 -08001201 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203EXPORT_SYMBOL(bitmap_release_region);
1204
Paul Jackson87e24802006-03-24 03:15:44 -08001205/**
1206 * bitmap_allocate_region - allocate bitmap region
Paul Jackson3cf64b92006-03-24 03:15:46 -08001207 * @bitmap: array of unsigned longs corresponding to the bitmap
1208 * @pos: beginning of bit region to allocate
1209 * @order: region size (log base 2 of number of bits) to allocate
Paul Jackson87e24802006-03-24 03:15:44 -08001210 *
1211 * Allocate (set bits in) a specified region of a bitmap.
Paul Jackson3cf64b92006-03-24 03:15:46 -08001212 *
Randy Dunlap6e1907ff2006-06-25 05:48:57 -07001213 * Return 0 on success, or %-EBUSY if specified region wasn't
Paul Jackson87e24802006-03-24 03:15:44 -08001214 * free (not all bits were zero).
1215 */
Rasmus Villemoes9279d322014-08-06 16:10:16 -07001216int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Paul Jackson3cf64b92006-03-24 03:15:46 -08001218 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1219 return -EBUSY;
Rasmus Villemoes2ac521d2014-08-06 16:10:18 -07001220 return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222EXPORT_SYMBOL(bitmap_allocate_region);
David Vrabelccbe3292008-09-17 16:34:03 +01001223
1224/**
1225 * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
1226 * @dst: destination buffer
1227 * @src: bitmap to copy
1228 * @nbits: number of bits in the bitmap
1229 *
1230 * Require nbits % BITS_PER_LONG == 0.
1231 */
Rasmus Villemoese8f24272015-02-13 14:36:00 -08001232#ifdef __BIG_ENDIAN
Rasmus Villemoes9b6c2d22015-02-13 14:35:57 -08001233void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
David Vrabelccbe3292008-09-17 16:34:03 +01001234{
Rasmus Villemoes9b6c2d22015-02-13 14:35:57 -08001235 unsigned int i;
David Vrabelccbe3292008-09-17 16:34:03 +01001236
1237 for (i = 0; i < nbits/BITS_PER_LONG; i++) {
1238 if (BITS_PER_LONG == 64)
Rasmus Villemoes9b6c2d22015-02-13 14:35:57 -08001239 dst[i] = cpu_to_le64(src[i]);
David Vrabelccbe3292008-09-17 16:34:03 +01001240 else
Rasmus Villemoes9b6c2d22015-02-13 14:35:57 -08001241 dst[i] = cpu_to_le32(src[i]);
David Vrabelccbe3292008-09-17 16:34:03 +01001242 }
1243}
1244EXPORT_SYMBOL(bitmap_copy_le);
Rasmus Villemoese8f24272015-02-13 14:36:00 -08001245#endif
Yury Norovc724f192018-02-06 15:38:02 -08001246
Andy Shevchenkoc42b65e2018-08-01 15:42:56 -07001247unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
1248{
1249 return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
1250 flags);
1251}
1252EXPORT_SYMBOL(bitmap_alloc);
1253
1254unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
1255{
1256 return bitmap_alloc(nbits, flags | __GFP_ZERO);
1257}
1258EXPORT_SYMBOL(bitmap_zalloc);
1259
1260void bitmap_free(const unsigned long *bitmap)
1261{
1262 kfree(bitmap);
1263}
1264EXPORT_SYMBOL(bitmap_free);
1265
Yury Norovc724f192018-02-06 15:38:02 -08001266#if BITS_PER_LONG == 64
1267/**
1268 * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
1269 * @bitmap: array of unsigned longs, the destination bitmap
1270 * @buf: array of u32 (in host byte order), the source bitmap
1271 * @nbits: number of bits in @bitmap
1272 */
Andy Shevchenkoccf7a6d2018-08-21 21:56:59 -07001273void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
Yury Norovc724f192018-02-06 15:38:02 -08001274{
1275 unsigned int i, halfwords;
1276
Yury Norovc724f192018-02-06 15:38:02 -08001277 halfwords = DIV_ROUND_UP(nbits, 32);
1278 for (i = 0; i < halfwords; i++) {
1279 bitmap[i/2] = (unsigned long) buf[i];
1280 if (++i < halfwords)
1281 bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
1282 }
1283
1284 /* Clear tail bits in last word beyond nbits. */
1285 if (nbits % BITS_PER_LONG)
1286 bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
1287}
1288EXPORT_SYMBOL(bitmap_from_arr32);
1289
1290/**
1291 * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
1292 * @buf: array of u32 (in host byte order), the dest bitmap
1293 * @bitmap: array of unsigned longs, the source bitmap
1294 * @nbits: number of bits in @bitmap
1295 */
1296void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
1297{
1298 unsigned int i, halfwords;
1299
Yury Norovc724f192018-02-06 15:38:02 -08001300 halfwords = DIV_ROUND_UP(nbits, 32);
1301 for (i = 0; i < halfwords; i++) {
1302 buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
1303 if (++i < halfwords)
1304 buf[i] = (u32) (bitmap[i/2] >> 32);
1305 }
1306
1307 /* Clear tail bits in last element of array beyond nbits. */
1308 if (nbits % BITS_PER_LONG)
1309 buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
1310}
1311EXPORT_SYMBOL(bitmap_to_arr32);
1312
1313#endif