blob: 58125b7e4effa2cc7defad720d110fd26bb977eb [file] [log] [blame]
Mandeep Singh Bainese162b392009-01-15 11:08:40 -08001/*
2 * Detect Hung Task
3 *
4 * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
5 *
6 */
7
8#include <linux/mm.h>
9#include <linux/cpu.h>
10#include <linux/nmi.h>
11#include <linux/init.h>
12#include <linux/delay.h>
13#include <linux/freezer.h>
14#include <linux/kthread.h>
15#include <linux/lockdep.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040016#include <linux/export.h>
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080017#include <linux/sysctl.h>
Vitaly Kuznetsove5387592018-10-17 13:23:55 +020018#include <linux/suspend.h>
Oleg Nesterov41e85ce2013-08-01 18:59:41 +020019#include <linux/utsname.h>
Oleg Nesterov6a716c92013-10-19 18:18:28 +020020#include <trace/events/sched.h>
Imran Khana9788f42017-09-18 15:48:30 +053021#include <linux/sched/sysctl.h>
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080022
23/*
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -080024 * The number of tasks checked:
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080025 */
Li Zefancd646472013-09-23 16:43:58 +080026int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -080027
28/*
Imran Khana9788f42017-09-18 15:48:30 +053029 * Selective monitoring of hung tasks.
30 *
31 * if set to 1, khungtaskd skips monitoring tasks, which has
32 * task_struct->hang_detection_enabled value not set, else monitors all tasks.
33 */
34int sysctl_hung_task_selective_monitoring = 1;
35
36/*
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -080037 * Limit number of tasks checked in a batch.
38 *
39 * This value controls the preemptibility of khungtaskd since preemption
40 * is disabled during the critical section. It also controls the size of
41 * the RCU grace period. So it needs to be upper-bound.
42 */
Tetsuo Handa629e4572019-01-03 15:26:31 -080043#define HUNG_TASK_LOCK_BREAK (HZ / 10)
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080044
45/*
46 * Zero means infinite timeout - no checking done:
47 */
Jeff Mahoneye11feaa2011-04-27 14:27:24 -040048unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080049
Aaron Tomlin270750db2014-01-20 17:34:13 +000050int __read_mostly sysctl_hung_task_warnings = 10;
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080051
52static int __read_mostly did_panic;
53
54static struct task_struct *watchdog_task;
55
56/*
57 * Should we panic (and reboot, if panic_timeout= is set) when a
58 * hung task is detected:
59 */
60unsigned int __read_mostly sysctl_hung_task_panic =
61 CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
62
63static int __init hung_task_panic_setup(char *str)
64{
Fabian Frederickb51dbec2014-06-04 16:11:26 -070065 int rc = kstrtouint(str, 0, &sysctl_hung_task_panic);
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080066
Fabian Frederickb51dbec2014-06-04 16:11:26 -070067 if (rc)
68 return rc;
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080069 return 1;
70}
71__setup("hung_task_panic=", hung_task_panic_setup);
72
73static int
74hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
75{
76 did_panic = 1;
77
78 return NOTIFY_DONE;
79}
80
81static struct notifier_block panic_block = {
82 .notifier_call = hung_task_panic,
83};
84
Mandeep Singh Baines17406b82009-02-06 15:37:47 -080085static void check_hung_task(struct task_struct *t, unsigned long timeout)
Mandeep Singh Bainese162b392009-01-15 11:08:40 -080086{
87 unsigned long switch_count = t->nvcsw + t->nivcsw;
88
Frederic Weisbeckercf2592f2009-02-10 16:52:37 +010089 /*
90 * Ensure the task is not frozen.
Mandeep Singh Bainesf9fab102012-01-03 14:41:13 -080091 * Also, skip vfork and any other user process that freezer should skip.
Frederic Weisbeckercf2592f2009-02-10 16:52:37 +010092 */
Mandeep Singh Bainesf9fab102012-01-03 14:41:13 -080093 if (unlikely(t->flags & (PF_FROZEN | PF_FREEZER_SKIP)))
94 return;
95
96 /*
97 * When a freshly created task is scheduled once, changes its state to
98 * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
99 * musn't be checked.
100 */
101 if (unlikely(!switch_count))
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800102 return;
103
Mandeep Singh Baines17406b82009-02-06 15:37:47 -0800104 if (switch_count != t->last_switch_count) {
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800105 t->last_switch_count = switch_count;
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800106 return;
107 }
Oleg Nesterov6a716c92013-10-19 18:18:28 +0200108
109 trace_sched_process_hang(t);
110
John Siddle48a6d642016-10-11 13:55:56 -0700111 if (!sysctl_hung_task_warnings && !sysctl_hung_task_panic)
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800112 return;
Aaron Tomlin270750db2014-01-20 17:34:13 +0000113
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800114 /*
115 * Ok, the task did not get scheduled for more than 2 minutes,
116 * complain:
117 */
John Siddle48a6d642016-10-11 13:55:56 -0700118 if (sysctl_hung_task_warnings) {
119 sysctl_hung_task_warnings--;
120 pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
121 t->comm, t->pid, timeout);
122 pr_err(" %s %s %.*s\n",
123 print_tainted(), init_utsname()->release,
124 (int)strcspn(init_utsname()->version, " "),
125 init_utsname()->version);
126 pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
127 " disables this message.\n");
128 sched_show_task(t);
129 debug_show_all_locks();
130 }
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800131
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800132 touch_nmi_watchdog();
133
Sasha Levin625056b2012-03-15 17:47:20 -0400134 if (sysctl_hung_task_panic) {
135 trigger_all_cpu_backtrace();
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800136 panic("hung_task: blocked tasks");
Sasha Levin625056b2012-03-15 17:47:20 -0400137 }
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800138}
139
140/*
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800141 * To avoid extending the RCU grace period for an unbounded amount of time,
142 * periodically exit the critical section and enter a new one.
143 *
144 * For preemptible RCU it is sufficient to call rcu_read_unlock in order
John Kacur6a103b02010-08-05 17:10:54 +0200145 * to exit the grace period. For classic RCU, a reschedule is required.
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800146 */
Oleg Nesterov6027ce42012-03-05 14:59:14 -0800147static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800148{
Oleg Nesterov6027ce42012-03-05 14:59:14 -0800149 bool can_cont;
150
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800151 get_task_struct(g);
152 get_task_struct(t);
153 rcu_read_unlock();
154 cond_resched();
155 rcu_read_lock();
Oleg Nesterov6027ce42012-03-05 14:59:14 -0800156 can_cont = pid_alive(g) && pid_alive(t);
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800157 put_task_struct(t);
158 put_task_struct(g);
Oleg Nesterov6027ce42012-03-05 14:59:14 -0800159
160 return can_cont;
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800161}
162
163/*
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800164 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
165 * a really long time (120 seconds). If that happens, print out
166 * a warning.
167 */
Mandeep Singh Baines603a1482009-01-17 10:31:48 -0800168static void check_hung_uninterruptible_tasks(unsigned long timeout)
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800169{
170 int max_count = sysctl_hung_task_check_count;
Tetsuo Handa629e4572019-01-03 15:26:31 -0800171 unsigned long last_break = jiffies;
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800172 struct task_struct *g, *t;
173
174 /*
175 * If the system crashed already then all bets are off,
176 * do not report extra hung tasks:
177 */
178 if (test_taint(TAINT_DIE) || did_panic)
179 return;
180
Mandeep Singh Baines94be52d2009-02-05 09:56:08 -0800181 rcu_read_lock();
Aaron Tomlin972fae62015-04-15 16:16:47 -0700182 for_each_process_thread(g, t) {
Anton Blancharde5af0222009-11-27 13:28:20 +1100183 if (!max_count--)
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800184 goto unlock;
Tetsuo Handa629e4572019-01-03 15:26:31 -0800185 if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
Oleg Nesterov6027ce42012-03-05 14:59:14 -0800186 if (!rcu_lock_break(g, t))
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800187 goto unlock;
Tetsuo Handa629e4572019-01-03 15:26:31 -0800188 last_break = jiffies;
Mandeep Singh Bainesce9dbe22009-02-04 20:35:48 -0800189 }
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800190 /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
191 if (t->state == TASK_UNINTERRUPTIBLE)
Imran Khana9788f42017-09-18 15:48:30 +0530192 /* Check for selective monitoring */
193 if (!sysctl_hung_task_selective_monitoring ||
194 t->hang_detection_enabled)
195 check_hung_task(t, timeout);
Aaron Tomlin972fae62015-04-15 16:16:47 -0700196 }
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800197 unlock:
Mandeep Singh Baines94be52d2009-02-05 09:56:08 -0800198 rcu_read_unlock();
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800199}
200
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700201static long hung_timeout_jiffies(unsigned long last_checked,
202 unsigned long timeout)
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800203{
204 /* timeout of 0 will disable the watchdog */
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700205 return timeout ? last_checked - jiffies + timeout * HZ :
206 MAX_SCHEDULE_TIMEOUT;
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800207}
208
209/*
210 * Process updating of timeout sysctl
211 */
212int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700213 void __user *buffer,
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800214 size_t *lenp, loff_t *ppos)
215{
216 int ret;
217
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700218 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800219
220 if (ret || !write)
221 goto out;
222
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800223 wake_up_process(watchdog_task);
224
225 out:
226 return ret;
227}
228
Marcelo Tosatti8b414522013-10-11 21:39:26 -0300229static atomic_t reset_hung_task = ATOMIC_INIT(0);
230
231void reset_hung_task_detector(void)
232{
233 atomic_set(&reset_hung_task, 1);
234}
235EXPORT_SYMBOL_GPL(reset_hung_task_detector);
236
Vitaly Kuznetsove5387592018-10-17 13:23:55 +0200237static bool hung_detector_suspended;
238
239static int hungtask_pm_notify(struct notifier_block *self,
240 unsigned long action, void *hcpu)
241{
242 switch (action) {
243 case PM_SUSPEND_PREPARE:
244 case PM_HIBERNATION_PREPARE:
245 case PM_RESTORE_PREPARE:
246 hung_detector_suspended = true;
247 break;
248 case PM_POST_SUSPEND:
249 case PM_POST_HIBERNATION:
250 case PM_POST_RESTORE:
251 hung_detector_suspended = false;
252 break;
253 default:
254 break;
255 }
256 return NOTIFY_OK;
257}
258
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800259/*
260 * kthread which checks for tasks stuck in D state
261 */
262static int watchdog(void *dummy)
263{
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700264 unsigned long hung_last_checked = jiffies;
265
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800266 set_user_nice(current, 0);
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800267
268 for ( ; ; ) {
Mandeep Singh Baines17406b82009-02-06 15:37:47 -0800269 unsigned long timeout = sysctl_hung_task_timeout_secs;
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700270 long t = hung_timeout_jiffies(hung_last_checked, timeout);
Mandeep Singh Baines603a1482009-01-17 10:31:48 -0800271
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700272 if (t <= 0) {
Vitaly Kuznetsove5387592018-10-17 13:23:55 +0200273 if (!atomic_xchg(&reset_hung_task, 0) &&
274 !hung_detector_suspended)
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700275 check_hung_uninterruptible_tasks(timeout);
276 hung_last_checked = jiffies;
Marcelo Tosatti8b414522013-10-11 21:39:26 -0300277 continue;
Tetsuo Handab4aa14a2016-03-22 14:24:39 -0700278 }
279 schedule_timeout_interruptible(t);
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800280 }
281
282 return 0;
283}
284
285static int __init hung_task_init(void)
286{
287 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Vitaly Kuznetsove5387592018-10-17 13:23:55 +0200288
289 /* Disable hung task detector on suspend */
290 pm_notifier(hungtask_pm_notify, 0);
291
Mandeep Singh Bainese162b392009-01-15 11:08:40 -0800292 watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
293
294 return 0;
295}
Paul Gortmakerc96d6662014-04-03 14:48:35 -0700296subsys_initcall(hung_task_init);