Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 1 | /* |
| 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 Cao | 86f5e6a | 2012-02-09 17:42:22 -0500 | [diff] [blame] | 6 | * 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 Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 9 | * to those contributors as well. |
| 10 | */ |
| 11 | |
Andrew Morton | 4501980 | 2012-03-23 15:01:55 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) "NMI watchdog: " fmt |
| 13 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 14 | #include <linux/mm.h> |
| 15 | #include <linux/cpu.h> |
| 16 | #include <linux/nmi.h> |
| 17 | #include <linux/init.h> |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/sysctl.h> |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 20 | #include <linux/smpboot.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 21 | #include <linux/sched/rt.h> |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 22 | #include <linux/tick.h> |
Tejun Heo | 82607adc | 2015-12-08 11:28:04 -0500 | [diff] [blame] | 23 | #include <linux/workqueue.h> |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 24 | |
| 25 | #include <asm/irq_regs.h> |
Eric B Munson | 5d1c0f4 | 2012-03-10 14:37:28 -0500 | [diff] [blame] | 26 | #include <linux/kvm_para.h> |
Ulrich Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 27 | #include <linux/kthread.h> |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 28 | |
Ulrich Obergfell | 84d56e6 | 2015-04-14 15:43:55 -0700 | [diff] [blame] | 29 | /* |
| 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 Zijlstra | ab992dc | 2015-05-18 11:31:50 +0200 | [diff] [blame] | 46 | static DEFINE_MUTEX(watchdog_proc_mutex); |
| 47 | |
Ulrich Obergfell | 84d56e6 | 2015-04-14 15:43:55 -0700 | [diff] [blame] | 48 | #ifdef CONFIG_HARDLOCKUP_DETECTOR |
| 49 | static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED; |
| 50 | #else |
| 51 | static unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED; |
| 52 | #endif |
| 53 | int __read_mostly nmi_watchdog_enabled; |
| 54 | int __read_mostly soft_watchdog_enabled; |
| 55 | int __read_mostly watchdog_user_enabled; |
Mandeep Singh Baines | 4eec42f | 2011-05-22 22:10:23 -0700 | [diff] [blame] | 56 | int __read_mostly watchdog_thresh = 10; |
Ulrich Obergfell | 84d56e6 | 2015-04-14 15:43:55 -0700 | [diff] [blame] | 57 | |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 58 | #ifdef CONFIG_SMP |
| 59 | int __read_mostly sysctl_softlockup_all_cpu_backtrace; |
Jiri Kosina | 5553787 | 2015-11-05 18:44:41 -0800 | [diff] [blame] | 60 | int __read_mostly sysctl_hardlockup_all_cpu_backtrace; |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 61 | #else |
| 62 | #define sysctl_softlockup_all_cpu_backtrace 0 |
Jiri Kosina | 5553787 | 2015-11-05 18:44:41 -0800 | [diff] [blame] | 63 | #define sysctl_hardlockup_all_cpu_backtrace 0 |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 64 | #endif |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 65 | static struct cpumask watchdog_cpumask __read_mostly; |
| 66 | unsigned 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 Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 71 | |
Ulrich Obergfell | ec6a906 | 2015-09-04 15:45:28 -0700 | [diff] [blame] | 72 | /* |
| 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 Weisbecker | 3c00ea8 | 2013-05-19 20:45:15 +0200 | [diff] [blame] | 77 | static int __read_mostly watchdog_running; |
Ulrich Obergfell | ec6a906 | 2015-09-04 15:45:28 -0700 | [diff] [blame] | 78 | /* |
| 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 | */ |
| 90 | static int __read_mostly watchdog_suspended; |
| 91 | |
Chuansheng Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 92 | static u64 __read_mostly sample_period; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 93 | |
| 94 | static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts); |
| 95 | static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog); |
| 96 | static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer); |
| 97 | static DEFINE_PER_CPU(bool, softlockup_touch_sync); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 98 | static DEFINE_PER_CPU(bool, soft_watchdog_warn); |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 99 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts); |
| 100 | static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt); |
chai wen | b1a8de1 | 2014-10-09 15:25:17 -0700 | [diff] [blame] | 101 | static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 102 | static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved); |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 103 | static unsigned long soft_lockup_nmi_warn; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 104 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 105 | unsigned int __read_mostly softlockup_panic = |
| 106 | CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE; |
| 107 | |
| 108 | static 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 | |
| 116 | static int __init nowatchdog_setup(char *str) |
| 117 | { |
Ulrich Obergfell | 195daf6 | 2015-04-14 15:44:13 -0700 | [diff] [blame] | 118 | watchdog_enabled = 0; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | __setup("nowatchdog", nowatchdog_setup); |
| 122 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 123 | static int __init nosoftlockup_setup(char *str) |
| 124 | { |
Ulrich Obergfell | 195daf6 | 2015-04-14 15:44:13 -0700 | [diff] [blame] | 125 | watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 126 | return 1; |
| 127 | } |
| 128 | __setup("nosoftlockup", nosoftlockup_setup); |
Ulrich Obergfell | 195daf6 | 2015-04-14 15:44:13 -0700 | [diff] [blame] | 129 | |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_SMP |
| 131 | static 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 Kosina | 5553787 | 2015-11-05 18:44:41 -0800 | [diff] [blame] | 138 | static 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 Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 145 | #endif |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 146 | |
Mandeep Singh Baines | 4eec42f | 2011-05-22 22:10:23 -0700 | [diff] [blame] | 147 | /* |
| 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 Molnar | 6e9101a | 2011-05-24 05:43:18 +0200 | [diff] [blame] | 154 | static int get_softlockup_thresh(void) |
Mandeep Singh Baines | 4eec42f | 2011-05-22 22:10:23 -0700 | [diff] [blame] | 155 | { |
| 156 | return watchdog_thresh * 2; |
| 157 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 158 | |
| 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 Kim | c06b4f1 | 2012-12-27 11:49:44 +0900 | [diff] [blame] | 164 | static unsigned long get_timestamp(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 165 | { |
Cyril Bur | 545a2bf | 2015-02-12 15:01:24 -0800 | [diff] [blame] | 166 | return running_clock() >> 30LL; /* 2^30 ~= 10^9 */ |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Chuansheng Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 169 | static void set_sample_period(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 170 | { |
| 171 | /* |
Mandeep Singh Baines | 586692a | 2011-05-22 22:10:22 -0700 | [diff] [blame] | 172 | * convert watchdog_thresh from seconds to ns |
Fernando Luis Vázquez Cao | 86f5e6a | 2012-02-09 17:42:22 -0500 | [diff] [blame] | 173 | * 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 Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 177 | */ |
Chuansheng Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 178 | sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Commands for resetting the watchdog */ |
| 182 | static void __touch_watchdog(void) |
| 183 | { |
Namhyung Kim | c06b4f1 | 2012-12-27 11:49:44 +0900 | [diff] [blame] | 184 | __this_cpu_write(watchdog_touch_ts, get_timestamp()); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Tejun Heo | 03e0d46 | 2015-12-08 11:28:04 -0500 | [diff] [blame] | 187 | /** |
| 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 | */ |
| 195 | void touch_softlockup_watchdog_sched(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 196 | { |
Andrew Morton | 7861144 | 2014-04-18 15:07:12 -0700 | [diff] [blame] | 197 | /* |
| 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 Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 202 | } |
Tejun Heo | 03e0d46 | 2015-12-08 11:28:04 -0500 | [diff] [blame] | 203 | |
| 204 | void touch_softlockup_watchdog(void) |
| 205 | { |
| 206 | touch_softlockup_watchdog_sched(); |
Tejun Heo | 82607adc | 2015-12-08 11:28:04 -0500 | [diff] [blame] | 207 | wq_watchdog_touch(raw_smp_processor_id()); |
Tejun Heo | 03e0d46 | 2015-12-08 11:28:04 -0500 | [diff] [blame] | 208 | } |
Ingo Molnar | 0167c78 | 2010-05-13 08:53:33 +0200 | [diff] [blame] | 209 | EXPORT_SYMBOL(touch_softlockup_watchdog); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 210 | |
Don Zickus | 332fbdb | 2010-05-07 17:11:45 -0400 | [diff] [blame] | 211 | void touch_all_softlockup_watchdogs(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 212 | { |
| 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 Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 220 | for_each_watchdog_cpu(cpu) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 221 | per_cpu(watchdog_touch_ts, cpu) = 0; |
Tejun Heo | 82607adc | 2015-12-08 11:28:04 -0500 | [diff] [blame] | 222 | wq_watchdog_touch(-1); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 225 | void touch_softlockup_watchdog_sync(void) |
| 226 | { |
Christoph Lameter | f7f66b0 | 2014-08-17 12:30:34 -0500 | [diff] [blame] | 227 | __this_cpu_write(softlockup_touch_sync, true); |
| 228 | __this_cpu_write(watchdog_touch_ts, 0); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 231 | /* watchdog detector functions */ |
Yaowei Bai | 451637e | 2015-11-05 18:44:24 -0800 | [diff] [blame] | 232 | static bool is_hardlockup(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 233 | { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 234 | unsigned long hrint = __this_cpu_read(hrtimer_interrupts); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 235 | |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 236 | if (__this_cpu_read(hrtimer_interrupts_saved) == hrint) |
Yaowei Bai | 451637e | 2015-11-05 18:44:24 -0800 | [diff] [blame] | 237 | return true; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 238 | |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 239 | __this_cpu_write(hrtimer_interrupts_saved, hrint); |
Yaowei Bai | 451637e | 2015-11-05 18:44:24 -0800 | [diff] [blame] | 240 | return false; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 241 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 242 | |
Don Zickus | 26e09c6 | 2010-05-17 18:06:04 -0400 | [diff] [blame] | 243 | static int is_softlockup(unsigned long touch_ts) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 244 | { |
Namhyung Kim | c06b4f1 | 2012-12-27 11:49:44 +0900 | [diff] [blame] | 245 | unsigned long now = get_timestamp(); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 246 | |
Ulrich Obergfell | 39d2da2 | 2015-11-05 18:44:56 -0800 | [diff] [blame] | 247 | if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){ |
Ulrich Obergfell | 195daf6 | 2015-04-14 15:44:13 -0700 | [diff] [blame] | 248 | /* Warn about unreasonable delays. */ |
| 249 | if (time_after(now, touch_ts + get_softlockup_thresh())) |
| 250 | return now - touch_ts; |
| 251 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 255 | static void watchdog_interrupt_count(void) |
| 256 | { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 257 | __this_cpu_inc(hrtimer_interrupts); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 258 | } |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 259 | |
Babu Moger | b969a24 | 2016-12-14 15:06:24 -0800 | [diff] [blame^] | 260 | /* |
| 261 | * These two functions are mostly architecture specific |
| 262 | * defining them as weak here. |
| 263 | */ |
| 264 | int __weak watchdog_nmi_enable(unsigned int cpu) |
| 265 | { |
| 266 | return 0; |
| 267 | } |
| 268 | void __weak watchdog_nmi_disable(unsigned int cpu) |
| 269 | { |
| 270 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 271 | |
Ulrich Obergfell | 58cf690 | 2015-11-05 18:44:30 -0800 | [diff] [blame] | 272 | static int watchdog_enable_all_cpus(void); |
| 273 | static void watchdog_disable_all_cpus(void); |
| 274 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 275 | /* watchdog kicker functions */ |
| 276 | static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) |
| 277 | { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 278 | unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 279 | struct pt_regs *regs = get_irq_regs(); |
| 280 | int duration; |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 281 | int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 282 | |
| 283 | /* kick the hardlockup detector */ |
| 284 | watchdog_interrupt_count(); |
| 285 | |
| 286 | /* kick the softlockup detector */ |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 287 | wake_up_process(__this_cpu_read(softlockup_watchdog)); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 288 | |
| 289 | /* .. and repeat */ |
Chuansheng Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 290 | hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period)); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 291 | |
| 292 | if (touch_ts == 0) { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 293 | if (unlikely(__this_cpu_read(softlockup_touch_sync))) { |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 294 | /* |
| 295 | * If the time stamp was touched atomically |
| 296 | * make sure the scheduler tick is up to date. |
| 297 | */ |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 298 | __this_cpu_write(softlockup_touch_sync, false); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 299 | sched_clock_tick(); |
| 300 | } |
Eric B Munson | 5d1c0f4 | 2012-03-10 14:37:28 -0500 | [diff] [blame] | 301 | |
| 302 | /* Clear the guest paused flag on watchdog reset */ |
| 303 | kvm_check_and_clear_guest_paused(); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 304 | __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 Zickus | 26e09c6 | 2010-05-17 18:06:04 -0400 | [diff] [blame] | 314 | duration = is_softlockup(touch_ts); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 315 | if (unlikely(duration)) { |
Eric B Munson | 5d1c0f4 | 2012-03-10 14:37:28 -0500 | [diff] [blame] | 316 | /* |
| 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 Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 324 | /* only warn once */ |
chai wen | b1a8de1 | 2014-10-09 15:25:17 -0700 | [diff] [blame] | 325 | 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 Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 339 | return HRTIMER_RESTART; |
chai wen | b1a8de1 | 2014-10-09 15:25:17 -0700 | [diff] [blame] | 340 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 341 | |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 342 | 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 Frederick | 656c3b7 | 2014-08-06 16:04:03 -0700 | [diff] [blame] | 353 | pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", |
Don Zickus | 26e09c6 | 2010-05-17 18:06:04 -0400 | [diff] [blame] | 354 | smp_processor_id(), duration, |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 355 | current->comm, task_pid_nr(current)); |
chai wen | b1a8de1 | 2014-10-09 15:25:17 -0700 | [diff] [blame] | 356 | __this_cpu_write(softlockup_task_ptr_saved, current); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 357 | print_modules(); |
| 358 | print_irqtrace_events(current); |
| 359 | if (regs) |
| 360 | show_regs(regs); |
| 361 | else |
| 362 | dump_stack(); |
| 363 | |
Aaron Tomlin | ed23587 | 2014-06-23 13:22:05 -0700 | [diff] [blame] | 364 | 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 Hunt | 69361ee | 2014-08-08 14:22:31 -0700 | [diff] [blame] | 375 | add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 376 | if (softlockup_panic) |
| 377 | panic("softlockup: hung tasks"); |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 378 | __this_cpu_write(soft_watchdog_warn, true); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 379 | } else |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 380 | __this_cpu_write(soft_watchdog_warn, false); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 381 | |
| 382 | return HRTIMER_RESTART; |
| 383 | } |
| 384 | |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 385 | static void watchdog_set_prio(unsigned int policy, unsigned int prio) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 386 | { |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 387 | struct sched_param param = { .sched_priority = prio }; |
| 388 | |
| 389 | sched_setscheduler(current, policy, ¶m); |
| 390 | } |
| 391 | |
| 392 | static void watchdog_enable(unsigned int cpu) |
| 393 | { |
Christoph Lameter | f7f66b0 | 2014-08-17 12:30:34 -0500 | [diff] [blame] | 394 | struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 395 | |
Bjørn Mork | 3935e895 | 2012-12-19 20:51:31 +0100 | [diff] [blame] | 396 | /* 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 Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 400 | /* Enable the perf event */ |
| 401 | watchdog_nmi_enable(cpu); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 402 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 403 | /* done here because hrtimer_start can only pin to smp_processor_id() */ |
Chuansheng Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 404 | hrtimer_start(hrtimer, ns_to_ktime(sample_period), |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 405 | HRTIMER_MODE_REL_PINNED); |
| 406 | |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 407 | /* initialize timestamp */ |
| 408 | watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1); |
| 409 | __touch_watchdog(); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 412 | static void watchdog_disable(unsigned int cpu) |
| 413 | { |
Christoph Lameter | f7f66b0 | 2014-08-17 12:30:34 -0500 | [diff] [blame] | 414 | struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer); |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 415 | |
| 416 | watchdog_set_prio(SCHED_NORMAL, 0); |
| 417 | hrtimer_cancel(hrtimer); |
| 418 | /* disable the perf event */ |
| 419 | watchdog_nmi_disable(cpu); |
| 420 | } |
| 421 | |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 422 | static void watchdog_cleanup(unsigned int cpu, bool online) |
| 423 | { |
| 424 | watchdog_disable(cpu); |
| 425 | } |
| 426 | |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 427 | static 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 Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 436 | * It only runs once every sample_period seconds (4 seconds by |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 437 | * 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 | */ |
| 441 | static void watchdog(unsigned int cpu) |
| 442 | { |
| 443 | __this_cpu_write(soft_lockup_hrtimer_cnt, |
| 444 | __this_cpu_read(hrtimer_interrupts)); |
| 445 | __touch_watchdog(); |
Ulrich Obergfell | bcfba4f | 2015-04-14 15:44:10 -0700 | [diff] [blame] | 446 | |
| 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 Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 461 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 462 | |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 463 | static 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 Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 474 | /* |
| 475 | * park all watchdog threads that are specified in 'watchdog_cpumask' |
Ulrich Obergfell | ee7fed5 | 2015-11-05 18:44:39 -0800 | [diff] [blame] | 476 | * |
| 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 Obergfell | a2a45b8 | 2015-11-05 18:44:53 -0800 | [diff] [blame] | 482 | * |
| 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 Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 485 | */ |
| 486 | static int watchdog_park_threads(void) |
| 487 | { |
| 488 | int cpu, ret = 0; |
| 489 | |
Ulrich Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 490 | for_each_watchdog_cpu(cpu) { |
| 491 | ret = kthread_park(per_cpu(softlockup_watchdog, cpu)); |
| 492 | if (ret) |
| 493 | break; |
| 494 | } |
Ulrich Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * unpark all watchdog threads that are specified in 'watchdog_cpumask' |
Ulrich Obergfell | a2a45b8 | 2015-11-05 18:44:53 -0800 | [diff] [blame] | 501 | * |
| 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 Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 504 | */ |
| 505 | static void watchdog_unpark_threads(void) |
| 506 | { |
| 507 | int cpu; |
| 508 | |
Ulrich Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 509 | for_each_watchdog_cpu(cpu) |
| 510 | kthread_unpark(per_cpu(softlockup_watchdog, cpu)); |
Ulrich Obergfell | 81a4bee | 2015-09-04 15:45:15 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 513 | /* |
| 514 | * Suspend the hard and soft lockup detector by parking the watchdog threads. |
| 515 | */ |
Ulrich Obergfell | ec6a906 | 2015-09-04 15:45:28 -0700 | [diff] [blame] | 516 | int lockup_detector_suspend(void) |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 517 | { |
| 518 | int ret = 0; |
| 519 | |
Ulrich Obergfell | ee89e71 | 2015-11-05 18:44:47 -0800 | [diff] [blame] | 520 | get_online_cpus(); |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 521 | 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 Obergfell | ec6a906 | 2015-09-04 15:45:28 -0700 | [diff] [blame] | 527 | * request is active (see related code in 'proc' handlers). |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 528 | */ |
| 529 | if (watchdog_running && !watchdog_suspended) |
| 530 | ret = watchdog_park_threads(); |
| 531 | |
| 532 | if (ret == 0) |
| 533 | watchdog_suspended++; |
Ulrich Obergfell | c993590 | 2015-11-05 18:44:36 -0800 | [diff] [blame] | 534 | else { |
| 535 | watchdog_disable_all_cpus(); |
| 536 | pr_err("Failed to suspend lockup detectors, disabled\n"); |
| 537 | watchdog_enabled = 0; |
| 538 | } |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 539 | |
| 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 Obergfell | ec6a906 | 2015-09-04 15:45:28 -0700 | [diff] [blame] | 548 | void lockup_detector_resume(void) |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 549 | { |
| 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 Obergfell | ee89e71 | 2015-11-05 18:44:47 -0800 | [diff] [blame] | 561 | put_online_cpus(); |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Ulrich Obergfell | b43cb43 | 2015-11-05 18:44:33 -0800 | [diff] [blame] | 564 | static int update_watchdog_all_cpus(void) |
Michal Hocko | 9809b18 | 2013-09-24 15:27:30 -0700 | [diff] [blame] | 565 | { |
Ulrich Obergfell | b43cb43 | 2015-11-05 18:44:33 -0800 | [diff] [blame] | 566 | int ret; |
| 567 | |
| 568 | ret = watchdog_park_threads(); |
| 569 | if (ret) |
| 570 | return ret; |
| 571 | |
Ulrich Obergfell | d4bdd0b | 2015-09-04 15:45:21 -0700 | [diff] [blame] | 572 | watchdog_unpark_threads(); |
Ulrich Obergfell | b43cb43 | 2015-11-05 18:44:33 -0800 | [diff] [blame] | 573 | |
| 574 | return 0; |
Michal Hocko | 9809b18 | 2013-09-24 15:27:30 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Ulrich Obergfell | b2f57c3 | 2015-04-14 15:44:16 -0700 | [diff] [blame] | 577 | static int watchdog_enable_all_cpus(void) |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 578 | { |
| 579 | int err = 0; |
| 580 | |
Frederic Weisbecker | 3c00ea8 | 2013-05-19 20:45:15 +0200 | [diff] [blame] | 581 | if (!watchdog_running) { |
Frederic Weisbecker | 230ec93 | 2015-09-04 15:45:06 -0700 | [diff] [blame] | 582 | err = smpboot_register_percpu_thread_cpumask(&watchdog_threads, |
| 583 | &watchdog_cpumask); |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 584 | if (err) |
| 585 | pr_err("Failed to create watchdog threads, disabled\n"); |
Frederic Weisbecker | 230ec93 | 2015-09-04 15:45:06 -0700 | [diff] [blame] | 586 | else |
Frederic Weisbecker | 3c00ea8 | 2013-05-19 20:45:15 +0200 | [diff] [blame] | 587 | watchdog_running = 1; |
Ulrich Obergfell | b2f57c3 | 2015-04-14 15:44:16 -0700 | [diff] [blame] | 588 | } else { |
| 589 | /* |
| 590 | * Enable/disable the lockup detectors or |
| 591 | * change the sample period 'on the fly'. |
| 592 | */ |
Ulrich Obergfell | b43cb43 | 2015-11-05 18:44:33 -0800 | [diff] [blame] | 593 | 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 Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Ulrich Obergfell | b43cb43 | 2015-11-05 18:44:33 -0800 | [diff] [blame] | 601 | if (err) |
| 602 | watchdog_enabled = 0; |
| 603 | |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 604 | return err; |
| 605 | } |
| 606 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 607 | static void watchdog_disable_all_cpus(void) |
| 608 | { |
Frederic Weisbecker | 3c00ea8 | 2013-05-19 20:45:15 +0200 | [diff] [blame] | 609 | if (watchdog_running) { |
| 610 | watchdog_running = 0; |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 611 | smpboot_unregister_percpu_thread(&watchdog_threads); |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 612 | } |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 613 | } |
| 614 | |
Ulrich Obergfell | 58cf690 | 2015-11-05 18:44:30 -0800 | [diff] [blame] | 615 | #ifdef CONFIG_SYSCTL |
| 616 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 617 | /* |
Ulrich Obergfell | a0c9cbb | 2015-04-14 15:43:58 -0700 | [diff] [blame] | 618 | * Update the run state of the lockup detectors. |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 619 | */ |
Ulrich Obergfell | a0c9cbb | 2015-04-14 15:43:58 -0700 | [diff] [blame] | 620 | static int proc_watchdog_update(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 621 | { |
Ulrich Obergfell | a0c9cbb | 2015-04-14 15:43:58 -0700 | [diff] [blame] | 622 | 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 Obergfell | b2f57c3 | 2015-04-14 15:44:16 -0700 | [diff] [blame] | 632 | err = watchdog_enable_all_cpus(); |
Ulrich Obergfell | a0c9cbb | 2015-04-14 15:43:58 -0700 | [diff] [blame] | 633 | else |
| 634 | watchdog_disable_all_cpus(); |
| 635 | |
| 636 | return err; |
| 637 | |
| 638 | } |
| 639 | |
| 640 | /* |
Ulrich Obergfell | ef246a2 | 2015-04-14 15:44:05 -0700 | [diff] [blame] | 641 | * 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 | */ |
| 652 | static 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 Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 657 | |
Ulrich Obergfell | 8614dde | 2015-11-05 18:44:50 -0800 | [diff] [blame] | 658 | get_online_cpus(); |
Michal Hocko | 359e6fa | 2013-09-24 15:27:29 -0700 | [diff] [blame] | 659 | mutex_lock(&watchdog_proc_mutex); |
Thomas Gleixner | bcd951c | 2012-07-16 10:42:38 +0000 | [diff] [blame] | 660 | |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 661 | if (watchdog_suspended) { |
| 662 | /* no parameter changes allowed while watchdog is suspended */ |
| 663 | err = -EAGAIN; |
| 664 | goto out; |
| 665 | } |
| 666 | |
Ulrich Obergfell | ef246a2 | 2015-04-14 15:44:05 -0700 | [diff] [blame] | 667 | /* |
| 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 Obergfell | b43cb43 | 2015-11-05 18:44:33 -0800 | [diff] [blame] | 700 | * 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 Obergfell | ef246a2 | 2015-04-14 15:44:05 -0700 | [diff] [blame] | 705 | */ |
Joshua Hunt | a1ee193 | 2016-03-17 14:17:23 -0700 | [diff] [blame] | 706 | if (old == new) |
| 707 | goto out; |
| 708 | |
Ulrich Obergfell | ef246a2 | 2015-04-14 15:44:05 -0700 | [diff] [blame] | 709 | err = proc_watchdog_update(); |
Ulrich Obergfell | ef246a2 | 2015-04-14 15:44:05 -0700 | [diff] [blame] | 710 | } |
| 711 | out: |
| 712 | mutex_unlock(&watchdog_proc_mutex); |
Ulrich Obergfell | 8614dde | 2015-11-05 18:44:50 -0800 | [diff] [blame] | 713 | put_online_cpus(); |
Ulrich Obergfell | ef246a2 | 2015-04-14 15:44:05 -0700 | [diff] [blame] | 714 | return err; |
| 715 | } |
| 716 | |
| 717 | /* |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 718 | * /proc/sys/kernel/watchdog |
| 719 | */ |
| 720 | int 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 | */ |
| 730 | int 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 | */ |
| 740 | int 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 | */ |
| 750 | int proc_watchdog_thresh(struct ctl_table *table, int write, |
| 751 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 752 | { |
Joshua Hunt | a1ee193 | 2016-03-17 14:17:23 -0700 | [diff] [blame] | 753 | int err, old, new; |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 754 | |
Ulrich Obergfell | 8614dde | 2015-11-05 18:44:50 -0800 | [diff] [blame] | 755 | get_online_cpus(); |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 756 | mutex_lock(&watchdog_proc_mutex); |
| 757 | |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 758 | if (watchdog_suspended) { |
| 759 | /* no parameter changes allowed while watchdog is suspended */ |
| 760 | err = -EAGAIN; |
| 761 | goto out; |
| 762 | } |
| 763 | |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 764 | old = ACCESS_ONCE(watchdog_thresh); |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 765 | err = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 766 | |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 767 | if (err || !write) |
Michal Hocko | 359e6fa | 2013-09-24 15:27:29 -0700 | [diff] [blame] | 768 | goto out; |
Mandeep Singh Baines | e04ab2b | 2011-05-22 22:10:21 -0700 | [diff] [blame] | 769 | |
anish kumar | b66a235 | 2013-03-12 14:44:08 -0400 | [diff] [blame] | 770 | /* |
Ulrich Obergfell | d283c64 | 2015-11-05 18:44:27 -0800 | [diff] [blame] | 771 | * Update the sample period. Restore on failure. |
anish kumar | b66a235 | 2013-03-12 14:44:08 -0400 | [diff] [blame] | 772 | */ |
Joshua Hunt | a1ee193 | 2016-03-17 14:17:23 -0700 | [diff] [blame] | 773 | new = ACCESS_ONCE(watchdog_thresh); |
| 774 | if (old == new) |
| 775 | goto out; |
| 776 | |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 777 | set_sample_period(); |
| 778 | err = proc_watchdog_update(); |
Ulrich Obergfell | d283c64 | 2015-11-05 18:44:27 -0800 | [diff] [blame] | 779 | if (err) { |
Ulrich Obergfell | 83a80a3 | 2015-04-14 15:44:08 -0700 | [diff] [blame] | 780 | watchdog_thresh = old; |
Ulrich Obergfell | d283c64 | 2015-11-05 18:44:27 -0800 | [diff] [blame] | 781 | set_sample_period(); |
| 782 | } |
Michal Hocko | 359e6fa | 2013-09-24 15:27:29 -0700 | [diff] [blame] | 783 | out: |
| 784 | mutex_unlock(&watchdog_proc_mutex); |
Ulrich Obergfell | 8614dde | 2015-11-05 18:44:50 -0800 | [diff] [blame] | 785 | put_online_cpus(); |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 786 | return err; |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 787 | } |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 788 | |
| 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 | */ |
| 795 | int 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 Obergfell | 8614dde | 2015-11-05 18:44:50 -0800 | [diff] [blame] | 800 | get_online_cpus(); |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 801 | mutex_lock(&watchdog_proc_mutex); |
Ulrich Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 802 | |
| 803 | if (watchdog_suspended) { |
| 804 | /* no parameter changes allowed while watchdog is suspended */ |
| 805 | err = -EAGAIN; |
| 806 | goto out; |
| 807 | } |
| 808 | |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 809 | 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 Obergfell | 8c073d2 | 2015-09-04 15:45:18 -0700 | [diff] [blame] | 826 | out: |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 827 | mutex_unlock(&watchdog_proc_mutex); |
Ulrich Obergfell | 8614dde | 2015-11-05 18:44:50 -0800 | [diff] [blame] | 828 | put_online_cpus(); |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 829 | return err; |
| 830 | } |
| 831 | |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 832 | #endif /* CONFIG_SYSCTL */ |
| 833 | |
Peter Zijlstra | 004417a | 2010-11-25 18:38:29 +0100 | [diff] [blame] | 834 | void __init lockup_detector_init(void) |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 835 | { |
Chuansheng Liu | 0f34c40 | 2012-12-17 15:59:50 -0800 | [diff] [blame] | 836 | set_sample_period(); |
Frederic Weisbecker | b8900bc | 2013-06-06 15:42:53 +0200 | [diff] [blame] | 837 | |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 838 | #ifdef CONFIG_NO_HZ_FULL |
| 839 | if (tick_nohz_full_enabled()) { |
Frederic Weisbecker | 314b08ff | 2015-09-04 15:45:09 -0700 | [diff] [blame] | 840 | pr_info("Disabling watchdog on nohz_full cores by default\n"); |
| 841 | cpumask_copy(&watchdog_cpumask, housekeeping_mask); |
Chris Metcalf | fe4ba3c | 2015-06-24 16:55:45 -0700 | [diff] [blame] | 842 | } else |
| 843 | cpumask_copy(&watchdog_cpumask, cpu_possible_mask); |
| 844 | #else |
| 845 | cpumask_copy(&watchdog_cpumask, cpu_possible_mask); |
| 846 | #endif |
| 847 | |
Ulrich Obergfell | 195daf6 | 2015-04-14 15:44:13 -0700 | [diff] [blame] | 848 | if (watchdog_enabled) |
Ulrich Obergfell | b2f57c3 | 2015-04-14 15:44:16 -0700 | [diff] [blame] | 849 | watchdog_enable_all_cpus(); |
Don Zickus | 58687ac | 2010-05-07 17:11:44 -0400 | [diff] [blame] | 850 | } |