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/delay.h>
19 #include <linux/freezer.h>
20 #include <linux/kthread.h>
21 #include <linux/lockdep.h>
22 #include <linux/notifier.h>
23 #include <linux/module.h>
24 #include <linux/sysctl.h>
25 #include <linux/smpboot.h>
26 #include <linux/sched/rt.h>
28 #include <asm/irq_regs.h>
29 #include <linux/kvm_para.h>
30 #include <linux/perf_event.h>
32 int watchdog_enabled
= 1;
33 int __read_mostly watchdog_thresh
= 10;
34 static int __read_mostly watchdog_disabled
;
35 static u64 __read_mostly sample_period
;
37 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts
);
38 static DEFINE_PER_CPU(struct task_struct
*, softlockup_watchdog
);
39 static DEFINE_PER_CPU(struct hrtimer
, watchdog_hrtimer
);
40 static DEFINE_PER_CPU(bool, softlockup_touch_sync
);
41 static DEFINE_PER_CPU(bool, soft_watchdog_warn
);
42 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts
);
43 static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt
);
44 #ifdef CONFIG_HARDLOCKUP_DETECTOR
45 static DEFINE_PER_CPU(bool, hard_watchdog_warn
);
46 static DEFINE_PER_CPU(bool, watchdog_nmi_touch
);
47 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved
);
49 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
50 static cpumask_t __read_mostly watchdog_cpus
;
52 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
53 static DEFINE_PER_CPU(struct perf_event
*, watchdog_ev
);
58 * Should we panic when a soft-lockup or hard-lockup occurs:
60 #ifdef CONFIG_HARDLOCKUP_DETECTOR
61 static int hardlockup_panic
=
62 CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE
;
64 static int __init
hardlockup_panic_setup(char *str
)
66 if (!strncmp(str
, "panic", 5))
68 else if (!strncmp(str
, "nopanic", 7))
70 else if (!strncmp(str
, "0", 1))
74 __setup("nmi_watchdog=", hardlockup_panic_setup
);
77 unsigned int __read_mostly softlockup_panic
=
78 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE
;
80 static int __init
softlockup_panic_setup(char *str
)
82 softlockup_panic
= simple_strtoul(str
, NULL
, 0);
86 __setup("softlockup_panic=", softlockup_panic_setup
);
88 static int __init
nowatchdog_setup(char *str
)
93 __setup("nowatchdog", nowatchdog_setup
);
96 static int __init
nosoftlockup_setup(char *str
)
101 __setup("nosoftlockup", nosoftlockup_setup
);
105 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
106 * lockups can have false positives under extreme conditions. So we generally
107 * want a higher threshold for soft lockups than for hard lockups. So we couple
108 * the thresholds with a factor: we make the soft threshold twice the amount of
109 * time the hard threshold is.
111 static int get_softlockup_thresh(void)
113 return watchdog_thresh
* 2;
117 * Returns seconds, approximately. We don't need nanosecond
118 * resolution, and we don't need to waste time with a big divide when
121 static unsigned long get_timestamp(void)
123 return local_clock() >> 30LL; /* 2^30 ~= 10^9 */
126 static void set_sample_period(void)
129 * convert watchdog_thresh from seconds to ns
130 * the divide by 5 is to give hrtimer several chances (two
131 * or three with the current relation between the soft
132 * and hard thresholds) to increment before the
133 * hardlockup detector generates a warning
135 sample_period
= get_softlockup_thresh() * ((u64
)NSEC_PER_SEC
/ 5);
138 /* Commands for resetting the watchdog */
139 static void __touch_watchdog(void)
141 __this_cpu_write(watchdog_touch_ts
, get_timestamp());
144 void touch_softlockup_watchdog(void)
146 __this_cpu_write(watchdog_touch_ts
, 0);
148 EXPORT_SYMBOL(touch_softlockup_watchdog
);
150 void touch_all_softlockup_watchdogs(void)
155 * this is done lockless
156 * do we care if a 0 races with a timestamp?
157 * all it means is the softlock check starts one cycle later
159 for_each_online_cpu(cpu
)
160 per_cpu(watchdog_touch_ts
, cpu
) = 0;
163 #ifdef CONFIG_HARDLOCKUP_DETECTOR
164 void touch_nmi_watchdog(void)
166 if (watchdog_enabled
) {
169 for_each_present_cpu(cpu
) {
170 if (per_cpu(watchdog_nmi_touch
, cpu
) != true)
171 per_cpu(watchdog_nmi_touch
, cpu
) = true;
174 touch_softlockup_watchdog();
176 EXPORT_SYMBOL(touch_nmi_watchdog
);
180 void touch_softlockup_watchdog_sync(void)
182 __raw_get_cpu_var(softlockup_touch_sync
) = true;
183 __raw_get_cpu_var(watchdog_touch_ts
) = 0;
186 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
187 /* watchdog detector functions */
188 static int is_hardlockup(void)
190 unsigned long hrint
= __this_cpu_read(hrtimer_interrupts
);
192 if (__this_cpu_read(hrtimer_interrupts_saved
) == hrint
)
195 __this_cpu_write(hrtimer_interrupts_saved
, hrint
);
200 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
201 static unsigned int watchdog_next_cpu(unsigned int cpu
)
203 cpumask_t cpus
= watchdog_cpus
;
204 unsigned int next_cpu
;
206 next_cpu
= cpumask_next(cpu
, &cpus
);
207 if (next_cpu
>= nr_cpu_ids
)
208 next_cpu
= cpumask_first(&cpus
);
216 static int is_hardlockup_other_cpu(unsigned int cpu
)
218 unsigned long hrint
= per_cpu(hrtimer_interrupts
, cpu
);
220 if (per_cpu(hrtimer_interrupts_saved
, cpu
) == hrint
)
223 per_cpu(hrtimer_interrupts_saved
, cpu
) = hrint
;
227 static void watchdog_check_hardlockup_other_cpu(void)
229 unsigned int next_cpu
;
232 * Test for hardlockups every 3 samples. The sample period is
233 * watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
234 * watchdog_thresh (over by 20%).
236 if (__this_cpu_read(hrtimer_interrupts
) % 3 != 0)
239 /* check for a hardlockup on the next cpu */
240 next_cpu
= watchdog_next_cpu(smp_processor_id());
241 if (next_cpu
>= nr_cpu_ids
)
246 if (per_cpu(watchdog_nmi_touch
, next_cpu
) == true) {
247 per_cpu(watchdog_nmi_touch
, next_cpu
) = false;
251 if (is_hardlockup_other_cpu(next_cpu
)) {
253 if (per_cpu(hard_watchdog_warn
, next_cpu
) == true)
256 if (hardlockup_panic
) {
257 exynos_ss_set_hardlockup(hardlockup_panic
);
258 panic("Watchdog detected hard LOCKUP on cpu %u", next_cpu
);
260 WARN(1, "Watchdog detected hard LOCKUP on cpu %u", next_cpu
);
263 per_cpu(hard_watchdog_warn
, next_cpu
) = true;
265 per_cpu(hard_watchdog_warn
, next_cpu
) = false;
269 static inline void watchdog_check_hardlockup_other_cpu(void) { return; }
272 static int is_softlockup(unsigned long touch_ts
)
274 unsigned long now
= get_timestamp();
276 /* Warn about unreasonable delays: */
277 if (time_after(now
, touch_ts
+ get_softlockup_thresh()))
278 return now
- touch_ts
;
283 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
285 static struct perf_event_attr wd_hw_attr
= {
286 .type
= PERF_TYPE_HARDWARE
,
287 .config
= PERF_COUNT_HW_CPU_CYCLES
,
288 .size
= sizeof(struct perf_event_attr
),
293 /* Callback function for perf event subsystem */
294 static void watchdog_overflow_callback(struct perf_event
*event
,
295 struct perf_sample_data
*data
,
296 struct pt_regs
*regs
)
298 /* Ensure the watchdog never gets throttled */
299 event
->hw
.interrupts
= 0;
301 if (__this_cpu_read(watchdog_nmi_touch
) == true) {
302 __this_cpu_write(watchdog_nmi_touch
, false);
306 /* check for a hardlockup
307 * This is done by making sure our timer interrupt
308 * is incrementing. The timer interrupt should have
309 * fired multiple times before we overflow'd. If it hasn't
310 * then this is a good indication the cpu is stuck
312 if (is_hardlockup()) {
313 int this_cpu
= smp_processor_id();
315 /* only print hardlockups once */
316 if (__this_cpu_read(hard_watchdog_warn
) == true)
319 if (hardlockup_panic
) {
320 exynos_ss_set_hardlockup(hardlockup_panic
);
321 panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu
);
323 WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu
);
326 __this_cpu_write(hard_watchdog_warn
, true);
330 __this_cpu_write(hard_watchdog_warn
, false);
333 #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
335 static void watchdog_interrupt_count(void)
337 __this_cpu_inc(hrtimer_interrupts
);
340 static int watchdog_nmi_enable(unsigned int cpu
);
341 static void watchdog_nmi_disable(unsigned int cpu
);
343 /* watchdog kicker functions */
344 static enum hrtimer_restart
watchdog_timer_fn(struct hrtimer
*hrtimer
)
346 unsigned long touch_ts
= __this_cpu_read(watchdog_touch_ts
);
347 struct pt_regs
*regs
= get_irq_regs();
350 /* kick the hardlockup detector */
351 watchdog_interrupt_count();
353 /* test for hardlockups on the next cpu */
354 watchdog_check_hardlockup_other_cpu();
356 /* kick the softlockup detector */
357 wake_up_process(__this_cpu_read(softlockup_watchdog
));
360 hrtimer_forward_now(hrtimer
, ns_to_ktime(sample_period
));
363 if (unlikely(__this_cpu_read(softlockup_touch_sync
))) {
365 * If the time stamp was touched atomically
366 * make sure the scheduler tick is up to date.
368 __this_cpu_write(softlockup_touch_sync
, false);
372 /* Clear the guest paused flag on watchdog reset */
373 kvm_check_and_clear_guest_paused();
375 return HRTIMER_RESTART
;
378 /* check for a softlockup
379 * This is done by making sure a high priority task is
380 * being scheduled. The task touches the watchdog to
381 * indicate it is getting cpu time. If it hasn't then
382 * this is a good indication some task is hogging the cpu
384 duration
= is_softlockup(touch_ts
);
385 if (unlikely(duration
)) {
387 * If a virtual machine is stopped by the host it can look to
388 * the watchdog like a soft lockup, check to see if the host
389 * stopped the vm before we issue the warning
391 if (kvm_check_and_clear_guest_paused())
392 return HRTIMER_RESTART
;
395 if (__this_cpu_read(soft_watchdog_warn
) == true)
396 return HRTIMER_RESTART
;
398 printk(KERN_EMERG
"BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
399 smp_processor_id(), duration
,
400 current
->comm
, task_pid_nr(current
));
402 print_irqtrace_events(current
);
408 if (softlockup_panic
)
409 panic("softlockup: hung tasks");
410 __this_cpu_write(soft_watchdog_warn
, true);
412 __this_cpu_write(soft_watchdog_warn
, false);
414 return HRTIMER_RESTART
;
417 static void watchdog_set_prio(unsigned int policy
, unsigned int prio
)
419 struct sched_param param
= { .sched_priority
= prio
};
421 sched_setscheduler(current
, policy
, ¶m
);
424 static void watchdog_enable(unsigned int cpu
)
426 struct hrtimer
*hrtimer
= &__raw_get_cpu_var(watchdog_hrtimer
);
428 /* kick off the timer for the hardlockup detector */
429 hrtimer_init(hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
430 hrtimer
->function
= watchdog_timer_fn
;
432 if (!watchdog_enabled
) {
433 kthread_park(current
);
437 /* Enable the perf event */
438 watchdog_nmi_enable(cpu
);
440 /* done here because hrtimer_start can only pin to smp_processor_id() */
441 hrtimer_start(hrtimer
, ns_to_ktime(sample_period
),
442 HRTIMER_MODE_REL_PINNED
);
444 /* initialize timestamp */
445 watchdog_set_prio(SCHED_FIFO
, MAX_RT_PRIO
- 1);
449 static void watchdog_disable(unsigned int cpu
)
451 struct hrtimer
*hrtimer
= &__raw_get_cpu_var(watchdog_hrtimer
);
453 watchdog_set_prio(SCHED_NORMAL
, 0);
454 hrtimer_cancel(hrtimer
);
455 /* disable the perf event */
456 watchdog_nmi_disable(cpu
);
459 static int watchdog_should_run(unsigned int cpu
)
461 return __this_cpu_read(hrtimer_interrupts
) !=
462 __this_cpu_read(soft_lockup_hrtimer_cnt
);
466 * The watchdog thread function - touches the timestamp.
468 * It only runs once every sample_period seconds (4 seconds by
469 * default) to reset the softlockup timestamp. If this gets delayed
470 * for more than 2*watchdog_thresh seconds then the debug-printout
471 * triggers in watchdog_timer_fn().
473 static void watchdog(unsigned int cpu
)
475 __this_cpu_write(soft_lockup_hrtimer_cnt
,
476 __this_cpu_read(hrtimer_interrupts
));
480 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
482 * People like the simple clean cpu node info on boot.
483 * Reduce the watchdog noise by only printing messages
484 * that are different from what cpu0 displayed.
486 static unsigned long cpu0_err
;
488 static int watchdog_nmi_enable(unsigned int cpu
)
490 struct perf_event_attr
*wd_attr
;
491 struct perf_event
*event
= per_cpu(watchdog_ev
, cpu
);
493 /* is it already setup and enabled? */
494 if (event
&& event
->state
> PERF_EVENT_STATE_OFF
)
497 /* it is setup but not enabled */
501 wd_attr
= &wd_hw_attr
;
502 wd_attr
->sample_period
= hw_nmi_get_sample_period(watchdog_thresh
);
504 /* Try to register using hardware perf events */
505 event
= perf_event_create_kernel_counter(wd_attr
, cpu
, NULL
, watchdog_overflow_callback
, NULL
);
507 /* save cpu0 error for future comparision */
508 if (cpu
== 0 && IS_ERR(event
))
509 cpu0_err
= PTR_ERR(event
);
511 if (!IS_ERR(event
)) {
512 /* only print for cpu0 or different than cpu0 */
513 if (cpu
== 0 || cpu0_err
)
514 pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
518 /* skip displaying the same error again */
519 if (cpu
> 0 && (PTR_ERR(event
) == cpu0_err
))
520 return PTR_ERR(event
);
522 /* vary the KERN level based on the returned errno */
523 if (PTR_ERR(event
) == -EOPNOTSUPP
)
524 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu
);
525 else if (PTR_ERR(event
) == -ENOENT
)
526 pr_warning("disabled (cpu%i): hardware events not enabled\n",
529 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
530 cpu
, PTR_ERR(event
));
531 return PTR_ERR(event
);
535 per_cpu(watchdog_ev
, cpu
) = event
;
537 perf_event_enable(per_cpu(watchdog_ev
, cpu
));
542 static void watchdog_nmi_disable(unsigned int cpu
)
544 struct perf_event
*event
= per_cpu(watchdog_ev
, cpu
);
547 perf_event_disable(event
);
548 per_cpu(watchdog_ev
, cpu
) = NULL
;
550 /* should be in cleanup, but blocks oprofile */
551 perf_event_release_kernel(event
);
556 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
557 static int watchdog_nmi_enable(unsigned int cpu
)
560 * The new cpu will be marked online before the first hrtimer interrupt
561 * runs on it. If another cpu tests for a hardlockup on the new cpu
562 * before it has run its first hrtimer, it will get a false positive.
563 * Touch the watchdog on the new cpu to delay the first check for at
564 * least 3 sampling periods to guarantee one hrtimer has run on the new
567 per_cpu(watchdog_nmi_touch
, cpu
) = true;
569 cpumask_set_cpu(cpu
, &watchdog_cpus
);
573 static void watchdog_nmi_disable(unsigned int cpu
)
575 unsigned int next_cpu
= watchdog_next_cpu(cpu
);
578 * Offlining this cpu will cause the cpu before this one to start
579 * checking the one after this one. If this cpu just finished checking
580 * the next cpu and updating hrtimer_interrupts_saved, and then the
581 * previous cpu checks it within one sample period, it will trigger a
582 * false positive. Touch the watchdog on the next cpu to prevent it.
584 if (next_cpu
< nr_cpu_ids
)
585 per_cpu(watchdog_nmi_touch
, next_cpu
) = true;
587 cpumask_clear_cpu(cpu
, &watchdog_cpus
);
590 static int watchdog_nmi_enable(unsigned int cpu
) { return 0; }
591 static void watchdog_nmi_disable(unsigned int cpu
) { return; }
592 #endif /* CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU */
593 #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
595 /* prepare/enable/disable routines */
596 /* sysctl functions */
598 static void watchdog_enable_all_cpus(void)
602 if (watchdog_disabled
) {
603 watchdog_disabled
= 0;
604 for_each_online_cpu(cpu
)
605 kthread_unpark(per_cpu(softlockup_watchdog
, cpu
));
609 static void watchdog_disable_all_cpus(void)
613 if (!watchdog_disabled
) {
614 watchdog_disabled
= 1;
615 for_each_online_cpu(cpu
)
616 kthread_park(per_cpu(softlockup_watchdog
, cpu
));
621 * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
624 int proc_dowatchdog(struct ctl_table
*table
, int write
,
625 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
629 if (watchdog_disabled
< 0)
632 ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
638 * Watchdog threads shouldn't be enabled if they are
639 * disabled. The 'watchdog_disabled' variable check in
640 * watchdog_*_all_cpus() function takes care of this.
642 if (watchdog_enabled
&& watchdog_thresh
)
643 watchdog_enable_all_cpus();
645 watchdog_disable_all_cpus();
649 #endif /* CONFIG_SYSCTL */
651 static struct smp_hotplug_thread watchdog_threads
= {
652 .store
= &softlockup_watchdog
,
653 .thread_should_run
= watchdog_should_run
,
654 .thread_fn
= watchdog
,
655 .thread_comm
= "watchdog/%u",
656 .setup
= watchdog_enable
,
657 .park
= watchdog_disable
,
658 .unpark
= watchdog_enable
,
661 void __init
lockup_detector_init(void)
664 if (smpboot_register_percpu_thread(&watchdog_threads
)) {
665 pr_err("Failed to create watchdog threads, disabled\n");
666 watchdog_disabled
= -ENODEV
;