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