hid: jarvis: Supress kernel debug prints in atvr_raw_event()
[linux-3.10.git] / kernel / watchdog.c
1 /*
2  * Detect hard and soft 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/mm.h>
15 #include <linux/cpu.h>
16 #include <linux/nmi.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/freezer.h>
20 #include <linux/kthread.h>
21 #include <linux/lockdep.h>
22 #include <linux/notifier.h>
23 #include <linux/module.h>
24 #include <linux/sysctl.h>
25 #include <linux/smpboot.h>
26 #include <linux/sched/rt.h>
27
28 #include <asm/irq_regs.h>
29 #include <linux/kvm_para.h>
30 #include <linux/perf_event.h>
31
32 int watchdog_enabled = 1;
33 int __read_mostly watchdog_thresh = 10;
34 static int __read_mostly watchdog_disabled;
35 static u64 __read_mostly sample_period;
36
37 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
38 static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
39 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
40 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
41 static DEFINE_PER_CPU(bool, soft_watchdog_warn);
42 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
43 static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
44 #ifdef CONFIG_HARDLOCKUP_DETECTOR
45 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
46 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
47 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
48 #endif
49 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
50 static cpumask_t __read_mostly watchdog_cpus;
51 #endif
52 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
53 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
54 #endif
55
56 static __read_mostly int soft_lockup_detected;
57 static __read_mostly int hard_lockup_detected;
58
59 /* boot commands */
60 /*
61  * Should we panic when a soft-lockup or hard-lockup occurs:
62  */
63 #ifdef CONFIG_HARDLOCKUP_DETECTOR
64 static int hardlockup_panic =
65                         CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE;
66
67 static int __init hardlockup_panic_setup(char *str)
68 {
69         if (!strncmp(str, "panic", 5))
70                 hardlockup_panic = 1;
71         else if (!strncmp(str, "nopanic", 7))
72                 hardlockup_panic = 0;
73         else if (!strncmp(str, "0", 1))
74                 watchdog_enabled = 0;
75         return 1;
76 }
77 __setup("nmi_watchdog=", hardlockup_panic_setup);
78 #endif
79
80 unsigned int __read_mostly softlockup_panic =
81                         CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
82
83 static int __init softlockup_panic_setup(char *str)
84 {
85         softlockup_panic = simple_strtoul(str, NULL, 0);
86
87         return 1;
88 }
89 __setup("softlockup_panic=", softlockup_panic_setup);
90
91 static int __init nowatchdog_setup(char *str)
92 {
93         watchdog_enabled = 0;
94         return 1;
95 }
96 __setup("nowatchdog", nowatchdog_setup);
97
98 /* deprecated */
99 static int __init nosoftlockup_setup(char *str)
100 {
101         watchdog_enabled = 0;
102         return 1;
103 }
104 __setup("nosoftlockup", nosoftlockup_setup);
105 /*  */
106
107 int watchdog_get_lockup_state(void)
108 {
109         return ((soft_lockup_detected << 8) || hard_lockup_detected);
110 }
111
112 /*
113  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
114  * lockups can have false positives under extreme conditions. So we generally
115  * want a higher threshold for soft lockups than for hard lockups. So we couple
116  * the thresholds with a factor: we make the soft threshold twice the amount of
117  * time the hard threshold is.
118  */
119 static int get_softlockup_thresh(void)
120 {
121         return watchdog_thresh * 2;
122 }
123
124 /*
125  * Returns seconds, approximately.  We don't need nanosecond
126  * resolution, and we don't need to waste time with a big divide when
127  * 2^30ns == 1.074s.
128  */
129 static unsigned long get_timestamp(void)
130 {
131         return local_clock() >> 30LL;  /* 2^30 ~= 10^9 */
132 }
133
134 static void set_sample_period(void)
135 {
136         /*
137          * convert watchdog_thresh from seconds to ns
138          * the divide by 5 is to give hrtimer several chances (two
139          * or three with the current relation between the soft
140          * and hard thresholds) to increment before the
141          * hardlockup detector generates a warning
142          */
143         sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
144 }
145
146 /* Commands for resetting the watchdog */
147 static void __touch_watchdog(void)
148 {
149         __this_cpu_write(watchdog_touch_ts, get_timestamp());
150 }
151
152 void touch_softlockup_watchdog(void)
153 {
154         __this_cpu_write(watchdog_touch_ts, 0);
155 }
156 EXPORT_SYMBOL(touch_softlockup_watchdog);
157
158 void touch_all_softlockup_watchdogs(void)
159 {
160         int cpu;
161
162         /*
163          * this is done lockless
164          * do we care if a 0 races with a timestamp?
165          * all it means is the softlock check starts one cycle later
166          */
167         for_each_online_cpu(cpu)
168                 per_cpu(watchdog_touch_ts, cpu) = 0;
169 }
170
171 #ifdef CONFIG_HARDLOCKUP_DETECTOR
172 void touch_nmi_watchdog(void)
173 {
174         if (watchdog_enabled) {
175                 unsigned cpu;
176
177                 for_each_present_cpu(cpu) {
178                         if (per_cpu(watchdog_nmi_touch, cpu) != true)
179                                 per_cpu(watchdog_nmi_touch, cpu) = true;
180                 }
181         }
182         touch_softlockup_watchdog();
183 }
184 EXPORT_SYMBOL(touch_nmi_watchdog);
185
186 #endif
187
188 void touch_softlockup_watchdog_sync(void)
189 {
190         __raw_get_cpu_var(softlockup_touch_sync) = true;
191         __raw_get_cpu_var(watchdog_touch_ts) = 0;
192 }
193
194 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
195 /* watchdog detector functions */
196 static int is_hardlockup(void)
197 {
198         unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
199
200         if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
201                 return 1;
202
203         __this_cpu_write(hrtimer_interrupts_saved, hrint);
204         return 0;
205 }
206 #endif
207
208 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
209 static unsigned int watchdog_next_cpu(unsigned int cpu)
210 {
211         cpumask_t cpus = watchdog_cpus;
212         unsigned int next_cpu;
213
214         next_cpu = cpumask_next(cpu, &cpus);
215         if (next_cpu >= nr_cpu_ids)
216                 next_cpu = cpumask_first(&cpus);
217
218         if (next_cpu == cpu)
219                 return nr_cpu_ids;
220
221         return next_cpu;
222 }
223
224 static int is_hardlockup_other_cpu(unsigned int cpu)
225 {
226         unsigned long hrint = per_cpu(hrtimer_interrupts, cpu);
227
228         if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
229                 return 1;
230
231         per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
232         return 0;
233 }
234
235 static void watchdog_check_hardlockup_other_cpu(void)
236 {
237         unsigned int next_cpu;
238
239         /*
240          * Test for hardlockups every 3 samples.  The sample period is
241          *  watchdog_thresh * 2 / 5, so 3 samples gets us back to slightly over
242          *  watchdog_thresh (over by 20%).
243          */
244         if (__this_cpu_read(hrtimer_interrupts) % 3 != 0)
245                 return;
246
247         /* check for a hardlockup on the next cpu */
248         next_cpu = watchdog_next_cpu(smp_processor_id());
249         if (next_cpu >= nr_cpu_ids)
250                 return;
251
252         smp_rmb();
253
254         if (per_cpu(watchdog_nmi_touch, next_cpu) == true) {
255                 per_cpu(watchdog_nmi_touch, next_cpu) = false;
256                 return;
257         }
258
259         if (is_hardlockup_other_cpu(next_cpu)) {
260                 /* only warn once */
261                 if (per_cpu(hard_watchdog_warn, next_cpu) == true)
262                         return;
263
264                 hard_lockup_detected = 1;
265
266                 if (hardlockup_panic)
267                         panic("Watchdog detected hard LOCKUP on cpu %u", next_cpu);
268                 else
269                         WARN(1, "Watchdog detected hard LOCKUP on cpu %u", next_cpu);
270
271                 per_cpu(hard_watchdog_warn, next_cpu) = true;
272         } else {
273                 per_cpu(hard_watchdog_warn, next_cpu) = false;
274         }
275 }
276 #else
277 static inline void watchdog_check_hardlockup_other_cpu(void) { return; }
278 #endif
279
280 static int is_softlockup(unsigned long touch_ts)
281 {
282         unsigned long now = get_timestamp();
283
284         /* Warn about unreasonable delays: */
285         if (time_after(now, touch_ts + get_softlockup_thresh()))
286                 return now - touch_ts;
287
288         return 0;
289 }
290
291 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
292
293 static struct perf_event_attr wd_hw_attr = {
294         .type           = PERF_TYPE_HARDWARE,
295         .config         = PERF_COUNT_HW_CPU_CYCLES,
296         .size           = sizeof(struct perf_event_attr),
297         .pinned         = 1,
298         .disabled       = 1,
299 };
300
301 /* Callback function for perf event subsystem */
302 static void watchdog_overflow_callback(struct perf_event *event,
303                  struct perf_sample_data *data,
304                  struct pt_regs *regs)
305 {
306         /* Ensure the watchdog never gets throttled */
307         event->hw.interrupts = 0;
308
309         if (__this_cpu_read(watchdog_nmi_touch) == true) {
310                 __this_cpu_write(watchdog_nmi_touch, false);
311                 return;
312         }
313
314         /* check for a hardlockup
315          * This is done by making sure our timer interrupt
316          * is incrementing.  The timer interrupt should have
317          * fired multiple times before we overflow'd.  If it hasn't
318          * then this is a good indication the cpu is stuck
319          */
320         if (is_hardlockup()) {
321                 int this_cpu = smp_processor_id();
322
323                 hard_lockup_detected = 1;
324
325                 /* only print hardlockups once */
326                 if (__this_cpu_read(hard_watchdog_warn) == true)
327                         return;
328
329                 if (hardlockup_panic)
330                         panic("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
331                 else
332                         WARN(1, "Watchdog detected hard LOCKUP on cpu %d", this_cpu);
333
334                 __this_cpu_write(hard_watchdog_warn, true);
335                 return;
336         }
337
338         __this_cpu_write(hard_watchdog_warn, false);
339         return;
340 }
341 #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
342
343 static void watchdog_interrupt_count(void)
344 {
345         __this_cpu_inc(hrtimer_interrupts);
346 }
347
348 static int watchdog_nmi_enable(unsigned int cpu);
349 static void watchdog_nmi_disable(unsigned int cpu);
350
351 /* watchdog kicker functions */
352 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
353 {
354         unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
355         struct pt_regs *regs = get_irq_regs();
356         int duration;
357
358         /* kick the hardlockup detector */
359         watchdog_interrupt_count();
360
361         /* test for hardlockups on the next cpu */
362         watchdog_check_hardlockup_other_cpu();
363
364         /* kick the softlockup detector */
365         wake_up_process(__this_cpu_read(softlockup_watchdog));
366
367         /* .. and repeat */
368         hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
369
370         if (touch_ts == 0) {
371                 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
372                         /*
373                          * If the time stamp was touched atomically
374                          * make sure the scheduler tick is up to date.
375                          */
376                         __this_cpu_write(softlockup_touch_sync, false);
377                         sched_clock_tick();
378                 }
379
380                 /* Clear the guest paused flag on watchdog reset */
381                 kvm_check_and_clear_guest_paused();
382                 __touch_watchdog();
383                 return HRTIMER_RESTART;
384         }
385
386         /* check for a softlockup
387          * This is done by making sure a high priority task is
388          * being scheduled.  The task touches the watchdog to
389          * indicate it is getting cpu time.  If it hasn't then
390          * this is a good indication some task is hogging the cpu
391          */
392         duration = is_softlockup(touch_ts);
393         if (unlikely(duration)) {
394                 /*
395                  * If a virtual machine is stopped by the host it can look to
396                  * the watchdog like a soft lockup, check to see if the host
397                  * stopped the vm before we issue the warning
398                  */
399                 if (kvm_check_and_clear_guest_paused())
400                         return HRTIMER_RESTART;
401
402                 /* only warn once */
403                 if (__this_cpu_read(soft_watchdog_warn) == true)
404                         return HRTIMER_RESTART;
405
406                 soft_lockup_detected = 1;
407
408                 printk(KERN_EMERG "BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
409                         smp_processor_id(), duration,
410                         current->comm, task_pid_nr(current));
411                 print_modules();
412                 print_irqtrace_events(current);
413                 if (regs)
414                         show_regs(regs);
415                 else
416                         dump_stack();
417
418                 if (softlockup_panic)
419                         panic("softlockup: hung tasks");
420                 __this_cpu_write(soft_watchdog_warn, true);
421         } else
422                 __this_cpu_write(soft_watchdog_warn, false);
423
424         return HRTIMER_RESTART;
425 }
426
427 static void watchdog_set_prio(unsigned int policy, unsigned int prio)
428 {
429         struct sched_param param = { .sched_priority = prio };
430
431         sched_setscheduler(current, policy, &param);
432 }
433
434 static void watchdog_enable(unsigned int cpu)
435 {
436         struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
437
438         /* kick off the timer for the hardlockup detector */
439         hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
440         hrtimer->function = watchdog_timer_fn;
441
442         if (!watchdog_enabled) {
443                 kthread_park(current);
444                 return;
445         }
446
447         /* Enable the perf event */
448         watchdog_nmi_enable(cpu);
449
450         /* done here because hrtimer_start can only pin to smp_processor_id() */
451         hrtimer_start(hrtimer, ns_to_ktime(sample_period),
452                       HRTIMER_MODE_REL_PINNED);
453
454         /* initialize timestamp */
455         watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
456         __touch_watchdog();
457 }
458
459 static void watchdog_disable(unsigned int cpu)
460 {
461         struct hrtimer *hrtimer = &__raw_get_cpu_var(watchdog_hrtimer);
462
463         watchdog_set_prio(SCHED_NORMAL, 0);
464         hrtimer_cancel(hrtimer);
465         /* disable the perf event */
466         watchdog_nmi_disable(cpu);
467 }
468
469 static int watchdog_should_run(unsigned int cpu)
470 {
471         return __this_cpu_read(hrtimer_interrupts) !=
472                 __this_cpu_read(soft_lockup_hrtimer_cnt);
473 }
474
475 /*
476  * The watchdog thread function - touches the timestamp.
477  *
478  * It only runs once every sample_period seconds (4 seconds by
479  * default) to reset the softlockup timestamp. If this gets delayed
480  * for more than 2*watchdog_thresh seconds then the debug-printout
481  * triggers in watchdog_timer_fn().
482  */
483 static void watchdog(unsigned int cpu)
484 {
485         __this_cpu_write(soft_lockup_hrtimer_cnt,
486                          __this_cpu_read(hrtimer_interrupts));
487         __touch_watchdog();
488 }
489
490 #ifdef CONFIG_HARDLOCKUP_DETECTOR_NMI
491 /*
492  * People like the simple clean cpu node info on boot.
493  * Reduce the watchdog noise by only printing messages
494  * that are different from what cpu0 displayed.
495  */
496 static unsigned long cpu0_err;
497
498 static int watchdog_nmi_enable(unsigned int cpu)
499 {
500         struct perf_event_attr *wd_attr;
501         struct perf_event *event = per_cpu(watchdog_ev, cpu);
502
503         /* is it already setup and enabled? */
504         if (event && event->state > PERF_EVENT_STATE_OFF)
505                 goto out;
506
507         /* it is setup but not enabled */
508         if (event != NULL)
509                 goto out_enable;
510
511         wd_attr = &wd_hw_attr;
512         wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
513
514         /* Try to register using hardware perf events */
515         event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
516
517         /* save cpu0 error for future comparision */
518         if (cpu == 0 && IS_ERR(event))
519                 cpu0_err = PTR_ERR(event);
520
521         if (!IS_ERR(event)) {
522                 /* only print for cpu0 or different than cpu0 */
523                 if (cpu == 0 || cpu0_err)
524                         pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
525                 goto out_save;
526         }
527
528         /* skip displaying the same error again */
529         if (cpu > 0 && (PTR_ERR(event) == cpu0_err))
530                 return PTR_ERR(event);
531
532         /* vary the KERN level based on the returned errno */
533         if (PTR_ERR(event) == -EOPNOTSUPP)
534                 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
535         else if (PTR_ERR(event) == -ENOENT)
536                 pr_warning("disabled (cpu%i): hardware events not enabled\n",
537                          cpu);
538         else
539                 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
540                         cpu, PTR_ERR(event));
541         return PTR_ERR(event);
542
543         /* success path */
544 out_save:
545         per_cpu(watchdog_ev, cpu) = event;
546 out_enable:
547         perf_event_enable(per_cpu(watchdog_ev, cpu));
548 out:
549         return 0;
550 }
551
552 static void watchdog_nmi_disable(unsigned int cpu)
553 {
554         struct perf_event *event = per_cpu(watchdog_ev, cpu);
555
556         if (event) {
557                 perf_event_disable(event);
558                 per_cpu(watchdog_ev, cpu) = NULL;
559
560                 /* should be in cleanup, but blocks oprofile */
561                 perf_event_release_kernel(event);
562         }
563         return;
564 }
565 #else
566 #ifdef CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU
567 static int watchdog_nmi_enable(unsigned int cpu)
568 {
569         /*
570          * The new cpu will be marked online before the first hrtimer interrupt
571          * runs on it.  If another cpu tests for a hardlockup on the new cpu
572          * before it has run its first hrtimer, it will get a false positive.
573          * Touch the watchdog on the new cpu to delay the first check for at
574          * least 3 sampling periods to guarantee one hrtimer has run on the new
575          * cpu.
576          */
577         per_cpu(watchdog_nmi_touch, cpu) = true;
578         smp_wmb();
579         cpumask_set_cpu(cpu, &watchdog_cpus);
580         return 0;
581 }
582
583 static void watchdog_nmi_disable(unsigned int cpu)
584 {
585         unsigned int next_cpu = watchdog_next_cpu(cpu);
586
587         /*
588          * Offlining this cpu will cause the cpu before this one to start
589          * checking the one after this one.  If this cpu just finished checking
590          * the next cpu and updating hrtimer_interrupts_saved, and then the
591          * previous cpu checks it within one sample period, it will trigger a
592          * false positive.  Touch the watchdog on the next cpu to prevent it.
593          */
594         if (next_cpu < nr_cpu_ids)
595                 per_cpu(watchdog_nmi_touch, next_cpu) = true;
596         smp_wmb();
597         cpumask_clear_cpu(cpu, &watchdog_cpus);
598 }
599 #else
600 static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
601 static void watchdog_nmi_disable(unsigned int cpu) { return; }
602 #endif /* CONFIG_HARDLOCKUP_DETECTOR_OTHER_CPU */
603 #endif /* CONFIG_HARDLOCKUP_DETECTOR_NMI */
604
605 /* prepare/enable/disable routines */
606 /* sysctl functions */
607 #ifdef CONFIG_SYSCTL
608 static void watchdog_enable_all_cpus(void)
609 {
610         unsigned int cpu;
611
612         if (watchdog_disabled) {
613                 watchdog_disabled = 0;
614                 for_each_online_cpu(cpu)
615                         kthread_unpark(per_cpu(softlockup_watchdog, cpu));
616         }
617 }
618
619 static void watchdog_disable_all_cpus(void)
620 {
621         unsigned int cpu;
622
623         if (!watchdog_disabled) {
624                 watchdog_disabled = 1;
625                 for_each_online_cpu(cpu)
626                         kthread_park(per_cpu(softlockup_watchdog, cpu));
627         }
628 }
629
630 /*
631  * proc handler for /proc/sys/kernel/nmi_watchdog,watchdog_thresh
632  */
633
634 int proc_dowatchdog(struct ctl_table *table, int write,
635                     void __user *buffer, size_t *lenp, loff_t *ppos)
636 {
637         int ret;
638
639         if (watchdog_disabled < 0)
640                 return -ENODEV;
641
642         ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
643         if (ret || !write)
644                 return ret;
645
646         set_sample_period();
647         /*
648          * Watchdog threads shouldn't be enabled if they are
649          * disabled. The 'watchdog_disabled' variable check in
650          * watchdog_*_all_cpus() function takes care of this.
651          */
652         if (watchdog_enabled && watchdog_thresh)
653                 watchdog_enable_all_cpus();
654         else
655                 watchdog_disable_all_cpus();
656
657         return ret;
658 }
659 #endif /* CONFIG_SYSCTL */
660
661 static struct smp_hotplug_thread watchdog_threads = {
662         .store                  = &softlockup_watchdog,
663         .thread_should_run      = watchdog_should_run,
664         .thread_fn              = watchdog,
665         .thread_comm            = "watchdog/%u",
666         .setup                  = watchdog_enable,
667         .park                   = watchdog_disable,
668         .unpark                 = watchdog_enable,
669 };
670
671 void __init lockup_detector_init(void)
672 {
673         set_sample_period();
674         if (smpboot_register_percpu_thread(&watchdog_threads)) {
675                 pr_err("Failed to create watchdog threads, disabled\n");
676                 watchdog_disabled = -ENODEV;
677         }
678 }