1 // SPDX-License-Identifier: GPL-2.0
3 * Watchdog support on powerpc systems.
5 * Copyright 2017, IBM Corporation.
7 * This uses code from arch/sparc/kernel/nmi.c and kernel/watchdog.c
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>
29 * The watchdog has a simple timer that runs on each CPU, once per timer
30 * period. This is the heartbeat.
32 * Then there are checks to see if the heartbeat has not triggered on a CPU
33 * for the panic timeout period. Currently the watchdog only supports an
34 * SMP check, so the heartbeat only turns on when we have 2 or more CPUs.
36 * This is not an NMI watchdog, but Linux uses that name for a generic
37 * watchdog in some cases, so NMI gets used in some places.
40 static cpumask_t wd_cpus_enabled __read_mostly;
42 static u64 wd_panic_timeout_tb __read_mostly; /* timebase ticks until panic */
43 static u64 wd_smp_panic_timeout_tb __read_mostly; /* panic other CPUs */
45 static u64 wd_timer_period_ms __read_mostly; /* interval between heartbeat */
47 static DEFINE_PER_CPU(struct timer_list, wd_timer);
48 static DEFINE_PER_CPU(u64, wd_timer_tb);
51 * These are for the SMP checker. CPUs clear their pending bit in their
52 * heartbeat. If the bitmask becomes empty, the time is noted and the
53 * bitmask is refilled.
55 * All CPUs clear their bit in the pending mask every timer period.
56 * Once all have cleared, the time is noted and the bits are reset.
57 * If the time since all clear was greater than the panic timeout,
58 * we can panic with the list of stuck CPUs.
60 * This will work best with NMI IPIs for crash code so the stuck CPUs
61 * can be pulled out to get their backtraces.
63 static unsigned long __wd_smp_lock;
64 static cpumask_t wd_smp_cpus_pending;
65 static cpumask_t wd_smp_cpus_stuck;
66 static u64 wd_smp_last_reset_tb;
68 static inline void wd_smp_lock(unsigned long *flags)
71 * Avoid locking layers if possible.
72 * This may be called from low level interrupt handlers at some
75 raw_local_irq_save(*flags);
76 hard_irq_disable(); /* Make it soft-NMI safe */
77 while (unlikely(test_and_set_bit_lock(0, &__wd_smp_lock))) {
78 raw_local_irq_restore(*flags);
79 spin_until_cond(!test_bit(0, &__wd_smp_lock));
80 raw_local_irq_save(*flags);
85 static inline void wd_smp_unlock(unsigned long *flags)
87 clear_bit_unlock(0, &__wd_smp_lock);
88 raw_local_irq_restore(*flags);
91 static void wd_lockup_ipi(struct pt_regs *regs)
93 pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", raw_smp_processor_id());
95 print_irqtrace_events(current);
101 if (hardlockup_panic)
102 nmi_panic(regs, "Hard LOCKUP");
105 static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb)
107 cpumask_or(&wd_smp_cpus_stuck, &wd_smp_cpus_stuck, cpumask);
108 cpumask_andnot(&wd_smp_cpus_pending, &wd_smp_cpus_pending, cpumask);
109 if (cpumask_empty(&wd_smp_cpus_pending)) {
110 wd_smp_last_reset_tb = tb;
111 cpumask_andnot(&wd_smp_cpus_pending,
116 static void set_cpu_stuck(int cpu, u64 tb)
118 set_cpumask_stuck(cpumask_of(cpu), tb);
121 static void watchdog_smp_panic(int cpu, u64 tb)
127 /* Double check some things under lock */
128 if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
130 if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
132 if (cpumask_weight(&wd_smp_cpus_pending) == 0)
135 pr_emerg("Watchdog CPU:%d detected Hard LOCKUP other CPUS:%*pbl\n",
136 cpu, cpumask_pr_args(&wd_smp_cpus_pending));
139 * Try to trigger the stuck CPUs.
141 for_each_cpu(c, &wd_smp_cpus_pending) {
144 smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
146 smp_flush_nmi_ipi(1000000);
148 /* Take the stuck CPUs out of the watch group */
149 set_cpumask_stuck(&wd_smp_cpus_pending, tb);
151 wd_smp_unlock(&flags);
155 * printk_safe_flush() seems to require another print
156 * before anything actually goes out to console.
158 if (sysctl_hardlockup_all_cpu_backtrace)
159 trigger_allbutself_cpu_backtrace();
161 if (hardlockup_panic)
162 nmi_panic(NULL, "Hard LOCKUP");
167 wd_smp_unlock(&flags);
170 static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
172 if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
173 if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
176 pr_emerg("Watchdog CPU:%d became unstuck\n", cpu);
178 cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
179 wd_smp_unlock(&flags);
183 cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
184 if (cpumask_empty(&wd_smp_cpus_pending)) {
188 if (cpumask_empty(&wd_smp_cpus_pending)) {
189 wd_smp_last_reset_tb = tb;
190 cpumask_andnot(&wd_smp_cpus_pending,
194 wd_smp_unlock(&flags);
198 static void watchdog_timer_interrupt(int cpu)
202 per_cpu(wd_timer_tb, cpu) = tb;
204 wd_smp_clear_cpu_pending(cpu, tb);
206 if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
207 watchdog_smp_panic(cpu, tb);
210 void soft_nmi_interrupt(struct pt_regs *regs)
213 int cpu = raw_smp_processor_id();
216 if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
221 __this_cpu_inc(irq_stat.soft_nmi_irqs);
224 if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
225 per_cpu(wd_timer_tb, cpu) = tb;
228 if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
229 wd_smp_unlock(&flags);
232 set_cpu_stuck(cpu, tb);
234 pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", cpu);
236 print_irqtrace_events(current);
242 wd_smp_unlock(&flags);
244 if (sysctl_hardlockup_all_cpu_backtrace)
245 trigger_allbutself_cpu_backtrace();
247 if (hardlockup_panic)
248 nmi_panic(regs, "Hard LOCKUP");
250 if (wd_panic_timeout_tb < 0x7fffffff)
251 mtspr(SPRN_DEC, wd_panic_timeout_tb);
257 static void wd_timer_reset(unsigned int cpu, struct timer_list *t)
259 t->expires = jiffies + msecs_to_jiffies(wd_timer_period_ms);
260 if (wd_timer_period_ms > 1000)
261 t->expires = __round_jiffies_up(t->expires, cpu);
262 add_timer_on(t, cpu);
265 static void wd_timer_fn(unsigned long data)
267 struct timer_list *t = this_cpu_ptr(&wd_timer);
268 int cpu = smp_processor_id();
270 watchdog_timer_interrupt(cpu);
272 wd_timer_reset(cpu, t);
275 void arch_touch_nmi_watchdog(void)
277 unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
278 int cpu = smp_processor_id();
281 if (tb - per_cpu(wd_timer_tb, cpu) >= ticks) {
282 per_cpu(wd_timer_tb, cpu) = tb;
283 wd_smp_clear_cpu_pending(cpu, tb);
286 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
288 static void start_watchdog_timer_on(unsigned int cpu)
290 struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
292 per_cpu(wd_timer_tb, cpu) = get_tb();
294 setup_pinned_timer(t, wd_timer_fn, 0);
295 wd_timer_reset(cpu, t);
298 static void stop_watchdog_timer_on(unsigned int cpu)
300 struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
305 static int start_wd_on_cpu(unsigned int cpu)
309 if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
314 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
317 if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
321 cpumask_set_cpu(cpu, &wd_cpus_enabled);
322 if (cpumask_weight(&wd_cpus_enabled) == 1) {
323 cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
324 wd_smp_last_reset_tb = get_tb();
326 wd_smp_unlock(&flags);
328 start_watchdog_timer_on(cpu);
333 static int stop_wd_on_cpu(unsigned int cpu)
337 if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
338 return 0; /* Can happen in CPU unplug case */
340 stop_watchdog_timer_on(cpu);
343 cpumask_clear_cpu(cpu, &wd_cpus_enabled);
344 wd_smp_unlock(&flags);
346 wd_smp_clear_cpu_pending(cpu, get_tb());
351 static void watchdog_calc_timeouts(void)
353 wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
355 /* Have the SMP detector trigger a bit later */
356 wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
358 /* 2/5 is the factor that the perf based detector uses */
359 wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
362 void watchdog_nmi_stop(void)
366 for_each_cpu(cpu, &wd_cpus_enabled)
370 void watchdog_nmi_start(void)
374 watchdog_calc_timeouts();
375 for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
376 start_wd_on_cpu(cpu);
380 * Invoked from core watchdog init.
382 int __init watchdog_nmi_probe(void)
386 err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
387 "powerpc/watchdog:online",
388 start_wd_on_cpu, stop_wd_on_cpu);
390 pr_warn("Watchdog could not be initialized");
396 static void handle_backtrace_ipi(struct pt_regs *regs)
398 nmi_cpu_backtrace(regs);
401 static void raise_backtrace_ipi(cpumask_t *mask)
405 for_each_cpu(cpu, mask) {
406 if (cpu == smp_processor_id())
407 handle_backtrace_ipi(NULL);
409 smp_send_nmi_ipi(cpu, handle_backtrace_ipi, 1000000);
413 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
415 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);