blob: 144d7b1b0364e8ab64639aed0d97e483885ff9ea [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>
Ingo Molnare6017572017-02-01 16:36:40 +010024#include <linux/sched/clock.h>
Don Zickus58687ac2010-05-07 17:11:44 -040025
26#include <asm/irq_regs.h>
Eric B Munson5d1c0f42012-03-10 14:37:28 -050027#include <linux/kvm_para.h>
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -070028#include <linux/kthread.h>
Don Zickus58687ac2010-05-07 17:11:44 -040029
Peter Zijlstraab992dc2015-05-18 11:31:50 +020030static DEFINE_MUTEX(watchdog_proc_mutex);
31
Babu Moger249e52e2016-12-14 15:06:21 -080032#if defined(CONFIG_HAVE_NMI_WATCHDOG) || defined(CONFIG_HARDLOCKUP_DETECTOR)
33unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070034#else
Babu Moger249e52e2016-12-14 15:06:21 -080035unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070036#endif
37int __read_mostly nmi_watchdog_enabled;
38int __read_mostly soft_watchdog_enabled;
39int __read_mostly watchdog_user_enabled;
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -070040int __read_mostly watchdog_thresh = 10;
Ulrich Obergfell84d56e62015-04-14 15:43:55 -070041
Aaron Tomlined235872014-06-23 13:22:05 -070042#ifdef CONFIG_SMP
43int __read_mostly sysctl_softlockup_all_cpu_backtrace;
Jiri Kosina55537872015-11-05 18:44:41 -080044int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
Aaron Tomlined235872014-06-23 13:22:05 -070045#endif
Chris Metcalffe4ba3c2015-06-24 16:55:45 -070046static struct cpumask watchdog_cpumask __read_mostly;
47unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
48
49/* Helper for online, unparked cpus. */
50#define for_each_watchdog_cpu(cpu) \
51 for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
Aaron Tomlined235872014-06-23 13:22:05 -070052
Don Zickusb94f5112017-01-24 15:17:53 -080053atomic_t watchdog_park_in_progress = ATOMIC_INIT(0);
54
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070055/*
56 * The 'watchdog_running' variable is set to 1 when the watchdog threads
57 * are registered/started and is set to 0 when the watchdog threads are
58 * unregistered/stopped, so it is an indicator whether the threads exist.
59 */
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +020060static int __read_mostly watchdog_running;
Ulrich Obergfellec6a9062015-09-04 15:45:28 -070061/*
62 * If a subsystem has a need to deactivate the watchdog temporarily, it
63 * can use the suspend/resume interface to achieve this. The content of
64 * the 'watchdog_suspended' variable reflects this state. Existing threads
65 * are parked/unparked by the lockup_detector_{suspend|resume} functions
66 * (see comment blocks pertaining to those functions for further details).
67 *
68 * 'watchdog_suspended' also prevents threads from being registered/started
69 * or unregistered/stopped via parameters in /proc/sys/kernel, so the state
70 * of 'watchdog_running' cannot change while the watchdog is deactivated
71 * temporarily (see related code in 'proc' handlers).
72 */
73static int __read_mostly watchdog_suspended;
74
Chuansheng Liu0f34c402012-12-17 15:59:50 -080075static u64 __read_mostly sample_period;
Don Zickus58687ac2010-05-07 17:11:44 -040076
77static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
78static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
79static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
80static DEFINE_PER_CPU(bool, softlockup_touch_sync);
Don Zickus58687ac2010-05-07 17:11:44 -040081static DEFINE_PER_CPU(bool, soft_watchdog_warn);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +000082static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
83static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
chai wenb1a8de12014-10-09 15:25:17 -070084static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
Don Zickus58687ac2010-05-07 17:11:44 -040085static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
Aaron Tomlined235872014-06-23 13:22:05 -070086static unsigned long soft_lockup_nmi_warn;
Don Zickus58687ac2010-05-07 17:11:44 -040087
Don Zickus58687ac2010-05-07 17:11:44 -040088unsigned int __read_mostly softlockup_panic =
89 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
90
91static int __init softlockup_panic_setup(char *str)
92{
93 softlockup_panic = simple_strtoul(str, NULL, 0);
94
95 return 1;
96}
97__setup("softlockup_panic=", softlockup_panic_setup);
98
99static int __init nowatchdog_setup(char *str)
100{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700101 watchdog_enabled = 0;
Don Zickus58687ac2010-05-07 17:11:44 -0400102 return 1;
103}
104__setup("nowatchdog", nowatchdog_setup);
105
Don Zickus58687ac2010-05-07 17:11:44 -0400106static int __init nosoftlockup_setup(char *str)
107{
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700108 watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
Don Zickus58687ac2010-05-07 17:11:44 -0400109 return 1;
110}
111__setup("nosoftlockup", nosoftlockup_setup);
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700112
Aaron Tomlined235872014-06-23 13:22:05 -0700113#ifdef CONFIG_SMP
114static int __init softlockup_all_cpu_backtrace_setup(char *str)
115{
116 sysctl_softlockup_all_cpu_backtrace =
117 !!simple_strtol(str, NULL, 0);
118 return 1;
119}
120__setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
Jiri Kosina55537872015-11-05 18:44:41 -0800121static int __init hardlockup_all_cpu_backtrace_setup(char *str)
122{
123 sysctl_hardlockup_all_cpu_backtrace =
124 !!simple_strtol(str, NULL, 0);
125 return 1;
126}
127__setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
Aaron Tomlined235872014-06-23 13:22:05 -0700128#endif
Don Zickus58687ac2010-05-07 17:11:44 -0400129
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700130/*
131 * Hard-lockup warnings should be triggered after just a few seconds. Soft-
132 * lockups can have false positives under extreme conditions. So we generally
133 * want a higher threshold for soft lockups than for hard lockups. So we couple
134 * the thresholds with a factor: we make the soft threshold twice the amount of
135 * time the hard threshold is.
136 */
Ingo Molnar6e9101a2011-05-24 05:43:18 +0200137static int get_softlockup_thresh(void)
Mandeep Singh Baines4eec42f2011-05-22 22:10:23 -0700138{
139 return watchdog_thresh * 2;
140}
Don Zickus58687ac2010-05-07 17:11:44 -0400141
142/*
143 * Returns seconds, approximately. We don't need nanosecond
144 * resolution, and we don't need to waste time with a big divide when
145 * 2^30ns == 1.074s.
146 */
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900147static unsigned long get_timestamp(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400148{
Cyril Bur545a2bf2015-02-12 15:01:24 -0800149 return running_clock() >> 30LL; /* 2^30 ~= 10^9 */
Don Zickus58687ac2010-05-07 17:11:44 -0400150}
151
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800152static void set_sample_period(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400153{
154 /*
Mandeep Singh Baines586692a2011-05-22 22:10:22 -0700155 * convert watchdog_thresh from seconds to ns
Fernando Luis Vázquez Cao86f5e6a2012-02-09 17:42:22 -0500156 * the divide by 5 is to give hrtimer several chances (two
157 * or three with the current relation between the soft
158 * and hard thresholds) to increment before the
159 * hardlockup detector generates a warning
Don Zickus58687ac2010-05-07 17:11:44 -0400160 */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800161 sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
Don Zickus58687ac2010-05-07 17:11:44 -0400162}
163
164/* Commands for resetting the watchdog */
165static void __touch_watchdog(void)
166{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900167 __this_cpu_write(watchdog_touch_ts, get_timestamp());
Don Zickus58687ac2010-05-07 17:11:44 -0400168}
169
Tejun Heo03e0d462015-12-08 11:28:04 -0500170/**
171 * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
172 *
173 * Call when the scheduler may have stalled for legitimate reasons
174 * preventing the watchdog task from executing - e.g. the scheduler
175 * entering idle state. This should only be used for scheduler events.
176 * Use touch_softlockup_watchdog() for everything else.
177 */
178void touch_softlockup_watchdog_sched(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400179{
Andrew Morton78611442014-04-18 15:07:12 -0700180 /*
181 * Preemption can be enabled. It doesn't matter which CPU's timestamp
182 * gets zeroed here, so use the raw_ operation.
183 */
184 raw_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400185}
Tejun Heo03e0d462015-12-08 11:28:04 -0500186
187void touch_softlockup_watchdog(void)
188{
189 touch_softlockup_watchdog_sched();
Tejun Heo82607adc2015-12-08 11:28:04 -0500190 wq_watchdog_touch(raw_smp_processor_id());
Tejun Heo03e0d462015-12-08 11:28:04 -0500191}
Ingo Molnar0167c782010-05-13 08:53:33 +0200192EXPORT_SYMBOL(touch_softlockup_watchdog);
Don Zickus58687ac2010-05-07 17:11:44 -0400193
Don Zickus332fbdb2010-05-07 17:11:45 -0400194void touch_all_softlockup_watchdogs(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400195{
196 int cpu;
197
198 /*
199 * this is done lockless
200 * do we care if a 0 races with a timestamp?
201 * all it means is the softlock check starts one cycle later
202 */
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700203 for_each_watchdog_cpu(cpu)
Don Zickus58687ac2010-05-07 17:11:44 -0400204 per_cpu(watchdog_touch_ts, cpu) = 0;
Tejun Heo82607adc2015-12-08 11:28:04 -0500205 wq_watchdog_touch(-1);
Don Zickus58687ac2010-05-07 17:11:44 -0400206}
207
Don Zickus58687ac2010-05-07 17:11:44 -0400208void touch_softlockup_watchdog_sync(void)
209{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500210 __this_cpu_write(softlockup_touch_sync, true);
211 __this_cpu_write(watchdog_touch_ts, 0);
Don Zickus58687ac2010-05-07 17:11:44 -0400212}
213
Don Zickus58687ac2010-05-07 17:11:44 -0400214/* watchdog detector functions */
Babu Moger249e52e2016-12-14 15:06:21 -0800215bool is_hardlockup(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400216{
Christoph Lameter909ea962010-12-08 16:22:55 +0100217 unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400218
Christoph Lameter909ea962010-12-08 16:22:55 +0100219 if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
Yaowei Bai451637e2015-11-05 18:44:24 -0800220 return true;
Don Zickus58687ac2010-05-07 17:11:44 -0400221
Christoph Lameter909ea962010-12-08 16:22:55 +0100222 __this_cpu_write(hrtimer_interrupts_saved, hrint);
Yaowei Bai451637e2015-11-05 18:44:24 -0800223 return false;
Don Zickus58687ac2010-05-07 17:11:44 -0400224}
Don Zickus58687ac2010-05-07 17:11:44 -0400225
Don Zickus26e09c62010-05-17 18:06:04 -0400226static int is_softlockup(unsigned long touch_ts)
Don Zickus58687ac2010-05-07 17:11:44 -0400227{
Namhyung Kimc06b4f12012-12-27 11:49:44 +0900228 unsigned long now = get_timestamp();
Don Zickus58687ac2010-05-07 17:11:44 -0400229
Ulrich Obergfell39d2da22015-11-05 18:44:56 -0800230 if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700231 /* Warn about unreasonable delays. */
232 if (time_after(now, touch_ts + get_softlockup_thresh()))
233 return now - touch_ts;
234 }
Don Zickus58687ac2010-05-07 17:11:44 -0400235 return 0;
236}
237
Don Zickus58687ac2010-05-07 17:11:44 -0400238static void watchdog_interrupt_count(void)
239{
Christoph Lameter909ea962010-12-08 16:22:55 +0100240 __this_cpu_inc(hrtimer_interrupts);
Don Zickus58687ac2010-05-07 17:11:44 -0400241}
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000242
Babu Moger73ce0512016-12-14 15:06:24 -0800243/*
244 * These two functions are mostly architecture specific
245 * defining them as weak here.
246 */
247int __weak watchdog_nmi_enable(unsigned int cpu)
248{
249 return 0;
250}
251void __weak watchdog_nmi_disable(unsigned int cpu)
252{
253}
Don Zickus58687ac2010-05-07 17:11:44 -0400254
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800255static int watchdog_enable_all_cpus(void);
256static void watchdog_disable_all_cpus(void);
257
Don Zickus58687ac2010-05-07 17:11:44 -0400258/* watchdog kicker functions */
259static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
260{
Christoph Lameter909ea962010-12-08 16:22:55 +0100261 unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400262 struct pt_regs *regs = get_irq_regs();
263 int duration;
Aaron Tomlined235872014-06-23 13:22:05 -0700264 int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
Don Zickus58687ac2010-05-07 17:11:44 -0400265
Don Zickusb94f5112017-01-24 15:17:53 -0800266 if (atomic_read(&watchdog_park_in_progress) != 0)
267 return HRTIMER_NORESTART;
268
Don Zickus58687ac2010-05-07 17:11:44 -0400269 /* kick the hardlockup detector */
270 watchdog_interrupt_count();
271
272 /* kick the softlockup detector */
Christoph Lameter909ea962010-12-08 16:22:55 +0100273 wake_up_process(__this_cpu_read(softlockup_watchdog));
Don Zickus58687ac2010-05-07 17:11:44 -0400274
275 /* .. and repeat */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800276 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
Don Zickus58687ac2010-05-07 17:11:44 -0400277
278 if (touch_ts == 0) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100279 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
Don Zickus58687ac2010-05-07 17:11:44 -0400280 /*
281 * If the time stamp was touched atomically
282 * make sure the scheduler tick is up to date.
283 */
Christoph Lameter909ea962010-12-08 16:22:55 +0100284 __this_cpu_write(softlockup_touch_sync, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400285 sched_clock_tick();
286 }
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500287
288 /* Clear the guest paused flag on watchdog reset */
289 kvm_check_and_clear_guest_paused();
Don Zickus58687ac2010-05-07 17:11:44 -0400290 __touch_watchdog();
291 return HRTIMER_RESTART;
292 }
293
294 /* check for a softlockup
295 * This is done by making sure a high priority task is
296 * being scheduled. The task touches the watchdog to
297 * indicate it is getting cpu time. If it hasn't then
298 * this is a good indication some task is hogging the cpu
299 */
Don Zickus26e09c62010-05-17 18:06:04 -0400300 duration = is_softlockup(touch_ts);
Don Zickus58687ac2010-05-07 17:11:44 -0400301 if (unlikely(duration)) {
Eric B Munson5d1c0f42012-03-10 14:37:28 -0500302 /*
303 * If a virtual machine is stopped by the host it can look to
304 * the watchdog like a soft lockup, check to see if the host
305 * stopped the vm before we issue the warning
306 */
307 if (kvm_check_and_clear_guest_paused())
308 return HRTIMER_RESTART;
309
Don Zickus58687ac2010-05-07 17:11:44 -0400310 /* only warn once */
chai wenb1a8de12014-10-09 15:25:17 -0700311 if (__this_cpu_read(soft_watchdog_warn) == true) {
312 /*
313 * When multiple processes are causing softlockups the
314 * softlockup detector only warns on the first one
315 * because the code relies on a full quiet cycle to
316 * re-arm. The second process prevents the quiet cycle
317 * and never gets reported. Use task pointers to detect
318 * this.
319 */
320 if (__this_cpu_read(softlockup_task_ptr_saved) !=
321 current) {
322 __this_cpu_write(soft_watchdog_warn, false);
323 __touch_watchdog();
324 }
Don Zickus58687ac2010-05-07 17:11:44 -0400325 return HRTIMER_RESTART;
chai wenb1a8de12014-10-09 15:25:17 -0700326 }
Don Zickus58687ac2010-05-07 17:11:44 -0400327
Aaron Tomlined235872014-06-23 13:22:05 -0700328 if (softlockup_all_cpu_backtrace) {
329 /* Prevent multiple soft-lockup reports if one cpu is already
330 * engaged in dumping cpu back traces
331 */
332 if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
333 /* Someone else will report us. Let's give up */
334 __this_cpu_write(soft_watchdog_warn, true);
335 return HRTIMER_RESTART;
336 }
337 }
338
Fabian Frederick656c3b72014-08-06 16:04:03 -0700339 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
Don Zickus26e09c62010-05-17 18:06:04 -0400340 smp_processor_id(), duration,
Don Zickus58687ac2010-05-07 17:11:44 -0400341 current->comm, task_pid_nr(current));
chai wenb1a8de12014-10-09 15:25:17 -0700342 __this_cpu_write(softlockup_task_ptr_saved, current);
Don Zickus58687ac2010-05-07 17:11:44 -0400343 print_modules();
344 print_irqtrace_events(current);
345 if (regs)
346 show_regs(regs);
347 else
348 dump_stack();
349
Aaron Tomlined235872014-06-23 13:22:05 -0700350 if (softlockup_all_cpu_backtrace) {
351 /* Avoid generating two back traces for current
352 * given that one is already made above
353 */
354 trigger_allbutself_cpu_backtrace();
355
356 clear_bit(0, &soft_lockup_nmi_warn);
357 /* Barrier to sync with other cpus */
358 smp_mb__after_atomic();
359 }
360
Josh Hunt69361ee2014-08-08 14:22:31 -0700361 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
Don Zickus58687ac2010-05-07 17:11:44 -0400362 if (softlockup_panic)
363 panic("softlockup: hung tasks");
Christoph Lameter909ea962010-12-08 16:22:55 +0100364 __this_cpu_write(soft_watchdog_warn, true);
Don Zickus58687ac2010-05-07 17:11:44 -0400365 } else
Christoph Lameter909ea962010-12-08 16:22:55 +0100366 __this_cpu_write(soft_watchdog_warn, false);
Don Zickus58687ac2010-05-07 17:11:44 -0400367
368 return HRTIMER_RESTART;
369}
370
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000371static void watchdog_set_prio(unsigned int policy, unsigned int prio)
Don Zickus58687ac2010-05-07 17:11:44 -0400372{
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000373 struct sched_param param = { .sched_priority = prio };
374
375 sched_setscheduler(current, policy, &param);
376}
377
378static void watchdog_enable(unsigned int cpu)
379{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500380 struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
Don Zickus58687ac2010-05-07 17:11:44 -0400381
Bjørn Mork3935e8952012-12-19 20:51:31 +0100382 /* kick off the timer for the hardlockup detector */
383 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
384 hrtimer->function = watchdog_timer_fn;
385
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000386 /* Enable the perf event */
387 watchdog_nmi_enable(cpu);
Don Zickus58687ac2010-05-07 17:11:44 -0400388
Don Zickus58687ac2010-05-07 17:11:44 -0400389 /* done here because hrtimer_start can only pin to smp_processor_id() */
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800390 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
Don Zickus58687ac2010-05-07 17:11:44 -0400391 HRTIMER_MODE_REL_PINNED);
392
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000393 /* initialize timestamp */
394 watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
395 __touch_watchdog();
Don Zickus58687ac2010-05-07 17:11:44 -0400396}
397
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000398static void watchdog_disable(unsigned int cpu)
399{
Christoph Lameterf7f66b02014-08-17 12:30:34 -0500400 struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000401
402 watchdog_set_prio(SCHED_NORMAL, 0);
403 hrtimer_cancel(hrtimer);
404 /* disable the perf event */
405 watchdog_nmi_disable(cpu);
406}
407
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200408static void watchdog_cleanup(unsigned int cpu, bool online)
409{
410 watchdog_disable(cpu);
411}
412
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000413static int watchdog_should_run(unsigned int cpu)
414{
415 return __this_cpu_read(hrtimer_interrupts) !=
416 __this_cpu_read(soft_lockup_hrtimer_cnt);
417}
418
419/*
420 * The watchdog thread function - touches the timestamp.
421 *
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800422 * It only runs once every sample_period seconds (4 seconds by
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000423 * default) to reset the softlockup timestamp. If this gets delayed
424 * for more than 2*watchdog_thresh seconds then the debug-printout
425 * triggers in watchdog_timer_fn().
426 */
427static void watchdog(unsigned int cpu)
428{
429 __this_cpu_write(soft_lockup_hrtimer_cnt,
430 __this_cpu_read(hrtimer_interrupts));
431 __touch_watchdog();
Ulrich Obergfellbcfba4f2015-04-14 15:44:10 -0700432
433 /*
434 * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
435 * failure path. Check for failures that can occur asynchronously -
436 * for example, when CPUs are on-lined - and shut down the hardware
437 * perf event on each CPU accordingly.
438 *
439 * The only non-obvious place this bit can be cleared is through
440 * watchdog_nmi_enable(), so a pr_info() is placed there. Placing a
441 * pr_info here would be too noisy as it would result in a message
442 * every few seconds if the hardlockup was disabled but the softlockup
443 * enabled.
444 */
445 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
446 watchdog_nmi_disable(cpu);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000447}
Don Zickus58687ac2010-05-07 17:11:44 -0400448
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200449static struct smp_hotplug_thread watchdog_threads = {
450 .store = &softlockup_watchdog,
451 .thread_should_run = watchdog_should_run,
452 .thread_fn = watchdog,
453 .thread_comm = "watchdog/%u",
454 .setup = watchdog_enable,
455 .cleanup = watchdog_cleanup,
456 .park = watchdog_disable,
457 .unpark = watchdog_enable,
458};
459
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700460/*
461 * park all watchdog threads that are specified in 'watchdog_cpumask'
Ulrich Obergfellee7fed52015-11-05 18:44:39 -0800462 *
463 * This function returns an error if kthread_park() of a watchdog thread
464 * fails. In this situation, the watchdog threads of some CPUs can already
465 * be parked and the watchdog threads of other CPUs can still be runnable.
466 * Callers are expected to handle this special condition as appropriate in
467 * their context.
Ulrich Obergfella2a45b82015-11-05 18:44:53 -0800468 *
469 * This function may only be called in a context that is protected against
470 * races with CPU hotplug - for example, via get_online_cpus().
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700471 */
472static int watchdog_park_threads(void)
473{
474 int cpu, ret = 0;
475
Don Zickusb94f5112017-01-24 15:17:53 -0800476 atomic_set(&watchdog_park_in_progress, 1);
477
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700478 for_each_watchdog_cpu(cpu) {
479 ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
480 if (ret)
481 break;
482 }
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700483
Don Zickusb94f5112017-01-24 15:17:53 -0800484 atomic_set(&watchdog_park_in_progress, 0);
485
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700486 return ret;
487}
488
489/*
490 * unpark all watchdog threads that are specified in 'watchdog_cpumask'
Ulrich Obergfella2a45b82015-11-05 18:44:53 -0800491 *
492 * This function may only be called in a context that is protected against
493 * races with CPU hotplug - for example, via get_online_cpus().
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700494 */
495static void watchdog_unpark_threads(void)
496{
497 int cpu;
498
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700499 for_each_watchdog_cpu(cpu)
500 kthread_unpark(per_cpu(softlockup_watchdog, cpu));
Ulrich Obergfell81a4bee2015-09-04 15:45:15 -0700501}
502
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700503/*
504 * Suspend the hard and soft lockup detector by parking the watchdog threads.
505 */
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700506int lockup_detector_suspend(void)
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700507{
508 int ret = 0;
509
Ulrich Obergfellee89e712015-11-05 18:44:47 -0800510 get_online_cpus();
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700511 mutex_lock(&watchdog_proc_mutex);
512 /*
513 * Multiple suspend requests can be active in parallel (counted by
514 * the 'watchdog_suspended' variable). If the watchdog threads are
515 * running, the first caller takes care that they will be parked.
516 * The state of 'watchdog_running' cannot change while a suspend
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700517 * request is active (see related code in 'proc' handlers).
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700518 */
519 if (watchdog_running && !watchdog_suspended)
520 ret = watchdog_park_threads();
521
522 if (ret == 0)
523 watchdog_suspended++;
Ulrich Obergfellc9935902015-11-05 18:44:36 -0800524 else {
525 watchdog_disable_all_cpus();
526 pr_err("Failed to suspend lockup detectors, disabled\n");
527 watchdog_enabled = 0;
528 }
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700529
530 mutex_unlock(&watchdog_proc_mutex);
531
532 return ret;
533}
534
535/*
536 * Resume the hard and soft lockup detector by unparking the watchdog threads.
537 */
Ulrich Obergfellec6a9062015-09-04 15:45:28 -0700538void lockup_detector_resume(void)
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700539{
540 mutex_lock(&watchdog_proc_mutex);
541
542 watchdog_suspended--;
543 /*
544 * The watchdog threads are unparked if they were previously running
545 * and if there is no more active suspend request.
546 */
547 if (watchdog_running && !watchdog_suspended)
548 watchdog_unpark_threads();
549
550 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfellee89e712015-11-05 18:44:47 -0800551 put_online_cpus();
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700552}
553
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800554static int update_watchdog_all_cpus(void)
Michal Hocko9809b182013-09-24 15:27:30 -0700555{
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800556 int ret;
557
558 ret = watchdog_park_threads();
559 if (ret)
560 return ret;
561
Ulrich Obergfelld4bdd0b212015-09-04 15:45:21 -0700562 watchdog_unpark_threads();
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800563
564 return 0;
Michal Hocko9809b182013-09-24 15:27:30 -0700565}
566
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700567static int watchdog_enable_all_cpus(void)
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200568{
569 int err = 0;
570
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200571 if (!watchdog_running) {
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700572 err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
573 &watchdog_cpumask);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200574 if (err)
575 pr_err("Failed to create watchdog threads, disabled\n");
Frederic Weisbecker230ec932015-09-04 15:45:06 -0700576 else
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200577 watchdog_running = 1;
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700578 } else {
579 /*
580 * Enable/disable the lockup detectors or
581 * change the sample period 'on the fly'.
582 */
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800583 err = update_watchdog_all_cpus();
584
585 if (err) {
586 watchdog_disable_all_cpus();
587 pr_err("Failed to update lockup detectors, disabled\n");
588 }
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200589 }
590
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800591 if (err)
592 watchdog_enabled = 0;
593
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200594 return err;
595}
596
Don Zickus58687ac2010-05-07 17:11:44 -0400597static void watchdog_disable_all_cpus(void)
598{
Frederic Weisbecker3c00ea82013-05-19 20:45:15 +0200599 if (watchdog_running) {
600 watchdog_running = 0;
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200601 smpboot_unregister_percpu_thread(&watchdog_threads);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000602 }
Don Zickus58687ac2010-05-07 17:11:44 -0400603}
604
Ulrich Obergfell58cf6902015-11-05 18:44:30 -0800605#ifdef CONFIG_SYSCTL
606
Don Zickus58687ac2010-05-07 17:11:44 -0400607/*
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700608 * Update the run state of the lockup detectors.
Don Zickus58687ac2010-05-07 17:11:44 -0400609 */
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700610static int proc_watchdog_update(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400611{
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700612 int err = 0;
613
614 /*
615 * Watchdog threads won't be started if they are already active.
616 * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
617 * care of this. If those threads are already active, the sample
618 * period will be updated and the lockup detectors will be enabled
619 * or disabled 'on the fly'.
620 */
621 if (watchdog_enabled && watchdog_thresh)
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700622 err = watchdog_enable_all_cpus();
Ulrich Obergfella0c9cbb2015-04-14 15:43:58 -0700623 else
624 watchdog_disable_all_cpus();
625
626 return err;
627
628}
629
630/*
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700631 * common function for watchdog, nmi_watchdog and soft_watchdog parameter
632 *
633 * caller | table->data points to | 'which' contains the flag(s)
634 * -------------------|-----------------------|-----------------------------
635 * proc_watchdog | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
636 * | | with SOFT_WATCHDOG_ENABLED
637 * -------------------|-----------------------|-----------------------------
638 * proc_nmi_watchdog | nmi_watchdog_enabled | NMI_WATCHDOG_ENABLED
639 * -------------------|-----------------------|-----------------------------
640 * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
641 */
642static int proc_watchdog_common(int which, struct ctl_table *table, int write,
643 void __user *buffer, size_t *lenp, loff_t *ppos)
644{
645 int err, old, new;
646 int *watchdog_param = (int *)table->data;
Don Zickus58687ac2010-05-07 17:11:44 -0400647
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800648 get_online_cpus();
Michal Hocko359e6fa2013-09-24 15:27:29 -0700649 mutex_lock(&watchdog_proc_mutex);
Thomas Gleixnerbcd951c2012-07-16 10:42:38 +0000650
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700651 if (watchdog_suspended) {
652 /* no parameter changes allowed while watchdog is suspended */
653 err = -EAGAIN;
654 goto out;
655 }
656
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700657 /*
658 * If the parameter is being read return the state of the corresponding
659 * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
660 * run state of the lockup detectors.
661 */
662 if (!write) {
663 *watchdog_param = (watchdog_enabled & which) != 0;
664 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
665 } else {
666 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
667 if (err)
668 goto out;
669
670 /*
671 * There is a race window between fetching the current value
672 * from 'watchdog_enabled' and storing the new value. During
673 * this race window, watchdog_nmi_enable() can sneak in and
674 * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
675 * The 'cmpxchg' detects this race and the loop retries.
676 */
677 do {
678 old = watchdog_enabled;
679 /*
680 * If the parameter value is not zero set the
681 * corresponding bit(s), else clear it(them).
682 */
683 if (*watchdog_param)
684 new = old | which;
685 else
686 new = old & ~which;
687 } while (cmpxchg(&watchdog_enabled, old, new) != old);
688
689 /*
Ulrich Obergfellb43cb432015-11-05 18:44:33 -0800690 * Update the run state of the lockup detectors. There is _no_
691 * need to check the value returned by proc_watchdog_update()
692 * and to restore the previous value of 'watchdog_enabled' as
693 * both lockup detectors are disabled if proc_watchdog_update()
694 * returns an error.
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700695 */
Joshua Hunta1ee1932016-03-17 14:17:23 -0700696 if (old == new)
697 goto out;
698
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700699 err = proc_watchdog_update();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700700 }
701out:
702 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800703 put_online_cpus();
Ulrich Obergfellef246a22015-04-14 15:44:05 -0700704 return err;
705}
706
707/*
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700708 * /proc/sys/kernel/watchdog
709 */
710int proc_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|SOFT_WATCHDOG_ENABLED,
714 table, write, buffer, lenp, ppos);
715}
716
717/*
718 * /proc/sys/kernel/nmi_watchdog
719 */
720int proc_nmi_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,
724 table, write, buffer, lenp, ppos);
725}
726
727/*
728 * /proc/sys/kernel/soft_watchdog
729 */
730int proc_soft_watchdog(struct ctl_table *table, int write,
731 void __user *buffer, size_t *lenp, loff_t *ppos)
732{
733 return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
734 table, write, buffer, lenp, ppos);
735}
736
737/*
738 * /proc/sys/kernel/watchdog_thresh
739 */
740int proc_watchdog_thresh(struct ctl_table *table, int write,
741 void __user *buffer, size_t *lenp, loff_t *ppos)
742{
Joshua Hunta1ee1932016-03-17 14:17:23 -0700743 int err, old, new;
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700744
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800745 get_online_cpus();
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700746 mutex_lock(&watchdog_proc_mutex);
747
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700748 if (watchdog_suspended) {
749 /* no parameter changes allowed while watchdog is suspended */
750 err = -EAGAIN;
751 goto out;
752 }
753
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700754 old = ACCESS_ONCE(watchdog_thresh);
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200755 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700756
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200757 if (err || !write)
Michal Hocko359e6fa2013-09-24 15:27:29 -0700758 goto out;
Mandeep Singh Bainese04ab2b2011-05-22 22:10:21 -0700759
anish kumarb66a23562013-03-12 14:44:08 -0400760 /*
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800761 * Update the sample period. Restore on failure.
anish kumarb66a23562013-03-12 14:44:08 -0400762 */
Joshua Hunta1ee1932016-03-17 14:17:23 -0700763 new = ACCESS_ONCE(watchdog_thresh);
764 if (old == new)
765 goto out;
766
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700767 set_sample_period();
768 err = proc_watchdog_update();
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800769 if (err) {
Ulrich Obergfell83a80a32015-04-14 15:44:08 -0700770 watchdog_thresh = old;
Ulrich Obergfelld283c642015-11-05 18:44:27 -0800771 set_sample_period();
772 }
Michal Hocko359e6fa2013-09-24 15:27:29 -0700773out:
774 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800775 put_online_cpus();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200776 return err;
Don Zickus58687ac2010-05-07 17:11:44 -0400777}
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700778
779/*
780 * The cpumask is the mask of possible cpus that the watchdog can run
781 * on, not the mask of cpus it is actually running on. This allows the
782 * user to specify a mask that will include cpus that have not yet
783 * been brought online, if desired.
784 */
785int proc_watchdog_cpumask(struct ctl_table *table, int write,
786 void __user *buffer, size_t *lenp, loff_t *ppos)
787{
788 int err;
789
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800790 get_online_cpus();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700791 mutex_lock(&watchdog_proc_mutex);
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700792
793 if (watchdog_suspended) {
794 /* no parameter changes allowed while watchdog is suspended */
795 err = -EAGAIN;
796 goto out;
797 }
798
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700799 err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
800 if (!err && write) {
801 /* Remove impossible cpus to keep sysctl output cleaner. */
802 cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
803 cpu_possible_mask);
804
805 if (watchdog_running) {
806 /*
807 * Failure would be due to being unable to allocate
808 * a temporary cpumask, so we are likely not in a
809 * position to do much else to make things better.
810 */
811 if (smpboot_update_cpumask_percpu_thread(
812 &watchdog_threads, &watchdog_cpumask) != 0)
813 pr_err("cpumask update failed\n");
814 }
815 }
Ulrich Obergfell8c073d22015-09-04 15:45:18 -0700816out:
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700817 mutex_unlock(&watchdog_proc_mutex);
Ulrich Obergfell8614dde2015-11-05 18:44:50 -0800818 put_online_cpus();
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700819 return err;
820}
821
Don Zickus58687ac2010-05-07 17:11:44 -0400822#endif /* CONFIG_SYSCTL */
823
Peter Zijlstra004417a2010-11-25 18:38:29 +0100824void __init lockup_detector_init(void)
Don Zickus58687ac2010-05-07 17:11:44 -0400825{
Chuansheng Liu0f34c402012-12-17 15:59:50 -0800826 set_sample_period();
Frederic Weisbeckerb8900bc2013-06-06 15:42:53 +0200827
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700828#ifdef CONFIG_NO_HZ_FULL
829 if (tick_nohz_full_enabled()) {
Frederic Weisbecker314b08ff2015-09-04 15:45:09 -0700830 pr_info("Disabling watchdog on nohz_full cores by default\n");
831 cpumask_copy(&watchdog_cpumask, housekeeping_mask);
Chris Metcalffe4ba3c2015-06-24 16:55:45 -0700832 } else
833 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
834#else
835 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
836#endif
837
Ulrich Obergfell195daf62015-04-14 15:44:13 -0700838 if (watchdog_enabled)
Ulrich Obergfellb2f57c32015-04-14 15:44:16 -0700839 watchdog_enable_all_cpus();
Don Zickus58687ac2010-05-07 17:11:44 -0400840}