ip6mr: advertise new mfc entries via rtnl
[sfrench/cifs-2.6.git] / kernel / rcupdate.c
1 /*
2  * Read-Copy Update mechanism for mutual exclusion
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright IBM Corporation, 2001
19  *
20  * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21  *          Manfred Spraul <manfred@colorfullife.com>
22  *
23  * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
24  * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
25  * Papers:
26  * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
27  * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
28  *
29  * For detailed explanation of Read-Copy Update mechanism see -
30  *              http://lse.sourceforge.net/locking/rcupdate.html
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  * Preemptible RCU implementation for rcu_read_lock().
59  * Just increment ->rcu_read_lock_nesting, shared state will be updated
60  * if we block.
61  */
62 void __rcu_read_lock(void)
63 {
64         current->rcu_read_lock_nesting++;
65         barrier();  /* critical section after entry code. */
66 }
67 EXPORT_SYMBOL_GPL(__rcu_read_lock);
68
69 /*
70  * Preemptible RCU implementation for rcu_read_unlock().
71  * Decrement ->rcu_read_lock_nesting.  If the result is zero (outermost
72  * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
73  * invoke rcu_read_unlock_special() to clean up after a context switch
74  * in an RCU read-side critical section and other special cases.
75  */
76 void __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();  /* critical section before exit code. */
84                 t->rcu_read_lock_nesting = INT_MIN;
85 #ifdef CONFIG_PROVE_RCU_DELAY
86                 udelay(10); /* Make preemption more probable. */
87 #endif /* #ifdef CONFIG_PROVE_RCU_DELAY */
88                 barrier();  /* assign before ->rcu_read_unlock_special load */
89                 if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
90                         rcu_read_unlock_special(t);
91                 barrier();  /* ->rcu_read_unlock_special load before assign */
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 /* #ifdef CONFIG_PROVE_LOCKING */
101 }
102 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
103
104 /*
105  * Check for a task exiting while in a preemptible-RCU read-side
106  * critical section, clean up if so.  No need to issue warnings,
107  * as debug_check_no_locks_held() already does this if lockdep
108  * is enabled.
109  */
110 void exit_rcu(void)
111 {
112         struct task_struct *t = current;
113
114         if (likely(list_empty(&current->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 /* #ifdef CONFIG_PREEMPT_RCU */
123
124 void exit_rcu(void)
125 {
126 }
127
128 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
129
130 #ifdef CONFIG_DEBUG_LOCK_ALLOC
131 static struct lock_class_key rcu_lock_key;
132 struct lockdep_map rcu_lock_map =
133         STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
134 EXPORT_SYMBOL_GPL(rcu_lock_map);
135
136 static struct lock_class_key rcu_bh_lock_key;
137 struct lockdep_map rcu_bh_lock_map =
138         STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
139 EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
140
141 static struct lock_class_key rcu_sched_lock_key;
142 struct lockdep_map rcu_sched_lock_map =
143         STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
144 EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
145 #endif
146
147 #ifdef CONFIG_DEBUG_LOCK_ALLOC
148
149 int debug_lockdep_rcu_enabled(void)
150 {
151         return rcu_scheduler_active && debug_locks &&
152                current->lockdep_recursion == 0;
153 }
154 EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
155
156 /**
157  * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
158  *
159  * Check for bottom half being disabled, which covers both the
160  * CONFIG_PROVE_RCU and not cases.  Note that if someone uses
161  * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
162  * will show the situation.  This is useful for debug checks in functions
163  * that require that they be called within an RCU read-side critical
164  * section.
165  *
166  * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
167  *
168  * Note that rcu_read_lock() is disallowed if the CPU is either idle or
169  * offline from an RCU perspective, so check for those as well.
170  */
171 int 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 }
181 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
182
183 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
184
185 struct rcu_synchronize {
186         struct rcu_head head;
187         struct completion completion;
188 };
189
190 /*
191  * Awaken the corresponding synchronize_rcu() instance now that a
192  * grace period has elapsed.
193  */
194 static 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
202 void 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         /* Will wake me after RCU finished. */
209         crf(&rcu.head, wakeme_after_rcu);
210         /* Wait for it. */
211         wait_for_completion(&rcu.completion);
212         destroy_rcu_head_on_stack(&rcu.head);
213 }
214 EXPORT_SYMBOL_GPL(wait_rcu_gp);
215
216 #ifdef CONFIG_PROVE_RCU
217 /*
218  * wrapper function to avoid #include problems.
219  */
220 int rcu_my_thread_group_empty(void)
221 {
222         return thread_group_empty(current);
223 }
224 EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
225 #endif /* #ifdef CONFIG_PROVE_RCU */
226
227 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
228 static inline void debug_init_rcu_head(struct rcu_head *head)
229 {
230         debug_object_init(head, &rcuhead_debug_descr);
231 }
232
233 static inline void debug_rcu_head_free(struct rcu_head *head)
234 {
235         debug_object_free(head, &rcuhead_debug_descr);
236 }
237
238 /*
239  * fixup_init is called when:
240  * - an active object is initialized
241  */
242 static 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                  * Ensure that queued callbacks are all executed.
250                  * If we detect that we are nested in a RCU read-side critical
251                  * section, we should simply fail, otherwise we would deadlock.
252                  * In !PREEMPT configurations, there is no way to tell if we are
253                  * in a RCU read-side critical section or not, so we never
254                  * attempt any fixup and just print a warning.
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  * fixup_activate is called when:
277  * - an active object is activated
278  * - an unknown object is activated (might be a statically initialized object)
279  * Activation is performed internally by call_rcu().
280  */
281 static 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                  * This is not really a fixup. We just make sure that it is
290                  * tracked in the object tracker.
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                  * Ensure that queued callbacks are all executed.
299                  * If we detect that we are nested in a RCU read-side critical
300                  * section, we should simply fail, otherwise we would deadlock.
301                  * In !PREEMPT configurations, there is no way to tell if we are
302                  * in a RCU read-side critical section or not, so we never
303                  * attempt any fixup and just print a warning.
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  * fixup_free is called when:
326  * - an active object is freed
327  */
328 static 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                  * Ensure that queued callbacks are all executed.
336                  * If we detect that we are nested in a RCU read-side critical
337                  * section, we should simply fail, otherwise we would deadlock.
338                  * In !PREEMPT configurations, there is no way to tell if we are
339                  * in a RCU read-side critical section or not, so we never
340                  * attempt any fixup and just print a warning.
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  * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
363  * @head: pointer to rcu_head structure to be initialized
364  *
365  * This function informs debugobjects of a new rcu_head structure that
366  * has been allocated as an auto variable on the stack.  This function
367  * is not required for rcu_head structures that are statically defined or
368  * that are dynamically allocated on the heap.  This function has no
369  * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
370  */
371 void init_rcu_head_on_stack(struct rcu_head *head)
372 {
373         debug_object_init_on_stack(head, &rcuhead_debug_descr);
374 }
375 EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
376
377 /**
378  * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
379  * @head: pointer to rcu_head structure to be initialized
380  *
381  * This function informs debugobjects that an on-stack rcu_head structure
382  * is about to go out of scope.  As with init_rcu_head_on_stack(), this
383  * function is not required for rcu_head structures that are statically
384  * defined or that are dynamically allocated on the heap.  Also as with
385  * init_rcu_head_on_stack(), this function has no effect for
386  * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
387  */
388 void destroy_rcu_head_on_stack(struct rcu_head *head)
389 {
390         debug_object_free(head, &rcuhead_debug_descr);
391 }
392 EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
393
394 struct 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 };
400 EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
401 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
402
403 #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
404 void do_trace_rcu_torture_read(char *rcutorturename, struct rcu_head *rhp)
405 {
406         trace_rcu_torture_read(rcutorturename, rhp);
407 }
408 EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
409 #else
410 #define do_trace_rcu_torture_read(rcutorturename, rhp) do { } while (0)
411 #endif