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
31
32
33#include <linux/types.h>
34#include <linux/kernel.h>
35#include <linux/init.h>
36#include <linux/spinlock.h>
37#include <linux/smp.h>
38#include <linux/interrupt.h>
39#include <linux/sched/signal.h>
40#include <linux/sched/debug.h>
41#include <linux/atomic.h>
42#include <linux/bitops.h>
43#include <linux/percpu.h>
44#include <linux/notifier.h>
45#include <linux/cpu.h>
46#include <linux/mutex.h>
47#include <linux/export.h>
48#include <linux/hardirq.h>
49#include <linux/delay.h>
50#include <linux/moduleparam.h>
51#include <linux/kthread.h>
52#include <linux/tick.h>
53#include <linux/rcupdate_wait.h>
54
55#define CREATE_TRACE_POINTS
56
57#include "rcu.h"
58
59#ifdef MODULE_PARAM_PREFIX
60#undef MODULE_PARAM_PREFIX
61#endif
62#define MODULE_PARAM_PREFIX "rcupdate."
63
64#ifndef CONFIG_TINY_RCU
65module_param(rcu_expedited, int, 0);
66module_param(rcu_normal, int, 0);
67static int rcu_normal_after_boot;
68module_param(rcu_normal_after_boot, int, 0);
69#endif
70
71#ifdef CONFIG_DEBUG_LOCK_ALLOC
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103int rcu_read_lock_sched_held(void)
104{
105 int lockdep_opinion = 0;
106
107 if (!debug_lockdep_rcu_enabled())
108 return 1;
109 if (!rcu_is_watching())
110 return 0;
111 if (!rcu_lockdep_current_cpu_online())
112 return 0;
113 if (debug_locks)
114 lockdep_opinion = lock_is_held(&rcu_sched_lock_map);
115 return lockdep_opinion || !preemptible();
116}
117EXPORT_SYMBOL(rcu_read_lock_sched_held);
118#endif
119
120#ifndef CONFIG_TINY_RCU
121
122
123
124
125
126
127
128
129
130bool rcu_gp_is_normal(void)
131{
132 return READ_ONCE(rcu_normal) &&
133 rcu_scheduler_active != RCU_SCHEDULER_INIT;
134}
135EXPORT_SYMBOL_GPL(rcu_gp_is_normal);
136
137static atomic_t rcu_expedited_nesting = ATOMIC_INIT(1);
138
139
140
141
142
143
144
145
146bool rcu_gp_is_expedited(void)
147{
148 return rcu_expedited || atomic_read(&rcu_expedited_nesting) ||
149 rcu_scheduler_active == RCU_SCHEDULER_INIT;
150}
151EXPORT_SYMBOL_GPL(rcu_gp_is_expedited);
152
153
154
155
156
157
158
159
160void rcu_expedite_gp(void)
161{
162 atomic_inc(&rcu_expedited_nesting);
163}
164EXPORT_SYMBOL_GPL(rcu_expedite_gp);
165
166
167
168
169
170
171
172
173
174
175void rcu_unexpedite_gp(void)
176{
177 atomic_dec(&rcu_expedited_nesting);
178}
179EXPORT_SYMBOL_GPL(rcu_unexpedite_gp);
180
181
182
183
184void rcu_end_inkernel_boot(void)
185{
186 rcu_unexpedite_gp();
187 if (rcu_normal_after_boot)
188 WRITE_ONCE(rcu_normal, 1);
189}
190
191#endif
192
193
194
195
196
197
198void rcu_test_sync_prims(void)
199{
200 if (!IS_ENABLED(CONFIG_PROVE_RCU))
201 return;
202 synchronize_rcu();
203 synchronize_rcu_bh();
204 synchronize_sched();
205 synchronize_rcu_expedited();
206 synchronize_rcu_bh_expedited();
207 synchronize_sched_expedited();
208}
209
210#if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU)
211
212
213
214
215static int __init rcu_set_runtime_mode(void)
216{
217 rcu_test_sync_prims();
218 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
219 rcu_test_sync_prims();
220 return 0;
221}
222core_initcall(rcu_set_runtime_mode);
223
224#endif
225
226#ifdef CONFIG_PREEMPT_RCU
227
228
229
230
231
232
233void __rcu_read_lock(void)
234{
235 current->rcu_read_lock_nesting++;
236 barrier();
237}
238EXPORT_SYMBOL_GPL(__rcu_read_lock);
239
240
241
242
243
244
245
246
247void __rcu_read_unlock(void)
248{
249 struct task_struct *t = current;
250
251 if (t->rcu_read_lock_nesting != 1) {
252 --t->rcu_read_lock_nesting;
253 } else {
254 barrier();
255 t->rcu_read_lock_nesting = INT_MIN;
256 barrier();
257 if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
258 rcu_read_unlock_special(t);
259 barrier();
260 t->rcu_read_lock_nesting = 0;
261 }
262#ifdef CONFIG_PROVE_LOCKING
263 {
264 int rrln = READ_ONCE(t->rcu_read_lock_nesting);
265
266 WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
267 }
268#endif
269}
270EXPORT_SYMBOL_GPL(__rcu_read_unlock);
271
272#endif
273
274#ifdef CONFIG_DEBUG_LOCK_ALLOC
275static struct lock_class_key rcu_lock_key;
276struct lockdep_map rcu_lock_map =
277 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
278EXPORT_SYMBOL_GPL(rcu_lock_map);
279
280static struct lock_class_key rcu_bh_lock_key;
281struct lockdep_map rcu_bh_lock_map =
282 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
283EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
284
285static struct lock_class_key rcu_sched_lock_key;
286struct lockdep_map rcu_sched_lock_map =
287 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
288EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
289
290static struct lock_class_key rcu_callback_key;
291struct lockdep_map rcu_callback_map =
292 STATIC_LOCKDEP_MAP_INIT("rcu_callback", &rcu_callback_key);
293EXPORT_SYMBOL_GPL(rcu_callback_map);
294
295int notrace debug_lockdep_rcu_enabled(void)
296{
297 return rcu_scheduler_active != RCU_SCHEDULER_INACTIVE && debug_locks &&
298 current->lockdep_recursion == 0;
299}
300EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322int rcu_read_lock_held(void)
323{
324 if (!debug_lockdep_rcu_enabled())
325 return 1;
326 if (!rcu_is_watching())
327 return 0;
328 if (!rcu_lockdep_current_cpu_online())
329 return 0;
330 return lock_is_held(&rcu_lock_map);
331}
332EXPORT_SYMBOL_GPL(rcu_read_lock_held);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349int rcu_read_lock_bh_held(void)
350{
351 if (!debug_lockdep_rcu_enabled())
352 return 1;
353 if (!rcu_is_watching())
354 return 0;
355 if (!rcu_lockdep_current_cpu_online())
356 return 0;
357 return in_softirq() || irqs_disabled();
358}
359EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
360
361#endif
362
363
364
365
366
367
368
369void wakeme_after_rcu(struct rcu_head *head)
370{
371 struct rcu_synchronize *rcu;
372
373 rcu = container_of(head, struct rcu_synchronize, head);
374 complete(&rcu->completion);
375}
376EXPORT_SYMBOL_GPL(wakeme_after_rcu);
377
378void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
379 struct rcu_synchronize *rs_array)
380{
381 int i;
382
383
384 for (i = 0; i < n; i++) {
385 if (checktiny &&
386 (crcu_array[i] == call_rcu ||
387 crcu_array[i] == call_rcu_bh)) {
388 might_sleep();
389 continue;
390 }
391 init_rcu_head_on_stack(&rs_array[i].head);
392 init_completion(&rs_array[i].completion);
393 (crcu_array[i])(&rs_array[i].head, wakeme_after_rcu);
394 }
395
396
397 for (i = 0; i < n; i++) {
398 if (checktiny &&
399 (crcu_array[i] == call_rcu ||
400 crcu_array[i] == call_rcu_bh))
401 continue;
402 wait_for_completion(&rs_array[i].completion);
403 destroy_rcu_head_on_stack(&rs_array[i].head);
404 }
405}
406EXPORT_SYMBOL_GPL(__wait_rcu_gp);
407
408#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
409void init_rcu_head(struct rcu_head *head)
410{
411 debug_object_init(head, &rcuhead_debug_descr);
412}
413
414void destroy_rcu_head(struct rcu_head *head)
415{
416 debug_object_free(head, &rcuhead_debug_descr);
417}
418
419static bool rcuhead_is_static_object(void *addr)
420{
421 return true;
422}
423
424
425
426
427
428
429
430
431
432
433
434void init_rcu_head_on_stack(struct rcu_head *head)
435{
436 debug_object_init_on_stack(head, &rcuhead_debug_descr);
437}
438EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
439
440
441
442
443
444
445
446
447
448
449
450
451void destroy_rcu_head_on_stack(struct rcu_head *head)
452{
453 debug_object_free(head, &rcuhead_debug_descr);
454}
455EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
456
457struct debug_obj_descr rcuhead_debug_descr = {
458 .name = "rcu_head",
459 .is_static_object = rcuhead_is_static_object,
460};
461EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
462#endif
463
464#if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
465void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
466 unsigned long secs,
467 unsigned long c_old, unsigned long c)
468{
469 trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
470}
471EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
472#else
473#define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
474 do { } while (0)
475#endif
476
477#ifdef CONFIG_RCU_STALL_COMMON
478
479#ifdef CONFIG_PROVE_RCU
480#define RCU_STALL_DELAY_DELTA (5 * HZ)
481#else
482#define RCU_STALL_DELAY_DELTA 0
483#endif
484
485int rcu_cpu_stall_suppress __read_mostly;
486static int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
487
488module_param(rcu_cpu_stall_suppress, int, 0644);
489module_param(rcu_cpu_stall_timeout, int, 0644);
490
491int rcu_jiffies_till_stall_check(void)
492{
493 int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
494
495
496
497
498
499 if (till_stall_check < 3) {
500 WRITE_ONCE(rcu_cpu_stall_timeout, 3);
501 till_stall_check = 3;
502 } else if (till_stall_check > 300) {
503 WRITE_ONCE(rcu_cpu_stall_timeout, 300);
504 till_stall_check = 300;
505 }
506 return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
507}
508
509void rcu_sysrq_start(void)
510{
511 if (!rcu_cpu_stall_suppress)
512 rcu_cpu_stall_suppress = 2;
513}
514
515void rcu_sysrq_end(void)
516{
517 if (rcu_cpu_stall_suppress == 2)
518 rcu_cpu_stall_suppress = 0;
519}
520
521static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
522{
523 rcu_cpu_stall_suppress = 1;
524 return NOTIFY_DONE;
525}
526
527static struct notifier_block rcu_panic_block = {
528 .notifier_call = rcu_panic,
529};
530
531static int __init check_cpu_stall_init(void)
532{
533 atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
534 return 0;
535}
536early_initcall(check_cpu_stall_init);
537
538#endif
539
540#ifdef CONFIG_TASKS_RCU
541
542
543
544
545
546
547
548
549
550
551
552
553
554static struct rcu_head *rcu_tasks_cbs_head;
555static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
556static DECLARE_WAIT_QUEUE_HEAD(rcu_tasks_cbs_wq);
557static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
558
559
560DEFINE_SRCU(tasks_rcu_exit_srcu);
561
562
563static int rcu_task_stall_timeout __read_mostly = HZ * 60 * 10;
564module_param(rcu_task_stall_timeout, int, 0644);
565
566static void rcu_spawn_tasks_kthread(void);
567static struct task_struct *rcu_tasks_kthread_ptr;
568
569
570
571
572
573void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
574{
575 unsigned long flags;
576 bool needwake;
577 bool havetask = READ_ONCE(rcu_tasks_kthread_ptr);
578
579 rhp->next = NULL;
580 rhp->func = func;
581 raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
582 needwake = !rcu_tasks_cbs_head;
583 *rcu_tasks_cbs_tail = rhp;
584 rcu_tasks_cbs_tail = &rhp->next;
585 raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
586
587 if ((needwake && havetask) ||
588 (!havetask && !irqs_disabled_flags(flags))) {
589 rcu_spawn_tasks_kthread();
590 wake_up(&rcu_tasks_cbs_wq);
591 }
592}
593EXPORT_SYMBOL_GPL(call_rcu_tasks);
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628void synchronize_rcu_tasks(void)
629{
630
631 RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
632 "synchronize_rcu_tasks called too soon");
633
634
635 wait_rcu_gp(call_rcu_tasks);
636}
637EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
638
639
640
641
642
643
644
645void rcu_barrier_tasks(void)
646{
647
648 synchronize_rcu_tasks();
649}
650EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
651
652
653static void check_holdout_task(struct task_struct *t,
654 bool needreport, bool *firstreport)
655{
656 int cpu;
657
658 if (!READ_ONCE(t->rcu_tasks_holdout) ||
659 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
660 !READ_ONCE(t->on_rq) ||
661 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
662 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
663 WRITE_ONCE(t->rcu_tasks_holdout, false);
664 list_del_init(&t->rcu_tasks_holdout_list);
665 put_task_struct(t);
666 return;
667 }
668 rcu_request_urgent_qs_task(t);
669 if (!needreport)
670 return;
671 if (*firstreport) {
672 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
673 *firstreport = false;
674 }
675 cpu = task_cpu(t);
676 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
677 t, ".I"[is_idle_task(t)],
678 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
679 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
680 t->rcu_tasks_idle_cpu, cpu);
681 sched_show_task(t);
682}
683
684
685static int __noreturn rcu_tasks_kthread(void *arg)
686{
687 unsigned long flags;
688 struct task_struct *g, *t;
689 unsigned long lastreport;
690 struct rcu_head *list;
691 struct rcu_head *next;
692 LIST_HEAD(rcu_tasks_holdouts);
693
694
695 housekeeping_affine(current);
696
697
698
699
700
701
702
703 for (;;) {
704
705
706 raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
707 list = rcu_tasks_cbs_head;
708 rcu_tasks_cbs_head = NULL;
709 rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
710 raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
711
712
713 if (!list) {
714 wait_event_interruptible(rcu_tasks_cbs_wq,
715 rcu_tasks_cbs_head);
716 if (!rcu_tasks_cbs_head) {
717 WARN_ON(signal_pending(current));
718 schedule_timeout_interruptible(HZ/10);
719 }
720 continue;
721 }
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737 synchronize_sched();
738
739
740
741
742
743
744
745
746 rcu_read_lock();
747 for_each_process_thread(g, t) {
748 if (t != current && READ_ONCE(t->on_rq) &&
749 !is_idle_task(t)) {
750 get_task_struct(t);
751 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
752 WRITE_ONCE(t->rcu_tasks_holdout, true);
753 list_add(&t->rcu_tasks_holdout_list,
754 &rcu_tasks_holdouts);
755 }
756 }
757 rcu_read_unlock();
758
759
760
761
762
763
764
765
766 synchronize_srcu(&tasks_rcu_exit_srcu);
767
768
769
770
771
772
773 lastreport = jiffies;
774 while (!list_empty(&rcu_tasks_holdouts)) {
775 bool firstreport;
776 bool needreport;
777 int rtst;
778 struct task_struct *t1;
779
780 schedule_timeout_interruptible(HZ);
781 rtst = READ_ONCE(rcu_task_stall_timeout);
782 needreport = rtst > 0 &&
783 time_after(jiffies, lastreport + rtst);
784 if (needreport)
785 lastreport = jiffies;
786 firstreport = true;
787 WARN_ON(signal_pending(current));
788 list_for_each_entry_safe(t, t1, &rcu_tasks_holdouts,
789 rcu_tasks_holdout_list) {
790 check_holdout_task(t, needreport, &firstreport);
791 cond_resched();
792 }
793 }
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815 synchronize_sched();
816
817
818 while (list) {
819 next = list->next;
820 local_bh_disable();
821 list->func(list);
822 local_bh_enable();
823 list = next;
824 cond_resched();
825 }
826 schedule_timeout_uninterruptible(HZ/10);
827 }
828}
829
830
831static void rcu_spawn_tasks_kthread(void)
832{
833 static DEFINE_MUTEX(rcu_tasks_kthread_mutex);
834 struct task_struct *t;
835
836 if (READ_ONCE(rcu_tasks_kthread_ptr)) {
837 smp_mb();
838 return;
839 }
840 mutex_lock(&rcu_tasks_kthread_mutex);
841 if (rcu_tasks_kthread_ptr) {
842 mutex_unlock(&rcu_tasks_kthread_mutex);
843 return;
844 }
845 t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
846 BUG_ON(IS_ERR(t));
847 smp_mb();
848 WRITE_ONCE(rcu_tasks_kthread_ptr, t);
849 mutex_unlock(&rcu_tasks_kthread_mutex);
850}
851
852#endif
853
854#ifdef CONFIG_PROVE_RCU
855
856
857
858
859static bool rcu_self_test;
860static bool rcu_self_test_bh;
861static bool rcu_self_test_sched;
862
863module_param(rcu_self_test, bool, 0444);
864module_param(rcu_self_test_bh, bool, 0444);
865module_param(rcu_self_test_sched, bool, 0444);
866
867static int rcu_self_test_counter;
868
869static void test_callback(struct rcu_head *r)
870{
871 rcu_self_test_counter++;
872 pr_info("RCU test callback executed %d\n", rcu_self_test_counter);
873}
874
875static void early_boot_test_call_rcu(void)
876{
877 static struct rcu_head head;
878
879 call_rcu(&head, test_callback);
880}
881
882static void early_boot_test_call_rcu_bh(void)
883{
884 static struct rcu_head head;
885
886 call_rcu_bh(&head, test_callback);
887}
888
889static void early_boot_test_call_rcu_sched(void)
890{
891 static struct rcu_head head;
892
893 call_rcu_sched(&head, test_callback);
894}
895
896void rcu_early_boot_tests(void)
897{
898 pr_info("Running RCU self tests\n");
899
900 if (rcu_self_test)
901 early_boot_test_call_rcu();
902 if (rcu_self_test_bh)
903 early_boot_test_call_rcu_bh();
904 if (rcu_self_test_sched)
905 early_boot_test_call_rcu_sched();
906 rcu_test_sync_prims();
907}
908
909static int rcu_verify_early_boot_tests(void)
910{
911 int ret = 0;
912 int early_boot_test_counter = 0;
913
914 if (rcu_self_test) {
915 early_boot_test_counter++;
916 rcu_barrier();
917 }
918 if (rcu_self_test_bh) {
919 early_boot_test_counter++;
920 rcu_barrier_bh();
921 }
922 if (rcu_self_test_sched) {
923 early_boot_test_counter++;
924 rcu_barrier_sched();
925 }
926
927 if (rcu_self_test_counter != early_boot_test_counter) {
928 WARN_ON(1);
929 ret = -1;
930 }
931
932 return ret;
933}
934late_initcall(rcu_verify_early_boot_tests);
935#else
936void rcu_early_boot_tests(void) {}
937#endif
938