idr-test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. /*
  255. * Check that we get the correct error when we run out of memory doing
  256. * allocations. To ensure we run out of memory, just "forget" to preload.
  257. * The first test is for not having a bitmap available, and the second test
  258. * is for not being able to allocate a level of the radix tree.
  259. */
  260. void ida_check_nomem(void)
  261. {
  262. DEFINE_IDA(ida);
  263. int id, err;
  264. err = ida_get_new_above(&ida, 256, &id);
  265. assert(err == -EAGAIN);
  266. err = ida_get_new_above(&ida, 1UL << 30, &id);
  267. assert(err == -EAGAIN);
  268. }
  269. /*
  270. * Check what happens when we fill a leaf and then delete it. This may
  271. * discover mishandling of IDR_FREE.
  272. */
  273. void ida_check_leaf(void)
  274. {
  275. DEFINE_IDA(ida);
  276. int id;
  277. unsigned long i;
  278. for (i = 0; i < IDA_BITMAP_BITS; i++) {
  279. assert(ida_pre_get(&ida, GFP_KERNEL));
  280. assert(!ida_get_new(&ida, &id));
  281. assert(id == i);
  282. }
  283. ida_destroy(&ida);
  284. assert(ida_is_empty(&ida));
  285. assert(ida_pre_get(&ida, GFP_KERNEL));
  286. assert(!ida_get_new(&ida, &id));
  287. assert(id == 0);
  288. ida_destroy(&ida);
  289. assert(ida_is_empty(&ida));
  290. }
  291. /*
  292. * Check handling of conversions between exceptional entries and full bitmaps.
  293. */
  294. void ida_check_conv(void)
  295. {
  296. DEFINE_IDA(ida);
  297. int id;
  298. unsigned long i;
  299. for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) {
  300. assert(ida_pre_get(&ida, GFP_KERNEL));
  301. assert(!ida_get_new_above(&ida, i + 1, &id));
  302. assert(id == i + 1);
  303. assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id));
  304. assert(id == i + BITS_PER_LONG);
  305. ida_remove(&ida, i + 1);
  306. ida_remove(&ida, i + BITS_PER_LONG);
  307. assert(ida_is_empty(&ida));
  308. }
  309. assert(ida_pre_get(&ida, GFP_KERNEL));
  310. for (i = 0; i < IDA_BITMAP_BITS * 2; i++) {
  311. assert(ida_pre_get(&ida, GFP_KERNEL));
  312. assert(!ida_get_new(&ida, &id));
  313. assert(id == i);
  314. }
  315. for (i = IDA_BITMAP_BITS * 2; i > 0; i--) {
  316. ida_remove(&ida, i - 1);
  317. }
  318. assert(ida_is_empty(&ida));
  319. for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) {
  320. assert(ida_pre_get(&ida, GFP_KERNEL));
  321. assert(!ida_get_new(&ida, &id));
  322. assert(id == i);
  323. }
  324. for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) {
  325. ida_remove(&ida, i - 1);
  326. }
  327. assert(ida_is_empty(&ida));
  328. radix_tree_cpu_dead(1);
  329. for (i = 0; i < 1000000; i++) {
  330. int err = ida_get_new(&ida, &id);
  331. if (err == -EAGAIN) {
  332. assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2));
  333. assert(ida_pre_get(&ida, GFP_KERNEL));
  334. err = ida_get_new(&ida, &id);
  335. } else {
  336. assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2));
  337. }
  338. assert(!err);
  339. assert(id == i);
  340. }
  341. ida_destroy(&ida);
  342. }
  343. /*
  344. * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
  345. * Allocating up to 2^31-1 should succeed, and then allocating the next one
  346. * should fail.
  347. */
  348. void ida_check_max(void)
  349. {
  350. DEFINE_IDA(ida);
  351. int id, err;
  352. unsigned long i, j;
  353. for (j = 1; j < 65537; j *= 2) {
  354. unsigned long base = (1UL << 31) - j;
  355. for (i = 0; i < j; i++) {
  356. assert(ida_pre_get(&ida, GFP_KERNEL));
  357. assert(!ida_get_new_above(&ida, base, &id));
  358. assert(id == base + i);
  359. }
  360. assert(ida_pre_get(&ida, GFP_KERNEL));
  361. err = ida_get_new_above(&ida, base, &id);
  362. assert(err == -ENOSPC);
  363. ida_destroy(&ida);
  364. assert(ida_is_empty(&ida));
  365. rcu_barrier();
  366. }
  367. }
  368. void ida_check_random(void)
  369. {
  370. DEFINE_IDA(ida);
  371. DECLARE_BITMAP(bitmap, 2048);
  372. int id, err;
  373. unsigned int i;
  374. time_t s = time(NULL);
  375. repeat:
  376. memset(bitmap, 0, sizeof(bitmap));
  377. for (i = 0; i < 100000; i++) {
  378. int i = rand();
  379. int bit = i & 2047;
  380. if (test_bit(bit, bitmap)) {
  381. __clear_bit(bit, bitmap);
  382. ida_remove(&ida, bit);
  383. } else {
  384. __set_bit(bit, bitmap);
  385. do {
  386. ida_pre_get(&ida, GFP_KERNEL);
  387. err = ida_get_new_above(&ida, bit, &id);
  388. } while (err == -EAGAIN);
  389. assert(!err);
  390. assert(id == bit);
  391. }
  392. }
  393. ida_destroy(&ida);
  394. if (time(NULL) < s + 10)
  395. goto repeat;
  396. }
  397. void ida_simple_get_remove_test(void)
  398. {
  399. DEFINE_IDA(ida);
  400. unsigned long i;
  401. for (i = 0; i < 10000; i++) {
  402. assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
  403. }
  404. assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
  405. for (i = 0; i < 10000; i++) {
  406. ida_simple_remove(&ida, i);
  407. }
  408. assert(ida_is_empty(&ida));
  409. ida_destroy(&ida);
  410. }
  411. void ida_checks(void)
  412. {
  413. DEFINE_IDA(ida);
  414. int id;
  415. unsigned long i;
  416. radix_tree_cpu_dead(1);
  417. ida_check_nomem();
  418. for (i = 0; i < 10000; i++) {
  419. assert(ida_pre_get(&ida, GFP_KERNEL));
  420. assert(!ida_get_new(&ida, &id));
  421. assert(id == i);
  422. }
  423. ida_remove(&ida, 20);
  424. ida_remove(&ida, 21);
  425. for (i = 0; i < 3; i++) {
  426. assert(ida_pre_get(&ida, GFP_KERNEL));
  427. assert(!ida_get_new(&ida, &id));
  428. if (i == 2)
  429. assert(id == 10000);
  430. }
  431. for (i = 0; i < 5000; i++)
  432. ida_remove(&ida, i);
  433. assert(ida_pre_get(&ida, GFP_KERNEL));
  434. assert(!ida_get_new_above(&ida, 5000, &id));
  435. assert(id == 10001);
  436. ida_destroy(&ida);
  437. assert(ida_is_empty(&ida));
  438. assert(ida_pre_get(&ida, GFP_KERNEL));
  439. assert(!ida_get_new_above(&ida, 1, &id));
  440. assert(id == 1);
  441. ida_remove(&ida, id);
  442. assert(ida_is_empty(&ida));
  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. ida_destroy(&ida);
  448. assert(ida_is_empty(&ida));
  449. assert(ida_pre_get(&ida, GFP_KERNEL));
  450. assert(!ida_get_new_above(&ida, 1, &id));
  451. assert(id == 1);
  452. assert(ida_pre_get(&ida, GFP_KERNEL));
  453. assert(!ida_get_new_above(&ida, 1025, &id));
  454. assert(id == 1025);
  455. assert(ida_pre_get(&ida, GFP_KERNEL));
  456. assert(!ida_get_new_above(&ida, 10000, &id));
  457. assert(id == 10000);
  458. ida_remove(&ida, 1025);
  459. ida_destroy(&ida);
  460. assert(ida_is_empty(&ida));
  461. ida_check_leaf();
  462. ida_check_max();
  463. ida_check_conv();
  464. ida_check_random();
  465. ida_simple_get_remove_test();
  466. radix_tree_cpu_dead(1);
  467. }
  468. static void *ida_random_fn(void *arg)
  469. {
  470. rcu_register_thread();
  471. ida_check_random();
  472. rcu_unregister_thread();
  473. return NULL;
  474. }
  475. void ida_thread_tests(void)
  476. {
  477. pthread_t threads[20];
  478. int i;
  479. for (i = 0; i < ARRAY_SIZE(threads); i++)
  480. if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
  481. perror("creating ida thread");
  482. exit(1);
  483. }
  484. while (i--)
  485. pthread_join(threads[i], NULL);
  486. }
  487. int __weak main(void)
  488. {
  489. radix_tree_init();
  490. idr_checks();
  491. ida_checks();
  492. ida_thread_tests();
  493. radix_tree_cpu_dead(1);
  494. rcu_barrier();
  495. if (nr_allocated)
  496. printf("nr_allocated = %d\n", nr_allocated);
  497. return 0;
  498. }