idr-test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. void idr_checks(void)
  191. {
  192. unsigned long i;
  193. DEFINE_IDR(idr);
  194. for (i = 0; i < 10000; i++) {
  195. struct item *item = item_create(i, 0);
  196. assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
  197. }
  198. assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
  199. for (i = 0; i < 5000; i++)
  200. item_idr_remove(&idr, i);
  201. idr_remove(&idr, 3);
  202. idr_for_each(&idr, item_idr_free, &idr);
  203. idr_destroy(&idr);
  204. assert(idr_is_empty(&idr));
  205. idr_remove(&idr, 3);
  206. idr_remove(&idr, 0);
  207. assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
  208. idr_remove(&idr, 1);
  209. for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
  210. assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
  211. idr_remove(&idr, 1 << 30);
  212. idr_destroy(&idr);
  213. for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
  214. struct item *item = item_create(i, 0);
  215. assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
  216. }
  217. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
  218. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
  219. idr_for_each(&idr, item_idr_free, &idr);
  220. idr_destroy(&idr);
  221. idr_destroy(&idr);
  222. assert(idr_is_empty(&idr));
  223. idr_set_cursor(&idr, INT_MAX - 3UL);
  224. for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
  225. struct item *item;
  226. unsigned int id;
  227. if (i <= INT_MAX)
  228. item = item_create(i, 0);
  229. else
  230. item = item_create(i - INT_MAX - 1, 0);
  231. id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
  232. assert(id == item->index);
  233. }
  234. idr_for_each(&idr, item_idr_free, &idr);
  235. idr_destroy(&idr);
  236. assert(idr_is_empty(&idr));
  237. for (i = 1; i < 10000; i++) {
  238. struct item *item = item_create(i, 0);
  239. assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
  240. }
  241. idr_for_each(&idr, item_idr_free, &idr);
  242. idr_destroy(&idr);
  243. idr_replace_test();
  244. idr_alloc_test();
  245. idr_null_test();
  246. idr_nowait_test();
  247. idr_get_next_test(0);
  248. idr_get_next_test(1);
  249. idr_get_next_test(4);
  250. idr_u32_test(4);
  251. idr_u32_test(1);
  252. idr_u32_test(0);
  253. }
  254. #define module_init(x)
  255. #define module_exit(x)
  256. #define MODULE_AUTHOR(x)
  257. #define MODULE_LICENSE(x)
  258. #define dump_stack() assert(0)
  259. void ida_dump(struct ida *);
  260. #include "../../../lib/test_ida.c"
  261. /*
  262. * Check that we get the correct error when we run out of memory doing
  263. * allocations. To ensure we run out of memory, just "forget" to preload.
  264. * The first test is for not having a bitmap available, and the second test
  265. * is for not being able to allocate a level of the radix tree.
  266. */
  267. void ida_check_nomem(void)
  268. {
  269. DEFINE_IDA(ida);
  270. int id, err;
  271. err = ida_get_new_above(&ida, 256, &id);
  272. assert(err == -EAGAIN);
  273. err = ida_get_new_above(&ida, 1UL << 30, &id);
  274. assert(err == -EAGAIN);
  275. }
  276. /*
  277. * Check what happens when we fill a leaf and then delete it. This may
  278. * discover mishandling of IDR_FREE.
  279. */
  280. void ida_check_leaf(void)
  281. {
  282. DEFINE_IDA(ida);
  283. int id;
  284. unsigned long i;
  285. for (i = 0; i < IDA_BITMAP_BITS; i++) {
  286. assert(ida_pre_get(&ida, GFP_KERNEL));
  287. assert(!ida_get_new(&ida, &id));
  288. assert(id == i);
  289. }
  290. ida_destroy(&ida);
  291. assert(ida_is_empty(&ida));
  292. assert(ida_pre_get(&ida, GFP_KERNEL));
  293. assert(!ida_get_new(&ida, &id));
  294. assert(id == 0);
  295. ida_destroy(&ida);
  296. assert(ida_is_empty(&ida));
  297. }
  298. /*
  299. * Check handling of conversions between exceptional entries and full bitmaps.
  300. */
  301. void ida_check_conv(void)
  302. {
  303. DEFINE_IDA(ida);
  304. int id;
  305. unsigned long i;
  306. for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) {
  307. assert(ida_pre_get(&ida, GFP_KERNEL));
  308. assert(!ida_get_new_above(&ida, i + 1, &id));
  309. assert(id == i + 1);
  310. assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id));
  311. assert(id == i + BITS_PER_LONG);
  312. ida_remove(&ida, i + 1);
  313. ida_remove(&ida, i + BITS_PER_LONG);
  314. assert(ida_is_empty(&ida));
  315. }
  316. assert(ida_pre_get(&ida, GFP_KERNEL));
  317. for (i = 0; i < IDA_BITMAP_BITS * 2; i++) {
  318. assert(ida_pre_get(&ida, GFP_KERNEL));
  319. assert(!ida_get_new(&ida, &id));
  320. assert(id == i);
  321. }
  322. for (i = IDA_BITMAP_BITS * 2; i > 0; i--) {
  323. ida_remove(&ida, i - 1);
  324. }
  325. assert(ida_is_empty(&ida));
  326. for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) {
  327. assert(ida_pre_get(&ida, GFP_KERNEL));
  328. assert(!ida_get_new(&ida, &id));
  329. assert(id == i);
  330. }
  331. for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) {
  332. ida_remove(&ida, i - 1);
  333. }
  334. assert(ida_is_empty(&ida));
  335. radix_tree_cpu_dead(1);
  336. for (i = 0; i < 1000000; i++) {
  337. int err = ida_get_new(&ida, &id);
  338. if (err == -EAGAIN) {
  339. assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2));
  340. assert(ida_pre_get(&ida, GFP_KERNEL));
  341. err = ida_get_new(&ida, &id);
  342. } else {
  343. assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2));
  344. }
  345. assert(!err);
  346. assert(id == i);
  347. }
  348. ida_destroy(&ida);
  349. }
  350. /*
  351. * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
  352. * Allocating up to 2^31-1 should succeed, and then allocating the next one
  353. * should fail.
  354. */
  355. void ida_check_max(void)
  356. {
  357. DEFINE_IDA(ida);
  358. int id, err;
  359. unsigned long i, j;
  360. for (j = 1; j < 65537; j *= 2) {
  361. unsigned long base = (1UL << 31) - j;
  362. for (i = 0; i < j; i++) {
  363. assert(ida_pre_get(&ida, GFP_KERNEL));
  364. assert(!ida_get_new_above(&ida, base, &id));
  365. assert(id == base + i);
  366. }
  367. assert(ida_pre_get(&ida, GFP_KERNEL));
  368. err = ida_get_new_above(&ida, base, &id);
  369. assert(err == -ENOSPC);
  370. ida_destroy(&ida);
  371. assert(ida_is_empty(&ida));
  372. rcu_barrier();
  373. }
  374. }
  375. void ida_check_random(void)
  376. {
  377. DEFINE_IDA(ida);
  378. DECLARE_BITMAP(bitmap, 2048);
  379. int id, err;
  380. unsigned int i;
  381. time_t s = time(NULL);
  382. repeat:
  383. memset(bitmap, 0, sizeof(bitmap));
  384. for (i = 0; i < 100000; i++) {
  385. int i = rand();
  386. int bit = i & 2047;
  387. if (test_bit(bit, bitmap)) {
  388. __clear_bit(bit, bitmap);
  389. ida_remove(&ida, bit);
  390. } else {
  391. __set_bit(bit, bitmap);
  392. do {
  393. ida_pre_get(&ida, GFP_KERNEL);
  394. err = ida_get_new_above(&ida, bit, &id);
  395. } while (err == -EAGAIN);
  396. assert(!err);
  397. assert(id == bit);
  398. }
  399. }
  400. ida_destroy(&ida);
  401. if (time(NULL) < s + 10)
  402. goto repeat;
  403. }
  404. void ida_simple_get_remove_test(void)
  405. {
  406. DEFINE_IDA(ida);
  407. unsigned long i;
  408. for (i = 0; i < 10000; i++) {
  409. assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
  410. }
  411. assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
  412. for (i = 0; i < 10000; i++) {
  413. ida_simple_remove(&ida, i);
  414. }
  415. assert(ida_is_empty(&ida));
  416. ida_destroy(&ida);
  417. }
  418. void user_ida_checks(void)
  419. {
  420. DEFINE_IDA(ida);
  421. int id;
  422. unsigned long i;
  423. radix_tree_cpu_dead(1);
  424. ida_check_nomem();
  425. for (i = 0; i < 10000; i++) {
  426. assert(ida_pre_get(&ida, GFP_KERNEL));
  427. assert(!ida_get_new(&ida, &id));
  428. assert(id == i);
  429. }
  430. ida_remove(&ida, 20);
  431. ida_remove(&ida, 21);
  432. for (i = 0; i < 3; i++) {
  433. assert(ida_pre_get(&ida, GFP_KERNEL));
  434. assert(!ida_get_new(&ida, &id));
  435. if (i == 2)
  436. assert(id == 10000);
  437. }
  438. for (i = 0; i < 5000; i++)
  439. ida_remove(&ida, i);
  440. assert(ida_pre_get(&ida, GFP_KERNEL));
  441. assert(!ida_get_new_above(&ida, 5000, &id));
  442. assert(id == 10001);
  443. ida_destroy(&ida);
  444. assert(ida_is_empty(&ida));
  445. assert(ida_pre_get(&ida, GFP_KERNEL));
  446. assert(!ida_get_new_above(&ida, 1, &id));
  447. assert(id == 1);
  448. ida_remove(&ida, id);
  449. assert(ida_is_empty(&ida));
  450. ida_destroy(&ida);
  451. assert(ida_is_empty(&ida));
  452. assert(ida_pre_get(&ida, GFP_KERNEL));
  453. assert(!ida_get_new_above(&ida, 1, &id));
  454. ida_destroy(&ida);
  455. assert(ida_is_empty(&ida));
  456. assert(ida_pre_get(&ida, GFP_KERNEL));
  457. assert(!ida_get_new_above(&ida, 1, &id));
  458. assert(id == 1);
  459. assert(ida_pre_get(&ida, GFP_KERNEL));
  460. assert(!ida_get_new_above(&ida, 1025, &id));
  461. assert(id == 1025);
  462. assert(ida_pre_get(&ida, GFP_KERNEL));
  463. assert(!ida_get_new_above(&ida, 10000, &id));
  464. assert(id == 10000);
  465. ida_remove(&ida, 1025);
  466. ida_destroy(&ida);
  467. assert(ida_is_empty(&ida));
  468. ida_check_leaf();
  469. ida_check_max();
  470. ida_check_conv();
  471. ida_check_random();
  472. ida_simple_get_remove_test();
  473. radix_tree_cpu_dead(1);
  474. }
  475. static void *ida_random_fn(void *arg)
  476. {
  477. rcu_register_thread();
  478. ida_check_random();
  479. rcu_unregister_thread();
  480. return NULL;
  481. }
  482. void ida_thread_tests(void)
  483. {
  484. pthread_t threads[20];
  485. int i;
  486. for (i = 0; i < ARRAY_SIZE(threads); i++)
  487. if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
  488. perror("creating ida thread");
  489. exit(1);
  490. }
  491. while (i--)
  492. pthread_join(threads[i], NULL);
  493. }
  494. void ida_tests(void)
  495. {
  496. user_ida_checks();
  497. ida_checks();
  498. ida_exit();
  499. ida_thread_tests();
  500. }
  501. int __weak main(void)
  502. {
  503. radix_tree_init();
  504. idr_checks();
  505. ida_tests();
  506. radix_tree_cpu_dead(1);
  507. rcu_barrier();
  508. if (nr_allocated)
  509. printf("nr_allocated = %d\n", nr_allocated);
  510. return 0;
  511. }