1
2
3
4
5
6
7
8
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
23int rcu_scheduler_active __read_mostly;
24static LIST_HEAD(srcu_boot_list);
25static bool srcu_init_done;
26
27static int init_srcu_struct_fields(struct srcu_struct *ssp)
28{
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 ssp->srcu_idx_max = 0;
38 INIT_WORK(&ssp->srcu_work, srcu_drive_gp);
39 INIT_LIST_HEAD(&ssp->srcu_work.entry);
40 return 0;
41}
42
43#ifdef CONFIG_DEBUG_LOCK_ALLOC
44
45int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
46 struct lock_class_key *key)
47{
48
49 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
50 lockdep_init_map(&ssp->dep_map, name, key, 0);
51 return init_srcu_struct_fields(ssp);
52}
53EXPORT_SYMBOL_GPL(__init_srcu_struct);
54
55#else
56
57
58
59
60
61
62
63
64
65int init_srcu_struct(struct srcu_struct *ssp)
66{
67 return init_srcu_struct_fields(ssp);
68}
69EXPORT_SYMBOL_GPL(init_srcu_struct);
70
71#endif
72
73
74
75
76
77
78
79
80void cleanup_srcu_struct(struct srcu_struct *ssp)
81{
82 WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
83 flush_work(&ssp->srcu_work);
84 WARN_ON(ssp->srcu_gp_running);
85 WARN_ON(ssp->srcu_gp_waiting);
86 WARN_ON(ssp->srcu_cb_head);
87 WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail);
88 WARN_ON(ssp->srcu_idx != ssp->srcu_idx_max);
89 WARN_ON(ssp->srcu_idx & 0x1);
90}
91EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
92
93
94
95
96
97void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
98{
99 int newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) - 1;
100
101 WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
102 if (!newval && READ_ONCE(ssp->srcu_gp_waiting))
103 swake_up_one(&ssp->srcu_wq);
104}
105EXPORT_SYMBOL_GPL(__srcu_read_unlock);
106
107
108
109
110
111
112void srcu_drive_gp(struct work_struct *wp)
113{
114 int idx;
115 struct rcu_head *lh;
116 struct rcu_head *rhp;
117 struct srcu_struct *ssp;
118
119 ssp = container_of(wp, struct srcu_struct, srcu_work);
120 if (ssp->srcu_gp_running || USHORT_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max)))
121 return;
122
123
124 WRITE_ONCE(ssp->srcu_gp_running, true);
125 local_irq_disable();
126 lh = ssp->srcu_cb_head;
127 ssp->srcu_cb_head = NULL;
128 ssp->srcu_cb_tail = &ssp->srcu_cb_head;
129 local_irq_enable();
130 idx = (ssp->srcu_idx & 0x2) / 2;
131 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
132 WRITE_ONCE(ssp->srcu_gp_waiting, true);
133 swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
134 WRITE_ONCE(ssp->srcu_gp_waiting, false);
135 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
136
137
138 while (lh) {
139 rhp = lh;
140 lh = lh->next;
141 local_bh_disable();
142 rhp->func(rhp);
143 local_bh_enable();
144 }
145
146
147
148
149
150
151
152 WRITE_ONCE(ssp->srcu_gp_running, false);
153 if (USHORT_CMP_LT(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max)))
154 schedule_work(&ssp->srcu_work);
155}
156EXPORT_SYMBOL_GPL(srcu_drive_gp);
157
158static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
159{
160 unsigned short cookie;
161
162 cookie = get_state_synchronize_srcu(ssp);
163 if (USHORT_CMP_GE(READ_ONCE(ssp->srcu_idx_max), cookie))
164 return;
165 WRITE_ONCE(ssp->srcu_idx_max, cookie);
166 if (!READ_ONCE(ssp->srcu_gp_running)) {
167 if (likely(srcu_init_done))
168 schedule_work(&ssp->srcu_work);
169 else if (list_empty(&ssp->srcu_work.entry))
170 list_add(&ssp->srcu_work.entry, &srcu_boot_list);
171 }
172}
173
174
175
176
177
178void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
179 rcu_callback_t func)
180{
181 unsigned long flags;
182
183 rhp->func = func;
184 rhp->next = NULL;
185 local_irq_save(flags);
186 *ssp->srcu_cb_tail = rhp;
187 ssp->srcu_cb_tail = &rhp->next;
188 local_irq_restore(flags);
189 srcu_gp_start_if_needed(ssp);
190}
191EXPORT_SYMBOL_GPL(call_srcu);
192
193
194
195
196void synchronize_srcu(struct srcu_struct *ssp)
197{
198 struct rcu_synchronize rs;
199
200 init_rcu_head_on_stack(&rs.head);
201 init_completion(&rs.completion);
202 call_srcu(ssp, &rs.head, wakeme_after_rcu);
203 wait_for_completion(&rs.completion);
204 destroy_rcu_head_on_stack(&rs.head);
205}
206EXPORT_SYMBOL_GPL(synchronize_srcu);
207
208
209
210
211unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
212{
213 unsigned long ret;
214
215 barrier();
216 ret = (READ_ONCE(ssp->srcu_idx) + 3) & ~0x1;
217 barrier();
218 return ret & USHRT_MAX;
219}
220EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
221
222
223
224
225
226
227
228
229unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
230{
231 unsigned long ret = get_state_synchronize_srcu(ssp);
232
233 srcu_gp_start_if_needed(ssp);
234 return ret;
235}
236EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
237
238
239
240
241bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
242{
243 bool ret = USHORT_CMP_GE(READ_ONCE(ssp->srcu_idx), cookie);
244
245 barrier();
246 return ret;
247}
248EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
249
250
251void __init rcu_scheduler_starting(void)
252{
253 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
254}
255
256
257
258
259
260
261void __init srcu_init(void)
262{
263 struct srcu_struct *ssp;
264
265 srcu_init_done = true;
266 while (!list_empty(&srcu_boot_list)) {
267 ssp = list_first_entry(&srcu_boot_list,
268 struct srcu_struct, srcu_work.entry);
269 list_del_init(&ssp->srcu_work.entry);
270 schedule_work(&ssp->srcu_work);
271 }
272}
273