nmi_watchdog: support for oprofile
[profile/ivi/kernel-x86-ivi.git] / kernel / nmi_watchdog.c
1 /*
2  * Detect Hard Lockups using the NMI
3  *
4  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5  *
6  * this code detects hard lockups: incidents in where on a CPU
7  * the kernel does not respond to anything except NMI.
8  *
9  * Note: Most of this code is borrowed heavily from softlockup.c,
10  * so thanks to Ingo for the initial implementation.
11  * Some chunks also taken from arch/x86/kernel/apic/nmi.c, thanks
12  * to those contributors as well.
13  */
14
15 #include <linux/mm.h>
16 #include <linux/cpu.h>
17 #include <linux/nmi.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/freezer.h>
21 #include <linux/lockdep.h>
22 #include <linux/notifier.h>
23 #include <linux/module.h>
24 #include <linux/sysctl.h>
25
26 #include <asm/irq_regs.h>
27 #include <linux/perf_event.h>
28
29 static DEFINE_PER_CPU(struct perf_event *, nmi_watchdog_ev);
30 static DEFINE_PER_CPU(int, nmi_watchdog_touch);
31 static DEFINE_PER_CPU(long, alert_counter);
32
33 static int panic_on_timeout;
34
35 void touch_nmi_watchdog(void)
36 {
37         __raw_get_cpu_var(nmi_watchdog_touch) = 1;
38         touch_softlockup_watchdog();
39 }
40 EXPORT_SYMBOL(touch_nmi_watchdog);
41
42 void touch_all_nmi_watchdog(void)
43 {
44         int cpu;
45
46         for_each_online_cpu(cpu)
47                 per_cpu(nmi_watchdog_touch, cpu) = 1;
48         touch_softlockup_watchdog();
49 }
50
51 static int __init setup_nmi_watchdog(char *str)
52 {
53         if (!strncmp(str, "panic", 5)) {
54                 panic_on_timeout = 1;
55                 str = strchr(str, ',');
56                 if (!str)
57                         return 1;
58                 ++str;
59         }
60         return 1;
61 }
62 __setup("nmi_watchdog=", setup_nmi_watchdog);
63
64 struct perf_event_attr wd_attr = {
65         .type = PERF_TYPE_HARDWARE,
66         .config = PERF_COUNT_HW_CPU_CYCLES,
67         .size = sizeof(struct perf_event_attr),
68         .pinned = 1,
69         .disabled = 1,
70 };
71
72 void wd_overflow(struct perf_event *event, int nmi,
73                  struct perf_sample_data *data,
74                  struct pt_regs *regs)
75 {
76         int cpu = smp_processor_id();
77         int touched = 0;
78
79         if (__get_cpu_var(nmi_watchdog_touch)) {
80                 per_cpu(nmi_watchdog_touch, cpu) = 0;
81                 touched = 1;
82         }
83
84         /* check to see if the cpu is doing anything */
85         if (!touched && hw_nmi_is_cpu_stuck(regs)) {
86                 /*
87                  * Ayiee, looks like this CPU is stuck ...
88                  * wait a few IRQs (5 seconds) before doing the oops ...
89                  */
90                 per_cpu(alert_counter,cpu) += 1;
91                 if (per_cpu(alert_counter,cpu) == 5) {
92                         if (panic_on_timeout) {
93                                 panic("NMI Watchdog detected LOCKUP on cpu %d", cpu);
94                         } else {
95                                 WARN(1, "NMI Watchdog detected LOCKUP on cpu %d", cpu);
96                         }
97                 }
98         } else {
99                 per_cpu(alert_counter,cpu) = 0;
100         }
101
102         return;
103 }
104
105 static int enable_nmi_watchdog(int cpu)
106 {
107         struct perf_event *event;
108
109         event = per_cpu(nmi_watchdog_ev, cpu);
110         if (event && event->state > PERF_EVENT_STATE_OFF)
111                 return 0;
112
113         if (event == NULL) {
114                 /* Try to register using hardware perf events first */
115                 wd_attr.sample_period = hw_nmi_get_sample_period();
116                 event = perf_event_create_kernel_counter(&wd_attr, cpu, -1, wd_overflow);
117                 if (IS_ERR(event)) {
118                         wd_attr.type = PERF_TYPE_SOFTWARE;
119                         event = perf_event_create_kernel_counter(&wd_attr, cpu, -1, wd_overflow);
120                         if (IS_ERR(event)) {
121                                 printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", cpu, event);
122                                 return -1;
123                         }
124                 }
125                 per_cpu(nmi_watchdog_ev, cpu) = event;
126         }
127         perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
128         return 0;
129 }
130
131 static void disable_nmi_watchdog(int cpu)
132 {
133         struct perf_event *event;
134
135         event = per_cpu(nmi_watchdog_ev, cpu);
136         if (event) {
137                 perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
138                 per_cpu(nmi_watchdog_ev, cpu) = NULL;
139                 perf_event_release_kernel(event);
140         }
141 }
142
143 #ifdef CONFIG_SYSCTL
144 /*
145  * proc handler for /proc/sys/kernel/nmi_watchdog
146  */
147 int nmi_watchdog_enabled;
148
149 int proc_nmi_enabled(struct ctl_table *table, int write,
150                      void __user *buffer, size_t *length, loff_t *ppos)
151 {
152         int cpu;
153
154         if (!write) {
155                 struct perf_event *event;
156                 for_each_online_cpu(cpu) {
157                         event = per_cpu(nmi_watchdog_ev, cpu);
158                         if (event && event->state > PERF_EVENT_STATE_OFF) {
159                                 nmi_watchdog_enabled = 1;
160                                 break;
161                         }
162                 }
163                 proc_dointvec(table, write, buffer, length, ppos);
164                 return 0;
165         }
166
167         touch_all_nmi_watchdog();
168         proc_dointvec(table, write, buffer, length, ppos);
169         if (nmi_watchdog_enabled) {
170                 for_each_online_cpu(cpu)
171                         if (enable_nmi_watchdog(cpu)) {
172                                 printk("NMI watchdog failed configuration, "
173                                         " can not be enabled\n");
174                         }
175         } else {
176                 for_each_online_cpu(cpu)
177                         disable_nmi_watchdog(cpu);
178         }
179         return 0;
180 }
181
182 #endif /* CONFIG_SYSCTL */
183
184 /*
185  * Create/destroy watchdog threads as CPUs come and go:
186  */
187 static int __cpuinit
188 cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
189 {
190         int hotcpu = (unsigned long)hcpu;
191
192         switch (action) {
193         case CPU_UP_PREPARE:
194         case CPU_UP_PREPARE_FROZEN:
195                 per_cpu(nmi_watchdog_touch, hotcpu) = 0;
196                 break;
197         case CPU_ONLINE:
198         case CPU_ONLINE_FROZEN:
199                 if (enable_nmi_watchdog(hotcpu))
200                         return NOTIFY_BAD;
201                 break;
202 #ifdef CONFIG_HOTPLUG_CPU
203         case CPU_UP_CANCELED:
204         case CPU_UP_CANCELED_FROZEN:
205                 disable_nmi_watchdog(hotcpu);
206         case CPU_DEAD:
207         case CPU_DEAD_FROZEN:
208                 break;
209 #endif /* CONFIG_HOTPLUG_CPU */
210         }
211         return NOTIFY_OK;
212 }
213
214 static struct notifier_block __cpuinitdata cpu_nfb = {
215         .notifier_call = cpu_callback
216 };
217
218 static int __initdata nonmi_watchdog;
219
220 static int __init nonmi_watchdog_setup(char *str)
221 {
222         nonmi_watchdog = 1;
223         return 1;
224 }
225 __setup("nonmi_watchdog", nonmi_watchdog_setup);
226
227 static int __init spawn_nmi_watchdog_task(void)
228 {
229         void *cpu = (void *)(long)smp_processor_id();
230         int err;
231
232         if (nonmi_watchdog)
233                 return 0;
234
235         err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
236         if (err == NOTIFY_BAD) {
237                 BUG();
238                 return 1;
239         }
240         cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
241         register_cpu_notifier(&cpu_nfb);
242
243         return 0;
244 }
245 early_initcall(spawn_nmi_watchdog_task);