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