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 <paulmck@linux.ibm.com> 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/irq_work.h> 15 #include <linux/llist.h> 16 #include <linux/notifier.h> 17 #include <linux/rcupdate_wait.h> 18 #include <linux/kernel.h> 19 #include <linux/export.h> 20 #include <linux/mutex.h> 21 #include <linux/sched.h> 22 #include <linux/types.h> 23 #include <linux/init.h> 24 #include <linux/time.h> 25 #include <linux/cpu.h> 26 #include <linux/prefetch.h> 27 #include <linux/slab.h> 28 #include <linux/mm.h> 29 30 #include "rcu.h" 31 32 /* Global control variables for rcupdate callback mechanism. */ 33 struct rcu_ctrlblk { 34 struct rcu_head *rcucblist; /* List of pending callbacks (CBs). */ 35 struct rcu_head **donetail; /* ->next pointer of last "done" CB. */ 36 struct rcu_head **curtail; /* ->next pointer of last CB. */ 37 unsigned long gp_seq; /* Grace-period counter. */ 38 }; 39 40 /* Definition for rcupdate control block. */ 41 static struct rcu_ctrlblk rcu_ctrlblk = { 42 .donetail = &rcu_ctrlblk.rcucblist, 43 .curtail = &rcu_ctrlblk.rcucblist, 44 .gp_seq = 0 - 300UL, 45 }; 46 47 /* 48 * The callback list is only accessed with interrupts disabled, so a call_rcu() 49 * that arrives with interrupts off stages the callback on a lockless list that 50 * an irq_work re-issues later. One global list and irq_work suffice, as Tiny 51 * RCU is uniprocessor. 52 */ 53 static void rcu_defer_drain(struct irq_work *iw); 54 static LLIST_HEAD(rcu_defer_list); 55 static struct irq_work rcu_defer_iw = IRQ_WORK_INIT_HARD(rcu_defer_drain); 56 static bool rcu_defer_draining; 57 58 /* 59 * Also called by __rcu_defer_drain() to re-issue a deferred callback, so it 60 * must not re-check the deferral condition. 61 */ 62 static void rcu_do_enqueue(struct rcu_head *head, rcu_callback_t func) 63 { 64 static atomic_t doublefrees; 65 unsigned long flags; 66 67 if (debug_rcu_head_queue(head)) { 68 if (atomic_inc_return(&doublefrees) < 4) { 69 pr_err("%s(): Double-freed CB %p->%pS()!!! ", __func__, head, head->func); 70 mem_dump_obj(head); 71 } 72 return; 73 } 74 75 head->func = func; 76 head->next = NULL; 77 78 local_irq_save(flags); 79 *rcu_ctrlblk.curtail = head; 80 rcu_ctrlblk.curtail = &head->next; 81 local_irq_restore(flags); 82 } 83 84 /* Force scheduling for rcu_qs() when enqueuing from the idle task. */ 85 static void rcu_resched_if_idle(void) 86 { 87 if (unlikely(is_idle_task(current))) 88 resched_cpu(0); 89 } 90 91 static void __rcu_defer_drain(void) 92 { 93 struct llist_node *node, *next; 94 bool drained = false; 95 unsigned long flags; 96 97 if (!IS_ENABLED(CONFIG_RCU_DEFER)) 98 return; 99 100 /* Re-issued newest-first; nothing depends on call_rcu() ordering. */ 101 local_irq_save(flags); 102 llist_for_each_safe(node, next, llist_del_all(&rcu_defer_list)) { 103 struct rcu_head *head = (struct rcu_head *)node; 104 105 /* Bounds a node self-linked by a double call_rcu(). */ 106 head->next = NULL; 107 rcu_do_enqueue(head, head->func); 108 drained = true; 109 } 110 local_irq_restore(flags); 111 112 if (drained) 113 rcu_resched_if_idle(); 114 } 115 116 /* Only the irq_work drain can be re-fed by its own re-issue; see Tree RCU. */ 117 static void rcu_defer_drain(struct irq_work *iw) 118 { 119 WRITE_ONCE(rcu_defer_draining, true); 120 __rcu_defer_drain(); 121 WRITE_ONCE(rcu_defer_draining, false); 122 } 123 124 static void call_rcu_defer(struct rcu_head *head, rcu_callback_t func) 125 { 126 /* A re-entrant call_rcu() during the drain would livelock it; drop it. */ 127 if (READ_ONCE(rcu_defer_draining) && !in_nmi()) { 128 WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU), 129 "call_rcu() re-entered during callback drain; leaking callback\n"); 130 return; 131 } 132 head->func = func; 133 if (llist_add((struct llist_node *)head, &rcu_defer_list)) 134 irq_work_queue(&rcu_defer_iw); 135 } 136 137 void rcu_barrier(void) 138 { 139 /* Register any deferred callbacks so the wait below covers them. */ 140 __rcu_defer_drain(); 141 wait_rcu_gp(call_rcu_hurry); 142 } 143 EXPORT_SYMBOL(rcu_barrier); 144 145 /* Record an rcu quiescent state. */ 146 void rcu_qs(void) 147 { 148 unsigned long flags; 149 150 local_irq_save(flags); 151 if (rcu_ctrlblk.donetail != rcu_ctrlblk.curtail) { 152 rcu_ctrlblk.donetail = rcu_ctrlblk.curtail; 153 raise_softirq_irqoff(RCU_SOFTIRQ); 154 } 155 WRITE_ONCE(rcu_ctrlblk.gp_seq, rcu_ctrlblk.gp_seq + 2); 156 local_irq_restore(flags); 157 } 158 159 /* 160 * Check to see if the scheduling-clock interrupt came from an extended 161 * quiescent state, and, if so, tell RCU about it. This function must 162 * be called from hardirq context. It is normally called from the 163 * scheduling-clock interrupt. 164 */ 165 void rcu_sched_clock_irq(int user) 166 { 167 if (user) 168 rcu_qs(); 169 else if (rcu_ctrlblk.donetail != rcu_ctrlblk.curtail) 170 set_need_resched_current(); 171 } 172 173 /* 174 * Reclaim the specified callback, either by invoking it for non-kfree cases or 175 * freeing it directly (for kfree). Return true if kfreeing, false otherwise. 176 */ 177 static inline bool rcu_reclaim_tiny(struct rcu_head *head) 178 { 179 rcu_callback_t f; 180 181 rcu_lock_acquire(&rcu_callback_map); 182 183 trace_rcu_invoke_callback("", head); 184 f = head->func; 185 debug_rcu_head_callback(head); 186 WRITE_ONCE(head->func, (rcu_callback_t)0L); 187 f(head); 188 rcu_lock_release(&rcu_callback_map); 189 return false; 190 } 191 192 /* Invoke the RCU callbacks whose grace period has elapsed. */ 193 static __latent_entropy void rcu_process_callbacks(void) 194 { 195 struct rcu_head *next, *list; 196 unsigned long flags; 197 198 /* Move the ready-to-invoke callbacks to a local list. */ 199 local_irq_save(flags); 200 if (rcu_ctrlblk.donetail == &rcu_ctrlblk.rcucblist) { 201 /* No callbacks ready, so just leave. */ 202 local_irq_restore(flags); 203 return; 204 } 205 list = rcu_ctrlblk.rcucblist; 206 rcu_ctrlblk.rcucblist = *rcu_ctrlblk.donetail; 207 *rcu_ctrlblk.donetail = NULL; 208 if (rcu_ctrlblk.curtail == rcu_ctrlblk.donetail) 209 rcu_ctrlblk.curtail = &rcu_ctrlblk.rcucblist; 210 rcu_ctrlblk.donetail = &rcu_ctrlblk.rcucblist; 211 local_irq_restore(flags); 212 213 /* Invoke the callbacks on the local list. */ 214 while (list) { 215 next = list->next; 216 prefetch(next); 217 debug_rcu_head_unqueue(list); 218 rcu_reclaim_tiny(list); 219 list = next; 220 } 221 } 222 223 /* 224 * Wait for a grace period to elapse. But it is illegal to invoke 225 * synchronize_rcu() from within an RCU read-side critical section. 226 * Therefore, any legal call to synchronize_rcu() is a quiescent state, 227 * and so on a UP system, synchronize_rcu() need do nothing, other than 228 * let the polled APIs know that another grace period elapsed. 229 * 230 * (But Lai Jiangshan points out the benefits of doing might_sleep() 231 * to reduce latency.) 232 * 233 * Cool, huh? (Due to Josh Triplett.) 234 */ 235 void synchronize_rcu(void) 236 { 237 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) || 238 lock_is_held(&rcu_lock_map) || 239 lock_is_held(&rcu_sched_lock_map), 240 "Illegal synchronize_rcu() in RCU read-side critical section"); 241 preempt_disable(); 242 WRITE_ONCE(rcu_ctrlblk.gp_seq, rcu_ctrlblk.gp_seq + 2); 243 preempt_enable(); 244 } 245 EXPORT_SYMBOL_GPL(synchronize_rcu); 246 247 /* 248 * Post an RCU callback to be invoked after the end of an RCU grace 249 * period. But since we have but one CPU, that would be after any 250 * quiescent state. 251 */ 252 void call_rcu(struct rcu_head *head, rcu_callback_t func) 253 { 254 if (should_rcu_defer()) { 255 call_rcu_defer(head, func); 256 return; 257 } 258 259 /* 260 * Only reachable from an NMI when deferral is off: before the scheduler 261 * is up, or with CONFIG_RCU_DEFER=n. The enqueue can then race. 262 */ 263 WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi()); 264 265 rcu_do_enqueue(head, func); 266 rcu_resched_if_idle(); 267 } 268 EXPORT_SYMBOL_GPL(call_rcu); 269 270 /* 271 * Store a grace-period-counter "cookie". For more information, 272 * see the Tree RCU header comment. 273 */ 274 void get_completed_synchronize_rcu_full(struct rcu_gp_seq *gsp) 275 { 276 gsp->norm = RCU_GET_STATE_COMPLETED; 277 } 278 EXPORT_SYMBOL_GPL(get_completed_synchronize_rcu_full); 279 280 /* 281 * Return a grace-period-counter "cookie". For more information, 282 * see the Tree RCU header comment. 283 */ 284 unsigned long get_state_synchronize_rcu(void) 285 { 286 return READ_ONCE(rcu_ctrlblk.gp_seq); 287 } 288 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu); 289 290 /* 291 * Return a grace-period-counter "cookie" and ensure that a future grace 292 * period completes. For more information, see the Tree RCU header comment. 293 */ 294 unsigned long start_poll_synchronize_rcu(void) 295 { 296 unsigned long gp_seq = get_state_synchronize_rcu(); 297 298 rcu_resched_if_idle(); 299 return gp_seq; 300 } 301 EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu); 302 303 /* 304 * Return true if the grace period corresponding to oldstate has completed 305 * and false otherwise. For more information, see the Tree RCU header 306 * comment. 307 */ 308 bool poll_state_synchronize_rcu(unsigned long oldstate) 309 { 310 return oldstate == RCU_GET_STATE_COMPLETED || READ_ONCE(rcu_ctrlblk.gp_seq) != oldstate; 311 } 312 EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu); 313 314 #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) 315 unsigned long long rcutorture_gather_gp_seqs(void) 316 { 317 return READ_ONCE(rcu_ctrlblk.gp_seq) & 0xffffULL; 318 } 319 EXPORT_SYMBOL_GPL(rcutorture_gather_gp_seqs); 320 321 void rcutorture_format_gp_seqs(unsigned long long seqs, char *cp, size_t len) 322 { 323 snprintf(cp, len, "g%04llx", seqs & 0xffffULL); 324 } 325 EXPORT_SYMBOL_GPL(rcutorture_format_gp_seqs); 326 #endif 327 328 void __init rcu_init(void) 329 { 330 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks); 331 rcu_early_boot_tests(); 332 tasks_cblist_init_generic(); 333 } 334