Paul E. McKenney | e7ee150 | 2019-01-17 10:18:16 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Sleepable Read-Copy Update mechanism for mutual exclusion, |
| 4 | * tiny version for non-preemptible single-CPU use. |
| 5 | * |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 6 | * Copyright (C) IBM Corporation, 2017 |
| 7 | * |
Paul E. McKenney | e7ee150 | 2019-01-17 10:18:16 -0800 | [diff] [blame] | 8 | * Author: Paul McKenney <paulmck@linux.ibm.com> |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/export.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/preempt.h> |
| 14 | #include <linux/rcupdate_wait.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/srcu.h> |
| 18 | |
| 19 | #include <linux/rcu_node_tree.h> |
Ingo Molnar | 45753c5 | 2017-05-02 10:31:18 +0200 | [diff] [blame] | 20 | #include "rcu_segcblist.h" |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 21 | #include "rcu.h" |
| 22 | |
Paul E. McKenney | 825c5bd | 2017-05-26 16:16:40 -0700 | [diff] [blame] | 23 | int rcu_scheduler_active __read_mostly; |
Paul E. McKenney | e0fcba9 | 2018-08-14 08:45:54 -0700 | [diff] [blame] | 24 | static LIST_HEAD(srcu_boot_list); |
| 25 | static bool srcu_init_done; |
Paul E. McKenney | 825c5bd | 2017-05-26 16:16:40 -0700 | [diff] [blame] | 26 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 27 | static int init_srcu_struct_fields(struct srcu_struct *ssp) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 28 | { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 29 | ssp->srcu_lock_nesting[0] = 0; |
| 30 | ssp->srcu_lock_nesting[1] = 0; |
| 31 | init_swait_queue_head(&ssp->srcu_wq); |
| 32 | ssp->srcu_cb_head = NULL; |
| 33 | ssp->srcu_cb_tail = &ssp->srcu_cb_head; |
| 34 | ssp->srcu_gp_running = false; |
| 35 | ssp->srcu_gp_waiting = false; |
| 36 | ssp->srcu_idx = 0; |
| 37 | INIT_WORK(&ssp->srcu_work, srcu_drive_gp); |
| 38 | INIT_LIST_HEAD(&ssp->srcu_work.entry); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 43 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 44 | int __init_srcu_struct(struct srcu_struct *ssp, const char *name, |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 45 | struct lock_class_key *key) |
| 46 | { |
| 47 | /* Don't re-initialize a lock while it is held. */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 48 | debug_check_no_locks_freed((void *)ssp, sizeof(*ssp)); |
| 49 | lockdep_init_map(&ssp->dep_map, name, key, 0); |
| 50 | return init_srcu_struct_fields(ssp); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 51 | } |
| 52 | EXPORT_SYMBOL_GPL(__init_srcu_struct); |
| 53 | |
| 54 | #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 55 | |
| 56 | /* |
| 57 | * init_srcu_struct - initialize a sleep-RCU structure |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 58 | * @ssp: structure to initialize. |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 59 | * |
| 60 | * Must invoke this on a given srcu_struct before passing that srcu_struct |
| 61 | * to any other function. Each srcu_struct represents a separate domain |
| 62 | * of SRCU protection. |
| 63 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 64 | int init_srcu_struct(struct srcu_struct *ssp) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 65 | { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 66 | return init_srcu_struct_fields(ssp); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 67 | } |
| 68 | EXPORT_SYMBOL_GPL(init_srcu_struct); |
| 69 | |
| 70 | #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 71 | |
| 72 | /* |
| 73 | * cleanup_srcu_struct - deconstruct a sleep-RCU structure |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 74 | * @ssp: structure to clean up. |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 75 | * |
| 76 | * Must invoke this after you are finished using a given srcu_struct that |
| 77 | * was initialized via init_srcu_struct(), else you leak memory. |
| 78 | */ |
Paul E. McKenney | f5ad399 | 2019-02-13 13:54:37 -0800 | [diff] [blame] | 79 | void cleanup_srcu_struct(struct srcu_struct *ssp) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 80 | { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 81 | WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]); |
Paul E. McKenney | f5ad399 | 2019-02-13 13:54:37 -0800 | [diff] [blame] | 82 | flush_work(&ssp->srcu_work); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 83 | WARN_ON(ssp->srcu_gp_running); |
| 84 | WARN_ON(ssp->srcu_gp_waiting); |
| 85 | WARN_ON(ssp->srcu_cb_head); |
| 86 | WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 87 | } |
Paul E. McKenney | f5ad399 | 2019-02-13 13:54:37 -0800 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(cleanup_srcu_struct); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 91 | * Removes the count for the old reader from the appropriate element of |
Paolo Bonzini | cdf7abc | 2017-05-31 14:03:10 +0200 | [diff] [blame] | 92 | * the srcu_struct. |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 93 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 94 | void __srcu_read_unlock(struct srcu_struct *ssp, int idx) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 95 | { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 96 | int newval = ssp->srcu_lock_nesting[idx] - 1; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 97 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 98 | WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval); |
| 99 | if (!newval && READ_ONCE(ssp->srcu_gp_waiting)) |
| 100 | swake_up_one(&ssp->srcu_wq); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL_GPL(__srcu_read_unlock); |
| 103 | |
| 104 | /* |
| 105 | * Workqueue handler to drive one grace period and invoke any callbacks |
Sebastian Andrzej Siewior | 90326f0 | 2019-10-15 21:18:14 +0200 | [diff] [blame] | 106 | * that become ready as a result. Single-CPU and !PREEMPTION operation |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 107 | * means that we get away with murder on synchronization. ;-) |
| 108 | */ |
| 109 | void srcu_drive_gp(struct work_struct *wp) |
| 110 | { |
| 111 | int idx; |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 112 | struct rcu_head *lh; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 113 | struct rcu_head *rhp; |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 114 | struct srcu_struct *ssp; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 115 | |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 116 | ssp = container_of(wp, struct srcu_struct, srcu_work); |
| 117 | if (ssp->srcu_gp_running || !READ_ONCE(ssp->srcu_cb_head)) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 118 | return; /* Already running or nothing to do. */ |
| 119 | |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 120 | /* Remove recently arrived callbacks and wait for readers. */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 121 | WRITE_ONCE(ssp->srcu_gp_running, true); |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 122 | local_irq_disable(); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 123 | lh = ssp->srcu_cb_head; |
| 124 | ssp->srcu_cb_head = NULL; |
| 125 | ssp->srcu_cb_tail = &ssp->srcu_cb_head; |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 126 | local_irq_enable(); |
Paul E. McKenney | 74612a0 | 2020-11-12 16:34:09 -0800 | [diff] [blame] | 127 | idx = (ssp->srcu_idx & 0x2) / 2; |
| 128 | WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 129 | WRITE_ONCE(ssp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */ |
| 130 | swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx])); |
| 131 | WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */ |
Paul E. McKenney | 74612a0 | 2020-11-12 16:34:09 -0800 | [diff] [blame] | 132 | WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 133 | |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 134 | /* Invoke the callbacks we removed above. */ |
| 135 | while (lh) { |
| 136 | rhp = lh; |
| 137 | lh = lh->next; |
| 138 | local_bh_disable(); |
| 139 | rhp->func(rhp); |
| 140 | local_bh_enable(); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 141 | } |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 142 | |
| 143 | /* |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 144 | * Enable rescheduling, and if there are more callbacks, |
| 145 | * reschedule ourselves. This can race with a call_srcu() |
| 146 | * at interrupt level, but the ->srcu_gp_running checks will |
| 147 | * straighten that out. |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 148 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 149 | WRITE_ONCE(ssp->srcu_gp_running, false); |
| 150 | if (READ_ONCE(ssp->srcu_cb_head)) |
| 151 | schedule_work(&ssp->srcu_work); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL_GPL(srcu_drive_gp); |
| 154 | |
Paul E. McKenney | 1a893c7 | 2020-11-13 09:37:39 -0800 | [diff] [blame^] | 155 | static void srcu_gp_start_if_needed(struct srcu_struct *ssp) |
| 156 | { |
| 157 | if (!READ_ONCE(ssp->srcu_gp_running)) { |
| 158 | if (likely(srcu_init_done)) |
| 159 | schedule_work(&ssp->srcu_work); |
| 160 | else if (list_empty(&ssp->srcu_work.entry)) |
| 161 | list_add(&ssp->srcu_work.entry, &srcu_boot_list); |
| 162 | } |
| 163 | } |
| 164 | |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Enqueue an SRCU callback on the specified srcu_struct structure, |
| 167 | * initiating grace-period processing if it is not already running. |
| 168 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 169 | void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp, |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 170 | rcu_callback_t func) |
| 171 | { |
| 172 | unsigned long flags; |
| 173 | |
Paul E. McKenney | 2464dd9 | 2017-05-04 14:29:16 -0700 | [diff] [blame] | 174 | rhp->func = func; |
| 175 | rhp->next = NULL; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 176 | local_irq_save(flags); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 177 | *ssp->srcu_cb_tail = rhp; |
| 178 | ssp->srcu_cb_tail = &rhp->next; |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 179 | local_irq_restore(flags); |
Paul E. McKenney | 1a893c7 | 2020-11-13 09:37:39 -0800 | [diff] [blame^] | 180 | srcu_gp_start_if_needed(ssp); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL_GPL(call_srcu); |
| 183 | |
| 184 | /* |
| 185 | * synchronize_srcu - wait for prior SRCU read-side critical-section completion |
| 186 | */ |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 187 | void synchronize_srcu(struct srcu_struct *ssp) |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 188 | { |
| 189 | struct rcu_synchronize rs; |
| 190 | |
| 191 | init_rcu_head_on_stack(&rs.head); |
| 192 | init_completion(&rs.completion); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 193 | call_srcu(ssp, &rs.head, wakeme_after_rcu); |
Paul E. McKenney | d8be817 | 2017-03-25 09:59:38 -0700 | [diff] [blame] | 194 | wait_for_completion(&rs.completion); |
| 195 | destroy_rcu_head_on_stack(&rs.head); |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(synchronize_srcu); |
Paul E. McKenney | 825c5bd | 2017-05-26 16:16:40 -0700 | [diff] [blame] | 198 | |
| 199 | /* Lockdep diagnostics. */ |
| 200 | void __init rcu_scheduler_starting(void) |
| 201 | { |
| 202 | rcu_scheduler_active = RCU_SCHEDULER_RUNNING; |
| 203 | } |
Paul E. McKenney | e0fcba9 | 2018-08-14 08:45:54 -0700 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Queue work for srcu_struct structures with early boot callbacks. |
| 207 | * The work won't actually execute until the workqueue initialization |
| 208 | * phase that takes place after the scheduler starts. |
| 209 | */ |
| 210 | void __init srcu_init(void) |
| 211 | { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 212 | struct srcu_struct *ssp; |
Paul E. McKenney | e0fcba9 | 2018-08-14 08:45:54 -0700 | [diff] [blame] | 213 | |
| 214 | srcu_init_done = true; |
| 215 | while (!list_empty(&srcu_boot_list)) { |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 216 | ssp = list_first_entry(&srcu_boot_list, |
Paul E. McKenney | 4e6ea4e | 2018-08-14 14:41:49 -0700 | [diff] [blame] | 217 | struct srcu_struct, srcu_work.entry); |
Paul E. McKenney | aacb5d9 | 2018-10-28 10:32:51 -0700 | [diff] [blame] | 218 | list_del_init(&ssp->srcu_work.entry); |
| 219 | schedule_work(&ssp->srcu_work); |
Paul E. McKenney | e0fcba9 | 2018-08-14 08:45:54 -0700 | [diff] [blame] | 220 | } |
| 221 | } |