Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / kernel / rcu / tree_plugin.h
1 /*
2  * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3  * Internal non-public definitions that provide either classic
4  * or preemptible semantics.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
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
27 #ifdef CONFIG_RCU_BOOST
28
29 #include "../locking/rtmutex_common.h"
30
31 #endif /* #ifdef CONFIG_RCU_BOOST */
32
33 /*
34  * Control variables for per-CPU and per-rcu_node kthreads.  These
35  * handle all flavors of RCU.
36  */
37 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
38 DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
39 DEFINE_PER_CPU(char, rcu_cpu_has_work);
40
41 #ifdef CONFIG_RCU_NOCB_CPU
42 static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
43 static bool have_rcu_nocb_mask;     /* Was rcu_nocb_mask allocated? */
44 static bool __read_mostly rcu_nocb_poll;    /* Offload kthread are to poll. */
45 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
46
47 /*
48  * Check the RCU kernel configuration parameters and print informative
49  * messages about anything out of the ordinary.  If you like #ifdef, you
50  * will love this function.
51  */
52 static void __init rcu_bootup_announce_oddness(void)
53 {
54         if (IS_ENABLED(CONFIG_RCU_TRACE))
55                 pr_info("\tRCU debugfs-based tracing is enabled.\n");
56         if ((IS_ENABLED(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) ||
57             (!IS_ENABLED(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32))
58                 pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
59                        CONFIG_RCU_FANOUT);
60         if (IS_ENABLED(CONFIG_RCU_FANOUT_EXACT))
61                 pr_info("\tHierarchical RCU autobalancing is disabled.\n");
62         if (IS_ENABLED(CONFIG_RCU_FAST_NO_HZ))
63                 pr_info("\tRCU dyntick-idle grace-period acceleration is enabled.\n");
64         if (IS_ENABLED(CONFIG_PROVE_RCU))
65                 pr_info("\tRCU lockdep checking is enabled.\n");
66         if (IS_ENABLED(CONFIG_RCU_TORTURE_TEST_RUNNABLE))
67                 pr_info("\tRCU torture testing starts during boot.\n");
68         if (IS_ENABLED(CONFIG_RCU_CPU_STALL_INFO))
69                 pr_info("\tAdditional per-CPU info printed with stalls.\n");
70         if (NUM_RCU_LVL_4 != 0)
71                 pr_info("\tFour-level hierarchy is enabled.\n");
72         if (CONFIG_RCU_FANOUT_LEAF != 16)
73                 pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
74                         CONFIG_RCU_FANOUT_LEAF);
75         if (rcu_fanout_leaf != CONFIG_RCU_FANOUT_LEAF)
76                 pr_info("\tBoot-time adjustment of leaf fanout to %d.\n", rcu_fanout_leaf);
77         if (nr_cpu_ids != NR_CPUS)
78                 pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%d.\n", NR_CPUS, nr_cpu_ids);
79         if (IS_ENABLED(CONFIG_RCU_BOOST))
80                 pr_info("\tRCU kthread priority: %d.\n", kthread_prio);
81 }
82
83 #ifdef CONFIG_PREEMPT_RCU
84
85 RCU_STATE_INITIALIZER(rcu_preempt, 'p', call_rcu);
86 static struct rcu_state *rcu_state_p = &rcu_preempt_state;
87
88 static int rcu_preempted_readers_exp(struct rcu_node *rnp);
89 static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
90                                bool wake);
91
92 /*
93  * Tell them what RCU they are running.
94  */
95 static void __init rcu_bootup_announce(void)
96 {
97         pr_info("Preemptible hierarchical RCU implementation.\n");
98         rcu_bootup_announce_oddness();
99 }
100
101 /*
102  * Record a preemptible-RCU quiescent state for the specified CPU.  Note
103  * that this just means that the task currently running on the CPU is
104  * not in a quiescent state.  There might be any number of tasks blocked
105  * while in an RCU read-side critical section.
106  *
107  * As with the other rcu_*_qs() functions, callers to this function
108  * must disable preemption.
109  */
110 static void rcu_preempt_qs(void)
111 {
112         if (!__this_cpu_read(rcu_preempt_data.passed_quiesce)) {
113                 trace_rcu_grace_period(TPS("rcu_preempt"),
114                                        __this_cpu_read(rcu_preempt_data.gpnum),
115                                        TPS("cpuqs"));
116                 __this_cpu_write(rcu_preempt_data.passed_quiesce, 1);
117                 barrier(); /* Coordinate with rcu_preempt_check_callbacks(). */
118                 current->rcu_read_unlock_special.b.need_qs = false;
119         }
120 }
121
122 /*
123  * We have entered the scheduler, and the current task might soon be
124  * context-switched away from.  If this task is in an RCU read-side
125  * critical section, we will no longer be able to rely on the CPU to
126  * record that fact, so we enqueue the task on the blkd_tasks list.
127  * The task will dequeue itself when it exits the outermost enclosing
128  * RCU read-side critical section.  Therefore, the current grace period
129  * cannot be permitted to complete until the blkd_tasks list entries
130  * predating the current grace period drain, in other words, until
131  * rnp->gp_tasks becomes NULL.
132  *
133  * Caller must disable preemption.
134  */
135 static void rcu_preempt_note_context_switch(void)
136 {
137         struct task_struct *t = current;
138         unsigned long flags;
139         struct rcu_data *rdp;
140         struct rcu_node *rnp;
141
142         if (t->rcu_read_lock_nesting > 0 &&
143             !t->rcu_read_unlock_special.b.blocked) {
144
145                 /* Possibly blocking in an RCU read-side critical section. */
146                 rdp = this_cpu_ptr(rcu_preempt_state.rda);
147                 rnp = rdp->mynode;
148                 raw_spin_lock_irqsave(&rnp->lock, flags);
149                 smp_mb__after_unlock_lock();
150                 t->rcu_read_unlock_special.b.blocked = true;
151                 t->rcu_blocked_node = rnp;
152
153                 /*
154                  * If this CPU has already checked in, then this task
155                  * will hold up the next grace period rather than the
156                  * current grace period.  Queue the task accordingly.
157                  * If the task is queued for the current grace period
158                  * (i.e., this CPU has not yet passed through a quiescent
159                  * state for the current grace period), then as long
160                  * as that task remains queued, the current grace period
161                  * cannot end.  Note that there is some uncertainty as
162                  * to exactly when the current grace period started.
163                  * We take a conservative approach, which can result
164                  * in unnecessarily waiting on tasks that started very
165                  * slightly after the current grace period began.  C'est
166                  * la vie!!!
167                  *
168                  * But first, note that the current CPU must still be
169                  * on line!
170                  */
171                 WARN_ON_ONCE((rdp->grpmask & rcu_rnp_online_cpus(rnp)) == 0);
172                 WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
173                 if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
174                         list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
175                         rnp->gp_tasks = &t->rcu_node_entry;
176 #ifdef CONFIG_RCU_BOOST
177                         if (rnp->boost_tasks != NULL)
178                                 rnp->boost_tasks = rnp->gp_tasks;
179 #endif /* #ifdef CONFIG_RCU_BOOST */
180                 } else {
181                         list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
182                         if (rnp->qsmask & rdp->grpmask)
183                                 rnp->gp_tasks = &t->rcu_node_entry;
184                 }
185                 trace_rcu_preempt_task(rdp->rsp->name,
186                                        t->pid,
187                                        (rnp->qsmask & rdp->grpmask)
188                                        ? rnp->gpnum
189                                        : rnp->gpnum + 1);
190                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
191         } else if (t->rcu_read_lock_nesting < 0 &&
192                    t->rcu_read_unlock_special.s) {
193
194                 /*
195                  * Complete exit from RCU read-side critical section on
196                  * behalf of preempted instance of __rcu_read_unlock().
197                  */
198                 rcu_read_unlock_special(t);
199         }
200
201         /*
202          * Either we were not in an RCU read-side critical section to
203          * begin with, or we have now recorded that critical section
204          * globally.  Either way, we can now note a quiescent state
205          * for this CPU.  Again, if we were in an RCU read-side critical
206          * section, and if that critical section was blocking the current
207          * grace period, then the fact that the task has been enqueued
208          * means that we continue to block the current grace period.
209          */
210         rcu_preempt_qs();
211 }
212
213 /*
214  * Check for preempted RCU readers blocking the current grace period
215  * for the specified rcu_node structure.  If the caller needs a reliable
216  * answer, it must hold the rcu_node's ->lock.
217  */
218 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
219 {
220         return rnp->gp_tasks != NULL;
221 }
222
223 /*
224  * Advance a ->blkd_tasks-list pointer to the next entry, instead
225  * returning NULL if at the end of the list.
226  */
227 static struct list_head *rcu_next_node_entry(struct task_struct *t,
228                                              struct rcu_node *rnp)
229 {
230         struct list_head *np;
231
232         np = t->rcu_node_entry.next;
233         if (np == &rnp->blkd_tasks)
234                 np = NULL;
235         return np;
236 }
237
238 /*
239  * Return true if the specified rcu_node structure has tasks that were
240  * preempted within an RCU read-side critical section.
241  */
242 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
243 {
244         return !list_empty(&rnp->blkd_tasks);
245 }
246
247 /*
248  * Handle special cases during rcu_read_unlock(), such as needing to
249  * notify RCU core processing or task having blocked during the RCU
250  * read-side critical section.
251  */
252 void rcu_read_unlock_special(struct task_struct *t)
253 {
254         bool empty_exp;
255         bool empty_norm;
256         bool empty_exp_now;
257         unsigned long flags;
258         struct list_head *np;
259 #ifdef CONFIG_RCU_BOOST
260         bool drop_boost_mutex = false;
261 #endif /* #ifdef CONFIG_RCU_BOOST */
262         struct rcu_node *rnp;
263         union rcu_special special;
264
265         /* NMI handlers cannot block and cannot safely manipulate state. */
266         if (in_nmi())
267                 return;
268
269         local_irq_save(flags);
270
271         /*
272          * If RCU core is waiting for this CPU to exit critical section,
273          * let it know that we have done so.  Because irqs are disabled,
274          * t->rcu_read_unlock_special cannot change.
275          */
276         special = t->rcu_read_unlock_special;
277         if (special.b.need_qs) {
278                 rcu_preempt_qs();
279                 t->rcu_read_unlock_special.b.need_qs = false;
280                 if (!t->rcu_read_unlock_special.s) {
281                         local_irq_restore(flags);
282                         return;
283                 }
284         }
285
286         /* Hardware IRQ handlers cannot block, complain if they get here. */
287         if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
288                 lockdep_rcu_suspicious(__FILE__, __LINE__,
289                                        "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
290                 pr_alert("->rcu_read_unlock_special: %#x (b: %d, nq: %d)\n",
291                          t->rcu_read_unlock_special.s,
292                          t->rcu_read_unlock_special.b.blocked,
293                          t->rcu_read_unlock_special.b.need_qs);
294                 local_irq_restore(flags);
295                 return;
296         }
297
298         /* Clean up if blocked during RCU read-side critical section. */
299         if (special.b.blocked) {
300                 t->rcu_read_unlock_special.b.blocked = false;
301
302                 /*
303                  * Remove this task from the list it blocked on.  The
304                  * task can migrate while we acquire the lock, but at
305                  * most one time.  So at most two passes through loop.
306                  */
307                 for (;;) {
308                         rnp = t->rcu_blocked_node;
309                         raw_spin_lock(&rnp->lock);  /* irqs already disabled. */
310                         smp_mb__after_unlock_lock();
311                         if (rnp == t->rcu_blocked_node)
312                                 break;
313                         raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
314                 }
315                 empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
316                 empty_exp = !rcu_preempted_readers_exp(rnp);
317                 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
318                 np = rcu_next_node_entry(t, rnp);
319                 list_del_init(&t->rcu_node_entry);
320                 t->rcu_blocked_node = NULL;
321                 trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
322                                                 rnp->gpnum, t->pid);
323                 if (&t->rcu_node_entry == rnp->gp_tasks)
324                         rnp->gp_tasks = np;
325                 if (&t->rcu_node_entry == rnp->exp_tasks)
326                         rnp->exp_tasks = np;
327 #ifdef CONFIG_RCU_BOOST
328                 if (&t->rcu_node_entry == rnp->boost_tasks)
329                         rnp->boost_tasks = np;
330                 /* Snapshot ->boost_mtx ownership with rcu_node lock held. */
331                 drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx) == t;
332 #endif /* #ifdef CONFIG_RCU_BOOST */
333
334                 /*
335                  * If this was the last task on the current list, and if
336                  * we aren't waiting on any CPUs, report the quiescent state.
337                  * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
338                  * so we must take a snapshot of the expedited state.
339                  */
340                 empty_exp_now = !rcu_preempted_readers_exp(rnp);
341                 if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
342                         trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
343                                                          rnp->gpnum,
344                                                          0, rnp->qsmask,
345                                                          rnp->level,
346                                                          rnp->grplo,
347                                                          rnp->grphi,
348                                                          !!rnp->gp_tasks);
349                         rcu_report_unblock_qs_rnp(&rcu_preempt_state,
350                                                   rnp, flags);
351                 } else {
352                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
353                 }
354
355 #ifdef CONFIG_RCU_BOOST
356                 /* Unboost if we were boosted. */
357                 if (drop_boost_mutex)
358                         rt_mutex_unlock(&rnp->boost_mtx);
359 #endif /* #ifdef CONFIG_RCU_BOOST */
360
361                 /*
362                  * If this was the last task on the expedited lists,
363                  * then we need to report up the rcu_node hierarchy.
364                  */
365                 if (!empty_exp && empty_exp_now)
366                         rcu_report_exp_rnp(&rcu_preempt_state, rnp, true);
367         } else {
368                 local_irq_restore(flags);
369         }
370 }
371
372 /*
373  * Dump detailed information for all tasks blocking the current RCU
374  * grace period on the specified rcu_node structure.
375  */
376 static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
377 {
378         unsigned long flags;
379         struct task_struct *t;
380
381         raw_spin_lock_irqsave(&rnp->lock, flags);
382         if (!rcu_preempt_blocked_readers_cgp(rnp)) {
383                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
384                 return;
385         }
386         t = list_entry(rnp->gp_tasks,
387                        struct task_struct, rcu_node_entry);
388         list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
389                 sched_show_task(t);
390         raw_spin_unlock_irqrestore(&rnp->lock, flags);
391 }
392
393 /*
394  * Dump detailed information for all tasks blocking the current RCU
395  * grace period.
396  */
397 static void rcu_print_detail_task_stall(struct rcu_state *rsp)
398 {
399         struct rcu_node *rnp = rcu_get_root(rsp);
400
401         rcu_print_detail_task_stall_rnp(rnp);
402         rcu_for_each_leaf_node(rsp, rnp)
403                 rcu_print_detail_task_stall_rnp(rnp);
404 }
405
406 #ifdef CONFIG_RCU_CPU_STALL_INFO
407
408 static void rcu_print_task_stall_begin(struct rcu_node *rnp)
409 {
410         pr_err("\tTasks blocked on level-%d rcu_node (CPUs %d-%d):",
411                rnp->level, rnp->grplo, rnp->grphi);
412 }
413
414 static void rcu_print_task_stall_end(void)
415 {
416         pr_cont("\n");
417 }
418
419 #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
420
421 static void rcu_print_task_stall_begin(struct rcu_node *rnp)
422 {
423 }
424
425 static void rcu_print_task_stall_end(void)
426 {
427 }
428
429 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
430
431 /*
432  * Scan the current list of tasks blocked within RCU read-side critical
433  * sections, printing out the tid of each.
434  */
435 static int rcu_print_task_stall(struct rcu_node *rnp)
436 {
437         struct task_struct *t;
438         int ndetected = 0;
439
440         if (!rcu_preempt_blocked_readers_cgp(rnp))
441                 return 0;
442         rcu_print_task_stall_begin(rnp);
443         t = list_entry(rnp->gp_tasks,
444                        struct task_struct, rcu_node_entry);
445         list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) {
446                 pr_cont(" P%d", t->pid);
447                 ndetected++;
448         }
449         rcu_print_task_stall_end();
450         return ndetected;
451 }
452
453 /*
454  * Check that the list of blocked tasks for the newly completed grace
455  * period is in fact empty.  It is a serious bug to complete a grace
456  * period that still has RCU readers blocked!  This function must be
457  * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
458  * must be held by the caller.
459  *
460  * Also, if there are blocked tasks on the list, they automatically
461  * block the newly created grace period, so set up ->gp_tasks accordingly.
462  */
463 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
464 {
465         WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
466         if (rcu_preempt_has_tasks(rnp))
467                 rnp->gp_tasks = rnp->blkd_tasks.next;
468         WARN_ON_ONCE(rnp->qsmask);
469 }
470
471 /*
472  * Check for a quiescent state from the current CPU.  When a task blocks,
473  * the task is recorded in the corresponding CPU's rcu_node structure,
474  * which is checked elsewhere.
475  *
476  * Caller must disable hard irqs.
477  */
478 static void rcu_preempt_check_callbacks(void)
479 {
480         struct task_struct *t = current;
481
482         if (t->rcu_read_lock_nesting == 0) {
483                 rcu_preempt_qs();
484                 return;
485         }
486         if (t->rcu_read_lock_nesting > 0 &&
487             __this_cpu_read(rcu_preempt_data.qs_pending) &&
488             !__this_cpu_read(rcu_preempt_data.passed_quiesce))
489                 t->rcu_read_unlock_special.b.need_qs = true;
490 }
491
492 /*
493  * Queue a preemptible-RCU callback for invocation after a grace period.
494  */
495 void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
496 {
497         __call_rcu(head, func, &rcu_preempt_state, -1, 0);
498 }
499 EXPORT_SYMBOL_GPL(call_rcu);
500
501 /**
502  * synchronize_rcu - wait until a grace period has elapsed.
503  *
504  * Control will return to the caller some time after a full grace
505  * period has elapsed, in other words after all currently executing RCU
506  * read-side critical sections have completed.  Note, however, that
507  * upon return from synchronize_rcu(), the caller might well be executing
508  * concurrently with new RCU read-side critical sections that began while
509  * synchronize_rcu() was waiting.  RCU read-side critical sections are
510  * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
511  *
512  * See the description of synchronize_sched() for more detailed information
513  * on memory ordering guarantees.
514  */
515 void synchronize_rcu(void)
516 {
517         rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
518                            !lock_is_held(&rcu_lock_map) &&
519                            !lock_is_held(&rcu_sched_lock_map),
520                            "Illegal synchronize_rcu() in RCU read-side critical section");
521         if (!rcu_scheduler_active)
522                 return;
523         if (rcu_gp_is_expedited())
524                 synchronize_rcu_expedited();
525         else
526                 wait_rcu_gp(call_rcu);
527 }
528 EXPORT_SYMBOL_GPL(synchronize_rcu);
529
530 static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
531 static unsigned long sync_rcu_preempt_exp_count;
532 static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
533
534 /*
535  * Return non-zero if there are any tasks in RCU read-side critical
536  * sections blocking the current preemptible-RCU expedited grace period.
537  * If there is no preemptible-RCU expedited grace period currently in
538  * progress, returns zero unconditionally.
539  */
540 static int rcu_preempted_readers_exp(struct rcu_node *rnp)
541 {
542         return rnp->exp_tasks != NULL;
543 }
544
545 /*
546  * return non-zero if there is no RCU expedited grace period in progress
547  * for the specified rcu_node structure, in other words, if all CPUs and
548  * tasks covered by the specified rcu_node structure have done their bit
549  * for the current expedited grace period.  Works only for preemptible
550  * RCU -- other RCU implementation use other means.
551  *
552  * Caller must hold sync_rcu_preempt_exp_mutex.
553  */
554 static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
555 {
556         return !rcu_preempted_readers_exp(rnp) &&
557                ACCESS_ONCE(rnp->expmask) == 0;
558 }
559
560 /*
561  * Report the exit from RCU read-side critical section for the last task
562  * that queued itself during or before the current expedited preemptible-RCU
563  * grace period.  This event is reported either to the rcu_node structure on
564  * which the task was queued or to one of that rcu_node structure's ancestors,
565  * recursively up the tree.  (Calm down, calm down, we do the recursion
566  * iteratively!)
567  *
568  * Caller must hold sync_rcu_preempt_exp_mutex.
569  */
570 static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
571                                bool wake)
572 {
573         unsigned long flags;
574         unsigned long mask;
575
576         raw_spin_lock_irqsave(&rnp->lock, flags);
577         smp_mb__after_unlock_lock();
578         for (;;) {
579                 if (!sync_rcu_preempt_exp_done(rnp)) {
580                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
581                         break;
582                 }
583                 if (rnp->parent == NULL) {
584                         raw_spin_unlock_irqrestore(&rnp->lock, flags);
585                         if (wake) {
586                                 smp_mb(); /* EGP done before wake_up(). */
587                                 wake_up(&sync_rcu_preempt_exp_wq);
588                         }
589                         break;
590                 }
591                 mask = rnp->grpmask;
592                 raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
593                 rnp = rnp->parent;
594                 raw_spin_lock(&rnp->lock); /* irqs already disabled */
595                 smp_mb__after_unlock_lock();
596                 rnp->expmask &= ~mask;
597         }
598 }
599
600 /*
601  * Snapshot the tasks blocking the newly started preemptible-RCU expedited
602  * grace period for the specified rcu_node structure, phase 1.  If there
603  * are such tasks, set the ->expmask bits up the rcu_node tree and also
604  * set the ->expmask bits on the leaf rcu_node structures to tell phase 2
605  * that work is needed here.
606  *
607  * Caller must hold sync_rcu_preempt_exp_mutex.
608  */
609 static void
610 sync_rcu_preempt_exp_init1(struct rcu_state *rsp, struct rcu_node *rnp)
611 {
612         unsigned long flags;
613         unsigned long mask;
614         struct rcu_node *rnp_up;
615
616         raw_spin_lock_irqsave(&rnp->lock, flags);
617         smp_mb__after_unlock_lock();
618         WARN_ON_ONCE(rnp->expmask);
619         WARN_ON_ONCE(rnp->exp_tasks);
620         if (!rcu_preempt_has_tasks(rnp)) {
621                 /* No blocked tasks, nothing to do. */
622                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
623                 return;
624         }
625         /* Call for Phase 2 and propagate ->expmask bits up the tree. */
626         rnp->expmask = 1;
627         rnp_up = rnp;
628         while (rnp_up->parent) {
629                 mask = rnp_up->grpmask;
630                 rnp_up = rnp_up->parent;
631                 if (rnp_up->expmask & mask)
632                         break;
633                 raw_spin_lock(&rnp_up->lock); /* irqs already off */
634                 smp_mb__after_unlock_lock();
635                 rnp_up->expmask |= mask;
636                 raw_spin_unlock(&rnp_up->lock); /* irqs still off */
637         }
638         raw_spin_unlock_irqrestore(&rnp->lock, flags);
639 }
640
641 /*
642  * Snapshot the tasks blocking the newly started preemptible-RCU expedited
643  * grace period for the specified rcu_node structure, phase 2.  If the
644  * leaf rcu_node structure has its ->expmask field set, check for tasks.
645  * If there are some, clear ->expmask and set ->exp_tasks accordingly,
646  * then initiate RCU priority boosting.  Otherwise, clear ->expmask and
647  * invoke rcu_report_exp_rnp() to clear out the upper-level ->expmask bits,
648  * enabling rcu_read_unlock_special() to do the bit-clearing.
649  *
650  * Caller must hold sync_rcu_preempt_exp_mutex.
651  */
652 static void
653 sync_rcu_preempt_exp_init2(struct rcu_state *rsp, struct rcu_node *rnp)
654 {
655         unsigned long flags;
656
657         raw_spin_lock_irqsave(&rnp->lock, flags);
658         smp_mb__after_unlock_lock();
659         if (!rnp->expmask) {
660                 /* Phase 1 didn't do anything, so Phase 2 doesn't either. */
661                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
662                 return;
663         }
664
665         /* Phase 1 is over. */
666         rnp->expmask = 0;
667
668         /*
669          * If there are still blocked tasks, set up ->exp_tasks so that
670          * rcu_read_unlock_special() will wake us and then boost them.
671          */
672         if (rcu_preempt_has_tasks(rnp)) {
673                 rnp->exp_tasks = rnp->blkd_tasks.next;
674                 rcu_initiate_boost(rnp, flags);  /* releases rnp->lock */
675                 return;
676         }
677
678         /* No longer any blocked tasks, so undo bit setting. */
679         raw_spin_unlock_irqrestore(&rnp->lock, flags);
680         rcu_report_exp_rnp(rsp, rnp, false);
681 }
682
683 /**
684  * synchronize_rcu_expedited - Brute-force RCU grace period
685  *
686  * Wait for an RCU-preempt grace period, but expedite it.  The basic
687  * idea is to invoke synchronize_sched_expedited() to push all the tasks to
688  * the ->blkd_tasks lists and wait for this list to drain.  This consumes
689  * significant time on all CPUs and is unfriendly to real-time workloads,
690  * so is thus not recommended for any sort of common-case code.
691  * In fact, if you are using synchronize_rcu_expedited() in a loop,
692  * please restructure your code to batch your updates, and then Use a
693  * single synchronize_rcu() instead.
694  */
695 void synchronize_rcu_expedited(void)
696 {
697         struct rcu_node *rnp;
698         struct rcu_state *rsp = &rcu_preempt_state;
699         unsigned long snap;
700         int trycount = 0;
701
702         smp_mb(); /* Caller's modifications seen first by other CPUs. */
703         snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
704         smp_mb(); /* Above access cannot bleed into critical section. */
705
706         /*
707          * Block CPU-hotplug operations.  This means that any CPU-hotplug
708          * operation that finds an rcu_node structure with tasks in the
709          * process of being boosted will know that all tasks blocking
710          * this expedited grace period will already be in the process of
711          * being boosted.  This simplifies the process of moving tasks
712          * from leaf to root rcu_node structures.
713          */
714         if (!try_get_online_cpus()) {
715                 /* CPU-hotplug operation in flight, fall back to normal GP. */
716                 wait_rcu_gp(call_rcu);
717                 return;
718         }
719
720         /*
721          * Acquire lock, falling back to synchronize_rcu() if too many
722          * lock-acquisition failures.  Of course, if someone does the
723          * expedited grace period for us, just leave.
724          */
725         while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
726                 if (ULONG_CMP_LT(snap,
727                     ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
728                         put_online_cpus();
729                         goto mb_ret; /* Others did our work for us. */
730                 }
731                 if (trycount++ < 10) {
732                         udelay(trycount * num_online_cpus());
733                 } else {
734                         put_online_cpus();
735                         wait_rcu_gp(call_rcu);
736                         return;
737                 }
738         }
739         if (ULONG_CMP_LT(snap, ACCESS_ONCE(sync_rcu_preempt_exp_count))) {
740                 put_online_cpus();
741                 goto unlock_mb_ret; /* Others did our work for us. */
742         }
743
744         /* force all RCU readers onto ->blkd_tasks lists. */
745         synchronize_sched_expedited();
746
747         /*
748          * Snapshot current state of ->blkd_tasks lists into ->expmask.
749          * Phase 1 sets bits and phase 2 permits rcu_read_unlock_special()
750          * to start clearing them.  Doing this in one phase leads to
751          * strange races between setting and clearing bits, so just say "no"!
752          */
753         rcu_for_each_leaf_node(rsp, rnp)
754                 sync_rcu_preempt_exp_init1(rsp, rnp);
755         rcu_for_each_leaf_node(rsp, rnp)
756                 sync_rcu_preempt_exp_init2(rsp, rnp);
757
758         put_online_cpus();
759
760         /* Wait for snapshotted ->blkd_tasks lists to drain. */
761         rnp = rcu_get_root(rsp);
762         wait_event(sync_rcu_preempt_exp_wq,
763                    sync_rcu_preempt_exp_done(rnp));
764
765         /* Clean up and exit. */
766         smp_mb(); /* ensure expedited GP seen before counter increment. */
767         ACCESS_ONCE(sync_rcu_preempt_exp_count) =
768                                         sync_rcu_preempt_exp_count + 1;
769 unlock_mb_ret:
770         mutex_unlock(&sync_rcu_preempt_exp_mutex);
771 mb_ret:
772         smp_mb(); /* ensure subsequent action seen after grace period. */
773 }
774 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
775
776 /**
777  * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
778  *
779  * Note that this primitive does not necessarily wait for an RCU grace period
780  * to complete.  For example, if there are no RCU callbacks queued anywhere
781  * in the system, then rcu_barrier() is within its rights to return
782  * immediately, without waiting for anything, much less an RCU grace period.
783  */
784 void rcu_barrier(void)
785 {
786         _rcu_barrier(&rcu_preempt_state);
787 }
788 EXPORT_SYMBOL_GPL(rcu_barrier);
789
790 /*
791  * Initialize preemptible RCU's state structures.
792  */
793 static void __init __rcu_init_preempt(void)
794 {
795         rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
796 }
797
798 /*
799  * Check for a task exiting while in a preemptible-RCU read-side
800  * critical section, clean up if so.  No need to issue warnings,
801  * as debug_check_no_locks_held() already does this if lockdep
802  * is enabled.
803  */
804 void exit_rcu(void)
805 {
806         struct task_struct *t = current;
807
808         if (likely(list_empty(&current->rcu_node_entry)))
809                 return;
810         t->rcu_read_lock_nesting = 1;
811         barrier();
812         t->rcu_read_unlock_special.b.blocked = true;
813         __rcu_read_unlock();
814 }
815
816 #else /* #ifdef CONFIG_PREEMPT_RCU */
817
818 static struct rcu_state *rcu_state_p = &rcu_sched_state;
819
820 /*
821  * Tell them what RCU they are running.
822  */
823 static void __init rcu_bootup_announce(void)
824 {
825         pr_info("Hierarchical RCU implementation.\n");
826         rcu_bootup_announce_oddness();
827 }
828
829 /*
830  * Because preemptible RCU does not exist, we never have to check for
831  * CPUs being in quiescent states.
832  */
833 static void rcu_preempt_note_context_switch(void)
834 {
835 }
836
837 /*
838  * Because preemptible RCU does not exist, there are never any preempted
839  * RCU readers.
840  */
841 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
842 {
843         return 0;
844 }
845
846 /*
847  * Because there is no preemptible RCU, there can be no readers blocked.
848  */
849 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
850 {
851         return false;
852 }
853
854 /*
855  * Because preemptible RCU does not exist, we never have to check for
856  * tasks blocked within RCU read-side critical sections.
857  */
858 static void rcu_print_detail_task_stall(struct rcu_state *rsp)
859 {
860 }
861
862 /*
863  * Because preemptible RCU does not exist, we never have to check for
864  * tasks blocked within RCU read-side critical sections.
865  */
866 static int rcu_print_task_stall(struct rcu_node *rnp)
867 {
868         return 0;
869 }
870
871 /*
872  * Because there is no preemptible RCU, there can be no readers blocked,
873  * so there is no need to check for blocked tasks.  So check only for
874  * bogus qsmask values.
875  */
876 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
877 {
878         WARN_ON_ONCE(rnp->qsmask);
879 }
880
881 /*
882  * Because preemptible RCU does not exist, it never has any callbacks
883  * to check.
884  */
885 static void rcu_preempt_check_callbacks(void)
886 {
887 }
888
889 /*
890  * Wait for an rcu-preempt grace period, but make it happen quickly.
891  * But because preemptible RCU does not exist, map to rcu-sched.
892  */
893 void synchronize_rcu_expedited(void)
894 {
895         synchronize_sched_expedited();
896 }
897 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
898
899 /*
900  * Because preemptible RCU does not exist, rcu_barrier() is just
901  * another name for rcu_barrier_sched().
902  */
903 void rcu_barrier(void)
904 {
905         rcu_barrier_sched();
906 }
907 EXPORT_SYMBOL_GPL(rcu_barrier);
908
909 /*
910  * Because preemptible RCU does not exist, it need not be initialized.
911  */
912 static void __init __rcu_init_preempt(void)
913 {
914 }
915
916 /*
917  * Because preemptible RCU does not exist, tasks cannot possibly exit
918  * while in preemptible RCU read-side critical sections.
919  */
920 void exit_rcu(void)
921 {
922 }
923
924 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
925
926 /*
927  * If boosting, set rcuc kthreads to realtime priority.
928  */
929 static void rcu_cpu_kthread_setup(unsigned int cpu)
930 {
931 #ifdef CONFIG_RCU_BOOST
932         struct sched_param sp;
933
934         sp.sched_priority = kthread_prio;
935         sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
936 #endif /* #ifdef CONFIG_RCU_BOOST */
937 }
938
939 #ifdef CONFIG_RCU_BOOST
940
941 #include "../locking/rtmutex_common.h"
942
943 #ifdef CONFIG_RCU_TRACE
944
945 static void rcu_initiate_boost_trace(struct rcu_node *rnp)
946 {
947         if (!rcu_preempt_has_tasks(rnp))
948                 rnp->n_balk_blkd_tasks++;
949         else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
950                 rnp->n_balk_exp_gp_tasks++;
951         else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
952                 rnp->n_balk_boost_tasks++;
953         else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
954                 rnp->n_balk_notblocked++;
955         else if (rnp->gp_tasks != NULL &&
956                  ULONG_CMP_LT(jiffies, rnp->boost_time))
957                 rnp->n_balk_notyet++;
958         else
959                 rnp->n_balk_nos++;
960 }
961
962 #else /* #ifdef CONFIG_RCU_TRACE */
963
964 static void rcu_initiate_boost_trace(struct rcu_node *rnp)
965 {
966 }
967
968 #endif /* #else #ifdef CONFIG_RCU_TRACE */
969
970 /*
971  * Carry out RCU priority boosting on the task indicated by ->exp_tasks
972  * or ->boost_tasks, advancing the pointer to the next task in the
973  * ->blkd_tasks list.
974  *
975  * Note that irqs must be enabled: boosting the task can block.
976  * Returns 1 if there are more tasks needing to be boosted.
977  */
978 static int rcu_boost(struct rcu_node *rnp)
979 {
980         unsigned long flags;
981         struct task_struct *t;
982         struct list_head *tb;
983
984         if (ACCESS_ONCE(rnp->exp_tasks) == NULL &&
985             ACCESS_ONCE(rnp->boost_tasks) == NULL)
986                 return 0;  /* Nothing left to boost. */
987
988         raw_spin_lock_irqsave(&rnp->lock, flags);
989         smp_mb__after_unlock_lock();
990
991         /*
992          * Recheck under the lock: all tasks in need of boosting
993          * might exit their RCU read-side critical sections on their own.
994          */
995         if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
996                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
997                 return 0;
998         }
999
1000         /*
1001          * Preferentially boost tasks blocking expedited grace periods.
1002          * This cannot starve the normal grace periods because a second
1003          * expedited grace period must boost all blocked tasks, including
1004          * those blocking the pre-existing normal grace period.
1005          */
1006         if (rnp->exp_tasks != NULL) {
1007                 tb = rnp->exp_tasks;
1008                 rnp->n_exp_boosts++;
1009         } else {
1010                 tb = rnp->boost_tasks;
1011                 rnp->n_normal_boosts++;
1012         }
1013         rnp->n_tasks_boosted++;
1014
1015         /*
1016          * We boost task t by manufacturing an rt_mutex that appears to
1017          * be held by task t.  We leave a pointer to that rt_mutex where
1018          * task t can find it, and task t will release the mutex when it
1019          * exits its outermost RCU read-side critical section.  Then
1020          * simply acquiring this artificial rt_mutex will boost task
1021          * t's priority.  (Thanks to tglx for suggesting this approach!)
1022          *
1023          * Note that task t must acquire rnp->lock to remove itself from
1024          * the ->blkd_tasks list, which it will do from exit() if from
1025          * nowhere else.  We therefore are guaranteed that task t will
1026          * stay around at least until we drop rnp->lock.  Note that
1027          * rnp->lock also resolves races between our priority boosting
1028          * and task t's exiting its outermost RCU read-side critical
1029          * section.
1030          */
1031         t = container_of(tb, struct task_struct, rcu_node_entry);
1032         rt_mutex_init_proxy_locked(&rnp->boost_mtx, t);
1033         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1034         /* Lock only for side effect: boosts task t's priority. */
1035         rt_mutex_lock(&rnp->boost_mtx);
1036         rt_mutex_unlock(&rnp->boost_mtx);  /* Then keep lockdep happy. */
1037
1038         return ACCESS_ONCE(rnp->exp_tasks) != NULL ||
1039                ACCESS_ONCE(rnp->boost_tasks) != NULL;
1040 }
1041
1042 /*
1043  * Priority-boosting kthread.  One per leaf rcu_node and one for the
1044  * root rcu_node.
1045  */
1046 static int rcu_boost_kthread(void *arg)
1047 {
1048         struct rcu_node *rnp = (struct rcu_node *)arg;
1049         int spincnt = 0;
1050         int more2boost;
1051
1052         trace_rcu_utilization(TPS("Start boost kthread@init"));
1053         for (;;) {
1054                 rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
1055                 trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
1056                 rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
1057                 trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
1058                 rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
1059                 more2boost = rcu_boost(rnp);
1060                 if (more2boost)
1061                         spincnt++;
1062                 else
1063                         spincnt = 0;
1064                 if (spincnt > 10) {
1065                         rnp->boost_kthread_status = RCU_KTHREAD_YIELDING;
1066                         trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
1067                         schedule_timeout_interruptible(2);
1068                         trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
1069                         spincnt = 0;
1070                 }
1071         }
1072         /* NOTREACHED */
1073         trace_rcu_utilization(TPS("End boost kthread@notreached"));
1074         return 0;
1075 }
1076
1077 /*
1078  * Check to see if it is time to start boosting RCU readers that are
1079  * blocking the current grace period, and, if so, tell the per-rcu_node
1080  * kthread to start boosting them.  If there is an expedited grace
1081  * period in progress, it is always time to boost.
1082  *
1083  * The caller must hold rnp->lock, which this function releases.
1084  * The ->boost_kthread_task is immortal, so we don't need to worry
1085  * about it going away.
1086  */
1087 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1088         __releases(rnp->lock)
1089 {
1090         struct task_struct *t;
1091
1092         if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
1093                 rnp->n_balk_exp_gp_tasks++;
1094                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1095                 return;
1096         }
1097         if (rnp->exp_tasks != NULL ||
1098             (rnp->gp_tasks != NULL &&
1099              rnp->boost_tasks == NULL &&
1100              rnp->qsmask == 0 &&
1101              ULONG_CMP_GE(jiffies, rnp->boost_time))) {
1102                 if (rnp->exp_tasks == NULL)
1103                         rnp->boost_tasks = rnp->gp_tasks;
1104                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1105                 t = rnp->boost_kthread_task;
1106                 if (t)
1107                         rcu_wake_cond(t, rnp->boost_kthread_status);
1108         } else {
1109                 rcu_initiate_boost_trace(rnp);
1110                 raw_spin_unlock_irqrestore(&rnp->lock, flags);
1111         }
1112 }
1113
1114 /*
1115  * Is the current CPU running the RCU-callbacks kthread?
1116  * Caller must have preemption disabled.
1117  */
1118 static bool rcu_is_callbacks_kthread(void)
1119 {
1120         return __this_cpu_read(rcu_cpu_kthread_task) == current;
1121 }
1122
1123 #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1124
1125 /*
1126  * Do priority-boost accounting for the start of a new grace period.
1127  */
1128 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1129 {
1130         rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1131 }
1132
1133 /*
1134  * Create an RCU-boost kthread for the specified node if one does not
1135  * already exist.  We only create this kthread for preemptible RCU.
1136  * Returns zero if all is well, a negated errno otherwise.
1137  */
1138 static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
1139                                        struct rcu_node *rnp)
1140 {
1141         int rnp_index = rnp - &rsp->node[0];
1142         unsigned long flags;
1143         struct sched_param sp;
1144         struct task_struct *t;
1145
1146         if (&rcu_preempt_state != rsp)
1147                 return 0;
1148
1149         if (!rcu_scheduler_fully_active || rcu_rnp_online_cpus(rnp) == 0)
1150                 return 0;
1151
1152         rsp->boost = 1;
1153         if (rnp->boost_kthread_task != NULL)
1154                 return 0;
1155         t = kthread_create(rcu_boost_kthread, (void *)rnp,
1156                            "rcub/%d", rnp_index);
1157         if (IS_ERR(t))
1158                 return PTR_ERR(t);
1159         raw_spin_lock_irqsave(&rnp->lock, flags);
1160         smp_mb__after_unlock_lock();
1161         rnp->boost_kthread_task = t;
1162         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1163         sp.sched_priority = kthread_prio;
1164         sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1165         wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1166         return 0;
1167 }
1168
1169 /*
1170  * Set the per-rcu_node kthread's affinity to cover all CPUs that are
1171  * served by the rcu_node in question.  The CPU hotplug lock is still
1172  * held, so the value of rnp->qsmaskinit will be stable.
1173  *
1174  * We don't include outgoingcpu in the affinity set, use -1 if there is
1175  * no outgoing CPU.  If there are no CPUs left in the affinity set,
1176  * this function allows the kthread to execute on any CPU.
1177  */
1178 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1179 {
1180         struct task_struct *t = rnp->boost_kthread_task;
1181         unsigned long mask = rcu_rnp_online_cpus(rnp);
1182         cpumask_var_t cm;
1183         int cpu;
1184
1185         if (!t)
1186                 return;
1187         if (!zalloc_cpumask_var(&cm, GFP_KERNEL))
1188                 return;
1189         for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++, mask >>= 1)
1190                 if ((mask & 0x1) && cpu != outgoingcpu)
1191                         cpumask_set_cpu(cpu, cm);
1192         if (cpumask_weight(cm) == 0)
1193                 cpumask_setall(cm);
1194         set_cpus_allowed_ptr(t, cm);
1195         free_cpumask_var(cm);
1196 }
1197
1198 /*
1199  * Spawn boost kthreads -- called as soon as the scheduler is running.
1200  */
1201 static void __init rcu_spawn_boost_kthreads(void)
1202 {
1203         struct rcu_node *rnp;
1204         rcu_for_each_leaf_node(rcu_state_p, rnp)
1205                 (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
1206 }
1207
1208 static void rcu_prepare_kthreads(int cpu)
1209 {
1210         struct rcu_data *rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
1211         struct rcu_node *rnp = rdp->mynode;
1212
1213         /* Fire up the incoming CPU's kthread and leaf rcu_node kthread. */
1214         if (rcu_scheduler_fully_active)
1215                 (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
1216 }
1217
1218 #else /* #ifdef CONFIG_RCU_BOOST */
1219
1220 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1221         __releases(rnp->lock)
1222 {
1223         raw_spin_unlock_irqrestore(&rnp->lock, flags);
1224 }
1225
1226 static bool rcu_is_callbacks_kthread(void)
1227 {
1228         return false;
1229 }
1230
1231 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1232 {
1233 }
1234
1235 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
1236 {
1237 }
1238
1239 static void __init rcu_spawn_boost_kthreads(void)
1240 {
1241 }
1242
1243 static void rcu_prepare_kthreads(int cpu)
1244 {
1245 }
1246
1247 #endif /* #else #ifdef CONFIG_RCU_BOOST */
1248
1249 #if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
1250
1251 /*
1252  * Check to see if any future RCU-related work will need to be done
1253  * by the current CPU, even if none need be done immediately, returning
1254  * 1 if so.  This function is part of the RCU implementation; it is -not-
1255  * an exported member of the RCU API.
1256  *
1257  * Because we not have RCU_FAST_NO_HZ, just check whether this CPU needs
1258  * any flavor of RCU.
1259  */
1260 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1261 int rcu_needs_cpu(unsigned long *delta_jiffies)
1262 {
1263         *delta_jiffies = ULONG_MAX;
1264         return rcu_cpu_has_callbacks(NULL);
1265 }
1266 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1267 #endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
1268
1269 #if !defined(CONFIG_RCU_FAST_NO_HZ)
1270 /*
1271  * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
1272  * after it.
1273  */
1274 static void rcu_cleanup_after_idle(void)
1275 {
1276 }
1277
1278 /*
1279  * Do the idle-entry grace-period work, which, because CONFIG_RCU_FAST_NO_HZ=n,
1280  * is nothing.
1281  */
1282 static void rcu_prepare_for_idle(void)
1283 {
1284 }
1285
1286 /*
1287  * Don't bother keeping a running count of the number of RCU callbacks
1288  * posted because CONFIG_RCU_FAST_NO_HZ=n.
1289  */
1290 static void rcu_idle_count_callbacks_posted(void)
1291 {
1292 }
1293
1294 #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1295
1296 /*
1297  * This code is invoked when a CPU goes idle, at which point we want
1298  * to have the CPU do everything required for RCU so that it can enter
1299  * the energy-efficient dyntick-idle mode.  This is handled by a
1300  * state machine implemented by rcu_prepare_for_idle() below.
1301  *
1302  * The following three proprocessor symbols control this state machine:
1303  *
1304  * RCU_IDLE_GP_DELAY gives the number of jiffies that a CPU is permitted
1305  *      to sleep in dyntick-idle mode with RCU callbacks pending.  This
1306  *      is sized to be roughly one RCU grace period.  Those energy-efficiency
1307  *      benchmarkers who might otherwise be tempted to set this to a large
1308  *      number, be warned: Setting RCU_IDLE_GP_DELAY too high can hang your
1309  *      system.  And if you are -that- concerned about energy efficiency,
1310  *      just power the system down and be done with it!
1311  * RCU_IDLE_LAZY_GP_DELAY gives the number of jiffies that a CPU is
1312  *      permitted to sleep in dyntick-idle mode with only lazy RCU
1313  *      callbacks pending.  Setting this too high can OOM your system.
1314  *
1315  * The values below work well in practice.  If future workloads require
1316  * adjustment, they can be converted into kernel config parameters, though
1317  * making the state machine smarter might be a better option.
1318  */
1319 #define RCU_IDLE_GP_DELAY 4             /* Roughly one grace period. */
1320 #define RCU_IDLE_LAZY_GP_DELAY (6 * HZ) /* Roughly six seconds. */
1321
1322 static int rcu_idle_gp_delay = RCU_IDLE_GP_DELAY;
1323 module_param(rcu_idle_gp_delay, int, 0644);
1324 static int rcu_idle_lazy_gp_delay = RCU_IDLE_LAZY_GP_DELAY;
1325 module_param(rcu_idle_lazy_gp_delay, int, 0644);
1326
1327 extern int tick_nohz_active;
1328
1329 /*
1330  * Try to advance callbacks for all flavors of RCU on the current CPU, but
1331  * only if it has been awhile since the last time we did so.  Afterwards,
1332  * if there are any callbacks ready for immediate invocation, return true.
1333  */
1334 static bool __maybe_unused rcu_try_advance_all_cbs(void)
1335 {
1336         bool cbs_ready = false;
1337         struct rcu_data *rdp;
1338         struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
1339         struct rcu_node *rnp;
1340         struct rcu_state *rsp;
1341
1342         /* Exit early if we advanced recently. */
1343         if (jiffies == rdtp->last_advance_all)
1344                 return false;
1345         rdtp->last_advance_all = jiffies;
1346
1347         for_each_rcu_flavor(rsp) {
1348                 rdp = this_cpu_ptr(rsp->rda);
1349                 rnp = rdp->mynode;
1350
1351                 /*
1352                  * Don't bother checking unless a grace period has
1353                  * completed since we last checked and there are
1354                  * callbacks not yet ready to invoke.
1355                  */
1356                 if ((rdp->completed != rnp->completed ||
1357                      unlikely(ACCESS_ONCE(rdp->gpwrap))) &&
1358                     rdp->nxttail[RCU_DONE_TAIL] != rdp->nxttail[RCU_NEXT_TAIL])
1359                         note_gp_changes(rsp, rdp);
1360
1361                 if (cpu_has_callbacks_ready_to_invoke(rdp))
1362                         cbs_ready = true;
1363         }
1364         return cbs_ready;
1365 }
1366
1367 #ifndef CONFIG_PREEMPT_RT_FULL
1368
1369 /*
1370  * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
1371  * to invoke.  If the CPU has callbacks, try to advance them.  Tell the
1372  * caller to set the timeout based on whether or not there are non-lazy
1373  * callbacks.
1374  *
1375  * The caller must have disabled interrupts.
1376  */
1377 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1378 int rcu_needs_cpu(unsigned long *dj)
1379 {
1380         struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
1381
1382         /* Snapshot to detect later posting of non-lazy callback. */
1383         rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
1384
1385         /* If no callbacks, RCU doesn't need the CPU. */
1386         if (!rcu_cpu_has_callbacks(&rdtp->all_lazy)) {
1387                 *dj = ULONG_MAX;
1388                 return 0;
1389         }
1390
1391         /* Attempt to advance callbacks. */
1392         if (rcu_try_advance_all_cbs()) {
1393                 /* Some ready to invoke, so initiate later invocation. */
1394                 invoke_rcu_core();
1395                 return 1;
1396         }
1397         rdtp->last_accelerate = jiffies;
1398
1399         /* Request timer delay depending on laziness, and round. */
1400         if (!rdtp->all_lazy) {
1401                 *dj = round_up(rcu_idle_gp_delay + jiffies,
1402                                rcu_idle_gp_delay) - jiffies;
1403         } else {
1404                 *dj = round_jiffies(rcu_idle_lazy_gp_delay + jiffies) - jiffies;
1405         }
1406         return 0;
1407 }
1408 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1409 #endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
1410 /*
1411  * Prepare a CPU for idle from an RCU perspective.  The first major task
1412  * is to sense whether nohz mode has been enabled or disabled via sysfs.
1413  * The second major task is to check to see if a non-lazy callback has
1414  * arrived at a CPU that previously had only lazy callbacks.  The third
1415  * major task is to accelerate (that is, assign grace-period numbers to)
1416  * any recently arrived callbacks.
1417  *
1418  * The caller must have disabled interrupts.
1419  */
1420 static void rcu_prepare_for_idle(void)
1421 {
1422 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1423         bool needwake;
1424         struct rcu_data *rdp;
1425         struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
1426         struct rcu_node *rnp;
1427         struct rcu_state *rsp;
1428         int tne;
1429
1430         /* Handle nohz enablement switches conservatively. */
1431         tne = ACCESS_ONCE(tick_nohz_active);
1432         if (tne != rdtp->tick_nohz_enabled_snap) {
1433                 if (rcu_cpu_has_callbacks(NULL))
1434                         invoke_rcu_core(); /* force nohz to see update. */
1435                 rdtp->tick_nohz_enabled_snap = tne;
1436                 return;
1437         }
1438         if (!tne)
1439                 return;
1440
1441         /* If this is a no-CBs CPU, no callbacks, just return. */
1442         if (rcu_is_nocb_cpu(smp_processor_id()))
1443                 return;
1444
1445         /*
1446          * If a non-lazy callback arrived at a CPU having only lazy
1447          * callbacks, invoke RCU core for the side-effect of recalculating
1448          * idle duration on re-entry to idle.
1449          */
1450         if (rdtp->all_lazy &&
1451             rdtp->nonlazy_posted != rdtp->nonlazy_posted_snap) {
1452                 rdtp->all_lazy = false;
1453                 rdtp->nonlazy_posted_snap = rdtp->nonlazy_posted;
1454                 invoke_rcu_core();
1455                 return;
1456         }
1457
1458         /*
1459          * If we have not yet accelerated this jiffy, accelerate all
1460          * callbacks on this CPU.
1461          */
1462         if (rdtp->last_accelerate == jiffies)
1463                 return;
1464         rdtp->last_accelerate = jiffies;
1465         for_each_rcu_flavor(rsp) {
1466                 rdp = this_cpu_ptr(rsp->rda);
1467                 if (!*rdp->nxttail[RCU_DONE_TAIL])
1468                         continue;
1469                 rnp = rdp->mynode;
1470                 raw_spin_lock(&rnp->lock); /* irqs already disabled. */
1471                 smp_mb__after_unlock_lock();
1472                 needwake = rcu_accelerate_cbs(rsp, rnp, rdp);
1473                 raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
1474                 if (needwake)
1475                         rcu_gp_kthread_wake(rsp);
1476         }
1477 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1478 }
1479
1480 /*
1481  * Clean up for exit from idle.  Attempt to advance callbacks based on
1482  * any grace periods that elapsed while the CPU was idle, and if any
1483  * callbacks are now ready to invoke, initiate invocation.
1484  */
1485 static void rcu_cleanup_after_idle(void)
1486 {
1487 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1488         if (rcu_is_nocb_cpu(smp_processor_id()))
1489                 return;
1490         if (rcu_try_advance_all_cbs())
1491                 invoke_rcu_core();
1492 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1493 }
1494
1495 /*
1496  * Keep a running count of the number of non-lazy callbacks posted
1497  * on this CPU.  This running counter (which is never decremented) allows
1498  * rcu_prepare_for_idle() to detect when something out of the idle loop
1499  * posts a callback, even if an equal number of callbacks are invoked.
1500  * Of course, callbacks should only be posted from within a trace event
1501  * designed to be called from idle or from within RCU_NONIDLE().
1502  */
1503 static void rcu_idle_count_callbacks_posted(void)
1504 {
1505         __this_cpu_add(rcu_dynticks.nonlazy_posted, 1);
1506 }
1507
1508 /*
1509  * Data for flushing lazy RCU callbacks at OOM time.
1510  */
1511 static atomic_t oom_callback_count;
1512 static DECLARE_WAIT_QUEUE_HEAD(oom_callback_wq);
1513
1514 /*
1515  * RCU OOM callback -- decrement the outstanding count and deliver the
1516  * wake-up if we are the last one.
1517  */
1518 static void rcu_oom_callback(struct rcu_head *rhp)
1519 {
1520         if (atomic_dec_and_test(&oom_callback_count))
1521                 wake_up(&oom_callback_wq);
1522 }
1523
1524 /*
1525  * Post an rcu_oom_notify callback on the current CPU if it has at
1526  * least one lazy callback.  This will unnecessarily post callbacks
1527  * to CPUs that already have a non-lazy callback at the end of their
1528  * callback list, but this is an infrequent operation, so accept some
1529  * extra overhead to keep things simple.
1530  */
1531 static void rcu_oom_notify_cpu(void *unused)
1532 {
1533         struct rcu_state *rsp;
1534         struct rcu_data *rdp;
1535
1536         for_each_rcu_flavor(rsp) {
1537                 rdp = raw_cpu_ptr(rsp->rda);
1538                 if (rdp->qlen_lazy != 0) {
1539                         atomic_inc(&oom_callback_count);
1540                         rsp->call(&rdp->oom_head, rcu_oom_callback);
1541                 }
1542         }
1543 }
1544
1545 /*
1546  * If low on memory, ensure that each CPU has a non-lazy callback.
1547  * This will wake up CPUs that have only lazy callbacks, in turn
1548  * ensuring that they free up the corresponding memory in a timely manner.
1549  * Because an uncertain amount of memory will be freed in some uncertain
1550  * timeframe, we do not claim to have freed anything.
1551  */
1552 static int rcu_oom_notify(struct notifier_block *self,
1553                           unsigned long notused, void *nfreed)
1554 {
1555         int cpu;
1556
1557         /* Wait for callbacks from earlier instance to complete. */
1558         wait_event(oom_callback_wq, atomic_read(&oom_callback_count) == 0);
1559         smp_mb(); /* Ensure callback reuse happens after callback invocation. */
1560
1561         /*
1562          * Prevent premature wakeup: ensure that all increments happen
1563          * before there is a chance of the counter reaching zero.
1564          */
1565         atomic_set(&oom_callback_count, 1);
1566
1567         get_online_cpus();
1568         for_each_online_cpu(cpu) {
1569                 smp_call_function_single(cpu, rcu_oom_notify_cpu, NULL, 1);
1570                 cond_resched_rcu_qs();
1571         }
1572         put_online_cpus();
1573
1574         /* Unconditionally decrement: no need to wake ourselves up. */
1575         atomic_dec(&oom_callback_count);
1576
1577         return NOTIFY_OK;
1578 }
1579
1580 static struct notifier_block rcu_oom_nb = {
1581         .notifier_call = rcu_oom_notify
1582 };
1583
1584 static int __init rcu_register_oom_notifier(void)
1585 {
1586         register_oom_notifier(&rcu_oom_nb);
1587         return 0;
1588 }
1589 early_initcall(rcu_register_oom_notifier);
1590
1591 #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1592
1593 #ifdef CONFIG_RCU_CPU_STALL_INFO
1594
1595 #ifdef CONFIG_RCU_FAST_NO_HZ
1596
1597 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1598 {
1599         struct rcu_dynticks *rdtp = &per_cpu(rcu_dynticks, cpu);
1600         unsigned long nlpd = rdtp->nonlazy_posted - rdtp->nonlazy_posted_snap;
1601
1602         sprintf(cp, "last_accelerate: %04lx/%04lx, nonlazy_posted: %ld, %c%c",
1603                 rdtp->last_accelerate & 0xffff, jiffies & 0xffff,
1604                 ulong2long(nlpd),
1605                 rdtp->all_lazy ? 'L' : '.',
1606                 rdtp->tick_nohz_enabled_snap ? '.' : 'D');
1607 }
1608
1609 #else /* #ifdef CONFIG_RCU_FAST_NO_HZ */
1610
1611 static void print_cpu_stall_fast_no_hz(char *cp, int cpu)
1612 {
1613         *cp = '\0';
1614 }
1615
1616 #endif /* #else #ifdef CONFIG_RCU_FAST_NO_HZ */
1617
1618 /* Initiate the stall-info list. */
1619 static void print_cpu_stall_info_begin(void)
1620 {
1621         pr_cont("\n");
1622 }
1623
1624 /*
1625  * Print out diagnostic information for the specified stalled CPU.
1626  *
1627  * If the specified CPU is aware of the current RCU grace period
1628  * (flavor specified by rsp), then print the number of scheduling
1629  * clock interrupts the CPU has taken during the time that it has
1630  * been aware.  Otherwise, print the number of RCU grace periods
1631  * that this CPU is ignorant of, for example, "1" if the CPU was
1632  * aware of the previous grace period.
1633  *
1634  * Also print out idle and (if CONFIG_RCU_FAST_NO_HZ) idle-entry info.
1635  */
1636 static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
1637 {
1638         char fast_no_hz[72];
1639         struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1640         struct rcu_dynticks *rdtp = rdp->dynticks;
1641         char *ticks_title;
1642         unsigned long ticks_value;
1643
1644         if (rsp->gpnum == rdp->gpnum) {
1645                 ticks_title = "ticks this GP";
1646                 ticks_value = rdp->ticks_this_gp;
1647         } else {
1648                 ticks_title = "GPs behind";
1649                 ticks_value = rsp->gpnum - rdp->gpnum;
1650         }
1651         print_cpu_stall_fast_no_hz(fast_no_hz, cpu);
1652         pr_err("\t%d: (%lu %s) idle=%03x/%llx/%d softirq=%u/%u fqs=%ld %s\n",
1653                cpu, ticks_value, ticks_title,
1654                atomic_read(&rdtp->dynticks) & 0xfff,
1655                rdtp->dynticks_nesting, rdtp->dynticks_nmi_nesting,
1656                rdp->softirq_snap, kstat_softirqs_cpu(RCU_SOFTIRQ, cpu),
1657                ACCESS_ONCE(rsp->n_force_qs) - rsp->n_force_qs_gpstart,
1658                fast_no_hz);
1659 }
1660
1661 /* Terminate the stall-info list. */
1662 static void print_cpu_stall_info_end(void)
1663 {
1664         pr_err("\t");
1665 }
1666
1667 /* Zero ->ticks_this_gp for all flavors of RCU. */
1668 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
1669 {
1670         rdp->ticks_this_gp = 0;
1671         rdp->softirq_snap = kstat_softirqs_cpu(RCU_SOFTIRQ, smp_processor_id());
1672 }
1673
1674 /* Increment ->ticks_this_gp for all flavors of RCU. */
1675 static void increment_cpu_stall_ticks(void)
1676 {
1677         struct rcu_state *rsp;
1678
1679         for_each_rcu_flavor(rsp)
1680                 raw_cpu_inc(rsp->rda->ticks_this_gp);
1681 }
1682
1683 #else /* #ifdef CONFIG_RCU_CPU_STALL_INFO */
1684
1685 static void print_cpu_stall_info_begin(void)
1686 {
1687         pr_cont(" {");
1688 }
1689
1690 static void print_cpu_stall_info(struct rcu_state *rsp, int cpu)
1691 {
1692         pr_cont(" %d", cpu);
1693 }
1694
1695 static void print_cpu_stall_info_end(void)
1696 {
1697         pr_cont("} ");
1698 }
1699
1700 static void zero_cpu_stall_ticks(struct rcu_data *rdp)
1701 {
1702 }
1703
1704 static void increment_cpu_stall_ticks(void)
1705 {
1706 }
1707
1708 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_INFO */
1709
1710 #ifdef CONFIG_RCU_NOCB_CPU
1711
1712 /*
1713  * Offload callback processing from the boot-time-specified set of CPUs
1714  * specified by rcu_nocb_mask.  For each CPU in the set, there is a
1715  * kthread created that pulls the callbacks from the corresponding CPU,
1716  * waits for a grace period to elapse, and invokes the callbacks.
1717  * The no-CBs CPUs do a wake_up() on their kthread when they insert
1718  * a callback into any empty list, unless the rcu_nocb_poll boot parameter
1719  * has been specified, in which case each kthread actively polls its
1720  * CPU.  (Which isn't so great for energy efficiency, but which does
1721  * reduce RCU's overhead on that CPU.)
1722  *
1723  * This is intended to be used in conjunction with Frederic Weisbecker's
1724  * adaptive-idle work, which would seriously reduce OS jitter on CPUs
1725  * running CPU-bound user-mode computations.
1726  *
1727  * Offloading of callback processing could also in theory be used as
1728  * an energy-efficiency measure because CPUs with no RCU callbacks
1729  * queued are more aggressive about entering dyntick-idle mode.
1730  */
1731
1732
1733 /* Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters. */
1734 static int __init rcu_nocb_setup(char *str)
1735 {
1736         alloc_bootmem_cpumask_var(&rcu_nocb_mask);
1737         have_rcu_nocb_mask = true;
1738         cpulist_parse(str, rcu_nocb_mask);
1739         return 1;
1740 }
1741 __setup("rcu_nocbs=", rcu_nocb_setup);
1742
1743 static int __init parse_rcu_nocb_poll(char *arg)
1744 {
1745         rcu_nocb_poll = 1;
1746         return 0;
1747 }
1748 early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
1749
1750 /*
1751  * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
1752  * grace period.
1753  */
1754 static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
1755 {
1756         swait_wake_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
1757 }
1758
1759 /*
1760  * Set the root rcu_node structure's ->need_future_gp field
1761  * based on the sum of those of all rcu_node structures.  This does
1762  * double-count the root rcu_node structure's requests, but this
1763  * is necessary to handle the possibility of a rcu_nocb_kthread()
1764  * having awakened during the time that the rcu_node structures
1765  * were being updated for the end of the previous grace period.
1766  */
1767 static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
1768 {
1769         rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
1770 }
1771
1772 static void rcu_init_one_nocb(struct rcu_node *rnp)
1773 {
1774         init_swait_head(&rnp->nocb_gp_wq[0]);
1775         init_swait_head(&rnp->nocb_gp_wq[1]);
1776 }
1777
1778 #ifndef CONFIG_RCU_NOCB_CPU_ALL
1779 /* Is the specified CPU a no-CBs CPU? */
1780 bool rcu_is_nocb_cpu(int cpu)
1781 {
1782         if (have_rcu_nocb_mask)
1783                 return cpumask_test_cpu(cpu, rcu_nocb_mask);
1784         return false;
1785 }
1786 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_ALL */
1787
1788 /*
1789  * Kick the leader kthread for this NOCB group.
1790  */
1791 static void wake_nocb_leader(struct rcu_data *rdp, bool force)
1792 {
1793         struct rcu_data *rdp_leader = rdp->nocb_leader;
1794
1795         if (!ACCESS_ONCE(rdp_leader->nocb_kthread))
1796                 return;
1797         if (ACCESS_ONCE(rdp_leader->nocb_leader_sleep) || force) {
1798                 /* Prior smp_mb__after_atomic() orders against prior enqueue. */
1799                 ACCESS_ONCE(rdp_leader->nocb_leader_sleep) = false;
1800                 swait_wake(&rdp_leader->nocb_wq);
1801         }
1802 }
1803
1804 /*
1805  * Does the specified CPU need an RCU callback for the specified flavor
1806  * of rcu_barrier()?
1807  */
1808 static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
1809 {
1810         struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
1811         unsigned long ret;
1812 #ifdef CONFIG_PROVE_RCU
1813         struct rcu_head *rhp;
1814 #endif /* #ifdef CONFIG_PROVE_RCU */
1815
1816         /*
1817          * Check count of all no-CBs callbacks awaiting invocation.
1818          * There needs to be a barrier before this function is called,
1819          * but associated with a prior determination that no more
1820          * callbacks would be posted.  In the worst case, the first
1821          * barrier in _rcu_barrier() suffices (but the caller cannot
1822          * necessarily rely on this, not a substitute for the caller
1823          * getting the concurrency design right!).  There must also be
1824          * a barrier between the following load an posting of a callback
1825          * (if a callback is in fact needed).  This is associated with an
1826          * atomic_inc() in the caller.
1827          */
1828         ret = atomic_long_read(&rdp->nocb_q_count);
1829
1830 #ifdef CONFIG_PROVE_RCU
1831         rhp = ACCESS_ONCE(rdp->nocb_head);
1832         if (!rhp)
1833                 rhp = ACCESS_ONCE(rdp->nocb_gp_head);
1834         if (!rhp)
1835                 rhp = ACCESS_ONCE(rdp->nocb_follower_head);
1836
1837         /* Having no rcuo kthread but CBs after scheduler starts is bad! */
1838         if (!ACCESS_ONCE(rdp->nocb_kthread) && rhp &&
1839             rcu_scheduler_fully_active) {
1840                 /* RCU callback enqueued before CPU first came online??? */
1841                 pr_err("RCU: Never-onlined no-CBs CPU %d has CB %p\n",
1842                        cpu, rhp->func);
1843                 WARN_ON_ONCE(1);
1844         }
1845 #endif /* #ifdef CONFIG_PROVE_RCU */
1846
1847         return !!ret;
1848 }
1849
1850 /*
1851  * Enqueue the specified string of rcu_head structures onto the specified
1852  * CPU's no-CBs lists.  The CPU is specified by rdp, the head of the
1853  * string by rhp, and the tail of the string by rhtp.  The non-lazy/lazy
1854  * counts are supplied by rhcount and rhcount_lazy.
1855  *
1856  * If warranted, also wake up the kthread servicing this CPUs queues.
1857  */
1858 static void __call_rcu_nocb_enqueue(struct rcu_data *rdp,
1859                                     struct rcu_head *rhp,
1860                                     struct rcu_head **rhtp,
1861                                     int rhcount, int rhcount_lazy,
1862                                     unsigned long flags)
1863 {
1864         int len;
1865         struct rcu_head **old_rhpp;
1866         struct task_struct *t;
1867
1868         /* Enqueue the callback on the nocb list and update counts. */
1869         atomic_long_add(rhcount, &rdp->nocb_q_count);
1870         /* rcu_barrier() relies on ->nocb_q_count add before xchg. */
1871         old_rhpp = xchg(&rdp->nocb_tail, rhtp);
1872         ACCESS_ONCE(*old_rhpp) = rhp;
1873         atomic_long_add(rhcount_lazy, &rdp->nocb_q_count_lazy);
1874         smp_mb__after_atomic(); /* Store *old_rhpp before _wake test. */
1875
1876         /* If we are not being polled and there is a kthread, awaken it ... */
1877         t = ACCESS_ONCE(rdp->nocb_kthread);
1878         if (rcu_nocb_poll || !t) {
1879                 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1880                                     TPS("WakeNotPoll"));
1881                 return;
1882         }
1883         len = atomic_long_read(&rdp->nocb_q_count);
1884         if (old_rhpp == &rdp->nocb_head) {
1885                 if (!irqs_disabled_flags(flags)) {
1886                         /* ... if queue was empty ... */
1887                         wake_nocb_leader(rdp, false);
1888                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1889                                             TPS("WakeEmpty"));
1890                 } else {
1891                         rdp->nocb_defer_wakeup = RCU_NOGP_WAKE;
1892                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1893                                             TPS("WakeEmptyIsDeferred"));
1894                 }
1895                 rdp->qlen_last_fqs_check = 0;
1896         } else if (len > rdp->qlen_last_fqs_check + qhimark) {
1897                 /* ... or if many callbacks queued. */
1898                 if (!irqs_disabled_flags(flags)) {
1899                         wake_nocb_leader(rdp, true);
1900                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1901                                             TPS("WakeOvf"));
1902                 } else {
1903                         rdp->nocb_defer_wakeup = RCU_NOGP_WAKE_FORCE;
1904                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
1905                                             TPS("WakeOvfIsDeferred"));
1906                 }
1907                 rdp->qlen_last_fqs_check = LONG_MAX / 2;
1908         } else {
1909                 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("WakeNot"));
1910         }
1911         return;
1912 }
1913
1914 /*
1915  * This is a helper for __call_rcu(), which invokes this when the normal
1916  * callback queue is inoperable.  If this is not a no-CBs CPU, this
1917  * function returns failure back to __call_rcu(), which can complain
1918  * appropriately.
1919  *
1920  * Otherwise, this function queues the callback where the corresponding
1921  * "rcuo" kthread can find it.
1922  */
1923 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
1924                             bool lazy, unsigned long flags)
1925 {
1926
1927         if (!rcu_is_nocb_cpu(rdp->cpu))
1928                 return false;
1929         __call_rcu_nocb_enqueue(rdp, rhp, &rhp->next, 1, lazy, flags);
1930         if (__is_kfree_rcu_offset((unsigned long)rhp->func))
1931                 trace_rcu_kfree_callback(rdp->rsp->name, rhp,
1932                                          (unsigned long)rhp->func,
1933                                          -atomic_long_read(&rdp->nocb_q_count_lazy),
1934                                          -atomic_long_read(&rdp->nocb_q_count));
1935         else
1936                 trace_rcu_callback(rdp->rsp->name, rhp,
1937                                    -atomic_long_read(&rdp->nocb_q_count_lazy),
1938                                    -atomic_long_read(&rdp->nocb_q_count));
1939
1940         /*
1941          * If called from an extended quiescent state with interrupts
1942          * disabled, invoke the RCU core in order to allow the idle-entry
1943          * deferred-wakeup check to function.
1944          */
1945         if (irqs_disabled_flags(flags) &&
1946             !rcu_is_watching() &&
1947             cpu_online(smp_processor_id()))
1948                 invoke_rcu_core();
1949
1950         return true;
1951 }
1952
1953 /*
1954  * Adopt orphaned callbacks on a no-CBs CPU, or return 0 if this is
1955  * not a no-CBs CPU.
1956  */
1957 static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
1958                                                      struct rcu_data *rdp,
1959                                                      unsigned long flags)
1960 {
1961         long ql = rsp->qlen;
1962         long qll = rsp->qlen_lazy;
1963
1964         /* If this is not a no-CBs CPU, tell the caller to do it the old way. */
1965         if (!rcu_is_nocb_cpu(smp_processor_id()))
1966                 return false;
1967         rsp->qlen = 0;
1968         rsp->qlen_lazy = 0;
1969
1970         /* First, enqueue the donelist, if any.  This preserves CB ordering. */
1971         if (rsp->orphan_donelist != NULL) {
1972                 __call_rcu_nocb_enqueue(rdp, rsp->orphan_donelist,
1973                                         rsp->orphan_donetail, ql, qll, flags);
1974                 ql = qll = 0;
1975                 rsp->orphan_donelist = NULL;
1976                 rsp->orphan_donetail = &rsp->orphan_donelist;
1977         }
1978         if (rsp->orphan_nxtlist != NULL) {
1979                 __call_rcu_nocb_enqueue(rdp, rsp->orphan_nxtlist,
1980                                         rsp->orphan_nxttail, ql, qll, flags);
1981                 ql = qll = 0;
1982                 rsp->orphan_nxtlist = NULL;
1983                 rsp->orphan_nxttail = &rsp->orphan_nxtlist;
1984         }
1985         return true;
1986 }
1987
1988 /*
1989  * If necessary, kick off a new grace period, and either way wait
1990  * for a subsequent grace period to complete.
1991  */
1992 static void rcu_nocb_wait_gp(struct rcu_data *rdp)
1993 {
1994         unsigned long c;
1995         bool d;
1996         unsigned long flags;
1997         bool needwake;
1998         struct rcu_node *rnp = rdp->mynode;
1999
2000         raw_spin_lock_irqsave(&rnp->lock, flags);
2001         smp_mb__after_unlock_lock();
2002         needwake = rcu_start_future_gp(rnp, rdp, &c);
2003         raw_spin_unlock_irqrestore(&rnp->lock, flags);
2004         if (needwake)
2005                 rcu_gp_kthread_wake(rdp->rsp);
2006
2007         /*
2008          * Wait for the grace period.  Do so interruptibly to avoid messing
2009          * up the load average.
2010          */
2011         trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
2012         for (;;) {
2013                 swait_event_interruptible(
2014                         rnp->nocb_gp_wq[c & 0x1],
2015                         (d = ULONG_CMP_GE(ACCESS_ONCE(rnp->completed), c)));
2016                 if (likely(d))
2017                         break;
2018                 WARN_ON(signal_pending(current));
2019                 trace_rcu_future_gp(rnp, rdp, c, TPS("ResumeWait"));
2020         }
2021         trace_rcu_future_gp(rnp, rdp, c, TPS("EndWait"));
2022         smp_mb(); /* Ensure that CB invocation happens after GP end. */
2023 }
2024
2025 /*
2026  * Leaders come here to wait for additional callbacks to show up.
2027  * This function does not return until callbacks appear.
2028  */
2029 static void nocb_leader_wait(struct rcu_data *my_rdp)
2030 {
2031         bool firsttime = true;
2032         bool gotcbs;
2033         struct rcu_data *rdp;
2034         struct rcu_head **tail;
2035
2036 wait_again:
2037
2038         /* Wait for callbacks to appear. */
2039         if (!rcu_nocb_poll) {
2040                 trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
2041                 swait_event_interruptible(my_rdp->nocb_wq,
2042                                 !ACCESS_ONCE(my_rdp->nocb_leader_sleep));
2043                 /* Memory barrier handled by smp_mb() calls below and repoll. */
2044         } else if (firsttime) {
2045                 firsttime = false; /* Don't drown trace log with "Poll"! */
2046                 trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Poll");
2047         }
2048
2049         /*
2050          * Each pass through the following loop checks a follower for CBs.
2051          * We are our own first follower.  Any CBs found are moved to
2052          * nocb_gp_head, where they await a grace period.
2053          */
2054         gotcbs = false;
2055         for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
2056                 rdp->nocb_gp_head = ACCESS_ONCE(rdp->nocb_head);
2057                 if (!rdp->nocb_gp_head)
2058                         continue;  /* No CBs here, try next follower. */
2059
2060                 /* Move callbacks to wait-for-GP list, which is empty. */
2061                 ACCESS_ONCE(rdp->nocb_head) = NULL;
2062                 rdp->nocb_gp_tail = xchg(&rdp->nocb_tail, &rdp->nocb_head);
2063                 gotcbs = true;
2064         }
2065
2066         /*
2067          * If there were no callbacks, sleep a bit, rescan after a
2068          * memory barrier, and go retry.
2069          */
2070         if (unlikely(!gotcbs)) {
2071                 if (!rcu_nocb_poll)
2072                         trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu,
2073                                             "WokeEmpty");
2074                 WARN_ON(signal_pending(current));
2075                 schedule_timeout_interruptible(1);
2076
2077                 /* Rescan in case we were a victim of memory ordering. */
2078                 my_rdp->nocb_leader_sleep = true;
2079                 smp_mb();  /* Ensure _sleep true before scan. */
2080                 for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower)
2081                         if (ACCESS_ONCE(rdp->nocb_head)) {
2082                                 /* Found CB, so short-circuit next wait. */
2083                                 my_rdp->nocb_leader_sleep = false;
2084                                 break;
2085                         }
2086                 goto wait_again;
2087         }
2088
2089         /* Wait for one grace period. */
2090         rcu_nocb_wait_gp(my_rdp);
2091
2092         /*
2093          * We left ->nocb_leader_sleep unset to reduce cache thrashing.
2094          * We set it now, but recheck for new callbacks while
2095          * traversing our follower list.
2096          */
2097         my_rdp->nocb_leader_sleep = true;
2098         smp_mb(); /* Ensure _sleep true before scan of ->nocb_head. */
2099
2100         /* Each pass through the following loop wakes a follower, if needed. */
2101         for (rdp = my_rdp; rdp; rdp = rdp->nocb_next_follower) {
2102                 if (ACCESS_ONCE(rdp->nocb_head))
2103                         my_rdp->nocb_leader_sleep = false;/* No need to sleep.*/
2104                 if (!rdp->nocb_gp_head)
2105                         continue; /* No CBs, so no need to wake follower. */
2106
2107                 /* Append callbacks to follower's "done" list. */
2108                 tail = xchg(&rdp->nocb_follower_tail, rdp->nocb_gp_tail);
2109                 *tail = rdp->nocb_gp_head;
2110                 smp_mb__after_atomic(); /* Store *tail before wakeup. */
2111                 if (rdp != my_rdp && tail == &rdp->nocb_follower_head) {
2112                         /*
2113                          * List was empty, wake up the follower.
2114                          * Memory barriers supplied by atomic_long_add().
2115                          */
2116                         swait_wake(&rdp->nocb_wq);
2117                 }
2118         }
2119
2120         /* If we (the leader) don't have CBs, go wait some more. */
2121         if (!my_rdp->nocb_follower_head)
2122                 goto wait_again;
2123 }
2124
2125 /*
2126  * Followers come here to wait for additional callbacks to show up.
2127  * This function does not return until callbacks appear.
2128  */
2129 static void nocb_follower_wait(struct rcu_data *rdp)
2130 {
2131         bool firsttime = true;
2132
2133         for (;;) {
2134                 if (!rcu_nocb_poll) {
2135                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2136                                             "FollowerSleep");
2137                         swait_event_interruptible(rdp->nocb_wq,
2138                                                  ACCESS_ONCE(rdp->nocb_follower_head));
2139                 } else if (firsttime) {
2140                         /* Don't drown trace log with "Poll"! */
2141                         firsttime = false;
2142                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "Poll");
2143                 }
2144                 if (smp_load_acquire(&rdp->nocb_follower_head)) {
2145                         /* ^^^ Ensure CB invocation follows _head test. */
2146                         return;
2147                 }
2148                 if (!rcu_nocb_poll)
2149                         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2150                                             "WokeEmpty");
2151                 WARN_ON(signal_pending(current));
2152                 schedule_timeout_interruptible(1);
2153         }
2154 }
2155
2156 /*
2157  * Per-rcu_data kthread, but only for no-CBs CPUs.  Each kthread invokes
2158  * callbacks queued by the corresponding no-CBs CPU, however, there is
2159  * an optional leader-follower relationship so that the grace-period
2160  * kthreads don't have to do quite so many wakeups.
2161  */
2162 static int rcu_nocb_kthread(void *arg)
2163 {
2164         int c, cl;
2165         struct rcu_head *list;
2166         struct rcu_head *next;
2167         struct rcu_head **tail;
2168         struct rcu_data *rdp = arg;
2169
2170         /* Each pass through this loop invokes one batch of callbacks */
2171         for (;;) {
2172                 /* Wait for callbacks. */
2173                 if (rdp->nocb_leader == rdp)
2174                         nocb_leader_wait(rdp);
2175                 else
2176                         nocb_follower_wait(rdp);
2177
2178                 /* Pull the ready-to-invoke callbacks onto local list. */
2179                 list = ACCESS_ONCE(rdp->nocb_follower_head);
2180                 BUG_ON(!list);
2181                 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, "WokeNonEmpty");
2182                 ACCESS_ONCE(rdp->nocb_follower_head) = NULL;
2183                 tail = xchg(&rdp->nocb_follower_tail, &rdp->nocb_follower_head);
2184
2185                 /* Each pass through the following loop invokes a callback. */
2186                 trace_rcu_batch_start(rdp->rsp->name,
2187                                       atomic_long_read(&rdp->nocb_q_count_lazy),
2188                                       atomic_long_read(&rdp->nocb_q_count), -1);
2189                 c = cl = 0;
2190                 while (list) {
2191                         next = list->next;
2192                         /* Wait for enqueuing to complete, if needed. */
2193                         while (next == NULL && &list->next != tail) {
2194                                 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2195                                                     TPS("WaitQueue"));
2196                                 schedule_timeout_interruptible(1);
2197                                 trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
2198                                                     TPS("WokeQueue"));
2199                                 next = list->next;
2200                         }
2201                         debug_rcu_head_unqueue(list);
2202                         local_bh_disable();
2203                         if (__rcu_reclaim(rdp->rsp->name, list))
2204                                 cl++;
2205                         c++;
2206                         local_bh_enable();
2207                         list = next;
2208                 }
2209                 trace_rcu_batch_end(rdp->rsp->name, c, !!list, 0, 0, 1);
2210                 smp_mb__before_atomic();  /* _add after CB invocation. */
2211                 atomic_long_add(-c, &rdp->nocb_q_count);
2212                 atomic_long_add(-cl, &rdp->nocb_q_count_lazy);
2213                 rdp->n_nocbs_invoked += c;
2214         }
2215         return 0;
2216 }
2217
2218 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
2219 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
2220 {
2221         return ACCESS_ONCE(rdp->nocb_defer_wakeup);
2222 }
2223
2224 /* Do a deferred wakeup of rcu_nocb_kthread(). */
2225 static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2226 {
2227         int ndw;
2228
2229         if (!rcu_nocb_need_deferred_wakeup(rdp))
2230                 return;
2231         ndw = ACCESS_ONCE(rdp->nocb_defer_wakeup);
2232         ACCESS_ONCE(rdp->nocb_defer_wakeup) = RCU_NOGP_WAKE_NOT;
2233         wake_nocb_leader(rdp, ndw == RCU_NOGP_WAKE_FORCE);
2234         trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu, TPS("DeferredWake"));
2235 }
2236
2237 void __init rcu_init_nohz(void)
2238 {
2239         int cpu;
2240         bool need_rcu_nocb_mask = true;
2241         struct rcu_state *rsp;
2242
2243 #ifdef CONFIG_RCU_NOCB_CPU_NONE
2244         need_rcu_nocb_mask = false;
2245 #endif /* #ifndef CONFIG_RCU_NOCB_CPU_NONE */
2246
2247 #if defined(CONFIG_NO_HZ_FULL)
2248         if (tick_nohz_full_running && cpumask_weight(tick_nohz_full_mask))
2249                 need_rcu_nocb_mask = true;
2250 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2251
2252         if (!have_rcu_nocb_mask && need_rcu_nocb_mask) {
2253                 if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
2254                         pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
2255                         return;
2256                 }
2257                 have_rcu_nocb_mask = true;
2258         }
2259         if (!have_rcu_nocb_mask)
2260                 return;
2261
2262 #ifdef CONFIG_RCU_NOCB_CPU_ZERO
2263         pr_info("\tOffload RCU callbacks from CPU 0\n");
2264         cpumask_set_cpu(0, rcu_nocb_mask);
2265 #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ZERO */
2266 #ifdef CONFIG_RCU_NOCB_CPU_ALL
2267         pr_info("\tOffload RCU callbacks from all CPUs\n");
2268         cpumask_copy(rcu_nocb_mask, cpu_possible_mask);
2269 #endif /* #ifdef CONFIG_RCU_NOCB_CPU_ALL */
2270 #if defined(CONFIG_NO_HZ_FULL)
2271         if (tick_nohz_full_running)
2272                 cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
2273 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
2274
2275         if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
2276                 pr_info("\tNote: kernel parameter 'rcu_nocbs=' contains nonexistent CPUs.\n");
2277                 cpumask_and(rcu_nocb_mask, cpu_possible_mask,
2278                             rcu_nocb_mask);
2279         }
2280         pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
2281                 cpumask_pr_args(rcu_nocb_mask));
2282         if (rcu_nocb_poll)
2283                 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
2284
2285         for_each_rcu_flavor(rsp) {
2286                 for_each_cpu(cpu, rcu_nocb_mask)
2287                         init_nocb_callback_list(per_cpu_ptr(rsp->rda, cpu));
2288                 rcu_organize_nocb_kthreads(rsp);
2289         }
2290 }
2291
2292 /* Initialize per-rcu_data variables for no-CBs CPUs. */
2293 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2294 {
2295         rdp->nocb_tail = &rdp->nocb_head;
2296         init_swait_head(&rdp->nocb_wq);
2297         rdp->nocb_follower_tail = &rdp->nocb_follower_head;
2298 }
2299
2300 /*
2301  * If the specified CPU is a no-CBs CPU that does not already have its
2302  * rcuo kthread for the specified RCU flavor, spawn it.  If the CPUs are
2303  * brought online out of order, this can require re-organizing the
2304  * leader-follower relationships.
2305  */
2306 static void rcu_spawn_one_nocb_kthread(struct rcu_state *rsp, int cpu)
2307 {
2308         struct rcu_data *rdp;
2309         struct rcu_data *rdp_last;
2310         struct rcu_data *rdp_old_leader;
2311         struct rcu_data *rdp_spawn = per_cpu_ptr(rsp->rda, cpu);
2312         struct task_struct *t;
2313
2314         /*
2315          * If this isn't a no-CBs CPU or if it already has an rcuo kthread,
2316          * then nothing to do.
2317          */
2318         if (!rcu_is_nocb_cpu(cpu) || rdp_spawn->nocb_kthread)
2319                 return;
2320
2321         /* If we didn't spawn the leader first, reorganize! */
2322         rdp_old_leader = rdp_spawn->nocb_leader;
2323         if (rdp_old_leader != rdp_spawn && !rdp_old_leader->nocb_kthread) {
2324                 rdp_last = NULL;
2325                 rdp = rdp_old_leader;
2326                 do {
2327                         rdp->nocb_leader = rdp_spawn;
2328                         if (rdp_last && rdp != rdp_spawn)
2329                                 rdp_last->nocb_next_follower = rdp;
2330                         if (rdp == rdp_spawn) {
2331                                 rdp = rdp->nocb_next_follower;
2332                         } else {
2333                                 rdp_last = rdp;
2334                                 rdp = rdp->nocb_next_follower;
2335                                 rdp_last->nocb_next_follower = NULL;
2336                         }
2337                 } while (rdp);
2338                 rdp_spawn->nocb_next_follower = rdp_old_leader;
2339         }
2340
2341         /* Spawn the kthread for this CPU and RCU flavor. */
2342         t = kthread_run(rcu_nocb_kthread, rdp_spawn,
2343                         "rcuo%c/%d", rsp->abbr, cpu);
2344         BUG_ON(IS_ERR(t));
2345         ACCESS_ONCE(rdp_spawn->nocb_kthread) = t;
2346 }
2347
2348 /*
2349  * If the specified CPU is a no-CBs CPU that does not already have its
2350  * rcuo kthreads, spawn them.
2351  */
2352 static void rcu_spawn_all_nocb_kthreads(int cpu)
2353 {
2354         struct rcu_state *rsp;
2355
2356         if (rcu_scheduler_fully_active)
2357                 for_each_rcu_flavor(rsp)
2358                         rcu_spawn_one_nocb_kthread(rsp, cpu);
2359 }
2360
2361 /*
2362  * Once the scheduler is running, spawn rcuo kthreads for all online
2363  * no-CBs CPUs.  This assumes that the early_initcall()s happen before
2364  * non-boot CPUs come online -- if this changes, we will need to add
2365  * some mutual exclusion.
2366  */
2367 static void __init rcu_spawn_nocb_kthreads(void)
2368 {
2369         int cpu;
2370
2371         for_each_online_cpu(cpu)
2372                 rcu_spawn_all_nocb_kthreads(cpu);
2373 }
2374
2375 /* How many follower CPU IDs per leader?  Default of -1 for sqrt(nr_cpu_ids). */
2376 static int rcu_nocb_leader_stride = -1;
2377 module_param(rcu_nocb_leader_stride, int, 0444);
2378
2379 /*
2380  * Initialize leader-follower relationships for all no-CBs CPU.
2381  */
2382 static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp)
2383 {
2384         int cpu;
2385         int ls = rcu_nocb_leader_stride;
2386         int nl = 0;  /* Next leader. */
2387         struct rcu_data *rdp;
2388         struct rcu_data *rdp_leader = NULL;  /* Suppress misguided gcc warn. */
2389         struct rcu_data *rdp_prev = NULL;
2390
2391         if (!have_rcu_nocb_mask)
2392                 return;
2393         if (ls == -1) {
2394                 ls = int_sqrt(nr_cpu_ids);
2395                 rcu_nocb_leader_stride = ls;
2396         }
2397
2398         /*
2399          * Each pass through this loop sets up one rcu_data structure and
2400          * spawns one rcu_nocb_kthread().
2401          */
2402         for_each_cpu(cpu, rcu_nocb_mask) {
2403                 rdp = per_cpu_ptr(rsp->rda, cpu);
2404                 if (rdp->cpu >= nl) {
2405                         /* New leader, set up for followers & next leader. */
2406                         nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
2407                         rdp->nocb_leader = rdp;
2408                         rdp_leader = rdp;
2409                 } else {
2410                         /* Another follower, link to previous leader. */
2411                         rdp->nocb_leader = rdp_leader;
2412                         rdp_prev->nocb_next_follower = rdp;
2413                 }
2414                 rdp_prev = rdp;
2415         }
2416 }
2417
2418 /* Prevent __call_rcu() from enqueuing callbacks on no-CBs CPUs */
2419 static bool init_nocb_callback_list(struct rcu_data *rdp)
2420 {
2421         if (!rcu_is_nocb_cpu(rdp->cpu))
2422                 return false;
2423
2424         /* If there are early-boot callbacks, move them to nocb lists. */
2425         if (rdp->nxtlist) {
2426                 rdp->nocb_head = rdp->nxtlist;
2427                 rdp->nocb_tail = rdp->nxttail[RCU_NEXT_TAIL];
2428                 atomic_long_set(&rdp->nocb_q_count, rdp->qlen);
2429                 atomic_long_set(&rdp->nocb_q_count_lazy, rdp->qlen_lazy);
2430                 rdp->nxtlist = NULL;
2431                 rdp->qlen = 0;
2432                 rdp->qlen_lazy = 0;
2433         }
2434         rdp->nxttail[RCU_NEXT_TAIL] = NULL;
2435         return true;
2436 }
2437
2438 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
2439
2440 static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
2441 {
2442         WARN_ON_ONCE(1); /* Should be dead code. */
2443         return false;
2444 }
2445
2446 static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
2447 {
2448 }
2449
2450 static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
2451 {
2452 }
2453
2454 static void rcu_init_one_nocb(struct rcu_node *rnp)
2455 {
2456 }
2457
2458 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
2459                             bool lazy, unsigned long flags)
2460 {
2461         return false;
2462 }
2463
2464 static bool __maybe_unused rcu_nocb_adopt_orphan_cbs(struct rcu_state *rsp,
2465                                                      struct rcu_data *rdp,
2466                                                      unsigned long flags)
2467 {
2468         return false;
2469 }
2470
2471 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
2472 {
2473 }
2474
2475 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp)
2476 {
2477         return false;
2478 }
2479
2480 static void do_nocb_deferred_wakeup(struct rcu_data *rdp)
2481 {
2482 }
2483
2484 static void rcu_spawn_all_nocb_kthreads(int cpu)
2485 {
2486 }
2487
2488 static void __init rcu_spawn_nocb_kthreads(void)
2489 {
2490 }
2491
2492 static bool init_nocb_callback_list(struct rcu_data *rdp)
2493 {
2494         return false;
2495 }
2496
2497 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
2498
2499 /*
2500  * An adaptive-ticks CPU can potentially execute in kernel mode for an
2501  * arbitrarily long period of time with the scheduling-clock tick turned
2502  * off.  RCU will be paying attention to this CPU because it is in the
2503  * kernel, but the CPU cannot be guaranteed to be executing the RCU state
2504  * machine because the scheduling-clock tick has been disabled.  Therefore,
2505  * if an adaptive-ticks CPU is failing to respond to the current grace
2506  * period and has not be idle from an RCU perspective, kick it.
2507  */
2508 static void __maybe_unused rcu_kick_nohz_cpu(int cpu)
2509 {
2510 #ifdef CONFIG_NO_HZ_FULL
2511         if (tick_nohz_full_cpu(cpu))
2512                 smp_send_reschedule(cpu);
2513 #endif /* #ifdef CONFIG_NO_HZ_FULL */
2514 }
2515
2516
2517 #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
2518
2519 static int full_sysidle_state;          /* Current system-idle state. */
2520 #define RCU_SYSIDLE_NOT         0       /* Some CPU is not idle. */
2521 #define RCU_SYSIDLE_SHORT       1       /* All CPUs idle for brief period. */
2522 #define RCU_SYSIDLE_LONG        2       /* All CPUs idle for long enough. */
2523 #define RCU_SYSIDLE_FULL        3       /* All CPUs idle, ready for sysidle. */
2524 #define RCU_SYSIDLE_FULL_NOTED  4       /* Actually entered sysidle state. */
2525
2526 /*
2527  * Invoked to note exit from irq or task transition to idle.  Note that
2528  * usermode execution does -not- count as idle here!  After all, we want
2529  * to detect full-system idle states, not RCU quiescent states and grace
2530  * periods.  The caller must have disabled interrupts.
2531  */
2532 static void rcu_sysidle_enter(int irq)
2533 {
2534         unsigned long j;
2535         struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
2536
2537         /* If there are no nohz_full= CPUs, no need to track this. */
2538         if (!tick_nohz_full_enabled())
2539                 return;
2540
2541         /* Adjust nesting, check for fully idle. */
2542         if (irq) {
2543                 rdtp->dynticks_idle_nesting--;
2544                 WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
2545                 if (rdtp->dynticks_idle_nesting != 0)
2546                         return;  /* Still not fully idle. */
2547         } else {
2548                 if ((rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) ==
2549                     DYNTICK_TASK_NEST_VALUE) {
2550                         rdtp->dynticks_idle_nesting = 0;
2551                 } else {
2552                         rdtp->dynticks_idle_nesting -= DYNTICK_TASK_NEST_VALUE;
2553                         WARN_ON_ONCE(rdtp->dynticks_idle_nesting < 0);
2554                         return;  /* Still not fully idle. */
2555                 }
2556         }
2557
2558         /* Record start of fully idle period. */
2559         j = jiffies;
2560         ACCESS_ONCE(rdtp->dynticks_idle_jiffies) = j;
2561         smp_mb__before_atomic();
2562         atomic_inc(&rdtp->dynticks_idle);
2563         smp_mb__after_atomic();
2564         WARN_ON_ONCE(atomic_read(&rdtp->dynticks_idle) & 0x1);
2565 }
2566
2567 /*
2568  * Unconditionally force exit from full system-idle state.  This is
2569  * invoked when a normal CPU exits idle, but must be called separately
2570  * for the timekeeping CPU (tick_do_timer_cpu).  The reason for this
2571  * is that the timekeeping CPU is permitted to take scheduling-clock
2572  * interrupts while the system is in system-idle state, and of course
2573  * rcu_sysidle_exit() has no way of distinguishing a scheduling-clock
2574  * interrupt from any other type of interrupt.
2575  */
2576 void rcu_sysidle_force_exit(void)
2577 {
2578         int oldstate = ACCESS_ONCE(full_sysidle_state);
2579         int newoldstate;
2580
2581         /*
2582          * Each pass through the following loop attempts to exit full
2583          * system-idle state.  If contention proves to be a problem,
2584          * a trylock-based contention tree could be used here.
2585          */
2586         while (oldstate > RCU_SYSIDLE_SHORT) {
2587                 newoldstate = cmpxchg(&full_sysidle_state,
2588                                       oldstate, RCU_SYSIDLE_NOT);
2589                 if (oldstate == newoldstate &&
2590                     oldstate == RCU_SYSIDLE_FULL_NOTED) {
2591                         rcu_kick_nohz_cpu(tick_do_timer_cpu);
2592                         return; /* We cleared it, done! */
2593                 }
2594                 oldstate = newoldstate;
2595         }
2596         smp_mb(); /* Order initial oldstate fetch vs. later non-idle work. */
2597 }
2598
2599 /*
2600  * Invoked to note entry to irq or task transition from idle.  Note that
2601  * usermode execution does -not- count as idle here!  The caller must
2602  * have disabled interrupts.
2603  */
2604 static void rcu_sysidle_exit(int irq)
2605 {
2606         struct rcu_dynticks *rdtp = this_cpu_ptr(&rcu_dynticks);
2607
2608         /* If there are no nohz_full= CPUs, no need to track this. */
2609         if (!tick_nohz_full_enabled())
2610                 return;
2611
2612         /* Adjust nesting, check for already non-idle. */
2613         if (irq) {
2614                 rdtp->dynticks_idle_nesting++;
2615                 WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
2616                 if (rdtp->dynticks_idle_nesting != 1)
2617                         return; /* Already non-idle. */
2618         } else {
2619                 /*
2620                  * Allow for irq misnesting.  Yes, it really is possible
2621                  * to enter an irq handler then never leave it, and maybe
2622                  * also vice versa.  Handle both possibilities.
2623                  */
2624                 if (rdtp->dynticks_idle_nesting & DYNTICK_TASK_NEST_MASK) {
2625                         rdtp->dynticks_idle_nesting += DYNTICK_TASK_NEST_VALUE;
2626                         WARN_ON_ONCE(rdtp->dynticks_idle_nesting <= 0);
2627                         return; /* Already non-idle. */
2628                 } else {
2629                         rdtp->dynticks_idle_nesting = DYNTICK_TASK_EXIT_IDLE;
2630                 }
2631         }
2632
2633         /* Record end of idle period. */
2634         smp_mb__before_atomic();
2635         atomic_inc(&rdtp->dynticks_idle);
2636         smp_mb__after_atomic();
2637         WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks_idle) & 0x1));
2638
2639         /*
2640          * If we are the timekeeping CPU, we are permitted to be non-idle
2641          * during a system-idle state.  This must be the case, because
2642          * the timekeeping CPU has to take scheduling-clock interrupts
2643          * during the time that the system is transitioning to full
2644          * system-idle state.  This means that the timekeeping CPU must
2645          * invoke rcu_sysidle_force_exit() directly if it does anything
2646          * more than take a scheduling-clock interrupt.
2647          */
2648         if (smp_processor_id() == tick_do_timer_cpu)
2649                 return;
2650
2651         /* Update system-idle state: We are clearly no longer fully idle! */
2652         rcu_sysidle_force_exit();
2653 }
2654
2655 /*
2656  * Check to see if the current CPU is idle.  Note that usermode execution
2657  * does not count as idle.  The caller must have disabled interrupts,
2658  * and must be running on tick_do_timer_cpu.
2659  */
2660 static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
2661                                   unsigned long *maxj)
2662 {
2663         int cur;
2664         unsigned long j;
2665         struct rcu_dynticks *rdtp = rdp->dynticks;
2666
2667         /* If there are no nohz_full= CPUs, don't check system-wide idleness. */
2668         if (!tick_nohz_full_enabled())
2669                 return;
2670
2671         /*
2672          * If some other CPU has already reported non-idle, if this is
2673          * not the flavor of RCU that tracks sysidle state, or if this
2674          * is an offline or the timekeeping CPU, nothing to do.
2675          */
2676         if (!*isidle || rdp->rsp != rcu_state_p ||
2677             cpu_is_offline(rdp->cpu) || rdp->cpu == tick_do_timer_cpu)
2678                 return;
2679         /* Verify affinity of current kthread. */
2680         WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu);
2681
2682         /* Pick up current idle and NMI-nesting counter and check. */
2683         cur = atomic_read(&rdtp->dynticks_idle);
2684         if (cur & 0x1) {
2685                 *isidle = false; /* We are not idle! */
2686                 return;
2687         }
2688         smp_mb(); /* Read counters before timestamps. */
2689
2690         /* Pick up timestamps. */
2691         j = ACCESS_ONCE(rdtp->dynticks_idle_jiffies);
2692         /* If this CPU entered idle more recently, update maxj timestamp. */
2693         if (ULONG_CMP_LT(*maxj, j))
2694                 *maxj = j;
2695 }
2696
2697 /*
2698  * Is this the flavor of RCU that is handling full-system idle?
2699  */
2700 static bool is_sysidle_rcu_state(struct rcu_state *rsp)
2701 {
2702         return rsp == rcu_state_p;
2703 }
2704
2705 /*
2706  * Return a delay in jiffies based on the number of CPUs, rcu_node
2707  * leaf fanout, and jiffies tick rate.  The idea is to allow larger
2708  * systems more time to transition to full-idle state in order to
2709  * avoid the cache thrashing that otherwise occur on the state variable.
2710  * Really small systems (less than a couple of tens of CPUs) should
2711  * instead use a single global atomically incremented counter, and later
2712  * versions of this will automatically reconfigure themselves accordingly.
2713  */
2714 static unsigned long rcu_sysidle_delay(void)
2715 {
2716         if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
2717                 return 0;
2718         return DIV_ROUND_UP(nr_cpu_ids * HZ, rcu_fanout_leaf * 1000);
2719 }
2720
2721 /*
2722  * Advance the full-system-idle state.  This is invoked when all of
2723  * the non-timekeeping CPUs are idle.
2724  */
2725 static void rcu_sysidle(unsigned long j)
2726 {
2727         /* Check the current state. */
2728         switch (ACCESS_ONCE(full_sysidle_state)) {
2729         case RCU_SYSIDLE_NOT:
2730
2731                 /* First time all are idle, so note a short idle period. */
2732                 ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_SHORT;
2733                 break;
2734
2735         case RCU_SYSIDLE_SHORT:
2736
2737                 /*
2738                  * Idle for a bit, time to advance to next state?
2739                  * cmpxchg failure means race with non-idle, let them win.
2740                  */
2741                 if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
2742                         (void)cmpxchg(&full_sysidle_state,
2743                                       RCU_SYSIDLE_SHORT, RCU_SYSIDLE_LONG);
2744                 break;
2745
2746         case RCU_SYSIDLE_LONG:
2747
2748                 /*
2749                  * Do an additional check pass before advancing to full.
2750                  * cmpxchg failure means race with non-idle, let them win.
2751                  */
2752                 if (ULONG_CMP_GE(jiffies, j + rcu_sysidle_delay()))
2753                         (void)cmpxchg(&full_sysidle_state,
2754                                       RCU_SYSIDLE_LONG, RCU_SYSIDLE_FULL);
2755                 break;
2756
2757         default:
2758                 break;
2759         }
2760 }
2761
2762 /*
2763  * Found a non-idle non-timekeeping CPU, so kick the system-idle state
2764  * back to the beginning.
2765  */
2766 static void rcu_sysidle_cancel(void)
2767 {
2768         smp_mb();
2769         if (full_sysidle_state > RCU_SYSIDLE_SHORT)
2770                 ACCESS_ONCE(full_sysidle_state) = RCU_SYSIDLE_NOT;
2771 }
2772
2773 /*
2774  * Update the sysidle state based on the results of a force-quiescent-state
2775  * scan of the CPUs' dyntick-idle state.
2776  */
2777 static void rcu_sysidle_report(struct rcu_state *rsp, int isidle,
2778                                unsigned long maxj, bool gpkt)
2779 {
2780         if (rsp != rcu_state_p)
2781                 return;  /* Wrong flavor, ignore. */
2782         if (gpkt && nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL)
2783                 return;  /* Running state machine from timekeeping CPU. */
2784         if (isidle)
2785                 rcu_sysidle(maxj);    /* More idle! */
2786         else
2787                 rcu_sysidle_cancel(); /* Idle is over. */
2788 }
2789
2790 /*
2791  * Wrapper for rcu_sysidle_report() when called from the grace-period
2792  * kthread's context.
2793  */
2794 static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
2795                                   unsigned long maxj)
2796 {
2797         /* If there are no nohz_full= CPUs, no need to track this. */
2798         if (!tick_nohz_full_enabled())
2799                 return;
2800
2801         rcu_sysidle_report(rsp, isidle, maxj, true);
2802 }
2803
2804 /* Callback and function for forcing an RCU grace period. */
2805 struct rcu_sysidle_head {
2806         struct rcu_head rh;
2807         int inuse;
2808 };
2809
2810 static void rcu_sysidle_cb(struct rcu_head *rhp)
2811 {
2812         struct rcu_sysidle_head *rshp;
2813
2814         /*
2815          * The following memory barrier is needed to replace the
2816          * memory barriers that would normally be in the memory
2817          * allocator.
2818          */
2819         smp_mb();  /* grace period precedes setting inuse. */
2820
2821         rshp = container_of(rhp, struct rcu_sysidle_head, rh);
2822         ACCESS_ONCE(rshp->inuse) = 0;
2823 }
2824
2825 /*
2826  * Check to see if the system is fully idle, other than the timekeeping CPU.
2827  * The caller must have disabled interrupts.  This is not intended to be
2828  * called unless tick_nohz_full_enabled().
2829  */
2830 bool rcu_sys_is_idle(void)
2831 {
2832         static struct rcu_sysidle_head rsh;
2833         int rss = ACCESS_ONCE(full_sysidle_state);
2834
2835         if (WARN_ON_ONCE(smp_processor_id() != tick_do_timer_cpu))
2836                 return false;
2837
2838         /* Handle small-system case by doing a full scan of CPUs. */
2839         if (nr_cpu_ids <= CONFIG_NO_HZ_FULL_SYSIDLE_SMALL) {
2840                 int oldrss = rss - 1;
2841
2842                 /*
2843                  * One pass to advance to each state up to _FULL.
2844                  * Give up if any pass fails to advance the state.
2845                  */
2846                 while (rss < RCU_SYSIDLE_FULL && oldrss < rss) {
2847                         int cpu;
2848                         bool isidle = true;
2849                         unsigned long maxj = jiffies - ULONG_MAX / 4;
2850                         struct rcu_data *rdp;
2851
2852                         /* Scan all the CPUs looking for nonidle CPUs. */
2853                         for_each_possible_cpu(cpu) {
2854                                 rdp = per_cpu_ptr(rcu_state_p->rda, cpu);
2855                                 rcu_sysidle_check_cpu(rdp, &isidle, &maxj);
2856                                 if (!isidle)
2857                                         break;
2858                         }
2859                         rcu_sysidle_report(rcu_state_p, isidle, maxj, false);
2860                         oldrss = rss;
2861                         rss = ACCESS_ONCE(full_sysidle_state);
2862                 }
2863         }
2864
2865         /* If this is the first observation of an idle period, record it. */
2866         if (rss == RCU_SYSIDLE_FULL) {
2867                 rss = cmpxchg(&full_sysidle_state,
2868                               RCU_SYSIDLE_FULL, RCU_SYSIDLE_FULL_NOTED);
2869                 return rss == RCU_SYSIDLE_FULL;
2870         }
2871
2872         smp_mb(); /* ensure rss load happens before later caller actions. */
2873
2874         /* If already fully idle, tell the caller (in case of races). */
2875         if (rss == RCU_SYSIDLE_FULL_NOTED)
2876                 return true;
2877
2878         /*
2879          * If we aren't there yet, and a grace period is not in flight,
2880          * initiate a grace period.  Either way, tell the caller that
2881          * we are not there yet.  We use an xchg() rather than an assignment
2882          * to make up for the memory barriers that would otherwise be
2883          * provided by the memory allocator.
2884          */
2885         if (nr_cpu_ids > CONFIG_NO_HZ_FULL_SYSIDLE_SMALL &&
2886             !rcu_gp_in_progress(rcu_state_p) &&
2887             !rsh.inuse && xchg(&rsh.inuse, 1) == 0)
2888                 call_rcu(&rsh.rh, rcu_sysidle_cb);
2889         return false;
2890 }
2891
2892 /*
2893  * Initialize dynticks sysidle state for CPUs coming online.
2894  */
2895 static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
2896 {
2897         rdtp->dynticks_idle_nesting = DYNTICK_TASK_NEST_VALUE;
2898 }
2899
2900 #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2901
2902 static void rcu_sysidle_enter(int irq)
2903 {
2904 }
2905
2906 static void rcu_sysidle_exit(int irq)
2907 {
2908 }
2909
2910 static void rcu_sysidle_check_cpu(struct rcu_data *rdp, bool *isidle,
2911                                   unsigned long *maxj)
2912 {
2913 }
2914
2915 static bool is_sysidle_rcu_state(struct rcu_state *rsp)
2916 {
2917         return false;
2918 }
2919
2920 static void rcu_sysidle_report_gp(struct rcu_state *rsp, int isidle,
2921                                   unsigned long maxj)
2922 {
2923 }
2924
2925 static void rcu_sysidle_init_percpu_data(struct rcu_dynticks *rdtp)
2926 {
2927 }
2928
2929 #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2930
2931 /*
2932  * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
2933  * grace-period kthread will do force_quiescent_state() processing?
2934  * The idea is to avoid waking up RCU core processing on such a
2935  * CPU unless the grace period has extended for too long.
2936  *
2937  * This code relies on the fact that all NO_HZ_FULL CPUs are also
2938  * CONFIG_RCU_NOCB_CPU CPUs.
2939  */
2940 static bool rcu_nohz_full_cpu(struct rcu_state *rsp)
2941 {
2942 #ifdef CONFIG_NO_HZ_FULL
2943         if (tick_nohz_full_cpu(smp_processor_id()) &&
2944             (!rcu_gp_in_progress(rsp) ||
2945              ULONG_CMP_LT(jiffies, ACCESS_ONCE(rsp->gp_start) + HZ)))
2946                 return 1;
2947 #endif /* #ifdef CONFIG_NO_HZ_FULL */
2948         return 0;
2949 }
2950
2951 /*
2952  * Bind the grace-period kthread for the sysidle flavor of RCU to the
2953  * timekeeping CPU.
2954  */
2955 static void rcu_bind_gp_kthread(void)
2956 {
2957         int __maybe_unused cpu;
2958
2959         if (!tick_nohz_full_enabled())
2960                 return;
2961 #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
2962         cpu = tick_do_timer_cpu;
2963         if (cpu >= 0 && cpu < nr_cpu_ids)
2964                 set_cpus_allowed_ptr(current, cpumask_of(cpu));
2965 #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2966         housekeeping_affine(current);
2967 #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
2968 }
2969
2970 /* Record the current task on dyntick-idle entry. */
2971 static void rcu_dynticks_task_enter(void)
2972 {
2973 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2974         ACCESS_ONCE(current->rcu_tasks_idle_cpu) = smp_processor_id();
2975 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2976 }
2977
2978 /* Record no current task on dyntick-idle exit. */
2979 static void rcu_dynticks_task_exit(void)
2980 {
2981 #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL)
2982         ACCESS_ONCE(current->rcu_tasks_idle_cpu) = -1;
2983 #endif /* #if defined(CONFIG_TASKS_RCU) && defined(CONFIG_NO_HZ_FULL) */
2984 }