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