nmi_watchdog: Clean up various small details
[platform/adaptation/renesas_rcar/renesas_kernel.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_hw_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 struct perf_event_attr wd_sw_attr = {
73         .type           = PERF_TYPE_SOFTWARE,
74         .config         = PERF_COUNT_SW_CPU_CLOCK,
75         .size           = sizeof(struct perf_event_attr),
76         .pinned         = 1,
77         .disabled       = 1,
78 };
79
80 void wd_overflow(struct perf_event *event, int nmi,
81                  struct perf_sample_data *data,
82                  struct pt_regs *regs)
83 {
84         int cpu = smp_processor_id();
85         int touched = 0;
86
87         if (__get_cpu_var(nmi_watchdog_touch)) {
88                 per_cpu(nmi_watchdog_touch, cpu) = 0;
89                 touched = 1;
90         }
91
92         /* check to see if the cpu is doing anything */
93         if (!touched && hw_nmi_is_cpu_stuck(regs)) {
94                 /*
95                  * Ayiee, looks like this CPU is stuck ...
96                  * wait a few IRQs (5 seconds) before doing the oops ...
97                  */
98                 per_cpu(alert_counter, cpu) += 1;
99                 if (per_cpu(alert_counter, cpu) == 5) {
100                         if (panic_on_timeout)
101                                 panic("NMI Watchdog detected LOCKUP on cpu %d", cpu);
102                         else
103                                 WARN(1, "NMI Watchdog detected LOCKUP on cpu %d", cpu);
104                 }
105         } else {
106                 per_cpu(alert_counter, cpu) = 0;
107         }
108
109         return;
110 }
111
112 static int enable_nmi_watchdog(int cpu)
113 {
114         struct perf_event *event;
115         struct perf_event_attr *wd_attr;
116
117         event = per_cpu(nmi_watchdog_ev, cpu);
118         if (event && event->state > PERF_EVENT_STATE_OFF)
119                 return 0;
120
121         if (event == NULL) {
122                 /* Try to register using hardware perf events first */
123                 wd_attr = &wd_hw_attr;
124                 wd_attr->sample_period = hw_nmi_get_sample_period();
125                 event = perf_event_create_kernel_counter(wd_attr, cpu, -1, wd_overflow);
126                 if (IS_ERR(event)) {
127                         /* hardware doesn't exist or not supported, fallback to software events */
128                         printk(KERN_INFO "nmi_watchdog: hardware not available, trying software events\n");
129                         wd_attr = &wd_sw_attr;
130                         wd_attr->sample_period = NSEC_PER_SEC;
131                         event = perf_event_create_kernel_counter(wd_attr, cpu, -1, wd_overflow);
132                         if (IS_ERR(event)) {
133                                 printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", cpu, event);
134                                 return -1;
135                         }
136                 }
137                 per_cpu(nmi_watchdog_ev, cpu) = event;
138         }
139         perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
140         return 0;
141 }
142
143 static void disable_nmi_watchdog(int cpu)
144 {
145         struct perf_event *event;
146
147         event = per_cpu(nmi_watchdog_ev, cpu);
148         if (event) {
149                 perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
150                 per_cpu(nmi_watchdog_ev, cpu) = NULL;
151                 perf_event_release_kernel(event);
152         }
153 }
154
155 #ifdef CONFIG_SYSCTL
156 /*
157  * proc handler for /proc/sys/kernel/nmi_watchdog
158  */
159 int nmi_watchdog_enabled;
160
161 int proc_nmi_enabled(struct ctl_table *table, int write,
162                      void __user *buffer, size_t *length, loff_t *ppos)
163 {
164         int cpu;
165
166         if (!write) {
167                 struct perf_event *event;
168                 for_each_online_cpu(cpu) {
169                         event = per_cpu(nmi_watchdog_ev, cpu);
170                         if (event && event->state > PERF_EVENT_STATE_OFF) {
171                                 nmi_watchdog_enabled = 1;
172                                 break;
173                         }
174                 }
175                 proc_dointvec(table, write, buffer, length, ppos);
176                 return 0;
177         }
178
179         touch_all_nmi_watchdog();
180         proc_dointvec(table, write, buffer, length, ppos);
181         if (nmi_watchdog_enabled) {
182                 for_each_online_cpu(cpu)
183                         if (enable_nmi_watchdog(cpu)) {
184                                 printk(KERN_ERR "NMI watchdog failed configuration, "
185                                         " can not be enabled\n");
186                         }
187         } else {
188                 for_each_online_cpu(cpu)
189                         disable_nmi_watchdog(cpu);
190         }
191         return 0;
192 }
193
194 #endif /* CONFIG_SYSCTL */
195
196 /*
197  * Create/destroy watchdog threads as CPUs come and go:
198  */
199 static int __cpuinit
200 cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
201 {
202         int hotcpu = (unsigned long)hcpu;
203
204         switch (action) {
205         case CPU_UP_PREPARE:
206         case CPU_UP_PREPARE_FROZEN:
207                 per_cpu(nmi_watchdog_touch, hotcpu) = 0;
208                 break;
209         case CPU_ONLINE:
210         case CPU_ONLINE_FROZEN:
211                 if (enable_nmi_watchdog(hotcpu))
212                         return NOTIFY_BAD;
213                 break;
214 #ifdef CONFIG_HOTPLUG_CPU
215         case CPU_UP_CANCELED:
216         case CPU_UP_CANCELED_FROZEN:
217                 disable_nmi_watchdog(hotcpu);
218         case CPU_DEAD:
219         case CPU_DEAD_FROZEN:
220                 break;
221 #endif /* CONFIG_HOTPLUG_CPU */
222         }
223         return NOTIFY_OK;
224 }
225
226 static struct notifier_block __cpuinitdata cpu_nfb = {
227         .notifier_call = cpu_callback
228 };
229
230 static int __initdata nonmi_watchdog;
231
232 static int __init nonmi_watchdog_setup(char *str)
233 {
234         nonmi_watchdog = 1;
235         return 1;
236 }
237 __setup("nonmi_watchdog", nonmi_watchdog_setup);
238
239 static int __init spawn_nmi_watchdog_task(void)
240 {
241         void *cpu = (void *)(long)smp_processor_id();
242         int err;
243
244         if (nonmi_watchdog)
245                 return 0;
246
247         err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
248         if (err == NOTIFY_BAD) {
249                 BUG();
250                 return 1;
251         }
252         cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
253         register_cpu_notifier(&cpu_nfb);
254
255         return 0;
256 }
257 early_initcall(spawn_nmi_watchdog_task);