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
21 * Author: Ingo Molnar <mingo@elte.hu>
22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
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 gp_seq; /* Track rsp->rcu_gp_seq. */
85 unsigned long gp_seq_needed; /* Track rsp->rcu_gp_seq_needed. */
86 unsigned long completedqs; /* All QSes done for this node. */
87 unsigned long qsmask; /* CPUs or groups that need to switch in */
88 /* order for current grace period to proceed.*/
89 /* In leaf rcu_node, each bit corresponds to */
90 /* an rcu_data structure, otherwise, each */
91 /* bit corresponds to a child rcu_node */
93 unsigned long qsmaskinit;
94 /* Per-GP initial value for qsmask. */
95 /* Initialized from ->qsmaskinitnext at the */
96 /* beginning of each grace period. */
97 unsigned long qsmaskinitnext;
98 /* Online CPUs for next grace period. */
99 unsigned long expmask; /* CPUs or groups that need to check in */
100 /* to allow the current expedited GP */
102 unsigned long expmaskinit;
103 /* Per-GP initial values for expmask. */
104 /* Initialized from ->expmaskinitnext at the */
105 /* beginning of each expedited GP. */
106 unsigned long expmaskinitnext;
107 /* Online CPUs for next expedited GP. */
108 /* Any CPU that has ever been online will */
109 /* have its bit set. */
110 unsigned long ffmask; /* Fully functional CPUs. */
111 unsigned long grpmask; /* Mask to apply to parent qsmask. */
112 /* Only one bit will be set in this mask. */
113 int grplo; /* lowest-numbered CPU or group here. */
114 int grphi; /* highest-numbered CPU or group here. */
115 u8 grpnum; /* CPU/group number for next level up. */
116 u8 level; /* root is at level 0. */
117 bool wait_blkd_tasks;/* Necessary to wait for blocked tasks to */
118 /* exit RCU read-side critical sections */
119 /* before propagating offline up the */
121 struct rcu_node *parent;
122 struct list_head blkd_tasks;
123 /* Tasks blocked in RCU read-side critical */
124 /* section. Tasks are placed at the head */
125 /* of this list and age towards the tail. */
126 struct list_head *gp_tasks;
127 /* Pointer to the first task blocking the */
128 /* current grace period, or NULL if there */
129 /* is no such task. */
130 struct list_head *exp_tasks;
131 /* Pointer to the first task blocking the */
132 /* current expedited grace period, or NULL */
133 /* if there is no such task. If there */
134 /* is no current expedited grace period, */
135 /* then there can cannot be any such task. */
136 struct list_head *boost_tasks;
137 /* Pointer to first task that needs to be */
138 /* priority boosted, or NULL if no priority */
139 /* boosting is needed for this rcu_node */
140 /* structure. If there are no tasks */
141 /* queued on this rcu_node structure that */
142 /* are blocking the current grace period, */
143 /* there can be no such task. */
144 struct rt_mutex boost_mtx;
145 /* Used only for the priority-boosting */
146 /* side effect, not as a lock. */
147 unsigned long boost_time;
148 /* When to start boosting (jiffies). */
149 struct task_struct *boost_kthread_task;
150 /* kthread that takes care of priority */
151 /* boosting for this rcu_node structure. */
152 unsigned int boost_kthread_status;
153 /* State of boost_kthread_task for tracing. */
154 #ifdef CONFIG_RCU_NOCB_CPU
155 struct swait_queue_head nocb_gp_wq[2];
156 /* Place for rcu_nocb_kthread() to wait GP. */
157 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
158 raw_spinlock_t fqslock ____cacheline_internodealigned_in_smp;
160 spinlock_t exp_lock ____cacheline_internodealigned_in_smp;
161 unsigned long exp_seq_rq;
162 wait_queue_head_t exp_wq[4];
163 struct rcu_exp_work rew;
164 bool exp_need_flush; /* Need to flush workitem? */
165 } ____cacheline_internodealigned_in_smp;
168 * Bitmasks in an rcu_node cover the interval [grplo, grphi] of CPU IDs, and
169 * are indexed relative to this interval rather than the global CPU ID space.
170 * This generates the bit for a CPU in node-local masks.
172 #define leaf_node_cpu_bit(rnp, cpu) (1UL << ((cpu) - (rnp)->grplo))
175 * Union to allow "aggregate OR" operation on the need for a quiescent
176 * state by the normal and expedited grace periods.
183 u16 s; /* Set of bits, aggregate OR here. */
186 /* Per-CPU data for read-copy update. */
188 /* 1) quiescent-state and grace-period handling : */
189 unsigned long gp_seq; /* Track rsp->rcu_gp_seq counter. */
190 unsigned long gp_seq_needed; /* Track rsp->rcu_gp_seq_needed ctr. */
191 unsigned long rcu_qs_ctr_snap;/* Snapshot of rcu_qs_ctr to check */
192 /* for rcu_all_qs() invocations. */
193 union rcu_noqs cpu_no_qs; /* No QSes yet for this CPU. */
194 bool core_needs_qs; /* Core waits for quiesc state. */
195 bool beenonline; /* CPU online at least once. */
196 bool gpwrap; /* Possible ->gp_seq wrap. */
197 struct rcu_node *mynode; /* This CPU's leaf of hierarchy */
198 unsigned long grpmask; /* Mask to apply to leaf qsmask. */
199 unsigned long ticks_this_gp; /* The number of scheduling-clock */
200 /* ticks this CPU has handled */
201 /* during and after the last grace */
202 /* period it is aware of. */
204 /* 2) batch handling */
205 struct rcu_segcblist cblist; /* Segmented callback list, with */
206 /* different callbacks waiting for */
207 /* different grace periods. */
208 long qlen_last_fqs_check;
209 /* qlen at last check for QS forcing */
210 unsigned long n_force_qs_snap;
211 /* did other CPU force QS recently? */
212 long blimit; /* Upper limit on a processed batch */
214 /* 3) dynticks interface. */
215 struct rcu_dynticks *dynticks; /* Shared per-CPU dynticks state. */
216 int dynticks_snap; /* Per-GP tracking for dynticks. */
218 /* 4) reasons this CPU needed to be kicked by force_quiescent_state */
219 unsigned long dynticks_fqs; /* Kicked due to dynticks idle. */
220 unsigned long offline_fqs; /* Kicked due to being offline. */
221 unsigned long cond_resched_completed;
222 /* Grace period that needs help */
223 /* from cond_resched(). */
225 /* 5) _rcu_barrier(), OOM callbacks, and expediting. */
226 struct rcu_head barrier_head;
227 #ifdef CONFIG_RCU_FAST_NO_HZ
228 struct rcu_head oom_head;
229 #endif /* #ifdef CONFIG_RCU_FAST_NO_HZ */
230 int exp_dynticks_snap; /* Double-check need for IPI. */
232 /* 6) Callback offloading. */
233 #ifdef CONFIG_RCU_NOCB_CPU
234 struct rcu_head *nocb_head; /* CBs waiting for kthread. */
235 struct rcu_head **nocb_tail;
236 atomic_long_t nocb_q_count; /* # CBs waiting for nocb */
237 atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
238 struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
239 struct rcu_head **nocb_follower_tail;
240 struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
241 struct task_struct *nocb_kthread;
242 raw_spinlock_t nocb_lock; /* Guard following pair of fields. */
243 int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
244 struct timer_list nocb_timer; /* Enforce finite deferral. */
246 /* The following fields are used by the leader, hence own cacheline. */
247 struct rcu_head *nocb_gp_head ____cacheline_internodealigned_in_smp;
248 /* CBs waiting for GP. */
249 struct rcu_head **nocb_gp_tail;
250 bool nocb_leader_sleep; /* Is the nocb leader thread asleep? */
251 struct rcu_data *nocb_next_follower;
252 /* Next follower in wakeup chain. */
254 /* The following fields are used by the follower, hence new cachline. */
255 struct rcu_data *nocb_leader ____cacheline_internodealigned_in_smp;
256 /* Leader CPU takes GP-end wakeups. */
257 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
259 /* 7) RCU CPU stall data. */
260 unsigned int softirq_snap; /* Snapshot of softirq activity. */
261 /* ->rcu_iw* fields protected by leaf rcu_node ->lock. */
262 struct irq_work rcu_iw; /* Check for non-irq activity. */
263 bool rcu_iw_pending; /* Is ->rcu_iw pending? */
264 unsigned long rcu_iw_gp_seq; /* ->gp_seq associated with ->rcu_iw. */
267 struct rcu_state *rsp;
270 /* Values for nocb_defer_wakeup field in struct rcu_data. */
271 #define RCU_NOCB_WAKE_NOT 0
272 #define RCU_NOCB_WAKE 1
273 #define RCU_NOCB_WAKE_FORCE 2
275 #define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500))
276 /* For jiffies_till_first_fqs and */
277 /* and jiffies_till_next_fqs. */
279 #define RCU_JIFFIES_FQS_DIV 256 /* Very large systems need more */
280 /* delay between bouts of */
281 /* quiescent-state forcing. */
283 #define RCU_STALL_RAT_DELAY 2 /* Allow other CPUs time to take */
284 /* at least one scheduling clock */
285 /* irq before ratting on them. */
287 #define rcu_wait(cond) \
290 set_current_state(TASK_INTERRUPTIBLE); \
295 __set_current_state(TASK_RUNNING); \
299 * RCU global state, including node hierarchy. This hierarchy is
300 * represented in "heap" form in a dense array. The root (first level)
301 * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second
302 * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]),
303 * and the third level in ->node[m+1] and following (->node[m+1] referenced
304 * by ->level[2]). The number of levels is determined by the number of
305 * CPUs and by CONFIG_RCU_FANOUT. Small systems will have a "hierarchy"
306 * consisting of a single rcu_node.
309 struct rcu_node node[NUM_RCU_NODES]; /* Hierarchy. */
310 struct rcu_node *level[RCU_NUM_LVLS + 1];
311 /* Hierarchy levels (+1 to */
312 /* shut bogus gcc warning) */
313 struct rcu_data __percpu *rda; /* pointer of percu rcu_data. */
314 call_rcu_func_t call; /* call_rcu() flavor. */
315 int ncpus; /* # CPUs seen so far. */
317 /* The following fields are guarded by the root rcu_node's lock. */
319 u8 boost ____cacheline_internodealigned_in_smp;
320 /* Subject to priority boost. */
321 unsigned long gp_seq; /* Grace-period sequence #. */
322 struct task_struct *gp_kthread; /* Task for grace periods. */
323 struct swait_queue_head gp_wq; /* Where GP task waits. */
324 short gp_flags; /* Commands for GP task. */
325 short gp_state; /* GP kthread sleep state. */
327 /* End of fields guarded by root rcu_node's lock. */
329 struct mutex barrier_mutex; /* Guards barrier fields. */
330 atomic_t barrier_cpu_count; /* # CPUs waiting on. */
331 struct completion barrier_completion; /* Wake at barrier end. */
332 unsigned long barrier_sequence; /* ++ at start and end of */
333 /* _rcu_barrier(). */
334 /* End of fields guarded by barrier_mutex. */
336 struct mutex exp_mutex; /* Serialize expedited GP. */
337 struct mutex exp_wake_mutex; /* Serialize wakeup. */
338 unsigned long expedited_sequence; /* Take a ticket. */
339 atomic_t expedited_need_qs; /* # CPUs left to check in. */
340 struct swait_queue_head expedited_wq; /* Wait for check-ins. */
341 int ncpus_snap; /* # CPUs seen last time. */
343 unsigned long jiffies_force_qs; /* Time at which to invoke */
344 /* force_quiescent_state(). */
345 unsigned long jiffies_kick_kthreads; /* Time at which to kick */
346 /* kthreads, if configured. */
347 unsigned long n_force_qs; /* Number of calls to */
348 /* force_quiescent_state(). */
349 unsigned long gp_start; /* Time at which GP started, */
350 /* but in jiffies. */
351 unsigned long gp_activity; /* Time of last GP kthread */
352 /* activity in jiffies. */
353 unsigned long gp_req_activity; /* Time of last GP request */
355 unsigned long jiffies_stall; /* Time at which to check */
356 /* for CPU stalls. */
357 unsigned long jiffies_resched; /* Time at which to resched */
358 /* a reluctant CPU. */
359 unsigned long n_force_qs_gpstart; /* Snapshot of n_force_qs at */
361 unsigned long gp_max; /* Maximum GP duration in */
363 const char *name; /* Name of structure. */
364 char abbr; /* Abbreviated name. */
365 struct list_head flavors; /* List of RCU flavors. */
368 /* Values for rcu_state structure's gp_flags field. */
369 #define RCU_GP_FLAG_INIT 0x1 /* Need grace-period initialization. */
370 #define RCU_GP_FLAG_FQS 0x2 /* Need grace-period quiescent-state forcing. */
372 /* Values for rcu_state structure's gp_state field. */
373 #define RCU_GP_IDLE 0 /* Initial state and no GP in progress. */
374 #define RCU_GP_WAIT_GPS 1 /* Wait for grace-period start. */
375 #define RCU_GP_DONE_GPS 2 /* Wait done for grace-period start. */
376 #define RCU_GP_WAIT_FQS 3 /* Wait for force-quiescent-state time. */
377 #define RCU_GP_DOING_FQS 4 /* Wait done for force-quiescent-state time. */
378 #define RCU_GP_CLEANUP 5 /* Grace-period cleanup started. */
379 #define RCU_GP_CLEANED 6 /* Grace-period cleanup complete. */
381 #ifndef RCU_TREE_NONCORE
382 static const char * const gp_state_names[] = {
391 #endif /* #ifndef RCU_TREE_NONCORE */
393 extern struct list_head rcu_struct_flavors;
395 /* Sequence through rcu_state structures for each RCU flavor. */
396 #define for_each_rcu_flavor(rsp) \
397 list_for_each_entry((rsp), &rcu_struct_flavors, flavors)
400 * RCU implementation internal declarations:
402 extern struct rcu_state rcu_sched_state;
404 extern struct rcu_state rcu_bh_state;
406 #ifdef CONFIG_PREEMPT_RCU
407 extern struct rcu_state rcu_preempt_state;
408 #endif /* #ifdef CONFIG_PREEMPT_RCU */
410 int rcu_dynticks_snap(struct rcu_dynticks *rdtp);
412 #ifdef CONFIG_RCU_BOOST
413 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
414 DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
415 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
416 DECLARE_PER_CPU(char, rcu_cpu_has_work);
417 #endif /* #ifdef CONFIG_RCU_BOOST */
419 #ifndef RCU_TREE_NONCORE
421 /* Forward declarations for rcutree_plugin.h */
422 static void rcu_bootup_announce(void);
423 static void rcu_preempt_note_context_switch(bool preempt);
424 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp);
425 #ifdef CONFIG_HOTPLUG_CPU
426 static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
427 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
428 static void rcu_print_detail_task_stall(struct rcu_state *rsp);
429 static int rcu_print_task_stall(struct rcu_node *rnp);
430 static int rcu_print_task_exp_stall(struct rcu_node *rnp);
431 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp);
432 static void rcu_preempt_check_callbacks(void);
433 void call_rcu(struct rcu_head *head, rcu_callback_t func);
434 static void __init __rcu_init_preempt(void);
435 static void dump_blkd_tasks(struct rcu_node *rnp, int ncheck);
436 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
437 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
438 static void invoke_rcu_callbacks_kthread(void);
439 static bool rcu_is_callbacks_kthread(void);
440 #ifdef CONFIG_RCU_BOOST
441 static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
442 struct rcu_node *rnp);
443 #endif /* #ifdef CONFIG_RCU_BOOST */
444 static void __init rcu_spawn_boost_kthreads(void);
445 static void rcu_prepare_kthreads(int cpu);
446 static void rcu_cleanup_after_idle(void);
447 static void rcu_prepare_for_idle(void);
448 static void rcu_idle_count_callbacks_posted(void);
449 static bool rcu_preempt_has_tasks(struct rcu_node *rnp);
450 static void print_cpu_stall_info_begin(void);
451 static void print_cpu_stall_info(struct rcu_state *rsp, int cpu);
452 static void print_cpu_stall_info_end(void);
453 static void zero_cpu_stall_ticks(struct rcu_data *rdp);
454 static void increment_cpu_stall_ticks(void);
455 static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
456 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
457 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
458 static void rcu_init_one_nocb(struct rcu_node *rnp);
459 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
460 bool lazy, unsigned long flags);
461 static bool rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp,
462 struct rcu_data *rdp,
463 unsigned long flags);
464 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp);
465 static void do_nocb_deferred_wakeup(struct rcu_data *rdp);
466 static void rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp);
467 static void rcu_spawn_all_nocb_kthreads(int cpu);
468 static void __init rcu_spawn_nocb_kthreads(void);
469 #ifdef CONFIG_RCU_NOCB_CPU
470 static void __init rcu_organize_nocb_kthreads(struct rcu_state *rsp);
471 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
472 static void __maybe_unused rcu_kick_nohz_cpu(int cpu);
473 static bool init_nocb_callback_list(struct rcu_data *rdp);
474 static void rcu_bind_gp_kthread(void);
475 static bool rcu_nohz_full_cpu(struct rcu_state *rsp);
476 static void rcu_dynticks_task_enter(void);
477 static void rcu_dynticks_task_exit(void);
480 void srcu_online_cpu(unsigned int cpu);
481 void srcu_offline_cpu(unsigned int cpu);
482 #else /* #ifdef CONFIG_SRCU */
483 void srcu_online_cpu(unsigned int cpu) { }
484 void srcu_offline_cpu(unsigned int cpu) { }
485 #endif /* #else #ifdef CONFIG_SRCU */
487 #endif /* #ifndef RCU_TREE_NONCORE */