xref: /openbmc/linux/kernel/watchdog.c (revision d7c54733)
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 
3058687acbSDon Zickus int watchdog_enabled;
3158687acbSDon Zickus int __read_mostly softlockup_thresh = 60;
3258687acbSDon Zickus 
3358687acbSDon Zickus static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
34d7c54733SDon Zickus static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
3558687acbSDon Zickus static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
3658687acbSDon Zickus static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
3758687acbSDon Zickus static DEFINE_PER_CPU(bool, softlockup_touch_sync);
3858687acbSDon Zickus static DEFINE_PER_CPU(bool, hard_watchdog_warn);
3958687acbSDon Zickus static DEFINE_PER_CPU(bool, soft_watchdog_warn);
4058687acbSDon Zickus #ifdef CONFIG_PERF_EVENTS_NMI
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 static int __read_mostly did_panic;
4758687acbSDon Zickus static int __initdata no_watchdog;
4858687acbSDon Zickus 
4958687acbSDon Zickus 
5058687acbSDon Zickus /* boot commands */
5158687acbSDon Zickus /*
5258687acbSDon Zickus  * Should we panic when a soft-lockup or hard-lockup occurs:
5358687acbSDon Zickus  */
5458687acbSDon Zickus #ifdef CONFIG_PERF_EVENTS_NMI
5558687acbSDon Zickus static int hardlockup_panic;
5658687acbSDon Zickus 
5758687acbSDon Zickus static int __init hardlockup_panic_setup(char *str)
5858687acbSDon Zickus {
5958687acbSDon Zickus 	if (!strncmp(str, "panic", 5))
6058687acbSDon Zickus 		hardlockup_panic = 1;
6158687acbSDon Zickus 	return 1;
6258687acbSDon Zickus }
6358687acbSDon Zickus __setup("nmi_watchdog=", hardlockup_panic_setup);
6458687acbSDon Zickus #endif
6558687acbSDon Zickus 
6658687acbSDon Zickus unsigned int __read_mostly softlockup_panic =
6758687acbSDon Zickus 			CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
6858687acbSDon Zickus 
6958687acbSDon Zickus static int __init softlockup_panic_setup(char *str)
7058687acbSDon Zickus {
7158687acbSDon Zickus 	softlockup_panic = simple_strtoul(str, NULL, 0);
7258687acbSDon Zickus 
7358687acbSDon Zickus 	return 1;
7458687acbSDon Zickus }
7558687acbSDon Zickus __setup("softlockup_panic=", softlockup_panic_setup);
7658687acbSDon Zickus 
7758687acbSDon Zickus static int __init nowatchdog_setup(char *str)
7858687acbSDon Zickus {
7958687acbSDon Zickus 	no_watchdog = 1;
8058687acbSDon Zickus 	return 1;
8158687acbSDon Zickus }
8258687acbSDon Zickus __setup("nowatchdog", nowatchdog_setup);
8358687acbSDon Zickus 
8458687acbSDon Zickus /* deprecated */
8558687acbSDon Zickus static int __init nosoftlockup_setup(char *str)
8658687acbSDon Zickus {
8758687acbSDon Zickus 	no_watchdog = 1;
8858687acbSDon Zickus 	return 1;
8958687acbSDon Zickus }
9058687acbSDon Zickus __setup("nosoftlockup", nosoftlockup_setup);
9158687acbSDon Zickus /*  */
9258687acbSDon Zickus 
9358687acbSDon Zickus 
9458687acbSDon Zickus /*
9558687acbSDon Zickus  * Returns seconds, approximately.  We don't need nanosecond
9658687acbSDon Zickus  * resolution, and we don't need to waste time with a big divide when
9758687acbSDon Zickus  * 2^30ns == 1.074s.
9858687acbSDon Zickus  */
9958687acbSDon Zickus static unsigned long get_timestamp(int this_cpu)
10058687acbSDon Zickus {
10158687acbSDon Zickus 	return cpu_clock(this_cpu) >> 30LL;  /* 2^30 ~= 10^9 */
10258687acbSDon Zickus }
10358687acbSDon Zickus 
10458687acbSDon Zickus static unsigned long get_sample_period(void)
10558687acbSDon Zickus {
10658687acbSDon Zickus 	/*
10758687acbSDon Zickus 	 * convert softlockup_thresh from seconds to ns
10858687acbSDon Zickus 	 * the divide by 5 is to give hrtimer 5 chances to
10958687acbSDon Zickus 	 * increment before the hardlockup detector generates
11058687acbSDon Zickus 	 * a warning
11158687acbSDon Zickus 	 */
11258687acbSDon Zickus 	return softlockup_thresh / 5 * NSEC_PER_SEC;
11358687acbSDon Zickus }
11458687acbSDon Zickus 
11558687acbSDon Zickus /* Commands for resetting the watchdog */
11658687acbSDon Zickus static void __touch_watchdog(void)
11758687acbSDon Zickus {
11858687acbSDon Zickus 	int this_cpu = raw_smp_processor_id();
11958687acbSDon Zickus 
12058687acbSDon Zickus 	__get_cpu_var(watchdog_touch_ts) = get_timestamp(this_cpu);
12158687acbSDon Zickus }
12258687acbSDon Zickus 
123332fbdbcSDon Zickus void touch_softlockup_watchdog(void)
12458687acbSDon Zickus {
12558687acbSDon Zickus 	__get_cpu_var(watchdog_touch_ts) = 0;
12658687acbSDon Zickus }
12758687acbSDon Zickus 
128332fbdbcSDon Zickus void touch_all_softlockup_watchdogs(void)
12958687acbSDon Zickus {
13058687acbSDon Zickus 	int cpu;
13158687acbSDon Zickus 
13258687acbSDon Zickus 	/*
13358687acbSDon Zickus 	 * this is done lockless
13458687acbSDon Zickus 	 * do we care if a 0 races with a timestamp?
13558687acbSDon Zickus 	 * all it means is the softlock check starts one cycle later
13658687acbSDon Zickus 	 */
13758687acbSDon Zickus 	for_each_online_cpu(cpu)
13858687acbSDon Zickus 		per_cpu(watchdog_touch_ts, cpu) = 0;
13958687acbSDon Zickus }
14058687acbSDon Zickus 
14158687acbSDon Zickus void touch_nmi_watchdog(void)
14258687acbSDon Zickus {
143d7c54733SDon Zickus 	__get_cpu_var(watchdog_nmi_touch) = true;
144332fbdbcSDon Zickus 	touch_softlockup_watchdog();
14558687acbSDon Zickus }
14658687acbSDon Zickus EXPORT_SYMBOL(touch_nmi_watchdog);
14758687acbSDon Zickus 
14858687acbSDon Zickus void touch_softlockup_watchdog_sync(void)
14958687acbSDon Zickus {
15058687acbSDon Zickus 	__raw_get_cpu_var(softlockup_touch_sync) = true;
15158687acbSDon Zickus 	__raw_get_cpu_var(watchdog_touch_ts) = 0;
15258687acbSDon Zickus }
15358687acbSDon Zickus 
15458687acbSDon Zickus #ifdef CONFIG_PERF_EVENTS_NMI
15558687acbSDon Zickus /* watchdog detector functions */
15658687acbSDon Zickus static int is_hardlockup(int cpu)
15758687acbSDon Zickus {
15858687acbSDon Zickus 	unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
15958687acbSDon Zickus 
16058687acbSDon Zickus 	if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
16158687acbSDon Zickus 		return 1;
16258687acbSDon Zickus 
16358687acbSDon Zickus 	per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
16458687acbSDon Zickus 	return 0;
16558687acbSDon Zickus }
16658687acbSDon Zickus #endif
16758687acbSDon Zickus 
16858687acbSDon Zickus static int is_softlockup(unsigned long touch_ts, int cpu)
16958687acbSDon Zickus {
17058687acbSDon Zickus 	unsigned long now = get_timestamp(cpu);
17158687acbSDon Zickus 
17258687acbSDon Zickus 	/* Warn about unreasonable delays: */
17358687acbSDon Zickus 	if (time_after(now, touch_ts + softlockup_thresh))
17458687acbSDon Zickus 		return now - touch_ts;
17558687acbSDon Zickus 
17658687acbSDon Zickus 	return 0;
17758687acbSDon Zickus }
17858687acbSDon Zickus 
17958687acbSDon Zickus static int
18058687acbSDon Zickus watchdog_panic(struct notifier_block *this, unsigned long event, void *ptr)
18158687acbSDon Zickus {
18258687acbSDon Zickus 	did_panic = 1;
18358687acbSDon Zickus 
18458687acbSDon Zickus 	return NOTIFY_DONE;
18558687acbSDon Zickus }
18658687acbSDon Zickus 
18758687acbSDon Zickus static struct notifier_block panic_block = {
18858687acbSDon Zickus 	.notifier_call = watchdog_panic,
18958687acbSDon Zickus };
19058687acbSDon Zickus 
19158687acbSDon Zickus #ifdef CONFIG_PERF_EVENTS_NMI
19258687acbSDon Zickus static struct perf_event_attr wd_hw_attr = {
19358687acbSDon Zickus 	.type		= PERF_TYPE_HARDWARE,
19458687acbSDon Zickus 	.config		= PERF_COUNT_HW_CPU_CYCLES,
19558687acbSDon Zickus 	.size		= sizeof(struct perf_event_attr),
19658687acbSDon Zickus 	.pinned		= 1,
19758687acbSDon Zickus 	.disabled	= 1,
19858687acbSDon Zickus };
19958687acbSDon Zickus 
20058687acbSDon Zickus /* Callback function for perf event subsystem */
20158687acbSDon Zickus void watchdog_overflow_callback(struct perf_event *event, int nmi,
20258687acbSDon Zickus 		 struct perf_sample_data *data,
20358687acbSDon Zickus 		 struct pt_regs *regs)
20458687acbSDon Zickus {
20558687acbSDon Zickus 	int this_cpu = smp_processor_id();
20658687acbSDon Zickus 
207d7c54733SDon Zickus 	if (__get_cpu_var(watchdog_nmi_touch) == true) {
208d7c54733SDon Zickus 		__get_cpu_var(watchdog_nmi_touch) = false;
20958687acbSDon Zickus 		return;
21058687acbSDon Zickus 	}
21158687acbSDon Zickus 
21258687acbSDon Zickus 	/* check for a hardlockup
21358687acbSDon Zickus 	 * This is done by making sure our timer interrupt
21458687acbSDon Zickus 	 * is incrementing.  The timer interrupt should have
21558687acbSDon Zickus 	 * fired multiple times before we overflow'd.  If it hasn't
21658687acbSDon Zickus 	 * then this is a good indication the cpu is stuck
21758687acbSDon Zickus 	 */
21858687acbSDon Zickus 	if (is_hardlockup(this_cpu)) {
21958687acbSDon Zickus 		/* only print hardlockups once */
22058687acbSDon Zickus 		if (__get_cpu_var(hard_watchdog_warn) == true)
22158687acbSDon Zickus 			return;
22258687acbSDon Zickus 
22358687acbSDon Zickus 		if (hardlockup_panic)
22458687acbSDon Zickus 			panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
22558687acbSDon Zickus 		else
22658687acbSDon Zickus 			WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
22758687acbSDon Zickus 
22858687acbSDon Zickus 		__get_cpu_var(hard_watchdog_warn) = true;
22958687acbSDon Zickus 		return;
23058687acbSDon Zickus 	}
23158687acbSDon Zickus 
23258687acbSDon Zickus 	__get_cpu_var(hard_watchdog_warn) = false;
23358687acbSDon Zickus 	return;
23458687acbSDon Zickus }
23558687acbSDon Zickus static void watchdog_interrupt_count(void)
23658687acbSDon Zickus {
23758687acbSDon Zickus 	__get_cpu_var(hrtimer_interrupts)++;
23858687acbSDon Zickus }
23958687acbSDon Zickus #else
24058687acbSDon Zickus static inline void watchdog_interrupt_count(void) { return; }
24158687acbSDon Zickus #endif /* CONFIG_PERF_EVENTS_NMI */
24258687acbSDon Zickus 
24358687acbSDon Zickus /* watchdog kicker functions */
24458687acbSDon Zickus static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
24558687acbSDon Zickus {
24658687acbSDon Zickus 	int this_cpu = smp_processor_id();
24758687acbSDon Zickus 	unsigned long touch_ts = __get_cpu_var(watchdog_touch_ts);
24858687acbSDon Zickus 	struct pt_regs *regs = get_irq_regs();
24958687acbSDon Zickus 	int duration;
25058687acbSDon Zickus 
25158687acbSDon Zickus 	/* kick the hardlockup detector */
25258687acbSDon Zickus 	watchdog_interrupt_count();
25358687acbSDon Zickus 
25458687acbSDon Zickus 	/* kick the softlockup detector */
25558687acbSDon Zickus 	wake_up_process(__get_cpu_var(softlockup_watchdog));
25658687acbSDon Zickus 
25758687acbSDon Zickus 	/* .. and repeat */
25858687acbSDon Zickus 	hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
25958687acbSDon Zickus 
26058687acbSDon Zickus 	if (touch_ts == 0) {
26158687acbSDon Zickus 		if (unlikely(per_cpu(softlockup_touch_sync, this_cpu))) {
26258687acbSDon Zickus 			/*
26358687acbSDon Zickus 			 * If the time stamp was touched atomically
26458687acbSDon Zickus 			 * make sure the scheduler tick is up to date.
26558687acbSDon Zickus 			 */
26658687acbSDon Zickus 			per_cpu(softlockup_touch_sync, this_cpu) = false;
26758687acbSDon Zickus 			sched_clock_tick();
26858687acbSDon Zickus 		}
26958687acbSDon Zickus 		__touch_watchdog();
27058687acbSDon Zickus 		return HRTIMER_RESTART;
27158687acbSDon Zickus 	}
27258687acbSDon Zickus 
27358687acbSDon Zickus 	/* check for a softlockup
27458687acbSDon Zickus 	 * This is done by making sure a high priority task is
27558687acbSDon Zickus 	 * being scheduled.  The task touches the watchdog to
27658687acbSDon Zickus 	 * indicate it is getting cpu time.  If it hasn't then
27758687acbSDon Zickus 	 * this is a good indication some task is hogging the cpu
27858687acbSDon Zickus 	 */
27958687acbSDon Zickus 	duration = is_softlockup(touch_ts, this_cpu);
28058687acbSDon Zickus 	if (unlikely(duration)) {
28158687acbSDon Zickus 		/* only warn once */
28258687acbSDon Zickus 		if (__get_cpu_var(soft_watchdog_warn) == true)
28358687acbSDon Zickus 			return HRTIMER_RESTART;
28458687acbSDon Zickus 
28558687acbSDon Zickus 		printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
28658687acbSDon Zickus 			this_cpu, duration,
28758687acbSDon Zickus 			current->comm, task_pid_nr(current));
28858687acbSDon Zickus 		print_modules();
28958687acbSDon Zickus 		print_irqtrace_events(current);
29058687acbSDon Zickus 		if (regs)
29158687acbSDon Zickus 			show_regs(regs);
29258687acbSDon Zickus 		else
29358687acbSDon Zickus 			dump_stack();
29458687acbSDon Zickus 
29558687acbSDon Zickus 		if (softlockup_panic)
29658687acbSDon Zickus 			panic("softlockup: hung tasks");
29758687acbSDon Zickus 		__get_cpu_var(soft_watchdog_warn) = true;
29858687acbSDon Zickus 	} else
29958687acbSDon Zickus 		__get_cpu_var(soft_watchdog_warn) = false;
30058687acbSDon Zickus 
30158687acbSDon Zickus 	return HRTIMER_RESTART;
30258687acbSDon Zickus }
30358687acbSDon Zickus 
30458687acbSDon Zickus 
30558687acbSDon Zickus /*
30658687acbSDon Zickus  * The watchdog thread - touches the timestamp.
30758687acbSDon Zickus  */
30858687acbSDon Zickus static int watchdog(void *__bind_cpu)
30958687acbSDon Zickus {
31058687acbSDon Zickus 	struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
31158687acbSDon Zickus 	struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, (unsigned long)__bind_cpu);
31258687acbSDon Zickus 
31358687acbSDon Zickus 	sched_setscheduler(current, SCHED_FIFO, &param);
31458687acbSDon Zickus 
31558687acbSDon Zickus 	/* initialize timestamp */
31658687acbSDon Zickus 	__touch_watchdog();
31758687acbSDon Zickus 
31858687acbSDon Zickus 	/* kick off the timer for the hardlockup detector */
31958687acbSDon Zickus 	/* done here because hrtimer_start can only pin to smp_processor_id() */
32058687acbSDon Zickus 	hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
32158687acbSDon Zickus 		      HRTIMER_MODE_REL_PINNED);
32258687acbSDon Zickus 
32358687acbSDon Zickus 	set_current_state(TASK_INTERRUPTIBLE);
32458687acbSDon Zickus 	/*
32558687acbSDon Zickus 	 * Run briefly once per second to reset the softlockup timestamp.
32658687acbSDon Zickus 	 * If this gets delayed for more than 60 seconds then the
32758687acbSDon Zickus 	 * debug-printout triggers in softlockup_tick().
32858687acbSDon Zickus 	 */
32958687acbSDon Zickus 	while (!kthread_should_stop()) {
33058687acbSDon Zickus 		__touch_watchdog();
33158687acbSDon Zickus 		schedule();
33258687acbSDon Zickus 
33358687acbSDon Zickus 		if (kthread_should_stop())
33458687acbSDon Zickus 			break;
33558687acbSDon Zickus 
33658687acbSDon Zickus 		set_current_state(TASK_INTERRUPTIBLE);
33758687acbSDon Zickus 	}
33858687acbSDon Zickus 	__set_current_state(TASK_RUNNING);
33958687acbSDon Zickus 
34058687acbSDon Zickus 	return 0;
34158687acbSDon Zickus }
34258687acbSDon Zickus 
34358687acbSDon Zickus 
34458687acbSDon Zickus #ifdef CONFIG_PERF_EVENTS_NMI
34558687acbSDon Zickus static int watchdog_nmi_enable(int cpu)
34658687acbSDon Zickus {
34758687acbSDon Zickus 	struct perf_event_attr *wd_attr;
34858687acbSDon Zickus 	struct perf_event *event = per_cpu(watchdog_ev, cpu);
34958687acbSDon Zickus 
35058687acbSDon Zickus 	/* is it already setup and enabled? */
35158687acbSDon Zickus 	if (event && event->state > PERF_EVENT_STATE_OFF)
35258687acbSDon Zickus 		goto out;
35358687acbSDon Zickus 
35458687acbSDon Zickus 	/* it is setup but not enabled */
35558687acbSDon Zickus 	if (event != NULL)
35658687acbSDon Zickus 		goto out_enable;
35758687acbSDon Zickus 
35858687acbSDon Zickus 	/* Try to register using hardware perf events */
35958687acbSDon Zickus 	wd_attr = &wd_hw_attr;
36058687acbSDon Zickus 	wd_attr->sample_period = hw_nmi_get_sample_period();
36158687acbSDon Zickus 	event = perf_event_create_kernel_counter(wd_attr, cpu, -1, watchdog_overflow_callback);
36258687acbSDon Zickus 	if (!IS_ERR(event)) {
36358687acbSDon Zickus 		printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
36458687acbSDon Zickus 		goto out_save;
36558687acbSDon Zickus 	}
36658687acbSDon Zickus 
36758687acbSDon Zickus 	printk(KERN_ERR "NMI watchdog failed to create perf event on cpu%i: %p\n", cpu, event);
36858687acbSDon Zickus 	return -1;
36958687acbSDon Zickus 
37058687acbSDon Zickus 	/* success path */
37158687acbSDon Zickus out_save:
37258687acbSDon Zickus 	per_cpu(watchdog_ev, cpu) = event;
37358687acbSDon Zickus out_enable:
37458687acbSDon Zickus 	perf_event_enable(per_cpu(watchdog_ev, cpu));
37558687acbSDon Zickus out:
37658687acbSDon Zickus 	return 0;
37758687acbSDon Zickus }
37858687acbSDon Zickus 
37958687acbSDon Zickus static void watchdog_nmi_disable(int cpu)
38058687acbSDon Zickus {
38158687acbSDon Zickus 	struct perf_event *event = per_cpu(watchdog_ev, cpu);
38258687acbSDon Zickus 
38358687acbSDon Zickus 	if (event) {
38458687acbSDon Zickus 		perf_event_disable(event);
38558687acbSDon Zickus 		per_cpu(watchdog_ev, cpu) = NULL;
38658687acbSDon Zickus 
38758687acbSDon Zickus 		/* should be in cleanup, but blocks oprofile */
38858687acbSDon Zickus 		perf_event_release_kernel(event);
38958687acbSDon Zickus 	}
39058687acbSDon Zickus 	return;
39158687acbSDon Zickus }
39258687acbSDon Zickus #else
39358687acbSDon Zickus static int watchdog_nmi_enable(int cpu) { return 0; }
39458687acbSDon Zickus static void watchdog_nmi_disable(int cpu) { return; }
39558687acbSDon Zickus #endif /* CONFIG_PERF_EVENTS_NMI */
39658687acbSDon Zickus 
39758687acbSDon Zickus /* prepare/enable/disable routines */
39858687acbSDon Zickus static int watchdog_prepare_cpu(int cpu)
39958687acbSDon Zickus {
40058687acbSDon Zickus 	struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
40158687acbSDon Zickus 
40258687acbSDon Zickus 	WARN_ON(per_cpu(softlockup_watchdog, cpu));
40358687acbSDon Zickus 	hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
40458687acbSDon Zickus 	hrtimer->function = watchdog_timer_fn;
40558687acbSDon Zickus 
40658687acbSDon Zickus 	return 0;
40758687acbSDon Zickus }
40858687acbSDon Zickus 
40958687acbSDon Zickus static int watchdog_enable(int cpu)
41058687acbSDon Zickus {
41158687acbSDon Zickus 	struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
41258687acbSDon Zickus 
41358687acbSDon Zickus 	/* enable the perf event */
41458687acbSDon Zickus 	if (watchdog_nmi_enable(cpu) != 0)
41558687acbSDon Zickus 		return -1;
41658687acbSDon Zickus 
41758687acbSDon Zickus 	/* create the watchdog thread */
41858687acbSDon Zickus 	if (!p) {
41958687acbSDon Zickus 		p = kthread_create(watchdog, (void *)(unsigned long)cpu, "watchdog/%d", cpu);
42058687acbSDon Zickus 		if (IS_ERR(p)) {
42158687acbSDon Zickus 			printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);
42258687acbSDon Zickus 			return -1;
42358687acbSDon Zickus 		}
42458687acbSDon Zickus 		kthread_bind(p, cpu);
42558687acbSDon Zickus 		per_cpu(watchdog_touch_ts, cpu) = 0;
42658687acbSDon Zickus 		per_cpu(softlockup_watchdog, cpu) = p;
42758687acbSDon Zickus 		wake_up_process(p);
42858687acbSDon Zickus 	}
42958687acbSDon Zickus 
43058687acbSDon Zickus 	return 0;
43158687acbSDon Zickus }
43258687acbSDon Zickus 
43358687acbSDon Zickus static void watchdog_disable(int cpu)
43458687acbSDon Zickus {
43558687acbSDon Zickus 	struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
43658687acbSDon Zickus 	struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
43758687acbSDon Zickus 
43858687acbSDon Zickus 	/*
43958687acbSDon Zickus 	 * cancel the timer first to stop incrementing the stats
44058687acbSDon Zickus 	 * and waking up the kthread
44158687acbSDon Zickus 	 */
44258687acbSDon Zickus 	hrtimer_cancel(hrtimer);
44358687acbSDon Zickus 
44458687acbSDon Zickus 	/* disable the perf event */
44558687acbSDon Zickus 	watchdog_nmi_disable(cpu);
44658687acbSDon Zickus 
44758687acbSDon Zickus 	/* stop the watchdog thread */
44858687acbSDon Zickus 	if (p) {
44958687acbSDon Zickus 		per_cpu(softlockup_watchdog, cpu) = NULL;
45058687acbSDon Zickus 		kthread_stop(p);
45158687acbSDon Zickus 	}
45258687acbSDon Zickus 
45358687acbSDon Zickus 	/* if any cpu succeeds, watchdog is considered enabled for the system */
45458687acbSDon Zickus 	watchdog_enabled = 1;
45558687acbSDon Zickus }
45658687acbSDon Zickus 
45758687acbSDon Zickus static void watchdog_enable_all_cpus(void)
45858687acbSDon Zickus {
45958687acbSDon Zickus 	int cpu;
46058687acbSDon Zickus 	int result;
46158687acbSDon Zickus 
46258687acbSDon Zickus 	for_each_online_cpu(cpu)
46358687acbSDon Zickus 		result += watchdog_enable(cpu);
46458687acbSDon Zickus 
46558687acbSDon Zickus 	if (result)
46658687acbSDon Zickus 		printk(KERN_ERR "watchdog: failed to be enabled on some cpus\n");
46758687acbSDon Zickus 
46858687acbSDon Zickus }
46958687acbSDon Zickus 
47058687acbSDon Zickus static void watchdog_disable_all_cpus(void)
47158687acbSDon Zickus {
47258687acbSDon Zickus 	int cpu;
47358687acbSDon Zickus 
47458687acbSDon Zickus 	for_each_online_cpu(cpu)
47558687acbSDon Zickus 		watchdog_disable(cpu);
47658687acbSDon Zickus 
47758687acbSDon Zickus 	/* if all watchdogs are disabled, then they are disabled for the system */
47858687acbSDon Zickus 	watchdog_enabled = 0;
47958687acbSDon Zickus }
48058687acbSDon Zickus 
48158687acbSDon Zickus 
48258687acbSDon Zickus /* sysctl functions */
48358687acbSDon Zickus #ifdef CONFIG_SYSCTL
48458687acbSDon Zickus /*
48558687acbSDon Zickus  * proc handler for /proc/sys/kernel/nmi_watchdog
48658687acbSDon Zickus  */
48758687acbSDon Zickus 
48858687acbSDon Zickus int proc_dowatchdog_enabled(struct ctl_table *table, int write,
48958687acbSDon Zickus 		     void __user *buffer, size_t *length, loff_t *ppos)
49058687acbSDon Zickus {
49158687acbSDon Zickus 	proc_dointvec(table, write, buffer, length, ppos);
49258687acbSDon Zickus 
49358687acbSDon Zickus 	if (watchdog_enabled)
49458687acbSDon Zickus 		watchdog_enable_all_cpus();
49558687acbSDon Zickus 	else
49658687acbSDon Zickus 		watchdog_disable_all_cpus();
49758687acbSDon Zickus 	return 0;
49858687acbSDon Zickus }
49958687acbSDon Zickus 
50058687acbSDon Zickus int proc_dowatchdog_thresh(struct ctl_table *table, int write,
50158687acbSDon Zickus 			     void __user *buffer,
50258687acbSDon Zickus 			     size_t *lenp, loff_t *ppos)
50358687acbSDon Zickus {
50458687acbSDon Zickus 	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
50558687acbSDon Zickus }
50658687acbSDon Zickus #endif /* CONFIG_SYSCTL */
50758687acbSDon Zickus 
50858687acbSDon Zickus 
50958687acbSDon Zickus /*
51058687acbSDon Zickus  * Create/destroy watchdog threads as CPUs come and go:
51158687acbSDon Zickus  */
51258687acbSDon Zickus static int __cpuinit
51358687acbSDon Zickus cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
51458687acbSDon Zickus {
51558687acbSDon Zickus 	int hotcpu = (unsigned long)hcpu;
51658687acbSDon Zickus 
51758687acbSDon Zickus 	switch (action) {
51858687acbSDon Zickus 	case CPU_UP_PREPARE:
51958687acbSDon Zickus 	case CPU_UP_PREPARE_FROZEN:
52058687acbSDon Zickus 		if (watchdog_prepare_cpu(hotcpu))
52158687acbSDon Zickus 			return NOTIFY_BAD;
52258687acbSDon Zickus 		break;
52358687acbSDon Zickus 	case CPU_ONLINE:
52458687acbSDon Zickus 	case CPU_ONLINE_FROZEN:
52558687acbSDon Zickus 		if (watchdog_enable(hotcpu))
52658687acbSDon Zickus 			return NOTIFY_BAD;
52758687acbSDon Zickus 		break;
52858687acbSDon Zickus #ifdef CONFIG_HOTPLUG_CPU
52958687acbSDon Zickus 	case CPU_UP_CANCELED:
53058687acbSDon Zickus 	case CPU_UP_CANCELED_FROZEN:
53158687acbSDon Zickus 		watchdog_disable(hotcpu);
53258687acbSDon Zickus 		break;
53358687acbSDon Zickus 	case CPU_DEAD:
53458687acbSDon Zickus 	case CPU_DEAD_FROZEN:
53558687acbSDon Zickus 		watchdog_disable(hotcpu);
53658687acbSDon Zickus 		break;
53758687acbSDon Zickus #endif /* CONFIG_HOTPLUG_CPU */
53858687acbSDon Zickus 	}
53958687acbSDon Zickus 	return NOTIFY_OK;
54058687acbSDon Zickus }
54158687acbSDon Zickus 
54258687acbSDon Zickus static struct notifier_block __cpuinitdata cpu_nfb = {
54358687acbSDon Zickus 	.notifier_call = cpu_callback
54458687acbSDon Zickus };
54558687acbSDon Zickus 
54658687acbSDon Zickus static int __init spawn_watchdog_task(void)
54758687acbSDon Zickus {
54858687acbSDon Zickus 	void *cpu = (void *)(long)smp_processor_id();
54958687acbSDon Zickus 	int err;
55058687acbSDon Zickus 
55158687acbSDon Zickus 	if (no_watchdog)
55258687acbSDon Zickus 		return 0;
55358687acbSDon Zickus 
55458687acbSDon Zickus 	err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
55558687acbSDon Zickus 	WARN_ON(err == NOTIFY_BAD);
55658687acbSDon Zickus 
55758687acbSDon Zickus 	cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
55858687acbSDon Zickus 	register_cpu_notifier(&cpu_nfb);
55958687acbSDon Zickus 
56058687acbSDon Zickus 	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
56158687acbSDon Zickus 
56258687acbSDon Zickus 	return 0;
56358687acbSDon Zickus }
56458687acbSDon Zickus early_initcall(spawn_watchdog_task);
565