1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/bitmap.h>
4 #include <linux/ctype.h>
5 #include <linux/errno.h>
7 #include <linux/export.h>
9 #include <linux/kernel.h>
11 #include <linux/string.h>
16 * bitmap_parse_user - convert an ASCII hex string in a user buffer into a bitmap
18 * @ubuf: pointer to user buffer containing string.
19 * @ulen: buffer size in bytes. If string is smaller than this
20 * then it must be terminated with a \0.
21 * @maskp: pointer to bitmap array that will contain result.
22 * @nmaskbits: size of bitmap, in bits.
24 int bitmap_parse_user(const char __user
*ubuf
,
25 unsigned int ulen
, unsigned long *maskp
,
31 buf
= memdup_user_nul(ubuf
, ulen
);
35 ret
= bitmap_parse(buf
, UINT_MAX
, maskp
, nmaskbits
);
40 EXPORT_SYMBOL(bitmap_parse_user
);
43 * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
44 * @list: indicates whether the bitmap must be list
45 * @buf: page aligned buffer into which string is placed
46 * @maskp: pointer to bitmap to convert
47 * @nmaskbits: size of bitmap, in bits
49 * Output format is a comma-separated list of decimal numbers and
50 * ranges if list is specified or hex digits grouped into comma-separated
51 * sets of 8 digits/set. Returns the number of characters written to buf.
53 * It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
54 * area and that sufficient storage remains at @buf to accommodate the
55 * bitmap_print_to_pagebuf() output. Returns the number of characters
56 * actually printed to @buf, excluding terminating '\0'.
58 int bitmap_print_to_pagebuf(bool list
, char *buf
, const unsigned long *maskp
,
61 ptrdiff_t len
= PAGE_SIZE
- offset_in_page(buf
);
63 return list
? scnprintf(buf
, len
, "%*pbl\n", nmaskbits
, maskp
) :
64 scnprintf(buf
, len
, "%*pb\n", nmaskbits
, maskp
);
66 EXPORT_SYMBOL(bitmap_print_to_pagebuf
);
69 * bitmap_print_to_buf - convert bitmap to list or hex format ASCII string
70 * @list: indicates whether the bitmap must be list
71 * true: print in decimal list format
72 * false: print in hexadecimal bitmask format
73 * @buf: buffer into which string is placed
74 * @maskp: pointer to bitmap to convert
75 * @nmaskbits: size of bitmap, in bits
76 * @off: in the string from which we are copying, We copy to @buf
77 * @count: the maximum number of bytes to print
79 static int bitmap_print_to_buf(bool list
, char *buf
, const unsigned long *maskp
,
80 int nmaskbits
, loff_t off
, size_t count
)
82 const char *fmt
= list
? "%*pbl\n" : "%*pb\n";
86 data
= kasprintf(GFP_KERNEL
, fmt
, nmaskbits
, maskp
);
90 size
= memory_read_from_buffer(buf
, count
, &off
, data
, strlen(data
) + 1);
97 * bitmap_print_bitmask_to_buf - convert bitmap to hex bitmask format ASCII string
98 * @buf: buffer into which string is placed
99 * @maskp: pointer to bitmap to convert
100 * @nmaskbits: size of bitmap, in bits
101 * @off: in the string from which we are copying, We copy to @buf
102 * @count: the maximum number of bytes to print
104 * The bitmap_print_to_pagebuf() is used indirectly via its cpumap wrapper
105 * cpumap_print_to_pagebuf() or directly by drivers to export hexadecimal
106 * bitmask and decimal list to userspace by sysfs ABI.
107 * Drivers might be using a normal attribute for this kind of ABIs. A
108 * normal attribute typically has show entry as below::
110 * static ssize_t example_attribute_show(struct device *dev,
111 * struct device_attribute *attr, char *buf)
114 * return bitmap_print_to_pagebuf(true, buf, &mask, nr_trig_max);
117 * show entry of attribute has no offset and count parameters and this
118 * means the file is limited to one page only.
119 * bitmap_print_to_pagebuf() API works terribly well for this kind of
120 * normal attribute with buf parameter and without offset, count::
122 * bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
127 * The problem is once we have a large bitmap, we have a chance to get a
128 * bitmask or list more than one page. Especially for list, it could be
129 * as complex as 0,3,5,7,9,... We have no simple way to know it exact size.
130 * It turns out bin_attribute is a way to break this limit. bin_attribute
131 * has show entry as below::
134 * example_bin_attribute_show(struct file *filp, struct kobject *kobj,
135 * struct bin_attribute *attr, char *buf,
136 * loff_t offset, size_t count)
141 * With the new offset and count parameters, this makes sysfs ABI be able
142 * to support file size more than one page. For example, offset could be
144 * bitmap_print_bitmask_to_buf(), bitmap_print_list_to_buf() wit their
145 * cpumap wrapper cpumap_print_bitmask_to_buf(), cpumap_print_list_to_buf()
146 * make those drivers be able to support large bitmask and list after they
147 * move to use bin_attribute. In result, we have to pass the corresponding
148 * parameters such as off, count from bin_attribute show entry to this API.
150 * The role of cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf()
151 * is similar with cpumap_print_to_pagebuf(), the difference is that
152 * bitmap_print_to_pagebuf() mainly serves sysfs attribute with the assumption
153 * the destination buffer is exactly one page and won't be more than one page.
154 * cpumap_print_bitmask_to_buf() and cpumap_print_list_to_buf(), on the other
155 * hand, mainly serves bin_attribute which doesn't work with exact one page,
156 * and it can break the size limit of converted decimal list and hexadecimal
161 * This function is not a replacement for sprintf() or bitmap_print_to_pagebuf().
162 * It is intended to workaround sysfs limitations discussed above and should be
163 * used carefully in general case for the following reasons:
165 * - Time complexity is O(nbits^2/count), comparing to O(nbits) for snprintf().
166 * - Memory complexity is O(nbits), comparing to O(1) for snprintf().
167 * - @off and @count are NOT offset and number of bits to print.
168 * - If printing part of bitmap as list, the resulting string is not a correct
169 * list representation of bitmap. Particularly, some bits within or out of
170 * related interval may be erroneously set or unset. The format of the string
171 * may be broken, so bitmap_parselist-like parser may fail parsing it.
172 * - If printing the whole bitmap as list by parts, user must ensure the order
173 * of calls of the function such that the offset is incremented linearly.
174 * - If printing the whole bitmap as list by parts, user must keep bitmap
175 * unchanged between the very first and very last call. Otherwise concatenated
176 * result may be incorrect, and format may be broken.
178 * Returns the number of characters actually printed to @buf
180 int bitmap_print_bitmask_to_buf(char *buf
, const unsigned long *maskp
,
181 int nmaskbits
, loff_t off
, size_t count
)
183 return bitmap_print_to_buf(false, buf
, maskp
, nmaskbits
, off
, count
);
185 EXPORT_SYMBOL(bitmap_print_bitmask_to_buf
);
188 * bitmap_print_list_to_buf - convert bitmap to decimal list format ASCII string
189 * @buf: buffer into which string is placed
190 * @maskp: pointer to bitmap to convert
191 * @nmaskbits: size of bitmap, in bits
192 * @off: in the string from which we are copying, We copy to @buf
193 * @count: the maximum number of bytes to print
195 * Everything is same with the above bitmap_print_bitmask_to_buf() except
198 int bitmap_print_list_to_buf(char *buf
, const unsigned long *maskp
,
199 int nmaskbits
, loff_t off
, size_t count
)
201 return bitmap_print_to_buf(true, buf
, maskp
, nmaskbits
, off
, count
);
203 EXPORT_SYMBOL(bitmap_print_list_to_buf
);
206 * Region 9-38:4/10 describes the following bitmap structure:
208 * .........****......****......****..................
210 * start off group_len end nbits
215 unsigned int group_len
;
220 static void bitmap_set_region(const struct region
*r
, unsigned long *bitmap
)
224 for (start
= r
->start
; start
<= r
->end
; start
+= r
->group_len
)
225 bitmap_set(bitmap
, start
, min(r
->end
- start
+ 1, r
->off
));
228 static int bitmap_check_region(const struct region
*r
)
230 if (r
->start
> r
->end
|| r
->group_len
== 0 || r
->off
> r
->group_len
)
233 if (r
->end
>= r
->nbits
)
239 static const char *bitmap_getnum(const char *str
, unsigned int *num
,
240 unsigned int lastbit
)
242 unsigned long long n
;
250 len
= _parse_integer(str
, 10, &n
);
252 return ERR_PTR(-EINVAL
);
253 if (len
& KSTRTOX_OVERFLOW
|| n
!= (unsigned int)n
)
254 return ERR_PTR(-EOVERFLOW
);
260 static inline bool end_of_str(char c
)
262 return c
== '\0' || c
== '\n';
265 static inline bool __end_of_region(char c
)
267 return isspace(c
) || c
== ',';
270 static inline bool end_of_region(char c
)
272 return __end_of_region(c
) || end_of_str(c
);
276 * The format allows commas and whitespaces at the beginning
279 static const char *bitmap_find_region(const char *str
)
281 while (__end_of_region(*str
))
284 return end_of_str(*str
) ? NULL
: str
;
287 static const char *bitmap_find_region_reverse(const char *start
, const char *end
)
289 while (start
<= end
&& __end_of_region(*end
))
295 static const char *bitmap_parse_region(const char *str
, struct region
*r
)
297 unsigned int lastbit
= r
->nbits
- 1;
299 if (!strncasecmp(str
, "all", 3)) {
307 str
= bitmap_getnum(str
, &r
->start
, lastbit
);
311 if (end_of_region(*str
))
315 return ERR_PTR(-EINVAL
);
317 str
= bitmap_getnum(str
+ 1, &r
->end
, lastbit
);
322 if (end_of_region(*str
))
326 return ERR_PTR(-EINVAL
);
328 str
= bitmap_getnum(str
+ 1, &r
->off
, lastbit
);
333 return ERR_PTR(-EINVAL
);
335 return bitmap_getnum(str
+ 1, &r
->group_len
, lastbit
);
341 r
->group_len
= r
->end
+ 1;
343 return end_of_str(*str
) ? NULL
: str
;
347 * bitmap_parselist - convert list format ASCII string to bitmap
348 * @buf: read user string from this buffer; must be terminated
350 * @maskp: write resulting mask here
351 * @nmaskbits: number of bits in mask to be written
353 * Input format is a comma-separated list of decimal numbers and
354 * ranges. Consecutively set bits are shown as two hyphen-separated
355 * decimal numbers, the smallest and largest bit numbers set in
357 * Optionally each range can be postfixed to denote that only parts of it
358 * should be set. The range will divided to groups of specific size.
359 * From each group will be used only defined amount of bits.
360 * Syntax: range:used_size/group_size
361 * Example: 0-1023:2/256 ==> 0,1,256,257,512,513,768,769
362 * The value 'N' can be used as a dynamically substituted token for the
363 * maximum allowed value; i.e (nmaskbits - 1). Keep in mind that it is
364 * dynamic, so if system changes cause the bitmap width to change, such
365 * as more cores in a CPU list, then any ranges using N will also change.
367 * Returns: 0 on success, -errno on invalid input strings. Error values:
369 * - ``-EINVAL``: wrong region format
370 * - ``-EINVAL``: invalid character in string
371 * - ``-ERANGE``: bit number specified too large for mask
372 * - ``-EOVERFLOW``: integer overflow in the input parameters
374 int bitmap_parselist(const char *buf
, unsigned long *maskp
, int nmaskbits
)
380 bitmap_zero(maskp
, r
.nbits
);
383 buf
= bitmap_find_region(buf
);
387 buf
= bitmap_parse_region(buf
, &r
);
391 ret
= bitmap_check_region(&r
);
395 bitmap_set_region(&r
, maskp
);
400 EXPORT_SYMBOL(bitmap_parselist
);
404 * bitmap_parselist_user() - convert user buffer's list format ASCII
407 * @ubuf: pointer to user buffer containing string.
408 * @ulen: buffer size in bytes. If string is smaller than this
409 * then it must be terminated with a \0.
410 * @maskp: pointer to bitmap array that will contain result.
411 * @nmaskbits: size of bitmap, in bits.
413 * Wrapper for bitmap_parselist(), providing it with user buffer.
415 int bitmap_parselist_user(const char __user
*ubuf
,
416 unsigned int ulen
, unsigned long *maskp
,
422 buf
= memdup_user_nul(ubuf
, ulen
);
426 ret
= bitmap_parselist(buf
, maskp
, nmaskbits
);
431 EXPORT_SYMBOL(bitmap_parselist_user
);
433 static const char *bitmap_get_x32_reverse(const char *start
,
434 const char *end
, u32
*num
)
439 for (i
= 0; i
< 32; i
+= 4) {
440 c
= hex_to_bin(*end
--);
442 return ERR_PTR(-EINVAL
);
446 if (start
> end
|| __end_of_region(*end
))
450 if (hex_to_bin(*end
--) >= 0)
451 return ERR_PTR(-EOVERFLOW
);
458 * bitmap_parse - convert an ASCII hex string into a bitmap.
459 * @start: pointer to buffer containing string.
460 * @buflen: buffer size in bytes. If string is smaller than this
461 * then it must be terminated with a \0 or \n. In that case,
462 * UINT_MAX may be provided instead of string length.
463 * @maskp: pointer to bitmap array that will contain result.
464 * @nmaskbits: size of bitmap, in bits.
466 * Commas group hex digits into chunks. Each chunk defines exactly 32
467 * bits of the resultant bitmask. No chunk may specify a value larger
468 * than 32 bits (%-EOVERFLOW), and if a chunk specifies a smaller value
469 * then leading 0-bits are prepended. %-EINVAL is returned for illegal
470 * characters. Grouping such as "1,,5", ",44", "," or "" is allowed.
471 * Leading, embedded and trailing whitespace accepted.
473 int bitmap_parse(const char *start
, unsigned int buflen
,
474 unsigned long *maskp
, int nmaskbits
)
476 const char *end
= strnchrnul(start
, buflen
, '\n') - 1;
477 int chunks
= BITS_TO_U32(nmaskbits
);
478 u32
*bitmap
= (u32
*)maskp
;
482 for (chunk
= 0; ; chunk
++) {
483 end
= bitmap_find_region_reverse(start
, end
);
490 #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
491 end
= bitmap_get_x32_reverse(start
, end
, &bitmap
[chunk
^ 1]);
493 end
= bitmap_get_x32_reverse(start
, end
, &bitmap
[chunk
]);
499 unset_bit
= (BITS_TO_U32(nmaskbits
) - chunks
) * 32;
500 if (unset_bit
< nmaskbits
) {
501 bitmap_clear(maskp
, unset_bit
, nmaskbits
- unset_bit
);
505 if (find_next_bit(maskp
, unset_bit
, nmaskbits
) != unset_bit
)
510 EXPORT_SYMBOL(bitmap_parse
);