0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifdef CONFIG_RCU_NOCB_CPU
0017 static cpumask_var_t rcu_nocb_mask;
0018 static bool __read_mostly rcu_nocb_poll;
0019 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
0020 {
0021 return lockdep_is_held(&rdp->nocb_lock);
0022 }
0023
0024 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
0025 {
0026
0027 if (!rdp->nocb_cb_kthread || !rdp->nocb_gp_kthread)
0028 return true;
0029
0030 if (current == rdp->nocb_cb_kthread || current == rdp->nocb_gp_kthread)
0031 if (in_task())
0032 return true;
0033 return false;
0034 }
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 static int __init rcu_nocb_setup(char *str)
0064 {
0065 alloc_bootmem_cpumask_var(&rcu_nocb_mask);
0066 if (*str == '=') {
0067 if (cpulist_parse(++str, rcu_nocb_mask)) {
0068 pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
0069 cpumask_setall(rcu_nocb_mask);
0070 }
0071 }
0072 rcu_state.nocb_is_setup = true;
0073 return 1;
0074 }
0075 __setup("rcu_nocbs", rcu_nocb_setup);
0076
0077 static int __init parse_rcu_nocb_poll(char *arg)
0078 {
0079 rcu_nocb_poll = true;
0080 return 0;
0081 }
0082 early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
0083
0084
0085
0086
0087
0088
0089 static int nocb_nobypass_lim_per_jiffy = 16 * 1000 / HZ;
0090 module_param(nocb_nobypass_lim_per_jiffy, int, 0);
0091
0092
0093
0094
0095
0096
0097 static void rcu_nocb_bypass_lock(struct rcu_data *rdp)
0098 __acquires(&rdp->nocb_bypass_lock)
0099 {
0100 lockdep_assert_irqs_disabled();
0101 if (raw_spin_trylock(&rdp->nocb_bypass_lock))
0102 return;
0103 atomic_inc(&rdp->nocb_lock_contended);
0104 WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
0105 smp_mb__after_atomic();
0106 raw_spin_lock(&rdp->nocb_bypass_lock);
0107 smp_mb__before_atomic();
0108 atomic_dec(&rdp->nocb_lock_contended);
0109 }
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 static void rcu_nocb_wait_contended(struct rcu_data *rdp)
0122 {
0123 WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
0124 while (WARN_ON_ONCE(atomic_read(&rdp->nocb_lock_contended)))
0125 cpu_relax();
0126 }
0127
0128
0129
0130
0131
0132 static bool rcu_nocb_bypass_trylock(struct rcu_data *rdp)
0133 {
0134 lockdep_assert_irqs_disabled();
0135 return raw_spin_trylock(&rdp->nocb_bypass_lock);
0136 }
0137
0138
0139
0140
0141 static void rcu_nocb_bypass_unlock(struct rcu_data *rdp)
0142 __releases(&rdp->nocb_bypass_lock)
0143 {
0144 lockdep_assert_irqs_disabled();
0145 raw_spin_unlock(&rdp->nocb_bypass_lock);
0146 }
0147
0148
0149
0150
0151
0152 static void rcu_nocb_lock(struct rcu_data *rdp)
0153 {
0154 lockdep_assert_irqs_disabled();
0155 if (!rcu_rdp_is_offloaded(rdp))
0156 return;
0157 raw_spin_lock(&rdp->nocb_lock);
0158 }
0159
0160
0161
0162
0163
0164 static void rcu_nocb_unlock(struct rcu_data *rdp)
0165 {
0166 if (rcu_rdp_is_offloaded(rdp)) {
0167 lockdep_assert_irqs_disabled();
0168 raw_spin_unlock(&rdp->nocb_lock);
0169 }
0170 }
0171
0172
0173
0174
0175
0176 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
0177 unsigned long flags)
0178 {
0179 if (rcu_rdp_is_offloaded(rdp)) {
0180 lockdep_assert_irqs_disabled();
0181 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
0182 } else {
0183 local_irq_restore(flags);
0184 }
0185 }
0186
0187
0188 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
0189 {
0190 lockdep_assert_irqs_disabled();
0191 if (rcu_rdp_is_offloaded(rdp))
0192 lockdep_assert_held(&rdp->nocb_lock);
0193 }
0194
0195
0196
0197
0198
0199 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
0200 {
0201 swake_up_all(sq);
0202 }
0203
0204 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
0205 {
0206 return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
0207 }
0208
0209 static void rcu_init_one_nocb(struct rcu_node *rnp)
0210 {
0211 init_swait_queue_head(&rnp->nocb_gp_wq[0]);
0212 init_swait_queue_head(&rnp->nocb_gp_wq[1]);
0213 }
0214
0215 static bool __wake_nocb_gp(struct rcu_data *rdp_gp,
0216 struct rcu_data *rdp,
0217 bool force, unsigned long flags)
0218 __releases(rdp_gp->nocb_gp_lock)
0219 {
0220 bool needwake = false;
0221
0222 if (!READ_ONCE(rdp_gp->nocb_gp_kthread)) {
0223 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
0224 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0225 TPS("AlreadyAwake"));
0226 return false;
0227 }
0228
0229 if (rdp_gp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
0230 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
0231 del_timer(&rdp_gp->nocb_timer);
0232 }
0233
0234 if (force || READ_ONCE(rdp_gp->nocb_gp_sleep)) {
0235 WRITE_ONCE(rdp_gp->nocb_gp_sleep, false);
0236 needwake = true;
0237 }
0238 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
0239 if (needwake) {
0240 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DoWake"));
0241 wake_up_process(rdp_gp->nocb_gp_kthread);
0242 }
0243
0244 return needwake;
0245 }
0246
0247
0248
0249
0250 static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
0251 {
0252 unsigned long flags;
0253 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
0254
0255 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
0256 return __wake_nocb_gp(rdp_gp, rdp, force, flags);
0257 }
0258
0259
0260
0261
0262
0263 static void wake_nocb_gp_defer(struct rcu_data *rdp, int waketype,
0264 const char *reason)
0265 {
0266 unsigned long flags;
0267 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
0268
0269 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
0270
0271
0272
0273
0274
0275 if (waketype == RCU_NOCB_WAKE_BYPASS) {
0276 mod_timer(&rdp_gp->nocb_timer, jiffies + 2);
0277 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
0278 } else {
0279 if (rdp_gp->nocb_defer_wakeup < RCU_NOCB_WAKE)
0280 mod_timer(&rdp_gp->nocb_timer, jiffies + 1);
0281 if (rdp_gp->nocb_defer_wakeup < waketype)
0282 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
0283 }
0284
0285 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
0286
0287 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
0288 }
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 static bool rcu_nocb_do_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
0299 unsigned long j)
0300 {
0301 struct rcu_cblist rcl;
0302
0303 WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp));
0304 rcu_lockdep_assert_cblist_protected(rdp);
0305 lockdep_assert_held(&rdp->nocb_bypass_lock);
0306 if (rhp && !rcu_cblist_n_cbs(&rdp->nocb_bypass)) {
0307 raw_spin_unlock(&rdp->nocb_bypass_lock);
0308 return false;
0309 }
0310
0311 if (rhp)
0312 rcu_segcblist_inc_len(&rdp->cblist);
0313 rcu_cblist_flush_enqueue(&rcl, &rdp->nocb_bypass, rhp);
0314 rcu_segcblist_insert_pend_cbs(&rdp->cblist, &rcl);
0315 WRITE_ONCE(rdp->nocb_bypass_first, j);
0316 rcu_nocb_bypass_unlock(rdp);
0317 return true;
0318 }
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
0329 unsigned long j)
0330 {
0331 if (!rcu_rdp_is_offloaded(rdp))
0332 return true;
0333 rcu_lockdep_assert_cblist_protected(rdp);
0334 rcu_nocb_bypass_lock(rdp);
0335 return rcu_nocb_do_flush_bypass(rdp, rhp, j);
0336 }
0337
0338
0339
0340
0341
0342 static void rcu_nocb_try_flush_bypass(struct rcu_data *rdp, unsigned long j)
0343 {
0344 rcu_lockdep_assert_cblist_protected(rdp);
0345 if (!rcu_rdp_is_offloaded(rdp) ||
0346 !rcu_nocb_bypass_trylock(rdp))
0347 return;
0348 WARN_ON_ONCE(!rcu_nocb_do_flush_bypass(rdp, NULL, j));
0349 }
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
0370 bool *was_alldone, unsigned long flags)
0371 {
0372 unsigned long c;
0373 unsigned long cur_gp_seq;
0374 unsigned long j = jiffies;
0375 long ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
0376
0377 lockdep_assert_irqs_disabled();
0378
0379
0380
0381 if (!rcu_rdp_is_offloaded(rdp)) {
0382 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
0383 return false;
0384 }
0385
0386
0387
0388 if (!rcu_segcblist_completely_offloaded(&rdp->cblist)) {
0389 rcu_nocb_lock(rdp);
0390 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
0391 return false;
0392 }
0393
0394
0395 if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING) {
0396 rcu_nocb_lock(rdp);
0397 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
0398 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
0399 return false;
0400 }
0401
0402
0403
0404 if (j == rdp->nocb_nobypass_last) {
0405 c = rdp->nocb_nobypass_count + 1;
0406 } else {
0407 WRITE_ONCE(rdp->nocb_nobypass_last, j);
0408 c = rdp->nocb_nobypass_count - nocb_nobypass_lim_per_jiffy;
0409 if (ULONG_CMP_LT(rdp->nocb_nobypass_count,
0410 nocb_nobypass_lim_per_jiffy))
0411 c = 0;
0412 else if (c > nocb_nobypass_lim_per_jiffy)
0413 c = nocb_nobypass_lim_per_jiffy;
0414 }
0415 WRITE_ONCE(rdp->nocb_nobypass_count, c);
0416
0417
0418
0419
0420 if (rdp->nocb_nobypass_count < nocb_nobypass_lim_per_jiffy) {
0421 rcu_nocb_lock(rdp);
0422 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
0423 if (*was_alldone)
0424 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0425 TPS("FirstQ"));
0426 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, j));
0427 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
0428 return false;
0429 }
0430
0431
0432
0433 if ((ncbs && j != READ_ONCE(rdp->nocb_bypass_first)) ||
0434 ncbs >= qhimark) {
0435 rcu_nocb_lock(rdp);
0436 if (!rcu_nocb_flush_bypass(rdp, rhp, j)) {
0437 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
0438 if (*was_alldone)
0439 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0440 TPS("FirstQ"));
0441 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
0442 return false;
0443 }
0444 if (j != rdp->nocb_gp_adv_time &&
0445 rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
0446 rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
0447 rcu_advance_cbs_nowake(rdp->mynode, rdp);
0448 rdp->nocb_gp_adv_time = j;
0449 }
0450 rcu_nocb_unlock_irqrestore(rdp, flags);
0451 return true;
0452 }
0453
0454
0455 rcu_nocb_wait_contended(rdp);
0456 rcu_nocb_bypass_lock(rdp);
0457 ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
0458 rcu_segcblist_inc_len(&rdp->cblist);
0459 rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
0460 if (!ncbs) {
0461 WRITE_ONCE(rdp->nocb_bypass_first, j);
0462 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FirstBQ"));
0463 }
0464 rcu_nocb_bypass_unlock(rdp);
0465 smp_mb();
0466 if (ncbs) {
0467 local_irq_restore(flags);
0468 } else {
0469
0470 rcu_nocb_lock(rdp);
0471 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) {
0472 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0473 TPS("FirstBQwake"));
0474 __call_rcu_nocb_wake(rdp, true, flags);
0475 } else {
0476 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0477 TPS("FirstBQnoWake"));
0478 rcu_nocb_unlock_irqrestore(rdp, flags);
0479 }
0480 }
0481 return true;
0482 }
0483
0484
0485
0486
0487
0488
0489
0490 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,
0491 unsigned long flags)
0492 __releases(rdp->nocb_lock)
0493 {
0494 unsigned long cur_gp_seq;
0495 unsigned long j;
0496 long len;
0497 struct task_struct *t;
0498
0499
0500 t = READ_ONCE(rdp->nocb_gp_kthread);
0501 if (rcu_nocb_poll || !t) {
0502 rcu_nocb_unlock_irqrestore(rdp, flags);
0503 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0504 TPS("WakeNotPoll"));
0505 return;
0506 }
0507
0508 len = rcu_segcblist_n_cbs(&rdp->cblist);
0509 if (was_alldone) {
0510 rdp->qlen_last_fqs_check = len;
0511 if (!irqs_disabled_flags(flags)) {
0512
0513 rcu_nocb_unlock_irqrestore(rdp, flags);
0514 wake_nocb_gp(rdp, false);
0515 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0516 TPS("WakeEmpty"));
0517 } else {
0518 rcu_nocb_unlock_irqrestore(rdp, flags);
0519 wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE,
0520 TPS("WakeEmptyIsDeferred"));
0521 }
0522 } else if (len > rdp->qlen_last_fqs_check + qhimark) {
0523
0524 rdp->qlen_last_fqs_check = len;
0525 j = jiffies;
0526 if (j != rdp->nocb_gp_adv_time &&
0527 rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
0528 rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
0529 rcu_advance_cbs_nowake(rdp->mynode, rdp);
0530 rdp->nocb_gp_adv_time = j;
0531 }
0532 smp_mb();
0533 if ((rdp->nocb_cb_sleep ||
0534 !rcu_segcblist_ready_cbs(&rdp->cblist)) &&
0535 !timer_pending(&rdp->nocb_timer)) {
0536 rcu_nocb_unlock_irqrestore(rdp, flags);
0537 wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_FORCE,
0538 TPS("WakeOvfIsDeferred"));
0539 } else {
0540 rcu_nocb_unlock_irqrestore(rdp, flags);
0541 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
0542 }
0543 } else {
0544 rcu_nocb_unlock_irqrestore(rdp, flags);
0545 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
0546 }
0547 }
0548
0549 static int nocb_gp_toggle_rdp(struct rcu_data *rdp,
0550 bool *wake_state)
0551 {
0552 struct rcu_segcblist *cblist = &rdp->cblist;
0553 unsigned long flags;
0554 int ret;
0555
0556 rcu_nocb_lock_irqsave(rdp, flags);
0557 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED) &&
0558 !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP)) {
0559
0560
0561
0562
0563 rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_GP);
0564 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
0565 *wake_state = true;
0566 ret = 1;
0567 } else if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED) &&
0568 rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP)) {
0569
0570
0571
0572
0573 rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_GP);
0574 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
0575 *wake_state = true;
0576 ret = 0;
0577 } else {
0578 WARN_ON_ONCE(1);
0579 ret = -1;
0580 }
0581
0582 rcu_nocb_unlock_irqrestore(rdp, flags);
0583
0584 return ret;
0585 }
0586
0587 static void nocb_gp_sleep(struct rcu_data *my_rdp, int cpu)
0588 {
0589 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Sleep"));
0590 swait_event_interruptible_exclusive(my_rdp->nocb_gp_wq,
0591 !READ_ONCE(my_rdp->nocb_gp_sleep));
0592 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("EndSleep"));
0593 }
0594
0595
0596
0597
0598
0599 static void nocb_gp_wait(struct rcu_data *my_rdp)
0600 {
0601 bool bypass = false;
0602 long bypass_ncbs;
0603 int __maybe_unused cpu = my_rdp->cpu;
0604 unsigned long cur_gp_seq;
0605 unsigned long flags;
0606 bool gotcbs = false;
0607 unsigned long j = jiffies;
0608 bool needwait_gp = false;
0609 bool needwake;
0610 bool needwake_gp;
0611 struct rcu_data *rdp, *rdp_toggling = NULL;
0612 struct rcu_node *rnp;
0613 unsigned long wait_gp_seq = 0;
0614 bool wasempty = false;
0615
0616
0617
0618
0619
0620
0621 WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636 list_for_each_entry(rdp, &my_rdp->nocb_head_rdp, nocb_entry_rdp) {
0637 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Check"));
0638 rcu_nocb_lock_irqsave(rdp, flags);
0639 lockdep_assert_held(&rdp->nocb_lock);
0640 bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
0641 if (bypass_ncbs &&
0642 (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + 1) ||
0643 bypass_ncbs > 2 * qhimark)) {
0644
0645 (void)rcu_nocb_try_flush_bypass(rdp, j);
0646 bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
0647 } else if (!bypass_ncbs && rcu_segcblist_empty(&rdp->cblist)) {
0648 rcu_nocb_unlock_irqrestore(rdp, flags);
0649 continue;
0650 }
0651 if (bypass_ncbs) {
0652 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0653 TPS("Bypass"));
0654 bypass = true;
0655 }
0656 rnp = rdp->mynode;
0657
0658
0659 needwake_gp = false;
0660 if (!rcu_segcblist_restempty(&rdp->cblist,
0661 RCU_NEXT_READY_TAIL) ||
0662 (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
0663 rcu_seq_done(&rnp->gp_seq, cur_gp_seq))) {
0664 raw_spin_lock_rcu_node(rnp);
0665 needwake_gp = rcu_advance_cbs(rnp, rdp);
0666 wasempty = rcu_segcblist_restempty(&rdp->cblist,
0667 RCU_NEXT_READY_TAIL);
0668 raw_spin_unlock_rcu_node(rnp);
0669 }
0670
0671 WARN_ON_ONCE(wasempty &&
0672 !rcu_segcblist_restempty(&rdp->cblist,
0673 RCU_NEXT_READY_TAIL));
0674 if (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq)) {
0675 if (!needwait_gp ||
0676 ULONG_CMP_LT(cur_gp_seq, wait_gp_seq))
0677 wait_gp_seq = cur_gp_seq;
0678 needwait_gp = true;
0679 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
0680 TPS("NeedWaitGP"));
0681 }
0682 if (rcu_segcblist_ready_cbs(&rdp->cblist)) {
0683 needwake = rdp->nocb_cb_sleep;
0684 WRITE_ONCE(rdp->nocb_cb_sleep, false);
0685 smp_mb();
0686 } else {
0687 needwake = false;
0688 }
0689 rcu_nocb_unlock_irqrestore(rdp, flags);
0690 if (needwake) {
0691 swake_up_one(&rdp->nocb_cb_wq);
0692 gotcbs = true;
0693 }
0694 if (needwake_gp)
0695 rcu_gp_kthread_wake();
0696 }
0697
0698 my_rdp->nocb_gp_bypass = bypass;
0699 my_rdp->nocb_gp_gp = needwait_gp;
0700 my_rdp->nocb_gp_seq = needwait_gp ? wait_gp_seq : 0;
0701
0702 if (bypass && !rcu_nocb_poll) {
0703
0704
0705 wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_BYPASS,
0706 TPS("WakeBypassIsDeferred"));
0707 }
0708 if (rcu_nocb_poll) {
0709
0710 if (gotcbs)
0711 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Poll"));
0712 if (list_empty(&my_rdp->nocb_head_rdp)) {
0713 raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
0714 if (!my_rdp->nocb_toggling_rdp)
0715 WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
0716 raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
0717
0718 nocb_gp_sleep(my_rdp, cpu);
0719 } else {
0720 schedule_timeout_idle(1);
0721 }
0722 } else if (!needwait_gp) {
0723
0724 nocb_gp_sleep(my_rdp, cpu);
0725 } else {
0726 rnp = my_rdp->mynode;
0727 trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("StartWait"));
0728 swait_event_interruptible_exclusive(
0729 rnp->nocb_gp_wq[rcu_seq_ctr(wait_gp_seq) & 0x1],
0730 rcu_seq_done(&rnp->gp_seq, wait_gp_seq) ||
0731 !READ_ONCE(my_rdp->nocb_gp_sleep));
0732 trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("EndWait"));
0733 }
0734
0735 if (!rcu_nocb_poll) {
0736 raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
0737
0738 rdp_toggling = my_rdp->nocb_toggling_rdp;
0739 if (rdp_toggling)
0740 my_rdp->nocb_toggling_rdp = NULL;
0741
0742 if (my_rdp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
0743 WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
0744 del_timer(&my_rdp->nocb_timer);
0745 }
0746 WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
0747 raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
0748 } else {
0749 rdp_toggling = READ_ONCE(my_rdp->nocb_toggling_rdp);
0750 if (rdp_toggling) {
0751
0752
0753
0754
0755
0756
0757
0758 raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
0759 my_rdp->nocb_toggling_rdp = NULL;
0760 raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
0761 }
0762 }
0763
0764 if (rdp_toggling) {
0765 bool wake_state = false;
0766 int ret;
0767
0768 ret = nocb_gp_toggle_rdp(rdp_toggling, &wake_state);
0769 if (ret == 1)
0770 list_add_tail(&rdp_toggling->nocb_entry_rdp, &my_rdp->nocb_head_rdp);
0771 else if (ret == 0)
0772 list_del(&rdp_toggling->nocb_entry_rdp);
0773 if (wake_state)
0774 swake_up_one(&rdp_toggling->nocb_state_wq);
0775 }
0776
0777 my_rdp->nocb_gp_seq = -1;
0778 WARN_ON(signal_pending(current));
0779 }
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789 static int rcu_nocb_gp_kthread(void *arg)
0790 {
0791 struct rcu_data *rdp = arg;
0792
0793 for (;;) {
0794 WRITE_ONCE(rdp->nocb_gp_loops, rdp->nocb_gp_loops + 1);
0795 nocb_gp_wait(rdp);
0796 cond_resched_tasks_rcu_qs();
0797 }
0798 return 0;
0799 }
0800
0801 static inline bool nocb_cb_can_run(struct rcu_data *rdp)
0802 {
0803 u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_CB;
0804
0805 return rcu_segcblist_test_flags(&rdp->cblist, flags);
0806 }
0807
0808 static inline bool nocb_cb_wait_cond(struct rcu_data *rdp)
0809 {
0810 return nocb_cb_can_run(rdp) && !READ_ONCE(rdp->nocb_cb_sleep);
0811 }
0812
0813
0814
0815
0816
0817 static void nocb_cb_wait(struct rcu_data *rdp)
0818 {
0819 struct rcu_segcblist *cblist = &rdp->cblist;
0820 unsigned long cur_gp_seq;
0821 unsigned long flags;
0822 bool needwake_state = false;
0823 bool needwake_gp = false;
0824 bool can_sleep = true;
0825 struct rcu_node *rnp = rdp->mynode;
0826
0827 do {
0828 swait_event_interruptible_exclusive(rdp->nocb_cb_wq,
0829 nocb_cb_wait_cond(rdp));
0830
0831
0832 if (smp_load_acquire(&rdp->nocb_cb_sleep)) {
0833 WARN_ON(signal_pending(current));
0834 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
0835 }
0836 } while (!nocb_cb_can_run(rdp));
0837
0838
0839 local_irq_save(flags);
0840 rcu_momentary_dyntick_idle();
0841 local_irq_restore(flags);
0842
0843
0844
0845
0846
0847
0848 local_bh_disable();
0849 rcu_do_batch(rdp);
0850 local_bh_enable();
0851 lockdep_assert_irqs_enabled();
0852 rcu_nocb_lock_irqsave(rdp, flags);
0853 if (rcu_segcblist_nextgp(cblist, &cur_gp_seq) &&
0854 rcu_seq_done(&rnp->gp_seq, cur_gp_seq) &&
0855 raw_spin_trylock_rcu_node(rnp)) {
0856 needwake_gp = rcu_advance_cbs(rdp->mynode, rdp);
0857 raw_spin_unlock_rcu_node(rnp);
0858 }
0859
0860 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
0861 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB)) {
0862 rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_CB);
0863 if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
0864 needwake_state = true;
0865 }
0866 if (rcu_segcblist_ready_cbs(cblist))
0867 can_sleep = false;
0868 } else {
0869
0870
0871
0872
0873
0874 WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB));
0875 rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_CB);
0876 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
0877 needwake_state = true;
0878 }
0879
0880 WRITE_ONCE(rdp->nocb_cb_sleep, can_sleep);
0881
0882 if (rdp->nocb_cb_sleep)
0883 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("CBSleep"));
0884
0885 rcu_nocb_unlock_irqrestore(rdp, flags);
0886 if (needwake_gp)
0887 rcu_gp_kthread_wake();
0888
0889 if (needwake_state)
0890 swake_up_one(&rdp->nocb_state_wq);
0891 }
0892
0893
0894
0895
0896
0897 static int rcu_nocb_cb_kthread(void *arg)
0898 {
0899 struct rcu_data *rdp = arg;
0900
0901
0902
0903 for (;;) {
0904 nocb_cb_wait(rdp);
0905 cond_resched_tasks_rcu_qs();
0906 }
0907 return 0;
0908 }
0909
0910
0911 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
0912 {
0913 return READ_ONCE(rdp->nocb_defer_wakeup) >= level;
0914 }
0915
0916
0917 static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp,
0918 struct rcu_data *rdp, int level,
0919 unsigned long flags)
0920 __releases(rdp_gp->nocb_gp_lock)
0921 {
0922 int ndw;
0923 int ret;
0924
0925 if (!rcu_nocb_need_deferred_wakeup(rdp_gp, level)) {
0926 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
0927 return false;
0928 }
0929
0930 ndw = rdp_gp->nocb_defer_wakeup;
0931 ret = __wake_nocb_gp(rdp_gp, rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
0932 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
0933
0934 return ret;
0935 }
0936
0937
0938 static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
0939 {
0940 unsigned long flags;
0941 struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
0942
0943 WARN_ON_ONCE(rdp->nocb_gp_rdp != rdp);
0944 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Timer"));
0945
0946 raw_spin_lock_irqsave(&rdp->nocb_gp_lock, flags);
0947 smp_mb__after_spinlock();
0948 do_nocb_deferred_wakeup_common(rdp, rdp, RCU_NOCB_WAKE_BYPASS, flags);
0949 }
0950
0951
0952
0953
0954
0955
0956 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
0957 {
0958 unsigned long flags;
0959 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
0960
0961 if (!rdp_gp || !rcu_nocb_need_deferred_wakeup(rdp_gp, RCU_NOCB_WAKE))
0962 return false;
0963
0964 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
0965 return do_nocb_deferred_wakeup_common(rdp_gp, rdp, RCU_NOCB_WAKE, flags);
0966 }
0967
0968 void rcu_nocb_flush_deferred_wakeup(void)
0969 {
0970 do_nocb_deferred_wakeup(this_cpu_ptr(&rcu_data));
0971 }
0972 EXPORT_SYMBOL_GPL(rcu_nocb_flush_deferred_wakeup);
0973
0974 static int rdp_offload_toggle(struct rcu_data *rdp,
0975 bool offload, unsigned long flags)
0976 __releases(rdp->nocb_lock)
0977 {
0978 struct rcu_segcblist *cblist = &rdp->cblist;
0979 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
0980 bool wake_gp = false;
0981
0982 rcu_segcblist_offload(cblist, offload);
0983
0984 if (rdp->nocb_cb_sleep)
0985 rdp->nocb_cb_sleep = false;
0986 rcu_nocb_unlock_irqrestore(rdp, flags);
0987
0988
0989
0990
0991
0992 swake_up_one(&rdp->nocb_cb_wq);
0993
0994 raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
0995
0996 WRITE_ONCE(rdp_gp->nocb_toggling_rdp, rdp);
0997 if (rdp_gp->nocb_gp_sleep) {
0998 rdp_gp->nocb_gp_sleep = false;
0999 wake_gp = true;
1000 }
1001 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
1002
1003 return wake_gp;
1004 }
1005
1006 static long rcu_nocb_rdp_deoffload(void *arg)
1007 {
1008 struct rcu_data *rdp = arg;
1009 struct rcu_segcblist *cblist = &rdp->cblist;
1010 unsigned long flags;
1011 int wake_gp;
1012 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
1013
1014
1015
1016
1017
1018
1019 WARN_ON_ONCE((rdp->cpu != raw_smp_processor_id()) && cpu_online(rdp->cpu));
1020
1021 pr_info("De-offloading %d\n", rdp->cpu);
1022
1023 rcu_nocb_lock_irqsave(rdp, flags);
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
1034
1035
1036
1037
1038
1039
1040
1041 rcu_segcblist_set_flags(cblist, SEGCBLIST_RCU_CORE);
1042 invoke_rcu_core();
1043 wake_gp = rdp_offload_toggle(rdp, false, flags);
1044
1045 mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
1046 if (rdp_gp->nocb_gp_kthread) {
1047 if (wake_gp)
1048 wake_up_process(rdp_gp->nocb_gp_kthread);
1049
1050
1051
1052
1053
1054 if (!rdp->nocb_cb_kthread) {
1055 rcu_nocb_lock_irqsave(rdp, flags);
1056 rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB);
1057 rcu_nocb_unlock_irqrestore(rdp, flags);
1058 }
1059
1060 swait_event_exclusive(rdp->nocb_state_wq,
1061 !rcu_segcblist_test_flags(cblist,
1062 SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP));
1063 } else {
1064
1065
1066
1067
1068
1069 rcu_nocb_lock_irqsave(rdp, flags);
1070 rcu_segcblist_clear_flags(&rdp->cblist,
1071 SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP);
1072 rcu_nocb_unlock_irqrestore(rdp, flags);
1073
1074 list_del(&rdp->nocb_entry_rdp);
1075 }
1076 mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1077
1078
1079
1080
1081
1082 rcu_nocb_lock_irqsave(rdp, flags);
1083
1084
1085
1086
1087 rcu_segcblist_clear_flags(cblist, SEGCBLIST_LOCKING);
1088
1089
1090
1091
1092 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1093
1094
1095 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
1096
1097
1098 return 0;
1099 }
1100
1101 int rcu_nocb_cpu_deoffload(int cpu)
1102 {
1103 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1104 int ret = 0;
1105
1106 cpus_read_lock();
1107 mutex_lock(&rcu_state.barrier_mutex);
1108 if (rcu_rdp_is_offloaded(rdp)) {
1109 if (cpu_online(cpu)) {
1110 ret = work_on_cpu(cpu, rcu_nocb_rdp_deoffload, rdp);
1111 if (!ret)
1112 cpumask_clear_cpu(cpu, rcu_nocb_mask);
1113 } else {
1114 pr_info("NOCB: Can't CB-deoffload an offline CPU\n");
1115 ret = -EINVAL;
1116 }
1117 }
1118 mutex_unlock(&rcu_state.barrier_mutex);
1119 cpus_read_unlock();
1120
1121 return ret;
1122 }
1123 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_deoffload);
1124
1125 static long rcu_nocb_rdp_offload(void *arg)
1126 {
1127 struct rcu_data *rdp = arg;
1128 struct rcu_segcblist *cblist = &rdp->cblist;
1129 unsigned long flags;
1130 int wake_gp;
1131 struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
1132
1133 WARN_ON_ONCE(rdp->cpu != raw_smp_processor_id());
1134
1135
1136
1137
1138 if (!rdp->nocb_gp_rdp)
1139 return -EINVAL;
1140
1141 if (WARN_ON_ONCE(!rdp_gp->nocb_gp_kthread))
1142 return -EINVAL;
1143
1144 pr_info("Offloading %d\n", rdp->cpu);
1145
1146
1147
1148
1149
1150 raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168 wake_gp = rdp_offload_toggle(rdp, true, flags);
1169 if (wake_gp)
1170 wake_up_process(rdp_gp->nocb_gp_kthread);
1171 swait_event_exclusive(rdp->nocb_state_wq,
1172 rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB) &&
1173 rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
1174
1175
1176
1177
1178
1179 rcu_nocb_lock_irqsave(rdp, flags);
1180 rcu_segcblist_clear_flags(cblist, SEGCBLIST_RCU_CORE);
1181 rcu_nocb_unlock_irqrestore(rdp, flags);
1182
1183 return 0;
1184 }
1185
1186 int rcu_nocb_cpu_offload(int cpu)
1187 {
1188 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1189 int ret = 0;
1190
1191 cpus_read_lock();
1192 mutex_lock(&rcu_state.barrier_mutex);
1193 if (!rcu_rdp_is_offloaded(rdp)) {
1194 if (cpu_online(cpu)) {
1195 ret = work_on_cpu(cpu, rcu_nocb_rdp_offload, rdp);
1196 if (!ret)
1197 cpumask_set_cpu(cpu, rcu_nocb_mask);
1198 } else {
1199 pr_info("NOCB: Can't CB-offload an offline CPU\n");
1200 ret = -EINVAL;
1201 }
1202 }
1203 mutex_unlock(&rcu_state.barrier_mutex);
1204 cpus_read_unlock();
1205
1206 return ret;
1207 }
1208 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_offload);
1209
1210 void __init rcu_init_nohz(void)
1211 {
1212 int cpu;
1213 bool need_rcu_nocb_mask = false;
1214 bool offload_all = false;
1215 struct rcu_data *rdp;
1216
1217 #if defined(CONFIG_RCU_NOCB_CPU_DEFAULT_ALL)
1218 if (!rcu_state.nocb_is_setup) {
1219 need_rcu_nocb_mask = true;
1220 offload_all = true;
1221 }
1222 #endif
1223
1224 #if defined(CONFIG_NO_HZ_FULL)
1225 if (tick_nohz_full_running && !cpumask_empty(tick_nohz_full_mask)) {
1226 need_rcu_nocb_mask = true;
1227 offload_all = false;
1228 }
1229 #endif
1230
1231 if (need_rcu_nocb_mask) {
1232 if (!cpumask_available(rcu_nocb_mask)) {
1233 if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
1234 pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
1235 return;
1236 }
1237 }
1238 rcu_state.nocb_is_setup = true;
1239 }
1240
1241 if (!rcu_state.nocb_is_setup)
1242 return;
1243
1244 #if defined(CONFIG_NO_HZ_FULL)
1245 if (tick_nohz_full_running)
1246 cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
1247 #endif
1248
1249 if (offload_all)
1250 cpumask_setall(rcu_nocb_mask);
1251
1252 if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
1253 pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
1254 cpumask_and(rcu_nocb_mask, cpu_possible_mask,
1255 rcu_nocb_mask);
1256 }
1257 if (cpumask_empty(rcu_nocb_mask))
1258 pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
1259 else
1260 pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
1261 cpumask_pr_args(rcu_nocb_mask));
1262 if (rcu_nocb_poll)
1263 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
1264
1265 for_each_cpu(cpu, rcu_nocb_mask) {
1266 rdp = per_cpu_ptr(&rcu_data, cpu);
1267 if (rcu_segcblist_empty(&rdp->cblist))
1268 rcu_segcblist_init(&rdp->cblist);
1269 rcu_segcblist_offload(&rdp->cblist, true);
1270 rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP);
1271 rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_RCU_CORE);
1272 }
1273 rcu_organize_nocb_kthreads();
1274 }
1275
1276
1277 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1278 {
1279 init_swait_queue_head(&rdp->nocb_cb_wq);
1280 init_swait_queue_head(&rdp->nocb_gp_wq);
1281 init_swait_queue_head(&rdp->nocb_state_wq);
1282 raw_spin_lock_init(&rdp->nocb_lock);
1283 raw_spin_lock_init(&rdp->nocb_bypass_lock);
1284 raw_spin_lock_init(&rdp->nocb_gp_lock);
1285 timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
1286 rcu_cblist_init(&rdp->nocb_bypass);
1287 mutex_init(&rdp->nocb_gp_kthread_mutex);
1288 }
1289
1290
1291
1292
1293
1294
1295 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1296 {
1297 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1298 struct rcu_data *rdp_gp;
1299 struct task_struct *t;
1300 struct sched_param sp;
1301
1302 if (!rcu_scheduler_fully_active || !rcu_state.nocb_is_setup)
1303 return;
1304
1305
1306 if (rdp->nocb_cb_kthread)
1307 return;
1308
1309
1310 sp.sched_priority = kthread_prio;
1311 rdp_gp = rdp->nocb_gp_rdp;
1312 mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
1313 if (!rdp_gp->nocb_gp_kthread) {
1314 t = kthread_run(rcu_nocb_gp_kthread, rdp_gp,
1315 "rcuog/%d", rdp_gp->cpu);
1316 if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo GP kthread, OOM is now expected behavior\n", __func__)) {
1317 mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1318 goto end;
1319 }
1320 WRITE_ONCE(rdp_gp->nocb_gp_kthread, t);
1321 if (kthread_prio)
1322 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1323 }
1324 mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1325
1326
1327 t = kthread_run(rcu_nocb_cb_kthread, rdp,
1328 "rcuo%c/%d", rcu_state.abbr, cpu);
1329 if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo CB kthread, OOM is now expected behavior\n", __func__))
1330 goto end;
1331
1332 if (IS_ENABLED(CONFIG_RCU_NOCB_CPU_CB_BOOST) && kthread_prio)
1333 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1334
1335 WRITE_ONCE(rdp->nocb_cb_kthread, t);
1336 WRITE_ONCE(rdp->nocb_gp_kthread, rdp_gp->nocb_gp_kthread);
1337 return;
1338 end:
1339 mutex_lock(&rcu_state.barrier_mutex);
1340 if (rcu_rdp_is_offloaded(rdp)) {
1341 rcu_nocb_rdp_deoffload(rdp);
1342 cpumask_clear_cpu(cpu, rcu_nocb_mask);
1343 }
1344 mutex_unlock(&rcu_state.barrier_mutex);
1345 }
1346
1347
1348 static int rcu_nocb_gp_stride = -1;
1349 module_param(rcu_nocb_gp_stride, int, 0444);
1350
1351
1352
1353
1354 static void __init rcu_organize_nocb_kthreads(void)
1355 {
1356 int cpu;
1357 bool firsttime = true;
1358 bool gotnocbs = false;
1359 bool gotnocbscbs = true;
1360 int ls = rcu_nocb_gp_stride;
1361 int nl = 0;
1362 struct rcu_data *rdp;
1363 struct rcu_data *rdp_gp = NULL;
1364
1365 if (!cpumask_available(rcu_nocb_mask))
1366 return;
1367 if (ls == -1) {
1368 ls = nr_cpu_ids / int_sqrt(nr_cpu_ids);
1369 rcu_nocb_gp_stride = ls;
1370 }
1371
1372
1373
1374
1375
1376
1377 for_each_possible_cpu(cpu) {
1378 rdp = per_cpu_ptr(&rcu_data, cpu);
1379 if (rdp->cpu >= nl) {
1380
1381 gotnocbs = true;
1382 nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
1383 rdp_gp = rdp;
1384 INIT_LIST_HEAD(&rdp->nocb_head_rdp);
1385 if (dump_tree) {
1386 if (!firsttime)
1387 pr_cont("%s\n", gotnocbscbs
1388 ? "" : " (self only)");
1389 gotnocbscbs = false;
1390 firsttime = false;
1391 pr_alert("%s: No-CB GP kthread CPU %d:",
1392 __func__, cpu);
1393 }
1394 } else {
1395
1396 gotnocbscbs = true;
1397 if (dump_tree)
1398 pr_cont(" %d", cpu);
1399 }
1400 rdp->nocb_gp_rdp = rdp_gp;
1401 if (cpumask_test_cpu(cpu, rcu_nocb_mask))
1402 list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
1403 }
1404 if (gotnocbs && dump_tree)
1405 pr_cont("%s\n", gotnocbscbs ? "" : " (self only)");
1406 }
1407
1408
1409
1410
1411
1412 void rcu_bind_current_to_nocb(void)
1413 {
1414 if (cpumask_available(rcu_nocb_mask) && !cpumask_empty(rcu_nocb_mask))
1415 WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
1416 }
1417 EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
1418
1419
1420 #ifdef CONFIG_SMP
1421 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1422 {
1423 return tsp && task_is_running(tsp) && !tsp->on_cpu ? "!" : "";
1424 }
1425 #else
1426 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1427 {
1428 return "";
1429 }
1430 #endif
1431
1432
1433
1434
1435
1436 static void show_rcu_nocb_gp_state(struct rcu_data *rdp)
1437 {
1438 struct rcu_node *rnp = rdp->mynode;
1439
1440 pr_info("nocb GP %d %c%c%c%c%c %c[%c%c] %c%c:%ld rnp %d:%d %lu %c CPU %d%s\n",
1441 rdp->cpu,
1442 "kK"[!!rdp->nocb_gp_kthread],
1443 "lL"[raw_spin_is_locked(&rdp->nocb_gp_lock)],
1444 "dD"[!!rdp->nocb_defer_wakeup],
1445 "tT"[timer_pending(&rdp->nocb_timer)],
1446 "sS"[!!rdp->nocb_gp_sleep],
1447 ".W"[swait_active(&rdp->nocb_gp_wq)],
1448 ".W"[swait_active(&rnp->nocb_gp_wq[0])],
1449 ".W"[swait_active(&rnp->nocb_gp_wq[1])],
1450 ".B"[!!rdp->nocb_gp_bypass],
1451 ".G"[!!rdp->nocb_gp_gp],
1452 (long)rdp->nocb_gp_seq,
1453 rnp->grplo, rnp->grphi, READ_ONCE(rdp->nocb_gp_loops),
1454 rdp->nocb_gp_kthread ? task_state_to_char(rdp->nocb_gp_kthread) : '.',
1455 rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1456 show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1457 }
1458
1459
1460 static void show_rcu_nocb_state(struct rcu_data *rdp)
1461 {
1462 char bufw[20];
1463 char bufr[20];
1464 struct rcu_data *nocb_next_rdp;
1465 struct rcu_segcblist *rsclp = &rdp->cblist;
1466 bool waslocked;
1467 bool wassleep;
1468
1469 if (rdp->nocb_gp_rdp == rdp)
1470 show_rcu_nocb_gp_state(rdp);
1471
1472 nocb_next_rdp = list_next_or_null_rcu(&rdp->nocb_gp_rdp->nocb_head_rdp,
1473 &rdp->nocb_entry_rdp,
1474 typeof(*rdp),
1475 nocb_entry_rdp);
1476
1477 sprintf(bufw, "%ld", rsclp->gp_seq[RCU_WAIT_TAIL]);
1478 sprintf(bufr, "%ld", rsclp->gp_seq[RCU_NEXT_READY_TAIL]);
1479 pr_info(" CB %d^%d->%d %c%c%c%c%c%c F%ld L%ld C%d %c%c%s%c%s%c%c q%ld %c CPU %d%s\n",
1480 rdp->cpu, rdp->nocb_gp_rdp->cpu,
1481 nocb_next_rdp ? nocb_next_rdp->cpu : -1,
1482 "kK"[!!rdp->nocb_cb_kthread],
1483 "bB"[raw_spin_is_locked(&rdp->nocb_bypass_lock)],
1484 "cC"[!!atomic_read(&rdp->nocb_lock_contended)],
1485 "lL"[raw_spin_is_locked(&rdp->nocb_lock)],
1486 "sS"[!!rdp->nocb_cb_sleep],
1487 ".W"[swait_active(&rdp->nocb_cb_wq)],
1488 jiffies - rdp->nocb_bypass_first,
1489 jiffies - rdp->nocb_nobypass_last,
1490 rdp->nocb_nobypass_count,
1491 ".D"[rcu_segcblist_ready_cbs(rsclp)],
1492 ".W"[!rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL)],
1493 rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL) ? "" : bufw,
1494 ".R"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL)],
1495 rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL) ? "" : bufr,
1496 ".N"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_TAIL)],
1497 ".B"[!!rcu_cblist_n_cbs(&rdp->nocb_bypass)],
1498 rcu_segcblist_n_cbs(&rdp->cblist),
1499 rdp->nocb_cb_kthread ? task_state_to_char(rdp->nocb_cb_kthread) : '.',
1500 rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1501 show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1502
1503
1504 if (rdp->nocb_gp_rdp == rdp)
1505 return;
1506
1507 waslocked = raw_spin_is_locked(&rdp->nocb_gp_lock);
1508 wassleep = swait_active(&rdp->nocb_gp_wq);
1509 if (!rdp->nocb_gp_sleep && !waslocked && !wassleep)
1510 return;
1511
1512 pr_info(" nocb GP activity on CB-only CPU!!! %c%c%c %c\n",
1513 "lL"[waslocked],
1514 "dD"[!!rdp->nocb_defer_wakeup],
1515 "sS"[!!rdp->nocb_gp_sleep],
1516 ".W"[wassleep]);
1517 }
1518
1519 #else
1520
1521 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
1522 {
1523 return 0;
1524 }
1525
1526 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
1527 {
1528 return false;
1529 }
1530
1531
1532 static void rcu_nocb_lock(struct rcu_data *rdp)
1533 {
1534 }
1535
1536
1537 static void rcu_nocb_unlock(struct rcu_data *rdp)
1538 {
1539 }
1540
1541
1542 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
1543 unsigned long flags)
1544 {
1545 local_irq_restore(flags);
1546 }
1547
1548
1549 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
1550 {
1551 lockdep_assert_irqs_disabled();
1552 }
1553
1554 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
1555 {
1556 }
1557
1558 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
1559 {
1560 return NULL;
1561 }
1562
1563 static void rcu_init_one_nocb(struct rcu_node *rnp)
1564 {
1565 }
1566
1567 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1568 unsigned long j)
1569 {
1570 return true;
1571 }
1572
1573 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1574 bool *was_alldone, unsigned long flags)
1575 {
1576 return false;
1577 }
1578
1579 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
1580 unsigned long flags)
1581 {
1582 WARN_ON_ONCE(1);
1583 }
1584
1585 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1586 {
1587 }
1588
1589 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
1590 {
1591 return false;
1592 }
1593
1594 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
1595 {
1596 return false;
1597 }
1598
1599 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1600 {
1601 }
1602
1603 static void show_rcu_nocb_state(struct rcu_data *rdp)
1604 {
1605 }
1606
1607 #endif