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