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#ifdef CONFIG_DEBUG_LOCK_ALLOC
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 || !preemptible();
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
383static bool rcuhead_is_static_object(void *addr)
384{
385 return true;
386}
387
388
389
390
391
392
393
394
395
396
397
398void init_rcu_head_on_stack(struct rcu_head *head)
399{
400 debug_object_init_on_stack(head, &rcuhead_debug_descr);
401}
402EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
403
404
405
406
407
408
409
410
411
412
413
414
415void destroy_rcu_head_on_stack(struct rcu_head *head)
416{
417 debug_object_free(head, &rcuhead_debug_descr);
418}
419EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
420
421struct debug_obj_descr rcuhead_debug_descr = {
422 .name = "rcu_head",
423 .is_static_object = rcuhead_is_static_object,
424};
425EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
426#endif
427
428#if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
429void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
430 unsigned long secs,
431 unsigned long c_old, unsigned long c)
432{
433 trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
434}
435EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
436#else
437#define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
438 do { } while (0)
439#endif
440
441#ifdef CONFIG_RCU_STALL_COMMON
442
443#ifdef CONFIG_PROVE_RCU
444#define RCU_STALL_DELAY_DELTA (5 * HZ)
445#else
446#define RCU_STALL_DELAY_DELTA 0
447#endif
448
449int rcu_cpu_stall_suppress __read_mostly;
450static int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
451
452module_param(rcu_cpu_stall_suppress, int, 0644);
453module_param(rcu_cpu_stall_timeout, int, 0644);
454
455int rcu_jiffies_till_stall_check(void)
456{
457 int till_stall_check = READ_ONCE(rcu_cpu_stall_timeout);
458
459
460
461
462
463 if (till_stall_check < 3) {
464 WRITE_ONCE(rcu_cpu_stall_timeout, 3);
465 till_stall_check = 3;
466 } else if (till_stall_check > 300) {
467 WRITE_ONCE(rcu_cpu_stall_timeout, 300);
468 till_stall_check = 300;
469 }
470 return till_stall_check * HZ + RCU_STALL_DELAY_DELTA;
471}
472
473void rcu_sysrq_start(void)
474{
475 if (!rcu_cpu_stall_suppress)
476 rcu_cpu_stall_suppress = 2;
477}
478
479void rcu_sysrq_end(void)
480{
481 if (rcu_cpu_stall_suppress == 2)
482 rcu_cpu_stall_suppress = 0;
483}
484
485static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr)
486{
487 rcu_cpu_stall_suppress = 1;
488 return NOTIFY_DONE;
489}
490
491static struct notifier_block rcu_panic_block = {
492 .notifier_call = rcu_panic,
493};
494
495static int __init check_cpu_stall_init(void)
496{
497 atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block);
498 return 0;
499}
500early_initcall(check_cpu_stall_init);
501
502#endif
503
504#ifdef CONFIG_TASKS_RCU
505
506
507
508
509
510
511
512
513
514
515
516
517
518static struct rcu_head *rcu_tasks_cbs_head;
519static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
520static DECLARE_WAIT_QUEUE_HEAD(rcu_tasks_cbs_wq);
521static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
522
523
524DEFINE_SRCU(tasks_rcu_exit_srcu);
525
526
527static int rcu_task_stall_timeout __read_mostly = HZ * 60 * 10;
528module_param(rcu_task_stall_timeout, int, 0644);
529
530static void rcu_spawn_tasks_kthread(void);
531static struct task_struct *rcu_tasks_kthread_ptr;
532
533
534
535
536
537void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
538{
539 unsigned long flags;
540 bool needwake;
541 bool havetask = READ_ONCE(rcu_tasks_kthread_ptr);
542
543 rhp->next = NULL;
544 rhp->func = func;
545 raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
546 needwake = !rcu_tasks_cbs_head;
547 *rcu_tasks_cbs_tail = rhp;
548 rcu_tasks_cbs_tail = &rhp->next;
549 raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
550
551 if ((needwake && havetask) ||
552 (!havetask && !irqs_disabled_flags(flags))) {
553 rcu_spawn_tasks_kthread();
554 wake_up(&rcu_tasks_cbs_wq);
555 }
556}
557EXPORT_SYMBOL_GPL(call_rcu_tasks);
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592void synchronize_rcu_tasks(void)
593{
594
595 RCU_LOCKDEP_WARN(!rcu_scheduler_active,
596 "synchronize_rcu_tasks called too soon");
597
598
599 wait_rcu_gp(call_rcu_tasks);
600}
601EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
602
603
604
605
606
607
608
609void rcu_barrier_tasks(void)
610{
611
612 synchronize_rcu_tasks();
613}
614EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
615
616
617static void check_holdout_task(struct task_struct *t,
618 bool needreport, bool *firstreport)
619{
620 int cpu;
621
622 if (!READ_ONCE(t->rcu_tasks_holdout) ||
623 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
624 !READ_ONCE(t->on_rq) ||
625 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
626 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
627 WRITE_ONCE(t->rcu_tasks_holdout, false);
628 list_del_init(&t->rcu_tasks_holdout_list);
629 put_task_struct(t);
630 return;
631 }
632 if (!needreport)
633 return;
634 if (*firstreport) {
635 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
636 *firstreport = false;
637 }
638 cpu = task_cpu(t);
639 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
640 t, ".I"[is_idle_task(t)],
641 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
642 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
643 t->rcu_tasks_idle_cpu, cpu);
644 sched_show_task(t);
645}
646
647
648static int __noreturn rcu_tasks_kthread(void *arg)
649{
650 unsigned long flags;
651 struct task_struct *g, *t;
652 unsigned long lastreport;
653 struct rcu_head *list;
654 struct rcu_head *next;
655 LIST_HEAD(rcu_tasks_holdouts);
656
657
658 housekeeping_affine(current);
659
660
661
662
663
664
665
666 for (;;) {
667
668
669 raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
670 list = rcu_tasks_cbs_head;
671 rcu_tasks_cbs_head = NULL;
672 rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
673 raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
674
675
676 if (!list) {
677 wait_event_interruptible(rcu_tasks_cbs_wq,
678 rcu_tasks_cbs_head);
679 if (!rcu_tasks_cbs_head) {
680 WARN_ON(signal_pending(current));
681 schedule_timeout_interruptible(HZ/10);
682 }
683 continue;
684 }
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700 synchronize_sched();
701
702
703
704
705
706
707
708
709 rcu_read_lock();
710 for_each_process_thread(g, t) {
711 if (t != current && READ_ONCE(t->on_rq) &&
712 !is_idle_task(t)) {
713 get_task_struct(t);
714 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
715 WRITE_ONCE(t->rcu_tasks_holdout, true);
716 list_add(&t->rcu_tasks_holdout_list,
717 &rcu_tasks_holdouts);
718 }
719 }
720 rcu_read_unlock();
721
722
723
724
725
726
727
728
729 synchronize_srcu(&tasks_rcu_exit_srcu);
730
731
732
733
734
735
736 lastreport = jiffies;
737 while (!list_empty(&rcu_tasks_holdouts)) {
738 bool firstreport;
739 bool needreport;
740 int rtst;
741 struct task_struct *t1;
742
743 schedule_timeout_interruptible(HZ);
744 rtst = READ_ONCE(rcu_task_stall_timeout);
745 needreport = rtst > 0 &&
746 time_after(jiffies, lastreport + rtst);
747 if (needreport)
748 lastreport = jiffies;
749 firstreport = true;
750 WARN_ON(signal_pending(current));
751 list_for_each_entry_safe(t, t1, &rcu_tasks_holdouts,
752 rcu_tasks_holdout_list) {
753 check_holdout_task(t, needreport, &firstreport);
754 cond_resched();
755 }
756 }
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778 synchronize_sched();
779
780
781 while (list) {
782 next = list->next;
783 local_bh_disable();
784 list->func(list);
785 local_bh_enable();
786 list = next;
787 cond_resched();
788 }
789 schedule_timeout_uninterruptible(HZ/10);
790 }
791}
792
793
794static void rcu_spawn_tasks_kthread(void)
795{
796 static DEFINE_MUTEX(rcu_tasks_kthread_mutex);
797 struct task_struct *t;
798
799 if (READ_ONCE(rcu_tasks_kthread_ptr)) {
800 smp_mb();
801 return;
802 }
803 mutex_lock(&rcu_tasks_kthread_mutex);
804 if (rcu_tasks_kthread_ptr) {
805 mutex_unlock(&rcu_tasks_kthread_mutex);
806 return;
807 }
808 t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
809 BUG_ON(IS_ERR(t));
810 smp_mb();
811 WRITE_ONCE(rcu_tasks_kthread_ptr, t);
812 mutex_unlock(&rcu_tasks_kthread_mutex);
813}
814
815#endif
816
817#ifdef CONFIG_PROVE_RCU
818
819
820
821
822static bool rcu_self_test;
823static bool rcu_self_test_bh;
824static bool rcu_self_test_sched;
825
826module_param(rcu_self_test, bool, 0444);
827module_param(rcu_self_test_bh, bool, 0444);
828module_param(rcu_self_test_sched, bool, 0444);
829
830static int rcu_self_test_counter;
831
832static void test_callback(struct rcu_head *r)
833{
834 rcu_self_test_counter++;
835 pr_info("RCU test callback executed %d\n", rcu_self_test_counter);
836}
837
838static void early_boot_test_call_rcu(void)
839{
840 static struct rcu_head head;
841
842 call_rcu(&head, test_callback);
843}
844
845static void early_boot_test_call_rcu_bh(void)
846{
847 static struct rcu_head head;
848
849 call_rcu_bh(&head, test_callback);
850}
851
852static void early_boot_test_call_rcu_sched(void)
853{
854 static struct rcu_head head;
855
856 call_rcu_sched(&head, test_callback);
857}
858
859void rcu_early_boot_tests(void)
860{
861 pr_info("Running RCU self tests\n");
862
863 if (rcu_self_test)
864 early_boot_test_call_rcu();
865 if (rcu_self_test_bh)
866 early_boot_test_call_rcu_bh();
867 if (rcu_self_test_sched)
868 early_boot_test_call_rcu_sched();
869}
870
871static int rcu_verify_early_boot_tests(void)
872{
873 int ret = 0;
874 int early_boot_test_counter = 0;
875
876 if (rcu_self_test) {
877 early_boot_test_counter++;
878 rcu_barrier();
879 }
880 if (rcu_self_test_bh) {
881 early_boot_test_counter++;
882 rcu_barrier_bh();
883 }
884 if (rcu_self_test_sched) {
885 early_boot_test_counter++;
886 rcu_barrier_sched();
887 }
888
889 if (rcu_self_test_counter != early_boot_test_counter) {
890 WARN_ON(1);
891 ret = -1;
892 }
893
894 return ret;
895}
896late_initcall(rcu_verify_early_boot_tests);
897#else
898void rcu_early_boot_tests(void) {}
899#endif
900