test_bitmap.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test cases for bitmap API.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/bitmap.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/printk.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include <linux/uaccess.h>
  14. #include "../tools/testing/selftests/kselftest_module.h"
  15. #define EXP1_IN_BITS (sizeof(exp1) * 8)
  16. KSTM_MODULE_GLOBALS();
  17. static char pbl_buffer[PAGE_SIZE] __initdata;
  18. static char print_buf[PAGE_SIZE * 2] __initdata;
  19. static const unsigned long exp1[] __initconst = {
  20. BITMAP_FROM_U64(1),
  21. BITMAP_FROM_U64(2),
  22. BITMAP_FROM_U64(0x0000ffff),
  23. BITMAP_FROM_U64(0xffff0000),
  24. BITMAP_FROM_U64(0x55555555),
  25. BITMAP_FROM_U64(0xaaaaaaaa),
  26. BITMAP_FROM_U64(0x11111111),
  27. BITMAP_FROM_U64(0x22222222),
  28. BITMAP_FROM_U64(0xffffffff),
  29. BITMAP_FROM_U64(0xfffffffe),
  30. BITMAP_FROM_U64(0x3333333311111111ULL),
  31. BITMAP_FROM_U64(0xffffffff77777777ULL),
  32. BITMAP_FROM_U64(0),
  33. BITMAP_FROM_U64(0x00008000),
  34. BITMAP_FROM_U64(0x80000000),
  35. };
  36. static const unsigned long exp2[] __initconst = {
  37. BITMAP_FROM_U64(0x3333333311111111ULL),
  38. BITMAP_FROM_U64(0xffffffff77777777ULL),
  39. };
  40. /* Fibonacci sequence */
  41. static const unsigned long exp2_to_exp3_mask[] __initconst = {
  42. BITMAP_FROM_U64(0x008000020020212eULL),
  43. };
  44. /* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */
  45. static const unsigned long exp3_0_1[] __initconst = {
  46. BITMAP_FROM_U64(0x33b3333311313137ULL),
  47. };
  48. /* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */
  49. static const unsigned long exp3_1_0[] __initconst = {
  50. BITMAP_FROM_U64(0xff7fffff77575751ULL),
  51. };
  52. static bool __init
  53. __check_eq_uint(const char *srcfile, unsigned int line,
  54. const unsigned int exp_uint, unsigned int x)
  55. {
  56. if (exp_uint != x) {
  57. pr_err("[%s:%u] expected %u, got %u\n",
  58. srcfile, line, exp_uint, x);
  59. return false;
  60. }
  61. return true;
  62. }
  63. static bool __init
  64. __check_eq_bitmap(const char *srcfile, unsigned int line,
  65. const unsigned long *exp_bmap, const unsigned long *bmap,
  66. unsigned int nbits)
  67. {
  68. if (!bitmap_equal(exp_bmap, bmap, nbits)) {
  69. pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
  70. srcfile, line,
  71. nbits, exp_bmap, nbits, bmap);
  72. return false;
  73. }
  74. return true;
  75. }
  76. static bool __init
  77. __check_eq_pbl(const char *srcfile, unsigned int line,
  78. const char *expected_pbl,
  79. const unsigned long *bitmap, unsigned int nbits)
  80. {
  81. snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
  82. if (strcmp(expected_pbl, pbl_buffer)) {
  83. pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
  84. srcfile, line,
  85. expected_pbl, pbl_buffer);
  86. return false;
  87. }
  88. return true;
  89. }
  90. static bool __init
  91. __check_eq_u32_array(const char *srcfile, unsigned int line,
  92. const u32 *exp_arr, unsigned int exp_len,
  93. const u32 *arr, unsigned int len) __used;
  94. static bool __init
  95. __check_eq_u32_array(const char *srcfile, unsigned int line,
  96. const u32 *exp_arr, unsigned int exp_len,
  97. const u32 *arr, unsigned int len)
  98. {
  99. if (exp_len != len) {
  100. pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
  101. srcfile, line,
  102. exp_len, len);
  103. return false;
  104. }
  105. if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
  106. pr_warn("[%s:%u] array contents differ\n", srcfile, line);
  107. print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET,
  108. 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
  109. print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET,
  110. 32, 4, arr, len*sizeof(*arr), false);
  111. return false;
  112. }
  113. return true;
  114. }
  115. static bool __init __check_eq_clump8(const char *srcfile, unsigned int line,
  116. const unsigned int offset,
  117. const unsigned int size,
  118. const unsigned char *const clump_exp,
  119. const unsigned long *const clump)
  120. {
  121. unsigned long exp;
  122. if (offset >= size) {
  123. pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n",
  124. srcfile, line, size, offset);
  125. return false;
  126. }
  127. exp = clump_exp[offset / 8];
  128. if (!exp) {
  129. pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0",
  130. srcfile, line, offset);
  131. return false;
  132. }
  133. if (*clump != exp) {
  134. pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX",
  135. srcfile, line, exp, *clump);
  136. return false;
  137. }
  138. return true;
  139. }
  140. static bool __init
  141. __check_eq_str(const char *srcfile, unsigned int line,
  142. const char *exp_str, const char *str,
  143. unsigned int len)
  144. {
  145. bool eq;
  146. eq = strncmp(exp_str, str, len) == 0;
  147. if (!eq)
  148. pr_err("[%s:%u] expected %s, got %s\n", srcfile, line, exp_str, str);
  149. return eq;
  150. }
  151. #define __expect_eq(suffix, ...) \
  152. ({ \
  153. int result = 0; \
  154. total_tests++; \
  155. if (!__check_eq_ ## suffix(__FILE__, __LINE__, \
  156. ##__VA_ARGS__)) { \
  157. failed_tests++; \
  158. result = 1; \
  159. } \
  160. result; \
  161. })
  162. #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__)
  163. #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__)
  164. #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__)
  165. #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__)
  166. #define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__)
  167. #define expect_eq_str(...) __expect_eq(str, ##__VA_ARGS__)
  168. static void __init test_zero_clear(void)
  169. {
  170. DECLARE_BITMAP(bmap, 1024);
  171. /* Known way to set all bits */
  172. memset(bmap, 0xff, 128);
  173. expect_eq_pbl("0-22", bmap, 23);
  174. expect_eq_pbl("0-1023", bmap, 1024);
  175. /* single-word bitmaps */
  176. bitmap_clear(bmap, 0, 9);
  177. expect_eq_pbl("9-1023", bmap, 1024);
  178. bitmap_zero(bmap, 35);
  179. expect_eq_pbl("64-1023", bmap, 1024);
  180. /* cross boundaries operations */
  181. bitmap_clear(bmap, 79, 19);
  182. expect_eq_pbl("64-78,98-1023", bmap, 1024);
  183. bitmap_zero(bmap, 115);
  184. expect_eq_pbl("128-1023", bmap, 1024);
  185. /* Zeroing entire area */
  186. bitmap_zero(bmap, 1024);
  187. expect_eq_pbl("", bmap, 1024);
  188. }
  189. static void __init test_find_nth_bit(void)
  190. {
  191. unsigned long b, bit, cnt = 0;
  192. DECLARE_BITMAP(bmap, 64 * 3);
  193. bitmap_zero(bmap, 64 * 3);
  194. __set_bit(10, bmap);
  195. __set_bit(20, bmap);
  196. __set_bit(30, bmap);
  197. __set_bit(40, bmap);
  198. __set_bit(50, bmap);
  199. __set_bit(60, bmap);
  200. __set_bit(80, bmap);
  201. __set_bit(123, bmap);
  202. expect_eq_uint(10, find_nth_bit(bmap, 64 * 3, 0));
  203. expect_eq_uint(20, find_nth_bit(bmap, 64 * 3, 1));
  204. expect_eq_uint(30, find_nth_bit(bmap, 64 * 3, 2));
  205. expect_eq_uint(40, find_nth_bit(bmap, 64 * 3, 3));
  206. expect_eq_uint(50, find_nth_bit(bmap, 64 * 3, 4));
  207. expect_eq_uint(60, find_nth_bit(bmap, 64 * 3, 5));
  208. expect_eq_uint(80, find_nth_bit(bmap, 64 * 3, 6));
  209. expect_eq_uint(123, find_nth_bit(bmap, 64 * 3, 7));
  210. expect_eq_uint(64 * 3, find_nth_bit(bmap, 64 * 3, 8));
  211. expect_eq_uint(10, find_nth_bit(bmap, 64 * 3 - 1, 0));
  212. expect_eq_uint(20, find_nth_bit(bmap, 64 * 3 - 1, 1));
  213. expect_eq_uint(30, find_nth_bit(bmap, 64 * 3 - 1, 2));
  214. expect_eq_uint(40, find_nth_bit(bmap, 64 * 3 - 1, 3));
  215. expect_eq_uint(50, find_nth_bit(bmap, 64 * 3 - 1, 4));
  216. expect_eq_uint(60, find_nth_bit(bmap, 64 * 3 - 1, 5));
  217. expect_eq_uint(80, find_nth_bit(bmap, 64 * 3 - 1, 6));
  218. expect_eq_uint(123, find_nth_bit(bmap, 64 * 3 - 1, 7));
  219. expect_eq_uint(64 * 3 - 1, find_nth_bit(bmap, 64 * 3 - 1, 8));
  220. for_each_set_bit(bit, exp1, EXP1_IN_BITS) {
  221. b = find_nth_bit(exp1, EXP1_IN_BITS, cnt++);
  222. expect_eq_uint(b, bit);
  223. }
  224. }
  225. static void __init test_fill_set(void)
  226. {
  227. DECLARE_BITMAP(bmap, 1024);
  228. /* Known way to clear all bits */
  229. memset(bmap, 0x00, 128);
  230. expect_eq_pbl("", bmap, 23);
  231. expect_eq_pbl("", bmap, 1024);
  232. /* single-word bitmaps */
  233. bitmap_set(bmap, 0, 9);
  234. expect_eq_pbl("0-8", bmap, 1024);
  235. bitmap_fill(bmap, 35);
  236. expect_eq_pbl("0-63", bmap, 1024);
  237. /* cross boundaries operations */
  238. bitmap_set(bmap, 79, 19);
  239. expect_eq_pbl("0-63,79-97", bmap, 1024);
  240. bitmap_fill(bmap, 115);
  241. expect_eq_pbl("0-127", bmap, 1024);
  242. /* Zeroing entire area */
  243. bitmap_fill(bmap, 1024);
  244. expect_eq_pbl("0-1023", bmap, 1024);
  245. }
  246. static void __init test_copy(void)
  247. {
  248. DECLARE_BITMAP(bmap1, 1024);
  249. DECLARE_BITMAP(bmap2, 1024);
  250. bitmap_zero(bmap1, 1024);
  251. bitmap_zero(bmap2, 1024);
  252. /* single-word bitmaps */
  253. bitmap_set(bmap1, 0, 19);
  254. bitmap_copy(bmap2, bmap1, 23);
  255. expect_eq_pbl("0-18", bmap2, 1024);
  256. bitmap_set(bmap2, 0, 23);
  257. bitmap_copy(bmap2, bmap1, 23);
  258. expect_eq_pbl("0-18", bmap2, 1024);
  259. /* multi-word bitmaps */
  260. bitmap_set(bmap1, 0, 109);
  261. bitmap_copy(bmap2, bmap1, 1024);
  262. expect_eq_pbl("0-108", bmap2, 1024);
  263. bitmap_fill(bmap2, 1024);
  264. bitmap_copy(bmap2, bmap1, 1024);
  265. expect_eq_pbl("0-108", bmap2, 1024);
  266. /* the following tests assume a 32- or 64-bit arch (even 128b
  267. * if we care)
  268. */
  269. bitmap_fill(bmap2, 1024);
  270. bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */
  271. expect_eq_pbl("0-108,128-1023", bmap2, 1024);
  272. bitmap_fill(bmap2, 1024);
  273. bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */
  274. expect_eq_pbl("0-108,128-1023", bmap2, 1024);
  275. }
  276. #define EXP2_IN_BITS (sizeof(exp2) * 8)
  277. static void __init test_replace(void)
  278. {
  279. unsigned int nbits = 64;
  280. unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);
  281. DECLARE_BITMAP(bmap, 1024);
  282. BUILD_BUG_ON(EXP2_IN_BITS < nbits * 2);
  283. bitmap_zero(bmap, 1024);
  284. bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
  285. expect_eq_bitmap(bmap, exp3_0_1, nbits);
  286. bitmap_zero(bmap, 1024);
  287. bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
  288. expect_eq_bitmap(bmap, exp3_1_0, nbits);
  289. bitmap_fill(bmap, 1024);
  290. bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);
  291. expect_eq_bitmap(bmap, exp3_0_1, nbits);
  292. bitmap_fill(bmap, 1024);
  293. bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);
  294. expect_eq_bitmap(bmap, exp3_1_0, nbits);
  295. }
  296. #define PARSE_TIME 0x1
  297. #define NO_LEN 0x2
  298. struct test_bitmap_parselist{
  299. const int errno;
  300. const char *in;
  301. const unsigned long *expected;
  302. const int nbits;
  303. const int flags;
  304. };
  305. static const struct test_bitmap_parselist parselist_tests[] __initconst = {
  306. #define step (sizeof(u64) / sizeof(unsigned long))
  307. {0, "0", &exp1[0], 8, 0},
  308. {0, "1", &exp1[1 * step], 8, 0},
  309. {0, "0-15", &exp1[2 * step], 32, 0},
  310. {0, "16-31", &exp1[3 * step], 32, 0},
  311. {0, "0-31:1/2", &exp1[4 * step], 32, 0},
  312. {0, "1-31:1/2", &exp1[5 * step], 32, 0},
  313. {0, "0-31:1/4", &exp1[6 * step], 32, 0},
  314. {0, "1-31:1/4", &exp1[7 * step], 32, 0},
  315. {0, "0-31:4/4", &exp1[8 * step], 32, 0},
  316. {0, "1-31:4/4", &exp1[9 * step], 32, 0},
  317. {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0},
  318. {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0},
  319. {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp1[11 * step], 64, 0},
  320. {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0},
  321. {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
  322. {0, "", &exp1[12 * step], 8, 0},
  323. {0, "\n", &exp1[12 * step], 8, 0},
  324. {0, ",, ,, , , ,", &exp1[12 * step], 8, 0},
  325. {0, " , ,, , , ", &exp1[12 * step], 8, 0},
  326. {0, " , ,, , , \n", &exp1[12 * step], 8, 0},
  327. {0, "0-0", &exp1[0], 32, 0},
  328. {0, "1-1", &exp1[1 * step], 32, 0},
  329. {0, "15-15", &exp1[13 * step], 32, 0},
  330. {0, "31-31", &exp1[14 * step], 32, 0},
  331. {0, "0-0:0/1", &exp1[12 * step], 32, 0},
  332. {0, "0-0:1/1", &exp1[0], 32, 0},
  333. {0, "0-0:1/31", &exp1[0], 32, 0},
  334. {0, "0-0:31/31", &exp1[0], 32, 0},
  335. {0, "1-1:1/1", &exp1[1 * step], 32, 0},
  336. {0, "0-15:16/31", &exp1[2 * step], 32, 0},
  337. {0, "15-15:1/2", &exp1[13 * step], 32, 0},
  338. {0, "15-15:31/31", &exp1[13 * step], 32, 0},
  339. {0, "15-31:1/31", &exp1[13 * step], 32, 0},
  340. {0, "16-31:16/31", &exp1[3 * step], 32, 0},
  341. {0, "31-31:31/31", &exp1[14 * step], 32, 0},
  342. {0, "N-N", &exp1[14 * step], 32, 0},
  343. {0, "0-0:1/N", &exp1[0], 32, 0},
  344. {0, "0-0:N/N", &exp1[0], 32, 0},
  345. {0, "0-15:16/N", &exp1[2 * step], 32, 0},
  346. {0, "15-15:N/N", &exp1[13 * step], 32, 0},
  347. {0, "15-N:1/N", &exp1[13 * step], 32, 0},
  348. {0, "16-N:16/N", &exp1[3 * step], 32, 0},
  349. {0, "N-N:N/N", &exp1[14 * step], 32, 0},
  350. {0, "0-N:1/3,1-N:1/3,2-N:1/3", &exp1[8 * step], 32, 0},
  351. {0, "0-31:1/3,1-31:1/3,2-31:1/3", &exp1[8 * step], 32, 0},
  352. {0, "1-10:8/12,8-31:24/29,0-31:0/3", &exp1[9 * step], 32, 0},
  353. {0, "all", &exp1[8 * step], 32, 0},
  354. {0, "0, 1, all, ", &exp1[8 * step], 32, 0},
  355. {0, "all:1/2", &exp1[4 * step], 32, 0},
  356. {0, "ALL:1/2", &exp1[4 * step], 32, 0},
  357. {-EINVAL, "al", NULL, 8, 0},
  358. {-EINVAL, "alll", NULL, 8, 0},
  359. {-EINVAL, "-1", NULL, 8, 0},
  360. {-EINVAL, "-0", NULL, 8, 0},
  361. {-EINVAL, "10-1", NULL, 8, 0},
  362. {-ERANGE, "8-8", NULL, 8, 0},
  363. {-ERANGE, "0-31", NULL, 8, 0},
  364. {-EINVAL, "0-31:", NULL, 32, 0},
  365. {-EINVAL, "0-31:0", NULL, 32, 0},
  366. {-EINVAL, "0-31:0/", NULL, 32, 0},
  367. {-EINVAL, "0-31:0/0", NULL, 32, 0},
  368. {-EINVAL, "0-31:1/0", NULL, 32, 0},
  369. {-EINVAL, "0-31:10/1", NULL, 32, 0},
  370. {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0},
  371. {-EINVAL, "a-31", NULL, 8, 0},
  372. {-EINVAL, "0-a1", NULL, 8, 0},
  373. {-EINVAL, "a-31:10/1", NULL, 8, 0},
  374. {-EINVAL, "0-31:a/1", NULL, 8, 0},
  375. {-EINVAL, "0-\n", NULL, 8, 0},
  376. };
  377. static void __init test_bitmap_parselist(void)
  378. {
  379. int i;
  380. int err;
  381. ktime_t time;
  382. DECLARE_BITMAP(bmap, 2048);
  383. for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
  384. #define ptest parselist_tests[i]
  385. time = ktime_get();
  386. err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
  387. time = ktime_get() - time;
  388. if (err != ptest.errno) {
  389. pr_err("parselist: %d: input is %s, errno is %d, expected %d\n",
  390. i, ptest.in, err, ptest.errno);
  391. continue;
  392. }
  393. if (!err && ptest.expected
  394. && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
  395. pr_err("parselist: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
  396. i, ptest.in, bmap[0],
  397. *ptest.expected);
  398. continue;
  399. }
  400. if (ptest.flags & PARSE_TIME)
  401. pr_err("parselist: %d: input is '%s' OK, Time: %llu\n",
  402. i, ptest.in, time);
  403. #undef ptest
  404. }
  405. }
  406. static void __init test_bitmap_printlist(void)
  407. {
  408. unsigned long *bmap = kmalloc(PAGE_SIZE, GFP_KERNEL);
  409. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  410. char expected[256];
  411. int ret, slen;
  412. ktime_t time;
  413. if (!buf || !bmap)
  414. goto out;
  415. memset(bmap, -1, PAGE_SIZE);
  416. slen = snprintf(expected, 256, "0-%ld", PAGE_SIZE * 8 - 1);
  417. if (slen < 0)
  418. goto out;
  419. time = ktime_get();
  420. ret = bitmap_print_to_pagebuf(true, buf, bmap, PAGE_SIZE * 8);
  421. time = ktime_get() - time;
  422. if (ret != slen + 1) {
  423. pr_err("bitmap_print_to_pagebuf: result is %d, expected %d\n", ret, slen);
  424. goto out;
  425. }
  426. if (strncmp(buf, expected, slen)) {
  427. pr_err("bitmap_print_to_pagebuf: result is %s, expected %s\n", buf, expected);
  428. goto out;
  429. }
  430. pr_err("bitmap_print_to_pagebuf: input is '%s', Time: %llu\n", buf, time);
  431. out:
  432. kfree(buf);
  433. kfree(bmap);
  434. }
  435. static const unsigned long parse_test[] __initconst = {
  436. BITMAP_FROM_U64(0),
  437. BITMAP_FROM_U64(1),
  438. BITMAP_FROM_U64(0xdeadbeef),
  439. BITMAP_FROM_U64(0x100000000ULL),
  440. };
  441. static const unsigned long parse_test2[] __initconst = {
  442. BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xdeadbeef),
  443. BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xbaadf00ddeadbeef),
  444. BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0x0badf00ddeadbeef),
  445. };
  446. static const struct test_bitmap_parselist parse_tests[] __initconst = {
  447. {0, "", &parse_test[0 * step], 32, 0},
  448. {0, " ", &parse_test[0 * step], 32, 0},
  449. {0, "0", &parse_test[0 * step], 32, 0},
  450. {0, "0\n", &parse_test[0 * step], 32, 0},
  451. {0, "1", &parse_test[1 * step], 32, 0},
  452. {0, "deadbeef", &parse_test[2 * step], 32, 0},
  453. {0, "1,0", &parse_test[3 * step], 33, 0},
  454. {0, "deadbeef,\n,0,1", &parse_test[2 * step], 96, 0},
  455. {0, "deadbeef,1,0", &parse_test2[0 * 2 * step], 96, 0},
  456. {0, "baadf00d,deadbeef,1,0", &parse_test2[1 * 2 * step], 128, 0},
  457. {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, 0},
  458. {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, NO_LEN},
  459. {0, " badf00d,deadbeef,1,0 ", &parse_test2[2 * 2 * step], 124, 0},
  460. {0, " , badf00d,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0},
  461. {0, " , badf00d, ,, ,,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0},
  462. {-EINVAL, "goodfood,deadbeef,1,0", NULL, 128, 0},
  463. {-EOVERFLOW, "3,0", NULL, 33, 0},
  464. {-EOVERFLOW, "123badf00d,deadbeef,1,0", NULL, 128, 0},
  465. {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 90, 0},
  466. {-EOVERFLOW, "fbadf00d,deadbeef,1,0", NULL, 95, 0},
  467. {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 100, 0},
  468. #undef step
  469. };
  470. static void __init test_bitmap_parse(void)
  471. {
  472. int i;
  473. int err;
  474. ktime_t time;
  475. DECLARE_BITMAP(bmap, 2048);
  476. for (i = 0; i < ARRAY_SIZE(parse_tests); i++) {
  477. struct test_bitmap_parselist test = parse_tests[i];
  478. size_t len = test.flags & NO_LEN ? UINT_MAX : strlen(test.in);
  479. time = ktime_get();
  480. err = bitmap_parse(test.in, len, bmap, test.nbits);
  481. time = ktime_get() - time;
  482. if (err != test.errno) {
  483. pr_err("parse: %d: input is %s, errno is %d, expected %d\n",
  484. i, test.in, err, test.errno);
  485. continue;
  486. }
  487. if (!err && test.expected
  488. && !__bitmap_equal(bmap, test.expected, test.nbits)) {
  489. pr_err("parse: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
  490. i, test.in, bmap[0],
  491. *test.expected);
  492. continue;
  493. }
  494. if (test.flags & PARSE_TIME)
  495. pr_err("parse: %d: input is '%s' OK, Time: %llu\n",
  496. i, test.in, time);
  497. }
  498. }
  499. static void __init test_bitmap_arr32(void)
  500. {
  501. unsigned int nbits, next_bit;
  502. u32 arr[EXP1_IN_BITS / 32];
  503. DECLARE_BITMAP(bmap2, EXP1_IN_BITS);
  504. memset(arr, 0xa5, sizeof(arr));
  505. for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
  506. bitmap_to_arr32(arr, exp1, nbits);
  507. bitmap_from_arr32(bmap2, arr, nbits);
  508. expect_eq_bitmap(bmap2, exp1, nbits);
  509. next_bit = find_next_bit(bmap2,
  510. round_up(nbits, BITS_PER_LONG), nbits);
  511. if (next_bit < round_up(nbits, BITS_PER_LONG))
  512. pr_err("bitmap_copy_arr32(nbits == %d:"
  513. " tail is not safely cleared: %d\n",
  514. nbits, next_bit);
  515. if (nbits < EXP1_IN_BITS - 32)
  516. expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
  517. 0xa5a5a5a5);
  518. }
  519. }
  520. static void __init test_bitmap_arr64(void)
  521. {
  522. unsigned int nbits, next_bit;
  523. u64 arr[EXP1_IN_BITS / 64];
  524. DECLARE_BITMAP(bmap2, EXP1_IN_BITS);
  525. memset(arr, 0xa5, sizeof(arr));
  526. for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) {
  527. memset(bmap2, 0xff, sizeof(arr));
  528. bitmap_to_arr64(arr, exp1, nbits);
  529. bitmap_from_arr64(bmap2, arr, nbits);
  530. expect_eq_bitmap(bmap2, exp1, nbits);
  531. next_bit = find_next_bit(bmap2, round_up(nbits, BITS_PER_LONG), nbits);
  532. if (next_bit < round_up(nbits, BITS_PER_LONG))
  533. pr_err("bitmap_copy_arr64(nbits == %d:"
  534. " tail is not safely cleared: %d\n", nbits, next_bit);
  535. if ((nbits % 64) &&
  536. (arr[(nbits - 1) / 64] & ~GENMASK_ULL((nbits - 1) % 64, 0)))
  537. pr_err("bitmap_to_arr64(nbits == %d): tail is not safely cleared: 0x%016llx (must be 0x%016llx)\n",
  538. nbits, arr[(nbits - 1) / 64],
  539. GENMASK_ULL((nbits - 1) % 64, 0));
  540. if (nbits < EXP1_IN_BITS - 64)
  541. expect_eq_uint(arr[DIV_ROUND_UP(nbits, 64)], 0xa5a5a5a5);
  542. }
  543. }
  544. static void noinline __init test_mem_optimisations(void)
  545. {
  546. DECLARE_BITMAP(bmap1, 1024);
  547. DECLARE_BITMAP(bmap2, 1024);
  548. unsigned int start, nbits;
  549. for (start = 0; start < 1024; start += 8) {
  550. for (nbits = 0; nbits < 1024 - start; nbits += 8) {
  551. memset(bmap1, 0x5a, sizeof(bmap1));
  552. memset(bmap2, 0x5a, sizeof(bmap2));
  553. bitmap_set(bmap1, start, nbits);
  554. __bitmap_set(bmap2, start, nbits);
  555. if (!bitmap_equal(bmap1, bmap2, 1024)) {
  556. printk("set not equal %d %d\n", start, nbits);
  557. failed_tests++;
  558. }
  559. if (!__bitmap_equal(bmap1, bmap2, 1024)) {
  560. printk("set not __equal %d %d\n", start, nbits);
  561. failed_tests++;
  562. }
  563. bitmap_clear(bmap1, start, nbits);
  564. __bitmap_clear(bmap2, start, nbits);
  565. if (!bitmap_equal(bmap1, bmap2, 1024)) {
  566. printk("clear not equal %d %d\n", start, nbits);
  567. failed_tests++;
  568. }
  569. if (!__bitmap_equal(bmap1, bmap2, 1024)) {
  570. printk("clear not __equal %d %d\n", start,
  571. nbits);
  572. failed_tests++;
  573. }
  574. }
  575. }
  576. }
  577. static const unsigned char clump_exp[] __initconst = {
  578. 0x01, /* 1 bit set */
  579. 0x02, /* non-edge 1 bit set */
  580. 0x00, /* zero bits set */
  581. 0x38, /* 3 bits set across 4-bit boundary */
  582. 0x38, /* Repeated clump */
  583. 0x0F, /* 4 bits set */
  584. 0xFF, /* all bits set */
  585. 0x05, /* non-adjacent 2 bits set */
  586. };
  587. static void __init test_for_each_set_clump8(void)
  588. {
  589. #define CLUMP_EXP_NUMBITS 64
  590. DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS);
  591. unsigned int start;
  592. unsigned long clump;
  593. /* set bitmap to test case */
  594. bitmap_zero(bits, CLUMP_EXP_NUMBITS);
  595. bitmap_set(bits, 0, 1); /* 0x01 */
  596. bitmap_set(bits, 9, 1); /* 0x02 */
  597. bitmap_set(bits, 27, 3); /* 0x28 */
  598. bitmap_set(bits, 35, 3); /* 0x28 */
  599. bitmap_set(bits, 40, 4); /* 0x0F */
  600. bitmap_set(bits, 48, 8); /* 0xFF */
  601. bitmap_set(bits, 56, 1); /* 0x05 - part 1 */
  602. bitmap_set(bits, 58, 1); /* 0x05 - part 2 */
  603. for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS)
  604. expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump);
  605. }
  606. static void __init test_for_each_set_bit_wrap(void)
  607. {
  608. DECLARE_BITMAP(orig, 500);
  609. DECLARE_BITMAP(copy, 500);
  610. unsigned int wr, bit;
  611. bitmap_zero(orig, 500);
  612. /* Set individual bits */
  613. for (bit = 0; bit < 500; bit += 10)
  614. bitmap_set(orig, bit, 1);
  615. /* Set range of bits */
  616. bitmap_set(orig, 100, 50);
  617. for (wr = 0; wr < 500; wr++) {
  618. bitmap_zero(copy, 500);
  619. for_each_set_bit_wrap(bit, orig, 500, wr)
  620. bitmap_set(copy, bit, 1);
  621. expect_eq_bitmap(orig, copy, 500);
  622. }
  623. }
  624. static void __init test_for_each_set_bit(void)
  625. {
  626. DECLARE_BITMAP(orig, 500);
  627. DECLARE_BITMAP(copy, 500);
  628. unsigned int bit;
  629. bitmap_zero(orig, 500);
  630. bitmap_zero(copy, 500);
  631. /* Set individual bits */
  632. for (bit = 0; bit < 500; bit += 10)
  633. bitmap_set(orig, bit, 1);
  634. /* Set range of bits */
  635. bitmap_set(orig, 100, 50);
  636. for_each_set_bit(bit, orig, 500)
  637. bitmap_set(copy, bit, 1);
  638. expect_eq_bitmap(orig, copy, 500);
  639. }
  640. static void __init test_for_each_set_bit_from(void)
  641. {
  642. DECLARE_BITMAP(orig, 500);
  643. DECLARE_BITMAP(copy, 500);
  644. unsigned int wr, bit;
  645. bitmap_zero(orig, 500);
  646. /* Set individual bits */
  647. for (bit = 0; bit < 500; bit += 10)
  648. bitmap_set(orig, bit, 1);
  649. /* Set range of bits */
  650. bitmap_set(orig, 100, 50);
  651. for (wr = 0; wr < 500; wr++) {
  652. DECLARE_BITMAP(tmp, 500);
  653. bitmap_zero(copy, 500);
  654. bit = wr;
  655. for_each_set_bit_from(bit, orig, 500)
  656. bitmap_set(copy, bit, 1);
  657. bitmap_copy(tmp, orig, 500);
  658. bitmap_clear(tmp, 0, wr);
  659. expect_eq_bitmap(tmp, copy, 500);
  660. }
  661. }
  662. static void __init test_for_each_clear_bit(void)
  663. {
  664. DECLARE_BITMAP(orig, 500);
  665. DECLARE_BITMAP(copy, 500);
  666. unsigned int bit;
  667. bitmap_fill(orig, 500);
  668. bitmap_fill(copy, 500);
  669. /* Set individual bits */
  670. for (bit = 0; bit < 500; bit += 10)
  671. bitmap_clear(orig, bit, 1);
  672. /* Set range of bits */
  673. bitmap_clear(orig, 100, 50);
  674. for_each_clear_bit(bit, orig, 500)
  675. bitmap_clear(copy, bit, 1);
  676. expect_eq_bitmap(orig, copy, 500);
  677. }
  678. static void __init test_for_each_clear_bit_from(void)
  679. {
  680. DECLARE_BITMAP(orig, 500);
  681. DECLARE_BITMAP(copy, 500);
  682. unsigned int wr, bit;
  683. bitmap_fill(orig, 500);
  684. /* Set individual bits */
  685. for (bit = 0; bit < 500; bit += 10)
  686. bitmap_clear(orig, bit, 1);
  687. /* Set range of bits */
  688. bitmap_clear(orig, 100, 50);
  689. for (wr = 0; wr < 500; wr++) {
  690. DECLARE_BITMAP(tmp, 500);
  691. bitmap_fill(copy, 500);
  692. bit = wr;
  693. for_each_clear_bit_from(bit, orig, 500)
  694. bitmap_clear(copy, bit, 1);
  695. bitmap_copy(tmp, orig, 500);
  696. bitmap_set(tmp, 0, wr);
  697. expect_eq_bitmap(tmp, copy, 500);
  698. }
  699. }
  700. static void __init test_for_each_set_bitrange(void)
  701. {
  702. DECLARE_BITMAP(orig, 500);
  703. DECLARE_BITMAP(copy, 500);
  704. unsigned int s, e;
  705. bitmap_zero(orig, 500);
  706. bitmap_zero(copy, 500);
  707. /* Set individual bits */
  708. for (s = 0; s < 500; s += 10)
  709. bitmap_set(orig, s, 1);
  710. /* Set range of bits */
  711. bitmap_set(orig, 100, 50);
  712. for_each_set_bitrange(s, e, orig, 500)
  713. bitmap_set(copy, s, e-s);
  714. expect_eq_bitmap(orig, copy, 500);
  715. }
  716. static void __init test_for_each_clear_bitrange(void)
  717. {
  718. DECLARE_BITMAP(orig, 500);
  719. DECLARE_BITMAP(copy, 500);
  720. unsigned int s, e;
  721. bitmap_fill(orig, 500);
  722. bitmap_fill(copy, 500);
  723. /* Set individual bits */
  724. for (s = 0; s < 500; s += 10)
  725. bitmap_clear(orig, s, 1);
  726. /* Set range of bits */
  727. bitmap_clear(orig, 100, 50);
  728. for_each_clear_bitrange(s, e, orig, 500)
  729. bitmap_clear(copy, s, e-s);
  730. expect_eq_bitmap(orig, copy, 500);
  731. }
  732. static void __init test_for_each_set_bitrange_from(void)
  733. {
  734. DECLARE_BITMAP(orig, 500);
  735. DECLARE_BITMAP(copy, 500);
  736. unsigned int wr, s, e;
  737. bitmap_zero(orig, 500);
  738. /* Set individual bits */
  739. for (s = 0; s < 500; s += 10)
  740. bitmap_set(orig, s, 1);
  741. /* Set range of bits */
  742. bitmap_set(orig, 100, 50);
  743. for (wr = 0; wr < 500; wr++) {
  744. DECLARE_BITMAP(tmp, 500);
  745. bitmap_zero(copy, 500);
  746. s = wr;
  747. for_each_set_bitrange_from(s, e, orig, 500)
  748. bitmap_set(copy, s, e - s);
  749. bitmap_copy(tmp, orig, 500);
  750. bitmap_clear(tmp, 0, wr);
  751. expect_eq_bitmap(tmp, copy, 500);
  752. }
  753. }
  754. static void __init test_for_each_clear_bitrange_from(void)
  755. {
  756. DECLARE_BITMAP(orig, 500);
  757. DECLARE_BITMAP(copy, 500);
  758. unsigned int wr, s, e;
  759. bitmap_fill(orig, 500);
  760. /* Set individual bits */
  761. for (s = 0; s < 500; s += 10)
  762. bitmap_clear(orig, s, 1);
  763. /* Set range of bits */
  764. bitmap_set(orig, 100, 50);
  765. for (wr = 0; wr < 500; wr++) {
  766. DECLARE_BITMAP(tmp, 500);
  767. bitmap_fill(copy, 500);
  768. s = wr;
  769. for_each_clear_bitrange_from(s, e, orig, 500)
  770. bitmap_clear(copy, s, e - s);
  771. bitmap_copy(tmp, orig, 500);
  772. bitmap_set(tmp, 0, wr);
  773. expect_eq_bitmap(tmp, copy, 500);
  774. }
  775. }
  776. struct test_bitmap_cut {
  777. unsigned int first;
  778. unsigned int cut;
  779. unsigned int nbits;
  780. unsigned long in[4];
  781. unsigned long expected[4];
  782. };
  783. static struct test_bitmap_cut test_cut[] = {
  784. { 0, 0, 8, { 0x0000000aUL, }, { 0x0000000aUL, }, },
  785. { 0, 0, 32, { 0xdadadeadUL, }, { 0xdadadeadUL, }, },
  786. { 0, 3, 8, { 0x000000aaUL, }, { 0x00000015UL, }, },
  787. { 3, 3, 8, { 0x000000aaUL, }, { 0x00000012UL, }, },
  788. { 0, 1, 32, { 0xa5a5a5a5UL, }, { 0x52d2d2d2UL, }, },
  789. { 0, 8, 32, { 0xdeadc0deUL, }, { 0x00deadc0UL, }, },
  790. { 1, 1, 32, { 0x5a5a5a5aUL, }, { 0x2d2d2d2cUL, }, },
  791. { 0, 15, 32, { 0xa5a5a5a5UL, }, { 0x00014b4bUL, }, },
  792. { 0, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, },
  793. { 15, 15, 32, { 0xa5a5a5a5UL, }, { 0x000125a5UL, }, },
  794. { 15, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, },
  795. { 16, 15, 32, { 0xa5a5a5a5UL, }, { 0x0001a5a5UL, }, },
  796. { BITS_PER_LONG, BITS_PER_LONG, BITS_PER_LONG,
  797. { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
  798. { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
  799. },
  800. { 1, BITS_PER_LONG - 1, BITS_PER_LONG,
  801. { 0xa5a5a5a5UL, 0xa5a5a5a5UL, },
  802. { 0x00000001UL, 0x00000001UL, },
  803. },
  804. { 0, BITS_PER_LONG * 2, BITS_PER_LONG * 2 + 1,
  805. { 0xa5a5a5a5UL, 0x00000001UL, 0x00000001UL, 0x00000001UL },
  806. { 0x00000001UL, },
  807. },
  808. { 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16,
  809. { 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL },
  810. { 0x2d2dffffUL, },
  811. },
  812. };
  813. static void __init test_bitmap_cut(void)
  814. {
  815. unsigned long b[5], *in = &b[1], *out = &b[0]; /* Partial overlap */
  816. int i;
  817. for (i = 0; i < ARRAY_SIZE(test_cut); i++) {
  818. struct test_bitmap_cut *t = &test_cut[i];
  819. memcpy(in, t->in, sizeof(t->in));
  820. bitmap_cut(out, in, t->first, t->cut, t->nbits);
  821. expect_eq_bitmap(t->expected, out, t->nbits);
  822. }
  823. }
  824. struct test_bitmap_print {
  825. const unsigned long *bitmap;
  826. unsigned long nbits;
  827. const char *mask;
  828. const char *list;
  829. };
  830. static const unsigned long small_bitmap[] __initconst = {
  831. BITMAP_FROM_U64(0x3333333311111111ULL),
  832. };
  833. static const char small_mask[] __initconst = "33333333,11111111\n";
  834. static const char small_list[] __initconst = "0,4,8,12,16,20,24,28,32-33,36-37,40-41,44-45,48-49,52-53,56-57,60-61\n";
  835. static const unsigned long large_bitmap[] __initconst = {
  836. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  837. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  838. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  839. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  840. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  841. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  842. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  843. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  844. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  845. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  846. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  847. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  848. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  849. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  850. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  851. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  852. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  853. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  854. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  855. BITMAP_FROM_U64(0x3333333311111111ULL), BITMAP_FROM_U64(0x3333333311111111ULL),
  856. };
  857. static const char large_mask[] __initconst = "33333333,11111111,33333333,11111111,"
  858. "33333333,11111111,33333333,11111111,"
  859. "33333333,11111111,33333333,11111111,"
  860. "33333333,11111111,33333333,11111111,"
  861. "33333333,11111111,33333333,11111111,"
  862. "33333333,11111111,33333333,11111111,"
  863. "33333333,11111111,33333333,11111111,"
  864. "33333333,11111111,33333333,11111111,"
  865. "33333333,11111111,33333333,11111111,"
  866. "33333333,11111111,33333333,11111111,"
  867. "33333333,11111111,33333333,11111111,"
  868. "33333333,11111111,33333333,11111111,"
  869. "33333333,11111111,33333333,11111111,"
  870. "33333333,11111111,33333333,11111111,"
  871. "33333333,11111111,33333333,11111111,"
  872. "33333333,11111111,33333333,11111111,"
  873. "33333333,11111111,33333333,11111111,"
  874. "33333333,11111111,33333333,11111111,"
  875. "33333333,11111111,33333333,11111111,"
  876. "33333333,11111111,33333333,11111111\n";
  877. static const char large_list[] __initconst = /* more than 4KB */
  878. "0,4,8,12,16,20,24,28,32-33,36-37,40-41,44-45,48-49,52-53,56-57,60-61,64,68,72,76,80,84,88,92,96-97,100-101,104-1"
  879. "05,108-109,112-113,116-117,120-121,124-125,128,132,136,140,144,148,152,156,160-161,164-165,168-169,172-173,176-1"
  880. "77,180-181,184-185,188-189,192,196,200,204,208,212,216,220,224-225,228-229,232-233,236-237,240-241,244-245,248-2"
  881. "49,252-253,256,260,264,268,272,276,280,284,288-289,292-293,296-297,300-301,304-305,308-309,312-313,316-317,320,3"
  882. "24,328,332,336,340,344,348,352-353,356-357,360-361,364-365,368-369,372-373,376-377,380-381,384,388,392,396,400,4"
  883. "04,408,412,416-417,420-421,424-425,428-429,432-433,436-437,440-441,444-445,448,452,456,460,464,468,472,476,480-4"
  884. "81,484-485,488-489,492-493,496-497,500-501,504-505,508-509,512,516,520,524,528,532,536,540,544-545,548-549,552-5"
  885. "53,556-557,560-561,564-565,568-569,572-573,576,580,584,588,592,596,600,604,608-609,612-613,616-617,620-621,624-6"
  886. "25,628-629,632-633,636-637,640,644,648,652,656,660,664,668,672-673,676-677,680-681,684-685,688-689,692-693,696-6"
  887. "97,700-701,704,708,712,716,720,724,728,732,736-737,740-741,744-745,748-749,752-753,756-757,760-761,764-765,768,7"
  888. "72,776,780,784,788,792,796,800-801,804-805,808-809,812-813,816-817,820-821,824-825,828-829,832,836,840,844,848,8"
  889. "52,856,860,864-865,868-869,872-873,876-877,880-881,884-885,888-889,892-893,896,900,904,908,912,916,920,924,928-9"
  890. "29,932-933,936-937,940-941,944-945,948-949,952-953,956-957,960,964,968,972,976,980,984,988,992-993,996-997,1000-"
  891. "1001,1004-1005,1008-1009,1012-1013,1016-1017,1020-1021,1024,1028,1032,1036,1040,1044,1048,1052,1056-1057,1060-10"
  892. "61,1064-1065,1068-1069,1072-1073,1076-1077,1080-1081,1084-1085,1088,1092,1096,1100,1104,1108,1112,1116,1120-1121"
  893. ",1124-1125,1128-1129,1132-1133,1136-1137,1140-1141,1144-1145,1148-1149,1152,1156,1160,1164,1168,1172,1176,1180,1"
  894. "184-1185,1188-1189,1192-1193,1196-1197,1200-1201,1204-1205,1208-1209,1212-1213,1216,1220,1224,1228,1232,1236,124"
  895. "0,1244,1248-1249,1252-1253,1256-1257,1260-1261,1264-1265,1268-1269,1272-1273,1276-1277,1280,1284,1288,1292,1296,"
  896. "1300,1304,1308,1312-1313,1316-1317,1320-1321,1324-1325,1328-1329,1332-1333,1336-1337,1340-1341,1344,1348,1352,13"
  897. "56,1360,1364,1368,1372,1376-1377,1380-1381,1384-1385,1388-1389,1392-1393,1396-1397,1400-1401,1404-1405,1408,1412"
  898. ",1416,1420,1424,1428,1432,1436,1440-1441,1444-1445,1448-1449,1452-1453,1456-1457,1460-1461,1464-1465,1468-1469,1"
  899. "472,1476,1480,1484,1488,1492,1496,1500,1504-1505,1508-1509,1512-1513,1516-1517,1520-1521,1524-1525,1528-1529,153"
  900. "2-1533,1536,1540,1544,1548,1552,1556,1560,1564,1568-1569,1572-1573,1576-1577,1580-1581,1584-1585,1588-1589,1592-"
  901. "1593,1596-1597,1600,1604,1608,1612,1616,1620,1624,1628,1632-1633,1636-1637,1640-1641,1644-1645,1648-1649,1652-16"
  902. "53,1656-1657,1660-1661,1664,1668,1672,1676,1680,1684,1688,1692,1696-1697,1700-1701,1704-1705,1708-1709,1712-1713"
  903. ",1716-1717,1720-1721,1724-1725,1728,1732,1736,1740,1744,1748,1752,1756,1760-1761,1764-1765,1768-1769,1772-1773,1"
  904. "776-1777,1780-1781,1784-1785,1788-1789,1792,1796,1800,1804,1808,1812,1816,1820,1824-1825,1828-1829,1832-1833,183"
  905. "6-1837,1840-1841,1844-1845,1848-1849,1852-1853,1856,1860,1864,1868,1872,1876,1880,1884,1888-1889,1892-1893,1896-"
  906. "1897,1900-1901,1904-1905,1908-1909,1912-1913,1916-1917,1920,1924,1928,1932,1936,1940,1944,1948,1952-1953,1956-19"
  907. "57,1960-1961,1964-1965,1968-1969,1972-1973,1976-1977,1980-1981,1984,1988,1992,1996,2000,2004,2008,2012,2016-2017"
  908. ",2020-2021,2024-2025,2028-2029,2032-2033,2036-2037,2040-2041,2044-2045,2048,2052,2056,2060,2064,2068,2072,2076,2"
  909. "080-2081,2084-2085,2088-2089,2092-2093,2096-2097,2100-2101,2104-2105,2108-2109,2112,2116,2120,2124,2128,2132,213"
  910. "6,2140,2144-2145,2148-2149,2152-2153,2156-2157,2160-2161,2164-2165,2168-2169,2172-2173,2176,2180,2184,2188,2192,"
  911. "2196,2200,2204,2208-2209,2212-2213,2216-2217,2220-2221,2224-2225,2228-2229,2232-2233,2236-2237,2240,2244,2248,22"
  912. "52,2256,2260,2264,2268,2272-2273,2276-2277,2280-2281,2284-2285,2288-2289,2292-2293,2296-2297,2300-2301,2304,2308"
  913. ",2312,2316,2320,2324,2328,2332,2336-2337,2340-2341,2344-2345,2348-2349,2352-2353,2356-2357,2360-2361,2364-2365,2"
  914. "368,2372,2376,2380,2384,2388,2392,2396,2400-2401,2404-2405,2408-2409,2412-2413,2416-2417,2420-2421,2424-2425,242"
  915. "8-2429,2432,2436,2440,2444,2448,2452,2456,2460,2464-2465,2468-2469,2472-2473,2476-2477,2480-2481,2484-2485,2488-"
  916. "2489,2492-2493,2496,2500,2504,2508,2512,2516,2520,2524,2528-2529,2532-2533,2536-2537,2540-2541,2544-2545,2548-25"
  917. "49,2552-2553,2556-2557\n";
  918. static const struct test_bitmap_print test_print[] __initconst = {
  919. { small_bitmap, sizeof(small_bitmap) * BITS_PER_BYTE, small_mask, small_list },
  920. { large_bitmap, sizeof(large_bitmap) * BITS_PER_BYTE, large_mask, large_list },
  921. };
  922. static void __init test_bitmap_print_buf(void)
  923. {
  924. int i;
  925. for (i = 0; i < ARRAY_SIZE(test_print); i++) {
  926. const struct test_bitmap_print *t = &test_print[i];
  927. int n;
  928. n = bitmap_print_bitmask_to_buf(print_buf, t->bitmap, t->nbits,
  929. 0, 2 * PAGE_SIZE);
  930. expect_eq_uint(strlen(t->mask) + 1, n);
  931. expect_eq_str(t->mask, print_buf, n);
  932. n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
  933. 0, 2 * PAGE_SIZE);
  934. expect_eq_uint(strlen(t->list) + 1, n);
  935. expect_eq_str(t->list, print_buf, n);
  936. /* test by non-zero offset */
  937. if (strlen(t->list) > PAGE_SIZE) {
  938. n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
  939. PAGE_SIZE, PAGE_SIZE);
  940. expect_eq_uint(strlen(t->list) + 1 - PAGE_SIZE, n);
  941. expect_eq_str(t->list + PAGE_SIZE, print_buf, n);
  942. }
  943. }
  944. }
  945. /*
  946. * FIXME: Clang breaks compile-time evaluations when KASAN and GCOV are enabled.
  947. * To workaround it, GCOV is force-disabled in Makefile for this configuration.
  948. */
  949. static void __init test_bitmap_const_eval(void)
  950. {
  951. DECLARE_BITMAP(bitmap, BITS_PER_LONG);
  952. unsigned long initvar = BIT(2);
  953. unsigned long bitopvar = 0;
  954. unsigned long var = 0;
  955. int res;
  956. /*
  957. * Compilers must be able to optimize all of those to compile-time
  958. * constants on any supported optimization level (-O2, -Os) and any
  959. * architecture. Otherwise, trigger a build bug.
  960. * The whole function gets optimized out then, there's nothing to do
  961. * in runtime.
  962. */
  963. /*
  964. * Equals to `unsigned long bitmap[1] = { GENMASK(6, 5), }`.
  965. * Clang on s390 optimizes bitops at compile-time as intended, but at
  966. * the same time stops treating @bitmap and @bitopvar as compile-time
  967. * constants after regular test_bit() is executed, thus triggering the
  968. * build bugs below. So, call const_test_bit() there directly until
  969. * the compiler is fixed.
  970. */
  971. bitmap_clear(bitmap, 0, BITS_PER_LONG);
  972. if (!test_bit(7, bitmap))
  973. bitmap_set(bitmap, 5, 2);
  974. /* Equals to `unsigned long bitopvar = BIT(20)` */
  975. __change_bit(31, &bitopvar);
  976. bitmap_shift_right(&bitopvar, &bitopvar, 11, BITS_PER_LONG);
  977. /* Equals to `unsigned long var = BIT(25)` */
  978. var |= BIT(25);
  979. if (var & BIT(0))
  980. var ^= GENMASK(9, 6);
  981. /* __const_hweight<32|64>(GENMASK(6, 5)) == 2 */
  982. res = bitmap_weight(bitmap, 20);
  983. BUILD_BUG_ON(!__builtin_constant_p(res));
  984. BUILD_BUG_ON(res != 2);
  985. /* !(BIT(31) & BIT(18)) == 1 */
  986. res = !test_bit(18, &bitopvar);
  987. BUILD_BUG_ON(!__builtin_constant_p(res));
  988. BUILD_BUG_ON(!res);
  989. /* BIT(2) & GENMASK(14, 8) == 0 */
  990. res = initvar & GENMASK(14, 8);
  991. BUILD_BUG_ON(!__builtin_constant_p(res));
  992. BUILD_BUG_ON(res);
  993. /* ~BIT(25) */
  994. BUILD_BUG_ON(!__builtin_constant_p(~var));
  995. BUILD_BUG_ON(~var != ~BIT(25));
  996. }
  997. static void __init selftest(void)
  998. {
  999. test_zero_clear();
  1000. test_fill_set();
  1001. test_copy();
  1002. test_replace();
  1003. test_bitmap_arr32();
  1004. test_bitmap_arr64();
  1005. test_bitmap_parse();
  1006. test_bitmap_parselist();
  1007. test_bitmap_printlist();
  1008. test_mem_optimisations();
  1009. test_bitmap_cut();
  1010. test_bitmap_print_buf();
  1011. test_bitmap_const_eval();
  1012. test_find_nth_bit();
  1013. test_for_each_set_bit();
  1014. test_for_each_set_bit_from();
  1015. test_for_each_clear_bit();
  1016. test_for_each_clear_bit_from();
  1017. test_for_each_set_bitrange();
  1018. test_for_each_clear_bitrange();
  1019. test_for_each_set_bitrange_from();
  1020. test_for_each_clear_bitrange_from();
  1021. test_for_each_set_clump8();
  1022. test_for_each_set_bit_wrap();
  1023. }
  1024. KSTM_MODULE_LOADERS(test_bitmap);
  1025. MODULE_AUTHOR("david decotigny <[email protected]>");
  1026. MODULE_LICENSE("GPL");