blob: ef2b5231afa41120d3c8a12f3fb8826717d7b26c [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
Paul E. McKenneycc5df652012-06-15 18:16:00 -070071 printk(KERN_INFO "\tFour-level hierarchy is enabled.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -070072#endif
Paul E. McKenneyf885b7f2012-04-23 15:52:53 -070073 if (rcu_fanout_leaf != CONFIG_RCU_FANOUT_LEAF)
74 printk(KERN_INFO "\tExperimental boot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
Paul E. McKenneycca6f392012-05-08 21:00:28 -070075 if (nr_cpu_ids != NR_CPUS)
76 printk(KERN_INFO "\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
Paul E. McKenney26845c22010-04-13 14:19:23 -070077}
78
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070079#ifdef CONFIG_TREE_PREEMPT_RCU
80
Paul E. McKenneye99033c2011-06-21 00:13:44 -070081struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070082DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
Paul E. McKenney27f4d282011-02-07 12:47:15 -080083static struct rcu_state *rcu_state = &rcu_preempt_state;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070084
Paul E. McKenney10f39bb2011-07-17 21:14:35 -070085static void rcu_read_unlock_special(struct task_struct *t);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -080086static int rcu_preempted_readers_exp(struct rcu_node *rnp);
87
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070088/*
89 * Tell them what RCU they are running.
90 */
Paul E. McKenney0e0fc1c2009-11-11 11:28:06 -080091static void __init rcu_bootup_announce(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070092{
Paul E. McKenney6cc68792011-03-02 13:15:15 -080093 printk(KERN_INFO "Preemptible hierarchical RCU implementation.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -070094 rcu_bootup_announce_oddness();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -070095}
96
97/*
98 * Return the number of RCU-preempt batches processed thus far
99 * for debug and statistics.
100 */
101long rcu_batches_completed_preempt(void)
102{
103 return rcu_preempt_state.completed;
104}
105EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
106
107/*
108 * Return the number of RCU batches processed thus far for debug & stats.
109 */
110long rcu_batches_completed(void)
111{
112 return rcu_batches_completed_preempt();
113}
114EXPORT_SYMBOL_GPL(rcu_batches_completed);
115
116/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800117 * Force a quiescent state for preemptible RCU.
118 */
119void rcu_force_quiescent_state(void)
120{
121 force_quiescent_state(&rcu_preempt_state, 0);
122}
123EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
124
125/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800126 * Record a preemptible-RCU quiescent state for the specified CPU. Note
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700127 * that this just means that the task currently running on the CPU is
128 * not in a quiescent state. There might be any number of tasks blocked
129 * while in an RCU read-side critical section.
Paul E. McKenney25502a62010-04-01 17:37:01 -0700130 *
131 * Unlike the other rcu_*_qs() functions, callers to this function
132 * must disable irqs in order to protect the assignment to
133 * ->rcu_read_unlock_special.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700134 */
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700135static void rcu_preempt_qs(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700136{
137 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -0700138
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700139 rdp->passed_quiesce_gpnum = rdp->gpnum;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700140 barrier();
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700141 if (rdp->passed_quiesce == 0)
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700142 trace_rcu_grace_period("rcu_preempt", rdp->gpnum, "cpuqs");
Paul E. McKenneye4cc1f22011-06-27 00:17:43 -0700143 rdp->passed_quiesce = 1;
Paul E. McKenney25502a62010-04-01 17:37:01 -0700144 current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700145}
146
147/*
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700148 * We have entered the scheduler, and the current task might soon be
149 * context-switched away from. If this task is in an RCU read-side
150 * critical section, we will no longer be able to rely on the CPU to
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800151 * record that fact, so we enqueue the task on the blkd_tasks list.
152 * The task will dequeue itself when it exits the outermost enclosing
153 * RCU read-side critical section. Therefore, the current grace period
154 * cannot be permitted to complete until the blkd_tasks list entries
155 * predating the current grace period drain, in other words, until
156 * rnp->gp_tasks becomes NULL.
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700157 *
158 * Caller must disable preemption.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700159 */
Paul E. McKenneycba6d0d2012-07-02 07:08:42 -0700160static void rcu_preempt_note_context_switch(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700161{
162 struct task_struct *t = current;
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700163 unsigned long flags;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700164 struct rcu_data *rdp;
165 struct rcu_node *rnp;
166
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700167 if (t->rcu_read_lock_nesting > 0 &&
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700168 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
169
170 /* Possibly blocking in an RCU read-side critical section. */
Paul E. McKenneycba6d0d2012-07-02 07:08:42 -0700171 rdp = per_cpu_ptr(rcu_preempt_state.rda, cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700172 rnp = rdp->mynode;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800173 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700174 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
Paul E. McKenney86848962009-08-27 15:00:12 -0700175 t->rcu_blocked_node = rnp;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700176
177 /*
178 * If this CPU has already checked in, then this task
179 * will hold up the next grace period rather than the
180 * current grace period. Queue the task accordingly.
181 * If the task is queued for the current grace period
182 * (i.e., this CPU has not yet passed through a quiescent
183 * state for the current grace period), then as long
184 * as that task remains queued, the current grace period
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800185 * cannot end. Note that there is some uncertainty as
186 * to exactly when the current grace period started.
187 * We take a conservative approach, which can result
188 * in unnecessarily waiting on tasks that started very
189 * slightly after the current grace period began. C'est
190 * la vie!!!
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700191 *
192 * But first, note that the current CPU must still be
193 * on line!
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700194 */
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700195 WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700196 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800197 if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
198 list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
199 rnp->gp_tasks = &t->rcu_node_entry;
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800200#ifdef CONFIG_RCU_BOOST
201 if (rnp->boost_tasks != NULL)
202 rnp->boost_tasks = rnp->gp_tasks;
203#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800204 } else {
205 list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
206 if (rnp->qsmask & rdp->grpmask)
207 rnp->gp_tasks = &t->rcu_node_entry;
208 }
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700209 trace_rcu_preempt_task(rdp->rsp->name,
210 t->pid,
211 (rnp->qsmask & rdp->grpmask)
212 ? rnp->gpnum
213 : rnp->gpnum + 1);
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800214 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700215 } else if (t->rcu_read_lock_nesting < 0 &&
216 t->rcu_read_unlock_special) {
217
218 /*
219 * Complete exit from RCU read-side critical section on
220 * behalf of preempted instance of __rcu_read_unlock().
221 */
222 rcu_read_unlock_special(t);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700223 }
224
225 /*
226 * Either we were not in an RCU read-side critical section to
227 * begin with, or we have now recorded that critical section
228 * globally. Either way, we can now note a quiescent state
229 * for this CPU. Again, if we were in an RCU read-side critical
230 * section, and if that critical section was blocking the current
231 * grace period, then the fact that the task has been enqueued
232 * means that we continue to block the current grace period.
233 */
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700234 local_irq_save(flags);
Paul E. McKenneycba6d0d2012-07-02 07:08:42 -0700235 rcu_preempt_qs(cpu);
Paul E. McKenneye7d88422009-09-18 09:50:18 -0700236 local_irq_restore(flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700237}
238
239/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800240 * Tree-preemptible RCU implementation for rcu_read_lock().
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700241 * Just increment ->rcu_read_lock_nesting, shared state will be updated
242 * if we block.
243 */
244void __rcu_read_lock(void)
245{
Paul E. McKenney80dcf602010-08-19 16:57:45 -0700246 current->rcu_read_lock_nesting++;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700247 barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
248}
249EXPORT_SYMBOL_GPL(__rcu_read_lock);
250
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -0700251/*
252 * Check for preempted RCU readers blocking the current grace period
253 * for the specified rcu_node structure. If the caller needs a reliable
254 * answer, it must hold the rcu_node's ->lock.
255 */
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800256static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -0700257{
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800258 return rnp->gp_tasks != NULL;
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -0700259}
260
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800261/*
262 * Record a quiescent state for all tasks that were previously queued
263 * on the specified rcu_node structure and that were blocking the current
264 * RCU grace period. The caller must hold the specified rnp->lock with
265 * irqs disabled, and this lock is released upon return, but irqs remain
266 * disabled.
267 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800268static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800269 __releases(rnp->lock)
270{
271 unsigned long mask;
272 struct rcu_node *rnp_p;
273
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800274 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800275 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800276 return; /* Still need more quiescent states! */
277 }
278
279 rnp_p = rnp->parent;
280 if (rnp_p == NULL) {
281 /*
282 * Either there is only one rcu_node in the tree,
283 * or tasks were kicked up to root rcu_node due to
284 * CPUs going offline.
285 */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800286 rcu_report_qs_rsp(&rcu_preempt_state, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800287 return;
288 }
289
290 /* Report up the rest of the hierarchy. */
291 mask = rnp->grpmask;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800292 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
293 raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800294 rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800295}
296
297/*
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800298 * Advance a ->blkd_tasks-list pointer to the next entry, instead
299 * returning NULL if at the end of the list.
300 */
301static struct list_head *rcu_next_node_entry(struct task_struct *t,
302 struct rcu_node *rnp)
303{
304 struct list_head *np;
305
306 np = t->rcu_node_entry.next;
307 if (np == &rnp->blkd_tasks)
308 np = NULL;
309 return np;
310}
311
312/*
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800313 * Handle special cases during rcu_read_unlock(), such as needing to
314 * notify RCU core processing or task having blocked during the RCU
315 * read-side critical section.
316 */
Paul E. McKenneybe0e1e22011-05-21 05:57:18 -0700317static noinline void rcu_read_unlock_special(struct task_struct *t)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700318{
319 int empty;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800320 int empty_exp;
Paul E. McKenney389abd42011-09-21 14:41:37 -0700321 int empty_exp_now;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700322 unsigned long flags;
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800323 struct list_head *np;
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700324#ifdef CONFIG_RCU_BOOST
325 struct rt_mutex *rbmp = NULL;
326#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700327 struct rcu_node *rnp;
328 int special;
329
330 /* NMI handlers cannot block and cannot safely manipulate state. */
331 if (in_nmi())
332 return;
333
334 local_irq_save(flags);
335
336 /*
337 * If RCU core is waiting for this CPU to exit critical section,
338 * let it know that we have done so.
339 */
340 special = t->rcu_read_unlock_special;
341 if (special & RCU_READ_UNLOCK_NEED_QS) {
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700342 rcu_preempt_qs(smp_processor_id());
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700343 }
344
345 /* Hardware IRQ handlers cannot block. */
Peter Zijlstraec433f02011-07-19 15:32:00 -0700346 if (in_irq() || in_serving_softirq()) {
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700347 local_irq_restore(flags);
348 return;
349 }
350
351 /* Clean up if blocked during RCU read-side critical section. */
352 if (special & RCU_READ_UNLOCK_BLOCKED) {
353 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
354
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700355 /*
356 * Remove this task from the list it blocked on. The
357 * task can migrate while we acquire the lock, but at
358 * most one time. So at most two passes through loop.
359 */
360 for (;;) {
Paul E. McKenney86848962009-08-27 15:00:12 -0700361 rnp = t->rcu_blocked_node;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800362 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenney86848962009-08-27 15:00:12 -0700363 if (rnp == t->rcu_blocked_node)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700364 break;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800365 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700366 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800367 empty = !rcu_preempt_blocked_readers_cgp(rnp);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800368 empty_exp = !rcu_preempted_readers_exp(rnp);
369 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800370 np = rcu_next_node_entry(t, rnp);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700371 list_del_init(&t->rcu_node_entry);
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700372 t->rcu_blocked_node = NULL;
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700373 trace_rcu_unlock_preempted_task("rcu_preempt",
374 rnp->gpnum, t->pid);
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800375 if (&t->rcu_node_entry == rnp->gp_tasks)
376 rnp->gp_tasks = np;
377 if (&t->rcu_node_entry == rnp->exp_tasks)
378 rnp->exp_tasks = np;
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800379#ifdef CONFIG_RCU_BOOST
380 if (&t->rcu_node_entry == rnp->boost_tasks)
381 rnp->boost_tasks = np;
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700382 /* Snapshot/clear ->rcu_boost_mutex with rcu_node lock held. */
383 if (t->rcu_boost_mutex) {
384 rbmp = t->rcu_boost_mutex;
385 t->rcu_boost_mutex = NULL;
Paul E. McKenney7765be22011-07-14 12:24:11 -0700386 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800387#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700388
389 /*
390 * If this was the last task on the current list, and if
391 * we aren't waiting on any CPUs, report the quiescent state.
Paul E. McKenney389abd42011-09-21 14:41:37 -0700392 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
393 * so we must take a snapshot of the expedited state.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700394 */
Paul E. McKenney389abd42011-09-21 14:41:37 -0700395 empty_exp_now = !rcu_preempted_readers_exp(rnp);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700396 if (!empty && !rcu_preempt_blocked_readers_cgp(rnp)) {
397 trace_rcu_quiescent_state_report("preempt_rcu",
398 rnp->gpnum,
399 0, rnp->qsmask,
400 rnp->level,
401 rnp->grplo,
402 rnp->grphi,
403 !!rnp->gp_tasks);
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -0800404 rcu_report_unblock_qs_rnp(rnp, flags);
Paul E. McKenneyd4c08f22011-06-25 06:36:56 -0700405 } else
406 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800407
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800408#ifdef CONFIG_RCU_BOOST
409 /* Unboost if we were boosted. */
Paul E. McKenney82e78d82011-08-04 07:55:34 -0700410 if (rbmp)
411 rt_mutex_unlock(rbmp);
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800412#endif /* #ifdef CONFIG_RCU_BOOST */
413
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800414 /*
415 * If this was the last task on the expedited lists,
416 * then we need to report up the rcu_node hierarchy.
417 */
Paul E. McKenney389abd42011-09-21 14:41:37 -0700418 if (!empty_exp && empty_exp_now)
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700419 rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800420 } else {
421 local_irq_restore(flags);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700422 }
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700423}
424
425/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800426 * Tree-preemptible RCU implementation for rcu_read_unlock().
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700427 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
428 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
429 * invoke rcu_read_unlock_special() to clean up after a context switch
430 * in an RCU read-side critical section and other special cases.
431 */
432void __rcu_read_unlock(void)
433{
434 struct task_struct *t = current;
435
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700436 if (t->rcu_read_lock_nesting != 1)
437 --t->rcu_read_lock_nesting;
438 else {
Paul E. McKenney6206ab92011-08-01 06:22:11 -0700439 barrier(); /* critical section before exit code. */
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700440 t->rcu_read_lock_nesting = INT_MIN;
441 barrier(); /* assign before ->rcu_read_unlock_special load */
Paul E. McKenneybe0e1e22011-05-21 05:57:18 -0700442 if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
443 rcu_read_unlock_special(t);
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700444 barrier(); /* ->rcu_read_unlock_special load before assign */
445 t->rcu_read_lock_nesting = 0;
Paul E. McKenneybe0e1e22011-05-21 05:57:18 -0700446 }
Paul E. McKenneycba82442010-01-04 16:04:01 -0800447#ifdef CONFIG_PROVE_LOCKING
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700448 {
449 int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
450
451 WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
452 }
Paul E. McKenneycba82442010-01-04 16:04:01 -0800453#endif /* #ifdef CONFIG_PROVE_LOCKING */
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700454}
455EXPORT_SYMBOL_GPL(__rcu_read_unlock);
456
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800457#ifdef CONFIG_RCU_CPU_STALL_VERBOSE
458
459/*
460 * Dump detailed information for all tasks blocking the current RCU
461 * grace period on the specified rcu_node structure.
462 */
463static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
464{
465 unsigned long flags;
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800466 struct task_struct *t;
467
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800468 if (!rcu_preempt_blocked_readers_cgp(rnp))
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800469 return;
470 raw_spin_lock_irqsave(&rnp->lock, flags);
471 t = list_entry(rnp->gp_tasks,
472 struct task_struct, rcu_node_entry);
473 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
474 sched_show_task(t);
475 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney1ed509a2010-02-22 17:05:05 -0800476}
477
478/*
479 * Dump detailed information for all tasks blocking the current RCU
480 * grace period.
481 */
482static void rcu_print_detail_task_stall(struct rcu_state *rsp)
483{
484 struct rcu_node *rnp = rcu_get_root(rsp);
485
486 rcu_print_detail_task_stall_rnp(rnp);
487 rcu_for_each_leaf_node(rsp, rnp)
488 rcu_print_detail_task_stall_rnp(rnp);
489}
490
491#else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
492
493static void rcu_print_detail_task_stall(struct rcu_state *rsp)
494{
495}
496
497#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
498
Paul E. McKenneya858af22012-01-16 13:29:10 -0800499#ifdef CONFIG_RCU_CPU_STALL_INFO
500
501static void rcu_print_task_stall_begin(struct rcu_node *rnp)
502{
503 printk(KERN_ERR "\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
504 rnp->level, rnp->grplo, rnp->grphi);
505}
506
507static void rcu_print_task_stall_end(void)
508{
509 printk(KERN_CONT "\n");
510}
511
512#else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
513
514static void rcu_print_task_stall_begin(struct rcu_node *rnp)
515{
516}
517
518static void rcu_print_task_stall_end(void)
519{
520}
521
522#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
523
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700524/*
525 * Scan the current list of tasks blocked within RCU read-side critical
526 * sections, printing out the tid of each.
527 */
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700528static int rcu_print_task_stall(struct rcu_node *rnp)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700529{
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700530 struct task_struct *t;
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700531 int ndetected = 0;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700532
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800533 if (!rcu_preempt_blocked_readers_cgp(rnp))
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700534 return 0;
Paul E. McKenneya858af22012-01-16 13:29:10 -0800535 rcu_print_task_stall_begin(rnp);
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800536 t = list_entry(rnp->gp_tasks,
537 struct task_struct, rcu_node_entry);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700538 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
Paul E. McKenneya858af22012-01-16 13:29:10 -0800539 printk(KERN_CONT " P%d", t->pid);
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700540 ndetected++;
541 }
Paul E. McKenneya858af22012-01-16 13:29:10 -0800542 rcu_print_task_stall_end();
Paul E. McKenney9bc8b552011-08-13 13:31:47 -0700543 return ndetected;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700544}
545
Paul E. McKenney53d84e02010-08-10 14:28:53 -0700546/*
547 * Suppress preemptible RCU's CPU stall warnings by pushing the
548 * time of the next stall-warning message comfortably far into the
549 * future.
550 */
551static void rcu_preempt_stall_reset(void)
552{
553 rcu_preempt_state.jiffies_stall = jiffies + ULONG_MAX / 2;
554}
555
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700556/*
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700557 * Check that the list of blocked tasks for the newly completed grace
558 * period is in fact empty. It is a serious bug to complete a grace
559 * period that still has RCU readers blocked! This function must be
560 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
561 * must be held by the caller.
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800562 *
563 * Also, if there are blocked tasks on the list, they automatically
564 * block the newly created grace period, so set up ->gp_tasks accordingly.
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700565 */
566static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
567{
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800568 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800569 if (!list_empty(&rnp->blkd_tasks))
570 rnp->gp_tasks = rnp->blkd_tasks.next;
Paul E. McKenney28ecd582009-09-18 09:50:17 -0700571 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -0700572}
573
Paul E. McKenney33f76142009-08-24 09:42:01 -0700574#ifdef CONFIG_HOTPLUG_CPU
575
576/*
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700577 * Handle tasklist migration for case in which all CPUs covered by the
578 * specified rcu_node have gone offline. Move them up to the root
579 * rcu_node. The reason for not just moving them to the immediate
580 * parent is to remove the need for rcu_read_unlock_special() to
581 * make more than two attempts to acquire the target rcu_node's lock.
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -0800582 * Returns true if there were tasks blocking the current RCU grace
583 * period.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700584 *
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700585 * Returns 1 if there was previously a task blocking the current grace
586 * period on the specified rcu_node structure.
587 *
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700588 * The caller must hold rnp->lock with irqs disabled.
589 */
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700590static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
591 struct rcu_node *rnp,
592 struct rcu_data *rdp)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700593{
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700594 struct list_head *lp;
595 struct list_head *lp_root;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800596 int retval = 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700597 struct rcu_node *rnp_root = rcu_get_root(rsp);
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800598 struct task_struct *t;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700599
Paul E. McKenney86848962009-08-27 15:00:12 -0700600 if (rnp == rnp_root) {
601 WARN_ONCE(1, "Last CPU thought to be offlined?");
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700602 return 0; /* Shouldn't happen: at least one CPU online. */
Paul E. McKenney86848962009-08-27 15:00:12 -0700603 }
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800604
605 /* If we are on an internal node, complain bitterly. */
606 WARN_ON_ONCE(rnp != rdp->mynode);
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700607
608 /*
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800609 * Move tasks up to root rcu_node. Don't try to get fancy for
610 * this corner-case operation -- just put this node's tasks
611 * at the head of the root node's list, and update the root node's
612 * ->gp_tasks and ->exp_tasks pointers to those of this node's,
613 * if non-NULL. This might result in waiting for more tasks than
614 * absolutely necessary, but this is a good performance/complexity
615 * tradeoff.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700616 */
Paul E. McKenney2036d942012-01-30 17:02:47 -0800617 if (rcu_preempt_blocked_readers_cgp(rnp) && rnp->qsmask == 0)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800618 retval |= RCU_OFL_TASKS_NORM_GP;
619 if (rcu_preempted_readers_exp(rnp))
620 retval |= RCU_OFL_TASKS_EXP_GP;
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800621 lp = &rnp->blkd_tasks;
622 lp_root = &rnp_root->blkd_tasks;
623 while (!list_empty(lp)) {
624 t = list_entry(lp->next, typeof(*t), rcu_node_entry);
625 raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
626 list_del(&t->rcu_node_entry);
627 t->rcu_blocked_node = rnp_root;
628 list_add(&t->rcu_node_entry, lp_root);
629 if (&t->rcu_node_entry == rnp->gp_tasks)
630 rnp_root->gp_tasks = rnp->gp_tasks;
631 if (&t->rcu_node_entry == rnp->exp_tasks)
632 rnp_root->exp_tasks = rnp->exp_tasks;
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800633#ifdef CONFIG_RCU_BOOST
634 if (&t->rcu_node_entry == rnp->boost_tasks)
635 rnp_root->boost_tasks = rnp->boost_tasks;
636#endif /* #ifdef CONFIG_RCU_BOOST */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800637 raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700638 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800639
640#ifdef CONFIG_RCU_BOOST
641 /* In case root is being boosted and leaf is not. */
642 raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
643 if (rnp_root->boost_tasks != NULL &&
644 rnp_root->boost_tasks != rnp_root->gp_tasks)
645 rnp_root->boost_tasks = rnp_root->gp_tasks;
646 raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
647#endif /* #ifdef CONFIG_RCU_BOOST */
648
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800649 rnp->gp_tasks = NULL;
650 rnp->exp_tasks = NULL;
Paul E. McKenney237c80c2009-10-15 09:26:14 -0700651 return retval;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700652}
653
Paul E. McKenneye5601402012-01-07 11:03:57 -0800654#endif /* #ifdef CONFIG_HOTPLUG_CPU */
655
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -0700656/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800657 * Do CPU-offline processing for preemptible RCU.
Paul E. McKenney33f76142009-08-24 09:42:01 -0700658 */
Paul E. McKenneye5601402012-01-07 11:03:57 -0800659static void rcu_preempt_cleanup_dead_cpu(int cpu)
Paul E. McKenney33f76142009-08-24 09:42:01 -0700660{
Paul E. McKenneye5601402012-01-07 11:03:57 -0800661 rcu_cleanup_dead_cpu(cpu, &rcu_preempt_state);
Paul E. McKenney33f76142009-08-24 09:42:01 -0700662}
663
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700664/*
665 * Check for a quiescent state from the current CPU. When a task blocks,
666 * the task is recorded in the corresponding CPU's rcu_node structure,
667 * which is checked elsewhere.
668 *
669 * Caller must disable hard irqs.
670 */
671static void rcu_preempt_check_callbacks(int cpu)
672{
673 struct task_struct *t = current;
674
675 if (t->rcu_read_lock_nesting == 0) {
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700676 rcu_preempt_qs(cpu);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700677 return;
678 }
Paul E. McKenney10f39bb2011-07-17 21:14:35 -0700679 if (t->rcu_read_lock_nesting > 0 &&
680 per_cpu(rcu_preempt_data, cpu).qs_pending)
Paul E. McKenneyc3422be2009-09-13 09:15:10 -0700681 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700682}
683
684/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800685 * Process callbacks for preemptible RCU.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700686 */
687static void rcu_preempt_process_callbacks(void)
688{
689 __rcu_process_callbacks(&rcu_preempt_state,
690 &__get_cpu_var(rcu_preempt_data));
691}
692
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700693#ifdef CONFIG_RCU_BOOST
694
Shaohua Li09223372011-06-14 13:26:25 +0800695static void rcu_preempt_do_callbacks(void)
696{
697 rcu_do_batch(&rcu_preempt_state, &__get_cpu_var(rcu_preempt_data));
698}
699
Paul E. McKenneya46e0892011-06-15 15:47:09 -0700700#endif /* #ifdef CONFIG_RCU_BOOST */
701
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700702/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800703 * Queue a preemptible-RCU callback for invocation after a grace period.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700704 */
705void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
706{
Paul E. McKenney486e2592012-01-06 14:11:30 -0800707 __call_rcu(head, func, &rcu_preempt_state, 0);
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700708}
709EXPORT_SYMBOL_GPL(call_rcu);
710
Paul E. McKenney486e2592012-01-06 14:11:30 -0800711/*
712 * Queue an RCU callback for lazy invocation after a grace period.
713 * This will likely be later named something like "call_rcu_lazy()",
714 * but this change will require some way of tagging the lazy RCU
715 * callbacks in the list of pending callbacks. Until then, this
716 * function may only be called from __kfree_rcu().
717 */
718void kfree_call_rcu(struct rcu_head *head,
719 void (*func)(struct rcu_head *rcu))
720{
721 __call_rcu(head, func, &rcu_preempt_state, 1);
722}
723EXPORT_SYMBOL_GPL(kfree_call_rcu);
724
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800725/**
726 * synchronize_rcu - wait until a grace period has elapsed.
727 *
728 * Control will return to the caller some time after a full grace
729 * period has elapsed, in other words after all currently executing RCU
Paul E. McKenney77d84852010-07-08 17:38:59 -0700730 * read-side critical sections have completed. Note, however, that
731 * upon return from synchronize_rcu(), the caller might well be executing
732 * concurrently with new RCU read-side critical sections that began while
733 * synchronize_rcu() was waiting. RCU read-side critical sections are
734 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800735 */
736void synchronize_rcu(void)
737{
Paul E. McKenneyfe15d702012-01-04 13:30:33 -0800738 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
739 !lock_is_held(&rcu_lock_map) &&
740 !lock_is_held(&rcu_sched_lock_map),
741 "Illegal synchronize_rcu() in RCU read-side critical section");
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800742 if (!rcu_scheduler_active)
743 return;
Paul E. McKenney2c428182011-05-26 22:14:36 -0700744 wait_rcu_gp(call_rcu);
Paul E. McKenney6ebb2372009-11-22 08:53:50 -0800745}
746EXPORT_SYMBOL_GPL(synchronize_rcu);
747
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800748static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
749static long sync_rcu_preempt_exp_count;
750static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
751
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700752/*
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800753 * Return non-zero if there are any tasks in RCU read-side critical
754 * sections blocking the current preemptible-RCU expedited grace period.
755 * If there is no preemptible-RCU expedited grace period currently in
756 * progress, returns zero unconditionally.
757 */
758static int rcu_preempted_readers_exp(struct rcu_node *rnp)
759{
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800760 return rnp->exp_tasks != NULL;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800761}
762
763/*
764 * return non-zero if there is no RCU expedited grace period in progress
765 * for the specified rcu_node structure, in other words, if all CPUs and
766 * tasks covered by the specified rcu_node structure have done their bit
767 * for the current expedited grace period. Works only for preemptible
768 * RCU -- other RCU implementation use other means.
769 *
770 * Caller must hold sync_rcu_preempt_exp_mutex.
771 */
772static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
773{
774 return !rcu_preempted_readers_exp(rnp) &&
775 ACCESS_ONCE(rnp->expmask) == 0;
776}
777
778/*
779 * Report the exit from RCU read-side critical section for the last task
780 * that queued itself during or before the current expedited preemptible-RCU
781 * grace period. This event is reported either to the rcu_node structure on
782 * which the task was queued or to one of that rcu_node structure's ancestors,
783 * recursively up the tree. (Calm down, calm down, we do the recursion
784 * iteratively!)
785 *
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700786 * Most callers will set the "wake" flag, but the task initiating the
787 * expedited grace period need not wake itself.
788 *
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800789 * Caller must hold sync_rcu_preempt_exp_mutex.
790 */
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700791static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
792 bool wake)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800793{
794 unsigned long flags;
795 unsigned long mask;
796
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800797 raw_spin_lock_irqsave(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800798 for (;;) {
Paul E. McKenney131906b2011-07-17 02:05:49 -0700799 if (!sync_rcu_preempt_exp_done(rnp)) {
800 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800801 break;
Paul E. McKenney131906b2011-07-17 02:05:49 -0700802 }
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800803 if (rnp->parent == NULL) {
Paul E. McKenney131906b2011-07-17 02:05:49 -0700804 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700805 if (wake)
806 wake_up(&sync_rcu_preempt_exp_wq);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800807 break;
808 }
809 mask = rnp->grpmask;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800810 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800811 rnp = rnp->parent;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800812 raw_spin_lock(&rnp->lock); /* irqs already disabled */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800813 rnp->expmask &= ~mask;
814 }
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800815}
816
817/*
818 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
819 * grace period for the specified rcu_node structure. If there are no such
820 * tasks, report it up the rcu_node hierarchy.
821 *
822 * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
823 */
824static void
825sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
826{
Paul E. McKenney1217ed12011-05-04 21:43:49 -0700827 unsigned long flags;
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800828 int must_wait = 0;
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800829
Paul E. McKenney1217ed12011-05-04 21:43:49 -0700830 raw_spin_lock_irqsave(&rnp->lock, flags);
831 if (list_empty(&rnp->blkd_tasks))
832 raw_spin_unlock_irqrestore(&rnp->lock, flags);
833 else {
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800834 rnp->exp_tasks = rnp->blkd_tasks.next;
Paul E. McKenney1217ed12011-05-04 21:43:49 -0700835 rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800836 must_wait = 1;
837 }
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800838 if (!must_wait)
Thomas Gleixnerb40d2932011-10-22 07:12:34 -0700839 rcu_report_exp_rnp(rsp, rnp, false); /* Don't wake self. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800840}
841
Paul E. McKenney236fefa2012-01-31 14:00:41 -0800842/**
843 * synchronize_rcu_expedited - Brute-force RCU grace period
844 *
845 * Wait for an RCU-preempt grace period, but expedite it. The basic
846 * idea is to invoke synchronize_sched_expedited() to push all the tasks to
847 * the ->blkd_tasks lists and wait for this list to drain. This consumes
848 * significant time on all CPUs and is unfriendly to real-time workloads,
849 * so is thus not recommended for any sort of common-case code.
850 * In fact, if you are using synchronize_rcu_expedited() in a loop,
851 * please restructure your code to batch your updates, and then Use a
852 * single synchronize_rcu() instead.
853 *
854 * Note that it is illegal to call this function while holding any lock
855 * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal
856 * to call this function from a CPU-hotplug notifier. Failing to observe
857 * these restriction will result in deadlock.
Paul E. McKenney019129d52009-10-14 10:15:56 -0700858 */
859void synchronize_rcu_expedited(void)
860{
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800861 unsigned long flags;
862 struct rcu_node *rnp;
863 struct rcu_state *rsp = &rcu_preempt_state;
864 long snap;
865 int trycount = 0;
866
867 smp_mb(); /* Caller's modifications seen first by other CPUs. */
868 snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
869 smp_mb(); /* Above access cannot bleed into critical section. */
870
871 /*
872 * Acquire lock, falling back to synchronize_rcu() if too many
873 * lock-acquisition failures. Of course, if someone does the
874 * expedited grace period for us, just leave.
875 */
876 while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
877 if (trycount++ < 10)
878 udelay(trycount * num_online_cpus());
879 else {
880 synchronize_rcu();
881 return;
882 }
883 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
884 goto mb_ret; /* Others did our work for us. */
885 }
886 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
887 goto unlock_mb_ret; /* Others did our work for us. */
888
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800889 /* force all RCU readers onto ->blkd_tasks lists. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800890 synchronize_sched_expedited();
891
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800892 raw_spin_lock_irqsave(&rsp->onofflock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800893
894 /* Initialize ->expmask for all non-leaf rcu_node structures. */
895 rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800896 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800897 rnp->expmask = rnp->qsmaskinit;
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800898 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800899 }
900
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800901 /* Snapshot current state of ->blkd_tasks lists. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800902 rcu_for_each_leaf_node(rsp, rnp)
903 sync_rcu_preempt_exp_init(rsp, rnp);
904 if (NUM_RCU_NODES > 1)
905 sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
906
Paul E. McKenney1304afb2010-02-22 17:05:02 -0800907 raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800908
Paul E. McKenney12f5f522010-11-29 21:56:39 -0800909 /* Wait for snapshotted ->blkd_tasks lists to drain. */
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -0800910 rnp = rcu_get_root(rsp);
911 wait_event(sync_rcu_preempt_exp_wq,
912 sync_rcu_preempt_exp_done(rnp));
913
914 /* Clean up and exit. */
915 smp_mb(); /* ensure expedited GP seen before counter increment. */
916 ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
917unlock_mb_ret:
918 mutex_unlock(&sync_rcu_preempt_exp_mutex);
919mb_ret:
920 smp_mb(); /* ensure subsequent action seen after grace period. */
Paul E. McKenney019129d52009-10-14 10:15:56 -0700921}
922EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
923
924/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800925 * Check to see if there is any immediate preemptible-RCU-related work
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700926 * to be done.
927 */
928static int rcu_preempt_pending(int cpu)
929{
930 return __rcu_pending(&rcu_preempt_state,
931 &per_cpu(rcu_preempt_data, cpu));
932}
933
934/*
Paul E. McKenney30fbcc92012-01-12 11:01:14 -0800935 * Does preemptible RCU have callbacks on this CPU?
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700936 */
Paul E. McKenney30fbcc92012-01-12 11:01:14 -0800937static int rcu_preempt_cpu_has_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700938{
939 return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
940}
941
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700942/**
943 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
944 */
945void rcu_barrier(void)
946{
947 _rcu_barrier(&rcu_preempt_state, call_rcu);
948}
949EXPORT_SYMBOL_GPL(rcu_barrier);
950
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700951/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800952 * Initialize preemptible RCU's per-CPU data.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700953 */
954static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
955{
956 rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
957}
958
959/*
Paul E. McKenneye5601402012-01-07 11:03:57 -0800960 * Move preemptible RCU's callbacks from dying CPU to other online CPU
961 * and record a quiescent state.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700962 */
Paul E. McKenneye5601402012-01-07 11:03:57 -0800963static void rcu_preempt_cleanup_dying_cpu(void)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700964{
Paul E. McKenneye5601402012-01-07 11:03:57 -0800965 rcu_cleanup_dying_cpu(&rcu_preempt_state);
Paul E. McKenneye74f4c42009-10-06 21:48:17 -0700966}
967
968/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -0800969 * Initialize preemptible RCU's state structures.
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700970 */
971static void __init __rcu_init_preempt(void)
972{
Lai Jiangshan394f99a2010-06-28 16:25:04 +0800973 rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
Paul E. McKenney1eba8f82009-09-23 09:50:42 -0700974}
975
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700976#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
977
Paul E. McKenney27f4d282011-02-07 12:47:15 -0800978static struct rcu_state *rcu_state = &rcu_sched_state;
979
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700980/*
981 * Tell them what RCU they are running.
982 */
Paul E. McKenney0e0fc1c2009-11-11 11:28:06 -0800983static void __init rcu_bootup_announce(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700984{
985 printk(KERN_INFO "Hierarchical RCU implementation.\n");
Paul E. McKenney26845c22010-04-13 14:19:23 -0700986 rcu_bootup_announce_oddness();
Paul E. McKenneyf41d9112009-08-22 13:56:52 -0700987}
988
989/*
990 * Return the number of RCU batches processed thus far for debug & stats.
991 */
992long rcu_batches_completed(void)
993{
994 return rcu_batches_completed_sched();
995}
996EXPORT_SYMBOL_GPL(rcu_batches_completed);
997
998/*
Paul E. McKenneybf66f182010-01-04 15:09:10 -0800999 * Force a quiescent state for RCU, which, because there is no preemptible
1000 * RCU, becomes the same as rcu-sched.
1001 */
1002void rcu_force_quiescent_state(void)
1003{
1004 rcu_sched_force_quiescent_state();
1005}
1006EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
1007
1008/*
Paul E. McKenneycba6d0d2012-07-02 07:08:42 -07001009 * Because preemptible RCU does not exist, we never have to check for
1010 * CPUs being in quiescent states.
1011 */
1012static void rcu_preempt_note_context_switch(int cpu)
1013{
1014}
1015
1016/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001017 * Because preemptible RCU does not exist, there are never any preempted
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -07001018 * RCU readers.
1019 */
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001020static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
Paul E. McKenneyfc2219d2009-09-23 09:50:41 -07001021{
1022 return 0;
1023}
1024
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -08001025#ifdef CONFIG_HOTPLUG_CPU
1026
1027/* Because preemptible RCU does not exist, no quieting of tasks. */
Paul E. McKenneyd3f6bad2009-12-02 12:10:13 -08001028static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -08001029{
Paul E. McKenney1304afb2010-02-22 17:05:02 -08001030 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenneyb668c9c2009-11-22 08:53:48 -08001031}
1032
1033#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1034
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001035/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001036 * Because preemptible RCU does not exist, we never have to check for
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001037 * tasks blocked within RCU read-side critical sections.
1038 */
Paul E. McKenney1ed509a2010-02-22 17:05:05 -08001039static void rcu_print_detail_task_stall(struct rcu_state *rsp)
1040{
1041}
1042
1043/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001044 * Because preemptible RCU does not exist, we never have to check for
Paul E. McKenney1ed509a2010-02-22 17:05:05 -08001045 * tasks blocked within RCU read-side critical sections.
1046 */
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001047static int rcu_print_task_stall(struct rcu_node *rnp)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001048{
Paul E. McKenney9bc8b552011-08-13 13:31:47 -07001049 return 0;
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001050}
1051
Paul E. McKenney53d84e02010-08-10 14:28:53 -07001052/*
1053 * Because preemptible RCU does not exist, there is no need to suppress
1054 * its CPU stall warnings.
1055 */
1056static void rcu_preempt_stall_reset(void)
1057{
1058}
1059
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001060/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001061 * Because there is no preemptible RCU, there can be no readers blocked,
Paul E. McKenney49e29122009-09-18 09:50:19 -07001062 * so there is no need to check for blocked tasks. So check only for
1063 * bogus qsmask values.
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -07001064 */
1065static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
1066{
Paul E. McKenney49e29122009-09-18 09:50:19 -07001067 WARN_ON_ONCE(rnp->qsmask);
Paul E. McKenneyb0e165c2009-09-13 09:15:09 -07001068}
1069
Paul E. McKenney33f76142009-08-24 09:42:01 -07001070#ifdef CONFIG_HOTPLUG_CPU
1071
1072/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001073 * Because preemptible RCU does not exist, it never needs to migrate
Paul E. McKenney237c80c2009-10-15 09:26:14 -07001074 * tasks that were blocked within RCU read-side critical sections, and
1075 * such non-existent tasks cannot possibly have been blocking the current
1076 * grace period.
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001077 */
Paul E. McKenney237c80c2009-10-15 09:26:14 -07001078static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
1079 struct rcu_node *rnp,
1080 struct rcu_data *rdp)
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001081{
Paul E. McKenney237c80c2009-10-15 09:26:14 -07001082 return 0;
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001083}
1084
Paul E. McKenneye5601402012-01-07 11:03:57 -08001085#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1086
Paul E. McKenneydd5d19b2009-08-27 14:58:16 -07001087/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001088 * Because preemptible RCU does not exist, it never needs CPU-offline
Paul E. McKenney33f76142009-08-24 09:42:01 -07001089 * processing.
1090 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001091static void rcu_preempt_cleanup_dead_cpu(int cpu)
Paul E. McKenney33f76142009-08-24 09:42:01 -07001092{
1093}
1094
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001095/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001096 * Because preemptible RCU does not exist, it never has any callbacks
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001097 * to check.
1098 */
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001099static void rcu_preempt_check_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001100{
1101}
1102
1103/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001104 * Because preemptible RCU does not exist, it never has any callbacks
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001105 * to process.
1106 */
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001107static void rcu_preempt_process_callbacks(void)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001108{
1109}
1110
1111/*
Paul E. McKenney486e2592012-01-06 14:11:30 -08001112 * Queue an RCU callback for lazy invocation after a grace period.
1113 * This will likely be later named something like "call_rcu_lazy()",
1114 * but this change will require some way of tagging the lazy RCU
1115 * callbacks in the list of pending callbacks. Until then, this
1116 * function may only be called from __kfree_rcu().
1117 *
1118 * Because there is no preemptible RCU, we use RCU-sched instead.
1119 */
1120void kfree_call_rcu(struct rcu_head *head,
1121 void (*func)(struct rcu_head *rcu))
1122{
1123 __call_rcu(head, func, &rcu_sched_state, 1);
1124}
1125EXPORT_SYMBOL_GPL(kfree_call_rcu);
1126
1127/*
Paul E. McKenney019129d52009-10-14 10:15:56 -07001128 * Wait for an rcu-preempt grace period, but make it happen quickly.
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001129 * But because preemptible RCU does not exist, map to rcu-sched.
Paul E. McKenney019129d52009-10-14 10:15:56 -07001130 */
1131void synchronize_rcu_expedited(void)
1132{
1133 synchronize_sched_expedited();
1134}
1135EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
1136
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001137#ifdef CONFIG_HOTPLUG_CPU
1138
1139/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001140 * Because preemptible RCU does not exist, there is never any need to
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001141 * report on tasks preempted in RCU read-side critical sections during
1142 * expedited RCU grace periods.
1143 */
Thomas Gleixnerb40d2932011-10-22 07:12:34 -07001144static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
1145 bool wake)
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001146{
Paul E. McKenneyd9a3da02009-12-02 12:10:15 -08001147}
1148
1149#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1150
Paul E. McKenney019129d52009-10-14 10:15:56 -07001151/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001152 * Because preemptible RCU does not exist, it never has any work to do.
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001153 */
1154static int rcu_preempt_pending(int cpu)
1155{
1156 return 0;
1157}
1158
1159/*
Paul E. McKenney30fbcc92012-01-12 11:01:14 -08001160 * Because preemptible RCU does not exist, it never has callbacks
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001161 */
Paul E. McKenney30fbcc92012-01-12 11:01:14 -08001162static int rcu_preempt_cpu_has_callbacks(int cpu)
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001163{
1164 return 0;
1165}
1166
1167/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001168 * Because preemptible RCU does not exist, rcu_barrier() is just
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001169 * another name for rcu_barrier_sched().
1170 */
1171void rcu_barrier(void)
1172{
1173 rcu_barrier_sched();
1174}
1175EXPORT_SYMBOL_GPL(rcu_barrier);
1176
1177/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001178 * Because preemptible RCU does not exist, there is no per-CPU
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001179 * data to initialize.
1180 */
1181static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
1182{
1183}
1184
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001185/*
Paul E. McKenneye5601402012-01-07 11:03:57 -08001186 * Because there is no preemptible RCU, there is no cleanup to do.
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001187 */
Paul E. McKenneye5601402012-01-07 11:03:57 -08001188static void rcu_preempt_cleanup_dying_cpu(void)
Paul E. McKenneye74f4c42009-10-06 21:48:17 -07001189{
1190}
1191
1192/*
Paul E. McKenney6cc68792011-03-02 13:15:15 -08001193 * Because preemptible RCU does not exist, it need not be initialized.
Paul E. McKenney1eba8f82009-09-23 09:50:42 -07001194 */
1195static void __init __rcu_init_preempt(void)
1196{
1197}
1198
Paul E. McKenneyf41d9112009-08-22 13:56:52 -07001199#endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001200
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001201#ifdef CONFIG_RCU_BOOST
1202
1203#include "rtmutex_common.h"
1204
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001205#ifdef CONFIG_RCU_TRACE
1206
1207static void rcu_initiate_boost_trace(struct rcu_node *rnp)
1208{
1209 if (list_empty(&rnp->blkd_tasks))
1210 rnp->n_balk_blkd_tasks++;
1211 else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
1212 rnp->n_balk_exp_gp_tasks++;
1213 else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
1214 rnp->n_balk_boost_tasks++;
1215 else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
1216 rnp->n_balk_notblocked++;
1217 else if (rnp->gp_tasks != NULL &&
Paul E. McKenneya9f47932011-05-02 03:46:10 -07001218 ULONG_CMP_LT(jiffies, rnp->boost_time))
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001219 rnp->n_balk_notyet++;
1220 else
1221 rnp->n_balk_nos++;
1222}
1223
1224#else /* #ifdef CONFIG_RCU_TRACE */
1225
1226static void rcu_initiate_boost_trace(struct rcu_node *rnp)
1227{
1228}
1229
1230#endif /* #else #ifdef CONFIG_RCU_TRACE */
1231
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001232/*
1233 * Carry out RCU priority boosting on the task indicated by ->exp_tasks
1234 * or ->boost_tasks, advancing the pointer to the next task in the
1235 * ->blkd_tasks list.
1236 *
1237 * Note that irqs must be enabled: boosting the task can block.
1238 * Returns 1 if there are more tasks needing to be boosted.
1239 */
1240static int rcu_boost(struct rcu_node *rnp)
1241{
1242 unsigned long flags;
1243 struct rt_mutex mtx;
1244 struct task_struct *t;
1245 struct list_head *tb;
1246
1247 if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL)
1248 return 0; /* Nothing left to boost. */
1249
1250 raw_spin_lock_irqsave(&rnp->lock, flags);
1251
1252 /*
1253 * Recheck under the lock: all tasks in need of boosting
1254 * might exit their RCU read-side critical sections on their own.
1255 */
1256 if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
1257 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1258 return 0;
1259 }
1260
1261 /*
1262 * Preferentially boost tasks blocking expedited grace periods.
1263 * This cannot starve the normal grace periods because a second
1264 * expedited grace period must boost all blocked tasks, including
1265 * those blocking the pre-existing normal grace period.
1266 */
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001267 if (rnp->exp_tasks != NULL) {
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001268 tb = rnp->exp_tasks;
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001269 rnp->n_exp_boosts++;
1270 } else {
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001271 tb = rnp->boost_tasks;
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001272 rnp->n_normal_boosts++;
1273 }
1274 rnp->n_tasks_boosted++;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001275
1276 /*
1277 * We boost task t by manufacturing an rt_mutex that appears to
1278 * be held by task t. We leave a pointer to that rt_mutex where
1279 * task t can find it, and task t will release the mutex when it
1280 * exits its outermost RCU read-side critical section. Then
1281 * simply acquiring this artificial rt_mutex will boost task
1282 * t's priority. (Thanks to tglx for suggesting this approach!)
1283 *
1284 * Note that task t must acquire rnp->lock to remove itself from
1285 * the ->blkd_tasks list, which it will do from exit() if from
1286 * nowhere else. We therefore are guaranteed that task t will
1287 * stay around at least until we drop rnp->lock. Note that
1288 * rnp->lock also resolves races between our priority boosting
1289 * and task t's exiting its outermost RCU read-side critical
1290 * section.
1291 */
1292 t = container_of(tb, struct task_struct, rcu_node_entry);
1293 rt_mutex_init_proxy_locked(&mtx, t);
1294 t->rcu_boost_mutex = &mtx;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001295 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1296 rt_mutex_lock(&mtx); /* Side effect: boosts task t's priority. */
1297 rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
1298
Paul E. McKenney4f89b332011-12-09 14:43:47 -08001299 return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
1300 ACCESS_ONCE(rnp->boost_tasks) != NULL;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001301}
1302
1303/*
1304 * Timer handler to initiate waking up of boost kthreads that
1305 * have yielded the CPU due to excessive numbers of tasks to
1306 * boost. We wake up the per-rcu_node kthread, which in turn
1307 * will wake up the booster kthread.
1308 */
1309static void rcu_boost_kthread_timer(unsigned long arg)
1310{
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001311 invoke_rcu_node_kthread((struct rcu_node *)arg);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001312}
1313
1314/*
1315 * Priority-boosting kthread. One per leaf rcu_node and one for the
1316 * root rcu_node.
1317 */
1318static int rcu_boost_kthread(void *arg)
1319{
1320 struct rcu_node *rnp = (struct rcu_node *)arg;
1321 int spincnt = 0;
1322 int more2boost;
1323
Paul E. McKenney385680a2011-06-21 22:43:26 -07001324 trace_rcu_utilization("Start boost kthread@init");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001325 for (;;) {
Paul E. McKenneyd71df902011-03-29 17:48:28 -07001326 rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001327 trace_rcu_utilization("End boost kthread@rcu_wait");
Peter Zijlstra08bca602011-05-20 16:06:29 -07001328 rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
Paul E. McKenney385680a2011-06-21 22:43:26 -07001329 trace_rcu_utilization("Start boost kthread@rcu_wait");
Paul E. McKenneyd71df902011-03-29 17:48:28 -07001330 rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001331 more2boost = rcu_boost(rnp);
1332 if (more2boost)
1333 spincnt++;
1334 else
1335 spincnt = 0;
1336 if (spincnt > 10) {
Paul E. McKenney385680a2011-06-21 22:43:26 -07001337 trace_rcu_utilization("End boost kthread@rcu_yield");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001338 rcu_yield(rcu_boost_kthread_timer, (unsigned long)rnp);
Paul E. McKenney385680a2011-06-21 22:43:26 -07001339 trace_rcu_utilization("Start boost kthread@rcu_yield");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001340 spincnt = 0;
1341 }
1342 }
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001343 /* NOTREACHED */
Paul E. McKenney385680a2011-06-21 22:43:26 -07001344 trace_rcu_utilization("End boost kthread@notreached");
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001345 return 0;
1346}
1347
1348/*
1349 * Check to see if it is time to start boosting RCU readers that are
1350 * blocking the current grace period, and, if so, tell the per-rcu_node
1351 * kthread to start boosting them. If there is an expedited grace
1352 * period in progress, it is always time to boost.
1353 *
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001354 * The caller must hold rnp->lock, which this function releases,
1355 * but irqs remain disabled. The ->boost_kthread_task is immortal,
1356 * so we don't need to worry about it going away.
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001357 */
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001358static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001359{
1360 struct task_struct *t;
1361
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001362 if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
1363 rnp->n_balk_exp_gp_tasks++;
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001364 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001365 return;
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001366 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001367 if (rnp->exp_tasks != NULL ||
1368 (rnp->gp_tasks != NULL &&
1369 rnp->boost_tasks == NULL &&
1370 rnp->qsmask == 0 &&
1371 ULONG_CMP_GE(jiffies, rnp->boost_time))) {
1372 if (rnp->exp_tasks == NULL)
1373 rnp->boost_tasks = rnp->gp_tasks;
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001374 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001375 t = rnp->boost_kthread_task;
1376 if (t != NULL)
1377 wake_up_process(t);
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001378 } else {
Paul E. McKenney0ea1f2e2011-02-22 13:42:43 -08001379 rcu_initiate_boost_trace(rnp);
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001380 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1381 }
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001382}
1383
Paul E. McKenney0f962a52011-04-14 12:13:53 -07001384/*
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001385 * Wake up the per-CPU kthread to invoke RCU callbacks.
1386 */
1387static void invoke_rcu_callbacks_kthread(void)
1388{
1389 unsigned long flags;
1390
1391 local_irq_save(flags);
1392 __this_cpu_write(rcu_cpu_has_work, 1);
Shaohua Li1eb52122011-06-16 16:02:54 -07001393 if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
1394 current != __this_cpu_read(rcu_cpu_kthread_task))
1395 wake_up_process(__this_cpu_read(rcu_cpu_kthread_task));
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001396 local_irq_restore(flags);
1397}
1398
1399/*
Paul E. McKenneydff16722011-11-29 15:57:13 -08001400 * Is the current CPU running the RCU-callbacks kthread?
1401 * Caller must have preemption disabled.
1402 */
1403static bool rcu_is_callbacks_kthread(void)
1404{
1405 return __get_cpu_var(rcu_cpu_kthread_task) == current;
1406}
1407
1408/*
Paul E. McKenney0f962a52011-04-14 12:13:53 -07001409 * Set the affinity of the boost kthread. The CPU-hotplug locks are
1410 * held, so no one should be messing with the existence of the boost
1411 * kthread.
1412 */
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001413static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
1414 cpumask_var_t cm)
1415{
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001416 struct task_struct *t;
1417
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001418 t = rnp->boost_kthread_task;
1419 if (t != NULL)
1420 set_cpus_allowed_ptr(rnp->boost_kthread_task, cm);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001421}
1422
1423#define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1424
1425/*
1426 * Do priority-boost accounting for the start of a new grace period.
1427 */
1428static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1429{
1430 rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1431}
1432
1433/*
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001434 * Create an RCU-boost kthread for the specified node if one does not
1435 * already exist. We only create this kthread for preemptible RCU.
1436 * Returns zero if all is well, a negated errno otherwise.
1437 */
1438static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
1439 struct rcu_node *rnp,
1440 int rnp_index)
1441{
1442 unsigned long flags;
1443 struct sched_param sp;
1444 struct task_struct *t;
1445
1446 if (&rcu_preempt_state != rsp)
1447 return 0;
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001448 rsp->boost = 1;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001449 if (rnp->boost_kthread_task != NULL)
1450 return 0;
1451 t = kthread_create(rcu_boost_kthread, (void *)rnp,
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001452 "rcub/%d", rnp_index);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001453 if (IS_ERR(t))
1454 return PTR_ERR(t);
1455 raw_spin_lock_irqsave(&rnp->lock, flags);
1456 rnp->boost_kthread_task = t;
1457 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001458 sp.sched_priority = RCU_BOOST_PRIO;
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001459 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
Paul E. McKenney9a432732011-05-30 20:38:55 -07001460 wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001461 return 0;
1462}
1463
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001464#ifdef CONFIG_HOTPLUG_CPU
1465
1466/*
1467 * Stop the RCU's per-CPU kthread when its CPU goes offline,.
1468 */
1469static void rcu_stop_cpu_kthread(int cpu)
1470{
1471 struct task_struct *t;
1472
1473 /* Stop the CPU's kthread. */
1474 t = per_cpu(rcu_cpu_kthread_task, cpu);
1475 if (t != NULL) {
1476 per_cpu(rcu_cpu_kthread_task, cpu) = NULL;
1477 kthread_stop(t);
1478 }
1479}
1480
1481#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1482
1483static void rcu_kthread_do_work(void)
1484{
1485 rcu_do_batch(&rcu_sched_state, &__get_cpu_var(rcu_sched_data));
1486 rcu_do_batch(&rcu_bh_state, &__get_cpu_var(rcu_bh_data));
1487 rcu_preempt_do_callbacks();
1488}
1489
1490/*
1491 * Wake up the specified per-rcu_node-structure kthread.
1492 * Because the per-rcu_node kthreads are immortal, we don't need
1493 * to do anything to keep them alive.
1494 */
1495static void invoke_rcu_node_kthread(struct rcu_node *rnp)
1496{
1497 struct task_struct *t;
1498
1499 t = rnp->node_kthread_task;
1500 if (t != NULL)
1501 wake_up_process(t);
1502}
1503
1504/*
1505 * Set the specified CPU's kthread to run RT or not, as specified by
1506 * the to_rt argument. The CPU-hotplug locks are held, so the task
1507 * is not going away.
1508 */
1509static void rcu_cpu_kthread_setrt(int cpu, int to_rt)
1510{
1511 int policy;
1512 struct sched_param sp;
1513 struct task_struct *t;
1514
1515 t = per_cpu(rcu_cpu_kthread_task, cpu);
1516 if (t == NULL)
1517 return;
1518 if (to_rt) {
1519 policy = SCHED_FIFO;
1520 sp.sched_priority = RCU_KTHREAD_PRIO;
1521 } else {
1522 policy = SCHED_NORMAL;
1523 sp.sched_priority = 0;
1524 }
1525 sched_setscheduler_nocheck(t, policy, &sp);
1526}
1527
1528/*
1529 * Timer handler to initiate the waking up of per-CPU kthreads that
1530 * have yielded the CPU due to excess numbers of RCU callbacks.
1531 * We wake up the per-rcu_node kthread, which in turn will wake up
1532 * the booster kthread.
1533 */
1534static void rcu_cpu_kthread_timer(unsigned long arg)
1535{
1536 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, arg);
1537 struct rcu_node *rnp = rdp->mynode;
1538
1539 atomic_or(rdp->grpmask, &rnp->wakemask);
1540 invoke_rcu_node_kthread(rnp);
1541}
1542
1543/*
1544 * Drop to non-real-time priority and yield, but only after posting a
1545 * timer that will cause us to regain our real-time priority if we
1546 * remain preempted. Either way, we restore our real-time priority
1547 * before returning.
1548 */
1549static void rcu_yield(void (*f)(unsigned long), unsigned long arg)
1550{
1551 struct sched_param sp;
1552 struct timer_list yield_timer;
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001553 int prio = current->rt_priority;
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001554
1555 setup_timer_on_stack(&yield_timer, f, arg);
1556 mod_timer(&yield_timer, jiffies + 2);
1557 sp.sched_priority = 0;
1558 sched_setscheduler_nocheck(current, SCHED_NORMAL, &sp);
1559 set_user_nice(current, 19);
1560 schedule();
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001561 set_user_nice(current, 0);
1562 sp.sched_priority = prio;
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001563 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1564 del_timer(&yield_timer);
1565}
1566
1567/*
1568 * Handle cases where the rcu_cpu_kthread() ends up on the wrong CPU.
1569 * This can happen while the corresponding CPU is either coming online
1570 * or going offline. We cannot wait until the CPU is fully online
1571 * before starting the kthread, because the various notifier functions
1572 * can wait for RCU grace periods. So we park rcu_cpu_kthread() until
1573 * the corresponding CPU is online.
1574 *
1575 * Return 1 if the kthread needs to stop, 0 otherwise.
1576 *
1577 * Caller must disable bh. This function can momentarily enable it.
1578 */
1579static int rcu_cpu_kthread_should_stop(int cpu)
1580{
1581 while (cpu_is_offline(cpu) ||
1582 !cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)) ||
1583 smp_processor_id() != cpu) {
1584 if (kthread_should_stop())
1585 return 1;
1586 per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
1587 per_cpu(rcu_cpu_kthread_cpu, cpu) = raw_smp_processor_id();
1588 local_bh_enable();
1589 schedule_timeout_uninterruptible(1);
1590 if (!cpumask_equal(&current->cpus_allowed, cpumask_of(cpu)))
1591 set_cpus_allowed_ptr(current, cpumask_of(cpu));
1592 local_bh_disable();
1593 }
1594 per_cpu(rcu_cpu_kthread_cpu, cpu) = cpu;
1595 return 0;
1596}
1597
1598/*
1599 * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
Paul E. McKenneye0f23062011-06-21 01:29:39 -07001600 * RCU softirq used in flavors and configurations of RCU that do not
1601 * support RCU priority boosting.
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001602 */
1603static int rcu_cpu_kthread(void *arg)
1604{
1605 int cpu = (int)(long)arg;
1606 unsigned long flags;
1607 int spincnt = 0;
1608 unsigned int *statusp = &per_cpu(rcu_cpu_kthread_status, cpu);
1609 char work;
1610 char *workp = &per_cpu(rcu_cpu_has_work, cpu);
1611
Paul E. McKenney385680a2011-06-21 22:43:26 -07001612 trace_rcu_utilization("Start CPU kthread@init");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001613 for (;;) {
1614 *statusp = RCU_KTHREAD_WAITING;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001615 trace_rcu_utilization("End CPU kthread@rcu_wait");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001616 rcu_wait(*workp != 0 || kthread_should_stop());
Paul E. McKenney385680a2011-06-21 22:43:26 -07001617 trace_rcu_utilization("Start CPU kthread@rcu_wait");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001618 local_bh_disable();
1619 if (rcu_cpu_kthread_should_stop(cpu)) {
1620 local_bh_enable();
1621 break;
1622 }
1623 *statusp = RCU_KTHREAD_RUNNING;
1624 per_cpu(rcu_cpu_kthread_loops, cpu)++;
1625 local_irq_save(flags);
1626 work = *workp;
1627 *workp = 0;
1628 local_irq_restore(flags);
1629 if (work)
1630 rcu_kthread_do_work();
1631 local_bh_enable();
1632 if (*workp != 0)
1633 spincnt++;
1634 else
1635 spincnt = 0;
1636 if (spincnt > 10) {
1637 *statusp = RCU_KTHREAD_YIELDING;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001638 trace_rcu_utilization("End CPU kthread@rcu_yield");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001639 rcu_yield(rcu_cpu_kthread_timer, (unsigned long)cpu);
Paul E. McKenney385680a2011-06-21 22:43:26 -07001640 trace_rcu_utilization("Start CPU kthread@rcu_yield");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001641 spincnt = 0;
1642 }
1643 }
1644 *statusp = RCU_KTHREAD_STOPPED;
Paul E. McKenney385680a2011-06-21 22:43:26 -07001645 trace_rcu_utilization("End CPU kthread@term");
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001646 return 0;
1647}
1648
1649/*
1650 * Spawn a per-CPU kthread, setting up affinity and priority.
1651 * Because the CPU hotplug lock is held, no other CPU will be attempting
1652 * to manipulate rcu_cpu_kthread_task. There might be another CPU
1653 * attempting to access it during boot, but the locking in kthread_bind()
1654 * will enforce sufficient ordering.
1655 *
1656 * Please note that we cannot simply refuse to wake up the per-CPU
1657 * kthread because kthreads are created in TASK_UNINTERRUPTIBLE state,
1658 * which can result in softlockup complaints if the task ends up being
1659 * idle for more than a couple of minutes.
1660 *
1661 * However, please note also that we cannot bind the per-CPU kthread to its
1662 * CPU until that CPU is fully online. We also cannot wait until the
1663 * CPU is fully online before we create its per-CPU kthread, as this would
1664 * deadlock the system when CPU notifiers tried waiting for grace
1665 * periods. So we bind the per-CPU kthread to its CPU only if the CPU
1666 * is online. If its CPU is not yet fully online, then the code in
1667 * rcu_cpu_kthread() will wait until it is fully online, and then do
1668 * the binding.
1669 */
1670static int __cpuinit rcu_spawn_one_cpu_kthread(int cpu)
1671{
1672 struct sched_param sp;
1673 struct task_struct *t;
1674
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001675 if (!rcu_scheduler_fully_active ||
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001676 per_cpu(rcu_cpu_kthread_task, cpu) != NULL)
1677 return 0;
Eric Dumazet1f288092011-06-16 15:53:18 -07001678 t = kthread_create_on_node(rcu_cpu_kthread,
1679 (void *)(long)cpu,
1680 cpu_to_node(cpu),
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001681 "rcuc/%d", cpu);
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001682 if (IS_ERR(t))
1683 return PTR_ERR(t);
1684 if (cpu_online(cpu))
1685 kthread_bind(t, cpu);
1686 per_cpu(rcu_cpu_kthread_cpu, cpu) = cpu;
1687 WARN_ON_ONCE(per_cpu(rcu_cpu_kthread_task, cpu) != NULL);
1688 sp.sched_priority = RCU_KTHREAD_PRIO;
1689 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1690 per_cpu(rcu_cpu_kthread_task, cpu) = t;
1691 wake_up_process(t); /* Get to TASK_INTERRUPTIBLE quickly. */
1692 return 0;
1693}
1694
1695/*
1696 * Per-rcu_node kthread, which is in charge of waking up the per-CPU
1697 * kthreads when needed. We ignore requests to wake up kthreads
1698 * for offline CPUs, which is OK because force_quiescent_state()
1699 * takes care of this case.
1700 */
1701static int rcu_node_kthread(void *arg)
1702{
1703 int cpu;
1704 unsigned long flags;
1705 unsigned long mask;
1706 struct rcu_node *rnp = (struct rcu_node *)arg;
1707 struct sched_param sp;
1708 struct task_struct *t;
1709
1710 for (;;) {
1711 rnp->node_kthread_status = RCU_KTHREAD_WAITING;
1712 rcu_wait(atomic_read(&rnp->wakemask) != 0);
1713 rnp->node_kthread_status = RCU_KTHREAD_RUNNING;
1714 raw_spin_lock_irqsave(&rnp->lock, flags);
1715 mask = atomic_xchg(&rnp->wakemask, 0);
1716 rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */
1717 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1) {
1718 if ((mask & 0x1) == 0)
1719 continue;
1720 preempt_disable();
1721 t = per_cpu(rcu_cpu_kthread_task, cpu);
1722 if (!cpu_online(cpu) || t == NULL) {
1723 preempt_enable();
1724 continue;
1725 }
1726 per_cpu(rcu_cpu_has_work, cpu) = 1;
1727 sp.sched_priority = RCU_KTHREAD_PRIO;
1728 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1729 preempt_enable();
1730 }
1731 }
1732 /* NOTREACHED */
1733 rnp->node_kthread_status = RCU_KTHREAD_STOPPED;
1734 return 0;
1735}
1736
1737/*
1738 * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1739 * served by the rcu_node in question. The CPU hotplug lock is still
1740 * held, so the value of rnp->qsmaskinit will be stable.
1741 *
1742 * We don't include outgoingcpu in the affinity set, use -1 if there is
1743 * no outgoing CPU. If there are no CPUs left in the affinity set,
1744 * this function allows the kthread to execute on any CPU.
1745 */
1746static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1747{
1748 cpumask_var_t cm;
1749 int cpu;
1750 unsigned long mask = rnp->qsmaskinit;
1751
1752 if (rnp->node_kthread_task == NULL)
1753 return;
1754 if (!alloc_cpumask_var(&cm, GFP_KERNEL))
1755 return;
1756 cpumask_clear(cm);
1757 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
1758 if ((mask & 0x1) && cpu != outgoingcpu)
1759 cpumask_set_cpu(cpu, cm);
1760 if (cpumask_weight(cm) == 0) {
1761 cpumask_setall(cm);
1762 for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++)
1763 cpumask_clear_cpu(cpu, cm);
1764 WARN_ON_ONCE(cpumask_weight(cm) == 0);
1765 }
1766 set_cpus_allowed_ptr(rnp->node_kthread_task, cm);
1767 rcu_boost_kthread_setaffinity(rnp, cm);
1768 free_cpumask_var(cm);
1769}
1770
1771/*
1772 * Spawn a per-rcu_node kthread, setting priority and affinity.
1773 * Called during boot before online/offline can happen, or, if
1774 * during runtime, with the main CPU-hotplug locks held. So only
1775 * one of these can be executing at a time.
1776 */
1777static int __cpuinit rcu_spawn_one_node_kthread(struct rcu_state *rsp,
1778 struct rcu_node *rnp)
1779{
1780 unsigned long flags;
1781 int rnp_index = rnp - &rsp->node[0];
1782 struct sched_param sp;
1783 struct task_struct *t;
1784
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001785 if (!rcu_scheduler_fully_active ||
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001786 rnp->qsmaskinit == 0)
1787 return 0;
1788 if (rnp->node_kthread_task == NULL) {
1789 t = kthread_create(rcu_node_kthread, (void *)rnp,
Mike Galbraith5b61b0b2011-08-19 11:39:11 -07001790 "rcun/%d", rnp_index);
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001791 if (IS_ERR(t))
1792 return PTR_ERR(t);
1793 raw_spin_lock_irqsave(&rnp->lock, flags);
1794 rnp->node_kthread_task = t;
1795 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1796 sp.sched_priority = 99;
1797 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1798 wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1799 }
1800 return rcu_spawn_one_boost_kthread(rsp, rnp, rnp_index);
1801}
1802
1803/*
1804 * Spawn all kthreads -- called as soon as the scheduler is running.
1805 */
1806static int __init rcu_spawn_kthreads(void)
1807{
1808 int cpu;
1809 struct rcu_node *rnp;
1810
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001811 rcu_scheduler_fully_active = 1;
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001812 for_each_possible_cpu(cpu) {
1813 per_cpu(rcu_cpu_has_work, cpu) = 0;
1814 if (cpu_online(cpu))
1815 (void)rcu_spawn_one_cpu_kthread(cpu);
1816 }
1817 rnp = rcu_get_root(rcu_state);
1818 (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1819 if (NUM_RCU_NODES > 1) {
1820 rcu_for_each_leaf_node(rcu_state, rnp)
1821 (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1822 }
1823 return 0;
1824}
1825early_initcall(rcu_spawn_kthreads);
1826
1827static void __cpuinit rcu_prepare_kthreads(int cpu)
1828{
1829 struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu);
1830 struct rcu_node *rnp = rdp->mynode;
1831
1832 /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001833 if (rcu_scheduler_fully_active) {
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001834 (void)rcu_spawn_one_cpu_kthread(cpu);
1835 if (rnp->node_kthread_task == NULL)
1836 (void)rcu_spawn_one_node_kthread(rcu_state, rnp);
1837 }
1838}
1839
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001840#else /* #ifdef CONFIG_RCU_BOOST */
1841
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001842static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001843{
Paul E. McKenney1217ed12011-05-04 21:43:49 -07001844 raw_spin_unlock_irqrestore(&rnp->lock, flags);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001845}
1846
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001847static void invoke_rcu_callbacks_kthread(void)
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001848{
Paul E. McKenneya46e0892011-06-15 15:47:09 -07001849 WARN_ON_ONCE(1);
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001850}
1851
Paul E. McKenneydff16722011-11-29 15:57:13 -08001852static bool rcu_is_callbacks_kthread(void)
1853{
1854 return false;
1855}
1856
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001857static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1858{
1859}
1860
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001861#ifdef CONFIG_HOTPLUG_CPU
1862
1863static void rcu_stop_cpu_kthread(int cpu)
1864{
1865}
1866
1867#endif /* #ifdef CONFIG_HOTPLUG_CPU */
1868
1869static void rcu_node_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1870{
1871}
1872
1873static void rcu_cpu_kthread_setrt(int cpu, int to_rt)
1874{
1875}
1876
Paul E. McKenneyb0d30412011-07-10 15:57:35 -07001877static int __init rcu_scheduler_really_started(void)
1878{
1879 rcu_scheduler_fully_active = 1;
1880 return 0;
1881}
1882early_initcall(rcu_scheduler_really_started);
1883
Paul E. McKenneyf8b7fc62011-06-16 08:26:32 -07001884static void __cpuinit rcu_prepare_kthreads(int cpu)
1885{
1886}
1887
Paul E. McKenney27f4d282011-02-07 12:47:15 -08001888#endif /* #else #ifdef CONFIG_RCU_BOOST */
1889
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001890#if !defined(CONFIG_RCU_FAST_NO_HZ)
1891
1892/*
1893 * Check to see if any future RCU-related work will need to be done
1894 * by the current CPU, even if none need be done immediately, returning
1895 * 1 if so. This function is part of the RCU implementation; it is -not-
1896 * an exported member of the RCU API.
1897 *
Paul E. McKenney7cb92492011-11-28 12:28:34 -08001898 * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
1899 * any flavor of RCU.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001900 */
Paul E. McKenneyaa9b16302012-05-10 16:41:44 -07001901int rcu_needs_cpu(int cpu, unsigned long *delta_jiffies)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001902{
Paul E. McKenneyaa9b16302012-05-10 16:41:44 -07001903 *delta_jiffies = ULONG_MAX;
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07001904 return rcu_cpu_has_callbacks(cpu);
1905}
1906
1907/*
Paul E. McKenney7cb92492011-11-28 12:28:34 -08001908 * Because we do not have RCU_FAST_NO_HZ, don't bother initializing for it.
1909 */
1910static void rcu_prepare_for_idle_init(int cpu)
1911{
1912}
1913
1914/*
1915 * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1916 * after it.
1917 */
1918static void rcu_cleanup_after_idle(int cpu)
1919{
1920}
1921
1922/*
Paul E. McKenneya858af22012-01-16 13:29:10 -08001923 * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07001924 * is nothing.
1925 */
1926static void rcu_prepare_for_idle(int cpu)
1927{
1928}
1929
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08001930/*
1931 * Don't bother keeping a running count of the number of RCU callbacks
1932 * posted because CONFIG_RCU_FAST_NO_HZ=n.
1933 */
1934static void rcu_idle_count_callbacks_posted(void)
1935{
1936}
1937
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001938#else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1939
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08001940/*
1941 * This code is invoked when a CPU goes idle, at which point we want
1942 * to have the CPU do everything required for RCU so that it can enter
1943 * the energy-efficient dyntick-idle mode. This is handled by a
1944 * state machine implemented by rcu_prepare_for_idle() below.
1945 *
1946 * The following three proprocessor symbols control this state machine:
1947 *
1948 * RCU_IDLE_FLUSHES gives the maximum number of times that we will attempt
1949 * to satisfy RCU. Beyond this point, it is better to incur a periodic
1950 * scheduling-clock interrupt than to loop through the state machine
1951 * at full power.
1952 * RCU_IDLE_OPT_FLUSHES gives the number of RCU_IDLE_FLUSHES that are
1953 * optional if RCU does not need anything immediately from this
1954 * CPU, even if this CPU still has RCU callbacks queued. The first
1955 * times through the state machine are mandatory: we need to give
1956 * the state machine a chance to communicate a quiescent state
1957 * to the RCU core.
1958 * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1959 * to sleep in dyntick-idle mode with RCU callbacks pending. This
1960 * is sized to be roughly one RCU grace period. Those energy-efficiency
1961 * benchmarkers who might otherwise be tempted to set this to a large
1962 * number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1963 * system. And if you are -that- concerned about energy efficiency,
1964 * just power the system down and be done with it!
Paul E. McKenney778d2502012-01-10 14:13:24 -08001965 * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1966 * permitted to sleep in dyntick-idle mode with only lazy RCU
1967 * callbacks pending. Setting this too high can OOM your system.
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08001968 *
1969 * The values below work well in practice. If future workloads require
1970 * adjustment, they can be converted into kernel config parameters, though
1971 * making the state machine smarter might be a better option.
1972 */
1973#define RCU_IDLE_FLUSHES 5 /* Number of dyntick-idle tries. */
1974#define RCU_IDLE_OPT_FLUSHES 3 /* Optional dyntick-idle tries. */
Paul E. McKenney7cb92492011-11-28 12:28:34 -08001975#define RCU_IDLE_GP_DELAY 6 /* Roughly one grace period. */
Paul E. McKenney778d2502012-01-10 14:13:24 -08001976#define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08001977
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08001978/*
Paul E. McKenney486e2592012-01-06 14:11:30 -08001979 * Does the specified flavor of RCU have non-lazy callbacks pending on
1980 * the specified CPU? Both RCU flavor and CPU are specified by the
1981 * rcu_data structure.
1982 */
1983static bool __rcu_cpu_has_nonlazy_callbacks(struct rcu_data *rdp)
1984{
1985 return rdp->qlen != rdp->qlen_lazy;
1986}
1987
1988#ifdef CONFIG_TREE_PREEMPT_RCU
1989
1990/*
1991 * Are there non-lazy RCU-preempt callbacks? (There cannot be if there
1992 * is no RCU-preempt in the kernel.)
1993 */
1994static bool rcu_preempt_cpu_has_nonlazy_callbacks(int cpu)
1995{
1996 struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
1997
1998 return __rcu_cpu_has_nonlazy_callbacks(rdp);
1999}
2000
2001#else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
2002
2003static bool rcu_preempt_cpu_has_nonlazy_callbacks(int cpu)
2004{
2005 return 0;
2006}
2007
2008#endif /* else #ifdef CONFIG_TREE_PREEMPT_RCU */
2009
2010/*
2011 * Does any flavor of RCU have non-lazy callbacks on the specified CPU?
2012 */
2013static bool rcu_cpu_has_nonlazy_callbacks(int cpu)
2014{
2015 return __rcu_cpu_has_nonlazy_callbacks(&per_cpu(rcu_sched_data, cpu)) ||
2016 __rcu_cpu_has_nonlazy_callbacks(&per_cpu(rcu_bh_data, cpu)) ||
2017 rcu_preempt_cpu_has_nonlazy_callbacks(cpu);
2018}
2019
2020/*
Paul E. McKenneyaa9b16302012-05-10 16:41:44 -07002021 * Allow the CPU to enter dyntick-idle mode if either: (1) There are no
2022 * callbacks on this CPU, (2) this CPU has not yet attempted to enter
2023 * dyntick-idle mode, or (3) this CPU is in the process of attempting to
2024 * enter dyntick-idle mode. Otherwise, if we have recently tried and failed
2025 * to enter dyntick-idle mode, we refuse to try to enter it. After all,
2026 * it is better to incur scheduling-clock interrupts than to spin
2027 * continuously for the same time duration!
2028 *
2029 * The delta_jiffies argument is used to store the time when RCU is
2030 * going to need the CPU again if it still has callbacks. The reason
2031 * for this is that rcu_prepare_for_idle() might need to post a timer,
2032 * but if so, it will do so after tick_nohz_stop_sched_tick() has set
2033 * the wakeup time for this CPU. This means that RCU's timer can be
2034 * delayed until the wakeup time, which defeats the purpose of posting
2035 * a timer.
2036 */
2037int rcu_needs_cpu(int cpu, unsigned long *delta_jiffies)
2038{
2039 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2040
2041 /* Flag a new idle sojourn to the idle-entry state machine. */
2042 rdtp->idle_first_pass = 1;
2043 /* If no callbacks, RCU doesn't need the CPU. */
2044 if (!rcu_cpu_has_callbacks(cpu)) {
2045 *delta_jiffies = ULONG_MAX;
2046 return 0;
2047 }
2048 if (rdtp->dyntick_holdoff == jiffies) {
2049 /* RCU recently tried and failed, so don't try again. */
2050 *delta_jiffies = 1;
2051 return 1;
2052 }
2053 /* Set up for the possibility that RCU will post a timer. */
2054 if (rcu_cpu_has_nonlazy_callbacks(cpu))
2055 *delta_jiffies = RCU_IDLE_GP_DELAY;
2056 else
2057 *delta_jiffies = RCU_IDLE_LAZY_GP_DELAY;
2058 return 0;
2059}
2060
2061/*
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002062 * Handler for smp_call_function_single(). The only point of this
2063 * handler is to wake the CPU up, so the handler does only tracing.
2064 */
2065void rcu_idle_demigrate(void *unused)
2066{
2067 trace_rcu_prep_idle("Demigrate");
2068}
2069
2070/*
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002071 * Timer handler used to force CPU to start pushing its remaining RCU
2072 * callbacks in the case where it entered dyntick-idle mode with callbacks
2073 * pending. The hander doesn't really need to do anything because the
2074 * real work is done upon re-entry to idle, or by the next scheduling-clock
2075 * interrupt should idle not be re-entered.
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002076 *
2077 * One special case: the timer gets migrated without awakening the CPU
2078 * on which the timer was scheduled on. In this case, we must wake up
2079 * that CPU. We do so with smp_call_function_single().
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002080 */
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002081static void rcu_idle_gp_timer_func(unsigned long cpu_in)
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002082{
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002083 int cpu = (int)cpu_in;
2084
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002085 trace_rcu_prep_idle("Timer");
Paul E. McKenney21e52e12012-04-30 14:16:19 -07002086 if (cpu != smp_processor_id())
2087 smp_call_function_single(cpu, rcu_idle_demigrate, NULL, 0);
2088 else
2089 WARN_ON_ONCE(1); /* Getting here can hang the system... */
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002090}
2091
2092/*
2093 * Initialize the timer used to pull CPUs out of dyntick-idle mode.
2094 */
2095static void rcu_prepare_for_idle_init(int cpu)
2096{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002097 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2098
2099 rdtp->dyntick_holdoff = jiffies - 1;
2100 setup_timer(&rdtp->idle_gp_timer, rcu_idle_gp_timer_func, cpu);
2101 rdtp->idle_gp_timer_expires = jiffies - 1;
2102 rdtp->idle_first_pass = 1;
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002103}
2104
2105/*
2106 * Clean up for exit from idle. Because we are exiting from idle, there
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002107 * is no longer any point to ->idle_gp_timer, so cancel it. This will
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002108 * do nothing if this timer is not active, so just cancel it unconditionally.
2109 */
2110static void rcu_cleanup_after_idle(int cpu)
2111{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002112 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2113
2114 del_timer(&rdtp->idle_gp_timer);
Paul E. McKenney2fdbb312012-02-23 15:58:29 -08002115 trace_rcu_prep_idle("Cleanup after idle");
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002116}
2117
2118/*
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002119 * Check to see if any RCU-related work can be done by the current CPU,
2120 * and if so, schedule a softirq to get it done. This function is part
2121 * of the RCU implementation; it is -not- an exported member of the RCU API.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002122 *
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002123 * The idea is for the current CPU to clear out all work required by the
2124 * RCU core for the current grace period, so that this CPU can be permitted
2125 * to enter dyntick-idle mode. In some cases, it will need to be awakened
2126 * at the end of the grace period by whatever CPU ends the grace period.
2127 * This allows CPUs to go dyntick-idle more quickly, and to reduce the
2128 * number of wakeups by a modest integer factor.
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002129 *
2130 * Because it is not legal to invoke rcu_process_callbacks() with irqs
2131 * disabled, we do one pass of force_quiescent_state(), then do a
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002132 * invoke_rcu_core() to cause rcu_process_callbacks() to be invoked
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002133 * later. The ->dyntick_drain field controls the sequencing.
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002134 *
2135 * The caller must have disabled interrupts.
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002136 */
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002137static void rcu_prepare_for_idle(int cpu)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002138{
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002139 struct timer_list *tp;
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002140 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002141
Paul E. McKenney3084f2f2011-11-22 17:07:11 -08002142 /*
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002143 * If this is an idle re-entry, for example, due to use of
2144 * RCU_NONIDLE() or the new idle-loop tracing API within the idle
2145 * loop, then don't take any state-machine actions, unless the
2146 * momentary exit from idle queued additional non-lazy callbacks.
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002147 * Instead, repost the ->idle_gp_timer if this CPU has callbacks
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002148 * pending.
2149 */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002150 if (!rdtp->idle_first_pass &&
2151 (rdtp->nonlazy_posted == rdtp->nonlazy_posted_snap)) {
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002152 if (rcu_cpu_has_callbacks(cpu)) {
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002153 tp = &rdtp->idle_gp_timer;
2154 mod_timer_pinned(tp, rdtp->idle_gp_timer_expires);
Paul E. McKenneyf511fc62012-03-15 12:16:26 -07002155 }
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002156 return;
2157 }
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002158 rdtp->idle_first_pass = 0;
2159 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted - 1;
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002160
2161 /*
Paul E. McKenneyf535a602011-11-22 20:43:02 -08002162 * If there are no callbacks on this CPU, enter dyntick-idle mode.
2163 * Also reset state to avoid prejudicing later attempts.
Paul E. McKenney3084f2f2011-11-22 17:07:11 -08002164 */
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002165 if (!rcu_cpu_has_callbacks(cpu)) {
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002166 rdtp->dyntick_holdoff = jiffies - 1;
2167 rdtp->dyntick_drain = 0;
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002168 trace_rcu_prep_idle("No callbacks");
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002169 return;
Paul E. McKenney77e38ed2010-04-25 21:04:29 -07002170 }
Paul E. McKenney3084f2f2011-11-22 17:07:11 -08002171
2172 /*
2173 * If in holdoff mode, just return. We will presumably have
2174 * refrained from disabling the scheduling-clock tick.
2175 */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002176 if (rdtp->dyntick_holdoff == jiffies) {
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002177 trace_rcu_prep_idle("In holdoff");
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002178 return;
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002179 }
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002180
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002181 /* Check and update the ->dyntick_drain sequencing. */
2182 if (rdtp->dyntick_drain <= 0) {
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002183 /* First time through, initialize the counter. */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002184 rdtp->dyntick_drain = RCU_IDLE_FLUSHES;
2185 } else if (rdtp->dyntick_drain <= RCU_IDLE_OPT_FLUSHES &&
Paul E. McKenneyc3ce9102012-02-14 10:12:54 -08002186 !rcu_pending(cpu) &&
2187 !local_softirq_pending()) {
Paul E. McKenney7cb92492011-11-28 12:28:34 -08002188 /* Can we go dyntick-idle despite still having callbacks? */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002189 rdtp->dyntick_drain = 0;
2190 rdtp->dyntick_holdoff = jiffies;
Paul E. McKenneyfd4b3522012-05-05 19:10:35 -07002191 if (rcu_cpu_has_nonlazy_callbacks(cpu)) {
2192 trace_rcu_prep_idle("Dyntick with callbacks");
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002193 rdtp->idle_gp_timer_expires =
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002194 jiffies + RCU_IDLE_GP_DELAY;
Paul E. McKenneyfd4b3522012-05-05 19:10:35 -07002195 } else {
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002196 rdtp->idle_gp_timer_expires =
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002197 jiffies + RCU_IDLE_LAZY_GP_DELAY;
Paul E. McKenneyfd4b3522012-05-05 19:10:35 -07002198 trace_rcu_prep_idle("Dyntick with lazy callbacks");
2199 }
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002200 tp = &rdtp->idle_gp_timer;
2201 mod_timer_pinned(tp, rdtp->idle_gp_timer_expires);
2202 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
Paul E. McKenneyf23f7fa2011-11-30 15:41:14 -08002203 return; /* Nothing more to do immediately. */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002204 } else if (--(rdtp->dyntick_drain) <= 0) {
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002205 /* We have hit the limit, so time to give up. */
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002206 rdtp->dyntick_holdoff = jiffies;
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002207 trace_rcu_prep_idle("Begin holdoff");
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002208 invoke_rcu_core(); /* Force the CPU out of dyntick-idle. */
2209 return;
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002210 }
2211
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002212 /*
2213 * Do one step of pushing the remaining RCU callbacks through
2214 * the RCU core state machine.
2215 */
2216#ifdef CONFIG_TREE_PREEMPT_RCU
2217 if (per_cpu(rcu_preempt_data, cpu).nxtlist) {
2218 rcu_preempt_qs(cpu);
2219 force_quiescent_state(&rcu_preempt_state, 0);
Paul E. McKenneyaea1b352011-11-02 06:54:54 -07002220 }
2221#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002222 if (per_cpu(rcu_sched_data, cpu).nxtlist) {
2223 rcu_sched_qs(cpu);
2224 force_quiescent_state(&rcu_sched_state, 0);
Paul E. McKenneya47cd882010-02-26 16:38:56 -08002225 }
2226 if (per_cpu(rcu_bh_data, cpu).nxtlist) {
2227 rcu_bh_qs(cpu);
2228 force_quiescent_state(&rcu_bh_state, 0);
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002229 }
2230
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002231 /*
2232 * If RCU callbacks are still pending, RCU still needs this CPU.
2233 * So try forcing the callbacks through the grace period.
2234 */
Paul E. McKenney3ad0dec2011-11-22 21:08:13 -08002235 if (rcu_cpu_has_callbacks(cpu)) {
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002236 trace_rcu_prep_idle("More callbacks");
Paul E. McKenneya46e0892011-06-15 15:47:09 -07002237 invoke_rcu_core();
Paul E. McKenneyc0cfbbb2012-01-23 17:23:35 -08002238 } else
Paul E. McKenney433cddd2011-11-22 14:58:03 -08002239 trace_rcu_prep_idle("Callbacks drained");
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002240}
2241
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002242/*
Paul E. McKenney98248a02012-05-03 15:38:10 -07002243 * Keep a running count of the number of non-lazy callbacks posted
2244 * on this CPU. This running counter (which is never decremented) allows
2245 * rcu_prepare_for_idle() to detect when something out of the idle loop
2246 * posts a callback, even if an equal number of callbacks are invoked.
2247 * Of course, callbacks should only be posted from within a trace event
2248 * designed to be called from idle or from within RCU_NONIDLE().
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002249 */
2250static void rcu_idle_count_callbacks_posted(void)
2251{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002252 __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
Paul E. McKenneyc57afe82012-02-28 11:02:21 -08002253}
2254
Paul E. McKenney8bd93a22010-02-22 17:04:59 -08002255#endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
Paul E. McKenneya858af22012-01-16 13:29:10 -08002256
2257#ifdef CONFIG_RCU_CPU_STALL_INFO
2258
2259#ifdef CONFIG_RCU_FAST_NO_HZ
2260
2261static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
2262{
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002263 struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
2264 struct timer_list *tltp = &rdtp->idle_gp_timer;
Paul E. McKenneya858af22012-01-16 13:29:10 -08002265
Paul E. McKenney2ee3dc82012-02-23 17:13:19 -08002266 sprintf(cp, "drain=%d %c timer=%lu",
Paul E. McKenney5955f7e2012-05-09 12:07:05 -07002267 rdtp->dyntick_drain,
2268 rdtp->dyntick_holdoff == jiffies ? 'H' : '.',
Paul E. McKenney2ee3dc82012-02-23 17:13:19 -08002269 timer_pending(tltp) ? tltp->expires - jiffies : -1);
Paul E. McKenneya858af22012-01-16 13:29:10 -08002270}
2271
2272#else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
2273
2274static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
2275{
2276}
2277
2278#endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
2279
2280/* Initiate the stall-info list. */
2281static void print_cpu_stall_info_begin(void)
2282{
2283 printk(KERN_CONT "\n");
2284}
2285
2286/*
2287 * Print out diagnostic information for the specified stalled CPU.
2288 *
2289 * If the specified CPU is aware of the current RCU grace period
2290 * (flavor specified by rsp), then print the number of scheduling
2291 * clock interrupts the CPU has taken during the time that it has
2292 * been aware. Otherwise, print the number of RCU grace periods
2293 * that this CPU is ignorant of, for example, "1" if the CPU was
2294 * aware of the previous grace period.
2295 *
2296 * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
2297 */
2298static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
2299{
2300 char fast_no_hz[72];
2301 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
2302 struct rcu_dynticks *rdtp = rdp->dynticks;
2303 char *ticks_title;
2304 unsigned long ticks_value;
2305
2306 if (rsp->gpnum == rdp->gpnum) {
2307 ticks_title = "ticks this GP";
2308 ticks_value = rdp->ticks_this_gp;
2309 } else {
2310 ticks_title = "GPs behind";
2311 ticks_value = rsp->gpnum - rdp->gpnum;
2312 }
2313 print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
2314 printk(KERN_ERR "\t%d: (%lu %s) idle=%03x/%llx/%d %s\n",
2315 cpu, ticks_value, ticks_title,
2316 atomic_read(&rdtp->dynticks) & 0xfff,
2317 rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
2318 fast_no_hz);
2319}
2320
2321/* Terminate the stall-info list. */
2322static void print_cpu_stall_info_end(void)
2323{
2324 printk(KERN_ERR "\t");
2325}
2326
2327/* Zero ->ticks_this_gp for all flavors of RCU. */
2328static void zero_cpu_stall_ticks(struct rcu_data *rdp)
2329{
2330 rdp->ticks_this_gp = 0;
2331}
2332
2333/* Increment ->ticks_this_gp for all flavors of RCU. */
2334static void increment_cpu_stall_ticks(void)
2335{
2336 __get_cpu_var(rcu_sched_data).ticks_this_gp++;
2337 __get_cpu_var(rcu_bh_data).ticks_this_gp++;
2338#ifdef CONFIG_TREE_PREEMPT_RCU
2339 __get_cpu_var(rcu_preempt_data).ticks_this_gp++;
2340#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
2341}
2342
2343#else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
2344
2345static void print_cpu_stall_info_begin(void)
2346{
2347 printk(KERN_CONT " {");
2348}
2349
2350static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
2351{
2352 printk(KERN_CONT " %d", cpu);
2353}
2354
2355static void print_cpu_stall_info_end(void)
2356{
2357 printk(KERN_CONT "} ");
2358}
2359
2360static void zero_cpu_stall_ticks(struct rcu_data *rdp)
2361{
2362}
2363
2364static void increment_cpu_stall_ticks(void)
2365{
2366}
2367
2368#endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */