test_rcu_defer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/kmem.h>
  45. #include <vm/page.h>
  46. #define TEST_LOOPS_PER_PRINT 100000
  47. struct test_pdsc
  48. {
  49. struct work work;
  50. void *addr;
  51. };
  52. #define TEST_VALIDATION_BYTE 0xab
  53. static struct mutex test_lock;
  54. static struct condition test_condition;
  55. static struct test_pdsc *test_pdsc;
  56. static struct kmem_cache test_pdsc_cache;
  57. static void
  58. test_alloc (void *arg __unused)
  59. {
  60. mutex_lock (&test_lock);
  61. for (size_t nr_loops = 0 ; ; ++nr_loops)
  62. {
  63. while (test_pdsc)
  64. condition_wait (&test_condition, &test_lock);
  65. struct test_pdsc *pdsc = kmem_cache_alloc (&test_pdsc_cache);
  66. if (pdsc)
  67. {
  68. _Auto page = vm_page_alloc (0, VM_PAGE_SEL_DIRECTMAP,
  69. VM_PAGE_KERNEL, VM_PAGE_SLEEP);
  70. if (page)
  71. pdsc->addr = memset (vm_page_direct_ptr (page),
  72. TEST_VALIDATION_BYTE, PAGE_SIZE);
  73. }
  74. rcu_store (&test_pdsc, pdsc);
  75. condition_signal (&test_condition);
  76. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0)
  77. printf ("alloc ");
  78. }
  79. }
  80. static void
  81. test_deferred_free (struct work *work)
  82. {
  83. struct test_pdsc *pdsc = structof (work, struct test_pdsc, work);
  84. if (pdsc->addr)
  85. vm_page_free (vm_page_lookup (vm_page_direct_pa ((uintptr_t)pdsc->addr)),
  86. 0, VM_PAGE_SLEEP);
  87. kmem_cache_free (&test_pdsc_cache, pdsc);
  88. }
  89. static void
  90. test_free (void *arg __unused)
  91. {
  92. mutex_lock (&test_lock);
  93. for (size_t nr_loops = 0 ; ; ++nr_loops)
  94. {
  95. while (! test_pdsc)
  96. condition_wait (&test_condition, &test_lock);
  97. struct test_pdsc *pdsc = test_pdsc;
  98. rcu_store (&test_pdsc, NULL);
  99. if (pdsc)
  100. {
  101. work_init (&pdsc->work, test_deferred_free);
  102. rcu_defer (&pdsc->work);
  103. }
  104. condition_signal (&test_condition);
  105. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0)
  106. printf ("free ");
  107. }
  108. }
  109. static void
  110. test_read (void *arg __unused)
  111. {
  112. size_t nr_loops = 0;
  113. while (1)
  114. {
  115. RCU_GUARD ();
  116. struct test_pdsc *pdsc = rcu_load (&test_pdsc);
  117. if (! pdsc)
  118. continue;
  119. _Auto s = (const unsigned char *)pdsc->addr;
  120. if (! s)
  121. continue;
  122. for (size_t i = 0; i < PAGE_SIZE; ++i)
  123. if (s[i] != TEST_VALIDATION_BYTE)
  124. panic ("invalid content");
  125. if ((nr_loops % TEST_LOOPS_PER_PRINT) == 0)
  126. printf ("read ");
  127. ++nr_loops;
  128. }
  129. }
  130. TEST_DEFERRED (rcu_defer)
  131. {
  132. condition_init (&test_condition);
  133. mutex_init (&test_lock);
  134. kmem_cache_init (&test_pdsc_cache, "test_pdsc",
  135. sizeof (struct test_pdsc), 0, NULL, 0);
  136. struct thread_attr attr;
  137. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_rcu_alloc");
  138. thread_attr_set_detached (&attr);
  139. struct thread *thread;
  140. int error = thread_create (&thread, &attr, test_alloc, NULL);
  141. error_check (error, "thread_create");
  142. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_rcu_free");
  143. thread_attr_set_detached (&attr);
  144. error = thread_create (&thread, &attr, test_free, NULL);
  145. error_check (error, "thread_create");
  146. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_rcu_read");
  147. thread_attr_set_detached (&attr);
  148. error = thread_create (&thread, &attr, test_read, NULL);
  149. error_check (error, "thread_create");
  150. return (TEST_RUNNING);
  151. }