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