nmi_watchdog: Fallback to software events when no hardware pmu detected
[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 #ifdef CONFIG_SYSCTL
65 /*
66  * proc handler for /proc/sys/kernel/nmi_watchdog
67  */
68 int nmi_watchdog_enabled;
69
70 int proc_nmi_enabled(struct ctl_table *table, int write,
71                      void __user *buffer, size_t *length, loff_t *ppos)
72 {
73         int cpu;
74
75         if (!write) {
76                 struct perf_event *event;
77                 for_each_online_cpu(cpu) {
78                         event = per_cpu(nmi_watchdog_ev, cpu);
79                         if (event->state > PERF_EVENT_STATE_OFF) {
80                                 nmi_watchdog_enabled = 1;
81                                 break;
82                         }
83                 }
84                 proc_dointvec(table, write, buffer, length, ppos);
85                 return 0;
86         }
87
88         if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL) {
89                 nmi_watchdog_enabled = 0;
90                 proc_dointvec(table, write, buffer, length, ppos);
91                 printk("NMI watchdog failed configuration, can not be enabled\n");
92                 return 0;
93         }
94
95         touch_all_nmi_watchdog();
96         proc_dointvec(table, write, buffer, length, ppos);
97         if (nmi_watchdog_enabled)
98                 for_each_online_cpu(cpu)
99                         perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
100         else
101                 for_each_online_cpu(cpu)
102                         perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
103         return 0;
104 }
105
106 #endif /* CONFIG_SYSCTL */
107
108 struct perf_event_attr wd_attr = {
109         .type = PERF_TYPE_HARDWARE,
110         .config = PERF_COUNT_HW_CPU_CYCLES,
111         .size = sizeof(struct perf_event_attr),
112         .pinned = 1,
113         .disabled = 1,
114 };
115
116 void wd_overflow(struct perf_event *event, int nmi,
117                  struct perf_sample_data *data,
118                  struct pt_regs *regs)
119 {
120         int cpu = smp_processor_id();
121         int touched = 0;
122
123         if (__get_cpu_var(nmi_watchdog_touch)) {
124                 per_cpu(nmi_watchdog_touch, cpu) = 0;
125                 touched = 1;
126         }
127
128         /* check to see if the cpu is doing anything */
129         if (!touched && hw_nmi_is_cpu_stuck(regs)) {
130                 /*
131                  * Ayiee, looks like this CPU is stuck ...
132                  * wait a few IRQs (5 seconds) before doing the oops ...
133                  */
134                 per_cpu(alert_counter,cpu) += 1;
135                 if (per_cpu(alert_counter,cpu) == 5) {
136                         if (panic_on_timeout) {
137                                 panic("NMI Watchdog detected LOCKUP on cpu %d", cpu);
138                         } else {
139                                 WARN(1, "NMI Watchdog detected LOCKUP on cpu %d", cpu);
140                         }
141                 }
142         } else {
143                 per_cpu(alert_counter,cpu) = 0;
144         }
145
146         return;
147 }
148
149 /*
150  * Create/destroy watchdog threads as CPUs come and go:
151  */
152 static int __cpuinit
153 cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
154 {
155         int hotcpu = (unsigned long)hcpu;
156         struct perf_event *event;
157
158         switch (action) {
159         case CPU_UP_PREPARE:
160         case CPU_UP_PREPARE_FROZEN:
161                 per_cpu(nmi_watchdog_touch, hotcpu) = 0;
162                 break;
163         case CPU_ONLINE:
164         case CPU_ONLINE_FROZEN:
165                 /* originally wanted the below chunk to be in CPU_UP_PREPARE, but caps is unpriv for non-CPU0 */
166                 wd_attr.sample_period = hw_nmi_get_sample_period();
167                 event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
168                 if (IS_ERR(event)) {
169                         wd_attr.type = PERF_TYPE_SOFTWARE;
170                         event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
171                         if (IS_ERR(event)) {
172                                 printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event);
173                                 return NOTIFY_BAD;
174                         }
175                 }
176                 per_cpu(nmi_watchdog_ev, hotcpu) = event;
177                 perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu));
178                 break;
179 #ifdef CONFIG_HOTPLUG_CPU
180         case CPU_UP_CANCELED:
181         case CPU_UP_CANCELED_FROZEN:
182                 perf_event_disable(per_cpu(nmi_watchdog_ev, hotcpu));
183         case CPU_DEAD:
184         case CPU_DEAD_FROZEN:
185                 event = per_cpu(nmi_watchdog_ev, hotcpu);
186                 per_cpu(nmi_watchdog_ev, hotcpu) = NULL;
187                 perf_event_release_kernel(event);
188                 break;
189 #endif /* CONFIG_HOTPLUG_CPU */
190         }
191         return NOTIFY_OK;
192 }
193
194 static struct notifier_block __cpuinitdata cpu_nfb = {
195         .notifier_call = cpu_callback
196 };
197
198 static int __initdata nonmi_watchdog;
199
200 static int __init nonmi_watchdog_setup(char *str)
201 {
202         nonmi_watchdog = 1;
203         return 1;
204 }
205 __setup("nonmi_watchdog", nonmi_watchdog_setup);
206
207 static int __init spawn_nmi_watchdog_task(void)
208 {
209         void *cpu = (void *)(long)smp_processor_id();
210         int err;
211
212         if (nonmi_watchdog)
213                 return 0;
214
215         err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
216         if (err == NOTIFY_BAD) {
217                 BUG();
218                 return 1;
219         }
220         cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
221         register_cpu_notifier(&cpu_nfb);
222
223         return 0;
224 }
225 early_initcall(spawn_nmi_watchdog_task);