1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/cpu.h> 4 #include <linux/cpumask.h> 5 #include <linux/kernel.h> 6 #include <linux/nmi.h> 7 #include <linux/percpu-defs.h> 8 9 static cpumask_t __read_mostly watchdog_cpus; 10 11 static unsigned int watchdog_next_cpu(unsigned int cpu) 12 { 13 unsigned int next_cpu; 14 15 next_cpu = cpumask_next(cpu, &watchdog_cpus); 16 if (next_cpu >= nr_cpu_ids) 17 next_cpu = cpumask_first(&watchdog_cpus); 18 19 if (next_cpu == cpu) 20 return nr_cpu_ids; 21 22 return next_cpu; 23 } 24 25 int __init watchdog_hardlockup_probe(void) 26 { 27 return 0; 28 } 29 30 void watchdog_hardlockup_enable(unsigned int cpu) 31 { 32 unsigned int next_cpu; 33 34 /* 35 * The new CPU will be marked online before the hrtimer interrupt 36 * gets a chance to run on it. If another CPU tests for a 37 * hardlockup on the new CPU before it has run its the hrtimer 38 * interrupt, it will get a false positive. Touch the watchdog on 39 * the new CPU to delay the check for at least 3 sampling periods 40 * to guarantee one hrtimer has run on the new CPU. 41 */ 42 watchdog_hardlockup_touch_cpu(cpu); 43 44 /* 45 * We are going to check the next CPU. Our watchdog_hrtimer 46 * need not be zero if the CPU has already been online earlier. 47 * Touch the watchdog on the next CPU to avoid false positive 48 * if we try to check it in less then 3 interrupts. 49 */ 50 next_cpu = watchdog_next_cpu(cpu); 51 if (next_cpu < nr_cpu_ids) 52 watchdog_hardlockup_touch_cpu(next_cpu); 53 54 cpumask_set_cpu(cpu, &watchdog_cpus); 55 } 56 57 void watchdog_hardlockup_disable(unsigned int cpu) 58 { 59 unsigned int next_cpu = watchdog_next_cpu(cpu); 60 61 /* 62 * Offlining this CPU will cause the CPU before this one to start 63 * checking the one after this one. If this CPU just finished checking 64 * the next CPU and updating hrtimer_interrupts_saved, and then the 65 * previous CPU checks it within one sample period, it will trigger a 66 * false positive. Touch the watchdog on the next CPU to prevent it. 67 */ 68 if (next_cpu < nr_cpu_ids) 69 watchdog_hardlockup_touch_cpu(next_cpu); 70 71 cpumask_clear_cpu(cpu, &watchdog_cpus); 72 } 73 74 void watchdog_buddy_check_hardlockup(int hrtimer_interrupts) 75 { 76 unsigned int next_cpu; 77 78 /* 79 * Test for hardlockups every 3 samples. The sample period is 80 * watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over 81 * watchdog_thresh (over by 20%). 82 */ 83 if (hrtimer_interrupts % 3 != 0) 84 return; 85 86 /* check for a hardlockup on the next CPU */ 87 next_cpu = watchdog_next_cpu(smp_processor_id()); 88 if (next_cpu >= nr_cpu_ids) 89 return; 90 91 watchdog_hardlockup_check(next_cpu, NULL); 92 } 93