aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/kernel/apic/hw_nmi.c114
-rw-r--r--kernel/nmi_watchdog.c191
2 files changed, 305 insertions, 0 deletions
diff --git a/arch/x86/kernel/apic/hw_nmi.c b/arch/x86/kernel/apic/hw_nmi.c
new file mode 100644
index 000000000000..8c0e6a410d05
--- /dev/null
+++ b/arch/x86/kernel/apic/hw_nmi.c
@@ -0,0 +1,114 @@
1/*
2 * HW NMI watchdog support
3 *
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5 *
6 * Arch specific calls to support NMI watchdog
7 *
8 * Bits copied from original nmi.c file
9 *
10 */
11
12#include <asm/apic.h>
13#include <linux/smp.h>
14#include <linux/cpumask.h>
15#include <linux/sched.h>
16#include <linux/percpu.h>
17#include <linux/cpumask.h>
18#include <linux/kernel_stat.h>
19#include <asm/mce.h>
20
21#include <linux/nmi.h>
22#include <linux/module.h>
23
24/* For reliability, we're prepared to waste bits here. */
25static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
26
27static DEFINE_PER_CPU(unsigned, last_irq_sum);
28
29/*
30 * Take the local apic timer and PIT/HPET into account. We don't
31 * know which one is active, when we have highres/dyntick on
32 */
33static inline unsigned int get_timer_irqs(int cpu)
34{
35 return per_cpu(irq_stat, cpu).apic_timer_irqs +
36 per_cpu(irq_stat, cpu).irq0_irqs;
37}
38
39static inline int mce_in_progress(void)
40{
41#if defined(CONFIG_X86_MCE)
42 return atomic_read(&mce_entry) > 0;
43#endif
44 return 0;
45}
46
47int hw_nmi_is_cpu_stuck(struct pt_regs *regs)
48{
49 unsigned int sum;
50 int cpu = smp_processor_id();
51
52 /* FIXME: cheap hack for this check, probably should get its own
53 * die_notifier handler
54 */
55 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
56 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
57
58 spin_lock(&lock);
59 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
60 show_regs(regs);
61 dump_stack();
62 spin_unlock(&lock);
63 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
64 }
65
66 /* if we are doing an mce, just assume the cpu is not stuck */
67 /* Could check oops_in_progress here too, but it's safer not to */
68 if (mce_in_progress())
69 return 0;
70
71 /* We determine if the cpu is stuck by checking whether any
72 * interrupts have happened since we last checked. Of course
73 * an nmi storm could create false positives, but the higher
74 * level logic should account for that
75 */
76 sum = get_timer_irqs(cpu);
77 if (__get_cpu_var(last_irq_sum) == sum) {
78 return 1;
79 } else {
80 __get_cpu_var(last_irq_sum) = sum;
81 return 0;
82 }
83}
84
85void arch_trigger_all_cpu_backtrace(void)
86{
87 int i;
88
89 cpumask_copy(to_cpumask(backtrace_mask), cpu_online_mask);
90
91 printk(KERN_INFO "sending NMI to all CPUs:\n");
92 apic->send_IPI_all(NMI_VECTOR);
93
94 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
95 for (i = 0; i < 10 * 1000; i++) {
96 if (cpumask_empty(to_cpumask(backtrace_mask)))
97 break;
98 mdelay(1);
99 }
100}
101
102/* STUB calls to mimic old nmi_watchdog behaviour */
103unsigned int nmi_watchdog = NMI_NONE;
104EXPORT_SYMBOL(nmi_watchdog);
105atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
106EXPORT_SYMBOL(nmi_active);
107int nmi_watchdog_enabled;
108int unknown_nmi_panic;
109void cpu_nmi_set_wd_enabled(void) { return; }
110void acpi_nmi_enable(void) { return; }
111void acpi_nmi_disable(void) { return; }
112void stop_apic_nmi_watchdog(void *unused) { return; }
113void setup_apic_nmi_watchdog(void *unused) { return; }
114int __init check_nmi_watchdog(void) { return 0; }
diff --git a/kernel/nmi_watchdog.c b/kernel/nmi_watchdog.c
new file mode 100644
index 000000000000..36817b214d69
--- /dev/null
+++ b/kernel/nmi_watchdog.c
@@ -0,0 +1,191 @@
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
29static DEFINE_PER_CPU(struct perf_event *, nmi_watchdog_ev);
30static DEFINE_PER_CPU(int, nmi_watchdog_touch);
31static DEFINE_PER_CPU(long, alert_counter);
32
33void touch_nmi_watchdog(void)
34{
35 __raw_get_cpu_var(nmi_watchdog_touch) = 1;
36 touch_softlockup_watchdog();
37}
38EXPORT_SYMBOL(touch_nmi_watchdog);
39
40void touch_all_nmi_watchdog(void)
41{
42 int cpu;
43
44 for_each_online_cpu(cpu)
45 per_cpu(nmi_watchdog_touch, cpu) = 1;
46 touch_softlockup_watchdog();
47}
48
49#ifdef CONFIG_SYSCTL
50/*
51 * proc handler for /proc/sys/kernel/nmi_watchdog
52 */
53int proc_nmi_enabled(struct ctl_table *table, int write,
54 void __user *buffer, size_t *length, loff_t *ppos)
55{
56 int cpu;
57
58 if (per_cpu(nmi_watchdog_ev, smp_processor_id()) == NULL)
59 nmi_watchdog_enabled = 0;
60 else
61 nmi_watchdog_enabled = 1;
62
63 touch_all_nmi_watchdog();
64 proc_dointvec(table, write, buffer, length, ppos);
65 if (nmi_watchdog_enabled)
66 for_each_online_cpu(cpu)
67 perf_event_enable(per_cpu(nmi_watchdog_ev, cpu));
68 else
69 for_each_online_cpu(cpu)
70 perf_event_disable(per_cpu(nmi_watchdog_ev, cpu));
71 return 0;
72}
73
74#endif /* CONFIG_SYSCTL */
75
76struct perf_event_attr wd_attr = {
77 .type = PERF_TYPE_HARDWARE,
78 .config = PERF_COUNT_HW_CPU_CYCLES,
79 .size = sizeof(struct perf_event_attr),
80 .pinned = 1,
81 .disabled = 1,
82};
83
84static int panic_on_timeout;
85
86void wd_overflow(struct perf_event *event, int nmi,
87 struct perf_sample_data *data,
88 struct pt_regs *regs)
89{
90 int cpu = smp_processor_id();
91 int touched = 0;
92
93 if (__get_cpu_var(nmi_watchdog_touch)) {
94 per_cpu(nmi_watchdog_touch, cpu) = 0;
95 touched = 1;
96 }
97
98 /* check to see if the cpu is doing anything */
99 if (!touched && hw_nmi_is_cpu_stuck(regs)) {
100 /*
101 * Ayiee, looks like this CPU is stuck ...
102 * wait a few IRQs (5 seconds) before doing the oops ...
103 */
104 per_cpu(alert_counter,cpu) += 1;
105 if (per_cpu(alert_counter,cpu) == 5) {
106 /*
107 * die_nmi will return ONLY if NOTIFY_STOP happens..
108 */
109 die_nmi("BUG: NMI Watchdog detected LOCKUP",
110 regs, panic_on_timeout);
111 }
112 } else {
113 per_cpu(alert_counter,cpu) = 0;
114 }
115
116 return;
117}
118
119/*
120 * Create/destroy watchdog threads as CPUs come and go:
121 */
122static int __cpuinit
123cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
124{
125 int hotcpu = (unsigned long)hcpu;
126 struct perf_event *event;
127
128 switch (action) {
129 case CPU_UP_PREPARE:
130 case CPU_UP_PREPARE_FROZEN:
131 per_cpu(nmi_watchdog_touch, hotcpu) = 0;
132 break;
133 case CPU_ONLINE:
134 case CPU_ONLINE_FROZEN:
135 /* originally wanted the below chunk to be in CPU_UP_PREPARE, but caps is unpriv for non-CPU0 */
136 wd_attr.sample_period = cpu_khz * 1000;
137 event = perf_event_create_kernel_counter(&wd_attr, hotcpu, -1, wd_overflow);
138 if (IS_ERR(event)) {
139 printk(KERN_ERR "nmi watchdog failed to create perf event on %i: %p\n", hotcpu, event);
140 return NOTIFY_BAD;
141 }
142 per_cpu(nmi_watchdog_ev, hotcpu) = event;
143 perf_event_enable(per_cpu(nmi_watchdog_ev, hotcpu));
144 break;
145#ifdef CONFIG_HOTPLUG_CPU
146 case CPU_UP_CANCELED:
147 case CPU_UP_CANCELED_FROZEN:
148 perf_event_disable(per_cpu(nmi_watchdog_ev, hotcpu));
149 case CPU_DEAD:
150 case CPU_DEAD_FROZEN:
151 event = per_cpu(nmi_watchdog_ev, hotcpu);
152 per_cpu(nmi_watchdog_ev, hotcpu) = NULL;
153 perf_event_release_kernel(event);
154 break;
155#endif /* CONFIG_HOTPLUG_CPU */
156 }
157 return NOTIFY_OK;
158}
159
160static struct notifier_block __cpuinitdata cpu_nfb = {
161 .notifier_call = cpu_callback
162};
163
164static int __initdata nonmi_watchdog;
165
166static int __init nonmi_watchdog_setup(char *str)
167{
168 nonmi_watchdog = 1;
169 return 1;
170}
171__setup("nonmi_watchdog", nonmi_watchdog_setup);
172
173static int __init spawn_nmi_watchdog_task(void)
174{
175 void *cpu = (void *)(long)smp_processor_id();
176 int err;
177
178 if (nonmi_watchdog)
179 return 0;
180
181 err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
182 if (err == NOTIFY_BAD) {
183 BUG();
184 return 1;
185 }
186 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
187 register_cpu_notifier(&cpu_nfb);
188
189 return 0;
190}
191early_initcall(spawn_nmi_watchdog_task);