2 * Read-Copy Update mechanism for mutual exclusion
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.
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.
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.
18 * Copyright IBM Corporation, 2001
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
23 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
24 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
26 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
27 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
29 * For detailed explanation of Read-Copy Update mechanism see -
30 * http://lse.sourceforge.net/locking/rcupdate.html
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>
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/rcu.h>
55 #ifdef CONFIG_PREEMPT_RCU
58 * Preemptible RCU implementation for rcu_read_lock().
59 * Just increment ->rcu_read_lock_nesting, shared state will be updated
62 void __rcu_read_lock(void)
64 current->rcu_read_lock_nesting++;
65 barrier(); /* critical section after entry code. */
67 EXPORT_SYMBOL_GPL(__rcu_read_lock);
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.
76 void __rcu_read_unlock(void)
78 struct task_struct *t = current;
80 if (t->rcu_read_lock_nesting != 1) {
81 --t->rcu_read_lock_nesting;
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;
94 #ifdef CONFIG_PROVE_LOCKING
96 int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
98 WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
100 #endif /* #ifdef CONFIG_PROVE_LOCKING */
102 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
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
112 struct task_struct *t = current;
114 if (likely(list_empty(¤t->rcu_node_entry)))
116 t->rcu_read_lock_nesting = 1;
118 t->rcu_read_unlock_special = RCU_READ_UNLOCK_BLOCKED;
122 #else /* #ifdef CONFIG_PREEMPT_RCU */
128 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
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);
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);
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);
147 #ifdef CONFIG_DEBUG_LOCK_ALLOC
149 int debug_lockdep_rcu_enabled(void)
151 return rcu_scheduler_active && debug_locks &&
152 current->lockdep_recursion == 0;
154 EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
157 * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
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
166 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
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.
171 int rcu_read_lock_bh_held(void)
173 if (!debug_lockdep_rcu_enabled())
175 if (rcu_is_cpu_idle())
177 if (!rcu_lockdep_current_cpu_online())
179 return in_softirq() || irqs_disabled();
181 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
183 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
185 struct rcu_synchronize {
186 struct rcu_head head;
187 struct completion completion;
191 * Awaken the corresponding synchronize_rcu() instance now that a
192 * grace period has elapsed.
194 static void wakeme_after_rcu(struct rcu_head *head)
196 struct rcu_synchronize *rcu;
198 rcu = container_of(head, struct rcu_synchronize, head);
199 complete(&rcu->completion);
202 void wait_rcu_gp(call_rcu_func_t crf)
204 struct rcu_synchronize rcu;
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);
211 wait_for_completion(&rcu.completion);
212 destroy_rcu_head_on_stack(&rcu.head);
214 EXPORT_SYMBOL_GPL(wait_rcu_gp);
216 #ifdef CONFIG_PROVE_RCU
218 * wrapper function to avoid #include problems.
220 int rcu_my_thread_group_empty(void)
222 return thread_group_empty(current);
224 EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
225 #endif /* #ifdef CONFIG_PROVE_RCU */
227 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
228 static inline void debug_init_rcu_head(struct rcu_head *head)
230 debug_object_init(head, &rcuhead_debug_descr);
233 static inline void debug_rcu_head_free(struct rcu_head *head)
235 debug_object_free(head, &rcuhead_debug_descr);
239 * fixup_init is called when:
240 * - an active object is initialized
242 static int rcuhead_fixup_init(void *addr, enum debug_obj_state state)
244 struct rcu_head *head = addr;
247 case ODEBUG_STATE_ACTIVE:
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.
256 #ifndef CONFIG_PREEMPT
260 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
268 debug_object_init(head, &rcuhead_debug_descr);
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().
281 static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
283 struct rcu_head *head = addr;
287 case ODEBUG_STATE_NOTAVAILABLE:
289 * This is not really a fixup. We just make sure that it is
290 * tracked in the object tracker.
292 debug_object_init(head, &rcuhead_debug_descr);
293 debug_object_activate(head, &rcuhead_debug_descr);
296 case ODEBUG_STATE_ACTIVE:
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.
305 #ifndef CONFIG_PREEMPT
309 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
317 debug_object_activate(head, &rcuhead_debug_descr);
325 * fixup_free is called when:
326 * - an active object is freed
328 static int rcuhead_fixup_free(void *addr, enum debug_obj_state state)
330 struct rcu_head *head = addr;
333 case ODEBUG_STATE_ACTIVE:
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.
342 #ifndef CONFIG_PREEMPT
346 if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
354 debug_object_free(head, &rcuhead_debug_descr);
362 * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
363 * @head: pointer to rcu_head structure to be initialized
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.
371 void init_rcu_head_on_stack(struct rcu_head *head)
373 debug_object_init_on_stack(head, &rcuhead_debug_descr);
375 EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
378 * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
379 * @head: pointer to rcu_head structure to be initialized
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.
388 void destroy_rcu_head_on_stack(struct rcu_head *head)
390 debug_object_free(head, &rcuhead_debug_descr);
392 EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
394 struct debug_obj_descr rcuhead_debug_descr = {
396 .fixup_init = rcuhead_fixup_init,
397 .fixup_activate = rcuhead_fixup_activate,
398 .fixup_free = rcuhead_fixup_free,
400 EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
401 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
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)
406 trace_rcu_torture_read(rcutorturename, rhp);
408 EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
410 #define do_trace_rcu_torture_read(rcutorturename, rhp) do { } while (0)