idr-test.c 13 KB

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