lockup_detector: Remove old softlockup code
[profile/ivi/kernel-x86-ivi.git] / kernel / nmi_watchdog.c
index 73c1954..a79d211 100644 (file)
@@ -50,17 +50,108 @@ void touch_all_nmi_watchdog(void)
 
 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;
+       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);
 
+struct perf_event_attr wd_hw_attr = {
+       .type           = PERF_TYPE_HARDWARE,
+       .config         = PERF_COUNT_HW_CPU_CYCLES,
+       .size           = sizeof(struct perf_event_attr),
+       .pinned         = 1,
+       .disabled       = 1,
+};
+
+struct perf_event_attr wd_sw_attr = {
+       .type           = PERF_TYPE_SOFTWARE,
+       .config         = PERF_COUNT_SW_CPU_CLOCK,
+       .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;
+}
+
+static int enable_nmi_watchdog(int cpu)
+{
+       struct perf_event *event;
+       struct perf_event_attr *wd_attr;
+
+       event = per_cpu(nmi_watchdog_ev, cpu);
+       if (event && event->state > PERF_EVENT_STATE_OFF)
+               return 0;
+
+       if (event == NULL) {
+               /* Try to register using hardware perf events first */
+               wd_attr = &wd_hw_attr;
+               wd_attr->sample_period = hw_nmi_get_sample_period();
+               event = perf_event_create_kernel_counter(wd_attr, cpu, -1, wd_overflow);
+               if (IS_ERR(event)) {
+                       /* hardware doesn't exist or not supported, fallback to software events */
+                       printk(KERN_INFO "nmi_watchdog: hardware not available, trying software events\n");
+                       wd_attr = &wd_sw_attr;
+                       wd_attr->sample_period = NSEC_PER_SEC;
+                       event = perf_event_create_kernel_counter(wd_attr, cpu, -1, wd_overflow);
+                       if (IS_ERR(event)) {
+                               printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", cpu, event);
+                               return -1;
+                       }
+               }
+               per_cpu(nmi_watchdog_ev, cpu) = event;
+       }
+       perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
+       return 0;
+}
+
+static void disable_nmi_watchdog(int cpu)
+{
+       struct perf_event *event;
+
+       event = per_cpu(nmi_watchdog_ev, cpu);
+       if (event) {
+               perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
+               per_cpu(nmi_watchdog_ev, cpu) = NULL;
+               perf_event_release_kernel(event);
+       }
+}
+
 #ifdef CONFIG_SYSCTL
 /*
  * proc handler for /proc/sys/kernel/nmi_watchdog
@@ -76,7 +167,7 @@ int proc_nmi_enabled(struct ctl_table *table, int write,
                struct perf_event *event;
                for_each_online_cpu(cpu) {
                        event = per_cpu(nmi_watchdog_ev, cpu);
-                       if (event->state > PERF_EVENT_STATE_OFF) {
+                       if (event && event->state > PERF_EVENT_STATE_OFF) {
                                nmi_watchdog_enabled = 1;
                                break;
                        }
@@ -85,67 +176,23 @@ int proc_nmi_enabled(struct ctl_table *table, int write,
                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)
+       if (nmi_watchdog_enabled) {
                for_each_online_cpu(cpu)
-                       perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
-       else
+                       if (enable_nmi_watchdog(cpu)) {
+                               printk(KERN_ERR "NMI watchdog failed configuration, "
+                                       " can not be enabled\n");
+                       }
+       } else {
                for_each_online_cpu(cpu)
-                       perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
+                       disable_nmi_watchdog(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:
  */
@@ -153,7 +200,6 @@ 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:
@@ -162,25 +208,15 @@ cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
                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);
+               if (enable_nmi_watchdog(hotcpu))
                        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));
+               disable_nmi_watchdog(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 */
        }
@@ -208,6 +244,8 @@ static int __init spawn_nmi_watchdog_task(void)
        if (nonmi_watchdog)
                return 0;
 
+       printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
+
        err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
        if (err == NOTIFY_BAD) {
                BUG();