Merge tag 'usb-serial-4.14-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.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 static struct cpumask dead_events_mask;
25
26 static unsigned long hardlockup_allcpu_dumped;
27 static unsigned int watchdog_cpus;
28
29 void arch_touch_nmi_watchdog(void)
30 {
31         /*
32          * Using __raw here because some code paths have
33          * preemption enabled.  If preemption is enabled
34          * then interrupts should be enabled too, in which
35          * case we shouldn't have to worry about the watchdog
36          * going off.
37          */
38         raw_cpu_write(watchdog_nmi_touch, true);
39 }
40 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
41
42 #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
43 static DEFINE_PER_CPU(ktime_t, last_timestamp);
44 static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
45 static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
46
47 void watchdog_update_hrtimer_threshold(u64 period)
48 {
49         /*
50          * The hrtimer runs with a period of (watchdog_threshold * 2) / 5
51          *
52          * So it runs effectively with 2.5 times the rate of the NMI
53          * watchdog. That means the hrtimer should fire 2-3 times before
54          * the NMI watchdog expires. The NMI watchdog on x86 is based on
55          * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles
56          * might run way faster than expected and the NMI fires in a
57          * smaller period than the one deduced from the nominal CPU
58          * frequency. Depending on the Turbo-Mode factor this might be fast
59          * enough to get the NMI period smaller than the hrtimer watchdog
60          * period and trigger false positives.
61          *
62          * The sample threshold is used to check in the NMI handler whether
63          * the minimum time between two NMI samples has elapsed. That
64          * prevents false positives.
65          *
66          * Set this to 4/5 of the actual watchdog threshold period so the
67          * hrtimer is guaranteed to fire at least once within the real
68          * watchdog threshold.
69          */
70         watchdog_hrtimer_sample_threshold = period * 2;
71 }
72
73 static bool watchdog_check_timestamp(void)
74 {
75         ktime_t delta, now = ktime_get_mono_fast_ns();
76
77         delta = now - __this_cpu_read(last_timestamp);
78         if (delta < watchdog_hrtimer_sample_threshold) {
79                 /*
80                  * If ktime is jiffies based, a stalled timer would prevent
81                  * jiffies from being incremented and the filter would look
82                  * at a stale timestamp and never trigger.
83                  */
84                 if (__this_cpu_inc_return(nmi_rearmed) < 10)
85                         return false;
86         }
87         __this_cpu_write(nmi_rearmed, 0);
88         __this_cpu_write(last_timestamp, now);
89         return true;
90 }
91 #else
92 static inline bool watchdog_check_timestamp(void)
93 {
94         return true;
95 }
96 #endif
97
98 static struct perf_event_attr wd_hw_attr = {
99         .type           = PERF_TYPE_HARDWARE,
100         .config         = PERF_COUNT_HW_CPU_CYCLES,
101         .size           = sizeof(struct perf_event_attr),
102         .pinned         = 1,
103         .disabled       = 1,
104 };
105
106 /* Callback function for perf event subsystem */
107 static void watchdog_overflow_callback(struct perf_event *event,
108                                        struct perf_sample_data *data,
109                                        struct pt_regs *regs)
110 {
111         /* Ensure the watchdog never gets throttled */
112         event->hw.interrupts = 0;
113
114         if (__this_cpu_read(watchdog_nmi_touch) == true) {
115                 __this_cpu_write(watchdog_nmi_touch, false);
116                 return;
117         }
118
119         if (!watchdog_check_timestamp())
120                 return;
121
122         /* check for a hardlockup
123          * This is done by making sure our timer interrupt
124          * is incrementing.  The timer interrupt should have
125          * fired multiple times before we overflow'd.  If it hasn't
126          * then this is a good indication the cpu is stuck
127          */
128         if (is_hardlockup()) {
129                 int this_cpu = smp_processor_id();
130
131                 /* only print hardlockups once */
132                 if (__this_cpu_read(hard_watchdog_warn) == true)
133                         return;
134
135                 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
136                 print_modules();
137                 print_irqtrace_events(current);
138                 if (regs)
139                         show_regs(regs);
140                 else
141                         dump_stack();
142
143                 /*
144                  * Perform all-CPU dump only once to avoid multiple hardlockups
145                  * generating interleaving traces
146                  */
147                 if (sysctl_hardlockup_all_cpu_backtrace &&
148                                 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
149                         trigger_allbutself_cpu_backtrace();
150
151                 if (hardlockup_panic)
152                         nmi_panic(regs, "Hard LOCKUP");
153
154                 __this_cpu_write(hard_watchdog_warn, true);
155                 return;
156         }
157
158         __this_cpu_write(hard_watchdog_warn, false);
159         return;
160 }
161
162 static int hardlockup_detector_event_create(void)
163 {
164         unsigned int cpu = smp_processor_id();
165         struct perf_event_attr *wd_attr;
166         struct perf_event *evt;
167
168         wd_attr = &wd_hw_attr;
169         wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
170
171         /* Try to register using hardware perf events */
172         evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
173                                                watchdog_overflow_callback, NULL);
174         if (IS_ERR(evt)) {
175                 pr_info("Perf event create on CPU %d failed with %ld\n", cpu,
176                         PTR_ERR(evt));
177                 return PTR_ERR(evt);
178         }
179         this_cpu_write(watchdog_ev, evt);
180         return 0;
181 }
182
183 /**
184  * hardlockup_detector_perf_enable - Enable the local event
185  */
186 void hardlockup_detector_perf_enable(void)
187 {
188         if (hardlockup_detector_event_create())
189                 return;
190
191         if (!watchdog_cpus++)
192                 pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
193
194         perf_event_enable(this_cpu_read(watchdog_ev));
195 }
196
197 /**
198  * hardlockup_detector_perf_disable - Disable the local event
199  */
200 void hardlockup_detector_perf_disable(void)
201 {
202         struct perf_event *event = this_cpu_read(watchdog_ev);
203
204         if (event) {
205                 perf_event_disable(event);
206                 cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
207                 watchdog_cpus--;
208         }
209 }
210
211 /**
212  * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them
213  *
214  * Called from lockup_detector_cleanup(). Serialized by the caller.
215  */
216 void hardlockup_detector_perf_cleanup(void)
217 {
218         int cpu;
219
220         for_each_cpu(cpu, &dead_events_mask) {
221                 struct perf_event *event = per_cpu(watchdog_ev, cpu);
222
223                 /*
224                  * Required because for_each_cpu() reports  unconditionally
225                  * CPU0 as set on UP kernels. Sigh.
226                  */
227                 if (event)
228                         perf_event_release_kernel(event);
229                 per_cpu(watchdog_ev, cpu) = NULL;
230         }
231         cpumask_clear(&dead_events_mask);
232 }
233
234 /**
235  * hardlockup_detector_perf_stop - Globally stop watchdog events
236  *
237  * Special interface for x86 to handle the perf HT bug.
238  */
239 void __init hardlockup_detector_perf_stop(void)
240 {
241         int cpu;
242
243         lockdep_assert_cpus_held();
244
245         for_each_online_cpu(cpu) {
246                 struct perf_event *event = per_cpu(watchdog_ev, cpu);
247
248                 if (event)
249                         perf_event_disable(event);
250         }
251 }
252
253 /**
254  * hardlockup_detector_perf_restart - Globally restart watchdog events
255  *
256  * Special interface for x86 to handle the perf HT bug.
257  */
258 void __init hardlockup_detector_perf_restart(void)
259 {
260         int cpu;
261
262         lockdep_assert_cpus_held();
263
264         if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
265                 return;
266
267         for_each_online_cpu(cpu) {
268                 struct perf_event *event = per_cpu(watchdog_ev, cpu);
269
270                 if (event)
271                         perf_event_enable(event);
272         }
273 }
274
275 /**
276  * hardlockup_detector_perf_init - Probe whether NMI event is available at all
277  */
278 int __init hardlockup_detector_perf_init(void)
279 {
280         int ret = hardlockup_detector_event_create();
281
282         if (ret) {
283                 pr_info("Perf NMI watchdog permanently disabled\n");
284         } else {
285                 perf_event_release_kernel(this_cpu_read(watchdog_ev));
286                 this_cpu_write(watchdog_ev, NULL);
287         }
288         return ret;
289 }