1 /* 2 * Read-Copy Update mechanism for mutual exclusion (tree-based version) 3 * Internal non-public definitions. 4 * 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. 9 * 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. 14 * 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. 18 * 19 * Copyright IBM Corporation, 2008 20 * 21 * Author: Ingo Molnar <mingo@elte.hu> 22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com> 23 */ 24 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> 34 35 #include "rcu_segcblist.h" 36 37 /* Communicate arguments to a workqueue handler. */ 38 struct rcu_exp_work { 39 unsigned long rew_s; 40 struct work_struct rew_work; 41 }; 42 43 /* RCU's kthread states for tracing. */ 44 #define RCU_KTHREAD_STOPPED 0 45 #define RCU_KTHREAD_RUNNING 1 46 #define RCU_KTHREAD_WAITING 2 47 #define RCU_KTHREAD_OFFCPU 3 48 #define RCU_KTHREAD_YIELDING 4 49 #define RCU_KTHREAD_MAX 4 50 51 /* 52 * Definition for node within the RCU grace-period-detection hierarchy. 53 */ 54 struct rcu_node { 55 raw_spinlock_t __private lock; /* Root rcu_node's lock protects */ 56 /* some rcu_state fields as well as */ 57 /* following. */ 58 unsigned long gp_seq; /* Track rsp->rcu_gp_seq. */ 59 unsigned long gp_seq_needed; /* Track furthest future GP request. */ 60 unsigned long completedqs; /* All QSes done for this node. */ 61 unsigned long qsmask; /* CPUs or groups that need to switch in */ 62 /* order for current grace period to proceed.*/ 63 /* In leaf rcu_node, each bit corresponds to */ 64 /* an rcu_data structure, otherwise, each */ 65 /* bit corresponds to a child rcu_node */ 66 /* structure. */ 67 unsigned long rcu_gp_init_mask; /* Mask of offline CPUs at GP init. */ 68 unsigned long qsmaskinit; 69 /* Per-GP initial value for qsmask. */ 70 /* Initialized from ->qsmaskinitnext at the */ 71 /* beginning of each grace period. */ 72 unsigned long qsmaskinitnext; 73 /* Online CPUs for next grace period. */ 74 unsigned long expmask; /* CPUs or groups that need to check in */ 75 /* to allow the current expedited GP */ 76 /* to complete. */ 77 unsigned long expmaskinit; 78 /* Per-GP initial values for expmask. */ 79 /* Initialized from ->expmaskinitnext at the */ 80 /* beginning of each expedited GP. */ 81 unsigned long expmaskinitnext; 82 /* Online CPUs for next expedited GP. */ 83 /* Any CPU that has ever been online will */ 84 /* have its bit set. */ 85 unsigned long ffmask; /* Fully functional CPUs. */ 86 unsigned long grpmask; /* Mask to apply to parent qsmask. */ 87 /* Only one bit will be set in this mask. */ 88 int grplo; /* lowest-numbered CPU or group here. */ 89 int grphi; /* highest-numbered CPU or group here. */ 90 u8 grpnum; /* CPU/group number for next level up. */ 91 u8 level; /* root is at level 0. */ 92 bool wait_blkd_tasks;/* Necessary to wait for blocked tasks to */ 93 /* exit RCU read-side critical sections */ 94 /* before propagating offline up the */ 95 /* rcu_node tree? */ 96 struct rcu_node *parent; 97 struct list_head blkd_tasks; 98 /* Tasks blocked in RCU read-side critical */ 99 /* section. Tasks are placed at the head */ 100 /* of this list and age towards the tail. */ 101 struct list_head *gp_tasks; 102 /* Pointer to the first task blocking the */ 103 /* current grace period, or NULL if there */ 104 /* is no such task. */ 105 struct list_head *exp_tasks; 106 /* Pointer to the first task blocking the */ 107 /* current expedited grace period, or NULL */ 108 /* if there is no such task. If there */ 109 /* is no current expedited grace period, */ 110 /* then there can cannot be any such task. */ 111 struct list_head *boost_tasks; 112 /* Pointer to first task that needs to be */ 113 /* priority boosted, or NULL if no priority */ 114 /* boosting is needed for this rcu_node */ 115 /* structure. If there are no tasks */ 116 /* queued on this rcu_node structure that */ 117 /* are blocking the current grace period, */ 118 /* there can be no such task. */ 119 struct rt_mutex boost_mtx; 120 /* Used only for the priority-boosting */ 121 /* side effect, not as a lock. */ 122 unsigned long boost_time; 123 /* When to start boosting (jiffies). */ 124 struct task_struct *boost_kthread_task; 125 /* kthread that takes care of priority */ 126 /* boosting for this rcu_node structure. */ 127 unsigned int boost_kthread_status; 128 /* State of boost_kthread_task for tracing. */ 129 #ifdef CONFIG_RCU_NOCB_CPU 130 struct swait_queue_head nocb_gp_wq[2]; 131 /* Place for rcu_nocb_kthread() to wait GP. */ 132 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */ 133 raw_spinlock_t fqslock ____cacheline_internodealigned_in_smp; 134 135 spinlock_t exp_lock ____cacheline_internodealigned_in_smp; 136 unsigned long exp_seq_rq; 137 wait_queue_head_t exp_wq[4]; 138 struct rcu_exp_work rew; 139 bool exp_need_flush; /* Need to flush workitem? */ 140 } ____cacheline_internodealigned_in_smp; 141 142 /* 143 * Bitmasks in an rcu_node cover the interval [grplo, grphi] of CPU IDs, and 144 * are indexed relative to this interval rather than the global CPU ID space. 145 * This generates the bit for a CPU in node-local masks. 146 */ 147 #define leaf_node_cpu_bit(rnp, cpu) (BIT((cpu) - (rnp)->grplo)) 148 149 /* 150 * Union to allow "aggregate OR" operation on the need for a quiescent 151 * state by the normal and expedited grace periods. 152 */ 153 union rcu_noqs { 154 struct { 155 u8 norm; 156 u8 exp; 157 } b; /* Bits. */ 158 u16 s; /* Set of bits, aggregate OR here. */ 159 }; 160 161 /* Per-CPU data for read-copy update. */ 162 struct rcu_data { 163 /* 1) quiescent-state and grace-period handling : */ 164 unsigned long gp_seq; /* Track rsp->rcu_gp_seq counter. */ 165 unsigned long gp_seq_needed; /* Track furthest future GP request. */ 166 union rcu_noqs cpu_no_qs; /* No QSes yet for this CPU. */ 167 bool core_needs_qs; /* Core waits for quiesc state. */ 168 bool beenonline; /* CPU online at least once. */ 169 bool gpwrap; /* Possible ->gp_seq wrap. */ 170 bool deferred_qs; /* This CPU awaiting a deferred QS? */ 171 struct rcu_node *mynode; /* This CPU's leaf of hierarchy */ 172 unsigned long grpmask; /* Mask to apply to leaf qsmask. */ 173 unsigned long ticks_this_gp; /* The number of scheduling-clock */ 174 /* ticks this CPU has handled */ 175 /* during and after the last grace */ 176 /* period it is aware of. */ 177 178 /* 2) batch handling */ 179 struct rcu_segcblist cblist; /* Segmented callback list, with */ 180 /* different callbacks waiting for */ 181 /* different grace periods. */ 182 long qlen_last_fqs_check; 183 /* qlen at last check for QS forcing */ 184 unsigned long n_force_qs_snap; 185 /* did other CPU force QS recently? */ 186 long blimit; /* Upper limit on a processed batch */ 187 188 /* 3) dynticks interface. */ 189 int dynticks_snap; /* Per-GP tracking for dynticks. */ 190 long dynticks_nesting; /* Track process nesting level. */ 191 long dynticks_nmi_nesting; /* Track irq/NMI nesting level. */ 192 atomic_t dynticks; /* Even value for idle, else odd. */ 193 bool rcu_need_heavy_qs; /* GP old, so heavy quiescent state! */ 194 bool rcu_urgent_qs; /* GP old need light quiescent state. */ 195 #ifdef CONFIG_RCU_FAST_NO_HZ 196 bool all_lazy; /* Are all CPU's CBs lazy? */ 197 unsigned long nonlazy_posted; /* # times non-lazy CB posted to CPU. */ 198 unsigned long nonlazy_posted_snap; 199 /* Nonlazy_posted snapshot. */ 200 unsigned long last_accelerate; /* Last jiffy CBs were accelerated. */ 201 unsigned long last_advance_all; /* Last jiffy CBs were all advanced. */ 202 int tick_nohz_enabled_snap; /* Previously seen value from sysfs. */ 203 #endif /* #ifdef CONFIG_RCU_FAST_NO_HZ */ 204 205 /* 4) rcu_barrier(), OOM callbacks, and expediting. */ 206 struct rcu_head barrier_head; 207 int exp_dynticks_snap; /* Double-check need for IPI. */ 208 209 /* 5) Callback offloading. */ 210 #ifdef CONFIG_RCU_NOCB_CPU 211 struct rcu_head *nocb_head; /* CBs waiting for kthread. */ 212 struct rcu_head **nocb_tail; 213 atomic_long_t nocb_q_count; /* # CBs waiting for nocb */ 214 atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */ 215 struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */ 216 struct rcu_head **nocb_follower_tail; 217 struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */ 218 struct task_struct *nocb_kthread; 219 raw_spinlock_t nocb_lock; /* Guard following pair of fields. */ 220 int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */ 221 struct timer_list nocb_timer; /* Enforce finite deferral. */ 222 223 /* The following fields are used by the leader, hence own cacheline. */ 224 struct rcu_head *nocb_gp_head ____cacheline_internodealigned_in_smp; 225 /* CBs waiting for GP. */ 226 struct rcu_head **nocb_gp_tail; 227 bool nocb_leader_sleep; /* Is the nocb leader thread asleep? */ 228 struct rcu_data *nocb_next_follower; 229 /* Next follower in wakeup chain. */ 230 231 /* The following fields are used by the follower, hence new cachline. */ 232 struct rcu_data *nocb_leader ____cacheline_internodealigned_in_smp; 233 /* Leader CPU takes GP-end wakeups. */ 234 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */ 235 236 /* 6) Diagnostic data, including RCU CPU stall warnings. */ 237 unsigned int softirq_snap; /* Snapshot of softirq activity. */ 238 /* ->rcu_iw* fields protected by leaf rcu_node ->lock. */ 239 struct irq_work rcu_iw; /* Check for non-irq activity. */ 240 bool rcu_iw_pending; /* Is ->rcu_iw pending? */ 241 unsigned long rcu_iw_gp_seq; /* ->gp_seq associated with ->rcu_iw. */ 242 unsigned long rcu_ofl_gp_seq; /* ->gp_seq at last offline. */ 243 short rcu_ofl_gp_flags; /* ->gp_flags at last offline. */ 244 unsigned long rcu_onl_gp_seq; /* ->gp_seq at last online. */ 245 short rcu_onl_gp_flags; /* ->gp_flags at last online. */ 246 unsigned long last_fqs_resched; /* Time of last rcu_resched(). */ 247 248 int cpu; 249 }; 250 251 /* Values for nocb_defer_wakeup field in struct rcu_data. */ 252 #define RCU_NOCB_WAKE_NOT 0 253 #define RCU_NOCB_WAKE 1 254 #define RCU_NOCB_WAKE_FORCE 2 255 256 #define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500)) 257 /* For jiffies_till_first_fqs and */ 258 /* and jiffies_till_next_fqs. */ 259 260 #define RCU_JIFFIES_FQS_DIV 256 /* Very large systems need more */ 261 /* delay between bouts of */ 262 /* quiescent-state forcing. */ 263 264 #define RCU_STALL_RAT_DELAY 2 /* Allow other CPUs time to take */ 265 /* at least one scheduling clock */ 266 /* irq before ratting on them. */ 267 268 #define rcu_wait(cond) \ 269 do { \ 270 for (;;) { \ 271 set_current_state(TASK_INTERRUPTIBLE); \ 272 if (cond) \ 273 break; \ 274 schedule(); \ 275 } \ 276 __set_current_state(TASK_RUNNING); \ 277 } while (0) 278 279 /* 280 * RCU global state, including node hierarchy. This hierarchy is 281 * represented in "heap" form in a dense array. The root (first level) 282 * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second 283 * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]), 284 * and the third level in ->node[m+1] and following (->node[m+1] referenced 285 * by ->level[2]). The number of levels is determined by the number of 286 * CPUs and by CONFIG_RCU_FANOUT. Small systems will have a "hierarchy" 287 * consisting of a single rcu_node. 288 */ 289 struct rcu_state { 290 struct rcu_node node[NUM_RCU_NODES]; /* Hierarchy. */ 291 struct rcu_node *level[RCU_NUM_LVLS + 1]; 292 /* Hierarchy levels (+1 to */ 293 /* shut bogus gcc warning) */ 294 int ncpus; /* # CPUs seen so far. */ 295 296 /* The following fields are guarded by the root rcu_node's lock. */ 297 298 u8 boost ____cacheline_internodealigned_in_smp; 299 /* Subject to priority boost. */ 300 unsigned long gp_seq; /* Grace-period sequence #. */ 301 struct task_struct *gp_kthread; /* Task for grace periods. */ 302 struct swait_queue_head gp_wq; /* Where GP task waits. */ 303 short gp_flags; /* Commands for GP task. */ 304 short gp_state; /* GP kthread sleep state. */ 305 306 /* End of fields guarded by root rcu_node's lock. */ 307 308 struct mutex barrier_mutex; /* Guards barrier fields. */ 309 atomic_t barrier_cpu_count; /* # CPUs waiting on. */ 310 struct completion barrier_completion; /* Wake at barrier end. */ 311 unsigned long barrier_sequence; /* ++ at start and end of */ 312 /* rcu_barrier(). */ 313 /* End of fields guarded by barrier_mutex. */ 314 315 struct mutex exp_mutex; /* Serialize expedited GP. */ 316 struct mutex exp_wake_mutex; /* Serialize wakeup. */ 317 unsigned long expedited_sequence; /* Take a ticket. */ 318 atomic_t expedited_need_qs; /* # CPUs left to check in. */ 319 struct swait_queue_head expedited_wq; /* Wait for check-ins. */ 320 int ncpus_snap; /* # CPUs seen last time. */ 321 322 unsigned long jiffies_force_qs; /* Time at which to invoke */ 323 /* force_quiescent_state(). */ 324 unsigned long jiffies_kick_kthreads; /* Time at which to kick */ 325 /* kthreads, if configured. */ 326 unsigned long n_force_qs; /* Number of calls to */ 327 /* force_quiescent_state(). */ 328 unsigned long gp_start; /* Time at which GP started, */ 329 /* but in jiffies. */ 330 unsigned long gp_end; /* Time last GP ended, again */ 331 /* in jiffies. */ 332 unsigned long gp_activity; /* Time of last GP kthread */ 333 /* activity in jiffies. */ 334 unsigned long gp_req_activity; /* Time of last GP request */ 335 /* in jiffies. */ 336 unsigned long jiffies_stall; /* Time at which to check */ 337 /* for CPU stalls. */ 338 unsigned long jiffies_resched; /* Time at which to resched */ 339 /* a reluctant CPU. */ 340 unsigned long n_force_qs_gpstart; /* Snapshot of n_force_qs at */ 341 /* GP start. */ 342 unsigned long gp_max; /* Maximum GP duration in */ 343 /* jiffies. */ 344 const char *name; /* Name of structure. */ 345 char abbr; /* Abbreviated name. */ 346 347 raw_spinlock_t ofl_lock ____cacheline_internodealigned_in_smp; 348 /* Synchronize offline with */ 349 /* GP pre-initialization. */ 350 }; 351 352 /* Values for rcu_state structure's gp_flags field. */ 353 #define RCU_GP_FLAG_INIT 0x1 /* Need grace-period initialization. */ 354 #define RCU_GP_FLAG_FQS 0x2 /* Need grace-period quiescent-state forcing. */ 355 356 /* Values for rcu_state structure's gp_state field. */ 357 #define RCU_GP_IDLE 0 /* Initial state and no GP in progress. */ 358 #define RCU_GP_WAIT_GPS 1 /* Wait for grace-period start. */ 359 #define RCU_GP_DONE_GPS 2 /* Wait done for grace-period start. */ 360 #define RCU_GP_ONOFF 3 /* Grace-period initialization hotplug. */ 361 #define RCU_GP_INIT 4 /* Grace-period initialization. */ 362 #define RCU_GP_WAIT_FQS 5 /* Wait for force-quiescent-state time. */ 363 #define RCU_GP_DOING_FQS 6 /* Wait done for force-quiescent-state time. */ 364 #define RCU_GP_CLEANUP 7 /* Grace-period cleanup started. */ 365 #define RCU_GP_CLEANED 8 /* Grace-period cleanup complete. */ 366 367 static const char * const gp_state_names[] = { 368 "RCU_GP_IDLE", 369 "RCU_GP_WAIT_GPS", 370 "RCU_GP_DONE_GPS", 371 "RCU_GP_ONOFF", 372 "RCU_GP_INIT", 373 "RCU_GP_WAIT_FQS", 374 "RCU_GP_DOING_FQS", 375 "RCU_GP_CLEANUP", 376 "RCU_GP_CLEANED", 377 }; 378 379 /* 380 * In order to export the rcu_state name to the tracing tools, it 381 * needs to be added in the __tracepoint_string section. 382 * This requires defining a separate variable tp_<sname>_varname 383 * that points to the string being used, and this will allow 384 * the tracing userspace tools to be able to decipher the string 385 * address to the matching string. 386 */ 387 #ifdef CONFIG_PREEMPT_RCU 388 #define RCU_ABBR 'p' 389 #define RCU_NAME_RAW "rcu_preempt" 390 #else /* #ifdef CONFIG_PREEMPT_RCU */ 391 #define RCU_ABBR 's' 392 #define RCU_NAME_RAW "rcu_sched" 393 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */ 394 #ifndef CONFIG_TRACING 395 #define RCU_NAME RCU_NAME_RAW 396 #else /* #ifdef CONFIG_TRACING */ 397 static char rcu_name[] = RCU_NAME_RAW; 398 static const char *tp_rcu_varname __used __tracepoint_string = rcu_name; 399 #define RCU_NAME rcu_name 400 #endif /* #else #ifdef CONFIG_TRACING */ 401 402 int rcu_dynticks_snap(struct rcu_data *rdp); 403 404 #ifdef CONFIG_RCU_BOOST 405 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status); 406 DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu); 407 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops); 408 DECLARE_PER_CPU(char, rcu_cpu_has_work); 409 #endif /* #ifdef CONFIG_RCU_BOOST */ 410 411 /* Forward declarations for rcutree_plugin.h */ 412 static void rcu_bootup_announce(void); 413 static void rcu_qs(void); 414 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp); 415 #ifdef CONFIG_HOTPLUG_CPU 416 static bool rcu_preempt_has_tasks(struct rcu_node *rnp); 417 #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 418 static void rcu_print_detail_task_stall(void); 419 static int rcu_print_task_stall(struct rcu_node *rnp); 420 static int rcu_print_task_exp_stall(struct rcu_node *rnp); 421 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp); 422 static void rcu_flavor_check_callbacks(int user); 423 void call_rcu(struct rcu_head *head, rcu_callback_t func); 424 static void dump_blkd_tasks(struct rcu_node *rnp, int ncheck); 425 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags); 426 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp); 427 static void invoke_rcu_callbacks_kthread(void); 428 static bool rcu_is_callbacks_kthread(void); 429 static void __init rcu_spawn_boost_kthreads(void); 430 static void rcu_prepare_kthreads(int cpu); 431 static void rcu_cleanup_after_idle(void); 432 static void rcu_prepare_for_idle(void); 433 static void rcu_idle_count_callbacks_posted(void); 434 static bool rcu_preempt_has_tasks(struct rcu_node *rnp); 435 static bool rcu_preempt_need_deferred_qs(struct task_struct *t); 436 static void rcu_preempt_deferred_qs(struct task_struct *t); 437 static void print_cpu_stall_info_begin(void); 438 static void print_cpu_stall_info(int cpu); 439 static void print_cpu_stall_info_end(void); 440 static void zero_cpu_stall_ticks(struct rcu_data *rdp); 441 static bool rcu_nocb_cpu_needs_barrier(int cpu); 442 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp); 443 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq); 444 static void rcu_init_one_nocb(struct rcu_node *rnp); 445 static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp, 446 bool lazy, unsigned long flags); 447 static bool rcu_nocb_adopt_orphan_cbs(struct rcu_data *my_rdp, 448 struct rcu_data *rdp, 449 unsigned long flags); 450 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp); 451 static void do_nocb_deferred_wakeup(struct rcu_data *rdp); 452 static void rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp); 453 static void rcu_spawn_cpu_nocb_kthread(int cpu); 454 static void __init rcu_spawn_nocb_kthreads(void); 455 #ifdef CONFIG_RCU_NOCB_CPU 456 static void __init rcu_organize_nocb_kthreads(void); 457 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */ 458 static bool init_nocb_callback_list(struct rcu_data *rdp); 459 static unsigned long rcu_get_n_cbs_nocb_cpu(struct rcu_data *rdp); 460 static void rcu_bind_gp_kthread(void); 461 static bool rcu_nohz_full_cpu(void); 462 static void rcu_dynticks_task_enter(void); 463 static void rcu_dynticks_task_exit(void); 464 465 #ifdef CONFIG_SRCU 466 void srcu_online_cpu(unsigned int cpu); 467 void srcu_offline_cpu(unsigned int cpu); 468 #else /* #ifdef CONFIG_SRCU */ 469 void srcu_online_cpu(unsigned int cpu) { } 470 void srcu_offline_cpu(unsigned int cpu) { } 471 #endif /* #else #ifdef CONFIG_SRCU */ 472