1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) "NMI watchdog: " fmt
14
15#include <linux/nmi.h>
16#include <linux/atomic.h>
17#include <linux/module.h>
18#include <linux/sched/debug.h>
19
20#include <asm/irq_regs.h>
21#include <linux/perf_event.h>
22
23static DEFINE_PER_CPU(bool, hard_watchdog_warn);
24static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
25static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
26static DEFINE_PER_CPU(struct perf_event *, dead_event);
27static struct cpumask dead_events_mask;
28
29static unsigned long hardlockup_allcpu_dumped;
30static atomic_t watchdog_cpus = ATOMIC_INIT(0);
31
32notrace void arch_touch_nmi_watchdog(void)
33{
34
35
36
37
38
39
40
41 raw_cpu_write(watchdog_nmi_touch, true);
42}
43EXPORT_SYMBOL(arch_touch_nmi_watchdog);
44
45#ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
46static DEFINE_PER_CPU(ktime_t, last_timestamp);
47static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
48static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
49
50void watchdog_update_hrtimer_threshold(u64 period)
51{
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 watchdog_hrtimer_sample_threshold = period * 2;
74}
75
76static bool watchdog_check_timestamp(void)
77{
78 ktime_t delta, now = ktime_get_mono_fast_ns();
79
80 delta = now - __this_cpu_read(last_timestamp);
81 if (delta < watchdog_hrtimer_sample_threshold) {
82
83
84
85
86
87 if (__this_cpu_inc_return(nmi_rearmed) < 10)
88 return false;
89 }
90 __this_cpu_write(nmi_rearmed, 0);
91 __this_cpu_write(last_timestamp, now);
92 return true;
93}
94#else
95static inline bool watchdog_check_timestamp(void)
96{
97 return true;
98}
99#endif
100
101static struct perf_event_attr wd_hw_attr = {
102 .type = PERF_TYPE_HARDWARE,
103 .config = PERF_COUNT_HW_CPU_CYCLES,
104 .size = sizeof(struct perf_event_attr),
105 .pinned = 1,
106 .disabled = 1,
107};
108
109
110static void watchdog_overflow_callback(struct perf_event *event,
111 struct perf_sample_data *data,
112 struct pt_regs *regs)
113{
114
115 event->hw.interrupts = 0;
116
117 if (__this_cpu_read(watchdog_nmi_touch) == true) {
118 __this_cpu_write(watchdog_nmi_touch, false);
119 return;
120 }
121
122 if (!watchdog_check_timestamp())
123 return;
124
125
126
127
128
129
130
131 if (is_hardlockup()) {
132 int this_cpu = smp_processor_id();
133
134
135 if (__this_cpu_read(hard_watchdog_warn) == true)
136 return;
137
138 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
139 print_modules();
140 print_irqtrace_events(current);
141 if (regs)
142 show_regs(regs);
143 else
144 dump_stack();
145
146
147
148
149
150 if (sysctl_hardlockup_all_cpu_backtrace &&
151 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
152 trigger_allbutself_cpu_backtrace();
153
154 if (hardlockup_panic)
155 nmi_panic(regs, "Hard LOCKUP");
156
157 __this_cpu_write(hard_watchdog_warn, true);
158 return;
159 }
160
161 __this_cpu_write(hard_watchdog_warn, false);
162 return;
163}
164
165static int hardlockup_detector_event_create(void)
166{
167 unsigned int cpu = smp_processor_id();
168 struct perf_event_attr *wd_attr;
169 struct perf_event *evt;
170
171 wd_attr = &wd_hw_attr;
172 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
173
174
175 evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
176 watchdog_overflow_callback, NULL);
177 if (IS_ERR(evt)) {
178 pr_debug("Perf event create on CPU %d failed with %ld\n", cpu,
179 PTR_ERR(evt));
180 return PTR_ERR(evt);
181 }
182 this_cpu_write(watchdog_ev, evt);
183 return 0;
184}
185
186
187
188
189void hardlockup_detector_perf_enable(void)
190{
191 if (hardlockup_detector_event_create())
192 return;
193
194
195 if (!atomic_fetch_inc(&watchdog_cpus))
196 pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
197
198 perf_event_enable(this_cpu_read(watchdog_ev));
199}
200
201
202
203
204void hardlockup_detector_perf_disable(void)
205{
206 struct perf_event *event = this_cpu_read(watchdog_ev);
207
208 if (event) {
209 perf_event_disable(event);
210 this_cpu_write(watchdog_ev, NULL);
211 this_cpu_write(dead_event, event);
212 cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
213 atomic_dec(&watchdog_cpus);
214 }
215}
216
217
218
219
220
221
222void hardlockup_detector_perf_cleanup(void)
223{
224 int cpu;
225
226 for_each_cpu(cpu, &dead_events_mask) {
227 struct perf_event *event = per_cpu(dead_event, cpu);
228
229
230
231
232
233 if (event)
234 perf_event_release_kernel(event);
235 per_cpu(dead_event, cpu) = NULL;
236 }
237 cpumask_clear(&dead_events_mask);
238}
239
240
241
242
243
244
245void __init hardlockup_detector_perf_stop(void)
246{
247 int cpu;
248
249 lockdep_assert_cpus_held();
250
251 for_each_online_cpu(cpu) {
252 struct perf_event *event = per_cpu(watchdog_ev, cpu);
253
254 if (event)
255 perf_event_disable(event);
256 }
257}
258
259
260
261
262
263
264void __init hardlockup_detector_perf_restart(void)
265{
266 int cpu;
267
268 lockdep_assert_cpus_held();
269
270 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
271 return;
272
273 for_each_online_cpu(cpu) {
274 struct perf_event *event = per_cpu(watchdog_ev, cpu);
275
276 if (event)
277 perf_event_enable(event);
278 }
279}
280
281
282
283
284int __init hardlockup_detector_perf_init(void)
285{
286 int ret = hardlockup_detector_event_create();
287
288 if (ret) {
289 pr_info("Perf NMI watchdog permanently disabled\n");
290 } else {
291 perf_event_release_kernel(this_cpu_read(watchdog_ev));
292 this_cpu_write(watchdog_ev, NULL);
293 }
294 return ret;
295}
296