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