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