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