iteration_check.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * iteration_check.c: test races having to do with radix tree iteration
  3. * Copyright (c) 2016 Intel Corporation
  4. * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/radix-tree.h>
  16. #include <pthread.h>
  17. #include "test.h"
  18. #define NUM_THREADS 5
  19. #define MAX_IDX 100
  20. #define TAG XA_MARK_0
  21. #define NEW_TAG XA_MARK_1
  22. static pthread_t threads[NUM_THREADS];
  23. static unsigned int seeds[3];
  24. static RADIX_TREE(tree, GFP_KERNEL);
  25. static bool test_complete;
  26. static int max_order;
  27. /* relentlessly fill the tree with tagged entries */
  28. static void *add_entries_fn(void *arg)
  29. {
  30. rcu_register_thread();
  31. while (!test_complete) {
  32. unsigned long pgoff;
  33. int order;
  34. for (pgoff = 0; pgoff < MAX_IDX; pgoff++) {
  35. xa_lock(&tree);
  36. for (order = max_order; order >= 0; order--) {
  37. if (item_insert_order(&tree, pgoff, order)
  38. == 0) {
  39. item_tag_set(&tree, pgoff, TAG);
  40. break;
  41. }
  42. }
  43. xa_unlock(&tree);
  44. }
  45. }
  46. rcu_unregister_thread();
  47. return NULL;
  48. }
  49. /*
  50. * Iterate over the tagged entries, doing a radix_tree_iter_retry() as we find
  51. * things that have been removed and randomly resetting our iteration to the
  52. * next chunk with radix_tree_iter_resume(). Both radix_tree_iter_retry() and
  53. * radix_tree_iter_resume() cause radix_tree_next_slot() to be called with a
  54. * NULL 'slot' variable.
  55. */
  56. static void *tagged_iteration_fn(void *arg)
  57. {
  58. struct radix_tree_iter iter;
  59. void **slot;
  60. rcu_register_thread();
  61. while (!test_complete) {
  62. rcu_read_lock();
  63. radix_tree_for_each_tagged(slot, &tree, &iter, 0, TAG) {
  64. void *entry = radix_tree_deref_slot(slot);
  65. if (unlikely(!entry))
  66. continue;
  67. if (radix_tree_deref_retry(entry)) {
  68. slot = radix_tree_iter_retry(&iter);
  69. continue;
  70. }
  71. if (rand_r(&seeds[0]) % 50 == 0) {
  72. slot = radix_tree_iter_resume(slot, &iter);
  73. rcu_read_unlock();
  74. rcu_barrier();
  75. rcu_read_lock();
  76. }
  77. }
  78. rcu_read_unlock();
  79. }
  80. rcu_unregister_thread();
  81. return NULL;
  82. }
  83. /*
  84. * Iterate over the entries, doing a radix_tree_iter_retry() as we find things
  85. * that have been removed and randomly resetting our iteration to the next
  86. * chunk with radix_tree_iter_resume(). Both radix_tree_iter_retry() and
  87. * radix_tree_iter_resume() cause radix_tree_next_slot() to be called with a
  88. * NULL 'slot' variable.
  89. */
  90. static void *untagged_iteration_fn(void *arg)
  91. {
  92. struct radix_tree_iter iter;
  93. void **slot;
  94. rcu_register_thread();
  95. while (!test_complete) {
  96. rcu_read_lock();
  97. radix_tree_for_each_slot(slot, &tree, &iter, 0) {
  98. void *entry = radix_tree_deref_slot(slot);
  99. if (unlikely(!entry))
  100. continue;
  101. if (radix_tree_deref_retry(entry)) {
  102. slot = radix_tree_iter_retry(&iter);
  103. continue;
  104. }
  105. if (rand_r(&seeds[1]) % 50 == 0) {
  106. slot = radix_tree_iter_resume(slot, &iter);
  107. rcu_read_unlock();
  108. rcu_barrier();
  109. rcu_read_lock();
  110. }
  111. }
  112. rcu_read_unlock();
  113. }
  114. rcu_unregister_thread();
  115. return NULL;
  116. }
  117. /*
  118. * Randomly remove entries to help induce radix_tree_iter_retry() calls in the
  119. * two iteration functions.
  120. */
  121. static void *remove_entries_fn(void *arg)
  122. {
  123. rcu_register_thread();
  124. while (!test_complete) {
  125. int pgoff;
  126. pgoff = rand_r(&seeds[2]) % MAX_IDX;
  127. xa_lock(&tree);
  128. item_delete(&tree, pgoff);
  129. xa_unlock(&tree);
  130. }
  131. rcu_unregister_thread();
  132. return NULL;
  133. }
  134. static void *tag_entries_fn(void *arg)
  135. {
  136. rcu_register_thread();
  137. while (!test_complete) {
  138. tag_tagged_items(&tree, 0, MAX_IDX, 10, TAG, NEW_TAG);
  139. }
  140. rcu_unregister_thread();
  141. return NULL;
  142. }
  143. /* This is a unit test for a bug found by the syzkaller tester */
  144. void iteration_test(unsigned order, unsigned test_duration)
  145. {
  146. int i;
  147. printv(1, "Running %siteration tests for %d seconds\n",
  148. order > 0 ? "multiorder " : "", test_duration);
  149. max_order = order;
  150. test_complete = false;
  151. for (i = 0; i < 3; i++)
  152. seeds[i] = rand();
  153. if (pthread_create(&threads[0], NULL, tagged_iteration_fn, NULL)) {
  154. perror("create tagged iteration thread");
  155. exit(1);
  156. }
  157. if (pthread_create(&threads[1], NULL, untagged_iteration_fn, NULL)) {
  158. perror("create untagged iteration thread");
  159. exit(1);
  160. }
  161. if (pthread_create(&threads[2], NULL, add_entries_fn, NULL)) {
  162. perror("create add entry thread");
  163. exit(1);
  164. }
  165. if (pthread_create(&threads[3], NULL, remove_entries_fn, NULL)) {
  166. perror("create remove entry thread");
  167. exit(1);
  168. }
  169. if (pthread_create(&threads[4], NULL, tag_entries_fn, NULL)) {
  170. perror("create tag entry thread");
  171. exit(1);
  172. }
  173. sleep(test_duration);
  174. test_complete = true;
  175. for (i = 0; i < NUM_THREADS; i++) {
  176. if (pthread_join(threads[i], NULL)) {
  177. perror("pthread_join");
  178. exit(1);
  179. }
  180. }
  181. item_kill_tree(&tree);
  182. }