1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * RCU expedited grace periods 4 * 5 * Copyright IBM Corporation, 2016 6 * 7 * Authors: Paul E. McKenney <paulmck@linux.ibm.com> 8 */ 9 10 #include <linux/console.h> 11 #include <linux/lockdep.h> 12 13 static void rcu_exp_handler(void *unused); 14 static int rcu_print_task_exp_stall(struct rcu_node *rnp); 15 static void rcu_exp_print_detail_task_stall_rnp(struct rcu_node *rnp); 16 17 /* 18 * Record the start of an expedited grace period. 19 */ 20 static void rcu_exp_gp_seq_start(void) 21 { 22 rcu_seq_start(&rcu_state.expedited_sequence); 23 rcu_poll_gp_seq_start_unlocked(&rcu_state.gp_seq_polled_exp_snap); 24 } 25 26 /* 27 * Return the value that the expedited-grace-period counter will have 28 * at the end of the current grace period. 29 */ 30 static __maybe_unused unsigned long rcu_exp_gp_seq_endval(void) 31 { 32 return rcu_seq_endval(&rcu_state.expedited_sequence); 33 } 34 35 /* 36 * Record the end of an expedited grace period. 37 */ 38 static void rcu_exp_gp_seq_end(void) 39 { 40 rcu_poll_gp_seq_end_unlocked(&rcu_state.gp_seq_polled_exp_snap); 41 rcu_seq_end(&rcu_state.expedited_sequence); 42 smp_mb(); /* Ensure that consecutive grace periods serialize. */ 43 } 44 45 /* 46 * Take a snapshot of the expedited-grace-period counter, which is the 47 * earliest value that will indicate that a full grace period has 48 * elapsed since the current time. 49 */ 50 static unsigned long rcu_exp_gp_seq_snap(void) 51 { 52 unsigned long s; 53 54 smp_mb(); /* Caller's modifications seen first by other CPUs. */ 55 s = rcu_seq_snap(&rcu_state.expedited_sequence); 56 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("snap")); 57 return s; 58 } 59 60 /* 61 * Given a counter snapshot from rcu_exp_gp_seq_snap(), return true 62 * if a full expedited grace period has elapsed since that snapshot 63 * was taken. 64 */ 65 static bool rcu_exp_gp_seq_done(unsigned long s) 66 { 67 return rcu_seq_done(&rcu_state.expedited_sequence, s); 68 } 69 70 /* 71 * Reset the ->expmaskinit values in the rcu_node tree to reflect any 72 * recent CPU-online activity. Note that these masks are not cleared 73 * when CPUs go offline, so they reflect the union of all CPUs that have 74 * ever been online. This means that this function normally takes its 75 * no-work-to-do fastpath. 76 */ 77 static void sync_exp_reset_tree_hotplug(void) 78 { 79 bool done; 80 unsigned long flags; 81 unsigned long mask; 82 unsigned long oldmask; 83 int ncpus = smp_load_acquire(&rcu_state.ncpus); /* Order vs. locking. */ 84 struct rcu_node *rnp; 85 struct rcu_node *rnp_up; 86 87 /* If no new CPUs onlined since last time, nothing to do. */ 88 if (likely(ncpus == rcu_state.ncpus_snap)) 89 return; 90 rcu_state.ncpus_snap = ncpus; 91 92 /* 93 * Each pass through the following loop propagates newly onlined 94 * CPUs for the current rcu_node structure up the rcu_node tree. 95 */ 96 rcu_for_each_leaf_node(rnp) { 97 raw_spin_lock_irqsave_rcu_node(rnp, flags); 98 if (rnp->expmaskinit == rnp->expmaskinitnext) { 99 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 100 continue; /* No new CPUs, nothing to do. */ 101 } 102 103 /* Update this node's mask, track old value for propagation. */ 104 oldmask = rnp->expmaskinit; 105 rnp->expmaskinit = rnp->expmaskinitnext; 106 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 107 108 /* If was already nonzero, nothing to propagate. */ 109 if (oldmask) 110 continue; 111 112 /* Propagate the new CPU up the tree. */ 113 mask = rnp->grpmask; 114 rnp_up = rnp->parent; 115 done = false; 116 while (rnp_up) { 117 raw_spin_lock_irqsave_rcu_node(rnp_up, flags); 118 if (rnp_up->expmaskinit) 119 done = true; 120 rnp_up->expmaskinit |= mask; 121 raw_spin_unlock_irqrestore_rcu_node(rnp_up, flags); 122 if (done) 123 break; 124 mask = rnp_up->grpmask; 125 rnp_up = rnp_up->parent; 126 } 127 } 128 } 129 130 /* 131 * Reset the ->expmask values in the rcu_node tree in preparation for 132 * a new expedited grace period. 133 */ 134 static void __maybe_unused sync_exp_reset_tree(void) 135 { 136 unsigned long flags; 137 struct rcu_node *rnp; 138 139 sync_exp_reset_tree_hotplug(); 140 rcu_for_each_node_breadth_first(rnp) { 141 raw_spin_lock_irqsave_rcu_node(rnp, flags); 142 WARN_ON_ONCE(rnp->expmask); 143 WRITE_ONCE(rnp->expmask, rnp->expmaskinit); 144 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 145 } 146 } 147 148 /* 149 * Return non-zero if there is no RCU expedited grace period in progress 150 * for the specified rcu_node structure, in other words, if all CPUs and 151 * tasks covered by the specified rcu_node structure have done their bit 152 * for the current expedited grace period. 153 */ 154 static bool sync_rcu_exp_done(struct rcu_node *rnp) 155 { 156 raw_lockdep_assert_held_rcu_node(rnp); 157 return READ_ONCE(rnp->exp_tasks) == NULL && 158 READ_ONCE(rnp->expmask) == 0; 159 } 160 161 /* 162 * Like sync_rcu_exp_done(), but where the caller does not hold the 163 * rcu_node's ->lock. 164 */ 165 static bool sync_rcu_exp_done_unlocked(struct rcu_node *rnp) 166 { 167 unsigned long flags; 168 bool ret; 169 170 raw_spin_lock_irqsave_rcu_node(rnp, flags); 171 ret = sync_rcu_exp_done(rnp); 172 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 173 174 return ret; 175 } 176 177 /* 178 * Report the exit from RCU read-side critical section for the last task 179 * that queued itself during or before the current expedited preemptible-RCU 180 * grace period. This event is reported either to the rcu_node structure on 181 * which the task was queued or to one of that rcu_node structure's ancestors, 182 * recursively up the tree. (Calm down, calm down, we do the recursion 183 * iteratively!) 184 */ 185 static void __rcu_report_exp_rnp(struct rcu_node *rnp, 186 bool wake, unsigned long flags) 187 __releases(rnp->lock) 188 { 189 unsigned long mask; 190 191 raw_lockdep_assert_held_rcu_node(rnp); 192 for (;;) { 193 if (!sync_rcu_exp_done(rnp)) { 194 if (!rnp->expmask) 195 rcu_initiate_boost(rnp, flags); 196 else 197 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 198 break; 199 } 200 if (rnp->parent == NULL) { 201 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 202 if (wake) 203 swake_up_one_online(&rcu_state.expedited_wq); 204 205 break; 206 } 207 mask = rnp->grpmask; 208 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled */ 209 rnp = rnp->parent; 210 raw_spin_lock_rcu_node(rnp); /* irqs already disabled */ 211 WARN_ON_ONCE(!(rnp->expmask & mask)); 212 WRITE_ONCE(rnp->expmask, rnp->expmask & ~mask); 213 } 214 } 215 216 /* 217 * Report expedited quiescent state for specified node. This is a 218 * lock-acquisition wrapper function for __rcu_report_exp_rnp(). 219 */ 220 static void __maybe_unused rcu_report_exp_rnp(struct rcu_node *rnp, bool wake) 221 { 222 unsigned long flags; 223 224 raw_spin_lock_irqsave_rcu_node(rnp, flags); 225 __rcu_report_exp_rnp(rnp, wake, flags); 226 } 227 228 /* 229 * Report expedited quiescent state for multiple CPUs, all covered by the 230 * specified leaf rcu_node structure, which is acquired by the caller. 231 */ 232 static void rcu_report_exp_cpu_mult(struct rcu_node *rnp, unsigned long flags, 233 unsigned long mask, bool wake) 234 __releases(rnp->lock) 235 { 236 int cpu; 237 struct rcu_data *rdp; 238 239 raw_lockdep_assert_held_rcu_node(rnp); 240 if (!(rnp->expmask & mask)) { 241 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 242 return; 243 } 244 WRITE_ONCE(rnp->expmask, rnp->expmask & ~mask); 245 for_each_leaf_node_cpu_mask(rnp, cpu, mask) { 246 rdp = per_cpu_ptr(&rcu_data, cpu); 247 if (!IS_ENABLED(CONFIG_NO_HZ_FULL) || !rdp->rcu_forced_tick_exp) 248 continue; 249 rdp->rcu_forced_tick_exp = false; 250 tick_dep_clear_cpu(cpu, TICK_DEP_BIT_RCU_EXP); 251 } 252 __rcu_report_exp_rnp(rnp, wake, flags); /* Releases rnp->lock. */ 253 } 254 255 /* 256 * Report expedited quiescent state for specified rcu_data (CPU). 257 */ 258 static void rcu_report_exp_rdp(struct rcu_data *rdp) 259 { 260 unsigned long flags; 261 struct rcu_node *rnp = rdp->mynode; 262 263 raw_spin_lock_irqsave_rcu_node(rnp, flags); 264 WRITE_ONCE(rdp->cpu_no_qs.b.exp, false); 265 ASSERT_EXCLUSIVE_WRITER(rdp->cpu_no_qs.b.exp); 266 rcu_report_exp_cpu_mult(rnp, flags, rdp->grpmask, true); 267 } 268 269 /* Common code for work-done checking. */ 270 static bool sync_exp_work_done(unsigned long s) 271 { 272 if (rcu_exp_gp_seq_done(s)) { 273 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("done")); 274 /* 275 * Order GP completion with preceding accesses. Order also GP 276 * completion with post GP update side accesses. Pairs with 277 * rcu_seq_end(). 278 */ 279 smp_mb(); 280 return true; 281 } 282 return false; 283 } 284 285 /* 286 * Funnel-lock acquisition for expedited grace periods. Returns true 287 * if some other task completed an expedited grace period that this task 288 * can piggy-back on, and with no mutex held. Otherwise, returns false 289 * with the mutex held, indicating that the caller must actually do the 290 * expedited grace period. 291 */ 292 static bool exp_funnel_lock(unsigned long s) 293 { 294 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, raw_smp_processor_id()); 295 struct rcu_node *rnp = rdp->mynode; 296 struct rcu_node *rnp_root = rcu_get_root(); 297 298 /* Low-contention fastpath. */ 299 if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s) && 300 (rnp == rnp_root || 301 ULONG_CMP_LT(READ_ONCE(rnp_root->exp_seq_rq), s)) && 302 mutex_trylock(&rcu_state.exp_mutex)) 303 goto fastpath; 304 305 /* 306 * Each pass through the following loop works its way up 307 * the rcu_node tree, returning if others have done the work or 308 * otherwise falls through to acquire ->exp_mutex. The mapping 309 * from CPU to rcu_node structure can be inexact, as it is just 310 * promoting locality and is not strictly needed for correctness. 311 */ 312 for (; rnp != NULL; rnp = rnp->parent) { 313 if (sync_exp_work_done(s)) 314 return true; 315 316 /* Work not done, either wait here or go up. */ 317 spin_lock(&rnp->exp_lock); 318 if (ULONG_CMP_GE(rnp->exp_seq_rq, s)) { 319 320 /* Someone else doing GP, so wait for them. */ 321 spin_unlock(&rnp->exp_lock); 322 trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level, 323 rnp->grplo, rnp->grphi, 324 TPS("wait")); 325 wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3], 326 sync_exp_work_done(s)); 327 return true; 328 } 329 WRITE_ONCE(rnp->exp_seq_rq, s); /* Followers can wait on us. */ 330 spin_unlock(&rnp->exp_lock); 331 trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level, 332 rnp->grplo, rnp->grphi, TPS("nxtlvl")); 333 } 334 mutex_lock(&rcu_state.exp_mutex); 335 fastpath: 336 if (sync_exp_work_done(s)) { 337 mutex_unlock(&rcu_state.exp_mutex); 338 return true; 339 } 340 rcu_exp_gp_seq_start(); 341 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("start")); 342 return false; 343 } 344 345 /* 346 * Select the CPUs within the specified rcu_node that the upcoming 347 * expedited grace period needs to wait for. 348 */ 349 static void __sync_rcu_exp_select_node_cpus(struct rcu_exp_work *rewp) 350 { 351 int cpu; 352 unsigned long flags; 353 unsigned long mask_ofl_test; 354 unsigned long mask_ofl_ipi; 355 int ret; 356 struct rcu_node *rnp = container_of(rewp, struct rcu_node, rew); 357 358 raw_spin_lock_irqsave_rcu_node(rnp, flags); 359 360 /* Each pass checks a CPU for identity, offline, and idle. */ 361 mask_ofl_test = 0; 362 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) { 363 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 364 unsigned long mask = rdp->grpmask; 365 int snap; 366 367 if (raw_smp_processor_id() == cpu || 368 !(rnp->qsmaskinitnext & mask)) { 369 mask_ofl_test |= mask; 370 } else { 371 /* 372 * Full ordering between remote CPU's post idle accesses 373 * and updater's accesses prior to current GP (and also 374 * the started GP sequence number) is enforced by 375 * rcu_seq_start() implicit barrier, relayed by kworkers 376 * locking and even further by smp_mb__after_unlock_lock() 377 * barriers chained all the way throughout the rnp locking 378 * tree since sync_exp_reset_tree() and up to the current 379 * leaf rnp locking. 380 * 381 * Ordering between remote CPU's pre idle accesses and 382 * post grace period updater's accesses is enforced by the 383 * below acquire semantic. 384 */ 385 snap = ct_rcu_watching_cpu_acquire(cpu); 386 if (rcu_watching_snap_in_eqs(snap)) 387 mask_ofl_test |= mask; 388 else 389 rdp->exp_watching_snap = snap; 390 } 391 } 392 mask_ofl_ipi = rnp->expmask & ~mask_ofl_test; 393 394 /* 395 * Need to wait for any blocked tasks as well. Note that 396 * additional blocking tasks will also block the expedited GP 397 * until such time as the ->expmask bits are cleared. 398 */ 399 if (rcu_preempt_has_tasks(rnp)) 400 WRITE_ONCE(rnp->exp_tasks, rnp->blkd_tasks.next); 401 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 402 403 /* IPI the remaining CPUs for expedited quiescent state. */ 404 for_each_leaf_node_cpu_mask(rnp, cpu, mask_ofl_ipi) { 405 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu); 406 unsigned long mask = rdp->grpmask; 407 408 retry_ipi: 409 if (rcu_watching_snap_stopped_since(rdp, rdp->exp_watching_snap)) { 410 mask_ofl_test |= mask; 411 continue; 412 } 413 if (get_cpu() == cpu) { 414 mask_ofl_test |= mask; 415 put_cpu(); 416 continue; 417 } 418 ret = smp_call_function_single(cpu, rcu_exp_handler, NULL, 0); 419 put_cpu(); 420 /* The CPU will report the QS in response to the IPI. */ 421 if (!ret) 422 continue; 423 424 /* Failed, raced with CPU hotplug operation. */ 425 raw_spin_lock_irqsave_rcu_node(rnp, flags); 426 if ((rnp->qsmaskinitnext & mask) && 427 (rnp->expmask & mask)) { 428 /* Online, so delay for a bit and try again. */ 429 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 430 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("selectofl")); 431 schedule_timeout_idle(1); 432 goto retry_ipi; 433 } 434 /* CPU really is offline, so we must report its QS. */ 435 if (rnp->expmask & mask) 436 mask_ofl_test |= mask; 437 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 438 } 439 /* Report quiescent states for those that went offline. */ 440 if (mask_ofl_test) { 441 raw_spin_lock_irqsave_rcu_node(rnp, flags); 442 rcu_report_exp_cpu_mult(rnp, flags, mask_ofl_test, false); 443 } 444 } 445 446 static void rcu_exp_sel_wait_wake(unsigned long s); 447 448 static void sync_rcu_exp_select_node_cpus(struct kthread_work *wp) 449 { 450 struct rcu_exp_work *rewp = 451 container_of(wp, struct rcu_exp_work, rew_work); 452 453 __sync_rcu_exp_select_node_cpus(rewp); 454 } 455 456 static inline bool rcu_exp_worker_started(void) 457 { 458 return !!READ_ONCE(rcu_exp_gp_kworker); 459 } 460 461 static inline bool rcu_exp_par_worker_started(struct rcu_node *rnp) 462 { 463 return !!READ_ONCE(rnp->exp_kworker); 464 } 465 466 static inline void sync_rcu_exp_select_cpus_queue_work(struct rcu_node *rnp) 467 { 468 kthread_init_work(&rnp->rew.rew_work, sync_rcu_exp_select_node_cpus); 469 /* 470 * Use rcu_exp_par_gp_kworker, because flushing a work item from 471 * another work item on the same kthread worker can result in 472 * deadlock. 473 */ 474 kthread_queue_work(READ_ONCE(rnp->exp_kworker), &rnp->rew.rew_work); 475 } 476 477 static inline void sync_rcu_exp_select_cpus_flush_work(struct rcu_node *rnp) 478 { 479 kthread_flush_work(&rnp->rew.rew_work); 480 } 481 482 /* 483 * Work-queue handler to drive an expedited grace period forward. 484 */ 485 static void wait_rcu_exp_gp(struct kthread_work *wp) 486 { 487 struct rcu_exp_work *rewp; 488 489 rewp = container_of(wp, struct rcu_exp_work, rew_work); 490 rcu_exp_sel_wait_wake(rewp->rew_s); 491 } 492 493 static inline void synchronize_rcu_expedited_queue_work(struct rcu_exp_work *rew) 494 { 495 kthread_init_work(&rew->rew_work, wait_rcu_exp_gp); 496 kthread_queue_work(rcu_exp_gp_kworker, &rew->rew_work); 497 } 498 499 /* 500 * Select the nodes that the upcoming expedited grace period needs 501 * to wait for. 502 */ 503 static void sync_rcu_exp_select_cpus(void) 504 { 505 struct rcu_node *rnp; 506 507 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("reset")); 508 sync_exp_reset_tree(); 509 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("select")); 510 511 /* Schedule work for each leaf rcu_node structure. */ 512 rcu_for_each_leaf_node(rnp) { 513 rnp->exp_need_flush = false; 514 if (!READ_ONCE(rnp->expmask)) 515 continue; /* Avoid early boot non-existent wq. */ 516 if (!rcu_exp_par_worker_started(rnp) || 517 rcu_scheduler_active != RCU_SCHEDULER_RUNNING || 518 rcu_is_last_leaf_node(rnp)) { 519 /* No worker started yet or last leaf, do direct call. */ 520 sync_rcu_exp_select_node_cpus(&rnp->rew.rew_work); 521 continue; 522 } 523 sync_rcu_exp_select_cpus_queue_work(rnp); 524 rnp->exp_need_flush = true; 525 } 526 527 /* Wait for jobs (if any) to complete. */ 528 rcu_for_each_leaf_node(rnp) 529 if (rnp->exp_need_flush) 530 sync_rcu_exp_select_cpus_flush_work(rnp); 531 } 532 533 /* 534 * Wait for the expedited grace period to elapse, within time limit. 535 * If the time limit is exceeded without the grace period elapsing, 536 * return false, otherwise return true. 537 */ 538 static bool synchronize_rcu_expedited_wait_once(long tlimit) 539 { 540 int t; 541 struct rcu_node *rnp_root = rcu_get_root(); 542 543 t = swait_event_timeout_exclusive(rcu_state.expedited_wq, 544 sync_rcu_exp_done_unlocked(rnp_root), 545 tlimit); 546 // Workqueues should not be signaled. 547 if (t > 0 || sync_rcu_exp_done_unlocked(rnp_root)) 548 return true; 549 WARN_ON(t < 0); /* workqueues should not be signaled. */ 550 return false; 551 } 552 553 /* 554 * Print out an expedited RCU CPU stall warning message. 555 */ 556 static void synchronize_rcu_expedited_stall(unsigned long jiffies_start, unsigned long j) 557 { 558 int cpu; 559 unsigned long mask; 560 int ndetected; 561 struct rcu_node *rnp; 562 struct rcu_node *rnp_root = rcu_get_root(); 563 564 if (READ_ONCE(csd_lock_suppress_rcu_stall) && csd_lock_is_stuck()) { 565 pr_err("INFO: %s detected expedited stalls, but suppressed full report due to a stuck CSD-lock.\n", rcu_state.name); 566 return; 567 } 568 pr_err("INFO: %s detected expedited stalls on CPUs/tasks: {", rcu_state.name); 569 ndetected = 0; 570 rcu_for_each_leaf_node(rnp) { 571 ndetected += rcu_print_task_exp_stall(rnp); 572 for_each_leaf_node_possible_cpu(rnp, cpu) { 573 struct rcu_data *rdp; 574 575 mask = leaf_node_cpu_bit(rnp, cpu); 576 if (!(READ_ONCE(rnp->expmask) & mask)) 577 continue; 578 ndetected++; 579 rdp = per_cpu_ptr(&rcu_data, cpu); 580 pr_cont(" %d-%c%c%c%c", cpu, 581 "O."[!!cpu_online(cpu)], 582 "o."[!!(rdp->grpmask & rnp->expmaskinit)], 583 "N."[!!(rdp->grpmask & rnp->expmaskinitnext)], 584 "D."[!!data_race(rdp->cpu_no_qs.b.exp)]); 585 } 586 } 587 pr_cont(" } %lu jiffies s: %lu root: %#lx/%c\n", 588 j - jiffies_start, rcu_state.expedited_sequence, data_race(rnp_root->expmask), 589 ".T"[!!data_race(rnp_root->exp_tasks)]); 590 if (ndetected) { 591 pr_err("blocking rcu_node structures (internal RCU debug):"); 592 rcu_for_each_node_breadth_first(rnp) { 593 if (rnp == rnp_root) 594 continue; /* printed unconditionally */ 595 if (sync_rcu_exp_done_unlocked(rnp)) 596 continue; 597 pr_cont(" l=%u:%d-%d:%#lx/%c", 598 rnp->level, rnp->grplo, rnp->grphi, data_race(rnp->expmask), 599 ".T"[!!data_race(rnp->exp_tasks)]); 600 } 601 pr_cont("\n"); 602 } 603 rcu_for_each_leaf_node(rnp) { 604 for_each_leaf_node_possible_cpu(rnp, cpu) { 605 mask = leaf_node_cpu_bit(rnp, cpu); 606 if (!(READ_ONCE(rnp->expmask) & mask)) 607 continue; 608 dump_cpu_task(cpu); 609 } 610 rcu_exp_print_detail_task_stall_rnp(rnp); 611 } 612 } 613 614 /* 615 * Wait for the expedited grace period to elapse, issuing any needed 616 * RCU CPU stall warnings along the way. 617 */ 618 static void synchronize_rcu_expedited_wait(void) 619 { 620 int cpu; 621 unsigned long j; 622 unsigned long jiffies_stall; 623 unsigned long jiffies_start; 624 unsigned long mask; 625 struct rcu_data *rdp; 626 struct rcu_node *rnp; 627 unsigned long flags; 628 629 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("startwait")); 630 jiffies_stall = rcu_exp_jiffies_till_stall_check(); 631 jiffies_start = jiffies; 632 if (tick_nohz_full_enabled() && rcu_inkernel_boot_has_ended()) { 633 if (synchronize_rcu_expedited_wait_once(1)) 634 return; 635 rcu_for_each_leaf_node(rnp) { 636 raw_spin_lock_irqsave_rcu_node(rnp, flags); 637 mask = READ_ONCE(rnp->expmask); 638 for_each_leaf_node_cpu_mask(rnp, cpu, mask) { 639 rdp = per_cpu_ptr(&rcu_data, cpu); 640 if (rdp->rcu_forced_tick_exp) 641 continue; 642 rdp->rcu_forced_tick_exp = true; 643 if (cpu_online(cpu)) 644 tick_dep_set_cpu(cpu, TICK_DEP_BIT_RCU_EXP); 645 } 646 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 647 } 648 j = READ_ONCE(jiffies_till_first_fqs); 649 if (synchronize_rcu_expedited_wait_once(j + HZ)) 650 return; 651 } 652 653 for (;;) { 654 unsigned long j; 655 656 if (synchronize_rcu_expedited_wait_once(jiffies_stall)) 657 return; 658 if (rcu_stall_is_suppressed()) 659 continue; 660 661 nbcon_cpu_emergency_enter(); 662 663 j = jiffies; 664 rcu_stall_notifier_call_chain(RCU_STALL_NOTIFY_EXP, (void *)(j - jiffies_start)); 665 trace_rcu_stall_warning(rcu_state.name, TPS("ExpeditedStall")); 666 synchronize_rcu_expedited_stall(jiffies_start, j); 667 jiffies_stall = 3 * rcu_exp_jiffies_till_stall_check() + 3; 668 669 nbcon_cpu_emergency_exit(); 670 671 panic_on_rcu_stall(); 672 } 673 } 674 675 /* 676 * Wait for the current expedited grace period to complete, and then 677 * wake up everyone who piggybacked on the just-completed expedited 678 * grace period. Also update all the ->exp_seq_rq counters as needed 679 * in order to avoid counter-wrap problems. 680 */ 681 static void rcu_exp_wait_wake(unsigned long s) 682 { 683 struct rcu_node *rnp; 684 685 synchronize_rcu_expedited_wait(); 686 687 // Switch over to wakeup mode, allowing the next GP to proceed. 688 // End the previous grace period only after acquiring the mutex 689 // to ensure that only one GP runs concurrently with wakeups. 690 mutex_lock(&rcu_state.exp_wake_mutex); 691 rcu_exp_gp_seq_end(); 692 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("end")); 693 694 rcu_for_each_node_breadth_first(rnp) { 695 if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s)) { 696 spin_lock(&rnp->exp_lock); 697 /* Recheck, avoid hang in case someone just arrived. */ 698 if (ULONG_CMP_LT(rnp->exp_seq_rq, s)) 699 WRITE_ONCE(rnp->exp_seq_rq, s); 700 spin_unlock(&rnp->exp_lock); 701 } 702 smp_mb(); /* All above changes before wakeup. */ 703 wake_up_all(&rnp->exp_wq[rcu_seq_ctr(s) & 0x3]); 704 } 705 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("endwake")); 706 mutex_unlock(&rcu_state.exp_wake_mutex); 707 } 708 709 /* 710 * Common code to drive an expedited grace period forward, used by 711 * workqueues and mid-boot-time tasks. 712 */ 713 static void rcu_exp_sel_wait_wake(unsigned long s) 714 { 715 /* Initialize the rcu_node tree in preparation for the wait. */ 716 sync_rcu_exp_select_cpus(); 717 718 /* Wait and clean up, including waking everyone. */ 719 rcu_exp_wait_wake(s); 720 } 721 722 /* Request an expedited quiescent state. */ 723 static void rcu_exp_need_qs(void) 724 { 725 lockdep_assert_irqs_disabled(); 726 ASSERT_EXCLUSIVE_WRITER_SCOPED(*this_cpu_ptr(&rcu_data.cpu_no_qs.b.exp)); 727 __this_cpu_write(rcu_data.cpu_no_qs.b.exp, true); 728 /* Store .exp before .rcu_urgent_qs. */ 729 smp_store_release(this_cpu_ptr(&rcu_data.rcu_urgent_qs), true); 730 set_tsk_need_resched(current); 731 set_preempt_need_resched(); 732 } 733 734 #ifdef CONFIG_PREEMPT_RCU 735 736 /* 737 * Remote handler for smp_call_function_single(). If there is an 738 * RCU read-side critical section in effect, request that the 739 * next rcu_read_unlock() record the quiescent state up the 740 * ->expmask fields in the rcu_node tree. Otherwise, immediately 741 * report the quiescent state. 742 */ 743 static void rcu_exp_handler(void *unused) 744 { 745 int depth = rcu_preempt_depth(); 746 unsigned long flags; 747 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 748 struct rcu_node *rnp = rdp->mynode; 749 struct task_struct *t = current; 750 751 /* 752 * First, is there no need for a quiescent state from this CPU, 753 * or is this CPU already looking for a quiescent state for the 754 * current grace period? If either is the case, just leave. 755 * However, this should not happen due to the preemptible 756 * sync_sched_exp_online_cleanup() implementation being a no-op, 757 * so warn if this does happen. 758 */ 759 ASSERT_EXCLUSIVE_WRITER_SCOPED(rdp->cpu_no_qs.b.exp); 760 if (WARN_ON_ONCE(!(READ_ONCE(rnp->expmask) & rdp->grpmask) || 761 READ_ONCE(rdp->cpu_no_qs.b.exp))) 762 return; 763 764 /* 765 * Second, the common case of not being in an RCU read-side 766 * critical section. If also enabled or idle, immediately 767 * report the quiescent state, otherwise defer. 768 */ 769 if (!depth) { 770 if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) || 771 rcu_is_cpu_rrupt_from_idle()) 772 rcu_report_exp_rdp(rdp); 773 else 774 rcu_exp_need_qs(); 775 return; 776 } 777 778 /* 779 * Third, the less-common case of being in an RCU read-side 780 * critical section. In this case we can count on a future 781 * rcu_read_unlock(). However, this rcu_read_unlock() might 782 * execute on some other CPU, but in that case there will be 783 * a future context switch. Either way, if the expedited 784 * grace period is still waiting on this CPU, set ->deferred_qs 785 * so that the eventual quiescent state will be reported. 786 * Note that there is a large group of race conditions that 787 * can have caused this quiescent state to already have been 788 * reported, so we really do need to check ->expmask. 789 */ 790 if (depth > 0) { 791 raw_spin_lock_irqsave_rcu_node(rnp, flags); 792 if (rnp->expmask & rdp->grpmask) { 793 WRITE_ONCE(rdp->cpu_no_qs.b.exp, true); 794 t->rcu_read_unlock_special.b.exp_hint = true; 795 } 796 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 797 return; 798 } 799 800 // Fourth and finally, negative nesting depth should not happen. 801 WARN_ON_ONCE(1); 802 } 803 804 /* PREEMPTION=y, so no PREEMPTION=n expedited grace period to clean up after. */ 805 static void sync_sched_exp_online_cleanup(int cpu) 806 { 807 } 808 809 /* 810 * Scan the current list of tasks blocked within RCU read-side critical 811 * sections, printing out the tid of each that is blocking the current 812 * expedited grace period. 813 */ 814 static int rcu_print_task_exp_stall(struct rcu_node *rnp) 815 { 816 unsigned long flags; 817 int ndetected = 0; 818 struct task_struct *t; 819 820 raw_spin_lock_irqsave_rcu_node(rnp, flags); 821 if (!rnp->exp_tasks) { 822 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 823 return 0; 824 } 825 t = list_entry(rnp->exp_tasks->prev, 826 struct task_struct, rcu_node_entry); 827 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) { 828 pr_cont(" P%d", t->pid); 829 ndetected++; 830 } 831 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 832 return ndetected; 833 } 834 835 /* 836 * Scan the current list of tasks blocked within RCU read-side critical 837 * sections, dumping the stack of each that is blocking the current 838 * expedited grace period. 839 */ 840 static void rcu_exp_print_detail_task_stall_rnp(struct rcu_node *rnp) 841 { 842 unsigned long flags; 843 struct task_struct *t; 844 845 if (!rcu_exp_stall_task_details) 846 return; 847 raw_spin_lock_irqsave_rcu_node(rnp, flags); 848 if (!READ_ONCE(rnp->exp_tasks)) { 849 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 850 return; 851 } 852 t = list_entry(rnp->exp_tasks->prev, 853 struct task_struct, rcu_node_entry); 854 list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry) { 855 /* 856 * We could be printing a lot while holding a spinlock. 857 * Avoid triggering hard lockup. 858 */ 859 touch_nmi_watchdog(); 860 sched_show_task(t); 861 } 862 raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 863 } 864 865 #else /* #ifdef CONFIG_PREEMPT_RCU */ 866 867 /* Invoked on each online non-idle CPU for expedited quiescent state. */ 868 static void rcu_exp_handler(void *unused) 869 { 870 struct rcu_data *rdp = this_cpu_ptr(&rcu_data); 871 struct rcu_node *rnp = rdp->mynode; 872 bool preempt_bh_enabled = !(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)); 873 874 ASSERT_EXCLUSIVE_WRITER_SCOPED(rdp->cpu_no_qs.b.exp); 875 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) || 876 __this_cpu_read(rcu_data.cpu_no_qs.b.exp)) 877 return; 878 if (rcu_is_cpu_rrupt_from_idle() || 879 (IS_ENABLED(CONFIG_PREEMPT_COUNT) && preempt_bh_enabled)) { 880 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data)); 881 return; 882 } 883 rcu_exp_need_qs(); 884 } 885 886 /* Send IPI for expedited cleanup if needed at end of CPU-hotplug operation. */ 887 static void sync_sched_exp_online_cleanup(int cpu) 888 { 889 unsigned long flags; 890 int my_cpu; 891 struct rcu_data *rdp; 892 int ret; 893 struct rcu_node *rnp; 894 895 rdp = per_cpu_ptr(&rcu_data, cpu); 896 rnp = rdp->mynode; 897 my_cpu = get_cpu(); 898 /* Quiescent state either not needed or already requested, leave. */ 899 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) || 900 READ_ONCE(rdp->cpu_no_qs.b.exp)) { 901 put_cpu(); 902 return; 903 } 904 /* Quiescent state needed on current CPU, so set it up locally. */ 905 if (my_cpu == cpu) { 906 local_irq_save(flags); 907 rcu_exp_need_qs(); 908 local_irq_restore(flags); 909 put_cpu(); 910 return; 911 } 912 /* Quiescent state needed on some other CPU, send IPI. */ 913 ret = smp_call_function_single(cpu, rcu_exp_handler, NULL, 0); 914 put_cpu(); 915 WARN_ON_ONCE(ret); 916 } 917 918 /* 919 * Because preemptible RCU does not exist, we never have to check for 920 * tasks blocked within RCU read-side critical sections that are 921 * blocking the current expedited grace period. 922 */ 923 static int rcu_print_task_exp_stall(struct rcu_node *rnp) 924 { 925 return 0; 926 } 927 928 /* 929 * Because preemptible RCU does not exist, we never have to print out 930 * tasks blocked within RCU read-side critical sections that are blocking 931 * the current expedited grace period. 932 */ 933 static void rcu_exp_print_detail_task_stall_rnp(struct rcu_node *rnp) 934 { 935 } 936 937 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */ 938 939 /** 940 * synchronize_rcu_expedited - Brute-force RCU grace period 941 * 942 * Wait for an RCU grace period, but expedite it. The basic idea is to 943 * IPI all non-idle non-nohz online CPUs. The IPI handler checks whether 944 * the CPU is in an RCU critical section, and if so, it sets a flag that 945 * causes the outermost rcu_read_unlock() to report the quiescent state 946 * for RCU-preempt or asks the scheduler for help for RCU-sched. On the 947 * other hand, if the CPU is not in an RCU read-side critical section, 948 * the IPI handler reports the quiescent state immediately. 949 * 950 * Although this is a great improvement over previous expedited 951 * implementations, it is still unfriendly to real-time workloads, so is 952 * thus not recommended for any sort of common-case code. In fact, if 953 * you are using synchronize_rcu_expedited() in a loop, please restructure 954 * your code to batch your updates, and then use a single synchronize_rcu() 955 * instead. 956 * 957 * This has the same semantics as (but is more brutal than) synchronize_rcu(). 958 */ 959 void synchronize_rcu_expedited(void) 960 { 961 unsigned long flags; 962 struct rcu_exp_work rew; 963 struct rcu_node *rnp; 964 unsigned long s; 965 966 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) || 967 lock_is_held(&rcu_lock_map) || 968 lock_is_held(&rcu_sched_lock_map), 969 "Illegal synchronize_rcu_expedited() in RCU read-side critical section"); 970 971 /* Is the state is such that the call is a grace period? */ 972 if (rcu_blocking_is_gp()) { 973 // Note well that this code runs with !PREEMPT && !SMP. 974 // In addition, all code that advances grace periods runs 975 // at process level. Therefore, this expedited GP overlaps 976 // with other expedited GPs only by being fully nested within 977 // them, which allows reuse of ->gp_seq_polled_exp_snap. 978 rcu_poll_gp_seq_start_unlocked(&rcu_state.gp_seq_polled_exp_snap); 979 rcu_poll_gp_seq_end_unlocked(&rcu_state.gp_seq_polled_exp_snap); 980 981 local_irq_save(flags); 982 WARN_ON_ONCE(num_online_cpus() > 1); 983 rcu_state.expedited_sequence += (1 << RCU_SEQ_CTR_SHIFT); 984 local_irq_restore(flags); 985 return; // Context allows vacuous grace periods. 986 } 987 988 /* If expedited grace periods are prohibited, fall back to normal. */ 989 if (rcu_gp_is_normal()) { 990 synchronize_rcu_normal(); 991 return; 992 } 993 994 /* Take a snapshot of the sequence number. */ 995 s = rcu_exp_gp_seq_snap(); 996 if (exp_funnel_lock(s)) 997 return; /* Someone else did our work for us. */ 998 999 /* Ensure that load happens before action based on it. */ 1000 if (unlikely((rcu_scheduler_active == RCU_SCHEDULER_INIT) || !rcu_exp_worker_started())) { 1001 /* Direct call during scheduler init and early_initcalls(). */ 1002 rcu_exp_sel_wait_wake(s); 1003 } else { 1004 /* Marshall arguments & schedule the expedited grace period. */ 1005 rew.rew_s = s; 1006 synchronize_rcu_expedited_queue_work(&rew); 1007 } 1008 1009 /* Wait for expedited grace period to complete. */ 1010 rnp = rcu_get_root(); 1011 wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3], 1012 sync_exp_work_done(s)); 1013 1014 /* Let the next expedited grace period start. */ 1015 mutex_unlock(&rcu_state.exp_mutex); 1016 } 1017 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited); 1018 1019 /* 1020 * Ensure that start_poll_synchronize_rcu_expedited() has the expedited 1021 * RCU grace periods that it needs. 1022 */ 1023 static void sync_rcu_do_polled_gp(struct work_struct *wp) 1024 { 1025 unsigned long flags; 1026 int i = 0; 1027 struct rcu_node *rnp = container_of(wp, struct rcu_node, exp_poll_wq); 1028 unsigned long s; 1029 1030 raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags); 1031 s = rnp->exp_seq_poll_rq; 1032 rnp->exp_seq_poll_rq = RCU_GET_STATE_COMPLETED; 1033 raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags); 1034 if (s == RCU_GET_STATE_COMPLETED) 1035 return; 1036 while (!poll_state_synchronize_rcu(s)) { 1037 synchronize_rcu_expedited(); 1038 if (i == 10 || i == 20) 1039 pr_info("%s: i = %d s = %lx gp_seq_polled = %lx\n", __func__, i, s, READ_ONCE(rcu_state.gp_seq_polled)); 1040 i++; 1041 } 1042 raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags); 1043 s = rnp->exp_seq_poll_rq; 1044 if (poll_state_synchronize_rcu(s)) 1045 rnp->exp_seq_poll_rq = RCU_GET_STATE_COMPLETED; 1046 raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags); 1047 } 1048 1049 /** 1050 * start_poll_synchronize_rcu_expedited - Snapshot current RCU state and start expedited grace period 1051 * 1052 * Returns a cookie to pass to a call to cond_synchronize_rcu(), 1053 * cond_synchronize_rcu_expedited(), or poll_state_synchronize_rcu(), 1054 * allowing them to determine whether or not any sort of grace period has 1055 * elapsed in the meantime. If the needed expedited grace period is not 1056 * already slated to start, initiates that grace period. 1057 */ 1058 unsigned long start_poll_synchronize_rcu_expedited(void) 1059 { 1060 unsigned long flags; 1061 struct rcu_data *rdp; 1062 struct rcu_node *rnp; 1063 unsigned long s; 1064 1065 s = get_state_synchronize_rcu(); 1066 rdp = per_cpu_ptr(&rcu_data, raw_smp_processor_id()); 1067 rnp = rdp->mynode; 1068 if (rcu_init_invoked()) 1069 raw_spin_lock_irqsave(&rnp->exp_poll_lock, flags); 1070 if (!poll_state_synchronize_rcu(s)) { 1071 if (rcu_init_invoked()) { 1072 rnp->exp_seq_poll_rq = s; 1073 queue_work(rcu_gp_wq, &rnp->exp_poll_wq); 1074 } 1075 } 1076 if (rcu_init_invoked()) 1077 raw_spin_unlock_irqrestore(&rnp->exp_poll_lock, flags); 1078 1079 return s; 1080 } 1081 EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu_expedited); 1082 1083 /** 1084 * start_poll_synchronize_rcu_expedited_full - Take a full snapshot and start expedited grace period 1085 * @rgosp: Place to put snapshot of grace-period state 1086 * 1087 * Places the normal and expedited grace-period states in rgosp. This 1088 * state value can be passed to a later call to cond_synchronize_rcu_full() 1089 * or poll_state_synchronize_rcu_full() to determine whether or not a 1090 * grace period (whether normal or expedited) has elapsed in the meantime. 1091 * If the needed expedited grace period is not already slated to start, 1092 * initiates that grace period. 1093 */ 1094 void start_poll_synchronize_rcu_expedited_full(struct rcu_gp_oldstate *rgosp) 1095 { 1096 get_state_synchronize_rcu_full(rgosp); 1097 (void)start_poll_synchronize_rcu_expedited(); 1098 } 1099 EXPORT_SYMBOL_GPL(start_poll_synchronize_rcu_expedited_full); 1100 1101 /** 1102 * cond_synchronize_rcu_expedited - Conditionally wait for an expedited RCU grace period 1103 * 1104 * @oldstate: value from get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or start_poll_synchronize_rcu_expedited() 1105 * 1106 * If any type of full RCU grace period has elapsed since the earlier 1107 * call to get_state_synchronize_rcu(), start_poll_synchronize_rcu(), 1108 * or start_poll_synchronize_rcu_expedited(), just return. Otherwise, 1109 * invoke synchronize_rcu_expedited() to wait for a full grace period. 1110 * 1111 * Yes, this function does not take counter wrap into account. 1112 * But counter wrap is harmless. If the counter wraps, we have waited for 1113 * more than 2 billion grace periods (and way more on a 64-bit system!), 1114 * so waiting for a couple of additional grace periods should be just fine. 1115 * 1116 * This function provides the same memory-ordering guarantees that 1117 * would be provided by a synchronize_rcu() that was invoked at the call 1118 * to the function that provided @oldstate and that returned at the end 1119 * of this function. 1120 */ 1121 void cond_synchronize_rcu_expedited(unsigned long oldstate) 1122 { 1123 if (!poll_state_synchronize_rcu(oldstate)) 1124 synchronize_rcu_expedited(); 1125 } 1126 EXPORT_SYMBOL_GPL(cond_synchronize_rcu_expedited); 1127 1128 /** 1129 * cond_synchronize_rcu_expedited_full - Conditionally wait for an expedited RCU grace period 1130 * @rgosp: value from get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(), or start_poll_synchronize_rcu_expedited_full() 1131 * 1132 * If a full RCU grace period has elapsed since the call to 1133 * get_state_synchronize_rcu_full(), start_poll_synchronize_rcu_full(), 1134 * or start_poll_synchronize_rcu_expedited_full() from which @rgosp was 1135 * obtained, just return. Otherwise, invoke synchronize_rcu_expedited() 1136 * to wait for a full grace period. 1137 * 1138 * Yes, this function does not take counter wrap into account. 1139 * But counter wrap is harmless. If the counter wraps, we have waited for 1140 * more than 2 billion grace periods (and way more on a 64-bit system!), 1141 * so waiting for a couple of additional grace periods should be just fine. 1142 * 1143 * This function provides the same memory-ordering guarantees that 1144 * would be provided by a synchronize_rcu() that was invoked at the call 1145 * to the function that provided @rgosp and that returned at the end of 1146 * this function. 1147 */ 1148 void cond_synchronize_rcu_expedited_full(struct rcu_gp_oldstate *rgosp) 1149 { 1150 if (!poll_state_synchronize_rcu_full(rgosp)) 1151 synchronize_rcu_expedited(); 1152 } 1153 EXPORT_SYMBOL_GPL(cond_synchronize_rcu_expedited_full); 1154