blob: 5271a020887e6ea515aa29c9f52f9f612d9056ac [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
Paul E. McKenney6cc68792011-03-02 13:15:15 -08004 * or preemptible semantics.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07005 *
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>
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070028
Mike Galbraith5b61b0b2011-08-19 11:39:11 -070029#define RCU_KTHREAD_PRIO 1
30
31#ifdef CONFIG_RCU_BOOST
32#define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
33#else
34#define RCU_BOOST_PRIO RCU_KTHREAD_PRIO
35#endif
36
Paul E. McKenney26845c22010-04-13 14:19:23 -070037/*
38 * Check the RCU kernel configuration parameters and print informative
39 * messages about anything out of the ordinary. If you like #ifdef, you
40 * will love this function.
41 */
42static void __init rcu_bootup_announce_oddness(void)
43{
44#ifdef CONFIG_RCU_TRACE
45 printk(KERN_INFO "\tRCU debugfs-based tracing is enabled.\n");
46#endif
47#if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
48 printk(KERN_INFO "\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
49 CONFIG_RCU_FANOUT);
50#endif
51#ifdef CONFIG_RCU_FANOUT_EXACT
52 printk(KERN_INFO "\tHierarchical RCU autobalancing is disabled.\n");
53#endif
54#ifdef CONFIG_RCU_FAST_NO_HZ
55 printk(KERN_INFO
56 "\tRCU dyntick-idle grace-period acceleration is enabled.\n");
57#endif
58#ifdef CONFIG_PROVE_RCU
59 printk(KERN_INFO "\tRCU lockdep checking is enabled.\n");
60#endif
61#ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
62 printk(KERN_INFO "\tRCU torture testing starts during boot.\n");
63#endif
Paul E. McKenney81a294c2010-08-30 09:52:50 -070064#if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
Paul E. McKenneya858af22012-01-16 13:29:10 -080065 printk(KERN_INFO "\tDump stacks of tasks blocking RCU-preempt GP.\n");
66#endif
67#if defined(CONFIG_RCU_CPU_STALL_INFO)
68 printk(KERN_INFO "\tAdditional per-CPU info printed with stalls.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -070069#endif
70#if NUM_RCU_LVL_4 != 0
71 printk(KERN_INFO "\tExperimental four-level hierarchy is enabled.\n");
72#endif
73}
74
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070075#ifdef CONFIG_TREE_PREEMPT_RCU
76
Paul E. McKenneye99033c2011-06-21 00:13:44 -070077struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070078DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
Paul E. McKenney27f4d282011-02-07 12:47:15 -080079static struct rcu_state *rcu_state = &rcu_preempt_state;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070080
Paul E. McKenney10f39bb2011-07-17 21:14:35 -070081static void rcu_read_unlock_special(struct task_struct *t);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -080082static int rcu_preempted_readers_exp(struct rcu_node *rnp);
83
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070084/*
85 * Tell them what RCU they are running.
86 */
Paul E. McKenney0e0fc1c2009-11-11 11:28:06 -080087static void __init rcu_bootup_announce(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070088{
Paul E. McKenney6cc68792011-03-02 13:15:15 -080089 printk(KERN_INFO "Preemptible hierarchical RCU implementation.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -070090 rcu_bootup_announce_oddness();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070091}
92
93/*
94 * Return the number of RCU-preempt batches processed thus far
95 * for debug and statistics.
96 */
97long rcu_batches_completed_preempt(void)
98{
99 return rcu_preempt_state.completed;
100}
101EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
102
103/*
104 * Return the number of RCU batches processed thus far for debug & stats.
105 */
106long rcu_batches_completed(void)
107{
108 return rcu_batches_completed_preempt();
109}
110EXPORT_SYMBOL_GPL(rcu_batches_completed);
111
112/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800113 * Force a quiescent state for preemptible RCU.
114 */
115void rcu_force_quiescent_state(void)
116{
117 force_quiescent_state(&rcu_preempt_state, 0);
118}
119EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
120
121/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800122 * Record a preemptible-RCU quiescent state for the specified CPU. Note
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700123 * that this just means that the task currently running on the CPU is
124 * not in a quiescent state. There might be any number of tasks blocked
125 * while in an RCU read-side critical section.
Paul E. McKenney25502a62010-04-01 17:37:01 -0700126 *
127 * Unlike the other rcu_*_qs() functions, callers to this function
128 * must disable irqs in order to protect the assignment to
129 * ->rcu_read_unlock_special.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700130 */
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700131static void rcu_preempt_qs(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700132{
133 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -0700134
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700135 rdp->passed_quiesce_gpnum = rdp->gpnum;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700136 barrier();
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700137 if (rdp->passed_quiesce == 0)
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700138 trace_rcu_grace_period("rcu_preempt", rdp->gpnum, "cpuqs");
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700139 rdp->passed_quiesce = 1;
Paul E. McKenney25502a62010-04-01 17:37:01 -0700140 current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700141}
142
143/*
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700144 * We have entered the scheduler, and the current task might soon be
145 * context-switched away from. If this task is in an RCU read-side
146 * critical section, we will no longer be able to rely on the CPU to
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800147 * record that fact, so we enqueue the task on the blkd_tasks list.
148 * The task will dequeue itself when it exits the outermost enclosing
149 * RCU read-side critical section. Therefore, the current grace period
150 * cannot be permitted to complete until the blkd_tasks list entries
151 * predating the current grace period drain, in other words, until
152 * rnp->gp_tasks becomes NULL.
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700153 *
154 * Caller must disable preemption.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700155 */
Paul E. McKenney616c3102012-03-27 16:02:08 -0700156void rcu_preempt_note_context_switch(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700157{
158 struct task_struct *t = current;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700159 unsigned long flags;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700160 struct rcu_data *rdp;
161 struct rcu_node *rnp;
162
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700163 if (t->rcu_read_lock_nesting > 0 &&
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700164 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
165
166 /* Possibly blocking in an RCU read-side critical section. */
Paul E. McKenney616c3102012-03-27 16:02:08 -0700167 rdp = __this_cpu_ptr(rcu_preempt_state.rda);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700168 rnp = rdp->mynode;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800169 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700170 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
Paul E. McKenney86848962009-08-27 15:00:12 -0700171 t->rcu_blocked_node = rnp;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700172
173 /*
174 * If this CPU has already checked in, then this task
175 * will hold up the next grace period rather than the
176 * current grace period. Queue the task accordingly.
177 * If the task is queued for the current grace period
178 * (i.e., this CPU has not yet passed through a quiescent
179 * state for the current grace period), then as long
180 * as that task remains queued, the current grace period
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800181 * cannot end. Note that there is some uncertainty as
182 * to exactly when the current grace period started.
183 * We take a conservative approach, which can result
184 * in unnecessarily waiting on tasks that started very
185 * slightly after the current grace period began. C'est
186 * la vie!!!
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700187 *
188 * But first, note that the current CPU must still be
189 * on line!
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700190 */
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700191 WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700192 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800193 if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
194 list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
195 rnp->gp_tasks = &t->rcu_node_entry;
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800196#ifdef CONFIG_RCU_BOOST
197 if (rnp->boost_tasks != NULL)
198 rnp->boost_tasks = rnp->gp_tasks;
199#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800200 } else {
201 list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
202 if (rnp->qsmask & rdp->grpmask)
203 rnp->gp_tasks = &t->rcu_node_entry;
204 }
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700205 trace_rcu_preempt_task(rdp->rsp->name,
206 t->pid,
207 (rnp->qsmask & rdp->grpmask)
208 ? rnp->gpnum
209 : rnp->gpnum + 1);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800210 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700211 } else if (t->rcu_read_lock_nesting < 0 &&
212 t->rcu_read_unlock_special) {
213
214 /*
215 * Complete exit from RCU read-side critical section on
216 * behalf of preempted instance of __rcu_read_unlock().
217 */
218 rcu_read_unlock_special(t);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700219 }
220
221 /*
222 * Either we were not in an RCU read-side critical section to
223 * begin with, or we have now recorded that critical section
224 * globally. Either way, we can now note a quiescent state
225 * for this CPU. Again, if we were in an RCU read-side critical
226 * section, and if that critical section was blocking the current
227 * grace period, then the fact that the task has been enqueued
228 * means that we continue to block the current grace period.
229 */
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700230 local_irq_save(flags);
Paul E. McKenney616c3102012-03-27 16:02:08 -0700231 rcu_preempt_qs(smp_processor_id());
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700232 local_irq_restore(flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700233}
234
235/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800236 * Tree-preemptible RCU implementation for rcu_read_lock().
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700237 * Just increment ->rcu_read_lock_nesting, shared state will be updated
238 * if we block.
239 */
240void __rcu_read_lock(void)
241{
Paul E. McKenney80dcf602010-08-19 16:57:45 -0700242 current->rcu_read_lock_nesting++;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700243 barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
244}
245EXPORT_SYMBOL_GPL(__rcu_read_lock);
246
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700247/*
248 * Check for preempted RCU readers blocking the current grace period
249 * for the specified rcu_node structure. If the caller needs a reliable
250 * answer, it must hold the rcu_node's ->lock.
251 */
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800252static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700253{
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800254 return rnp->gp_tasks != NULL;
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -0700255}
256
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800257/*
258 * Record a quiescent state for all tasks that were previously queued
259 * on the specified rcu_node structure and that were blocking the current
260 * RCU grace period. The caller must hold the specified rnp->lock with
261 * irqs disabled, and this lock is released upon return, but irqs remain
262 * disabled.
263 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800264static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800265 __releases(rnp->lock)
266{
267 unsigned long mask;
268 struct rcu_node *rnp_p;
269
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800270 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800271 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800272 return; /* Still need more quiescent states! */
273 }
274
275 rnp_p = rnp->parent;
276 if (rnp_p == NULL) {
277 /*
278 * Either there is only one rcu_node in the tree,
279 * or tasks were kicked up to root rcu_node due to
280 * CPUs going offline.
281 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800282 rcu_report_qs_rsp(&rcu_preempt_state, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800283 return;
284 }
285
286 /* Report up the rest of the hierarchy. */
287 mask = rnp->grpmask;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800288 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
289 raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800290 rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800291}
292
293/*
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800294 * Advance a ->blkd_tasks-list pointer to the next entry, instead
295 * returning NULL if at the end of the list.
296 */
297static struct list_head *rcu_next_node_entry(struct task_struct *t,
298 struct rcu_node *rnp)
299{
300 struct list_head *np;
301
302 np = t->rcu_node_entry.next;
303 if (np == &rnp->blkd_tasks)
304 np = NULL;
305 return np;
306}
307
308/*
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800309 * Handle special cases during rcu_read_unlock(), such as needing to
310 * notify RCU core processing or task having blocked during the RCU
311 * read-side critical section.
312 */
Paul E. McKenneybe0e1e22011-05-21 05:57:18 -0700313static noinline void rcu_read_unlock_special(struct task_struct *t)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700314{
315 int empty;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800316 int empty_exp;
Paul E. McKenney389abd42011-09-21 14:41:37 -0700317 int empty_exp_now;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700318 unsigned long flags;
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800319 struct list_head *np;
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700320#ifdef CONFIG_RCU_BOOST
321 struct rt_mutex *rbmp = NULL;
322#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700323 struct rcu_node *rnp;
324 int special;
325
326 /* NMI handlers cannot block and cannot safely manipulate state. */
327 if (in_nmi())
328 return;
329
330 local_irq_save(flags);
331
332 /*
333 * If RCU core is waiting for this CPU to exit critical section,
334 * let it know that we have done so.
335 */
336 special = t->rcu_read_unlock_special;
337 if (special & RCU_READ_UNLOCK_NEED_QS) {
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700338 rcu_preempt_qs(smp_processor_id());
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700339 }
340
341 /* Hardware IRQ handlers cannot block. */
Peter Zijlstraec433f02011-07-19 15:32:00 -0700342 if (in_irq() || in_serving_softirq()) {
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700343 local_irq_restore(flags);
344 return;
345 }
346
347 /* Clean up if blocked during RCU read-side critical section. */
348 if (special & RCU_READ_UNLOCK_BLOCKED) {
349 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
350
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700351 /*
352 * Remove this task from the list it blocked on. The
353 * task can migrate while we acquire the lock, but at
354 * most one time. So at most two passes through loop.
355 */
356 for (;;) {
Paul E. McKenney86848962009-08-27 15:00:12 -0700357 rnp = t->rcu_blocked_node;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800358 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenney86848962009-08-27 15:00:12 -0700359 if (rnp == t->rcu_blocked_node)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700360 break;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800361 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700362 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800363 empty = !rcu_preempt_blocked_readers_cgp(rnp);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800364 empty_exp = !rcu_preempted_readers_exp(rnp);
365 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800366 np = rcu_next_node_entry(t, rnp);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700367 list_del_init(&t->rcu_node_entry);
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700368 t->rcu_blocked_node = NULL;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700369 trace_rcu_unlock_preempted_task("rcu_preempt",
370 rnp->gpnum, t->pid);
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800371 if (&t->rcu_node_entry == rnp->gp_tasks)
372 rnp->gp_tasks = np;
373 if (&t->rcu_node_entry == rnp->exp_tasks)
374 rnp->exp_tasks = np;
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800375#ifdef CONFIG_RCU_BOOST
376 if (&t->rcu_node_entry == rnp->boost_tasks)
377 rnp->boost_tasks = np;
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700378 /* Snapshot/clear ->rcu_boost_mutex with rcu_node lock held. */
379 if (t->rcu_boost_mutex) {
380 rbmp = t->rcu_boost_mutex;
381 t->rcu_boost_mutex = NULL;
Paul E. McKenney7765be22011-07-14 12:24:11 -0700382 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800383#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700384
385 /*
386 * If this was the last task on the current list, and if
387 * we aren't waiting on any CPUs, report the quiescent state.
Paul E. McKenney389abd42011-09-21 14:41:37 -0700388 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
389 * so we must take a snapshot of the expedited state.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700390 */
Paul E. McKenney389abd42011-09-21 14:41:37 -0700391 empty_exp_now = !rcu_preempted_readers_exp(rnp);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700392 if (!empty && !rcu_preempt_blocked_readers_cgp(rnp)) {
393 trace_rcu_quiescent_state_report("preempt_rcu",
394 rnp->gpnum,
395 0, rnp->qsmask,
396 rnp->level,
397 rnp->grplo,
398 rnp->grphi,
399 !!rnp->gp_tasks);
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800400 rcu_report_unblock_qs_rnp(rnp, flags);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700401 } else
402 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800403
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800404#ifdef CONFIG_RCU_BOOST
405 /* Unboost if we were boosted. */
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700406 if (rbmp)
407 rt_mutex_unlock(rbmp);
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800408#endif /* #ifdef CONFIG_RCU_BOOST */
409
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800410 /*
411 * If this was the last task on the expedited lists,
412 * then we need to report up the rcu_node hierarchy.
413 */
Paul E. McKenney389abd42011-09-21 14:41:37 -0700414 if (!empty_exp && empty_exp_now)
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700415 rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800416 } else {
417 local_irq_restore(flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700418 }
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700419}
420
421/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800422 * Tree-preemptible RCU implementation for rcu_read_unlock().
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700423 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
424 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
425 * invoke rcu_read_unlock_special() to clean up after a context switch
426 * in an RCU read-side critical section and other special cases.
427 */
428void __rcu_read_unlock(void)
429{
430 struct task_struct *t = current;
431
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700432 if (t->rcu_read_lock_nesting != 1)
433 --t->rcu_read_lock_nesting;
434 else {
Paul E. McKenney6206ab92011-08-01 06:22:11 -0700435 barrier(); /* critical section before exit code. */
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700436 t->rcu_read_lock_nesting = INT_MIN;
437 barrier(); /* assign before ->rcu_read_unlock_special load */
Paul E. McKenneybe0e1e22011-05-21 05:57:18 -0700438 if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
439 rcu_read_unlock_special(t);
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700440 barrier(); /* ->rcu_read_unlock_special load before assign */
441 t->rcu_read_lock_nesting = 0;
Paul E. McKenneybe0e1e22011-05-21 05:57:18 -0700442 }
Paul E. McKenneycba82442010-01-04 16:04:01 -0800443#ifdef CONFIG_PROVE_LOCKING
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700444 {
445 int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
446
447 WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
448 }
Paul E. McKenneycba82442010-01-04 16:04:01 -0800449#endif /* #ifdef CONFIG_PROVE_LOCKING */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700450}
451EXPORT_SYMBOL_GPL(__rcu_read_unlock);
452
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800453#ifdef CONFIG_RCU_CPU_STALL_VERBOSE
454
455/*
456 * Dump detailed information for all tasks blocking the current RCU
457 * grace period on the specified rcu_node structure.
458 */
459static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
460{
461 unsigned long flags;
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800462 struct task_struct *t;
463
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800464 if (!rcu_preempt_blocked_readers_cgp(rnp))
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800465 return;
466 raw_spin_lock_irqsave(&rnp->lock, flags);
467 t = list_entry(rnp->gp_tasks,
468 struct task_struct, rcu_node_entry);
469 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
470 sched_show_task(t);
471 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800472}
473
474/*
475 * Dump detailed information for all tasks blocking the current RCU
476 * grace period.
477 */
478static void rcu_print_detail_task_stall(struct rcu_state *rsp)
479{
480 struct rcu_node *rnp = rcu_get_root(rsp);
481
482 rcu_print_detail_task_stall_rnp(rnp);
483 rcu_for_each_leaf_node(rsp, rnp)
484 rcu_print_detail_task_stall_rnp(rnp);
485}
486
487#else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
488
489static void rcu_print_detail_task_stall(struct rcu_state *rsp)
490{
491}
492
493#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
494
Paul E. McKenneya858af22012-01-16 13:29:10 -0800495#ifdef CONFIG_RCU_CPU_STALL_INFO
496
497static void rcu_print_task_stall_begin(struct rcu_node *rnp)
498{
499 printk(KERN_ERR "\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
500 rnp->level, rnp->grplo, rnp->grphi);
501}
502
503static void rcu_print_task_stall_end(void)
504{
505 printk(KERN_CONT "\n");
506}
507
508#else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
509
510static void rcu_print_task_stall_begin(struct rcu_node *rnp)
511{
512}
513
514static void rcu_print_task_stall_end(void)
515{
516}
517
518#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
519
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700520/*
521 * Scan the current list of tasks blocked within RCU read-side critical
522 * sections, printing out the tid of each.
523 */
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700524static int rcu_print_task_stall(struct rcu_node *rnp)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700525{
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700526 struct task_struct *t;
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700527 int ndetected = 0;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700528
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800529 if (!rcu_preempt_blocked_readers_cgp(rnp))
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700530 return 0;
Paul E. McKenneya858af22012-01-16 13:29:10 -0800531 rcu_print_task_stall_begin(rnp);
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800532 t = list_entry(rnp->gp_tasks,
533 struct task_struct, rcu_node_entry);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700534 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
Paul E. McKenneya858af22012-01-16 13:29:10 -0800535 printk(KERN_CONT " P%d", t->pid);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700536 ndetected++;
537 }
Paul E. McKenneya858af22012-01-16 13:29:10 -0800538 rcu_print_task_stall_end();
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700539 return ndetected;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700540}
541
Paul E. McKenney53d84e02010-08-10 14:28:53 -0700542/*
543 * Suppress preemptible RCU's CPU stall warnings by pushing the
544 * time of the next stall-warning message comfortably far into the
545 * future.
546 */
547static void rcu_preempt_stall_reset(void)
548{
549 rcu_preempt_state.jiffies_stall = jiffies + ULONG_MAX / 2;
550}
551
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700552/*
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700553 * Check that the list of blocked tasks for the newly completed grace
554 * period is in fact empty. It is a serious bug to complete a grace
555 * period that still has RCU readers blocked! This function must be
556 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
557 * must be held by the caller.
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800558 *
559 * Also, if there are blocked tasks on the list, they automatically
560 * block the newly created grace period, so set up ->gp_tasks accordingly.
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700561 */
562static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
563{
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800564 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800565 if (!list_empty(&rnp->blkd_tasks))
566 rnp->gp_tasks = rnp->blkd_tasks.next;
Paul E. McKenney28ecd582009-09-18 09:50:17 -0700567 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700568}
569
Paul E. McKenney33f76142009-08-24 09:42:01 -0700570#ifdef CONFIG_HOTPLUG_CPU
571
572/*
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700573 * Handle tasklist migration for case in which all CPUs covered by the
574 * specified rcu_node have gone offline. Move them up to the root
575 * rcu_node. The reason for not just moving them to the immediate
576 * parent is to remove the need for rcu_read_unlock_special() to
577 * make more than two attempts to acquire the target rcu_node's lock.
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800578 * Returns true if there were tasks blocking the current RCU grace
579 * period.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700580 *
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700581 * Returns 1 if there was previously a task blocking the current grace
582 * period on the specified rcu_node structure.
583 *
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700584 * The caller must hold rnp->lock with irqs disabled.
585 */
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700586static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
587 struct rcu_node *rnp,
588 struct rcu_data *rdp)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700589{
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700590 struct list_head *lp;
591 struct list_head *lp_root;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800592 int retval = 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700593 struct rcu_node *rnp_root = rcu_get_root(rsp);
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800594 struct task_struct *t;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700595
Paul E. McKenney86848962009-08-27 15:00:12 -0700596 if (rnp == rnp_root) {
597 WARN_ONCE(1, "Last CPU thought to be offlined?");
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700598 return 0; /* Shouldn't happen: at least one CPU online. */
Paul E. McKenney86848962009-08-27 15:00:12 -0700599 }
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800600
601 /* If we are on an internal node, complain bitterly. */
602 WARN_ON_ONCE(rnp != rdp->mynode);
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700603
604 /*
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800605 * Move tasks up to root rcu_node. Don't try to get fancy for
606 * this corner-case operation -- just put this node's tasks
607 * at the head of the root node's list, and update the root node's
608 * ->gp_tasks and ->exp_tasks pointers to those of this node's,
609 * if non-NULL. This might result in waiting for more tasks than
610 * absolutely necessary, but this is a good performance/complexity
611 * tradeoff.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700612 */
Paul E. McKenney2036d942012-01-30 17:02:47 -0800613 if (rcu_preempt_blocked_readers_cgp(rnp) && rnp->qsmask == 0)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800614 retval |= RCU_OFL_TASKS_NORM_GP;
615 if (rcu_preempted_readers_exp(rnp))
616 retval |= RCU_OFL_TASKS_EXP_GP;
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800617 lp = &rnp->blkd_tasks;
618 lp_root = &rnp_root->blkd_tasks;
619 while (!list_empty(lp)) {
620 t = list_entry(lp->next, typeof(*t), rcu_node_entry);
621 raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
622 list_del(&t->rcu_node_entry);
623 t->rcu_blocked_node = rnp_root;
624 list_add(&t->rcu_node_entry, lp_root);
625 if (&t->rcu_node_entry == rnp->gp_tasks)
626 rnp_root->gp_tasks = rnp->gp_tasks;
627 if (&t->rcu_node_entry == rnp->exp_tasks)
628 rnp_root->exp_tasks = rnp->exp_tasks;
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800629#ifdef CONFIG_RCU_BOOST
630 if (&t->rcu_node_entry == rnp->boost_tasks)
631 rnp_root->boost_tasks = rnp->boost_tasks;
632#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800633 raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700634 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800635
636#ifdef CONFIG_RCU_BOOST
637 /* In case root is being boosted and leaf is not. */
638 raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
639 if (rnp_root->boost_tasks != NULL &&
640 rnp_root->boost_tasks != rnp_root->gp_tasks)
641 rnp_root->boost_tasks = rnp_root->gp_tasks;
642 raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
643#endif /* #ifdef CONFIG_RCU_BOOST */
644
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800645 rnp->gp_tasks = NULL;
646 rnp->exp_tasks = NULL;
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700647 return retval;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700648}
649
Paul E. McKenneye5601402012-01-07 11:03:57 -0800650#endif /* #ifdef CONFIG_HOTPLUG_CPU */
651
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700652/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800653 * Do CPU-offline processing for preemptible RCU.
Paul E. McKenney33f76142009-08-24 09:42:01 -0700654 */
Paul E. McKenneye5601402012-01-07 11:03:57 -0800655static void rcu_preempt_cleanup_dead_cpu(int cpu)
Paul E. McKenney33f76142009-08-24 09:42:01 -0700656{
Paul E. McKenneye5601402012-01-07 11:03:57 -0800657 rcu_cleanup_dead_cpu(cpu, &rcu_preempt_state);
Paul E. McKenney33f76142009-08-24 09:42:01 -0700658}
659
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700660/*
661 * Check for a quiescent state from the current CPU. When a task blocks,
662 * the task is recorded in the corresponding CPU's rcu_node structure,
663 * which is checked elsewhere.
664 *
665 * Caller must disable hard irqs.
666 */
667static void rcu_preempt_check_callbacks(int cpu)
668{
669 struct task_struct *t = current;
670
671 if (t->rcu_read_lock_nesting == 0) {
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700672 rcu_preempt_qs(cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700673 return;
674 }
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700675 if (t->rcu_read_lock_nesting > 0 &&
676 per_cpu(rcu_preempt_data, cpu).qs_pending)
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700677 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700678}
679
680/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800681 * Process callbacks for preemptible RCU.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700682 */
683static void rcu_preempt_process_callbacks(void)
684{
685 __rcu_process_callbacks(&rcu_preempt_state,
686 &__get_cpu_var(rcu_preempt_data));
687}
688
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700689#ifdef CONFIG_RCU_BOOST
690
Shaohua Li09223372011-06-14 13:26:25 +0800691static void rcu_preempt_do_callbacks(void)
692{
693 rcu_do_batch(&rcu_preempt_state, &__get_cpu_var(rcu_preempt_data));
694}
695
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700696#endif /* #ifdef CONFIG_RCU_BOOST */
697
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700698/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800699 * Queue a preemptible-RCU callback for invocation after a grace period.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700700 */
701void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
702{
Paul E. McKenney486e2592012-01-06 14:11:30 -0800703 __call_rcu(head, func, &rcu_preempt_state, 0);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700704}
705EXPORT_SYMBOL_GPL(call_rcu);
706
Paul E. McKenney486e2592012-01-06 14:11:30 -0800707/*
708 * Queue an RCU callback for lazy invocation after a grace period.
709 * This will likely be later named something like "call_rcu_lazy()",
710 * but this change will require some way of tagging the lazy RCU
711 * callbacks in the list of pending callbacks. Until then, this
712 * function may only be called from __kfree_rcu().
713 */
714void kfree_call_rcu(struct rcu_head *head,
715 void (*func)(struct rcu_head *rcu))
716{
717 __call_rcu(head, func, &rcu_preempt_state, 1);
718}
719EXPORT_SYMBOL_GPL(kfree_call_rcu);
720
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800721/**
722 * synchronize_rcu - wait until a grace period has elapsed.
723 *
724 * Control will return to the caller some time after a full grace
725 * period has elapsed, in other words after all currently executing RCU
Paul E. McKenney77d84852010-07-08 17:38:59 -0700726 * read-side critical sections have completed. Note, however, that
727 * upon return from synchronize_rcu(), the caller might well be executing
728 * concurrently with new RCU read-side critical sections that began while
729 * synchronize_rcu() was waiting. RCU read-side critical sections are
730 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800731 */
732void synchronize_rcu(void)
733{
Paul E. McKenneyfe15d702012-01-04 13:30:33 -0800734 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
735 !lock_is_held(&rcu_lock_map) &&
736 !lock_is_held(&rcu_sched_lock_map),
737 "Illegal synchronize_rcu() in RCU read-side critical section");
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800738 if (!rcu_scheduler_active)
739 return;
Paul E. McKenney2c428182011-05-26 22:14:36 -0700740 wait_rcu_gp(call_rcu);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800741}
742EXPORT_SYMBOL_GPL(synchronize_rcu);
743
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800744static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
745static long sync_rcu_preempt_exp_count;
746static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
747
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700748/*
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800749 * Return non-zero if there are any tasks in RCU read-side critical
750 * sections blocking the current preemptible-RCU expedited grace period.
751 * If there is no preemptible-RCU expedited grace period currently in
752 * progress, returns zero unconditionally.
753 */
754static int rcu_preempted_readers_exp(struct rcu_node *rnp)
755{
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800756 return rnp->exp_tasks != NULL;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800757}
758
759/*
760 * return non-zero if there is no RCU expedited grace period in progress
761 * for the specified rcu_node structure, in other words, if all CPUs and
762 * tasks covered by the specified rcu_node structure have done their bit
763 * for the current expedited grace period. Works only for preemptible
764 * RCU -- other RCU implementation use other means.
765 *
766 * Caller must hold sync_rcu_preempt_exp_mutex.
767 */
768static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
769{
770 return !rcu_preempted_readers_exp(rnp) &&
771 ACCESS_ONCE(rnp->expmask) == 0;
772}
773
774/*
775 * Report the exit from RCU read-side critical section for the last task
776 * that queued itself during or before the current expedited preemptible-RCU
777 * grace period. This event is reported either to the rcu_node structure on
778 * which the task was queued or to one of that rcu_node structure's ancestors,
779 * recursively up the tree. (Calm down, calm down, we do the recursion
780 * iteratively!)
781 *
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700782 * Most callers will set the "wake" flag, but the task initiating the
783 * expedited grace period need not wake itself.
784 *
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800785 * Caller must hold sync_rcu_preempt_exp_mutex.
786 */
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700787static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
788 bool wake)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800789{
790 unsigned long flags;
791 unsigned long mask;
792
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800793 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800794 for (;;) {
Paul E. McKenney131906b2011-07-17 02:05:49 -0700795 if (!sync_rcu_preempt_exp_done(rnp)) {
796 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800797 break;
Paul E. McKenney131906b2011-07-17 02:05:49 -0700798 }
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800799 if (rnp->parent == NULL) {
Paul E. McKenney131906b2011-07-17 02:05:49 -0700800 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700801 if (wake)
802 wake_up(&sync_rcu_preempt_exp_wq);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800803 break;
804 }
805 mask = rnp->grpmask;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800806 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800807 rnp = rnp->parent;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800808 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800809 rnp->expmask &= ~mask;
810 }
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800811}
812
813/*
814 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
815 * grace period for the specified rcu_node structure. If there are no such
816 * tasks, report it up the rcu_node hierarchy.
817 *
818 * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
819 */
820static void
821sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
822{
Paul E. McKenney1217ed12011-05-04 21:43:49 -0700823 unsigned long flags;
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800824 int must_wait = 0;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800825
Paul E. McKenney1217ed12011-05-04 21:43:49 -0700826 raw_spin_lock_irqsave(&rnp->lock, flags);
827 if (list_empty(&rnp->blkd_tasks))
828 raw_spin_unlock_irqrestore(&rnp->lock, flags);
829 else {
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800830 rnp->exp_tasks = rnp->blkd_tasks.next;
Paul E. McKenney1217ed12011-05-04 21:43:49 -0700831 rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800832 must_wait = 1;
833 }
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800834 if (!must_wait)
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700835 rcu_report_exp_rnp(rsp, rnp, false); /* Don't wake self. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800836}
837
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800838/**
839 * synchronize_rcu_expedited - Brute-force RCU grace period
840 *
841 * Wait for an RCU-preempt grace period, but expedite it. The basic
842 * idea is to invoke synchronize_sched_expedited() to push all the tasks to
843 * the ->blkd_tasks lists and wait for this list to drain. This consumes
844 * significant time on all CPUs and is unfriendly to real-time workloads,
845 * so is thus not recommended for any sort of common-case code.
846 * In fact, if you are using synchronize_rcu_expedited() in a loop,
847 * please restructure your code to batch your updates, and then Use a
848 * single synchronize_rcu() instead.
849 *
850 * Note that it is illegal to call this function while holding any lock
851 * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
852 * to call this function from a CPU-hotplug notifier. Failing to observe
853 * these restriction will result in deadlock.
Paul E. McKenney019129d52009-10-14 10:15:56 -0700854 */
855void synchronize_rcu_expedited(void)
856{
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800857 unsigned long flags;
858 struct rcu_node *rnp;
859 struct rcu_state *rsp = &rcu_preempt_state;
860 long snap;
861 int trycount = 0;
862
863 smp_mb(); /* Caller's modifications seen first by other CPUs. */
864 snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
865 smp_mb(); /* Above access cannot bleed into critical section. */
866
867 /*
868 * Acquire lock, falling back to synchronize_rcu() if too many
869 * lock-acquisition failures. Of course, if someone does the
870 * expedited grace period for us, just leave.
871 */
872 while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
873 if (trycount++ < 10)
874 udelay(trycount * num_online_cpus());
875 else {
876 synchronize_rcu();
877 return;
878 }
879 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
880 goto mb_ret; /* Others did our work for us. */
881 }
882 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
883 goto unlock_mb_ret; /* Others did our work for us. */
884
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800885 /* force all RCU readers onto ->blkd_tasks lists. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800886 synchronize_sched_expedited();
887
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800888 raw_spin_lock_irqsave(&rsp->onofflock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800889
890 /* Initialize ->expmask for all non-leaf rcu_node structures. */
891 rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800892 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800893 rnp->expmask = rnp->qsmaskinit;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800894 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800895 }
896
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800897 /* Snapshot current state of ->blkd_tasks lists. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800898 rcu_for_each_leaf_node(rsp, rnp)
899 sync_rcu_preempt_exp_init(rsp, rnp);
900 if (NUM_RCU_NODES > 1)
901 sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
902
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800903 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800904
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800905 /* Wait for snapshotted ->blkd_tasks lists to drain. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800906 rnp = rcu_get_root(rsp);
907 wait_event(sync_rcu_preempt_exp_wq,
908 sync_rcu_preempt_exp_done(rnp));
909
910 /* Clean up and exit. */
911 smp_mb(); /* ensure expedited GP seen before counter increment. */
912 ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
913unlock_mb_ret:
914 mutex_unlock(&sync_rcu_preempt_exp_mutex);
915mb_ret:
916 smp_mb(); /* ensure subsequent action seen after grace period. */
Paul E. McKenney019129d52009-10-14 10:15:56 -0700917}
918EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
919
920/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800921 * Check to see if there is any immediate preemptible-RCU-related work
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700922 * to be done.
923 */
924static int rcu_preempt_pending(int cpu)
925{
926 return __rcu_pending(&rcu_preempt_state,
927 &per_cpu(rcu_preempt_data, cpu));
928}
929
930/*
Paul E. McKenney30fbcc92012-01-12 11:01:14 -0800931 * Does preemptible RCU have callbacks on this CPU?
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700932 */
Paul E. McKenney30fbcc92012-01-12 11:01:14 -0800933static int rcu_preempt_cpu_has_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700934{
935 return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
936}
937
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700938/**
939 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
940 */
941void rcu_barrier(void)
942{
943 _rcu_barrier(&rcu_preempt_state, call_rcu);
944}
945EXPORT_SYMBOL_GPL(rcu_barrier);
946
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700947/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800948 * Initialize preemptible RCU's per-CPU data.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700949 */
950static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
951{
952 rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
953}
954
955/*
Paul E. McKenneye5601402012-01-07 11:03:57 -0800956 * Move preemptible RCU's callbacks from dying CPU to other online CPU
957 * and record a quiescent state.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700958 */
Paul E. McKenneye5601402012-01-07 11:03:57 -0800959static void rcu_preempt_cleanup_dying_cpu(void)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700960{
Paul E. McKenneye5601402012-01-07 11:03:57 -0800961 rcu_cleanup_dying_cpu(&rcu_preempt_state);
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700962}
963
964/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800965 * Initialize preemptible RCU's state structures.
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700966 */
967static void __init __rcu_init_preempt(void)
968{
Lai Jiangshan394f99a2010-06-28 16:25:04 +0800969 rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700970}
971
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700972#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
973
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800974static struct rcu_state *rcu_state = &rcu_sched_state;
975
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700976/*
977 * Tell them what RCU they are running.
978 */
Paul E. McKenney0e0fc1c2009-11-11 11:28:06 -0800979static void __init rcu_bootup_announce(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700980{
981 printk(KERN_INFO "Hierarchical RCU implementation.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -0700982 rcu_bootup_announce_oddness();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700983}
984
985/*
986 * Return the number of RCU batches processed thus far for debug & stats.
987 */
988long rcu_batches_completed(void)
989{
990 return rcu_batches_completed_sched();
991}
992EXPORT_SYMBOL_GPL(rcu_batches_completed);
993
994/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800995 * Force a quiescent state for RCU, which, because there is no preemptible
996 * RCU, becomes the same as rcu-sched.
997 */
998void rcu_force_quiescent_state(void)
999{
1000 rcu_sched_force_quiescent_state();
1001}
1002EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
1003
1004/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001005 * Because preemptible RCU does not exist, there are never any preempted
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -07001006 * RCU readers.
1007 */
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001008static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
Paul E. McKenneyfc2219d42009-09-23 09:50:41 -07001009{
1010 return 0;
1011}
1012
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -08001013#ifdef CONFIG_HOTPLUG_CPU
1014
1015/* Because preemptible RCU does not exist, no quieting of tasks. */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001016static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -08001017{
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001018 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -08001019}
1020
1021#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1022
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001023/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001024 * Because preemptible RCU does not exist, we never have to check for
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001025 * tasks blocked within RCU read-side critical sections.
1026 */
Paul E. McKenney1ed509a2010-02-22 17:05:05 -08001027static void rcu_print_detail_task_stall(struct rcu_state *rsp)
1028{
1029}
1030
1031/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001032 * Because preemptible RCU does not exist, we never have to check for
Paul E. McKenney1ed509a2010-02-22 17:05:05 -08001033 * tasks blocked within RCU read-side critical sections.
1034 */
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001035static int rcu_print_task_stall(struct rcu_node *rnp)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001036{
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001037 return 0;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001038}
1039
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001040/*
1041 * Because preemptible RCU does not exist, there is no need to suppress
1042 * its CPU stall warnings.
1043 */
1044static void rcu_preempt_stall_reset(void)
1045{
1046}
1047
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001048/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001049 * Because there is no preemptible RCU, there can be no readers blocked,
Paul E. McKenney49e29122009-09-18 09:50:19 -07001050 * so there is no need to check for blocked tasks. So check only for
1051 * bogus qsmask values.
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -07001052 */
1053static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
1054{
Paul E. McKenney49e29122009-09-18 09:50:19 -07001055 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -07001056}
1057
Paul E. McKenney33f76142009-08-24 09:42:01 -07001058#ifdef CONFIG_HOTPLUG_CPU
1059
1060/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001061 * Because preemptible RCU does not exist, it never needs to migrate
Paul E. McKenney237c80c2009-10-15 09:26:14 -07001062 * tasks that were blocked within RCU read-side critical sections, and
1063 * such non-existent tasks cannot possibly have been blocking the current
1064 * grace period.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001065 */
Paul E. McKenney237c80c2009-10-15 09:26:14 -07001066static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
1067 struct rcu_node *rnp,
1068 struct rcu_data *rdp)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001069{
Paul E. McKenney237c80c2009-10-15 09:26:14 -07001070 return 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001071}
1072
Paul E. McKenneye5601402012-01-07 11:03:57 -08001073#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1074
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001075/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001076 * Because preemptible RCU does not exist, it never needs CPU-offline
Paul E. McKenney33f76142009-08-24 09:42:01 -07001077 * processing.
1078 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001079static void rcu_preempt_cleanup_dead_cpu(int cpu)
Paul E. McKenney33f76142009-08-24 09:42:01 -07001080{
1081}
1082
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001083/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001084 * Because preemptible RCU does not exist, it never has any callbacks
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001085 * to check.
1086 */
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001087static void rcu_preempt_check_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001088{
1089}
1090
1091/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001092 * Because preemptible RCU does not exist, it never has any callbacks
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001093 * to process.
1094 */
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001095static void rcu_preempt_process_callbacks(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001096{
1097}
1098
1099/*
Paul E. McKenney486e2592012-01-06 14:11:30 -08001100 * Queue an RCU callback for lazy invocation after a grace period.
1101 * This will likely be later named something like "call_rcu_lazy()",
1102 * but this change will require some way of tagging the lazy RCU
1103 * callbacks in the list of pending callbacks. Until then, this
1104 * function may only be called from __kfree_rcu().
1105 *
1106 * Because there is no preemptible RCU, we use RCU-sched instead.
1107 */
1108void kfree_call_rcu(struct rcu_head *head,
1109 void (*func)(struct rcu_head *rcu))
1110{
1111 __call_rcu(head, func, &rcu_sched_state, 1);
1112}
1113EXPORT_SYMBOL_GPL(kfree_call_rcu);
1114
1115/*
Paul E. McKenney019129d52009-10-14 10:15:56 -07001116 * Wait for an rcu-preempt grace period, but make it happen quickly.
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001117 * But because preemptible RCU does not exist, map to rcu-sched.
Paul E. McKenney019129d52009-10-14 10:15:56 -07001118 */
1119void synchronize_rcu_expedited(void)
1120{
1121 synchronize_sched_expedited();
1122}
1123EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
1124
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001125#ifdef CONFIG_HOTPLUG_CPU
1126
1127/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001128 * Because preemptible RCU does not exist, there is never any need to
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001129 * report on tasks preempted in RCU read-side critical sections during
1130 * expedited RCU grace periods.
1131 */
Thomas Gleixnerb40d2932011-10-22 07:12:34 -07001132static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
1133 bool wake)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001134{
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001135}
1136
1137#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1138
Paul E. McKenney019129d52009-10-14 10:15:56 -07001139/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001140 * Because preemptible RCU does not exist, it never has any work to do.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001141 */
1142static int rcu_preempt_pending(int cpu)
1143{
1144 return 0;
1145}
1146
1147/*
Paul E. McKenney30fbcc92012-01-12 11:01:14 -08001148 * Because preemptible RCU does not exist, it never has callbacks
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001149 */
Paul E. McKenney30fbcc92012-01-12 11:01:14 -08001150static int rcu_preempt_cpu_has_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001151{
1152 return 0;
1153}
1154
1155/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001156 * Because preemptible RCU does not exist, rcu_barrier() is just
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001157 * another name for rcu_barrier_sched().
1158 */
1159void rcu_barrier(void)
1160{
1161 rcu_barrier_sched();
1162}
1163EXPORT_SYMBOL_GPL(rcu_barrier);
1164
1165/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001166 * Because preemptible RCU does not exist, there is no per-CPU
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001167 * data to initialize.
1168 */
1169static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
1170{
1171}
1172
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001173/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08001174 * Because there is no preemptible RCU, there is no cleanup to do.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001175 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001176static void rcu_preempt_cleanup_dying_cpu(void)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001177{
1178}
1179
1180/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001181 * Because preemptible RCU does not exist, it need not be initialized.
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001182 */
1183static void __init __rcu_init_preempt(void)
1184{
1185}
1186
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001187#endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001188
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001189#ifdef CONFIG_RCU_BOOST
1190
1191#include "rtmutex_common.h"
1192
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001193#ifdef CONFIG_RCU_TRACE
1194
1195static void rcu_initiate_boost_trace(struct rcu_node *rnp)
1196{
1197 if (list_empty(&rnp->blkd_tasks))
1198 rnp->n_balk_blkd_tasks++;
1199 else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
1200 rnp->n_balk_exp_gp_tasks++;
1201 else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
1202 rnp->n_balk_boost_tasks++;
1203 else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
1204 rnp->n_balk_notblocked++;
1205 else if (rnp->gp_tasks != NULL &&
Paul E. McKenneya9f47932011-05-02 03:46:10 -07001206 ULONG_CMP_LT(jiffies, rnp->boost_time))
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001207 rnp->n_balk_notyet++;
1208 else
1209 rnp->n_balk_nos++;
1210}
1211
1212#else /* #ifdef CONFIG_RCU_TRACE */
1213
1214static void rcu_initiate_boost_trace(struct rcu_node *rnp)
1215{
1216}
1217
1218#endif /* #else #ifdef CONFIG_RCU_TRACE */
1219
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001220/*
1221 * Carry out RCU priority boosting on the task indicated by ->exp_tasks
1222 * or ->boost_tasks, advancing the pointer to the next task in the
1223 * ->blkd_tasks list.
1224 *
1225 * Note that irqs must be enabled: boosting the task can block.
1226 * Returns 1 if there are more tasks needing to be boosted.
1227 */
1228static int rcu_boost(struct rcu_node *rnp)
1229{
1230 unsigned long flags;
1231 struct rt_mutex mtx;
1232 struct task_struct *t;
1233 struct list_head *tb;
1234
1235 if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL)
1236 return 0; /* Nothing left to boost. */
1237
1238 raw_spin_lock_irqsave(&rnp->lock, flags);
1239
1240 /*
1241 * Recheck under the lock: all tasks in need of boosting
1242 * might exit their RCU read-side critical sections on their own.
1243 */
1244 if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
1245 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1246 return 0;
1247 }
1248
1249 /*
1250 * Preferentially boost tasks blocking expedited grace periods.
1251 * This cannot starve the normal grace periods because a second
1252 * expedited grace period must boost all blocked tasks, including
1253 * those blocking the pre-existing normal grace period.
1254 */
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001255 if (rnp->exp_tasks != NULL) {
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001256 tb = rnp->exp_tasks;
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001257 rnp->n_exp_boosts++;
1258 } else {
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001259 tb = rnp->boost_tasks;
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001260 rnp->n_normal_boosts++;
1261 }
1262 rnp->n_tasks_boosted++;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001263
1264 /*
1265 * We boost task t by manufacturing an rt_mutex that appears to
1266 * be held by task t. We leave a pointer to that rt_mutex where
1267 * task t can find it, and task t will release the mutex when it
1268 * exits its outermost RCU read-side critical section. Then
1269 * simply acquiring this artificial rt_mutex will boost task
1270 * t's priority. (Thanks to tglx for suggesting this approach!)
1271 *
1272 * Note that task t must acquire rnp->lock to remove itself from
1273 * the ->blkd_tasks list, which it will do from exit() if from
1274 * nowhere else. We therefore are guaranteed that task t will
1275 * stay around at least until we drop rnp->lock. Note that
1276 * rnp->lock also resolves races between our priority boosting
1277 * and task t's exiting its outermost RCU read-side critical
1278 * section.
1279 */
1280 t = container_of(tb, struct task_struct, rcu_node_entry);
1281 rt_mutex_init_proxy_locked(&mtx, t);
1282 t->rcu_boost_mutex = &mtx;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001283 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1284 rt_mutex_lock(&mtx); /* Side effect: boosts task t's priority. */
1285 rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
1286
Paul E. McKenney4f89b332011-12-09 14:43:47 -08001287 return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
1288 ACCESS_ONCE(rnp->boost_tasks) != NULL;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001289}
1290
1291/*
1292 * Timer handler to initiate waking up of boost kthreads that
1293 * have yielded the CPU due to excessive numbers of tasks to
1294 * boost. We wake up the per-rcu_node kthread, which in turn
1295 * will wake up the booster kthread.
1296 */
1297static void rcu_boost_kthread_timer(unsigned long arg)
1298{
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001299 invoke_rcu_node_kthread((struct rcu_node *)arg);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001300}
1301
1302/*
1303 * Priority-boosting kthread. One per leaf rcu_node and one for the
1304 * root rcu_node.
1305 */
1306static int rcu_boost_kthread(void *arg)
1307{
1308 struct rcu_node *rnp = (struct rcu_node *)arg;
1309 int spincnt = 0;
1310 int more2boost;
1311
Paul E. McKenney385680a2011-06-21 22:43:26 -07001312 trace_rcu_utilization("Start boost kthread@init");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001313 for (;;) {
Paul E. McKenneyd71df902011-03-29 17:48:28 -07001314 rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001315 trace_rcu_utilization("End boost kthread@rcu_wait");
Peter Zijlstra08bca602011-05-20 16:06:29 -07001316 rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
Paul E. McKenney385680a2011-06-21 22:43:26 -07001317 trace_rcu_utilization("Start boost kthread@rcu_wait");
Paul E. McKenneyd71df902011-03-29 17:48:28 -07001318 rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001319 more2boost = rcu_boost(rnp);
1320 if (more2boost)
1321 spincnt++;
1322 else
1323 spincnt = 0;
1324 if (spincnt > 10) {
Paul E. McKenney385680a2011-06-21 22:43:26 -07001325 trace_rcu_utilization("End boost kthread@rcu_yield");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001326 rcu_yield(rcu_boost_kthread_timer, (unsigned long)rnp);
Paul E. McKenney385680a2011-06-21 22:43:26 -07001327 trace_rcu_utilization("Start boost kthread@rcu_yield");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001328 spincnt = 0;
1329 }
1330 }
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001331 /* NOTREACHED */
Paul E. McKenney385680a2011-06-21 22:43:26 -07001332 trace_rcu_utilization("End boost kthread@notreached");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001333 return 0;
1334}
1335
1336/*
1337 * Check to see if it is time to start boosting RCU readers that are
1338 * blocking the current grace period, and, if so, tell the per-rcu_node
1339 * kthread to start boosting them. If there is an expedited grace
1340 * period in progress, it is always time to boost.
1341 *
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001342 * The caller must hold rnp->lock, which this function releases,
1343 * but irqs remain disabled. The ->boost_kthread_task is immortal,
1344 * so we don't need to worry about it going away.
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001345 */
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001346static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001347{
1348 struct task_struct *t;
1349
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001350 if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
1351 rnp->n_balk_exp_gp_tasks++;
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001352 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001353 return;
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001354 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001355 if (rnp->exp_tasks != NULL ||
1356 (rnp->gp_tasks != NULL &&
1357 rnp->boost_tasks == NULL &&
1358 rnp->qsmask == 0 &&
1359 ULONG_CMP_GE(jiffies, rnp->boost_time))) {
1360 if (rnp->exp_tasks == NULL)
1361 rnp->boost_tasks = rnp->gp_tasks;
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001362 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001363 t = rnp->boost_kthread_task;
1364 if (t != NULL)
1365 wake_up_process(t);
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001366 } else {
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001367 rcu_initiate_boost_trace(rnp);
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001368 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1369 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001370}
1371
Paul E. McKenney0f962a52011-04-14 12:13:53 -07001372/*
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001373 * Wake up the per-CPU kthread to invoke RCU callbacks.
1374 */
1375static void invoke_rcu_callbacks_kthread(void)
1376{
1377 unsigned long flags;
1378
1379 local_irq_save(flags);
1380 __this_cpu_write(rcu_cpu_has_work, 1);
Shaohua Li1eb52122011-06-16 16:02:54 -07001381 if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
1382 current != __this_cpu_read(rcu_cpu_kthread_task))
1383 wake_up_process(__this_cpu_read(rcu_cpu_kthread_task));
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001384 local_irq_restore(flags);
1385}
1386
1387/*
Paul E. McKenneydff16722011-11-29 15:57:13 -08001388 * Is the current CPU running the RCU-callbacks kthread?
1389 * Caller must have preemption disabled.
1390 */
1391static bool rcu_is_callbacks_kthread(void)
1392{
1393 return __get_cpu_var(rcu_cpu_kthread_task) == current;
1394}
1395
1396/*
Paul E. McKenney0f962a52011-04-14 12:13:53 -07001397 * Set the affinity of the boost kthread. The CPU-hotplug locks are
1398 * held, so no one should be messing with the existence of the boost
1399 * kthread.
1400 */
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001401static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
1402 cpumask_var_t cm)
1403{
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001404 struct task_struct *t;
1405
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001406 t = rnp->boost_kthread_task;
1407 if (t != NULL)
1408 set_cpus_allowed_ptr(rnp->boost_kthread_task, cm);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001409}
1410
1411#define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1412
1413/*
1414 * Do priority-boost accounting for the start of a new grace period.
1415 */
1416static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1417{
1418 rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1419}
1420
1421/*
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001422 * Create an RCU-boost kthread for the specified node if one does not
1423 * already exist. We only create this kthread for preemptible RCU.
1424 * Returns zero if all is well, a negated errno otherwise.
1425 */
1426static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
1427 struct rcu_node *rnp,
1428 int rnp_index)
1429{
1430 unsigned long flags;
1431 struct sched_param sp;
1432 struct task_struct *t;
1433
1434 if (&rcu_preempt_state != rsp)
1435 return 0;
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001436 rsp->boost = 1;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001437 if (rnp->boost_kthread_task != NULL)
1438 return 0;
1439 t = kthread_create(rcu_boost_kthread, (void *)rnp,
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001440 "rcub/%d", rnp_index);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001441 if (IS_ERR(t))
1442 return PTR_ERR(t);
1443 raw_spin_lock_irqsave(&rnp->lock, flags);
1444 rnp->boost_kthread_task = t;
1445 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001446 sp.sched_priority = RCU_BOOST_PRIO;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001447 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
Paul E. McKenney9a432732011-05-30 20:38:55 -07001448 wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001449 return 0;
1450}
1451
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001452#ifdef CONFIG_HOTPLUG_CPU
1453
1454/*
1455 * Stop the RCU's per-CPU kthread when its CPU goes offline,.
1456 */
1457static void rcu_stop_cpu_kthread(int cpu)
1458{
1459 struct task_struct *t;
1460
1461 /* Stop the CPU's kthread. */
1462 t = per_cpu(rcu_cpu_kthread_task, cpu);
1463 if (t != NULL) {
1464 per_cpu(rcu_cpu_kthread_task, cpu) = NULL;
1465 kthread_stop(t);
1466 }
1467}
1468
1469#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1470
1471static void rcu_kthread_do_work(void)
1472{
1473 rcu_do_batch(&rcu_sched_state, &__get_cpu_var(rcu_sched_data));
1474 rcu_do_batch(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
1475 rcu_preempt_do_callbacks();
1476}
1477
1478/*
1479 * Wake up the specified per-rcu_node-structure kthread.
1480 * Because the per-rcu_node kthreads are immortal, we don't need
1481 * to do anything to keep them alive.
1482 */
1483static void invoke_rcu_node_kthread(struct rcu_node *rnp)
1484{
1485 struct task_struct *t;
1486
1487 t = rnp->node_kthread_task;
1488 if (t != NULL)
1489 wake_up_process(t);
1490}
1491
1492/*
1493 * Set the specified CPU's kthread to run RT or not, as specified by
1494 * the to_rt argument. The CPU-hotplug locks are held, so the task
1495 * is not going away.
1496 */
1497static void rcu_cpu_kthread_setrt(int cpu, int to_rt)
1498{
1499 int policy;
1500 struct sched_param sp;
1501 struct task_struct *t;
1502
1503 t = per_cpu(rcu_cpu_kthread_task, cpu);
1504 if (t == NULL)
1505 return;
1506 if (to_rt) {
1507 policy = SCHED_FIFO;
1508 sp.sched_priority = RCU_KTHREAD_PRIO;
1509 } else {
1510 policy = SCHED_NORMAL;
1511 sp.sched_priority = 0;
1512 }
1513 sched_setscheduler_nocheck(t, policy, &sp);
1514}
1515
1516/*
1517 * Timer handler to initiate the waking up of per-CPU kthreads that
1518 * have yielded the CPU due to excess numbers of RCU callbacks.
1519 * We wake up the per-rcu_node kthread, which in turn will wake up
1520 * the booster kthread.
1521 */
1522static void rcu_cpu_kthread_timer(unsigned long arg)
1523{
1524 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, arg);
1525 struct rcu_node *rnp = rdp->mynode;
1526
1527 atomic_or(rdp->grpmask, &rnp->wakemask);
1528 invoke_rcu_node_kthread(rnp);
1529}
1530
1531/*
1532 * Drop to non-real-time priority and yield, but only after posting a
1533 * timer that will cause us to regain our real-time priority if we
1534 * remain preempted. Either way, we restore our real-time priority
1535 * before returning.
1536 */
1537static void rcu_yield(void (*f)(unsigned long), unsigned long arg)
1538{
1539 struct sched_param sp;
1540 struct timer_list yield_timer;
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001541 int prio = current->rt_priority;
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001542
1543 setup_timer_on_stack(&yield_timer, f, arg);
1544 mod_timer(&yield_timer, jiffies + 2);
1545 sp.sched_priority = 0;
1546 sched_setscheduler_nocheck(current, SCHED_NORMAL, &sp);
1547 set_user_nice(current, 19);
1548 schedule();
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001549 set_user_nice(current, 0);
1550 sp.sched_priority = prio;
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001551 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1552 del_timer(&yield_timer);
1553}
1554
1555/*
1556 * Handle cases where the rcu_cpu_kthread() ends up on the wrong CPU.
1557 * This can happen while the corresponding CPU is either coming online
1558 * or going offline. We cannot wait until the CPU is fully online
1559 * before starting the kthread, because the various notifier functions
1560 * can wait for RCU grace periods. So we park rcu_cpu_kthread() until
1561 * the corresponding CPU is online.
1562 *
1563 * Return 1 if the kthread needs to stop, 0 otherwise.
1564 *
1565 * Caller must disable bh. This function can momentarily enable it.
1566 */
1567static int rcu_cpu_kthread_should_stop(int cpu)
1568{
1569 while (cpu_is_offline(cpu) ||
1570 !cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)) ||
1571 smp_processor_id() != cpu) {
1572 if (kthread_should_stop())
1573 return 1;
1574 per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
1575 per_cpu(rcu_cpu_kthread_cpu, cpu) = raw_smp_processor_id();
1576 local_bh_enable();
1577 schedule_timeout_uninterruptible(1);
1578 if (!cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)))
1579 set_cpus_allowed_ptr(current, cpumask_of(cpu));
1580 local_bh_disable();
1581 }
1582 per_cpu(rcu_cpu_kthread_cpu, cpu) = cpu;
1583 return 0;
1584}
1585
1586/*
1587 * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001588 * RCU softirq used in flavors and configurations of RCU that do not
1589 * support RCU priority boosting.
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001590 */
1591static int rcu_cpu_kthread(void *arg)
1592{
1593 int cpu = (int)(long)arg;
1594 unsigned long flags;
1595 int spincnt = 0;
1596 unsigned int *statusp = &per_cpu(rcu_cpu_kthread_status, cpu);
1597 char work;
1598 char *workp = &per_cpu(rcu_cpu_has_work, cpu);
1599
Paul E. McKenney385680a2011-06-21 22:43:26 -07001600 trace_rcu_utilization("Start CPU kthread@init");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001601 for (;;) {
1602 *statusp = RCU_KTHREAD_WAITING;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001603 trace_rcu_utilization("End CPU kthread@rcu_wait");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001604 rcu_wait(*workp != 0 || kthread_should_stop());
Paul E. McKenney385680a2011-06-21 22:43:26 -07001605 trace_rcu_utilization("Start CPU kthread@rcu_wait");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001606 local_bh_disable();
1607 if (rcu_cpu_kthread_should_stop(cpu)) {
1608 local_bh_enable();
1609 break;
1610 }
1611 *statusp = RCU_KTHREAD_RUNNING;
1612 per_cpu(rcu_cpu_kthread_loops, cpu)++;
1613 local_irq_save(flags);
1614 work = *workp;
1615 *workp = 0;
1616 local_irq_restore(flags);
1617 if (work)
1618 rcu_kthread_do_work();
1619 local_bh_enable();
1620 if (*workp != 0)
1621 spincnt++;
1622 else
1623 spincnt = 0;
1624 if (spincnt > 10) {
1625 *statusp = RCU_KTHREAD_YIELDING;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001626 trace_rcu_utilization("End CPU kthread@rcu_yield");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001627 rcu_yield(rcu_cpu_kthread_timer, (unsigned long)cpu);
Paul E. McKenney385680a2011-06-21 22:43:26 -07001628 trace_rcu_utilization("Start CPU kthread@rcu_yield");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001629 spincnt = 0;
1630 }
1631 }
1632 *statusp = RCU_KTHREAD_STOPPED;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001633 trace_rcu_utilization("End CPU kthread@term");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001634 return 0;
1635}
1636
1637/*
1638 * Spawn a per-CPU kthread, setting up affinity and priority.
1639 * Because the CPU hotplug lock is held, no other CPU will be attempting
1640 * to manipulate rcu_cpu_kthread_task. There might be another CPU
1641 * attempting to access it during boot, but the locking in kthread_bind()
1642 * will enforce sufficient ordering.
1643 *
1644 * Please note that we cannot simply refuse to wake up the per-CPU
1645 * kthread because kthreads are created in TASK_UNINTERRUPTIBLE state,
1646 * which can result in softlockup complaints if the task ends up being
1647 * idle for more than a couple of minutes.
1648 *
1649 * However, please note also that we cannot bind the per-CPU kthread to its
1650 * CPU until that CPU is fully online. We also cannot wait until the
1651 * CPU is fully online before we create its per-CPU kthread, as this would
1652 * deadlock the system when CPU notifiers tried waiting for grace
1653 * periods. So we bind the per-CPU kthread to its CPU only if the CPU
1654 * is online. If its CPU is not yet fully online, then the code in
1655 * rcu_cpu_kthread() will wait until it is fully online, and then do
1656 * the binding.
1657 */
1658static int __cpuinit rcu_spawn_one_cpu_kthread(int cpu)
1659{
1660 struct sched_param sp;
1661 struct task_struct *t;
1662
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001663 if (!rcu_scheduler_fully_active ||
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001664 per_cpu(rcu_cpu_kthread_task, cpu) != NULL)
1665 return 0;
Eric Dumazet1f288092011-06-16 15:53:18 -07001666 t = kthread_create_on_node(rcu_cpu_kthread,
1667 (void *)(long)cpu,
1668 cpu_to_node(cpu),
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001669 "rcuc/%d", cpu);
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001670 if (IS_ERR(t))
1671 return PTR_ERR(t);
1672 if (cpu_online(cpu))
1673 kthread_bind(t, cpu);
1674 per_cpu(rcu_cpu_kthread_cpu, cpu) = cpu;
1675 WARN_ON_ONCE(per_cpu(rcu_cpu_kthread_task, cpu) != NULL);
1676 sp.sched_priority = RCU_KTHREAD_PRIO;
1677 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1678 per_cpu(rcu_cpu_kthread_task, cpu) = t;
1679 wake_up_process(t); /* Get to TASK_INTERRUPTIBLE quickly. */
1680 return 0;
1681}
1682
1683/*
1684 * Per-rcu_node kthread, which is in charge of waking up the per-CPU
1685 * kthreads when needed. We ignore requests to wake up kthreads
1686 * for offline CPUs, which is OK because force_quiescent_state()
1687 * takes care of this case.
1688 */
1689static int rcu_node_kthread(void *arg)
1690{
1691 int cpu;
1692 unsigned long flags;
1693 unsigned long mask;
1694 struct rcu_node *rnp = (struct rcu_node *)arg;
1695 struct sched_param sp;
1696 struct task_struct *t;
1697
1698 for (;;) {
1699 rnp->node_kthread_status = RCU_KTHREAD_WAITING;
1700 rcu_wait(atomic_read(&rnp->wakemask) != 0);
1701 rnp->node_kthread_status = RCU_KTHREAD_RUNNING;
1702 raw_spin_lock_irqsave(&rnp->lock, flags);
1703 mask = atomic_xchg(&rnp->wakemask, 0);
1704 rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1705 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1) {
1706 if ((mask & 0x1) == 0)
1707 continue;
1708 preempt_disable();
1709 t = per_cpu(rcu_cpu_kthread_task, cpu);
1710 if (!cpu_online(cpu) || t == NULL) {
1711 preempt_enable();
1712 continue;
1713 }
1714 per_cpu(rcu_cpu_has_work, cpu) = 1;
1715 sp.sched_priority = RCU_KTHREAD_PRIO;
1716 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1717 preempt_enable();
1718 }
1719 }
1720 /* NOTREACHED */
1721 rnp->node_kthread_status = RCU_KTHREAD_STOPPED;
1722 return 0;
1723}
1724
1725/*
1726 * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1727 * served by the rcu_node in question. The CPU hotplug lock is still
1728 * held, so the value of rnp->qsmaskinit will be stable.
1729 *
1730 * We don't include outgoingcpu in the affinity set, use -1 if there is
1731 * no outgoing CPU. If there are no CPUs left in the affinity set,
1732 * this function allows the kthread to execute on any CPU.
1733 */
1734static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1735{
1736 cpumask_var_t cm;
1737 int cpu;
1738 unsigned long mask = rnp->qsmaskinit;
1739
1740 if (rnp->node_kthread_task == NULL)
1741 return;
1742 if (!alloc_cpumask_var(&cm, GFP_KERNEL))
1743 return;
1744 cpumask_clear(cm);
1745 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
1746 if ((mask & 0x1) && cpu != outgoingcpu)
1747 cpumask_set_cpu(cpu, cm);
1748 if (cpumask_weight(cm) == 0) {
1749 cpumask_setall(cm);
1750 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++)
1751 cpumask_clear_cpu(cpu, cm);
1752 WARN_ON_ONCE(cpumask_weight(cm) == 0);
1753 }
1754 set_cpus_allowed_ptr(rnp->node_kthread_task, cm);
1755 rcu_boost_kthread_setaffinity(rnp, cm);
1756 free_cpumask_var(cm);
1757}
1758
1759/*
1760 * Spawn a per-rcu_node kthread, setting priority and affinity.
1761 * Called during boot before online/offline can happen, or, if
1762 * during runtime, with the main CPU-hotplug locks held. So only
1763 * one of these can be executing at a time.
1764 */
1765static int __cpuinit rcu_spawn_one_node_kthread(struct rcu_state *rsp,
1766 struct rcu_node *rnp)
1767{
1768 unsigned long flags;
1769 int rnp_index = rnp - &rsp->node[0];
1770 struct sched_param sp;
1771 struct task_struct *t;
1772
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001773 if (!rcu_scheduler_fully_active ||
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001774 rnp->qsmaskinit == 0)
1775 return 0;
1776 if (rnp->node_kthread_task == NULL) {
1777 t = kthread_create(rcu_node_kthread, (void *)rnp,
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001778 "rcun/%d", rnp_index);
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001779 if (IS_ERR(t))
1780 return PTR_ERR(t);
1781 raw_spin_lock_irqsave(&rnp->lock, flags);
1782 rnp->node_kthread_task = t;
1783 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1784 sp.sched_priority = 99;
1785 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1786 wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1787 }
1788 return rcu_spawn_one_boost_kthread(rsp, rnp, rnp_index);
1789}
1790
1791/*
1792 * Spawn all kthreads -- called as soon as the scheduler is running.
1793 */
1794static int __init rcu_spawn_kthreads(void)
1795{
1796 int cpu;
1797 struct rcu_node *rnp;
1798
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001799 rcu_scheduler_fully_active = 1;
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001800 for_each_possible_cpu(cpu) {
1801 per_cpu(rcu_cpu_has_work, cpu) = 0;
1802 if (cpu_online(cpu))
1803 (void)rcu_spawn_one_cpu_kthread(cpu);
1804 }
1805 rnp = rcu_get_root(rcu_state);
1806 (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1807 if (NUM_RCU_NODES > 1) {
1808 rcu_for_each_leaf_node(rcu_state, rnp)
1809 (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1810 }
1811 return 0;
1812}
1813early_initcall(rcu_spawn_kthreads);
1814
1815static void __cpuinit rcu_prepare_kthreads(int cpu)
1816{
1817 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
1818 struct rcu_node *rnp = rdp->mynode;
1819
1820 /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001821 if (rcu_scheduler_fully_active) {
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001822 (void)rcu_spawn_one_cpu_kthread(cpu);
1823 if (rnp->node_kthread_task == NULL)
1824 (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1825 }
1826}
1827
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001828#else /* #ifdef CONFIG_RCU_BOOST */
1829
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001830static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001831{
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001832 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001833}
1834
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001835static void invoke_rcu_callbacks_kthread(void)
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001836{
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001837 WARN_ON_ONCE(1);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001838}
1839
Paul E. McKenneydff16722011-11-29 15:57:13 -08001840static bool rcu_is_callbacks_kthread(void)
1841{
1842 return false;
1843}
1844
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001845static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1846{
1847}
1848
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001849#ifdef CONFIG_HOTPLUG_CPU
1850
1851static void rcu_stop_cpu_kthread(int cpu)
1852{
1853}
1854
1855#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1856
1857static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1858{
1859}
1860
1861static void rcu_cpu_kthread_setrt(int cpu, int to_rt)
1862{
1863}
1864
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001865static int __init rcu_scheduler_really_started(void)
1866{
1867 rcu_scheduler_fully_active = 1;
1868 return 0;
1869}
1870early_initcall(rcu_scheduler_really_started);
1871
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001872static void __cpuinit rcu_prepare_kthreads(int cpu)
1873{
1874}
1875
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001876#endif /* #else #ifdef CONFIG_RCU_BOOST */
1877
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001878#if !defined(CONFIG_RCU_FAST_NO_HZ)
1879
1880/*
1881 * Check to see if any future RCU-related work will need to be done
1882 * by the current CPU, even if none need be done immediately, returning
1883 * 1 if so. This function is part of the RCU implementation; it is -not-
1884 * an exported member of the RCU API.
1885 *
Paul E. McKenney7cb92492011-11-28 12:28:34 -08001886 * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
1887 * any flavor of RCU.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001888 */
Paul E. McKenneyaa9b16302012-05-10 16:41:44 -07001889int rcu_needs_cpu(int cpu, unsigned long *delta_jiffies)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001890{
Paul E. McKenneyaa9b16302012-05-10 16:41:44 -07001891 *delta_jiffies = ULONG_MAX;
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07001892 return rcu_cpu_has_callbacks(cpu);
1893}
1894
1895/*
Paul E. McKenney7cb92492011-11-28 12:28:34 -08001896 * Because we do not have RCU_FAST_NO_HZ, don't bother initializing for it.
1897 */
1898static void rcu_prepare_for_idle_init(int cpu)
1899{
1900}
1901
1902/*
1903 * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1904 * after it.
1905 */
1906static void rcu_cleanup_after_idle(int cpu)
1907{
1908}
1909
1910/*
Paul E. McKenneya858af22012-01-16 13:29:10 -08001911 * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07001912 * is nothing.
1913 */
1914static void rcu_prepare_for_idle(int cpu)
1915{
1916}
1917
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08001918/*
1919 * Don't bother keeping a running count of the number of RCU callbacks
1920 * posted because CONFIG_RCU_FAST_NO_HZ=n.
1921 */
1922static void rcu_idle_count_callbacks_posted(void)
1923{
1924}
1925
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001926#else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1927
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08001928/*
1929 * This code is invoked when a CPU goes idle, at which point we want
1930 * to have the CPU do everything required for RCU so that it can enter
1931 * the energy-efficient dyntick-idle mode. This is handled by a
1932 * state machine implemented by rcu_prepare_for_idle() below.
1933 *
1934 * The following three proprocessor symbols control this state machine:
1935 *
1936 * RCU_IDLE_FLUSHES gives the maximum number of times that we will attempt
1937 * to satisfy RCU. Beyond this point, it is better to incur a periodic
1938 * scheduling-clock interrupt than to loop through the state machine
1939 * at full power.
1940 * RCU_IDLE_OPT_FLUSHES gives the number of RCU_IDLE_FLUSHES that are
1941 * optional if RCU does not need anything immediately from this
1942 * CPU, even if this CPU still has RCU callbacks queued. The first
1943 * times through the state machine are mandatory: we need to give
1944 * the state machine a chance to communicate a quiescent state
1945 * to the RCU core.
1946 * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1947 * to sleep in dyntick-idle mode with RCU callbacks pending. This
1948 * is sized to be roughly one RCU grace period. Those energy-efficiency
1949 * benchmarkers who might otherwise be tempted to set this to a large
1950 * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1951 * system. And if you are -that- concerned about energy efficiency,
1952 * just power the system down and be done with it!
Paul E. McKenney778d2502012-01-10 14:13:24 -08001953 * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1954 * permitted to sleep in dyntick-idle mode with only lazy RCU
1955 * callbacks pending. Setting this too high can OOM your system.
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08001956 *
1957 * The values below work well in practice. If future workloads require
1958 * adjustment, they can be converted into kernel config parameters, though
1959 * making the state machine smarter might be a better option.
1960 */
1961#define RCU_IDLE_FLUSHES 5 /* Number of dyntick-idle tries. */
1962#define RCU_IDLE_OPT_FLUSHES 3 /* Optional dyntick-idle tries. */
Paul E. McKenney7cb92492011-11-28 12:28:34 -08001963#define RCU_IDLE_GP_DELAY 6 /* Roughly one grace period. */
Paul E. McKenney778d2502012-01-10 14:13:24 -08001964#define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08001965
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001966/*
Paul E. McKenney486e2592012-01-06 14:11:30 -08001967 * Does the specified flavor of RCU have non-lazy callbacks pending on
1968 * the specified CPU? Both RCU flavor and CPU are specified by the
1969 * rcu_data structure.
1970 */
1971static bool __rcu_cpu_has_nonlazy_callbacks(struct rcu_data *rdp)
1972{
1973 return rdp->qlen != rdp->qlen_lazy;
1974}
1975
1976#ifdef CONFIG_TREE_PREEMPT_RCU
1977
1978/*
1979 * Are there non-lazy RCU-preempt callbacks? (There cannot be if there
1980 * is no RCU-preempt in the kernel.)
1981 */
1982static bool rcu_preempt_cpu_has_nonlazy_callbacks(int cpu)
1983{
1984 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
1985
1986 return __rcu_cpu_has_nonlazy_callbacks(rdp);
1987}
1988
1989#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
1990
1991static bool rcu_preempt_cpu_has_nonlazy_callbacks(int cpu)
1992{
1993 return 0;
1994}
1995
1996#endif /* else #ifdef CONFIG_TREE_PREEMPT_RCU */
1997
1998/*
1999 * Does any flavor of RCU have non-lazy callbacks on the specified CPU?
2000 */
2001static bool rcu_cpu_has_nonlazy_callbacks(int cpu)
2002{
2003 return __rcu_cpu_has_nonlazy_callbacks(&per_cpu(rcu_sched_data, cpu)) ||
2004 __rcu_cpu_has_nonlazy_callbacks(&per_cpu(rcu_bh_data, cpu)) ||
2005 rcu_preempt_cpu_has_nonlazy_callbacks(cpu);
2006}
2007
2008/*
Paul E. McKenneyaa9b16302012-05-10 16:41:44 -07002009 * Allow the CPU to enter dyntick-idle mode if either: (1) There are no
2010 * callbacks on this CPU, (2) this CPU has not yet attempted to enter
2011 * dyntick-idle mode, or (3) this CPU is in the process of attempting to
2012 * enter dyntick-idle mode. Otherwise, if we have recently tried and failed
2013 * to enter dyntick-idle mode, we refuse to try to enter it. After all,
2014 * it is better to incur scheduling-clock interrupts than to spin
2015 * continuously for the same time duration!
2016 *
2017 * The delta_jiffies argument is used to store the time when RCU is
2018 * going to need the CPU again if it still has callbacks. The reason
2019 * for this is that rcu_prepare_for_idle() might need to post a timer,
2020 * but if so, it will do so after tick_nohz_stop_sched_tick() has set
2021 * the wakeup time for this CPU. This means that RCU's timer can be
2022 * delayed until the wakeup time, which defeats the purpose of posting
2023 * a timer.
2024 */
2025int rcu_needs_cpu(int cpu, unsigned long *delta_jiffies)
2026{
2027 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2028
2029 /* Flag a new idle sojourn to the idle-entry state machine. */
2030 rdtp->idle_first_pass = 1;
2031 /* If no callbacks, RCU doesn't need the CPU. */
2032 if (!rcu_cpu_has_callbacks(cpu)) {
2033 *delta_jiffies = ULONG_MAX;
2034 return 0;
2035 }
2036 if (rdtp->dyntick_holdoff == jiffies) {
2037 /* RCU recently tried and failed, so don't try again. */
2038 *delta_jiffies = 1;
2039 return 1;
2040 }
2041 /* Set up for the possibility that RCU will post a timer. */
2042 if (rcu_cpu_has_nonlazy_callbacks(cpu))
2043 *delta_jiffies = RCU_IDLE_GP_DELAY;
2044 else
2045 *delta_jiffies = RCU_IDLE_LAZY_GP_DELAY;
2046 return 0;
2047}
2048
2049/*
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002050 * Handler for smp_call_function_single(). The only point of this
2051 * handler is to wake the CPU up, so the handler does only tracing.
2052 */
2053void rcu_idle_demigrate(void *unused)
2054{
2055 trace_rcu_prep_idle("Demigrate");
2056}
2057
2058/*
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002059 * Timer handler used to force CPU to start pushing its remaining RCU
2060 * callbacks in the case where it entered dyntick-idle mode with callbacks
2061 * pending. The hander doesn't really need to do anything because the
2062 * real work is done upon re-entry to idle, or by the next scheduling-clock
2063 * interrupt should idle not be re-entered.
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002064 *
2065 * One special case: the timer gets migrated without awakening the CPU
2066 * on which the timer was scheduled on. In this case, we must wake up
2067 * that CPU. We do so with smp_call_function_single().
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002068 */
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002069static void rcu_idle_gp_timer_func(unsigned long cpu_in)
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002070{
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002071 int cpu = (int)cpu_in;
2072
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002073 trace_rcu_prep_idle("Timer");
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002074 if (cpu != smp_processor_id())
2075 smp_call_function_single(cpu, rcu_idle_demigrate, NULL, 0);
2076 else
2077 WARN_ON_ONCE(1); /* Getting here can hang the system... */
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002078}
2079
2080/*
2081 * Initialize the timer used to pull CPUs out of dyntick-idle mode.
2082 */
2083static void rcu_prepare_for_idle_init(int cpu)
2084{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002085 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2086
2087 rdtp->dyntick_holdoff = jiffies - 1;
2088 setup_timer(&rdtp->idle_gp_timer, rcu_idle_gp_timer_func, cpu);
2089 rdtp->idle_gp_timer_expires = jiffies - 1;
2090 rdtp->idle_first_pass = 1;
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002091}
2092
2093/*
2094 * Clean up for exit from idle. Because we are exiting from idle, there
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002095 * is no longer any point to ->idle_gp_timer, so cancel it. This will
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002096 * do nothing if this timer is not active, so just cancel it unconditionally.
2097 */
2098static void rcu_cleanup_after_idle(int cpu)
2099{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002100 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2101
2102 del_timer(&rdtp->idle_gp_timer);
Paul E. McKenney2fdbb312012-02-23 15:58:29 -08002103 trace_rcu_prep_idle("Cleanup after idle");
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002104}
2105
2106/*
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002107 * Check to see if any RCU-related work can be done by the current CPU,
2108 * and if so, schedule a softirq to get it done. This function is part
2109 * of the RCU implementation; it is -not- an exported member of the RCU API.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002110 *
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002111 * The idea is for the current CPU to clear out all work required by the
2112 * RCU core for the current grace period, so that this CPU can be permitted
2113 * to enter dyntick-idle mode. In some cases, it will need to be awakened
2114 * at the end of the grace period by whatever CPU ends the grace period.
2115 * This allows CPUs to go dyntick-idle more quickly, and to reduce the
2116 * number of wakeups by a modest integer factor.
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002117 *
2118 * Because it is not legal to invoke rcu_process_callbacks() with irqs
2119 * disabled, we do one pass of force_quiescent_state(), then do a
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002120 * invoke_rcu_core() to cause rcu_process_callbacks() to be invoked
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002121 * later. The ->dyntick_drain field controls the sequencing.
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002122 *
2123 * The caller must have disabled interrupts.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002124 */
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002125static void rcu_prepare_for_idle(int cpu)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002126{
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002127 struct timer_list *tp;
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002128 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002129
Paul E. McKenney3084f2f2011-11-22 17:07:11 -08002130 /*
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002131 * If this is an idle re-entry, for example, due to use of
2132 * RCU_NONIDLE() or the new idle-loop tracing API within the idle
2133 * loop, then don't take any state-machine actions, unless the
2134 * momentary exit from idle queued additional non-lazy callbacks.
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002135 * Instead, repost the ->idle_gp_timer if this CPU has callbacks
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002136 * pending.
2137 */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002138 if (!rdtp->idle_first_pass &&
2139 (rdtp->nonlazy_posted == rdtp->nonlazy_posted_snap)) {
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002140 if (rcu_cpu_has_callbacks(cpu)) {
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002141 tp = &rdtp->idle_gp_timer;
2142 mod_timer_pinned(tp, rdtp->idle_gp_timer_expires);
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002143 }
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002144 return;
2145 }
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002146 rdtp->idle_first_pass = 0;
2147 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted - 1;
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002148
2149 /*
Paul E. McKenneyf535a602011-11-22 20:43:02 -08002150 * If there are no callbacks on this CPU, enter dyntick-idle mode.
2151 * Also reset state to avoid prejudicing later attempts.
Paul E. McKenney3084f2f2011-11-22 17:07:11 -08002152 */
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002153 if (!rcu_cpu_has_callbacks(cpu)) {
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002154 rdtp->dyntick_holdoff = jiffies - 1;
2155 rdtp->dyntick_drain = 0;
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002156 trace_rcu_prep_idle("No callbacks");
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002157 return;
Paul E. McKenney77e38ed2010-04-25 21:04:29 -07002158 }
Paul E. McKenney3084f2f2011-11-22 17:07:11 -08002159
2160 /*
2161 * If in holdoff mode, just return. We will presumably have
2162 * refrained from disabling the scheduling-clock tick.
2163 */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002164 if (rdtp->dyntick_holdoff == jiffies) {
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002165 trace_rcu_prep_idle("In holdoff");
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002166 return;
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002167 }
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002168
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002169 /* Check and update the ->dyntick_drain sequencing. */
2170 if (rdtp->dyntick_drain <= 0) {
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002171 /* First time through, initialize the counter. */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002172 rdtp->dyntick_drain = RCU_IDLE_FLUSHES;
2173 } else if (rdtp->dyntick_drain <= RCU_IDLE_OPT_FLUSHES &&
Paul E. McKenneyc3ce9102012-02-14 10:12:54 -08002174 !rcu_pending(cpu) &&
2175 !local_softirq_pending()) {
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002176 /* Can we go dyntick-idle despite still having callbacks? */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002177 rdtp->dyntick_drain = 0;
2178 rdtp->dyntick_holdoff = jiffies;
Paul E. McKenneyfd4b3522012-05-05 19:10:35 -07002179 if (rcu_cpu_has_nonlazy_callbacks(cpu)) {
2180 trace_rcu_prep_idle("Dyntick with callbacks");
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002181 rdtp->idle_gp_timer_expires =
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002182 jiffies + RCU_IDLE_GP_DELAY;
Paul E. McKenneyfd4b3522012-05-05 19:10:35 -07002183 } else {
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002184 rdtp->idle_gp_timer_expires =
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002185 jiffies + RCU_IDLE_LAZY_GP_DELAY;
Paul E. McKenneyfd4b3522012-05-05 19:10:35 -07002186 trace_rcu_prep_idle("Dyntick with lazy callbacks");
2187 }
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002188 tp = &rdtp->idle_gp_timer;
2189 mod_timer_pinned(tp, rdtp->idle_gp_timer_expires);
2190 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08002191 return; /* Nothing more to do immediately. */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002192 } else if (--(rdtp->dyntick_drain) <= 0) {
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002193 /* We have hit the limit, so time to give up. */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002194 rdtp->dyntick_holdoff = jiffies;
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002195 trace_rcu_prep_idle("Begin holdoff");
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002196 invoke_rcu_core(); /* Force the CPU out of dyntick-idle. */
2197 return;
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002198 }
2199
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002200 /*
2201 * Do one step of pushing the remaining RCU callbacks through
2202 * the RCU core state machine.
2203 */
2204#ifdef CONFIG_TREE_PREEMPT_RCU
2205 if (per_cpu(rcu_preempt_data, cpu).nxtlist) {
2206 rcu_preempt_qs(cpu);
2207 force_quiescent_state(&rcu_preempt_state, 0);
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002208 }
2209#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002210 if (per_cpu(rcu_sched_data, cpu).nxtlist) {
2211 rcu_sched_qs(cpu);
2212 force_quiescent_state(&rcu_sched_state, 0);
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002213 }
2214 if (per_cpu(rcu_bh_data, cpu).nxtlist) {
2215 rcu_bh_qs(cpu);
2216 force_quiescent_state(&rcu_bh_state, 0);
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002217 }
2218
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002219 /*
2220 * If RCU callbacks are still pending, RCU still needs this CPU.
2221 * So try forcing the callbacks through the grace period.
2222 */
Paul E. McKenney3ad0dec2011-11-22 21:08:13 -08002223 if (rcu_cpu_has_callbacks(cpu)) {
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002224 trace_rcu_prep_idle("More callbacks");
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002225 invoke_rcu_core();
Paul E. McKenneyc0cfbbb2012-01-23 17:23:35 -08002226 } else
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002227 trace_rcu_prep_idle("Callbacks drained");
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002228}
2229
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002230/*
Paul E. McKenney98248a02012-05-03 15:38:10 -07002231 * Keep a running count of the number of non-lazy callbacks posted
2232 * on this CPU. This running counter (which is never decremented) allows
2233 * rcu_prepare_for_idle() to detect when something out of the idle loop
2234 * posts a callback, even if an equal number of callbacks are invoked.
2235 * Of course, callbacks should only be posted from within a trace event
2236 * designed to be called from idle or from within RCU_NONIDLE().
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002237 */
2238static void rcu_idle_count_callbacks_posted(void)
2239{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002240 __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002241}
2242
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002243#endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
Paul E. McKenneya858af22012-01-16 13:29:10 -08002244
2245#ifdef CONFIG_RCU_CPU_STALL_INFO
2246
2247#ifdef CONFIG_RCU_FAST_NO_HZ
2248
2249static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
2250{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002251 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2252 struct timer_list *tltp = &rdtp->idle_gp_timer;
Paul E. McKenneya858af22012-01-16 13:29:10 -08002253
Paul E. McKenney2ee3dc82012-02-23 17:13:19 -08002254 sprintf(cp, "drain=%d %c timer=%lu",
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002255 rdtp->dyntick_drain,
2256 rdtp->dyntick_holdoff == jiffies ? 'H' : '.',
Paul E. McKenney2ee3dc82012-02-23 17:13:19 -08002257 timer_pending(tltp) ? tltp->expires - jiffies : -1);
Paul E. McKenneya858af22012-01-16 13:29:10 -08002258}
2259
2260#else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
2261
2262static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
2263{
2264}
2265
2266#endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
2267
2268/* Initiate the stall-info list. */
2269static void print_cpu_stall_info_begin(void)
2270{
2271 printk(KERN_CONT "\n");
2272}
2273
2274/*
2275 * Print out diagnostic information for the specified stalled CPU.
2276 *
2277 * If the specified CPU is aware of the current RCU grace period
2278 * (flavor specified by rsp), then print the number of scheduling
2279 * clock interrupts the CPU has taken during the time that it has
2280 * been aware. Otherwise, print the number of RCU grace periods
2281 * that this CPU is ignorant of, for example, "1" if the CPU was
2282 * aware of the previous grace period.
2283 *
2284 * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
2285 */
2286static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
2287{
2288 char fast_no_hz[72];
2289 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2290 struct rcu_dynticks *rdtp = rdp->dynticks;
2291 char *ticks_title;
2292 unsigned long ticks_value;
2293
2294 if (rsp->gpnum == rdp->gpnum) {
2295 ticks_title = "ticks this GP";
2296 ticks_value = rdp->ticks_this_gp;
2297 } else {
2298 ticks_title = "GPs behind";
2299 ticks_value = rsp->gpnum - rdp->gpnum;
2300 }
2301 print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
2302 printk(KERN_ERR "\t%d: (%lu %s) idle=%03x/%llx/%d %s\n",
2303 cpu, ticks_value, ticks_title,
2304 atomic_read(&rdtp->dynticks) & 0xfff,
2305 rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
2306 fast_no_hz);
2307}
2308
2309/* Terminate the stall-info list. */
2310static void print_cpu_stall_info_end(void)
2311{
2312 printk(KERN_ERR "\t");
2313}
2314
2315/* Zero ->ticks_this_gp for all flavors of RCU. */
2316static void zero_cpu_stall_ticks(struct rcu_data *rdp)
2317{
2318 rdp->ticks_this_gp = 0;
2319}
2320
2321/* Increment ->ticks_this_gp for all flavors of RCU. */
2322static void increment_cpu_stall_ticks(void)
2323{
2324 __get_cpu_var(rcu_sched_data).ticks_this_gp++;
2325 __get_cpu_var(rcu_bh_data).ticks_this_gp++;
2326#ifdef CONFIG_TREE_PREEMPT_RCU
2327 __get_cpu_var(rcu_preempt_data).ticks_this_gp++;
2328#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
2329}
2330
2331#else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
2332
2333static void print_cpu_stall_info_begin(void)
2334{
2335 printk(KERN_CONT " {");
2336}
2337
2338static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
2339{
2340 printk(KERN_CONT " %d", cpu);
2341}
2342
2343static void print_cpu_stall_info_end(void)
2344{
2345 printk(KERN_CONT "} ");
2346}
2347
2348static void zero_cpu_stall_ticks(struct rcu_data *rdp)
2349{
2350}
2351
2352static void increment_cpu_stall_ticks(void)
2353{
2354}
2355
2356#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */