powerpc/watchdog: Improve watchdog lock primitive
[karo-tx-linux.git] / arch / powerpc / kernel / watchdog.c
1 /*
2  * Watchdog support on powerpc systems.
3  *
4  * Copyright 2017, IBM Corporation.
5  *
6  * This uses code from arch/sparc/kernel/nmi.c and kernel/watchdog.c
7  */
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>
24
25 #include <asm/paca.h>
26
27 /*
28  * The watchdog has a simple timer that runs on each CPU, once per timer
29  * period. This is the heartbeat.
30  *
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.
34  *
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.
37  */
38
39 static cpumask_t wd_cpus_enabled __read_mostly;
40
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 */
43
44 static u64 wd_timer_period_ms __read_mostly;  /* interval between heartbeat */
45
46 static DEFINE_PER_CPU(struct timer_list, wd_timer);
47 static DEFINE_PER_CPU(u64, wd_timer_tb);
48
49 /*
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.
53  *
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.
58  *
59  * This will work best with NMI IPIs for crash code so the stuck CPUs
60  * can be pulled out to get their backtraces.
61  */
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;
66
67 static inline void wd_smp_lock(unsigned long *flags)
68 {
69         /*
70          * Avoid locking layers if possible.
71          * This may be called from low level interrupt handlers at some
72          * point in future.
73          */
74         raw_local_irq_save(*flags);
75         hard_irq_disable(); /* Make it soft-NMI safe */
76         while (unlikely(test_and_set_bit_lock(0, &__wd_smp_lock))) {
77                 raw_local_irq_restore(*flags);
78                 spin_until_cond(!test_bit(0, &__wd_smp_lock));
79                 raw_local_irq_save(*flags);
80                 hard_irq_disable();
81         }
82 }
83
84 static inline void wd_smp_unlock(unsigned long *flags)
85 {
86         clear_bit_unlock(0, &__wd_smp_lock);
87         raw_local_irq_restore(*flags);
88 }
89
90 static void wd_lockup_ipi(struct pt_regs *regs)
91 {
92         pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", raw_smp_processor_id());
93         print_modules();
94         print_irqtrace_events(current);
95         if (regs)
96                 show_regs(regs);
97         else
98                 dump_stack();
99
100         if (hardlockup_panic)
101                 nmi_panic(regs, "Hard LOCKUP");
102 }
103
104 static void set_cpu_stuck(int cpu, u64 tb)
105 {
106         cpumask_set_cpu(cpu, &wd_smp_cpus_stuck);
107         cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
108         if (cpumask_empty(&wd_smp_cpus_pending)) {
109                 wd_smp_last_reset_tb = tb;
110                 cpumask_andnot(&wd_smp_cpus_pending,
111                                 &wd_cpus_enabled,
112                                 &wd_smp_cpus_stuck);
113         }
114 }
115
116 static void watchdog_smp_panic(int cpu, u64 tb)
117 {
118         unsigned long flags;
119         int c;
120
121         wd_smp_lock(&flags);
122         /* Double check some things under lock */
123         if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
124                 goto out;
125         if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
126                 goto out;
127         if (cpumask_weight(&wd_smp_cpus_pending) == 0)
128                 goto out;
129
130         pr_emerg("Watchdog CPU:%d detected Hard LOCKUP other CPUS:%*pbl\n",
131                         cpu, cpumask_pr_args(&wd_smp_cpus_pending));
132
133         /*
134          * Try to trigger the stuck CPUs.
135          */
136         for_each_cpu(c, &wd_smp_cpus_pending) {
137                 if (c == cpu)
138                         continue;
139                 smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
140         }
141         smp_flush_nmi_ipi(1000000);
142
143         /* Take the stuck CPU out of the watch group */
144         for_each_cpu(c, &wd_smp_cpus_pending)
145                 set_cpu_stuck(c, tb);
146
147 out:
148         wd_smp_unlock(&flags);
149
150         printk_safe_flush();
151         /*
152          * printk_safe_flush() seems to require another print
153          * before anything actually goes out to console.
154          */
155         if (sysctl_hardlockup_all_cpu_backtrace)
156                 trigger_allbutself_cpu_backtrace();
157
158         if (hardlockup_panic)
159                 nmi_panic(NULL, "Hard LOCKUP");
160 }
161
162 static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
163 {
164         if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
165                 if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
166                         unsigned long flags;
167
168                         pr_emerg("Watchdog CPU:%d became unstuck\n", cpu);
169                         wd_smp_lock(&flags);
170                         cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
171                         wd_smp_unlock(&flags);
172                 }
173                 return;
174         }
175         cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
176         if (cpumask_empty(&wd_smp_cpus_pending)) {
177                 unsigned long flags;
178
179                 wd_smp_lock(&flags);
180                 if (cpumask_empty(&wd_smp_cpus_pending)) {
181                         wd_smp_last_reset_tb = tb;
182                         cpumask_andnot(&wd_smp_cpus_pending,
183                                         &wd_cpus_enabled,
184                                         &wd_smp_cpus_stuck);
185                 }
186                 wd_smp_unlock(&flags);
187         }
188 }
189
190 static void watchdog_timer_interrupt(int cpu)
191 {
192         u64 tb = get_tb();
193
194         per_cpu(wd_timer_tb, cpu) = tb;
195
196         wd_smp_clear_cpu_pending(cpu, tb);
197
198         if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
199                 watchdog_smp_panic(cpu, tb);
200 }
201
202 void soft_nmi_interrupt(struct pt_regs *regs)
203 {
204         unsigned long flags;
205         int cpu = raw_smp_processor_id();
206         u64 tb;
207
208         if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
209                 return;
210
211         nmi_enter();
212         tb = get_tb();
213         if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
214                 per_cpu(wd_timer_tb, cpu) = tb;
215
216                 wd_smp_lock(&flags);
217                 if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
218                         wd_smp_unlock(&flags);
219                         goto out;
220                 }
221                 set_cpu_stuck(cpu, tb);
222
223                 pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", cpu);
224                 print_modules();
225                 print_irqtrace_events(current);
226                 if (regs)
227                         show_regs(regs);
228                 else
229                         dump_stack();
230
231                 wd_smp_unlock(&flags);
232
233                 if (sysctl_hardlockup_all_cpu_backtrace)
234                         trigger_allbutself_cpu_backtrace();
235
236                 if (hardlockup_panic)
237                         nmi_panic(regs, "Hard LOCKUP");
238         }
239         if (wd_panic_timeout_tb < 0x7fffffff)
240                 mtspr(SPRN_DEC, wd_panic_timeout_tb);
241
242 out:
243         nmi_exit();
244 }
245
246 static void wd_timer_reset(unsigned int cpu, struct timer_list *t)
247 {
248         t->expires = jiffies + msecs_to_jiffies(wd_timer_period_ms);
249         if (wd_timer_period_ms > 1000)
250                 t->expires = __round_jiffies_up(t->expires, cpu);
251         add_timer_on(t, cpu);
252 }
253
254 static void wd_timer_fn(unsigned long data)
255 {
256         struct timer_list *t = this_cpu_ptr(&wd_timer);
257         int cpu = smp_processor_id();
258
259         watchdog_timer_interrupt(cpu);
260
261         wd_timer_reset(cpu, t);
262 }
263
264 void arch_touch_nmi_watchdog(void)
265 {
266         int cpu = smp_processor_id();
267
268         watchdog_timer_interrupt(cpu);
269 }
270 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
271
272 static void start_watchdog_timer_on(unsigned int cpu)
273 {
274         struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
275
276         per_cpu(wd_timer_tb, cpu) = get_tb();
277
278         setup_pinned_timer(t, wd_timer_fn, 0);
279         wd_timer_reset(cpu, t);
280 }
281
282 static void stop_watchdog_timer_on(unsigned int cpu)
283 {
284         struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
285
286         del_timer_sync(t);
287 }
288
289 static int start_wd_on_cpu(unsigned int cpu)
290 {
291         if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
292                 WARN_ON(1);
293                 return 0;
294         }
295
296         if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
297                 return 0;
298
299         if (watchdog_suspended)
300                 return 0;
301
302         if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
303                 return 0;
304
305         cpumask_set_cpu(cpu, &wd_cpus_enabled);
306         if (cpumask_weight(&wd_cpus_enabled) == 1) {
307                 cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
308                 wd_smp_last_reset_tb = get_tb();
309         }
310         smp_wmb();
311         start_watchdog_timer_on(cpu);
312
313         return 0;
314 }
315
316 static int stop_wd_on_cpu(unsigned int cpu)
317 {
318         if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
319                 return 0; /* Can happen in CPU unplug case */
320
321         stop_watchdog_timer_on(cpu);
322
323         cpumask_clear_cpu(cpu, &wd_cpus_enabled);
324         wd_smp_clear_cpu_pending(cpu, get_tb());
325
326         return 0;
327 }
328
329 static void watchdog_calc_timeouts(void)
330 {
331         wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
332
333         /* Have the SMP detector trigger a bit later */
334         wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
335
336         /* 2/5 is the factor that the perf based detector uses */
337         wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
338 }
339
340 void watchdog_nmi_reconfigure(void)
341 {
342         int cpu;
343
344         watchdog_calc_timeouts();
345
346         for_each_cpu(cpu, &wd_cpus_enabled)
347                 stop_wd_on_cpu(cpu);
348
349         for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
350                 start_wd_on_cpu(cpu);
351 }
352
353 /*
354  * This runs after lockup_detector_init() which sets up watchdog_cpumask.
355  */
356 static int __init powerpc_watchdog_init(void)
357 {
358         int err;
359
360         watchdog_calc_timeouts();
361
362         err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/watchdog:online",
363                                 start_wd_on_cpu, stop_wd_on_cpu);
364         if (err < 0)
365                 pr_warn("Watchdog could not be initialized");
366
367         return 0;
368 }
369 arch_initcall(powerpc_watchdog_init);
370
371 static void handle_backtrace_ipi(struct pt_regs *regs)
372 {
373         nmi_cpu_backtrace(regs);
374 }
375
376 static void raise_backtrace_ipi(cpumask_t *mask)
377 {
378         unsigned int cpu;
379
380         for_each_cpu(cpu, mask) {
381                 if (cpu == smp_processor_id())
382                         handle_backtrace_ipi(NULL);
383                 else
384                         smp_send_nmi_ipi(cpu, handle_backtrace_ipi, 1000000);
385         }
386 }
387
388 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
389 {
390         nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
391 }