test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <linux/types.h>
  6. #include <linux/kernel.h>
  7. #include <linux/bitops.h>
  8. #include "test.h"
  9. struct item *
  10. item_tag_set(struct radix_tree_root *root, unsigned long index, int tag)
  11. {
  12. return radix_tree_tag_set(root, index, tag);
  13. }
  14. struct item *
  15. item_tag_clear(struct radix_tree_root *root, unsigned long index, int tag)
  16. {
  17. return radix_tree_tag_clear(root, index, tag);
  18. }
  19. int item_tag_get(struct radix_tree_root *root, unsigned long index, int tag)
  20. {
  21. return radix_tree_tag_get(root, index, tag);
  22. }
  23. int __item_insert(struct radix_tree_root *root, struct item *item)
  24. {
  25. return __radix_tree_insert(root, item->index, item->order, item);
  26. }
  27. struct item *item_create(unsigned long index, unsigned int order)
  28. {
  29. struct item *ret = malloc(sizeof(*ret));
  30. ret->index = index;
  31. ret->order = order;
  32. return ret;
  33. }
  34. int item_insert_order(struct radix_tree_root *root, unsigned long index,
  35. unsigned order)
  36. {
  37. struct item *item = item_create(index, order);
  38. int err = __item_insert(root, item);
  39. if (err)
  40. free(item);
  41. return err;
  42. }
  43. int item_insert(struct radix_tree_root *root, unsigned long index)
  44. {
  45. return item_insert_order(root, index, 0);
  46. }
  47. void item_sanity(struct item *item, unsigned long index)
  48. {
  49. unsigned long mask;
  50. assert(!radix_tree_is_internal_node(item));
  51. assert(item->order < BITS_PER_LONG);
  52. mask = (1UL << item->order) - 1;
  53. assert((item->index | mask) == (index | mask));
  54. }
  55. int item_delete(struct radix_tree_root *root, unsigned long index)
  56. {
  57. struct item *item = radix_tree_delete(root, index);
  58. if (item) {
  59. item_sanity(item, index);
  60. free(item);
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. static void item_free_rcu(struct rcu_head *head)
  66. {
  67. struct item *item = container_of(head, struct item, rcu_head);
  68. free(item);
  69. }
  70. int item_delete_rcu(struct radix_tree_root *root, unsigned long index)
  71. {
  72. struct item *item = radix_tree_delete(root, index);
  73. if (item) {
  74. item_sanity(item, index);
  75. call_rcu(&item->rcu_head, item_free_rcu);
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. void item_check_present(struct radix_tree_root *root, unsigned long index)
  81. {
  82. struct item *item;
  83. item = radix_tree_lookup(root, index);
  84. assert(item != NULL);
  85. item_sanity(item, index);
  86. }
  87. struct item *item_lookup(struct radix_tree_root *root, unsigned long index)
  88. {
  89. return radix_tree_lookup(root, index);
  90. }
  91. void item_check_absent(struct radix_tree_root *root, unsigned long index)
  92. {
  93. struct item *item;
  94. item = radix_tree_lookup(root, index);
  95. assert(item == NULL);
  96. }
  97. /*
  98. * Scan only the passed (start, start+nr] for present items
  99. */
  100. void item_gang_check_present(struct radix_tree_root *root,
  101. unsigned long start, unsigned long nr,
  102. int chunk, int hop)
  103. {
  104. struct item *items[chunk];
  105. unsigned long into;
  106. for (into = 0; into < nr; ) {
  107. int nfound;
  108. int nr_to_find = chunk;
  109. int i;
  110. if (nr_to_find > (nr - into))
  111. nr_to_find = nr - into;
  112. nfound = radix_tree_gang_lookup(root, (void **)items,
  113. start + into, nr_to_find);
  114. assert(nfound == nr_to_find);
  115. for (i = 0; i < nfound; i++)
  116. assert(items[i]->index == start + into + i);
  117. into += hop;
  118. }
  119. }
  120. /*
  121. * Scan the entire tree, only expecting present items (start, start+nr]
  122. */
  123. void item_full_scan(struct radix_tree_root *root, unsigned long start,
  124. unsigned long nr, int chunk)
  125. {
  126. struct item *items[chunk];
  127. unsigned long into = 0;
  128. unsigned long this_index = start;
  129. int nfound;
  130. int i;
  131. // printf("%s(0x%08lx, 0x%08lx, %d)\n", __FUNCTION__, start, nr, chunk);
  132. while ((nfound = radix_tree_gang_lookup(root, (void **)items, into,
  133. chunk))) {
  134. // printf("At 0x%08lx, nfound=%d\n", into, nfound);
  135. for (i = 0; i < nfound; i++) {
  136. assert(items[i]->index == this_index);
  137. this_index++;
  138. }
  139. // printf("Found 0x%08lx->0x%08lx\n",
  140. // items[0]->index, items[nfound-1]->index);
  141. into = this_index;
  142. }
  143. if (chunk)
  144. assert(this_index == start + nr);
  145. nfound = radix_tree_gang_lookup(root, (void **)items,
  146. this_index, chunk);
  147. assert(nfound == 0);
  148. }
  149. /* Use the same pattern as tag_pages_for_writeback() in mm/page-writeback.c */
  150. int tag_tagged_items(struct radix_tree_root *root, pthread_mutex_t *lock,
  151. unsigned long start, unsigned long end, unsigned batch,
  152. unsigned iftag, unsigned thentag)
  153. {
  154. unsigned long tagged = 0;
  155. struct radix_tree_iter iter;
  156. void **slot;
  157. if (batch == 0)
  158. batch = 1;
  159. if (lock)
  160. pthread_mutex_lock(lock);
  161. radix_tree_for_each_tagged(slot, root, &iter, start, iftag) {
  162. if (iter.index > end)
  163. break;
  164. radix_tree_iter_tag_set(root, &iter, thentag);
  165. tagged++;
  166. if ((tagged % batch) != 0)
  167. continue;
  168. slot = radix_tree_iter_resume(slot, &iter);
  169. if (lock) {
  170. pthread_mutex_unlock(lock);
  171. rcu_barrier();
  172. pthread_mutex_lock(lock);
  173. }
  174. }
  175. if (lock)
  176. pthread_mutex_unlock(lock);
  177. return tagged;
  178. }
  179. /* Use the same pattern as find_swap_entry() in mm/shmem.c */
  180. unsigned long find_item(struct radix_tree_root *root, void *item)
  181. {
  182. struct radix_tree_iter iter;
  183. void **slot;
  184. unsigned long found = -1;
  185. unsigned long checked = 0;
  186. radix_tree_for_each_slot(slot, root, &iter, 0) {
  187. if (*slot == item) {
  188. found = iter.index;
  189. break;
  190. }
  191. checked++;
  192. if ((checked % 4) != 0)
  193. continue;
  194. slot = radix_tree_iter_resume(slot, &iter);
  195. }
  196. return found;
  197. }
  198. static int verify_node(struct radix_tree_node *slot, unsigned int tag,
  199. int tagged)
  200. {
  201. int anyset = 0;
  202. int i;
  203. int j;
  204. slot = entry_to_node(slot);
  205. /* Verify consistency at this level */
  206. for (i = 0; i < RADIX_TREE_TAG_LONGS; i++) {
  207. if (slot->tags[tag][i]) {
  208. anyset = 1;
  209. break;
  210. }
  211. }
  212. if (tagged != anyset) {
  213. printf("tag: %u, shift %u, tagged: %d, anyset: %d\n",
  214. tag, slot->shift, tagged, anyset);
  215. for (j = 0; j < RADIX_TREE_MAX_TAGS; j++) {
  216. printf("tag %d: ", j);
  217. for (i = 0; i < RADIX_TREE_TAG_LONGS; i++)
  218. printf("%016lx ", slot->tags[j][i]);
  219. printf("\n");
  220. }
  221. return 1;
  222. }
  223. assert(tagged == anyset);
  224. /* Go for next level */
  225. if (slot->shift > 0) {
  226. for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
  227. if (slot->slots[i])
  228. if (verify_node(slot->slots[i], tag,
  229. !!test_bit(i, slot->tags[tag]))) {
  230. printf("Failure at off %d\n", i);
  231. for (j = 0; j < RADIX_TREE_MAX_TAGS; j++) {
  232. printf("tag %d: ", j);
  233. for (i = 0; i < RADIX_TREE_TAG_LONGS; i++)
  234. printf("%016lx ", slot->tags[j][i]);
  235. printf("\n");
  236. }
  237. return 1;
  238. }
  239. }
  240. return 0;
  241. }
  242. void verify_tag_consistency(struct radix_tree_root *root, unsigned int tag)
  243. {
  244. struct radix_tree_node *node = root->rnode;
  245. if (!radix_tree_is_internal_node(node))
  246. return;
  247. verify_node(node, tag, !!root_tag_get(root, tag));
  248. }
  249. void item_kill_tree(struct radix_tree_root *root)
  250. {
  251. struct radix_tree_iter iter;
  252. void **slot;
  253. struct item *items[32];
  254. int nfound;
  255. radix_tree_for_each_slot(slot, root, &iter, 0) {
  256. if (radix_tree_exceptional_entry(*slot))
  257. radix_tree_delete(root, iter.index);
  258. }
  259. while ((nfound = radix_tree_gang_lookup(root, (void **)items, 0, 32))) {
  260. int i;
  261. for (i = 0; i < nfound; i++) {
  262. void *ret;
  263. ret = radix_tree_delete(root, items[i]->index);
  264. assert(ret == items[i]);
  265. free(items[i]);
  266. }
  267. }
  268. assert(radix_tree_gang_lookup(root, (void **)items, 0, 32) == 0);
  269. assert(root->rnode == NULL);
  270. }
  271. void tree_verify_min_height(struct radix_tree_root *root, int maxindex)
  272. {
  273. unsigned shift;
  274. struct radix_tree_node *node = root->rnode;
  275. if (!radix_tree_is_internal_node(node)) {
  276. assert(maxindex == 0);
  277. return;
  278. }
  279. node = entry_to_node(node);
  280. assert(maxindex <= node_maxindex(node));
  281. shift = node->shift;
  282. if (shift > 0)
  283. assert(maxindex > shift_maxindex(shift - RADIX_TREE_MAP_SHIFT));
  284. else
  285. assert(maxindex > 0);
  286. }