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