GNU Linux-libre 4.14.328-gnu1
[releases.git] / arch / powerpc / kernel / watchdog.c
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 watchdog has a simple timer that runs on each CPU, once per timer
30  * period. This is the heartbeat.
31  *
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.
35  *
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.
38  */
39
40 static cpumask_t wd_cpus_enabled __read_mostly;
41
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 */
44
45 static u64 wd_timer_period_ms __read_mostly;  /* interval between heartbeat */
46
47 static DEFINE_PER_CPU(struct timer_list, wd_timer);
48 static DEFINE_PER_CPU(u64, wd_timer_tb);
49
50 /*
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.
54  *
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.
59  *
60  * This will work best with NMI IPIs for crash code so the stuck CPUs
61  * can be pulled out to get their backtraces.
62  */
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;
67
68 static inline void wd_smp_lock(unsigned long *flags)
69 {
70         /*
71          * Avoid locking layers if possible.
72          * This may be called from low level interrupt handlers at some
73          * point in future.
74          */
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);
81                 hard_irq_disable();
82         }
83 }
84
85 static inline void wd_smp_unlock(unsigned long *flags)
86 {
87         clear_bit_unlock(0, &__wd_smp_lock);
88         raw_local_irq_restore(*flags);
89 }
90
91 static void wd_lockup_ipi(struct pt_regs *regs)
92 {
93         pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", raw_smp_processor_id());
94         print_modules();
95         print_irqtrace_events(current);
96         if (regs)
97                 show_regs(regs);
98         else
99                 dump_stack();
100
101         if (hardlockup_panic)
102                 nmi_panic(regs, "Hard LOCKUP");
103 }
104
105 static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb)
106 {
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         /*
110          * See wd_smp_clear_cpu_pending()
111          */
112         smp_mb();
113         if (cpumask_empty(&wd_smp_cpus_pending)) {
114                 wd_smp_last_reset_tb = tb;
115                 cpumask_andnot(&wd_smp_cpus_pending,
116                                 &wd_cpus_enabled,
117                                 &wd_smp_cpus_stuck);
118         }
119 }
120 static void set_cpu_stuck(int cpu, u64 tb)
121 {
122         set_cpumask_stuck(cpumask_of(cpu), tb);
123 }
124
125 static void watchdog_smp_panic(int cpu, u64 tb)
126 {
127         unsigned long flags;
128         int c;
129
130         wd_smp_lock(&flags);
131         /* Double check some things under lock */
132         if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
133                 goto out;
134         if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
135                 goto out;
136         if (cpumask_weight(&wd_smp_cpus_pending) == 0)
137                 goto out;
138
139         pr_emerg("Watchdog CPU:%d detected Hard LOCKUP other CPUS:%*pbl\n",
140                         cpu, cpumask_pr_args(&wd_smp_cpus_pending));
141
142         /*
143          * Try to trigger the stuck CPUs.
144          */
145         for_each_cpu(c, &wd_smp_cpus_pending) {
146                 if (c == cpu)
147                         continue;
148                 smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
149         }
150         smp_flush_nmi_ipi(1000000);
151
152         /* Take the stuck CPUs out of the watch group */
153         set_cpumask_stuck(&wd_smp_cpus_pending, tb);
154
155         wd_smp_unlock(&flags);
156
157         printk_safe_flush();
158         /*
159          * printk_safe_flush() seems to require another print
160          * before anything actually goes out to console.
161          */
162         if (sysctl_hardlockup_all_cpu_backtrace)
163                 trigger_allbutself_cpu_backtrace();
164
165         if (hardlockup_panic)
166                 nmi_panic(NULL, "Hard LOCKUP");
167
168         return;
169
170 out:
171         wd_smp_unlock(&flags);
172 }
173
174 static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
175 {
176         if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
177                 if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
178                         unsigned long flags;
179
180                         pr_emerg("Watchdog CPU:%d became unstuck\n", cpu);
181                         wd_smp_lock(&flags);
182                         cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
183                         wd_smp_unlock(&flags);
184                 } else {
185                         /*
186                          * The last CPU to clear pending should have reset the
187                          * watchdog so we generally should not find it empty
188                          * here if our CPU was clear. However it could happen
189                          * due to a rare race with another CPU taking the
190                          * last CPU out of the mask concurrently.
191                          *
192                          * We can't add a warning for it. But just in case
193                          * there is a problem with the watchdog that is causing
194                          * the mask to not be reset, try to kick it along here.
195                          */
196                         if (unlikely(cpumask_empty(&wd_smp_cpus_pending)))
197                                 goto none_pending;
198                 }
199                 return;
200         }
201
202         cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
203
204         /*
205          * Order the store to clear pending with the load(s) to check all
206          * words in the pending mask to check they are all empty. This orders
207          * with the same barrier on another CPU. This prevents two CPUs
208          * clearing the last 2 pending bits, but neither seeing the other's
209          * store when checking if the mask is empty, and missing an empty
210          * mask, which ends with a false positive.
211          */
212         smp_mb();
213         if (cpumask_empty(&wd_smp_cpus_pending)) {
214                 unsigned long flags;
215
216 none_pending:
217                 /*
218                  * Double check under lock because more than one CPU could see
219                  * a clear mask with the lockless check after clearing their
220                  * pending bits.
221                  */
222                 wd_smp_lock(&flags);
223                 if (cpumask_empty(&wd_smp_cpus_pending)) {
224                         wd_smp_last_reset_tb = tb;
225                         cpumask_andnot(&wd_smp_cpus_pending,
226                                         &wd_cpus_enabled,
227                                         &wd_smp_cpus_stuck);
228                 }
229                 wd_smp_unlock(&flags);
230         }
231 }
232
233 static void watchdog_timer_interrupt(int cpu)
234 {
235         u64 tb = get_tb();
236
237         per_cpu(wd_timer_tb, cpu) = tb;
238
239         wd_smp_clear_cpu_pending(cpu, tb);
240
241         if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
242                 watchdog_smp_panic(cpu, tb);
243 }
244
245 void soft_nmi_interrupt(struct pt_regs *regs)
246 {
247         unsigned long flags;
248         int cpu = raw_smp_processor_id();
249         u64 tb;
250
251         if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
252                 return;
253
254         nmi_enter();
255
256         __this_cpu_inc(irq_stat.soft_nmi_irqs);
257
258         tb = get_tb();
259         if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
260                 per_cpu(wd_timer_tb, cpu) = tb;
261
262                 wd_smp_lock(&flags);
263                 if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
264                         wd_smp_unlock(&flags);
265                         goto out;
266                 }
267                 set_cpu_stuck(cpu, tb);
268
269                 pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", cpu);
270                 print_modules();
271                 print_irqtrace_events(current);
272                 if (regs)
273                         show_regs(regs);
274                 else
275                         dump_stack();
276
277                 wd_smp_unlock(&flags);
278
279                 if (sysctl_hardlockup_all_cpu_backtrace)
280                         trigger_allbutself_cpu_backtrace();
281
282                 if (hardlockup_panic)
283                         nmi_panic(regs, "Hard LOCKUP");
284         }
285         if (wd_panic_timeout_tb < 0x7fffffff)
286                 mtspr(SPRN_DEC, wd_panic_timeout_tb);
287
288 out:
289         nmi_exit();
290 }
291
292 static void wd_timer_reset(unsigned int cpu, struct timer_list *t)
293 {
294         t->expires = jiffies + msecs_to_jiffies(wd_timer_period_ms);
295         if (wd_timer_period_ms > 1000)
296                 t->expires = __round_jiffies_up(t->expires, cpu);
297         add_timer_on(t, cpu);
298 }
299
300 static void wd_timer_fn(unsigned long data)
301 {
302         struct timer_list *t = this_cpu_ptr(&wd_timer);
303         int cpu = smp_processor_id();
304
305         watchdog_timer_interrupt(cpu);
306
307         wd_timer_reset(cpu, t);
308 }
309
310 void arch_touch_nmi_watchdog(void)
311 {
312         unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
313         int cpu = smp_processor_id();
314         u64 tb;
315
316         if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
317                 return;
318
319         tb = get_tb();
320         if (tb - per_cpu(wd_timer_tb, cpu) >= ticks) {
321                 per_cpu(wd_timer_tb, cpu) = tb;
322                 wd_smp_clear_cpu_pending(cpu, tb);
323         }
324 }
325 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
326
327 static void start_watchdog_timer_on(unsigned int cpu)
328 {
329         struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
330
331         per_cpu(wd_timer_tb, cpu) = get_tb();
332
333         setup_pinned_timer(t, wd_timer_fn, 0);
334         wd_timer_reset(cpu, t);
335 }
336
337 static void stop_watchdog_timer_on(unsigned int cpu)
338 {
339         struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
340
341         del_timer_sync(t);
342 }
343
344 static int start_wd_on_cpu(unsigned int cpu)
345 {
346         unsigned long flags;
347
348         if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
349                 WARN_ON(1);
350                 return 0;
351         }
352
353         if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
354                 return 0;
355
356         if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
357                 return 0;
358
359         wd_smp_lock(&flags);
360         cpumask_set_cpu(cpu, &wd_cpus_enabled);
361         if (cpumask_weight(&wd_cpus_enabled) == 1) {
362                 cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
363                 wd_smp_last_reset_tb = get_tb();
364         }
365         wd_smp_unlock(&flags);
366
367         start_watchdog_timer_on(cpu);
368
369         return 0;
370 }
371
372 static int stop_wd_on_cpu(unsigned int cpu)
373 {
374         unsigned long flags;
375
376         if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
377                 return 0; /* Can happen in CPU unplug case */
378
379         stop_watchdog_timer_on(cpu);
380
381         wd_smp_lock(&flags);
382         cpumask_clear_cpu(cpu, &wd_cpus_enabled);
383         wd_smp_unlock(&flags);
384
385         wd_smp_clear_cpu_pending(cpu, get_tb());
386
387         return 0;
388 }
389
390 static void watchdog_calc_timeouts(void)
391 {
392         wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
393
394         /* Have the SMP detector trigger a bit later */
395         wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
396
397         /* 2/5 is the factor that the perf based detector uses */
398         wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
399 }
400
401 void watchdog_nmi_stop(void)
402 {
403         int cpu;
404
405         for_each_cpu(cpu, &wd_cpus_enabled)
406                 stop_wd_on_cpu(cpu);
407 }
408
409 void watchdog_nmi_start(void)
410 {
411         int cpu;
412
413         watchdog_calc_timeouts();
414         for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
415                 start_wd_on_cpu(cpu);
416 }
417
418 /*
419  * Invoked from core watchdog init.
420  */
421 int __init watchdog_nmi_probe(void)
422 {
423         int err;
424
425         err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
426                                         "powerpc/watchdog:online",
427                                         start_wd_on_cpu, stop_wd_on_cpu);
428         if (err < 0) {
429                 pr_warn("Watchdog could not be initialized");
430                 return err;
431         }
432         return 0;
433 }
434
435 static void handle_backtrace_ipi(struct pt_regs *regs)
436 {
437         nmi_cpu_backtrace(regs);
438 }
439
440 static void raise_backtrace_ipi(cpumask_t *mask)
441 {
442         unsigned int cpu;
443
444         for_each_cpu(cpu, mask) {
445                 if (cpu == smp_processor_id())
446                         handle_backtrace_ipi(NULL);
447                 else
448                         smp_send_nmi_ipi(cpu, handle_backtrace_ipi, 1000000);
449         }
450 }
451
452 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
453 {
454         nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
455 }