blob: 57b8e2c25eda73a16755efa639cb63b5d593c087 [file] [log] [blame]
Don Zickus58687ac2010-05-07 17:11:44 -04001/*
2 * Detect hard and soft lockups on a system
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/kthread.h>
22#include <linux/lockdep.h>
23#include <linux/notifier.h>
24#include <linux/module.h>
25#include <linux/sysctl.h>
26
27#include <asm/irq_regs.h>
28#include <linux/perf_event.h>
29
30int watchdog_enabled;
31int __read_mostly softlockup_thresh = 60;
32
33static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
Don Zickusd7c54732010-05-07 17:11:51 -040034static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
Don Zickus58687ac2010-05-07 17:11:44 -040035static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
36static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
37static DEFINE_PER_CPU(bool, softlockup_touch_sync);
38static DEFINE_PER_CPU(bool, hard_watchdog_warn);
39static DEFINE_PER_CPU(bool, soft_watchdog_warn);
40#ifdef CONFIG_PERF_EVENTS_NMI
41static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
42static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
43static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
44#endif
45
46static int __read_mostly did_panic;
47static int __initdata no_watchdog;
48
49
50/* boot commands */
51/*
52 * Should we panic when a soft-lockup or hard-lockup occurs:
53 */
54#ifdef CONFIG_PERF_EVENTS_NMI
55static int hardlockup_panic;
56
57static int __init hardlockup_panic_setup(char *str)
58{
59 if (!strncmp(str, "panic", 5))
60 hardlockup_panic = 1;
61 return 1;
62}
63__setup("nmi_watchdog=", hardlockup_panic_setup);
64#endif
65
66unsigned int __read_mostly softlockup_panic =
67 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
68
69static int __init softlockup_panic_setup(char *str)
70{
71 softlockup_panic = simple_strtoul(str, NULL, 0);
72
73 return 1;
74}
75__setup("softlockup_panic=", softlockup_panic_setup);
76
77static int __init nowatchdog_setup(char *str)
78{
79 no_watchdog = 1;
80 return 1;
81}
82__setup("nowatchdog", nowatchdog_setup);
83
84/* deprecated */
85static int __init nosoftlockup_setup(char *str)
86{
87 no_watchdog = 1;
88 return 1;
89}
90__setup("nosoftlockup", nosoftlockup_setup);
91/* */
92
93
94/*
95 * Returns seconds, approximately. We don't need nanosecond
96 * resolution, and we don't need to waste time with a big divide when
97 * 2^30ns == 1.074s.
98 */
99static unsigned long get_timestamp(int this_cpu)
100{
101 return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
102}
103
104static unsigned long get_sample_period(void)
105{
106 /*
107 * convert softlockup_thresh from seconds to ns
108 * the divide by 5 is to give hrtimer 5 chances to
109 * increment before the hardlockup detector generates
110 * a warning
111 */
112 return softlockup_thresh / 5 * NSEC_PER_SEC;
113}
114
115/* Commands for resetting the watchdog */
116static void __touch_watchdog(void)
117{
118 int this_cpu = raw_smp_processor_id();
119
120 __get_cpu_var(watchdog_touch_ts) = get_timestamp(this_cpu);
121}
122
Don Zickus332fbdb2010-05-07 17:11:45 -0400123void touch_softlockup_watchdog(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400124{
125 __get_cpu_var(watchdog_touch_ts) = 0;
126}
Don Zickus58687ac2010-05-07 17:11:44 -0400127
Don Zickus332fbdb2010-05-07 17:11:45 -0400128void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400129{
130 int cpu;
131
132 /*
133 * this is done lockless
134 * do we care if a 0 races with a timestamp?
135 * all it means is the softlock check starts one cycle later
136 */
137 for_each_online_cpu(cpu)
138 per_cpu(watchdog_touch_ts, cpu) = 0;
139}
140
141void touch_nmi_watchdog(void)
142{
Don Zickusd7c54732010-05-07 17:11:51 -0400143 __get_cpu_var(watchdog_nmi_touch) = true;
Don Zickus332fbdb2010-05-07 17:11:45 -0400144 touch_softlockup_watchdog();
Don Zickus58687ac2010-05-07 17:11:44 -0400145}
146EXPORT_SYMBOL(touch_nmi_watchdog);
147
Don Zickus58687ac2010-05-07 17:11:44 -0400148void touch_softlockup_watchdog_sync(void)
149{
150 __raw_get_cpu_var(softlockup_touch_sync) = true;
151 __raw_get_cpu_var(watchdog_touch_ts) = 0;
152}
153
Don Zickus58687ac2010-05-07 17:11:44 -0400154#ifdef CONFIG_PERF_EVENTS_NMI
155/* watchdog detector functions */
156static int is_hardlockup(int cpu)
157{
158 unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
159
160 if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
161 return 1;
162
163 per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
164 return 0;
165}
166#endif
167
168static int is_softlockup(unsigned long touch_ts, int cpu)
169{
170 unsigned long now = get_timestamp(cpu);
171
172 /* Warn about unreasonable delays: */
173 if (time_after(now, touch_ts + softlockup_thresh))
174 return now - touch_ts;
175
176 return 0;
177}
178
179static int
180watchdog_panic(struct notifier_block *this, unsigned long event, void *ptr)
181{
182 did_panic = 1;
183
184 return NOTIFY_DONE;
185}
186
187static struct notifier_block panic_block = {
188 .notifier_call = watchdog_panic,
189};
190
191#ifdef CONFIG_PERF_EVENTS_NMI
192static struct perf_event_attr wd_hw_attr = {
193 .type = PERF_TYPE_HARDWARE,
194 .config = PERF_COUNT_HW_CPU_CYCLES,
195 .size = sizeof(struct perf_event_attr),
196 .pinned = 1,
197 .disabled = 1,
198};
199
200/* Callback function for perf event subsystem */
201void watchdog_overflow_callback(struct perf_event *event, int nmi,
202 struct perf_sample_data *data,
203 struct pt_regs *regs)
204{
205 int this_cpu = smp_processor_id();
Don Zickus58687ac2010-05-07 17:11:44 -0400206
Don Zickusd7c54732010-05-07 17:11:51 -0400207 if (__get_cpu_var(watchdog_nmi_touch) == true) {
208 __get_cpu_var(watchdog_nmi_touch) = false;
Don Zickus58687ac2010-05-07 17:11:44 -0400209 return;
210 }
211
212 /* check for a hardlockup
213 * This is done by making sure our timer interrupt
214 * is incrementing. The timer interrupt should have
215 * fired multiple times before we overflow'd. If it hasn't
216 * then this is a good indication the cpu is stuck
217 */
218 if (is_hardlockup(this_cpu)) {
219 /* only print hardlockups once */
220 if (__get_cpu_var(hard_watchdog_warn) == true)
221 return;
222
223 if (hardlockup_panic)
224 panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
225 else
226 WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
227
228 __get_cpu_var(hard_watchdog_warn) = true;
229 return;
230 }
231
232 __get_cpu_var(hard_watchdog_warn) = false;
233 return;
234}
235static void watchdog_interrupt_count(void)
236{
237 __get_cpu_var(hrtimer_interrupts)++;
238}
239#else
240static inline void watchdog_interrupt_count(void) { return; }
241#endif /* CONFIG_PERF_EVENTS_NMI */
242
243/* watchdog kicker functions */
244static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
245{
246 int this_cpu = smp_processor_id();
247 unsigned long touch_ts = __get_cpu_var(watchdog_touch_ts);
248 struct pt_regs *regs = get_irq_regs();
249 int duration;
250
251 /* kick the hardlockup detector */
252 watchdog_interrupt_count();
253
254 /* kick the softlockup detector */
255 wake_up_process(__get_cpu_var(softlockup_watchdog));
256
257 /* .. and repeat */
258 hrtimer_forward_now(hrtimer, ns_to_ktime(get_sample_period()));
259
260 if (touch_ts == 0) {
261 if (unlikely(per_cpu(softlockup_touch_sync, this_cpu))) {
262 /*
263 * If the time stamp was touched atomically
264 * make sure the scheduler tick is up to date.
265 */
266 per_cpu(softlockup_touch_sync, this_cpu) = false;
267 sched_clock_tick();
268 }
269 __touch_watchdog();
270 return HRTIMER_RESTART;
271 }
272
273 /* check for a softlockup
274 * This is done by making sure a high priority task is
275 * being scheduled. The task touches the watchdog to
276 * indicate it is getting cpu time. If it hasn't then
277 * this is a good indication some task is hogging the cpu
278 */
279 duration = is_softlockup(touch_ts, this_cpu);
280 if (unlikely(duration)) {
281 /* only warn once */
282 if (__get_cpu_var(soft_watchdog_warn) == true)
283 return HRTIMER_RESTART;
284
285 printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
286 this_cpu, duration,
287 current->comm, task_pid_nr(current));
288 print_modules();
289 print_irqtrace_events(current);
290 if (regs)
291 show_regs(regs);
292 else
293 dump_stack();
294
295 if (softlockup_panic)
296 panic("softlockup: hung tasks");
297 __get_cpu_var(soft_watchdog_warn) = true;
298 } else
299 __get_cpu_var(soft_watchdog_warn) = false;
300
301 return HRTIMER_RESTART;
302}
303
304
305/*
306 * The watchdog thread - touches the timestamp.
307 */
308static int watchdog(void *__bind_cpu)
309{
310 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
311 struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, (unsigned long)__bind_cpu);
312
313 sched_setscheduler(current, SCHED_FIFO, &param);
314
315 /* initialize timestamp */
316 __touch_watchdog();
317
318 /* kick off the timer for the hardlockup detector */
319 /* done here because hrtimer_start can only pin to smp_processor_id() */
320 hrtimer_start(hrtimer, ns_to_ktime(get_sample_period()),
321 HRTIMER_MODE_REL_PINNED);
322
323 set_current_state(TASK_INTERRUPTIBLE);
324 /*
325 * Run briefly once per second to reset the softlockup timestamp.
326 * If this gets delayed for more than 60 seconds then the
327 * debug-printout triggers in softlockup_tick().
328 */
329 while (!kthread_should_stop()) {
330 __touch_watchdog();
331 schedule();
332
333 if (kthread_should_stop())
334 break;
335
336 set_current_state(TASK_INTERRUPTIBLE);
337 }
338 __set_current_state(TASK_RUNNING);
339
340 return 0;
341}
342
343
344#ifdef CONFIG_PERF_EVENTS_NMI
345static int watchdog_nmi_enable(int cpu)
346{
347 struct perf_event_attr *wd_attr;
348 struct perf_event *event = per_cpu(watchdog_ev, cpu);
349
350 /* is it already setup and enabled? */
351 if (event && event->state > PERF_EVENT_STATE_OFF)
352 goto out;
353
354 /* it is setup but not enabled */
355 if (event != NULL)
356 goto out_enable;
357
358 /* Try to register using hardware perf events */
359 wd_attr = &wd_hw_attr;
360 wd_attr->sample_period = hw_nmi_get_sample_period();
361 event = perf_event_create_kernel_counter(wd_attr, cpu, -1, watchdog_overflow_callback);
362 if (!IS_ERR(event)) {
363 printk(KERN_INFO "NMI watchdog enabled, takes one hw-pmu counter.\n");
364 goto out_save;
365 }
366
367 printk(KERN_ERR "NMI watchdog failed to create perf event on cpu%i: %p\n", cpu, event);
368 return -1;
369
370 /* success path */
371out_save:
372 per_cpu(watchdog_ev, cpu) = event;
373out_enable:
374 perf_event_enable(per_cpu(watchdog_ev, cpu));
375out:
376 return 0;
377}
378
379static void watchdog_nmi_disable(int cpu)
380{
381 struct perf_event *event = per_cpu(watchdog_ev, cpu);
382
383 if (event) {
384 perf_event_disable(event);
385 per_cpu(watchdog_ev, cpu) = NULL;
386
387 /* should be in cleanup, but blocks oprofile */
388 perf_event_release_kernel(event);
389 }
390 return;
391}
392#else
393static int watchdog_nmi_enable(int cpu) { return 0; }
394static void watchdog_nmi_disable(int cpu) { return; }
395#endif /* CONFIG_PERF_EVENTS_NMI */
396
397/* prepare/enable/disable routines */
398static int watchdog_prepare_cpu(int cpu)
399{
400 struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
401
402 WARN_ON(per_cpu(softlockup_watchdog, cpu));
403 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
404 hrtimer->function = watchdog_timer_fn;
405
406 return 0;
407}
408
409static int watchdog_enable(int cpu)
410{
411 struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
412
413 /* enable the perf event */
414 if (watchdog_nmi_enable(cpu) != 0)
415 return -1;
416
417 /* create the watchdog thread */
418 if (!p) {
419 p = kthread_create(watchdog, (void *)(unsigned long)cpu, "watchdog/%d", cpu);
420 if (IS_ERR(p)) {
421 printk(KERN_ERR "softlockup watchdog for %i failed\n", cpu);
422 return -1;
423 }
424 kthread_bind(p, cpu);
425 per_cpu(watchdog_touch_ts, cpu) = 0;
426 per_cpu(softlockup_watchdog, cpu) = p;
427 wake_up_process(p);
428 }
429
430 return 0;
431}
432
433static void watchdog_disable(int cpu)
434{
435 struct task_struct *p = per_cpu(softlockup_watchdog, cpu);
436 struct hrtimer *hrtimer = &per_cpu(watchdog_hrtimer, cpu);
437
438 /*
439 * cancel the timer first to stop incrementing the stats
440 * and waking up the kthread
441 */
442 hrtimer_cancel(hrtimer);
443
444 /* disable the perf event */
445 watchdog_nmi_disable(cpu);
446
447 /* stop the watchdog thread */
448 if (p) {
449 per_cpu(softlockup_watchdog, cpu) = NULL;
450 kthread_stop(p);
451 }
452
453 /* if any cpu succeeds, watchdog is considered enabled for the system */
454 watchdog_enabled = 1;
455}
456
457static void watchdog_enable_all_cpus(void)
458{
459 int cpu;
460 int result;
461
462 for_each_online_cpu(cpu)
463 result += watchdog_enable(cpu);
464
465 if (result)
466 printk(KERN_ERR "watchdog: failed to be enabled on some cpus\n");
467
468}
469
470static void watchdog_disable_all_cpus(void)
471{
472 int cpu;
473
474 for_each_online_cpu(cpu)
475 watchdog_disable(cpu);
476
477 /* if all watchdogs are disabled, then they are disabled for the system */
478 watchdog_enabled = 0;
479}
480
481
482/* sysctl functions */
483#ifdef CONFIG_SYSCTL
484/*
485 * proc handler for /proc/sys/kernel/nmi_watchdog
486 */
487
488int proc_dowatchdog_enabled(struct ctl_table *table, int write,
489 void __user *buffer, size_t *length, loff_t *ppos)
490{
491 proc_dointvec(table, write, buffer, length, ppos);
492
493 if (watchdog_enabled)
494 watchdog_enable_all_cpus();
495 else
496 watchdog_disable_all_cpus();
497 return 0;
498}
499
500int proc_dowatchdog_thresh(struct ctl_table *table, int write,
501 void __user *buffer,
502 size_t *lenp, loff_t *ppos)
503{
504 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
505}
Don Zickus58687ac2010-05-07 17:11:44 -0400506#endif /* CONFIG_SYSCTL */
507
508
509/*
510 * Create/destroy watchdog threads as CPUs come and go:
511 */
512static int __cpuinit
513cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
514{
515 int hotcpu = (unsigned long)hcpu;
516
517 switch (action) {
518 case CPU_UP_PREPARE:
519 case CPU_UP_PREPARE_FROZEN:
520 if (watchdog_prepare_cpu(hotcpu))
521 return NOTIFY_BAD;
522 break;
523 case CPU_ONLINE:
524 case CPU_ONLINE_FROZEN:
525 if (watchdog_enable(hotcpu))
526 return NOTIFY_BAD;
527 break;
528#ifdef CONFIG_HOTPLUG_CPU
529 case CPU_UP_CANCELED:
530 case CPU_UP_CANCELED_FROZEN:
531 watchdog_disable(hotcpu);
532 break;
533 case CPU_DEAD:
534 case CPU_DEAD_FROZEN:
535 watchdog_disable(hotcpu);
536 break;
537#endif /* CONFIG_HOTPLUG_CPU */
538 }
539 return NOTIFY_OK;
540}
541
542static struct notifier_block __cpuinitdata cpu_nfb = {
543 .notifier_call = cpu_callback
544};
545
546static int __init spawn_watchdog_task(void)
547{
548 void *cpu = (void *)(long)smp_processor_id();
549 int err;
550
551 if (no_watchdog)
552 return 0;
553
554 err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
555 WARN_ON(err == NOTIFY_BAD);
556
557 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
558 register_cpu_notifier(&cpu_nfb);
559
560 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
561
562 return 0;
563}
564early_initcall(spawn_watchdog_task);