• 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 #include <trace/hooks/hung_task.h>
28 
29 /*
30  * The number of tasks checked:
31  */
32 static int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
33 
34 /*
35  * Limit number of tasks checked in a batch.
36  *
37  * This value controls the preemptibility of khungtaskd since preemption
38  * is disabled during the critical section. It also controls the size of
39  * the RCU grace period. So it needs to be upper-bound.
40  */
41 #define HUNG_TASK_LOCK_BREAK (HZ / 10)
42 
43 /*
44  * Zero means infinite timeout - no checking done:
45  */
46 unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
47 EXPORT_SYMBOL_GPL(sysctl_hung_task_timeout_secs);
48 
49 /*
50  * Zero (default value) means use sysctl_hung_task_timeout_secs:
51  */
52 static unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
53 
54 static 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 static 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 
92 
93 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
debug_show_blocker(struct task_struct * task)94 static void debug_show_blocker(struct task_struct *task)
95 {
96 	struct task_struct *g, *t;
97 	unsigned long owner;
98 	struct mutex *lock;
99 
100 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "No rcu lock held");
101 
102 	lock = READ_ONCE(task->blocker_mutex);
103 	if (!lock)
104 		return;
105 
106 	owner = mutex_get_owner(lock);
107 	if (unlikely(!owner)) {
108 		pr_err("INFO: task %s:%d is blocked on a mutex, but the owner is not found.\n",
109 			task->comm, task->pid);
110 		return;
111 	}
112 
113 	/* Ensure the owner information is correct. */
114 	for_each_process_thread(g, t) {
115 		if ((unsigned long)t == owner) {
116 			pr_err("INFO: task %s:%d is blocked on a mutex likely owned by task %s:%d.\n",
117 				task->comm, task->pid, t->comm, t->pid);
118 			sched_show_task(t);
119 			return;
120 		}
121 	}
122 }
123 #else
debug_show_blocker(struct task_struct * task)124 static inline void debug_show_blocker(struct task_struct *task)
125 {
126 }
127 #endif
128 
check_hung_task(struct task_struct * t,unsigned long timeout)129 static void check_hung_task(struct task_struct *t, unsigned long timeout)
130 {
131 	unsigned long switch_count = t->nvcsw + t->nivcsw;
132 
133 	/*
134 	 * Ensure the task is not frozen.
135 	 * Also, skip vfork and any other user process that freezer should skip.
136 	 */
137 	if (unlikely(READ_ONCE(t->__state) & TASK_FROZEN))
138 		return;
139 
140 	/*
141 	 * When a freshly created task is scheduled once, changes its state to
142 	 * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
143 	 * musn't be checked.
144 	 */
145 	if (unlikely(!switch_count))
146 		return;
147 
148 	if (switch_count != t->last_switch_count) {
149 		t->last_switch_count = switch_count;
150 		t->last_switch_time = jiffies;
151 		return;
152 	}
153 	if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
154 		return;
155 
156 	trace_sched_process_hang(t);
157 
158 	if (sysctl_hung_task_panic) {
159 		console_verbose();
160 		hung_task_show_lock = true;
161 		hung_task_call_panic = true;
162 	}
163 
164 	/*
165 	 * Ok, the task did not get scheduled for more than 2 minutes,
166 	 * complain:
167 	 */
168 	if (sysctl_hung_task_warnings || hung_task_call_panic) {
169 		if (sysctl_hung_task_warnings > 0)
170 			sysctl_hung_task_warnings--;
171 		pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
172 		       t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
173 		pr_err("      %s %s %.*s\n",
174 			print_tainted(), init_utsname()->release,
175 			(int)strcspn(init_utsname()->version, " "),
176 			init_utsname()->version);
177 		pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
178 			" disables this message.\n");
179 		sched_show_task(t);
180 		debug_show_blocker(t);
181 		hung_task_show_lock = true;
182 
183 		if (sysctl_hung_task_all_cpu_backtrace)
184 			hung_task_show_all_bt = true;
185 		if (!sysctl_hung_task_warnings)
186 			pr_info("Future hung task reports are suppressed, see sysctl kernel.hung_task_warnings\n");
187 	}
188 
189 	touch_nmi_watchdog();
190 }
191 
192 /*
193  * To avoid extending the RCU grace period for an unbounded amount of time,
194  * periodically exit the critical section and enter a new one.
195  *
196  * For preemptible RCU it is sufficient to call rcu_read_unlock in order
197  * to exit the grace period. For classic RCU, a reschedule is required.
198  */
rcu_lock_break(struct task_struct * g,struct task_struct * t)199 static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
200 {
201 	bool can_cont;
202 
203 	get_task_struct(g);
204 	get_task_struct(t);
205 	rcu_read_unlock();
206 	cond_resched();
207 	rcu_read_lock();
208 	can_cont = pid_alive(g) && pid_alive(t);
209 	put_task_struct(t);
210 	put_task_struct(g);
211 
212 	return can_cont;
213 }
214 
215 /*
216  * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
217  * a really long time (120 seconds). If that happens, print out
218  * a warning.
219  */
check_hung_uninterruptible_tasks(unsigned long timeout)220 static void check_hung_uninterruptible_tasks(unsigned long timeout)
221 {
222 	int max_count = sysctl_hung_task_check_count;
223 	unsigned long last_break = jiffies;
224 	struct task_struct *g, *t;
225 	bool need_check = true;
226 
227 	/*
228 	 * If the system crashed already then all bets are off,
229 	 * do not report extra hung tasks:
230 	 */
231 	if (test_taint(TAINT_DIE) || did_panic)
232 		return;
233 
234 	hung_task_show_lock = false;
235 	rcu_read_lock();
236 	for_each_process_thread(g, t) {
237 		unsigned int state;
238 
239 		if (!max_count--)
240 			goto unlock;
241 		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
242 			if (!rcu_lock_break(g, t))
243 				goto unlock;
244 			last_break = jiffies;
245 		}
246 		/*
247 		 * skip the TASK_KILLABLE tasks -- these can be killed
248 		 * skip the TASK_IDLE tasks -- those are genuinely idle
249 		 */
250 		trace_android_vh_check_uninterruptible_tasks(t, timeout, &need_check);
251 		if (need_check) {
252 			state = READ_ONCE(t->__state);
253 			if ((state & TASK_UNINTERRUPTIBLE) &&
254 			    !(state & TASK_WAKEKILL) &&
255 			    !(state & TASK_NOLOAD))
256 				check_hung_task(t, timeout);
257 		}
258 	}
259 	trace_android_vh_check_uninterruptible_tasks_dn(NULL);
260  unlock:
261 	rcu_read_unlock();
262 	if (hung_task_show_lock)
263 		debug_show_all_locks();
264 
265 	if (hung_task_show_all_bt) {
266 		hung_task_show_all_bt = false;
267 		trigger_all_cpu_backtrace();
268 	}
269 
270 	if (hung_task_call_panic)
271 		panic("hung_task: blocked tasks");
272 }
273 
hung_timeout_jiffies(unsigned long last_checked,unsigned long timeout)274 static long hung_timeout_jiffies(unsigned long last_checked,
275 				 unsigned long timeout)
276 {
277 	/* timeout of 0 will disable the watchdog */
278 	return timeout ? last_checked - jiffies + timeout * HZ :
279 		MAX_SCHEDULE_TIMEOUT;
280 }
281 
282 #ifdef CONFIG_SYSCTL
283 /*
284  * Process updating of timeout sysctl
285  */
proc_dohung_task_timeout_secs(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)286 static int proc_dohung_task_timeout_secs(const struct ctl_table *table, int write,
287 				  void *buffer,
288 				  size_t *lenp, loff_t *ppos)
289 {
290 	int ret;
291 
292 	ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
293 
294 	if (ret || !write)
295 		goto out;
296 
297 	wake_up_process(watchdog_task);
298 
299  out:
300 	return ret;
301 }
302 
303 /*
304  * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
305  * and hung_task_check_interval_secs
306  */
307 static const unsigned long hung_task_timeout_max = (LONG_MAX / HZ);
308 static struct ctl_table hung_task_sysctls[] = {
309 #ifdef CONFIG_SMP
310 	{
311 		.procname	= "hung_task_all_cpu_backtrace",
312 		.data		= &sysctl_hung_task_all_cpu_backtrace,
313 		.maxlen		= sizeof(int),
314 		.mode		= 0644,
315 		.proc_handler	= proc_dointvec_minmax,
316 		.extra1		= SYSCTL_ZERO,
317 		.extra2		= SYSCTL_ONE,
318 	},
319 #endif /* CONFIG_SMP */
320 	{
321 		.procname	= "hung_task_panic",
322 		.data		= &sysctl_hung_task_panic,
323 		.maxlen		= sizeof(int),
324 		.mode		= 0644,
325 		.proc_handler	= proc_dointvec_minmax,
326 		.extra1		= SYSCTL_ZERO,
327 		.extra2		= SYSCTL_ONE,
328 	},
329 	{
330 		.procname	= "hung_task_check_count",
331 		.data		= &sysctl_hung_task_check_count,
332 		.maxlen		= sizeof(int),
333 		.mode		= 0644,
334 		.proc_handler	= proc_dointvec_minmax,
335 		.extra1		= SYSCTL_ZERO,
336 	},
337 	{
338 		.procname	= "hung_task_timeout_secs",
339 		.data		= &sysctl_hung_task_timeout_secs,
340 		.maxlen		= sizeof(unsigned long),
341 		.mode		= 0644,
342 		.proc_handler	= proc_dohung_task_timeout_secs,
343 		.extra2		= (void *)&hung_task_timeout_max,
344 	},
345 	{
346 		.procname	= "hung_task_check_interval_secs",
347 		.data		= &sysctl_hung_task_check_interval_secs,
348 		.maxlen		= sizeof(unsigned long),
349 		.mode		= 0644,
350 		.proc_handler	= proc_dohung_task_timeout_secs,
351 		.extra2		= (void *)&hung_task_timeout_max,
352 	},
353 	{
354 		.procname	= "hung_task_warnings",
355 		.data		= &sysctl_hung_task_warnings,
356 		.maxlen		= sizeof(int),
357 		.mode		= 0644,
358 		.proc_handler	= proc_dointvec_minmax,
359 		.extra1		= SYSCTL_NEG_ONE,
360 	},
361 };
362 
hung_task_sysctl_init(void)363 static void __init hung_task_sysctl_init(void)
364 {
365 	register_sysctl_init("kernel", hung_task_sysctls);
366 }
367 #else
368 #define hung_task_sysctl_init() do { } while (0)
369 #endif /* CONFIG_SYSCTL */
370 
371 
372 static atomic_t reset_hung_task = ATOMIC_INIT(0);
373 
reset_hung_task_detector(void)374 void reset_hung_task_detector(void)
375 {
376 	atomic_set(&reset_hung_task, 1);
377 }
378 EXPORT_SYMBOL_GPL(reset_hung_task_detector);
379 
380 static bool hung_detector_suspended;
381 
hungtask_pm_notify(struct notifier_block * self,unsigned long action,void * hcpu)382 static int hungtask_pm_notify(struct notifier_block *self,
383 			      unsigned long action, void *hcpu)
384 {
385 	switch (action) {
386 	case PM_SUSPEND_PREPARE:
387 	case PM_HIBERNATION_PREPARE:
388 	case PM_RESTORE_PREPARE:
389 		hung_detector_suspended = true;
390 		break;
391 	case PM_POST_SUSPEND:
392 	case PM_POST_HIBERNATION:
393 	case PM_POST_RESTORE:
394 		hung_detector_suspended = false;
395 		break;
396 	default:
397 		break;
398 	}
399 	return NOTIFY_OK;
400 }
401 
402 /*
403  * kthread which checks for tasks stuck in D state
404  */
watchdog(void * dummy)405 static int watchdog(void *dummy)
406 {
407 	unsigned long hung_last_checked = jiffies;
408 
409 	set_user_nice(current, 0);
410 
411 	for ( ; ; ) {
412 		unsigned long timeout = sysctl_hung_task_timeout_secs;
413 		unsigned long interval = sysctl_hung_task_check_interval_secs;
414 		long t;
415 
416 		if (interval == 0)
417 			interval = timeout;
418 		interval = min_t(unsigned long, interval, timeout);
419 		t = hung_timeout_jiffies(hung_last_checked, interval);
420 		if (t <= 0) {
421 			if (!atomic_xchg(&reset_hung_task, 0) &&
422 			    !hung_detector_suspended)
423 				check_hung_uninterruptible_tasks(timeout);
424 			hung_last_checked = jiffies;
425 			continue;
426 		}
427 		schedule_timeout_interruptible(t);
428 	}
429 
430 	return 0;
431 }
432 
hung_task_init(void)433 static int __init hung_task_init(void)
434 {
435 	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
436 
437 	/* Disable hung task detector on suspend */
438 	pm_notifier(hungtask_pm_notify, 0);
439 
440 	watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
441 	hung_task_sysctl_init();
442 
443 	return 0;
444 }
445 subsys_initcall(hung_task_init);
446