This source file includes following definitions.
- rcu_get_gp_kthreads_prio
- rcu_rnp_online_cpus
- rcu_gp_in_progress
- rcu_get_n_cbs_cpu
- rcu_softirq_qs
- rcu_dynticks_eqs_enter
- rcu_dynticks_eqs_exit
- rcu_dynticks_eqs_online
- rcu_dynticks_curr_cpu_in_eqs
- rcu_dynticks_snap
- rcu_dynticks_in_eqs
- rcu_dynticks_in_eqs_since
- rcu_eqs_special_set
- rcu_momentary_dyntick_idle
- rcu_is_cpu_rrupt_from_idle
- adjust_jiffies_till_sched_qs
- param_set_first_fqs_jiffies
- param_set_next_fqs_jiffies
- rcu_get_gp_seq
- rcu_exp_batches_completed
- rcu_get_root
- gp_state_getname
- rcutorture_get_gp_data
- rcu_eqs_enter
- rcu_idle_enter
- rcu_user_enter
- rcu_nmi_exit_common
- rcu_nmi_exit
- rcu_irq_exit
- rcu_irq_exit_irqson
- rcu_eqs_exit
- rcu_idle_exit
- rcu_user_exit
- rcu_nmi_enter_common
- rcu_nmi_enter
- rcu_irq_enter
- rcu_irq_enter_irqson
- rcu_is_watching
- rcu_request_urgent_qs_task
- rcu_lockdep_current_cpu_online
- rcu_gpnum_ovf
- dyntick_save_progress_counter
- rcu_implicit_dynticks_qs
- trace_rcu_this_gp
- rcu_start_this_gp
- rcu_future_gp_cleanup
- rcu_gp_kthread_wake
- rcu_accelerate_cbs
- rcu_accelerate_cbs_unlocked
- rcu_advance_cbs
- rcu_advance_cbs_nowake
- __note_gp_changes
- note_gp_changes
- rcu_gp_slow
- rcu_gp_init
- rcu_gp_fqs_check_wake
- rcu_gp_fqs
- rcu_gp_fqs_loop
- rcu_gp_cleanup
- rcu_gp_kthread
- rcu_report_qs_rsp
- rcu_report_qs_rnp
- rcu_report_unblock_qs_rnp
- rcu_report_qs_rdp
- rcu_check_quiescent_state
- rcutree_dying_cpu
- rcu_cleanup_dead_rnp
- rcutree_dead_cpu
- rcu_do_batch
- rcu_sched_clock_irq
- force_qs_rnp
- rcu_force_quiescent_state
- rcu_core
- rcu_core_si
- rcu_wake_cond
- invoke_rcu_core_kthread
- invoke_rcu_core
- rcu_cpu_kthread_park
- rcu_cpu_kthread_should_run
- rcu_cpu_kthread
- rcu_spawn_core_kthreads
- __call_rcu_core
- rcu_leak_callback
- __call_rcu
- call_rcu
- kfree_call_rcu
- rcu_blocking_is_gp
- synchronize_rcu
- get_state_synchronize_rcu
- cond_synchronize_rcu
- rcu_pending
- rcu_barrier_trace
- rcu_barrier_callback
- rcu_barrier_func
- rcu_barrier
- rcu_init_new_rnp
- rcu_boot_init_percpu_data
- rcutree_prepare_cpu
- rcutree_affinity_setting
- rcutree_online_cpu
- rcutree_offline_cpu
- rcu_cpu_starting
- rcu_report_dead
- rcutree_migrate_callbacks
- rcu_pm_notify
- rcu_spawn_gp_kthread
- rcu_scheduler_starting
- rcu_init_one
- rcu_init_geometry
- rcu_dump_rcu_node_tree
- rcu_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #define pr_fmt(fmt) "rcu: " fmt
19
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/spinlock.h>
24 #include <linux/smp.h>
25 #include <linux/rcupdate_wait.h>
26 #include <linux/interrupt.h>
27 #include <linux/sched.h>
28 #include <linux/sched/debug.h>
29 #include <linux/nmi.h>
30 #include <linux/atomic.h>
31 #include <linux/bitops.h>
32 #include <linux/export.h>
33 #include <linux/completion.h>
34 #include <linux/moduleparam.h>
35 #include <linux/percpu.h>
36 #include <linux/notifier.h>
37 #include <linux/cpu.h>
38 #include <linux/mutex.h>
39 #include <linux/time.h>
40 #include <linux/kernel_stat.h>
41 #include <linux/wait.h>
42 #include <linux/kthread.h>
43 #include <uapi/linux/sched/types.h>
44 #include <linux/prefetch.h>
45 #include <linux/delay.h>
46 #include <linux/stop_machine.h>
47 #include <linux/random.h>
48 #include <linux/trace_events.h>
49 #include <linux/suspend.h>
50 #include <linux/ftrace.h>
51 #include <linux/tick.h>
52 #include <linux/sysrq.h>
53 #include <linux/kprobes.h>
54 #include <linux/gfp.h>
55 #include <linux/oom.h>
56 #include <linux/smpboot.h>
57 #include <linux/jiffies.h>
58 #include <linux/sched/isolation.h>
59 #include <linux/sched/clock.h>
60 #include "../time/tick-internal.h"
61
62 #include "tree.h"
63 #include "rcu.h"
64
65 #ifdef MODULE_PARAM_PREFIX
66 #undef MODULE_PARAM_PREFIX
67 #endif
68 #define MODULE_PARAM_PREFIX "rcutree."
69
70
71
72
73
74
75
76 #define RCU_DYNTICK_CTRL_MASK 0x1
77 #define RCU_DYNTICK_CTRL_CTR (RCU_DYNTICK_CTRL_MASK + 1)
78 #ifndef rcu_eqs_special_exit
79 #define rcu_eqs_special_exit() do { } while (0)
80 #endif
81
82 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = {
83 .dynticks_nesting = 1,
84 .dynticks_nmi_nesting = DYNTICK_IRQ_NONIDLE,
85 .dynticks = ATOMIC_INIT(RCU_DYNTICK_CTRL_CTR),
86 };
87 struct rcu_state rcu_state = {
88 .level = { &rcu_state.node[0] },
89 .gp_state = RCU_GP_IDLE,
90 .gp_seq = (0UL - 300UL) << RCU_SEQ_CTR_SHIFT,
91 .barrier_mutex = __MUTEX_INITIALIZER(rcu_state.barrier_mutex),
92 .name = RCU_NAME,
93 .abbr = RCU_ABBR,
94 .exp_mutex = __MUTEX_INITIALIZER(rcu_state.exp_mutex),
95 .exp_wake_mutex = __MUTEX_INITIALIZER(rcu_state.exp_wake_mutex),
96 .ofl_lock = __RAW_SPIN_LOCK_UNLOCKED(rcu_state.ofl_lock),
97 };
98
99
100 static bool dump_tree;
101 module_param(dump_tree, bool, 0444);
102
103 static bool use_softirq = 1;
104 module_param(use_softirq, bool, 0444);
105
106 static bool rcu_fanout_exact;
107 module_param(rcu_fanout_exact, bool, 0444);
108
109 static int rcu_fanout_leaf = RCU_FANOUT_LEAF;
110 module_param(rcu_fanout_leaf, int, 0444);
111 int rcu_num_lvls __read_mostly = RCU_NUM_LVLS;
112
113 int num_rcu_lvl[] = NUM_RCU_LVL_INIT;
114 int rcu_num_nodes __read_mostly = NUM_RCU_NODES;
115
116
117
118
119
120
121
122
123
124
125
126
127
128 int rcu_scheduler_active __read_mostly;
129 EXPORT_SYMBOL_GPL(rcu_scheduler_active);
130
131
132
133
134
135
136
137
138
139
140
141
142
143 static int rcu_scheduler_fully_active __read_mostly;
144
145 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
146 unsigned long gps, unsigned long flags);
147 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf);
148 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf);
149 static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu);
150 static void invoke_rcu_core(void);
151 static void rcu_report_exp_rdp(struct rcu_data *rdp);
152 static void sync_sched_exp_online_cleanup(int cpu);
153
154
155 static int kthread_prio = IS_ENABLED(CONFIG_RCU_BOOST) ? 1 : 0;
156 module_param(kthread_prio, int, 0444);
157
158
159
160 static int gp_preinit_delay;
161 module_param(gp_preinit_delay, int, 0444);
162 static int gp_init_delay;
163 module_param(gp_init_delay, int, 0444);
164 static int gp_cleanup_delay;
165 module_param(gp_cleanup_delay, int, 0444);
166
167
168 int rcu_get_gp_kthreads_prio(void)
169 {
170 return kthread_prio;
171 }
172 EXPORT_SYMBOL_GPL(rcu_get_gp_kthreads_prio);
173
174
175
176
177
178
179
180
181
182
183 #define PER_RCU_NODE_PERIOD 3
184
185
186
187
188
189
190
191 unsigned long rcu_rnp_online_cpus(struct rcu_node *rnp)
192 {
193 return READ_ONCE(rnp->qsmaskinitnext);
194 }
195
196
197
198
199
200
201 static int rcu_gp_in_progress(void)
202 {
203 return rcu_seq_state(rcu_seq_current(&rcu_state.gp_seq));
204 }
205
206
207
208
209
210 static long rcu_get_n_cbs_cpu(int cpu)
211 {
212 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
213
214 if (rcu_segcblist_is_enabled(&rdp->cblist))
215 return rcu_segcblist_n_cbs(&rdp->cblist);
216 return 0;
217 }
218
219 void rcu_softirq_qs(void)
220 {
221 rcu_qs();
222 rcu_preempt_deferred_qs(current);
223 }
224
225
226
227
228
229 static void rcu_dynticks_eqs_enter(void)
230 {
231 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
232 int seq;
233
234
235
236
237
238
239 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
240
241 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
242 (seq & RCU_DYNTICK_CTRL_CTR));
243
244 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
245 (seq & RCU_DYNTICK_CTRL_MASK));
246 }
247
248
249
250
251
252 static void rcu_dynticks_eqs_exit(void)
253 {
254 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
255 int seq;
256
257
258
259
260
261
262 seq = atomic_add_return(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
263 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
264 !(seq & RCU_DYNTICK_CTRL_CTR));
265 if (seq & RCU_DYNTICK_CTRL_MASK) {
266 atomic_andnot(RCU_DYNTICK_CTRL_MASK, &rdp->dynticks);
267 smp_mb__after_atomic();
268
269 rcu_eqs_special_exit();
270 }
271 }
272
273
274
275
276
277
278
279
280
281
282
283 static void rcu_dynticks_eqs_online(void)
284 {
285 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
286
287 if (atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR)
288 return;
289 atomic_add(RCU_DYNTICK_CTRL_CTR, &rdp->dynticks);
290 }
291
292
293
294
295
296
297 bool rcu_dynticks_curr_cpu_in_eqs(void)
298 {
299 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
300
301 return !(atomic_read(&rdp->dynticks) & RCU_DYNTICK_CTRL_CTR);
302 }
303
304
305
306
307
308 int rcu_dynticks_snap(struct rcu_data *rdp)
309 {
310 int snap = atomic_add_return(0, &rdp->dynticks);
311
312 return snap & ~RCU_DYNTICK_CTRL_MASK;
313 }
314
315
316
317
318
319 static bool rcu_dynticks_in_eqs(int snap)
320 {
321 return !(snap & RCU_DYNTICK_CTRL_CTR);
322 }
323
324
325
326
327
328
329 static bool rcu_dynticks_in_eqs_since(struct rcu_data *rdp, int snap)
330 {
331 return snap != rcu_dynticks_snap(rdp);
332 }
333
334
335
336
337
338
339
340
341 bool rcu_eqs_special_set(int cpu)
342 {
343 int old;
344 int new;
345 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
346
347 do {
348 old = atomic_read(&rdp->dynticks);
349 if (old & RCU_DYNTICK_CTRL_CTR)
350 return false;
351 new = old | RCU_DYNTICK_CTRL_MASK;
352 } while (atomic_cmpxchg(&rdp->dynticks, old, new) != old);
353 return true;
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367 static void __maybe_unused rcu_momentary_dyntick_idle(void)
368 {
369 int special;
370
371 raw_cpu_write(rcu_data.rcu_need_heavy_qs, false);
372 special = atomic_add_return(2 * RCU_DYNTICK_CTRL_CTR,
373 &this_cpu_ptr(&rcu_data)->dynticks);
374
375 WARN_ON_ONCE(!(special & RCU_DYNTICK_CTRL_CTR));
376 rcu_preempt_deferred_qs(current);
377 }
378
379
380
381
382
383
384
385
386 static int rcu_is_cpu_rrupt_from_idle(void)
387 {
388
389 lockdep_assert_in_irq();
390
391
392 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nesting) < 0,
393 "RCU dynticks_nesting counter underflow!");
394 RCU_LOCKDEP_WARN(__this_cpu_read(rcu_data.dynticks_nmi_nesting) <= 0,
395 "RCU dynticks_nmi_nesting counter underflow/zero!");
396
397
398 if (__this_cpu_read(rcu_data.dynticks_nmi_nesting) != 1)
399 return false;
400
401
402 return __this_cpu_read(rcu_data.dynticks_nesting) == 0;
403 }
404
405 #define DEFAULT_RCU_BLIMIT 10
406 #define DEFAULT_MAX_RCU_BLIMIT 10000
407 static long blimit = DEFAULT_RCU_BLIMIT;
408 #define DEFAULT_RCU_QHIMARK 10000
409 static long qhimark = DEFAULT_RCU_QHIMARK;
410 #define DEFAULT_RCU_QLOMARK 100
411 static long qlowmark = DEFAULT_RCU_QLOMARK;
412
413 module_param(blimit, long, 0444);
414 module_param(qhimark, long, 0444);
415 module_param(qlowmark, long, 0444);
416
417 static ulong jiffies_till_first_fqs = ULONG_MAX;
418 static ulong jiffies_till_next_fqs = ULONG_MAX;
419 static bool rcu_kick_kthreads;
420 static int rcu_divisor = 7;
421 module_param(rcu_divisor, int, 0644);
422
423
424 static long rcu_resched_ns = 3 * NSEC_PER_MSEC;
425 module_param(rcu_resched_ns, long, 0644);
426
427
428
429
430
431 static ulong jiffies_till_sched_qs = ULONG_MAX;
432 module_param(jiffies_till_sched_qs, ulong, 0444);
433 static ulong jiffies_to_sched_qs;
434 module_param(jiffies_to_sched_qs, ulong, 0444);
435
436
437
438
439
440
441
442 static void adjust_jiffies_till_sched_qs(void)
443 {
444 unsigned long j;
445
446
447 if (jiffies_till_sched_qs != ULONG_MAX) {
448 WRITE_ONCE(jiffies_to_sched_qs, jiffies_till_sched_qs);
449 return;
450 }
451
452 j = READ_ONCE(jiffies_till_first_fqs) +
453 2 * READ_ONCE(jiffies_till_next_fqs);
454 if (j < HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV)
455 j = HZ / 10 + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
456 pr_info("RCU calculated value of scheduler-enlistment delay is %ld jiffies.\n", j);
457 WRITE_ONCE(jiffies_to_sched_qs, j);
458 }
459
460 static int param_set_first_fqs_jiffies(const char *val, const struct kernel_param *kp)
461 {
462 ulong j;
463 int ret = kstrtoul(val, 0, &j);
464
465 if (!ret) {
466 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : j);
467 adjust_jiffies_till_sched_qs();
468 }
469 return ret;
470 }
471
472 static int param_set_next_fqs_jiffies(const char *val, const struct kernel_param *kp)
473 {
474 ulong j;
475 int ret = kstrtoul(val, 0, &j);
476
477 if (!ret) {
478 WRITE_ONCE(*(ulong *)kp->arg, (j > HZ) ? HZ : (j ?: 1));
479 adjust_jiffies_till_sched_qs();
480 }
481 return ret;
482 }
483
484 static struct kernel_param_ops first_fqs_jiffies_ops = {
485 .set = param_set_first_fqs_jiffies,
486 .get = param_get_ulong,
487 };
488
489 static struct kernel_param_ops next_fqs_jiffies_ops = {
490 .set = param_set_next_fqs_jiffies,
491 .get = param_get_ulong,
492 };
493
494 module_param_cb(jiffies_till_first_fqs, &first_fqs_jiffies_ops, &jiffies_till_first_fqs, 0644);
495 module_param_cb(jiffies_till_next_fqs, &next_fqs_jiffies_ops, &jiffies_till_next_fqs, 0644);
496 module_param(rcu_kick_kthreads, bool, 0644);
497
498 static void force_qs_rnp(int (*f)(struct rcu_data *rdp));
499 static int rcu_pending(void);
500
501
502
503
504 unsigned long rcu_get_gp_seq(void)
505 {
506 return READ_ONCE(rcu_state.gp_seq);
507 }
508 EXPORT_SYMBOL_GPL(rcu_get_gp_seq);
509
510
511
512
513
514
515
516 unsigned long rcu_exp_batches_completed(void)
517 {
518 return rcu_state.expedited_sequence;
519 }
520 EXPORT_SYMBOL_GPL(rcu_exp_batches_completed);
521
522
523
524
525 static struct rcu_node *rcu_get_root(void)
526 {
527 return &rcu_state.node[0];
528 }
529
530
531
532
533 static const char *gp_state_getname(short gs)
534 {
535 if (gs < 0 || gs >= ARRAY_SIZE(gp_state_names))
536 return "???";
537 return gp_state_names[gs];
538 }
539
540
541
542
543 void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
544 unsigned long *gp_seq)
545 {
546 switch (test_type) {
547 case RCU_FLAVOR:
548 *flags = READ_ONCE(rcu_state.gp_flags);
549 *gp_seq = rcu_seq_current(&rcu_state.gp_seq);
550 break;
551 default:
552 break;
553 }
554 }
555 EXPORT_SYMBOL_GPL(rcutorture_get_gp_data);
556
557
558
559
560
561
562
563
564
565 static void rcu_eqs_enter(bool user)
566 {
567 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
568
569 WARN_ON_ONCE(rdp->dynticks_nmi_nesting != DYNTICK_IRQ_NONIDLE);
570 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
571 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) &&
572 rdp->dynticks_nesting == 0);
573 if (rdp->dynticks_nesting != 1) {
574 rdp->dynticks_nesting--;
575 return;
576 }
577
578 lockdep_assert_irqs_disabled();
579 trace_rcu_dyntick(TPS("Start"), rdp->dynticks_nesting, 0, atomic_read(&rdp->dynticks));
580 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
581 rdp = this_cpu_ptr(&rcu_data);
582 do_nocb_deferred_wakeup(rdp);
583 rcu_prepare_for_idle();
584 rcu_preempt_deferred_qs(current);
585 WRITE_ONCE(rdp->dynticks_nesting, 0);
586 rcu_dynticks_eqs_enter();
587 rcu_dynticks_task_enter();
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601 void rcu_idle_enter(void)
602 {
603 lockdep_assert_irqs_disabled();
604 rcu_eqs_enter(false);
605 }
606
607 #ifdef CONFIG_NO_HZ_FULL
608
609
610
611
612
613
614
615
616
617
618
619 void rcu_user_enter(void)
620 {
621 lockdep_assert_irqs_disabled();
622 rcu_eqs_enter(true);
623 }
624 #endif
625
626
627
628
629
630
631
632
633
634
635 static __always_inline void rcu_nmi_exit_common(bool irq)
636 {
637 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
638
639
640
641
642
643
644 WARN_ON_ONCE(rdp->dynticks_nmi_nesting <= 0);
645 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs());
646
647
648
649
650
651 if (rdp->dynticks_nmi_nesting != 1) {
652 trace_rcu_dyntick(TPS("--="), rdp->dynticks_nmi_nesting, rdp->dynticks_nmi_nesting - 2,
653 atomic_read(&rdp->dynticks));
654 WRITE_ONCE(rdp->dynticks_nmi_nesting,
655 rdp->dynticks_nmi_nesting - 2);
656 return;
657 }
658
659
660 trace_rcu_dyntick(TPS("Startirq"), rdp->dynticks_nmi_nesting, 0, atomic_read(&rdp->dynticks));
661 WRITE_ONCE(rdp->dynticks_nmi_nesting, 0);
662
663 if (irq)
664 rcu_prepare_for_idle();
665
666 rcu_dynticks_eqs_enter();
667
668 if (irq)
669 rcu_dynticks_task_enter();
670 }
671
672
673
674
675
676
677
678 void rcu_nmi_exit(void)
679 {
680 rcu_nmi_exit_common(false);
681 }
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702 void rcu_irq_exit(void)
703 {
704 lockdep_assert_irqs_disabled();
705 rcu_nmi_exit_common(true);
706 }
707
708
709
710
711
712
713
714 void rcu_irq_exit_irqson(void)
715 {
716 unsigned long flags;
717
718 local_irq_save(flags);
719 rcu_irq_exit();
720 local_irq_restore(flags);
721 }
722
723
724
725
726
727
728
729
730
731 static void rcu_eqs_exit(bool user)
732 {
733 struct rcu_data *rdp;
734 long oldval;
735
736 lockdep_assert_irqs_disabled();
737 rdp = this_cpu_ptr(&rcu_data);
738 oldval = rdp->dynticks_nesting;
739 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && oldval < 0);
740 if (oldval) {
741 rdp->dynticks_nesting++;
742 return;
743 }
744 rcu_dynticks_task_exit();
745 rcu_dynticks_eqs_exit();
746 rcu_cleanup_after_idle();
747 trace_rcu_dyntick(TPS("End"), rdp->dynticks_nesting, 1, atomic_read(&rdp->dynticks));
748 WARN_ON_ONCE(IS_ENABLED(CONFIG_RCU_EQS_DEBUG) && !user && !is_idle_task(current));
749 WRITE_ONCE(rdp->dynticks_nesting, 1);
750 WARN_ON_ONCE(rdp->dynticks_nmi_nesting);
751 WRITE_ONCE(rdp->dynticks_nmi_nesting, DYNTICK_IRQ_NONIDLE);
752 }
753
754
755
756
757
758
759
760
761
762
763 void rcu_idle_exit(void)
764 {
765 unsigned long flags;
766
767 local_irq_save(flags);
768 rcu_eqs_exit(false);
769 local_irq_restore(flags);
770 }
771
772 #ifdef CONFIG_NO_HZ_FULL
773
774
775
776
777
778
779
780
781
782 void rcu_user_exit(void)
783 {
784 rcu_eqs_exit(1);
785 }
786 #endif
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801 static __always_inline void rcu_nmi_enter_common(bool irq)
802 {
803 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
804 long incby = 2;
805
806
807 WARN_ON_ONCE(rdp->dynticks_nmi_nesting < 0);
808
809
810
811
812
813
814
815
816
817 if (rcu_dynticks_curr_cpu_in_eqs()) {
818
819 if (irq)
820 rcu_dynticks_task_exit();
821
822 rcu_dynticks_eqs_exit();
823
824 if (irq)
825 rcu_cleanup_after_idle();
826
827 incby = 1;
828 }
829 trace_rcu_dyntick(incby == 1 ? TPS("Endirq") : TPS("++="),
830 rdp->dynticks_nmi_nesting,
831 rdp->dynticks_nmi_nesting + incby, atomic_read(&rdp->dynticks));
832 WRITE_ONCE(rdp->dynticks_nmi_nesting,
833 rdp->dynticks_nmi_nesting + incby);
834 barrier();
835 }
836
837
838
839
840 void rcu_nmi_enter(void)
841 {
842 rcu_nmi_enter_common(false);
843 }
844 NOKPROBE_SYMBOL(rcu_nmi_enter);
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868 void rcu_irq_enter(void)
869 {
870 lockdep_assert_irqs_disabled();
871 rcu_nmi_enter_common(true);
872 }
873
874
875
876
877
878
879
880 void rcu_irq_enter_irqson(void)
881 {
882 unsigned long flags;
883
884 local_irq_save(flags);
885 rcu_irq_enter();
886 local_irq_restore(flags);
887 }
888
889
890
891
892
893
894
895
896
897 bool notrace rcu_is_watching(void)
898 {
899 bool ret;
900
901 preempt_disable_notrace();
902 ret = !rcu_dynticks_curr_cpu_in_eqs();
903 preempt_enable_notrace();
904 return ret;
905 }
906 EXPORT_SYMBOL_GPL(rcu_is_watching);
907
908
909
910
911
912
913
914
915 void rcu_request_urgent_qs_task(struct task_struct *t)
916 {
917 int cpu;
918
919 barrier();
920 cpu = task_cpu(t);
921 if (!task_curr(t))
922 return;
923 smp_store_release(per_cpu_ptr(&rcu_data.rcu_urgent_qs, cpu), true);
924 }
925
926 #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU)
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941 bool rcu_lockdep_current_cpu_online(void)
942 {
943 struct rcu_data *rdp;
944 struct rcu_node *rnp;
945 bool ret = false;
946
947 if (in_nmi() || !rcu_scheduler_fully_active)
948 return true;
949 preempt_disable();
950 rdp = this_cpu_ptr(&rcu_data);
951 rnp = rdp->mynode;
952 if (rdp->grpmask & rcu_rnp_online_cpus(rnp))
953 ret = true;
954 preempt_enable();
955 return ret;
956 }
957 EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online);
958
959 #endif
960
961
962
963
964
965
966
967
968 static void rcu_gpnum_ovf(struct rcu_node *rnp, struct rcu_data *rdp)
969 {
970 raw_lockdep_assert_held_rcu_node(rnp);
971 if (ULONG_CMP_LT(rcu_seq_current(&rdp->gp_seq) + ULONG_MAX / 4,
972 rnp->gp_seq))
973 WRITE_ONCE(rdp->gpwrap, true);
974 if (ULONG_CMP_LT(rdp->rcu_iw_gp_seq + ULONG_MAX / 4, rnp->gp_seq))
975 rdp->rcu_iw_gp_seq = rnp->gp_seq + ULONG_MAX / 4;
976 }
977
978
979
980
981
982
983 static int dyntick_save_progress_counter(struct rcu_data *rdp)
984 {
985 rdp->dynticks_snap = rcu_dynticks_snap(rdp);
986 if (rcu_dynticks_in_eqs(rdp->dynticks_snap)) {
987 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
988 rcu_gpnum_ovf(rdp->mynode, rdp);
989 return 1;
990 }
991 return 0;
992 }
993
994
995
996
997
998
999
1000 static int rcu_implicit_dynticks_qs(struct rcu_data *rdp)
1001 {
1002 unsigned long jtsq;
1003 bool *rnhqp;
1004 bool *ruqp;
1005 struct rcu_node *rnp = rdp->mynode;
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015 if (rcu_dynticks_in_eqs_since(rdp, rdp->dynticks_snap)) {
1016 trace_rcu_fqs(rcu_state.name, rdp->gp_seq, rdp->cpu, TPS("dti"));
1017 rcu_gpnum_ovf(rnp, rdp);
1018 return 1;
1019 }
1020
1021
1022 if (!(rdp->grpmask & rcu_rnp_online_cpus(rnp)) &&
1023 time_after(jiffies, rcu_state.gp_start + HZ)) {
1024 bool onl;
1025 struct rcu_node *rnp1;
1026
1027 WARN_ON(1);
1028 pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
1029 __func__, rnp->grplo, rnp->grphi, rnp->level,
1030 (long)rnp->gp_seq, (long)rnp->completedqs);
1031 for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
1032 pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx ->rcu_gp_init_mask %#lx\n",
1033 __func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext, rnp1->rcu_gp_init_mask);
1034 onl = !!(rdp->grpmask & rcu_rnp_online_cpus(rnp));
1035 pr_info("%s %d: %c online: %ld(%d) offline: %ld(%d)\n",
1036 __func__, rdp->cpu, ".o"[onl],
1037 (long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_flags,
1038 (long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_flags);
1039 return 1;
1040 }
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053 jtsq = READ_ONCE(jiffies_to_sched_qs);
1054 ruqp = per_cpu_ptr(&rcu_data.rcu_urgent_qs, rdp->cpu);
1055 rnhqp = &per_cpu(rcu_data.rcu_need_heavy_qs, rdp->cpu);
1056 if (!READ_ONCE(*rnhqp) &&
1057 (time_after(jiffies, rcu_state.gp_start + jtsq * 2) ||
1058 time_after(jiffies, rcu_state.jiffies_resched))) {
1059 WRITE_ONCE(*rnhqp, true);
1060
1061 smp_store_release(ruqp, true);
1062 } else if (time_after(jiffies, rcu_state.gp_start + jtsq)) {
1063 WRITE_ONCE(*ruqp, true);
1064 }
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074 if (tick_nohz_full_cpu(rdp->cpu) &&
1075 time_after(jiffies,
1076 READ_ONCE(rdp->last_fqs_resched) + jtsq * 3)) {
1077 resched_cpu(rdp->cpu);
1078 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088 if (time_after(jiffies, rcu_state.jiffies_resched)) {
1089 if (time_after(jiffies,
1090 READ_ONCE(rdp->last_fqs_resched) + jtsq)) {
1091 resched_cpu(rdp->cpu);
1092 WRITE_ONCE(rdp->last_fqs_resched, jiffies);
1093 }
1094 if (IS_ENABLED(CONFIG_IRQ_WORK) &&
1095 !rdp->rcu_iw_pending && rdp->rcu_iw_gp_seq != rnp->gp_seq &&
1096 (rnp->ffmask & rdp->grpmask)) {
1097 init_irq_work(&rdp->rcu_iw, rcu_iw_handler);
1098 rdp->rcu_iw_pending = true;
1099 rdp->rcu_iw_gp_seq = rnp->gp_seq;
1100 irq_work_queue_on(&rdp->rcu_iw, rdp->cpu);
1101 }
1102 }
1103
1104 return 0;
1105 }
1106
1107
1108 static void trace_rcu_this_gp(struct rcu_node *rnp, struct rcu_data *rdp,
1109 unsigned long gp_seq_req, const char *s)
1110 {
1111 trace_rcu_future_grace_period(rcu_state.name, rnp->gp_seq, gp_seq_req,
1112 rnp->level, rnp->grplo, rnp->grphi, s);
1113 }
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131 static bool rcu_start_this_gp(struct rcu_node *rnp_start, struct rcu_data *rdp,
1132 unsigned long gp_seq_req)
1133 {
1134 bool ret = false;
1135 struct rcu_node *rnp;
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 raw_lockdep_assert_held_rcu_node(rnp_start);
1147 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req, TPS("Startleaf"));
1148 for (rnp = rnp_start; 1; rnp = rnp->parent) {
1149 if (rnp != rnp_start)
1150 raw_spin_lock_rcu_node(rnp);
1151 if (ULONG_CMP_GE(rnp->gp_seq_needed, gp_seq_req) ||
1152 rcu_seq_started(&rnp->gp_seq, gp_seq_req) ||
1153 (rnp != rnp_start &&
1154 rcu_seq_state(rcu_seq_current(&rnp->gp_seq)))) {
1155 trace_rcu_this_gp(rnp, rdp, gp_seq_req,
1156 TPS("Prestarted"));
1157 goto unlock_out;
1158 }
1159 rnp->gp_seq_needed = gp_seq_req;
1160 if (rcu_seq_state(rcu_seq_current(&rnp->gp_seq))) {
1161
1162
1163
1164
1165
1166
1167 trace_rcu_this_gp(rnp_start, rdp, gp_seq_req,
1168 TPS("Startedleaf"));
1169 goto unlock_out;
1170 }
1171 if (rnp != rnp_start && rnp->parent != NULL)
1172 raw_spin_unlock_rcu_node(rnp);
1173 if (!rnp->parent)
1174 break;
1175 }
1176
1177
1178 if (rcu_gp_in_progress()) {
1179 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedleafroot"));
1180 goto unlock_out;
1181 }
1182 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("Startedroot"));
1183 WRITE_ONCE(rcu_state.gp_flags, rcu_state.gp_flags | RCU_GP_FLAG_INIT);
1184 rcu_state.gp_req_activity = jiffies;
1185 if (!rcu_state.gp_kthread) {
1186 trace_rcu_this_gp(rnp, rdp, gp_seq_req, TPS("NoGPkthread"));
1187 goto unlock_out;
1188 }
1189 trace_rcu_grace_period(rcu_state.name, READ_ONCE(rcu_state.gp_seq), TPS("newreq"));
1190 ret = true;
1191 unlock_out:
1192
1193 if (ULONG_CMP_LT(gp_seq_req, rnp->gp_seq_needed)) {
1194 rnp_start->gp_seq_needed = rnp->gp_seq_needed;
1195 rdp->gp_seq_needed = rnp->gp_seq_needed;
1196 }
1197 if (rnp != rnp_start)
1198 raw_spin_unlock_rcu_node(rnp);
1199 return ret;
1200 }
1201
1202
1203
1204
1205
1206 static bool rcu_future_gp_cleanup(struct rcu_node *rnp)
1207 {
1208 bool needmore;
1209 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1210
1211 needmore = ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed);
1212 if (!needmore)
1213 rnp->gp_seq_needed = rnp->gp_seq;
1214 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq,
1215 needmore ? TPS("CleanupMore") : TPS("Cleanup"));
1216 return needmore;
1217 }
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233 static void rcu_gp_kthread_wake(void)
1234 {
1235 if ((current == rcu_state.gp_kthread &&
1236 !in_irq() && !in_serving_softirq()) ||
1237 !READ_ONCE(rcu_state.gp_flags) ||
1238 !rcu_state.gp_kthread)
1239 return;
1240 WRITE_ONCE(rcu_state.gp_wake_time, jiffies);
1241 WRITE_ONCE(rcu_state.gp_wake_seq, READ_ONCE(rcu_state.gp_seq));
1242 swake_up_one(&rcu_state.gp_wq);
1243 }
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257 static bool rcu_accelerate_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
1258 {
1259 unsigned long gp_seq_req;
1260 bool ret = false;
1261
1262 rcu_lockdep_assert_cblist_protected(rdp);
1263 raw_lockdep_assert_held_rcu_node(rnp);
1264
1265
1266 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
1267 return false;
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279 gp_seq_req = rcu_seq_snap(&rcu_state.gp_seq);
1280 if (rcu_segcblist_accelerate(&rdp->cblist, gp_seq_req))
1281 ret = rcu_start_this_gp(rnp, rdp, gp_seq_req);
1282
1283
1284 if (rcu_segcblist_restempty(&rdp->cblist, RCU_WAIT_TAIL))
1285 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccWaitCB"));
1286 else
1287 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("AccReadyCB"));
1288 return ret;
1289 }
1290
1291
1292
1293
1294
1295
1296
1297
1298 static void rcu_accelerate_cbs_unlocked(struct rcu_node *rnp,
1299 struct rcu_data *rdp)
1300 {
1301 unsigned long c;
1302 bool needwake;
1303
1304 rcu_lockdep_assert_cblist_protected(rdp);
1305 c = rcu_seq_snap(&rcu_state.gp_seq);
1306 if (!rdp->gpwrap && ULONG_CMP_GE(rdp->gp_seq_needed, c)) {
1307
1308 (void)rcu_segcblist_accelerate(&rdp->cblist, c);
1309 return;
1310 }
1311 raw_spin_lock_rcu_node(rnp);
1312 needwake = rcu_accelerate_cbs(rnp, rdp);
1313 raw_spin_unlock_rcu_node(rnp);
1314 if (needwake)
1315 rcu_gp_kthread_wake();
1316 }
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 static bool rcu_advance_cbs(struct rcu_node *rnp, struct rcu_data *rdp)
1329 {
1330 rcu_lockdep_assert_cblist_protected(rdp);
1331 raw_lockdep_assert_held_rcu_node(rnp);
1332
1333
1334 if (!rcu_segcblist_pend_cbs(&rdp->cblist))
1335 return false;
1336
1337
1338
1339
1340
1341 rcu_segcblist_advance(&rdp->cblist, rnp->gp_seq);
1342
1343
1344 return rcu_accelerate_cbs(rnp, rdp);
1345 }
1346
1347
1348
1349
1350
1351 static void __maybe_unused rcu_advance_cbs_nowake(struct rcu_node *rnp,
1352 struct rcu_data *rdp)
1353 {
1354 rcu_lockdep_assert_cblist_protected(rdp);
1355 if (!rcu_seq_state(rcu_seq_current(&rnp->gp_seq)) ||
1356 !raw_spin_trylock_rcu_node(rnp))
1357 return;
1358 WARN_ON_ONCE(rcu_advance_cbs(rnp, rdp));
1359 raw_spin_unlock_rcu_node(rnp);
1360 }
1361
1362
1363
1364
1365
1366
1367
1368 static bool __note_gp_changes(struct rcu_node *rnp, struct rcu_data *rdp)
1369 {
1370 bool ret = false;
1371 bool need_gp;
1372 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1373 rcu_segcblist_is_offloaded(&rdp->cblist);
1374
1375 raw_lockdep_assert_held_rcu_node(rnp);
1376
1377 if (rdp->gp_seq == rnp->gp_seq)
1378 return false;
1379
1380
1381 if (rcu_seq_completed_gp(rdp->gp_seq, rnp->gp_seq) ||
1382 unlikely(READ_ONCE(rdp->gpwrap))) {
1383 if (!offloaded)
1384 ret = rcu_advance_cbs(rnp, rdp);
1385 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuend"));
1386 } else {
1387 if (!offloaded)
1388 ret = rcu_accelerate_cbs(rnp, rdp);
1389 }
1390
1391
1392 if (rcu_seq_new_gp(rdp->gp_seq, rnp->gp_seq) ||
1393 unlikely(READ_ONCE(rdp->gpwrap))) {
1394
1395
1396
1397
1398
1399 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq, TPS("cpustart"));
1400 need_gp = !!(rnp->qsmask & rdp->grpmask);
1401 rdp->cpu_no_qs.b.norm = need_gp;
1402 rdp->core_needs_qs = need_gp;
1403 zero_cpu_stall_ticks(rdp);
1404 }
1405 rdp->gp_seq = rnp->gp_seq;
1406 if (ULONG_CMP_LT(rdp->gp_seq_needed, rnp->gp_seq_needed) || rdp->gpwrap)
1407 rdp->gp_seq_needed = rnp->gp_seq_needed;
1408 WRITE_ONCE(rdp->gpwrap, false);
1409 rcu_gpnum_ovf(rnp, rdp);
1410 return ret;
1411 }
1412
1413 static void note_gp_changes(struct rcu_data *rdp)
1414 {
1415 unsigned long flags;
1416 bool needwake;
1417 struct rcu_node *rnp;
1418
1419 local_irq_save(flags);
1420 rnp = rdp->mynode;
1421 if ((rdp->gp_seq == rcu_seq_current(&rnp->gp_seq) &&
1422 !unlikely(READ_ONCE(rdp->gpwrap))) ||
1423 !raw_spin_trylock_rcu_node(rnp)) {
1424 local_irq_restore(flags);
1425 return;
1426 }
1427 needwake = __note_gp_changes(rnp, rdp);
1428 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1429 if (needwake)
1430 rcu_gp_kthread_wake();
1431 }
1432
1433 static void rcu_gp_slow(int delay)
1434 {
1435 if (delay > 0 &&
1436 !(rcu_seq_ctr(rcu_state.gp_seq) %
1437 (rcu_num_nodes * PER_RCU_NODE_PERIOD * delay)))
1438 schedule_timeout_uninterruptible(delay);
1439 }
1440
1441
1442
1443
1444 static bool rcu_gp_init(void)
1445 {
1446 unsigned long flags;
1447 unsigned long oldmask;
1448 unsigned long mask;
1449 struct rcu_data *rdp;
1450 struct rcu_node *rnp = rcu_get_root();
1451
1452 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1453 raw_spin_lock_irq_rcu_node(rnp);
1454 if (!READ_ONCE(rcu_state.gp_flags)) {
1455
1456 raw_spin_unlock_irq_rcu_node(rnp);
1457 return false;
1458 }
1459 WRITE_ONCE(rcu_state.gp_flags, 0);
1460
1461 if (WARN_ON_ONCE(rcu_gp_in_progress())) {
1462
1463
1464
1465
1466 raw_spin_unlock_irq_rcu_node(rnp);
1467 return false;
1468 }
1469
1470
1471 record_gp_stall_check_time();
1472
1473 rcu_seq_start(&rcu_state.gp_seq);
1474 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("start"));
1475 raw_spin_unlock_irq_rcu_node(rnp);
1476
1477
1478
1479
1480
1481
1482
1483 rcu_state.gp_state = RCU_GP_ONOFF;
1484 rcu_for_each_leaf_node(rnp) {
1485 raw_spin_lock(&rcu_state.ofl_lock);
1486 raw_spin_lock_irq_rcu_node(rnp);
1487 if (rnp->qsmaskinit == rnp->qsmaskinitnext &&
1488 !rnp->wait_blkd_tasks) {
1489
1490 raw_spin_unlock_irq_rcu_node(rnp);
1491 raw_spin_unlock(&rcu_state.ofl_lock);
1492 continue;
1493 }
1494
1495
1496 oldmask = rnp->qsmaskinit;
1497 rnp->qsmaskinit = rnp->qsmaskinitnext;
1498
1499
1500 if (!oldmask != !rnp->qsmaskinit) {
1501 if (!oldmask) {
1502 if (!rnp->wait_blkd_tasks)
1503 rcu_init_new_rnp(rnp);
1504 } else if (rcu_preempt_has_tasks(rnp)) {
1505 rnp->wait_blkd_tasks = true;
1506 } else {
1507 rcu_cleanup_dead_rnp(rnp);
1508 }
1509 }
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519 if (rnp->wait_blkd_tasks &&
1520 (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
1521 rnp->wait_blkd_tasks = false;
1522 if (!rnp->qsmaskinit)
1523 rcu_cleanup_dead_rnp(rnp);
1524 }
1525
1526 raw_spin_unlock_irq_rcu_node(rnp);
1527 raw_spin_unlock(&rcu_state.ofl_lock);
1528 }
1529 rcu_gp_slow(gp_preinit_delay);
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543 rcu_state.gp_state = RCU_GP_INIT;
1544 rcu_for_each_node_breadth_first(rnp) {
1545 rcu_gp_slow(gp_init_delay);
1546 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1547 rdp = this_cpu_ptr(&rcu_data);
1548 rcu_preempt_check_blocked_tasks(rnp);
1549 rnp->qsmask = rnp->qsmaskinit;
1550 WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);
1551 if (rnp == rdp->mynode)
1552 (void)__note_gp_changes(rnp, rdp);
1553 rcu_preempt_boost_start_gp(rnp);
1554 trace_rcu_grace_period_init(rcu_state.name, rnp->gp_seq,
1555 rnp->level, rnp->grplo,
1556 rnp->grphi, rnp->qsmask);
1557
1558 mask = rnp->qsmask & ~rnp->qsmaskinitnext;
1559 rnp->rcu_gp_init_mask = mask;
1560 if ((mask || rnp->wait_blkd_tasks) && rcu_is_leaf_node(rnp))
1561 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
1562 else
1563 raw_spin_unlock_irq_rcu_node(rnp);
1564 cond_resched_tasks_rcu_qs();
1565 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1566 }
1567
1568 return true;
1569 }
1570
1571
1572
1573
1574
1575 static bool rcu_gp_fqs_check_wake(int *gfp)
1576 {
1577 struct rcu_node *rnp = rcu_get_root();
1578
1579
1580 *gfp = READ_ONCE(rcu_state.gp_flags);
1581 if (*gfp & RCU_GP_FLAG_FQS)
1582 return true;
1583
1584
1585 if (!READ_ONCE(rnp->qsmask) && !rcu_preempt_blocked_readers_cgp(rnp))
1586 return true;
1587
1588 return false;
1589 }
1590
1591
1592
1593
1594 static void rcu_gp_fqs(bool first_time)
1595 {
1596 struct rcu_node *rnp = rcu_get_root();
1597
1598 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1599 rcu_state.n_force_qs++;
1600 if (first_time) {
1601
1602 force_qs_rnp(dyntick_save_progress_counter);
1603 } else {
1604
1605 force_qs_rnp(rcu_implicit_dynticks_qs);
1606 }
1607
1608 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
1609 raw_spin_lock_irq_rcu_node(rnp);
1610 WRITE_ONCE(rcu_state.gp_flags,
1611 READ_ONCE(rcu_state.gp_flags) & ~RCU_GP_FLAG_FQS);
1612 raw_spin_unlock_irq_rcu_node(rnp);
1613 }
1614 }
1615
1616
1617
1618
1619 static void rcu_gp_fqs_loop(void)
1620 {
1621 bool first_gp_fqs;
1622 int gf;
1623 unsigned long j;
1624 int ret;
1625 struct rcu_node *rnp = rcu_get_root();
1626
1627 first_gp_fqs = true;
1628 j = READ_ONCE(jiffies_till_first_fqs);
1629 ret = 0;
1630 for (;;) {
1631 if (!ret) {
1632 rcu_state.jiffies_force_qs = jiffies + j;
1633 WRITE_ONCE(rcu_state.jiffies_kick_kthreads,
1634 jiffies + (j ? 3 * j : 2));
1635 }
1636 trace_rcu_grace_period(rcu_state.name,
1637 READ_ONCE(rcu_state.gp_seq),
1638 TPS("fqswait"));
1639 rcu_state.gp_state = RCU_GP_WAIT_FQS;
1640 ret = swait_event_idle_timeout_exclusive(
1641 rcu_state.gp_wq, rcu_gp_fqs_check_wake(&gf), j);
1642 rcu_state.gp_state = RCU_GP_DOING_FQS;
1643
1644
1645 if (!READ_ONCE(rnp->qsmask) &&
1646 !rcu_preempt_blocked_readers_cgp(rnp))
1647 break;
1648
1649 if (ULONG_CMP_GE(jiffies, rcu_state.jiffies_force_qs) ||
1650 (gf & RCU_GP_FLAG_FQS)) {
1651 trace_rcu_grace_period(rcu_state.name,
1652 READ_ONCE(rcu_state.gp_seq),
1653 TPS("fqsstart"));
1654 rcu_gp_fqs(first_gp_fqs);
1655 first_gp_fqs = false;
1656 trace_rcu_grace_period(rcu_state.name,
1657 READ_ONCE(rcu_state.gp_seq),
1658 TPS("fqsend"));
1659 cond_resched_tasks_rcu_qs();
1660 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1661 ret = 0;
1662 j = READ_ONCE(jiffies_till_next_fqs);
1663 } else {
1664
1665 cond_resched_tasks_rcu_qs();
1666 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1667 WARN_ON(signal_pending(current));
1668 trace_rcu_grace_period(rcu_state.name,
1669 READ_ONCE(rcu_state.gp_seq),
1670 TPS("fqswaitsig"));
1671 ret = 1;
1672 j = jiffies;
1673 if (time_after(jiffies, rcu_state.jiffies_force_qs))
1674 j = 1;
1675 else
1676 j = rcu_state.jiffies_force_qs - j;
1677 }
1678 }
1679 }
1680
1681
1682
1683
1684 static void rcu_gp_cleanup(void)
1685 {
1686 unsigned long gp_duration;
1687 bool needgp = false;
1688 unsigned long new_gp_seq;
1689 bool offloaded;
1690 struct rcu_data *rdp;
1691 struct rcu_node *rnp = rcu_get_root();
1692 struct swait_queue_head *sq;
1693
1694 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1695 raw_spin_lock_irq_rcu_node(rnp);
1696 rcu_state.gp_end = jiffies;
1697 gp_duration = rcu_state.gp_end - rcu_state.gp_start;
1698 if (gp_duration > rcu_state.gp_max)
1699 rcu_state.gp_max = gp_duration;
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709 raw_spin_unlock_irq_rcu_node(rnp);
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720 new_gp_seq = rcu_state.gp_seq;
1721 rcu_seq_end(&new_gp_seq);
1722 rcu_for_each_node_breadth_first(rnp) {
1723 raw_spin_lock_irq_rcu_node(rnp);
1724 if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
1725 dump_blkd_tasks(rnp, 10);
1726 WARN_ON_ONCE(rnp->qsmask);
1727 WRITE_ONCE(rnp->gp_seq, new_gp_seq);
1728 rdp = this_cpu_ptr(&rcu_data);
1729 if (rnp == rdp->mynode)
1730 needgp = __note_gp_changes(rnp, rdp) || needgp;
1731
1732 needgp = rcu_future_gp_cleanup(rnp) || needgp;
1733 sq = rcu_nocb_gp_get(rnp);
1734 raw_spin_unlock_irq_rcu_node(rnp);
1735 rcu_nocb_gp_cleanup(sq);
1736 cond_resched_tasks_rcu_qs();
1737 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1738 rcu_gp_slow(gp_cleanup_delay);
1739 }
1740 rnp = rcu_get_root();
1741 raw_spin_lock_irq_rcu_node(rnp);
1742
1743
1744 trace_rcu_grace_period(rcu_state.name, rcu_state.gp_seq, TPS("end"));
1745 rcu_seq_end(&rcu_state.gp_seq);
1746 rcu_state.gp_state = RCU_GP_IDLE;
1747
1748 rdp = this_cpu_ptr(&rcu_data);
1749 if (!needgp && ULONG_CMP_LT(rnp->gp_seq, rnp->gp_seq_needed)) {
1750 trace_rcu_this_gp(rnp, rdp, rnp->gp_seq_needed,
1751 TPS("CleanupMore"));
1752 needgp = true;
1753 }
1754
1755 offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1756 rcu_segcblist_is_offloaded(&rdp->cblist);
1757 if ((offloaded || !rcu_accelerate_cbs(rnp, rdp)) && needgp) {
1758 WRITE_ONCE(rcu_state.gp_flags, RCU_GP_FLAG_INIT);
1759 rcu_state.gp_req_activity = jiffies;
1760 trace_rcu_grace_period(rcu_state.name,
1761 READ_ONCE(rcu_state.gp_seq),
1762 TPS("newreq"));
1763 } else {
1764 WRITE_ONCE(rcu_state.gp_flags,
1765 rcu_state.gp_flags & RCU_GP_FLAG_INIT);
1766 }
1767 raw_spin_unlock_irq_rcu_node(rnp);
1768 }
1769
1770
1771
1772
1773 static int __noreturn rcu_gp_kthread(void *unused)
1774 {
1775 rcu_bind_gp_kthread();
1776 for (;;) {
1777
1778
1779 for (;;) {
1780 trace_rcu_grace_period(rcu_state.name,
1781 READ_ONCE(rcu_state.gp_seq),
1782 TPS("reqwait"));
1783 rcu_state.gp_state = RCU_GP_WAIT_GPS;
1784 swait_event_idle_exclusive(rcu_state.gp_wq,
1785 READ_ONCE(rcu_state.gp_flags) &
1786 RCU_GP_FLAG_INIT);
1787 rcu_state.gp_state = RCU_GP_DONE_GPS;
1788
1789 if (rcu_gp_init())
1790 break;
1791 cond_resched_tasks_rcu_qs();
1792 WRITE_ONCE(rcu_state.gp_activity, jiffies);
1793 WARN_ON(signal_pending(current));
1794 trace_rcu_grace_period(rcu_state.name,
1795 READ_ONCE(rcu_state.gp_seq),
1796 TPS("reqwaitsig"));
1797 }
1798
1799
1800 rcu_gp_fqs_loop();
1801
1802
1803 rcu_state.gp_state = RCU_GP_CLEANUP;
1804 rcu_gp_cleanup();
1805 rcu_state.gp_state = RCU_GP_CLEANED;
1806 }
1807 }
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818 static void rcu_report_qs_rsp(unsigned long flags)
1819 __releases(rcu_get_root()->lock)
1820 {
1821 raw_lockdep_assert_held_rcu_node(rcu_get_root());
1822 WARN_ON_ONCE(!rcu_gp_in_progress());
1823 WRITE_ONCE(rcu_state.gp_flags,
1824 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
1825 raw_spin_unlock_irqrestore_rcu_node(rcu_get_root(), flags);
1826 rcu_gp_kthread_wake();
1827 }
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843 static void rcu_report_qs_rnp(unsigned long mask, struct rcu_node *rnp,
1844 unsigned long gps, unsigned long flags)
1845 __releases(rnp->lock)
1846 {
1847 unsigned long oldmask = 0;
1848 struct rcu_node *rnp_c;
1849
1850 raw_lockdep_assert_held_rcu_node(rnp);
1851
1852
1853 for (;;) {
1854 if ((!(rnp->qsmask & mask) && mask) || rnp->gp_seq != gps) {
1855
1856
1857
1858
1859
1860 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1861 return;
1862 }
1863 WARN_ON_ONCE(oldmask);
1864 WARN_ON_ONCE(!rcu_is_leaf_node(rnp) &&
1865 rcu_preempt_blocked_readers_cgp(rnp));
1866 rnp->qsmask &= ~mask;
1867 trace_rcu_quiescent_state_report(rcu_state.name, rnp->gp_seq,
1868 mask, rnp->qsmask, rnp->level,
1869 rnp->grplo, rnp->grphi,
1870 !!rnp->gp_tasks);
1871 if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
1872
1873
1874 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1875 return;
1876 }
1877 rnp->completedqs = rnp->gp_seq;
1878 mask = rnp->grpmask;
1879 if (rnp->parent == NULL) {
1880
1881
1882
1883 break;
1884 }
1885 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1886 rnp_c = rnp;
1887 rnp = rnp->parent;
1888 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1889 oldmask = rnp_c->qsmask;
1890 }
1891
1892
1893
1894
1895
1896
1897 rcu_report_qs_rsp(flags);
1898 }
1899
1900
1901
1902
1903
1904
1905
1906
1907 static void __maybe_unused
1908 rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
1909 __releases(rnp->lock)
1910 {
1911 unsigned long gps;
1912 unsigned long mask;
1913 struct rcu_node *rnp_p;
1914
1915 raw_lockdep_assert_held_rcu_node(rnp);
1916 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPTION)) ||
1917 WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)) ||
1918 rnp->qsmask != 0) {
1919 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1920 return;
1921 }
1922
1923 rnp->completedqs = rnp->gp_seq;
1924 rnp_p = rnp->parent;
1925 if (rnp_p == NULL) {
1926
1927
1928
1929
1930 rcu_report_qs_rsp(flags);
1931 return;
1932 }
1933
1934
1935 gps = rnp->gp_seq;
1936 mask = rnp->grpmask;
1937 raw_spin_unlock_rcu_node(rnp);
1938 raw_spin_lock_rcu_node(rnp_p);
1939 rcu_report_qs_rnp(mask, rnp_p, gps, flags);
1940 }
1941
1942
1943
1944
1945
1946 static void
1947 rcu_report_qs_rdp(int cpu, struct rcu_data *rdp)
1948 {
1949 unsigned long flags;
1950 unsigned long mask;
1951 bool needwake = false;
1952 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
1953 rcu_segcblist_is_offloaded(&rdp->cblist);
1954 struct rcu_node *rnp;
1955
1956 rnp = rdp->mynode;
1957 raw_spin_lock_irqsave_rcu_node(rnp, flags);
1958 if (rdp->cpu_no_qs.b.norm || rdp->gp_seq != rnp->gp_seq ||
1959 rdp->gpwrap) {
1960
1961
1962
1963
1964
1965
1966
1967 rdp->cpu_no_qs.b.norm = true;
1968 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1969 return;
1970 }
1971 mask = rdp->grpmask;
1972 rdp->core_needs_qs = false;
1973 if ((rnp->qsmask & mask) == 0) {
1974 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1975 } else {
1976
1977
1978
1979
1980 if (!offloaded)
1981 needwake = rcu_accelerate_cbs(rnp, rdp);
1982
1983 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
1984
1985 if (needwake)
1986 rcu_gp_kthread_wake();
1987 }
1988 }
1989
1990
1991
1992
1993
1994
1995
1996 static void
1997 rcu_check_quiescent_state(struct rcu_data *rdp)
1998 {
1999
2000 note_gp_changes(rdp);
2001
2002
2003
2004
2005
2006 if (!rdp->core_needs_qs)
2007 return;
2008
2009
2010
2011
2012
2013 if (rdp->cpu_no_qs.b.norm)
2014 return;
2015
2016
2017
2018
2019
2020 rcu_report_qs_rdp(rdp->cpu, rdp);
2021 }
2022
2023
2024
2025
2026
2027 int rcutree_dying_cpu(unsigned int cpu)
2028 {
2029 bool blkd;
2030 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2031 struct rcu_node *rnp = rdp->mynode;
2032
2033 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2034 return 0;
2035
2036 blkd = !!(rnp->qsmask & rdp->grpmask);
2037 trace_rcu_grace_period(rcu_state.name, rnp->gp_seq,
2038 blkd ? TPS("cpuofl") : TPS("cpuofl-bgp"));
2039 return 0;
2040 }
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059 static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
2060 {
2061 long mask;
2062 struct rcu_node *rnp = rnp_leaf;
2063
2064 raw_lockdep_assert_held_rcu_node(rnp_leaf);
2065 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
2066 WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
2067 WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
2068 return;
2069 for (;;) {
2070 mask = rnp->grpmask;
2071 rnp = rnp->parent;
2072 if (!rnp)
2073 break;
2074 raw_spin_lock_rcu_node(rnp);
2075 rnp->qsmaskinit &= ~mask;
2076
2077 WARN_ON_ONCE(rnp->qsmask);
2078 if (rnp->qsmaskinit) {
2079 raw_spin_unlock_rcu_node(rnp);
2080
2081 return;
2082 }
2083 raw_spin_unlock_rcu_node(rnp);
2084 }
2085 }
2086
2087
2088
2089
2090
2091
2092
2093 int rcutree_dead_cpu(unsigned int cpu)
2094 {
2095 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
2096 struct rcu_node *rnp = rdp->mynode;
2097
2098 if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
2099 return 0;
2100
2101
2102 rcu_boost_kthread_setaffinity(rnp, -1);
2103
2104 do_nocb_deferred_wakeup(per_cpu_ptr(&rcu_data, cpu));
2105 return 0;
2106 }
2107
2108
2109
2110
2111
2112 static void rcu_do_batch(struct rcu_data *rdp)
2113 {
2114 unsigned long flags;
2115 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2116 rcu_segcblist_is_offloaded(&rdp->cblist);
2117 struct rcu_head *rhp;
2118 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
2119 long bl, count;
2120 long pending, tlimit = 0;
2121
2122
2123 if (!rcu_segcblist_ready_cbs(&rdp->cblist)) {
2124 trace_rcu_batch_start(rcu_state.name,
2125 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2126 rcu_segcblist_n_cbs(&rdp->cblist), 0);
2127 trace_rcu_batch_end(rcu_state.name, 0,
2128 !rcu_segcblist_empty(&rdp->cblist),
2129 need_resched(), is_idle_task(current),
2130 rcu_is_callbacks_kthread());
2131 return;
2132 }
2133
2134
2135
2136
2137
2138
2139 local_irq_save(flags);
2140 rcu_nocb_lock(rdp);
2141 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
2142 pending = rcu_segcblist_n_cbs(&rdp->cblist);
2143 bl = max(rdp->blimit, pending >> rcu_divisor);
2144 if (unlikely(bl > 100))
2145 tlimit = local_clock() + rcu_resched_ns;
2146 trace_rcu_batch_start(rcu_state.name,
2147 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2148 rcu_segcblist_n_cbs(&rdp->cblist), bl);
2149 rcu_segcblist_extract_done_cbs(&rdp->cblist, &rcl);
2150 if (offloaded)
2151 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
2152 rcu_nocb_unlock_irqrestore(rdp, flags);
2153
2154
2155 rhp = rcu_cblist_dequeue(&rcl);
2156 for (; rhp; rhp = rcu_cblist_dequeue(&rcl)) {
2157 debug_rcu_head_unqueue(rhp);
2158 if (__rcu_reclaim(rcu_state.name, rhp))
2159 rcu_cblist_dequeued_lazy(&rcl);
2160
2161
2162
2163
2164 if (-rcl.len >= bl && !offloaded &&
2165 (need_resched() ||
2166 (!is_idle_task(current) && !rcu_is_callbacks_kthread())))
2167 break;
2168 if (unlikely(tlimit)) {
2169
2170 if (likely((-rcl.len & 31) || local_clock() < tlimit))
2171 continue;
2172
2173 break;
2174 }
2175 if (offloaded) {
2176 WARN_ON_ONCE(in_serving_softirq());
2177 local_bh_enable();
2178 lockdep_assert_irqs_enabled();
2179 cond_resched_tasks_rcu_qs();
2180 lockdep_assert_irqs_enabled();
2181 local_bh_disable();
2182 }
2183 }
2184
2185 local_irq_save(flags);
2186 rcu_nocb_lock(rdp);
2187 count = -rcl.len;
2188 trace_rcu_batch_end(rcu_state.name, count, !!rcl.head, need_resched(),
2189 is_idle_task(current), rcu_is_callbacks_kthread());
2190
2191
2192 rcu_segcblist_insert_done_cbs(&rdp->cblist, &rcl);
2193 smp_mb();
2194 rcu_segcblist_insert_count(&rdp->cblist, &rcl);
2195
2196
2197 count = rcu_segcblist_n_cbs(&rdp->cblist);
2198 if (rdp->blimit >= DEFAULT_MAX_RCU_BLIMIT && count <= qlowmark)
2199 rdp->blimit = blimit;
2200
2201
2202 if (count == 0 && rdp->qlen_last_fqs_check != 0) {
2203 rdp->qlen_last_fqs_check = 0;
2204 rdp->n_force_qs_snap = rcu_state.n_force_qs;
2205 } else if (count < rdp->qlen_last_fqs_check - qhimark)
2206 rdp->qlen_last_fqs_check = count;
2207
2208
2209
2210
2211
2212 WARN_ON_ONCE(count == 0 && !rcu_segcblist_empty(&rdp->cblist));
2213 WARN_ON_ONCE(!IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2214 count != 0 && rcu_segcblist_empty(&rdp->cblist));
2215
2216 rcu_nocb_unlock_irqrestore(rdp, flags);
2217
2218
2219 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist))
2220 invoke_rcu_core();
2221 }
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231 void rcu_sched_clock_irq(int user)
2232 {
2233 trace_rcu_utilization(TPS("Start scheduler-tick"));
2234 raw_cpu_inc(rcu_data.ticks_this_gp);
2235
2236 if (smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
2237
2238 if (!rcu_is_cpu_rrupt_from_idle() && !user) {
2239 set_tsk_need_resched(current);
2240 set_preempt_need_resched();
2241 }
2242 __this_cpu_write(rcu_data.rcu_urgent_qs, false);
2243 }
2244 rcu_flavor_sched_clock_irq(user);
2245 if (rcu_pending())
2246 invoke_rcu_core();
2247
2248 trace_rcu_utilization(TPS("End scheduler-tick"));
2249 }
2250
2251
2252
2253
2254
2255
2256
2257
2258 static void force_qs_rnp(int (*f)(struct rcu_data *rdp))
2259 {
2260 int cpu;
2261 unsigned long flags;
2262 unsigned long mask;
2263 struct rcu_node *rnp;
2264
2265 rcu_for_each_leaf_node(rnp) {
2266 cond_resched_tasks_rcu_qs();
2267 mask = 0;
2268 raw_spin_lock_irqsave_rcu_node(rnp, flags);
2269 if (rnp->qsmask == 0) {
2270 if (!IS_ENABLED(CONFIG_PREEMPTION) ||
2271 rcu_preempt_blocked_readers_cgp(rnp)) {
2272
2273
2274
2275
2276
2277 rcu_initiate_boost(rnp, flags);
2278
2279 continue;
2280 }
2281 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2282 continue;
2283 }
2284 for_each_leaf_node_possible_cpu(rnp, cpu) {
2285 unsigned long bit = leaf_node_cpu_bit(rnp, cpu);
2286 if ((rnp->qsmask & bit) != 0) {
2287 if (f(per_cpu_ptr(&rcu_data, cpu)))
2288 mask |= bit;
2289 }
2290 }
2291 if (mask != 0) {
2292
2293 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
2294 } else {
2295
2296 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
2297 }
2298 }
2299 }
2300
2301
2302
2303
2304
2305 void rcu_force_quiescent_state(void)
2306 {
2307 unsigned long flags;
2308 bool ret;
2309 struct rcu_node *rnp;
2310 struct rcu_node *rnp_old = NULL;
2311
2312
2313 rnp = __this_cpu_read(rcu_data.mynode);
2314 for (; rnp != NULL; rnp = rnp->parent) {
2315 ret = (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) ||
2316 !raw_spin_trylock(&rnp->fqslock);
2317 if (rnp_old != NULL)
2318 raw_spin_unlock(&rnp_old->fqslock);
2319 if (ret)
2320 return;
2321 rnp_old = rnp;
2322 }
2323
2324
2325
2326 raw_spin_lock_irqsave_rcu_node(rnp_old, flags);
2327 raw_spin_unlock(&rnp_old->fqslock);
2328 if (READ_ONCE(rcu_state.gp_flags) & RCU_GP_FLAG_FQS) {
2329 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
2330 return;
2331 }
2332 WRITE_ONCE(rcu_state.gp_flags,
2333 READ_ONCE(rcu_state.gp_flags) | RCU_GP_FLAG_FQS);
2334 raw_spin_unlock_irqrestore_rcu_node(rnp_old, flags);
2335 rcu_gp_kthread_wake();
2336 }
2337 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
2338
2339
2340 static __latent_entropy void rcu_core(void)
2341 {
2342 unsigned long flags;
2343 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
2344 struct rcu_node *rnp = rdp->mynode;
2345 const bool offloaded = IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2346 rcu_segcblist_is_offloaded(&rdp->cblist);
2347
2348 if (cpu_is_offline(smp_processor_id()))
2349 return;
2350 trace_rcu_utilization(TPS("Start RCU core"));
2351 WARN_ON_ONCE(!rdp->beenonline);
2352
2353
2354 if (!(preempt_count() & PREEMPT_MASK)) {
2355 rcu_preempt_deferred_qs(current);
2356 } else if (rcu_preempt_need_deferred_qs(current)) {
2357 set_tsk_need_resched(current);
2358 set_preempt_need_resched();
2359 }
2360
2361
2362 rcu_check_quiescent_state(rdp);
2363
2364
2365 if (!rcu_gp_in_progress() &&
2366 rcu_segcblist_is_enabled(&rdp->cblist) && !offloaded) {
2367 local_irq_save(flags);
2368 if (!rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
2369 rcu_accelerate_cbs_unlocked(rnp, rdp);
2370 local_irq_restore(flags);
2371 }
2372
2373 rcu_check_gp_start_stall(rnp, rdp, rcu_jiffies_till_stall_check());
2374
2375
2376 if (!offloaded && rcu_segcblist_ready_cbs(&rdp->cblist) &&
2377 likely(READ_ONCE(rcu_scheduler_fully_active)))
2378 rcu_do_batch(rdp);
2379
2380
2381 do_nocb_deferred_wakeup(rdp);
2382 trace_rcu_utilization(TPS("End RCU core"));
2383 }
2384
2385 static void rcu_core_si(struct softirq_action *h)
2386 {
2387 rcu_core();
2388 }
2389
2390 static void rcu_wake_cond(struct task_struct *t, int status)
2391 {
2392
2393
2394
2395
2396 if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
2397 wake_up_process(t);
2398 }
2399
2400 static void invoke_rcu_core_kthread(void)
2401 {
2402 struct task_struct *t;
2403 unsigned long flags;
2404
2405 local_irq_save(flags);
2406 __this_cpu_write(rcu_data.rcu_cpu_has_work, 1);
2407 t = __this_cpu_read(rcu_data.rcu_cpu_kthread_task);
2408 if (t != NULL && t != current)
2409 rcu_wake_cond(t, __this_cpu_read(rcu_data.rcu_cpu_kthread_status));
2410 local_irq_restore(flags);
2411 }
2412
2413
2414
2415
2416 static void invoke_rcu_core(void)
2417 {
2418 if (!cpu_online(smp_processor_id()))
2419 return;
2420 if (use_softirq)
2421 raise_softirq(RCU_SOFTIRQ);
2422 else
2423 invoke_rcu_core_kthread();
2424 }
2425
2426 static void rcu_cpu_kthread_park(unsigned int cpu)
2427 {
2428 per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
2429 }
2430
2431 static int rcu_cpu_kthread_should_run(unsigned int cpu)
2432 {
2433 return __this_cpu_read(rcu_data.rcu_cpu_has_work);
2434 }
2435
2436
2437
2438
2439
2440
2441 static void rcu_cpu_kthread(unsigned int cpu)
2442 {
2443 unsigned int *statusp = this_cpu_ptr(&rcu_data.rcu_cpu_kthread_status);
2444 char work, *workp = this_cpu_ptr(&rcu_data.rcu_cpu_has_work);
2445 int spincnt;
2446
2447 for (spincnt = 0; spincnt < 10; spincnt++) {
2448 trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
2449 local_bh_disable();
2450 *statusp = RCU_KTHREAD_RUNNING;
2451 local_irq_disable();
2452 work = *workp;
2453 *workp = 0;
2454 local_irq_enable();
2455 if (work)
2456 rcu_core();
2457 local_bh_enable();
2458 if (*workp == 0) {
2459 trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
2460 *statusp = RCU_KTHREAD_WAITING;
2461 return;
2462 }
2463 }
2464 *statusp = RCU_KTHREAD_YIELDING;
2465 trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
2466 schedule_timeout_interruptible(2);
2467 trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
2468 *statusp = RCU_KTHREAD_WAITING;
2469 }
2470
2471 static struct smp_hotplug_thread rcu_cpu_thread_spec = {
2472 .store = &rcu_data.rcu_cpu_kthread_task,
2473 .thread_should_run = rcu_cpu_kthread_should_run,
2474 .thread_fn = rcu_cpu_kthread,
2475 .thread_comm = "rcuc/%u",
2476 .setup = rcu_cpu_kthread_setup,
2477 .park = rcu_cpu_kthread_park,
2478 };
2479
2480
2481
2482
2483 static int __init rcu_spawn_core_kthreads(void)
2484 {
2485 int cpu;
2486
2487 for_each_possible_cpu(cpu)
2488 per_cpu(rcu_data.rcu_cpu_has_work, cpu) = 0;
2489 if (!IS_ENABLED(CONFIG_RCU_BOOST) && use_softirq)
2490 return 0;
2491 WARN_ONCE(smpboot_register_percpu_thread(&rcu_cpu_thread_spec),
2492 "%s: Could not start rcuc kthread, OOM is now expected behavior\n", __func__);
2493 return 0;
2494 }
2495 early_initcall(rcu_spawn_core_kthreads);
2496
2497
2498
2499
2500 static void __call_rcu_core(struct rcu_data *rdp, struct rcu_head *head,
2501 unsigned long flags)
2502 {
2503
2504
2505
2506
2507 if (!rcu_is_watching())
2508 invoke_rcu_core();
2509
2510
2511 if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id()))
2512 return;
2513
2514
2515
2516
2517
2518
2519
2520
2521 if (unlikely(rcu_segcblist_n_cbs(&rdp->cblist) >
2522 rdp->qlen_last_fqs_check + qhimark)) {
2523
2524
2525 note_gp_changes(rdp);
2526
2527
2528 if (!rcu_gp_in_progress()) {
2529 rcu_accelerate_cbs_unlocked(rdp->mynode, rdp);
2530 } else {
2531
2532 rdp->blimit = DEFAULT_MAX_RCU_BLIMIT;
2533 if (rcu_state.n_force_qs == rdp->n_force_qs_snap &&
2534 rcu_segcblist_first_pend_cb(&rdp->cblist) != head)
2535 rcu_force_quiescent_state();
2536 rdp->n_force_qs_snap = rcu_state.n_force_qs;
2537 rdp->qlen_last_fqs_check = rcu_segcblist_n_cbs(&rdp->cblist);
2538 }
2539 }
2540 }
2541
2542
2543
2544
2545 static void rcu_leak_callback(struct rcu_head *rhp)
2546 {
2547 }
2548
2549
2550
2551
2552
2553
2554
2555 static void
2556 __call_rcu(struct rcu_head *head, rcu_callback_t func, bool lazy)
2557 {
2558 unsigned long flags;
2559 struct rcu_data *rdp;
2560 bool was_alldone;
2561
2562
2563 WARN_ON_ONCE((unsigned long)head & (sizeof(void *) - 1));
2564
2565 if (debug_rcu_head_queue(head)) {
2566
2567
2568
2569
2570
2571 WARN_ONCE(1, "__call_rcu(): Double-freed CB %p->%pS()!!!\n",
2572 head, head->func);
2573 WRITE_ONCE(head->func, rcu_leak_callback);
2574 return;
2575 }
2576 head->func = func;
2577 head->next = NULL;
2578 local_irq_save(flags);
2579 rdp = this_cpu_ptr(&rcu_data);
2580
2581
2582 if (unlikely(!rcu_segcblist_is_enabled(&rdp->cblist))) {
2583
2584 WARN_ON_ONCE(rcu_scheduler_active != RCU_SCHEDULER_INACTIVE);
2585 WARN_ON_ONCE(!rcu_is_watching());
2586
2587
2588 if (rcu_segcblist_empty(&rdp->cblist))
2589 rcu_segcblist_init(&rdp->cblist);
2590 }
2591 if (rcu_nocb_try_bypass(rdp, head, &was_alldone, flags))
2592 return;
2593
2594 rcu_segcblist_enqueue(&rdp->cblist, head, lazy);
2595 if (__is_kfree_rcu_offset((unsigned long)func))
2596 trace_rcu_kfree_callback(rcu_state.name, head,
2597 (unsigned long)func,
2598 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2599 rcu_segcblist_n_cbs(&rdp->cblist));
2600 else
2601 trace_rcu_callback(rcu_state.name, head,
2602 rcu_segcblist_n_lazy_cbs(&rdp->cblist),
2603 rcu_segcblist_n_cbs(&rdp->cblist));
2604
2605
2606 if (IS_ENABLED(CONFIG_RCU_NOCB_CPU) &&
2607 unlikely(rcu_segcblist_is_offloaded(&rdp->cblist))) {
2608 __call_rcu_nocb_wake(rdp, was_alldone, flags);
2609 } else {
2610 __call_rcu_core(rdp, head, flags);
2611 local_irq_restore(flags);
2612 }
2613 }
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650 void call_rcu(struct rcu_head *head, rcu_callback_t func)
2651 {
2652 __call_rcu(head, func, 0);
2653 }
2654 EXPORT_SYMBOL_GPL(call_rcu);
2655
2656
2657
2658
2659
2660
2661
2662
2663 void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func)
2664 {
2665 __call_rcu(head, func, 1);
2666 }
2667 EXPORT_SYMBOL_GPL(kfree_call_rcu);
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681 static int rcu_blocking_is_gp(void)
2682 {
2683 int ret;
2684
2685 if (IS_ENABLED(CONFIG_PREEMPTION))
2686 return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE;
2687 might_sleep();
2688 preempt_disable();
2689 ret = num_online_cpus() <= 1;
2690 preempt_enable();
2691 return ret;
2692 }
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727 void synchronize_rcu(void)
2728 {
2729 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
2730 lock_is_held(&rcu_lock_map) ||
2731 lock_is_held(&rcu_sched_lock_map),
2732 "Illegal synchronize_rcu() in RCU read-side critical section");
2733 if (rcu_blocking_is_gp())
2734 return;
2735 if (rcu_gp_is_expedited())
2736 synchronize_rcu_expedited();
2737 else
2738 wait_rcu_gp(call_rcu);
2739 }
2740 EXPORT_SYMBOL_GPL(synchronize_rcu);
2741
2742
2743
2744
2745
2746
2747
2748
2749 unsigned long get_state_synchronize_rcu(void)
2750 {
2751
2752
2753
2754
2755 smp_mb();
2756 return rcu_seq_snap(&rcu_state.gp_seq);
2757 }
2758 EXPORT_SYMBOL_GPL(get_state_synchronize_rcu);
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774 void cond_synchronize_rcu(unsigned long oldstate)
2775 {
2776 if (!rcu_seq_done(&rcu_state.gp_seq, oldstate))
2777 synchronize_rcu();
2778 else
2779 smp_mb();
2780 }
2781 EXPORT_SYMBOL_GPL(cond_synchronize_rcu);
2782
2783
2784
2785
2786
2787
2788
2789
2790 static int rcu_pending(void)
2791 {
2792 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
2793 struct rcu_node *rnp = rdp->mynode;
2794
2795
2796 check_cpu_stall(rdp);
2797
2798
2799 if (rcu_nocb_need_deferred_wakeup(rdp))
2800 return 1;
2801
2802
2803 if (rcu_nohz_full_cpu())
2804 return 0;
2805
2806
2807 if (rdp->core_needs_qs && !rdp->cpu_no_qs.b.norm)
2808 return 1;
2809
2810
2811 if (rcu_segcblist_ready_cbs(&rdp->cblist))
2812 return 1;
2813
2814
2815 if (!rcu_gp_in_progress() &&
2816 rcu_segcblist_is_enabled(&rdp->cblist) &&
2817 (!IS_ENABLED(CONFIG_RCU_NOCB_CPU) ||
2818 !rcu_segcblist_is_offloaded(&rdp->cblist)) &&
2819 !rcu_segcblist_restempty(&rdp->cblist, RCU_NEXT_READY_TAIL))
2820 return 1;
2821
2822
2823 if (rcu_seq_current(&rnp->gp_seq) != rdp->gp_seq ||
2824 unlikely(READ_ONCE(rdp->gpwrap)))
2825 return 1;
2826
2827
2828 return 0;
2829 }
2830
2831
2832
2833
2834
2835 static void rcu_barrier_trace(const char *s, int cpu, unsigned long done)
2836 {
2837 trace_rcu_barrier(rcu_state.name, s, cpu,
2838 atomic_read(&rcu_state.barrier_cpu_count), done);
2839 }
2840
2841
2842
2843
2844
2845 static void rcu_barrier_callback(struct rcu_head *rhp)
2846 {
2847 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count)) {
2848 rcu_barrier_trace(TPS("LastCB"), -1,
2849 rcu_state.barrier_sequence);
2850 complete(&rcu_state.barrier_completion);
2851 } else {
2852 rcu_barrier_trace(TPS("CB"), -1, rcu_state.barrier_sequence);
2853 }
2854 }
2855
2856
2857
2858
2859 static void rcu_barrier_func(void *unused)
2860 {
2861 struct rcu_data *rdp = raw_cpu_ptr(&rcu_data);
2862
2863 rcu_barrier_trace(TPS("IRQ"), -1, rcu_state.barrier_sequence);
2864 rdp->barrier_head.func = rcu_barrier_callback;
2865 debug_rcu_head_queue(&rdp->barrier_head);
2866 rcu_nocb_lock(rdp);
2867 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
2868 if (rcu_segcblist_entrain(&rdp->cblist, &rdp->barrier_head, 0)) {
2869 atomic_inc(&rcu_state.barrier_cpu_count);
2870 } else {
2871 debug_rcu_head_unqueue(&rdp->barrier_head);
2872 rcu_barrier_trace(TPS("IRQNQ"), -1,
2873 rcu_state.barrier_sequence);
2874 }
2875 rcu_nocb_unlock(rdp);
2876 }
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886 void rcu_barrier(void)
2887 {
2888 int cpu;
2889 struct rcu_data *rdp;
2890 unsigned long s = rcu_seq_snap(&rcu_state.barrier_sequence);
2891
2892 rcu_barrier_trace(TPS("Begin"), -1, s);
2893
2894
2895 mutex_lock(&rcu_state.barrier_mutex);
2896
2897
2898 if (rcu_seq_done(&rcu_state.barrier_sequence, s)) {
2899 rcu_barrier_trace(TPS("EarlyExit"), -1,
2900 rcu_state.barrier_sequence);
2901 smp_mb();
2902 mutex_unlock(&rcu_state.barrier_mutex);
2903 return;
2904 }
2905
2906
2907 rcu_seq_start(&rcu_state.barrier_sequence);
2908 rcu_barrier_trace(TPS("Inc1"), -1, rcu_state.barrier_sequence);
2909
2910
2911
2912
2913
2914
2915
2916 init_completion(&rcu_state.barrier_completion);
2917 atomic_set(&rcu_state.barrier_cpu_count, 1);
2918 get_online_cpus();
2919
2920
2921
2922
2923
2924
2925 for_each_possible_cpu(cpu) {
2926 rdp = per_cpu_ptr(&rcu_data, cpu);
2927 if (!cpu_online(cpu) &&
2928 !rcu_segcblist_is_offloaded(&rdp->cblist))
2929 continue;
2930 if (rcu_segcblist_n_cbs(&rdp->cblist)) {
2931 rcu_barrier_trace(TPS("OnlineQ"), cpu,
2932 rcu_state.barrier_sequence);
2933 smp_call_function_single(cpu, rcu_barrier_func, NULL, 1);
2934 } else {
2935 rcu_barrier_trace(TPS("OnlineNQ"), cpu,
2936 rcu_state.barrier_sequence);
2937 }
2938 }
2939 put_online_cpus();
2940
2941
2942
2943
2944
2945 if (atomic_dec_and_test(&rcu_state.barrier_cpu_count))
2946 complete(&rcu_state.barrier_completion);
2947
2948
2949 wait_for_completion(&rcu_state.barrier_completion);
2950
2951
2952 rcu_barrier_trace(TPS("Inc2"), -1, rcu_state.barrier_sequence);
2953 rcu_seq_end(&rcu_state.barrier_sequence);
2954
2955
2956 mutex_unlock(&rcu_state.barrier_mutex);
2957 }
2958 EXPORT_SYMBOL_GPL(rcu_barrier);
2959
2960
2961
2962
2963
2964
2965
2966 static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
2967 {
2968 long mask;
2969 long oldmask;
2970 struct rcu_node *rnp = rnp_leaf;
2971
2972 raw_lockdep_assert_held_rcu_node(rnp_leaf);
2973 WARN_ON_ONCE(rnp->wait_blkd_tasks);
2974 for (;;) {
2975 mask = rnp->grpmask;
2976 rnp = rnp->parent;
2977 if (rnp == NULL)
2978 return;
2979 raw_spin_lock_rcu_node(rnp);
2980 oldmask = rnp->qsmaskinit;
2981 rnp->qsmaskinit |= mask;
2982 raw_spin_unlock_rcu_node(rnp);
2983 if (oldmask)
2984 return;
2985 }
2986 }
2987
2988
2989
2990
2991 static void __init
2992 rcu_boot_init_percpu_data(int cpu)
2993 {
2994 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
2995
2996
2997 rdp->grpmask = leaf_node_cpu_bit(rdp->mynode, cpu);
2998 WARN_ON_ONCE(rdp->dynticks_nesting != 1);
2999 WARN_ON_ONCE(rcu_dynticks_in_eqs(rcu_dynticks_snap(rdp)));
3000 rdp->rcu_ofl_gp_seq = rcu_state.gp_seq;
3001 rdp->rcu_ofl_gp_flags = RCU_GP_CLEANED;
3002 rdp->rcu_onl_gp_seq = rcu_state.gp_seq;
3003 rdp->rcu_onl_gp_flags = RCU_GP_CLEANED;
3004 rdp->cpu = cpu;
3005 rcu_boot_init_nocb_percpu_data(rdp);
3006 }
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018 int rcutree_prepare_cpu(unsigned int cpu)
3019 {
3020 unsigned long flags;
3021 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3022 struct rcu_node *rnp = rcu_get_root();
3023
3024
3025 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3026 rdp->qlen_last_fqs_check = 0;
3027 rdp->n_force_qs_snap = rcu_state.n_force_qs;
3028 rdp->blimit = blimit;
3029 if (rcu_segcblist_empty(&rdp->cblist) &&
3030 !rcu_segcblist_is_offloaded(&rdp->cblist))
3031 rcu_segcblist_init(&rdp->cblist);
3032 rdp->dynticks_nesting = 1;
3033 rcu_dynticks_eqs_online();
3034 raw_spin_unlock_rcu_node(rnp);
3035
3036
3037
3038
3039
3040
3041 rnp = rdp->mynode;
3042 raw_spin_lock_rcu_node(rnp);
3043 rdp->beenonline = true;
3044 rdp->gp_seq = rnp->gp_seq;
3045 rdp->gp_seq_needed = rnp->gp_seq;
3046 rdp->cpu_no_qs.b.norm = true;
3047 rdp->core_needs_qs = false;
3048 rdp->rcu_iw_pending = false;
3049 rdp->rcu_iw_gp_seq = rnp->gp_seq - 1;
3050 trace_rcu_grace_period(rcu_state.name, rdp->gp_seq, TPS("cpuonl"));
3051 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3052 rcu_prepare_kthreads(cpu);
3053 rcu_spawn_cpu_nocb_kthread(cpu);
3054
3055 return 0;
3056 }
3057
3058
3059
3060
3061 static void rcutree_affinity_setting(unsigned int cpu, int outgoing)
3062 {
3063 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3064
3065 rcu_boost_kthread_setaffinity(rdp->mynode, outgoing);
3066 }
3067
3068
3069
3070
3071
3072 int rcutree_online_cpu(unsigned int cpu)
3073 {
3074 unsigned long flags;
3075 struct rcu_data *rdp;
3076 struct rcu_node *rnp;
3077
3078 rdp = per_cpu_ptr(&rcu_data, cpu);
3079 rnp = rdp->mynode;
3080 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3081 rnp->ffmask |= rdp->grpmask;
3082 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3083 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
3084 return 0;
3085 sync_sched_exp_online_cleanup(cpu);
3086 rcutree_affinity_setting(cpu, -1);
3087 return 0;
3088 }
3089
3090
3091
3092
3093
3094 int rcutree_offline_cpu(unsigned int cpu)
3095 {
3096 unsigned long flags;
3097 struct rcu_data *rdp;
3098 struct rcu_node *rnp;
3099
3100 rdp = per_cpu_ptr(&rcu_data, cpu);
3101 rnp = rdp->mynode;
3102 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3103 rnp->ffmask &= ~rdp->grpmask;
3104 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3105
3106 rcutree_affinity_setting(cpu, cpu);
3107 return 0;
3108 }
3109
3110 static DEFINE_PER_CPU(int, rcu_cpu_started);
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123 void rcu_cpu_starting(unsigned int cpu)
3124 {
3125 unsigned long flags;
3126 unsigned long mask;
3127 int nbits;
3128 unsigned long oldmask;
3129 struct rcu_data *rdp;
3130 struct rcu_node *rnp;
3131
3132 if (per_cpu(rcu_cpu_started, cpu))
3133 return;
3134
3135 per_cpu(rcu_cpu_started, cpu) = 1;
3136
3137 rdp = per_cpu_ptr(&rcu_data, cpu);
3138 rnp = rdp->mynode;
3139 mask = rdp->grpmask;
3140 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3141 rnp->qsmaskinitnext |= mask;
3142 oldmask = rnp->expmaskinitnext;
3143 rnp->expmaskinitnext |= mask;
3144 oldmask ^= rnp->expmaskinitnext;
3145 nbits = bitmap_weight(&oldmask, BITS_PER_LONG);
3146
3147 smp_store_release(&rcu_state.ncpus, rcu_state.ncpus + nbits);
3148 rcu_gpnum_ovf(rnp, rdp);
3149 rdp->rcu_onl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3150 rdp->rcu_onl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3151 if (rnp->qsmask & mask) {
3152
3153 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3154 } else {
3155 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3156 }
3157 smp_mb();
3158 }
3159
3160 #ifdef CONFIG_HOTPLUG_CPU
3161
3162
3163
3164
3165
3166
3167
3168
3169 void rcu_report_dead(unsigned int cpu)
3170 {
3171 unsigned long flags;
3172 unsigned long mask;
3173 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3174 struct rcu_node *rnp = rdp->mynode;
3175
3176
3177 preempt_disable();
3178 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
3179 preempt_enable();
3180 rcu_preempt_deferred_qs(current);
3181
3182
3183 mask = rdp->grpmask;
3184 raw_spin_lock(&rcu_state.ofl_lock);
3185 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3186 rdp->rcu_ofl_gp_seq = READ_ONCE(rcu_state.gp_seq);
3187 rdp->rcu_ofl_gp_flags = READ_ONCE(rcu_state.gp_flags);
3188 if (rnp->qsmask & mask) {
3189
3190 rcu_report_qs_rnp(mask, rnp, rnp->gp_seq, flags);
3191 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3192 }
3193 rnp->qsmaskinitnext &= ~mask;
3194 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3195 raw_spin_unlock(&rcu_state.ofl_lock);
3196
3197 per_cpu(rcu_cpu_started, cpu) = 0;
3198 }
3199
3200
3201
3202
3203
3204
3205 void rcutree_migrate_callbacks(int cpu)
3206 {
3207 unsigned long flags;
3208 struct rcu_data *my_rdp;
3209 struct rcu_node *my_rnp;
3210 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
3211 bool needwake;
3212
3213 if (rcu_segcblist_is_offloaded(&rdp->cblist) ||
3214 rcu_segcblist_empty(&rdp->cblist))
3215 return;
3216
3217 local_irq_save(flags);
3218 my_rdp = this_cpu_ptr(&rcu_data);
3219 my_rnp = my_rdp->mynode;
3220 rcu_nocb_lock(my_rdp);
3221 WARN_ON_ONCE(!rcu_nocb_flush_bypass(my_rdp, NULL, jiffies));
3222 raw_spin_lock_rcu_node(my_rnp);
3223
3224 needwake = rcu_advance_cbs(my_rnp, rdp) ||
3225 rcu_advance_cbs(my_rnp, my_rdp);
3226 rcu_segcblist_merge(&my_rdp->cblist, &rdp->cblist);
3227 needwake = needwake || rcu_advance_cbs(my_rnp, my_rdp);
3228 rcu_segcblist_disable(&rdp->cblist);
3229 WARN_ON_ONCE(rcu_segcblist_empty(&my_rdp->cblist) !=
3230 !rcu_segcblist_n_cbs(&my_rdp->cblist));
3231 if (rcu_segcblist_is_offloaded(&my_rdp->cblist)) {
3232 raw_spin_unlock_rcu_node(my_rnp);
3233 __call_rcu_nocb_wake(my_rdp, true, flags);
3234 } else {
3235 rcu_nocb_unlock(my_rdp);
3236 raw_spin_unlock_irqrestore_rcu_node(my_rnp, flags);
3237 }
3238 if (needwake)
3239 rcu_gp_kthread_wake();
3240 lockdep_assert_irqs_enabled();
3241 WARN_ONCE(rcu_segcblist_n_cbs(&rdp->cblist) != 0 ||
3242 !rcu_segcblist_empty(&rdp->cblist),
3243 "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, 1stCB=%p\n",
3244 cpu, rcu_segcblist_n_cbs(&rdp->cblist),
3245 rcu_segcblist_first_cb(&rdp->cblist));
3246 }
3247 #endif
3248
3249
3250
3251
3252
3253 static int rcu_pm_notify(struct notifier_block *self,
3254 unsigned long action, void *hcpu)
3255 {
3256 switch (action) {
3257 case PM_HIBERNATION_PREPARE:
3258 case PM_SUSPEND_PREPARE:
3259 rcu_expedite_gp();
3260 break;
3261 case PM_POST_HIBERNATION:
3262 case PM_POST_SUSPEND:
3263 rcu_unexpedite_gp();
3264 break;
3265 default:
3266 break;
3267 }
3268 return NOTIFY_OK;
3269 }
3270
3271
3272
3273
3274 static int __init rcu_spawn_gp_kthread(void)
3275 {
3276 unsigned long flags;
3277 int kthread_prio_in = kthread_prio;
3278 struct rcu_node *rnp;
3279 struct sched_param sp;
3280 struct task_struct *t;
3281
3282
3283 if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 2
3284 && IS_BUILTIN(CONFIG_RCU_TORTURE_TEST))
3285 kthread_prio = 2;
3286 else if (IS_ENABLED(CONFIG_RCU_BOOST) && kthread_prio < 1)
3287 kthread_prio = 1;
3288 else if (kthread_prio < 0)
3289 kthread_prio = 0;
3290 else if (kthread_prio > 99)
3291 kthread_prio = 99;
3292
3293 if (kthread_prio != kthread_prio_in)
3294 pr_alert("rcu_spawn_gp_kthread(): Limited prio to %d from %d\n",
3295 kthread_prio, kthread_prio_in);
3296
3297 rcu_scheduler_fully_active = 1;
3298 t = kthread_create(rcu_gp_kthread, NULL, "%s", rcu_state.name);
3299 if (WARN_ONCE(IS_ERR(t), "%s: Could not start grace-period kthread, OOM is now expected behavior\n", __func__))
3300 return 0;
3301 if (kthread_prio) {
3302 sp.sched_priority = kthread_prio;
3303 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
3304 }
3305 rnp = rcu_get_root();
3306 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3307 rcu_state.gp_kthread = t;
3308 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
3309 wake_up_process(t);
3310 rcu_spawn_nocb_kthreads();
3311 rcu_spawn_boost_kthreads();
3312 return 0;
3313 }
3314 early_initcall(rcu_spawn_gp_kthread);
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326 void rcu_scheduler_starting(void)
3327 {
3328 WARN_ON(num_online_cpus() != 1);
3329 WARN_ON(nr_context_switches() > 0);
3330 rcu_test_sync_prims();
3331 rcu_scheduler_active = RCU_SCHEDULER_INIT;
3332 rcu_test_sync_prims();
3333 }
3334
3335
3336
3337
3338 static void __init rcu_init_one(void)
3339 {
3340 static const char * const buf[] = RCU_NODE_NAME_INIT;
3341 static const char * const fqs[] = RCU_FQS_NAME_INIT;
3342 static struct lock_class_key rcu_node_class[RCU_NUM_LVLS];
3343 static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS];
3344
3345 int levelspread[RCU_NUM_LVLS];
3346 int cpustride = 1;
3347 int i;
3348 int j;
3349 struct rcu_node *rnp;
3350
3351 BUILD_BUG_ON(RCU_NUM_LVLS > ARRAY_SIZE(buf));
3352
3353
3354 if (rcu_num_lvls <= 0 || rcu_num_lvls > RCU_NUM_LVLS)
3355 panic("rcu_init_one: rcu_num_lvls out of range");
3356
3357
3358
3359 for (i = 1; i < rcu_num_lvls; i++)
3360 rcu_state.level[i] =
3361 rcu_state.level[i - 1] + num_rcu_lvl[i - 1];
3362 rcu_init_levelspread(levelspread, num_rcu_lvl);
3363
3364
3365
3366 for (i = rcu_num_lvls - 1; i >= 0; i--) {
3367 cpustride *= levelspread[i];
3368 rnp = rcu_state.level[i];
3369 for (j = 0; j < num_rcu_lvl[i]; j++, rnp++) {
3370 raw_spin_lock_init(&ACCESS_PRIVATE(rnp, lock));
3371 lockdep_set_class_and_name(&ACCESS_PRIVATE(rnp, lock),
3372 &rcu_node_class[i], buf[i]);
3373 raw_spin_lock_init(&rnp->fqslock);
3374 lockdep_set_class_and_name(&rnp->fqslock,
3375 &rcu_fqs_class[i], fqs[i]);
3376 rnp->gp_seq = rcu_state.gp_seq;
3377 rnp->gp_seq_needed = rcu_state.gp_seq;
3378 rnp->completedqs = rcu_state.gp_seq;
3379 rnp->qsmask = 0;
3380 rnp->qsmaskinit = 0;
3381 rnp->grplo = j * cpustride;
3382 rnp->grphi = (j + 1) * cpustride - 1;
3383 if (rnp->grphi >= nr_cpu_ids)
3384 rnp->grphi = nr_cpu_ids - 1;
3385 if (i == 0) {
3386 rnp->grpnum = 0;
3387 rnp->grpmask = 0;
3388 rnp->parent = NULL;
3389 } else {
3390 rnp->grpnum = j % levelspread[i - 1];
3391 rnp->grpmask = BIT(rnp->grpnum);
3392 rnp->parent = rcu_state.level[i - 1] +
3393 j / levelspread[i - 1];
3394 }
3395 rnp->level = i;
3396 INIT_LIST_HEAD(&rnp->blkd_tasks);
3397 rcu_init_one_nocb(rnp);
3398 init_waitqueue_head(&rnp->exp_wq[0]);
3399 init_waitqueue_head(&rnp->exp_wq[1]);
3400 init_waitqueue_head(&rnp->exp_wq[2]);
3401 init_waitqueue_head(&rnp->exp_wq[3]);
3402 spin_lock_init(&rnp->exp_lock);
3403 }
3404 }
3405
3406 init_swait_queue_head(&rcu_state.gp_wq);
3407 init_swait_queue_head(&rcu_state.expedited_wq);
3408 rnp = rcu_first_leaf_node();
3409 for_each_possible_cpu(i) {
3410 while (i > rnp->grphi)
3411 rnp++;
3412 per_cpu_ptr(&rcu_data, i)->mynode = rnp;
3413 rcu_boot_init_percpu_data(i);
3414 }
3415 }
3416
3417
3418
3419
3420
3421
3422 static void __init rcu_init_geometry(void)
3423 {
3424 ulong d;
3425 int i;
3426 int rcu_capacity[RCU_NUM_LVLS];
3427
3428
3429
3430
3431
3432
3433
3434
3435 d = RCU_JIFFIES_TILL_FORCE_QS + nr_cpu_ids / RCU_JIFFIES_FQS_DIV;
3436 if (jiffies_till_first_fqs == ULONG_MAX)
3437 jiffies_till_first_fqs = d;
3438 if (jiffies_till_next_fqs == ULONG_MAX)
3439 jiffies_till_next_fqs = d;
3440 adjust_jiffies_till_sched_qs();
3441
3442
3443 if (rcu_fanout_leaf == RCU_FANOUT_LEAF &&
3444 nr_cpu_ids == NR_CPUS)
3445 return;
3446 pr_info("Adjusting geometry for rcu_fanout_leaf=%d, nr_cpu_ids=%u\n",
3447 rcu_fanout_leaf, nr_cpu_ids);
3448
3449
3450
3451
3452
3453
3454
3455 if (rcu_fanout_leaf < 2 ||
3456 rcu_fanout_leaf > sizeof(unsigned long) * 8) {
3457 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3458 WARN_ON(1);
3459 return;
3460 }
3461
3462
3463
3464
3465
3466 rcu_capacity[0] = rcu_fanout_leaf;
3467 for (i = 1; i < RCU_NUM_LVLS; i++)
3468 rcu_capacity[i] = rcu_capacity[i - 1] * RCU_FANOUT;
3469
3470
3471
3472
3473
3474 if (nr_cpu_ids > rcu_capacity[RCU_NUM_LVLS - 1]) {
3475 rcu_fanout_leaf = RCU_FANOUT_LEAF;
3476 WARN_ON(1);
3477 return;
3478 }
3479
3480
3481 for (i = 0; nr_cpu_ids > rcu_capacity[i]; i++) {
3482 }
3483 rcu_num_lvls = i + 1;
3484
3485
3486 for (i = 0; i < rcu_num_lvls; i++) {
3487 int cap = rcu_capacity[(rcu_num_lvls - 1) - i];
3488 num_rcu_lvl[i] = DIV_ROUND_UP(nr_cpu_ids, cap);
3489 }
3490
3491
3492 rcu_num_nodes = 0;
3493 for (i = 0; i < rcu_num_lvls; i++)
3494 rcu_num_nodes += num_rcu_lvl[i];
3495 }
3496
3497
3498
3499
3500
3501 static void __init rcu_dump_rcu_node_tree(void)
3502 {
3503 int level = 0;
3504 struct rcu_node *rnp;
3505
3506 pr_info("rcu_node tree layout dump\n");
3507 pr_info(" ");
3508 rcu_for_each_node_breadth_first(rnp) {
3509 if (rnp->level != level) {
3510 pr_cont("\n");
3511 pr_info(" ");
3512 level = rnp->level;
3513 }
3514 pr_cont("%d:%d ^%d ", rnp->grplo, rnp->grphi, rnp->grpnum);
3515 }
3516 pr_cont("\n");
3517 }
3518
3519 struct workqueue_struct *rcu_gp_wq;
3520 struct workqueue_struct *rcu_par_gp_wq;
3521
3522 void __init rcu_init(void)
3523 {
3524 int cpu;
3525
3526 rcu_early_boot_tests();
3527
3528 rcu_bootup_announce();
3529 rcu_init_geometry();
3530 rcu_init_one();
3531 if (dump_tree)
3532 rcu_dump_rcu_node_tree();
3533 if (use_softirq)
3534 open_softirq(RCU_SOFTIRQ, rcu_core_si);
3535
3536
3537
3538
3539
3540
3541 pm_notifier(rcu_pm_notify, 0);
3542 for_each_online_cpu(cpu) {
3543 rcutree_prepare_cpu(cpu);
3544 rcu_cpu_starting(cpu);
3545 rcutree_online_cpu(cpu);
3546 }
3547
3548
3549 rcu_gp_wq = alloc_workqueue("rcu_gp", WQ_MEM_RECLAIM, 0);
3550 WARN_ON(!rcu_gp_wq);
3551 rcu_par_gp_wq = alloc_workqueue("rcu_par_gp", WQ_MEM_RECLAIM, 0);
3552 WARN_ON(!rcu_par_gp_wq);
3553 srcu_init();
3554 }
3555
3556 #include "tree_stall.h"
3557 #include "tree_exp.h"
3558 #include "tree_plugin.h"