bitmap-str.c 15 KB

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