2 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3 * Internal non-public definitions.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
19 * Copyright IBM Corporation, 2008
25 #include <linux/cache.h>
26 #include <linux/spinlock.h>
27 #include <linux/rtmutex.h>
28 #include <linux/threads.h>
29 #include <linux/cpumask.h>
30 #include <linux/seqlock.h>
31 #include <linux/swait.h>
32 #include <linux/stop_machine.h>
33 #include <linux/rcu_node_tree.h>
35 #include "rcu_segcblist.h"
38 * Dynticks per-CPU state.
41 long dynticks_nesting; /* Track process nesting level. */
42 long dynticks_nmi_nesting; /* Track irq/NMI nesting level. */
43 atomic_t dynticks; /* Even value for idle, else odd. */
44 bool rcu_need_heavy_qs; /* GP old, need heavy quiescent state. */
45 unsigned long rcu_qs_ctr; /* Light universal quiescent state ctr. */
46 bool rcu_urgent_qs; /* GP old need light quiescent state. */
47 #ifdef CONFIG_RCU_FAST_NO_HZ
48 bool all_lazy; /* Are all CPU's CBs lazy? */
49 unsigned long nonlazy_posted;
50 /* # times non-lazy CBs posted to CPU. */
51 unsigned long nonlazy_posted_snap;
52 /* idle-period nonlazy_posted snapshot. */
53 unsigned long last_accelerate;
54 /* Last jiffy CBs were accelerated. */
55 unsigned long last_advance_all;
56 /* Last jiffy CBs were all advanced. */
57 int tick_nohz_enabled_snap; /* Previously seen value from sysfs. */
58 #endif /* #ifdef CONFIG_RCU_FAST_NO_HZ */
61 /* Communicate arguments to a workqueue handler. */
63 smp_call_func_t rew_func;
64 struct rcu_state *rew_rsp;
66 struct work_struct rew_work;
69 /* RCU's kthread states for tracing. */
70 #define RCU_KTHREAD_STOPPED 0
71 #define RCU_KTHREAD_RUNNING 1
72 #define RCU_KTHREAD_WAITING 2
73 #define RCU_KTHREAD_OFFCPU 3
74 #define RCU_KTHREAD_YIELDING 4
75 #define RCU_KTHREAD_MAX 4
78 * Definition for node within the RCU grace-period-detection hierarchy.
81 raw_spinlock_t __private lock; /* Root rcu_node's lock protects */
82 /* some rcu_state fields as well as */
84 unsigned long gpnum; /* Current grace period for this node. */
85 /* This will either be equal to or one */
86 /* behind the root rcu_node's gpnum. */
87 unsigned long completed; /* Last GP completed for this node. */
88 /* This will either be equal to or one */
89 /* behind the root rcu_node's gpnum. */
90 unsigned long qsmask; /* CPUs or groups that need to switch in */
91 /* order for current grace period to proceed.*/
92 /* In leaf rcu_node, each bit corresponds to */
93 /* an rcu_data structure, otherwise, each */
94 /* bit corresponds to a child rcu_node */
96 unsigned long qsmaskinit;
97 /* Per-GP initial value for qsmask. */
98 /* Initialized from ->qsmaskinitnext at the */
99 /* beginning of each grace period. */
100 unsigned long qsmaskinitnext;
101 /* Online CPUs for next grace period. */
102 unsigned long expmask; /* CPUs or groups that need to check in */
103 /* to allow the current expedited GP */
105 unsigned long expmaskinit;
106 /* Per-GP initial values for expmask. */
107 /* Initialized from ->expmaskinitnext at the */
108 /* beginning of each expedited GP. */
109 unsigned long expmaskinitnext;
110 /* Online CPUs for next expedited GP. */
111 /* Any CPU that has ever been online will */
112 /* have its bit set. */
113 unsigned long ffmask; /* Fully functional CPUs. */
114 unsigned long grpmask; /* Mask to apply to parent qsmask. */
115 /* Only one bit will be set in this mask. */
116 int grplo; /* lowest-numbered CPU or group here. */
117 int grphi; /* highest-numbered CPU or group here. */
118 u8 grpnum; /* CPU/group number for next level up. */
119 u8 level; /* root is at level 0. */
120 bool wait_blkd_tasks;/* Necessary to wait for blocked tasks to */
121 /* exit RCU read-side critical sections */
122 /* before propagating offline up the */
124 struct rcu_node *parent;
125 struct list_head blkd_tasks;
126 /* Tasks blocked in RCU read-side critical */
127 /* section. Tasks are placed at the head */
128 /* of this list and age towards the tail. */
129 struct list_head *gp_tasks;
130 /* Pointer to the first task blocking the */
131 /* current grace period, or NULL if there */
132 /* is no such task. */
133 struct list_head *exp_tasks;
134 /* Pointer to the first task blocking the */
135 /* current expedited grace period, or NULL */
136 /* if there is no such task. If there */
137 /* is no current expedited grace period, */
138 /* then there can cannot be any such task. */
139 struct list_head *boost_tasks;
140 /* Pointer to first task that needs to be */
141 /* priority boosted, or NULL if no priority */
142 /* boosting is needed for this rcu_node */
143 /* structure. If there are no tasks */
144 /* queued on this rcu_node structure that */
145 /* are blocking the current grace period, */
146 /* there can be no such task. */
147 struct rt_mutex boost_mtx;
148 /* Used only for the priority-boosting */
149 /* side effect, not as a lock. */
150 unsigned long boost_time;
151 /* When to start boosting (jiffies). */
152 struct task_struct *boost_kthread_task;
153 /* kthread that takes care of priority */
154 /* boosting for this rcu_node structure. */
155 unsigned int boost_kthread_status;
156 /* State of boost_kthread_task for tracing. */
157 #ifdef CONFIG_RCU_NOCB_CPU
158 struct swait_queue_head nocb_gp_wq[2];
159 /* Place for rcu_nocb_kthread() to wait GP. */
160 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
161 u8 need_future_gp[4]; /* Counts of upcoming GP requests. */
162 raw_spinlock_t fqslock ____cacheline_internodealigned_in_smp;
164 spinlock_t exp_lock ____cacheline_internodealigned_in_smp;
165 unsigned long exp_seq_rq;
166 wait_queue_head_t exp_wq[4];
167 struct rcu_exp_work rew;
168 bool exp_need_flush; /* Need to flush workitem? */
169 } ____cacheline_internodealigned_in_smp;
171 /* Accessors for ->need_future_gp[] array. */
172 #define need_future_gp_mask() \
173 (ARRAY_SIZE(((struct rcu_node *)NULL)->need_future_gp) - 1)
174 #define need_future_gp_element(rnp, c) \
175 ((rnp)->need_future_gp[(c) & need_future_gp_mask()])
176 #define need_any_future_gp(rnp) \
179 bool __nonzero = false; \
181 for (__i = 0; __i < ARRAY_SIZE((rnp)->need_future_gp); __i++) \
182 __nonzero = __nonzero || \
183 READ_ONCE((rnp)->need_future_gp[__i]); \
188 * Bitmasks in an rcu_node cover the interval [grplo, grphi] of CPU IDs, and
189 * are indexed relative to this interval rather than the global CPU ID space.
190 * This generates the bit for a CPU in node-local masks.
192 #define leaf_node_cpu_bit(rnp, cpu) (1UL << ((cpu) - (rnp)->grplo))
195 * Union to allow "aggregate OR" operation on the need for a quiescent
196 * state by the normal and expedited grace periods.
203 u16 s; /* Set of bits, aggregate OR here. */
206 /* Per-CPU data for read-copy update. */
208 /* 1) quiescent-state and grace-period handling : */
209 unsigned long completed; /* Track rsp->completed gp number */
210 /* in order to detect GP end. */
211 unsigned long gpnum; /* Highest gp number that this CPU */
212 /* is aware of having started. */
213 unsigned long rcu_qs_ctr_snap;/* Snapshot of rcu_qs_ctr to check */
214 /* for rcu_all_qs() invocations. */
215 union rcu_noqs cpu_no_qs; /* No QSes yet for this CPU. */
216 bool core_needs_qs; /* Core waits for quiesc state. */
217 bool beenonline; /* CPU online at least once. */
218 bool gpwrap; /* Possible gpnum/completed wrap. */
219 struct rcu_node *mynode; /* This CPU's leaf of hierarchy */
220 unsigned long grpmask; /* Mask to apply to leaf qsmask. */
221 unsigned long ticks_this_gp; /* The number of scheduling-clock */
222 /* ticks this CPU has handled */
223 /* during and after the last grace */
224 /* period it is aware of. */
226 /* 2) batch handling */
227 struct rcu_segcblist cblist; /* Segmented callback list, with */
228 /* different callbacks waiting for */
229 /* different grace periods. */
230 long qlen_last_fqs_check;
231 /* qlen at last check for QS forcing */
232 unsigned long n_force_qs_snap;
233 /* did other CPU force QS recently? */
234 long blimit; /* Upper limit on a processed batch */
236 /* 3) dynticks interface. */
237 struct rcu_dynticks *dynticks; /* Shared per-CPU dynticks state. */
238 int dynticks_snap; /* Per-GP tracking for dynticks. */
240 /* 4) reasons this CPU needed to be kicked by force_quiescent_state */
241 unsigned long dynticks_fqs; /* Kicked due to dynticks idle. */
242 unsigned long offline_fqs; /* Kicked due to being offline. */
243 unsigned long cond_resched_completed;
244 /* Grace period that needs help */
245 /* from cond_resched(). */
247 /* 5) _rcu_barrier(), OOM callbacks, and expediting. */
248 struct rcu_head barrier_head;
249 #ifdef CONFIG_RCU_FAST_NO_HZ
250 struct rcu_head oom_head;
251 #endif /* #ifdef CONFIG_RCU_FAST_NO_HZ */
252 int exp_dynticks_snap; /* Double-check need for IPI. */
254 /* 6) Callback offloading. */
255 #ifdef CONFIG_RCU_NOCB_CPU
256 struct rcu_head *nocb_head; /* CBs waiting for kthread. */
257 struct rcu_head **nocb_tail;
258 atomic_long_t nocb_q_count; /* # CBs waiting for nocb */
259 atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
260 struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
261 struct rcu_head **nocb_follower_tail;
262 struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
263 struct task_struct *nocb_kthread;
264 raw_spinlock_t nocb_lock; /* Guard following pair of fields. */
265 int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
266 struct timer_list nocb_timer; /* Enforce finite deferral. */
268 /* The following fields are used by the leader, hence own cacheline. */
269 struct rcu_head *nocb_gp_head ____cacheline_internodealigned_in_smp;
270 /* CBs waiting for GP. */
271 struct rcu_head **nocb_gp_tail;
272 bool nocb_leader_sleep; /* Is the nocb leader thread asleep? */
273 struct rcu_data *nocb_next_follower;
274 /* Next follower in wakeup chain. */
276 /* The following fields are used by the follower, hence new cachline. */
277 struct rcu_data *nocb_leader ____cacheline_internodealigned_in_smp;
278 /* Leader CPU takes GP-end wakeups. */
279 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
281 /* 7) RCU CPU stall data. */
282 unsigned int softirq_snap; /* Snapshot of softirq activity. */
283 /* ->rcu_iw* fields protected by leaf rcu_node ->lock. */
284 struct irq_work rcu_iw; /* Check for non-irq activity. */
285 bool rcu_iw_pending; /* Is ->rcu_iw pending? */
286 unsigned long rcu_iw_gpnum; /* ->gpnum associated with ->rcu_iw. */
289 struct rcu_state *rsp;
292 /* Values for nocb_defer_wakeup field in struct rcu_data. */
293 #define RCU_NOCB_WAKE_NOT 0
294 #define RCU_NOCB_WAKE 1
295 #define RCU_NOCB_WAKE_FORCE 2
297 #define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500))
298 /* For jiffies_till_first_fqs and */
299 /* and jiffies_till_next_fqs. */
301 #define RCU_JIFFIES_FQS_DIV 256 /* Very large systems need more */
302 /* delay between bouts of */
303 /* quiescent-state forcing. */
305 #define RCU_STALL_RAT_DELAY 2 /* Allow other CPUs time to take */
306 /* at least one scheduling clock */
307 /* irq before ratting on them. */
309 #define rcu_wait(cond) \
312 set_current_state(TASK_INTERRUPTIBLE); \
317 __set_current_state(TASK_RUNNING); \
321 * RCU global state, including node hierarchy. This hierarchy is
322 * represented in "heap" form in a dense array. The root (first level)
323 * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second
324 * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]),
325 * and the third level in ->node[m+1] and following (->node[m+1] referenced
326 * by ->level[2]). The number of levels is determined by the number of
327 * CPUs and by CONFIG_RCU_FANOUT. Small systems will have a "hierarchy"
328 * consisting of a single rcu_node.
331 struct rcu_node node[NUM_RCU_NODES]; /* Hierarchy. */
332 struct rcu_node *level[RCU_NUM_LVLS + 1];
333 /* Hierarchy levels (+1 to */
334 /* shut bogus gcc warning) */
335 struct rcu_data __percpu *rda; /* pointer of percu rcu_data. */
336 call_rcu_func_t call; /* call_rcu() flavor. */
337 int ncpus; /* # CPUs seen so far. */
339 /* The following fields are guarded by the root rcu_node's lock. */
341 u8 boost ____cacheline_internodealigned_in_smp;
342 /* Subject to priority boost. */
343 unsigned long gpnum; /* Current gp number. */
344 unsigned long completed; /* # of last completed gp. */
345 struct task_struct *gp_kthread; /* Task for grace periods. */
346 struct swait_queue_head gp_wq; /* Where GP task waits. */
347 short gp_flags; /* Commands for GP task. */
348 short gp_state; /* GP kthread sleep state. */
350 /* End of fields guarded by root rcu_node's lock. */
352 struct mutex barrier_mutex; /* Guards barrier fields. */
353 atomic_t barrier_cpu_count; /* # CPUs waiting on. */
354 struct completion barrier_completion; /* Wake at barrier end. */
355 unsigned long barrier_sequence; /* ++ at start and end of */
356 /* _rcu_barrier(). */
357 /* End of fields guarded by barrier_mutex. */
359 struct mutex exp_mutex; /* Serialize expedited GP. */
360 struct mutex exp_wake_mutex; /* Serialize wakeup. */
361 unsigned long expedited_sequence; /* Take a ticket. */
362 atomic_t expedited_need_qs; /* # CPUs left to check in. */
363 struct swait_queue_head expedited_wq; /* Wait for check-ins. */
364 int ncpus_snap; /* # CPUs seen last time. */
366 unsigned long jiffies_force_qs; /* Time at which to invoke */
367 /* force_quiescent_state(). */
368 unsigned long jiffies_kick_kthreads; /* Time at which to kick */
369 /* kthreads, if configured. */
370 unsigned long n_force_qs; /* Number of calls to */
371 /* force_quiescent_state(). */
372 unsigned long gp_start; /* Time at which GP started, */
373 /* but in jiffies. */
374 unsigned long gp_activity; /* Time of last GP kthread */
375 /* activity in jiffies. */
376 unsigned long jiffies_stall; /* Time at which to check */
377 /* for CPU stalls. */
378 unsigned long jiffies_resched; /* Time at which to resched */
379 /* a reluctant CPU. */
380 unsigned long n_force_qs_gpstart; /* Snapshot of n_force_qs at */
382 unsigned long gp_max; /* Maximum GP duration in */
384 const char *name; /* Name of structure. */
385 char abbr; /* Abbreviated name. */
386 struct list_head flavors; /* List of RCU flavors. */
389 /* Values for rcu_state structure's gp_flags field. */
390 #define RCU_GP_FLAG_INIT 0x1 /* Need grace-period initialization. */
391 #define RCU_GP_FLAG_FQS 0x2 /* Need grace-period quiescent-state forcing. */
393 /* Values for rcu_state structure's gp_state field. */
394 #define RCU_GP_IDLE 0 /* Initial state and no GP in progress. */
395 #define RCU_GP_WAIT_GPS 1 /* Wait for grace-period start. */
396 #define RCU_GP_DONE_GPS 2 /* Wait done for grace-period start. */
397 #define RCU_GP_WAIT_FQS 3 /* Wait for force-quiescent-state time. */
398 #define RCU_GP_DOING_FQS 4 /* Wait done for force-quiescent-state time. */
399 #define RCU_GP_CLEANUP 5 /* Grace-period cleanup started. */
400 #define RCU_GP_CLEANED 6 /* Grace-period cleanup complete. */
402 #ifndef RCU_TREE_NONCORE
403 static const char * const gp_state_names[] = {
412 #endif /* #ifndef RCU_TREE_NONCORE */
414 extern struct list_head rcu_struct_flavors;
416 /* Sequence through rcu_state structures for each RCU flavor. */
417 #define for_each_rcu_flavor(rsp) \
418 list_for_each_entry((rsp), &rcu_struct_flavors, flavors)
421 * RCU implementation internal declarations:
423 extern struct rcu_state rcu_sched_state;
425 extern struct rcu_state rcu_bh_state;
427 #ifdef CONFIG_PREEMPT_RCU
428 extern struct rcu_state rcu_preempt_state;
429 #endif /* #ifdef CONFIG_PREEMPT_RCU */
431 int rcu_dynticks_snap(struct rcu_dynticks *rdtp);
433 #ifdef CONFIG_RCU_BOOST
434 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
435 DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
436 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
437 DECLARE_PER_CPU(char, rcu_cpu_has_work);
438 #endif /* #ifdef CONFIG_RCU_BOOST */
440 #ifndef RCU_TREE_NONCORE
442 /* Forward declarations for rcutree_plugin.h */
443 static void rcu_bootup_announce(void);
444 static void rcu_preempt_note_context_switch(bool preempt);
445 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp);
446 #ifdef CONFIG_HOTPLUG_CPU
447 static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
448 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
449 static void rcu_print_detail_task_stall(struct rcu_state *rsp);
450 static int rcu_print_task_stall(struct rcu_node *rnp);
451 static int rcu_print_task_exp_stall(struct rcu_node *rnp);
452 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp);
453 static void rcu_preempt_check_callbacks(void);
454 void call_rcu(struct rcu_head *head, rcu_callback_t func);
455 static void __init __rcu_init_preempt(void);
456 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
457 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
458 static void invoke_rcu_callbacks_kthread(void);
459 static bool rcu_is_callbacks_kthread(void);
460 #ifdef CONFIG_RCU_BOOST
461 static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
462 struct rcu_node *rnp);
463 #endif /* #ifdef CONFIG_RCU_BOOST */
464 static void __init rcu_spawn_boost_kthreads(void);
465 static void rcu_prepare_kthreads(int cpu);
466 static void rcu_cleanup_after_idle(void);
467 static void rcu_prepare_for_idle(void);
468 static void rcu_idle_count_callbacks_posted(void);
469 static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
470 static void print_cpu_stall_info_begin(void);
471 static void print_cpu_stall_info(struct rcu_state *rsp, int cpu);
472 static void print_cpu_stall_info_end(void);
473 static void zero_cpu_stall_ticks(struct rcu_data *rdp);
474 static void increment_cpu_stall_ticks(void);
475 static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
476 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
477 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
478 static void rcu_init_one_nocb(struct rcu_node *rnp);
479 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
480 bool lazy, unsigned long flags);
481 static bool rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
482 struct rcu_data *rdp,
483 unsigned long flags);
484 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp);
485 static void do_nocb_deferred_wakeup(struct rcu_data *rdp);
486 static void rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp);
487 static void rcu_spawn_all_nocb_kthreads(int cpu);
488 static void __init rcu_spawn_nocb_kthreads(void);
489 #ifdef CONFIG_RCU_NOCB_CPU
490 static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp);
491 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
492 static void __maybe_unused rcu_kick_nohz_cpu(int cpu);
493 static bool init_nocb_callback_list(struct rcu_data *rdp);
494 static void rcu_bind_gp_kthread(void);
495 static bool rcu_nohz_full_cpu(struct rcu_state *rsp);
496 static void rcu_dynticks_task_enter(void);
497 static void rcu_dynticks_task_exit(void);
500 void srcu_online_cpu(unsigned int cpu);
501 void srcu_offline_cpu(unsigned int cpu);
502 #else /* #ifdef CONFIG_SRCU */
503 void srcu_online_cpu(unsigned int cpu) { }
504 void srcu_offline_cpu(unsigned int cpu) { }
505 #endif /* #else #ifdef CONFIG_SRCU */
507 #endif /* #ifndef RCU_TREE_NONCORE */