xref: /openbmc/linux/arch/powerpc/kernel/watchdog.c (revision 65417d9f)
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 	/* Do not panic from here because that can recurse into NMI IPI layer */
102 }
103 
104 static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb)
105 {
106 	cpumask_or(&wd_smp_cpus_stuck, &wd_smp_cpus_stuck, cpumask);
107 	cpumask_andnot(&wd_smp_cpus_pending, &wd_smp_cpus_pending, cpumask);
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 static void set_cpu_stuck(int cpu, u64 tb)
116 {
117 	set_cpumask_stuck(cpumask_of(cpu), tb);
118 }
119 
120 static void watchdog_smp_panic(int cpu, u64 tb)
121 {
122 	unsigned long flags;
123 	int c;
124 
125 	wd_smp_lock(&flags);
126 	/* Double check some things under lock */
127 	if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
128 		goto out;
129 	if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
130 		goto out;
131 	if (cpumask_weight(&wd_smp_cpus_pending) == 0)
132 		goto out;
133 
134 	pr_emerg("Watchdog CPU:%d detected Hard LOCKUP other CPUS:%*pbl\n",
135 			cpu, cpumask_pr_args(&wd_smp_cpus_pending));
136 
137 	if (!sysctl_hardlockup_all_cpu_backtrace) {
138 		/*
139 		 * Try to trigger the stuck CPUs, unless we are going to
140 		 * get a backtrace on all of them anyway.
141 		 */
142 		for_each_cpu(c, &wd_smp_cpus_pending) {
143 			if (c == cpu)
144 				continue;
145 			smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
146 		}
147 		smp_flush_nmi_ipi(1000000);
148 	}
149 
150 	/* Take the stuck CPUs out of the watch group */
151 	set_cpumask_stuck(&wd_smp_cpus_pending, tb);
152 
153 	wd_smp_unlock(&flags);
154 
155 	printk_safe_flush();
156 	/*
157 	 * printk_safe_flush() seems to require another print
158 	 * before anything actually goes out to console.
159 	 */
160 	if (sysctl_hardlockup_all_cpu_backtrace)
161 		trigger_allbutself_cpu_backtrace();
162 
163 	if (hardlockup_panic)
164 		nmi_panic(NULL, "Hard LOCKUP");
165 
166 	return;
167 
168 out:
169 	wd_smp_unlock(&flags);
170 }
171 
172 static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
173 {
174 	if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
175 		if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
176 			unsigned long flags;
177 
178 			pr_emerg("Watchdog CPU:%d became unstuck\n", cpu);
179 			wd_smp_lock(&flags);
180 			cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
181 			wd_smp_unlock(&flags);
182 		}
183 		return;
184 	}
185 	cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
186 	if (cpumask_empty(&wd_smp_cpus_pending)) {
187 		unsigned long flags;
188 
189 		wd_smp_lock(&flags);
190 		if (cpumask_empty(&wd_smp_cpus_pending)) {
191 			wd_smp_last_reset_tb = tb;
192 			cpumask_andnot(&wd_smp_cpus_pending,
193 					&wd_cpus_enabled,
194 					&wd_smp_cpus_stuck);
195 		}
196 		wd_smp_unlock(&flags);
197 	}
198 }
199 
200 static void watchdog_timer_interrupt(int cpu)
201 {
202 	u64 tb = get_tb();
203 
204 	per_cpu(wd_timer_tb, cpu) = tb;
205 
206 	wd_smp_clear_cpu_pending(cpu, tb);
207 
208 	if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
209 		watchdog_smp_panic(cpu, tb);
210 }
211 
212 void soft_nmi_interrupt(struct pt_regs *regs)
213 {
214 	unsigned long flags;
215 	int cpu = raw_smp_processor_id();
216 	u64 tb;
217 
218 	if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
219 		return;
220 
221 	nmi_enter();
222 
223 	__this_cpu_inc(irq_stat.soft_nmi_irqs);
224 
225 	tb = get_tb();
226 	if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
227 		per_cpu(wd_timer_tb, cpu) = tb;
228 
229 		wd_smp_lock(&flags);
230 		if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
231 			wd_smp_unlock(&flags);
232 			goto out;
233 		}
234 		set_cpu_stuck(cpu, tb);
235 
236 		pr_emerg("Watchdog CPU:%d Hard LOCKUP\n", cpu);
237 		print_modules();
238 		print_irqtrace_events(current);
239 		if (regs)
240 			show_regs(regs);
241 		else
242 			dump_stack();
243 
244 		wd_smp_unlock(&flags);
245 
246 		if (sysctl_hardlockup_all_cpu_backtrace)
247 			trigger_allbutself_cpu_backtrace();
248 
249 		if (hardlockup_panic)
250 			nmi_panic(regs, "Hard LOCKUP");
251 	}
252 	if (wd_panic_timeout_tb < 0x7fffffff)
253 		mtspr(SPRN_DEC, wd_panic_timeout_tb);
254 
255 out:
256 	nmi_exit();
257 }
258 
259 static void wd_timer_reset(unsigned int cpu, struct timer_list *t)
260 {
261 	t->expires = jiffies + msecs_to_jiffies(wd_timer_period_ms);
262 	if (wd_timer_period_ms > 1000)
263 		t->expires = __round_jiffies_up(t->expires, cpu);
264 	add_timer_on(t, cpu);
265 }
266 
267 static void wd_timer_fn(struct timer_list *t)
268 {
269 	int cpu = smp_processor_id();
270 
271 	watchdog_timer_interrupt(cpu);
272 
273 	wd_timer_reset(cpu, t);
274 }
275 
276 void arch_touch_nmi_watchdog(void)
277 {
278 	unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
279 	int cpu = smp_processor_id();
280 	u64 tb = get_tb();
281 
282 	if (tb - per_cpu(wd_timer_tb, cpu) >= ticks) {
283 		per_cpu(wd_timer_tb, cpu) = tb;
284 		wd_smp_clear_cpu_pending(cpu, tb);
285 	}
286 }
287 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
288 
289 static void start_watchdog_timer_on(unsigned int cpu)
290 {
291 	struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
292 
293 	per_cpu(wd_timer_tb, cpu) = get_tb();
294 
295 	timer_setup(t, wd_timer_fn, TIMER_PINNED);
296 	wd_timer_reset(cpu, t);
297 }
298 
299 static void stop_watchdog_timer_on(unsigned int cpu)
300 {
301 	struct timer_list *t = per_cpu_ptr(&wd_timer, cpu);
302 
303 	del_timer_sync(t);
304 }
305 
306 static int start_wd_on_cpu(unsigned int cpu)
307 {
308 	unsigned long flags;
309 
310 	if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
311 		WARN_ON(1);
312 		return 0;
313 	}
314 
315 	if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
316 		return 0;
317 
318 	if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
319 		return 0;
320 
321 	wd_smp_lock(&flags);
322 	cpumask_set_cpu(cpu, &wd_cpus_enabled);
323 	if (cpumask_weight(&wd_cpus_enabled) == 1) {
324 		cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
325 		wd_smp_last_reset_tb = get_tb();
326 	}
327 	wd_smp_unlock(&flags);
328 
329 	start_watchdog_timer_on(cpu);
330 
331 	return 0;
332 }
333 
334 static int stop_wd_on_cpu(unsigned int cpu)
335 {
336 	unsigned long flags;
337 
338 	if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
339 		return 0; /* Can happen in CPU unplug case */
340 
341 	stop_watchdog_timer_on(cpu);
342 
343 	wd_smp_lock(&flags);
344 	cpumask_clear_cpu(cpu, &wd_cpus_enabled);
345 	wd_smp_unlock(&flags);
346 
347 	wd_smp_clear_cpu_pending(cpu, get_tb());
348 
349 	return 0;
350 }
351 
352 static void watchdog_calc_timeouts(void)
353 {
354 	wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
355 
356 	/* Have the SMP detector trigger a bit later */
357 	wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
358 
359 	/* 2/5 is the factor that the perf based detector uses */
360 	wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
361 }
362 
363 void watchdog_nmi_stop(void)
364 {
365 	int cpu;
366 
367 	for_each_cpu(cpu, &wd_cpus_enabled)
368 		stop_wd_on_cpu(cpu);
369 }
370 
371 void watchdog_nmi_start(void)
372 {
373 	int cpu;
374 
375 	watchdog_calc_timeouts();
376 	for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
377 		start_wd_on_cpu(cpu);
378 }
379 
380 /*
381  * Invoked from core watchdog init.
382  */
383 int __init watchdog_nmi_probe(void)
384 {
385 	int err;
386 
387 	err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
388 					"powerpc/watchdog:online",
389 					start_wd_on_cpu, stop_wd_on_cpu);
390 	if (err < 0) {
391 		pr_warn("Watchdog could not be initialized");
392 		return err;
393 	}
394 	return 0;
395 }
396 
397 static void handle_backtrace_ipi(struct pt_regs *regs)
398 {
399 	nmi_cpu_backtrace(regs);
400 }
401 
402 static void raise_backtrace_ipi(cpumask_t *mask)
403 {
404 	unsigned int cpu;
405 
406 	for_each_cpu(cpu, mask) {
407 		if (cpu == smp_processor_id())
408 			handle_backtrace_ipi(NULL);
409 		else
410 			smp_send_nmi_ipi(cpu, handle_backtrace_ipi, 1000000);
411 	}
412 }
413 
414 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
415 {
416 	nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace_ipi);
417 }
418