idr-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 *)0x12)
  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 inline void *idr_mk_value(unsigned long v)
  191. {
  192. BUG_ON((long)v < 0);
  193. return (void *)((v & 1) | 2 | (v << 1));
  194. }
  195. DEFINE_IDR(find_idr);
  196. static void *idr_throbber(void *arg)
  197. {
  198. time_t start = time(NULL);
  199. int id = *(int *)arg;
  200. rcu_register_thread();
  201. do {
  202. idr_alloc(&find_idr, idr_mk_value(id), id, id + 1, GFP_KERNEL);
  203. idr_remove(&find_idr, id);
  204. } while (time(NULL) < start + 10);
  205. rcu_unregister_thread();
  206. return NULL;
  207. }
  208. void idr_find_test_1(int anchor_id, int throbber_id)
  209. {
  210. pthread_t throbber;
  211. time_t start = time(NULL);
  212. pthread_create(&throbber, NULL, idr_throbber, &throbber_id);
  213. BUG_ON(idr_alloc(&find_idr, idr_mk_value(anchor_id), anchor_id,
  214. anchor_id + 1, GFP_KERNEL) != anchor_id);
  215. do {
  216. int id = 0;
  217. void *entry = idr_get_next(&find_idr, &id);
  218. BUG_ON(entry != idr_mk_value(id));
  219. } while (time(NULL) < start + 11);
  220. pthread_join(throbber, NULL);
  221. idr_remove(&find_idr, anchor_id);
  222. BUG_ON(!idr_is_empty(&find_idr));
  223. }
  224. void idr_find_test(void)
  225. {
  226. idr_find_test_1(100000, 0);
  227. idr_find_test_1(0, 100000);
  228. }
  229. void idr_checks(void)
  230. {
  231. unsigned long i;
  232. DEFINE_IDR(idr);
  233. for (i = 0; i < 10000; i++) {
  234. struct item *item = item_create(i, 0);
  235. assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
  236. }
  237. assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
  238. for (i = 0; i < 5000; i++)
  239. item_idr_remove(&idr, i);
  240. idr_remove(&idr, 3);
  241. idr_for_each(&idr, item_idr_free, &idr);
  242. idr_destroy(&idr);
  243. assert(idr_is_empty(&idr));
  244. idr_remove(&idr, 3);
  245. idr_remove(&idr, 0);
  246. assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
  247. idr_remove(&idr, 1);
  248. for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
  249. assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
  250. idr_remove(&idr, 1 << 30);
  251. idr_destroy(&idr);
  252. for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
  253. struct item *item = item_create(i, 0);
  254. assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
  255. }
  256. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
  257. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
  258. idr_for_each(&idr, item_idr_free, &idr);
  259. idr_destroy(&idr);
  260. idr_destroy(&idr);
  261. assert(idr_is_empty(&idr));
  262. idr_set_cursor(&idr, INT_MAX - 3UL);
  263. for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
  264. struct item *item;
  265. unsigned int id;
  266. if (i <= INT_MAX)
  267. item = item_create(i, 0);
  268. else
  269. item = item_create(i - INT_MAX - 1, 0);
  270. id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
  271. assert(id == item->index);
  272. }
  273. idr_for_each(&idr, item_idr_free, &idr);
  274. idr_destroy(&idr);
  275. assert(idr_is_empty(&idr));
  276. for (i = 1; i < 10000; i++) {
  277. struct item *item = item_create(i, 0);
  278. assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
  279. }
  280. idr_for_each(&idr, item_idr_free, &idr);
  281. idr_destroy(&idr);
  282. idr_replace_test();
  283. idr_alloc_test();
  284. idr_null_test();
  285. idr_nowait_test();
  286. idr_get_next_test(0);
  287. idr_get_next_test(1);
  288. idr_get_next_test(4);
  289. idr_u32_test(4);
  290. idr_u32_test(1);
  291. idr_u32_test(0);
  292. idr_find_test();
  293. }
  294. #define module_init(x)
  295. #define module_exit(x)
  296. #define MODULE_AUTHOR(x)
  297. #define MODULE_LICENSE(x)
  298. #define dump_stack() assert(0)
  299. void ida_dump(struct ida *);
  300. #include "../../../lib/test_ida.c"
  301. /*
  302. * Check that we get the correct error when we run out of memory doing
  303. * allocations. In userspace, GFP_NOWAIT will always fail an allocation.
  304. * The first test is for not having a bitmap available, and the second test
  305. * is for not being able to allocate a level of the radix tree.
  306. */
  307. void ida_check_nomem(void)
  308. {
  309. DEFINE_IDA(ida);
  310. int id;
  311. id = ida_alloc_min(&ida, 256, GFP_NOWAIT);
  312. IDA_BUG_ON(&ida, id != -ENOMEM);
  313. id = ida_alloc_min(&ida, 1UL << 30, GFP_NOWAIT);
  314. IDA_BUG_ON(&ida, id != -ENOMEM);
  315. IDA_BUG_ON(&ida, !ida_is_empty(&ida));
  316. }
  317. /*
  318. * Check handling of conversions between exceptional entries and full bitmaps.
  319. */
  320. void ida_check_conv_user(void)
  321. {
  322. DEFINE_IDA(ida);
  323. unsigned long i;
  324. radix_tree_cpu_dead(1);
  325. for (i = 0; i < 1000000; i++) {
  326. int id = ida_alloc(&ida, GFP_NOWAIT);
  327. if (id == -ENOMEM) {
  328. IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) !=
  329. BITS_PER_LONG - 2);
  330. id = ida_alloc(&ida, GFP_KERNEL);
  331. } else {
  332. IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) ==
  333. BITS_PER_LONG - 2);
  334. }
  335. IDA_BUG_ON(&ida, id != i);
  336. }
  337. ida_destroy(&ida);
  338. }
  339. void ida_check_random(void)
  340. {
  341. DEFINE_IDA(ida);
  342. DECLARE_BITMAP(bitmap, 2048);
  343. unsigned int i;
  344. time_t s = time(NULL);
  345. repeat:
  346. memset(bitmap, 0, sizeof(bitmap));
  347. for (i = 0; i < 100000; i++) {
  348. int i = rand();
  349. int bit = i & 2047;
  350. if (test_bit(bit, bitmap)) {
  351. __clear_bit(bit, bitmap);
  352. ida_free(&ida, bit);
  353. } else {
  354. __set_bit(bit, bitmap);
  355. IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL)
  356. != bit);
  357. }
  358. }
  359. ida_destroy(&ida);
  360. if (time(NULL) < s + 10)
  361. goto repeat;
  362. }
  363. void ida_simple_get_remove_test(void)
  364. {
  365. DEFINE_IDA(ida);
  366. unsigned long i;
  367. for (i = 0; i < 10000; i++) {
  368. assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
  369. }
  370. assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
  371. for (i = 0; i < 10000; i++) {
  372. ida_simple_remove(&ida, i);
  373. }
  374. assert(ida_is_empty(&ida));
  375. ida_destroy(&ida);
  376. }
  377. void user_ida_checks(void)
  378. {
  379. radix_tree_cpu_dead(1);
  380. ida_check_nomem();
  381. ida_check_conv_user();
  382. ida_check_random();
  383. ida_simple_get_remove_test();
  384. radix_tree_cpu_dead(1);
  385. }
  386. static void *ida_random_fn(void *arg)
  387. {
  388. rcu_register_thread();
  389. ida_check_random();
  390. rcu_unregister_thread();
  391. return NULL;
  392. }
  393. void ida_thread_tests(void)
  394. {
  395. pthread_t threads[20];
  396. int i;
  397. for (i = 0; i < ARRAY_SIZE(threads); i++)
  398. if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
  399. perror("creating ida thread");
  400. exit(1);
  401. }
  402. while (i--)
  403. pthread_join(threads[i], NULL);
  404. }
  405. void ida_tests(void)
  406. {
  407. user_ida_checks();
  408. ida_checks();
  409. ida_exit();
  410. ida_thread_tests();
  411. }
  412. int __weak main(void)
  413. {
  414. radix_tree_init();
  415. idr_checks();
  416. ida_tests();
  417. radix_tree_cpu_dead(1);
  418. rcu_barrier();
  419. if (nr_allocated)
  420. printf("nr_allocated = %d\n", nr_allocated);
  421. return 0;
  422. }