test_bitmap.c 43 KB

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