blob: 29ca1c6da59496133947e5a1c3d1a860ef261e3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Paul E. McKenney01c1c662008-01-25 21:08:24 +010018 * Copyright IBM Corporation, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
Paul E. McKenneya71fca52009-09-18 10:28:19 -070022 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * 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 -
Paul E. McKenneya71fca52009-09-18 10:28:19 -070030 * http://lse.sourceforge.net/locking/rcupdate.html
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
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>
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/percpu.h>
43#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/cpu.h>
Ingo Molnar9331b312006-03-23 03:00:19 -080045#include <linux/mutex.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040046#include <linux/export.h>
Paul E. McKenneye3818b82010-03-15 17:03:43 -070047#include <linux/hardirq.h>
Paul E. McKenneye3ebfb92012-07-02 14:42:01 -070048#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Paul E. McKenney29c00b42011-06-17 15:53:19 -070050#define CREATE_TRACE_POINTS
51#include <trace/events/rcu.h>
52
53#include "rcu.h"
54
Paul E. McKenney9dd8fb12012-04-13 12:54:22 -070055#ifdef CONFIG_PREEMPT_RCU
56
57/*
Paul E. McKenney2a3fa842012-05-21 11:58:36 -070058 * Preemptible RCU implementation for rcu_read_lock().
59 * Just increment ->rcu_read_lock_nesting, shared state will be updated
60 * if we block.
61 */
62void __rcu_read_lock(void)
63{
64 current->rcu_read_lock_nesting++;
65 barrier(); /* critical section after entry code. */
66}
67EXPORT_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 */
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(); /* critical section before exit code. */
84 t->rcu_read_lock_nesting = INT_MIN;
Paul E. McKenneye3ebfb92012-07-02 14:42:01 -070085#ifdef CONFIG_PROVE_RCU_DELAY
86 udelay(10); /* Make preemption more probable. */
87#endif /* #ifdef CONFIG_PROVE_RCU_DELAY */
Paul E. McKenney2a3fa842012-05-21 11:58:36 -070088 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}
102EXPORT_SYMBOL_GPL(__rcu_read_unlock);
103
104/*
Paul E. McKenney9dd8fb12012-04-13 12:54:22 -0700105 * 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 */
110void 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
124void exit_rcu(void)
125{
126}
127
128#endif /* #else #ifdef CONFIG_PREEMPT_RCU */
129
Paul E. McKenney162cc272009-09-23 16:18:13 -0700130#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);
Paul E. McKenney632ee202010-02-22 17:04:45 -0800135
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);
Paul E. McKenney162cc272009-09-23 16:18:13 -0700145#endif
146
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700147#ifdef CONFIG_DEBUG_LOCK_ALLOC
148
Paul E. McKenneybc293d62010-04-15 12:50:39 -0700149int 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
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700156/**
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -0700157 * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700158 *
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)
Paul E. McKenneyca5ecdd2010-04-28 14:39:09 -0700162 * 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.
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700165 *
166 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800167 *
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.
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700170 */
171int rcu_read_lock_bh_held(void)
172{
173 if (!debug_lockdep_rcu_enabled())
174 return 1;
Frederic Weisbeckere6b80a32011-10-07 16:25:18 -0700175 if (rcu_is_cpu_idle())
176 return 0;
Paul E. McKenneyc0d6d012012-01-23 12:41:26 -0800177 if (!rcu_lockdep_current_cpu_online())
178 return 0;
Paul E. McKenney773e3f92010-10-05 14:03:02 -0700179 return in_softirq() || irqs_disabled();
Paul E. McKenneye3818b82010-03-15 17:03:43 -0700180}
181EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
182
183#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
184
Paul E. McKenney2c428182011-05-26 22:14:36 -0700185struct rcu_synchronize {
186 struct rcu_head head;
187 struct completion completion;
188};
189
Paul E. McKenneyd9f1bb62010-02-25 14:06:47 -0800190/*
Paul E. McKenneyfbf6bfc2008-02-13 15:03:15 -0800191 * Awaken the corresponding synchronize_rcu() instance now that a
192 * grace period has elapsed.
193 */
Paul E. McKenney2c428182011-05-26 22:14:36 -0700194static void wakeme_after_rcu(struct rcu_head *head)
Dipankar Sarma21a1ea92006-03-07 21:55:33 -0800195{
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100196 struct rcu_synchronize *rcu;
197
198 rcu = container_of(head, struct rcu_synchronize, head);
199 complete(&rcu->completion);
Dipankar Sarma21a1ea92006-03-07 21:55:33 -0800200}
Paul E. McKenneyee84b822010-05-06 09:28:41 -0700201
Paul E. McKenney2c428182011-05-26 22:14:36 -0700202void 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}
214EXPORT_SYMBOL_GPL(wait_rcu_gp);
215
Paul E. McKenneyee84b822010-05-06 09:28:41 -0700216#ifdef CONFIG_PROVE_RCU
217/*
218 * wrapper function to avoid #include problems.
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 /* #ifdef CONFIG_PROVE_RCU */
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400226
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 * fixup_init is called when:
240 * - an active object is initialized
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 * 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.
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800252 * 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.
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400255 */
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800256#ifndef CONFIG_PREEMPT
Paul E. McKenney108aae22011-02-23 09:56:00 -0800257 WARN_ON_ONCE(1);
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800258 return 0;
259#endif
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400260 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
261 irqs_disabled()) {
Paul E. McKenney108aae22011-02-23 09:56:00 -0800262 WARN_ON_ONCE(1);
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400263 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 */
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 * 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.
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800301 * 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.
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400304 */
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800305#ifndef CONFIG_PREEMPT
Paul E. McKenney108aae22011-02-23 09:56:00 -0800306 WARN_ON_ONCE(1);
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800307 return 0;
308#endif
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400309 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
310 irqs_disabled()) {
Paul E. McKenney108aae22011-02-23 09:56:00 -0800311 WARN_ON_ONCE(1);
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400312 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 */
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 * 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.
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800338 * 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.
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400341 */
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800342#ifndef CONFIG_PREEMPT
Paul E. McKenney108aae22011-02-23 09:56:00 -0800343 WARN_ON_ONCE(1);
Mathieu Desnoyersfc2ecf72011-02-23 09:42:14 -0800344 return 0;
345#endif
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400346 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
347 irqs_disabled()) {
Paul E. McKenney108aae22011-02-23 09:56:00 -0800348 WARN_ON_ONCE(1);
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400349 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;
Mathieu Desnoyers551d55a2010-04-17 08:48:42 -0400356 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 */
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 * 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 */
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 /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
Paul E. McKenney91afaf32011-10-02 07:44:32 -0700402
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