Merge tag 'powerpc-4.16-1' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux-2.6-block.git] / arch / powerpc / kernel / watchdog.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
2104180a
NP
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 */
d8fa82e0
ME
9
10#define pr_fmt(fmt) "watchdog: " fmt
11
2104180a
NP
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/*
723b1133
NP
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.
2104180a 36 *
723b1133
NP
37 * Then there are two systems to check that the heartbeat is still running.
38 * The local soft-NMI, and the SMP checker.
2104180a 39 *
723b1133
NP
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 *
67 * Some platforms implement true NMI IPIs, which can by used by the SMP
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.
2104180a
NP
71 */
72
73static cpumask_t wd_cpus_enabled __read_mostly;
74
75static u64 wd_panic_timeout_tb __read_mostly; /* timebase ticks until panic */
76static u64 wd_smp_panic_timeout_tb __read_mostly; /* panic other CPUs */
77
78static u64 wd_timer_period_ms __read_mostly; /* interval between heartbeat */
79
80static DEFINE_PER_CPU(struct timer_list, wd_timer);
81static DEFINE_PER_CPU(u64, wd_timer_tb);
82
723b1133 83/* SMP checker bits */
2104180a
NP
84static unsigned long __wd_smp_lock;
85static cpumask_t wd_smp_cpus_pending;
86static cpumask_t wd_smp_cpus_stuck;
87static u64 wd_smp_last_reset_tb;
88
89static 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 */
d8e2a405
NP
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 }
2104180a
NP
104}
105
106static inline void wd_smp_unlock(unsigned long *flags)
107{
108 clear_bit_unlock(0, &__wd_smp_lock);
d8e2a405 109 raw_local_irq_restore(*flags);
2104180a
NP
110}
111
112static void wd_lockup_ipi(struct pt_regs *regs)
113{
d8fa82e0 114 pr_emerg("CPU %d Hard LOCKUP\n", raw_smp_processor_id());
2104180a
NP
115 print_modules();
116 print_irqtrace_events(current);
117 if (regs)
118 show_regs(regs);
119 else
120 dump_stack();
121
842dc1db 122 /* Do not panic from here because that can recurse into NMI IPI layer */
2104180a
NP
123}
124
87607a30 125static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb)
2104180a 126{
87607a30
NP
127 cpumask_or(&wd_smp_cpus_stuck, &wd_smp_cpus_stuck, cpumask);
128 cpumask_andnot(&wd_smp_cpus_pending, &wd_smp_cpus_pending, cpumask);
2104180a
NP
129 if (cpumask_empty(&wd_smp_cpus_pending)) {
130 wd_smp_last_reset_tb = tb;
131 cpumask_andnot(&wd_smp_cpus_pending,
132 &wd_cpus_enabled,
133 &wd_smp_cpus_stuck);
134 }
135}
87607a30
NP
136static void set_cpu_stuck(int cpu, u64 tb)
137{
138 set_cpumask_stuck(cpumask_of(cpu), tb);
139}
2104180a
NP
140
141static void watchdog_smp_panic(int cpu, u64 tb)
142{
143 unsigned long flags;
144 int c;
145
146 wd_smp_lock(&flags);
147 /* Double check some things under lock */
148 if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
149 goto out;
150 if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
151 goto out;
152 if (cpumask_weight(&wd_smp_cpus_pending) == 0)
153 goto out;
154
d8fa82e0
ME
155 pr_emerg("CPU %d detected hard LOCKUP on other CPUs %*pbl\n",
156 cpu, cpumask_pr_args(&wd_smp_cpus_pending));
2104180a 157
d58fdd9d
NP
158 if (!sysctl_hardlockup_all_cpu_backtrace) {
159 /*
160 * Try to trigger the stuck CPUs, unless we are going to
161 * get a backtrace on all of them anyway.
162 */
163 for_each_cpu(c, &wd_smp_cpus_pending) {
164 if (c == cpu)
165 continue;
166 smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
167 }
168 smp_flush_nmi_ipi(1000000);
2104180a 169 }
2104180a 170
87607a30
NP
171 /* Take the stuck CPUs out of the watch group */
172 set_cpumask_stuck(&wd_smp_cpus_pending, tb);
2104180a 173
2104180a
NP
174 wd_smp_unlock(&flags);
175
176 printk_safe_flush();
177 /*
178 * printk_safe_flush() seems to require another print
179 * before anything actually goes out to console.
180 */
181 if (sysctl_hardlockup_all_cpu_backtrace)
182 trigger_allbutself_cpu_backtrace();
183
184 if (hardlockup_panic)
185 nmi_panic(NULL, "Hard LOCKUP");
8e236921
NP
186
187 return;
188
189out:
190 wd_smp_unlock(&flags);
2104180a
NP
191}
192
193static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
194{
195 if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
196 if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
197 unsigned long flags;
198
d8fa82e0 199 pr_emerg("CPU %d became unstuck\n", cpu);
2104180a
NP
200 wd_smp_lock(&flags);
201 cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
202 wd_smp_unlock(&flags);
203 }
204 return;
205 }
206 cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
207 if (cpumask_empty(&wd_smp_cpus_pending)) {
208 unsigned long flags;
209
210 wd_smp_lock(&flags);
211 if (cpumask_empty(&wd_smp_cpus_pending)) {
212 wd_smp_last_reset_tb = tb;
213 cpumask_andnot(&wd_smp_cpus_pending,
214 &wd_cpus_enabled,
215 &wd_smp_cpus_stuck);
216 }
217 wd_smp_unlock(&flags);
218 }
219}
220
221static void watchdog_timer_interrupt(int cpu)
222{
223 u64 tb = get_tb();
224
225 per_cpu(wd_timer_tb, cpu) = tb;
226
227 wd_smp_clear_cpu_pending(cpu, tb);
228
229 if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
230 watchdog_smp_panic(cpu, tb);
231}
232
233void soft_nmi_interrupt(struct pt_regs *regs)
234{
235 unsigned long flags;
236 int cpu = raw_smp_processor_id();
237 u64 tb;
238
239 if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
240 return;
241
242 nmi_enter();
04019bf8
NP
243
244 __this_cpu_inc(irq_stat.soft_nmi_irqs);
245
2104180a
NP
246 tb = get_tb();
247 if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
248 per_cpu(wd_timer_tb, cpu) = tb;
249
250 wd_smp_lock(&flags);
251 if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
252 wd_smp_unlock(&flags);
253 goto out;
254 }
255 set_cpu_stuck(cpu, tb);
256
0bc00914 257 pr_emerg("CPU %d self-detected hard LOCKUP @ %pS\n", cpu, (void *)regs->nip);
2104180a
NP
258 print_modules();
259 print_irqtrace_events(current);
3ba45b7e 260 show_regs(regs);
2104180a
NP
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
273out:
274 nmi_exit();
275}
276
277static 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
5943cf4a 285static void wd_timer_fn(struct timer_list *t)
2104180a 286{
2104180a
NP
287 int cpu = smp_processor_id();
288
289 watchdog_timer_interrupt(cpu);
290
291 wd_timer_reset(cpu, t);
292}
293
294void arch_touch_nmi_watchdog(void)
295{
26c5c6e1 296 unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
2104180a 297 int cpu = smp_processor_id();
80e4d70b 298 u64 tb = get_tb();
2104180a 299
80e4d70b
NP
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 }
2104180a
NP
304}
305EXPORT_SYMBOL(arch_touch_nmi_watchdog);
306
307static 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
5943cf4a 313 timer_setup(t, wd_timer_fn, TIMER_PINNED);
2104180a
NP
314 wd_timer_reset(cpu, t);
315}
316
317static 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
324static int start_wd_on_cpu(unsigned int cpu)
325{
96ea91e7
NP
326 unsigned long flags;
327
2104180a
NP
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
2104180a
NP
336 if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
337 return 0;
338
96ea91e7 339 wd_smp_lock(&flags);
2104180a
NP
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 }
96ea91e7
NP
345 wd_smp_unlock(&flags);
346
2104180a
NP
347 start_watchdog_timer_on(cpu);
348
349 return 0;
350}
351
352static int stop_wd_on_cpu(unsigned int cpu)
353{
96ea91e7
NP
354 unsigned long flags;
355
2104180a
NP
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
96ea91e7 361 wd_smp_lock(&flags);
2104180a 362 cpumask_clear_cpu(cpu, &wd_cpus_enabled);
96ea91e7
NP
363 wd_smp_unlock(&flags);
364
2104180a
NP
365 wd_smp_clear_cpu_pending(cpu, get_tb());
366
367 return 0;
368}
369
370static 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
6b9dc480 381void watchdog_nmi_stop(void)
2104180a
NP
382{
383 int cpu;
384
6b9dc480
TG
385 for_each_cpu(cpu, &wd_cpus_enabled)
386 stop_wd_on_cpu(cpu);
6b9dc480
TG
387}
388
389void watchdog_nmi_start(void)
390{
391 int cpu;
392
6b9dc480
TG
393 watchdog_calc_timeouts();
394 for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
395 start_wd_on_cpu(cpu);
2104180a
NP
396}
397
398/*
34ddaa3e 399 * Invoked from core watchdog init.
2104180a 400 */
34ddaa3e 401int __init watchdog_nmi_probe(void)
2104180a
NP
402{
403 int err;
404
34ddaa3e
TG
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) {
d8fa82e0 409 pr_warn("could not be initialized");
34ddaa3e
TG
410 return err;
411 }
2104180a
NP
412 return 0;
413}