blob: 94aed27d4ffdca05b359f41842abfc2bc4b6d344 [file] [log] [blame]
Don Zickus58687ac2010-05-07 17:11:44 -04001/*
2 * Detect hard and soft lockups on a system
3 *
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5 *
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -05006 * Note: Most of this code is borrowed heavily from the original softlockup
7 * detector, so thanks to Ingo for the initial implementation.
8 * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
Don Zickus58687ac2010-05-07 17:11:44 -04009 * to those contributors as well.
10 */
11
Andrew Morton45019802012-03-23 15:01:55 -070012#define pr_fmt(fmt) "NMI watchdog: " fmt
13
Don Zickus58687ac2010-05-07 17:11:44 -040014#include <linux/mm.h>
15#include <linux/cpu.h>
16#include <linux/nmi.h>
17#include <linux/init.h>
Don Zickus58687ac2010-05-07 17:11:44 -040018#include <linux/module.h>
19#include <linux/sysctl.h>
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000020#include <linux/smpboot.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060021#include <linux/sched/rt.h>
Chris Metcalffe4ba3c2015-06-24 16:55:45 -070022#include <linux/tick.h>
Tejun Heo82607adc2015-12-08 11:28:04 -050023#include <linux/workqueue.h>
Don Zickus58687ac2010-05-07 17:11:44 -040024
25#include <asm/irq_regs.h>
Eric B Munson5d1c0f42012-03-10 14:37:28 -050026#include <linux/kvm_para.h>
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -070027#include <linux/kthread.h>
Don Zickus58687ac2010-05-07 17:11:44 -040028
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070029/*
30 * The run state of the lockup detectors is controlled by the content of the
31 * 'watchdog_enabled' variable. Each lockup detector has its dedicated bit -
32 * bit 0 for the hard lockup detector and bit 1 for the soft lockup detector.
33 *
34 * 'watchdog_user_enabled', 'nmi_watchdog_enabled' and 'soft_watchdog_enabled'
35 * are variables that are only used as an 'interface' between the parameters
36 * in /proc/sys/kernel and the internal state bits in 'watchdog_enabled'. The
37 * 'watchdog_thresh' variable is handled differently because its value is not
38 * boolean, and the lockup detectors are 'suspended' while 'watchdog_thresh'
39 * is equal zero.
40 */
41#define NMI_WATCHDOG_ENABLED_BIT 0
42#define SOFT_WATCHDOG_ENABLED_BIT 1
43#define NMI_WATCHDOG_ENABLED (1 << NMI_WATCHDOG_ENABLED_BIT)
44#define SOFT_WATCHDOG_ENABLED (1 << SOFT_WATCHDOG_ENABLED_BIT)
45
Peter Zijlstraab992dc2015-05-18 11:31:50 +020046static DEFINE_MUTEX(watchdog_proc_mutex);
47
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070048#ifdef CONFIG_HARDLOCKUP_DETECTOR
49static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED;
50#else
51static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
52#endif
53int __read_mostly nmi_watchdog_enabled;
54int __read_mostly soft_watchdog_enabled;
55int __read_mostly watchdog_user_enabled;
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -070056int __read_mostly watchdog_thresh = 10;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070057
Aaron Tomlined235872014-06-23 13:22:05 -070058#ifdef CONFIG_SMP
59int __read_mostly sysctl_softlockup_all_cpu_backtrace;
Jiri Kosina55537872015-11-05 18:44:41 -080060int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
Aaron Tomlined235872014-06-23 13:22:05 -070061#else
62#define sysctl_softlockup_all_cpu_backtrace 0
Jiri Kosina55537872015-11-05 18:44:41 -080063#define sysctl_hardlockup_all_cpu_backtrace 0
Aaron Tomlined235872014-06-23 13:22:05 -070064#endif
Chris Metcalffe4ba3c2015-06-24 16:55:45 -070065static struct cpumask watchdog_cpumask __read_mostly;
66unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
67
68/* Helper for online, unparked cpus. */
69#define for_each_watchdog_cpu(cpu) \
70 for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
Aaron Tomlined235872014-06-23 13:22:05 -070071
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070072/*
73 * The 'watchdog_running' variable is set to 1 when the watchdog threads
74 * are registered/started and is set to 0 when the watchdog threads are
75 * unregistered/stopped, so it is an indicator whether the threads exist.
76 */
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +020077static int __read_mostly watchdog_running;
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070078/*
79 * If a subsystem has a need to deactivate the watchdog temporarily, it
80 * can use the suspend/resume interface to achieve this. The content of
81 * the 'watchdog_suspended' variable reflects this state. Existing threads
82 * are parked/unparked by the lockup_detector_{suspend|resume} functions
83 * (see comment blocks pertaining to those functions for further details).
84 *
85 * 'watchdog_suspended' also prevents threads from being registered/started
86 * or unregistered/stopped via parameters in /proc/sys/kernel, so the state
87 * of 'watchdog_running' cannot change while the watchdog is deactivated
88 * temporarily (see related code in 'proc' handlers).
89 */
90static int __read_mostly watchdog_suspended;
91
Chuansheng Liu0f34c402012-12-17 15:59:50 -080092static u64 __read_mostly sample_period;
Don Zickus58687ac2010-05-07 17:11:44 -040093
94static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
95static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
96static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
97static DEFINE_PER_CPU(bool, softlockup_touch_sync);
Don Zickus58687ac2010-05-07 17:11:44 -040098static DEFINE_PER_CPU(bool, soft_watchdog_warn);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000099static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
100static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
chai wenb1a8de12014-10-09 15:25:17 -0700101static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
Don Zickus58687ac2010-05-07 17:11:44 -0400102static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
Aaron Tomlined235872014-06-23 13:22:05 -0700103static unsigned long soft_lockup_nmi_warn;
Don Zickus58687ac2010-05-07 17:11:44 -0400104
Don Zickus58687ac2010-05-07 17:11:44 -0400105unsigned int __read_mostly softlockup_panic =
106 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
107
108static int __init softlockup_panic_setup(char *str)
109{
110 softlockup_panic = simple_strtoul(str, NULL, 0);
111
112 return 1;
113}
114__setup("softlockup_panic=", softlockup_panic_setup);
115
116static int __init nowatchdog_setup(char *str)
117{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700118 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -0400119 return 1;
120}
121__setup("nowatchdog", nowatchdog_setup);
122
Don Zickus58687ac2010-05-07 17:11:44 -0400123static int __init nosoftlockup_setup(char *str)
124{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700125 watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
Don Zickus58687ac2010-05-07 17:11:44 -0400126 return 1;
127}
128__setup("nosoftlockup", nosoftlockup_setup);
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700129
Aaron Tomlined235872014-06-23 13:22:05 -0700130#ifdef CONFIG_SMP
131static int __init softlockup_all_cpu_backtrace_setup(char *str)
132{
133 sysctl_softlockup_all_cpu_backtrace =
134 !!simple_strtol(str, NULL, 0);
135 return 1;
136}
137__setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
Jiri Kosina55537872015-11-05 18:44:41 -0800138static int __init hardlockup_all_cpu_backtrace_setup(char *str)
139{
140 sysctl_hardlockup_all_cpu_backtrace =
141 !!simple_strtol(str, NULL, 0);
142 return 1;
143}
144__setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
Aaron Tomlined235872014-06-23 13:22:05 -0700145#endif
Don Zickus58687ac2010-05-07 17:11:44 -0400146
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700147/*
148 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
149 * lockups can have false positives under extreme conditions. So we generally
150 * want a higher threshold for soft lockups than for hard lockups. So we couple
151 * the thresholds with a factor: we make the soft threshold twice the amount of
152 * time the hard threshold is.
153 */
Ingo Molnar6e9101a2011-05-24 05:43:18 +0200154static int get_softlockup_thresh(void)
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700155{
156 return watchdog_thresh * 2;
157}
Don Zickus58687ac2010-05-07 17:11:44 -0400158
159/*
160 * Returns seconds, approximately. We don't need nanosecond
161 * resolution, and we don't need to waste time with a big divide when
162 * 2^30ns == 1.074s.
163 */
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900164static unsigned long get_timestamp(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400165{
Cyril Bur545a2bf2015-02-12 15:01:24 -0800166 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
Don Zickus58687ac2010-05-07 17:11:44 -0400167}
168
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800169static void set_sample_period(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400170{
171 /*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700172 * convert watchdog_thresh from seconds to ns
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -0500173 * the divide by 5 is to give hrtimer several chances (two
174 * or three with the current relation between the soft
175 * and hard thresholds) to increment before the
176 * hardlockup detector generates a warning
Don Zickus58687ac2010-05-07 17:11:44 -0400177 */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800178 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
Don Zickus58687ac2010-05-07 17:11:44 -0400179}
180
181/* Commands for resetting the watchdog */
182static void __touch_watchdog(void)
183{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900184 __this_cpu_write(watchdog_touch_ts, get_timestamp());
Don Zickus58687ac2010-05-07 17:11:44 -0400185}
186
Tejun Heo03e0d462015-12-08 11:28:04 -0500187/**
188 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
189 *
190 * Call when the scheduler may have stalled for legitimate reasons
191 * preventing the watchdog task from executing - e.g. the scheduler
192 * entering idle state. This should only be used for scheduler events.
193 * Use touch_softlockup_watchdog() for everything else.
194 */
195void touch_softlockup_watchdog_sched(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400196{
Andrew Morton78611442014-04-18 15:07:12 -0700197 /*
198 * Preemption can be enabled. It doesn't matter which CPU's timestamp
199 * gets zeroed here, so use the raw_ operation.
200 */
201 raw_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400202}
Tejun Heo03e0d462015-12-08 11:28:04 -0500203
204void touch_softlockup_watchdog(void)
205{
206 touch_softlockup_watchdog_sched();
Tejun Heo82607adc2015-12-08 11:28:04 -0500207 wq_watchdog_touch(raw_smp_processor_id());
Tejun Heo03e0d462015-12-08 11:28:04 -0500208}
Ingo Molnar0167c782010-05-13 08:53:33 +0200209EXPORT_SYMBOL(touch_softlockup_watchdog);
Don Zickus58687ac2010-05-07 17:11:44 -0400210
Don Zickus332fbdb2010-05-07 17:11:45 -0400211void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400212{
213 int cpu;
214
215 /*
216 * this is done lockless
217 * do we care if a 0 races with a timestamp?
218 * all it means is the softlock check starts one cycle later
219 */
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700220 for_each_watchdog_cpu(cpu)
Don Zickus58687ac2010-05-07 17:11:44 -0400221 per_cpu(watchdog_touch_ts, cpu) = 0;
Tejun Heo82607adc2015-12-08 11:28:04 -0500222 wq_watchdog_touch(-1);
Don Zickus58687ac2010-05-07 17:11:44 -0400223}
224
Don Zickus58687ac2010-05-07 17:11:44 -0400225void touch_softlockup_watchdog_sync(void)
226{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500227 __this_cpu_write(softlockup_touch_sync, true);
228 __this_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400229}
230
Don Zickus58687ac2010-05-07 17:11:44 -0400231/* watchdog detector functions */
Yaowei Bai451637e2015-11-05 18:44:24 -0800232static bool is_hardlockup(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400233{
Christoph Lameter909ea962010-12-08 16:22:55 +0100234 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400235
Christoph Lameter909ea962010-12-08 16:22:55 +0100236 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
Yaowei Bai451637e2015-11-05 18:44:24 -0800237 return true;
Don Zickus58687ac2010-05-07 17:11:44 -0400238
Christoph Lameter909ea962010-12-08 16:22:55 +0100239 __this_cpu_write(hrtimer_interrupts_saved, hrint);
Yaowei Bai451637e2015-11-05 18:44:24 -0800240 return false;
Don Zickus58687ac2010-05-07 17:11:44 -0400241}
Don Zickus58687ac2010-05-07 17:11:44 -0400242
Don Zickus26e09c62010-05-17 18:06:04 -0400243static int is_softlockup(unsigned long touch_ts)
Don Zickus58687ac2010-05-07 17:11:44 -0400244{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900245 unsigned long now = get_timestamp();
Don Zickus58687ac2010-05-07 17:11:44 -0400246
Ulrich Obergfell39d2da22015-11-05 18:44:56 -0800247 if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700248 /* Warn about unreasonable delays. */
249 if (time_after(now, touch_ts + get_softlockup_thresh()))
250 return now - touch_ts;
251 }
Don Zickus58687ac2010-05-07 17:11:44 -0400252 return 0;
253}
254
Don Zickus58687ac2010-05-07 17:11:44 -0400255static void watchdog_interrupt_count(void)
256{
Christoph Lameter909ea962010-12-08 16:22:55 +0100257 __this_cpu_inc(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400258}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000259
Babu Mogerb969a242016-12-14 15:06:24 -0800260/*
261 * These two functions are mostly architecture specific
262 * defining them as weak here.
263 */
264int __weak watchdog_nmi_enable(unsigned int cpu)
265{
266 return 0;
267}
268void __weak watchdog_nmi_disable(unsigned int cpu)
269{
270}
Don Zickus58687ac2010-05-07 17:11:44 -0400271
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800272static int watchdog_enable_all_cpus(void);
273static void watchdog_disable_all_cpus(void);
274
Don Zickus58687ac2010-05-07 17:11:44 -0400275/* watchdog kicker functions */
276static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
277{
Christoph Lameter909ea962010-12-08 16:22:55 +0100278 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400279 struct pt_regs *regs = get_irq_regs();
280 int duration;
Aaron Tomlined235872014-06-23 13:22:05 -0700281 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
Don Zickus58687ac2010-05-07 17:11:44 -0400282
283 /* kick the hardlockup detector */
284 watchdog_interrupt_count();
285
286 /* kick the softlockup detector */
Christoph Lameter909ea962010-12-08 16:22:55 +0100287 wake_up_process(__this_cpu_read(softlockup_watchdog));
Don Zickus58687ac2010-05-07 17:11:44 -0400288
289 /* .. and repeat */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800290 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
Don Zickus58687ac2010-05-07 17:11:44 -0400291
292 if (touch_ts == 0) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100293 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
Don Zickus58687ac2010-05-07 17:11:44 -0400294 /*
295 * If the time stamp was touched atomically
296 * make sure the scheduler tick is up to date.
297 */
Christoph Lameter909ea962010-12-08 16:22:55 +0100298 __this_cpu_write(softlockup_touch_sync, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400299 sched_clock_tick();
300 }
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500301
302 /* Clear the guest paused flag on watchdog reset */
303 kvm_check_and_clear_guest_paused();
Don Zickus58687ac2010-05-07 17:11:44 -0400304 __touch_watchdog();
305 return HRTIMER_RESTART;
306 }
307
308 /* check for a softlockup
309 * This is done by making sure a high priority task is
310 * being scheduled. The task touches the watchdog to
311 * indicate it is getting cpu time. If it hasn't then
312 * this is a good indication some task is hogging the cpu
313 */
Don Zickus26e09c62010-05-17 18:06:04 -0400314 duration = is_softlockup(touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400315 if (unlikely(duration)) {
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500316 /*
317 * If a virtual machine is stopped by the host it can look to
318 * the watchdog like a soft lockup, check to see if the host
319 * stopped the vm before we issue the warning
320 */
321 if (kvm_check_and_clear_guest_paused())
322 return HRTIMER_RESTART;
323
Don Zickus58687ac2010-05-07 17:11:44 -0400324 /* only warn once */
chai wenb1a8de12014-10-09 15:25:17 -0700325 if (__this_cpu_read(soft_watchdog_warn) == true) {
326 /*
327 * When multiple processes are causing softlockups the
328 * softlockup detector only warns on the first one
329 * because the code relies on a full quiet cycle to
330 * re-arm. The second process prevents the quiet cycle
331 * and never gets reported. Use task pointers to detect
332 * this.
333 */
334 if (__this_cpu_read(softlockup_task_ptr_saved) !=
335 current) {
336 __this_cpu_write(soft_watchdog_warn, false);
337 __touch_watchdog();
338 }
Don Zickus58687ac2010-05-07 17:11:44 -0400339 return HRTIMER_RESTART;
chai wenb1a8de12014-10-09 15:25:17 -0700340 }
Don Zickus58687ac2010-05-07 17:11:44 -0400341
Aaron Tomlined235872014-06-23 13:22:05 -0700342 if (softlockup_all_cpu_backtrace) {
343 /* Prevent multiple soft-lockup reports if one cpu is already
344 * engaged in dumping cpu back traces
345 */
346 if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
347 /* Someone else will report us. Let's give up */
348 __this_cpu_write(soft_watchdog_warn, true);
349 return HRTIMER_RESTART;
350 }
351 }
352
Fabian Frederick656c3b72014-08-06 16:04:03 -0700353 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
Don Zickus26e09c62010-05-17 18:06:04 -0400354 smp_processor_id(), duration,
Don Zickus58687ac2010-05-07 17:11:44 -0400355 current->comm, task_pid_nr(current));
chai wenb1a8de12014-10-09 15:25:17 -0700356 __this_cpu_write(softlockup_task_ptr_saved, current);
Don Zickus58687ac2010-05-07 17:11:44 -0400357 print_modules();
358 print_irqtrace_events(current);
359 if (regs)
360 show_regs(regs);
361 else
362 dump_stack();
363
Aaron Tomlined235872014-06-23 13:22:05 -0700364 if (softlockup_all_cpu_backtrace) {
365 /* Avoid generating two back traces for current
366 * given that one is already made above
367 */
368 trigger_allbutself_cpu_backtrace();
369
370 clear_bit(0, &soft_lockup_nmi_warn);
371 /* Barrier to sync with other cpus */
372 smp_mb__after_atomic();
373 }
374
Josh Hunt69361ee2014-08-08 14:22:31 -0700375 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
Don Zickus58687ac2010-05-07 17:11:44 -0400376 if (softlockup_panic)
377 panic("softlockup: hung tasks");
Christoph Lameter909ea962010-12-08 16:22:55 +0100378 __this_cpu_write(soft_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400379 } else
Christoph Lameter909ea962010-12-08 16:22:55 +0100380 __this_cpu_write(soft_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400381
382 return HRTIMER_RESTART;
383}
384
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000385static void watchdog_set_prio(unsigned int policy, unsigned int prio)
Don Zickus58687ac2010-05-07 17:11:44 -0400386{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000387 struct sched_param param = { .sched_priority = prio };
388
389 sched_setscheduler(current, policy, &param);
390}
391
392static void watchdog_enable(unsigned int cpu)
393{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500394 struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
Don Zickus58687ac2010-05-07 17:11:44 -0400395
Bjørn Mork3935e8952012-12-19 20:51:31 +0100396 /* kick off the timer for the hardlockup detector */
397 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
398 hrtimer->function = watchdog_timer_fn;
399
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000400 /* Enable the perf event */
401 watchdog_nmi_enable(cpu);
Don Zickus58687ac2010-05-07 17:11:44 -0400402
Don Zickus58687ac2010-05-07 17:11:44 -0400403 /* done here because hrtimer_start can only pin to smp_processor_id() */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800404 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
Don Zickus58687ac2010-05-07 17:11:44 -0400405 HRTIMER_MODE_REL_PINNED);
406
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000407 /* initialize timestamp */
408 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
409 __touch_watchdog();
Don Zickus58687ac2010-05-07 17:11:44 -0400410}
411
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000412static void watchdog_disable(unsigned int cpu)
413{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500414 struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000415
416 watchdog_set_prio(SCHED_NORMAL, 0);
417 hrtimer_cancel(hrtimer);
418 /* disable the perf event */
419 watchdog_nmi_disable(cpu);
420}
421
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200422static void watchdog_cleanup(unsigned int cpu, bool online)
423{
424 watchdog_disable(cpu);
425}
426
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000427static int watchdog_should_run(unsigned int cpu)
428{
429 return __this_cpu_read(hrtimer_interrupts) !=
430 __this_cpu_read(soft_lockup_hrtimer_cnt);
431}
432
433/*
434 * The watchdog thread function - touches the timestamp.
435 *
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800436 * It only runs once every sample_period seconds (4 seconds by
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000437 * default) to reset the softlockup timestamp. If this gets delayed
438 * for more than 2*watchdog_thresh seconds then the debug-printout
439 * triggers in watchdog_timer_fn().
440 */
441static void watchdog(unsigned int cpu)
442{
443 __this_cpu_write(soft_lockup_hrtimer_cnt,
444 __this_cpu_read(hrtimer_interrupts));
445 __touch_watchdog();
Ulrich Obergfellbcfba4f2015-04-14 15:44:10 -0700446
447 /*
448 * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
449 * failure path. Check for failures that can occur asynchronously -
450 * for example, when CPUs are on-lined - and shut down the hardware
451 * perf event on each CPU accordingly.
452 *
453 * The only non-obvious place this bit can be cleared is through
454 * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a
455 * pr_info here would be too noisy as it would result in a message
456 * every few seconds if the hardlockup was disabled but the softlockup
457 * enabled.
458 */
459 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
460 watchdog_nmi_disable(cpu);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000461}
Don Zickus58687ac2010-05-07 17:11:44 -0400462
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200463static struct smp_hotplug_thread watchdog_threads = {
464 .store = &softlockup_watchdog,
465 .thread_should_run = watchdog_should_run,
466 .thread_fn = watchdog,
467 .thread_comm = "watchdog/%u",
468 .setup = watchdog_enable,
469 .cleanup = watchdog_cleanup,
470 .park = watchdog_disable,
471 .unpark = watchdog_enable,
472};
473
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700474/*
475 * park all watchdog threads that are specified in 'watchdog_cpumask'
Ulrich Obergfellee7fed52015-11-05 18:44:39 -0800476 *
477 * This function returns an error if kthread_park() of a watchdog thread
478 * fails. In this situation, the watchdog threads of some CPUs can already
479 * be parked and the watchdog threads of other CPUs can still be runnable.
480 * Callers are expected to handle this special condition as appropriate in
481 * their context.
Ulrich Obergfella2a45b82015-11-05 18:44:53 -0800482 *
483 * This function may only be called in a context that is protected against
484 * races with CPU hotplug - for example, via get_online_cpus().
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700485 */
486static int watchdog_park_threads(void)
487{
488 int cpu, ret = 0;
489
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700490 for_each_watchdog_cpu(cpu) {
491 ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
492 if (ret)
493 break;
494 }
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700495
496 return ret;
497}
498
499/*
500 * unpark all watchdog threads that are specified in 'watchdog_cpumask'
Ulrich Obergfella2a45b82015-11-05 18:44:53 -0800501 *
502 * This function may only be called in a context that is protected against
503 * races with CPU hotplug - for example, via get_online_cpus().
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700504 */
505static void watchdog_unpark_threads(void)
506{
507 int cpu;
508
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700509 for_each_watchdog_cpu(cpu)
510 kthread_unpark(per_cpu(softlockup_watchdog, cpu));
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700511}
512
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700513/*
514 * Suspend the hard and soft lockup detector by parking the watchdog threads.
515 */
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700516int lockup_detector_suspend(void)
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700517{
518 int ret = 0;
519
Ulrich Obergfellee89e712015-11-05 18:44:47 -0800520 get_online_cpus();
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700521 mutex_lock(&watchdog_proc_mutex);
522 /*
523 * Multiple suspend requests can be active in parallel (counted by
524 * the 'watchdog_suspended' variable). If the watchdog threads are
525 * running, the first caller takes care that they will be parked.
526 * The state of 'watchdog_running' cannot change while a suspend
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700527 * request is active (see related code in 'proc' handlers).
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700528 */
529 if (watchdog_running && !watchdog_suspended)
530 ret = watchdog_park_threads();
531
532 if (ret == 0)
533 watchdog_suspended++;
Ulrich Obergfellc9935902015-11-05 18:44:36 -0800534 else {
535 watchdog_disable_all_cpus();
536 pr_err("Failed to suspend lockup detectors, disabled\n");
537 watchdog_enabled = 0;
538 }
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700539
540 mutex_unlock(&watchdog_proc_mutex);
541
542 return ret;
543}
544
545/*
546 * Resume the hard and soft lockup detector by unparking the watchdog threads.
547 */
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700548void lockup_detector_resume(void)
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700549{
550 mutex_lock(&watchdog_proc_mutex);
551
552 watchdog_suspended--;
553 /*
554 * The watchdog threads are unparked if they were previously running
555 * and if there is no more active suspend request.
556 */
557 if (watchdog_running && !watchdog_suspended)
558 watchdog_unpark_threads();
559
560 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfellee89e712015-11-05 18:44:47 -0800561 put_online_cpus();
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700562}
563
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800564static int update_watchdog_all_cpus(void)
Michal Hocko9809b182013-09-24 15:27:30 -0700565{
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800566 int ret;
567
568 ret = watchdog_park_threads();
569 if (ret)
570 return ret;
571
Ulrich Obergfelld4bdd0b2015-09-04 15:45:21 -0700572 watchdog_unpark_threads();
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800573
574 return 0;
Michal Hocko9809b182013-09-24 15:27:30 -0700575}
576
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700577static int watchdog_enable_all_cpus(void)
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200578{
579 int err = 0;
580
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200581 if (!watchdog_running) {
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700582 err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
583 &watchdog_cpumask);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200584 if (err)
585 pr_err("Failed to create watchdog threads, disabled\n");
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700586 else
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200587 watchdog_running = 1;
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700588 } else {
589 /*
590 * Enable/disable the lockup detectors or
591 * change the sample period 'on the fly'.
592 */
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800593 err = update_watchdog_all_cpus();
594
595 if (err) {
596 watchdog_disable_all_cpus();
597 pr_err("Failed to update lockup detectors, disabled\n");
598 }
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200599 }
600
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800601 if (err)
602 watchdog_enabled = 0;
603
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200604 return err;
605}
606
Don Zickus58687ac2010-05-07 17:11:44 -0400607static void watchdog_disable_all_cpus(void)
608{
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200609 if (watchdog_running) {
610 watchdog_running = 0;
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200611 smpboot_unregister_percpu_thread(&watchdog_threads);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000612 }
Don Zickus58687ac2010-05-07 17:11:44 -0400613}
614
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800615#ifdef CONFIG_SYSCTL
616
Don Zickus58687ac2010-05-07 17:11:44 -0400617/*
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700618 * Update the run state of the lockup detectors.
Don Zickus58687ac2010-05-07 17:11:44 -0400619 */
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700620static int proc_watchdog_update(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400621{
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700622 int err = 0;
623
624 /*
625 * Watchdog threads won't be started if they are already active.
626 * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
627 * care of this. If those threads are already active, the sample
628 * period will be updated and the lockup detectors will be enabled
629 * or disabled 'on the fly'.
630 */
631 if (watchdog_enabled && watchdog_thresh)
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700632 err = watchdog_enable_all_cpus();
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700633 else
634 watchdog_disable_all_cpus();
635
636 return err;
637
638}
639
640/*
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700641 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
642 *
643 * caller | table->data points to | 'which' contains the flag(s)
644 * -------------------|-----------------------|-----------------------------
645 * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
646 * | | with SOFT_WATCHDOG_ENABLED
647 * -------------------|-----------------------|-----------------------------
648 * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED
649 * -------------------|-----------------------|-----------------------------
650 * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
651 */
652static int proc_watchdog_common(int which, struct ctl_table *table, int write,
653 void __user *buffer, size_t *lenp, loff_t *ppos)
654{
655 int err, old, new;
656 int *watchdog_param = (int *)table->data;
Don Zickus58687ac2010-05-07 17:11:44 -0400657
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800658 get_online_cpus();
Michal Hocko359e6fa2013-09-24 15:27:29 -0700659 mutex_lock(&watchdog_proc_mutex);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000660
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700661 if (watchdog_suspended) {
662 /* no parameter changes allowed while watchdog is suspended */
663 err = -EAGAIN;
664 goto out;
665 }
666
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700667 /*
668 * If the parameter is being read return the state of the corresponding
669 * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
670 * run state of the lockup detectors.
671 */
672 if (!write) {
673 *watchdog_param = (watchdog_enabled & which) != 0;
674 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
675 } else {
676 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
677 if (err)
678 goto out;
679
680 /*
681 * There is a race window between fetching the current value
682 * from 'watchdog_enabled' and storing the new value. During
683 * this race window, watchdog_nmi_enable() can sneak in and
684 * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
685 * The 'cmpxchg' detects this race and the loop retries.
686 */
687 do {
688 old = watchdog_enabled;
689 /*
690 * If the parameter value is not zero set the
691 * corresponding bit(s), else clear it(them).
692 */
693 if (*watchdog_param)
694 new = old | which;
695 else
696 new = old & ~which;
697 } while (cmpxchg(&watchdog_enabled, old, new) != old);
698
699 /*
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800700 * Update the run state of the lockup detectors. There is _no_
701 * need to check the value returned by proc_watchdog_update()
702 * and to restore the previous value of 'watchdog_enabled' as
703 * both lockup detectors are disabled if proc_watchdog_update()
704 * returns an error.
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700705 */
Joshua Hunta1ee1932016-03-17 14:17:23 -0700706 if (old == new)
707 goto out;
708
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700709 err = proc_watchdog_update();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700710 }
711out:
712 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800713 put_online_cpus();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700714 return err;
715}
716
717/*
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700718 * /proc/sys/kernel/watchdog
719 */
720int proc_watchdog(struct ctl_table *table, int write,
721 void __user *buffer, size_t *lenp, loff_t *ppos)
722{
723 return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
724 table, write, buffer, lenp, ppos);
725}
726
727/*
728 * /proc/sys/kernel/nmi_watchdog
729 */
730int proc_nmi_watchdog(struct ctl_table *table, int write,
731 void __user *buffer, size_t *lenp, loff_t *ppos)
732{
733 return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
734 table, write, buffer, lenp, ppos);
735}
736
737/*
738 * /proc/sys/kernel/soft_watchdog
739 */
740int proc_soft_watchdog(struct ctl_table *table, int write,
741 void __user *buffer, size_t *lenp, loff_t *ppos)
742{
743 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
744 table, write, buffer, lenp, ppos);
745}
746
747/*
748 * /proc/sys/kernel/watchdog_thresh
749 */
750int proc_watchdog_thresh(struct ctl_table *table, int write,
751 void __user *buffer, size_t *lenp, loff_t *ppos)
752{
Joshua Hunta1ee1932016-03-17 14:17:23 -0700753 int err, old, new;
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700754
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800755 get_online_cpus();
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700756 mutex_lock(&watchdog_proc_mutex);
757
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700758 if (watchdog_suspended) {
759 /* no parameter changes allowed while watchdog is suspended */
760 err = -EAGAIN;
761 goto out;
762 }
763
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700764 old = ACCESS_ONCE(watchdog_thresh);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200765 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700766
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200767 if (err || !write)
Michal Hocko359e6fa2013-09-24 15:27:29 -0700768 goto out;
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700769
anish kumarb66a2352013-03-12 14:44:08 -0400770 /*
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800771 * Update the sample period. Restore on failure.
anish kumarb66a2352013-03-12 14:44:08 -0400772 */
Joshua Hunta1ee1932016-03-17 14:17:23 -0700773 new = ACCESS_ONCE(watchdog_thresh);
774 if (old == new)
775 goto out;
776
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700777 set_sample_period();
778 err = proc_watchdog_update();
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800779 if (err) {
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700780 watchdog_thresh = old;
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800781 set_sample_period();
782 }
Michal Hocko359e6fa2013-09-24 15:27:29 -0700783out:
784 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800785 put_online_cpus();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200786 return err;
Don Zickus58687ac2010-05-07 17:11:44 -0400787}
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700788
789/*
790 * The cpumask is the mask of possible cpus that the watchdog can run
791 * on, not the mask of cpus it is actually running on. This allows the
792 * user to specify a mask that will include cpus that have not yet
793 * been brought online, if desired.
794 */
795int proc_watchdog_cpumask(struct ctl_table *table, int write,
796 void __user *buffer, size_t *lenp, loff_t *ppos)
797{
798 int err;
799
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800800 get_online_cpus();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700801 mutex_lock(&watchdog_proc_mutex);
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700802
803 if (watchdog_suspended) {
804 /* no parameter changes allowed while watchdog is suspended */
805 err = -EAGAIN;
806 goto out;
807 }
808
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700809 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
810 if (!err && write) {
811 /* Remove impossible cpus to keep sysctl output cleaner. */
812 cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
813 cpu_possible_mask);
814
815 if (watchdog_running) {
816 /*
817 * Failure would be due to being unable to allocate
818 * a temporary cpumask, so we are likely not in a
819 * position to do much else to make things better.
820 */
821 if (smpboot_update_cpumask_percpu_thread(
822 &watchdog_threads, &watchdog_cpumask) != 0)
823 pr_err("cpumask update failed\n");
824 }
825 }
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700826out:
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700827 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800828 put_online_cpus();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700829 return err;
830}
831
Don Zickus58687ac2010-05-07 17:11:44 -0400832#endif /* CONFIG_SYSCTL */
833
Peter Zijlstra004417a2010-11-25 18:38:29 +0100834void __init lockup_detector_init(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400835{
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800836 set_sample_period();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200837
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700838#ifdef CONFIG_NO_HZ_FULL
839 if (tick_nohz_full_enabled()) {
Frederic Weisbecker314b08ff2015-09-04 15:45:09 -0700840 pr_info("Disabling watchdog on nohz_full cores by default\n");
841 cpumask_copy(&watchdog_cpumask, housekeeping_mask);
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700842 } else
843 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
844#else
845 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
846#endif
847
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700848 if (watchdog_enabled)
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700849 watchdog_enable_all_cpus();
Don Zickus58687ac2010-05-07 17:11:44 -0400850}