hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/kernel/watchdog.c
....@@ -27,6 +27,8 @@
2727 #include <asm/irq_regs.h>
2828 #include <linux/kvm_para.h>
2929
30
+#include <trace/hooks/softlockup.h>
31
+
3032 static DEFINE_MUTEX(watchdog_mutex);
3133
3234 #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG)
....@@ -42,14 +44,16 @@
4244 int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT;
4345 int __read_mostly soft_watchdog_user_enabled = 1;
4446 int __read_mostly watchdog_thresh = 10;
45
-int __read_mostly nmi_watchdog_available;
46
-
47
-struct cpumask watchdog_allowed_mask __read_mostly;
47
+static int __read_mostly nmi_watchdog_available;
4848
4949 struct cpumask watchdog_cpumask __read_mostly;
5050 unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
5151
5252 #ifdef CONFIG_HARDLOCKUP_DETECTOR
53
+
54
+# ifdef CONFIG_SMP
55
+int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
56
+# endif /* CONFIG_SMP */
5357
5458 ATOMIC_NOTIFIER_HEAD(hardlock_notifier_list);
5559
....@@ -85,16 +89,6 @@
8589 }
8690 __setup("nmi_watchdog=", hardlockup_panic_setup);
8791
88
-# ifdef CONFIG_SMP
89
-int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
90
-
91
-static int __init hardlockup_all_cpu_backtrace_setup(char *str)
92
-{
93
- sysctl_hardlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
94
- return 1;
95
-}
96
-__setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
97
-# endif /* CONFIG_SMP */
9892 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
9993
10094 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
....@@ -206,6 +200,12 @@
206200
207201 #define SOFTLOCKUP_RESET ULONG_MAX
208202
203
+#ifdef CONFIG_SMP
204
+int __read_mostly sysctl_softlockup_all_cpu_backtrace;
205
+#endif
206
+
207
+static struct cpumask watchdog_allowed_mask __read_mostly;
208
+
209209 /* Global variables, exported for sysctl */
210210 unsigned int __read_mostly softlockup_panic =
211211 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
....@@ -216,19 +216,9 @@
216216 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
217217 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
218218 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
219
-static DEFINE_PER_CPU(bool, soft_watchdog_warn);
220219 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
221
-static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
222
-static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
223220 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
224221 static unsigned long soft_lockup_nmi_warn;
225
-
226
-static int __init softlockup_panic_setup(char *str)
227
-{
228
- softlockup_panic = simple_strtoul(str, NULL, 0);
229
- return 1;
230
-}
231
-__setup("softlockup_panic=", softlockup_panic_setup);
232222
233223 static int __init nowatchdog_setup(char *str)
234224 {
....@@ -244,16 +234,12 @@
244234 }
245235 __setup("nosoftlockup", nosoftlockup_setup);
246236
247
-#ifdef CONFIG_SMP
248
-int __read_mostly sysctl_softlockup_all_cpu_backtrace;
249
-
250
-static int __init softlockup_all_cpu_backtrace_setup(char *str)
237
+static int __init watchdog_thresh_setup(char *str)
251238 {
252
- sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0);
239
+ get_option(&str, &watchdog_thresh);
253240 return 1;
254241 }
255
-__setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
256
-#endif
242
+__setup("watchdog_thresh=", watchdog_thresh_setup);
257243
258244 static void __lockup_detector_cleanup(void);
259245
....@@ -293,7 +279,7 @@
293279 }
294280
295281 /* Commands for resetting the watchdog */
296
-static void __touch_watchdog(void)
282
+static void update_touch_ts(void)
297283 {
298284 __this_cpu_write(watchdog_touch_ts, get_timestamp());
299285 }
....@@ -426,13 +412,12 @@
426412 if (per_cpu(hard_watchdog_warn, next_cpu) == true)
427413 return;
428414
429
- atomic_notifier_call_chain(&hardlock_notifier_list, 0, NULL);
430
-
431415 if (hardlockup_panic)
432416 panic("Watchdog detected hard LOCKUP on cpu %u", next_cpu);
433417 else
434418 WARN(1, "Watchdog detected hard LOCKUP on cpu %u", next_cpu);
435419
420
+ atomic_notifier_call_chain(&hardlock_notifier_list, 0, NULL);
436421 per_cpu(hard_watchdog_warn, next_cpu) = true;
437422 } else {
438423 per_cpu(hard_watchdog_warn, next_cpu) = false;
....@@ -460,9 +445,7 @@
460445 */
461446 static int softlockup_fn(void *data)
462447 {
463
- __this_cpu_write(soft_lockup_hrtimer_cnt,
464
- __this_cpu_read(hrtimer_interrupts));
465
- __touch_watchdog();
448
+ update_touch_ts();
466449 complete(this_cpu_ptr(&softlockup_completion));
467450
468451 return 0;
....@@ -509,7 +492,7 @@
509492
510493 /* Clear the guest paused flag on watchdog reset */
511494 kvm_check_and_clear_guest_paused();
512
- __touch_watchdog();
495
+ update_touch_ts();
513496 return HRTIMER_RESTART;
514497 }
515498
....@@ -529,39 +512,21 @@
529512 if (kvm_check_and_clear_guest_paused())
530513 return HRTIMER_RESTART;
531514
532
- /* only warn once */
533
- if (__this_cpu_read(soft_watchdog_warn) == true) {
534
- /*
535
- * When multiple processes are causing softlockups the
536
- * softlockup detector only warns on the first one
537
- * because the code relies on a full quiet cycle to
538
- * re-arm. The second process prevents the quiet cycle
539
- * and never gets reported. Use task pointers to detect
540
- * this.
541
- */
542
- if (__this_cpu_read(softlockup_task_ptr_saved) !=
543
- current) {
544
- __this_cpu_write(soft_watchdog_warn, false);
545
- __touch_watchdog();
546
- }
547
- return HRTIMER_RESTART;
515
+ /*
516
+ * Prevent multiple soft-lockup reports if one cpu is already
517
+ * engaged in dumping all cpu back traces.
518
+ */
519
+ if (softlockup_all_cpu_backtrace) {
520
+ if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
521
+ return HRTIMER_RESTART;
548522 }
549523
550
- if (softlockup_all_cpu_backtrace) {
551
- /* Prevent multiple soft-lockup reports if one cpu is already
552
- * engaged in dumping cpu back traces
553
- */
554
- if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
555
- /* Someone else will report us. Let's give up */
556
- __this_cpu_write(soft_watchdog_warn, true);
557
- return HRTIMER_RESTART;
558
- }
559
- }
524
+ /* Start period for the next softlockup warning. */
525
+ update_touch_ts();
560526
561527 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
562528 smp_processor_id(), duration,
563529 current->comm, task_pid_nr(current));
564
- __this_cpu_write(softlockup_task_ptr_saved, current);
565530 print_modules();
566531 print_irqtrace_events(current);
567532 if (regs)
....@@ -570,22 +535,15 @@
570535 dump_stack();
571536
572537 if (softlockup_all_cpu_backtrace) {
573
- /* Avoid generating two back traces for current
574
- * given that one is already made above
575
- */
576538 trigger_allbutself_cpu_backtrace();
577
-
578
- clear_bit(0, &soft_lockup_nmi_warn);
579
- /* Barrier to sync with other cpus */
580
- smp_mb__after_atomic();
539
+ clear_bit_unlock(0, &soft_lockup_nmi_warn);
581540 }
582541
542
+ trace_android_vh_watchdog_timer_softlockup(duration, regs, !!softlockup_panic);
583543 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
584544 if (softlockup_panic)
585545 panic("softlockup: hung tasks");
586
- __this_cpu_write(soft_watchdog_warn, true);
587
- } else
588
- __this_cpu_write(soft_watchdog_warn, false);
546
+ }
589547
590548 return HRTIMER_RESTART;
591549 }
....@@ -607,10 +565,10 @@
607565 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
608566 hrtimer->function = watchdog_timer_fn;
609567 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
610
- HRTIMER_MODE_REL_PINNED);
568
+ HRTIMER_MODE_REL_PINNED_HARD);
611569
612570 /* Initialize timestamp */
613
- __touch_watchdog();
571
+ update_touch_ts();
614572 /* Enable the perf event */
615573 if (watchdog_enabled & NMI_WATCHDOG_ENABLED)
616574 watchdog_nmi_enable(cpu);
....@@ -682,7 +640,7 @@
682640 return 0;
683641 }
684642
685
-static void lockup_detector_reconfigure(void)
643
+static void __lockup_detector_reconfigure(void)
686644 {
687645 cpus_read_lock();
688646 watchdog_nmi_stop();
....@@ -702,11 +660,18 @@
702660 __lockup_detector_cleanup();
703661 }
704662
663
+void lockup_detector_reconfigure(void)
664
+{
665
+ mutex_lock(&watchdog_mutex);
666
+ __lockup_detector_reconfigure();
667
+ mutex_unlock(&watchdog_mutex);
668
+}
669
+
705670 /*
706671 * Create the watchdog thread infrastructure and configure the detector(s).
707672 *
708673 * The threads are not unparked as watchdog_allowed_mask is empty. When
709
- * the threads are sucessfully initialized, take the proper locks and
674
+ * the threads are successfully initialized, take the proper locks and
710675 * unpark the threads in the watchdog_cpumask if the watchdog is enabled.
711676 */
712677 static __init void lockup_detector_setup(void)
....@@ -722,13 +687,13 @@
722687 return;
723688
724689 mutex_lock(&watchdog_mutex);
725
- lockup_detector_reconfigure();
690
+ __lockup_detector_reconfigure();
726691 softlockup_initialized = true;
727692 mutex_unlock(&watchdog_mutex);
728693 }
729694
730695 #else /* CONFIG_SOFTLOCKUP_DETECTOR */
731
-static void lockup_detector_reconfigure(void)
696
+static void __lockup_detector_reconfigure(void)
732697 {
733698 cpus_read_lock();
734699 watchdog_nmi_stop();
....@@ -736,9 +701,13 @@
736701 watchdog_nmi_start();
737702 cpus_read_unlock();
738703 }
704
+void lockup_detector_reconfigure(void)
705
+{
706
+ __lockup_detector_reconfigure();
707
+}
739708 static inline void lockup_detector_setup(void)
740709 {
741
- lockup_detector_reconfigure();
710
+ __lockup_detector_reconfigure();
742711 }
743712 #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
744713
....@@ -778,7 +747,7 @@
778747 {
779748 /* Remove impossible cpus to keep sysctl output clean. */
780749 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
781
- lockup_detector_reconfigure();
750
+ __lockup_detector_reconfigure();
782751 }
783752
784753 /*
....@@ -794,7 +763,7 @@
794763 * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED
795764 */
796765 static int proc_watchdog_common(int which, struct ctl_table *table, int write,
797
- void __user *buffer, size_t *lenp, loff_t *ppos)
766
+ void *buffer, size_t *lenp, loff_t *ppos)
798767 {
799768 int err, old, *param = table->data;
800769
....@@ -821,7 +790,7 @@
821790 * /proc/sys/kernel/watchdog
822791 */
823792 int proc_watchdog(struct ctl_table *table, int write,
824
- void __user *buffer, size_t *lenp, loff_t *ppos)
793
+ void *buffer, size_t *lenp, loff_t *ppos)
825794 {
826795 return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
827796 table, write, buffer, lenp, ppos);
....@@ -831,7 +800,7 @@
831800 * /proc/sys/kernel/nmi_watchdog
832801 */
833802 int proc_nmi_watchdog(struct ctl_table *table, int write,
834
- void __user *buffer, size_t *lenp, loff_t *ppos)
803
+ void *buffer, size_t *lenp, loff_t *ppos)
835804 {
836805 if (!nmi_watchdog_available && write)
837806 return -ENOTSUPP;
....@@ -843,7 +812,7 @@
843812 * /proc/sys/kernel/soft_watchdog
844813 */
845814 int proc_soft_watchdog(struct ctl_table *table, int write,
846
- void __user *buffer, size_t *lenp, loff_t *ppos)
815
+ void *buffer, size_t *lenp, loff_t *ppos)
847816 {
848817 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
849818 table, write, buffer, lenp, ppos);
....@@ -853,7 +822,7 @@
853822 * /proc/sys/kernel/watchdog_thresh
854823 */
855824 int proc_watchdog_thresh(struct ctl_table *table, int write,
856
- void __user *buffer, size_t *lenp, loff_t *ppos)
825
+ void *buffer, size_t *lenp, loff_t *ppos)
857826 {
858827 int err, old;
859828
....@@ -876,7 +845,7 @@
876845 * been brought online, if desired.
877846 */
878847 int proc_watchdog_cpumask(struct ctl_table *table, int write,
879
- void __user *buffer, size_t *lenp, loff_t *ppos)
848
+ void *buffer, size_t *lenp, loff_t *ppos)
880849 {
881850 int err;
882851