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