idr-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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(void)
  126. {
  127. unsigned long i;
  128. int nextid;
  129. DEFINE_IDR(idr);
  130. int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
  131. for(i = 0; indices[i]; i++) {
  132. struct item *item = item_create(indices[i], 0);
  133. assert(idr_alloc(&idr, item, indices[i], indices[i+1],
  134. GFP_KERNEL) == indices[i]);
  135. }
  136. for(i = 0, nextid = 0; indices[i]; i++) {
  137. idr_get_next(&idr, &nextid);
  138. assert(nextid == indices[i]);
  139. nextid++;
  140. }
  141. idr_for_each(&idr, item_idr_free, &idr);
  142. idr_destroy(&idr);
  143. }
  144. void idr_checks(void)
  145. {
  146. unsigned long i;
  147. DEFINE_IDR(idr);
  148. for (i = 0; i < 10000; i++) {
  149. struct item *item = item_create(i, 0);
  150. assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
  151. }
  152. assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
  153. for (i = 0; i < 5000; i++)
  154. item_idr_remove(&idr, i);
  155. idr_remove(&idr, 3);
  156. idr_for_each(&idr, item_idr_free, &idr);
  157. idr_destroy(&idr);
  158. assert(idr_is_empty(&idr));
  159. idr_remove(&idr, 3);
  160. idr_remove(&idr, 0);
  161. for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
  162. struct item *item = item_create(i, 0);
  163. assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
  164. }
  165. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
  166. idr_for_each(&idr, item_idr_free, &idr);
  167. idr_destroy(&idr);
  168. idr_destroy(&idr);
  169. assert(idr_is_empty(&idr));
  170. for (i = 1; i < 10000; i++) {
  171. struct item *item = item_create(i, 0);
  172. assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
  173. }
  174. idr_for_each(&idr, item_idr_free, &idr);
  175. idr_destroy(&idr);
  176. idr_replace_test();
  177. idr_alloc_test();
  178. idr_null_test();
  179. idr_nowait_test();
  180. idr_get_next_test();
  181. }
  182. /*
  183. * Check that we get the correct error when we run out of memory doing
  184. * allocations. To ensure we run out of memory, just "forget" to preload.
  185. * The first test is for not having a bitmap available, and the second test
  186. * is for not being able to allocate a level of the radix tree.
  187. */
  188. void ida_check_nomem(void)
  189. {
  190. DEFINE_IDA(ida);
  191. int id, err;
  192. err = ida_get_new_above(&ida, 256, &id);
  193. assert(err == -EAGAIN);
  194. err = ida_get_new_above(&ida, 1UL << 30, &id);
  195. assert(err == -EAGAIN);
  196. }
  197. /*
  198. * Check what happens when we fill a leaf and then delete it. This may
  199. * discover mishandling of IDR_FREE.
  200. */
  201. void ida_check_leaf(void)
  202. {
  203. DEFINE_IDA(ida);
  204. int id;
  205. unsigned long i;
  206. for (i = 0; i < IDA_BITMAP_BITS; i++) {
  207. assert(ida_pre_get(&ida, GFP_KERNEL));
  208. assert(!ida_get_new(&ida, &id));
  209. assert(id == i);
  210. }
  211. ida_destroy(&ida);
  212. assert(ida_is_empty(&ida));
  213. assert(ida_pre_get(&ida, GFP_KERNEL));
  214. assert(!ida_get_new(&ida, &id));
  215. assert(id == 0);
  216. ida_destroy(&ida);
  217. assert(ida_is_empty(&ida));
  218. }
  219. /*
  220. * Check handling of conversions between exceptional entries and full bitmaps.
  221. */
  222. void ida_check_conv(void)
  223. {
  224. DEFINE_IDA(ida);
  225. int id;
  226. unsigned long i;
  227. for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) {
  228. assert(ida_pre_get(&ida, GFP_KERNEL));
  229. assert(!ida_get_new_above(&ida, i + 1, &id));
  230. assert(id == i + 1);
  231. assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id));
  232. assert(id == i + BITS_PER_LONG);
  233. ida_remove(&ida, i + 1);
  234. ida_remove(&ida, i + BITS_PER_LONG);
  235. assert(ida_is_empty(&ida));
  236. }
  237. assert(ida_pre_get(&ida, GFP_KERNEL));
  238. for (i = 0; i < IDA_BITMAP_BITS * 2; i++) {
  239. assert(ida_pre_get(&ida, GFP_KERNEL));
  240. assert(!ida_get_new(&ida, &id));
  241. assert(id == i);
  242. }
  243. for (i = IDA_BITMAP_BITS * 2; i > 0; i--) {
  244. ida_remove(&ida, i - 1);
  245. }
  246. assert(ida_is_empty(&ida));
  247. for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) {
  248. assert(ida_pre_get(&ida, GFP_KERNEL));
  249. assert(!ida_get_new(&ida, &id));
  250. assert(id == i);
  251. }
  252. for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) {
  253. ida_remove(&ida, i - 1);
  254. }
  255. assert(ida_is_empty(&ida));
  256. radix_tree_cpu_dead(1);
  257. for (i = 0; i < 1000000; i++) {
  258. int err = ida_get_new(&ida, &id);
  259. if (err == -EAGAIN) {
  260. assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2));
  261. assert(ida_pre_get(&ida, GFP_KERNEL));
  262. err = ida_get_new(&ida, &id);
  263. } else {
  264. assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2));
  265. }
  266. assert(!err);
  267. assert(id == i);
  268. }
  269. ida_destroy(&ida);
  270. }
  271. /*
  272. * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
  273. * Allocating up to 2^31-1 should succeed, and then allocating the next one
  274. * should fail.
  275. */
  276. void ida_check_max(void)
  277. {
  278. DEFINE_IDA(ida);
  279. int id, err;
  280. unsigned long i, j;
  281. for (j = 1; j < 65537; j *= 2) {
  282. unsigned long base = (1UL << 31) - j;
  283. for (i = 0; i < j; i++) {
  284. assert(ida_pre_get(&ida, GFP_KERNEL));
  285. assert(!ida_get_new_above(&ida, base, &id));
  286. assert(id == base + i);
  287. }
  288. assert(ida_pre_get(&ida, GFP_KERNEL));
  289. err = ida_get_new_above(&ida, base, &id);
  290. assert(err == -ENOSPC);
  291. ida_destroy(&ida);
  292. assert(ida_is_empty(&ida));
  293. rcu_barrier();
  294. }
  295. }
  296. void ida_check_random(void)
  297. {
  298. DEFINE_IDA(ida);
  299. DECLARE_BITMAP(bitmap, 2048);
  300. int id, err;
  301. unsigned int i;
  302. time_t s = time(NULL);
  303. repeat:
  304. memset(bitmap, 0, sizeof(bitmap));
  305. for (i = 0; i < 100000; i++) {
  306. int i = rand();
  307. int bit = i & 2047;
  308. if (test_bit(bit, bitmap)) {
  309. __clear_bit(bit, bitmap);
  310. ida_remove(&ida, bit);
  311. } else {
  312. __set_bit(bit, bitmap);
  313. do {
  314. ida_pre_get(&ida, GFP_KERNEL);
  315. err = ida_get_new_above(&ida, bit, &id);
  316. } while (err == -ENOMEM);
  317. assert(!err);
  318. assert(id == bit);
  319. }
  320. }
  321. ida_destroy(&ida);
  322. if (time(NULL) < s + 10)
  323. goto repeat;
  324. }
  325. void ida_simple_get_remove_test(void)
  326. {
  327. DEFINE_IDA(ida);
  328. unsigned long i;
  329. for (i = 0; i < 10000; i++) {
  330. assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
  331. }
  332. assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
  333. for (i = 0; i < 10000; i++) {
  334. ida_simple_remove(&ida, i);
  335. }
  336. assert(ida_is_empty(&ida));
  337. ida_destroy(&ida);
  338. }
  339. void ida_checks(void)
  340. {
  341. DEFINE_IDA(ida);
  342. int id;
  343. unsigned long i;
  344. radix_tree_cpu_dead(1);
  345. ida_check_nomem();
  346. for (i = 0; i < 10000; i++) {
  347. assert(ida_pre_get(&ida, GFP_KERNEL));
  348. assert(!ida_get_new(&ida, &id));
  349. assert(id == i);
  350. }
  351. ida_remove(&ida, 20);
  352. ida_remove(&ida, 21);
  353. for (i = 0; i < 3; i++) {
  354. assert(ida_pre_get(&ida, GFP_KERNEL));
  355. assert(!ida_get_new(&ida, &id));
  356. if (i == 2)
  357. assert(id == 10000);
  358. }
  359. for (i = 0; i < 5000; i++)
  360. ida_remove(&ida, i);
  361. assert(ida_pre_get(&ida, GFP_KERNEL));
  362. assert(!ida_get_new_above(&ida, 5000, &id));
  363. assert(id == 10001);
  364. ida_destroy(&ida);
  365. assert(ida_is_empty(&ida));
  366. assert(ida_pre_get(&ida, GFP_KERNEL));
  367. assert(!ida_get_new_above(&ida, 1, &id));
  368. assert(id == 1);
  369. ida_remove(&ida, id);
  370. assert(ida_is_empty(&ida));
  371. ida_destroy(&ida);
  372. assert(ida_is_empty(&ida));
  373. assert(ida_pre_get(&ida, GFP_KERNEL));
  374. assert(!ida_get_new_above(&ida, 1, &id));
  375. ida_destroy(&ida);
  376. assert(ida_is_empty(&ida));
  377. assert(ida_pre_get(&ida, GFP_KERNEL));
  378. assert(!ida_get_new_above(&ida, 1, &id));
  379. assert(id == 1);
  380. assert(ida_pre_get(&ida, GFP_KERNEL));
  381. assert(!ida_get_new_above(&ida, 1025, &id));
  382. assert(id == 1025);
  383. assert(ida_pre_get(&ida, GFP_KERNEL));
  384. assert(!ida_get_new_above(&ida, 10000, &id));
  385. assert(id == 10000);
  386. ida_remove(&ida, 1025);
  387. ida_destroy(&ida);
  388. assert(ida_is_empty(&ida));
  389. ida_check_leaf();
  390. ida_check_max();
  391. ida_check_conv();
  392. ida_check_random();
  393. ida_simple_get_remove_test();
  394. radix_tree_cpu_dead(1);
  395. }
  396. static void *ida_random_fn(void *arg)
  397. {
  398. rcu_register_thread();
  399. ida_check_random();
  400. rcu_unregister_thread();
  401. return NULL;
  402. }
  403. void ida_thread_tests(void)
  404. {
  405. pthread_t threads[10];
  406. int i;
  407. for (i = 0; i < ARRAY_SIZE(threads); i++)
  408. if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
  409. perror("creating ida thread");
  410. exit(1);
  411. }
  412. while (i--)
  413. pthread_join(threads[i], NULL);
  414. }
  415. int __weak main(void)
  416. {
  417. radix_tree_init();
  418. idr_checks();
  419. ida_checks();
  420. ida_thread_tests();
  421. radix_tree_cpu_dead(1);
  422. rcu_barrier();
  423. if (nr_allocated)
  424. printf("nr_allocated = %d\n", nr_allocated);
  425. return 0;
  426. }