]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
58687acb DZ |
2 | /* |
3 | * Detect hard and soft lockups on a system | |
4 | * | |
5 | * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc. | |
6 | * | |
86f5e6a7 FLVC |
7 | * Note: Most of this code is borrowed heavily from the original softlockup |
8 | * detector, so thanks to Ingo for the initial implementation. | |
9 | * Some chunks also taken from the old x86-specific nmi watchdog code, thanks | |
58687acb DZ |
10 | * to those contributors as well. |
11 | */ | |
12 | ||
5f92a7b0 | 13 | #define pr_fmt(fmt) "watchdog: " fmt |
4501980a | 14 | |
58687acb DZ |
15 | #include <linux/mm.h> |
16 | #include <linux/cpu.h> | |
17 | #include <linux/nmi.h> | |
18 | #include <linux/init.h> | |
58687acb DZ |
19 | #include <linux/module.h> |
20 | #include <linux/sysctl.h> | |
fe4ba3c3 | 21 | #include <linux/tick.h> |
e6017571 | 22 | #include <linux/sched/clock.h> |
b17b0153 | 23 | #include <linux/sched/debug.h> |
78634061 | 24 | #include <linux/sched/isolation.h> |
9cf57731 | 25 | #include <linux/stop_machine.h> |
58687acb DZ |
26 | |
27 | #include <asm/irq_regs.h> | |
5d1c0f4a | 28 | #include <linux/kvm_para.h> |
58687acb | 29 | |
946d1977 | 30 | static DEFINE_MUTEX(watchdog_mutex); |
ab992dc3 | 31 | |
05a4a952 | 32 | #if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HAVE_NMI_WATCHDOG) |
09154985 TG |
33 | # define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED | NMI_WATCHDOG_ENABLED) |
34 | # define NMI_WATCHDOG_DEFAULT 1 | |
84d56e66 | 35 | #else |
09154985 TG |
36 | # define WATCHDOG_DEFAULT (SOFT_WATCHDOG_ENABLED) |
37 | # define NMI_WATCHDOG_DEFAULT 0 | |
84d56e66 | 38 | #endif |
05a4a952 | 39 | |
09154985 TG |
40 | unsigned long __read_mostly watchdog_enabled; |
41 | int __read_mostly watchdog_user_enabled = 1; | |
42 | int __read_mostly nmi_watchdog_user_enabled = NMI_WATCHDOG_DEFAULT; | |
43 | int __read_mostly soft_watchdog_user_enabled = 1; | |
7feeb9cd | 44 | int __read_mostly watchdog_thresh = 10; |
a994a314 | 45 | int __read_mostly nmi_watchdog_available; |
7feeb9cd TG |
46 | |
47 | struct cpumask watchdog_allowed_mask __read_mostly; | |
7feeb9cd TG |
48 | |
49 | struct cpumask watchdog_cpumask __read_mostly; | |
50 | unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask); | |
51 | ||
05a4a952 | 52 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
05a4a952 NP |
53 | /* |
54 | * Should we panic when a soft-lockup or hard-lockup occurs: | |
55 | */ | |
56 | unsigned int __read_mostly hardlockup_panic = | |
57 | CONFIG_BOOTPARAM_HARDLOCKUP_PANIC_VALUE; | |
58 | /* | |
59 | * We may not want to enable hard lockup detection by default in all cases, | |
60 | * for example when running the kernel as a guest on a hypervisor. In these | |
61 | * cases this function can be called to disable hard lockup detection. This | |
62 | * function should only be executed once by the boot processor before the | |
63 | * kernel command line parameters are parsed, because otherwise it is not | |
64 | * possible to override this in hardlockup_panic_setup(). | |
65 | */ | |
7a355820 | 66 | void __init hardlockup_detector_disable(void) |
05a4a952 | 67 | { |
09154985 | 68 | nmi_watchdog_user_enabled = 0; |
05a4a952 NP |
69 | } |
70 | ||
71 | static int __init hardlockup_panic_setup(char *str) | |
72 | { | |
73 | if (!strncmp(str, "panic", 5)) | |
74 | hardlockup_panic = 1; | |
75 | else if (!strncmp(str, "nopanic", 7)) | |
76 | hardlockup_panic = 0; | |
77 | else if (!strncmp(str, "0", 1)) | |
09154985 | 78 | nmi_watchdog_user_enabled = 0; |
05a4a952 | 79 | else if (!strncmp(str, "1", 1)) |
09154985 | 80 | nmi_watchdog_user_enabled = 1; |
05a4a952 NP |
81 | return 1; |
82 | } | |
83 | __setup("nmi_watchdog=", hardlockup_panic_setup); | |
84 | ||
368a7e2c TG |
85 | # ifdef CONFIG_SMP |
86 | int __read_mostly sysctl_hardlockup_all_cpu_backtrace; | |
05a4a952 | 87 | |
368a7e2c TG |
88 | static int __init hardlockup_all_cpu_backtrace_setup(char *str) |
89 | { | |
90 | sysctl_hardlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0); | |
91 | return 1; | |
92 | } | |
93 | __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup); | |
94 | # endif /* CONFIG_SMP */ | |
95 | #endif /* CONFIG_HARDLOCKUP_DETECTOR */ | |
05a4a952 | 96 | |
05a4a952 NP |
97 | /* |
98 | * These functions can be overridden if an architecture implements its | |
99 | * own hardlockup detector. | |
a10a842f NP |
100 | * |
101 | * watchdog_nmi_enable/disable can be implemented to start and stop when | |
102 | * softlockup watchdog threads start and stop. The arch must select the | |
103 | * SOFTLOCKUP_DETECTOR Kconfig. | |
05a4a952 NP |
104 | */ |
105 | int __weak watchdog_nmi_enable(unsigned int cpu) | |
106 | { | |
146c9d0e | 107 | hardlockup_detector_perf_enable(); |
05a4a952 NP |
108 | return 0; |
109 | } | |
941154bd | 110 | |
05a4a952 NP |
111 | void __weak watchdog_nmi_disable(unsigned int cpu) |
112 | { | |
941154bd | 113 | hardlockup_detector_perf_disable(); |
05a4a952 NP |
114 | } |
115 | ||
a994a314 TG |
116 | /* Return 0, if a NMI watchdog is available. Error code otherwise */ |
117 | int __weak __init watchdog_nmi_probe(void) | |
118 | { | |
119 | return hardlockup_detector_perf_init(); | |
120 | } | |
121 | ||
6592ad2f | 122 | /** |
6b9dc480 | 123 | * watchdog_nmi_stop - Stop the watchdog for reconfiguration |
6592ad2f | 124 | * |
6b9dc480 TG |
125 | * The reconfiguration steps are: |
126 | * watchdog_nmi_stop(); | |
6592ad2f | 127 | * update_variables(); |
6b9dc480 TG |
128 | * watchdog_nmi_start(); |
129 | */ | |
130 | void __weak watchdog_nmi_stop(void) { } | |
131 | ||
132 | /** | |
133 | * watchdog_nmi_start - Start the watchdog after reconfiguration | |
6592ad2f | 134 | * |
6b9dc480 TG |
135 | * Counterpart to watchdog_nmi_stop(). |
136 | * | |
137 | * The following variables have been updated in update_variables() and | |
138 | * contain the currently valid configuration: | |
7feeb9cd | 139 | * - watchdog_enabled |
a10a842f NP |
140 | * - watchdog_thresh |
141 | * - watchdog_cpumask | |
a10a842f | 142 | */ |
6b9dc480 | 143 | void __weak watchdog_nmi_start(void) { } |
a10a842f | 144 | |
09154985 TG |
145 | /** |
146 | * lockup_detector_update_enable - Update the sysctl enable bit | |
147 | * | |
148 | * Caller needs to make sure that the NMI/perf watchdogs are off, so this | |
149 | * can't race with watchdog_nmi_disable(). | |
150 | */ | |
151 | static void lockup_detector_update_enable(void) | |
152 | { | |
153 | watchdog_enabled = 0; | |
154 | if (!watchdog_user_enabled) | |
155 | return; | |
a994a314 | 156 | if (nmi_watchdog_available && nmi_watchdog_user_enabled) |
09154985 TG |
157 | watchdog_enabled |= NMI_WATCHDOG_ENABLED; |
158 | if (soft_watchdog_user_enabled) | |
159 | watchdog_enabled |= SOFT_WATCHDOG_ENABLED; | |
160 | } | |
161 | ||
05a4a952 NP |
162 | #ifdef CONFIG_SOFTLOCKUP_DETECTOR |
163 | ||
2b9d7f23 TG |
164 | /* Global variables, exported for sysctl */ |
165 | unsigned int __read_mostly softlockup_panic = | |
166 | CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE; | |
2eb2527f | 167 | |
9cf57731 | 168 | static bool softlockup_initialized __read_mostly; |
0f34c400 | 169 | static u64 __read_mostly sample_period; |
58687acb DZ |
170 | |
171 | static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts); | |
58687acb DZ |
172 | static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer); |
173 | static DEFINE_PER_CPU(bool, softlockup_touch_sync); | |
58687acb | 174 | static DEFINE_PER_CPU(bool, soft_watchdog_warn); |
bcd951cf TG |
175 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts); |
176 | static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt); | |
b1a8de1f | 177 | static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved); |
58687acb | 178 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved); |
ed235875 | 179 | static unsigned long soft_lockup_nmi_warn; |
58687acb | 180 | |
58687acb DZ |
181 | static int __init softlockup_panic_setup(char *str) |
182 | { | |
183 | softlockup_panic = simple_strtoul(str, NULL, 0); | |
58687acb DZ |
184 | return 1; |
185 | } | |
186 | __setup("softlockup_panic=", softlockup_panic_setup); | |
187 | ||
188 | static int __init nowatchdog_setup(char *str) | |
189 | { | |
09154985 | 190 | watchdog_user_enabled = 0; |
58687acb DZ |
191 | return 1; |
192 | } | |
193 | __setup("nowatchdog", nowatchdog_setup); | |
194 | ||
58687acb DZ |
195 | static int __init nosoftlockup_setup(char *str) |
196 | { | |
09154985 | 197 | soft_watchdog_user_enabled = 0; |
58687acb DZ |
198 | return 1; |
199 | } | |
200 | __setup("nosoftlockup", nosoftlockup_setup); | |
195daf66 | 201 | |
ed235875 | 202 | #ifdef CONFIG_SMP |
368a7e2c TG |
203 | int __read_mostly sysctl_softlockup_all_cpu_backtrace; |
204 | ||
ed235875 AT |
205 | static int __init softlockup_all_cpu_backtrace_setup(char *str) |
206 | { | |
368a7e2c | 207 | sysctl_softlockup_all_cpu_backtrace = !!simple_strtol(str, NULL, 0); |
ed235875 AT |
208 | return 1; |
209 | } | |
210 | __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup); | |
05a4a952 | 211 | #endif |
58687acb | 212 | |
941154bd TG |
213 | static void __lockup_detector_cleanup(void); |
214 | ||
4eec42f3 MSB |
215 | /* |
216 | * Hard-lockup warnings should be triggered after just a few seconds. Soft- | |
217 | * lockups can have false positives under extreme conditions. So we generally | |
218 | * want a higher threshold for soft lockups than for hard lockups. So we couple | |
219 | * the thresholds with a factor: we make the soft threshold twice the amount of | |
220 | * time the hard threshold is. | |
221 | */ | |
6e9101ae | 222 | static int get_softlockup_thresh(void) |
4eec42f3 MSB |
223 | { |
224 | return watchdog_thresh * 2; | |
225 | } | |
58687acb DZ |
226 | |
227 | /* | |
228 | * Returns seconds, approximately. We don't need nanosecond | |
229 | * resolution, and we don't need to waste time with a big divide when | |
230 | * 2^30ns == 1.074s. | |
231 | */ | |
c06b4f19 | 232 | static unsigned long get_timestamp(void) |
58687acb | 233 | { |
545a2bf7 | 234 | return running_clock() >> 30LL; /* 2^30 ~= 10^9 */ |
58687acb DZ |
235 | } |
236 | ||
0f34c400 | 237 | static void set_sample_period(void) |
58687acb DZ |
238 | { |
239 | /* | |
586692a5 | 240 | * convert watchdog_thresh from seconds to ns |
86f5e6a7 FLVC |
241 | * the divide by 5 is to give hrtimer several chances (two |
242 | * or three with the current relation between the soft | |
243 | * and hard thresholds) to increment before the | |
244 | * hardlockup detector generates a warning | |
58687acb | 245 | */ |
0f34c400 | 246 | sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5); |
7edaeb68 | 247 | watchdog_update_hrtimer_threshold(sample_period); |
58687acb DZ |
248 | } |
249 | ||
250 | /* Commands for resetting the watchdog */ | |
251 | static void __touch_watchdog(void) | |
252 | { | |
c06b4f19 | 253 | __this_cpu_write(watchdog_touch_ts, get_timestamp()); |
58687acb DZ |
254 | } |
255 | ||
03e0d461 TH |
256 | /** |
257 | * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls | |
258 | * | |
259 | * Call when the scheduler may have stalled for legitimate reasons | |
260 | * preventing the watchdog task from executing - e.g. the scheduler | |
261 | * entering idle state. This should only be used for scheduler events. | |
262 | * Use touch_softlockup_watchdog() for everything else. | |
263 | */ | |
264 | void touch_softlockup_watchdog_sched(void) | |
58687acb | 265 | { |
7861144b AM |
266 | /* |
267 | * Preemption can be enabled. It doesn't matter which CPU's timestamp | |
268 | * gets zeroed here, so use the raw_ operation. | |
269 | */ | |
270 | raw_cpu_write(watchdog_touch_ts, 0); | |
58687acb | 271 | } |
03e0d461 TH |
272 | |
273 | void touch_softlockup_watchdog(void) | |
274 | { | |
275 | touch_softlockup_watchdog_sched(); | |
82607adc | 276 | wq_watchdog_touch(raw_smp_processor_id()); |
03e0d461 | 277 | } |
0167c781 | 278 | EXPORT_SYMBOL(touch_softlockup_watchdog); |
58687acb | 279 | |
332fbdbc | 280 | void touch_all_softlockup_watchdogs(void) |
58687acb DZ |
281 | { |
282 | int cpu; | |
283 | ||
284 | /* | |
d57108d4 TG |
285 | * watchdog_mutex cannpt be taken here, as this might be called |
286 | * from (soft)interrupt context, so the access to | |
287 | * watchdog_allowed_cpumask might race with a concurrent update. | |
288 | * | |
289 | * The watchdog time stamp can race against a concurrent real | |
290 | * update as well, the only side effect might be a cycle delay for | |
291 | * the softlockup check. | |
58687acb | 292 | */ |
d57108d4 | 293 | for_each_cpu(cpu, &watchdog_allowed_mask) |
58687acb | 294 | per_cpu(watchdog_touch_ts, cpu) = 0; |
82607adc | 295 | wq_watchdog_touch(-1); |
58687acb DZ |
296 | } |
297 | ||
58687acb DZ |
298 | void touch_softlockup_watchdog_sync(void) |
299 | { | |
f7f66b05 CL |
300 | __this_cpu_write(softlockup_touch_sync, true); |
301 | __this_cpu_write(watchdog_touch_ts, 0); | |
58687acb DZ |
302 | } |
303 | ||
26e09c6e | 304 | static int is_softlockup(unsigned long touch_ts) |
58687acb | 305 | { |
c06b4f19 | 306 | unsigned long now = get_timestamp(); |
58687acb | 307 | |
39d2da21 | 308 | if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){ |
195daf66 UO |
309 | /* Warn about unreasonable delays. */ |
310 | if (time_after(now, touch_ts + get_softlockup_thresh())) | |
311 | return now - touch_ts; | |
312 | } | |
58687acb DZ |
313 | return 0; |
314 | } | |
315 | ||
05a4a952 NP |
316 | /* watchdog detector functions */ |
317 | bool is_hardlockup(void) | |
58687acb | 318 | { |
05a4a952 | 319 | unsigned long hrint = __this_cpu_read(hrtimer_interrupts); |
bcd951cf | 320 | |
05a4a952 NP |
321 | if (__this_cpu_read(hrtimer_interrupts_saved) == hrint) |
322 | return true; | |
323 | ||
324 | __this_cpu_write(hrtimer_interrupts_saved, hrint); | |
325 | return false; | |
73ce0511 | 326 | } |
05a4a952 NP |
327 | |
328 | static void watchdog_interrupt_count(void) | |
73ce0511 | 329 | { |
05a4a952 | 330 | __this_cpu_inc(hrtimer_interrupts); |
73ce0511 | 331 | } |
58687acb | 332 | |
9cf57731 PZ |
333 | /* |
334 | * The watchdog thread function - touches the timestamp. | |
335 | * | |
336 | * It only runs once every sample_period seconds (4 seconds by | |
337 | * default) to reset the softlockup timestamp. If this gets delayed | |
338 | * for more than 2*watchdog_thresh seconds then the debug-printout | |
339 | * triggers in watchdog_timer_fn(). | |
340 | */ | |
341 | static int softlockup_fn(void *data) | |
342 | { | |
343 | __this_cpu_write(soft_lockup_hrtimer_cnt, | |
344 | __this_cpu_read(hrtimer_interrupts)); | |
345 | __touch_watchdog(); | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work); | |
351 | ||
58687acb DZ |
352 | /* watchdog kicker functions */ |
353 | static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) | |
354 | { | |
909ea964 | 355 | unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts); |
58687acb DZ |
356 | struct pt_regs *regs = get_irq_regs(); |
357 | int duration; | |
ed235875 | 358 | int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace; |
58687acb | 359 | |
01f0a027 | 360 | if (!watchdog_enabled) |
b94f5118 DZ |
361 | return HRTIMER_NORESTART; |
362 | ||
58687acb DZ |
363 | /* kick the hardlockup detector */ |
364 | watchdog_interrupt_count(); | |
365 | ||
366 | /* kick the softlockup detector */ | |
9cf57731 PZ |
367 | stop_one_cpu_nowait(smp_processor_id(), |
368 | softlockup_fn, NULL, | |
369 | this_cpu_ptr(&softlockup_stop_work)); | |
58687acb DZ |
370 | |
371 | /* .. and repeat */ | |
0f34c400 | 372 | hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period)); |
58687acb DZ |
373 | |
374 | if (touch_ts == 0) { | |
909ea964 | 375 | if (unlikely(__this_cpu_read(softlockup_touch_sync))) { |
58687acb DZ |
376 | /* |
377 | * If the time stamp was touched atomically | |
378 | * make sure the scheduler tick is up to date. | |
379 | */ | |
909ea964 | 380 | __this_cpu_write(softlockup_touch_sync, false); |
58687acb DZ |
381 | sched_clock_tick(); |
382 | } | |
5d1c0f4a EM |
383 | |
384 | /* Clear the guest paused flag on watchdog reset */ | |
385 | kvm_check_and_clear_guest_paused(); | |
58687acb DZ |
386 | __touch_watchdog(); |
387 | return HRTIMER_RESTART; | |
388 | } | |
389 | ||
390 | /* check for a softlockup | |
391 | * This is done by making sure a high priority task is | |
392 | * being scheduled. The task touches the watchdog to | |
393 | * indicate it is getting cpu time. If it hasn't then | |
394 | * this is a good indication some task is hogging the cpu | |
395 | */ | |
26e09c6e | 396 | duration = is_softlockup(touch_ts); |
58687acb | 397 | if (unlikely(duration)) { |
5d1c0f4a EM |
398 | /* |
399 | * If a virtual machine is stopped by the host it can look to | |
400 | * the watchdog like a soft lockup, check to see if the host | |
401 | * stopped the vm before we issue the warning | |
402 | */ | |
403 | if (kvm_check_and_clear_guest_paused()) | |
404 | return HRTIMER_RESTART; | |
405 | ||
58687acb | 406 | /* only warn once */ |
b1a8de1f | 407 | if (__this_cpu_read(soft_watchdog_warn) == true) { |
408 | /* | |
409 | * When multiple processes are causing softlockups the | |
410 | * softlockup detector only warns on the first one | |
411 | * because the code relies on a full quiet cycle to | |
412 | * re-arm. The second process prevents the quiet cycle | |
413 | * and never gets reported. Use task pointers to detect | |
414 | * this. | |
415 | */ | |
416 | if (__this_cpu_read(softlockup_task_ptr_saved) != | |
417 | current) { | |
418 | __this_cpu_write(soft_watchdog_warn, false); | |
419 | __touch_watchdog(); | |
420 | } | |
58687acb | 421 | return HRTIMER_RESTART; |
b1a8de1f | 422 | } |
58687acb | 423 | |
ed235875 AT |
424 | if (softlockup_all_cpu_backtrace) { |
425 | /* Prevent multiple soft-lockup reports if one cpu is already | |
426 | * engaged in dumping cpu back traces | |
427 | */ | |
428 | if (test_and_set_bit(0, &soft_lockup_nmi_warn)) { | |
429 | /* Someone else will report us. Let's give up */ | |
430 | __this_cpu_write(soft_watchdog_warn, true); | |
431 | return HRTIMER_RESTART; | |
432 | } | |
433 | } | |
434 | ||
656c3b79 | 435 | pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", |
26e09c6e | 436 | smp_processor_id(), duration, |
58687acb | 437 | current->comm, task_pid_nr(current)); |
b1a8de1f | 438 | __this_cpu_write(softlockup_task_ptr_saved, current); |
58687acb DZ |
439 | print_modules(); |
440 | print_irqtrace_events(current); | |
441 | if (regs) | |
442 | show_regs(regs); | |
443 | else | |
444 | dump_stack(); | |
445 | ||
ed235875 AT |
446 | if (softlockup_all_cpu_backtrace) { |
447 | /* Avoid generating two back traces for current | |
448 | * given that one is already made above | |
449 | */ | |
450 | trigger_allbutself_cpu_backtrace(); | |
451 | ||
452 | clear_bit(0, &soft_lockup_nmi_warn); | |
453 | /* Barrier to sync with other cpus */ | |
454 | smp_mb__after_atomic(); | |
455 | } | |
456 | ||
69361eef | 457 | add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); |
58687acb DZ |
458 | if (softlockup_panic) |
459 | panic("softlockup: hung tasks"); | |
909ea964 | 460 | __this_cpu_write(soft_watchdog_warn, true); |
58687acb | 461 | } else |
909ea964 | 462 | __this_cpu_write(soft_watchdog_warn, false); |
58687acb DZ |
463 | |
464 | return HRTIMER_RESTART; | |
465 | } | |
466 | ||
bcd951cf | 467 | static void watchdog_enable(unsigned int cpu) |
58687acb | 468 | { |
01f0a027 | 469 | struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer); |
58687acb | 470 | |
9cf57731 PZ |
471 | WARN_ON_ONCE(cpu != smp_processor_id()); |
472 | ||
01f0a027 TG |
473 | /* |
474 | * Start the timer first to prevent the NMI watchdog triggering | |
475 | * before the timer has a chance to fire. | |
476 | */ | |
3935e895 BM |
477 | hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
478 | hrtimer->function = watchdog_timer_fn; | |
01f0a027 TG |
479 | hrtimer_start(hrtimer, ns_to_ktime(sample_period), |
480 | HRTIMER_MODE_REL_PINNED); | |
3935e895 | 481 | |
01f0a027 TG |
482 | /* Initialize timestamp */ |
483 | __touch_watchdog(); | |
bcd951cf | 484 | /* Enable the perf event */ |
146c9d0e TG |
485 | if (watchdog_enabled & NMI_WATCHDOG_ENABLED) |
486 | watchdog_nmi_enable(cpu); | |
bcd951cf | 487 | } |
58687acb | 488 | |
bcd951cf TG |
489 | static void watchdog_disable(unsigned int cpu) |
490 | { | |
01f0a027 | 491 | struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer); |
58687acb | 492 | |
9cf57731 PZ |
493 | WARN_ON_ONCE(cpu != smp_processor_id()); |
494 | ||
01f0a027 TG |
495 | /* |
496 | * Disable the perf event first. That prevents that a large delay | |
497 | * between disabling the timer and disabling the perf event causes | |
498 | * the perf NMI to detect a false positive. | |
499 | */ | |
bcd951cf | 500 | watchdog_nmi_disable(cpu); |
01f0a027 | 501 | hrtimer_cancel(hrtimer); |
58687acb DZ |
502 | } |
503 | ||
9cf57731 | 504 | static int softlockup_stop_fn(void *data) |
b8900bc0 | 505 | { |
9cf57731 PZ |
506 | watchdog_disable(smp_processor_id()); |
507 | return 0; | |
b8900bc0 FW |
508 | } |
509 | ||
9cf57731 | 510 | static void softlockup_stop_all(void) |
bcd951cf | 511 | { |
9cf57731 PZ |
512 | int cpu; |
513 | ||
514 | if (!softlockup_initialized) | |
515 | return; | |
516 | ||
517 | for_each_cpu(cpu, &watchdog_allowed_mask) | |
518 | smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false); | |
519 | ||
520 | cpumask_clear(&watchdog_allowed_mask); | |
bcd951cf TG |
521 | } |
522 | ||
9cf57731 | 523 | static int softlockup_start_fn(void *data) |
bcd951cf | 524 | { |
9cf57731 PZ |
525 | watchdog_enable(smp_processor_id()); |
526 | return 0; | |
bcd951cf | 527 | } |
58687acb | 528 | |
9cf57731 | 529 | static void softlockup_start_all(void) |
2eb2527f | 530 | { |
9cf57731 | 531 | int cpu; |
2eb2527f | 532 | |
9cf57731 PZ |
533 | cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask); |
534 | for_each_cpu(cpu, &watchdog_allowed_mask) | |
535 | smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false); | |
2eb2527f TG |
536 | } |
537 | ||
9cf57731 | 538 | int lockup_detector_online_cpu(unsigned int cpu) |
2eb2527f | 539 | { |
9cf57731 PZ |
540 | watchdog_enable(cpu); |
541 | return 0; | |
2eb2527f TG |
542 | } |
543 | ||
9cf57731 | 544 | int lockup_detector_offline_cpu(unsigned int cpu) |
2eb2527f | 545 | { |
9cf57731 PZ |
546 | watchdog_disable(cpu); |
547 | return 0; | |
2eb2527f TG |
548 | } |
549 | ||
5587185d | 550 | static void lockup_detector_reconfigure(void) |
2eb2527f | 551 | { |
e31d6883 | 552 | cpus_read_lock(); |
6b9dc480 | 553 | watchdog_nmi_stop(); |
9cf57731 PZ |
554 | |
555 | softlockup_stop_all(); | |
2eb2527f | 556 | set_sample_period(); |
09154985 TG |
557 | lockup_detector_update_enable(); |
558 | if (watchdog_enabled && watchdog_thresh) | |
9cf57731 PZ |
559 | softlockup_start_all(); |
560 | ||
6b9dc480 | 561 | watchdog_nmi_start(); |
e31d6883 TG |
562 | cpus_read_unlock(); |
563 | /* | |
564 | * Must be called outside the cpus locked section to prevent | |
565 | * recursive locking in the perf code. | |
566 | */ | |
567 | __lockup_detector_cleanup(); | |
2eb2527f TG |
568 | } |
569 | ||
570 | /* | |
5587185d | 571 | * Create the watchdog thread infrastructure and configure the detector(s). |
2eb2527f TG |
572 | * |
573 | * The threads are not unparked as watchdog_allowed_mask is empty. When | |
574 | * the threads are sucessfully initialized, take the proper locks and | |
575 | * unpark the threads in the watchdog_cpumask if the watchdog is enabled. | |
576 | */ | |
5587185d | 577 | static __init void lockup_detector_setup(void) |
2eb2527f | 578 | { |
2eb2527f TG |
579 | /* |
580 | * If sysctl is off and watchdog got disabled on the command line, | |
581 | * nothing to do here. | |
582 | */ | |
09154985 TG |
583 | lockup_detector_update_enable(); |
584 | ||
2eb2527f TG |
585 | if (!IS_ENABLED(CONFIG_SYSCTL) && |
586 | !(watchdog_enabled && watchdog_thresh)) | |
587 | return; | |
588 | ||
2eb2527f | 589 | mutex_lock(&watchdog_mutex); |
5587185d | 590 | lockup_detector_reconfigure(); |
9cf57731 | 591 | softlockup_initialized = true; |
2eb2527f TG |
592 | mutex_unlock(&watchdog_mutex); |
593 | } | |
594 | ||
2b9d7f23 | 595 | #else /* CONFIG_SOFTLOCKUP_DETECTOR */ |
5587185d | 596 | static void lockup_detector_reconfigure(void) |
6592ad2f | 597 | { |
e31d6883 | 598 | cpus_read_lock(); |
6b9dc480 | 599 | watchdog_nmi_stop(); |
09154985 | 600 | lockup_detector_update_enable(); |
6b9dc480 | 601 | watchdog_nmi_start(); |
e31d6883 | 602 | cpus_read_unlock(); |
6592ad2f | 603 | } |
5587185d | 604 | static inline void lockup_detector_setup(void) |
34ddaa3e | 605 | { |
5587185d | 606 | lockup_detector_reconfigure(); |
34ddaa3e | 607 | } |
2b9d7f23 | 608 | #endif /* !CONFIG_SOFTLOCKUP_DETECTOR */ |
05a4a952 | 609 | |
941154bd TG |
610 | static void __lockup_detector_cleanup(void) |
611 | { | |
612 | lockdep_assert_held(&watchdog_mutex); | |
613 | hardlockup_detector_perf_cleanup(); | |
614 | } | |
615 | ||
616 | /** | |
617 | * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes | |
618 | * | |
619 | * Caller must not hold the cpu hotplug rwsem. | |
620 | */ | |
621 | void lockup_detector_cleanup(void) | |
622 | { | |
623 | mutex_lock(&watchdog_mutex); | |
624 | __lockup_detector_cleanup(); | |
625 | mutex_unlock(&watchdog_mutex); | |
626 | } | |
627 | ||
6554fd8c TG |
628 | /** |
629 | * lockup_detector_soft_poweroff - Interface to stop lockup detector(s) | |
630 | * | |
631 | * Special interface for parisc. It prevents lockup detector warnings from | |
632 | * the default pm_poweroff() function which busy loops forever. | |
633 | */ | |
634 | void lockup_detector_soft_poweroff(void) | |
635 | { | |
636 | watchdog_enabled = 0; | |
637 | } | |
638 | ||
58cf690a UO |
639 | #ifdef CONFIG_SYSCTL |
640 | ||
e8b62b2d | 641 | /* Propagate any changes to the watchdog threads */ |
d57108d4 | 642 | static void proc_watchdog_update(void) |
a0c9cbb9 | 643 | { |
e8b62b2d TG |
644 | /* Remove impossible cpus to keep sysctl output clean. */ |
645 | cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask); | |
5587185d | 646 | lockup_detector_reconfigure(); |
a0c9cbb9 UO |
647 | } |
648 | ||
ef246a21 UO |
649 | /* |
650 | * common function for watchdog, nmi_watchdog and soft_watchdog parameter | |
651 | * | |
7feeb9cd TG |
652 | * caller | table->data points to | 'which' |
653 | * -------------------|----------------------------|-------------------------- | |
654 | * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED | | |
655 | * | | SOFT_WATCHDOG_ENABLED | |
656 | * -------------------|----------------------------|-------------------------- | |
657 | * proc_nmi_watchdog | nmi_watchdog_user_enabled | NMI_WATCHDOG_ENABLED | |
658 | * -------------------|----------------------------|-------------------------- | |
659 | * proc_soft_watchdog | soft_watchdog_user_enabled | SOFT_WATCHDOG_ENABLED | |
ef246a21 UO |
660 | */ |
661 | static int proc_watchdog_common(int which, struct ctl_table *table, int write, | |
662 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
663 | { | |
09154985 | 664 | int err, old, *param = table->data; |
ef246a21 | 665 | |
946d1977 | 666 | mutex_lock(&watchdog_mutex); |
ef246a21 | 667 | |
ef246a21 | 668 | if (!write) { |
09154985 TG |
669 | /* |
670 | * On read synchronize the userspace interface. This is a | |
671 | * racy snapshot. | |
672 | */ | |
673 | *param = (watchdog_enabled & which) != 0; | |
ef246a21 UO |
674 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
675 | } else { | |
09154985 | 676 | old = READ_ONCE(*param); |
ef246a21 | 677 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
09154985 | 678 | if (!err && old != READ_ONCE(*param)) |
d57108d4 | 679 | proc_watchdog_update(); |
ef246a21 | 680 | } |
946d1977 | 681 | mutex_unlock(&watchdog_mutex); |
ef246a21 UO |
682 | return err; |
683 | } | |
684 | ||
83a80a39 UO |
685 | /* |
686 | * /proc/sys/kernel/watchdog | |
687 | */ | |
688 | int proc_watchdog(struct ctl_table *table, int write, | |
689 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
690 | { | |
691 | return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED, | |
692 | table, write, buffer, lenp, ppos); | |
693 | } | |
694 | ||
695 | /* | |
696 | * /proc/sys/kernel/nmi_watchdog | |
58687acb | 697 | */ |
83a80a39 UO |
698 | int proc_nmi_watchdog(struct ctl_table *table, int write, |
699 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
700 | { | |
a994a314 TG |
701 | if (!nmi_watchdog_available && write) |
702 | return -ENOTSUPP; | |
83a80a39 UO |
703 | return proc_watchdog_common(NMI_WATCHDOG_ENABLED, |
704 | table, write, buffer, lenp, ppos); | |
705 | } | |
706 | ||
707 | /* | |
708 | * /proc/sys/kernel/soft_watchdog | |
709 | */ | |
710 | int proc_soft_watchdog(struct ctl_table *table, int write, | |
711 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
712 | { | |
713 | return proc_watchdog_common(SOFT_WATCHDOG_ENABLED, | |
714 | table, write, buffer, lenp, ppos); | |
715 | } | |
58687acb | 716 | |
83a80a39 UO |
717 | /* |
718 | * /proc/sys/kernel/watchdog_thresh | |
719 | */ | |
720 | int proc_watchdog_thresh(struct ctl_table *table, int write, | |
721 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
58687acb | 722 | { |
d57108d4 | 723 | int err, old; |
58687acb | 724 | |
946d1977 | 725 | mutex_lock(&watchdog_mutex); |
bcd951cf | 726 | |
d57108d4 | 727 | old = READ_ONCE(watchdog_thresh); |
b8900bc0 | 728 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
83a80a39 | 729 | |
d57108d4 TG |
730 | if (!err && write && old != READ_ONCE(watchdog_thresh)) |
731 | proc_watchdog_update(); | |
e04ab2bc | 732 | |
946d1977 | 733 | mutex_unlock(&watchdog_mutex); |
b8900bc0 | 734 | return err; |
58687acb | 735 | } |
fe4ba3c3 CM |
736 | |
737 | /* | |
738 | * The cpumask is the mask of possible cpus that the watchdog can run | |
739 | * on, not the mask of cpus it is actually running on. This allows the | |
740 | * user to specify a mask that will include cpus that have not yet | |
741 | * been brought online, if desired. | |
742 | */ | |
743 | int proc_watchdog_cpumask(struct ctl_table *table, int write, | |
744 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
745 | { | |
746 | int err; | |
747 | ||
946d1977 | 748 | mutex_lock(&watchdog_mutex); |
8c073d27 | 749 | |
fe4ba3c3 | 750 | err = proc_do_large_bitmap(table, write, buffer, lenp, ppos); |
05ba3de7 | 751 | if (!err && write) |
e8b62b2d | 752 | proc_watchdog_update(); |
5490125d | 753 | |
946d1977 | 754 | mutex_unlock(&watchdog_mutex); |
fe4ba3c3 CM |
755 | return err; |
756 | } | |
58687acb DZ |
757 | #endif /* CONFIG_SYSCTL */ |
758 | ||
004417a6 | 759 | void __init lockup_detector_init(void) |
58687acb | 760 | { |
13316b31 | 761 | if (tick_nohz_full_enabled()) |
314b08ff | 762 | pr_info("Disabling watchdog on nohz_full cores by default\n"); |
13316b31 | 763 | |
de201559 FW |
764 | cpumask_copy(&watchdog_cpumask, |
765 | housekeeping_cpumask(HK_FLAG_TIMER)); | |
fe4ba3c3 | 766 | |
a994a314 TG |
767 | if (!watchdog_nmi_probe()) |
768 | nmi_watchdog_available = true; | |
5587185d | 769 | lockup_detector_setup(); |
58687acb | 770 | } |