blob: f7e752e6e9b41cfe583642004a62b6d516753edf [file] [log] [blame]
Babu Moger73ce0512016-12-14 15:06:24 -08001/*
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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010016#include <linux/sched/debug.h>
17
Babu Moger73ce0512016-12-14 15:06:24 -080018#include <asm/irq_regs.h>
19#include <linux/perf_event.h>
20
21static DEFINE_PER_CPU(bool, hard_watchdog_warn);
22static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
23static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
Thomas Gleixner941154b2017-09-12 21:37:04 +020024static DEFINE_PER_CPU(struct perf_event *, dead_event);
25static struct cpumask dead_events_mask;
Babu Moger73ce0512016-12-14 15:06:24 -080026
Babu Moger73ce0512016-12-14 15:06:24 -080027static unsigned long hardlockup_allcpu_dumped;
Thomas Gleixner20d853f2017-09-12 21:37:03 +020028static bool hardlockup_detector_disabled;
Babu Moger73ce0512016-12-14 15:06:24 -080029
Nicholas Pigginf2e0cff2017-07-12 14:35:43 -070030void arch_touch_nmi_watchdog(void)
Babu Moger73ce0512016-12-14 15:06:24 -080031{
32 /*
33 * Using __raw here because some code paths have
34 * preemption enabled. If preemption is enabled
35 * then interrupts should be enabled too, in which
36 * case we shouldn't have to worry about the watchdog
37 * going off.
38 */
39 raw_cpu_write(watchdog_nmi_touch, true);
Babu Moger73ce0512016-12-14 15:06:24 -080040}
Nicholas Pigginf2e0cff2017-07-12 14:35:43 -070041EXPORT_SYMBOL(arch_touch_nmi_watchdog);
Babu Moger73ce0512016-12-14 15:06:24 -080042
Thomas Gleixner7edaeb62017-08-15 09:50:13 +020043#ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
44static DEFINE_PER_CPU(ktime_t, last_timestamp);
45static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
46static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
47
48void watchdog_update_hrtimer_threshold(u64 period)
49{
50 /*
51 * The hrtimer runs with a period of (watchdog_threshold * 2) / 5
52 *
53 * So it runs effectively with 2.5 times the rate of the NMI
54 * watchdog. That means the hrtimer should fire 2-3 times before
55 * the NMI watchdog expires. The NMI watchdog on x86 is based on
56 * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles
57 * might run way faster than expected and the NMI fires in a
58 * smaller period than the one deduced from the nominal CPU
59 * frequency. Depending on the Turbo-Mode factor this might be fast
60 * enough to get the NMI period smaller than the hrtimer watchdog
61 * period and trigger false positives.
62 *
63 * The sample threshold is used to check in the NMI handler whether
64 * the minimum time between two NMI samples has elapsed. That
65 * prevents false positives.
66 *
67 * Set this to 4/5 of the actual watchdog threshold period so the
68 * hrtimer is guaranteed to fire at least once within the real
69 * watchdog threshold.
70 */
71 watchdog_hrtimer_sample_threshold = period * 2;
72}
73
74static bool watchdog_check_timestamp(void)
75{
76 ktime_t delta, now = ktime_get_mono_fast_ns();
77
78 delta = now - __this_cpu_read(last_timestamp);
79 if (delta < watchdog_hrtimer_sample_threshold) {
80 /*
81 * If ktime is jiffies based, a stalled timer would prevent
82 * jiffies from being incremented and the filter would look
83 * at a stale timestamp and never trigger.
84 */
85 if (__this_cpu_inc_return(nmi_rearmed) < 10)
86 return false;
87 }
88 __this_cpu_write(nmi_rearmed, 0);
89 __this_cpu_write(last_timestamp, now);
90 return true;
91}
92#else
93static inline bool watchdog_check_timestamp(void)
94{
95 return true;
96}
97#endif
98
Babu Moger73ce0512016-12-14 15:06:24 -080099static struct perf_event_attr wd_hw_attr = {
100 .type = PERF_TYPE_HARDWARE,
101 .config = PERF_COUNT_HW_CPU_CYCLES,
102 .size = sizeof(struct perf_event_attr),
103 .pinned = 1,
104 .disabled = 1,
105};
106
107/* Callback function for perf event subsystem */
108static void watchdog_overflow_callback(struct perf_event *event,
Thomas Gleixner01f0a022017-09-12 21:37:05 +0200109 struct perf_sample_data *data,
110 struct pt_regs *regs)
Babu Moger73ce0512016-12-14 15:06:24 -0800111{
112 /* Ensure the watchdog never gets throttled */
113 event->hw.interrupts = 0;
114
Babu Moger73ce0512016-12-14 15:06:24 -0800115 if (__this_cpu_read(watchdog_nmi_touch) == true) {
116 __this_cpu_write(watchdog_nmi_touch, false);
117 return;
118 }
119
Thomas Gleixner7edaeb62017-08-15 09:50:13 +0200120 if (!watchdog_check_timestamp())
121 return;
122
Babu Moger73ce0512016-12-14 15:06:24 -0800123 /* check for a hardlockup
124 * This is done by making sure our timer interrupt
125 * is incrementing. The timer interrupt should have
126 * fired multiple times before we overflow'd. If it hasn't
127 * then this is a good indication the cpu is stuck
128 */
129 if (is_hardlockup()) {
130 int this_cpu = smp_processor_id();
131
132 /* only print hardlockups once */
133 if (__this_cpu_read(hard_watchdog_warn) == true)
134 return;
135
136 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
137 print_modules();
138 print_irqtrace_events(current);
139 if (regs)
140 show_regs(regs);
141 else
142 dump_stack();
143
144 /*
145 * Perform all-CPU dump only once to avoid multiple hardlockups
146 * generating interleaving traces
147 */
148 if (sysctl_hardlockup_all_cpu_backtrace &&
149 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
150 trigger_allbutself_cpu_backtrace();
151
152 if (hardlockup_panic)
153 nmi_panic(regs, "Hard LOCKUP");
154
155 __this_cpu_write(hard_watchdog_warn, true);
156 return;
157 }
158
159 __this_cpu_write(hard_watchdog_warn, false);
160 return;
161}
162
163/*
164 * People like the simple clean cpu node info on boot.
165 * Reduce the watchdog noise by only printing messages
166 * that are different from what cpu0 displayed.
167 */
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800168static unsigned long firstcpu_err;
169static atomic_t watchdog_cpus;
Babu Moger73ce0512016-12-14 15:06:24 -0800170
171int watchdog_nmi_enable(unsigned int cpu)
172{
173 struct perf_event_attr *wd_attr;
174 struct perf_event *event = per_cpu(watchdog_ev, cpu);
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800175 int firstcpu = 0;
Babu Moger73ce0512016-12-14 15:06:24 -0800176
177 /* nothing to do if the hard lockup detector is disabled */
178 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
179 goto out;
180
Thomas Gleixner20d853f2017-09-12 21:37:03 +0200181 /* A failure disabled the hardlockup detector permanently */
182 if (hardlockup_detector_disabled)
183 return -ENODEV;
184
Babu Moger73ce0512016-12-14 15:06:24 -0800185 /* is it already setup and enabled? */
186 if (event && event->state > PERF_EVENT_STATE_OFF)
187 goto out;
188
189 /* it is setup but not enabled */
190 if (event != NULL)
191 goto out_enable;
192
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800193 if (atomic_inc_return(&watchdog_cpus) == 1)
194 firstcpu = 1;
195
Babu Moger73ce0512016-12-14 15:06:24 -0800196 wd_attr = &wd_hw_attr;
197 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
198
199 /* Try to register using hardware perf events */
200 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
201
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800202 /* save the first cpu's error for future comparision */
203 if (firstcpu && IS_ERR(event))
204 firstcpu_err = PTR_ERR(event);
Babu Moger73ce0512016-12-14 15:06:24 -0800205
206 if (!IS_ERR(event)) {
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800207 /* only print for the first cpu initialized */
208 if (firstcpu || firstcpu_err)
Babu Moger73ce0512016-12-14 15:06:24 -0800209 pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
210 goto out_save;
211 }
212
Babu Moger73ce0512016-12-14 15:06:24 -0800213 /* skip displaying the same error again */
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800214 if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
Babu Moger73ce0512016-12-14 15:06:24 -0800215 return PTR_ERR(event);
216
217 /* vary the KERN level based on the returned errno */
218 if (PTR_ERR(event) == -EOPNOTSUPP)
219 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
220 else if (PTR_ERR(event) == -ENOENT)
221 pr_warn("disabled (cpu%i): hardware events not enabled\n",
222 cpu);
223 else
224 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
225 cpu, PTR_ERR(event));
226
Thomas Gleixner20d853f2017-09-12 21:37:03 +0200227 pr_info("Disabling hard lockup detector permanently\n");
228 hardlockup_detector_disabled = true;
Babu Moger73ce0512016-12-14 15:06:24 -0800229
230 return PTR_ERR(event);
231
232 /* success path */
233out_save:
234 per_cpu(watchdog_ev, cpu) = event;
235out_enable:
236 perf_event_enable(per_cpu(watchdog_ev, cpu));
237out:
238 return 0;
239}
240
Thomas Gleixner178b9f72017-09-12 21:37:18 +0200241static int hardlockup_detector_event_create(void)
242{
243 unsigned int cpu = smp_processor_id();
244 struct perf_event_attr *wd_attr;
245 struct perf_event *evt;
246
247 wd_attr = &wd_hw_attr;
248 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
249
250 /* Try to register using hardware perf events */
251 evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
252 watchdog_overflow_callback, NULL);
253 if (IS_ERR(evt)) {
254 pr_info("Perf event create on CPU %d failed with %ld\n", cpu,
255 PTR_ERR(evt));
256 return PTR_ERR(evt);
257 }
258 this_cpu_write(watchdog_ev, evt);
259 return 0;
260}
261
Thomas Gleixner941154b2017-09-12 21:37:04 +0200262/**
263 * hardlockup_detector_perf_disable - Disable the local event
264 */
265void hardlockup_detector_perf_disable(void)
Babu Moger73ce0512016-12-14 15:06:24 -0800266{
Thomas Gleixner941154b2017-09-12 21:37:04 +0200267 struct perf_event *event = this_cpu_read(watchdog_ev);
Babu Moger73ce0512016-12-14 15:06:24 -0800268
269 if (event) {
270 perf_event_disable(event);
Thomas Gleixner941154b2017-09-12 21:37:04 +0200271 this_cpu_write(watchdog_ev, NULL);
272 this_cpu_write(dead_event, event);
273 cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800274
Babu Moger73ce0512016-12-14 15:06:24 -0800275 /* watchdog_nmi_enable() expects this to be zero initially. */
Prarit Bhargava8dcde9d2017-02-22 15:40:56 -0800276 if (atomic_dec_and_test(&watchdog_cpus))
277 firstcpu_err = 0;
Babu Moger73ce0512016-12-14 15:06:24 -0800278 }
279}
Peter Zijlstrad0b6e0a2017-09-12 21:36:55 +0200280
281/**
Thomas Gleixner941154b2017-09-12 21:37:04 +0200282 * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them
283 *
284 * Called from lockup_detector_cleanup(). Serialized by the caller.
285 */
286void hardlockup_detector_perf_cleanup(void)
287{
288 int cpu;
289
290 for_each_cpu(cpu, &dead_events_mask) {
291 struct perf_event *event = per_cpu(dead_event, cpu);
292
293 per_cpu(dead_event, cpu) = NULL;
294 perf_event_release_kernel(event);
295 }
296 cpumask_clear(&dead_events_mask);
297}
298
299/**
Peter Zijlstrad0b6e0a2017-09-12 21:36:55 +0200300 * hardlockup_detector_perf_stop - Globally stop watchdog events
301 *
302 * Special interface for x86 to handle the perf HT bug.
303 */
304void __init hardlockup_detector_perf_stop(void)
305{
306 int cpu;
307
308 lockdep_assert_cpus_held();
309
310 for_each_online_cpu(cpu) {
311 struct perf_event *event = per_cpu(watchdog_ev, cpu);
312
313 if (event)
314 perf_event_disable(event);
315 }
316}
317
318/**
319 * hardlockup_detector_perf_restart - Globally restart watchdog events
320 *
321 * Special interface for x86 to handle the perf HT bug.
322 */
323void __init hardlockup_detector_perf_restart(void)
324{
325 int cpu;
326
327 lockdep_assert_cpus_held();
328
329 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
330 return;
331
332 for_each_online_cpu(cpu) {
333 struct perf_event *event = per_cpu(watchdog_ev, cpu);
334
335 if (event)
336 perf_event_enable(event);
337 }
338}
Thomas Gleixner178b9f72017-09-12 21:37:18 +0200339
340/**
341 * hardlockup_detector_perf_init - Probe whether NMI event is available at all
342 */
343int __init hardlockup_detector_perf_init(void)
344{
345 int ret = hardlockup_detector_event_create();
346
347 if (ret) {
348 pr_info("Perf NMI watchdog permanetely disabled\n");
349 } else {
350 perf_event_release_kernel(this_cpu_read(watchdog_ev));
351 this_cpu_write(watchdog_ev, NULL);
352 }
353 return ret;
354}