xref: /openbmc/linux/kernel/watchdog.c (revision 18e5a45d)
158687acbSDon Zickus /*
258687acbSDon Zickus  * Detect hard and soft lockups on a system
358687acbSDon Zickus  *
458687acbSDon Zickus  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
558687acbSDon Zickus  *
658687acbSDon Zickus  * this code detects hard lockups: incidents in where on a CPU
758687acbSDon Zickus  * the kernel does not respond to anything except NMI.
858687acbSDon Zickus  *
958687acbSDon Zickus  * Note: Most of this code is borrowed heavily from softlockup.c,
1058687acbSDon Zickus  * so thanks to Ingo for the initial implementation.
1158687acbSDon Zickus  * Some chunks also taken from arch/x86/kernel/apic/nmi.c, thanks
1258687acbSDon Zickus  * to those contributors as well.
1358687acbSDon Zickus  */
1458687acbSDon Zickus 
1558687acbSDon Zickus #include <linux/mm.h>
1658687acbSDon Zickus #include <linux/cpu.h>
1758687acbSDon Zickus #include <linux/nmi.h>
1858687acbSDon Zickus #include <linux/init.h>
1958687acbSDon Zickus #include <linux/delay.h>
2058687acbSDon Zickus #include <linux/freezer.h>
2158687acbSDon Zickus #include <linux/kthread.h>
2258687acbSDon Zickus #include <linux/lockdep.h>
2358687acbSDon Zickus #include <linux/notifier.h>
2458687acbSDon Zickus #include <linux/module.h>
2558687acbSDon Zickus #include <linux/sysctl.h>
2658687acbSDon Zickus 
2758687acbSDon Zickus #include <asm/irq_regs.h>
2858687acbSDon Zickus #include <linux/perf_event.h>
2958687acbSDon Zickus 
304135038aSMarcin Slusarz int watchdog_enabled = 1;
314eec42f3SMandeep Singh Baines int __read_mostly watchdog_thresh = 10;
3258687acbSDon Zickus 
3358687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
3458687acbSDon Zickus static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
3558687acbSDon Zickus static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
3658687acbSDon Zickus static DEFINE_PER_CPU(bool, softlockup_touch_sync);
3758687acbSDon Zickus static DEFINE_PER_CPU(bool, soft_watchdog_warn);
3823637d47SFrederic Weisbecker #ifdef CONFIG_HARDLOCKUP_DETECTOR
39cafcd80dSDon Zickus static DEFINE_PER_CPU(bool, hard_watchdog_warn);
40cafcd80dSDon Zickus static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
4158687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
4258687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
4358687acbSDon Zickus static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
4458687acbSDon Zickus #endif
4558687acbSDon Zickus 
4658687acbSDon Zickus /* boot commands */
4758687acbSDon Zickus /*
4858687acbSDon Zickus  * Should we panic when a soft-lockup or hard-lockup occurs:
4958687acbSDon Zickus  */
5023637d47SFrederic Weisbecker #ifdef CONFIG_HARDLOCKUP_DETECTOR
51fef2c9bcSDon Zickus static int hardlockup_panic =
52fef2c9bcSDon Zickus 			CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
5358687acbSDon Zickus 
5458687acbSDon Zickus static int __init hardlockup_panic_setup(char *str)
5558687acbSDon Zickus {
5658687acbSDon Zickus 	if (!strncmp(str, "panic", 5))
5758687acbSDon Zickus 		hardlockup_panic = 1;
58fef2c9bcSDon Zickus 	else if (!strncmp(str, "nopanic", 7))
59fef2c9bcSDon Zickus 		hardlockup_panic = 0;
605dc30558SDon Zickus 	else if (!strncmp(str, "0", 1))
614135038aSMarcin Slusarz 		watchdog_enabled = 0;
6258687acbSDon Zickus 	return 1;
6358687acbSDon Zickus }
6458687acbSDon Zickus __setup("nmi_watchdog=", hardlockup_panic_setup);
6558687acbSDon Zickus #endif
6658687acbSDon Zickus 
6758687acbSDon Zickus unsigned int __read_mostly softlockup_panic =
6858687acbSDon Zickus 			CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
6958687acbSDon Zickus 
7058687acbSDon Zickus static int __init softlockup_panic_setup(char *str)
7158687acbSDon Zickus {
7258687acbSDon Zickus 	softlockup_panic = simple_strtoul(str, NULL, 0);
7358687acbSDon Zickus 
7458687acbSDon Zickus 	return 1;
7558687acbSDon Zickus }
7658687acbSDon Zickus __setup("softlockup_panic=", softlockup_panic_setup);
7758687acbSDon Zickus 
7858687acbSDon Zickus static int __init nowatchdog_setup(char *str)
7958687acbSDon Zickus {
804135038aSMarcin Slusarz 	watchdog_enabled = 0;
8158687acbSDon Zickus 	return 1;
8258687acbSDon Zickus }
8358687acbSDon Zickus __setup("nowatchdog", nowatchdog_setup);
8458687acbSDon Zickus 
8558687acbSDon Zickus /* deprecated */
8658687acbSDon Zickus static int __init nosoftlockup_setup(char *str)
8758687acbSDon Zickus {
884135038aSMarcin Slusarz 	watchdog_enabled = 0;
8958687acbSDon Zickus 	return 1;
9058687acbSDon Zickus }
9158687acbSDon Zickus __setup("nosoftlockup", nosoftlockup_setup);
9258687acbSDon Zickus /*  */
9358687acbSDon Zickus 
944eec42f3SMandeep Singh Baines /*
954eec42f3SMandeep Singh Baines  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
964eec42f3SMandeep Singh Baines  * lockups can have false positives under extreme conditions. So we generally
974eec42f3SMandeep Singh Baines  * want a higher threshold for soft lockups than for hard lockups. So we couple
984eec42f3SMandeep Singh Baines  * the thresholds with a factor: we make the soft threshold twice the amount of
994eec42f3SMandeep Singh Baines  * time the hard threshold is.
1004eec42f3SMandeep Singh Baines  */
1016e9101aeSIngo Molnar static int get_softlockup_thresh(void)
1024eec42f3SMandeep Singh Baines {
1034eec42f3SMandeep Singh Baines 	return watchdog_thresh * 2;
1044eec42f3SMandeep Singh Baines }
10558687acbSDon Zickus 
10658687acbSDon Zickus /*
10758687acbSDon Zickus  * Returns seconds, approximately.  We don't need nanosecond
10858687acbSDon Zickus  * resolution, and we don't need to waste time with a big divide when
10958687acbSDon Zickus  * 2^30ns == 1.074s.
11058687acbSDon Zickus  */
11158687acbSDon Zickus static unsigned long get_timestamp(int this_cpu)
11258687acbSDon Zickus {
11358687acbSDon Zickus 	return cpu_clock(this_cpu) >> 30LL;  /* 2^30 ~= 10^9 */
11458687acbSDon Zickus }
11558687acbSDon Zickus 
11658687acbSDon Zickus static unsigned long get_sample_period(void)
11758687acbSDon Zickus {
11858687acbSDon Zickus 	/*
119586692a5SMandeep Singh Baines 	 * convert watchdog_thresh from seconds to ns
12058687acbSDon Zickus 	 * the divide by 5 is to give hrtimer 5 chances to
12158687acbSDon Zickus 	 * increment before the hardlockup detector generates
12258687acbSDon Zickus 	 * a warning
12358687acbSDon Zickus 	 */
1244eec42f3SMandeep Singh Baines 	return get_softlockup_thresh() * (NSEC_PER_SEC / 5);
12558687acbSDon Zickus }
12658687acbSDon Zickus 
12758687acbSDon Zickus /* Commands for resetting the watchdog */
12858687acbSDon Zickus static void __touch_watchdog(void)
12958687acbSDon Zickus {
13026e09c6eSDon Zickus 	int this_cpu = smp_processor_id();
13158687acbSDon Zickus 
132909ea964SChristoph Lameter 	__this_cpu_write(watchdog_touch_ts, get_timestamp(this_cpu));
13358687acbSDon Zickus }
13458687acbSDon Zickus 
135332fbdbcSDon Zickus void touch_softlockup_watchdog(void)
13658687acbSDon Zickus {
137909ea964SChristoph Lameter 	__this_cpu_write(watchdog_touch_ts, 0);
13858687acbSDon Zickus }
1390167c781SIngo Molnar EXPORT_SYMBOL(touch_softlockup_watchdog);
14058687acbSDon Zickus 
141332fbdbcSDon Zickus void touch_all_softlockup_watchdogs(void)
14258687acbSDon Zickus {
14358687acbSDon Zickus 	int cpu;
14458687acbSDon Zickus 
14558687acbSDon Zickus 	/*
14658687acbSDon Zickus 	 * this is done lockless
14758687acbSDon Zickus 	 * do we care if a 0 races with a timestamp?
14858687acbSDon Zickus 	 * all it means is the softlock check starts one cycle later
14958687acbSDon Zickus 	 */
15058687acbSDon Zickus 	for_each_online_cpu(cpu)
15158687acbSDon Zickus 		per_cpu(watchdog_touch_ts, cpu) = 0;
15258687acbSDon Zickus }
15358687acbSDon Zickus 
154cafcd80dSDon Zickus #ifdef CONFIG_HARDLOCKUP_DETECTOR
15558687acbSDon Zickus void touch_nmi_watchdog(void)
15658687acbSDon Zickus {
15768d3f1d8SDon Zickus 	if (watchdog_enabled) {
15868d3f1d8SDon Zickus 		unsigned cpu;
15968d3f1d8SDon Zickus 
16068d3f1d8SDon Zickus 		for_each_present_cpu(cpu) {
16168d3f1d8SDon Zickus 			if (per_cpu(watchdog_nmi_touch, cpu) != true)
16268d3f1d8SDon Zickus 				per_cpu(watchdog_nmi_touch, cpu) = true;
16368d3f1d8SDon Zickus 		}
16468d3f1d8SDon Zickus 	}
165332fbdbcSDon Zickus 	touch_softlockup_watchdog();
16658687acbSDon Zickus }
16758687acbSDon Zickus EXPORT_SYMBOL(touch_nmi_watchdog);
16858687acbSDon Zickus 
169cafcd80dSDon Zickus #endif
170cafcd80dSDon Zickus 
17158687acbSDon Zickus void touch_softlockup_watchdog_sync(void)
17258687acbSDon Zickus {
17358687acbSDon Zickus 	__raw_get_cpu_var(softlockup_touch_sync) = true;
17458687acbSDon Zickus 	__raw_get_cpu_var(watchdog_touch_ts) = 0;
17558687acbSDon Zickus }
17658687acbSDon Zickus 
17723637d47SFrederic Weisbecker #ifdef CONFIG_HARDLOCKUP_DETECTOR
17858687acbSDon Zickus /* watchdog detector functions */
17926e09c6eSDon Zickus static int is_hardlockup(void)
18058687acbSDon Zickus {
181909ea964SChristoph Lameter 	unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
18258687acbSDon Zickus 
183909ea964SChristoph Lameter 	if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
18458687acbSDon Zickus 		return 1;
18558687acbSDon Zickus 
186909ea964SChristoph Lameter 	__this_cpu_write(hrtimer_interrupts_saved, hrint);
18758687acbSDon Zickus 	return 0;
18858687acbSDon Zickus }
18958687acbSDon Zickus #endif
19058687acbSDon Zickus 
19126e09c6eSDon Zickus static int is_softlockup(unsigned long touch_ts)
19258687acbSDon Zickus {
19326e09c6eSDon Zickus 	unsigned long now = get_timestamp(smp_processor_id());
19458687acbSDon Zickus 
19558687acbSDon Zickus 	/* Warn about unreasonable delays: */
1964eec42f3SMandeep Singh Baines 	if (time_after(now, touch_ts + get_softlockup_thresh()))
19758687acbSDon Zickus 		return now - touch_ts;
19858687acbSDon Zickus 
19958687acbSDon Zickus 	return 0;
20058687acbSDon Zickus }
20158687acbSDon Zickus 
20223637d47SFrederic Weisbecker #ifdef CONFIG_HARDLOCKUP_DETECTOR
2031880c4aeSCyrill Gorcunov 
20458687acbSDon Zickus static struct perf_event_attr wd_hw_attr = {
20558687acbSDon Zickus 	.type		= PERF_TYPE_HARDWARE,
20658687acbSDon Zickus 	.config		= PERF_COUNT_HW_CPU_CYCLES,
20758687acbSDon Zickus 	.size		= sizeof(struct perf_event_attr),
20858687acbSDon Zickus 	.pinned		= 1,
20958687acbSDon Zickus 	.disabled	= 1,
21058687acbSDon Zickus };
21158687acbSDon Zickus 
21258687acbSDon Zickus /* Callback function for perf event subsystem */
213a8b0ca17SPeter Zijlstra static void watchdog_overflow_callback(struct perf_event *event,
21458687acbSDon Zickus 		 struct perf_sample_data *data,
21558687acbSDon Zickus 		 struct pt_regs *regs)
21658687acbSDon Zickus {
217c6db67cdSPeter Zijlstra 	/* Ensure the watchdog never gets throttled */
218c6db67cdSPeter Zijlstra 	event->hw.interrupts = 0;
219c6db67cdSPeter Zijlstra 
220909ea964SChristoph Lameter 	if (__this_cpu_read(watchdog_nmi_touch) == true) {
221909ea964SChristoph Lameter 		__this_cpu_write(watchdog_nmi_touch, false);
22258687acbSDon Zickus 		return;
22358687acbSDon Zickus 	}
22458687acbSDon Zickus 
22558687acbSDon Zickus 	/* check for a hardlockup
22658687acbSDon Zickus 	 * This is done by making sure our timer interrupt
22758687acbSDon Zickus 	 * is incrementing.  The timer interrupt should have
22858687acbSDon Zickus 	 * fired multiple times before we overflow'd.  If it hasn't
22958687acbSDon Zickus 	 * then this is a good indication the cpu is stuck
23058687acbSDon Zickus 	 */
23126e09c6eSDon Zickus 	if (is_hardlockup()) {
23226e09c6eSDon Zickus 		int this_cpu = smp_processor_id();
23326e09c6eSDon Zickus 
23458687acbSDon Zickus 		/* only print hardlockups once */
235909ea964SChristoph Lameter 		if (__this_cpu_read(hard_watchdog_warn) == true)
23658687acbSDon Zickus 			return;
23758687acbSDon Zickus 
23858687acbSDon Zickus 		if (hardlockup_panic)
23958687acbSDon Zickus 			panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
24058687acbSDon Zickus 		else
24158687acbSDon Zickus 			WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
24258687acbSDon Zickus 
243909ea964SChristoph Lameter 		__this_cpu_write(hard_watchdog_warn, true);
24458687acbSDon Zickus 		return;
24558687acbSDon Zickus 	}
24658687acbSDon Zickus 
247909ea964SChristoph Lameter 	__this_cpu_write(hard_watchdog_warn, false);
24858687acbSDon Zickus 	return;
24958687acbSDon Zickus }
25058687acbSDon Zickus static void watchdog_interrupt_count(void)
25158687acbSDon Zickus {
252909ea964SChristoph Lameter 	__this_cpu_inc(hrtimer_interrupts);
25358687acbSDon Zickus }
25458687acbSDon Zickus #else
25558687acbSDon Zickus static inline void watchdog_interrupt_count(void) { return; }
25623637d47SFrederic Weisbecker #endif /* CONFIG_HARDLOCKUP_DETECTOR */
25758687acbSDon Zickus 
25858687acbSDon Zickus /* watchdog kicker functions */
25958687acbSDon Zickus static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
26058687acbSDon Zickus {
261909ea964SChristoph Lameter 	unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
26258687acbSDon Zickus 	struct pt_regs *regs = get_irq_regs();
26358687acbSDon Zickus 	int duration;
26458687acbSDon Zickus 
26558687acbSDon Zickus 	/* kick the hardlockup detector */
26658687acbSDon Zickus 	watchdog_interrupt_count();
26758687acbSDon Zickus 
26858687acbSDon Zickus 	/* kick the softlockup detector */
269909ea964SChristoph Lameter 	wake_up_process(__this_cpu_read(softlockup_watchdog));
27058687acbSDon Zickus 
27158687acbSDon Zickus 	/* .. and repeat */
27258687acbSDon Zickus 	hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
27358687acbSDon Zickus 
27458687acbSDon Zickus 	if (touch_ts == 0) {
275909ea964SChristoph Lameter 		if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
27658687acbSDon Zickus 			/*
27758687acbSDon Zickus 			 * If the time stamp was touched atomically
27858687acbSDon Zickus 			 * make sure the scheduler tick is up to date.
27958687acbSDon Zickus 			 */
280909ea964SChristoph Lameter 			__this_cpu_write(softlockup_touch_sync, false);
28158687acbSDon Zickus 			sched_clock_tick();
28258687acbSDon Zickus 		}
28358687acbSDon Zickus 		__touch_watchdog();
28458687acbSDon Zickus 		return HRTIMER_RESTART;
28558687acbSDon Zickus 	}
28658687acbSDon Zickus 
28758687acbSDon Zickus 	/* check for a softlockup
28858687acbSDon Zickus 	 * This is done by making sure a high priority task is
28958687acbSDon Zickus 	 * being scheduled.  The task touches the watchdog to
29058687acbSDon Zickus 	 * indicate it is getting cpu time.  If it hasn't then
29158687acbSDon Zickus 	 * this is a good indication some task is hogging the cpu
29258687acbSDon Zickus 	 */
29326e09c6eSDon Zickus 	duration = is_softlockup(touch_ts);
29458687acbSDon Zickus 	if (unlikely(duration)) {
29558687acbSDon Zickus 		/* only warn once */
296909ea964SChristoph Lameter 		if (__this_cpu_read(soft_watchdog_warn) == true)
29758687acbSDon Zickus 			return HRTIMER_RESTART;
29858687acbSDon Zickus 
29958687acbSDon Zickus 		printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
30026e09c6eSDon Zickus 			smp_processor_id(), duration,
30158687acbSDon Zickus 			current->comm, task_pid_nr(current));
30258687acbSDon Zickus 		print_modules();
30358687acbSDon Zickus 		print_irqtrace_events(current);
30458687acbSDon Zickus 		if (regs)
30558687acbSDon Zickus 			show_regs(regs);
30658687acbSDon Zickus 		else
30758687acbSDon Zickus 			dump_stack();
30858687acbSDon Zickus 
30958687acbSDon Zickus 		if (softlockup_panic)
31058687acbSDon Zickus 			panic("softlockup: hung tasks");
311909ea964SChristoph Lameter 		__this_cpu_write(soft_watchdog_warn, true);
31258687acbSDon Zickus 	} else
313909ea964SChristoph Lameter 		__this_cpu_write(soft_watchdog_warn, false);
31458687acbSDon Zickus 
31558687acbSDon Zickus 	return HRTIMER_RESTART;
31658687acbSDon Zickus }
31758687acbSDon Zickus 
31858687acbSDon Zickus 
31958687acbSDon Zickus /*
32058687acbSDon Zickus  * The watchdog thread - touches the timestamp.
32158687acbSDon Zickus  */
32226e09c6eSDon Zickus static int watchdog(void *unused)
32358687acbSDon Zickus {
324fe7de49fSKOSAKI Motohiro 	static struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
32526e09c6eSDon Zickus 	struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
32658687acbSDon Zickus 
32758687acbSDon Zickus 	sched_setscheduler(current, SCHED_FIFO, &param);
32858687acbSDon Zickus 
32958687acbSDon Zickus 	/* initialize timestamp */
33058687acbSDon Zickus 	__touch_watchdog();
33158687acbSDon Zickus 
33258687acbSDon Zickus 	/* kick off the timer for the hardlockup detector */
33358687acbSDon Zickus 	/* done here because hrtimer_start can only pin to smp_processor_id() */
33458687acbSDon Zickus 	hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
33558687acbSDon Zickus 		      HRTIMER_MODE_REL_PINNED);
33658687acbSDon Zickus 
33758687acbSDon Zickus 	set_current_state(TASK_INTERRUPTIBLE);
33858687acbSDon Zickus 	/*
33958687acbSDon Zickus 	 * Run briefly once per second to reset the softlockup timestamp.
34058687acbSDon Zickus 	 * If this gets delayed for more than 60 seconds then the
34126e09c6eSDon Zickus 	 * debug-printout triggers in watchdog_timer_fn().
34258687acbSDon Zickus 	 */
34358687acbSDon Zickus 	while (!kthread_should_stop()) {
34458687acbSDon Zickus 		__touch_watchdog();
34558687acbSDon Zickus 		schedule();
34658687acbSDon Zickus 
34758687acbSDon Zickus 		if (kthread_should_stop())
34858687acbSDon Zickus 			break;
34958687acbSDon Zickus 
35058687acbSDon Zickus 		set_current_state(TASK_INTERRUPTIBLE);
35158687acbSDon Zickus 	}
35258687acbSDon Zickus 	__set_current_state(TASK_RUNNING);
35358687acbSDon Zickus 
35458687acbSDon Zickus 	return 0;
35558687acbSDon Zickus }
35658687acbSDon Zickus 
35758687acbSDon Zickus 
35823637d47SFrederic Weisbecker #ifdef CONFIG_HARDLOCKUP_DETECTOR
35958687acbSDon Zickus static int watchdog_nmi_enable(int cpu)
36058687acbSDon Zickus {
36158687acbSDon Zickus 	struct perf_event_attr *wd_attr;
36258687acbSDon Zickus 	struct perf_event *event = per_cpu(watchdog_ev, cpu);
36358687acbSDon Zickus 
36458687acbSDon Zickus 	/* is it already setup and enabled? */
36558687acbSDon Zickus 	if (event && event->state > PERF_EVENT_STATE_OFF)
36658687acbSDon Zickus 		goto out;
36758687acbSDon Zickus 
36858687acbSDon Zickus 	/* it is setup but not enabled */
36958687acbSDon Zickus 	if (event != NULL)
37058687acbSDon Zickus 		goto out_enable;
37158687acbSDon Zickus 
37258687acbSDon Zickus 	wd_attr = &wd_hw_attr;
3734eec42f3SMandeep Singh Baines 	wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
3741880c4aeSCyrill Gorcunov 
3751880c4aeSCyrill Gorcunov 	/* Try to register using hardware perf events */
3764dc0da86SAvi Kivity 	event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
37758687acbSDon Zickus 	if (!IS_ERR(event)) {
37858687acbSDon Zickus 		printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
37958687acbSDon Zickus 		goto out_save;
38058687acbSDon Zickus 	}
38158687acbSDon Zickus 
3825651f7f4SDon Zickus 
3835651f7f4SDon Zickus 	/* vary the KERN level based on the returned errno */
3845651f7f4SDon Zickus 	if (PTR_ERR(event) == -EOPNOTSUPP)
3855651f7f4SDon Zickus 		printk(KERN_INFO "NMI watchdog disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
3865651f7f4SDon Zickus 	else if (PTR_ERR(event) == -ENOENT)
3875651f7f4SDon Zickus 		printk(KERN_WARNING "NMI watchdog disabled (cpu%i): hardware events not enabled\n", cpu);
3885651f7f4SDon Zickus 	else
3895651f7f4SDon Zickus 		printk(KERN_ERR "NMI watchdog disabled (cpu%i): unable to create perf event: %ld\n", cpu, PTR_ERR(event));
390eac24335SAkinobu Mita 	return PTR_ERR(event);
39158687acbSDon Zickus 
39258687acbSDon Zickus 	/* success path */
39358687acbSDon Zickus out_save:
39458687acbSDon Zickus 	per_cpu(watchdog_ev, cpu) = event;
39558687acbSDon Zickus out_enable:
39658687acbSDon Zickus 	perf_event_enable(per_cpu(watchdog_ev, cpu));
39758687acbSDon Zickus out:
39858687acbSDon Zickus 	return 0;
39958687acbSDon Zickus }
40058687acbSDon Zickus 
40158687acbSDon Zickus static void watchdog_nmi_disable(int cpu)
40258687acbSDon Zickus {
40358687acbSDon Zickus 	struct perf_event *event = per_cpu(watchdog_ev, cpu);
40458687acbSDon Zickus 
40558687acbSDon Zickus 	if (event) {
40658687acbSDon Zickus 		perf_event_disable(event);
40758687acbSDon Zickus 		per_cpu(watchdog_ev, cpu) = NULL;
40858687acbSDon Zickus 
40958687acbSDon Zickus 		/* should be in cleanup, but blocks oprofile */
41058687acbSDon Zickus 		perf_event_release_kernel(event);
41158687acbSDon Zickus 	}
41258687acbSDon Zickus 	return;
41358687acbSDon Zickus }
41458687acbSDon Zickus #else
41558687acbSDon Zickus static int watchdog_nmi_enable(int cpu) { return 0; }
41658687acbSDon Zickus static void watchdog_nmi_disable(int cpu) { return; }
41723637d47SFrederic Weisbecker #endif /* CONFIG_HARDLOCKUP_DETECTOR */
41858687acbSDon Zickus 
41958687acbSDon Zickus /* prepare/enable/disable routines */
4206e9101aeSIngo Molnar static void watchdog_prepare_cpu(int cpu)
42158687acbSDon Zickus {
42258687acbSDon Zickus 	struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
42358687acbSDon Zickus 
42458687acbSDon Zickus 	WARN_ON(per_cpu(softlockup_watchdog, cpu));
42558687acbSDon Zickus 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
42658687acbSDon Zickus 	hrtimer->function = watchdog_timer_fn;
42758687acbSDon Zickus }
42858687acbSDon Zickus 
42958687acbSDon Zickus static int watchdog_enable(int cpu)
43058687acbSDon Zickus {
43158687acbSDon Zickus 	struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
432f99a9933SDon Zickus 	int err = 0;
43358687acbSDon Zickus 
43458687acbSDon Zickus 	/* enable the perf event */
435eac24335SAkinobu Mita 	err = watchdog_nmi_enable(cpu);
436f99a9933SDon Zickus 
437f99a9933SDon Zickus 	/* Regardless of err above, fall through and start softlockup */
43858687acbSDon Zickus 
43958687acbSDon Zickus 	/* create the watchdog thread */
44058687acbSDon Zickus 	if (!p) {
44118e5a45dSEric Dumazet 		p = kthread_create_on_node(watchdog, NULL, cpu_to_node(cpu), "watchdog/%d", cpu);
44258687acbSDon Zickus 		if (IS_ERR(p)) {
44358687acbSDon Zickus 			printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);
4441409f141SHillf Danton 			if (!err) {
445f99a9933SDon Zickus 				/* if hardlockup hasn't already set this */
446f99a9933SDon Zickus 				err = PTR_ERR(p);
4471409f141SHillf Danton 				/* and disable the perf event */
4481409f141SHillf Danton 				watchdog_nmi_disable(cpu);
4491409f141SHillf Danton 			}
450f99a9933SDon Zickus 			goto out;
45158687acbSDon Zickus 		}
45258687acbSDon Zickus 		kthread_bind(p, cpu);
45358687acbSDon Zickus 		per_cpu(watchdog_touch_ts, cpu) = 0;
45458687acbSDon Zickus 		per_cpu(softlockup_watchdog, cpu) = p;
45558687acbSDon Zickus 		wake_up_process(p);
45658687acbSDon Zickus 	}
45758687acbSDon Zickus 
458f99a9933SDon Zickus out:
459f99a9933SDon Zickus 	return err;
46058687acbSDon Zickus }
46158687acbSDon Zickus 
46258687acbSDon Zickus static void watchdog_disable(int cpu)
46358687acbSDon Zickus {
46458687acbSDon Zickus 	struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
46558687acbSDon Zickus 	struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
46658687acbSDon Zickus 
46758687acbSDon Zickus 	/*
46858687acbSDon Zickus 	 * cancel the timer first to stop incrementing the stats
46958687acbSDon Zickus 	 * and waking up the kthread
47058687acbSDon Zickus 	 */
47158687acbSDon Zickus 	hrtimer_cancel(hrtimer);
47258687acbSDon Zickus 
47358687acbSDon Zickus 	/* disable the perf event */
47458687acbSDon Zickus 	watchdog_nmi_disable(cpu);
47558687acbSDon Zickus 
47658687acbSDon Zickus 	/* stop the watchdog thread */
47758687acbSDon Zickus 	if (p) {
47858687acbSDon Zickus 		per_cpu(softlockup_watchdog, cpu) = NULL;
47958687acbSDon Zickus 		kthread_stop(p);
48058687acbSDon Zickus 	}
48158687acbSDon Zickus }
48258687acbSDon Zickus 
48358687acbSDon Zickus static void watchdog_enable_all_cpus(void)
48458687acbSDon Zickus {
48558687acbSDon Zickus 	int cpu;
48639735766SMarcin Slusarz 
48739735766SMarcin Slusarz 	watchdog_enabled = 0;
48858687acbSDon Zickus 
48958687acbSDon Zickus 	for_each_online_cpu(cpu)
49039735766SMarcin Slusarz 		if (!watchdog_enable(cpu))
49139735766SMarcin Slusarz 			/* if any cpu succeeds, watchdog is considered
49239735766SMarcin Slusarz 			   enabled for the system */
49339735766SMarcin Slusarz 			watchdog_enabled = 1;
49458687acbSDon Zickus 
49539735766SMarcin Slusarz 	if (!watchdog_enabled)
49658687acbSDon Zickus 		printk(KERN_ERR "watchdog: failed to be enabled on some cpus\n");
49758687acbSDon Zickus 
49858687acbSDon Zickus }
49958687acbSDon Zickus 
50058687acbSDon Zickus static void watchdog_disable_all_cpus(void)
50158687acbSDon Zickus {
50258687acbSDon Zickus 	int cpu;
50358687acbSDon Zickus 
50458687acbSDon Zickus 	for_each_online_cpu(cpu)
50558687acbSDon Zickus 		watchdog_disable(cpu);
50658687acbSDon Zickus 
50758687acbSDon Zickus 	/* if all watchdogs are disabled, then they are disabled for the system */
50858687acbSDon Zickus 	watchdog_enabled = 0;
50958687acbSDon Zickus }
51058687acbSDon Zickus 
51158687acbSDon Zickus 
51258687acbSDon Zickus /* sysctl functions */
51358687acbSDon Zickus #ifdef CONFIG_SYSCTL
51458687acbSDon Zickus /*
515586692a5SMandeep Singh Baines  * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
51658687acbSDon Zickus  */
51758687acbSDon Zickus 
518586692a5SMandeep Singh Baines int proc_dowatchdog(struct ctl_table *table, int write,
519586692a5SMandeep Singh Baines 		    void __user *buffer, size_t *lenp, loff_t *ppos)
52058687acbSDon Zickus {
521e04ab2bcSMandeep Singh Baines 	int ret;
52258687acbSDon Zickus 
523586692a5SMandeep Singh Baines 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
524e04ab2bcSMandeep Singh Baines 	if (ret || !write)
525e04ab2bcSMandeep Singh Baines 		goto out;
526e04ab2bcSMandeep Singh Baines 
527586692a5SMandeep Singh Baines 	if (watchdog_enabled && watchdog_thresh)
52858687acbSDon Zickus 		watchdog_enable_all_cpus();
52958687acbSDon Zickus 	else
53058687acbSDon Zickus 		watchdog_disable_all_cpus();
531e04ab2bcSMandeep Singh Baines 
532e04ab2bcSMandeep Singh Baines out:
533e04ab2bcSMandeep Singh Baines 	return ret;
53458687acbSDon Zickus }
53558687acbSDon Zickus #endif /* CONFIG_SYSCTL */
53658687acbSDon Zickus 
53758687acbSDon Zickus 
53858687acbSDon Zickus /*
53958687acbSDon Zickus  * Create/destroy watchdog threads as CPUs come and go:
54058687acbSDon Zickus  */
54158687acbSDon Zickus static int __cpuinit
54258687acbSDon Zickus cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
54358687acbSDon Zickus {
54458687acbSDon Zickus 	int hotcpu = (unsigned long)hcpu;
54558687acbSDon Zickus 
54658687acbSDon Zickus 	switch (action) {
54758687acbSDon Zickus 	case CPU_UP_PREPARE:
54858687acbSDon Zickus 	case CPU_UP_PREPARE_FROZEN:
5496e9101aeSIngo Molnar 		watchdog_prepare_cpu(hotcpu);
55058687acbSDon Zickus 		break;
55158687acbSDon Zickus 	case CPU_ONLINE:
55258687acbSDon Zickus 	case CPU_ONLINE_FROZEN:
5534135038aSMarcin Slusarz 		if (watchdog_enabled)
5546e9101aeSIngo Molnar 			watchdog_enable(hotcpu);
55558687acbSDon Zickus 		break;
55658687acbSDon Zickus #ifdef CONFIG_HOTPLUG_CPU
55758687acbSDon Zickus 	case CPU_UP_CANCELED:
55858687acbSDon Zickus 	case CPU_UP_CANCELED_FROZEN:
55958687acbSDon Zickus 		watchdog_disable(hotcpu);
56058687acbSDon Zickus 		break;
56158687acbSDon Zickus 	case CPU_DEAD:
56258687acbSDon Zickus 	case CPU_DEAD_FROZEN:
56358687acbSDon Zickus 		watchdog_disable(hotcpu);
56458687acbSDon Zickus 		break;
56558687acbSDon Zickus #endif /* CONFIG_HOTPLUG_CPU */
56658687acbSDon Zickus 	}
567f99a9933SDon Zickus 
568f99a9933SDon Zickus 	/*
569f99a9933SDon Zickus 	 * hardlockup and softlockup are not important enough
570f99a9933SDon Zickus 	 * to block cpu bring up.  Just always succeed and
571f99a9933SDon Zickus 	 * rely on printk output to flag problems.
572f99a9933SDon Zickus 	 */
573f99a9933SDon Zickus 	return NOTIFY_OK;
57458687acbSDon Zickus }
57558687acbSDon Zickus 
57658687acbSDon Zickus static struct notifier_block __cpuinitdata cpu_nfb = {
57758687acbSDon Zickus 	.notifier_call = cpu_callback
57858687acbSDon Zickus };
57958687acbSDon Zickus 
580004417a6SPeter Zijlstra void __init lockup_detector_init(void)
58158687acbSDon Zickus {
58258687acbSDon Zickus 	void *cpu = (void *)(long)smp_processor_id();
58358687acbSDon Zickus 	int err;
58458687acbSDon Zickus 
58558687acbSDon Zickus 	err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
586eac24335SAkinobu Mita 	WARN_ON(notifier_to_errno(err));
58758687acbSDon Zickus 
58858687acbSDon Zickus 	cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
58958687acbSDon Zickus 	register_cpu_notifier(&cpu_nfb);
59058687acbSDon Zickus 
591004417a6SPeter Zijlstra 	return;
59258687acbSDon Zickus }
593