Merge tag 'linux_kselftest_active-fixes-6.6-rc7' of git://git.kernel.org/pub/scm...
[linux-2.6-block.git] / kernel / watchdog.c
CommitLineData
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 30static DEFINE_MUTEX(watchdog_mutex);
ab992dc3 31
47f4cb43 32#if defined(CONFIG_HARDLOCKUP_DETECTOR) || defined(CONFIG_HARDLOCKUP_DETECTOR_SPARC64)
df95d308 33# define WATCHDOG_HARDLOCKUP_DEFAULT 1
84d56e66 34#else
df95d308 35# define WATCHDOG_HARDLOCKUP_DEFAULT 0
84d56e66 36#endif
05a4a952 37
09154985
TG
38unsigned long __read_mostly watchdog_enabled;
39int __read_mostly watchdog_user_enabled = 1;
df95d308
DA
40static int __read_mostly watchdog_hardlockup_user_enabled = WATCHDOG_HARDLOCKUP_DEFAULT;
41static int __read_mostly watchdog_softlockup_user_enabled = 1;
7feeb9cd 42int __read_mostly watchdog_thresh = 10;
df95d308 43static int __read_mostly watchdog_hardlockup_available;
7feeb9cd 44
7feeb9cd
TG
45struct cpumask watchdog_cpumask __read_mostly;
46unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
47
05a4a952 48#ifdef CONFIG_HARDLOCKUP_DETECTOR
f117955a
GP
49
50# ifdef CONFIG_SMP
51int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
52# endif /* CONFIG_SMP */
53
05a4a952
NP
54/*
55 * Should we panic when a soft-lockup or hard-lockup occurs:
56 */
57unsigned int __read_mostly hardlockup_panic =
67fca000 58 IS_ENABLED(CONFIG_BOOTPARAM_HARDLOCKUP_PANIC);
05a4a952
NP
59/*
60 * We may not want to enable hard lockup detection by default in all cases,
61 * for example when running the kernel as a guest on a hypervisor. In these
62 * cases this function can be called to disable hard lockup detection. This
63 * function should only be executed once by the boot processor before the
64 * kernel command line parameters are parsed, because otherwise it is not
65 * possible to override this in hardlockup_panic_setup().
66 */
7a355820 67void __init hardlockup_detector_disable(void)
05a4a952 68{
df95d308 69 watchdog_hardlockup_user_enabled = 0;
05a4a952
NP
70}
71
72static int __init hardlockup_panic_setup(char *str)
73{
74 if (!strncmp(str, "panic", 5))
75 hardlockup_panic = 1;
76 else if (!strncmp(str, "nopanic", 7))
77 hardlockup_panic = 0;
78 else if (!strncmp(str, "0", 1))
df95d308 79 watchdog_hardlockup_user_enabled = 0;
05a4a952 80 else if (!strncmp(str, "1", 1))
df95d308 81 watchdog_hardlockup_user_enabled = 1;
05a4a952
NP
82 return 1;
83}
84__setup("nmi_watchdog=", hardlockup_panic_setup);
85
368a7e2c 86#endif /* CONFIG_HARDLOCKUP_DETECTOR */
05a4a952 87
1f423c90 88#if defined(CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER)
81972551 89
77c12fc9
DA
90static DEFINE_PER_CPU(atomic_t, hrtimer_interrupts);
91static DEFINE_PER_CPU(int, hrtimer_interrupts_saved);
1610611a 92static DEFINE_PER_CPU(bool, watchdog_hardlockup_warned);
ed92e1ef 93static DEFINE_PER_CPU(bool, watchdog_hardlockup_touched);
1610611a 94static unsigned long watchdog_hardlockup_all_cpu_dumped;
81972551 95
ed92e1ef
DA
96notrace void arch_touch_nmi_watchdog(void)
97{
98 /*
99 * Using __raw here because some code paths have
100 * preemption enabled. If preemption is enabled
101 * then interrupts should be enabled too, in which
102 * case we shouldn't have to worry about the watchdog
103 * going off.
104 */
105 raw_cpu_write(watchdog_hardlockup_touched, true);
106}
107EXPORT_SYMBOL(arch_touch_nmi_watchdog);
108
1f423c90
DA
109void watchdog_hardlockup_touch_cpu(unsigned int cpu)
110{
111 per_cpu(watchdog_hardlockup_touched, cpu) = true;
1f423c90
DA
112}
113
77c12fc9 114static bool is_hardlockup(unsigned int cpu)
81972551 115{
77c12fc9 116 int hrint = atomic_read(&per_cpu(hrtimer_interrupts, cpu));
81972551 117
77c12fc9 118 if (per_cpu(hrtimer_interrupts_saved, cpu) == hrint)
81972551
DA
119 return true;
120
77c12fc9
DA
121 /*
122 * NOTE: we don't need any fancy atomic_t or READ_ONCE/WRITE_ONCE
123 * for hrtimer_interrupts_saved. hrtimer_interrupts_saved is
124 * written/read by a single CPU.
125 */
126 per_cpu(hrtimer_interrupts_saved, cpu) = hrint;
1610611a 127
81972551
DA
128 return false;
129}
130
d3b62ace 131static void watchdog_hardlockup_kick(void)
81972551 132{
d3b62ace
DA
133 int new_interrupts;
134
135 new_interrupts = atomic_inc_return(this_cpu_ptr(&hrtimer_interrupts));
136 watchdog_buddy_check_hardlockup(new_interrupts);
81972551
DA
137}
138
77c12fc9 139void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs)
81972551 140{
ed92e1ef
DA
141 if (per_cpu(watchdog_hardlockup_touched, cpu)) {
142 per_cpu(watchdog_hardlockup_touched, cpu) = false;
143 return;
144 }
145
1610611a
DA
146 /*
147 * Check for a hardlockup by making sure the CPU's timer
148 * interrupt is incrementing. The timer interrupt should have
149 * fired multiple times before we overflow'd. If it hasn't
81972551
DA
150 * then this is a good indication the cpu is stuck
151 */
77c12fc9 152 if (is_hardlockup(cpu)) {
1610611a 153 unsigned int this_cpu = smp_processor_id();
81972551 154
1610611a 155 /* Only print hardlockups once. */
77c12fc9 156 if (per_cpu(watchdog_hardlockup_warned, cpu))
81972551
DA
157 return;
158
77c12fc9 159 pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", cpu);
81972551
DA
160 print_modules();
161 print_irqtrace_events(current);
77c12fc9
DA
162 if (cpu == this_cpu) {
163 if (regs)
164 show_regs(regs);
165 else
166 dump_stack();
77c12fc9 167 } else {
1f38c86b 168 trigger_single_cpu_backtrace(cpu);
77c12fc9 169 }
81972551
DA
170
171 /*
77c12fc9
DA
172 * Perform multi-CPU dump only once to avoid multiple
173 * hardlockups generating interleaving traces
81972551
DA
174 */
175 if (sysctl_hardlockup_all_cpu_backtrace &&
1610611a 176 !test_and_set_bit(0, &watchdog_hardlockup_all_cpu_dumped))
1f38c86b 177 trigger_allbutcpu_cpu_backtrace(cpu);
81972551
DA
178
179 if (hardlockup_panic)
180 nmi_panic(regs, "Hard LOCKUP");
181
77c12fc9 182 per_cpu(watchdog_hardlockup_warned, cpu) = true;
1610611a 183 } else {
77c12fc9 184 per_cpu(watchdog_hardlockup_warned, cpu) = false;
81972551 185 }
81972551
DA
186}
187
1f423c90 188#else /* CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
81972551 189
d3b62ace 190static inline void watchdog_hardlockup_kick(void) { }
81972551 191
1f423c90 192#endif /* !CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER */
81972551 193
05a4a952 194/*
d9b3629a 195 * These functions can be overridden based on the configured hardlockdup detector.
a10a842f 196 *
df95d308 197 * watchdog_hardlockup_enable/disable can be implemented to start and stop when
d9b3629a 198 * softlockup watchdog start and stop. The detector must select the
a10a842f 199 * SOFTLOCKUP_DETECTOR Kconfig.
05a4a952 200 */
d9b3629a 201void __weak watchdog_hardlockup_enable(unsigned int cpu) { }
941154bd 202
d9b3629a 203void __weak watchdog_hardlockup_disable(unsigned int cpu) { }
05a4a952 204
930d8f8d
LC
205/*
206 * Watchdog-detector specific API.
207 *
208 * Return 0 when hardlockup watchdog is available, negative value otherwise.
209 * Note that the negative value means that a delayed probe might
210 * succeed later.
211 */
df95d308 212int __weak __init watchdog_hardlockup_probe(void)
a994a314 213{
d9b3629a 214 return -ENODEV;
a994a314
TG
215}
216
6592ad2f 217/**
df95d308 218 * watchdog_hardlockup_stop - Stop the watchdog for reconfiguration
6592ad2f 219 *
6b9dc480 220 * The reconfiguration steps are:
df95d308 221 * watchdog_hardlockup_stop();
6592ad2f 222 * update_variables();
df95d308 223 * watchdog_hardlockup_start();
6b9dc480 224 */
df95d308 225void __weak watchdog_hardlockup_stop(void) { }
6b9dc480
TG
226
227/**
df95d308 228 * watchdog_hardlockup_start - Start the watchdog after reconfiguration
6592ad2f 229 *
df95d308 230 * Counterpart to watchdog_hardlockup_stop().
6b9dc480
TG
231 *
232 * The following variables have been updated in update_variables() and
233 * contain the currently valid configuration:
7feeb9cd 234 * - watchdog_enabled
a10a842f
NP
235 * - watchdog_thresh
236 * - watchdog_cpumask
a10a842f 237 */
df95d308 238void __weak watchdog_hardlockup_start(void) { }
a10a842f 239
09154985
TG
240/**
241 * lockup_detector_update_enable - Update the sysctl enable bit
242 *
df95d308
DA
243 * Caller needs to make sure that the hard watchdogs are off, so this
244 * can't race with watchdog_hardlockup_disable().
09154985
TG
245 */
246static void lockup_detector_update_enable(void)
247{
248 watchdog_enabled = 0;
249 if (!watchdog_user_enabled)
250 return;
df95d308
DA
251 if (watchdog_hardlockup_available && watchdog_hardlockup_user_enabled)
252 watchdog_enabled |= WATCHDOG_HARDLOCKUP_ENABLED;
253 if (watchdog_softlockup_user_enabled)
254 watchdog_enabled |= WATCHDOG_SOFTOCKUP_ENABLED;
09154985
TG
255}
256
05a4a952
NP
257#ifdef CONFIG_SOFTLOCKUP_DETECTOR
258
fef06efc
PM
259/*
260 * Delay the soflockup report when running a known slow code.
261 * It does _not_ affect the timestamp of the last successdul reschedule.
262 */
263#define SOFTLOCKUP_DELAY_REPORT ULONG_MAX
11e31f60 264
f117955a
GP
265#ifdef CONFIG_SMP
266int __read_mostly sysctl_softlockup_all_cpu_backtrace;
267#endif
268
e7e04615
SS
269static struct cpumask watchdog_allowed_mask __read_mostly;
270
2b9d7f23
TG
271/* Global variables, exported for sysctl */
272unsigned int __read_mostly softlockup_panic =
67fca000 273 IS_ENABLED(CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC);
2eb2527f 274
9cf57731 275static bool softlockup_initialized __read_mostly;
0f34c400 276static u64 __read_mostly sample_period;
58687acb 277
fef06efc 278/* Timestamp taken after the last successful reschedule. */
58687acb 279static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
fef06efc
PM
280/* Timestamp of the last softlockup report. */
281static DEFINE_PER_CPU(unsigned long, watchdog_report_ts);
58687acb
DZ
282static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
283static DEFINE_PER_CPU(bool, softlockup_touch_sync);
ed235875 284static unsigned long soft_lockup_nmi_warn;
58687acb 285
58687acb
DZ
286static int __init nowatchdog_setup(char *str)
287{
09154985 288 watchdog_user_enabled = 0;
58687acb
DZ
289 return 1;
290}
291__setup("nowatchdog", nowatchdog_setup);
292
58687acb
DZ
293static int __init nosoftlockup_setup(char *str)
294{
df95d308 295 watchdog_softlockup_user_enabled = 0;
58687acb
DZ
296 return 1;
297}
298__setup("nosoftlockup", nosoftlockup_setup);
195daf66 299
11295055
LO
300static int __init watchdog_thresh_setup(char *str)
301{
302 get_option(&str, &watchdog_thresh);
303 return 1;
304}
305__setup("watchdog_thresh=", watchdog_thresh_setup);
306
941154bd
TG
307static void __lockup_detector_cleanup(void);
308
4eec42f3
MSB
309/*
310 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
311 * lockups can have false positives under extreme conditions. So we generally
312 * want a higher threshold for soft lockups than for hard lockups. So we couple
313 * the thresholds with a factor: we make the soft threshold twice the amount of
314 * time the hard threshold is.
315 */
6e9101ae 316static int get_softlockup_thresh(void)
4eec42f3
MSB
317{
318 return watchdog_thresh * 2;
319}
58687acb
DZ
320
321/*
322 * Returns seconds, approximately. We don't need nanosecond
323 * resolution, and we don't need to waste time with a big divide when
324 * 2^30ns == 1.074s.
325 */
c06b4f19 326static unsigned long get_timestamp(void)
58687acb 327{
545a2bf7 328 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
58687acb
DZ
329}
330
0f34c400 331static void set_sample_period(void)
58687acb
DZ
332{
333 /*
586692a5 334 * convert watchdog_thresh from seconds to ns
86f5e6a7
FLVC
335 * the divide by 5 is to give hrtimer several chances (two
336 * or three with the current relation between the soft
337 * and hard thresholds) to increment before the
338 * hardlockup detector generates a warning
58687acb 339 */
0f34c400 340 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
7edaeb68 341 watchdog_update_hrtimer_threshold(sample_period);
58687acb
DZ
342}
343
fef06efc
PM
344static void update_report_ts(void)
345{
346 __this_cpu_write(watchdog_report_ts, get_timestamp());
347}
348
58687acb 349/* Commands for resetting the watchdog */
7c0012f5 350static void update_touch_ts(void)
58687acb 351{
c06b4f19 352 __this_cpu_write(watchdog_touch_ts, get_timestamp());
fef06efc 353 update_report_ts();
58687acb
DZ
354}
355
03e0d461
TH
356/**
357 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
358 *
359 * Call when the scheduler may have stalled for legitimate reasons
360 * preventing the watchdog task from executing - e.g. the scheduler
361 * entering idle state. This should only be used for scheduler events.
362 * Use touch_softlockup_watchdog() for everything else.
363 */
cb9d7fd5 364notrace void touch_softlockup_watchdog_sched(void)
58687acb 365{
7861144b 366 /*
fef06efc
PM
367 * Preemption can be enabled. It doesn't matter which CPU's watchdog
368 * report period gets restarted here, so use the raw_ operation.
7861144b 369 */
fef06efc 370 raw_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
58687acb 371}
03e0d461 372
cb9d7fd5 373notrace void touch_softlockup_watchdog(void)
03e0d461
TH
374{
375 touch_softlockup_watchdog_sched();
82607adc 376 wq_watchdog_touch(raw_smp_processor_id());
03e0d461 377}
0167c781 378EXPORT_SYMBOL(touch_softlockup_watchdog);
58687acb 379
332fbdbc 380void touch_all_softlockup_watchdogs(void)
58687acb
DZ
381{
382 int cpu;
383
384 /*
d57108d4
TG
385 * watchdog_mutex cannpt be taken here, as this might be called
386 * from (soft)interrupt context, so the access to
387 * watchdog_allowed_cpumask might race with a concurrent update.
388 *
389 * The watchdog time stamp can race against a concurrent real
390 * update as well, the only side effect might be a cycle delay for
391 * the softlockup check.
58687acb 392 */
89e28ce6 393 for_each_cpu(cpu, &watchdog_allowed_mask) {
fef06efc 394 per_cpu(watchdog_report_ts, cpu) = SOFTLOCKUP_DELAY_REPORT;
89e28ce6
WQ
395 wq_watchdog_touch(cpu);
396 }
58687acb
DZ
397}
398
58687acb
DZ
399void touch_softlockup_watchdog_sync(void)
400{
f7f66b05 401 __this_cpu_write(softlockup_touch_sync, true);
fef06efc 402 __this_cpu_write(watchdog_report_ts, SOFTLOCKUP_DELAY_REPORT);
58687acb
DZ
403}
404
0f90b88d
PM
405static int is_softlockup(unsigned long touch_ts,
406 unsigned long period_ts,
407 unsigned long now)
58687acb 408{
df95d308 409 if ((watchdog_enabled & WATCHDOG_SOFTOCKUP_ENABLED) && watchdog_thresh) {
195daf66 410 /* Warn about unreasonable delays. */
fef06efc 411 if (time_after(now, period_ts + get_softlockup_thresh()))
195daf66
UO
412 return now - touch_ts;
413 }
58687acb
DZ
414 return 0;
415}
416
05a4a952 417/* watchdog detector functions */
be45bf53
PZ
418static DEFINE_PER_CPU(struct completion, softlockup_completion);
419static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work);
420
9cf57731 421/*
b124ac45 422 * The watchdog feed function - touches the timestamp.
9cf57731
PZ
423 *
424 * It only runs once every sample_period seconds (4 seconds by
425 * default) to reset the softlockup timestamp. If this gets delayed
426 * for more than 2*watchdog_thresh seconds then the debug-printout
427 * triggers in watchdog_timer_fn().
428 */
429static int softlockup_fn(void *data)
430{
7c0012f5 431 update_touch_ts();
be45bf53 432 complete(this_cpu_ptr(&softlockup_completion));
9cf57731
PZ
433
434 return 0;
435}
436
58687acb
DZ
437/* watchdog kicker functions */
438static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
439{
0f90b88d 440 unsigned long touch_ts, period_ts, now;
58687acb
DZ
441 struct pt_regs *regs = get_irq_regs();
442 int duration;
ed235875 443 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
58687acb 444
01f0a027 445 if (!watchdog_enabled)
b94f5118
DZ
446 return HRTIMER_NORESTART;
447
d3b62ace 448 watchdog_hardlockup_kick();
58687acb
DZ
449
450 /* kick the softlockup detector */
be45bf53
PZ
451 if (completion_done(this_cpu_ptr(&softlockup_completion))) {
452 reinit_completion(this_cpu_ptr(&softlockup_completion));
453 stop_one_cpu_nowait(smp_processor_id(),
454 softlockup_fn, NULL,
455 this_cpu_ptr(&softlockup_stop_work));
456 }
58687acb
DZ
457
458 /* .. and repeat */
0f34c400 459 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
58687acb 460
0f90b88d
PM
461 /*
462 * Read the current timestamp first. It might become invalid anytime
463 * when a virtual machine is stopped by the host or when the watchog
464 * is touched from NMI.
465 */
466 now = get_timestamp();
9bf3bc94
PM
467 /*
468 * If a virtual machine is stopped by the host it can look to
0f90b88d 469 * the watchdog like a soft lockup. This function touches the watchdog.
9bf3bc94
PM
470 */
471 kvm_check_and_clear_guest_paused();
0f90b88d
PM
472 /*
473 * The stored timestamp is comparable with @now only when not touched.
474 * It might get touched anytime from NMI. Make sure that is_softlockup()
475 * uses the same (valid) value.
476 */
477 period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts));
9bf3bc94
PM
478
479 /* Reset the interval when touched by known problematic code. */
fef06efc 480 if (period_ts == SOFTLOCKUP_DELAY_REPORT) {
909ea964 481 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
58687acb
DZ
482 /*
483 * If the time stamp was touched atomically
484 * make sure the scheduler tick is up to date.
485 */
909ea964 486 __this_cpu_write(softlockup_touch_sync, false);
58687acb
DZ
487 sched_clock_tick();
488 }
5d1c0f4a 489
fef06efc 490 update_report_ts();
58687acb
DZ
491 return HRTIMER_RESTART;
492 }
493
0f90b88d
PM
494 /* Check for a softlockup. */
495 touch_ts = __this_cpu_read(watchdog_touch_ts);
496 duration = is_softlockup(touch_ts, period_ts, now);
58687acb 497 if (unlikely(duration)) {
9f113bf7
PM
498 /*
499 * Prevent multiple soft-lockup reports if one cpu is already
500 * engaged in dumping all cpu back traces.
501 */
ed235875 502 if (softlockup_all_cpu_backtrace) {
9f113bf7 503 if (test_and_set_bit_lock(0, &soft_lockup_nmi_warn))
ed235875 504 return HRTIMER_RESTART;
ed235875
AT
505 }
506
c9ad17c9 507 /* Start period for the next softlockup warning. */
fef06efc 508 update_report_ts();
c9ad17c9 509
656c3b79 510 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
26e09c6e 511 smp_processor_id(), duration,
58687acb
DZ
512 current->comm, task_pid_nr(current));
513 print_modules();
514 print_irqtrace_events(current);
515 if (regs)
516 show_regs(regs);
517 else
518 dump_stack();
519
ed235875 520 if (softlockup_all_cpu_backtrace) {
8d539b84 521 trigger_allbutcpu_cpu_backtrace(smp_processor_id());
9f113bf7 522 clear_bit_unlock(0, &soft_lockup_nmi_warn);
ed235875
AT
523 }
524
69361eef 525 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
58687acb
DZ
526 if (softlockup_panic)
527 panic("softlockup: hung tasks");
1bc503cb 528 }
58687acb
DZ
529
530 return HRTIMER_RESTART;
531}
532
bcd951cf 533static void watchdog_enable(unsigned int cpu)
58687acb 534{
01f0a027 535 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
be45bf53 536 struct completion *done = this_cpu_ptr(&softlockup_completion);
58687acb 537
9cf57731
PZ
538 WARN_ON_ONCE(cpu != smp_processor_id());
539
be45bf53
PZ
540 init_completion(done);
541 complete(done);
542
01f0a027 543 /*
df95d308 544 * Start the timer first to prevent the hardlockup watchdog triggering
01f0a027
TG
545 * before the timer has a chance to fire.
546 */
d2ab4cf4 547 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
3935e895 548 hrtimer->function = watchdog_timer_fn;
01f0a027 549 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
d2ab4cf4 550 HRTIMER_MODE_REL_PINNED_HARD);
3935e895 551
01f0a027 552 /* Initialize timestamp */
7c0012f5 553 update_touch_ts();
df95d308
DA
554 /* Enable the hardlockup detector */
555 if (watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED)
556 watchdog_hardlockup_enable(cpu);
bcd951cf 557}
58687acb 558
bcd951cf
TG
559static void watchdog_disable(unsigned int cpu)
560{
01f0a027 561 struct hrtimer *hrtimer = this_cpu_ptr(&watchdog_hrtimer);
58687acb 562
9cf57731
PZ
563 WARN_ON_ONCE(cpu != smp_processor_id());
564
01f0a027 565 /*
df95d308
DA
566 * Disable the hardlockup detector first. That prevents that a large
567 * delay between disabling the timer and disabling the hardlockup
568 * detector causes a false positive.
01f0a027 569 */
df95d308 570 watchdog_hardlockup_disable(cpu);
01f0a027 571 hrtimer_cancel(hrtimer);
be45bf53 572 wait_for_completion(this_cpu_ptr(&softlockup_completion));
58687acb
DZ
573}
574
9cf57731 575static int softlockup_stop_fn(void *data)
b8900bc0 576{
9cf57731
PZ
577 watchdog_disable(smp_processor_id());
578 return 0;
b8900bc0
FW
579}
580
9cf57731 581static void softlockup_stop_all(void)
bcd951cf 582{
9cf57731
PZ
583 int cpu;
584
585 if (!softlockup_initialized)
586 return;
587
588 for_each_cpu(cpu, &watchdog_allowed_mask)
589 smp_call_on_cpu(cpu, softlockup_stop_fn, NULL, false);
590
591 cpumask_clear(&watchdog_allowed_mask);
bcd951cf
TG
592}
593
9cf57731 594static int softlockup_start_fn(void *data)
bcd951cf 595{
9cf57731
PZ
596 watchdog_enable(smp_processor_id());
597 return 0;
bcd951cf 598}
58687acb 599
9cf57731 600static void softlockup_start_all(void)
2eb2527f 601{
9cf57731 602 int cpu;
2eb2527f 603
9cf57731
PZ
604 cpumask_copy(&watchdog_allowed_mask, &watchdog_cpumask);
605 for_each_cpu(cpu, &watchdog_allowed_mask)
606 smp_call_on_cpu(cpu, softlockup_start_fn, NULL, false);
2eb2527f
TG
607}
608
9cf57731 609int lockup_detector_online_cpu(unsigned int cpu)
2eb2527f 610{
7dd47617
TG
611 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
612 watchdog_enable(cpu);
9cf57731 613 return 0;
2eb2527f
TG
614}
615
9cf57731 616int lockup_detector_offline_cpu(unsigned int cpu)
2eb2527f 617{
7dd47617
TG
618 if (cpumask_test_cpu(cpu, &watchdog_allowed_mask))
619 watchdog_disable(cpu);
9cf57731 620 return 0;
2eb2527f
TG
621}
622
7c56a873 623static void __lockup_detector_reconfigure(void)
2eb2527f 624{
e31d6883 625 cpus_read_lock();
df95d308 626 watchdog_hardlockup_stop();
9cf57731
PZ
627
628 softlockup_stop_all();
2eb2527f 629 set_sample_period();
09154985
TG
630 lockup_detector_update_enable();
631 if (watchdog_enabled && watchdog_thresh)
9cf57731
PZ
632 softlockup_start_all();
633
df95d308 634 watchdog_hardlockup_start();
e31d6883
TG
635 cpus_read_unlock();
636 /*
637 * Must be called outside the cpus locked section to prevent
638 * recursive locking in the perf code.
639 */
640 __lockup_detector_cleanup();
2eb2527f
TG
641}
642
7c56a873
LD
643void lockup_detector_reconfigure(void)
644{
645 mutex_lock(&watchdog_mutex);
646 __lockup_detector_reconfigure();
647 mutex_unlock(&watchdog_mutex);
648}
649
2eb2527f 650/*
b124ac45 651 * Create the watchdog infrastructure and configure the detector(s).
2eb2527f 652 */
5587185d 653static __init void lockup_detector_setup(void)
2eb2527f 654{
2eb2527f
TG
655 /*
656 * If sysctl is off and watchdog got disabled on the command line,
657 * nothing to do here.
658 */
09154985
TG
659 lockup_detector_update_enable();
660
2eb2527f
TG
661 if (!IS_ENABLED(CONFIG_SYSCTL) &&
662 !(watchdog_enabled && watchdog_thresh))
663 return;
664
2eb2527f 665 mutex_lock(&watchdog_mutex);
7c56a873 666 __lockup_detector_reconfigure();
9cf57731 667 softlockup_initialized = true;
2eb2527f
TG
668 mutex_unlock(&watchdog_mutex);
669}
670
2b9d7f23 671#else /* CONFIG_SOFTLOCKUP_DETECTOR */
7c56a873 672static void __lockup_detector_reconfigure(void)
6592ad2f 673{
e31d6883 674 cpus_read_lock();
df95d308 675 watchdog_hardlockup_stop();
09154985 676 lockup_detector_update_enable();
df95d308 677 watchdog_hardlockup_start();
e31d6883 678 cpus_read_unlock();
6592ad2f 679}
7c56a873
LD
680void lockup_detector_reconfigure(void)
681{
682 __lockup_detector_reconfigure();
683}
5587185d 684static inline void lockup_detector_setup(void)
34ddaa3e 685{
7c56a873 686 __lockup_detector_reconfigure();
34ddaa3e 687}
2b9d7f23 688#endif /* !CONFIG_SOFTLOCKUP_DETECTOR */
05a4a952 689
941154bd
TG
690static void __lockup_detector_cleanup(void)
691{
692 lockdep_assert_held(&watchdog_mutex);
693 hardlockup_detector_perf_cleanup();
694}
695
696/**
697 * lockup_detector_cleanup - Cleanup after cpu hotplug or sysctl changes
698 *
699 * Caller must not hold the cpu hotplug rwsem.
700 */
701void lockup_detector_cleanup(void)
702{
703 mutex_lock(&watchdog_mutex);
704 __lockup_detector_cleanup();
705 mutex_unlock(&watchdog_mutex);
706}
707
6554fd8c
TG
708/**
709 * lockup_detector_soft_poweroff - Interface to stop lockup detector(s)
710 *
711 * Special interface for parisc. It prevents lockup detector warnings from
712 * the default pm_poweroff() function which busy loops forever.
713 */
714void lockup_detector_soft_poweroff(void)
715{
716 watchdog_enabled = 0;
717}
718
58cf690a
UO
719#ifdef CONFIG_SYSCTL
720
b124ac45 721/* Propagate any changes to the watchdog infrastructure */
d57108d4 722static void proc_watchdog_update(void)
a0c9cbb9 723{
e8b62b2d
TG
724 /* Remove impossible cpus to keep sysctl output clean. */
725 cpumask_and(&watchdog_cpumask, &watchdog_cpumask, cpu_possible_mask);
7c56a873 726 __lockup_detector_reconfigure();
a0c9cbb9
UO
727}
728
ef246a21
UO
729/*
730 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
731 *
df95d308
DA
732 * caller | table->data points to | 'which'
733 * -------------------|----------------------------------|-------------------------------
734 * proc_watchdog | watchdog_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED |
735 * | | WATCHDOG_SOFTOCKUP_ENABLED
736 * -------------------|----------------------------------|-------------------------------
737 * proc_nmi_watchdog | watchdog_hardlockup_user_enabled | WATCHDOG_HARDLOCKUP_ENABLED
738 * -------------------|----------------------------------|-------------------------------
739 * proc_soft_watchdog | watchdog_softlockup_user_enabled | WATCHDOG_SOFTOCKUP_ENABLED
ef246a21
UO
740 */
741static int proc_watchdog_common(int which, struct ctl_table *table, int write,
32927393 742 void *buffer, size_t *lenp, loff_t *ppos)
ef246a21 743{
09154985 744 int err, old, *param = table->data;
ef246a21 745
946d1977 746 mutex_lock(&watchdog_mutex);
ef246a21 747
ef246a21 748 if (!write) {
09154985
TG
749 /*
750 * On read synchronize the userspace interface. This is a
751 * racy snapshot.
752 */
753 *param = (watchdog_enabled & which) != 0;
ef246a21
UO
754 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
755 } else {
09154985 756 old = READ_ONCE(*param);
ef246a21 757 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
09154985 758 if (!err && old != READ_ONCE(*param))
d57108d4 759 proc_watchdog_update();
ef246a21 760 }
946d1977 761 mutex_unlock(&watchdog_mutex);
ef246a21
UO
762 return err;
763}
764
83a80a39
UO
765/*
766 * /proc/sys/kernel/watchdog
767 */
768int proc_watchdog(struct ctl_table *table, int write,
32927393 769 void *buffer, size_t *lenp, loff_t *ppos)
83a80a39 770{
df95d308
DA
771 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED |
772 WATCHDOG_SOFTOCKUP_ENABLED,
83a80a39
UO
773 table, write, buffer, lenp, ppos);
774}
775
776/*
777 * /proc/sys/kernel/nmi_watchdog
58687acb 778 */
83a80a39 779int proc_nmi_watchdog(struct ctl_table *table, int write,
32927393 780 void *buffer, size_t *lenp, loff_t *ppos)
83a80a39 781{
df95d308 782 if (!watchdog_hardlockup_available && write)
a994a314 783 return -ENOTSUPP;
df95d308 784 return proc_watchdog_common(WATCHDOG_HARDLOCKUP_ENABLED,
83a80a39
UO
785 table, write, buffer, lenp, ppos);
786}
787
788/*
789 * /proc/sys/kernel/soft_watchdog
790 */
791int proc_soft_watchdog(struct ctl_table *table, int write,
32927393 792 void *buffer, size_t *lenp, loff_t *ppos)
83a80a39 793{
df95d308 794 return proc_watchdog_common(WATCHDOG_SOFTOCKUP_ENABLED,
83a80a39
UO
795 table, write, buffer, lenp, ppos);
796}
58687acb 797
83a80a39
UO
798/*
799 * /proc/sys/kernel/watchdog_thresh
800 */
801int proc_watchdog_thresh(struct ctl_table *table, int write,
32927393 802 void *buffer, size_t *lenp, loff_t *ppos)
58687acb 803{
d57108d4 804 int err, old;
58687acb 805
946d1977 806 mutex_lock(&watchdog_mutex);
bcd951cf 807
d57108d4 808 old = READ_ONCE(watchdog_thresh);
b8900bc0 809 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
83a80a39 810
d57108d4
TG
811 if (!err && write && old != READ_ONCE(watchdog_thresh))
812 proc_watchdog_update();
e04ab2bc 813
946d1977 814 mutex_unlock(&watchdog_mutex);
b8900bc0 815 return err;
58687acb 816}
fe4ba3c3
CM
817
818/*
819 * The cpumask is the mask of possible cpus that the watchdog can run
820 * on, not the mask of cpus it is actually running on. This allows the
821 * user to specify a mask that will include cpus that have not yet
822 * been brought online, if desired.
823 */
824int proc_watchdog_cpumask(struct ctl_table *table, int write,
32927393 825 void *buffer, size_t *lenp, loff_t *ppos)
fe4ba3c3
CM
826{
827 int err;
828
946d1977 829 mutex_lock(&watchdog_mutex);
8c073d27 830
fe4ba3c3 831 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
05ba3de7 832 if (!err && write)
e8b62b2d 833 proc_watchdog_update();
5490125d 834
946d1977 835 mutex_unlock(&watchdog_mutex);
fe4ba3c3
CM
836 return err;
837}
dd0693fd
XN
838
839static const int sixty = 60;
840
841static struct ctl_table watchdog_sysctls[] = {
842 {
843 .procname = "watchdog",
844 .data = &watchdog_user_enabled,
845 .maxlen = sizeof(int),
846 .mode = 0644,
847 .proc_handler = proc_watchdog,
848 .extra1 = SYSCTL_ZERO,
849 .extra2 = SYSCTL_ONE,
850 },
851 {
852 .procname = "watchdog_thresh",
853 .data = &watchdog_thresh,
854 .maxlen = sizeof(int),
855 .mode = 0644,
856 .proc_handler = proc_watchdog_thresh,
857 .extra1 = SYSCTL_ZERO,
858 .extra2 = (void *)&sixty,
859 },
dd0693fd
XN
860 {
861 .procname = "watchdog_cpumask",
862 .data = &watchdog_cpumask_bits,
863 .maxlen = NR_CPUS,
864 .mode = 0644,
865 .proc_handler = proc_watchdog_cpumask,
866 },
867#ifdef CONFIG_SOFTLOCKUP_DETECTOR
868 {
869 .procname = "soft_watchdog",
df95d308 870 .data = &watchdog_softlockup_user_enabled,
dd0693fd
XN
871 .maxlen = sizeof(int),
872 .mode = 0644,
873 .proc_handler = proc_soft_watchdog,
874 .extra1 = SYSCTL_ZERO,
875 .extra2 = SYSCTL_ONE,
876 },
877 {
878 .procname = "softlockup_panic",
879 .data = &softlockup_panic,
880 .maxlen = sizeof(int),
881 .mode = 0644,
882 .proc_handler = proc_dointvec_minmax,
883 .extra1 = SYSCTL_ZERO,
884 .extra2 = SYSCTL_ONE,
885 },
886#ifdef CONFIG_SMP
887 {
888 .procname = "softlockup_all_cpu_backtrace",
889 .data = &sysctl_softlockup_all_cpu_backtrace,
890 .maxlen = sizeof(int),
891 .mode = 0644,
892 .proc_handler = proc_dointvec_minmax,
893 .extra1 = SYSCTL_ZERO,
894 .extra2 = SYSCTL_ONE,
895 },
896#endif /* CONFIG_SMP */
897#endif
898#ifdef CONFIG_HARDLOCKUP_DETECTOR
899 {
900 .procname = "hardlockup_panic",
901 .data = &hardlockup_panic,
902 .maxlen = sizeof(int),
903 .mode = 0644,
904 .proc_handler = proc_dointvec_minmax,
905 .extra1 = SYSCTL_ZERO,
906 .extra2 = SYSCTL_ONE,
907 },
908#ifdef CONFIG_SMP
909 {
910 .procname = "hardlockup_all_cpu_backtrace",
911 .data = &sysctl_hardlockup_all_cpu_backtrace,
912 .maxlen = sizeof(int),
913 .mode = 0644,
914 .proc_handler = proc_dointvec_minmax,
915 .extra1 = SYSCTL_ZERO,
916 .extra2 = SYSCTL_ONE,
917 },
918#endif /* CONFIG_SMP */
919#endif
920 {}
921};
922
9ec272c5
DA
923static struct ctl_table watchdog_hardlockup_sysctl[] = {
924 {
925 .procname = "nmi_watchdog",
926 .data = &watchdog_hardlockup_user_enabled,
927 .maxlen = sizeof(int),
928 .mode = 0444,
929 .proc_handler = proc_nmi_watchdog,
930 .extra1 = SYSCTL_ZERO,
931 .extra2 = SYSCTL_ONE,
932 },
933 {}
934};
935
dd0693fd
XN
936static void __init watchdog_sysctl_init(void)
937{
938 register_sysctl_init("kernel", watchdog_sysctls);
9ec272c5
DA
939
940 if (watchdog_hardlockup_available)
941 watchdog_hardlockup_sysctl[0].mode = 0644;
942 register_sysctl_init("kernel", watchdog_hardlockup_sysctl);
dd0693fd 943}
9ec272c5 944
dd0693fd
XN
945#else
946#define watchdog_sysctl_init() do { } while (0)
58687acb
DZ
947#endif /* CONFIG_SYSCTL */
948
930d8f8d
LC
949static void __init lockup_detector_delay_init(struct work_struct *work);
950static bool allow_lockup_detector_init_retry __initdata;
951
952static struct work_struct detector_work __initdata =
953 __WORK_INITIALIZER(detector_work, lockup_detector_delay_init);
954
955static void __init lockup_detector_delay_init(struct work_struct *work)
956{
957 int ret;
958
959 ret = watchdog_hardlockup_probe();
960 if (ret) {
961 pr_info("Delayed init of the lockup detector failed: %d\n", ret);
962 pr_info("Hard watchdog permanently disabled\n");
963 return;
964 }
965
966 allow_lockup_detector_init_retry = false;
967
968 watchdog_hardlockup_available = true;
969 lockup_detector_setup();
970}
971
972/*
973 * lockup_detector_retry_init - retry init lockup detector if possible.
974 *
975 * Retry hardlockup detector init. It is useful when it requires some
976 * functionality that has to be initialized later on a particular
977 * platform.
978 */
979void __init lockup_detector_retry_init(void)
980{
981 /* Must be called before late init calls */
982 if (!allow_lockup_detector_init_retry)
983 return;
984
985 schedule_work(&detector_work);
986}
987
988/*
989 * Ensure that optional delayed hardlockup init is proceed before
990 * the init code and memory is freed.
991 */
992static int __init lockup_detector_check(void)
993{
994 /* Prevent any later retry. */
995 allow_lockup_detector_init_retry = false;
996
997 /* Make sure no work is pending. */
998 flush_work(&detector_work);
999
9ec272c5
DA
1000 watchdog_sysctl_init();
1001
930d8f8d
LC
1002 return 0;
1003
1004}
1005late_initcall_sync(lockup_detector_check);
1006
004417a6 1007void __init lockup_detector_init(void)
58687acb 1008{
13316b31 1009 if (tick_nohz_full_enabled())
314b08ff 1010 pr_info("Disabling watchdog on nohz_full cores by default\n");
13316b31 1011
de201559 1012 cpumask_copy(&watchdog_cpumask,
04d4e665 1013 housekeeping_cpumask(HK_TYPE_TIMER));
fe4ba3c3 1014
df95d308
DA
1015 if (!watchdog_hardlockup_probe())
1016 watchdog_hardlockup_available = true;
930d8f8d
LC
1017 else
1018 allow_lockup_detector_init_retry = true;
1019
5587185d 1020 lockup_detector_setup();
58687acb 1021}