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 
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/rcu.h>
51 
52 #include "rcu.h"
53 
54 #ifdef CONFIG_DEBUG_LOCK_ALLOC
55 static struct lock_class_key rcu_lock_key;
56 struct lockdep_map rcu_lock_map =
57 	STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
58 EXPORT_SYMBOL_GPL(rcu_lock_map);
59 
60 static struct lock_class_key rcu_bh_lock_key;
61 struct lockdep_map rcu_bh_lock_map =
62 	STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
63 EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
64 
65 static struct lock_class_key rcu_sched_lock_key;
66 struct lockdep_map rcu_sched_lock_map =
67 	STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
68 EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
69 #endif
70 
71 #ifdef CONFIG_DEBUG_LOCK_ALLOC
72 
debug_lockdep_rcu_enabled(void)73 int debug_lockdep_rcu_enabled(void)
74 {
75 	return rcu_scheduler_active && debug_locks &&
76 	       current->lockdep_recursion == 0;
77 }
78 EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
79 
80 /**
81  * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
82  *
83  * Check for bottom half being disabled, which covers both the
84  * CONFIG_PROVE_RCU and not cases.  Note that if someone uses
85  * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
86  * will show the situation.  This is useful for debug checks in functions
87  * that require that they be called within an RCU read-side critical
88  * section.
89  *
90  * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
91  *
92  * Note that rcu_read_lock() is disallowed if the CPU is either idle or
93  * offline from an RCU perspective, so check for those as well.
94  */
rcu_read_lock_bh_held(void)95 int rcu_read_lock_bh_held(void)
96 {
97 	if (!debug_lockdep_rcu_enabled())
98 		return 1;
99 	if (rcu_is_cpu_idle())
100 		return 0;
101 	if (!rcu_lockdep_current_cpu_online())
102 		return 0;
103 	return in_softirq() || irqs_disabled();
104 }
105 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
106 
107 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
108 
109 struct rcu_synchronize {
110 	struct rcu_head head;
111 	struct completion completion;
112 };
113 
114 /*
115  * Awaken the corresponding synchronize_rcu() instance now that a
116  * grace period has elapsed.
117  */
wakeme_after_rcu(struct rcu_head * head)118 static void wakeme_after_rcu(struct rcu_head  *head)
119 {
120 	struct rcu_synchronize *rcu;
121 
122 	rcu = container_of(head, struct rcu_synchronize, head);
123 	complete(&rcu->completion);
124 }
125 
wait_rcu_gp(call_rcu_func_t crf)126 void wait_rcu_gp(call_rcu_func_t crf)
127 {
128 	struct rcu_synchronize rcu;
129 
130 	init_rcu_head_on_stack(&rcu.head);
131 	init_completion(&rcu.completion);
132 	/* Will wake me after RCU finished. */
133 	crf(&rcu.head, wakeme_after_rcu);
134 	/* Wait for it. */
135 	wait_for_completion(&rcu.completion);
136 	destroy_rcu_head_on_stack(&rcu.head);
137 }
138 EXPORT_SYMBOL_GPL(wait_rcu_gp);
139 
140 #ifdef CONFIG_PROVE_RCU
141 /*
142  * wrapper function to avoid #include problems.
143  */
rcu_my_thread_group_empty(void)144 int rcu_my_thread_group_empty(void)
145 {
146 	return thread_group_empty(current);
147 }
148 EXPORT_SYMBOL_GPL(rcu_my_thread_group_empty);
149 #endif /* #ifdef CONFIG_PROVE_RCU */
150 
151 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
debug_init_rcu_head(struct rcu_head * head)152 static inline void debug_init_rcu_head(struct rcu_head *head)
153 {
154 	debug_object_init(head, &rcuhead_debug_descr);
155 }
156 
debug_rcu_head_free(struct rcu_head * head)157 static inline void debug_rcu_head_free(struct rcu_head *head)
158 {
159 	debug_object_free(head, &rcuhead_debug_descr);
160 }
161 
162 /*
163  * fixup_init is called when:
164  * - an active object is initialized
165  */
rcuhead_fixup_init(void * addr,enum debug_obj_state state)166 static int rcuhead_fixup_init(void *addr, enum debug_obj_state state)
167 {
168 	struct rcu_head *head = addr;
169 
170 	switch (state) {
171 	case ODEBUG_STATE_ACTIVE:
172 		/*
173 		 * Ensure that queued callbacks are all executed.
174 		 * If we detect that we are nested in a RCU read-side critical
175 		 * section, we should simply fail, otherwise we would deadlock.
176 		 * In !PREEMPT configurations, there is no way to tell if we are
177 		 * in a RCU read-side critical section or not, so we never
178 		 * attempt any fixup and just print a warning.
179 		 */
180 #ifndef CONFIG_PREEMPT
181 		WARN_ON_ONCE(1);
182 		return 0;
183 #endif
184 		if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
185 		    irqs_disabled()) {
186 			WARN_ON_ONCE(1);
187 			return 0;
188 		}
189 		rcu_barrier();
190 		rcu_barrier_sched();
191 		rcu_barrier_bh();
192 		debug_object_init(head, &rcuhead_debug_descr);
193 		return 1;
194 	default:
195 		return 0;
196 	}
197 }
198 
199 /*
200  * fixup_activate is called when:
201  * - an active object is activated
202  * - an unknown object is activated (might be a statically initialized object)
203  * Activation is performed internally by call_rcu().
204  */
rcuhead_fixup_activate(void * addr,enum debug_obj_state state)205 static int rcuhead_fixup_activate(void *addr, enum debug_obj_state state)
206 {
207 	struct rcu_head *head = addr;
208 
209 	switch (state) {
210 
211 	case ODEBUG_STATE_NOTAVAILABLE:
212 		/*
213 		 * This is not really a fixup. We just make sure that it is
214 		 * tracked in the object tracker.
215 		 */
216 		debug_object_init(head, &rcuhead_debug_descr);
217 		debug_object_activate(head, &rcuhead_debug_descr);
218 		return 0;
219 
220 	case ODEBUG_STATE_ACTIVE:
221 		/*
222 		 * Ensure that queued callbacks are all executed.
223 		 * If we detect that we are nested in a RCU read-side critical
224 		 * section, we should simply fail, otherwise we would deadlock.
225 		 * In !PREEMPT configurations, there is no way to tell if we are
226 		 * in a RCU read-side critical section or not, so we never
227 		 * attempt any fixup and just print a warning.
228 		 */
229 #ifndef CONFIG_PREEMPT
230 		WARN_ON_ONCE(1);
231 		return 0;
232 #endif
233 		if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
234 		    irqs_disabled()) {
235 			WARN_ON_ONCE(1);
236 			return 0;
237 		}
238 		rcu_barrier();
239 		rcu_barrier_sched();
240 		rcu_barrier_bh();
241 		debug_object_activate(head, &rcuhead_debug_descr);
242 		return 1;
243 	default:
244 		return 0;
245 	}
246 }
247 
248 /*
249  * fixup_free is called when:
250  * - an active object is freed
251  */
rcuhead_fixup_free(void * addr,enum debug_obj_state state)252 static int rcuhead_fixup_free(void *addr, enum debug_obj_state state)
253 {
254 	struct rcu_head *head = addr;
255 
256 	switch (state) {
257 	case ODEBUG_STATE_ACTIVE:
258 		/*
259 		 * Ensure that queued callbacks are all executed.
260 		 * If we detect that we are nested in a RCU read-side critical
261 		 * section, we should simply fail, otherwise we would deadlock.
262 		 * In !PREEMPT configurations, there is no way to tell if we are
263 		 * in a RCU read-side critical section or not, so we never
264 		 * attempt any fixup and just print a warning.
265 		 */
266 #ifndef CONFIG_PREEMPT
267 		WARN_ON_ONCE(1);
268 		return 0;
269 #endif
270 		if (rcu_preempt_depth() != 0 || preempt_count() != 0 ||
271 		    irqs_disabled()) {
272 			WARN_ON_ONCE(1);
273 			return 0;
274 		}
275 		rcu_barrier();
276 		rcu_barrier_sched();
277 		rcu_barrier_bh();
278 		debug_object_free(head, &rcuhead_debug_descr);
279 		return 1;
280 	default:
281 		return 0;
282 	}
283 }
284 
285 /**
286  * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
287  * @head: pointer to rcu_head structure to be initialized
288  *
289  * This function informs debugobjects of a new rcu_head structure that
290  * has been allocated as an auto variable on the stack.  This function
291  * is not required for rcu_head structures that are statically defined or
292  * that are dynamically allocated on the heap.  This function has no
293  * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
294  */
init_rcu_head_on_stack(struct rcu_head * head)295 void init_rcu_head_on_stack(struct rcu_head *head)
296 {
297 	debug_object_init_on_stack(head, &rcuhead_debug_descr);
298 }
299 EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
300 
301 /**
302  * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
303  * @head: pointer to rcu_head structure to be initialized
304  *
305  * This function informs debugobjects that an on-stack rcu_head structure
306  * is about to go out of scope.  As with init_rcu_head_on_stack(), this
307  * function is not required for rcu_head structures that are statically
308  * defined or that are dynamically allocated on the heap.  Also as with
309  * init_rcu_head_on_stack(), this function has no effect for
310  * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
311  */
destroy_rcu_head_on_stack(struct rcu_head * head)312 void destroy_rcu_head_on_stack(struct rcu_head *head)
313 {
314 	debug_object_free(head, &rcuhead_debug_descr);
315 }
316 EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
317 
318 struct debug_obj_descr rcuhead_debug_descr = {
319 	.name = "rcu_head",
320 	.fixup_init = rcuhead_fixup_init,
321 	.fixup_activate = rcuhead_fixup_activate,
322 	.fixup_free = rcuhead_fixup_free,
323 };
324 EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
325 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
326 
327 #if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
do_trace_rcu_torture_read(char * rcutorturename,struct rcu_head * rhp)328 void do_trace_rcu_torture_read(char *rcutorturename, struct rcu_head *rhp)
329 {
330 	trace_rcu_torture_read(rcutorturename, rhp);
331 }
332 EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
333 #else
334 #define do_trace_rcu_torture_read(rcutorturename, rhp) do { } while (0)
335 #endif
336