1*aae06fc1SYury Norov // SPDX-License-Identifier: GPL-2.0-only
2*aae06fc1SYury Norov
3*aae06fc1SYury Norov #include <linux/bitmap.h>
4*aae06fc1SYury Norov #include <linux/ctype.h>
5*aae06fc1SYury Norov #include <linux/errno.h>
6*aae06fc1SYury Norov #include <linux/err.h>
7*aae06fc1SYury Norov #include <linux/export.h>
8*aae06fc1SYury Norov #include <linux/hex.h>
9*aae06fc1SYury Norov #include <linux/kernel.h>
10*aae06fc1SYury Norov #include <linux/mm.h>
11*aae06fc1SYury Norov #include <linux/string.h>
12*aae06fc1SYury Norov
13*aae06fc1SYury Norov #include "kstrtox.h"
14*aae06fc1SYury Norov
15*aae06fc1SYury Norov /**
16*aae06fc1SYury Norov * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
17*aae06fc1SYury Norov *
18*aae06fc1SYury Norov * @ubuf: pointer to user buffer containing string.
19*aae06fc1SYury Norov * @ulen: buffer size in bytes. If string is smaller than this
20*aae06fc1SYury Norov * then it must be terminated with a \0.
21*aae06fc1SYury Norov * @maskp: pointer to bitmap array that will contain result.
22*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits.
23*aae06fc1SYury Norov */
bitmap_parse_user(const char __user * ubuf,unsigned int ulen,unsigned long * maskp,int nmaskbits)24*aae06fc1SYury Norov int bitmap_parse_user(const char __user *ubuf,
25*aae06fc1SYury Norov unsigned int ulen, unsigned long *maskp,
26*aae06fc1SYury Norov int nmaskbits)
27*aae06fc1SYury Norov {
28*aae06fc1SYury Norov char *buf;
29*aae06fc1SYury Norov int ret;
30*aae06fc1SYury Norov
31*aae06fc1SYury Norov buf = memdup_user_nul(ubuf, ulen);
32*aae06fc1SYury Norov if (IS_ERR(buf))
33*aae06fc1SYury Norov return PTR_ERR(buf);
34*aae06fc1SYury Norov
35*aae06fc1SYury Norov ret = bitmap_parse(buf, UINT_MAX, maskp, nmaskbits);
36*aae06fc1SYury Norov
37*aae06fc1SYury Norov kfree(buf);
38*aae06fc1SYury Norov return ret;
39*aae06fc1SYury Norov }
40*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_parse_user);
41*aae06fc1SYury Norov
42*aae06fc1SYury Norov /**
43*aae06fc1SYury Norov * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
44*aae06fc1SYury Norov * @list: indicates whether the bitmap must be list
45*aae06fc1SYury Norov * @buf: page aligned buffer into which string is placed
46*aae06fc1SYury Norov * @maskp: pointer to bitmap to convert
47*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits
48*aae06fc1SYury Norov *
49*aae06fc1SYury Norov * Output format is a comma-separated list of decimal numbers and
50*aae06fc1SYury Norov * ranges if list is specified or hex digits grouped into comma-separated
51*aae06fc1SYury Norov * sets of 8 digits/set. Returns the number of characters written to buf.
52*aae06fc1SYury Norov *
53*aae06fc1SYury Norov * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
54*aae06fc1SYury Norov * area and that sufficient storage remains at @buf to accommodate the
55*aae06fc1SYury Norov * bitmap_print_to_pagebuf() output. Returns the number of characters
56*aae06fc1SYury Norov * actually printed to @buf, excluding terminating '\0'.
57*aae06fc1SYury Norov */
bitmap_print_to_pagebuf(bool list,char * buf,const unsigned long * maskp,int nmaskbits)58*aae06fc1SYury Norov int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
59*aae06fc1SYury Norov int nmaskbits)
60*aae06fc1SYury Norov {
61*aae06fc1SYury Norov ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
62*aae06fc1SYury Norov
63*aae06fc1SYury Norov return list ? scnprintf(buf, len, "%*pbl\n", nmaskbits, maskp) :
64*aae06fc1SYury Norov scnprintf(buf, len, "%*pb\n", nmaskbits, maskp);
65*aae06fc1SYury Norov }
66*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_print_to_pagebuf);
67*aae06fc1SYury Norov
68*aae06fc1SYury Norov /**
69*aae06fc1SYury Norov * bitmap_print_to_buf - convert bitmap to list or hex format ASCII string
70*aae06fc1SYury Norov * @list: indicates whether the bitmap must be list
71*aae06fc1SYury Norov * true: print in decimal list format
72*aae06fc1SYury Norov * false: print in hexadecimal bitmask format
73*aae06fc1SYury Norov * @buf: buffer into which string is placed
74*aae06fc1SYury Norov * @maskp: pointer to bitmap to convert
75*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits
76*aae06fc1SYury Norov * @off: in the string from which we are copying, We copy to @buf
77*aae06fc1SYury Norov * @count: the maximum number of bytes to print
78*aae06fc1SYury Norov */
bitmap_print_to_buf(bool list,char * buf,const unsigned long * maskp,int nmaskbits,loff_t off,size_t count)79*aae06fc1SYury Norov static int bitmap_print_to_buf(bool list, char *buf, const unsigned long *maskp,
80*aae06fc1SYury Norov int nmaskbits, loff_t off, size_t count)
81*aae06fc1SYury Norov {
82*aae06fc1SYury Norov const char *fmt = list ? "%*pbl\n" : "%*pb\n";
83*aae06fc1SYury Norov ssize_t size;
84*aae06fc1SYury Norov void *data;
85*aae06fc1SYury Norov
86*aae06fc1SYury Norov data = kasprintf(GFP_KERNEL, fmt, nmaskbits, maskp);
87*aae06fc1SYury Norov if (!data)
88*aae06fc1SYury Norov return -ENOMEM;
89*aae06fc1SYury Norov
90*aae06fc1SYury Norov size = memory_read_from_buffer(buf, count, &off, data, strlen(data) + 1);
91*aae06fc1SYury Norov kfree(data);
92*aae06fc1SYury Norov
93*aae06fc1SYury Norov return size;
94*aae06fc1SYury Norov }
95*aae06fc1SYury Norov
96*aae06fc1SYury Norov /**
97*aae06fc1SYury Norov * bitmap_print_bitmask_to_buf - convert bitmap to hex bitmask format ASCII string
98*aae06fc1SYury Norov * @buf: buffer into which string is placed
99*aae06fc1SYury Norov * @maskp: pointer to bitmap to convert
100*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits
101*aae06fc1SYury Norov * @off: in the string from which we are copying, We copy to @buf
102*aae06fc1SYury Norov * @count: the maximum number of bytes to print
103*aae06fc1SYury Norov *
104*aae06fc1SYury Norov * The bitmap_print_to_pagebuf() is used indirectly via its cpumap wrapper
105*aae06fc1SYury Norov * cpumap_print_to_pagebuf() or directly by drivers to export hexadecimal
106*aae06fc1SYury Norov * bitmask and decimal list to userspace by sysfs ABI.
107*aae06fc1SYury Norov * Drivers might be using a normal attribute for this kind of ABIs. A
108*aae06fc1SYury Norov * normal attribute typically has show entry as below::
109*aae06fc1SYury Norov *
110*aae06fc1SYury Norov * static ssize_t example_attribute_show(struct device *dev,
111*aae06fc1SYury Norov * struct device_attribute *attr, char *buf)
112*aae06fc1SYury Norov * {
113*aae06fc1SYury Norov * ...
114*aae06fc1SYury Norov * return bitmap_print_to_pagebuf(true, buf, &mask, nr_trig_max);
115*aae06fc1SYury Norov * }
116*aae06fc1SYury Norov *
117*aae06fc1SYury Norov * show entry of attribute has no offset and count parameters and this
118*aae06fc1SYury Norov * means the file is limited to one page only.
119*aae06fc1SYury Norov * bitmap_print_to_pagebuf() API works terribly well for this kind of
120*aae06fc1SYury Norov * normal attribute with buf parameter and without offset, count::
121*aae06fc1SYury Norov *
122*aae06fc1SYury Norov * bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
123*aae06fc1SYury Norov * int nmaskbits)
124*aae06fc1SYury Norov * {
125*aae06fc1SYury Norov * }
126*aae06fc1SYury Norov *
127*aae06fc1SYury Norov * The problem is once we have a large bitmap, we have a chance to get a
128*aae06fc1SYury Norov * bitmask or list more than one page. Especially for list, it could be
129*aae06fc1SYury Norov * as complex as 0,3,5,7,9,... We have no simple way to know it exact size.
130*aae06fc1SYury Norov * It turns out bin_attribute is a way to break this limit. bin_attribute
131*aae06fc1SYury Norov * has show entry as below::
132*aae06fc1SYury Norov *
133*aae06fc1SYury Norov * static ssize_t
134*aae06fc1SYury Norov * example_bin_attribute_show(struct file *filp, struct kobject *kobj,
135*aae06fc1SYury Norov * struct bin_attribute *attr, char *buf,
136*aae06fc1SYury Norov * loff_t offset, size_t count)
137*aae06fc1SYury Norov * {
138*aae06fc1SYury Norov * ...
139*aae06fc1SYury Norov * }
140*aae06fc1SYury Norov *
141*aae06fc1SYury Norov * With the new offset and count parameters, this makes sysfs ABI be able
142*aae06fc1SYury Norov * to support file size more than one page. For example, offset could be
143*aae06fc1SYury Norov * >= 4096.
144*aae06fc1SYury Norov * bitmap_print_bitmask_to_buf(), bitmap_print_list_to_buf() wit their
145*aae06fc1SYury Norov * cpumap wrapper cpumap_print_bitmask_to_buf(), cpumap_print_list_to_buf()
146*aae06fc1SYury Norov * make those drivers be able to support large bitmask and list after they
147*aae06fc1SYury Norov * move to use bin_attribute. In result, we have to pass the corresponding
148*aae06fc1SYury Norov * parameters such as off, count from bin_attribute show entry to this API.
149*aae06fc1SYury Norov *
150*aae06fc1SYury Norov * The role of cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf()
151*aae06fc1SYury Norov * is similar with cpumap_print_to_pagebuf(), the difference is that
152*aae06fc1SYury Norov * bitmap_print_to_pagebuf() mainly serves sysfs attribute with the assumption
153*aae06fc1SYury Norov * the destination buffer is exactly one page and won't be more than one page.
154*aae06fc1SYury Norov * cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf(), on the other
155*aae06fc1SYury Norov * hand, mainly serves bin_attribute which doesn't work with exact one page,
156*aae06fc1SYury Norov * and it can break the size limit of converted decimal list and hexadecimal
157*aae06fc1SYury Norov * bitmask.
158*aae06fc1SYury Norov *
159*aae06fc1SYury Norov * WARNING!
160*aae06fc1SYury Norov *
161*aae06fc1SYury Norov * This function is not a replacement for sprintf() or bitmap_print_to_pagebuf().
162*aae06fc1SYury Norov * It is intended to workaround sysfs limitations discussed above and should be
163*aae06fc1SYury Norov * used carefully in general case for the following reasons:
164*aae06fc1SYury Norov *
165*aae06fc1SYury Norov * - Time complexity is O(nbits^2/count), comparing to O(nbits) for snprintf().
166*aae06fc1SYury Norov * - Memory complexity is O(nbits), comparing to O(1) for snprintf().
167*aae06fc1SYury Norov * - @off and @count are NOT offset and number of bits to print.
168*aae06fc1SYury Norov * - If printing part of bitmap as list, the resulting string is not a correct
169*aae06fc1SYury Norov * list representation of bitmap. Particularly, some bits within or out of
170*aae06fc1SYury Norov * related interval may be erroneously set or unset. The format of the string
171*aae06fc1SYury Norov * may be broken, so bitmap_parselist-like parser may fail parsing it.
172*aae06fc1SYury Norov * - If printing the whole bitmap as list by parts, user must ensure the order
173*aae06fc1SYury Norov * of calls of the function such that the offset is incremented linearly.
174*aae06fc1SYury Norov * - If printing the whole bitmap as list by parts, user must keep bitmap
175*aae06fc1SYury Norov * unchanged between the very first and very last call. Otherwise concatenated
176*aae06fc1SYury Norov * result may be incorrect, and format may be broken.
177*aae06fc1SYury Norov *
178*aae06fc1SYury Norov * Returns the number of characters actually printed to @buf
179*aae06fc1SYury Norov */
bitmap_print_bitmask_to_buf(char * buf,const unsigned long * maskp,int nmaskbits,loff_t off,size_t count)180*aae06fc1SYury Norov int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
181*aae06fc1SYury Norov int nmaskbits, loff_t off, size_t count)
182*aae06fc1SYury Norov {
183*aae06fc1SYury Norov return bitmap_print_to_buf(false, buf, maskp, nmaskbits, off, count);
184*aae06fc1SYury Norov }
185*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_print_bitmask_to_buf);
186*aae06fc1SYury Norov
187*aae06fc1SYury Norov /**
188*aae06fc1SYury Norov * bitmap_print_list_to_buf - convert bitmap to decimal list format ASCII string
189*aae06fc1SYury Norov * @buf: buffer into which string is placed
190*aae06fc1SYury Norov * @maskp: pointer to bitmap to convert
191*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits
192*aae06fc1SYury Norov * @off: in the string from which we are copying, We copy to @buf
193*aae06fc1SYury Norov * @count: the maximum number of bytes to print
194*aae06fc1SYury Norov *
195*aae06fc1SYury Norov * Everything is same with the above bitmap_print_bitmask_to_buf() except
196*aae06fc1SYury Norov * the print format.
197*aae06fc1SYury Norov */
bitmap_print_list_to_buf(char * buf,const unsigned long * maskp,int nmaskbits,loff_t off,size_t count)198*aae06fc1SYury Norov int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
199*aae06fc1SYury Norov int nmaskbits, loff_t off, size_t count)
200*aae06fc1SYury Norov {
201*aae06fc1SYury Norov return bitmap_print_to_buf(true, buf, maskp, nmaskbits, off, count);
202*aae06fc1SYury Norov }
203*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_print_list_to_buf);
204*aae06fc1SYury Norov
205*aae06fc1SYury Norov /*
206*aae06fc1SYury Norov * Region 9-38:4/10 describes the following bitmap structure:
207*aae06fc1SYury Norov * 0 9 12 18 38 N
208*aae06fc1SYury Norov * .........****......****......****..................
209*aae06fc1SYury Norov * ^ ^ ^ ^ ^
210*aae06fc1SYury Norov * start off group_len end nbits
211*aae06fc1SYury Norov */
212*aae06fc1SYury Norov struct region {
213*aae06fc1SYury Norov unsigned int start;
214*aae06fc1SYury Norov unsigned int off;
215*aae06fc1SYury Norov unsigned int group_len;
216*aae06fc1SYury Norov unsigned int end;
217*aae06fc1SYury Norov unsigned int nbits;
218*aae06fc1SYury Norov };
219*aae06fc1SYury Norov
bitmap_set_region(const struct region * r,unsigned long * bitmap)220*aae06fc1SYury Norov static void bitmap_set_region(const struct region *r, unsigned long *bitmap)
221*aae06fc1SYury Norov {
222*aae06fc1SYury Norov unsigned int start;
223*aae06fc1SYury Norov
224*aae06fc1SYury Norov for (start = r->start; start <= r->end; start += r->group_len)
225*aae06fc1SYury Norov bitmap_set(bitmap, start, min(r->end - start + 1, r->off));
226*aae06fc1SYury Norov }
227*aae06fc1SYury Norov
bitmap_check_region(const struct region * r)228*aae06fc1SYury Norov static int bitmap_check_region(const struct region *r)
229*aae06fc1SYury Norov {
230*aae06fc1SYury Norov if (r->start > r->end || r->group_len == 0 || r->off > r->group_len)
231*aae06fc1SYury Norov return -EINVAL;
232*aae06fc1SYury Norov
233*aae06fc1SYury Norov if (r->end >= r->nbits)
234*aae06fc1SYury Norov return -ERANGE;
235*aae06fc1SYury Norov
236*aae06fc1SYury Norov return 0;
237*aae06fc1SYury Norov }
238*aae06fc1SYury Norov
bitmap_getnum(const char * str,unsigned int * num,unsigned int lastbit)239*aae06fc1SYury Norov static const char *bitmap_getnum(const char *str, unsigned int *num,
240*aae06fc1SYury Norov unsigned int lastbit)
241*aae06fc1SYury Norov {
242*aae06fc1SYury Norov unsigned long long n;
243*aae06fc1SYury Norov unsigned int len;
244*aae06fc1SYury Norov
245*aae06fc1SYury Norov if (str[0] == 'N') {
246*aae06fc1SYury Norov *num = lastbit;
247*aae06fc1SYury Norov return str + 1;
248*aae06fc1SYury Norov }
249*aae06fc1SYury Norov
250*aae06fc1SYury Norov len = _parse_integer(str, 10, &n);
251*aae06fc1SYury Norov if (!len)
252*aae06fc1SYury Norov return ERR_PTR(-EINVAL);
253*aae06fc1SYury Norov if (len & KSTRTOX_OVERFLOW || n != (unsigned int)n)
254*aae06fc1SYury Norov return ERR_PTR(-EOVERFLOW);
255*aae06fc1SYury Norov
256*aae06fc1SYury Norov *num = n;
257*aae06fc1SYury Norov return str + len;
258*aae06fc1SYury Norov }
259*aae06fc1SYury Norov
end_of_str(char c)260*aae06fc1SYury Norov static inline bool end_of_str(char c)
261*aae06fc1SYury Norov {
262*aae06fc1SYury Norov return c == '\0' || c == '\n';
263*aae06fc1SYury Norov }
264*aae06fc1SYury Norov
__end_of_region(char c)265*aae06fc1SYury Norov static inline bool __end_of_region(char c)
266*aae06fc1SYury Norov {
267*aae06fc1SYury Norov return isspace(c) || c == ',';
268*aae06fc1SYury Norov }
269*aae06fc1SYury Norov
end_of_region(char c)270*aae06fc1SYury Norov static inline bool end_of_region(char c)
271*aae06fc1SYury Norov {
272*aae06fc1SYury Norov return __end_of_region(c) || end_of_str(c);
273*aae06fc1SYury Norov }
274*aae06fc1SYury Norov
275*aae06fc1SYury Norov /*
276*aae06fc1SYury Norov * The format allows commas and whitespaces at the beginning
277*aae06fc1SYury Norov * of the region.
278*aae06fc1SYury Norov */
bitmap_find_region(const char * str)279*aae06fc1SYury Norov static const char *bitmap_find_region(const char *str)
280*aae06fc1SYury Norov {
281*aae06fc1SYury Norov while (__end_of_region(*str))
282*aae06fc1SYury Norov str++;
283*aae06fc1SYury Norov
284*aae06fc1SYury Norov return end_of_str(*str) ? NULL : str;
285*aae06fc1SYury Norov }
286*aae06fc1SYury Norov
bitmap_find_region_reverse(const char * start,const char * end)287*aae06fc1SYury Norov static const char *bitmap_find_region_reverse(const char *start, const char *end)
288*aae06fc1SYury Norov {
289*aae06fc1SYury Norov while (start <= end && __end_of_region(*end))
290*aae06fc1SYury Norov end--;
291*aae06fc1SYury Norov
292*aae06fc1SYury Norov return end;
293*aae06fc1SYury Norov }
294*aae06fc1SYury Norov
bitmap_parse_region(const char * str,struct region * r)295*aae06fc1SYury Norov static const char *bitmap_parse_region(const char *str, struct region *r)
296*aae06fc1SYury Norov {
297*aae06fc1SYury Norov unsigned int lastbit = r->nbits - 1;
298*aae06fc1SYury Norov
299*aae06fc1SYury Norov if (!strncasecmp(str, "all", 3)) {
300*aae06fc1SYury Norov r->start = 0;
301*aae06fc1SYury Norov r->end = lastbit;
302*aae06fc1SYury Norov str += 3;
303*aae06fc1SYury Norov
304*aae06fc1SYury Norov goto check_pattern;
305*aae06fc1SYury Norov }
306*aae06fc1SYury Norov
307*aae06fc1SYury Norov str = bitmap_getnum(str, &r->start, lastbit);
308*aae06fc1SYury Norov if (IS_ERR(str))
309*aae06fc1SYury Norov return str;
310*aae06fc1SYury Norov
311*aae06fc1SYury Norov if (end_of_region(*str))
312*aae06fc1SYury Norov goto no_end;
313*aae06fc1SYury Norov
314*aae06fc1SYury Norov if (*str != '-')
315*aae06fc1SYury Norov return ERR_PTR(-EINVAL);
316*aae06fc1SYury Norov
317*aae06fc1SYury Norov str = bitmap_getnum(str + 1, &r->end, lastbit);
318*aae06fc1SYury Norov if (IS_ERR(str))
319*aae06fc1SYury Norov return str;
320*aae06fc1SYury Norov
321*aae06fc1SYury Norov check_pattern:
322*aae06fc1SYury Norov if (end_of_region(*str))
323*aae06fc1SYury Norov goto no_pattern;
324*aae06fc1SYury Norov
325*aae06fc1SYury Norov if (*str != ':')
326*aae06fc1SYury Norov return ERR_PTR(-EINVAL);
327*aae06fc1SYury Norov
328*aae06fc1SYury Norov str = bitmap_getnum(str + 1, &r->off, lastbit);
329*aae06fc1SYury Norov if (IS_ERR(str))
330*aae06fc1SYury Norov return str;
331*aae06fc1SYury Norov
332*aae06fc1SYury Norov if (*str != '/')
333*aae06fc1SYury Norov return ERR_PTR(-EINVAL);
334*aae06fc1SYury Norov
335*aae06fc1SYury Norov return bitmap_getnum(str + 1, &r->group_len, lastbit);
336*aae06fc1SYury Norov
337*aae06fc1SYury Norov no_end:
338*aae06fc1SYury Norov r->end = r->start;
339*aae06fc1SYury Norov no_pattern:
340*aae06fc1SYury Norov r->off = r->end + 1;
341*aae06fc1SYury Norov r->group_len = r->end + 1;
342*aae06fc1SYury Norov
343*aae06fc1SYury Norov return end_of_str(*str) ? NULL : str;
344*aae06fc1SYury Norov }
345*aae06fc1SYury Norov
346*aae06fc1SYury Norov /**
347*aae06fc1SYury Norov * bitmap_parselist - convert list format ASCII string to bitmap
348*aae06fc1SYury Norov * @buf: read user string from this buffer; must be terminated
349*aae06fc1SYury Norov * with a \0 or \n.
350*aae06fc1SYury Norov * @maskp: write resulting mask here
351*aae06fc1SYury Norov * @nmaskbits: number of bits in mask to be written
352*aae06fc1SYury Norov *
353*aae06fc1SYury Norov * Input format is a comma-separated list of decimal numbers and
354*aae06fc1SYury Norov * ranges. Consecutively set bits are shown as two hyphen-separated
355*aae06fc1SYury Norov * decimal numbers, the smallest and largest bit numbers set in
356*aae06fc1SYury Norov * the range.
357*aae06fc1SYury Norov * Optionally each range can be postfixed to denote that only parts of it
358*aae06fc1SYury Norov * should be set. The range will divided to groups of specific size.
359*aae06fc1SYury Norov * From each group will be used only defined amount of bits.
360*aae06fc1SYury Norov * Syntax: range:used_size/group_size
361*aae06fc1SYury Norov * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
362*aae06fc1SYury Norov * The value 'N' can be used as a dynamically substituted token for the
363*aae06fc1SYury Norov * maximum allowed value; i.e (nmaskbits - 1). Keep in mind that it is
364*aae06fc1SYury Norov * dynamic, so if system changes cause the bitmap width to change, such
365*aae06fc1SYury Norov * as more cores in a CPU list, then any ranges using N will also change.
366*aae06fc1SYury Norov *
367*aae06fc1SYury Norov * Returns: 0 on success, -errno on invalid input strings. Error values:
368*aae06fc1SYury Norov *
369*aae06fc1SYury Norov * - ``-EINVAL``: wrong region format
370*aae06fc1SYury Norov * - ``-EINVAL``: invalid character in string
371*aae06fc1SYury Norov * - ``-ERANGE``: bit number specified too large for mask
372*aae06fc1SYury Norov * - ``-EOVERFLOW``: integer overflow in the input parameters
373*aae06fc1SYury Norov */
bitmap_parselist(const char * buf,unsigned long * maskp,int nmaskbits)374*aae06fc1SYury Norov int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits)
375*aae06fc1SYury Norov {
376*aae06fc1SYury Norov struct region r;
377*aae06fc1SYury Norov long ret;
378*aae06fc1SYury Norov
379*aae06fc1SYury Norov r.nbits = nmaskbits;
380*aae06fc1SYury Norov bitmap_zero(maskp, r.nbits);
381*aae06fc1SYury Norov
382*aae06fc1SYury Norov while (buf) {
383*aae06fc1SYury Norov buf = bitmap_find_region(buf);
384*aae06fc1SYury Norov if (buf == NULL)
385*aae06fc1SYury Norov return 0;
386*aae06fc1SYury Norov
387*aae06fc1SYury Norov buf = bitmap_parse_region(buf, &r);
388*aae06fc1SYury Norov if (IS_ERR(buf))
389*aae06fc1SYury Norov return PTR_ERR(buf);
390*aae06fc1SYury Norov
391*aae06fc1SYury Norov ret = bitmap_check_region(&r);
392*aae06fc1SYury Norov if (ret)
393*aae06fc1SYury Norov return ret;
394*aae06fc1SYury Norov
395*aae06fc1SYury Norov bitmap_set_region(&r, maskp);
396*aae06fc1SYury Norov }
397*aae06fc1SYury Norov
398*aae06fc1SYury Norov return 0;
399*aae06fc1SYury Norov }
400*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_parselist);
401*aae06fc1SYury Norov
402*aae06fc1SYury Norov
403*aae06fc1SYury Norov /**
404*aae06fc1SYury Norov * bitmap_parselist_user() - convert user buffer's list format ASCII
405*aae06fc1SYury Norov * string to bitmap
406*aae06fc1SYury Norov *
407*aae06fc1SYury Norov * @ubuf: pointer to user buffer containing string.
408*aae06fc1SYury Norov * @ulen: buffer size in bytes. If string is smaller than this
409*aae06fc1SYury Norov * then it must be terminated with a \0.
410*aae06fc1SYury Norov * @maskp: pointer to bitmap array that will contain result.
411*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits.
412*aae06fc1SYury Norov *
413*aae06fc1SYury Norov * Wrapper for bitmap_parselist(), providing it with user buffer.
414*aae06fc1SYury Norov */
bitmap_parselist_user(const char __user * ubuf,unsigned int ulen,unsigned long * maskp,int nmaskbits)415*aae06fc1SYury Norov int bitmap_parselist_user(const char __user *ubuf,
416*aae06fc1SYury Norov unsigned int ulen, unsigned long *maskp,
417*aae06fc1SYury Norov int nmaskbits)
418*aae06fc1SYury Norov {
419*aae06fc1SYury Norov char *buf;
420*aae06fc1SYury Norov int ret;
421*aae06fc1SYury Norov
422*aae06fc1SYury Norov buf = memdup_user_nul(ubuf, ulen);
423*aae06fc1SYury Norov if (IS_ERR(buf))
424*aae06fc1SYury Norov return PTR_ERR(buf);
425*aae06fc1SYury Norov
426*aae06fc1SYury Norov ret = bitmap_parselist(buf, maskp, nmaskbits);
427*aae06fc1SYury Norov
428*aae06fc1SYury Norov kfree(buf);
429*aae06fc1SYury Norov return ret;
430*aae06fc1SYury Norov }
431*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_parselist_user);
432*aae06fc1SYury Norov
bitmap_get_x32_reverse(const char * start,const char * end,u32 * num)433*aae06fc1SYury Norov static const char *bitmap_get_x32_reverse(const char *start,
434*aae06fc1SYury Norov const char *end, u32 *num)
435*aae06fc1SYury Norov {
436*aae06fc1SYury Norov u32 ret = 0;
437*aae06fc1SYury Norov int c, i;
438*aae06fc1SYury Norov
439*aae06fc1SYury Norov for (i = 0; i < 32; i += 4) {
440*aae06fc1SYury Norov c = hex_to_bin(*end--);
441*aae06fc1SYury Norov if (c < 0)
442*aae06fc1SYury Norov return ERR_PTR(-EINVAL);
443*aae06fc1SYury Norov
444*aae06fc1SYury Norov ret |= c << i;
445*aae06fc1SYury Norov
446*aae06fc1SYury Norov if (start > end || __end_of_region(*end))
447*aae06fc1SYury Norov goto out;
448*aae06fc1SYury Norov }
449*aae06fc1SYury Norov
450*aae06fc1SYury Norov if (hex_to_bin(*end--) >= 0)
451*aae06fc1SYury Norov return ERR_PTR(-EOVERFLOW);
452*aae06fc1SYury Norov out:
453*aae06fc1SYury Norov *num = ret;
454*aae06fc1SYury Norov return end;
455*aae06fc1SYury Norov }
456*aae06fc1SYury Norov
457*aae06fc1SYury Norov /**
458*aae06fc1SYury Norov * bitmap_parse - convert an ASCII hex string into a bitmap.
459*aae06fc1SYury Norov * @start: pointer to buffer containing string.
460*aae06fc1SYury Norov * @buflen: buffer size in bytes. If string is smaller than this
461*aae06fc1SYury Norov * then it must be terminated with a \0 or \n. In that case,
462*aae06fc1SYury Norov * UINT_MAX may be provided instead of string length.
463*aae06fc1SYury Norov * @maskp: pointer to bitmap array that will contain result.
464*aae06fc1SYury Norov * @nmaskbits: size of bitmap, in bits.
465*aae06fc1SYury Norov *
466*aae06fc1SYury Norov * Commas group hex digits into chunks. Each chunk defines exactly 32
467*aae06fc1SYury Norov * bits of the resultant bitmask. No chunk may specify a value larger
468*aae06fc1SYury Norov * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
469*aae06fc1SYury Norov * then leading 0-bits are prepended. %-EINVAL is returned for illegal
470*aae06fc1SYury Norov * characters. Grouping such as "1,,5", ",44", "," or "" is allowed.
471*aae06fc1SYury Norov * Leading, embedded and trailing whitespace accepted.
472*aae06fc1SYury Norov */
bitmap_parse(const char * start,unsigned int buflen,unsigned long * maskp,int nmaskbits)473*aae06fc1SYury Norov int bitmap_parse(const char *start, unsigned int buflen,
474*aae06fc1SYury Norov unsigned long *maskp, int nmaskbits)
475*aae06fc1SYury Norov {
476*aae06fc1SYury Norov const char *end = strnchrnul(start, buflen, '\n') - 1;
477*aae06fc1SYury Norov int chunks = BITS_TO_U32(nmaskbits);
478*aae06fc1SYury Norov u32 *bitmap = (u32 *)maskp;
479*aae06fc1SYury Norov int unset_bit;
480*aae06fc1SYury Norov int chunk;
481*aae06fc1SYury Norov
482*aae06fc1SYury Norov for (chunk = 0; ; chunk++) {
483*aae06fc1SYury Norov end = bitmap_find_region_reverse(start, end);
484*aae06fc1SYury Norov if (start > end)
485*aae06fc1SYury Norov break;
486*aae06fc1SYury Norov
487*aae06fc1SYury Norov if (!chunks--)
488*aae06fc1SYury Norov return -EOVERFLOW;
489*aae06fc1SYury Norov
490*aae06fc1SYury Norov #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
491*aae06fc1SYury Norov end = bitmap_get_x32_reverse(start, end, &bitmap[chunk ^ 1]);
492*aae06fc1SYury Norov #else
493*aae06fc1SYury Norov end = bitmap_get_x32_reverse(start, end, &bitmap[chunk]);
494*aae06fc1SYury Norov #endif
495*aae06fc1SYury Norov if (IS_ERR(end))
496*aae06fc1SYury Norov return PTR_ERR(end);
497*aae06fc1SYury Norov }
498*aae06fc1SYury Norov
499*aae06fc1SYury Norov unset_bit = (BITS_TO_U32(nmaskbits) - chunks) * 32;
500*aae06fc1SYury Norov if (unset_bit < nmaskbits) {
501*aae06fc1SYury Norov bitmap_clear(maskp, unset_bit, nmaskbits - unset_bit);
502*aae06fc1SYury Norov return 0;
503*aae06fc1SYury Norov }
504*aae06fc1SYury Norov
505*aae06fc1SYury Norov if (find_next_bit(maskp, unset_bit, nmaskbits) != unset_bit)
506*aae06fc1SYury Norov return -EOVERFLOW;
507*aae06fc1SYury Norov
508*aae06fc1SYury Norov return 0;
509*aae06fc1SYury Norov }
510*aae06fc1SYury Norov EXPORT_SYMBOL(bitmap_parse);
511