• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Detect hard and soft lockups on a system
3  *
4  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5  *
6  * Note: Most of this code is borrowed heavily from the original softlockup
7  * detector, so thanks to Ingo for the initial implementation.
8  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
9  * to those contributors as well.
10  */
11 
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
13 
14 #include <linux/mm.h>
15 #include <linux/cpu.h>
16 #include <linux/nmi.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/sysctl.h>
20 #include <linux/smpboot.h>
21 #include <linux/sched/rt.h>
22 
23 #include <asm/irq_regs.h>
24 #include <linux/kvm_para.h>
25 #include <linux/perf_event.h>
26 
27 int watchdog_user_enabled = 1;
28 int __read_mostly watchdog_thresh = 10;
29 #ifdef CONFIG_SMP
30 int __read_mostly sysctl_softlockup_all_cpu_backtrace;
31 #else
32 #define sysctl_softlockup_all_cpu_backtrace 0
33 #endif
34 
35 static int __read_mostly watchdog_running;
36 static u64 __read_mostly sample_period;
37 
38 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
39 static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
40 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
41 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
42 static DEFINE_PER_CPU(bool, soft_watchdog_warn);
43 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
44 static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
45 static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
46 #ifdef CONFIG_HARDLOCKUP_DETECTOR
47 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
48 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
49 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
50 #endif
51 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
52 static cpumask_t __read_mostly watchdog_cpus;
53 #endif
54 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
55 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
56 #endif
57 static unsigned long soft_lockup_nmi_warn;
58 
59 /* boot commands */
60 /*
61  * Should we panic when a soft-lockup or hard-lockup occurs:
62  */
63 #ifdef CONFIG_HARDLOCKUP_DETECTOR
64 static int hardlockup_panic =
65 			CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
66 
67 static bool hardlockup_detector_enabled = true;
68 /*
69  * We may not want to enable hard lockup detection by default in all cases,
70  * for example when running the kernel as a guest on a hypervisor. In these
71  * cases this function can be called to disable hard lockup detection. This
72  * function should only be executed once by the boot processor before the
73  * kernel command line parameters are parsed, because otherwise it is not
74  * possible to override this in hardlockup_panic_setup().
75  */
watchdog_enable_hardlockup_detector(bool val)76 void watchdog_enable_hardlockup_detector(bool val)
77 {
78 	hardlockup_detector_enabled = val;
79 }
80 
watchdog_hardlockup_detector_is_enabled(void)81 bool watchdog_hardlockup_detector_is_enabled(void)
82 {
83 	return hardlockup_detector_enabled;
84 }
85 
hardlockup_panic_setup(char * str)86 static int __init hardlockup_panic_setup(char *str)
87 {
88 	if (!strncmp(str, "panic", 5))
89 		hardlockup_panic = 1;
90 	else if (!strncmp(str, "nopanic", 7))
91 		hardlockup_panic = 0;
92 	else if (!strncmp(str, "0", 1))
93 		watchdog_user_enabled = 0;
94 	else if (!strncmp(str, "1", 1) || !strncmp(str, "2", 1)) {
95 		/*
96 		 * Setting 'nmi_watchdog=1' or 'nmi_watchdog=2' (legacy option)
97 		 * has the same effect.
98 		 */
99 		watchdog_user_enabled = 1;
100 		watchdog_enable_hardlockup_detector(true);
101 	}
102 	return 1;
103 }
104 __setup("nmi_watchdog=", hardlockup_panic_setup);
105 #endif
106 
107 unsigned int __read_mostly softlockup_panic =
108 			CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
109 
softlockup_panic_setup(char * str)110 static int __init softlockup_panic_setup(char *str)
111 {
112 	softlockup_panic = simple_strtoul(str, NULL, 0);
113 
114 	return 1;
115 }
116 __setup("softlockup_panic=", softlockup_panic_setup);
117 
nowatchdog_setup(char * str)118 static int __init nowatchdog_setup(char *str)
119 {
120 	watchdog_user_enabled = 0;
121 	return 1;
122 }
123 __setup("nowatchdog", nowatchdog_setup);
124 
125 /* deprecated */
nosoftlockup_setup(char * str)126 static int __init nosoftlockup_setup(char *str)
127 {
128 	watchdog_user_enabled = 0;
129 	return 1;
130 }
131 __setup("nosoftlockup", nosoftlockup_setup);
132 /*  */
133 #ifdef CONFIG_SMP
softlockup_all_cpu_backtrace_setup(char * str)134 static int __init softlockup_all_cpu_backtrace_setup(char *str)
135 {
136 	sysctl_softlockup_all_cpu_backtrace =
137 		!!simple_strtol(str, NULL, 0);
138 	return 1;
139 }
140 __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
141 #endif
142 
143 /*
144  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
145  * lockups can have false positives under extreme conditions. So we generally
146  * want a higher threshold for soft lockups than for hard lockups. So we couple
147  * the thresholds with a factor: we make the soft threshold twice the amount of
148  * time the hard threshold is.
149  */
get_softlockup_thresh(void)150 static int get_softlockup_thresh(void)
151 {
152 	return watchdog_thresh * 2;
153 }
154 
155 /*
156  * Returns seconds, approximately.  We don't need nanosecond
157  * resolution, and we don't need to waste time with a big divide when
158  * 2^30ns == 1.074s.
159  */
get_timestamp(void)160 static unsigned long get_timestamp(void)
161 {
162 	return local_clock() >> 30LL;  /* 2^30 ~= 10^9 */
163 }
164 
set_sample_period(void)165 static void set_sample_period(void)
166 {
167 	/*
168 	 * convert watchdog_thresh from seconds to ns
169 	 * the divide by 5 is to give hrtimer several chances (two
170 	 * or three with the current relation between the soft
171 	 * and hard thresholds) to increment before the
172 	 * hardlockup detector generates a warning
173 	 */
174 	sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
175 }
176 
177 /* Commands for resetting the watchdog */
__touch_watchdog(void)178 static void __touch_watchdog(void)
179 {
180 	__this_cpu_write(watchdog_touch_ts, get_timestamp());
181 }
182 
touch_softlockup_watchdog(void)183 void touch_softlockup_watchdog(void)
184 {
185 	/*
186 	 * Preemption can be enabled.  It doesn't matter which CPU's timestamp
187 	 * gets zeroed here, so use the raw_ operation.
188 	 */
189 	raw_cpu_write(watchdog_touch_ts, 0);
190 }
191 EXPORT_SYMBOL(touch_softlockup_watchdog);
192 
touch_all_softlockup_watchdogs(void)193 void touch_all_softlockup_watchdogs(void)
194 {
195 	int cpu;
196 
197 	/*
198 	 * this is done lockless
199 	 * do we care if a 0 races with a timestamp?
200 	 * all it means is the softlock check starts one cycle later
201 	 */
202 	for_each_online_cpu(cpu)
203 		per_cpu(watchdog_touch_ts, cpu) = 0;
204 }
205 
206 #ifdef CONFIG_HARDLOCKUP_DETECTOR
touch_nmi_watchdog(void)207 void touch_nmi_watchdog(void)
208 {
209 	/*
210 	 * Using __raw here because some code paths have
211 	 * preemption enabled.  If preemption is enabled
212 	 * then interrupts should be enabled too, in which
213 	 * case we shouldn't have to worry about the watchdog
214 	 * going off.
215 	 */
216 	raw_cpu_write(watchdog_nmi_touch, true);
217 	touch_softlockup_watchdog();
218 }
219 EXPORT_SYMBOL(touch_nmi_watchdog);
220 
221 #endif
222 
touch_softlockup_watchdog_sync(void)223 void touch_softlockup_watchdog_sync(void)
224 {
225 	__this_cpu_write(softlockup_touch_sync, true);
226 	__this_cpu_write(watchdog_touch_ts, 0);
227 }
228 
229 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
230 /* watchdog detector functions */
is_hardlockup(void)231 static int is_hardlockup(void)
232 {
233 	unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
234 
235 	if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
236 		return 1;
237 
238 	__this_cpu_write(hrtimer_interrupts_saved, hrint);
239 	return 0;
240 }
241 #endif
242 
243 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
watchdog_next_cpu(unsigned int cpu)244 static unsigned int watchdog_next_cpu(unsigned int cpu)
245 {
246 	cpumask_t cpus = watchdog_cpus;
247 	unsigned int next_cpu;
248 
249 	next_cpu = cpumask_next(cpu, &cpus);
250 	if (next_cpu >= nr_cpu_ids)
251 		next_cpu = cpumask_first(&cpus);
252 
253 	if (next_cpu == cpu)
254 		return nr_cpu_ids;
255 
256 	return next_cpu;
257 }
258 
is_hardlockup_other_cpu(unsigned int cpu)259 static int is_hardlockup_other_cpu(unsigned int cpu)
260 {
261 	unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
262 
263 	if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
264 		return 1;
265 
266 	per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
267 	return 0;
268 }
269 
watchdog_check_hardlockup_other_cpu(void)270 static void watchdog_check_hardlockup_other_cpu(void)
271 {
272 	unsigned int next_cpu;
273 
274 	/*
275 	 * Test for hardlockups every 3 samples.  The sample period is
276 	 *  watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
277 	 *  watchdog_thresh (over by 20%).
278 	 */
279 	if (__this_cpu_read(hrtimer_interrupts) % 3 != 0)
280 		return;
281 
282 	/* check for a hardlockup on the next cpu */
283 	next_cpu = watchdog_next_cpu(smp_processor_id());
284 	if (next_cpu >= nr_cpu_ids)
285 		return;
286 
287 	smp_rmb();
288 
289 	if (per_cpu(watchdog_nmi_touch, next_cpu) == true) {
290 		per_cpu(watchdog_nmi_touch, next_cpu) = false;
291 		return;
292 	}
293 
294 	if (is_hardlockup_other_cpu(next_cpu)) {
295 		/* only warn once */
296 		if (per_cpu(hard_watchdog_warn, next_cpu) == true)
297 			return;
298 
299 		if (hardlockup_panic)
300 			panic("Watchdog detected hard LOCKUP on cpu %u", next_cpu);
301 		else
302 			WARN(1, "Watchdog detected hard LOCKUP on cpu %u", next_cpu);
303 
304 		per_cpu(hard_watchdog_warn, next_cpu) = true;
305 	} else {
306 		per_cpu(hard_watchdog_warn, next_cpu) = false;
307 	}
308 }
309 #else
watchdog_check_hardlockup_other_cpu(void)310 static inline void watchdog_check_hardlockup_other_cpu(void) { return; }
311 #endif
312 
is_softlockup(unsigned long touch_ts)313 static int is_softlockup(unsigned long touch_ts)
314 {
315 	unsigned long now = get_timestamp();
316 
317 	/* Warn about unreasonable delays: */
318 	if (time_after(now, touch_ts + get_softlockup_thresh()))
319 		return now - touch_ts;
320 
321 	return 0;
322 }
323 
324 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
325 
326 static struct perf_event_attr wd_hw_attr = {
327 	.type		= PERF_TYPE_HARDWARE,
328 	.config		= PERF_COUNT_HW_CPU_CYCLES,
329 	.size		= sizeof(struct perf_event_attr),
330 	.pinned		= 1,
331 	.disabled	= 1,
332 };
333 
334 /* Callback function for perf event subsystem */
watchdog_overflow_callback(struct perf_event * event,struct perf_sample_data * data,struct pt_regs * regs)335 static void watchdog_overflow_callback(struct perf_event *event,
336 		 struct perf_sample_data *data,
337 		 struct pt_regs *regs)
338 {
339 	/* Ensure the watchdog never gets throttled */
340 	event->hw.interrupts = 0;
341 
342 	if (__this_cpu_read(watchdog_nmi_touch) == true) {
343 		__this_cpu_write(watchdog_nmi_touch, false);
344 		return;
345 	}
346 
347 	/* check for a hardlockup
348 	 * This is done by making sure our timer interrupt
349 	 * is incrementing.  The timer interrupt should have
350 	 * fired multiple times before we overflow'd.  If it hasn't
351 	 * then this is a good indication the cpu is stuck
352 	 */
353 	if (is_hardlockup()) {
354 		int this_cpu = smp_processor_id();
355 
356 		/* only print hardlockups once */
357 		if (__this_cpu_read(hard_watchdog_warn) == true)
358 			return;
359 
360 		if (hardlockup_panic)
361 			panic("Watchdog detected hard LOCKUP on cpu %d",
362 			      this_cpu);
363 		else
364 			WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
365 			     this_cpu);
366 
367 		__this_cpu_write(hard_watchdog_warn, true);
368 		return;
369 	}
370 
371 	__this_cpu_write(hard_watchdog_warn, false);
372 	return;
373 }
374 #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
375 
watchdog_interrupt_count(void)376 static void watchdog_interrupt_count(void)
377 {
378 	__this_cpu_inc(hrtimer_interrupts);
379 }
380 
381 static int watchdog_nmi_enable(unsigned int cpu);
382 static void watchdog_nmi_disable(unsigned int cpu);
383 
384 /* watchdog kicker functions */
watchdog_timer_fn(struct hrtimer * hrtimer)385 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
386 {
387 	unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
388 	struct pt_regs *regs = get_irq_regs();
389 	int duration;
390 	int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
391 
392 	/* kick the hardlockup detector */
393 	watchdog_interrupt_count();
394 
395 	/* test for hardlockups on the next cpu */
396 	watchdog_check_hardlockup_other_cpu();
397 
398 	/* kick the softlockup detector */
399 	wake_up_process(__this_cpu_read(softlockup_watchdog));
400 
401 	/* .. and repeat */
402 	hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
403 
404 	if (touch_ts == 0) {
405 		if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
406 			/*
407 			 * If the time stamp was touched atomically
408 			 * make sure the scheduler tick is up to date.
409 			 */
410 			__this_cpu_write(softlockup_touch_sync, false);
411 			sched_clock_tick();
412 		}
413 
414 		/* Clear the guest paused flag on watchdog reset */
415 		kvm_check_and_clear_guest_paused();
416 		__touch_watchdog();
417 		return HRTIMER_RESTART;
418 	}
419 
420 	/* check for a softlockup
421 	 * This is done by making sure a high priority task is
422 	 * being scheduled.  The task touches the watchdog to
423 	 * indicate it is getting cpu time.  If it hasn't then
424 	 * this is a good indication some task is hogging the cpu
425 	 */
426 	duration = is_softlockup(touch_ts);
427 	if (unlikely(duration)) {
428 		/*
429 		 * If a virtual machine is stopped by the host it can look to
430 		 * the watchdog like a soft lockup, check to see if the host
431 		 * stopped the vm before we issue the warning
432 		 */
433 		if (kvm_check_and_clear_guest_paused())
434 			return HRTIMER_RESTART;
435 
436 		/* only warn once */
437 		if (__this_cpu_read(soft_watchdog_warn) == true) {
438 			/*
439 			 * When multiple processes are causing softlockups the
440 			 * softlockup detector only warns on the first one
441 			 * because the code relies on a full quiet cycle to
442 			 * re-arm.  The second process prevents the quiet cycle
443 			 * and never gets reported.  Use task pointers to detect
444 			 * this.
445 			 */
446 			if (__this_cpu_read(softlockup_task_ptr_saved) !=
447 			    current) {
448 				__this_cpu_write(soft_watchdog_warn, false);
449 				__touch_watchdog();
450 			}
451 			return HRTIMER_RESTART;
452 		}
453 
454 		if (softlockup_all_cpu_backtrace) {
455 			/* Prevent multiple soft-lockup reports if one cpu is already
456 			 * engaged in dumping cpu back traces
457 			 */
458 			if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
459 				/* Someone else will report us. Let's give up */
460 				__this_cpu_write(soft_watchdog_warn, true);
461 				return HRTIMER_RESTART;
462 			}
463 		}
464 
465 		pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
466 			smp_processor_id(), duration,
467 			current->comm, task_pid_nr(current));
468 		__this_cpu_write(softlockup_task_ptr_saved, current);
469 		print_modules();
470 		print_irqtrace_events(current);
471 		if (regs)
472 			show_regs(regs);
473 		else
474 			dump_stack();
475 
476 		if (softlockup_all_cpu_backtrace) {
477 			/* Avoid generating two back traces for current
478 			 * given that one is already made above
479 			 */
480 			trigger_allbutself_cpu_backtrace();
481 
482 			clear_bit(0, &soft_lockup_nmi_warn);
483 			/* Barrier to sync with other cpus */
484 			smp_mb__after_atomic();
485 		}
486 
487 		add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
488 		if (softlockup_panic)
489 			panic("softlockup: hung tasks");
490 		__this_cpu_write(soft_watchdog_warn, true);
491 	} else
492 		__this_cpu_write(soft_watchdog_warn, false);
493 
494 	return HRTIMER_RESTART;
495 }
496 
watchdog_set_prio(unsigned int policy,unsigned int prio)497 static void watchdog_set_prio(unsigned int policy, unsigned int prio)
498 {
499 	struct sched_param param = { .sched_priority = prio };
500 
501 	sched_setscheduler(current, policy, &param);
502 }
503 
watchdog_enable(unsigned int cpu)504 static void watchdog_enable(unsigned int cpu)
505 {
506 	struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
507 
508 	/* kick off the timer for the hardlockup detector */
509 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
510 	hrtimer->function = watchdog_timer_fn;
511 
512 	/* Enable the perf event */
513 	watchdog_nmi_enable(cpu);
514 
515 	/* done here because hrtimer_start can only pin to smp_processor_id() */
516 	hrtimer_start(hrtimer, ns_to_ktime(sample_period),
517 		      HRTIMER_MODE_REL_PINNED);
518 
519 	/* initialize timestamp */
520 	watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
521 	__touch_watchdog();
522 }
523 
watchdog_disable(unsigned int cpu)524 static void watchdog_disable(unsigned int cpu)
525 {
526 	struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
527 
528 	watchdog_set_prio(SCHED_NORMAL, 0);
529 	hrtimer_cancel(hrtimer);
530 	/* disable the perf event */
531 	watchdog_nmi_disable(cpu);
532 }
533 
watchdog_cleanup(unsigned int cpu,bool online)534 static void watchdog_cleanup(unsigned int cpu, bool online)
535 {
536 	watchdog_disable(cpu);
537 }
538 
watchdog_should_run(unsigned int cpu)539 static int watchdog_should_run(unsigned int cpu)
540 {
541 	return __this_cpu_read(hrtimer_interrupts) !=
542 		__this_cpu_read(soft_lockup_hrtimer_cnt);
543 }
544 
545 /*
546  * The watchdog thread function - touches the timestamp.
547  *
548  * It only runs once every sample_period seconds (4 seconds by
549  * default) to reset the softlockup timestamp. If this gets delayed
550  * for more than 2*watchdog_thresh seconds then the debug-printout
551  * triggers in watchdog_timer_fn().
552  */
watchdog(unsigned int cpu)553 static void watchdog(unsigned int cpu)
554 {
555 	__this_cpu_write(soft_lockup_hrtimer_cnt,
556 			 __this_cpu_read(hrtimer_interrupts));
557 	__touch_watchdog();
558 }
559 
560 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
561 /*
562  * People like the simple clean cpu node info on boot.
563  * Reduce the watchdog noise by only printing messages
564  * that are different from what cpu0 displayed.
565  */
566 static unsigned long cpu0_err;
567 
watchdog_nmi_enable(unsigned int cpu)568 static int watchdog_nmi_enable(unsigned int cpu)
569 {
570 	struct perf_event_attr *wd_attr;
571 	struct perf_event *event = per_cpu(watchdog_ev, cpu);
572 
573 	/*
574 	 * Some kernels need to default hard lockup detection to
575 	 * 'disabled', for example a guest on a hypervisor.
576 	 */
577 	if (!watchdog_hardlockup_detector_is_enabled()) {
578 		event = ERR_PTR(-ENOENT);
579 		goto handle_err;
580 	}
581 
582 	/* is it already setup and enabled? */
583 	if (event && event->state > PERF_EVENT_STATE_OFF)
584 		goto out;
585 
586 	/* it is setup but not enabled */
587 	if (event != NULL)
588 		goto out_enable;
589 
590 	wd_attr = &wd_hw_attr;
591 	wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
592 
593 	/* Try to register using hardware perf events */
594 	event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
595 
596 handle_err:
597 	/* save cpu0 error for future comparision */
598 	if (cpu == 0 && IS_ERR(event))
599 		cpu0_err = PTR_ERR(event);
600 
601 	if (!IS_ERR(event)) {
602 		/* only print for cpu0 or different than cpu0 */
603 		if (cpu == 0 || cpu0_err)
604 			pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
605 		goto out_save;
606 	}
607 
608 	/* skip displaying the same error again */
609 	if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
610 		return PTR_ERR(event);
611 
612 	/* vary the KERN level based on the returned errno */
613 	if (PTR_ERR(event) == -EOPNOTSUPP)
614 		pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
615 	else if (PTR_ERR(event) == -ENOENT)
616 		pr_warn("disabled (cpu%i): hardware events not enabled\n",
617 			 cpu);
618 	else
619 		pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
620 			cpu, PTR_ERR(event));
621 	return PTR_ERR(event);
622 
623 	/* success path */
624 out_save:
625 	per_cpu(watchdog_ev, cpu) = event;
626 out_enable:
627 	perf_event_enable(per_cpu(watchdog_ev, cpu));
628 out:
629 	return 0;
630 }
631 
watchdog_nmi_disable(unsigned int cpu)632 static void watchdog_nmi_disable(unsigned int cpu)
633 {
634 	struct perf_event *event = per_cpu(watchdog_ev, cpu);
635 
636 	if (event) {
637 		perf_event_disable(event);
638 		per_cpu(watchdog_ev, cpu) = NULL;
639 
640 		/* should be in cleanup, but blocks oprofile */
641 		perf_event_release_kernel(event);
642 	}
643 	if (cpu == 0) {
644 		/* watchdog_nmi_enable() expects this to be zero initially. */
645 		cpu0_err = 0;
646 	}
647 }
648 #else
649 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
watchdog_nmi_enable(unsigned int cpu)650 static int watchdog_nmi_enable(unsigned int cpu)
651 {
652 	/*
653 	 * The new cpu will be marked online before the first hrtimer interrupt
654 	 * runs on it.  If another cpu tests for a hardlockup on the new cpu
655 	 * before it has run its first hrtimer, it will get a false positive.
656 	 * Touch the watchdog on the new cpu to delay the first check for at
657 	 * least 3 sampling periods to guarantee one hrtimer has run on the new
658 	 * cpu.
659 	 */
660 	per_cpu(watchdog_nmi_touch, cpu) = true;
661 	smp_wmb();
662 	cpumask_set_cpu(cpu, &watchdog_cpus);
663 	return 0;
664 }
665 
watchdog_nmi_disable(unsigned int cpu)666 static void watchdog_nmi_disable(unsigned int cpu)
667 {
668 	unsigned int next_cpu = watchdog_next_cpu(cpu);
669 
670 	/*
671 	 * Offlining this cpu will cause the cpu before this one to start
672 	 * checking the one after this one.  If this cpu just finished checking
673 	 * the next cpu and updating hrtimer_interrupts_saved, and then the
674 	 * previous cpu checks it within one sample period, it will trigger a
675 	 * false positive.  Touch the watchdog on the next cpu to prevent it.
676 	 */
677 	if (next_cpu < nr_cpu_ids)
678 		per_cpu(watchdog_nmi_touch, next_cpu) = true;
679 	smp_wmb();
680 	cpumask_clear_cpu(cpu, &watchdog_cpus);
681 }
682 #else
watchdog_nmi_enable(unsigned int cpu)683 static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
watchdog_nmi_disable(unsigned int cpu)684 static void watchdog_nmi_disable(unsigned int cpu) { return; }
685 #endif /* CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU */
686 #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
687 
688 static struct smp_hotplug_thread watchdog_threads = {
689 	.store			= &softlockup_watchdog,
690 	.thread_should_run	= watchdog_should_run,
691 	.thread_fn		= watchdog,
692 	.thread_comm		= "watchdog/%u",
693 	.setup			= watchdog_enable,
694 	.cleanup		= watchdog_cleanup,
695 	.park			= watchdog_disable,
696 	.unpark			= watchdog_enable,
697 };
698 
restart_watchdog_hrtimer(void * info)699 static void restart_watchdog_hrtimer(void *info)
700 {
701 	struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
702 	int ret;
703 
704 	/*
705 	 * No need to cancel and restart hrtimer if it is currently executing
706 	 * because it will reprogram itself with the new period now.
707 	 * We should never see it unqueued here because we are running per-cpu
708 	 * with interrupts disabled.
709 	 */
710 	ret = hrtimer_try_to_cancel(hrtimer);
711 	if (ret == 1)
712 		hrtimer_start(hrtimer, ns_to_ktime(sample_period),
713 				HRTIMER_MODE_REL_PINNED);
714 }
715 
update_timers(int cpu)716 static void update_timers(int cpu)
717 {
718 	/*
719 	 * Make sure that perf event counter will adopt to a new
720 	 * sampling period. Updating the sampling period directly would
721 	 * be much nicer but we do not have an API for that now so
722 	 * let's use a big hammer.
723 	 * Hrtimer will adopt the new period on the next tick but this
724 	 * might be late already so we have to restart the timer as well.
725 	 */
726 	watchdog_nmi_disable(cpu);
727 	smp_call_function_single(cpu, restart_watchdog_hrtimer, NULL, 1);
728 	watchdog_nmi_enable(cpu);
729 }
730 
update_timers_all_cpus(void)731 static void update_timers_all_cpus(void)
732 {
733 	int cpu;
734 
735 	get_online_cpus();
736 	for_each_online_cpu(cpu)
737 		update_timers(cpu);
738 	put_online_cpus();
739 }
740 
watchdog_enable_all_cpus(bool sample_period_changed)741 static int watchdog_enable_all_cpus(bool sample_period_changed)
742 {
743 	int err = 0;
744 
745 	if (!watchdog_running) {
746 		err = smpboot_register_percpu_thread(&watchdog_threads);
747 		if (err)
748 			pr_err("Failed to create watchdog threads, disabled\n");
749 		else
750 			watchdog_running = 1;
751 	} else if (sample_period_changed) {
752 		update_timers_all_cpus();
753 	}
754 
755 	return err;
756 }
757 
758 /* prepare/enable/disable routines */
759 /* sysctl functions */
760 #ifdef CONFIG_SYSCTL
watchdog_disable_all_cpus(void)761 static void watchdog_disable_all_cpus(void)
762 {
763 	if (watchdog_running) {
764 		watchdog_running = 0;
765 		smpboot_unregister_percpu_thread(&watchdog_threads);
766 	}
767 }
768 
769 /*
770  * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
771  */
772 
proc_dowatchdog(struct ctl_table * table,int write,void __user * buffer,size_t * lenp,loff_t * ppos)773 int proc_dowatchdog(struct ctl_table *table, int write,
774 		    void __user *buffer, size_t *lenp, loff_t *ppos)
775 {
776 	int err, old_thresh, old_enabled;
777 	bool old_hardlockup;
778 	static DEFINE_MUTEX(watchdog_proc_mutex);
779 
780 	mutex_lock(&watchdog_proc_mutex);
781 	old_thresh = ACCESS_ONCE(watchdog_thresh);
782 	old_enabled = ACCESS_ONCE(watchdog_user_enabled);
783 	old_hardlockup = watchdog_hardlockup_detector_is_enabled();
784 
785 	err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
786 	if (err || !write)
787 		goto out;
788 
789 	set_sample_period();
790 	/*
791 	 * Watchdog threads shouldn't be enabled if they are
792 	 * disabled. The 'watchdog_running' variable check in
793 	 * watchdog_*_all_cpus() function takes care of this.
794 	 */
795 	if (watchdog_user_enabled && watchdog_thresh) {
796 		/*
797 		 * Prevent a change in watchdog_thresh accidentally overriding
798 		 * the enablement of the hardlockup detector.
799 		 */
800 		if (watchdog_user_enabled != old_enabled)
801 			watchdog_enable_hardlockup_detector(true);
802 		err = watchdog_enable_all_cpus(old_thresh != watchdog_thresh);
803 	} else
804 		watchdog_disable_all_cpus();
805 
806 	/* Restore old values on failure */
807 	if (err) {
808 		watchdog_thresh = old_thresh;
809 		watchdog_user_enabled = old_enabled;
810 		watchdog_enable_hardlockup_detector(old_hardlockup);
811 	}
812 out:
813 	mutex_unlock(&watchdog_proc_mutex);
814 	return err;
815 }
816 #endif /* CONFIG_SYSCTL */
817 
lockup_detector_init(void)818 void __init lockup_detector_init(void)
819 {
820 	set_sample_period();
821 
822 	if (watchdog_user_enabled)
823 		watchdog_enable_all_cpus(false);
824 }
825