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