iteration_check.c 4.5 KB

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