• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static unsigned int __read_mostly sysctl_hung_task_all_cpu_backtrace;
69 #else
70 #define sysctl_hung_task_all_cpu_backtrace 0
71 #endif /* CONFIG_SMP */
72 
73 /*
74  * Should we panic (and reboot, if panic_timeout= is set) when a
75  * hung task is detected:
76  */
77 unsigned int __read_mostly sysctl_hung_task_panic =
78 				IS_ENABLED(CONFIG_BOOTPARAM_HUNG_TASK_PANIC);
79 
80 static int
hung_task_panic(struct notifier_block * this,unsigned long event,void * ptr)81 hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
82 {
83 	did_panic = 1;
84 
85 	return NOTIFY_DONE;
86 }
87 
88 static struct notifier_block panic_block = {
89 	.notifier_call = hung_task_panic,
90 };
91 
check_hung_task(struct task_struct * t,unsigned long timeout)92 static void check_hung_task(struct task_struct *t, unsigned long timeout)
93 {
94 	unsigned long switch_count = t->nvcsw + t->nivcsw;
95 
96 	/*
97 	 * Ensure the task is not frozen.
98 	 * Also, skip vfork and any other user process that freezer should skip.
99 	 */
100 	if (unlikely(READ_ONCE(t->__state) & TASK_FROZEN))
101 		return;
102 
103 	/*
104 	 * When a freshly created task is scheduled once, changes its state to
105 	 * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
106 	 * musn't be checked.
107 	 */
108 	if (unlikely(!switch_count))
109 		return;
110 
111 	if (switch_count != t->last_switch_count) {
112 		t->last_switch_count = switch_count;
113 		t->last_switch_time = jiffies;
114 		return;
115 	}
116 	if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
117 		return;
118 
119 	trace_sched_process_hang(t);
120 
121 	if (sysctl_hung_task_panic) {
122 		console_verbose();
123 		hung_task_show_lock = true;
124 		hung_task_call_panic = true;
125 	}
126 
127 	/*
128 	 * Ok, the task did not get scheduled for more than 2 minutes,
129 	 * complain:
130 	 */
131 	if (sysctl_hung_task_warnings) {
132 		if (sysctl_hung_task_warnings > 0)
133 			sysctl_hung_task_warnings--;
134 		pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
135 		       t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
136 		pr_err("      %s %s %.*s\n",
137 			print_tainted(), init_utsname()->release,
138 			(int)strcspn(init_utsname()->version, " "),
139 			init_utsname()->version);
140 		pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
141 			" disables this message.\n");
142 		sched_show_task(t);
143 		hung_task_show_lock = true;
144 
145 		if (sysctl_hung_task_all_cpu_backtrace)
146 			hung_task_show_all_bt = true;
147 	}
148 
149 	touch_nmi_watchdog();
150 }
151 
152 /*
153  * To avoid extending the RCU grace period for an unbounded amount of time,
154  * periodically exit the critical section and enter a new one.
155  *
156  * For preemptible RCU it is sufficient to call rcu_read_unlock in order
157  * to exit the grace period. For classic RCU, a reschedule is required.
158  */
rcu_lock_break(struct task_struct * g,struct task_struct * t)159 static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
160 {
161 	bool can_cont;
162 
163 	get_task_struct(g);
164 	get_task_struct(t);
165 	rcu_read_unlock();
166 	cond_resched();
167 	rcu_read_lock();
168 	can_cont = pid_alive(g) && pid_alive(t);
169 	put_task_struct(t);
170 	put_task_struct(g);
171 
172 	return can_cont;
173 }
174 
175 /*
176  * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
177  * a really long time (120 seconds). If that happens, print out
178  * a warning.
179  */
check_hung_uninterruptible_tasks(unsigned long timeout)180 static void check_hung_uninterruptible_tasks(unsigned long timeout)
181 {
182 	int max_count = sysctl_hung_task_check_count;
183 	unsigned long last_break = jiffies;
184 	struct task_struct *g, *t;
185 	bool need_check = true;
186 
187 	/*
188 	 * If the system crashed already then all bets are off,
189 	 * do not report extra hung tasks:
190 	 */
191 	if (test_taint(TAINT_DIE) || did_panic)
192 		return;
193 
194 	hung_task_show_lock = false;
195 	rcu_read_lock();
196 	for_each_process_thread(g, t) {
197 		unsigned int state;
198 
199 		if (!max_count--)
200 			goto unlock;
201 		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
202 			if (!rcu_lock_break(g, t))
203 				goto unlock;
204 			last_break = jiffies;
205 		}
206 		/*
207 		 * skip the TASK_KILLABLE tasks -- these can be killed
208 		 * skip the TASK_IDLE tasks -- those are genuinely idle
209 		 */
210 		trace_android_vh_check_uninterrupt_tasks(t, timeout, &need_check);
211 		if (need_check) {
212 			state = READ_ONCE(t->__state);
213 			if ((state & TASK_UNINTERRUPTIBLE) &&
214 			    !(state & TASK_WAKEKILL) &&
215 			    !(state & TASK_NOLOAD))
216 				check_hung_task(t, timeout);
217 		}
218 	}
219 	trace_android_vh_check_uninterrupt_tasks_done(NULL);
220  unlock:
221 	rcu_read_unlock();
222 	if (hung_task_show_lock)
223 		debug_show_all_locks();
224 
225 	if (hung_task_show_all_bt) {
226 		hung_task_show_all_bt = false;
227 		trigger_all_cpu_backtrace();
228 	}
229 
230 	if (hung_task_call_panic)
231 		panic("hung_task: blocked tasks");
232 }
233 
hung_timeout_jiffies(unsigned long last_checked,unsigned long timeout)234 static long hung_timeout_jiffies(unsigned long last_checked,
235 				 unsigned long timeout)
236 {
237 	/* timeout of 0 will disable the watchdog */
238 	return timeout ? last_checked - jiffies + timeout * HZ :
239 		MAX_SCHEDULE_TIMEOUT;
240 }
241 
242 #ifdef CONFIG_SYSCTL
243 /*
244  * Process updating of timeout sysctl
245  */
proc_dohung_task_timeout_secs(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)246 static int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
247 				  void *buffer,
248 				  size_t *lenp, loff_t *ppos)
249 {
250 	int ret;
251 
252 	ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
253 
254 	if (ret || !write)
255 		goto out;
256 
257 	wake_up_process(watchdog_task);
258 
259  out:
260 	return ret;
261 }
262 
263 /*
264  * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
265  * and hung_task_check_interval_secs
266  */
267 static const unsigned long hung_task_timeout_max = (LONG_MAX / HZ);
268 static struct ctl_table hung_task_sysctls[] = {
269 #ifdef CONFIG_SMP
270 	{
271 		.procname	= "hung_task_all_cpu_backtrace",
272 		.data		= &sysctl_hung_task_all_cpu_backtrace,
273 		.maxlen		= sizeof(int),
274 		.mode		= 0644,
275 		.proc_handler	= proc_dointvec_minmax,
276 		.extra1		= SYSCTL_ZERO,
277 		.extra2		= SYSCTL_ONE,
278 	},
279 #endif /* CONFIG_SMP */
280 	{
281 		.procname	= "hung_task_panic",
282 		.data		= &sysctl_hung_task_panic,
283 		.maxlen		= sizeof(int),
284 		.mode		= 0644,
285 		.proc_handler	= proc_dointvec_minmax,
286 		.extra1		= SYSCTL_ZERO,
287 		.extra2		= SYSCTL_ONE,
288 	},
289 	{
290 		.procname	= "hung_task_check_count",
291 		.data		= &sysctl_hung_task_check_count,
292 		.maxlen		= sizeof(int),
293 		.mode		= 0644,
294 		.proc_handler	= proc_dointvec_minmax,
295 		.extra1		= SYSCTL_ZERO,
296 	},
297 	{
298 		.procname	= "hung_task_timeout_secs",
299 		.data		= &sysctl_hung_task_timeout_secs,
300 		.maxlen		= sizeof(unsigned long),
301 		.mode		= 0644,
302 		.proc_handler	= proc_dohung_task_timeout_secs,
303 		.extra2		= (void *)&hung_task_timeout_max,
304 	},
305 	{
306 		.procname	= "hung_task_check_interval_secs",
307 		.data		= &sysctl_hung_task_check_interval_secs,
308 		.maxlen		= sizeof(unsigned long),
309 		.mode		= 0644,
310 		.proc_handler	= proc_dohung_task_timeout_secs,
311 		.extra2		= (void *)&hung_task_timeout_max,
312 	},
313 	{
314 		.procname	= "hung_task_warnings",
315 		.data		= &sysctl_hung_task_warnings,
316 		.maxlen		= sizeof(int),
317 		.mode		= 0644,
318 		.proc_handler	= proc_dointvec_minmax,
319 		.extra1		= SYSCTL_NEG_ONE,
320 	},
321 	{}
322 };
323 
hung_task_sysctl_init(void)324 static void __init hung_task_sysctl_init(void)
325 {
326 	register_sysctl_init("kernel", hung_task_sysctls);
327 }
328 #else
329 #define hung_task_sysctl_init() do { } while (0)
330 #endif /* CONFIG_SYSCTL */
331 
332 
333 static atomic_t reset_hung_task = ATOMIC_INIT(0);
334 
reset_hung_task_detector(void)335 void reset_hung_task_detector(void)
336 {
337 	atomic_set(&reset_hung_task, 1);
338 }
339 EXPORT_SYMBOL_GPL(reset_hung_task_detector);
340 
341 static bool hung_detector_suspended;
342 
hungtask_pm_notify(struct notifier_block * self,unsigned long action,void * hcpu)343 static int hungtask_pm_notify(struct notifier_block *self,
344 			      unsigned long action, void *hcpu)
345 {
346 	switch (action) {
347 	case PM_SUSPEND_PREPARE:
348 	case PM_HIBERNATION_PREPARE:
349 	case PM_RESTORE_PREPARE:
350 		hung_detector_suspended = true;
351 		break;
352 	case PM_POST_SUSPEND:
353 	case PM_POST_HIBERNATION:
354 	case PM_POST_RESTORE:
355 		hung_detector_suspended = false;
356 		break;
357 	default:
358 		break;
359 	}
360 	return NOTIFY_OK;
361 }
362 
363 /*
364  * kthread which checks for tasks stuck in D state
365  */
watchdog(void * dummy)366 static int watchdog(void *dummy)
367 {
368 	unsigned long hung_last_checked = jiffies;
369 
370 	set_user_nice(current, 0);
371 
372 	for ( ; ; ) {
373 		unsigned long timeout = sysctl_hung_task_timeout_secs;
374 		unsigned long interval = sysctl_hung_task_check_interval_secs;
375 		long t;
376 
377 		if (interval == 0)
378 			interval = timeout;
379 		interval = min_t(unsigned long, interval, timeout);
380 		t = hung_timeout_jiffies(hung_last_checked, interval);
381 		if (t <= 0) {
382 			if (!atomic_xchg(&reset_hung_task, 0) &&
383 			    !hung_detector_suspended)
384 				check_hung_uninterruptible_tasks(timeout);
385 			hung_last_checked = jiffies;
386 			continue;
387 		}
388 		schedule_timeout_interruptible(t);
389 	}
390 
391 	return 0;
392 }
393 
hung_task_init(void)394 static int __init hung_task_init(void)
395 {
396 	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
397 
398 	/* Disable hung task detector on suspend */
399 	pm_notifier(hungtask_pm_notify, 0);
400 
401 	watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
402 	hung_task_sysctl_init();
403 
404 	return 0;
405 }
406 subsys_initcall(hung_task_init);
407