summaryrefslogtreecommitdiff
path: root/kernel/nmi_watchdog.c
blob: 73c1954a97bb8cd40d558f1ff3b1d3b7aaccec8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Detect Hard Lockups using the NMI
 *
 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
 *
 * this code detects hard lockups: incidents in where on a CPU
 * the kernel does not respond to anything except NMI.
 *
 * Note: Most of this code is borrowed heavily from softlockup.c,
 * so thanks to Ingo for the initial implementation.
 * Some chunks also taken from arch/x86/kernel/apic/nmi.c, thanks
 * to those contributors as well.
 */

#include <linux/mm.h>
#include <linux/cpu.h>
#include <linux/nmi.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/freezer.h>
#include <linux/lockdep.h>
#include <linux/notifier.h>
#include <linux/module.h>
#include <linux/sysctl.h>

#include <asm/irq_regs.h>
#include <linux/perf_event.h>

static DEFINE_PER_CPU(struct perf_event *, nmi_watchdog_ev);
static DEFINE_PER_CPU(int, nmi_watchdog_touch);
static DEFINE_PER_CPU(long, alert_counter);

static int panic_on_timeout;

void touch_nmi_watchdog(void)
{
	__raw_get_cpu_var(nmi_watchdog_touch) = 1;
	touch_softlockup_watchdog();
}
EXPORT_SYMBOL(touch_nmi_watchdog);

void touch_all_nmi_watchdog(void)
{
	int cpu;

	for_each_online_cpu(cpu)
		per_cpu(nmi_watchdog_touch, cpu) = 1;
	touch_softlockup_watchdog();
}

static int __init setup_nmi_watchdog(char *str)
{
        if (!strncmp(str, "panic", 5)) {
                panic_on_timeout = 1;
                str = strchr(str, ',');
                if (!str)
                        return 1;
                ++str;
        }
        return 1;
}
__setup("nmi_watchdog=", setup_nmi_watchdog);

#ifdef CONFIG_SYSCTL
/*
 * proc handler for /proc/sys/kernel/nmi_watchdog
 */
int nmi_watchdog_enabled;

int proc_nmi_enabled(struct ctl_table *table, int write,
		     void __user *buffer, size_t *length, loff_t *ppos)
{
	int cpu;

	if (!write) {
		struct perf_event *event;
		for_each_online_cpu(cpu) {
			event = per_cpu(nmi_watchdog_ev, cpu);
			if (event->state > PERF_EVENT_STATE_OFF) {
				nmi_watchdog_enabled = 1;
				break;
			}
		}
		proc_dointvec(table, write, buffer, length, ppos);
		return 0;
	}

	if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL) {
		nmi_watchdog_enabled = 0;
		proc_dointvec(table, write, buffer, length, ppos);
		printk("NMI watchdog failed configuration, can not be enabled\n");
		return 0;
	}

	touch_all_nmi_watchdog();
	proc_dointvec(table, write, buffer, length, ppos);
	if (nmi_watchdog_enabled)
		for_each_online_cpu(cpu)
			perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
	else
		for_each_online_cpu(cpu)
			perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
	return 0;
}

#endif /* CONFIG_SYSCTL */

struct perf_event_attr wd_attr = {
	.type = PERF_TYPE_HARDWARE,
	.config = PERF_COUNT_HW_CPU_CYCLES,
	.size = sizeof(struct perf_event_attr),
	.pinned = 1,
	.disabled = 1,
};

void wd_overflow(struct perf_event *event, int nmi,
		 struct perf_sample_data *data,
		 struct pt_regs *regs)
{
	int cpu = smp_processor_id();
	int touched = 0;

	if (__get_cpu_var(nmi_watchdog_touch)) {
		per_cpu(nmi_watchdog_touch, cpu) = 0;
		touched = 1;
	}

	/* check to see if the cpu is doing anything */
	if (!touched && hw_nmi_is_cpu_stuck(regs)) {
		/*
		 * Ayiee, looks like this CPU is stuck ...
		 * wait a few IRQs (5 seconds) before doing the oops ...
		 */
		per_cpu(alert_counter,cpu) += 1;
		if (per_cpu(alert_counter,cpu) == 5) {
			if (panic_on_timeout) {
				panic("NMI Watchdog detected LOCKUP on cpu %d", cpu);
			} else {
				WARN(1, "NMI Watchdog detected LOCKUP on cpu %d", cpu);
			}
		}
	} else {
		per_cpu(alert_counter,cpu) = 0;
	}

	return;
}

/*
 * Create/destroy watchdog threads as CPUs come and go:
 */
static int __cpuinit
cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
{
	int hotcpu = (unsigned long)hcpu;
	struct perf_event *event;

	switch (action) {
	case CPU_UP_PREPARE:
	case CPU_UP_PREPARE_FROZEN:
		per_cpu(nmi_watchdog_touch, hotcpu) = 0;
		break;
	case CPU_ONLINE:
	case CPU_ONLINE_FROZEN:
		/* originally wanted the below chunk to be in CPU_UP_PREPARE, but caps is unpriv for non-CPU0 */
		wd_attr.sample_period = hw_nmi_get_sample_period();
		event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
		if (IS_ERR(event)) {
			printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event);
			return NOTIFY_BAD;
		}
		per_cpu(nmi_watchdog_ev, hotcpu) = event;
		perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu));
		break;
#ifdef CONFIG_HOTPLUG_CPU
	case CPU_UP_CANCELED:
	case CPU_UP_CANCELED_FROZEN:
		perf_event_disable(per_cpu(nmi_watchdog_ev, hotcpu));
	case CPU_DEAD:
	case CPU_DEAD_FROZEN:
		event = per_cpu(nmi_watchdog_ev, hotcpu);
		per_cpu(nmi_watchdog_ev, hotcpu) = NULL;
		perf_event_release_kernel(event);
		break;
#endif /* CONFIG_HOTPLUG_CPU */
	}
	return NOTIFY_OK;
}

static struct notifier_block __cpuinitdata cpu_nfb = {
	.notifier_call = cpu_callback
};

static int __initdata nonmi_watchdog;

static int __init nonmi_watchdog_setup(char *str)
{
	nonmi_watchdog = 1;
	return 1;
}
__setup("nonmi_watchdog", nonmi_watchdog_setup);

static int __init spawn_nmi_watchdog_task(void)
{
	void *cpu = (void *)(long)smp_processor_id();
	int err;

	if (nonmi_watchdog)
		return 0;

	err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
	if (err == NOTIFY_BAD) {
		BUG();
		return 1;
	}
	cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
	register_cpu_notifier(&cpu_nfb);

	return 0;
}
early_initcall(spawn_nmi_watchdog_task);