1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/types.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/spinlock.h>
37#include <linux/smp.h>
38#include <linux/interrupt.h>
39#include <linux/sched.h>
40#include <linux/atomic.h>
41#include <linux/bitops.h>
42#include <linux/percpu.h>
43#include <linux/notifier.h>
44#include <linux/cpu.h>
45#include <linux/mutex.h>
46#include <linux/export.h>
47#include <linux/hardirq.h>
48#include <linux/delay.h>
49
50#define CREATE_TRACE_POINTS
51#include <trace/events/rcu.h>
52
53#include "rcu.h"
54
55#ifdef CONFIG_PREEMPT_RCU
56
57
58
59
60
61
62void __rcu_read_lock(void)
63{
64 current->rcu_read_lock_nesting++;
65 barrier();
66}
67EXPORT_SYMBOL_GPL(__rcu_read_lock);
68
69
70
71
72
73
74
75
76void __rcu_read_unlock(void)
77{
78 struct task_struct *t = current;
79
80 if (t->rcu_read_lock_nesting != 1) {
81 --t->rcu_read_lock_nesting;
82 } else {
83 barrier();
84 t->rcu_read_lock_nesting = INT_MIN;
85#ifdef CONFIG_PROVE_RCU_DELAY
86 udelay(10);
87#endif
88 barrier();
89 if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
90 rcu_read_unlock_special(t);
91 barrier();
92 t->rcu_read_lock_nesting = 0;
93 }
94#ifdef CONFIG_PROVE_LOCKING
95 {
96 int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
97
98 WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
99 }
100#endif
101}
102EXPORT_SYMBOL_GPL(__rcu_read_unlock);
103
104
105
106
107
108
109
110void exit_rcu(void)
111{
112 struct task_struct *t = current;
113
114 if (likely(list_empty(¤t->rcu_node_entry)))
115 return;
116 t->rcu_read_lock_nesting = 1;
117 barrier();
118 t->rcu_read_unlock_special = RCU_READ_UNLOCK_BLOCKED;
119 __rcu_read_unlock();
120}
121
122#else
123
124void exit_rcu(void)
125{
126}
127
128#endif
129
130#ifdef CONFIG_DEBUG_LOCK_ALLOC
131static struct lock_class_key rcu_lock_key;
132struct lockdep_map rcu_lock_map =
133 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
134EXPORT_SYMBOL_GPL(rcu_lock_map);
135
136static struct lock_class_key rcu_bh_lock_key;
137struct lockdep_map rcu_bh_lock_map =
138 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
139EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
140
141static struct lock_class_key rcu_sched_lock_key;
142struct lockdep_map rcu_sched_lock_map =
143 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
144EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
145#endif
146
147#ifdef CONFIG_DEBUG_LOCK_ALLOC
148
149int debug_lockdep_rcu_enabled(void)
150{
151 return rcu_scheduler_active && debug_locks &&
152 current->lockdep_recursion == 0;
153}
154EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171int rcu_read_lock_bh_held(void)
172{
173 if (!debug_lockdep_rcu_enabled())
174 return 1;
175 if (rcu_is_cpu_idle())
176 return 0;
177 if (!rcu_lockdep_current_cpu_online())
178 return 0;
179 return in_softirq() || irqs_disabled();
180}
181EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
182
183#endif
184
185struct rcu_synchronize {
186 struct rcu_head head;
187 struct completion completion;
188};
189
190
191
192
193
194static void wakeme_after_rcu(struct rcu_head *head)
195{
196 struct rcu_synchronize *rcu;
197
198 rcu = container_of(head, struct rcu_synchronize, head);
199 complete(&rcu->completion);
200}
201
202void wait_rcu_gp(call_rcu_func_t crf)
203{
204 struct rcu_synchronize rcu;
205
206 init_rcu_head_on_stack(&rcu.head);
207 init_completion(&rcu.completion);
208
209 crf(&rcu.head, wakeme_after_rcu);
210
211 wait_for_completion(&rcu.completion);
212 destroy_rcu_head_on_stack(&rcu.head);
213}
214EXPORT_SYMBOL_GPL(wait_rcu_gp);
215
216#ifdef CONFIG_PROVE_RCU
217
218
219
220int rcu_my_thread_group_empty(void)
221{
222 return thread_group_empty(current);
223}
224EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
225#endif
226
227#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
228static inline void debug_init_rcu_head(struct rcu_head *head)
229{
230 debug_object_init(head, &rcuhead_debug_descr);
231}
232
233static inline void debug_rcu_head_free(struct rcu_head *head)
234{
235 debug_object_free(head, &rcuhead_debug_descr);
236}
237
238
239
240
241
242static int rcuhead_fixup_init(void *addr, enum debug_obj_state state)
243{
244 struct rcu_head *head = addr;
245
246 switch (state) {
247 case ODEBUG_STATE_ACTIVE:
248
249
250
251
252
253
254
255
256#ifndef CONFIG_PREEMPT
257 WARN_ON_ONCE(1);
258 return 0;
259#endif
260 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
261 irqs_disabled()) {
262 WARN_ON_ONCE(1);
263 return 0;
264 }
265 rcu_barrier();
266 rcu_barrier_sched();
267 rcu_barrier_bh();
268 debug_object_init(head, &rcuhead_debug_descr);
269 return 1;
270 default:
271 return 0;
272 }
273}
274
275
276
277
278
279
280
281static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
282{
283 struct rcu_head *head = addr;
284
285 switch (state) {
286
287 case ODEBUG_STATE_NOTAVAILABLE:
288
289
290
291
292 debug_object_init(head, &rcuhead_debug_descr);
293 debug_object_activate(head, &rcuhead_debug_descr);
294 return 0;
295
296 case ODEBUG_STATE_ACTIVE:
297
298
299
300
301
302
303
304
305#ifndef CONFIG_PREEMPT
306 WARN_ON_ONCE(1);
307 return 0;
308#endif
309 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
310 irqs_disabled()) {
311 WARN_ON_ONCE(1);
312 return 0;
313 }
314 rcu_barrier();
315 rcu_barrier_sched();
316 rcu_barrier_bh();
317 debug_object_activate(head, &rcuhead_debug_descr);
318 return 1;
319 default:
320 return 0;
321 }
322}
323
324
325
326
327
328static int rcuhead_fixup_free(void *addr, enum debug_obj_state state)
329{
330 struct rcu_head *head = addr;
331
332 switch (state) {
333 case ODEBUG_STATE_ACTIVE:
334
335
336
337
338
339
340
341
342#ifndef CONFIG_PREEMPT
343 WARN_ON_ONCE(1);
344 return 0;
345#endif
346 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
347 irqs_disabled()) {
348 WARN_ON_ONCE(1);
349 return 0;
350 }
351 rcu_barrier();
352 rcu_barrier_sched();
353 rcu_barrier_bh();
354 debug_object_free(head, &rcuhead_debug_descr);
355 return 1;
356 default:
357 return 0;
358 }
359}
360
361
362
363
364
365
366
367
368
369
370
371void init_rcu_head_on_stack(struct rcu_head *head)
372{
373 debug_object_init_on_stack(head, &rcuhead_debug_descr);
374}
375EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
376
377
378
379
380
381
382
383
384
385
386
387
388void destroy_rcu_head_on_stack(struct rcu_head *head)
389{
390 debug_object_free(head, &rcuhead_debug_descr);
391}
392EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
393
394struct debug_obj_descr rcuhead_debug_descr = {
395 .name = "rcu_head",
396 .fixup_init = rcuhead_fixup_init,
397 .fixup_activate = rcuhead_fixup_activate,
398 .fixup_free = rcuhead_fixup_free,
399};
400EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
401#endif
402
403#if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
404void do_trace_rcu_torture_read(char *rcutorturename, struct rcu_head *rhp)
405{
406 trace_rcu_torture_read(rcutorturename, rhp);
407}
408EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
409#else
410#define do_trace_rcu_torture_read(rcutorturename, rhp) do { } while (0)
411#endif
412