idr-test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * idr-test.c: Test the IDR API
  3. * Copyright (c) 2016 Matthew Wilcox <willy@infradead.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/bitmap.h>
  15. #include <linux/idr.h>
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include "test.h"
  20. #define DUMMY_PTR ((void *)0x10)
  21. int item_idr_free(int id, void *p, void *data)
  22. {
  23. struct item *item = p;
  24. assert(item->index == id);
  25. free(p);
  26. return 0;
  27. }
  28. void item_idr_remove(struct idr *idr, int id)
  29. {
  30. struct item *item = idr_find(idr, id);
  31. assert(item->index == id);
  32. idr_remove(idr, id);
  33. free(item);
  34. }
  35. void idr_alloc_test(void)
  36. {
  37. unsigned long i;
  38. DEFINE_IDR(idr);
  39. assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0, 0x4000, GFP_KERNEL) == 0);
  40. assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0x3ffd, 0x4000, GFP_KERNEL) == 0x3ffd);
  41. idr_remove(&idr, 0x3ffd);
  42. idr_remove(&idr, 0);
  43. for (i = 0x3ffe; i < 0x4003; i++) {
  44. int id;
  45. struct item *item;
  46. if (i < 0x4000)
  47. item = item_create(i, 0);
  48. else
  49. item = item_create(i - 0x3fff, 0);
  50. id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL);
  51. assert(id == item->index);
  52. }
  53. idr_for_each(&idr, item_idr_free, &idr);
  54. idr_destroy(&idr);
  55. }
  56. void idr_replace_test(void)
  57. {
  58. DEFINE_IDR(idr);
  59. idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL);
  60. idr_replace(&idr, &idr, 10);
  61. idr_destroy(&idr);
  62. }
  63. /*
  64. * Unlike the radix tree, you can put a NULL pointer -- with care -- into
  65. * the IDR. Some interfaces, like idr_find() do not distinguish between
  66. * "present, value is NULL" and "not present", but that's exactly what some
  67. * users want.
  68. */
  69. void idr_null_test(void)
  70. {
  71. int i;
  72. DEFINE_IDR(idr);
  73. assert(idr_is_empty(&idr));
  74. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
  75. assert(!idr_is_empty(&idr));
  76. idr_remove(&idr, 0);
  77. assert(idr_is_empty(&idr));
  78. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
  79. assert(!idr_is_empty(&idr));
  80. idr_destroy(&idr);
  81. assert(idr_is_empty(&idr));
  82. for (i = 0; i < 10; i++) {
  83. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i);
  84. }
  85. assert(idr_replace(&idr, DUMMY_PTR, 3) == NULL);
  86. assert(idr_replace(&idr, DUMMY_PTR, 4) == NULL);
  87. assert(idr_replace(&idr, NULL, 4) == DUMMY_PTR);
  88. assert(idr_replace(&idr, DUMMY_PTR, 11) == ERR_PTR(-ENOENT));
  89. idr_remove(&idr, 5);
  90. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 5);
  91. idr_remove(&idr, 5);
  92. for (i = 0; i < 9; i++) {
  93. idr_remove(&idr, i);
  94. assert(!idr_is_empty(&idr));
  95. }
  96. idr_remove(&idr, 8);
  97. assert(!idr_is_empty(&idr));
  98. idr_remove(&idr, 9);
  99. assert(idr_is_empty(&idr));
  100. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
  101. assert(idr_replace(&idr, DUMMY_PTR, 3) == ERR_PTR(-ENOENT));
  102. assert(idr_replace(&idr, DUMMY_PTR, 0) == NULL);
  103. assert(idr_replace(&idr, NULL, 0) == DUMMY_PTR);
  104. idr_destroy(&idr);
  105. assert(idr_is_empty(&idr));
  106. for (i = 1; i < 10; i++) {
  107. assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i);
  108. }
  109. idr_destroy(&idr);
  110. assert(idr_is_empty(&idr));
  111. }
  112. void idr_nowait_test(void)
  113. {
  114. unsigned int i;
  115. DEFINE_IDR(idr);
  116. idr_preload(GFP_KERNEL);
  117. for (i = 0; i < 3; i++) {
  118. struct item *item = item_create(i, 0);
  119. assert(idr_alloc(&idr, item, i, i + 1, GFP_NOWAIT) == i);
  120. }
  121. idr_preload_end();
  122. idr_for_each(&idr, item_idr_free, &idr);
  123. idr_destroy(&idr);
  124. }
  125. void idr_get_next_test(int base)
  126. {
  127. unsigned long i;
  128. int nextid;
  129. DEFINE_IDR(idr);
  130. idr_init_base(&idr, base);
  131. int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
  132. for(i = 0; indices[i]; i++) {
  133. struct item *item = item_create(indices[i], 0);
  134. assert(idr_alloc(&idr, item, indices[i], indices[i+1],
  135. GFP_KERNEL) == indices[i]);
  136. }
  137. for(i = 0, nextid = 0; indices[i]; i++) {
  138. idr_get_next(&idr, &nextid);
  139. assert(nextid == indices[i]);
  140. nextid++;
  141. }
  142. idr_for_each(&idr, item_idr_free, &idr);
  143. idr_destroy(&idr);
  144. }
  145. int idr_u32_cb(int id, void *ptr, void *data)
  146. {
  147. BUG_ON(id < 0);
  148. BUG_ON(ptr != DUMMY_PTR);
  149. return 0;
  150. }
  151. void idr_u32_test1(struct idr *idr, u32 handle)
  152. {
  153. static bool warned = false;
  154. u32 id = handle;
  155. int sid = 0;
  156. void *ptr;
  157. BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL));
  158. BUG_ON(id != handle);
  159. BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL) != -ENOSPC);
  160. BUG_ON(id != handle);
  161. if (!warned && id > INT_MAX)
  162. printk("vvv Ignore these warnings\n");
  163. ptr = idr_get_next(idr, &sid);
  164. if (id > INT_MAX) {
  165. BUG_ON(ptr != NULL);
  166. BUG_ON(sid != 0);
  167. } else {
  168. BUG_ON(ptr != DUMMY_PTR);
  169. BUG_ON(sid != id);
  170. }
  171. idr_for_each(idr, idr_u32_cb, NULL);
  172. if (!warned && id > INT_MAX) {
  173. printk("^^^ Warnings over\n");
  174. warned = true;
  175. }
  176. BUG_ON(idr_remove(idr, id) != DUMMY_PTR);
  177. BUG_ON(!idr_is_empty(idr));
  178. }
  179. void idr_u32_test(int base)
  180. {
  181. DEFINE_IDR(idr);
  182. idr_init_base(&idr, base);
  183. idr_u32_test1(&idr, 10);
  184. idr_u32_test1(&idr, 0x7fffffff);
  185. idr_u32_test1(&idr, 0x80000000);
  186. idr_u32_test1(&idr, 0x80000001);
  187. idr_u32_test1(&idr, 0xffe00000);
  188. idr_u32_test1(&idr, 0xffffffff);
  189. }
  190. static void idr_align_test(struct idr *idr)
  191. {
  192. char name[] = "Motorola 68000";
  193. int i, id;
  194. void *entry;
  195. for (i = 0; i < 9; i++) {
  196. BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i);
  197. idr_for_each_entry(idr, entry, id);
  198. }
  199. idr_destroy(idr);
  200. for (i = 1; i < 10; i++) {
  201. BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 1);
  202. idr_for_each_entry(idr, entry, id);
  203. }
  204. idr_destroy(idr);
  205. for (i = 2; i < 11; i++) {
  206. BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 2);
  207. idr_for_each_entry(idr, entry, id);
  208. }
  209. idr_destroy(idr);
  210. for (i = 3; i < 12; i++) {
  211. BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 3);
  212. idr_for_each_entry(idr, entry, id);
  213. }
  214. idr_destroy(idr);
  215. for (i = 0; i < 8; i++) {
  216. BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0);
  217. BUG_ON(idr_alloc(idr, &name[i + 1], 0, 0, GFP_KERNEL) != 1);
  218. idr_for_each_entry(idr, entry, id);
  219. idr_remove(idr, 1);
  220. idr_for_each_entry(idr, entry, id);
  221. idr_remove(idr, 0);
  222. BUG_ON(!idr_is_empty(idr));
  223. }
  224. for (i = 0; i < 8; i++) {
  225. BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 0);
  226. idr_for_each_entry(idr, entry, id);
  227. idr_replace(idr, &name[i], 0);
  228. idr_for_each_entry(idr, entry, id);
  229. BUG_ON(idr_find(idr, 0) != &name[i]);
  230. idr_remove(idr, 0);
  231. }
  232. for (i = 0; i < 8; i++) {
  233. BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0);
  234. BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 1);
  235. idr_remove(idr, 1);
  236. idr_for_each_entry(idr, entry, id);
  237. idr_replace(idr, &name[i + 1], 0);
  238. idr_for_each_entry(idr, entry, id);
  239. idr_remove(idr, 0);
  240. }
  241. }
  242. void idr_checks(void)
  243. {
  244. unsigned long i;
  245. DEFINE_IDR(idr);
  246. for (i = 0; i < 10000; i++) {
  247. struct item *item = item_create(i, 0);
  248. assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
  249. }
  250. assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
  251. for (i = 0; i < 5000; i++)
  252. item_idr_remove(&idr, i);
  253. idr_remove(&idr, 3);
  254. idr_for_each(&idr, item_idr_free, &idr);
  255. idr_destroy(&idr);
  256. assert(idr_is_empty(&idr));
  257. idr_remove(&idr, 3);
  258. idr_remove(&idr, 0);
  259. assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
  260. idr_remove(&idr, 1);
  261. for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
  262. assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
  263. idr_remove(&idr, 1 << 30);
  264. idr_destroy(&idr);
  265. for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
  266. struct item *item = item_create(i, 0);
  267. assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
  268. }
  269. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
  270. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
  271. idr_for_each(&idr, item_idr_free, &idr);
  272. idr_destroy(&idr);
  273. idr_destroy(&idr);
  274. assert(idr_is_empty(&idr));
  275. idr_set_cursor(&idr, INT_MAX - 3UL);
  276. for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
  277. struct item *item;
  278. unsigned int id;
  279. if (i <= INT_MAX)
  280. item = item_create(i, 0);
  281. else
  282. item = item_create(i - INT_MAX - 1, 0);
  283. id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
  284. assert(id == item->index);
  285. }
  286. idr_for_each(&idr, item_idr_free, &idr);
  287. idr_destroy(&idr);
  288. assert(idr_is_empty(&idr));
  289. for (i = 1; i < 10000; i++) {
  290. struct item *item = item_create(i, 0);
  291. assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
  292. }
  293. idr_for_each(&idr, item_idr_free, &idr);
  294. idr_destroy(&idr);
  295. idr_replace_test();
  296. idr_alloc_test();
  297. idr_null_test();
  298. idr_nowait_test();
  299. idr_get_next_test(0);
  300. idr_get_next_test(1);
  301. idr_get_next_test(4);
  302. idr_u32_test(4);
  303. idr_u32_test(1);
  304. idr_u32_test(0);
  305. idr_align_test(&idr);
  306. }
  307. #define module_init(x)
  308. #define module_exit(x)
  309. #define MODULE_AUTHOR(x)
  310. #define MODULE_LICENSE(x)
  311. #define dump_stack() assert(0)
  312. void ida_dump(struct ida *);
  313. #include "../../../lib/test_ida.c"
  314. /*
  315. * Check that we get the correct error when we run out of memory doing
  316. * allocations. In userspace, GFP_NOWAIT will always fail an allocation.
  317. * The first test is for not having a bitmap available, and the second test
  318. * is for not being able to allocate a level of the radix tree.
  319. */
  320. void ida_check_nomem(void)
  321. {
  322. DEFINE_IDA(ida);
  323. int id;
  324. id = ida_alloc_min(&ida, 256, GFP_NOWAIT);
  325. IDA_BUG_ON(&ida, id != -ENOMEM);
  326. id = ida_alloc_min(&ida, 1UL << 30, GFP_NOWAIT);
  327. IDA_BUG_ON(&ida, id != -ENOMEM);
  328. IDA_BUG_ON(&ida, !ida_is_empty(&ida));
  329. }
  330. /*
  331. * Check handling of conversions between exceptional entries and full bitmaps.
  332. */
  333. void ida_check_conv_user(void)
  334. {
  335. DEFINE_IDA(ida);
  336. unsigned long i;
  337. for (i = 0; i < 1000000; i++) {
  338. int id = ida_alloc(&ida, GFP_NOWAIT);
  339. if (id == -ENOMEM) {
  340. IDA_BUG_ON(&ida, ((i % IDA_BITMAP_BITS) !=
  341. BITS_PER_XA_VALUE) &&
  342. ((i % IDA_BITMAP_BITS) != 0));
  343. id = ida_alloc(&ida, GFP_KERNEL);
  344. } else {
  345. IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) ==
  346. BITS_PER_XA_VALUE);
  347. }
  348. IDA_BUG_ON(&ida, id != i);
  349. }
  350. ida_destroy(&ida);
  351. }
  352. void ida_check_random(void)
  353. {
  354. DEFINE_IDA(ida);
  355. DECLARE_BITMAP(bitmap, 2048);
  356. unsigned int i;
  357. time_t s = time(NULL);
  358. repeat:
  359. memset(bitmap, 0, sizeof(bitmap));
  360. for (i = 0; i < 100000; i++) {
  361. int i = rand();
  362. int bit = i & 2047;
  363. if (test_bit(bit, bitmap)) {
  364. __clear_bit(bit, bitmap);
  365. ida_free(&ida, bit);
  366. } else {
  367. __set_bit(bit, bitmap);
  368. IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL)
  369. != bit);
  370. }
  371. }
  372. ida_destroy(&ida);
  373. if (time(NULL) < s + 10)
  374. goto repeat;
  375. }
  376. void ida_simple_get_remove_test(void)
  377. {
  378. DEFINE_IDA(ida);
  379. unsigned long i;
  380. for (i = 0; i < 10000; i++) {
  381. assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
  382. }
  383. assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
  384. for (i = 0; i < 10000; i++) {
  385. ida_simple_remove(&ida, i);
  386. }
  387. assert(ida_is_empty(&ida));
  388. ida_destroy(&ida);
  389. }
  390. void user_ida_checks(void)
  391. {
  392. radix_tree_cpu_dead(1);
  393. ida_check_nomem();
  394. ida_check_conv_user();
  395. ida_check_random();
  396. ida_simple_get_remove_test();
  397. radix_tree_cpu_dead(1);
  398. }
  399. static void *ida_random_fn(void *arg)
  400. {
  401. rcu_register_thread();
  402. ida_check_random();
  403. rcu_unregister_thread();
  404. return NULL;
  405. }
  406. void ida_thread_tests(void)
  407. {
  408. pthread_t threads[20];
  409. int i;
  410. for (i = 0; i < ARRAY_SIZE(threads); i++)
  411. if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
  412. perror("creating ida thread");
  413. exit(1);
  414. }
  415. while (i--)
  416. pthread_join(threads[i], NULL);
  417. }
  418. void ida_tests(void)
  419. {
  420. user_ida_checks();
  421. ida_checks();
  422. ida_exit();
  423. ida_thread_tests();
  424. }
  425. int __weak main(void)
  426. {
  427. radix_tree_init();
  428. idr_checks();
  429. ida_tests();
  430. radix_tree_cpu_dead(1);
  431. rcu_barrier();
  432. if (nr_allocated)
  433. printf("nr_allocated = %d\n", nr_allocated);
  434. return 0;
  435. }