1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) "watchdog: " fmt
11
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73static cpumask_t wd_cpus_enabled __read_mostly;
74
75static u64 wd_panic_timeout_tb __read_mostly;
76static u64 wd_smp_panic_timeout_tb __read_mostly;
77
78static u64 wd_timer_period_ms __read_mostly;
79
80static DEFINE_PER_CPU(struct timer_list, wd_timer);
81static DEFINE_PER_CPU(u64, wd_timer_tb);
82
83
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
93
94
95
96 raw_local_irq_save(*flags);
97 hard_irq_disable();
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 }
104}
105
106static inline void wd_smp_unlock(unsigned long *flags)
107{
108 clear_bit_unlock(0, &__wd_smp_lock);
109 raw_local_irq_restore(*flags);
110}
111
112static void wd_lockup_ipi(struct pt_regs *regs)
113{
114 int cpu = raw_smp_processor_id();
115 u64 tb = get_tb();
116
117 pr_emerg("CPU %d Hard LOCKUP\n", cpu);
118 pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n",
119 cpu, tb, per_cpu(wd_timer_tb, cpu),
120 tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000);
121 print_modules();
122 print_irqtrace_events(current);
123 if (regs)
124 show_regs(regs);
125 else
126 dump_stack();
127
128
129}
130
131static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb)
132{
133 cpumask_or(&wd_smp_cpus_stuck, &wd_smp_cpus_stuck, cpumask);
134 cpumask_andnot(&wd_smp_cpus_pending, &wd_smp_cpus_pending, cpumask);
135 if (cpumask_empty(&wd_smp_cpus_pending)) {
136 wd_smp_last_reset_tb = tb;
137 cpumask_andnot(&wd_smp_cpus_pending,
138 &wd_cpus_enabled,
139 &wd_smp_cpus_stuck);
140 }
141}
142static void set_cpu_stuck(int cpu, u64 tb)
143{
144 set_cpumask_stuck(cpumask_of(cpu), tb);
145}
146
147static void watchdog_smp_panic(int cpu, u64 tb)
148{
149 unsigned long flags;
150 int c;
151
152 wd_smp_lock(&flags);
153
154 if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
155 goto out;
156 if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
157 goto out;
158 if (cpumask_weight(&wd_smp_cpus_pending) == 0)
159 goto out;
160
161 pr_emerg("CPU %d detected hard LOCKUP on other CPUs %*pbl\n",
162 cpu, cpumask_pr_args(&wd_smp_cpus_pending));
163 pr_emerg("CPU %d TB:%lld, last SMP heartbeat TB:%lld (%lldms ago)\n",
164 cpu, tb, wd_smp_last_reset_tb,
165 tb_to_ns(tb - wd_smp_last_reset_tb) / 1000000);
166
167 if (!sysctl_hardlockup_all_cpu_backtrace) {
168
169
170
171
172 for_each_cpu(c, &wd_smp_cpus_pending) {
173 if (c == cpu)
174 continue;
175 smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
176 }
177 smp_flush_nmi_ipi(1000000);
178 }
179
180
181 set_cpumask_stuck(&wd_smp_cpus_pending, tb);
182
183 wd_smp_unlock(&flags);
184
185 printk_safe_flush();
186
187
188
189
190 if (sysctl_hardlockup_all_cpu_backtrace)
191 trigger_allbutself_cpu_backtrace();
192
193 if (hardlockup_panic)
194 nmi_panic(NULL, "Hard LOCKUP");
195
196 return;
197
198out:
199 wd_smp_unlock(&flags);
200}
201
202static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
203{
204 if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
205 if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
206 struct pt_regs *regs = get_irq_regs();
207 unsigned long flags;
208
209 wd_smp_lock(&flags);
210
211 pr_emerg("CPU %d became unstuck TB:%lld\n",
212 cpu, tb);
213 print_irqtrace_events(current);
214 if (regs)
215 show_regs(regs);
216 else
217 dump_stack();
218
219 cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
220 wd_smp_unlock(&flags);
221 }
222 return;
223 }
224 cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
225 if (cpumask_empty(&wd_smp_cpus_pending)) {
226 unsigned long flags;
227
228 wd_smp_lock(&flags);
229 if (cpumask_empty(&wd_smp_cpus_pending)) {
230 wd_smp_last_reset_tb = tb;
231 cpumask_andnot(&wd_smp_cpus_pending,
232 &wd_cpus_enabled,
233 &wd_smp_cpus_stuck);
234 }
235 wd_smp_unlock(&flags);
236 }
237}
238
239static void watchdog_timer_interrupt(int cpu)
240{
241 u64 tb = get_tb();
242
243 per_cpu(wd_timer_tb, cpu) = tb;
244
245 wd_smp_clear_cpu_pending(cpu, tb);
246
247 if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
248 watchdog_smp_panic(cpu, tb);
249}
250
251void soft_nmi_interrupt(struct pt_regs *regs)
252{
253 unsigned long flags;
254 int cpu = raw_smp_processor_id();
255 u64 tb;
256
257 if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
258 return;
259
260 nmi_enter();
261
262 __this_cpu_inc(irq_stat.soft_nmi_irqs);
263
264 tb = get_tb();
265 if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
266 wd_smp_lock(&flags);
267 if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
268 wd_smp_unlock(&flags);
269 goto out;
270 }
271 set_cpu_stuck(cpu, tb);
272
273 pr_emerg("CPU %d self-detected hard LOCKUP @ %pS\n",
274 cpu, (void *)regs->nip);
275 pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n",
276 cpu, tb, per_cpu(wd_timer_tb, cpu),
277 tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000);
278 print_modules();
279 print_irqtrace_events(current);
280 show_regs(regs);
281
282 wd_smp_unlock(&flags);
283
284 if (sysctl_hardlockup_all_cpu_backtrace)
285 trigger_allbutself_cpu_backtrace();
286
287 if (hardlockup_panic)
288 nmi_panic(regs, "Hard LOCKUP");
289 }
290 if (wd_panic_timeout_tb < 0x7fffffff)
291 mtspr(SPRN_DEC, wd_panic_timeout_tb);
292
293out:
294 nmi_exit();
295}
296
297static void wd_timer_reset(unsigned int cpu, struct timer_list *t)
298{
299 t->expires = jiffies + msecs_to_jiffies(wd_timer_period_ms);
300 if (wd_timer_period_ms > 1000)
301 t->expires = __round_jiffies_up(t->expires, cpu);
302 add_timer_on(t, cpu);
303}
304
305static void wd_timer_fn(struct timer_list *t)
306{
307 int cpu = smp_processor_id();
308
309 watchdog_timer_interrupt(cpu);
310
311 wd_timer_reset(cpu, t);
312}
313
314void arch_touch_nmi_watchdog(void)
315{
316 unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
317 int cpu = smp_processor_id();
318 u64 tb = get_tb();
319
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}
325EXPORT_SYMBOL(arch_touch_nmi_watchdog);
326
327static 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 timer_setup(t, wd_timer_fn, TIMER_PINNED);
334 wd_timer_reset(cpu, t);
335}
336
337static 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
344static 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
372static 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;
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
390static void watchdog_calc_timeouts(void)
391{
392 wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
393
394
395 wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
396
397
398 wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
399}
400
401void 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
409void 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
420
421int __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("could not be initialized");
430 return err;
431 }
432 return 0;
433}
434