Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog support on powerpc systems. |
| 4 | * |
| 5 | * Copyright 2017, IBM Corporation. |
| 6 | * |
| 7 | * This uses code from arch/sparc/kernel/nmi.c and kernel/watchdog.c |
| 8 | */ |
Michael Ellerman | d8fa82e | 2017-10-12 15:44:32 +1100 | [diff] [blame] | 9 | |
| 10 | #define pr_fmt(fmt) "watchdog: " fmt |
| 11 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/param.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/percpu.h> |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/nmi.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/export.h> |
| 20 | #include <linux/kprobes.h> |
| 21 | #include <linux/hardirq.h> |
| 22 | #include <linux/reboot.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/kdebug.h> |
| 25 | #include <linux/sched/debug.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/smp.h> |
| 28 | |
| 29 | #include <asm/paca.h> |
| 30 | |
| 31 | /* |
Nicholas Piggin | 723b113 | 2017-11-01 11:27:33 +1100 | [diff] [blame] | 32 | * The powerpc watchdog ensures that each CPU is able to service timers. |
| 33 | * The watchdog sets up a simple timer on each CPU to run once per timer |
| 34 | * period, and updates a per-cpu timestamp and a "pending" cpumask. This is |
| 35 | * the heartbeat. |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 36 | * |
Nicholas Piggin | 723b113 | 2017-11-01 11:27:33 +1100 | [diff] [blame] | 37 | * Then there are two systems to check that the heartbeat is still running. |
| 38 | * The local soft-NMI, and the SMP checker. |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 39 | * |
Nicholas Piggin | 723b113 | 2017-11-01 11:27:33 +1100 | [diff] [blame] | 40 | * The soft-NMI checker can detect lockups on the local CPU. When interrupts |
| 41 | * are disabled with local_irq_disable(), platforms that use soft-masking |
| 42 | * can leave hardware interrupts enabled and handle them with a masked |
| 43 | * interrupt handler. The masked handler can send the timer interrupt to the |
| 44 | * watchdog's soft_nmi_interrupt(), which appears to Linux as an NMI |
| 45 | * interrupt, and can be used to detect CPUs stuck with IRQs disabled. |
| 46 | * |
| 47 | * The soft-NMI checker will compare the heartbeat timestamp for this CPU |
| 48 | * with the current time, and take action if the difference exceeds the |
| 49 | * watchdog threshold. |
| 50 | * |
| 51 | * The limitation of the soft-NMI watchdog is that it does not work when |
| 52 | * interrupts are hard disabled or otherwise not being serviced. This is |
| 53 | * solved by also having a SMP watchdog where all CPUs check all other |
| 54 | * CPUs heartbeat. |
| 55 | * |
| 56 | * The SMP checker can detect lockups on other CPUs. A gobal "pending" |
| 57 | * cpumask is kept, containing all CPUs which enable the watchdog. Each |
| 58 | * CPU clears their pending bit in their heartbeat timer. When the bitmask |
| 59 | * becomes empty, the last CPU to clear its pending bit updates a global |
| 60 | * timestamp and refills the pending bitmask. |
| 61 | * |
| 62 | * In the heartbeat timer, if any CPU notices that the global timestamp has |
| 63 | * not been updated for a period exceeding the watchdog threshold, then it |
| 64 | * means the CPU(s) with their bit still set in the pending mask have had |
| 65 | * their heartbeat stop, and action is taken. |
| 66 | * |
Wolfram Sang | 7c18659 | 2018-05-06 13:23:46 +0200 | [diff] [blame] | 67 | * Some platforms implement true NMI IPIs, which can be used by the SMP |
Nicholas Piggin | 723b113 | 2017-11-01 11:27:33 +1100 | [diff] [blame] | 68 | * watchdog to detect an unresponsive CPU and pull it out of its stuck |
| 69 | * state with the NMI IPI, to get crash/debug data from it. This way the |
| 70 | * SMP watchdog can detect hardware interrupts off lockups. |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | static cpumask_t wd_cpus_enabled __read_mostly; |
| 74 | |
| 75 | static u64 wd_panic_timeout_tb __read_mostly; /* timebase ticks until panic */ |
| 76 | static u64 wd_smp_panic_timeout_tb __read_mostly; /* panic other CPUs */ |
| 77 | |
| 78 | static u64 wd_timer_period_ms __read_mostly; /* interval between heartbeat */ |
| 79 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 80 | static DEFINE_PER_CPU(struct hrtimer, wd_hrtimer); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 81 | static DEFINE_PER_CPU(u64, wd_timer_tb); |
| 82 | |
Nicholas Piggin | 723b113 | 2017-11-01 11:27:33 +1100 | [diff] [blame] | 83 | /* SMP checker bits */ |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 84 | static unsigned long __wd_smp_lock; |
| 85 | static cpumask_t wd_smp_cpus_pending; |
| 86 | static cpumask_t wd_smp_cpus_stuck; |
| 87 | static u64 wd_smp_last_reset_tb; |
| 88 | |
| 89 | static inline void wd_smp_lock(unsigned long *flags) |
| 90 | { |
| 91 | /* |
| 92 | * Avoid locking layers if possible. |
| 93 | * This may be called from low level interrupt handlers at some |
| 94 | * point in future. |
| 95 | */ |
Nicholas Piggin | d8e2a40 | 2017-08-09 22:41:22 +1000 | [diff] [blame] | 96 | raw_local_irq_save(*flags); |
| 97 | hard_irq_disable(); /* Make it soft-NMI safe */ |
| 98 | while (unlikely(test_and_set_bit_lock(0, &__wd_smp_lock))) { |
| 99 | raw_local_irq_restore(*flags); |
| 100 | spin_until_cond(!test_bit(0, &__wd_smp_lock)); |
| 101 | raw_local_irq_save(*flags); |
| 102 | hard_irq_disable(); |
| 103 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline void wd_smp_unlock(unsigned long *flags) |
| 107 | { |
| 108 | clear_bit_unlock(0, &__wd_smp_lock); |
Nicholas Piggin | d8e2a40 | 2017-08-09 22:41:22 +1000 | [diff] [blame] | 109 | raw_local_irq_restore(*flags); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void wd_lockup_ipi(struct pt_regs *regs) |
| 113 | { |
Nicholas Piggin | 4e49226 | 2018-05-05 17:26:00 +1000 | [diff] [blame] | 114 | int cpu = raw_smp_processor_id(); |
| 115 | u64 tb = get_tb(); |
| 116 | |
| 117 | pr_emerg("CPU %d Hard LOCKUP\n", cpu); |
| 118 | pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n", |
| 119 | cpu, tb, per_cpu(wd_timer_tb, cpu), |
| 120 | tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 121 | print_modules(); |
| 122 | print_irqtrace_events(current); |
| 123 | if (regs) |
| 124 | show_regs(regs); |
| 125 | else |
| 126 | dump_stack(); |
| 127 | |
Nicholas Piggin | 842dc1d | 2017-09-29 13:29:37 +1000 | [diff] [blame] | 128 | /* Do not panic from here because that can recurse into NMI IPI layer */ |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Nicholas Piggin | 87607a3 | 2017-08-09 22:41:25 +1000 | [diff] [blame] | 131 | static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb) |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 132 | { |
Nicholas Piggin | 87607a3 | 2017-08-09 22:41:25 +1000 | [diff] [blame] | 133 | cpumask_or(&wd_smp_cpus_stuck, &wd_smp_cpus_stuck, cpumask); |
| 134 | cpumask_andnot(&wd_smp_cpus_pending, &wd_smp_cpus_pending, cpumask); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 135 | if (cpumask_empty(&wd_smp_cpus_pending)) { |
| 136 | wd_smp_last_reset_tb = tb; |
| 137 | cpumask_andnot(&wd_smp_cpus_pending, |
| 138 | &wd_cpus_enabled, |
| 139 | &wd_smp_cpus_stuck); |
| 140 | } |
| 141 | } |
Nicholas Piggin | 87607a3 | 2017-08-09 22:41:25 +1000 | [diff] [blame] | 142 | static void set_cpu_stuck(int cpu, u64 tb) |
| 143 | { |
| 144 | set_cpumask_stuck(cpumask_of(cpu), tb); |
| 145 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 146 | |
| 147 | static void watchdog_smp_panic(int cpu, u64 tb) |
| 148 | { |
| 149 | unsigned long flags; |
| 150 | int c; |
| 151 | |
| 152 | wd_smp_lock(&flags); |
| 153 | /* Double check some things under lock */ |
| 154 | if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb) |
| 155 | goto out; |
| 156 | if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) |
| 157 | goto out; |
| 158 | if (cpumask_weight(&wd_smp_cpus_pending) == 0) |
| 159 | goto out; |
| 160 | |
Michael Ellerman | d8fa82e | 2017-10-12 15:44:32 +1100 | [diff] [blame] | 161 | pr_emerg("CPU %d detected hard LOCKUP on other CPUs %*pbl\n", |
| 162 | cpu, cpumask_pr_args(&wd_smp_cpus_pending)); |
Nicholas Piggin | 4e49226 | 2018-05-05 17:26:00 +1000 | [diff] [blame] | 163 | pr_emerg("CPU %d TB:%lld, last SMP heartbeat TB:%lld (%lldms ago)\n", |
| 164 | cpu, tb, wd_smp_last_reset_tb, |
| 165 | tb_to_ns(tb - wd_smp_last_reset_tb) / 1000000); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 166 | |
Nicholas Piggin | d58fdd9 | 2017-09-29 13:29:38 +1000 | [diff] [blame] | 167 | if (!sysctl_hardlockup_all_cpu_backtrace) { |
| 168 | /* |
| 169 | * Try to trigger the stuck CPUs, unless we are going to |
| 170 | * get a backtrace on all of them anyway. |
| 171 | */ |
| 172 | for_each_cpu(c, &wd_smp_cpus_pending) { |
| 173 | if (c == cpu) |
| 174 | continue; |
| 175 | smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000); |
| 176 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 177 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 178 | |
Nicholas Piggin | 87607a3 | 2017-08-09 22:41:25 +1000 | [diff] [blame] | 179 | /* Take the stuck CPUs out of the watch group */ |
| 180 | set_cpumask_stuck(&wd_smp_cpus_pending, tb); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 181 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 182 | wd_smp_unlock(&flags); |
| 183 | |
| 184 | printk_safe_flush(); |
| 185 | /* |
| 186 | * printk_safe_flush() seems to require another print |
| 187 | * before anything actually goes out to console. |
| 188 | */ |
| 189 | if (sysctl_hardlockup_all_cpu_backtrace) |
| 190 | trigger_allbutself_cpu_backtrace(); |
| 191 | |
| 192 | if (hardlockup_panic) |
| 193 | nmi_panic(NULL, "Hard LOCKUP"); |
Nicholas Piggin | 8e23692 | 2017-08-09 22:41:24 +1000 | [diff] [blame] | 194 | |
| 195 | return; |
| 196 | |
| 197 | out: |
| 198 | wd_smp_unlock(&flags); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void wd_smp_clear_cpu_pending(int cpu, u64 tb) |
| 202 | { |
| 203 | if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) { |
| 204 | if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) { |
Nicholas Piggin | 4e49226 | 2018-05-05 17:26:00 +1000 | [diff] [blame] | 205 | struct pt_regs *regs = get_irq_regs(); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 206 | unsigned long flags; |
| 207 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 208 | wd_smp_lock(&flags); |
Nicholas Piggin | 4e49226 | 2018-05-05 17:26:00 +1000 | [diff] [blame] | 209 | |
| 210 | pr_emerg("CPU %d became unstuck TB:%lld\n", |
| 211 | cpu, tb); |
| 212 | print_irqtrace_events(current); |
| 213 | if (regs) |
| 214 | show_regs(regs); |
| 215 | else |
| 216 | dump_stack(); |
| 217 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 218 | cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck); |
| 219 | wd_smp_unlock(&flags); |
| 220 | } |
| 221 | return; |
| 222 | } |
| 223 | cpumask_clear_cpu(cpu, &wd_smp_cpus_pending); |
| 224 | if (cpumask_empty(&wd_smp_cpus_pending)) { |
| 225 | unsigned long flags; |
| 226 | |
| 227 | wd_smp_lock(&flags); |
| 228 | if (cpumask_empty(&wd_smp_cpus_pending)) { |
| 229 | wd_smp_last_reset_tb = tb; |
| 230 | cpumask_andnot(&wd_smp_cpus_pending, |
| 231 | &wd_cpus_enabled, |
| 232 | &wd_smp_cpus_stuck); |
| 233 | } |
| 234 | wd_smp_unlock(&flags); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static void watchdog_timer_interrupt(int cpu) |
| 239 | { |
| 240 | u64 tb = get_tb(); |
| 241 | |
| 242 | per_cpu(wd_timer_tb, cpu) = tb; |
| 243 | |
| 244 | wd_smp_clear_cpu_pending(cpu, tb); |
| 245 | |
| 246 | if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb) |
| 247 | watchdog_smp_panic(cpu, tb); |
| 248 | } |
| 249 | |
| 250 | void soft_nmi_interrupt(struct pt_regs *regs) |
| 251 | { |
| 252 | unsigned long flags; |
| 253 | int cpu = raw_smp_processor_id(); |
| 254 | u64 tb; |
| 255 | |
| 256 | if (!cpumask_test_cpu(cpu, &wd_cpus_enabled)) |
| 257 | return; |
| 258 | |
| 259 | nmi_enter(); |
Nicholas Piggin | 04019bf | 2017-08-01 22:00:54 +1000 | [diff] [blame] | 260 | |
| 261 | __this_cpu_inc(irq_stat.soft_nmi_irqs); |
| 262 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 263 | tb = get_tb(); |
| 264 | if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) { |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 265 | wd_smp_lock(&flags); |
| 266 | if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) { |
| 267 | wd_smp_unlock(&flags); |
| 268 | goto out; |
| 269 | } |
| 270 | set_cpu_stuck(cpu, tb); |
| 271 | |
Nicholas Piggin | 4e49226 | 2018-05-05 17:26:00 +1000 | [diff] [blame] | 272 | pr_emerg("CPU %d self-detected hard LOCKUP @ %pS\n", |
| 273 | cpu, (void *)regs->nip); |
| 274 | pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n", |
| 275 | cpu, tb, per_cpu(wd_timer_tb, cpu), |
| 276 | tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 277 | print_modules(); |
| 278 | print_irqtrace_events(current); |
Michael Ellerman | 3ba45b7 | 2017-10-12 15:44:33 +1100 | [diff] [blame] | 279 | show_regs(regs); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 280 | |
| 281 | wd_smp_unlock(&flags); |
| 282 | |
| 283 | if (sysctl_hardlockup_all_cpu_backtrace) |
| 284 | trigger_allbutself_cpu_backtrace(); |
| 285 | |
| 286 | if (hardlockup_panic) |
| 287 | nmi_panic(regs, "Hard LOCKUP"); |
| 288 | } |
| 289 | if (wd_panic_timeout_tb < 0x7fffffff) |
| 290 | mtspr(SPRN_DEC, wd_panic_timeout_tb); |
| 291 | |
| 292 | out: |
| 293 | nmi_exit(); |
| 294 | } |
| 295 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 296 | static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 297 | { |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 298 | int cpu = smp_processor_id(); |
| 299 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 300 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) |
| 301 | return HRTIMER_NORESTART; |
| 302 | |
| 303 | if (!cpumask_test_cpu(cpu, &watchdog_cpumask)) |
| 304 | return HRTIMER_NORESTART; |
| 305 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 306 | watchdog_timer_interrupt(cpu); |
| 307 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 308 | hrtimer_forward_now(hrtimer, ms_to_ktime(wd_timer_period_ms)); |
| 309 | |
| 310 | return HRTIMER_RESTART; |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void arch_touch_nmi_watchdog(void) |
| 314 | { |
Nicholas Piggin | 26c5c6e | 2017-08-09 22:41:23 +1000 | [diff] [blame] | 315 | unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000; |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 316 | int cpu = smp_processor_id(); |
Nicholas Piggin | 80e4d70 | 2017-09-29 13:29:39 +1000 | [diff] [blame] | 317 | u64 tb = get_tb(); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 318 | |
Nicholas Piggin | 80e4d70 | 2017-09-29 13:29:39 +1000 | [diff] [blame] | 319 | if (tb - per_cpu(wd_timer_tb, cpu) >= ticks) { |
| 320 | per_cpu(wd_timer_tb, cpu) = tb; |
| 321 | wd_smp_clear_cpu_pending(cpu, tb); |
| 322 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 323 | } |
| 324 | EXPORT_SYMBOL(arch_touch_nmi_watchdog); |
| 325 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 326 | static void start_watchdog(void *arg) |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 327 | { |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 328 | struct hrtimer *hrtimer = this_cpu_ptr(&wd_hrtimer); |
| 329 | int cpu = smp_processor_id(); |
Nicholas Piggin | 96ea91e | 2017-08-09 22:41:26 +1000 | [diff] [blame] | 330 | unsigned long flags; |
| 331 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 332 | if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) { |
| 333 | WARN_ON(1); |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 334 | return; |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED)) |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 338 | return; |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 339 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 340 | if (!cpumask_test_cpu(cpu, &watchdog_cpumask)) |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 341 | return; |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 342 | |
Nicholas Piggin | 96ea91e | 2017-08-09 22:41:26 +1000 | [diff] [blame] | 343 | wd_smp_lock(&flags); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 344 | cpumask_set_cpu(cpu, &wd_cpus_enabled); |
| 345 | if (cpumask_weight(&wd_cpus_enabled) == 1) { |
| 346 | cpumask_set_cpu(cpu, &wd_smp_cpus_pending); |
| 347 | wd_smp_last_reset_tb = get_tb(); |
| 348 | } |
Nicholas Piggin | 96ea91e | 2017-08-09 22:41:26 +1000 | [diff] [blame] | 349 | wd_smp_unlock(&flags); |
| 350 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 351 | *this_cpu_ptr(&wd_timer_tb) = get_tb(); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 352 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 353 | hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 354 | hrtimer->function = watchdog_timer_fn; |
| 355 | hrtimer_start(hrtimer, ms_to_ktime(wd_timer_period_ms), |
| 356 | HRTIMER_MODE_REL_PINNED); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 359 | static int start_watchdog_on_cpu(unsigned int cpu) |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 360 | { |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 361 | return smp_call_function_single(cpu, start_watchdog, NULL, true); |
| 362 | } |
| 363 | |
| 364 | static void stop_watchdog(void *arg) |
| 365 | { |
| 366 | struct hrtimer *hrtimer = this_cpu_ptr(&wd_hrtimer); |
| 367 | int cpu = smp_processor_id(); |
Nicholas Piggin | 96ea91e | 2017-08-09 22:41:26 +1000 | [diff] [blame] | 368 | unsigned long flags; |
| 369 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 370 | if (!cpumask_test_cpu(cpu, &wd_cpus_enabled)) |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 371 | return; /* Can happen in CPU unplug case */ |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 372 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 373 | hrtimer_cancel(hrtimer); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 374 | |
Nicholas Piggin | 96ea91e | 2017-08-09 22:41:26 +1000 | [diff] [blame] | 375 | wd_smp_lock(&flags); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 376 | cpumask_clear_cpu(cpu, &wd_cpus_enabled); |
Nicholas Piggin | 96ea91e | 2017-08-09 22:41:26 +1000 | [diff] [blame] | 377 | wd_smp_unlock(&flags); |
| 378 | |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 379 | wd_smp_clear_cpu_pending(cpu, get_tb()); |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 380 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 381 | |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 382 | static int stop_watchdog_on_cpu(unsigned int cpu) |
| 383 | { |
| 384 | return smp_call_function_single(cpu, stop_watchdog, NULL, true); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static void watchdog_calc_timeouts(void) |
| 388 | { |
| 389 | wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq; |
| 390 | |
| 391 | /* Have the SMP detector trigger a bit later */ |
| 392 | wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2; |
| 393 | |
| 394 | /* 2/5 is the factor that the perf based detector uses */ |
| 395 | wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5; |
| 396 | } |
| 397 | |
Thomas Gleixner | 6b9dc48 | 2017-10-02 12:34:50 +0200 | [diff] [blame] | 398 | void watchdog_nmi_stop(void) |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 399 | { |
| 400 | int cpu; |
| 401 | |
Thomas Gleixner | 6b9dc48 | 2017-10-02 12:34:50 +0200 | [diff] [blame] | 402 | for_each_cpu(cpu, &wd_cpus_enabled) |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 403 | stop_watchdog_on_cpu(cpu); |
Thomas Gleixner | 6b9dc48 | 2017-10-02 12:34:50 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void watchdog_nmi_start(void) |
| 407 | { |
| 408 | int cpu; |
| 409 | |
Thomas Gleixner | 6b9dc48 | 2017-10-02 12:34:50 +0200 | [diff] [blame] | 410 | watchdog_calc_timeouts(); |
| 411 | for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask) |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 412 | start_watchdog_on_cpu(cpu); |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
Thomas Gleixner | 34ddaa3 | 2017-10-03 16:39:02 +0200 | [diff] [blame] | 416 | * Invoked from core watchdog init. |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 417 | */ |
Thomas Gleixner | 34ddaa3 | 2017-10-03 16:39:02 +0200 | [diff] [blame] | 418 | int __init watchdog_nmi_probe(void) |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 419 | { |
| 420 | int err; |
| 421 | |
Thomas Gleixner | 34ddaa3 | 2017-10-03 16:39:02 +0200 | [diff] [blame] | 422 | err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, |
| 423 | "powerpc/watchdog:online", |
Nicholas Piggin | 7ae3f6e | 2019-04-09 14:40:05 +1000 | [diff] [blame] | 424 | start_watchdog_on_cpu, |
| 425 | stop_watchdog_on_cpu); |
Thomas Gleixner | 34ddaa3 | 2017-10-03 16:39:02 +0200 | [diff] [blame] | 426 | if (err < 0) { |
Michael Ellerman | d8fa82e | 2017-10-12 15:44:32 +1100 | [diff] [blame] | 427 | pr_warn("could not be initialized"); |
Thomas Gleixner | 34ddaa3 | 2017-10-03 16:39:02 +0200 | [diff] [blame] | 428 | return err; |
| 429 | } |
Nicholas Piggin | 2104180 | 2017-07-12 14:35:52 -0700 | [diff] [blame] | 430 | return 0; |
| 431 | } |