| 1 | |
| 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 |
|
| 28 | #include "rcu.h"
|
| 29 |
|
| 30 |
|
| 31 | struct rcu_ctrlblk {
|
| 32 | struct rcu_head *rcucblist;
|
| 33 | struct rcu_head **donetail;
|
| 34 | struct rcu_head **curtail;
|
| 35 | unsigned long gp_seq;
|
| 36 | };
|
| 37 |
|
| 38 |
|
| 39 | static struct rcu_ctrlblk rcu_ctrlblk = {
|
| 40 | .donetail = &rcu_ctrlblk.rcucblist,
|
| 41 | .curtail = &rcu_ctrlblk.rcucblist,
|
| 42 | .gp_seq = 0 - 300UL,
|
| 43 | };
|
| 44 |
|
| 45 | void rcu_barrier(void)
|
| 46 | {
|
| 47 | wait_rcu_gp(call_rcu_hurry);
|
| 48 | }
|
| 49 | EXPORT_SYMBOL(rcu_barrier);
|
| 50 |
|
| 51 |
|
| 52 | void rcu_qs(void)
|
| 53 | {
|
| 54 | unsigned long flags;
|
| 55 |
|
| 56 | local_irq_save(flags);
|
| 57 | if (rcu_ctrlblk.donetail != rcu_ctrlblk.curtail) {
|
| 58 | rcu_ctrlblk.donetail = rcu_ctrlblk.curtail;
|
| 59 | raise_softirq_irqoff(RCU_SOFTIRQ);
|
| 60 | }
|
| 61 | WRITE_ONCE(rcu_ctrlblk.gp_seq, rcu_ctrlblk.gp_seq + 2);
|
| 62 | local_irq_restore(flags);
|
| 63 | }
|
| 64 |
|
| 65 | |
| 66 | * Check to see if the scheduling-clock interrupt came from an extended
|
| 67 | * quiescent state, and, if so, tell RCU about it. This function must
|
| 68 | * be called from hardirq context. It is normally called from the
|
| 69 | * scheduling-clock interrupt.
|
| 70 |
|
| 71 | void rcu_sched_clock_irq(int user)
|
| 72 | {
|
| 73 | if (user)
|
| 74 | rcu_qs();
|
| 75 | else if (rcu_ctrlblk.donetail != rcu_ctrlblk.curtail)
|
| 76 | set_need_resched_current();
|
| 77 | }
|
| 78 |
|
| 79 | |
| 80 | * Reclaim the specified callback, either by invoking it for non-kfree cases or
|
| 81 | * freeing it directly (for kfree). Return true if kfreeing, false otherwise.
|
| 82 |
|
| 83 | static inline bool rcu_reclaim_tiny(struct rcu_head *head)
|
| 84 | {
|
| 85 | rcu_callback_t f;
|
| 86 |
|
| 87 | rcu_lock_acquire(&rcu_callback_map);
|
| 88 |
|
| 89 | trace_rcu_invoke_callback("", head);
|
| 90 | f = head->func;
|
| 91 | debug_rcu_head_callback(head);
|
| 92 | WRITE_ONCE(head->func, (rcu_callback_t)0L);
|
| 93 | f(head);
|
| 94 | rcu_lock_release(&rcu_callback_map);
|
| 95 | return false;
|
| 96 | }
|
| 97 |
|
| 98 |
|
| 99 | static __latent_entropy void rcu_process_callbacks(void)
|
| 100 | {
|
| 101 | struct rcu_head *next, *list;
|
| 102 | unsigned long flags;
|
| 103 |
|
| 104 |
|
| 105 | local_irq_save(flags);
|
| 106 | if (rcu_ctrlblk.donetail == &rcu_ctrlblk.rcucblist) {
|
| 107 |
|
| 108 | local_irq_restore(flags);
|
| 109 | return;
|
| 110 | }
|
| 111 | list = rcu_ctrlblk.rcucblist;
|
| 112 | rcu_ctrlblk.rcucblist = *rcu_ctrlblk.donetail;
|
| 113 | *rcu_ctrlblk.donetail = NULL;
|
| 114 | if (rcu_ctrlblk.curtail == rcu_ctrlblk.donetail)
|
| 115 | rcu_ctrlblk.curtail = &rcu_ctrlblk.rcucblist;
|
| 116 | rcu_ctrlblk.donetail = &rcu_ctrlblk.rcucblist;
|
| 117 | local_irq_restore(flags);
|
| 118 |
|
| 119 |
|
| 120 | while (list) {
|
| 121 | next = list->next;
|
| 122 | prefetch(next);
|
| 123 | debug_rcu_head_unqueue(list);
|
| 124 | rcu_reclaim_tiny(list);
|
| 125 | list = next;
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | |
| 130 | * Wait for a grace period to elapse. But it is illegal to invoke
|
| 131 | * synchronize_rcu() from within an RCU read-side critical section.
|
| 132 | * Therefore, any legal call to synchronize_rcu() is a quiescent state,
|
| 133 | * and so on a UP system, synchronize_rcu() need do nothing, other than
|
| 134 | * let the polled APIs know that another grace period elapsed.
|
| 135 | *
|
| 136 | * (But Lai Jiangshan points out the benefits of doing might_sleep()
|
| 137 | * to reduce latency.)
|
| 138 | *
|
| 139 | * Cool, huh? (Due to Josh Triplett.)
|
| 140 |
|
| 141 | void synchronize_rcu(void)
|
| 142 | {
|
| 143 | RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
|
| 144 | lock_is_held(&rcu_lock_map) ||
|
| 145 | lock_is_held(&rcu_sched_lock_map),
|
| 146 | "Illegal synchronize_rcu() in RCU read-side critical section");
|
| 147 | preempt_disable();
|
| 148 | WRITE_ONCE(rcu_ctrlblk.gp_seq, rcu_ctrlblk.gp_seq + 2);
|
| 149 | preempt_enable();
|
| 150 | }
|
| 151 | EXPORT_SYMBOL_GPL(synchronize_rcu);
|
| 152 |
|
| 153 | |
| 154 | * Post an RCU callback to be invoked after the end of an RCU grace
|
| 155 | * period. But since we have but one CPU, that would be after any
|
| 156 | * quiescent state.
|
| 157 |
|
| 158 | void call_rcu(struct rcu_head *head, rcu_callback_t func)
|
| 159 | {
|
| 160 | static atomic_t doublefrees;
|
| 161 | unsigned long flags;
|
| 162 |
|
| 163 | if (debug_rcu_head_queue(head)) {
|
| 164 | if (atomic_inc_return(&doublefrees) < 4) {
|
| 165 | pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func);
|
| 166 | mem_dump_obj(head);
|
| 167 | }
|
| 168 | return;
|
| 169 | }
|
| 170 |
|
| 171 | head->func = func;
|
| 172 | head->next = NULL;
|
| 173 |
|
| 174 | local_irq_save(flags);
|
| 175 | *rcu_ctrlblk.curtail = head;
|
| 176 | rcu_ctrlblk.curtail = &head->next;
|
| 177 | local_irq_restore(flags);
|
| 178 |
|
| 179 | if (unlikely(is_idle_task(current))) {
|
| 180 |
|
| 181 | resched_cpu(0);
|
| 182 | }
|
| 183 | }
|
| 184 | EXPORT_SYMBOL_GPL(call_rcu);
|
| 185 |
|
| 186 | |
| 187 | * Store a grace-period-counter "cookie". For more information,
|
| 188 | * see the Tree RCU header comment.
|
| 189 |
|
| 190 | void get_completed_synchronize_rcu_full(struct rcu_gp_seq *gsp)
|
| 191 | {
|
| 192 | gsp->norm = RCU_GET_STATE_COMPLETED;
|
| 193 | }
|
| 194 | EXPORT_SYMBOL_GPL(get_completed_synchronize_rcu_full);
|
| 195 |
|
| 196 | |
| 197 | * Return a grace-period-counter "cookie". For more information,
|
| 198 | * see the Tree RCU header comment.
|
| 199 |
|
| 200 | unsigned long get_state_synchronize_rcu(void)
|
| 201 | {
|
| 202 | return READ_ONCE(rcu_ctrlblk.gp_seq);
|
| 203 | }
|
| 204 | EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
|
| 205 |
|
| 206 | |
| 207 | * Return a grace-period-counter "cookie" and ensure that a future grace
|
| 208 | * period completes. For more information, see the Tree RCU header comment.
|
| 209 |
|
| 210 | unsigned long start_poll_synchronize_rcu(void)
|
| 211 | {
|
| 212 | unsigned long gp_seq = get_state_synchronize_rcu();
|
| 213 |
|
| 214 | if (unlikely(is_idle_task(current))) {
|
| 215 |
|
| 216 | resched_cpu(0);
|
| 217 | }
|
| 218 | return gp_seq;
|
| 219 | }
|
| 220 | EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu);
|
| 221 |
|
| 222 | |
| 223 | * Return true if the grace period corresponding to oldstate has completed
|
| 224 | * and false otherwise. For more information, see the Tree RCU header
|
| 225 | * comment.
|
| 226 |
|
| 227 | bool poll_state_synchronize_rcu(unsigned long oldstate)
|
| 228 | {
|
| 229 | return oldstate == RCU_GET_STATE_COMPLETED || READ_ONCE(rcu_ctrlblk.gp_seq) != oldstate;
|
| 230 | }
|
| 231 | EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu);
|
| 232 |
|
| 233 | #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST)
|
| 234 | unsigned long long rcutorture_gather_gp_seqs(void)
|
| 235 | {
|
| 236 | return READ_ONCE(rcu_ctrlblk.gp_seq) & 0xffffULL;
|
| 237 | }
|
| 238 | EXPORT_SYMBOL_GPL(rcutorture_gather_gp_seqs);
|
| 239 |
|
| 240 | void rcutorture_format_gp_seqs(unsigned long long seqs, char *cp, size_t len)
|
| 241 | {
|
| 242 | snprintf(cp, len, "g%04llx", seqs & 0xffffULL);
|
| 243 | }
|
| 244 | EXPORT_SYMBOL_GPL(rcutorture_format_gp_seqs);
|
| 245 | #endif
|
| 246 |
|
| 247 | void __init rcu_init(void)
|
| 248 | {
|
| 249 | open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
|
| 250 | rcu_early_boot_tests();
|
| 251 | tasks_cblist_init_generic();
|
| 252 | }
|