tiny.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Read-Copy Update mechanism for mutual exclusion, the Bloatwatch edition.
  4. *
  5. * Copyright IBM Corporation, 2008
  6. *
  7. * Author: Paul E. McKenney <[email protected]>
  8. *
  9. * For detailed explanation of Read-Copy Update mechanism see -
  10. * Documentation/RCU
  11. */
  12. #include <linux/completion.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/notifier.h>
  15. #include <linux/rcupdate_wait.h>
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include <linux/mutex.h>
  19. #include <linux/sched.h>
  20. #include <linux/types.h>
  21. #include <linux/init.h>
  22. #include <linux/time.h>
  23. #include <linux/cpu.h>
  24. #include <linux/prefetch.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include "rcu.h"
  28. /* Global control variables for rcupdate callback mechanism. */
  29. struct rcu_ctrlblk {
  30. struct rcu_head *rcucblist; /* List of pending callbacks (CBs). */
  31. struct rcu_head **donetail; /* ->next pointer of last "done" CB. */
  32. struct rcu_head **curtail; /* ->next pointer of last CB. */
  33. unsigned long gp_seq; /* Grace-period counter. */
  34. };
  35. /* Definition for rcupdate control block. */
  36. static struct rcu_ctrlblk rcu_ctrlblk = {
  37. .donetail = &rcu_ctrlblk.rcucblist,
  38. .curtail = &rcu_ctrlblk.rcucblist,
  39. .gp_seq = 0 - 300UL,
  40. };
  41. void rcu_barrier(void)
  42. {
  43. wait_rcu_gp(call_rcu_hurry);
  44. }
  45. EXPORT_SYMBOL(rcu_barrier);
  46. /* Record an rcu quiescent state. */
  47. void rcu_qs(void)
  48. {
  49. unsigned long flags;
  50. local_irq_save(flags);
  51. if (rcu_ctrlblk.donetail != rcu_ctrlblk.curtail) {
  52. rcu_ctrlblk.donetail = rcu_ctrlblk.curtail;
  53. raise_softirq_irqoff(RCU_SOFTIRQ);
  54. }
  55. WRITE_ONCE(rcu_ctrlblk.gp_seq, rcu_ctrlblk.gp_seq + 2);
  56. local_irq_restore(flags);
  57. }
  58. /*
  59. * Check to see if the scheduling-clock interrupt came from an extended
  60. * quiescent state, and, if so, tell RCU about it. This function must
  61. * be called from hardirq context. It is normally called from the
  62. * scheduling-clock interrupt.
  63. */
  64. void rcu_sched_clock_irq(int user)
  65. {
  66. if (user) {
  67. rcu_qs();
  68. } else if (rcu_ctrlblk.donetail != rcu_ctrlblk.curtail) {
  69. set_tsk_need_resched(current);
  70. set_preempt_need_resched();
  71. }
  72. }
  73. /*
  74. * Reclaim the specified callback, either by invoking it for non-kfree cases or
  75. * freeing it directly (for kfree). Return true if kfreeing, false otherwise.
  76. */
  77. static inline bool rcu_reclaim_tiny(struct rcu_head *head)
  78. {
  79. rcu_callback_t f;
  80. unsigned long offset = (unsigned long)head->func;
  81. rcu_lock_acquire(&rcu_callback_map);
  82. if (__is_kvfree_rcu_offset(offset)) {
  83. trace_rcu_invoke_kvfree_callback("", head, offset);
  84. kvfree((void *)head - offset);
  85. rcu_lock_release(&rcu_callback_map);
  86. return true;
  87. }
  88. trace_rcu_invoke_callback("", head);
  89. f = head->func;
  90. WRITE_ONCE(head->func, (rcu_callback_t)0L);
  91. f(head);
  92. rcu_lock_release(&rcu_callback_map);
  93. return false;
  94. }
  95. /* Invoke the RCU callbacks whose grace period has elapsed. */
  96. static __latent_entropy void rcu_process_callbacks(struct softirq_action *unused)
  97. {
  98. struct rcu_head *next, *list;
  99. unsigned long flags;
  100. /* Move the ready-to-invoke callbacks to a local list. */
  101. local_irq_save(flags);
  102. if (rcu_ctrlblk.donetail == &rcu_ctrlblk.rcucblist) {
  103. /* No callbacks ready, so just leave. */
  104. local_irq_restore(flags);
  105. return;
  106. }
  107. list = rcu_ctrlblk.rcucblist;
  108. rcu_ctrlblk.rcucblist = *rcu_ctrlblk.donetail;
  109. *rcu_ctrlblk.donetail = NULL;
  110. if (rcu_ctrlblk.curtail == rcu_ctrlblk.donetail)
  111. rcu_ctrlblk.curtail = &rcu_ctrlblk.rcucblist;
  112. rcu_ctrlblk.donetail = &rcu_ctrlblk.rcucblist;
  113. local_irq_restore(flags);
  114. /* Invoke the callbacks on the local list. */
  115. while (list) {
  116. next = list->next;
  117. prefetch(next);
  118. debug_rcu_head_unqueue(list);
  119. local_bh_disable();
  120. rcu_reclaim_tiny(list);
  121. local_bh_enable();
  122. list = next;
  123. }
  124. }
  125. /*
  126. * Wait for a grace period to elapse. But it is illegal to invoke
  127. * synchronize_rcu() from within an RCU read-side critical section.
  128. * Therefore, any legal call to synchronize_rcu() is a quiescent state,
  129. * and so on a UP system, synchronize_rcu() need do nothing, other than
  130. * let the polled APIs know that another grace period elapsed.
  131. *
  132. * (But Lai Jiangshan points out the benefits of doing might_sleep()
  133. * to reduce latency.)
  134. *
  135. * Cool, huh? (Due to Josh Triplett.)
  136. */
  137. void synchronize_rcu(void)
  138. {
  139. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
  140. lock_is_held(&rcu_lock_map) ||
  141. lock_is_held(&rcu_sched_lock_map),
  142. "Illegal synchronize_rcu() in RCU read-side critical section");
  143. WRITE_ONCE(rcu_ctrlblk.gp_seq, rcu_ctrlblk.gp_seq + 2);
  144. }
  145. EXPORT_SYMBOL_GPL(synchronize_rcu);
  146. static void tiny_rcu_leak_callback(struct rcu_head *rhp)
  147. {
  148. }
  149. /*
  150. * Post an RCU callback to be invoked after the end of an RCU grace
  151. * period. But since we have but one CPU, that would be after any
  152. * quiescent state.
  153. */
  154. void call_rcu(struct rcu_head *head, rcu_callback_t func)
  155. {
  156. static atomic_t doublefrees;
  157. unsigned long flags;
  158. if (debug_rcu_head_queue(head)) {
  159. if (atomic_inc_return(&doublefrees) < 4) {
  160. pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func);
  161. mem_dump_obj(head);
  162. }
  163. if (!__is_kvfree_rcu_offset((unsigned long)head->func))
  164. WRITE_ONCE(head->func, tiny_rcu_leak_callback);
  165. return;
  166. }
  167. head->func = func;
  168. head->next = NULL;
  169. local_irq_save(flags);
  170. *rcu_ctrlblk.curtail = head;
  171. rcu_ctrlblk.curtail = &head->next;
  172. local_irq_restore(flags);
  173. if (unlikely(is_idle_task(current))) {
  174. /* force scheduling for rcu_qs() */
  175. resched_cpu(0);
  176. }
  177. }
  178. EXPORT_SYMBOL_GPL(call_rcu);
  179. /*
  180. * Store a grace-period-counter "cookie". For more information,
  181. * see the Tree RCU header comment.
  182. */
  183. void get_completed_synchronize_rcu_full(struct rcu_gp_oldstate *rgosp)
  184. {
  185. rgosp->rgos_norm = RCU_GET_STATE_COMPLETED;
  186. }
  187. EXPORT_SYMBOL_GPL(get_completed_synchronize_rcu_full);
  188. /*
  189. * Return a grace-period-counter "cookie". For more information,
  190. * see the Tree RCU header comment.
  191. */
  192. unsigned long get_state_synchronize_rcu(void)
  193. {
  194. return READ_ONCE(rcu_ctrlblk.gp_seq);
  195. }
  196. EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
  197. /*
  198. * Return a grace-period-counter "cookie" and ensure that a future grace
  199. * period completes. For more information, see the Tree RCU header comment.
  200. */
  201. unsigned long start_poll_synchronize_rcu(void)
  202. {
  203. unsigned long gp_seq = get_state_synchronize_rcu();
  204. if (unlikely(is_idle_task(current))) {
  205. /* force scheduling for rcu_qs() */
  206. resched_cpu(0);
  207. }
  208. return gp_seq;
  209. }
  210. EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu);
  211. /*
  212. * Return true if the grace period corresponding to oldstate has completed
  213. * and false otherwise. For more information, see the Tree RCU header
  214. * comment.
  215. */
  216. bool poll_state_synchronize_rcu(unsigned long oldstate)
  217. {
  218. return oldstate == RCU_GET_STATE_COMPLETED || READ_ONCE(rcu_ctrlblk.gp_seq) != oldstate;
  219. }
  220. EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu);
  221. #ifdef CONFIG_KASAN_GENERIC
  222. void kvfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
  223. {
  224. if (head) {
  225. void *ptr = (void *) head - (unsigned long) func;
  226. kasan_record_aux_stack_noalloc(ptr);
  227. }
  228. __kvfree_call_rcu(head, func);
  229. }
  230. EXPORT_SYMBOL_GPL(kvfree_call_rcu);
  231. #endif
  232. void __init rcu_init(void)
  233. {
  234. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  235. rcu_early_boot_tests();
  236. }