2 * Detect hard and soft lockups on a system
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
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.
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
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>
23 #include <asm/irq_regs.h>
24 #include <linux/kvm_para.h>
25 #include <linux/perf_event.h>
27 int watchdog_user_enabled = 1;
28 int __read_mostly watchdog_thresh = 10;
30 int __read_mostly sysctl_softlockup_all_cpu_backtrace;
32 #define sysctl_softlockup_all_cpu_backtrace 0
35 static int __read_mostly watchdog_running;
36 static u64 __read_mostly sample_period;
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);
52 static unsigned long soft_lockup_nmi_warn;
56 * Should we panic when a soft-lockup or hard-lockup occurs:
58 #ifdef CONFIG_HARDLOCKUP_DETECTOR
59 static int hardlockup_panic =
60 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
62 static int __init hardlockup_panic_setup(char *str)
64 if (!strncmp(str, "panic", 5))
66 else if (!strncmp(str, "nopanic", 7))
68 else if (!strncmp(str, "0", 1))
69 watchdog_user_enabled = 0;
72 __setup("nmi_watchdog=", hardlockup_panic_setup);
75 unsigned int __read_mostly softlockup_panic =
76 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
78 static int __init softlockup_panic_setup(char *str)
80 softlockup_panic = simple_strtoul(str, NULL, 0);
84 __setup("softlockup_panic=", softlockup_panic_setup);
86 static int __init nowatchdog_setup(char *str)
88 watchdog_user_enabled = 0;
91 __setup("nowatchdog", nowatchdog_setup);
94 static int __init nosoftlockup_setup(char *str)
96 watchdog_user_enabled = 0;
99 __setup("nosoftlockup", nosoftlockup_setup);
102 static int __init softlockup_all_cpu_backtrace_setup(char *str)
104 sysctl_softlockup_all_cpu_backtrace =
105 !!simple_strtol(str, NULL, 0);
108 __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
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.
118 static int get_softlockup_thresh(void)
120 return watchdog_thresh * 2;
124 * Returns seconds, approximately. We don't need nanosecond
125 * resolution, and we don't need to waste time with a big divide when
128 static unsigned long get_timestamp(void)
130 return local_clock() >> 30LL; /* 2^30 ~= 10^9 */
133 static void set_sample_period(void)
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
142 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
145 /* Commands for resetting the watchdog */
146 static void __touch_watchdog(void)
148 __this_cpu_write(watchdog_touch_ts, get_timestamp());
151 void touch_softlockup_watchdog(void)
154 * Preemption can be enabled. It doesn't matter which CPU's timestamp
155 * gets zeroed here, so use the raw_ operation.
157 raw_cpu_write(watchdog_touch_ts, 0);
159 EXPORT_SYMBOL(touch_softlockup_watchdog);
161 void touch_all_softlockup_watchdogs(void)
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
170 for_each_online_cpu(cpu)
171 per_cpu(watchdog_touch_ts, cpu) = 0;
174 #ifdef CONFIG_HARDLOCKUP_DETECTOR
175 void touch_nmi_watchdog(void)
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
184 __raw_get_cpu_var(watchdog_nmi_touch) = true;
185 touch_softlockup_watchdog();
187 EXPORT_SYMBOL(touch_nmi_watchdog);
191 void touch_softlockup_watchdog_sync(void)
193 __raw_get_cpu_var(softlockup_touch_sync) = true;
194 __raw_get_cpu_var(watchdog_touch_ts) = 0;
197 #ifdef CONFIG_HARDLOCKUP_DETECTOR
198 /* watchdog detector functions */
199 static int is_hardlockup(void)
201 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
203 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
206 __this_cpu_write(hrtimer_interrupts_saved, hrint);
211 static int is_softlockup(unsigned long touch_ts)
213 unsigned long now = get_timestamp();
215 /* Warn about unreasonable delays: */
216 if (time_after(now, touch_ts + get_softlockup_thresh()))
217 return now - touch_ts;
222 #ifdef CONFIG_HARDLOCKUP_DETECTOR
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),
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)
237 /* Ensure the watchdog never gets throttled */
238 event->hw.interrupts = 0;
240 if (__this_cpu_read(watchdog_nmi_touch) == true) {
241 __this_cpu_write(watchdog_nmi_touch, false);
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
251 if (is_hardlockup()) {
252 int this_cpu = smp_processor_id();
254 /* only print hardlockups once */
255 if (__this_cpu_read(hard_watchdog_warn) == true)
258 if (hardlockup_panic)
259 panic("Watchdog detected hard LOCKUP on cpu %d",
262 WARN(1, "Watchdog detected hard LOCKUP on cpu %d",
265 __this_cpu_write(hard_watchdog_warn, true);
269 __this_cpu_write(hard_watchdog_warn, false);
272 #endif /* CONFIG_HARDLOCKUP_DETECTOR */
274 static void watchdog_interrupt_count(void)
276 __this_cpu_inc(hrtimer_interrupts);
279 static int watchdog_nmi_enable(unsigned int cpu);
280 static void watchdog_nmi_disable(unsigned int cpu);
282 /* watchdog kicker functions */
283 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
285 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
286 struct pt_regs *regs = get_irq_regs();
288 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
290 /* kick the hardlockup detector */
291 watchdog_interrupt_count();
293 /* kick the softlockup detector */
294 wake_up_process(__this_cpu_read(softlockup_watchdog));
297 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
300 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
302 * If the time stamp was touched atomically
303 * make sure the scheduler tick is up to date.
305 __this_cpu_write(softlockup_touch_sync, false);
309 /* Clear the guest paused flag on watchdog reset */
310 kvm_check_and_clear_guest_paused();
312 return HRTIMER_RESTART;
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
321 duration = is_softlockup(touch_ts);
322 if (unlikely(duration)) {
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
328 if (kvm_check_and_clear_guest_paused())
329 return HRTIMER_RESTART;
332 if (__this_cpu_read(soft_watchdog_warn) == true) {
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
341 if (__this_cpu_read(softlockup_task_ptr_saved) !=
343 __this_cpu_write(soft_watchdog_warn, false);
346 return HRTIMER_RESTART;
349 if (softlockup_all_cpu_backtrace) {
350 /* Prevent multiple soft-lockup reports if one cpu is already
351 * engaged in dumping cpu back traces
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;
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);
365 print_irqtrace_events(current);
371 if (softlockup_all_cpu_backtrace) {
372 /* Avoid generating two back traces for current
373 * given that one is already made above
375 trigger_allbutself_cpu_backtrace();
377 clear_bit(0, &soft_lockup_nmi_warn);
378 /* Barrier to sync with other cpus */
379 smp_mb__after_atomic();
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);
387 __this_cpu_write(soft_watchdog_warn, false);
389 return HRTIMER_RESTART;
392 static void watchdog_set_prio(unsigned int policy, unsigned int prio)
394 struct sched_param param = { .sched_priority = prio };
396 sched_setscheduler(current, policy, ¶m);
399 static void watchdog_enable(unsigned int cpu)
401 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
403 /* kick off the timer for the hardlockup detector */
404 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
405 hrtimer->function = watchdog_timer_fn;
407 /* Enable the perf event */
408 watchdog_nmi_enable(cpu);
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);
414 /* initialize timestamp */
415 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
419 static void watchdog_disable(unsigned int cpu)
421 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
423 watchdog_set_prio(SCHED_NORMAL, 0);
424 hrtimer_cancel(hrtimer);
425 /* disable the perf event */
426 watchdog_nmi_disable(cpu);
429 static void watchdog_cleanup(unsigned int cpu, bool online)
431 watchdog_disable(cpu);
434 static int watchdog_should_run(unsigned int cpu)
436 return __this_cpu_read(hrtimer_interrupts) !=
437 __this_cpu_read(soft_lockup_hrtimer_cnt);
441 * The watchdog thread function - touches the timestamp.
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().
448 static void watchdog(unsigned int cpu)
450 __this_cpu_write(soft_lockup_hrtimer_cnt,
451 __this_cpu_read(hrtimer_interrupts));
455 #ifdef CONFIG_HARDLOCKUP_DETECTOR
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.
461 static unsigned long cpu0_err;
463 static int watchdog_nmi_enable(unsigned int cpu)
465 struct perf_event_attr *wd_attr;
466 struct perf_event *event = per_cpu(watchdog_ev, cpu);
468 /* is it already setup and enabled? */
469 if (event && event->state > PERF_EVENT_STATE_OFF)
472 /* it is setup but not enabled */
476 wd_attr = &wd_hw_attr;
477 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
479 /* Try to register using hardware perf events */
480 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
482 /* save cpu0 error for future comparision */
483 if (cpu == 0 && IS_ERR(event))
484 cpu0_err = PTR_ERR(event);
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");
493 /* skip displaying the same error again */
494 if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
495 return PTR_ERR(event);
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",
504 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
505 cpu, PTR_ERR(event));
506 return PTR_ERR(event);
510 per_cpu(watchdog_ev, cpu) = event;
512 perf_event_enable(per_cpu(watchdog_ev, cpu));
517 static void watchdog_nmi_disable(unsigned int cpu)
519 struct perf_event *event = per_cpu(watchdog_ev, cpu);
522 perf_event_disable(event);
523 per_cpu(watchdog_ev, cpu) = NULL;
525 /* should be in cleanup, but blocks oprofile */
526 perf_event_release_kernel(event);
529 /* watchdog_nmi_enable() expects this to be zero initially. */
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 */
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,
549 static void restart_watchdog_hrtimer(void *info)
551 struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
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.
560 ret = hrtimer_try_to_cancel(hrtimer);
562 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
563 HRTIMER_MODE_REL_PINNED);
566 static void update_timers(int cpu)
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.
576 watchdog_nmi_disable(cpu);
577 smp_call_function_single(cpu, restart_watchdog_hrtimer, NULL, 1);
578 watchdog_nmi_enable(cpu);
581 static void update_timers_all_cpus(void)
586 for_each_online_cpu(cpu)
591 static int watchdog_enable_all_cpus(bool sample_period_changed)
595 if (!watchdog_running) {
596 err = smpboot_register_percpu_thread(&watchdog_threads);
598 pr_err("Failed to create watchdog threads, disabled\n");
600 watchdog_running = 1;
601 } else if (sample_period_changed) {
602 update_timers_all_cpus();
608 /* prepare/enable/disable routines */
609 /* sysctl functions */
611 static void watchdog_disable_all_cpus(void)
613 if (watchdog_running) {
614 watchdog_running = 0;
615 smpboot_unregister_percpu_thread(&watchdog_threads);
620 * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
623 int proc_dowatchdog(struct ctl_table *table, int write,
624 void __user *buffer, size_t *lenp, loff_t *ppos)
626 int err, old_thresh, old_enabled;
627 static DEFINE_MUTEX(watchdog_proc_mutex);
629 mutex_lock(&watchdog_proc_mutex);
630 old_thresh = ACCESS_ONCE(watchdog_thresh);
631 old_enabled = ACCESS_ONCE(watchdog_user_enabled);
633 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
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.
643 if (watchdog_user_enabled && watchdog_thresh)
644 err = watchdog_enable_all_cpus(old_thresh != watchdog_thresh);
646 watchdog_disable_all_cpus();
648 /* Restore old values on failure */
650 watchdog_thresh = old_thresh;
651 watchdog_user_enabled = old_enabled;
654 mutex_unlock(&watchdog_proc_mutex);
657 #endif /* CONFIG_SYSCTL */
659 void __init lockup_detector_init(void)
663 if (watchdog_user_enabled)
664 watchdog_enable_all_cpus(false);