]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - kernel/watchdog_hld.c
kernel/watchdog: introduce arch_touch_nmi_watchdog()
[karo-tx-linux.git] / kernel / watchdog_hld.c
1 /*
2  * Detect hard lockups on a system
3  *
4  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5  *
6  * Note: Most of this code is borrowed heavily from the original softlockup
7  * detector, so thanks to Ingo for the initial implementation.
8  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
9  * to those contributors as well.
10  */
11
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
13
14 #include <linux/nmi.h>
15 #include <linux/module.h>
16 #include <linux/sched/debug.h>
17
18 #include <asm/irq_regs.h>
19 #include <linux/perf_event.h>
20
21 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
22 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
23 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
24
25 /* boot commands */
26 /*
27  * Should we panic when a soft-lockup or hard-lockup occurs:
28  */
29 unsigned int __read_mostly hardlockup_panic =
30                         CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
31 static unsigned long hardlockup_allcpu_dumped;
32 /*
33  * We may not want to enable hard lockup detection by default in all cases,
34  * for example when running the kernel as a guest on a hypervisor. In these
35  * cases this function can be called to disable hard lockup detection. This
36  * function should only be executed once by the boot processor before the
37  * kernel command line parameters are parsed, because otherwise it is not
38  * possible to override this in hardlockup_panic_setup().
39  */
40 void hardlockup_detector_disable(void)
41 {
42         watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
43 }
44
45 static int __init hardlockup_panic_setup(char *str)
46 {
47         if (!strncmp(str, "panic", 5))
48                 hardlockup_panic = 1;
49         else if (!strncmp(str, "nopanic", 7))
50                 hardlockup_panic = 0;
51         else if (!strncmp(str, "0", 1))
52                 watchdog_enabled &= ~NMI_WATCHDOG_ENABLED;
53         else if (!strncmp(str, "1", 1))
54                 watchdog_enabled |= NMI_WATCHDOG_ENABLED;
55         return 1;
56 }
57 __setup("nmi_watchdog=", hardlockup_panic_setup);
58
59 void arch_touch_nmi_watchdog(void)
60 {
61         /*
62          * Using __raw here because some code paths have
63          * preemption enabled.  If preemption is enabled
64          * then interrupts should be enabled too, in which
65          * case we shouldn't have to worry about the watchdog
66          * going off.
67          */
68         raw_cpu_write(watchdog_nmi_touch, true);
69 }
70 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
71
72 static struct perf_event_attr wd_hw_attr = {
73         .type           = PERF_TYPE_HARDWARE,
74         .config         = PERF_COUNT_HW_CPU_CYCLES,
75         .size           = sizeof(struct perf_event_attr),
76         .pinned         = 1,
77         .disabled       = 1,
78 };
79
80 /* Callback function for perf event subsystem */
81 static void watchdog_overflow_callback(struct perf_event *event,
82                  struct perf_sample_data *data,
83                  struct pt_regs *regs)
84 {
85         /* Ensure the watchdog never gets throttled */
86         event->hw.interrupts = 0;
87
88         if (atomic_read(&watchdog_park_in_progress) != 0)
89                 return;
90
91         if (__this_cpu_read(watchdog_nmi_touch) == true) {
92                 __this_cpu_write(watchdog_nmi_touch, false);
93                 return;
94         }
95
96         /* check for a hardlockup
97          * This is done by making sure our timer interrupt
98          * is incrementing.  The timer interrupt should have
99          * fired multiple times before we overflow'd.  If it hasn't
100          * then this is a good indication the cpu is stuck
101          */
102         if (is_hardlockup()) {
103                 int this_cpu = smp_processor_id();
104
105                 /* only print hardlockups once */
106                 if (__this_cpu_read(hard_watchdog_warn) == true)
107                         return;
108
109                 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
110                 print_modules();
111                 print_irqtrace_events(current);
112                 if (regs)
113                         show_regs(regs);
114                 else
115                         dump_stack();
116
117                 /*
118                  * Perform all-CPU dump only once to avoid multiple hardlockups
119                  * generating interleaving traces
120                  */
121                 if (sysctl_hardlockup_all_cpu_backtrace &&
122                                 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
123                         trigger_allbutself_cpu_backtrace();
124
125                 if (hardlockup_panic)
126                         nmi_panic(regs, "Hard LOCKUP");
127
128                 __this_cpu_write(hard_watchdog_warn, true);
129                 return;
130         }
131
132         __this_cpu_write(hard_watchdog_warn, false);
133         return;
134 }
135
136 /*
137  * People like the simple clean cpu node info on boot.
138  * Reduce the watchdog noise by only printing messages
139  * that are different from what cpu0 displayed.
140  */
141 static unsigned long firstcpu_err;
142 static atomic_t watchdog_cpus;
143
144 int watchdog_nmi_enable(unsigned int cpu)
145 {
146         struct perf_event_attr *wd_attr;
147         struct perf_event *event = per_cpu(watchdog_ev, cpu);
148         int firstcpu = 0;
149
150         /* nothing to do if the hard lockup detector is disabled */
151         if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
152                 goto out;
153
154         /* is it already setup and enabled? */
155         if (event && event->state > PERF_EVENT_STATE_OFF)
156                 goto out;
157
158         /* it is setup but not enabled */
159         if (event != NULL)
160                 goto out_enable;
161
162         if (atomic_inc_return(&watchdog_cpus) == 1)
163                 firstcpu = 1;
164
165         wd_attr = &wd_hw_attr;
166         wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
167
168         /* Try to register using hardware perf events */
169         event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
170
171         /* save the first cpu's error for future comparision */
172         if (firstcpu && IS_ERR(event))
173                 firstcpu_err = PTR_ERR(event);
174
175         if (!IS_ERR(event)) {
176                 /* only print for the first cpu initialized */
177                 if (firstcpu || firstcpu_err)
178                         pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
179                 goto out_save;
180         }
181
182         /*
183          * Disable the hard lockup detector if _any_ CPU fails to set up
184          * set up the hardware perf event. The watchdog() function checks
185          * the NMI_WATCHDOG_ENABLED bit periodically.
186          *
187          * The barriers are for syncing up watchdog_enabled across all the
188          * cpus, as clear_bit() does not use barriers.
189          */
190         smp_mb__before_atomic();
191         clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
192         smp_mb__after_atomic();
193
194         /* skip displaying the same error again */
195         if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
196                 return PTR_ERR(event);
197
198         /* vary the KERN level based on the returned errno */
199         if (PTR_ERR(event) == -EOPNOTSUPP)
200                 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
201         else if (PTR_ERR(event) == -ENOENT)
202                 pr_warn("disabled (cpu%i): hardware events not enabled\n",
203                          cpu);
204         else
205                 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
206                         cpu, PTR_ERR(event));
207
208         pr_info("Shutting down hard lockup detector on all cpus\n");
209
210         return PTR_ERR(event);
211
212         /* success path */
213 out_save:
214         per_cpu(watchdog_ev, cpu) = event;
215 out_enable:
216         perf_event_enable(per_cpu(watchdog_ev, cpu));
217 out:
218         return 0;
219 }
220
221 void watchdog_nmi_disable(unsigned int cpu)
222 {
223         struct perf_event *event = per_cpu(watchdog_ev, cpu);
224
225         if (event) {
226                 perf_event_disable(event);
227                 per_cpu(watchdog_ev, cpu) = NULL;
228
229                 /* should be in cleanup, but blocks oprofile */
230                 perf_event_release_kernel(event);
231
232                 /* watchdog_nmi_enable() expects this to be zero initially. */
233                 if (atomic_dec_and_test(&watchdog_cpus))
234                         firstcpu_err = 0;
235         }
236 }