idr-test.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/idr.h>
  15. #include <linux/slab.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include "test.h"
  19. #define DUMMY_PTR ((void *)0x12)
  20. int item_idr_free(int id, void *p, void *data)
  21. {
  22. struct item *item = p;
  23. assert(item->index == id);
  24. free(p);
  25. return 0;
  26. }
  27. void item_idr_remove(struct idr *idr, int id)
  28. {
  29. struct item *item = idr_find(idr, id);
  30. assert(item->index == id);
  31. idr_remove(idr, id);
  32. free(item);
  33. }
  34. void idr_alloc_test(void)
  35. {
  36. unsigned long i;
  37. DEFINE_IDR(idr);
  38. assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0, 0x4000, GFP_KERNEL) == 0);
  39. assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0x3ffd, 0x4000, GFP_KERNEL) == 0x3ffd);
  40. idr_remove(&idr, 0x3ffd);
  41. idr_remove(&idr, 0);
  42. for (i = 0x3ffe; i < 0x4003; i++) {
  43. int id;
  44. struct item *item;
  45. if (i < 0x4000)
  46. item = item_create(i, 0);
  47. else
  48. item = item_create(i - 0x3fff, 0);
  49. id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL);
  50. assert(id == item->index);
  51. }
  52. idr_for_each(&idr, item_idr_free, &idr);
  53. idr_destroy(&idr);
  54. }
  55. void idr_replace_test(void)
  56. {
  57. DEFINE_IDR(idr);
  58. idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL);
  59. idr_replace(&idr, &idr, 10);
  60. idr_destroy(&idr);
  61. }
  62. /*
  63. * Unlike the radix tree, you can put a NULL pointer -- with care -- into
  64. * the IDR. Some interfaces, like idr_find() do not distinguish between
  65. * "present, value is NULL" and "not present", but that's exactly what some
  66. * users want.
  67. */
  68. void idr_null_test(void)
  69. {
  70. int i;
  71. DEFINE_IDR(idr);
  72. assert(idr_is_empty(&idr));
  73. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
  74. assert(!idr_is_empty(&idr));
  75. idr_remove(&idr, 0);
  76. assert(idr_is_empty(&idr));
  77. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
  78. assert(!idr_is_empty(&idr));
  79. idr_destroy(&idr);
  80. assert(idr_is_empty(&idr));
  81. for (i = 0; i < 10; i++) {
  82. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i);
  83. }
  84. assert(idr_replace(&idr, DUMMY_PTR, 3) == NULL);
  85. assert(idr_replace(&idr, DUMMY_PTR, 4) == NULL);
  86. assert(idr_replace(&idr, NULL, 4) == DUMMY_PTR);
  87. assert(idr_replace(&idr, DUMMY_PTR, 11) == ERR_PTR(-ENOENT));
  88. idr_remove(&idr, 5);
  89. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 5);
  90. idr_remove(&idr, 5);
  91. for (i = 0; i < 9; i++) {
  92. idr_remove(&idr, i);
  93. assert(!idr_is_empty(&idr));
  94. }
  95. idr_remove(&idr, 8);
  96. assert(!idr_is_empty(&idr));
  97. idr_remove(&idr, 9);
  98. assert(idr_is_empty(&idr));
  99. assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
  100. assert(idr_replace(&idr, DUMMY_PTR, 3) == ERR_PTR(-ENOENT));
  101. assert(idr_replace(&idr, DUMMY_PTR, 0) == NULL);
  102. assert(idr_replace(&idr, NULL, 0) == DUMMY_PTR);
  103. idr_destroy(&idr);
  104. assert(idr_is_empty(&idr));
  105. for (i = 1; i < 10; i++) {
  106. assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i);
  107. }
  108. idr_destroy(&idr);
  109. assert(idr_is_empty(&idr));
  110. }
  111. void idr_nowait_test(void)
  112. {
  113. unsigned int i;
  114. DEFINE_IDR(idr);
  115. idr_preload(GFP_KERNEL);
  116. for (i = 0; i < 3; i++) {
  117. struct item *item = item_create(i, 0);
  118. assert(idr_alloc(&idr, item, i, i + 1, GFP_NOWAIT) == i);
  119. }
  120. idr_preload_end();
  121. idr_for_each(&idr, item_idr_free, &idr);
  122. idr_destroy(&idr);
  123. }
  124. void idr_checks(void)
  125. {
  126. unsigned long i;
  127. DEFINE_IDR(idr);
  128. for (i = 0; i < 10000; i++) {
  129. struct item *item = item_create(i, 0);
  130. assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
  131. }
  132. assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
  133. for (i = 0; i < 5000; i++)
  134. item_idr_remove(&idr, i);
  135. idr_remove(&idr, 3);
  136. idr_for_each(&idr, item_idr_free, &idr);
  137. idr_destroy(&idr);
  138. assert(idr_is_empty(&idr));
  139. idr_remove(&idr, 3);
  140. idr_remove(&idr, 0);
  141. for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
  142. struct item *item = item_create(i, 0);
  143. assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
  144. }
  145. assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
  146. idr_for_each(&idr, item_idr_free, &idr);
  147. idr_destroy(&idr);
  148. idr_destroy(&idr);
  149. assert(idr_is_empty(&idr));
  150. for (i = 1; i < 10000; i++) {
  151. struct item *item = item_create(i, 0);
  152. assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
  153. }
  154. idr_for_each(&idr, item_idr_free, &idr);
  155. idr_destroy(&idr);
  156. idr_replace_test();
  157. idr_alloc_test();
  158. idr_null_test();
  159. idr_nowait_test();
  160. }
  161. /*
  162. * Check that we get the correct error when we run out of memory doing
  163. * allocations. To ensure we run out of memory, just "forget" to preload.
  164. * The first test is for not having a bitmap available, and the second test
  165. * is for not being able to allocate a level of the radix tree.
  166. */
  167. void ida_check_nomem(void)
  168. {
  169. DEFINE_IDA(ida);
  170. int id, err;
  171. err = ida_get_new(&ida, &id);
  172. assert(err == -EAGAIN);
  173. err = ida_get_new_above(&ida, 1UL << 30, &id);
  174. assert(err == -EAGAIN);
  175. }
  176. /*
  177. * Check what happens when we fill a leaf and then delete it. This may
  178. * discover mishandling of IDR_FREE.
  179. */
  180. void ida_check_leaf(void)
  181. {
  182. DEFINE_IDA(ida);
  183. int id;
  184. unsigned long i;
  185. for (i = 0; i < IDA_BITMAP_BITS; i++) {
  186. assert(ida_pre_get(&ida, GFP_KERNEL));
  187. assert(!ida_get_new(&ida, &id));
  188. assert(id == i);
  189. }
  190. ida_destroy(&ida);
  191. assert(ida_is_empty(&ida));
  192. assert(ida_pre_get(&ida, GFP_KERNEL));
  193. assert(!ida_get_new(&ida, &id));
  194. assert(id == 0);
  195. ida_destroy(&ida);
  196. assert(ida_is_empty(&ida));
  197. }
  198. /*
  199. * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
  200. * Allocating up to 2^31-1 should succeed, and then allocating the next one
  201. * should fail.
  202. */
  203. void ida_check_max(void)
  204. {
  205. DEFINE_IDA(ida);
  206. int id, err;
  207. unsigned long i, j;
  208. for (j = 1; j < 65537; j *= 2) {
  209. unsigned long base = (1UL << 31) - j;
  210. for (i = 0; i < j; i++) {
  211. assert(ida_pre_get(&ida, GFP_KERNEL));
  212. assert(!ida_get_new_above(&ida, base, &id));
  213. assert(id == base + i);
  214. }
  215. assert(ida_pre_get(&ida, GFP_KERNEL));
  216. err = ida_get_new_above(&ida, base, &id);
  217. assert(err == -ENOSPC);
  218. ida_destroy(&ida);
  219. assert(ida_is_empty(&ida));
  220. rcu_barrier();
  221. }
  222. }
  223. void ida_checks(void)
  224. {
  225. DEFINE_IDA(ida);
  226. int id;
  227. unsigned long i;
  228. radix_tree_cpu_dead(1);
  229. ida_check_nomem();
  230. for (i = 0; i < 10000; i++) {
  231. assert(ida_pre_get(&ida, GFP_KERNEL));
  232. assert(!ida_get_new(&ida, &id));
  233. assert(id == i);
  234. }
  235. ida_remove(&ida, 20);
  236. ida_remove(&ida, 21);
  237. for (i = 0; i < 3; i++) {
  238. assert(ida_pre_get(&ida, GFP_KERNEL));
  239. assert(!ida_get_new(&ida, &id));
  240. if (i == 2)
  241. assert(id == 10000);
  242. }
  243. for (i = 0; i < 5000; i++)
  244. ida_remove(&ida, i);
  245. assert(ida_pre_get(&ida, GFP_KERNEL));
  246. assert(!ida_get_new_above(&ida, 5000, &id));
  247. assert(id == 10001);
  248. ida_destroy(&ida);
  249. assert(ida_is_empty(&ida));
  250. assert(ida_pre_get(&ida, GFP_KERNEL));
  251. assert(!ida_get_new_above(&ida, 1, &id));
  252. assert(id == 1);
  253. ida_remove(&ida, id);
  254. assert(ida_is_empty(&ida));
  255. ida_destroy(&ida);
  256. assert(ida_is_empty(&ida));
  257. assert(ida_pre_get(&ida, GFP_KERNEL));
  258. assert(!ida_get_new_above(&ida, 1, &id));
  259. ida_destroy(&ida);
  260. assert(ida_is_empty(&ida));
  261. assert(ida_pre_get(&ida, GFP_KERNEL));
  262. assert(!ida_get_new_above(&ida, 1, &id));
  263. assert(id == 1);
  264. assert(ida_pre_get(&ida, GFP_KERNEL));
  265. assert(!ida_get_new_above(&ida, 1025, &id));
  266. assert(id == 1025);
  267. assert(ida_pre_get(&ida, GFP_KERNEL));
  268. assert(!ida_get_new_above(&ida, 10000, &id));
  269. assert(id == 10000);
  270. ida_remove(&ida, 1025);
  271. ida_destroy(&ida);
  272. assert(ida_is_empty(&ida));
  273. ida_check_leaf();
  274. ida_check_max();
  275. radix_tree_cpu_dead(1);
  276. }