test_xarray.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * test_xarray.c: Test the XArray API
  4. * Copyright (c) 2017-2018 Microsoft Corporation
  5. * Author: Matthew Wilcox <willy@infradead.org>
  6. */
  7. #include <linux/xarray.h>
  8. #include <linux/module.h>
  9. static unsigned int tests_run;
  10. static unsigned int tests_passed;
  11. #ifndef XA_DEBUG
  12. # ifdef __KERNEL__
  13. void xa_dump(const struct xarray *xa) { }
  14. # endif
  15. #undef XA_BUG_ON
  16. #define XA_BUG_ON(xa, x) do { \
  17. tests_run++; \
  18. if (x) { \
  19. printk("BUG at %s:%d\n", __func__, __LINE__); \
  20. xa_dump(xa); \
  21. dump_stack(); \
  22. } else { \
  23. tests_passed++; \
  24. } \
  25. } while (0)
  26. #endif
  27. static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp)
  28. {
  29. return xa_store(xa, index, xa_mk_value(index & LONG_MAX), gfp);
  30. }
  31. static void xa_erase_index(struct xarray *xa, unsigned long index)
  32. {
  33. XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_value(index & LONG_MAX));
  34. XA_BUG_ON(xa, xa_load(xa, index) != NULL);
  35. }
  36. /*
  37. * If anyone needs this, please move it to xarray.c. We have no current
  38. * users outside the test suite because all current multislot users want
  39. * to use the advanced API.
  40. */
  41. static void *xa_store_order(struct xarray *xa, unsigned long index,
  42. unsigned order, void *entry, gfp_t gfp)
  43. {
  44. XA_STATE_ORDER(xas, xa, index, order);
  45. void *curr;
  46. do {
  47. xas_lock(&xas);
  48. curr = xas_store(&xas, entry);
  49. xas_unlock(&xas);
  50. } while (xas_nomem(&xas, gfp));
  51. return curr;
  52. }
  53. static noinline void check_xa_err(struct xarray *xa)
  54. {
  55. XA_BUG_ON(xa, xa_err(xa_store_index(xa, 0, GFP_NOWAIT)) != 0);
  56. XA_BUG_ON(xa, xa_err(xa_erase(xa, 0)) != 0);
  57. #ifndef __KERNEL__
  58. /* The kernel does not fail GFP_NOWAIT allocations */
  59. XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM);
  60. XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM);
  61. #endif
  62. XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_KERNEL)) != 0);
  63. XA_BUG_ON(xa, xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) != 0);
  64. XA_BUG_ON(xa, xa_err(xa_erase(xa, 1)) != 0);
  65. // kills the test-suite :-(
  66. // XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL);
  67. }
  68. static noinline void check_xa_load(struct xarray *xa)
  69. {
  70. unsigned long i, j;
  71. for (i = 0; i < 1024; i++) {
  72. for (j = 0; j < 1024; j++) {
  73. void *entry = xa_load(xa, j);
  74. if (j < i)
  75. XA_BUG_ON(xa, xa_to_value(entry) != j);
  76. else
  77. XA_BUG_ON(xa, entry);
  78. }
  79. XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL);
  80. }
  81. for (i = 0; i < 1024; i++) {
  82. for (j = 0; j < 1024; j++) {
  83. void *entry = xa_load(xa, j);
  84. if (j >= i)
  85. XA_BUG_ON(xa, xa_to_value(entry) != j);
  86. else
  87. XA_BUG_ON(xa, entry);
  88. }
  89. xa_erase_index(xa, i);
  90. }
  91. XA_BUG_ON(xa, !xa_empty(xa));
  92. }
  93. static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index)
  94. {
  95. unsigned int order;
  96. unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1;
  97. /* NULL elements have no marks set */
  98. XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  99. xa_set_mark(xa, index, XA_MARK_0);
  100. XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  101. /* Storing a pointer will not make a mark appear */
  102. XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL);
  103. XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  104. xa_set_mark(xa, index, XA_MARK_0);
  105. XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0));
  106. /* Setting one mark will not set another mark */
  107. XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0));
  108. XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1));
  109. /* Storing NULL clears marks, and they can't be set again */
  110. xa_erase_index(xa, index);
  111. XA_BUG_ON(xa, !xa_empty(xa));
  112. XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  113. xa_set_mark(xa, index, XA_MARK_0);
  114. XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0));
  115. /*
  116. * Storing a multi-index entry over entries with marks gives the
  117. * entire entry the union of the marks
  118. */
  119. BUG_ON((index % 4) != 0);
  120. for (order = 2; order < max_order; order++) {
  121. unsigned long base = round_down(index, 1UL << order);
  122. unsigned long next = base + (1UL << order);
  123. unsigned long i;
  124. XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL));
  125. xa_set_mark(xa, index + 1, XA_MARK_0);
  126. XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL));
  127. xa_set_mark(xa, index + 2, XA_MARK_1);
  128. XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL));
  129. xa_store_order(xa, index, order, xa_mk_value(index),
  130. GFP_KERNEL);
  131. for (i = base; i < next; i++) {
  132. XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0));
  133. XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_1));
  134. XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_2));
  135. }
  136. XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0));
  137. XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1));
  138. XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2));
  139. xa_erase_index(xa, index);
  140. xa_erase_index(xa, next);
  141. XA_BUG_ON(xa, !xa_empty(xa));
  142. }
  143. XA_BUG_ON(xa, !xa_empty(xa));
  144. }
  145. static noinline void check_xa_mark(struct xarray *xa)
  146. {
  147. unsigned long index;
  148. for (index = 0; index < 16384; index += 4)
  149. check_xa_mark_1(xa, index);
  150. }
  151. static noinline void check_xa_shrink(struct xarray *xa)
  152. {
  153. XA_STATE(xas, xa, 1);
  154. struct xa_node *node;
  155. XA_BUG_ON(xa, !xa_empty(xa));
  156. XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL);
  157. XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL);
  158. /*
  159. * Check that erasing the entry at 1 shrinks the tree and properly
  160. * marks the node as being deleted.
  161. */
  162. xas_lock(&xas);
  163. XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1));
  164. node = xas.xa_node;
  165. XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0));
  166. XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1));
  167. XA_BUG_ON(xa, xa_load(xa, 1) != NULL);
  168. XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS);
  169. XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY);
  170. XA_BUG_ON(xa, xas_load(&xas) != NULL);
  171. xas_unlock(&xas);
  172. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  173. xa_erase_index(xa, 0);
  174. XA_BUG_ON(xa, !xa_empty(xa));
  175. }
  176. static noinline void check_multi_store(struct xarray *xa)
  177. {
  178. #ifdef CONFIG_XARRAY_MULTI
  179. unsigned long i, j, k;
  180. unsigned int max_order = (sizeof(long) == 4) ? 30 : 60;
  181. /* Loading from any position returns the same value */
  182. xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL);
  183. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  184. XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
  185. XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
  186. rcu_read_lock();
  187. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2);
  188. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
  189. rcu_read_unlock();
  190. /* Storing adjacent to the value does not alter the value */
  191. xa_store(xa, 3, xa, GFP_KERNEL);
  192. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0));
  193. XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0));
  194. XA_BUG_ON(xa, xa_load(xa, 2) != NULL);
  195. rcu_read_lock();
  196. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3);
  197. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2);
  198. rcu_read_unlock();
  199. /* Overwriting multiple indexes works */
  200. xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL);
  201. XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1));
  202. XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1));
  203. XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1));
  204. XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1));
  205. XA_BUG_ON(xa, xa_load(xa, 4) != NULL);
  206. rcu_read_lock();
  207. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4);
  208. XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4);
  209. rcu_read_unlock();
  210. /* We can erase multiple values with a single store */
  211. xa_store_order(xa, 0, 63, NULL, GFP_KERNEL);
  212. XA_BUG_ON(xa, !xa_empty(xa));
  213. /* Even when the first slot is empty but the others aren't */
  214. xa_store_index(xa, 1, GFP_KERNEL);
  215. xa_store_index(xa, 2, GFP_KERNEL);
  216. xa_store_order(xa, 0, 2, NULL, GFP_KERNEL);
  217. XA_BUG_ON(xa, !xa_empty(xa));
  218. for (i = 0; i < max_order; i++) {
  219. for (j = 0; j < max_order; j++) {
  220. xa_store_order(xa, 0, i, xa_mk_value(i), GFP_KERNEL);
  221. xa_store_order(xa, 0, j, xa_mk_value(j), GFP_KERNEL);
  222. for (k = 0; k < max_order; k++) {
  223. void *entry = xa_load(xa, (1UL << k) - 1);
  224. if ((i < k) && (j < k))
  225. XA_BUG_ON(xa, entry != NULL);
  226. else
  227. XA_BUG_ON(xa, entry != xa_mk_value(j));
  228. }
  229. xa_erase(xa, 0);
  230. XA_BUG_ON(xa, !xa_empty(xa));
  231. }
  232. }
  233. #endif
  234. }
  235. static DEFINE_XARRAY(array);
  236. static int xarray_checks(void)
  237. {
  238. check_xa_err(&array);
  239. check_xa_load(&array);
  240. check_xa_mark(&array);
  241. check_xa_shrink(&array);
  242. check_multi_store(&array);
  243. printk("XArray: %u of %u tests passed\n", tests_passed, tests_run);
  244. return (tests_run == tests_passed) ? 0 : -EINVAL;
  245. }
  246. static void xarray_exit(void)
  247. {
  248. }
  249. module_init(xarray_checks);
  250. module_exit(xarray_exit);
  251. MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>");
  252. MODULE_LICENSE("GPL");