Merge branch 'x86-fpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / kernel / watchdog.c
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 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
51 #endif
52 static unsigned long soft_lockup_nmi_warn;
53
54 /* boot commands */
55 /*
56 * Should we panic when a soft-lockup or hard-lockup occurs:
57 */
58 #ifdef CONFIG_HARDLOCKUP_DETECTOR
59 static int hardlockup_panic =
60 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
61
62 static int __init hardlockup_panic_setup(char *str)
63 {
64 if (!strncmp(str, "panic", 5))
65 hardlockup_panic = 1;
66 else if (!strncmp(str, "nopanic", 7))
67 hardlockup_panic = 0;
68 else if (!strncmp(str, "0", 1))
69 watchdog_user_enabled = 0;
70 return 1;
71 }
72 __setup("nmi_watchdog=", hardlockup_panic_setup);
73 #endif
74
75 unsigned int __read_mostly softlockup_panic =
76 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
77
78 static int __init softlockup_panic_setup(char *str)
79 {
80 softlockup_panic = simple_strtoul(str, NULL, 0);
81
82 return 1;
83 }
84 __setup("softlockup_panic=", softlockup_panic_setup);
85
86 static int __init nowatchdog_setup(char *str)
87 {
88 watchdog_user_enabled = 0;
89 return 1;
90 }
91 __setup("nowatchdog", nowatchdog_setup);
92
93 /* deprecated */
94 static int __init nosoftlockup_setup(char *str)
95 {
96 watchdog_user_enabled = 0;
97 return 1;
98 }
99 __setup("nosoftlockup", nosoftlockup_setup);
100 /* */
101 #ifdef CONFIG_SMP
102 static int __init softlockup_all_cpu_backtrace_setup(char *str)
103 {
104 sysctl_softlockup_all_cpu_backtrace =
105 !!simple_strtol(str, NULL, 0);
106 return 1;
107 }
108 __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
109 #endif
110
111 /*
112 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
113 * lockups can have false positives under extreme conditions. So we generally
114 * want a higher threshold for soft lockups than for hard lockups. So we couple
115 * the thresholds with a factor: we make the soft threshold twice the amount of
116 * time the hard threshold is.
117 */
118 static int get_softlockup_thresh(void)
119 {
120 return watchdog_thresh * 2;
121 }
122
123 /*
124 * Returns seconds, approximately. We don't need nanosecond
125 * resolution, and we don't need to waste time with a big divide when
126 * 2^30ns == 1.074s.
127 */
128 static unsigned long get_timestamp(void)
129 {
130 return local_clock() >> 30LL; /* 2^30 ~= 10^9 */
131 }
132
133 static void set_sample_period(void)
134 {
135 /*
136 * convert watchdog_thresh from seconds to ns
137 * the divide by 5 is to give hrtimer several chances (two
138 * or three with the current relation between the soft
139 * and hard thresholds) to increment before the
140 * hardlockup detector generates a warning
141 */
142 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
143 }
144
145 /* Commands for resetting the watchdog */
146 static void __touch_watchdog(void)
147 {
148 __this_cpu_write(watchdog_touch_ts, get_timestamp());
149 }
150
151 void touch_softlockup_watchdog(void)
152 {
153 /*
154 * Preemption can be enabled. It doesn't matter which CPU's timestamp
155 * gets zeroed here, so use the raw_ operation.
156 */
157 raw_cpu_write(watchdog_touch_ts, 0);
158 }
159 EXPORT_SYMBOL(touch_softlockup_watchdog);
160
161 void touch_all_softlockup_watchdogs(void)
162 {
163 int cpu;
164
165 /*
166 * this is done lockless
167 * do we care if a 0 races with a timestamp?
168 * all it means is the softlock check starts one cycle later
169 */
170 for_each_online_cpu(cpu)
171 per_cpu(watchdog_touch_ts, cpu) = 0;
172 }
173
174 #ifdef CONFIG_HARDLOCKUP_DETECTOR
175 void touch_nmi_watchdog(void)
176 {
177 /*
178 * Using __raw here because some code paths have
179 * preemption enabled. If preemption is enabled
180 * then interrupts should be enabled too, in which
181 * case we shouldn't have to worry about the watchdog
182 * going off.
183 */
184 __raw_get_cpu_var(watchdog_nmi_touch) = true;
185 touch_softlockup_watchdog();
186 }
187 EXPORT_SYMBOL(touch_nmi_watchdog);
188
189 #endif
190
191 void touch_softlockup_watchdog_sync(void)
192 {
193 __raw_get_cpu_var(softlockup_touch_sync) = true;
194 __raw_get_cpu_var(watchdog_touch_ts) = 0;
195 }
196
197 #ifdef CONFIG_HARDLOCKUP_DETECTOR
198 /* watchdog detector functions */
199 static int is_hardlockup(void)
200 {
201 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
202
203 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
204 return 1;
205
206 __this_cpu_write(hrtimer_interrupts_saved, hrint);
207 return 0;
208 }
209 #endif
210
211 static int is_softlockup(unsigned long touch_ts)
212 {
213 unsigned long now = get_timestamp();
214
215 /* Warn about unreasonable delays: */
216 if (time_after(now, touch_ts + get_softlockup_thresh()))
217 return now - touch_ts;
218
219 return 0;
220 }
221
222 #ifdef CONFIG_HARDLOCKUP_DETECTOR
223
224 static struct perf_event_attr wd_hw_attr = {
225 .type = PERF_TYPE_HARDWARE,
226 .config = PERF_COUNT_HW_CPU_CYCLES,
227 .size = sizeof(struct perf_event_attr),
228 .pinned = 1,
229 .disabled = 1,
230 };
231
232 /* Callback function for perf event subsystem */
233 static void watchdog_overflow_callback(struct perf_event *event,
234 struct perf_sample_data *data,
235 struct pt_regs *regs)
236 {
237 /* Ensure the watchdog never gets throttled */
238 event->hw.interrupts = 0;
239
240 if (__this_cpu_read(watchdog_nmi_touch) == true) {
241 __this_cpu_write(watchdog_nmi_touch, false);
242 return;
243 }
244
245 /* check for a hardlockup
246 * This is done by making sure our timer interrupt
247 * is incrementing. The timer interrupt should have
248 * fired multiple times before we overflow'd. If it hasn't
249 * then this is a good indication the cpu is stuck
250 */
251 if (is_hardlockup()) {
252 int this_cpu = smp_processor_id();
253
254 /* only print hardlockups once */
255 if (__this_cpu_read(hard_watchdog_warn) == true)
256 return;
257
258 if (hardlockup_panic)
259 panic("Watchdog detected hard LOCKUP on cpu %d",
260 this_cpu);
261 else
262 WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
263 this_cpu);
264
265 __this_cpu_write(hard_watchdog_warn, true);
266 return;
267 }
268
269 __this_cpu_write(hard_watchdog_warn, false);
270 return;
271 }
272 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
273
274 static void watchdog_interrupt_count(void)
275 {
276 __this_cpu_inc(hrtimer_interrupts);
277 }
278
279 static int watchdog_nmi_enable(unsigned int cpu);
280 static void watchdog_nmi_disable(unsigned int cpu);
281
282 /* watchdog kicker functions */
283 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
284 {
285 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
286 struct pt_regs *regs = get_irq_regs();
287 int duration;
288 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
289
290 /* kick the hardlockup detector */
291 watchdog_interrupt_count();
292
293 /* kick the softlockup detector */
294 wake_up_process(__this_cpu_read(softlockup_watchdog));
295
296 /* .. and repeat */
297 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
298
299 if (touch_ts == 0) {
300 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
301 /*
302 * If the time stamp was touched atomically
303 * make sure the scheduler tick is up to date.
304 */
305 __this_cpu_write(softlockup_touch_sync, false);
306 sched_clock_tick();
307 }
308
309 /* Clear the guest paused flag on watchdog reset */
310 kvm_check_and_clear_guest_paused();
311 __touch_watchdog();
312 return HRTIMER_RESTART;
313 }
314
315 /* check for a softlockup
316 * This is done by making sure a high priority task is
317 * being scheduled. The task touches the watchdog to
318 * indicate it is getting cpu time. If it hasn't then
319 * this is a good indication some task is hogging the cpu
320 */
321 duration = is_softlockup(touch_ts);
322 if (unlikely(duration)) {
323 /*
324 * If a virtual machine is stopped by the host it can look to
325 * the watchdog like a soft lockup, check to see if the host
326 * stopped the vm before we issue the warning
327 */
328 if (kvm_check_and_clear_guest_paused())
329 return HRTIMER_RESTART;
330
331 /* only warn once */
332 if (__this_cpu_read(soft_watchdog_warn) == true) {
333 /*
334 * When multiple processes are causing softlockups the
335 * softlockup detector only warns on the first one
336 * because the code relies on a full quiet cycle to
337 * re-arm. The second process prevents the quiet cycle
338 * and never gets reported. Use task pointers to detect
339 * this.
340 */
341 if (__this_cpu_read(softlockup_task_ptr_saved) !=
342 current) {
343 __this_cpu_write(soft_watchdog_warn, false);
344 __touch_watchdog();
345 }
346 return HRTIMER_RESTART;
347 }
348
349 if (softlockup_all_cpu_backtrace) {
350 /* Prevent multiple soft-lockup reports if one cpu is already
351 * engaged in dumping cpu back traces
352 */
353 if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
354 /* Someone else will report us. Let's give up */
355 __this_cpu_write(soft_watchdog_warn, true);
356 return HRTIMER_RESTART;
357 }
358 }
359
360 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
361 smp_processor_id(), duration,
362 current->comm, task_pid_nr(current));
363 __this_cpu_write(softlockup_task_ptr_saved, current);
364 print_modules();
365 print_irqtrace_events(current);
366 if (regs)
367 show_regs(regs);
368 else
369 dump_stack();
370
371 if (softlockup_all_cpu_backtrace) {
372 /* Avoid generating two back traces for current
373 * given that one is already made above
374 */
375 trigger_allbutself_cpu_backtrace();
376
377 clear_bit(0, &soft_lockup_nmi_warn);
378 /* Barrier to sync with other cpus */
379 smp_mb__after_atomic();
380 }
381
382 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
383 if (softlockup_panic)
384 panic("softlockup: hung tasks");
385 __this_cpu_write(soft_watchdog_warn, true);
386 } else
387 __this_cpu_write(soft_watchdog_warn, false);
388
389 return HRTIMER_RESTART;
390 }
391
392 static void watchdog_set_prio(unsigned int policy, unsigned int prio)
393 {
394 struct sched_param param = { .sched_priority = prio };
395
396 sched_setscheduler(current, policy, &param);
397 }
398
399 static void watchdog_enable(unsigned int cpu)
400 {
401 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
402
403 /* kick off the timer for the hardlockup detector */
404 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
405 hrtimer->function = watchdog_timer_fn;
406
407 /* Enable the perf event */
408 watchdog_nmi_enable(cpu);
409
410 /* done here because hrtimer_start can only pin to smp_processor_id() */
411 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
412 HRTIMER_MODE_REL_PINNED);
413
414 /* initialize timestamp */
415 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
416 __touch_watchdog();
417 }
418
419 static void watchdog_disable(unsigned int cpu)
420 {
421 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
422
423 watchdog_set_prio(SCHED_NORMAL, 0);
424 hrtimer_cancel(hrtimer);
425 /* disable the perf event */
426 watchdog_nmi_disable(cpu);
427 }
428
429 static void watchdog_cleanup(unsigned int cpu, bool online)
430 {
431 watchdog_disable(cpu);
432 }
433
434 static int watchdog_should_run(unsigned int cpu)
435 {
436 return __this_cpu_read(hrtimer_interrupts) !=
437 __this_cpu_read(soft_lockup_hrtimer_cnt);
438 }
439
440 /*
441 * The watchdog thread function - touches the timestamp.
442 *
443 * It only runs once every sample_period seconds (4 seconds by
444 * default) to reset the softlockup timestamp. If this gets delayed
445 * for more than 2*watchdog_thresh seconds then the debug-printout
446 * triggers in watchdog_timer_fn().
447 */
448 static void watchdog(unsigned int cpu)
449 {
450 __this_cpu_write(soft_lockup_hrtimer_cnt,
451 __this_cpu_read(hrtimer_interrupts));
452 __touch_watchdog();
453 }
454
455 #ifdef CONFIG_HARDLOCKUP_DETECTOR
456 /*
457 * People like the simple clean cpu node info on boot.
458 * Reduce the watchdog noise by only printing messages
459 * that are different from what cpu0 displayed.
460 */
461 static unsigned long cpu0_err;
462
463 static int watchdog_nmi_enable(unsigned int cpu)
464 {
465 struct perf_event_attr *wd_attr;
466 struct perf_event *event = per_cpu(watchdog_ev, cpu);
467
468 /* is it already setup and enabled? */
469 if (event && event->state > PERF_EVENT_STATE_OFF)
470 goto out;
471
472 /* it is setup but not enabled */
473 if (event != NULL)
474 goto out_enable;
475
476 wd_attr = &wd_hw_attr;
477 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
478
479 /* Try to register using hardware perf events */
480 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
481
482 /* save cpu0 error for future comparision */
483 if (cpu == 0 && IS_ERR(event))
484 cpu0_err = PTR_ERR(event);
485
486 if (!IS_ERR(event)) {
487 /* only print for cpu0 or different than cpu0 */
488 if (cpu == 0 || cpu0_err)
489 pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
490 goto out_save;
491 }
492
493 /* skip displaying the same error again */
494 if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
495 return PTR_ERR(event);
496
497 /* vary the KERN level based on the returned errno */
498 if (PTR_ERR(event) == -EOPNOTSUPP)
499 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
500 else if (PTR_ERR(event) == -ENOENT)
501 pr_warn("disabled (cpu%i): hardware events not enabled\n",
502 cpu);
503 else
504 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
505 cpu, PTR_ERR(event));
506 return PTR_ERR(event);
507
508 /* success path */
509 out_save:
510 per_cpu(watchdog_ev, cpu) = event;
511 out_enable:
512 perf_event_enable(per_cpu(watchdog_ev, cpu));
513 out:
514 return 0;
515 }
516
517 static void watchdog_nmi_disable(unsigned int cpu)
518 {
519 struct perf_event *event = per_cpu(watchdog_ev, cpu);
520
521 if (event) {
522 perf_event_disable(event);
523 per_cpu(watchdog_ev, cpu) = NULL;
524
525 /* should be in cleanup, but blocks oprofile */
526 perf_event_release_kernel(event);
527 }
528 if (cpu == 0) {
529 /* watchdog_nmi_enable() expects this to be zero initially. */
530 cpu0_err = 0;
531 }
532 }
533 #else
534 static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
535 static void watchdog_nmi_disable(unsigned int cpu) { return; }
536 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
537
538 static struct smp_hotplug_thread watchdog_threads = {
539 .store = &softlockup_watchdog,
540 .thread_should_run = watchdog_should_run,
541 .thread_fn = watchdog,
542 .thread_comm = "watchdog/%u",
543 .setup = watchdog_enable,
544 .cleanup = watchdog_cleanup,
545 .park = watchdog_disable,
546 .unpark = watchdog_enable,
547 };
548
549 static void restart_watchdog_hrtimer(void *info)
550 {
551 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
552 int ret;
553
554 /*
555 * No need to cancel and restart hrtimer if it is currently executing
556 * because it will reprogram itself with the new period now.
557 * We should never see it unqueued here because we are running per-cpu
558 * with interrupts disabled.
559 */
560 ret = hrtimer_try_to_cancel(hrtimer);
561 if (ret == 1)
562 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
563 HRTIMER_MODE_REL_PINNED);
564 }
565
566 static void update_timers(int cpu)
567 {
568 /*
569 * Make sure that perf event counter will adopt to a new
570 * sampling period. Updating the sampling period directly would
571 * be much nicer but we do not have an API for that now so
572 * let's use a big hammer.
573 * Hrtimer will adopt the new period on the next tick but this
574 * might be late already so we have to restart the timer as well.
575 */
576 watchdog_nmi_disable(cpu);
577 smp_call_function_single(cpu, restart_watchdog_hrtimer, NULL, 1);
578 watchdog_nmi_enable(cpu);
579 }
580
581 static void update_timers_all_cpus(void)
582 {
583 int cpu;
584
585 get_online_cpus();
586 for_each_online_cpu(cpu)
587 update_timers(cpu);
588 put_online_cpus();
589 }
590
591 static int watchdog_enable_all_cpus(bool sample_period_changed)
592 {
593 int err = 0;
594
595 if (!watchdog_running) {
596 err = smpboot_register_percpu_thread(&watchdog_threads);
597 if (err)
598 pr_err("Failed to create watchdog threads, disabled\n");
599 else
600 watchdog_running = 1;
601 } else if (sample_period_changed) {
602 update_timers_all_cpus();
603 }
604
605 return err;
606 }
607
608 /* prepare/enable/disable routines */
609 /* sysctl functions */
610 #ifdef CONFIG_SYSCTL
611 static void watchdog_disable_all_cpus(void)
612 {
613 if (watchdog_running) {
614 watchdog_running = 0;
615 smpboot_unregister_percpu_thread(&watchdog_threads);
616 }
617 }
618
619 /*
620 * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
621 */
622
623 int proc_dowatchdog(struct ctl_table *table, int write,
624 void __user *buffer, size_t *lenp, loff_t *ppos)
625 {
626 int err, old_thresh, old_enabled;
627 static DEFINE_MUTEX(watchdog_proc_mutex);
628
629 mutex_lock(&watchdog_proc_mutex);
630 old_thresh = ACCESS_ONCE(watchdog_thresh);
631 old_enabled = ACCESS_ONCE(watchdog_user_enabled);
632
633 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
634 if (err || !write)
635 goto out;
636
637 set_sample_period();
638 /*
639 * Watchdog threads shouldn't be enabled if they are
640 * disabled. The 'watchdog_running' variable check in
641 * watchdog_*_all_cpus() function takes care of this.
642 */
643 if (watchdog_user_enabled && watchdog_thresh)
644 err = watchdog_enable_all_cpus(old_thresh != watchdog_thresh);
645 else
646 watchdog_disable_all_cpus();
647
648 /* Restore old values on failure */
649 if (err) {
650 watchdog_thresh = old_thresh;
651 watchdog_user_enabled = old_enabled;
652 }
653 out:
654 mutex_unlock(&watchdog_proc_mutex);
655 return err;
656 }
657 #endif /* CONFIG_SYSCTL */
658
659 void __init lockup_detector_init(void)
660 {
661 set_sample_period();
662
663 if (watchdog_user_enabled)
664 watchdog_enable_all_cpus(false);
665 }
This page took 0.043908 seconds and 6 git commands to generate.