iteration_check.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * iteration_check.c: test races having to do with xarray iteration
  4. * Copyright (c) 2016 Intel Corporation
  5. * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
  6. */
  7. #include <pthread.h>
  8. #include "test.h"
  9. #define NUM_THREADS 5
  10. #define MAX_IDX 100
  11. #define TAG XA_MARK_0
  12. #define NEW_TAG XA_MARK_1
  13. static pthread_t threads[NUM_THREADS];
  14. static unsigned int seeds[3];
  15. static DEFINE_XARRAY(array);
  16. static bool test_complete;
  17. static int max_order;
  18. void my_item_insert(struct xarray *xa, unsigned long index)
  19. {
  20. XA_STATE(xas, xa, index);
  21. struct item *item = item_create(index, 0);
  22. int order;
  23. retry:
  24. xas_lock(&xas);
  25. for (order = max_order; order >= 0; order--) {
  26. xas_set_order(&xas, index, order);
  27. item->order = order;
  28. if (xas_find_conflict(&xas))
  29. continue;
  30. xas_store(&xas, item);
  31. xas_set_mark(&xas, TAG);
  32. break;
  33. }
  34. xas_unlock(&xas);
  35. if (xas_nomem(&xas, GFP_KERNEL))
  36. goto retry;
  37. if (order < 0)
  38. free(item);
  39. }
  40. /* relentlessly fill the array with tagged entries */
  41. static void *add_entries_fn(void *arg)
  42. {
  43. rcu_register_thread();
  44. while (!test_complete) {
  45. unsigned long pgoff;
  46. for (pgoff = 0; pgoff < MAX_IDX; pgoff++) {
  47. my_item_insert(&array, pgoff);
  48. }
  49. }
  50. rcu_unregister_thread();
  51. return NULL;
  52. }
  53. /*
  54. * Iterate over tagged entries, retrying when we find ourselves in a deleted
  55. * node and randomly pausing the iteration.
  56. */
  57. static void *tagged_iteration_fn(void *arg)
  58. {
  59. XA_STATE(xas, &array, 0);
  60. void *entry;
  61. rcu_register_thread();
  62. while (!test_complete) {
  63. xas_set(&xas, 0);
  64. rcu_read_lock();
  65. xas_for_each_marked(&xas, entry, ULONG_MAX, TAG) {
  66. if (xas_retry(&xas, entry))
  67. continue;
  68. if (rand_r(&seeds[0]) % 50 == 0) {
  69. xas_pause(&xas);
  70. rcu_read_unlock();
  71. rcu_barrier();
  72. rcu_read_lock();
  73. }
  74. }
  75. rcu_read_unlock();
  76. }
  77. rcu_unregister_thread();
  78. return NULL;
  79. }
  80. /*
  81. * Iterate over the entries, retrying when we find ourselves in a deleted
  82. * node and randomly pausing the iteration.
  83. */
  84. static void *untagged_iteration_fn(void *arg)
  85. {
  86. XA_STATE(xas, &array, 0);
  87. void *entry;
  88. rcu_register_thread();
  89. while (!test_complete) {
  90. xas_set(&xas, 0);
  91. rcu_read_lock();
  92. xas_for_each(&xas, entry, ULONG_MAX) {
  93. if (xas_retry(&xas, entry))
  94. continue;
  95. if (rand_r(&seeds[1]) % 50 == 0) {
  96. xas_pause(&xas);
  97. rcu_read_unlock();
  98. rcu_barrier();
  99. rcu_read_lock();
  100. }
  101. }
  102. rcu_read_unlock();
  103. }
  104. rcu_unregister_thread();
  105. return NULL;
  106. }
  107. /*
  108. * Randomly remove entries to help induce retries in the
  109. * two iteration functions.
  110. */
  111. static void *remove_entries_fn(void *arg)
  112. {
  113. rcu_register_thread();
  114. while (!test_complete) {
  115. int pgoff;
  116. struct item *item;
  117. pgoff = rand_r(&seeds[2]) % MAX_IDX;
  118. item = xa_erase(&array, pgoff);
  119. if (item)
  120. item_free(item, pgoff);
  121. }
  122. rcu_unregister_thread();
  123. return NULL;
  124. }
  125. static void *tag_entries_fn(void *arg)
  126. {
  127. rcu_register_thread();
  128. while (!test_complete) {
  129. tag_tagged_items(&array, 0, MAX_IDX, 10, TAG, NEW_TAG);
  130. }
  131. rcu_unregister_thread();
  132. return NULL;
  133. }
  134. /* This is a unit test for a bug found by the syzkaller tester */
  135. void iteration_test(unsigned order, unsigned test_duration)
  136. {
  137. int i;
  138. printv(1, "Running %siteration tests for %d seconds\n",
  139. order > 0 ? "multiorder " : "", test_duration);
  140. max_order = order;
  141. test_complete = false;
  142. for (i = 0; i < 3; i++)
  143. seeds[i] = rand();
  144. if (pthread_create(&threads[0], NULL, tagged_iteration_fn, NULL)) {
  145. perror("create tagged iteration thread");
  146. exit(1);
  147. }
  148. if (pthread_create(&threads[1], NULL, untagged_iteration_fn, NULL)) {
  149. perror("create untagged iteration thread");
  150. exit(1);
  151. }
  152. if (pthread_create(&threads[2], NULL, add_entries_fn, NULL)) {
  153. perror("create add entry thread");
  154. exit(1);
  155. }
  156. if (pthread_create(&threads[3], NULL, remove_entries_fn, NULL)) {
  157. perror("create remove entry thread");
  158. exit(1);
  159. }
  160. if (pthread_create(&threads[4], NULL, tag_entries_fn, NULL)) {
  161. perror("create tag entry thread");
  162. exit(1);
  163. }
  164. sleep(test_duration);
  165. test_complete = true;
  166. for (i = 0; i < NUM_THREADS; i++) {
  167. if (pthread_join(threads[i], NULL)) {
  168. perror("pthread_join");
  169. exit(1);
  170. }
  171. }
  172. item_kill_tree(&array);
  173. }