1
2
3
4
5
6
7
8#include <linux/mm.h>
9#include <linux/cpu.h>
10#include <linux/nmi.h>
11#include <linux/init.h>
12#include <linux/delay.h>
13#include <linux/freezer.h>
14#include <linux/kthread.h>
15#include <linux/lockdep.h>
16#include <linux/export.h>
17#include <linux/sysctl.h>
18#include <linux/suspend.h>
19#include <linux/utsname.h>
20#include <linux/sched/signal.h>
21#include <linux/sched/debug.h>
22
23#include <trace/events/sched.h>
24
25
26
27
28int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
29
30
31
32
33
34
35
36
37#define HUNG_TASK_LOCK_BREAK (HZ / 10)
38
39
40
41
42unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
43
44int __read_mostly sysctl_hung_task_warnings = 10;
45
46static int __read_mostly did_panic;
47static bool hung_task_show_lock;
48static bool hung_task_call_panic;
49
50static struct task_struct *watchdog_task;
51
52
53
54
55
56unsigned int __read_mostly sysctl_hung_task_panic =
57 CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
58
59static int __init hung_task_panic_setup(char *str)
60{
61 int rc = kstrtouint(str, 0, &sysctl_hung_task_panic);
62
63 if (rc)
64 return rc;
65 return 1;
66}
67__setup("hung_task_panic=", hung_task_panic_setup);
68
69static int
70hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
71{
72 did_panic = 1;
73
74 return NOTIFY_DONE;
75}
76
77static struct notifier_block panic_block = {
78 .notifier_call = hung_task_panic,
79};
80
81static void check_hung_task(struct task_struct *t, unsigned long timeout)
82{
83 unsigned long switch_count = t->nvcsw + t->nivcsw;
84
85
86
87
88
89 if (unlikely(t->flags & (PF_FROZEN | PF_FREEZER_SKIP)))
90 return;
91
92
93
94
95
96
97 if (unlikely(!switch_count))
98 return;
99
100 if (switch_count != t->last_switch_count) {
101 t->last_switch_count = switch_count;
102 return;
103 }
104
105 trace_sched_process_hang(t);
106
107 if (!sysctl_hung_task_warnings && !sysctl_hung_task_panic)
108 return;
109
110
111
112
113
114 if (sysctl_hung_task_warnings) {
115 if (sysctl_hung_task_warnings > 0)
116 sysctl_hung_task_warnings--;
117 pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
118 t->comm, t->pid, timeout);
119 pr_err(" %s %s %.*s\n",
120 print_tainted(), init_utsname()->release,
121 (int)strcspn(init_utsname()->version, " "),
122 init_utsname()->version);
123 pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
124 " disables this message.\n");
125 sched_show_task(t);
126 hung_task_show_lock = true;
127 }
128
129 touch_nmi_watchdog();
130
131 if (sysctl_hung_task_panic) {
132 hung_task_show_lock = true;
133 hung_task_call_panic = true;
134 }
135}
136
137
138
139
140
141
142
143
144static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
145{
146 bool can_cont;
147
148 get_task_struct(g);
149 get_task_struct(t);
150 rcu_read_unlock();
151 cond_resched();
152 rcu_read_lock();
153 can_cont = pid_alive(g) && pid_alive(t);
154 put_task_struct(t);
155 put_task_struct(g);
156
157 return can_cont;
158}
159
160
161
162
163
164
165static void check_hung_uninterruptible_tasks(unsigned long timeout)
166{
167 int max_count = sysctl_hung_task_check_count;
168 unsigned long last_break = jiffies;
169 struct task_struct *g, *t;
170
171
172
173
174
175 if (test_taint(TAINT_DIE) || did_panic)
176 return;
177
178 hung_task_show_lock = false;
179 rcu_read_lock();
180 for_each_process_thread(g, t) {
181 if (!max_count--)
182 goto unlock;
183 if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
184 if (!rcu_lock_break(g, t))
185 goto unlock;
186 last_break = jiffies;
187 }
188
189 if (READ_ONCE(t->__state) == TASK_UNINTERRUPTIBLE)
190 check_hung_task(t, timeout);
191 }
192 unlock:
193 rcu_read_unlock();
194 if (hung_task_show_lock)
195 debug_show_all_locks();
196 if (hung_task_call_panic) {
197 trigger_all_cpu_backtrace();
198 panic("hung_task: blocked tasks");
199 }
200}
201
202static long hung_timeout_jiffies(unsigned long last_checked,
203 unsigned long timeout)
204{
205
206 return timeout ? last_checked - jiffies + timeout * HZ :
207 MAX_SCHEDULE_TIMEOUT;
208}
209
210
211
212
213int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
214 void __user *buffer,
215 size_t *lenp, loff_t *ppos)
216{
217 int ret;
218
219 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
220
221 if (ret || !write)
222 goto out;
223
224 wake_up_process(watchdog_task);
225
226 out:
227 return ret;
228}
229
230static atomic_t reset_hung_task = ATOMIC_INIT(0);
231
232void reset_hung_task_detector(void)
233{
234 atomic_set(&reset_hung_task, 1);
235}
236EXPORT_SYMBOL_GPL(reset_hung_task_detector);
237
238static bool hung_detector_suspended;
239
240static int hungtask_pm_notify(struct notifier_block *self,
241 unsigned long action, void *hcpu)
242{
243 switch (action) {
244 case PM_SUSPEND_PREPARE:
245 case PM_HIBERNATION_PREPARE:
246 case PM_RESTORE_PREPARE:
247 hung_detector_suspended = true;
248 break;
249 case PM_POST_SUSPEND:
250 case PM_POST_HIBERNATION:
251 case PM_POST_RESTORE:
252 hung_detector_suspended = false;
253 break;
254 default:
255 break;
256 }
257 return NOTIFY_OK;
258}
259
260
261
262
263static int watchdog(void *dummy)
264{
265 unsigned long hung_last_checked = jiffies;
266
267 set_user_nice(current, 0);
268
269 for ( ; ; ) {
270 unsigned long timeout = sysctl_hung_task_timeout_secs;
271 long t = hung_timeout_jiffies(hung_last_checked, timeout);
272
273 if (t <= 0) {
274 if (!atomic_xchg(&reset_hung_task, 0) &&
275 !hung_detector_suspended)
276 check_hung_uninterruptible_tasks(timeout);
277 hung_last_checked = jiffies;
278 continue;
279 }
280 schedule_timeout_interruptible(t);
281 }
282
283 return 0;
284}
285
286static int __init hung_task_init(void)
287{
288 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
289
290
291 pm_notifier(hungtask_pm_notify, 0);
292
293 watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
294
295 return 0;
296}
297subsys_initcall(hung_task_init);
298