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