blob: d4b0fa01cae39cd720661d7a62f50a7926f9db69 [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
Peter Zijlstraab992dc2015-05-18 11:31:50 +020029static DEFINE_MUTEX(watchdog_proc_mutex);
30
Babu Moger0ce66ee2016-12-14 15:06:21 -080031#if defined(CONFIG_HAVE_NMI_WATCHDOG) || defined(CONFIG_HARDLOCKUP_DETECTOR)
32unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070033#else
Babu Moger0ce66ee2016-12-14 15:06:21 -080034unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070035#endif
36int __read_mostly nmi_watchdog_enabled;
37int __read_mostly soft_watchdog_enabled;
38int __read_mostly watchdog_user_enabled;
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -070039int __read_mostly watchdog_thresh = 10;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070040
Aaron Tomlined235872014-06-23 13:22:05 -070041#ifdef CONFIG_SMP
42int __read_mostly sysctl_softlockup_all_cpu_backtrace;
Jiri Kosina55537872015-11-05 18:44:41 -080043int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
Aaron Tomlined235872014-06-23 13:22:05 -070044#endif
Chris Metcalffe4ba3c2015-06-24 16:55:45 -070045static struct cpumask watchdog_cpumask __read_mostly;
46unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
47
48/* Helper for online, unparked cpus. */
49#define for_each_watchdog_cpu(cpu) \
50 for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
Aaron Tomlined235872014-06-23 13:22:05 -070051
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070052/*
53 * The 'watchdog_running' variable is set to 1 when the watchdog threads
54 * are registered/started and is set to 0 when the watchdog threads are
55 * unregistered/stopped, so it is an indicator whether the threads exist.
56 */
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +020057static int __read_mostly watchdog_running;
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070058/*
59 * If a subsystem has a need to deactivate the watchdog temporarily, it
60 * can use the suspend/resume interface to achieve this. The content of
61 * the 'watchdog_suspended' variable reflects this state. Existing threads
62 * are parked/unparked by the lockup_detector_{suspend|resume} functions
63 * (see comment blocks pertaining to those functions for further details).
64 *
65 * 'watchdog_suspended' also prevents threads from being registered/started
66 * or unregistered/stopped via parameters in /proc/sys/kernel, so the state
67 * of 'watchdog_running' cannot change while the watchdog is deactivated
68 * temporarily (see related code in 'proc' handlers).
69 */
70static int __read_mostly watchdog_suspended;
71
Chuansheng Liu0f34c402012-12-17 15:59:50 -080072static u64 __read_mostly sample_period;
Don Zickus58687ac2010-05-07 17:11:44 -040073
74static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
75static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
76static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
77static DEFINE_PER_CPU(bool, softlockup_touch_sync);
Don Zickus58687ac2010-05-07 17:11:44 -040078static DEFINE_PER_CPU(bool, soft_watchdog_warn);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000079static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
80static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
chai wenb1a8de12014-10-09 15:25:17 -070081static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
Don Zickus58687ac2010-05-07 17:11:44 -040082static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
Aaron Tomlined235872014-06-23 13:22:05 -070083static unsigned long soft_lockup_nmi_warn;
Don Zickus58687ac2010-05-07 17:11:44 -040084
Don Zickus58687ac2010-05-07 17:11:44 -040085unsigned int __read_mostly softlockup_panic =
86 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
87
88static int __init softlockup_panic_setup(char *str)
89{
90 softlockup_panic = simple_strtoul(str, NULL, 0);
91
92 return 1;
93}
94__setup("softlockup_panic=", softlockup_panic_setup);
95
96static int __init nowatchdog_setup(char *str)
97{
Ulrich Obergfell195daf62015-04-14 15:44:13 -070098 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -040099 return 1;
100}
101__setup("nowatchdog", nowatchdog_setup);
102
Don Zickus58687ac2010-05-07 17:11:44 -0400103static int __init nosoftlockup_setup(char *str)
104{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700105 watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
Don Zickus58687ac2010-05-07 17:11:44 -0400106 return 1;
107}
108__setup("nosoftlockup", nosoftlockup_setup);
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700109
Aaron Tomlined235872014-06-23 13:22:05 -0700110#ifdef CONFIG_SMP
111static int __init softlockup_all_cpu_backtrace_setup(char *str)
112{
113 sysctl_softlockup_all_cpu_backtrace =
114 !!simple_strtol(str, NULL, 0);
115 return 1;
116}
117__setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
Jiri Kosina55537872015-11-05 18:44:41 -0800118static int __init hardlockup_all_cpu_backtrace_setup(char *str)
119{
120 sysctl_hardlockup_all_cpu_backtrace =
121 !!simple_strtol(str, NULL, 0);
122 return 1;
123}
124__setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
Aaron Tomlined235872014-06-23 13:22:05 -0700125#endif
Don Zickus58687ac2010-05-07 17:11:44 -0400126
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700127/*
128 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
129 * lockups can have false positives under extreme conditions. So we generally
130 * want a higher threshold for soft lockups than for hard lockups. So we couple
131 * the thresholds with a factor: we make the soft threshold twice the amount of
132 * time the hard threshold is.
133 */
Ingo Molnar6e9101a2011-05-24 05:43:18 +0200134static int get_softlockup_thresh(void)
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700135{
136 return watchdog_thresh * 2;
137}
Don Zickus58687ac2010-05-07 17:11:44 -0400138
139/*
140 * Returns seconds, approximately. We don't need nanosecond
141 * resolution, and we don't need to waste time with a big divide when
142 * 2^30ns == 1.074s.
143 */
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900144static unsigned long get_timestamp(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400145{
Cyril Bur545a2bf2015-02-12 15:01:24 -0800146 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
Don Zickus58687ac2010-05-07 17:11:44 -0400147}
148
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800149static void set_sample_period(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400150{
151 /*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700152 * convert watchdog_thresh from seconds to ns
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -0500153 * the divide by 5 is to give hrtimer several chances (two
154 * or three with the current relation between the soft
155 * and hard thresholds) to increment before the
156 * hardlockup detector generates a warning
Don Zickus58687ac2010-05-07 17:11:44 -0400157 */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800158 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
Don Zickus58687ac2010-05-07 17:11:44 -0400159}
160
161/* Commands for resetting the watchdog */
162static void __touch_watchdog(void)
163{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900164 __this_cpu_write(watchdog_touch_ts, get_timestamp());
Don Zickus58687ac2010-05-07 17:11:44 -0400165}
166
Tejun Heo03e0d462015-12-08 11:28:04 -0500167/**
168 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
169 *
170 * Call when the scheduler may have stalled for legitimate reasons
171 * preventing the watchdog task from executing - e.g. the scheduler
172 * entering idle state. This should only be used for scheduler events.
173 * Use touch_softlockup_watchdog() for everything else.
174 */
175void touch_softlockup_watchdog_sched(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400176{
Andrew Morton78611442014-04-18 15:07:12 -0700177 /*
178 * Preemption can be enabled. It doesn't matter which CPU's timestamp
179 * gets zeroed here, so use the raw_ operation.
180 */
181 raw_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400182}
Tejun Heo03e0d462015-12-08 11:28:04 -0500183
184void touch_softlockup_watchdog(void)
185{
186 touch_softlockup_watchdog_sched();
Tejun Heo82607adc2015-12-08 11:28:04 -0500187 wq_watchdog_touch(raw_smp_processor_id());
Tejun Heo03e0d462015-12-08 11:28:04 -0500188}
Ingo Molnar0167c782010-05-13 08:53:33 +0200189EXPORT_SYMBOL(touch_softlockup_watchdog);
Don Zickus58687ac2010-05-07 17:11:44 -0400190
Don Zickus332fbdb2010-05-07 17:11:45 -0400191void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400192{
193 int cpu;
194
195 /*
196 * this is done lockless
197 * do we care if a 0 races with a timestamp?
198 * all it means is the softlock check starts one cycle later
199 */
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700200 for_each_watchdog_cpu(cpu)
Don Zickus58687ac2010-05-07 17:11:44 -0400201 per_cpu(watchdog_touch_ts, cpu) = 0;
Tejun Heo82607adc2015-12-08 11:28:04 -0500202 wq_watchdog_touch(-1);
Don Zickus58687ac2010-05-07 17:11:44 -0400203}
204
Don Zickus58687ac2010-05-07 17:11:44 -0400205void touch_softlockup_watchdog_sync(void)
206{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500207 __this_cpu_write(softlockup_touch_sync, true);
208 __this_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400209}
210
Don Zickus58687ac2010-05-07 17:11:44 -0400211/* watchdog detector functions */
Babu Moger0ce66ee2016-12-14 15:06:21 -0800212bool is_hardlockup(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400213{
Christoph Lameter909ea962010-12-08 16:22:55 +0100214 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400215
Christoph Lameter909ea962010-12-08 16:22:55 +0100216 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
Yaowei Bai451637e2015-11-05 18:44:24 -0800217 return true;
Don Zickus58687ac2010-05-07 17:11:44 -0400218
Christoph Lameter909ea962010-12-08 16:22:55 +0100219 __this_cpu_write(hrtimer_interrupts_saved, hrint);
Yaowei Bai451637e2015-11-05 18:44:24 -0800220 return false;
Don Zickus58687ac2010-05-07 17:11:44 -0400221}
Don Zickus58687ac2010-05-07 17:11:44 -0400222
Don Zickus26e09c62010-05-17 18:06:04 -0400223static int is_softlockup(unsigned long touch_ts)
Don Zickus58687ac2010-05-07 17:11:44 -0400224{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900225 unsigned long now = get_timestamp();
Don Zickus58687ac2010-05-07 17:11:44 -0400226
Ulrich Obergfell39d2da22015-11-05 18:44:56 -0800227 if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700228 /* Warn about unreasonable delays. */
229 if (time_after(now, touch_ts + get_softlockup_thresh()))
230 return now - touch_ts;
231 }
Don Zickus58687ac2010-05-07 17:11:44 -0400232 return 0;
233}
234
Don Zickus58687ac2010-05-07 17:11:44 -0400235static void watchdog_interrupt_count(void)
236{
Christoph Lameter909ea962010-12-08 16:22:55 +0100237 __this_cpu_inc(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400238}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000239
Babu Mogerb969a242016-12-14 15:06:24 -0800240/*
241 * These two functions are mostly architecture specific
242 * defining them as weak here.
243 */
244int __weak watchdog_nmi_enable(unsigned int cpu)
245{
246 return 0;
247}
248void __weak watchdog_nmi_disable(unsigned int cpu)
249{
250}
Don Zickus58687ac2010-05-07 17:11:44 -0400251
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800252static int watchdog_enable_all_cpus(void);
253static void watchdog_disable_all_cpus(void);
254
Don Zickus58687ac2010-05-07 17:11:44 -0400255/* watchdog kicker functions */
256static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
257{
Christoph Lameter909ea962010-12-08 16:22:55 +0100258 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400259 struct pt_regs *regs = get_irq_regs();
260 int duration;
Aaron Tomlined235872014-06-23 13:22:05 -0700261 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
Don Zickus58687ac2010-05-07 17:11:44 -0400262
263 /* kick the hardlockup detector */
264 watchdog_interrupt_count();
265
266 /* kick the softlockup detector */
Christoph Lameter909ea962010-12-08 16:22:55 +0100267 wake_up_process(__this_cpu_read(softlockup_watchdog));
Don Zickus58687ac2010-05-07 17:11:44 -0400268
269 /* .. and repeat */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800270 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
Don Zickus58687ac2010-05-07 17:11:44 -0400271
272 if (touch_ts == 0) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100273 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
Don Zickus58687ac2010-05-07 17:11:44 -0400274 /*
275 * If the time stamp was touched atomically
276 * make sure the scheduler tick is up to date.
277 */
Christoph Lameter909ea962010-12-08 16:22:55 +0100278 __this_cpu_write(softlockup_touch_sync, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400279 sched_clock_tick();
280 }
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500281
282 /* Clear the guest paused flag on watchdog reset */
283 kvm_check_and_clear_guest_paused();
Don Zickus58687ac2010-05-07 17:11:44 -0400284 __touch_watchdog();
285 return HRTIMER_RESTART;
286 }
287
288 /* check for a softlockup
289 * This is done by making sure a high priority task is
290 * being scheduled. The task touches the watchdog to
291 * indicate it is getting cpu time. If it hasn't then
292 * this is a good indication some task is hogging the cpu
293 */
Don Zickus26e09c62010-05-17 18:06:04 -0400294 duration = is_softlockup(touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400295 if (unlikely(duration)) {
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500296 /*
297 * If a virtual machine is stopped by the host it can look to
298 * the watchdog like a soft lockup, check to see if the host
299 * stopped the vm before we issue the warning
300 */
301 if (kvm_check_and_clear_guest_paused())
302 return HRTIMER_RESTART;
303
Don Zickus58687ac2010-05-07 17:11:44 -0400304 /* only warn once */
chai wenb1a8de12014-10-09 15:25:17 -0700305 if (__this_cpu_read(soft_watchdog_warn) == true) {
306 /*
307 * When multiple processes are causing softlockups the
308 * softlockup detector only warns on the first one
309 * because the code relies on a full quiet cycle to
310 * re-arm. The second process prevents the quiet cycle
311 * and never gets reported. Use task pointers to detect
312 * this.
313 */
314 if (__this_cpu_read(softlockup_task_ptr_saved) !=
315 current) {
316 __this_cpu_write(soft_watchdog_warn, false);
317 __touch_watchdog();
318 }
Don Zickus58687ac2010-05-07 17:11:44 -0400319 return HRTIMER_RESTART;
chai wenb1a8de12014-10-09 15:25:17 -0700320 }
Don Zickus58687ac2010-05-07 17:11:44 -0400321
Aaron Tomlined235872014-06-23 13:22:05 -0700322 if (softlockup_all_cpu_backtrace) {
323 /* Prevent multiple soft-lockup reports if one cpu is already
324 * engaged in dumping cpu back traces
325 */
326 if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
327 /* Someone else will report us. Let's give up */
328 __this_cpu_write(soft_watchdog_warn, true);
329 return HRTIMER_RESTART;
330 }
331 }
332
Fabian Frederick656c3b72014-08-06 16:04:03 -0700333 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
Don Zickus26e09c62010-05-17 18:06:04 -0400334 smp_processor_id(), duration,
Don Zickus58687ac2010-05-07 17:11:44 -0400335 current->comm, task_pid_nr(current));
chai wenb1a8de12014-10-09 15:25:17 -0700336 __this_cpu_write(softlockup_task_ptr_saved, current);
Don Zickus58687ac2010-05-07 17:11:44 -0400337 print_modules();
338 print_irqtrace_events(current);
339 if (regs)
340 show_regs(regs);
341 else
342 dump_stack();
343
Aaron Tomlined235872014-06-23 13:22:05 -0700344 if (softlockup_all_cpu_backtrace) {
345 /* Avoid generating two back traces for current
346 * given that one is already made above
347 */
348 trigger_allbutself_cpu_backtrace();
349
350 clear_bit(0, &soft_lockup_nmi_warn);
351 /* Barrier to sync with other cpus */
352 smp_mb__after_atomic();
353 }
354
Josh Hunt69361ee2014-08-08 14:22:31 -0700355 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
Don Zickus58687ac2010-05-07 17:11:44 -0400356 if (softlockup_panic)
357 panic("softlockup: hung tasks");
Christoph Lameter909ea962010-12-08 16:22:55 +0100358 __this_cpu_write(soft_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400359 } else
Christoph Lameter909ea962010-12-08 16:22:55 +0100360 __this_cpu_write(soft_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400361
362 return HRTIMER_RESTART;
363}
364
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000365static void watchdog_set_prio(unsigned int policy, unsigned int prio)
Don Zickus58687ac2010-05-07 17:11:44 -0400366{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000367 struct sched_param param = { .sched_priority = prio };
368
369 sched_setscheduler(current, policy, &param);
370}
371
372static void watchdog_enable(unsigned int cpu)
373{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500374 struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
Don Zickus58687ac2010-05-07 17:11:44 -0400375
Bjørn Mork3935e8952012-12-19 20:51:31 +0100376 /* kick off the timer for the hardlockup detector */
377 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
378 hrtimer->function = watchdog_timer_fn;
379
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000380 /* Enable the perf event */
381 watchdog_nmi_enable(cpu);
Don Zickus58687ac2010-05-07 17:11:44 -0400382
Don Zickus58687ac2010-05-07 17:11:44 -0400383 /* done here because hrtimer_start can only pin to smp_processor_id() */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800384 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
Don Zickus58687ac2010-05-07 17:11:44 -0400385 HRTIMER_MODE_REL_PINNED);
386
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000387 /* initialize timestamp */
388 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
389 __touch_watchdog();
Don Zickus58687ac2010-05-07 17:11:44 -0400390}
391
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000392static void watchdog_disable(unsigned int cpu)
393{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500394 struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000395
396 watchdog_set_prio(SCHED_NORMAL, 0);
397 hrtimer_cancel(hrtimer);
398 /* disable the perf event */
399 watchdog_nmi_disable(cpu);
400}
401
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200402static void watchdog_cleanup(unsigned int cpu, bool online)
403{
404 watchdog_disable(cpu);
405}
406
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000407static int watchdog_should_run(unsigned int cpu)
408{
409 return __this_cpu_read(hrtimer_interrupts) !=
410 __this_cpu_read(soft_lockup_hrtimer_cnt);
411}
412
413/*
414 * The watchdog thread function - touches the timestamp.
415 *
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800416 * It only runs once every sample_period seconds (4 seconds by
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000417 * default) to reset the softlockup timestamp. If this gets delayed
418 * for more than 2*watchdog_thresh seconds then the debug-printout
419 * triggers in watchdog_timer_fn().
420 */
421static void watchdog(unsigned int cpu)
422{
423 __this_cpu_write(soft_lockup_hrtimer_cnt,
424 __this_cpu_read(hrtimer_interrupts));
425 __touch_watchdog();
Ulrich Obergfellbcfba4f2015-04-14 15:44:10 -0700426
427 /*
428 * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
429 * failure path. Check for failures that can occur asynchronously -
430 * for example, when CPUs are on-lined - and shut down the hardware
431 * perf event on each CPU accordingly.
432 *
433 * The only non-obvious place this bit can be cleared is through
434 * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a
435 * pr_info here would be too noisy as it would result in a message
436 * every few seconds if the hardlockup was disabled but the softlockup
437 * enabled.
438 */
439 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
440 watchdog_nmi_disable(cpu);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000441}
Don Zickus58687ac2010-05-07 17:11:44 -0400442
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200443static struct smp_hotplug_thread watchdog_threads = {
444 .store = &softlockup_watchdog,
445 .thread_should_run = watchdog_should_run,
446 .thread_fn = watchdog,
447 .thread_comm = "watchdog/%u",
448 .setup = watchdog_enable,
449 .cleanup = watchdog_cleanup,
450 .park = watchdog_disable,
451 .unpark = watchdog_enable,
452};
453
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700454/*
455 * park all watchdog threads that are specified in 'watchdog_cpumask'
Ulrich Obergfellee7fed52015-11-05 18:44:39 -0800456 *
457 * This function returns an error if kthread_park() of a watchdog thread
458 * fails. In this situation, the watchdog threads of some CPUs can already
459 * be parked and the watchdog threads of other CPUs can still be runnable.
460 * Callers are expected to handle this special condition as appropriate in
461 * their context.
Ulrich Obergfella2a45b82015-11-05 18:44:53 -0800462 *
463 * This function may only be called in a context that is protected against
464 * races with CPU hotplug - for example, via get_online_cpus().
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700465 */
466static int watchdog_park_threads(void)
467{
468 int cpu, ret = 0;
469
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700470 for_each_watchdog_cpu(cpu) {
471 ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
472 if (ret)
473 break;
474 }
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700475
476 return ret;
477}
478
479/*
480 * unpark all watchdog threads that are specified in 'watchdog_cpumask'
Ulrich Obergfella2a45b82015-11-05 18:44:53 -0800481 *
482 * This function may only be called in a context that is protected against
483 * races with CPU hotplug - for example, via get_online_cpus().
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700484 */
485static void watchdog_unpark_threads(void)
486{
487 int cpu;
488
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700489 for_each_watchdog_cpu(cpu)
490 kthread_unpark(per_cpu(softlockup_watchdog, cpu));
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700491}
492
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700493/*
494 * Suspend the hard and soft lockup detector by parking the watchdog threads.
495 */
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700496int lockup_detector_suspend(void)
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700497{
498 int ret = 0;
499
Ulrich Obergfellee89e712015-11-05 18:44:47 -0800500 get_online_cpus();
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700501 mutex_lock(&watchdog_proc_mutex);
502 /*
503 * Multiple suspend requests can be active in parallel (counted by
504 * the 'watchdog_suspended' variable). If the watchdog threads are
505 * running, the first caller takes care that they will be parked.
506 * The state of 'watchdog_running' cannot change while a suspend
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700507 * request is active (see related code in 'proc' handlers).
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700508 */
509 if (watchdog_running && !watchdog_suspended)
510 ret = watchdog_park_threads();
511
512 if (ret == 0)
513 watchdog_suspended++;
Ulrich Obergfellc9935902015-11-05 18:44:36 -0800514 else {
515 watchdog_disable_all_cpus();
516 pr_err("Failed to suspend lockup detectors, disabled\n");
517 watchdog_enabled = 0;
518 }
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700519
520 mutex_unlock(&watchdog_proc_mutex);
521
522 return ret;
523}
524
525/*
526 * Resume the hard and soft lockup detector by unparking the watchdog threads.
527 */
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700528void lockup_detector_resume(void)
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700529{
530 mutex_lock(&watchdog_proc_mutex);
531
532 watchdog_suspended--;
533 /*
534 * The watchdog threads are unparked if they were previously running
535 * and if there is no more active suspend request.
536 */
537 if (watchdog_running && !watchdog_suspended)
538 watchdog_unpark_threads();
539
540 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfellee89e712015-11-05 18:44:47 -0800541 put_online_cpus();
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700542}
543
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800544static int update_watchdog_all_cpus(void)
Michal Hocko9809b182013-09-24 15:27:30 -0700545{
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800546 int ret;
547
548 ret = watchdog_park_threads();
549 if (ret)
550 return ret;
551
Ulrich Obergfelld4bdd0b2015-09-04 15:45:21 -0700552 watchdog_unpark_threads();
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800553
554 return 0;
Michal Hocko9809b182013-09-24 15:27:30 -0700555}
556
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700557static int watchdog_enable_all_cpus(void)
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200558{
559 int err = 0;
560
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200561 if (!watchdog_running) {
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700562 err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
563 &watchdog_cpumask);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200564 if (err)
565 pr_err("Failed to create watchdog threads, disabled\n");
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700566 else
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200567 watchdog_running = 1;
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700568 } else {
569 /*
570 * Enable/disable the lockup detectors or
571 * change the sample period 'on the fly'.
572 */
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800573 err = update_watchdog_all_cpus();
574
575 if (err) {
576 watchdog_disable_all_cpus();
577 pr_err("Failed to update lockup detectors, disabled\n");
578 }
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200579 }
580
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800581 if (err)
582 watchdog_enabled = 0;
583
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200584 return err;
585}
586
Don Zickus58687ac2010-05-07 17:11:44 -0400587static void watchdog_disable_all_cpus(void)
588{
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200589 if (watchdog_running) {
590 watchdog_running = 0;
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200591 smpboot_unregister_percpu_thread(&watchdog_threads);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000592 }
Don Zickus58687ac2010-05-07 17:11:44 -0400593}
594
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800595#ifdef CONFIG_SYSCTL
596
Don Zickus58687ac2010-05-07 17:11:44 -0400597/*
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700598 * Update the run state of the lockup detectors.
Don Zickus58687ac2010-05-07 17:11:44 -0400599 */
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700600static int proc_watchdog_update(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400601{
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700602 int err = 0;
603
604 /*
605 * Watchdog threads won't be started if they are already active.
606 * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
607 * care of this. If those threads are already active, the sample
608 * period will be updated and the lockup detectors will be enabled
609 * or disabled 'on the fly'.
610 */
611 if (watchdog_enabled && watchdog_thresh)
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700612 err = watchdog_enable_all_cpus();
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700613 else
614 watchdog_disable_all_cpus();
615
616 return err;
617
618}
619
620/*
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700621 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
622 *
623 * caller | table->data points to | 'which' contains the flag(s)
624 * -------------------|-----------------------|-----------------------------
625 * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
626 * | | with SOFT_WATCHDOG_ENABLED
627 * -------------------|-----------------------|-----------------------------
628 * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED
629 * -------------------|-----------------------|-----------------------------
630 * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
631 */
632static int proc_watchdog_common(int which, struct ctl_table *table, int write,
633 void __user *buffer, size_t *lenp, loff_t *ppos)
634{
635 int err, old, new;
636 int *watchdog_param = (int *)table->data;
Don Zickus58687ac2010-05-07 17:11:44 -0400637
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800638 get_online_cpus();
Michal Hocko359e6fa2013-09-24 15:27:29 -0700639 mutex_lock(&watchdog_proc_mutex);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000640
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700641 if (watchdog_suspended) {
642 /* no parameter changes allowed while watchdog is suspended */
643 err = -EAGAIN;
644 goto out;
645 }
646
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700647 /*
648 * If the parameter is being read return the state of the corresponding
649 * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
650 * run state of the lockup detectors.
651 */
652 if (!write) {
653 *watchdog_param = (watchdog_enabled & which) != 0;
654 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
655 } else {
656 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
657 if (err)
658 goto out;
659
660 /*
661 * There is a race window between fetching the current value
662 * from 'watchdog_enabled' and storing the new value. During
663 * this race window, watchdog_nmi_enable() can sneak in and
664 * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
665 * The 'cmpxchg' detects this race and the loop retries.
666 */
667 do {
668 old = watchdog_enabled;
669 /*
670 * If the parameter value is not zero set the
671 * corresponding bit(s), else clear it(them).
672 */
673 if (*watchdog_param)
674 new = old | which;
675 else
676 new = old & ~which;
677 } while (cmpxchg(&watchdog_enabled, old, new) != old);
678
679 /*
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800680 * Update the run state of the lockup detectors. There is _no_
681 * need to check the value returned by proc_watchdog_update()
682 * and to restore the previous value of 'watchdog_enabled' as
683 * both lockup detectors are disabled if proc_watchdog_update()
684 * returns an error.
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700685 */
Joshua Hunta1ee1932016-03-17 14:17:23 -0700686 if (old == new)
687 goto out;
688
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700689 err = proc_watchdog_update();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700690 }
691out:
692 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800693 put_online_cpus();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700694 return err;
695}
696
697/*
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700698 * /proc/sys/kernel/watchdog
699 */
700int proc_watchdog(struct ctl_table *table, int write,
701 void __user *buffer, size_t *lenp, loff_t *ppos)
702{
703 return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
704 table, write, buffer, lenp, ppos);
705}
706
707/*
708 * /proc/sys/kernel/nmi_watchdog
709 */
710int proc_nmi_watchdog(struct ctl_table *table, int write,
711 void __user *buffer, size_t *lenp, loff_t *ppos)
712{
713 return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
714 table, write, buffer, lenp, ppos);
715}
716
717/*
718 * /proc/sys/kernel/soft_watchdog
719 */
720int proc_soft_watchdog(struct ctl_table *table, int write,
721 void __user *buffer, size_t *lenp, loff_t *ppos)
722{
723 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
724 table, write, buffer, lenp, ppos);
725}
726
727/*
728 * /proc/sys/kernel/watchdog_thresh
729 */
730int proc_watchdog_thresh(struct ctl_table *table, int write,
731 void __user *buffer, size_t *lenp, loff_t *ppos)
732{
Joshua Hunta1ee1932016-03-17 14:17:23 -0700733 int err, old, new;
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700734
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800735 get_online_cpus();
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700736 mutex_lock(&watchdog_proc_mutex);
737
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700738 if (watchdog_suspended) {
739 /* no parameter changes allowed while watchdog is suspended */
740 err = -EAGAIN;
741 goto out;
742 }
743
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700744 old = ACCESS_ONCE(watchdog_thresh);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200745 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700746
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200747 if (err || !write)
Michal Hocko359e6fa2013-09-24 15:27:29 -0700748 goto out;
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700749
anish kumarb66a2352013-03-12 14:44:08 -0400750 /*
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800751 * Update the sample period. Restore on failure.
anish kumarb66a2352013-03-12 14:44:08 -0400752 */
Joshua Hunta1ee1932016-03-17 14:17:23 -0700753 new = ACCESS_ONCE(watchdog_thresh);
754 if (old == new)
755 goto out;
756
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700757 set_sample_period();
758 err = proc_watchdog_update();
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800759 if (err) {
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700760 watchdog_thresh = old;
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800761 set_sample_period();
762 }
Michal Hocko359e6fa2013-09-24 15:27:29 -0700763out:
764 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800765 put_online_cpus();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200766 return err;
Don Zickus58687ac2010-05-07 17:11:44 -0400767}
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700768
769/*
770 * The cpumask is the mask of possible cpus that the watchdog can run
771 * on, not the mask of cpus it is actually running on. This allows the
772 * user to specify a mask that will include cpus that have not yet
773 * been brought online, if desired.
774 */
775int proc_watchdog_cpumask(struct ctl_table *table, int write,
776 void __user *buffer, size_t *lenp, loff_t *ppos)
777{
778 int err;
779
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800780 get_online_cpus();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700781 mutex_lock(&watchdog_proc_mutex);
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700782
783 if (watchdog_suspended) {
784 /* no parameter changes allowed while watchdog is suspended */
785 err = -EAGAIN;
786 goto out;
787 }
788
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700789 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
790 if (!err && write) {
791 /* Remove impossible cpus to keep sysctl output cleaner. */
792 cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
793 cpu_possible_mask);
794
795 if (watchdog_running) {
796 /*
797 * Failure would be due to being unable to allocate
798 * a temporary cpumask, so we are likely not in a
799 * position to do much else to make things better.
800 */
801 if (smpboot_update_cpumask_percpu_thread(
802 &watchdog_threads, &watchdog_cpumask) != 0)
803 pr_err("cpumask update failed\n");
804 }
805 }
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700806out:
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700807 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800808 put_online_cpus();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700809 return err;
810}
811
Don Zickus58687ac2010-05-07 17:11:44 -0400812#endif /* CONFIG_SYSCTL */
813
Peter Zijlstra004417a2010-11-25 18:38:29 +0100814void __init lockup_detector_init(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400815{
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800816 set_sample_period();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200817
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700818#ifdef CONFIG_NO_HZ_FULL
819 if (tick_nohz_full_enabled()) {
Frederic Weisbecker314b08ff2015-09-04 15:45:09 -0700820 pr_info("Disabling watchdog on nohz_full cores by default\n");
821 cpumask_copy(&watchdog_cpumask, housekeeping_mask);
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700822 } else
823 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
824#else
825 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
826#endif
827
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700828 if (watchdog_enabled)
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700829 watchdog_enable_all_cpus();
Don Zickus58687ac2010-05-07 17:11:44 -0400830}