xref: /openbmc/linux/arch/powerpc/kernel/watchdog.c (revision c4f7ac64)
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 
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/interrupt.h>
30 #include <asm/paca.h>
31 #include <asm/nmi.h>
32 
33 /*
34  * The powerpc watchdog ensures that each CPU is able to service timers.
35  * The watchdog sets up a simple timer on each CPU to run once per timer
36  * period, and updates a per-cpu timestamp and a "pending" cpumask. This is
37  * the heartbeat.
38  *
39  * Then there are two systems to check that the heartbeat is still running.
40  * The local soft-NMI, and the SMP checker.
41  *
42  * The soft-NMI checker can detect lockups on the local CPU. When interrupts
43  * are disabled with local_irq_disable(), platforms that use soft-masking
44  * can leave hardware interrupts enabled and handle them with a masked
45  * interrupt handler. The masked handler can send the timer interrupt to the
46  * watchdog's soft_nmi_interrupt(), which appears to Linux as an NMI
47  * interrupt, and can be used to detect CPUs stuck with IRQs disabled.
48  *
49  * The soft-NMI checker will compare the heartbeat timestamp for this CPU
50  * with the current time, and take action if the difference exceeds the
51  * watchdog threshold.
52  *
53  * The limitation of the soft-NMI watchdog is that it does not work when
54  * interrupts are hard disabled or otherwise not being serviced. This is
55  * solved by also having a SMP watchdog where all CPUs check all other
56  * CPUs heartbeat.
57  *
58  * The SMP checker can detect lockups on other CPUs. A gobal "pending"
59  * cpumask is kept, containing all CPUs which enable the watchdog. Each
60  * CPU clears their pending bit in their heartbeat timer. When the bitmask
61  * becomes empty, the last CPU to clear its pending bit updates a global
62  * timestamp and refills the pending bitmask.
63  *
64  * In the heartbeat timer, if any CPU notices that the global timestamp has
65  * not been updated for a period exceeding the watchdog threshold, then it
66  * means the CPU(s) with their bit still set in the pending mask have had
67  * their heartbeat stop, and action is taken.
68  *
69  * Some platforms implement true NMI IPIs, which can be used by the SMP
70  * watchdog to detect an unresponsive CPU and pull it out of its stuck
71  * state with the NMI IPI, to get crash/debug data from it. This way the
72  * SMP watchdog can detect hardware interrupts off lockups.
73  */
74 
75 static cpumask_t wd_cpus_enabled __read_mostly;
76 
77 static u64 wd_panic_timeout_tb __read_mostly; /* timebase ticks until panic */
78 static u64 wd_smp_panic_timeout_tb __read_mostly; /* panic other CPUs */
79 
80 static u64 wd_timer_period_ms __read_mostly;  /* interval between heartbeat */
81 
82 static DEFINE_PER_CPU(struct hrtimer, wd_hrtimer);
83 static DEFINE_PER_CPU(u64, wd_timer_tb);
84 
85 /* SMP checker bits */
86 static unsigned long __wd_smp_lock;
87 static cpumask_t wd_smp_cpus_pending;
88 static cpumask_t wd_smp_cpus_stuck;
89 static u64 wd_smp_last_reset_tb;
90 
91 static inline void wd_smp_lock(unsigned long *flags)
92 {
93 	/*
94 	 * Avoid locking layers if possible.
95 	 * This may be called from low level interrupt handlers at some
96 	 * point in future.
97 	 */
98 	raw_local_irq_save(*flags);
99 	hard_irq_disable(); /* Make it soft-NMI safe */
100 	while (unlikely(test_and_set_bit_lock(0, &__wd_smp_lock))) {
101 		raw_local_irq_restore(*flags);
102 		spin_until_cond(!test_bit(0, &__wd_smp_lock));
103 		raw_local_irq_save(*flags);
104 		hard_irq_disable();
105 	}
106 }
107 
108 static inline void wd_smp_unlock(unsigned long *flags)
109 {
110 	clear_bit_unlock(0, &__wd_smp_lock);
111 	raw_local_irq_restore(*flags);
112 }
113 
114 static void wd_lockup_ipi(struct pt_regs *regs)
115 {
116 	int cpu = raw_smp_processor_id();
117 	u64 tb = get_tb();
118 
119 	pr_emerg("CPU %d Hard LOCKUP\n", cpu);
120 	pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n",
121 		 cpu, tb, per_cpu(wd_timer_tb, cpu),
122 		 tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000);
123 	print_modules();
124 	print_irqtrace_events(current);
125 	if (regs)
126 		show_regs(regs);
127 	else
128 		dump_stack();
129 
130 	/* Do not panic from here because that can recurse into NMI IPI layer */
131 }
132 
133 static void set_cpumask_stuck(const struct cpumask *cpumask, u64 tb)
134 {
135 	cpumask_or(&wd_smp_cpus_stuck, &wd_smp_cpus_stuck, cpumask);
136 	cpumask_andnot(&wd_smp_cpus_pending, &wd_smp_cpus_pending, cpumask);
137 	if (cpumask_empty(&wd_smp_cpus_pending)) {
138 		wd_smp_last_reset_tb = tb;
139 		cpumask_andnot(&wd_smp_cpus_pending,
140 				&wd_cpus_enabled,
141 				&wd_smp_cpus_stuck);
142 	}
143 }
144 static void set_cpu_stuck(int cpu, u64 tb)
145 {
146 	set_cpumask_stuck(cpumask_of(cpu), tb);
147 }
148 
149 static void watchdog_smp_panic(int cpu, u64 tb)
150 {
151 	unsigned long flags;
152 	int c;
153 
154 	wd_smp_lock(&flags);
155 	/* Double check some things under lock */
156 	if ((s64)(tb - wd_smp_last_reset_tb) < (s64)wd_smp_panic_timeout_tb)
157 		goto out;
158 	if (cpumask_test_cpu(cpu, &wd_smp_cpus_pending))
159 		goto out;
160 	if (cpumask_weight(&wd_smp_cpus_pending) == 0)
161 		goto out;
162 
163 	pr_emerg("CPU %d detected hard LOCKUP on other CPUs %*pbl\n",
164 		 cpu, cpumask_pr_args(&wd_smp_cpus_pending));
165 	pr_emerg("CPU %d TB:%lld, last SMP heartbeat TB:%lld (%lldms ago)\n",
166 		 cpu, tb, wd_smp_last_reset_tb,
167 		 tb_to_ns(tb - wd_smp_last_reset_tb) / 1000000);
168 
169 	if (!sysctl_hardlockup_all_cpu_backtrace) {
170 		/*
171 		 * Try to trigger the stuck CPUs, unless we are going to
172 		 * get a backtrace on all of them anyway.
173 		 */
174 		for_each_cpu(c, &wd_smp_cpus_pending) {
175 			if (c == cpu)
176 				continue;
177 			smp_send_nmi_ipi(c, wd_lockup_ipi, 1000000);
178 		}
179 	}
180 
181 	/* Take the stuck CPUs out of the watch group */
182 	set_cpumask_stuck(&wd_smp_cpus_pending, tb);
183 
184 	wd_smp_unlock(&flags);
185 
186 	printk_safe_flush();
187 	/*
188 	 * printk_safe_flush() seems to require another print
189 	 * before anything actually goes out to console.
190 	 */
191 	if (sysctl_hardlockup_all_cpu_backtrace)
192 		trigger_allbutself_cpu_backtrace();
193 
194 	if (hardlockup_panic)
195 		nmi_panic(NULL, "Hard LOCKUP");
196 
197 	return;
198 
199 out:
200 	wd_smp_unlock(&flags);
201 }
202 
203 static void wd_smp_clear_cpu_pending(int cpu, u64 tb)
204 {
205 	if (!cpumask_test_cpu(cpu, &wd_smp_cpus_pending)) {
206 		if (unlikely(cpumask_test_cpu(cpu, &wd_smp_cpus_stuck))) {
207 			struct pt_regs *regs = get_irq_regs();
208 			unsigned long flags;
209 
210 			wd_smp_lock(&flags);
211 
212 			pr_emerg("CPU %d became unstuck TB:%lld\n",
213 				 cpu, tb);
214 			print_irqtrace_events(current);
215 			if (regs)
216 				show_regs(regs);
217 			else
218 				dump_stack();
219 
220 			cpumask_clear_cpu(cpu, &wd_smp_cpus_stuck);
221 			wd_smp_unlock(&flags);
222 		}
223 		return;
224 	}
225 	cpumask_clear_cpu(cpu, &wd_smp_cpus_pending);
226 	if (cpumask_empty(&wd_smp_cpus_pending)) {
227 		unsigned long flags;
228 
229 		wd_smp_lock(&flags);
230 		if (cpumask_empty(&wd_smp_cpus_pending)) {
231 			wd_smp_last_reset_tb = tb;
232 			cpumask_andnot(&wd_smp_cpus_pending,
233 					&wd_cpus_enabled,
234 					&wd_smp_cpus_stuck);
235 		}
236 		wd_smp_unlock(&flags);
237 	}
238 }
239 
240 static void watchdog_timer_interrupt(int cpu)
241 {
242 	u64 tb = get_tb();
243 
244 	per_cpu(wd_timer_tb, cpu) = tb;
245 
246 	wd_smp_clear_cpu_pending(cpu, tb);
247 
248 	if ((s64)(tb - wd_smp_last_reset_tb) >= (s64)wd_smp_panic_timeout_tb)
249 		watchdog_smp_panic(cpu, tb);
250 }
251 
252 DEFINE_INTERRUPT_HANDLER_NMI(soft_nmi_interrupt)
253 {
254 	unsigned long flags;
255 	int cpu = raw_smp_processor_id();
256 	u64 tb;
257 
258 	/* should only arrive from kernel, with irqs disabled */
259 	WARN_ON_ONCE(!arch_irq_disabled_regs(regs));
260 
261 	if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
262 		return 0;
263 
264 	__this_cpu_inc(irq_stat.soft_nmi_irqs);
265 
266 	tb = get_tb();
267 	if (tb - per_cpu(wd_timer_tb, cpu) >= wd_panic_timeout_tb) {
268 		wd_smp_lock(&flags);
269 		if (cpumask_test_cpu(cpu, &wd_smp_cpus_stuck)) {
270 			wd_smp_unlock(&flags);
271 			return 0;
272 		}
273 		set_cpu_stuck(cpu, tb);
274 
275 		pr_emerg("CPU %d self-detected hard LOCKUP @ %pS\n",
276 			 cpu, (void *)regs->nip);
277 		pr_emerg("CPU %d TB:%lld, last heartbeat TB:%lld (%lldms ago)\n",
278 			 cpu, tb, per_cpu(wd_timer_tb, cpu),
279 			 tb_to_ns(tb - per_cpu(wd_timer_tb, cpu)) / 1000000);
280 		print_modules();
281 		print_irqtrace_events(current);
282 		show_regs(regs);
283 
284 		wd_smp_unlock(&flags);
285 
286 		if (sysctl_hardlockup_all_cpu_backtrace)
287 			trigger_allbutself_cpu_backtrace();
288 
289 		if (hardlockup_panic)
290 			nmi_panic(regs, "Hard LOCKUP");
291 	}
292 	if (wd_panic_timeout_tb < 0x7fffffff)
293 		mtspr(SPRN_DEC, wd_panic_timeout_tb);
294 
295 	return 0;
296 }
297 
298 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
299 {
300 	int cpu = smp_processor_id();
301 
302 	if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
303 		return HRTIMER_NORESTART;
304 
305 	if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
306 		return HRTIMER_NORESTART;
307 
308 	watchdog_timer_interrupt(cpu);
309 
310 	hrtimer_forward_now(hrtimer, ms_to_ktime(wd_timer_period_ms));
311 
312 	return HRTIMER_RESTART;
313 }
314 
315 void arch_touch_nmi_watchdog(void)
316 {
317 	unsigned long ticks = tb_ticks_per_usec * wd_timer_period_ms * 1000;
318 	int cpu = smp_processor_id();
319 	u64 tb = get_tb();
320 
321 	if (tb - per_cpu(wd_timer_tb, cpu) >= ticks) {
322 		per_cpu(wd_timer_tb, cpu) = tb;
323 		wd_smp_clear_cpu_pending(cpu, tb);
324 	}
325 }
326 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
327 
328 static void start_watchdog(void *arg)
329 {
330 	struct hrtimer *hrtimer = this_cpu_ptr(&wd_hrtimer);
331 	int cpu = smp_processor_id();
332 	unsigned long flags;
333 
334 	if (cpumask_test_cpu(cpu, &wd_cpus_enabled)) {
335 		WARN_ON(1);
336 		return;
337 	}
338 
339 	if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
340 		return;
341 
342 	if (!cpumask_test_cpu(cpu, &watchdog_cpumask))
343 		return;
344 
345 	wd_smp_lock(&flags);
346 	cpumask_set_cpu(cpu, &wd_cpus_enabled);
347 	if (cpumask_weight(&wd_cpus_enabled) == 1) {
348 		cpumask_set_cpu(cpu, &wd_smp_cpus_pending);
349 		wd_smp_last_reset_tb = get_tb();
350 	}
351 	wd_smp_unlock(&flags);
352 
353 	*this_cpu_ptr(&wd_timer_tb) = get_tb();
354 
355 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
356 	hrtimer->function = watchdog_timer_fn;
357 	hrtimer_start(hrtimer, ms_to_ktime(wd_timer_period_ms),
358 		      HRTIMER_MODE_REL_PINNED);
359 }
360 
361 static int start_watchdog_on_cpu(unsigned int cpu)
362 {
363 	return smp_call_function_single(cpu, start_watchdog, NULL, true);
364 }
365 
366 static void stop_watchdog(void *arg)
367 {
368 	struct hrtimer *hrtimer = this_cpu_ptr(&wd_hrtimer);
369 	int cpu = smp_processor_id();
370 	unsigned long flags;
371 
372 	if (!cpumask_test_cpu(cpu, &wd_cpus_enabled))
373 		return; /* Can happen in CPU unplug case */
374 
375 	hrtimer_cancel(hrtimer);
376 
377 	wd_smp_lock(&flags);
378 	cpumask_clear_cpu(cpu, &wd_cpus_enabled);
379 	wd_smp_unlock(&flags);
380 
381 	wd_smp_clear_cpu_pending(cpu, get_tb());
382 }
383 
384 static int stop_watchdog_on_cpu(unsigned int cpu)
385 {
386 	return smp_call_function_single(cpu, stop_watchdog, NULL, true);
387 }
388 
389 static void watchdog_calc_timeouts(void)
390 {
391 	wd_panic_timeout_tb = watchdog_thresh * ppc_tb_freq;
392 
393 	/* Have the SMP detector trigger a bit later */
394 	wd_smp_panic_timeout_tb = wd_panic_timeout_tb * 3 / 2;
395 
396 	/* 2/5 is the factor that the perf based detector uses */
397 	wd_timer_period_ms = watchdog_thresh * 1000 * 2 / 5;
398 }
399 
400 void watchdog_nmi_stop(void)
401 {
402 	int cpu;
403 
404 	for_each_cpu(cpu, &wd_cpus_enabled)
405 		stop_watchdog_on_cpu(cpu);
406 }
407 
408 void watchdog_nmi_start(void)
409 {
410 	int cpu;
411 
412 	watchdog_calc_timeouts();
413 	for_each_cpu_and(cpu, cpu_online_mask, &watchdog_cpumask)
414 		start_watchdog_on_cpu(cpu);
415 }
416 
417 /*
418  * Invoked from core watchdog init.
419  */
420 int __init watchdog_nmi_probe(void)
421 {
422 	int err;
423 
424 	err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
425 					"powerpc/watchdog:online",
426 					start_watchdog_on_cpu,
427 					stop_watchdog_on_cpu);
428 	if (err < 0) {
429 		pr_warn("could not be initialized");
430 		return err;
431 	}
432 	return 0;
433 }
434