xref: /OK3568_Linux_fs/kernel/lib/bitmap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * lib/bitmap.c
4*4882a593Smuzhiyun  * Helper functions for bitmap.h.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/bitmap.h>
8*4882a593Smuzhiyun #include <linux/bitops.h>
9*4882a593Smuzhiyun #include <linux/bug.h>
10*4882a593Smuzhiyun #include <linux/ctype.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <linux/export.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/mm.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/string.h>
18*4882a593Smuzhiyun #include <linux/thread_info.h>
19*4882a593Smuzhiyun #include <linux/uaccess.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <asm/page.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "kstrtox.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * DOC: bitmap introduction
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * bitmaps provide an array of bits, implemented using an
29*4882a593Smuzhiyun  * array of unsigned longs.  The number of valid bits in a
30*4882a593Smuzhiyun  * given bitmap does _not_ need to be an exact multiple of
31*4882a593Smuzhiyun  * BITS_PER_LONG.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * The possible unused bits in the last, partially used word
34*4882a593Smuzhiyun  * of a bitmap are 'don't care'.  The implementation makes
35*4882a593Smuzhiyun  * no particular effort to keep them zero.  It ensures that
36*4882a593Smuzhiyun  * their value will not affect the results of any operation.
37*4882a593Smuzhiyun  * The bitmap operations that return Boolean (bitmap_empty,
38*4882a593Smuzhiyun  * for example) or scalar (bitmap_weight, for example) results
39*4882a593Smuzhiyun  * carefully filter out these unused bits from impacting their
40*4882a593Smuzhiyun  * results.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * The byte ordering of bitmaps is more natural on little
43*4882a593Smuzhiyun  * endian architectures.  See the big-endian headers
44*4882a593Smuzhiyun  * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h
45*4882a593Smuzhiyun  * for the best explanations of this ordering.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
__bitmap_equal(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)48*4882a593Smuzhiyun int __bitmap_equal(const unsigned long *bitmap1,
49*4882a593Smuzhiyun 		const unsigned long *bitmap2, unsigned int bits)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	unsigned int k, lim = bits/BITS_PER_LONG;
52*4882a593Smuzhiyun 	for (k = 0; k < lim; ++k)
53*4882a593Smuzhiyun 		if (bitmap1[k] != bitmap2[k])
54*4882a593Smuzhiyun 			return 0;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	if (bits % BITS_PER_LONG)
57*4882a593Smuzhiyun 		if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
58*4882a593Smuzhiyun 			return 0;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	return 1;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_equal);
63*4882a593Smuzhiyun 
__bitmap_or_equal(const unsigned long * bitmap1,const unsigned long * bitmap2,const unsigned long * bitmap3,unsigned int bits)64*4882a593Smuzhiyun bool __bitmap_or_equal(const unsigned long *bitmap1,
65*4882a593Smuzhiyun 		       const unsigned long *bitmap2,
66*4882a593Smuzhiyun 		       const unsigned long *bitmap3,
67*4882a593Smuzhiyun 		       unsigned int bits)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	unsigned int k, lim = bits / BITS_PER_LONG;
70*4882a593Smuzhiyun 	unsigned long tmp;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	for (k = 0; k < lim; ++k) {
73*4882a593Smuzhiyun 		if ((bitmap1[k] | bitmap2[k]) != bitmap3[k])
74*4882a593Smuzhiyun 			return false;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (!(bits % BITS_PER_LONG))
78*4882a593Smuzhiyun 		return true;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	tmp = (bitmap1[k] | bitmap2[k]) ^ bitmap3[k];
81*4882a593Smuzhiyun 	return (tmp & BITMAP_LAST_WORD_MASK(bits)) == 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
__bitmap_complement(unsigned long * dst,const unsigned long * src,unsigned int bits)84*4882a593Smuzhiyun void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	unsigned int k, lim = BITS_TO_LONGS(bits);
87*4882a593Smuzhiyun 	for (k = 0; k < lim; ++k)
88*4882a593Smuzhiyun 		dst[k] = ~src[k];
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_complement);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * __bitmap_shift_right - logical right shift of the bits in a bitmap
94*4882a593Smuzhiyun  *   @dst : destination bitmap
95*4882a593Smuzhiyun  *   @src : source bitmap
96*4882a593Smuzhiyun  *   @shift : shift by this many bits
97*4882a593Smuzhiyun  *   @nbits : bitmap size, in bits
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * Shifting right (dividing) means moving bits in the MS -> LS bit
100*4882a593Smuzhiyun  * direction.  Zeros are fed into the vacated MS positions and the
101*4882a593Smuzhiyun  * LS bits shifted off the bottom are lost.
102*4882a593Smuzhiyun  */
__bitmap_shift_right(unsigned long * dst,const unsigned long * src,unsigned shift,unsigned nbits)103*4882a593Smuzhiyun void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
104*4882a593Smuzhiyun 			unsigned shift, unsigned nbits)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	unsigned k, lim = BITS_TO_LONGS(nbits);
107*4882a593Smuzhiyun 	unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
108*4882a593Smuzhiyun 	unsigned long mask = BITMAP_LAST_WORD_MASK(nbits);
109*4882a593Smuzhiyun 	for (k = 0; off + k < lim; ++k) {
110*4882a593Smuzhiyun 		unsigned long upper, lower;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 		/*
113*4882a593Smuzhiyun 		 * If shift is not word aligned, take lower rem bits of
114*4882a593Smuzhiyun 		 * word above and make them the top rem bits of result.
115*4882a593Smuzhiyun 		 */
116*4882a593Smuzhiyun 		if (!rem || off + k + 1 >= lim)
117*4882a593Smuzhiyun 			upper = 0;
118*4882a593Smuzhiyun 		else {
119*4882a593Smuzhiyun 			upper = src[off + k + 1];
120*4882a593Smuzhiyun 			if (off + k + 1 == lim - 1)
121*4882a593Smuzhiyun 				upper &= mask;
122*4882a593Smuzhiyun 			upper <<= (BITS_PER_LONG - rem);
123*4882a593Smuzhiyun 		}
124*4882a593Smuzhiyun 		lower = src[off + k];
125*4882a593Smuzhiyun 		if (off + k == lim - 1)
126*4882a593Smuzhiyun 			lower &= mask;
127*4882a593Smuzhiyun 		lower >>= rem;
128*4882a593Smuzhiyun 		dst[k] = lower | upper;
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 	if (off)
131*4882a593Smuzhiyun 		memset(&dst[lim - off], 0, off*sizeof(unsigned long));
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_shift_right);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * __bitmap_shift_left - logical left shift of the bits in a bitmap
138*4882a593Smuzhiyun  *   @dst : destination bitmap
139*4882a593Smuzhiyun  *   @src : source bitmap
140*4882a593Smuzhiyun  *   @shift : shift by this many bits
141*4882a593Smuzhiyun  *   @nbits : bitmap size, in bits
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * Shifting left (multiplying) means moving bits in the LS -> MS
144*4882a593Smuzhiyun  * direction.  Zeros are fed into the vacated LS bit positions
145*4882a593Smuzhiyun  * and those MS bits shifted off the top are lost.
146*4882a593Smuzhiyun  */
147*4882a593Smuzhiyun 
__bitmap_shift_left(unsigned long * dst,const unsigned long * src,unsigned int shift,unsigned int nbits)148*4882a593Smuzhiyun void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
149*4882a593Smuzhiyun 			unsigned int shift, unsigned int nbits)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	int k;
152*4882a593Smuzhiyun 	unsigned int lim = BITS_TO_LONGS(nbits);
153*4882a593Smuzhiyun 	unsigned int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
154*4882a593Smuzhiyun 	for (k = lim - off - 1; k >= 0; --k) {
155*4882a593Smuzhiyun 		unsigned long upper, lower;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		/*
158*4882a593Smuzhiyun 		 * If shift is not word aligned, take upper rem bits of
159*4882a593Smuzhiyun 		 * word below and make them the bottom rem bits of result.
160*4882a593Smuzhiyun 		 */
161*4882a593Smuzhiyun 		if (rem && k > 0)
162*4882a593Smuzhiyun 			lower = src[k - 1] >> (BITS_PER_LONG - rem);
163*4882a593Smuzhiyun 		else
164*4882a593Smuzhiyun 			lower = 0;
165*4882a593Smuzhiyun 		upper = src[k] << rem;
166*4882a593Smuzhiyun 		dst[k + off] = lower | upper;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 	if (off)
169*4882a593Smuzhiyun 		memset(dst, 0, off*sizeof(unsigned long));
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_shift_left);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * bitmap_cut() - remove bit region from bitmap and right shift remaining bits
175*4882a593Smuzhiyun  * @dst: destination bitmap, might overlap with src
176*4882a593Smuzhiyun  * @src: source bitmap
177*4882a593Smuzhiyun  * @first: start bit of region to be removed
178*4882a593Smuzhiyun  * @cut: number of bits to remove
179*4882a593Smuzhiyun  * @nbits: bitmap size, in bits
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * Set the n-th bit of @dst iff the n-th bit of @src is set and
182*4882a593Smuzhiyun  * n is less than @first, or the m-th bit of @src is set for any
183*4882a593Smuzhiyun  * m such that @first <= n < nbits, and m = n + @cut.
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * In pictures, example for a big-endian 32-bit architecture:
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * The @src bitmap is::
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  *   31                                   63
190*4882a593Smuzhiyun  *   |                                    |
191*4882a593Smuzhiyun  *   10000000 11000001 11110010 00010101  10000000 11000001 01110010 00010101
192*4882a593Smuzhiyun  *                   |  |              |                                    |
193*4882a593Smuzhiyun  *                  16  14             0                                   32
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * if @cut is 3, and @first is 14, bits 14-16 in @src are cut and @dst is::
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  *   31                                   63
198*4882a593Smuzhiyun  *   |                                    |
199*4882a593Smuzhiyun  *   10110000 00011000 00110010 00010101  00010000 00011000 00101110 01000010
200*4882a593Smuzhiyun  *                      |              |                                    |
201*4882a593Smuzhiyun  *                      14 (bit 17     0                                   32
202*4882a593Smuzhiyun  *                          from @src)
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  * Note that @dst and @src might overlap partially or entirely.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * This is implemented in the obvious way, with a shift and carry
207*4882a593Smuzhiyun  * step for each moved bit. Optimisation is left as an exercise
208*4882a593Smuzhiyun  * for the compiler.
209*4882a593Smuzhiyun  */
bitmap_cut(unsigned long * dst,const unsigned long * src,unsigned int first,unsigned int cut,unsigned int nbits)210*4882a593Smuzhiyun void bitmap_cut(unsigned long *dst, const unsigned long *src,
211*4882a593Smuzhiyun 		unsigned int first, unsigned int cut, unsigned int nbits)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	unsigned int len = BITS_TO_LONGS(nbits);
214*4882a593Smuzhiyun 	unsigned long keep = 0, carry;
215*4882a593Smuzhiyun 	int i;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (first % BITS_PER_LONG) {
218*4882a593Smuzhiyun 		keep = src[first / BITS_PER_LONG] &
219*4882a593Smuzhiyun 		       (~0UL >> (BITS_PER_LONG - first % BITS_PER_LONG));
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	memmove(dst, src, len * sizeof(*dst));
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	while (cut--) {
225*4882a593Smuzhiyun 		for (i = first / BITS_PER_LONG; i < len; i++) {
226*4882a593Smuzhiyun 			if (i < len - 1)
227*4882a593Smuzhiyun 				carry = dst[i + 1] & 1UL;
228*4882a593Smuzhiyun 			else
229*4882a593Smuzhiyun 				carry = 0;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 			dst[i] = (dst[i] >> 1) | (carry << (BITS_PER_LONG - 1));
232*4882a593Smuzhiyun 		}
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	dst[first / BITS_PER_LONG] &= ~0UL << (first % BITS_PER_LONG);
236*4882a593Smuzhiyun 	dst[first / BITS_PER_LONG] |= keep;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_cut);
239*4882a593Smuzhiyun 
__bitmap_and(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)240*4882a593Smuzhiyun int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
241*4882a593Smuzhiyun 				const unsigned long *bitmap2, unsigned int bits)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	unsigned int k;
244*4882a593Smuzhiyun 	unsigned int lim = bits/BITS_PER_LONG;
245*4882a593Smuzhiyun 	unsigned long result = 0;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	for (k = 0; k < lim; k++)
248*4882a593Smuzhiyun 		result |= (dst[k] = bitmap1[k] & bitmap2[k]);
249*4882a593Smuzhiyun 	if (bits % BITS_PER_LONG)
250*4882a593Smuzhiyun 		result |= (dst[k] = bitmap1[k] & bitmap2[k] &
251*4882a593Smuzhiyun 			   BITMAP_LAST_WORD_MASK(bits));
252*4882a593Smuzhiyun 	return result != 0;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_and);
255*4882a593Smuzhiyun 
__bitmap_or(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)256*4882a593Smuzhiyun void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
257*4882a593Smuzhiyun 				const unsigned long *bitmap2, unsigned int bits)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	unsigned int k;
260*4882a593Smuzhiyun 	unsigned int nr = BITS_TO_LONGS(bits);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	for (k = 0; k < nr; k++)
263*4882a593Smuzhiyun 		dst[k] = bitmap1[k] | bitmap2[k];
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_or);
266*4882a593Smuzhiyun 
__bitmap_xor(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)267*4882a593Smuzhiyun void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
268*4882a593Smuzhiyun 				const unsigned long *bitmap2, unsigned int bits)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	unsigned int k;
271*4882a593Smuzhiyun 	unsigned int nr = BITS_TO_LONGS(bits);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	for (k = 0; k < nr; k++)
274*4882a593Smuzhiyun 		dst[k] = bitmap1[k] ^ bitmap2[k];
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_xor);
277*4882a593Smuzhiyun 
__bitmap_andnot(unsigned long * dst,const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)278*4882a593Smuzhiyun int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
279*4882a593Smuzhiyun 				const unsigned long *bitmap2, unsigned int bits)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	unsigned int k;
282*4882a593Smuzhiyun 	unsigned int lim = bits/BITS_PER_LONG;
283*4882a593Smuzhiyun 	unsigned long result = 0;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	for (k = 0; k < lim; k++)
286*4882a593Smuzhiyun 		result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
287*4882a593Smuzhiyun 	if (bits % BITS_PER_LONG)
288*4882a593Smuzhiyun 		result |= (dst[k] = bitmap1[k] & ~bitmap2[k] &
289*4882a593Smuzhiyun 			   BITMAP_LAST_WORD_MASK(bits));
290*4882a593Smuzhiyun 	return result != 0;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_andnot);
293*4882a593Smuzhiyun 
__bitmap_replace(unsigned long * dst,const unsigned long * old,const unsigned long * new,const unsigned long * mask,unsigned int nbits)294*4882a593Smuzhiyun void __bitmap_replace(unsigned long *dst,
295*4882a593Smuzhiyun 		      const unsigned long *old, const unsigned long *new,
296*4882a593Smuzhiyun 		      const unsigned long *mask, unsigned int nbits)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	unsigned int k;
299*4882a593Smuzhiyun 	unsigned int nr = BITS_TO_LONGS(nbits);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	for (k = 0; k < nr; k++)
302*4882a593Smuzhiyun 		dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_replace);
305*4882a593Smuzhiyun 
__bitmap_intersects(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)306*4882a593Smuzhiyun int __bitmap_intersects(const unsigned long *bitmap1,
307*4882a593Smuzhiyun 			const unsigned long *bitmap2, unsigned int bits)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	unsigned int k, lim = bits/BITS_PER_LONG;
310*4882a593Smuzhiyun 	for (k = 0; k < lim; ++k)
311*4882a593Smuzhiyun 		if (bitmap1[k] & bitmap2[k])
312*4882a593Smuzhiyun 			return 1;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (bits % BITS_PER_LONG)
315*4882a593Smuzhiyun 		if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
316*4882a593Smuzhiyun 			return 1;
317*4882a593Smuzhiyun 	return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_intersects);
320*4882a593Smuzhiyun 
__bitmap_subset(const unsigned long * bitmap1,const unsigned long * bitmap2,unsigned int bits)321*4882a593Smuzhiyun int __bitmap_subset(const unsigned long *bitmap1,
322*4882a593Smuzhiyun 		    const unsigned long *bitmap2, unsigned int bits)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	unsigned int k, lim = bits/BITS_PER_LONG;
325*4882a593Smuzhiyun 	for (k = 0; k < lim; ++k)
326*4882a593Smuzhiyun 		if (bitmap1[k] & ~bitmap2[k])
327*4882a593Smuzhiyun 			return 0;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (bits % BITS_PER_LONG)
330*4882a593Smuzhiyun 		if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
331*4882a593Smuzhiyun 			return 0;
332*4882a593Smuzhiyun 	return 1;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_subset);
335*4882a593Smuzhiyun 
__bitmap_weight(const unsigned long * bitmap,unsigned int bits)336*4882a593Smuzhiyun int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	unsigned int k, lim = bits/BITS_PER_LONG;
339*4882a593Smuzhiyun 	int w = 0;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	for (k = 0; k < lim; k++)
342*4882a593Smuzhiyun 		w += hweight_long(bitmap[k]);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	if (bits % BITS_PER_LONG)
345*4882a593Smuzhiyun 		w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	return w;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_weight);
350*4882a593Smuzhiyun 
__bitmap_set(unsigned long * map,unsigned int start,int len)351*4882a593Smuzhiyun void __bitmap_set(unsigned long *map, unsigned int start, int len)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	unsigned long *p = map + BIT_WORD(start);
354*4882a593Smuzhiyun 	const unsigned int size = start + len;
355*4882a593Smuzhiyun 	int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
356*4882a593Smuzhiyun 	unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	while (len - bits_to_set >= 0) {
359*4882a593Smuzhiyun 		*p |= mask_to_set;
360*4882a593Smuzhiyun 		len -= bits_to_set;
361*4882a593Smuzhiyun 		bits_to_set = BITS_PER_LONG;
362*4882a593Smuzhiyun 		mask_to_set = ~0UL;
363*4882a593Smuzhiyun 		p++;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 	if (len) {
366*4882a593Smuzhiyun 		mask_to_set &= BITMAP_LAST_WORD_MASK(size);
367*4882a593Smuzhiyun 		*p |= mask_to_set;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_set);
371*4882a593Smuzhiyun 
__bitmap_clear(unsigned long * map,unsigned int start,int len)372*4882a593Smuzhiyun void __bitmap_clear(unsigned long *map, unsigned int start, int len)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	unsigned long *p = map + BIT_WORD(start);
375*4882a593Smuzhiyun 	const unsigned int size = start + len;
376*4882a593Smuzhiyun 	int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
377*4882a593Smuzhiyun 	unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	while (len - bits_to_clear >= 0) {
380*4882a593Smuzhiyun 		*p &= ~mask_to_clear;
381*4882a593Smuzhiyun 		len -= bits_to_clear;
382*4882a593Smuzhiyun 		bits_to_clear = BITS_PER_LONG;
383*4882a593Smuzhiyun 		mask_to_clear = ~0UL;
384*4882a593Smuzhiyun 		p++;
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 	if (len) {
387*4882a593Smuzhiyun 		mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
388*4882a593Smuzhiyun 		*p &= ~mask_to_clear;
389*4882a593Smuzhiyun 	}
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun EXPORT_SYMBOL(__bitmap_clear);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /**
394*4882a593Smuzhiyun  * bitmap_find_next_zero_area_off - find a contiguous aligned zero area
395*4882a593Smuzhiyun  * @map: The address to base the search on
396*4882a593Smuzhiyun  * @size: The bitmap size in bits
397*4882a593Smuzhiyun  * @start: The bitnumber to start searching at
398*4882a593Smuzhiyun  * @nr: The number of zeroed bits we're looking for
399*4882a593Smuzhiyun  * @align_mask: Alignment mask for zero area
400*4882a593Smuzhiyun  * @align_offset: Alignment offset for zero area.
401*4882a593Smuzhiyun  *
402*4882a593Smuzhiyun  * The @align_mask should be one less than a power of 2; the effect is that
403*4882a593Smuzhiyun  * the bit offset of all zero areas this function finds plus @align_offset
404*4882a593Smuzhiyun  * is multiple of that power of 2.
405*4882a593Smuzhiyun  */
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)406*4882a593Smuzhiyun unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
407*4882a593Smuzhiyun 					     unsigned long size,
408*4882a593Smuzhiyun 					     unsigned long start,
409*4882a593Smuzhiyun 					     unsigned int nr,
410*4882a593Smuzhiyun 					     unsigned long align_mask,
411*4882a593Smuzhiyun 					     unsigned long align_offset)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun 	unsigned long index, end, i;
414*4882a593Smuzhiyun again:
415*4882a593Smuzhiyun 	index = find_next_zero_bit(map, size, start);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* Align allocation */
418*4882a593Smuzhiyun 	index = __ALIGN_MASK(index + align_offset, align_mask) - align_offset;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	end = index + nr;
421*4882a593Smuzhiyun 	if (end > size)
422*4882a593Smuzhiyun 		return end;
423*4882a593Smuzhiyun 	i = find_next_bit(map, end, index);
424*4882a593Smuzhiyun 	if (i < end) {
425*4882a593Smuzhiyun 		start = i + 1;
426*4882a593Smuzhiyun 		goto again;
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun 	return index;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_find_next_zero_area_off);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun  * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers,
434*4882a593Smuzhiyun  * second version by Paul Jackson, third by Joe Korty.
435*4882a593Smuzhiyun  */
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /**
438*4882a593Smuzhiyun  * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * @ubuf: pointer to user buffer containing string.
441*4882a593Smuzhiyun  * @ulen: buffer size in bytes.  If string is smaller than this
442*4882a593Smuzhiyun  *    then it must be terminated with a \0.
443*4882a593Smuzhiyun  * @maskp: pointer to bitmap array that will contain result.
444*4882a593Smuzhiyun  * @nmaskbits: size of bitmap, in bits.
445*4882a593Smuzhiyun  */
bitmap_parse_user(const char __user * ubuf,unsigned int ulen,unsigned long * maskp,int nmaskbits)446*4882a593Smuzhiyun int bitmap_parse_user(const char __user *ubuf,
447*4882a593Smuzhiyun 			unsigned int ulen, unsigned long *maskp,
448*4882a593Smuzhiyun 			int nmaskbits)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	char *buf;
451*4882a593Smuzhiyun 	int ret;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, ulen);
454*4882a593Smuzhiyun 	if (IS_ERR(buf))
455*4882a593Smuzhiyun 		return PTR_ERR(buf);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	kfree(buf);
460*4882a593Smuzhiyun 	return ret;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_parse_user);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /**
465*4882a593Smuzhiyun  * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
466*4882a593Smuzhiyun  * @list: indicates whether the bitmap must be list
467*4882a593Smuzhiyun  * @buf: page aligned buffer into which string is placed
468*4882a593Smuzhiyun  * @maskp: pointer to bitmap to convert
469*4882a593Smuzhiyun  * @nmaskbits: size of bitmap, in bits
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  * Output format is a comma-separated list of decimal numbers and
472*4882a593Smuzhiyun  * ranges if list is specified or hex digits grouped into comma-separated
473*4882a593Smuzhiyun  * sets of 8 digits/set. Returns the number of characters written to buf.
474*4882a593Smuzhiyun  *
475*4882a593Smuzhiyun  * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
476*4882a593Smuzhiyun  * area and that sufficient storage remains at @buf to accommodate the
477*4882a593Smuzhiyun  * bitmap_print_to_pagebuf() output. Returns the number of characters
478*4882a593Smuzhiyun  * actually printed to @buf, excluding terminating '\0'.
479*4882a593Smuzhiyun  */
bitmap_print_to_pagebuf(bool list,char * buf,const unsigned long * maskp,int nmaskbits)480*4882a593Smuzhiyun int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
481*4882a593Smuzhiyun 			    int nmaskbits)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
486*4882a593Smuzhiyun 		      scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_print_to_pagebuf);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun  * Region 9-38:4/10 describes the following bitmap structure:
492*4882a593Smuzhiyun  * 0	   9  12    18			38
493*4882a593Smuzhiyun  * .........****......****......****......
494*4882a593Smuzhiyun  *	    ^  ^     ^			 ^
495*4882a593Smuzhiyun  *      start  off   group_len	       end
496*4882a593Smuzhiyun  */
497*4882a593Smuzhiyun struct region {
498*4882a593Smuzhiyun 	unsigned int start;
499*4882a593Smuzhiyun 	unsigned int off;
500*4882a593Smuzhiyun 	unsigned int group_len;
501*4882a593Smuzhiyun 	unsigned int end;
502*4882a593Smuzhiyun };
503*4882a593Smuzhiyun 
bitmap_set_region(const struct region * r,unsigned long * bitmap,int nbits)504*4882a593Smuzhiyun static int bitmap_set_region(const struct region *r,
505*4882a593Smuzhiyun 				unsigned long *bitmap, int nbits)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	unsigned int start;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	if (r->end >= nbits)
510*4882a593Smuzhiyun 		return -ERANGE;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	for (start = r->start; start <= r->end; start += r->group_len)
513*4882a593Smuzhiyun 		bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	return 0;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
bitmap_check_region(const struct region * r)518*4882a593Smuzhiyun static int bitmap_check_region(const struct region *r)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun 	if (r->start > r->end || r->group_len == 0 || r->off > r->group_len)
521*4882a593Smuzhiyun 		return -EINVAL;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	return 0;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
bitmap_getnum(const char * str,unsigned int * num)526*4882a593Smuzhiyun static const char *bitmap_getnum(const char *str, unsigned int *num)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	unsigned long long n;
529*4882a593Smuzhiyun 	unsigned int len;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	len = _parse_integer(str, 10, &n);
532*4882a593Smuzhiyun 	if (!len)
533*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
534*4882a593Smuzhiyun 	if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n)
535*4882a593Smuzhiyun 		return ERR_PTR(-EOVERFLOW);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	*num = n;
538*4882a593Smuzhiyun 	return str + len;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun 
end_of_str(char c)541*4882a593Smuzhiyun static inline bool end_of_str(char c)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun 	return c == '\0' || c == '\n';
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun 
__end_of_region(char c)546*4882a593Smuzhiyun static inline bool __end_of_region(char c)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun 	return isspace(c) || c == ',';
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
end_of_region(char c)551*4882a593Smuzhiyun static inline bool end_of_region(char c)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun 	return __end_of_region(c) || end_of_str(c);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun  * The format allows commas and whitespaces at the beginning
558*4882a593Smuzhiyun  * of the region.
559*4882a593Smuzhiyun  */
bitmap_find_region(const char * str)560*4882a593Smuzhiyun static const char *bitmap_find_region(const char *str)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	while (__end_of_region(*str))
563*4882a593Smuzhiyun 		str++;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	return end_of_str(*str) ? NULL : str;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
bitmap_find_region_reverse(const char * start,const char * end)568*4882a593Smuzhiyun static const char *bitmap_find_region_reverse(const char *start, const char *end)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	while (start <= end && __end_of_region(*end))
571*4882a593Smuzhiyun 		end--;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	return end;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
bitmap_parse_region(const char * str,struct region * r)576*4882a593Smuzhiyun static const char *bitmap_parse_region(const char *str, struct region *r)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	str = bitmap_getnum(str, &r->start);
579*4882a593Smuzhiyun 	if (IS_ERR(str))
580*4882a593Smuzhiyun 		return str;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	if (end_of_region(*str))
583*4882a593Smuzhiyun 		goto no_end;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	if (*str != '-')
586*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	str = bitmap_getnum(str + 1, &r->end);
589*4882a593Smuzhiyun 	if (IS_ERR(str))
590*4882a593Smuzhiyun 		return str;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (end_of_region(*str))
593*4882a593Smuzhiyun 		goto no_pattern;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (*str != ':')
596*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	str = bitmap_getnum(str + 1, &r->off);
599*4882a593Smuzhiyun 	if (IS_ERR(str))
600*4882a593Smuzhiyun 		return str;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	if (*str != '/')
603*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	return bitmap_getnum(str + 1, &r->group_len);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun no_end:
608*4882a593Smuzhiyun 	r->end = r->start;
609*4882a593Smuzhiyun no_pattern:
610*4882a593Smuzhiyun 	r->off = r->end + 1;
611*4882a593Smuzhiyun 	r->group_len = r->end + 1;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	return end_of_str(*str) ? NULL : str;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun /**
617*4882a593Smuzhiyun  * bitmap_parselist - convert list format ASCII string to bitmap
618*4882a593Smuzhiyun  * @buf: read user string from this buffer; must be terminated
619*4882a593Smuzhiyun  *    with a \0 or \n.
620*4882a593Smuzhiyun  * @maskp: write resulting mask here
621*4882a593Smuzhiyun  * @nmaskbits: number of bits in mask to be written
622*4882a593Smuzhiyun  *
623*4882a593Smuzhiyun  * Input format is a comma-separated list of decimal numbers and
624*4882a593Smuzhiyun  * ranges.  Consecutively set bits are shown as two hyphen-separated
625*4882a593Smuzhiyun  * decimal numbers, the smallest and largest bit numbers set in
626*4882a593Smuzhiyun  * the range.
627*4882a593Smuzhiyun  * Optionally each range can be postfixed to denote that only parts of it
628*4882a593Smuzhiyun  * should be set. The range will divided to groups of specific size.
629*4882a593Smuzhiyun  * From each group will be used only defined amount of bits.
630*4882a593Smuzhiyun  * Syntax: range:used_size/group_size
631*4882a593Smuzhiyun  * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
632*4882a593Smuzhiyun  *
633*4882a593Smuzhiyun  * Returns: 0 on success, -errno on invalid input strings. Error values:
634*4882a593Smuzhiyun  *
635*4882a593Smuzhiyun  *   - ``-EINVAL``: wrong region format
636*4882a593Smuzhiyun  *   - ``-EINVAL``: invalid character in string
637*4882a593Smuzhiyun  *   - ``-ERANGE``: bit number specified too large for mask
638*4882a593Smuzhiyun  *   - ``-EOVERFLOW``: integer overflow in the input parameters
639*4882a593Smuzhiyun  */
bitmap_parselist(const char * buf,unsigned long * maskp,int nmaskbits)640*4882a593Smuzhiyun int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits)
641*4882a593Smuzhiyun {
642*4882a593Smuzhiyun 	struct region r;
643*4882a593Smuzhiyun 	long ret;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	bitmap_zero(maskp, nmaskbits);
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	while (buf) {
648*4882a593Smuzhiyun 		buf = bitmap_find_region(buf);
649*4882a593Smuzhiyun 		if (buf == NULL)
650*4882a593Smuzhiyun 			return 0;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 		buf = bitmap_parse_region(buf, &r);
653*4882a593Smuzhiyun 		if (IS_ERR(buf))
654*4882a593Smuzhiyun 			return PTR_ERR(buf);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 		ret = bitmap_check_region(&r);
657*4882a593Smuzhiyun 		if (ret)
658*4882a593Smuzhiyun 			return ret;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 		ret = bitmap_set_region(&r, maskp, nmaskbits);
661*4882a593Smuzhiyun 		if (ret)
662*4882a593Smuzhiyun 			return ret;
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	return 0;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_parselist);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun /**
671*4882a593Smuzhiyun  * bitmap_parselist_user()
672*4882a593Smuzhiyun  *
673*4882a593Smuzhiyun  * @ubuf: pointer to user buffer containing string.
674*4882a593Smuzhiyun  * @ulen: buffer size in bytes.  If string is smaller than this
675*4882a593Smuzhiyun  *    then it must be terminated with a \0.
676*4882a593Smuzhiyun  * @maskp: pointer to bitmap array that will contain result.
677*4882a593Smuzhiyun  * @nmaskbits: size of bitmap, in bits.
678*4882a593Smuzhiyun  *
679*4882a593Smuzhiyun  * Wrapper for bitmap_parselist(), providing it with user buffer.
680*4882a593Smuzhiyun  */
bitmap_parselist_user(const char __user * ubuf,unsigned int ulen,unsigned long * maskp,int nmaskbits)681*4882a593Smuzhiyun int bitmap_parselist_user(const char __user *ubuf,
682*4882a593Smuzhiyun 			unsigned int ulen, unsigned long *maskp,
683*4882a593Smuzhiyun 			int nmaskbits)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	char *buf;
686*4882a593Smuzhiyun 	int ret;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	buf = memdup_user_nul(ubuf, ulen);
689*4882a593Smuzhiyun 	if (IS_ERR(buf))
690*4882a593Smuzhiyun 		return PTR_ERR(buf);
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	ret = bitmap_parselist(buf, maskp, nmaskbits);
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	kfree(buf);
695*4882a593Smuzhiyun 	return ret;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_parselist_user);
698*4882a593Smuzhiyun 
bitmap_get_x32_reverse(const char * start,const char * end,u32 * num)699*4882a593Smuzhiyun static const char *bitmap_get_x32_reverse(const char *start,
700*4882a593Smuzhiyun 					const char *end, u32 *num)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun 	u32 ret = 0;
703*4882a593Smuzhiyun 	int c, i;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	for (i = 0; i < 32; i += 4) {
706*4882a593Smuzhiyun 		c = hex_to_bin(*end--);
707*4882a593Smuzhiyun 		if (c < 0)
708*4882a593Smuzhiyun 			return ERR_PTR(-EINVAL);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 		ret |= c << i;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 		if (start > end || __end_of_region(*end))
713*4882a593Smuzhiyun 			goto out;
714*4882a593Smuzhiyun 	}
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	if (hex_to_bin(*end--) >= 0)
717*4882a593Smuzhiyun 		return ERR_PTR(-EOVERFLOW);
718*4882a593Smuzhiyun out:
719*4882a593Smuzhiyun 	*num = ret;
720*4882a593Smuzhiyun 	return end;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun /**
724*4882a593Smuzhiyun  * bitmap_parse - convert an ASCII hex string into a bitmap.
725*4882a593Smuzhiyun  * @start: pointer to buffer containing string.
726*4882a593Smuzhiyun  * @buflen: buffer size in bytes.  If string is smaller than this
727*4882a593Smuzhiyun  *    then it must be terminated with a \0 or \n. In that case,
728*4882a593Smuzhiyun  *    UINT_MAX may be provided instead of string length.
729*4882a593Smuzhiyun  * @maskp: pointer to bitmap array that will contain result.
730*4882a593Smuzhiyun  * @nmaskbits: size of bitmap, in bits.
731*4882a593Smuzhiyun  *
732*4882a593Smuzhiyun  * Commas group hex digits into chunks.  Each chunk defines exactly 32
733*4882a593Smuzhiyun  * bits of the resultant bitmask.  No chunk may specify a value larger
734*4882a593Smuzhiyun  * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
735*4882a593Smuzhiyun  * then leading 0-bits are prepended.  %-EINVAL is returned for illegal
736*4882a593Smuzhiyun  * characters. Grouping such as "1,,5", ",44", "," or "" is allowed.
737*4882a593Smuzhiyun  * Leading, embedded and trailing whitespace accepted.
738*4882a593Smuzhiyun  */
bitmap_parse(const char * start,unsigned int buflen,unsigned long * maskp,int nmaskbits)739*4882a593Smuzhiyun int bitmap_parse(const char *start, unsigned int buflen,
740*4882a593Smuzhiyun 		unsigned long *maskp, int nmaskbits)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	const char *end = strnchrnul(start, buflen, '\n') - 1;
743*4882a593Smuzhiyun 	int chunks = BITS_TO_U32(nmaskbits);
744*4882a593Smuzhiyun 	u32 *bitmap = (u32 *)maskp;
745*4882a593Smuzhiyun 	int unset_bit;
746*4882a593Smuzhiyun 	int chunk;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	for (chunk = 0; ; chunk++) {
749*4882a593Smuzhiyun 		end = bitmap_find_region_reverse(start, end);
750*4882a593Smuzhiyun 		if (start > end)
751*4882a593Smuzhiyun 			break;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 		if (!chunks--)
754*4882a593Smuzhiyun 			return -EOVERFLOW;
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
757*4882a593Smuzhiyun 		end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]);
758*4882a593Smuzhiyun #else
759*4882a593Smuzhiyun 		end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]);
760*4882a593Smuzhiyun #endif
761*4882a593Smuzhiyun 		if (IS_ERR(end))
762*4882a593Smuzhiyun 			return PTR_ERR(end);
763*4882a593Smuzhiyun 	}
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32;
766*4882a593Smuzhiyun 	if (unset_bit < nmaskbits) {
767*4882a593Smuzhiyun 		bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit);
768*4882a593Smuzhiyun 		return 0;
769*4882a593Smuzhiyun 	}
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit)
772*4882a593Smuzhiyun 		return -EOVERFLOW;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	return 0;
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_parse);
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun #ifdef CONFIG_NUMA
780*4882a593Smuzhiyun /**
781*4882a593Smuzhiyun  * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
782*4882a593Smuzhiyun  *	@buf: pointer to a bitmap
783*4882a593Smuzhiyun  *	@pos: a bit position in @buf (0 <= @pos < @nbits)
784*4882a593Smuzhiyun  *	@nbits: number of valid bit positions in @buf
785*4882a593Smuzhiyun  *
786*4882a593Smuzhiyun  * Map the bit at position @pos in @buf (of length @nbits) to the
787*4882a593Smuzhiyun  * ordinal of which set bit it is.  If it is not set or if @pos
788*4882a593Smuzhiyun  * is not a valid bit position, map to -1.
789*4882a593Smuzhiyun  *
790*4882a593Smuzhiyun  * If for example, just bits 4 through 7 are set in @buf, then @pos
791*4882a593Smuzhiyun  * values 4 through 7 will get mapped to 0 through 3, respectively,
792*4882a593Smuzhiyun  * and other @pos values will get mapped to -1.  When @pos value 7
793*4882a593Smuzhiyun  * gets mapped to (returns) @ord value 3 in this example, that means
794*4882a593Smuzhiyun  * that bit 7 is the 3rd (starting with 0th) set bit in @buf.
795*4882a593Smuzhiyun  *
796*4882a593Smuzhiyun  * The bit positions 0 through @bits are valid positions in @buf.
797*4882a593Smuzhiyun  */
bitmap_pos_to_ord(const unsigned long * buf,unsigned int pos,unsigned int nbits)798*4882a593Smuzhiyun static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	if (pos >= nbits || !test_bit(pos, buf))
801*4882a593Smuzhiyun 		return -1;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	return __bitmap_weight(buf, pos);
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun /**
807*4882a593Smuzhiyun  * bitmap_ord_to_pos - find position of n-th set bit in bitmap
808*4882a593Smuzhiyun  *	@buf: pointer to bitmap
809*4882a593Smuzhiyun  *	@ord: ordinal bit position (n-th set bit, n >= 0)
810*4882a593Smuzhiyun  *	@nbits: number of valid bit positions in @buf
811*4882a593Smuzhiyun  *
812*4882a593Smuzhiyun  * Map the ordinal offset of bit @ord in @buf to its position in @buf.
813*4882a593Smuzhiyun  * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
814*4882a593Smuzhiyun  * >= weight(buf), returns @nbits.
815*4882a593Smuzhiyun  *
816*4882a593Smuzhiyun  * If for example, just bits 4 through 7 are set in @buf, then @ord
817*4882a593Smuzhiyun  * values 0 through 3 will get mapped to 4 through 7, respectively,
818*4882a593Smuzhiyun  * and all other @ord values returns @nbits.  When @ord value 3
819*4882a593Smuzhiyun  * gets mapped to (returns) @pos value 7 in this example, that means
820*4882a593Smuzhiyun  * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
821*4882a593Smuzhiyun  *
822*4882a593Smuzhiyun  * The bit positions 0 through @nbits-1 are valid positions in @buf.
823*4882a593Smuzhiyun  */
bitmap_ord_to_pos(const unsigned long * buf,unsigned int ord,unsigned int nbits)824*4882a593Smuzhiyun unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun 	unsigned int pos;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	for (pos = find_first_bit(buf, nbits);
829*4882a593Smuzhiyun 	     pos < nbits && ord;
830*4882a593Smuzhiyun 	     pos = find_next_bit(buf, nbits, pos + 1))
831*4882a593Smuzhiyun 		ord--;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	return pos;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun /**
837*4882a593Smuzhiyun  * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap
838*4882a593Smuzhiyun  *	@dst: remapped result
839*4882a593Smuzhiyun  *	@src: subset to be remapped
840*4882a593Smuzhiyun  *	@old: defines domain of map
841*4882a593Smuzhiyun  *	@new: defines range of map
842*4882a593Smuzhiyun  *	@nbits: number of bits in each of these bitmaps
843*4882a593Smuzhiyun  *
844*4882a593Smuzhiyun  * Let @old and @new define a mapping of bit positions, such that
845*4882a593Smuzhiyun  * whatever position is held by the n-th set bit in @old is mapped
846*4882a593Smuzhiyun  * to the n-th set bit in @new.  In the more general case, allowing
847*4882a593Smuzhiyun  * for the possibility that the weight 'w' of @new is less than the
848*4882a593Smuzhiyun  * weight of @old, map the position of the n-th set bit in @old to
849*4882a593Smuzhiyun  * the position of the m-th set bit in @new, where m == n % w.
850*4882a593Smuzhiyun  *
851*4882a593Smuzhiyun  * If either of the @old and @new bitmaps are empty, or if @src and
852*4882a593Smuzhiyun  * @dst point to the same location, then this routine copies @src
853*4882a593Smuzhiyun  * to @dst.
854*4882a593Smuzhiyun  *
855*4882a593Smuzhiyun  * The positions of unset bits in @old are mapped to themselves
856*4882a593Smuzhiyun  * (the identify map).
857*4882a593Smuzhiyun  *
858*4882a593Smuzhiyun  * Apply the above specified mapping to @src, placing the result in
859*4882a593Smuzhiyun  * @dst, clearing any bits previously set in @dst.
860*4882a593Smuzhiyun  *
861*4882a593Smuzhiyun  * For example, lets say that @old has bits 4 through 7 set, and
862*4882a593Smuzhiyun  * @new has bits 12 through 15 set.  This defines the mapping of bit
863*4882a593Smuzhiyun  * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
864*4882a593Smuzhiyun  * bit positions unchanged.  So if say @src comes into this routine
865*4882a593Smuzhiyun  * with bits 1, 5 and 7 set, then @dst should leave with bits 1,
866*4882a593Smuzhiyun  * 13 and 15 set.
867*4882a593Smuzhiyun  */
bitmap_remap(unsigned long * dst,const unsigned long * src,const unsigned long * old,const unsigned long * new,unsigned int nbits)868*4882a593Smuzhiyun void bitmap_remap(unsigned long *dst, const unsigned long *src,
869*4882a593Smuzhiyun 		const unsigned long *old, const unsigned long *new,
870*4882a593Smuzhiyun 		unsigned int nbits)
871*4882a593Smuzhiyun {
872*4882a593Smuzhiyun 	unsigned int oldbit, w;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	if (dst == src)		/* following doesn't handle inplace remaps */
875*4882a593Smuzhiyun 		return;
876*4882a593Smuzhiyun 	bitmap_zero(dst, nbits);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	w = bitmap_weight(new, nbits);
879*4882a593Smuzhiyun 	for_each_set_bit(oldbit, src, nbits) {
880*4882a593Smuzhiyun 		int n = bitmap_pos_to_ord(old, oldbit, nbits);
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 		if (n < 0 || w == 0)
883*4882a593Smuzhiyun 			set_bit(oldbit, dst);	/* identity map */
884*4882a593Smuzhiyun 		else
885*4882a593Smuzhiyun 			set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
886*4882a593Smuzhiyun 	}
887*4882a593Smuzhiyun }
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun /**
890*4882a593Smuzhiyun  * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit
891*4882a593Smuzhiyun  *	@oldbit: bit position to be mapped
892*4882a593Smuzhiyun  *	@old: defines domain of map
893*4882a593Smuzhiyun  *	@new: defines range of map
894*4882a593Smuzhiyun  *	@bits: number of bits in each of these bitmaps
895*4882a593Smuzhiyun  *
896*4882a593Smuzhiyun  * Let @old and @new define a mapping of bit positions, such that
897*4882a593Smuzhiyun  * whatever position is held by the n-th set bit in @old is mapped
898*4882a593Smuzhiyun  * to the n-th set bit in @new.  In the more general case, allowing
899*4882a593Smuzhiyun  * for the possibility that the weight 'w' of @new is less than the
900*4882a593Smuzhiyun  * weight of @old, map the position of the n-th set bit in @old to
901*4882a593Smuzhiyun  * the position of the m-th set bit in @new, where m == n % w.
902*4882a593Smuzhiyun  *
903*4882a593Smuzhiyun  * The positions of unset bits in @old are mapped to themselves
904*4882a593Smuzhiyun  * (the identify map).
905*4882a593Smuzhiyun  *
906*4882a593Smuzhiyun  * Apply the above specified mapping to bit position @oldbit, returning
907*4882a593Smuzhiyun  * the new bit position.
908*4882a593Smuzhiyun  *
909*4882a593Smuzhiyun  * For example, lets say that @old has bits 4 through 7 set, and
910*4882a593Smuzhiyun  * @new has bits 12 through 15 set.  This defines the mapping of bit
911*4882a593Smuzhiyun  * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other
912*4882a593Smuzhiyun  * bit positions unchanged.  So if say @oldbit is 5, then this routine
913*4882a593Smuzhiyun  * returns 13.
914*4882a593Smuzhiyun  */
bitmap_bitremap(int oldbit,const unsigned long * old,const unsigned long * new,int bits)915*4882a593Smuzhiyun int bitmap_bitremap(int oldbit, const unsigned long *old,
916*4882a593Smuzhiyun 				const unsigned long *new, int bits)
917*4882a593Smuzhiyun {
918*4882a593Smuzhiyun 	int w = bitmap_weight(new, bits);
919*4882a593Smuzhiyun 	int n = bitmap_pos_to_ord(old, oldbit, bits);
920*4882a593Smuzhiyun 	if (n < 0 || w == 0)
921*4882a593Smuzhiyun 		return oldbit;
922*4882a593Smuzhiyun 	else
923*4882a593Smuzhiyun 		return bitmap_ord_to_pos(new, n % w, bits);
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun /**
927*4882a593Smuzhiyun  * bitmap_onto - translate one bitmap relative to another
928*4882a593Smuzhiyun  *	@dst: resulting translated bitmap
929*4882a593Smuzhiyun  * 	@orig: original untranslated bitmap
930*4882a593Smuzhiyun  * 	@relmap: bitmap relative to which translated
931*4882a593Smuzhiyun  *	@bits: number of bits in each of these bitmaps
932*4882a593Smuzhiyun  *
933*4882a593Smuzhiyun  * Set the n-th bit of @dst iff there exists some m such that the
934*4882a593Smuzhiyun  * n-th bit of @relmap is set, the m-th bit of @orig is set, and
935*4882a593Smuzhiyun  * the n-th bit of @relmap is also the m-th _set_ bit of @relmap.
936*4882a593Smuzhiyun  * (If you understood the previous sentence the first time your
937*4882a593Smuzhiyun  * read it, you're overqualified for your current job.)
938*4882a593Smuzhiyun  *
939*4882a593Smuzhiyun  * In other words, @orig is mapped onto (surjectively) @dst,
940*4882a593Smuzhiyun  * using the map { <n, m> | the n-th bit of @relmap is the
941*4882a593Smuzhiyun  * m-th set bit of @relmap }.
942*4882a593Smuzhiyun  *
943*4882a593Smuzhiyun  * Any set bits in @orig above bit number W, where W is the
944*4882a593Smuzhiyun  * weight of (number of set bits in) @relmap are mapped nowhere.
945*4882a593Smuzhiyun  * In particular, if for all bits m set in @orig, m >= W, then
946*4882a593Smuzhiyun  * @dst will end up empty.  In situations where the possibility
947*4882a593Smuzhiyun  * of such an empty result is not desired, one way to avoid it is
948*4882a593Smuzhiyun  * to use the bitmap_fold() operator, below, to first fold the
949*4882a593Smuzhiyun  * @orig bitmap over itself so that all its set bits x are in the
950*4882a593Smuzhiyun  * range 0 <= x < W.  The bitmap_fold() operator does this by
951*4882a593Smuzhiyun  * setting the bit (m % W) in @dst, for each bit (m) set in @orig.
952*4882a593Smuzhiyun  *
953*4882a593Smuzhiyun  * Example [1] for bitmap_onto():
954*4882a593Smuzhiyun  *  Let's say @relmap has bits 30-39 set, and @orig has bits
955*4882a593Smuzhiyun  *  1, 3, 5, 7, 9 and 11 set.  Then on return from this routine,
956*4882a593Smuzhiyun  *  @dst will have bits 31, 33, 35, 37 and 39 set.
957*4882a593Smuzhiyun  *
958*4882a593Smuzhiyun  *  When bit 0 is set in @orig, it means turn on the bit in
959*4882a593Smuzhiyun  *  @dst corresponding to whatever is the first bit (if any)
960*4882a593Smuzhiyun  *  that is turned on in @relmap.  Since bit 0 was off in the
961*4882a593Smuzhiyun  *  above example, we leave off that bit (bit 30) in @dst.
962*4882a593Smuzhiyun  *
963*4882a593Smuzhiyun  *  When bit 1 is set in @orig (as in the above example), it
964*4882a593Smuzhiyun  *  means turn on the bit in @dst corresponding to whatever
965*4882a593Smuzhiyun  *  is the second bit that is turned on in @relmap.  The second
966*4882a593Smuzhiyun  *  bit in @relmap that was turned on in the above example was
967*4882a593Smuzhiyun  *  bit 31, so we turned on bit 31 in @dst.
968*4882a593Smuzhiyun  *
969*4882a593Smuzhiyun  *  Similarly, we turned on bits 33, 35, 37 and 39 in @dst,
970*4882a593Smuzhiyun  *  because they were the 4th, 6th, 8th and 10th set bits
971*4882a593Smuzhiyun  *  set in @relmap, and the 4th, 6th, 8th and 10th bits of
972*4882a593Smuzhiyun  *  @orig (i.e. bits 3, 5, 7 and 9) were also set.
973*4882a593Smuzhiyun  *
974*4882a593Smuzhiyun  *  When bit 11 is set in @orig, it means turn on the bit in
975*4882a593Smuzhiyun  *  @dst corresponding to whatever is the twelfth bit that is
976*4882a593Smuzhiyun  *  turned on in @relmap.  In the above example, there were
977*4882a593Smuzhiyun  *  only ten bits turned on in @relmap (30..39), so that bit
978*4882a593Smuzhiyun  *  11 was set in @orig had no affect on @dst.
979*4882a593Smuzhiyun  *
980*4882a593Smuzhiyun  * Example [2] for bitmap_fold() + bitmap_onto():
981*4882a593Smuzhiyun  *  Let's say @relmap has these ten bits set::
982*4882a593Smuzhiyun  *
983*4882a593Smuzhiyun  *		40 41 42 43 45 48 53 61 74 95
984*4882a593Smuzhiyun  *
985*4882a593Smuzhiyun  *  (for the curious, that's 40 plus the first ten terms of the
986*4882a593Smuzhiyun  *  Fibonacci sequence.)
987*4882a593Smuzhiyun  *
988*4882a593Smuzhiyun  *  Further lets say we use the following code, invoking
989*4882a593Smuzhiyun  *  bitmap_fold() then bitmap_onto, as suggested above to
990*4882a593Smuzhiyun  *  avoid the possibility of an empty @dst result::
991*4882a593Smuzhiyun  *
992*4882a593Smuzhiyun  *	unsigned long *tmp;	// a temporary bitmap's bits
993*4882a593Smuzhiyun  *
994*4882a593Smuzhiyun  *	bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits);
995*4882a593Smuzhiyun  *	bitmap_onto(dst, tmp, relmap, bits);
996*4882a593Smuzhiyun  *
997*4882a593Smuzhiyun  *  Then this table shows what various values of @dst would be, for
998*4882a593Smuzhiyun  *  various @orig's.  I list the zero-based positions of each set bit.
999*4882a593Smuzhiyun  *  The tmp column shows the intermediate result, as computed by
1000*4882a593Smuzhiyun  *  using bitmap_fold() to fold the @orig bitmap modulo ten
1001*4882a593Smuzhiyun  *  (the weight of @relmap):
1002*4882a593Smuzhiyun  *
1003*4882a593Smuzhiyun  *      =============== ============== =================
1004*4882a593Smuzhiyun  *      @orig           tmp            @dst
1005*4882a593Smuzhiyun  *      0                0             40
1006*4882a593Smuzhiyun  *      1                1             41
1007*4882a593Smuzhiyun  *      9                9             95
1008*4882a593Smuzhiyun  *      10               0             40 [#f1]_
1009*4882a593Smuzhiyun  *      1 3 5 7          1 3 5 7       41 43 48 61
1010*4882a593Smuzhiyun  *      0 1 2 3 4        0 1 2 3 4     40 41 42 43 45
1011*4882a593Smuzhiyun  *      0 9 18 27        0 9 8 7       40 61 74 95
1012*4882a593Smuzhiyun  *      0 10 20 30       0             40
1013*4882a593Smuzhiyun  *      0 11 22 33       0 1 2 3       40 41 42 43
1014*4882a593Smuzhiyun  *      0 12 24 36       0 2 4 6       40 42 45 53
1015*4882a593Smuzhiyun  *      78 102 211       1 2 8         41 42 74 [#f1]_
1016*4882a593Smuzhiyun  *      =============== ============== =================
1017*4882a593Smuzhiyun  *
1018*4882a593Smuzhiyun  * .. [#f1]
1019*4882a593Smuzhiyun  *
1020*4882a593Smuzhiyun  *     For these marked lines, if we hadn't first done bitmap_fold()
1021*4882a593Smuzhiyun  *     into tmp, then the @dst result would have been empty.
1022*4882a593Smuzhiyun  *
1023*4882a593Smuzhiyun  * If either of @orig or @relmap is empty (no set bits), then @dst
1024*4882a593Smuzhiyun  * will be returned empty.
1025*4882a593Smuzhiyun  *
1026*4882a593Smuzhiyun  * If (as explained above) the only set bits in @orig are in positions
1027*4882a593Smuzhiyun  * m where m >= W, (where W is the weight of @relmap) then @dst will
1028*4882a593Smuzhiyun  * once again be returned empty.
1029*4882a593Smuzhiyun  *
1030*4882a593Smuzhiyun  * All bits in @dst not set by the above rule are cleared.
1031*4882a593Smuzhiyun  */
bitmap_onto(unsigned long * dst,const unsigned long * orig,const unsigned long * relmap,unsigned int bits)1032*4882a593Smuzhiyun void bitmap_onto(unsigned long *dst, const unsigned long *orig,
1033*4882a593Smuzhiyun 			const unsigned long *relmap, unsigned int bits)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	unsigned int n, m;	/* same meaning as in above comment */
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	if (dst == orig)	/* following doesn't handle inplace mappings */
1038*4882a593Smuzhiyun 		return;
1039*4882a593Smuzhiyun 	bitmap_zero(dst, bits);
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	/*
1042*4882a593Smuzhiyun 	 * The following code is a more efficient, but less
1043*4882a593Smuzhiyun 	 * obvious, equivalent to the loop:
1044*4882a593Smuzhiyun 	 *	for (m = 0; m < bitmap_weight(relmap, bits); m++) {
1045*4882a593Smuzhiyun 	 *		n = bitmap_ord_to_pos(orig, m, bits);
1046*4882a593Smuzhiyun 	 *		if (test_bit(m, orig))
1047*4882a593Smuzhiyun 	 *			set_bit(n, dst);
1048*4882a593Smuzhiyun 	 *	}
1049*4882a593Smuzhiyun 	 */
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	m = 0;
1052*4882a593Smuzhiyun 	for_each_set_bit(n, relmap, bits) {
1053*4882a593Smuzhiyun 		/* m == bitmap_pos_to_ord(relmap, n, bits) */
1054*4882a593Smuzhiyun 		if (test_bit(m, orig))
1055*4882a593Smuzhiyun 			set_bit(n, dst);
1056*4882a593Smuzhiyun 		m++;
1057*4882a593Smuzhiyun 	}
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun /**
1061*4882a593Smuzhiyun  * bitmap_fold - fold larger bitmap into smaller, modulo specified size
1062*4882a593Smuzhiyun  *	@dst: resulting smaller bitmap
1063*4882a593Smuzhiyun  *	@orig: original larger bitmap
1064*4882a593Smuzhiyun  *	@sz: specified size
1065*4882a593Smuzhiyun  *	@nbits: number of bits in each of these bitmaps
1066*4882a593Smuzhiyun  *
1067*4882a593Smuzhiyun  * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
1068*4882a593Smuzhiyun  * Clear all other bits in @dst.  See further the comment and
1069*4882a593Smuzhiyun  * Example [2] for bitmap_onto() for why and how to use this.
1070*4882a593Smuzhiyun  */
bitmap_fold(unsigned long * dst,const unsigned long * orig,unsigned int sz,unsigned int nbits)1071*4882a593Smuzhiyun void bitmap_fold(unsigned long *dst, const unsigned long *orig,
1072*4882a593Smuzhiyun 			unsigned int sz, unsigned int nbits)
1073*4882a593Smuzhiyun {
1074*4882a593Smuzhiyun 	unsigned int oldbit;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	if (dst == orig)	/* following doesn't handle inplace mappings */
1077*4882a593Smuzhiyun 		return;
1078*4882a593Smuzhiyun 	bitmap_zero(dst, nbits);
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 	for_each_set_bit(oldbit, orig, nbits)
1081*4882a593Smuzhiyun 		set_bit(oldbit % sz, dst);
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun #endif /* CONFIG_NUMA */
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun /*
1086*4882a593Smuzhiyun  * Common code for bitmap_*_region() routines.
1087*4882a593Smuzhiyun  *	bitmap: array of unsigned longs corresponding to the bitmap
1088*4882a593Smuzhiyun  *	pos: the beginning of the region
1089*4882a593Smuzhiyun  *	order: region size (log base 2 of number of bits)
1090*4882a593Smuzhiyun  *	reg_op: operation(s) to perform on that region of bitmap
1091*4882a593Smuzhiyun  *
1092*4882a593Smuzhiyun  * Can set, verify and/or release a region of bits in a bitmap,
1093*4882a593Smuzhiyun  * depending on which combination of REG_OP_* flag bits is set.
1094*4882a593Smuzhiyun  *
1095*4882a593Smuzhiyun  * A region of a bitmap is a sequence of bits in the bitmap, of
1096*4882a593Smuzhiyun  * some size '1 << order' (a power of two), aligned to that same
1097*4882a593Smuzhiyun  * '1 << order' power of two.
1098*4882a593Smuzhiyun  *
1099*4882a593Smuzhiyun  * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits).
1100*4882a593Smuzhiyun  * Returns 0 in all other cases and reg_ops.
1101*4882a593Smuzhiyun  */
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun enum {
1104*4882a593Smuzhiyun 	REG_OP_ISFREE,		/* true if region is all zero bits */
1105*4882a593Smuzhiyun 	REG_OP_ALLOC,		/* set all bits in region */
1106*4882a593Smuzhiyun 	REG_OP_RELEASE,		/* clear all bits in region */
1107*4882a593Smuzhiyun };
1108*4882a593Smuzhiyun 
__reg_op(unsigned long * bitmap,unsigned int pos,int order,int reg_op)1109*4882a593Smuzhiyun static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op)
1110*4882a593Smuzhiyun {
1111*4882a593Smuzhiyun 	int nbits_reg;		/* number of bits in region */
1112*4882a593Smuzhiyun 	int index;		/* index first long of region in bitmap */
1113*4882a593Smuzhiyun 	int offset;		/* bit offset region in bitmap[index] */
1114*4882a593Smuzhiyun 	int nlongs_reg;		/* num longs spanned by region in bitmap */
1115*4882a593Smuzhiyun 	int nbitsinlong;	/* num bits of region in each spanned long */
1116*4882a593Smuzhiyun 	unsigned long mask;	/* bitmask for one long of region */
1117*4882a593Smuzhiyun 	int i;			/* scans bitmap by longs */
1118*4882a593Smuzhiyun 	int ret = 0;		/* return value */
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	/*
1121*4882a593Smuzhiyun 	 * Either nlongs_reg == 1 (for small orders that fit in one long)
1122*4882a593Smuzhiyun 	 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
1123*4882a593Smuzhiyun 	 */
1124*4882a593Smuzhiyun 	nbits_reg = 1 << order;
1125*4882a593Smuzhiyun 	index = pos / BITS_PER_LONG;
1126*4882a593Smuzhiyun 	offset = pos - (index * BITS_PER_LONG);
1127*4882a593Smuzhiyun 	nlongs_reg = BITS_TO_LONGS(nbits_reg);
1128*4882a593Smuzhiyun 	nbitsinlong = min(nbits_reg,  BITS_PER_LONG);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	/*
1131*4882a593Smuzhiyun 	 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
1132*4882a593Smuzhiyun 	 * overflows if nbitsinlong == BITS_PER_LONG.
1133*4882a593Smuzhiyun 	 */
1134*4882a593Smuzhiyun 	mask = (1UL << (nbitsinlong - 1));
1135*4882a593Smuzhiyun 	mask += mask - 1;
1136*4882a593Smuzhiyun 	mask <<= offset;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	switch (reg_op) {
1139*4882a593Smuzhiyun 	case REG_OP_ISFREE:
1140*4882a593Smuzhiyun 		for (i = 0; i < nlongs_reg; i++) {
1141*4882a593Smuzhiyun 			if (bitmap[index + i] & mask)
1142*4882a593Smuzhiyun 				goto done;
1143*4882a593Smuzhiyun 		}
1144*4882a593Smuzhiyun 		ret = 1;	/* all bits in region free (zero) */
1145*4882a593Smuzhiyun 		break;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 	case REG_OP_ALLOC:
1148*4882a593Smuzhiyun 		for (i = 0; i < nlongs_reg; i++)
1149*4882a593Smuzhiyun 			bitmap[index + i] |= mask;
1150*4882a593Smuzhiyun 		break;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	case REG_OP_RELEASE:
1153*4882a593Smuzhiyun 		for (i = 0; i < nlongs_reg; i++)
1154*4882a593Smuzhiyun 			bitmap[index + i] &= ~mask;
1155*4882a593Smuzhiyun 		break;
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun done:
1158*4882a593Smuzhiyun 	return ret;
1159*4882a593Smuzhiyun }
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun /**
1162*4882a593Smuzhiyun  * bitmap_find_free_region - find a contiguous aligned mem region
1163*4882a593Smuzhiyun  *	@bitmap: array of unsigned longs corresponding to the bitmap
1164*4882a593Smuzhiyun  *	@bits: number of bits in the bitmap
1165*4882a593Smuzhiyun  *	@order: region size (log base 2 of number of bits) to find
1166*4882a593Smuzhiyun  *
1167*4882a593Smuzhiyun  * Find a region of free (zero) bits in a @bitmap of @bits bits and
1168*4882a593Smuzhiyun  * allocate them (set them to one).  Only consider regions of length
1169*4882a593Smuzhiyun  * a power (@order) of two, aligned to that power of two, which
1170*4882a593Smuzhiyun  * makes the search algorithm much faster.
1171*4882a593Smuzhiyun  *
1172*4882a593Smuzhiyun  * Return the bit offset in bitmap of the allocated region,
1173*4882a593Smuzhiyun  * or -errno on failure.
1174*4882a593Smuzhiyun  */
bitmap_find_free_region(unsigned long * bitmap,unsigned int bits,int order)1175*4882a593Smuzhiyun int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
1176*4882a593Smuzhiyun {
1177*4882a593Smuzhiyun 	unsigned int pos, end;		/* scans bitmap by regions of size order */
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) {
1180*4882a593Smuzhiyun 		if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1181*4882a593Smuzhiyun 			continue;
1182*4882a593Smuzhiyun 		__reg_op(bitmap, pos, order, REG_OP_ALLOC);
1183*4882a593Smuzhiyun 		return pos;
1184*4882a593Smuzhiyun 	}
1185*4882a593Smuzhiyun 	return -ENOMEM;
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_find_free_region);
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun /**
1190*4882a593Smuzhiyun  * bitmap_release_region - release allocated bitmap region
1191*4882a593Smuzhiyun  *	@bitmap: array of unsigned longs corresponding to the bitmap
1192*4882a593Smuzhiyun  *	@pos: beginning of bit region to release
1193*4882a593Smuzhiyun  *	@order: region size (log base 2 of number of bits) to release
1194*4882a593Smuzhiyun  *
1195*4882a593Smuzhiyun  * This is the complement to __bitmap_find_free_region() and releases
1196*4882a593Smuzhiyun  * the found region (by clearing it in the bitmap).
1197*4882a593Smuzhiyun  *
1198*4882a593Smuzhiyun  * No return value.
1199*4882a593Smuzhiyun  */
bitmap_release_region(unsigned long * bitmap,unsigned int pos,int order)1200*4882a593Smuzhiyun void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun 	__reg_op(bitmap, pos, order, REG_OP_RELEASE);
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_release_region);
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun /**
1207*4882a593Smuzhiyun  * bitmap_allocate_region - allocate bitmap region
1208*4882a593Smuzhiyun  *	@bitmap: array of unsigned longs corresponding to the bitmap
1209*4882a593Smuzhiyun  *	@pos: beginning of bit region to allocate
1210*4882a593Smuzhiyun  *	@order: region size (log base 2 of number of bits) to allocate
1211*4882a593Smuzhiyun  *
1212*4882a593Smuzhiyun  * Allocate (set bits in) a specified region of a bitmap.
1213*4882a593Smuzhiyun  *
1214*4882a593Smuzhiyun  * Return 0 on success, or %-EBUSY if specified region wasn't
1215*4882a593Smuzhiyun  * free (not all bits were zero).
1216*4882a593Smuzhiyun  */
bitmap_allocate_region(unsigned long * bitmap,unsigned int pos,int order)1217*4882a593Smuzhiyun int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun 	if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
1220*4882a593Smuzhiyun 		return -EBUSY;
1221*4882a593Smuzhiyun 	return __reg_op(bitmap, pos, order, REG_OP_ALLOC);
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_allocate_region);
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun /**
1226*4882a593Smuzhiyun  * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order.
1227*4882a593Smuzhiyun  * @dst:   destination buffer
1228*4882a593Smuzhiyun  * @src:   bitmap to copy
1229*4882a593Smuzhiyun  * @nbits: number of bits in the bitmap
1230*4882a593Smuzhiyun  *
1231*4882a593Smuzhiyun  * Require nbits % BITS_PER_LONG == 0.
1232*4882a593Smuzhiyun  */
1233*4882a593Smuzhiyun #ifdef __BIG_ENDIAN
bitmap_copy_le(unsigned long * dst,const unsigned long * src,unsigned int nbits)1234*4882a593Smuzhiyun void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun 	unsigned int i;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	for (i = 0; i < nbits/BITS_PER_LONG; i++) {
1239*4882a593Smuzhiyun 		if (BITS_PER_LONG == 64)
1240*4882a593Smuzhiyun 			dst[i] = cpu_to_le64(src[i]);
1241*4882a593Smuzhiyun 		else
1242*4882a593Smuzhiyun 			dst[i] = cpu_to_le32(src[i]);
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_copy_le);
1246*4882a593Smuzhiyun #endif
1247*4882a593Smuzhiyun 
bitmap_alloc(unsigned int nbits,gfp_t flags)1248*4882a593Smuzhiyun unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
1249*4882a593Smuzhiyun {
1250*4882a593Smuzhiyun 	return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
1251*4882a593Smuzhiyun 			     flags);
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_alloc);
1254*4882a593Smuzhiyun 
bitmap_zalloc(unsigned int nbits,gfp_t flags)1255*4882a593Smuzhiyun unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun 	return bitmap_alloc(nbits, flags | __GFP_ZERO);
1258*4882a593Smuzhiyun }
1259*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_zalloc);
1260*4882a593Smuzhiyun 
bitmap_free(const unsigned long * bitmap)1261*4882a593Smuzhiyun void bitmap_free(const unsigned long *bitmap)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun 	kfree(bitmap);
1264*4882a593Smuzhiyun }
1265*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_free);
1266*4882a593Smuzhiyun 
devm_bitmap_free(void * data)1267*4882a593Smuzhiyun static void devm_bitmap_free(void *data)
1268*4882a593Smuzhiyun {
1269*4882a593Smuzhiyun 	unsigned long *bitmap = data;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	bitmap_free(bitmap);
1272*4882a593Smuzhiyun }
1273*4882a593Smuzhiyun 
devm_bitmap_alloc(struct device * dev,unsigned int nbits,gfp_t flags)1274*4882a593Smuzhiyun unsigned long *devm_bitmap_alloc(struct device *dev,
1275*4882a593Smuzhiyun 				 unsigned int nbits, gfp_t flags)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun 	unsigned long *bitmap;
1278*4882a593Smuzhiyun 	int ret;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	bitmap = bitmap_alloc(nbits, flags);
1281*4882a593Smuzhiyun 	if (!bitmap)
1282*4882a593Smuzhiyun 		return NULL;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
1285*4882a593Smuzhiyun 	if (ret)
1286*4882a593Smuzhiyun 		return NULL;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	return bitmap;
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_bitmap_alloc);
1291*4882a593Smuzhiyun 
devm_bitmap_zalloc(struct device * dev,unsigned int nbits,gfp_t flags)1292*4882a593Smuzhiyun unsigned long *devm_bitmap_zalloc(struct device *dev,
1293*4882a593Smuzhiyun 				  unsigned int nbits, gfp_t flags)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun 	return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun #if BITS_PER_LONG == 64
1300*4882a593Smuzhiyun /**
1301*4882a593Smuzhiyun  * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
1302*4882a593Smuzhiyun  *	@bitmap: array of unsigned longs, the destination bitmap
1303*4882a593Smuzhiyun  *	@buf: array of u32 (in host byte order), the source bitmap
1304*4882a593Smuzhiyun  *	@nbits: number of bits in @bitmap
1305*4882a593Smuzhiyun  */
bitmap_from_arr32(unsigned long * bitmap,const u32 * buf,unsigned int nbits)1306*4882a593Smuzhiyun void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits)
1307*4882a593Smuzhiyun {
1308*4882a593Smuzhiyun 	unsigned int i, halfwords;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	halfwords = DIV_ROUND_UP(nbits, 32);
1311*4882a593Smuzhiyun 	for (i = 0; i < halfwords; i++) {
1312*4882a593Smuzhiyun 		bitmap[i/2] = (unsigned long) buf[i];
1313*4882a593Smuzhiyun 		if (++i < halfwords)
1314*4882a593Smuzhiyun 			bitmap[i/2] |= ((unsigned long) buf[i]) << 32;
1315*4882a593Smuzhiyun 	}
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	/* Clear tail bits in last word beyond nbits. */
1318*4882a593Smuzhiyun 	if (nbits % BITS_PER_LONG)
1319*4882a593Smuzhiyun 		bitmap[(halfwords - 1) / 2] &= BITMAP_LAST_WORD_MASK(nbits);
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_from_arr32);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun /**
1324*4882a593Smuzhiyun  * bitmap_to_arr32 - copy the contents of bitmap to a u32 array of bits
1325*4882a593Smuzhiyun  *	@buf: array of u32 (in host byte order), the dest bitmap
1326*4882a593Smuzhiyun  *	@bitmap: array of unsigned longs, the source bitmap
1327*4882a593Smuzhiyun  *	@nbits: number of bits in @bitmap
1328*4882a593Smuzhiyun  */
bitmap_to_arr32(u32 * buf,const unsigned long * bitmap,unsigned int nbits)1329*4882a593Smuzhiyun void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits)
1330*4882a593Smuzhiyun {
1331*4882a593Smuzhiyun 	unsigned int i, halfwords;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	halfwords = DIV_ROUND_UP(nbits, 32);
1334*4882a593Smuzhiyun 	for (i = 0; i < halfwords; i++) {
1335*4882a593Smuzhiyun 		buf[i] = (u32) (bitmap[i/2] & UINT_MAX);
1336*4882a593Smuzhiyun 		if (++i < halfwords)
1337*4882a593Smuzhiyun 			buf[i] = (u32) (bitmap[i/2] >> 32);
1338*4882a593Smuzhiyun 	}
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	/* Clear tail bits in last element of array beyond nbits. */
1341*4882a593Smuzhiyun 	if (nbits % BITS_PER_LONG)
1342*4882a593Smuzhiyun 		buf[halfwords - 1] &= (u32) (UINT_MAX >> ((-nbits) & 31));
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun EXPORT_SYMBOL(bitmap_to_arr32);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun #endif
1347