1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion,
4 * tiny version for non-preemptible single-CPU use.
5 *
6 * Copyright (C) IBM Corporation, 2017
7 *
8 * Author: Paul McKenney <paulmck@linux.ibm.com>
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>
20 #include "rcu_segcblist.h"
21 #include "rcu.h"
22
23 #ifndef CONFIG_TREE_RCU
24 int rcu_scheduler_active __read_mostly;
25 #else // #ifndef CONFIG_TREE_RCU
26 extern int rcu_scheduler_active;
27 #endif // #else // #ifndef CONFIG_TREE_RCU
28 static LIST_HEAD(srcu_boot_list);
29 static bool srcu_init_done;
30
init_srcu_struct_fields(struct srcu_struct * ssp)31 static int init_srcu_struct_fields(struct srcu_struct *ssp)
32 {
33 ssp->srcu_lock_nesting[0] = 0;
34 ssp->srcu_lock_nesting[1] = 0;
35 init_swait_queue_head(&ssp->srcu_wq);
36 ssp->srcu_cb_head = NULL;
37 ssp->srcu_cb_tail = &ssp->srcu_cb_head;
38 ssp->srcu_gp_running = false;
39 ssp->srcu_gp_waiting = false;
40 ssp->srcu_idx = 0;
41 ssp->srcu_idx_max = 0;
42 INIT_WORK(&ssp->srcu_work, srcu_drive_gp);
43 INIT_LIST_HEAD(&ssp->srcu_work.entry);
44 return 0;
45 }
46
47 #ifdef CONFIG_DEBUG_LOCK_ALLOC
48
__init_srcu_struct(struct srcu_struct * ssp,const char * name,struct lock_class_key * key)49 int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
50 struct lock_class_key *key)
51 {
52 /* Don't re-initialize a lock while it is held. */
53 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
54 lockdep_init_map(&ssp->dep_map, name, key, 0);
55 return init_srcu_struct_fields(ssp);
56 }
57 EXPORT_SYMBOL_GPL(__init_srcu_struct);
58
59 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
60
61 /*
62 * init_srcu_struct - initialize a sleep-RCU structure
63 * @ssp: structure to initialize.
64 *
65 * Must invoke this on a given srcu_struct before passing that srcu_struct
66 * to any other function. Each srcu_struct represents a separate domain
67 * of SRCU protection.
68 */
init_srcu_struct(struct srcu_struct * ssp)69 int init_srcu_struct(struct srcu_struct *ssp)
70 {
71 return init_srcu_struct_fields(ssp);
72 }
73 EXPORT_SYMBOL_GPL(init_srcu_struct);
74
75 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
76
77 /*
78 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
79 * @ssp: structure to clean up.
80 *
81 * Must invoke this after you are finished using a given srcu_struct that
82 * was initialized via init_srcu_struct(), else you leak memory.
83 */
cleanup_srcu_struct(struct srcu_struct * ssp)84 void cleanup_srcu_struct(struct srcu_struct *ssp)
85 {
86 WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
87 flush_work(&ssp->srcu_work);
88 WARN_ON(ssp->srcu_gp_running);
89 WARN_ON(ssp->srcu_gp_waiting);
90 WARN_ON(ssp->srcu_cb_head);
91 WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail);
92 WARN_ON(ssp->srcu_idx != ssp->srcu_idx_max);
93 WARN_ON(ssp->srcu_idx & 0x1);
94 }
95 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
96
97 /*
98 * Removes the count for the old reader from the appropriate element of
99 * the srcu_struct.
100 */
__srcu_read_unlock(struct srcu_struct * ssp,int idx)101 void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
102 {
103 int newval;
104
105 preempt_disable(); // Needed for PREEMPT_LAZY
106 newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) - 1;
107 WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
108 preempt_enable();
109 if (!newval && READ_ONCE(ssp->srcu_gp_waiting) && in_task())
110 swake_up_one(&ssp->srcu_wq);
111 }
112 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
113
114 /*
115 * Workqueue handler to drive one grace period and invoke any callbacks
116 * that become ready as a result. Single-CPU and !PREEMPTION operation
117 * means that we get away with murder on synchronization. ;-)
118 */
srcu_drive_gp(struct work_struct * wp)119 void srcu_drive_gp(struct work_struct *wp)
120 {
121 int idx;
122 struct rcu_head *lh;
123 struct rcu_head *rhp;
124 struct srcu_struct *ssp;
125
126 ssp = container_of(wp, struct srcu_struct, srcu_work);
127 preempt_disable(); // Needed for PREEMPT_LAZY
128 if (ssp->srcu_gp_running || ULONG_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max))) {
129 preempt_enable();
130 return; /* Already running or nothing to do. */
131 }
132
133 /* Remove recently arrived callbacks and wait for readers. */
134 WRITE_ONCE(ssp->srcu_gp_running, true);
135 local_irq_disable();
136 lh = ssp->srcu_cb_head;
137 ssp->srcu_cb_head = NULL;
138 ssp->srcu_cb_tail = &ssp->srcu_cb_head;
139 local_irq_enable();
140 idx = (ssp->srcu_idx & 0x2) / 2;
141 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
142 WRITE_ONCE(ssp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
143 preempt_enable();
144 swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
145 preempt_disable(); // Needed for PREEMPT_LAZY
146 WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
147 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
148 preempt_enable();
149
150 /* Invoke the callbacks we removed above. */
151 while (lh) {
152 rhp = lh;
153 lh = lh->next;
154 debug_rcu_head_callback(rhp);
155 local_bh_disable();
156 rhp->func(rhp);
157 local_bh_enable();
158 }
159
160 /*
161 * Enable rescheduling, and if there are more callbacks,
162 * reschedule ourselves. This can race with a call_srcu()
163 * at interrupt level, but the ->srcu_gp_running checks will
164 * straighten that out.
165 */
166 preempt_disable(); // Needed for PREEMPT_LAZY
167 WRITE_ONCE(ssp->srcu_gp_running, false);
168 idx = ULONG_CMP_LT(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max));
169 preempt_enable();
170 if (idx)
171 schedule_work(&ssp->srcu_work);
172 }
173 EXPORT_SYMBOL_GPL(srcu_drive_gp);
174
srcu_gp_start_if_needed(struct srcu_struct * ssp)175 static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
176 {
177 unsigned long cookie;
178
179 preempt_disable(); // Needed for PREEMPT_LAZY
180 cookie = get_state_synchronize_srcu(ssp);
181 if (ULONG_CMP_GE(READ_ONCE(ssp->srcu_idx_max), cookie)) {
182 preempt_enable();
183 return;
184 }
185 WRITE_ONCE(ssp->srcu_idx_max, cookie);
186 if (!READ_ONCE(ssp->srcu_gp_running)) {
187 if (likely(srcu_init_done))
188 schedule_work(&ssp->srcu_work);
189 else if (list_empty(&ssp->srcu_work.entry))
190 list_add(&ssp->srcu_work.entry, &srcu_boot_list);
191 }
192 preempt_enable();
193 }
194
195 /*
196 * Enqueue an SRCU callback on the specified srcu_struct structure,
197 * initiating grace-period processing if it is not already running.
198 */
call_srcu(struct srcu_struct * ssp,struct rcu_head * rhp,rcu_callback_t func)199 void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
200 rcu_callback_t func)
201 {
202 unsigned long flags;
203
204 rhp->func = func;
205 rhp->next = NULL;
206 preempt_disable(); // Needed for PREEMPT_LAZY
207 local_irq_save(flags);
208 *ssp->srcu_cb_tail = rhp;
209 ssp->srcu_cb_tail = &rhp->next;
210 local_irq_restore(flags);
211 srcu_gp_start_if_needed(ssp);
212 preempt_enable();
213 }
214 EXPORT_SYMBOL_GPL(call_srcu);
215
216 /*
217 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
218 */
synchronize_srcu(struct srcu_struct * ssp)219 void synchronize_srcu(struct srcu_struct *ssp)
220 {
221 struct rcu_synchronize rs;
222
223 srcu_lock_sync(&ssp->dep_map);
224
225 RCU_LOCKDEP_WARN(lockdep_is_held(ssp) ||
226 lock_is_held(&rcu_bh_lock_map) ||
227 lock_is_held(&rcu_lock_map) ||
228 lock_is_held(&rcu_sched_lock_map),
229 "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
230
231 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
232 return;
233
234 might_sleep();
235 init_rcu_head_on_stack(&rs.head);
236 init_completion(&rs.completion);
237 call_srcu(ssp, &rs.head, wakeme_after_rcu);
238 wait_for_completion(&rs.completion);
239 destroy_rcu_head_on_stack(&rs.head);
240 }
241 EXPORT_SYMBOL_GPL(synchronize_srcu);
242
243 /*
244 * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
245 */
get_state_synchronize_srcu(struct srcu_struct * ssp)246 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
247 {
248 unsigned long ret;
249
250 barrier();
251 ret = (READ_ONCE(ssp->srcu_idx) + 3) & ~0x1;
252 barrier();
253 return ret;
254 }
255 EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
256
257 /*
258 * start_poll_synchronize_srcu - Provide cookie and start grace period
259 *
260 * The difference between this and get_state_synchronize_srcu() is that
261 * this function ensures that the poll_state_synchronize_srcu() will
262 * eventually return the value true.
263 */
start_poll_synchronize_srcu(struct srcu_struct * ssp)264 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
265 {
266 unsigned long ret;
267
268 preempt_disable(); // Needed for PREEMPT_LAZY
269 ret = get_state_synchronize_srcu(ssp);
270 srcu_gp_start_if_needed(ssp);
271 preempt_enable();
272 return ret;
273 }
274 EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
275
276 /*
277 * poll_state_synchronize_srcu - Has cookie's grace period ended?
278 */
poll_state_synchronize_srcu(struct srcu_struct * ssp,unsigned long cookie)279 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
280 {
281 unsigned long cur_s = READ_ONCE(ssp->srcu_idx);
282
283 barrier();
284 return cookie == SRCU_GET_STATE_COMPLETED ||
285 ULONG_CMP_GE(cur_s, cookie) || ULONG_CMP_LT(cur_s, cookie - 3);
286 }
287 EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
288
289 #ifndef CONFIG_TREE_RCU
290 /* Lockdep diagnostics. */
rcu_scheduler_starting(void)291 void __init rcu_scheduler_starting(void)
292 {
293 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
294 }
295 #endif // #ifndef CONFIG_TREE_RCU
296
297 /*
298 * Queue work for srcu_struct structures with early boot callbacks.
299 * The work won't actually execute until the workqueue initialization
300 * phase that takes place after the scheduler starts.
301 */
srcu_init(void)302 void __init srcu_init(void)
303 {
304 struct srcu_struct *ssp;
305
306 srcu_init_done = true;
307 while (!list_empty(&srcu_boot_list)) {
308 ssp = list_first_entry(&srcu_boot_list,
309 struct srcu_struct, srcu_work.entry);
310 list_del_init(&ssp->srcu_work.entry);
311 schedule_work(&ssp->srcu_work);
312 }
313 }
314