test_rcu_defer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2014-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This test module is a stress test, expected to never terminate, of the
  19. * work deferring functionality of the rcu module. It creates three
  20. * threads, a producer, a consumer, and a reader. The producer allocates
  21. * a page and writes it. It then transfers the page to the consumer, using
  22. * the rcu interface to update the global page pointer. Once at the
  23. * consumer, the rcu interface is used to defer the release of the page.
  24. * Concurrently, the reader accesses the page and checks its content when
  25. * available. These accesses are performed inside a read-side critical
  26. * section and should therefore never fail.
  27. *
  28. * Each thread regularly prints a string to report that it's making progress.
  29. */
  30. #include <stddef.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <kern/condition.h>
  34. #include <kern/error.h>
  35. #include <kern/kmem.h>
  36. #include <kern/macros.h>
  37. #include <kern/mutex.h>
  38. #include <kern/panic.h>
  39. #include <kern/rcu.h>
  40. #include <kern/thread.h>
  41. #include <kern/work.h>
  42. #include <machine/page.h>
  43. #include <test/test.h>
  44. #include <vm/vm_kmem.h>
  45. #define TEST_LOOPS_PER_PRINT 100000
  46. struct test_pdsc {
  47. struct work work;
  48. void *addr;
  49. };
  50. #define TEST_VALIDATION_BYTE 0xab
  51. static struct mutex test_lock;
  52. static struct condition test_condition;
  53. static struct test_pdsc *test_pdsc;
  54. static struct kmem_cache test_pdsc_cache;
  55. static void
  56. test_alloc(void *arg)
  57. {
  58. struct test_pdsc *pdsc;
  59. unsigned long nr_loops;
  60. (void)arg;
  61. nr_loops = 0;
  62. mutex_lock(&test_lock);
  63. for (;;) {
  64. while (test_pdsc != NULL) {
  65. condition_wait(&test_condition, &test_lock);
  66. }
  67. pdsc = kmem_cache_alloc(&test_pdsc_cache);
  68. if (pdsc != NULL) {
  69. pdsc->addr = vm_kmem_alloc(PAGE_SIZE);
  70. if (pdsc->addr != NULL) {
  71. memset(pdsc->addr, TEST_VALIDATION_BYTE, PAGE_SIZE);
  72. }
  73. }
  74. rcu_store_ptr(test_pdsc, pdsc);
  75. condition_signal(&test_condition);
  76. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0) {
  77. printf("alloc ");
  78. }
  79. nr_loops++;
  80. }
  81. }
  82. static void
  83. test_deferred_free(struct work *work)
  84. {
  85. struct test_pdsc *pdsc;
  86. pdsc = structof(work, struct test_pdsc, work);
  87. if (pdsc->addr != NULL) {
  88. vm_kmem_free(pdsc->addr, PAGE_SIZE);
  89. }
  90. kmem_cache_free(&test_pdsc_cache, pdsc);
  91. }
  92. static void
  93. test_free(void *arg)
  94. {
  95. struct test_pdsc *pdsc;
  96. unsigned long nr_loops;
  97. (void)arg;
  98. nr_loops = 0;
  99. mutex_lock(&test_lock);
  100. for (;;) {
  101. while (test_pdsc == NULL) {
  102. condition_wait(&test_condition, &test_lock);
  103. }
  104. pdsc = test_pdsc;
  105. rcu_store_ptr(test_pdsc, NULL);
  106. if (pdsc != NULL) {
  107. work_init(&pdsc->work, test_deferred_free);
  108. rcu_defer(&pdsc->work);
  109. }
  110. condition_signal(&test_condition);
  111. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0) {
  112. printf("free ");
  113. }
  114. nr_loops++;
  115. }
  116. }
  117. static void
  118. test_read(void *arg)
  119. {
  120. const struct test_pdsc *pdsc;
  121. const unsigned char *s;
  122. unsigned long nr_loops;
  123. (void)arg;
  124. nr_loops = 0;
  125. for (;;) {
  126. rcu_read_enter();
  127. pdsc = rcu_load_ptr(test_pdsc);
  128. if (pdsc != NULL) {
  129. s = (const unsigned char *)pdsc->addr;
  130. if (s != NULL) {
  131. for (unsigned int i = 0; i < PAGE_SIZE; i++) {
  132. if (s[i] != TEST_VALIDATION_BYTE) {
  133. panic("invalid content");
  134. }
  135. }
  136. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0) {
  137. printf("read ");
  138. }
  139. nr_loops++;
  140. }
  141. }
  142. rcu_read_leave();
  143. }
  144. }
  145. void
  146. test_setup(void)
  147. {
  148. struct thread_attr attr;
  149. struct thread *thread;
  150. int error;
  151. condition_init(&test_condition);
  152. mutex_init(&test_lock);
  153. kmem_cache_init(&test_pdsc_cache, "test_pdsc",
  154. sizeof(struct test_pdsc), 0, NULL, 0);
  155. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_alloc");
  156. thread_attr_set_detached(&attr);
  157. error = thread_create(&thread, &attr, test_alloc, NULL);
  158. error_check(error, "thread_create");
  159. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_free");
  160. thread_attr_set_detached(&attr);
  161. error = thread_create(&thread, &attr, test_free, NULL);
  162. error_check(error, "thread_create");
  163. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_read");
  164. thread_attr_set_detached(&attr);
  165. error = thread_create(&thread, &attr, test_read, NULL);
  166. error_check(error, "thread_create");
  167. }