2 * Watchdog support on powerpc systems.
4 * Copyright 2017, IBM Corporation.
6 * This uses code from arch/sparc/kernel/nmi.c and kernel/watchdog.c
8 #include <linux/kernel.h>
9 #include <linux/param.h>
10 #include <linux/init.h>
11 #include <linux/percpu.h>
12 #include <linux/cpu.h>
13 #include <linux/nmi.h>
14 #include <linux/module.h>
15 #include <linux/export.h>
16 #include <linux/kprobes.h>
17 #include <linux/hardirq.h>
18 #include <linux/reboot.h>
19 #include <linux/slab.h>
20 #include <linux/kdebug.h>
21 #include <linux/sched/debug.h>
22 #include <linux/delay.h>
23 #include <linux/smp.h>
28 * The watchdog has a simple timer that runs on each CPU, once per timer
29 * period. This is the heartbeat.
31 * Then there are checks to see if the heartbeat has not triggered on a CPU
32 * for the panic timeout period. Currently the watchdog only supports an
33 * SMP check, so the heartbeat only turns on when we have 2 or more CPUs.
35 * This is not an NMI watchdog, but Linux uses that name for a generic
36 * watchdog in some cases, so NMI gets used in some places.
39 static cpumask_t wd_cpus_enabled __read_mostly;
41 static u64 wd_panic_timeout_tb __read_mostly; /* timebase ticks until panic */
42 static u64 wd_smp_panic_timeout_tb __read_mostly; /* panic other CPUs */
44 static u64 wd_timer_period_ms __read_mostly; /* interval between heartbeat */
46 static DEFINE_PER_CPU(struct timer_list, wd_timer);
47 static DEFINE_PER_CPU(u64, wd_timer_tb);
50 * These are for the SMP checker. CPUs clear their pending bit in their
51 * heartbeat. If the bitmask becomes empty, the time is noted and the
52 * bitmask is refilled.
54 * All CPUs clear their bit in the pending mask every timer period.
55 * Once all have cleared, the time is noted and the bits are reset.
56 * If the time since all clear was greater than the panic timeout,
57 * we can panic with the list of stuck CPUs.
59 * This will work best with NMI IPIs for crash code so the stuck CPUs
60 * can be pulled out to get their backtraces.
62 static unsigned long __wd_smp_lock;
63 static cpumask_t wd_smp_cpus_pending;
64 static cpumask_t wd_smp_cpus_stuck;
65 static u64 wd_smp_last_reset_tb;
67 static inline void wd_smp_lock(unsigned long *flags)
70 * Avoid locking layers if possible.
71 * This may be called from low level interrupt handlers at some
74 local_irq_save(*flags);
75 while (unlikely(test_and_set_bit_lock(0, &__wd_smp_lock)))
79 static inline void wd_smp_unlock(unsigned long *flags)
81 clear_bit_unlock(0, &__wd_smp_lock);
82 local_irq_restore(*flags);
85 static void wd_lockup_ipi(struct pt_regs *regs)
87 pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", raw_smp_processor_id());
89 print_irqtrace_events(current);
96 nmi_panic(regs, "Hard LOCKUP");
99 static void set_cpu_stuck(int cpu, u64 tb)
101 cpumask_set_cpu(cpu, &wd_smp_cpus_stuck);
102 cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
103 if (cpumask_empty(&wd_smp_cpus_pending)) {
104 wd_smp_last_reset_tb = tb;
105 cpumask_andnot(&wd_smp_cpus_pending,
111 static void watchdog_smp_panic(int cpu, u64 tb)
117 /* Double check some things under lock */
118 if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
120 if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
122 if (cpumask_weight(&wd_smp_cpus_pending) == 0)
125 pr_emerg("Watchdog CPU:%d detected Hard LOCKUP other CPUS:%*pbl\n",
126 cpu, cpumask_pr_args(&wd_smp_cpus_pending));
129 * Try to trigger the stuck CPUs.
131 for_each_cpu(c, &wd_smp_cpus_pending) {
134 smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
136 smp_flush_nmi_ipi(1000000);
138 /* Take the stuck CPU out of the watch group */
139 for_each_cpu(c, &wd_smp_cpus_pending)
140 set_cpu_stuck(c, tb);
143 wd_smp_unlock(&flags);
147 * printk_safe_flush() seems to require another print
148 * before anything actually goes out to console.
150 if (sysctl_hardlockup_all_cpu_backtrace)
151 trigger_allbutself_cpu_backtrace();
153 if (hardlockup_panic)
154 nmi_panic(NULL, "Hard LOCKUP");
157 static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
159 if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
160 if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
163 pr_emerg("Watchdog CPU:%d became unstuck\n", cpu);
165 cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
166 wd_smp_unlock(&flags);
170 cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
171 if (cpumask_empty(&wd_smp_cpus_pending)) {
175 if (cpumask_empty(&wd_smp_cpus_pending)) {
176 wd_smp_last_reset_tb = tb;
177 cpumask_andnot(&wd_smp_cpus_pending,
181 wd_smp_unlock(&flags);
185 static void watchdog_timer_interrupt(int cpu)
189 per_cpu(wd_timer_tb, cpu) = tb;
191 wd_smp_clear_cpu_pending(cpu, tb);
193 if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
194 watchdog_smp_panic(cpu, tb);
197 void soft_nmi_interrupt(struct pt_regs *regs)
200 int cpu = raw_smp_processor_id();
203 if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
208 if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
209 per_cpu(wd_timer_tb, cpu) = tb;
212 if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
213 wd_smp_unlock(&flags);
216 set_cpu_stuck(cpu, tb);
218 pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", cpu);
220 print_irqtrace_events(current);
226 wd_smp_unlock(&flags);
228 if (sysctl_hardlockup_all_cpu_backtrace)
229 trigger_allbutself_cpu_backtrace();
231 if (hardlockup_panic)
232 nmi_panic(regs, "Hard LOCKUP");
234 if (wd_panic_timeout_tb < 0x7fffffff)
235 mtspr(SPRN_DEC, wd_panic_timeout_tb);
241 static void wd_timer_reset(unsigned int cpu, struct timer_list *t)
243 t->expires = jiffies + msecs_to_jiffies(wd_timer_period_ms);
244 if (wd_timer_period_ms > 1000)
245 t->expires = __round_jiffies_up(t->expires, cpu);
246 add_timer_on(t, cpu);
249 static void wd_timer_fn(unsigned long data)
251 struct timer_list *t = this_cpu_ptr(&wd_timer);
252 int cpu = smp_processor_id();
254 watchdog_timer_interrupt(cpu);
256 wd_timer_reset(cpu, t);
259 void arch_touch_nmi_watchdog(void)
261 int cpu = smp_processor_id();
263 watchdog_timer_interrupt(cpu);
265 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
267 static void start_watchdog_timer_on(unsigned int cpu)
269 struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
271 per_cpu(wd_timer_tb, cpu) = get_tb();
273 setup_pinned_timer(t, wd_timer_fn, 0);
274 wd_timer_reset(cpu, t);
277 static void stop_watchdog_timer_on(unsigned int cpu)
279 struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
284 static int start_wd_on_cpu(unsigned int cpu)
286 if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
291 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
294 if (watchdog_suspended)
297 if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
300 cpumask_set_cpu(cpu, &wd_cpus_enabled);
301 if (cpumask_weight(&wd_cpus_enabled) == 1) {
302 cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
303 wd_smp_last_reset_tb = get_tb();
306 start_watchdog_timer_on(cpu);
311 static int stop_wd_on_cpu(unsigned int cpu)
313 if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
314 return 0; /* Can happen in CPU unplug case */
316 stop_watchdog_timer_on(cpu);
318 cpumask_clear_cpu(cpu, &wd_cpus_enabled);
319 wd_smp_clear_cpu_pending(cpu, get_tb());
324 static void watchdog_calc_timeouts(void)
326 wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
328 /* Have the SMP detector trigger a bit later */
329 wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
331 /* 2/5 is the factor that the perf based detector uses */
332 wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
335 void watchdog_nmi_reconfigure(void)
339 watchdog_calc_timeouts();
341 for_each_cpu(cpu, &wd_cpus_enabled)
344 for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
345 start_wd_on_cpu(cpu);
349 * This runs after lockup_detector_init() which sets up watchdog_cpumask.
351 static int __init powerpc_watchdog_init(void)
355 watchdog_calc_timeouts();
357 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/watchdog:online",
358 start_wd_on_cpu, stop_wd_on_cpu);
360 pr_warn("Watchdog could not be initialized");
364 arch_initcall(powerpc_watchdog_init);
366 static void handle_backtrace_ipi(struct pt_regs *regs)
368 nmi_cpu_backtrace(regs);
371 static void raise_backtrace_ipi(cpumask_t *mask)
375 for_each_cpu(cpu, mask) {
376 if (cpu == smp_processor_id())
377 handle_backtrace_ipi(NULL);
379 smp_send_nmi_ipi(cpu, handle_backtrace_ipi, 1000000);
383 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
385 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);