blob: a3638710dc67f4627f5cdb88e1cafb43b500d24a [file] [log] [blame]
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001/*
2 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3 * Internal non-public definitions that provide either classic
4 * or preemptable semantics.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright Red Hat, 2009
21 * Copyright IBM Corporation, 2009
22 *
23 * Author: Ingo Molnar <mingo@elte.hu>
24 * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
25 */
26
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -080027#include <linux/delay.h>
Lai Jiangshan7b27d542010-10-21 11:29:05 +080028#include <linux/stop_machine.h>
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070029
Paul E. McKenney26845c22010-04-13 14:19:23 -070030/*
31 * Check the RCU kernel configuration parameters and print informative
32 * messages about anything out of the ordinary. If you like #ifdef, you
33 * will love this function.
34 */
35static void __init rcu_bootup_announce_oddness(void)
36{
37#ifdef CONFIG_RCU_TRACE
38 printk(KERN_INFO "\tRCU debugfs-based tracing is enabled.\n");
39#endif
40#if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
41 printk(KERN_INFO "\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
42 CONFIG_RCU_FANOUT);
43#endif
44#ifdef CONFIG_RCU_FANOUT_EXACT
45 printk(KERN_INFO "\tHierarchical RCU autobalancing is disabled.\n");
46#endif
47#ifdef CONFIG_RCU_FAST_NO_HZ
48 printk(KERN_INFO
49 "\tRCU dyntick-idle grace-period acceleration is enabled.\n");
50#endif
51#ifdef CONFIG_PROVE_RCU
52 printk(KERN_INFO "\tRCU lockdep checking is enabled.\n");
53#endif
54#ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
55 printk(KERN_INFO "\tRCU torture testing starts during boot.\n");
56#endif
57#ifndef CONFIG_RCU_CPU_STALL_DETECTOR
58 printk(KERN_INFO
59 "\tRCU-based detection of stalled CPUs is disabled.\n");
60#endif
Paul E. McKenney81a294c2010-08-30 09:52:50 -070061#if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
Paul E. McKenney26845c22010-04-13 14:19:23 -070062 printk(KERN_INFO "\tVerbose stalled-CPUs detection is disabled.\n");
63#endif
64#if NUM_RCU_LVL_4 != 0
65 printk(KERN_INFO "\tExperimental four-level hierarchy is enabled.\n");
66#endif
67}
68
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070069#ifdef CONFIG_TREE_PREEMPT_RCU
70
71struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
72DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
73
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -080074static int rcu_preempted_readers_exp(struct rcu_node *rnp);
75
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070076/*
77 * Tell them what RCU they are running.
78 */
Paul E. McKenney0e0fc1c2009-11-11 11:28:06 -080079static void __init rcu_bootup_announce(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070080{
Paul E. McKenney26845c22010-04-13 14:19:23 -070081 printk(KERN_INFO "Preemptable hierarchical RCU implementation.\n");
82 rcu_bootup_announce_oddness();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070083}
84
85/*
86 * Return the number of RCU-preempt batches processed thus far
87 * for debug and statistics.
88 */
89long rcu_batches_completed_preempt(void)
90{
91 return rcu_preempt_state.completed;
92}
93EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
94
95/*
96 * Return the number of RCU batches processed thus far for debug & stats.
97 */
98long rcu_batches_completed(void)
99{
100 return rcu_batches_completed_preempt();
101}
102EXPORT_SYMBOL_GPL(rcu_batches_completed);
103
104/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800105 * Force a quiescent state for preemptible RCU.
106 */
107void rcu_force_quiescent_state(void)
108{
109 force_quiescent_state(&rcu_preempt_state, 0);
110}
111EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
112
113/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700114 * Record a preemptable-RCU quiescent state for the specified CPU. Note
115 * that this just means that the task currently running on the CPU is
116 * not in a quiescent state. There might be any number of tasks blocked
117 * while in an RCU read-side critical section.
Paul E. McKenney25502a62010-04-01 17:37:01 -0700118 *
119 * Unlike the other rcu_*_qs() functions, callers to this function
120 * must disable irqs in order to protect the assignment to
121 * ->rcu_read_unlock_special.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700122 */
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700123static void rcu_preempt_qs(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700124{
125 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -0700126
Paul E. McKenneyc64ac3c2009-11-10 13:37:22 -0800127 rdp->passed_quiesc_completed = rdp->gpnum - 1;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700128 barrier();
129 rdp->passed_quiesc = 1;
Paul E. McKenney25502a62010-04-01 17:37:01 -0700130 current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700131}
132
133/*
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700134 * We have entered the scheduler, and the current task might soon be
135 * context-switched away from. If this task is in an RCU read-side
136 * critical section, we will no longer be able to rely on the CPU to
137 * record that fact, so we enqueue the task on the appropriate entry
138 * of the blocked_tasks[] array. The task will dequeue itself when
139 * it exits the outermost enclosing RCU read-side critical section.
140 * Therefore, the current grace period cannot be permitted to complete
141 * until the blocked_tasks[] entry indexed by the low-order bit of
142 * rnp->gpnum empties.
143 *
144 * Caller must disable preemption.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700145 */
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700146static void rcu_preempt_note_context_switch(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700147{
148 struct task_struct *t = current;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700149 unsigned long flags;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700150 int phase;
151 struct rcu_data *rdp;
152 struct rcu_node *rnp;
153
154 if (t->rcu_read_lock_nesting &&
155 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
156
157 /* Possibly blocking in an RCU read-side critical section. */
Lai Jiangshan394f99a2010-06-28 16:25:04 +0800158 rdp = per_cpu_ptr(rcu_preempt_state.rda, cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700159 rnp = rdp->mynode;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800160 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700161 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
Paul E. McKenney86848962009-08-27 15:00:12 -0700162 t->rcu_blocked_node = rnp;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700163
164 /*
165 * If this CPU has already checked in, then this task
166 * will hold up the next grace period rather than the
167 * current grace period. Queue the task accordingly.
168 * If the task is queued for the current grace period
169 * (i.e., this CPU has not yet passed through a quiescent
170 * state for the current grace period), then as long
171 * as that task remains queued, the current grace period
172 * cannot end.
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700173 *
174 * But first, note that the current CPU must still be
175 * on line!
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700176 */
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700177 WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700178 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
179 phase = (rnp->gpnum + !(rnp->qsmask & rdp->grpmask)) & 0x1;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700180 list_add(&t->rcu_node_entry, &rnp->blocked_tasks[phase]);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800181 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700182 }
183
184 /*
185 * Either we were not in an RCU read-side critical section to
186 * begin with, or we have now recorded that critical section
187 * globally. Either way, we can now note a quiescent state
188 * for this CPU. Again, if we were in an RCU read-side critical
189 * section, and if that critical section was blocking the current
190 * grace period, then the fact that the task has been enqueued
191 * means that we continue to block the current grace period.
192 */
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700193 local_irq_save(flags);
Paul E. McKenney25502a62010-04-01 17:37:01 -0700194 rcu_preempt_qs(cpu);
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700195 local_irq_restore(flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700196}
197
198/*
199 * Tree-preemptable RCU implementation for rcu_read_lock().
200 * Just increment ->rcu_read_lock_nesting, shared state will be updated
201 * if we block.
202 */
203void __rcu_read_lock(void)
204{
Paul E. McKenney80dcf602010-08-19 16:57:45 -0700205 current->rcu_read_lock_nesting++;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700206 barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
207}
208EXPORT_SYMBOL_GPL(__rcu_read_lock);
209
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700210/*
211 * Check for preempted RCU readers blocking the current grace period
212 * for the specified rcu_node structure. If the caller needs a reliable
213 * answer, it must hold the rcu_node's ->lock.
214 */
215static int rcu_preempted_readers(struct rcu_node *rnp)
216{
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800217 int phase = rnp->gpnum & 0x1;
218
219 return !list_empty(&rnp->blocked_tasks[phase]) ||
220 !list_empty(&rnp->blocked_tasks[phase + 2]);
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700221}
222
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800223/*
224 * Record a quiescent state for all tasks that were previously queued
225 * on the specified rcu_node structure and that were blocking the current
226 * RCU grace period. The caller must hold the specified rnp->lock with
227 * irqs disabled, and this lock is released upon return, but irqs remain
228 * disabled.
229 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800230static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800231 __releases(rnp->lock)
232{
233 unsigned long mask;
234 struct rcu_node *rnp_p;
235
236 if (rnp->qsmask != 0 || rcu_preempted_readers(rnp)) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800237 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800238 return; /* Still need more quiescent states! */
239 }
240
241 rnp_p = rnp->parent;
242 if (rnp_p == NULL) {
243 /*
244 * Either there is only one rcu_node in the tree,
245 * or tasks were kicked up to root rcu_node due to
246 * CPUs going offline.
247 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800248 rcu_report_qs_rsp(&rcu_preempt_state, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800249 return;
250 }
251
252 /* Report up the rest of the hierarchy. */
253 mask = rnp->grpmask;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800254 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
255 raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800256 rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800257}
258
259/*
260 * Handle special cases during rcu_read_unlock(), such as needing to
261 * notify RCU core processing or task having blocked during the RCU
262 * read-side critical section.
263 */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700264static void rcu_read_unlock_special(struct task_struct *t)
265{
266 int empty;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800267 int empty_exp;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700268 unsigned long flags;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700269 struct rcu_node *rnp;
270 int special;
271
272 /* NMI handlers cannot block and cannot safely manipulate state. */
273 if (in_nmi())
274 return;
275
276 local_irq_save(flags);
277
278 /*
279 * If RCU core is waiting for this CPU to exit critical section,
280 * let it know that we have done so.
281 */
282 special = t->rcu_read_unlock_special;
283 if (special & RCU_READ_UNLOCK_NEED_QS) {
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700284 rcu_preempt_qs(smp_processor_id());
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700285 }
286
287 /* Hardware IRQ handlers cannot block. */
288 if (in_irq()) {
289 local_irq_restore(flags);
290 return;
291 }
292
293 /* Clean up if blocked during RCU read-side critical section. */
294 if (special & RCU_READ_UNLOCK_BLOCKED) {
295 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
296
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700297 /*
298 * Remove this task from the list it blocked on. The
299 * task can migrate while we acquire the lock, but at
300 * most one time. So at most two passes through loop.
301 */
302 for (;;) {
Paul E. McKenney86848962009-08-27 15:00:12 -0700303 rnp = t->rcu_blocked_node;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800304 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenney86848962009-08-27 15:00:12 -0700305 if (rnp == t->rcu_blocked_node)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700306 break;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800307 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700308 }
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700309 empty = !rcu_preempted_readers(rnp);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800310 empty_exp = !rcu_preempted_readers_exp(rnp);
311 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700312 list_del_init(&t->rcu_node_entry);
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700313 t->rcu_blocked_node = NULL;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700314
315 /*
316 * If this was the last task on the current list, and if
317 * we aren't waiting on any CPUs, report the quiescent state.
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800318 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700319 */
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800320 if (empty)
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800321 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800322 else
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800323 rcu_report_unblock_qs_rnp(rnp, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800324
325 /*
326 * If this was the last task on the expedited lists,
327 * then we need to report up the rcu_node hierarchy.
328 */
329 if (!empty_exp && !rcu_preempted_readers_exp(rnp))
330 rcu_report_exp_rnp(&rcu_preempt_state, rnp);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800331 } else {
332 local_irq_restore(flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700333 }
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700334}
335
336/*
337 * Tree-preemptable RCU implementation for rcu_read_unlock().
338 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
339 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
340 * invoke rcu_read_unlock_special() to clean up after a context switch
341 * in an RCU read-side critical section and other special cases.
342 */
343void __rcu_read_unlock(void)
344{
345 struct task_struct *t = current;
346
347 barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
Paul E. McKenney80dcf602010-08-19 16:57:45 -0700348 --t->rcu_read_lock_nesting;
349 barrier(); /* decrement before load of ->rcu_read_unlock_special */
350 if (t->rcu_read_lock_nesting == 0 &&
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700351 unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
352 rcu_read_unlock_special(t);
Paul E. McKenneycba82442010-01-04 16:04:01 -0800353#ifdef CONFIG_PROVE_LOCKING
354 WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
355#endif /* #ifdef CONFIG_PROVE_LOCKING */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700356}
357EXPORT_SYMBOL_GPL(__rcu_read_unlock);
358
359#ifdef CONFIG_RCU_CPU_STALL_DETECTOR
360
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800361#ifdef CONFIG_RCU_CPU_STALL_VERBOSE
362
363/*
364 * Dump detailed information for all tasks blocking the current RCU
365 * grace period on the specified rcu_node structure.
366 */
367static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
368{
369 unsigned long flags;
370 struct list_head *lp;
371 int phase;
372 struct task_struct *t;
373
374 if (rcu_preempted_readers(rnp)) {
375 raw_spin_lock_irqsave(&rnp->lock, flags);
376 phase = rnp->gpnum & 0x1;
377 lp = &rnp->blocked_tasks[phase];
378 list_for_each_entry(t, lp, rcu_node_entry)
379 sched_show_task(t);
380 raw_spin_unlock_irqrestore(&rnp->lock, flags);
381 }
382}
383
384/*
385 * Dump detailed information for all tasks blocking the current RCU
386 * grace period.
387 */
388static void rcu_print_detail_task_stall(struct rcu_state *rsp)
389{
390 struct rcu_node *rnp = rcu_get_root(rsp);
391
392 rcu_print_detail_task_stall_rnp(rnp);
393 rcu_for_each_leaf_node(rsp, rnp)
394 rcu_print_detail_task_stall_rnp(rnp);
395}
396
397#else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
398
399static void rcu_print_detail_task_stall(struct rcu_state *rsp)
400{
401}
402
403#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
404
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700405/*
406 * Scan the current list of tasks blocked within RCU read-side critical
407 * sections, printing out the tid of each.
408 */
409static void rcu_print_task_stall(struct rcu_node *rnp)
410{
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700411 struct list_head *lp;
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700412 int phase;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700413 struct task_struct *t;
414
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700415 if (rcu_preempted_readers(rnp)) {
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700416 phase = rnp->gpnum & 0x1;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700417 lp = &rnp->blocked_tasks[phase];
418 list_for_each_entry(t, lp, rcu_node_entry)
419 printk(" P%d", t->pid);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700420 }
421}
422
Paul E. McKenney53d84e02010-08-10 14:28:53 -0700423/*
424 * Suppress preemptible RCU's CPU stall warnings by pushing the
425 * time of the next stall-warning message comfortably far into the
426 * future.
427 */
428static void rcu_preempt_stall_reset(void)
429{
430 rcu_preempt_state.jiffies_stall = jiffies + ULONG_MAX / 2;
431}
432
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700433#endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
434
435/*
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700436 * Check that the list of blocked tasks for the newly completed grace
437 * period is in fact empty. It is a serious bug to complete a grace
438 * period that still has RCU readers blocked! This function must be
439 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
440 * must be held by the caller.
441 */
442static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
443{
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700444 WARN_ON_ONCE(rcu_preempted_readers(rnp));
Paul E. McKenney28ecd582009-09-18 09:50:17 -0700445 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700446}
447
Paul E. McKenney33f76142009-08-24 09:42:01 -0700448#ifdef CONFIG_HOTPLUG_CPU
449
450/*
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700451 * Handle tasklist migration for case in which all CPUs covered by the
452 * specified rcu_node have gone offline. Move them up to the root
453 * rcu_node. The reason for not just moving them to the immediate
454 * parent is to remove the need for rcu_read_unlock_special() to
455 * make more than two attempts to acquire the target rcu_node's lock.
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800456 * Returns true if there were tasks blocking the current RCU grace
457 * period.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700458 *
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700459 * Returns 1 if there was previously a task blocking the current grace
460 * period on the specified rcu_node structure.
461 *
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700462 * The caller must hold rnp->lock with irqs disabled.
463 */
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700464static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
465 struct rcu_node *rnp,
466 struct rcu_data *rdp)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700467{
468 int i;
469 struct list_head *lp;
470 struct list_head *lp_root;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800471 int retval = 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700472 struct rcu_node *rnp_root = rcu_get_root(rsp);
473 struct task_struct *tp;
474
Paul E. McKenney86848962009-08-27 15:00:12 -0700475 if (rnp == rnp_root) {
476 WARN_ONCE(1, "Last CPU thought to be offlined?");
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700477 return 0; /* Shouldn't happen: at least one CPU online. */
Paul E. McKenney86848962009-08-27 15:00:12 -0700478 }
Paul E. McKenney28ecd582009-09-18 09:50:17 -0700479 WARN_ON_ONCE(rnp != rdp->mynode &&
480 (!list_empty(&rnp->blocked_tasks[0]) ||
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800481 !list_empty(&rnp->blocked_tasks[1]) ||
482 !list_empty(&rnp->blocked_tasks[2]) ||
483 !list_empty(&rnp->blocked_tasks[3])));
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700484
485 /*
486 * Move tasks up to root rcu_node. Rely on the fact that the
487 * root rcu_node can be at most one ahead of the rest of the
488 * rcu_nodes in terms of gp_num value. This fact allows us to
489 * move the blocked_tasks[] array directly, element by element.
490 */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800491 if (rcu_preempted_readers(rnp))
492 retval |= RCU_OFL_TASKS_NORM_GP;
493 if (rcu_preempted_readers_exp(rnp))
494 retval |= RCU_OFL_TASKS_EXP_GP;
495 for (i = 0; i < 4; i++) {
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700496 lp = &rnp->blocked_tasks[i];
497 lp_root = &rnp_root->blocked_tasks[i];
498 while (!list_empty(lp)) {
499 tp = list_entry(lp->next, typeof(*tp), rcu_node_entry);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800500 raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700501 list_del(&tp->rcu_node_entry);
502 tp->rcu_blocked_node = rnp_root;
503 list_add(&tp->rcu_node_entry, lp_root);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800504 raw_spin_unlock(&rnp_root->lock); /* irqs remain disabled */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700505 }
506 }
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700507 return retval;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700508}
509
510/*
Paul E. McKenney33f76142009-08-24 09:42:01 -0700511 * Do CPU-offline processing for preemptable RCU.
512 */
513static void rcu_preempt_offline_cpu(int cpu)
514{
515 __rcu_offline_cpu(cpu, &rcu_preempt_state);
516}
517
518#endif /* #ifdef CONFIG_HOTPLUG_CPU */
519
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700520/*
521 * Check for a quiescent state from the current CPU. When a task blocks,
522 * the task is recorded in the corresponding CPU's rcu_node structure,
523 * which is checked elsewhere.
524 *
525 * Caller must disable hard irqs.
526 */
527static void rcu_preempt_check_callbacks(int cpu)
528{
529 struct task_struct *t = current;
530
531 if (t->rcu_read_lock_nesting == 0) {
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700532 rcu_preempt_qs(cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700533 return;
534 }
Paul E. McKenneya71fca52009-09-18 10:28:19 -0700535 if (per_cpu(rcu_preempt_data, cpu).qs_pending)
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700536 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700537}
538
539/*
540 * Process callbacks for preemptable RCU.
541 */
542static void rcu_preempt_process_callbacks(void)
543{
544 __rcu_process_callbacks(&rcu_preempt_state,
545 &__get_cpu_var(rcu_preempt_data));
546}
547
548/*
549 * Queue a preemptable-RCU callback for invocation after a grace period.
550 */
551void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
552{
553 __call_rcu(head, func, &rcu_preempt_state);
554}
555EXPORT_SYMBOL_GPL(call_rcu);
556
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800557/**
558 * synchronize_rcu - wait until a grace period has elapsed.
559 *
560 * Control will return to the caller some time after a full grace
561 * period has elapsed, in other words after all currently executing RCU
Paul E. McKenney77d84852010-07-08 17:38:59 -0700562 * read-side critical sections have completed. Note, however, that
563 * upon return from synchronize_rcu(), the caller might well be executing
564 * concurrently with new RCU read-side critical sections that began while
565 * synchronize_rcu() was waiting. RCU read-side critical sections are
566 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800567 */
568void synchronize_rcu(void)
569{
570 struct rcu_synchronize rcu;
571
572 if (!rcu_scheduler_active)
573 return;
574
Paul E. McKenney72d5a9f2010-05-10 17:12:17 -0700575 init_rcu_head_on_stack(&rcu.head);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800576 init_completion(&rcu.completion);
577 /* Will wake me after RCU finished. */
578 call_rcu(&rcu.head, wakeme_after_rcu);
579 /* Wait for it. */
580 wait_for_completion(&rcu.completion);
Paul E. McKenney72d5a9f2010-05-10 17:12:17 -0700581 destroy_rcu_head_on_stack(&rcu.head);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800582}
583EXPORT_SYMBOL_GPL(synchronize_rcu);
584
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800585static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
586static long sync_rcu_preempt_exp_count;
587static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
588
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700589/*
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800590 * Return non-zero if there are any tasks in RCU read-side critical
591 * sections blocking the current preemptible-RCU expedited grace period.
592 * If there is no preemptible-RCU expedited grace period currently in
593 * progress, returns zero unconditionally.
594 */
595static int rcu_preempted_readers_exp(struct rcu_node *rnp)
596{
597 return !list_empty(&rnp->blocked_tasks[2]) ||
598 !list_empty(&rnp->blocked_tasks[3]);
599}
600
601/*
602 * return non-zero if there is no RCU expedited grace period in progress
603 * for the specified rcu_node structure, in other words, if all CPUs and
604 * tasks covered by the specified rcu_node structure have done their bit
605 * for the current expedited grace period. Works only for preemptible
606 * RCU -- other RCU implementation use other means.
607 *
608 * Caller must hold sync_rcu_preempt_exp_mutex.
609 */
610static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
611{
612 return !rcu_preempted_readers_exp(rnp) &&
613 ACCESS_ONCE(rnp->expmask) == 0;
614}
615
616/*
617 * Report the exit from RCU read-side critical section for the last task
618 * that queued itself during or before the current expedited preemptible-RCU
619 * grace period. This event is reported either to the rcu_node structure on
620 * which the task was queued or to one of that rcu_node structure's ancestors,
621 * recursively up the tree. (Calm down, calm down, we do the recursion
622 * iteratively!)
623 *
624 * Caller must hold sync_rcu_preempt_exp_mutex.
625 */
626static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
627{
628 unsigned long flags;
629 unsigned long mask;
630
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800631 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800632 for (;;) {
633 if (!sync_rcu_preempt_exp_done(rnp))
634 break;
635 if (rnp->parent == NULL) {
636 wake_up(&sync_rcu_preempt_exp_wq);
637 break;
638 }
639 mask = rnp->grpmask;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800640 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800641 rnp = rnp->parent;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800642 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800643 rnp->expmask &= ~mask;
644 }
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800645 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800646}
647
648/*
649 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
650 * grace period for the specified rcu_node structure. If there are no such
651 * tasks, report it up the rcu_node hierarchy.
652 *
653 * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
654 */
655static void
656sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
657{
658 int must_wait;
659
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800660 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800661 list_splice_init(&rnp->blocked_tasks[0], &rnp->blocked_tasks[2]);
662 list_splice_init(&rnp->blocked_tasks[1], &rnp->blocked_tasks[3]);
663 must_wait = rcu_preempted_readers_exp(rnp);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800664 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800665 if (!must_wait)
666 rcu_report_exp_rnp(rsp, rnp);
667}
668
669/*
670 * Wait for an rcu-preempt grace period, but expedite it. The basic idea
671 * is to invoke synchronize_sched_expedited() to push all the tasks to
672 * the ->blocked_tasks[] lists, move all entries from the first set of
673 * ->blocked_tasks[] lists to the second set, and finally wait for this
674 * second set to drain.
Paul E. McKenney019129d52009-10-14 10:15:56 -0700675 */
676void synchronize_rcu_expedited(void)
677{
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800678 unsigned long flags;
679 struct rcu_node *rnp;
680 struct rcu_state *rsp = &rcu_preempt_state;
681 long snap;
682 int trycount = 0;
683
684 smp_mb(); /* Caller's modifications seen first by other CPUs. */
685 snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
686 smp_mb(); /* Above access cannot bleed into critical section. */
687
688 /*
689 * Acquire lock, falling back to synchronize_rcu() if too many
690 * lock-acquisition failures. Of course, if someone does the
691 * expedited grace period for us, just leave.
692 */
693 while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
694 if (trycount++ < 10)
695 udelay(trycount * num_online_cpus());
696 else {
697 synchronize_rcu();
698 return;
699 }
700 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
701 goto mb_ret; /* Others did our work for us. */
702 }
703 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
704 goto unlock_mb_ret; /* Others did our work for us. */
705
706 /* force all RCU readers onto blocked_tasks[]. */
707 synchronize_sched_expedited();
708
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800709 raw_spin_lock_irqsave(&rsp->onofflock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800710
711 /* Initialize ->expmask for all non-leaf rcu_node structures. */
712 rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800713 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800714 rnp->expmask = rnp->qsmaskinit;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800715 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800716 }
717
718 /* Snapshot current state of ->blocked_tasks[] lists. */
719 rcu_for_each_leaf_node(rsp, rnp)
720 sync_rcu_preempt_exp_init(rsp, rnp);
721 if (NUM_RCU_NODES > 1)
722 sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
723
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800724 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800725
726 /* Wait for snapshotted ->blocked_tasks[] lists to drain. */
727 rnp = rcu_get_root(rsp);
728 wait_event(sync_rcu_preempt_exp_wq,
729 sync_rcu_preempt_exp_done(rnp));
730
731 /* Clean up and exit. */
732 smp_mb(); /* ensure expedited GP seen before counter increment. */
733 ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
734unlock_mb_ret:
735 mutex_unlock(&sync_rcu_preempt_exp_mutex);
736mb_ret:
737 smp_mb(); /* ensure subsequent action seen after grace period. */
Paul E. McKenney019129d52009-10-14 10:15:56 -0700738}
739EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
740
741/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700742 * Check to see if there is any immediate preemptable-RCU-related work
743 * to be done.
744 */
745static int rcu_preempt_pending(int cpu)
746{
747 return __rcu_pending(&rcu_preempt_state,
748 &per_cpu(rcu_preempt_data, cpu));
749}
750
751/*
752 * Does preemptable RCU need the CPU to stay out of dynticks mode?
753 */
754static int rcu_preempt_needs_cpu(int cpu)
755{
756 return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
757}
758
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700759/**
760 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
761 */
762void rcu_barrier(void)
763{
764 _rcu_barrier(&rcu_preempt_state, call_rcu);
765}
766EXPORT_SYMBOL_GPL(rcu_barrier);
767
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700768/*
769 * Initialize preemptable RCU's per-CPU data.
770 */
771static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
772{
773 rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
774}
775
776/*
Paul E. McKenney2d999e02010-10-20 12:06:18 -0700777 * Move preemptable RCU's callbacks from dying CPU to other online CPU.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700778 */
Lai Jiangshan29494be2010-10-20 14:13:06 +0800779static void rcu_preempt_send_cbs_to_online(void)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700780{
Lai Jiangshan29494be2010-10-20 14:13:06 +0800781 rcu_send_cbs_to_online(&rcu_preempt_state);
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700782}
783
784/*
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700785 * Initialize preemptable RCU's state structures.
786 */
787static void __init __rcu_init_preempt(void)
788{
Lai Jiangshan394f99a2010-06-28 16:25:04 +0800789 rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700790}
791
792/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700793 * Check for a task exiting while in a preemptable-RCU read-side
794 * critical section, clean up if so. No need to issue warnings,
795 * as debug_check_no_locks_held() already does this if lockdep
796 * is enabled.
797 */
798void exit_rcu(void)
799{
800 struct task_struct *t = current;
801
802 if (t->rcu_read_lock_nesting == 0)
803 return;
804 t->rcu_read_lock_nesting = 1;
805 rcu_read_unlock();
806}
807
808#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
809
810/*
811 * Tell them what RCU they are running.
812 */
Paul E. McKenney0e0fc1c2009-11-11 11:28:06 -0800813static void __init rcu_bootup_announce(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700814{
815 printk(KERN_INFO "Hierarchical RCU implementation.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -0700816 rcu_bootup_announce_oddness();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700817}
818
819/*
820 * Return the number of RCU batches processed thus far for debug & stats.
821 */
822long rcu_batches_completed(void)
823{
824 return rcu_batches_completed_sched();
825}
826EXPORT_SYMBOL_GPL(rcu_batches_completed);
827
828/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800829 * Force a quiescent state for RCU, which, because there is no preemptible
830 * RCU, becomes the same as rcu-sched.
831 */
832void rcu_force_quiescent_state(void)
833{
834 rcu_sched_force_quiescent_state();
835}
836EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
837
838/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700839 * Because preemptable RCU does not exist, we never have to check for
840 * CPUs being in quiescent states.
841 */
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700842static void rcu_preempt_note_context_switch(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700843{
844}
845
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700846/*
847 * Because preemptable RCU does not exist, there are never any preempted
848 * RCU readers.
849 */
850static int rcu_preempted_readers(struct rcu_node *rnp)
851{
852 return 0;
853}
854
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800855#ifdef CONFIG_HOTPLUG_CPU
856
857/* Because preemptible RCU does not exist, no quieting of tasks. */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800858static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800859{
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800860 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800861}
862
863#endif /* #ifdef CONFIG_HOTPLUG_CPU */
864
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700865#ifdef CONFIG_RCU_CPU_STALL_DETECTOR
866
867/*
868 * Because preemptable RCU does not exist, we never have to check for
869 * tasks blocked within RCU read-side critical sections.
870 */
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800871static void rcu_print_detail_task_stall(struct rcu_state *rsp)
872{
873}
874
875/*
876 * Because preemptable RCU does not exist, we never have to check for
877 * tasks blocked within RCU read-side critical sections.
878 */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700879static void rcu_print_task_stall(struct rcu_node *rnp)
880{
881}
882
Paul E. McKenney53d84e02010-08-10 14:28:53 -0700883/*
884 * Because preemptible RCU does not exist, there is no need to suppress
885 * its CPU stall warnings.
886 */
887static void rcu_preempt_stall_reset(void)
888{
889}
890
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700891#endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
892
893/*
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700894 * Because there is no preemptable RCU, there can be no readers blocked,
Paul E. McKenney49e29122009-09-18 09:50:19 -0700895 * so there is no need to check for blocked tasks. So check only for
896 * bogus qsmask values.
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700897 */
898static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
899{
Paul E. McKenney49e29122009-09-18 09:50:19 -0700900 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700901}
902
Paul E. McKenney33f76142009-08-24 09:42:01 -0700903#ifdef CONFIG_HOTPLUG_CPU
904
905/*
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700906 * Because preemptable RCU does not exist, it never needs to migrate
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700907 * tasks that were blocked within RCU read-side critical sections, and
908 * such non-existent tasks cannot possibly have been blocking the current
909 * grace period.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700910 */
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700911static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
912 struct rcu_node *rnp,
913 struct rcu_data *rdp)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700914{
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700915 return 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700916}
917
918/*
Paul E. McKenney33f76142009-08-24 09:42:01 -0700919 * Because preemptable RCU does not exist, it never needs CPU-offline
920 * processing.
921 */
922static void rcu_preempt_offline_cpu(int cpu)
923{
924}
925
926#endif /* #ifdef CONFIG_HOTPLUG_CPU */
927
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700928/*
929 * Because preemptable RCU does not exist, it never has any callbacks
930 * to check.
931 */
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700932static void rcu_preempt_check_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700933{
934}
935
936/*
937 * Because preemptable RCU does not exist, it never has any callbacks
938 * to process.
939 */
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700940static void rcu_preempt_process_callbacks(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700941{
942}
943
944/*
Paul E. McKenney019129d52009-10-14 10:15:56 -0700945 * Wait for an rcu-preempt grace period, but make it happen quickly.
946 * But because preemptable RCU does not exist, map to rcu-sched.
947 */
948void synchronize_rcu_expedited(void)
949{
950 synchronize_sched_expedited();
951}
952EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
953
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800954#ifdef CONFIG_HOTPLUG_CPU
955
956/*
957 * Because preemptable RCU does not exist, there is never any need to
958 * report on tasks preempted in RCU read-side critical sections during
959 * expedited RCU grace periods.
960 */
961static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
962{
963 return;
964}
965
966#endif /* #ifdef CONFIG_HOTPLUG_CPU */
967
Paul E. McKenney019129d52009-10-14 10:15:56 -0700968/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700969 * Because preemptable RCU does not exist, it never has any work to do.
970 */
971static int rcu_preempt_pending(int cpu)
972{
973 return 0;
974}
975
976/*
977 * Because preemptable RCU does not exist, it never needs any CPU.
978 */
979static int rcu_preempt_needs_cpu(int cpu)
980{
981 return 0;
982}
983
984/*
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700985 * Because preemptable RCU does not exist, rcu_barrier() is just
986 * another name for rcu_barrier_sched().
987 */
988void rcu_barrier(void)
989{
990 rcu_barrier_sched();
991}
992EXPORT_SYMBOL_GPL(rcu_barrier);
993
994/*
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700995 * Because preemptable RCU does not exist, there is no per-CPU
996 * data to initialize.
997 */
998static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
999{
1000}
1001
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001002/*
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001003 * Because there is no preemptable RCU, there are no callbacks to move.
1004 */
Lai Jiangshan29494be2010-10-20 14:13:06 +08001005static void rcu_preempt_send_cbs_to_online(void)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001006{
1007}
1008
1009/*
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001010 * Because preemptable RCU does not exist, it need not be initialized.
1011 */
1012static void __init __rcu_init_preempt(void)
1013{
1014}
1015
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001016#endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001017
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001018#ifndef CONFIG_SMP
1019
1020void synchronize_sched_expedited(void)
1021{
1022 cond_resched();
1023}
1024EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
1025
1026#else /* #ifndef CONFIG_SMP */
1027
Tejun Heoe27fc962010-11-22 21:36:11 -08001028static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0);
1029static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0);
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001030
1031static int synchronize_sched_expedited_cpu_stop(void *data)
1032{
1033 /*
1034 * There must be a full memory barrier on each affected CPU
1035 * between the time that try_stop_cpus() is called and the
1036 * time that it returns.
1037 *
1038 * In the current initial implementation of cpu_stop, the
1039 * above condition is already met when the control reaches
1040 * this point and the following smp_mb() is not strictly
1041 * necessary. Do smp_mb() anyway for documentation and
1042 * robustness against future implementation changes.
1043 */
1044 smp_mb(); /* See above comment block. */
1045 return 0;
1046}
1047
1048/*
1049 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
1050 * approach to force grace period to end quickly. This consumes
1051 * significant time on all CPUs, and is thus not recommended for
1052 * any sort of common-case code.
1053 *
1054 * Note that it is illegal to call this function while holding any
1055 * lock that is acquired by a CPU-hotplug notifier. Failing to
1056 * observe this restriction will result in deadlock.
Paul E. McKenneydb3a8922010-10-25 07:39:22 -07001057 *
Tejun Heoe27fc962010-11-22 21:36:11 -08001058 * This implementation can be thought of as an application of ticket
1059 * locking to RCU, with sync_sched_expedited_started and
1060 * sync_sched_expedited_done taking on the roles of the halves
1061 * of the ticket-lock word. Each task atomically increments
1062 * sync_sched_expedited_started upon entry, snapshotting the old value,
1063 * then attempts to stop all the CPUs. If this succeeds, then each
1064 * CPU will have executed a context switch, resulting in an RCU-sched
1065 * grace period. We are then done, so we use atomic_cmpxchg() to
1066 * update sync_sched_expedited_done to match our snapshot -- but
1067 * only if someone else has not already advanced past our snapshot.
1068 *
1069 * On the other hand, if try_stop_cpus() fails, we check the value
1070 * of sync_sched_expedited_done. If it has advanced past our
1071 * initial snapshot, then someone else must have forced a grace period
1072 * some time after we took our snapshot. In this case, our work is
1073 * done for us, and we can simply return. Otherwise, we try again,
1074 * but keep our initial snapshot for purposes of checking for someone
1075 * doing our work for us.
1076 *
1077 * If we fail too many times in a row, we fall back to synchronize_sched().
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001078 */
1079void synchronize_sched_expedited(void)
1080{
Tejun Heoe27fc962010-11-22 21:36:11 -08001081 int firstsnap, s, snap, trycount = 0;
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001082
Tejun Heoe27fc962010-11-22 21:36:11 -08001083 /* Note that atomic_inc_return() implies full memory barrier. */
1084 firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started);
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001085 get_online_cpus();
Tejun Heoe27fc962010-11-22 21:36:11 -08001086
1087 /*
1088 * Each pass through the following loop attempts to force a
1089 * context switch on each CPU.
1090 */
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001091 while (try_stop_cpus(cpu_online_mask,
1092 synchronize_sched_expedited_cpu_stop,
1093 NULL) == -EAGAIN) {
1094 put_online_cpus();
Tejun Heoe27fc962010-11-22 21:36:11 -08001095
1096 /* No joy, try again later. Or just synchronize_sched(). */
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001097 if (trycount++ < 10)
1098 udelay(trycount * num_online_cpus());
1099 else {
1100 synchronize_sched();
1101 return;
1102 }
Tejun Heoe27fc962010-11-22 21:36:11 -08001103
1104 /* Check to see if someone else did our work for us. */
1105 s = atomic_read(&sync_sched_expedited_done);
1106 if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) {
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001107 smp_mb(); /* ensure test happens before caller kfree */
1108 return;
1109 }
Tejun Heoe27fc962010-11-22 21:36:11 -08001110
1111 /*
1112 * Refetching sync_sched_expedited_started allows later
1113 * callers to piggyback on our grace period. We subtract
1114 * 1 to get the same token that the last incrementer got.
1115 * We retry after they started, so our grace period works
1116 * for them, and they started after our first try, so their
1117 * grace period works for us.
1118 */
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001119 get_online_cpus();
Tejun Heoe27fc962010-11-22 21:36:11 -08001120 snap = atomic_read(&sync_sched_expedited_started) - 1;
1121 smp_mb(); /* ensure read is before try_stop_cpus(). */
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001122 }
Tejun Heoe27fc962010-11-22 21:36:11 -08001123
1124 /*
1125 * Everyone up to our most recent fetch is covered by our grace
1126 * period. Update the counter, but only if our work is still
1127 * relevant -- which it won't be if someone who started later
1128 * than we did beat us to the punch.
1129 */
1130 do {
1131 s = atomic_read(&sync_sched_expedited_done);
1132 if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) {
1133 smp_mb(); /* ensure test happens before caller kfree */
1134 break;
1135 }
1136 } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s);
1137
Lai Jiangshan7b27d542010-10-21 11:29:05 +08001138 put_online_cpus();
1139}
1140EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
1141
1142#endif /* #else #ifndef CONFIG_SMP */
1143
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001144#if !defined(CONFIG_RCU_FAST_NO_HZ)
1145
1146/*
1147 * Check to see if any future RCU-related work will need to be done
1148 * by the current CPU, even if none need be done immediately, returning
1149 * 1 if so. This function is part of the RCU implementation; it is -not-
1150 * an exported member of the RCU API.
1151 *
1152 * Because we have preemptible RCU, just check whether this CPU needs
1153 * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
1154 * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
1155 */
1156int rcu_needs_cpu(int cpu)
1157{
1158 return rcu_needs_cpu_quick_check(cpu);
1159}
1160
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001161/*
1162 * Check to see if we need to continue a callback-flush operations to
1163 * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
1164 * entry is not configured, so we never do need to.
1165 */
1166static void rcu_needs_cpu_flush(void)
1167{
1168}
1169
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001170#else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1171
1172#define RCU_NEEDS_CPU_FLUSHES 5
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001173static DEFINE_PER_CPU(int, rcu_dyntick_drain);
Paul E. McKenney71da8132010-02-26 16:38:58 -08001174static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001175
1176/*
1177 * Check to see if any future RCU-related work will need to be done
1178 * by the current CPU, even if none need be done immediately, returning
1179 * 1 if so. This function is part of the RCU implementation; it is -not-
1180 * an exported member of the RCU API.
1181 *
1182 * Because we are not supporting preemptible RCU, attempt to accelerate
1183 * any current grace periods so that RCU no longer needs this CPU, but
1184 * only if all other CPUs are already in dynticks-idle mode. This will
1185 * allow the CPU cores to be powered down immediately, as opposed to after
1186 * waiting many milliseconds for grace periods to elapse.
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001187 *
1188 * Because it is not legal to invoke rcu_process_callbacks() with irqs
1189 * disabled, we do one pass of force_quiescent_state(), then do a
1190 * raise_softirq() to cause rcu_process_callbacks() to be invoked later.
1191 * The per-cpu rcu_dyntick_drain variable controls the sequencing.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001192 */
1193int rcu_needs_cpu(int cpu)
1194{
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001195 int c = 0;
Paul E. McKenney77e38ed2010-04-25 21:04:29 -07001196 int snap;
1197 int snap_nmi;
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001198 int thatcpu;
1199
Paul E. McKenney622ea682010-02-27 14:53:07 -08001200 /* Check for being in the holdoff period. */
1201 if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
1202 return rcu_needs_cpu_quick_check(cpu);
1203
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001204 /* Don't bother unless we are the last non-dyntick-idle CPU. */
Paul E. McKenney77e38ed2010-04-25 21:04:29 -07001205 for_each_online_cpu(thatcpu) {
1206 if (thatcpu == cpu)
1207 continue;
Paul E. McKenneyd822ed12010-05-08 19:58:22 -07001208 snap = per_cpu(rcu_dynticks, thatcpu).dynticks;
1209 snap_nmi = per_cpu(rcu_dynticks, thatcpu).dynticks_nmi;
Paul E. McKenney77e38ed2010-04-25 21:04:29 -07001210 smp_mb(); /* Order sampling of snap with end of grace period. */
1211 if (((snap & 0x1) != 0) || ((snap_nmi & 0x1) != 0)) {
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001212 per_cpu(rcu_dyntick_drain, cpu) = 0;
Paul E. McKenney71da8132010-02-26 16:38:58 -08001213 per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001214 return rcu_needs_cpu_quick_check(cpu);
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001215 }
Paul E. McKenney77e38ed2010-04-25 21:04:29 -07001216 }
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001217
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001218 /* Check and update the rcu_dyntick_drain sequencing. */
1219 if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
1220 /* First time through, initialize the counter. */
1221 per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
1222 } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
1223 /* We have hit the limit, so time to give up. */
Paul E. McKenney71da8132010-02-26 16:38:58 -08001224 per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001225 return rcu_needs_cpu_quick_check(cpu);
1226 }
1227
1228 /* Do one step pushing remaining RCU callbacks through. */
1229 if (per_cpu(rcu_sched_data, cpu).nxtlist) {
1230 rcu_sched_qs(cpu);
1231 force_quiescent_state(&rcu_sched_state, 0);
1232 c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
1233 }
1234 if (per_cpu(rcu_bh_data, cpu).nxtlist) {
1235 rcu_bh_qs(cpu);
1236 force_quiescent_state(&rcu_bh_state, 0);
1237 c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001238 }
1239
1240 /* If RCU callbacks are still pending, RCU still needs this CPU. */
Paul E. McKenney622ea682010-02-27 14:53:07 -08001241 if (c)
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001242 raise_softirq(RCU_SOFTIRQ);
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001243 return c;
1244}
1245
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001246/*
1247 * Check to see if we need to continue a callback-flush operations to
1248 * allow the last CPU to enter dyntick-idle mode.
1249 */
1250static void rcu_needs_cpu_flush(void)
1251{
1252 int cpu = smp_processor_id();
Paul E. McKenney71da8132010-02-26 16:38:58 -08001253 unsigned long flags;
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001254
1255 if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
1256 return;
Paul E. McKenney71da8132010-02-26 16:38:58 -08001257 local_irq_save(flags);
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001258 (void)rcu_needs_cpu(cpu);
Paul E. McKenney71da8132010-02-26 16:38:58 -08001259 local_irq_restore(flags);
Paul E. McKenneya47cd882010-02-26 16:38:56 -08001260}
1261
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001262#endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */