1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Detect Hung Task
4 *
5 * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
6 *
7 */
8
9 #include <linux/mm.h>
10 #include <linux/cpu.h>
11 #include <linux/nmi.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/freezer.h>
15 #include <linux/kthread.h>
16 #include <linux/lockdep.h>
17 #include <linux/export.h>
18 #include <linux/panic_notifier.h>
19 #include <linux/sysctl.h>
20 #include <linux/suspend.h>
21 #include <linux/utsname.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/debug.h>
24 #include <linux/sched/sysctl.h>
25
26 #include <trace/events/sched.h>
27 #undef CREATE_TRACE_POINTS
28 #include <trace/hooks/hung_task.h>
29
30 /*
31 * The number of tasks checked:
32 */
33 int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
34
35 /*
36 * Limit number of tasks checked in a batch.
37 *
38 * This value controls the preemptibility of khungtaskd since preemption
39 * is disabled during the critical section. It also controls the size of
40 * the RCU grace period. So it needs to be upper-bound.
41 */
42 #define HUNG_TASK_LOCK_BREAK (HZ / 10)
43
44 /*
45 * Zero means infinite timeout - no checking done:
46 */
47 unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
48
49 /*
50 * Zero (default value) means use sysctl_hung_task_timeout_secs:
51 */
52 unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
53
54 int __read_mostly sysctl_hung_task_warnings = 10;
55
56 static int __read_mostly did_panic;
57 static bool hung_task_show_lock;
58 static bool hung_task_call_panic;
59 static bool hung_task_show_all_bt;
60
61 static struct task_struct *watchdog_task;
62
63 #ifdef CONFIG_SMP
64 /*
65 * Should we dump all CPUs backtraces in a hung task event?
66 * Defaults to 0, can be changed via sysctl.
67 */
68 unsigned int __read_mostly sysctl_hung_task_all_cpu_backtrace;
69 #endif /* CONFIG_SMP */
70
71 /*
72 * Should we panic (and reboot, if panic_timeout= is set) when a
73 * hung task is detected:
74 */
75 unsigned int __read_mostly sysctl_hung_task_panic =
76 CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
77
78 static int
hung_task_panic(struct notifier_block * this,unsigned long event,void * ptr)79 hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
80 {
81 did_panic = 1;
82
83 return NOTIFY_DONE;
84 }
85
86 static struct notifier_block panic_block = {
87 .notifier_call = hung_task_panic,
88 };
89
check_hung_task(struct task_struct * t,unsigned long timeout)90 static void check_hung_task(struct task_struct *t, unsigned long timeout)
91 {
92 unsigned long switch_count = t->nvcsw + t->nivcsw;
93
94 /*
95 * Ensure the task is not frozen.
96 * Also, skip vfork and any other user process that freezer should skip.
97 */
98 if (unlikely(frozen_or_skipped(t)))
99 return;
100
101 /*
102 * When a freshly created task is scheduled once, changes its state to
103 * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
104 * musn't be checked.
105 */
106 if (unlikely(!switch_count))
107 return;
108
109 if (switch_count != t->last_switch_count) {
110 t->last_switch_count = switch_count;
111 t->last_switch_time = jiffies;
112 return;
113 }
114 if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
115 return;
116
117 trace_sched_process_hang(t);
118
119 if (sysctl_hung_task_panic) {
120 console_verbose();
121 hung_task_show_lock = true;
122 hung_task_call_panic = true;
123 }
124
125 /*
126 * Ok, the task did not get scheduled for more than 2 minutes,
127 * complain:
128 */
129 if (sysctl_hung_task_warnings) {
130 if (sysctl_hung_task_warnings > 0)
131 sysctl_hung_task_warnings--;
132 pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
133 t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
134 pr_err(" %s %s %.*s\n",
135 print_tainted(), init_utsname()->release,
136 (int)strcspn(init_utsname()->version, " "),
137 init_utsname()->version);
138 pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
139 " disables this message.\n");
140 sched_show_task(t);
141 hung_task_show_lock = true;
142
143 if (sysctl_hung_task_all_cpu_backtrace)
144 hung_task_show_all_bt = true;
145 }
146
147 touch_nmi_watchdog();
148 }
149
150 /*
151 * To avoid extending the RCU grace period for an unbounded amount of time,
152 * periodically exit the critical section and enter a new one.
153 *
154 * For preemptible RCU it is sufficient to call rcu_read_unlock in order
155 * to exit the grace period. For classic RCU, a reschedule is required.
156 */
rcu_lock_break(struct task_struct * g,struct task_struct * t)157 static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
158 {
159 bool can_cont;
160
161 get_task_struct(g);
162 get_task_struct(t);
163 rcu_read_unlock();
164 cond_resched();
165 rcu_read_lock();
166 can_cont = pid_alive(g) && pid_alive(t);
167 put_task_struct(t);
168 put_task_struct(g);
169
170 return can_cont;
171 }
172
173 /*
174 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
175 * a really long time (120 seconds). If that happens, print out
176 * a warning.
177 */
check_hung_uninterruptible_tasks(unsigned long timeout)178 static void check_hung_uninterruptible_tasks(unsigned long timeout)
179 {
180 int max_count = sysctl_hung_task_check_count;
181 unsigned long last_break = jiffies;
182 struct task_struct *g, *t;
183 bool need_check = true;
184
185 /*
186 * If the system crashed already then all bets are off,
187 * do not report extra hung tasks:
188 */
189 if (test_taint(TAINT_DIE) || did_panic)
190 return;
191
192 hung_task_show_lock = false;
193 rcu_read_lock();
194 for_each_process_thread(g, t) {
195 if (!max_count--)
196 goto unlock;
197 if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
198 if (!rcu_lock_break(g, t))
199 goto unlock;
200 last_break = jiffies;
201 }
202 trace_android_vh_check_uninterruptible_tasks(t, timeout, &need_check);
203 if (need_check)
204 /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
205 if (READ_ONCE(t->__state) == TASK_UNINTERRUPTIBLE)
206 check_hung_task(t, timeout);
207 }
208 trace_android_vh_check_uninterruptible_tasks_dn(NULL);
209 unlock:
210 rcu_read_unlock();
211 if (hung_task_show_lock)
212 debug_show_all_locks();
213
214 if (hung_task_show_all_bt) {
215 hung_task_show_all_bt = false;
216 trigger_all_cpu_backtrace();
217 }
218
219 if (hung_task_call_panic)
220 panic("hung_task: blocked tasks");
221 }
222
hung_timeout_jiffies(unsigned long last_checked,unsigned long timeout)223 static long hung_timeout_jiffies(unsigned long last_checked,
224 unsigned long timeout)
225 {
226 /* timeout of 0 will disable the watchdog */
227 return timeout ? last_checked - jiffies + timeout * HZ :
228 MAX_SCHEDULE_TIMEOUT;
229 }
230
231 /*
232 * Process updating of timeout sysctl
233 */
proc_dohung_task_timeout_secs(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)234 int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
235 void *buffer, size_t *lenp, loff_t *ppos)
236 {
237 int ret;
238
239 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
240
241 if (ret || !write)
242 goto out;
243
244 wake_up_process(watchdog_task);
245
246 out:
247 return ret;
248 }
249
250 static atomic_t reset_hung_task = ATOMIC_INIT(0);
251
reset_hung_task_detector(void)252 void reset_hung_task_detector(void)
253 {
254 atomic_set(&reset_hung_task, 1);
255 }
256 EXPORT_SYMBOL_GPL(reset_hung_task_detector);
257
258 static bool hung_detector_suspended;
259
hungtask_pm_notify(struct notifier_block * self,unsigned long action,void * hcpu)260 static int hungtask_pm_notify(struct notifier_block *self,
261 unsigned long action, void *hcpu)
262 {
263 switch (action) {
264 case PM_SUSPEND_PREPARE:
265 case PM_HIBERNATION_PREPARE:
266 case PM_RESTORE_PREPARE:
267 hung_detector_suspended = true;
268 break;
269 case PM_POST_SUSPEND:
270 case PM_POST_HIBERNATION:
271 case PM_POST_RESTORE:
272 hung_detector_suspended = false;
273 break;
274 default:
275 break;
276 }
277 return NOTIFY_OK;
278 }
279
280 /*
281 * kthread which checks for tasks stuck in D state
282 */
watchdog(void * dummy)283 static int watchdog(void *dummy)
284 {
285 unsigned long hung_last_checked = jiffies;
286
287 set_user_nice(current, 0);
288
289 for ( ; ; ) {
290 unsigned long timeout = sysctl_hung_task_timeout_secs;
291 unsigned long interval = sysctl_hung_task_check_interval_secs;
292 long t;
293
294 if (interval == 0)
295 interval = timeout;
296 interval = min_t(unsigned long, interval, timeout);
297 t = hung_timeout_jiffies(hung_last_checked, interval);
298 if (t <= 0) {
299 if (!atomic_xchg(&reset_hung_task, 0) &&
300 !hung_detector_suspended)
301 check_hung_uninterruptible_tasks(timeout);
302 hung_last_checked = jiffies;
303 continue;
304 }
305 schedule_timeout_interruptible(t);
306 }
307
308 return 0;
309 }
310
hung_task_init(void)311 static int __init hung_task_init(void)
312 {
313 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
314
315 /* Disable hung task detector on suspend */
316 pm_notifier(hungtask_pm_notify, 0);
317
318 watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
319
320 return 0;
321 }
322 subsys_initcall(hung_task_init);
323