1 // SPDX-License-Identifier: GPL-2.0-only
5 * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
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 #include <linux/hung_task.h>
27 #include <trace/events/sched.h>
30 * The number of tasks checked:
32 static int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
35 * Total number of tasks detected as hung since boot:
37 static unsigned long __read_mostly sysctl_hung_task_detect_count;
40 * Limit number of tasks checked in a batch.
42 * This value controls the preemptibility of khungtaskd since preemption
43 * is disabled during the critical section. It also controls the size of
44 * the RCU grace period. So it needs to be upper-bound.
46 #define HUNG_TASK_LOCK_BREAK (HZ / 10)
49 * Zero means infinite timeout - no checking done:
51 unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
52 EXPORT_SYMBOL_GPL(sysctl_hung_task_timeout_secs);
55 * Zero (default value) means use sysctl_hung_task_timeout_secs:
57 static unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
59 static int __read_mostly sysctl_hung_task_warnings = 10;
61 static int __read_mostly did_panic;
62 static bool hung_task_show_lock;
63 static bool hung_task_call_panic;
64 static bool hung_task_show_all_bt;
66 static struct task_struct *watchdog_task;
70 * Should we dump all CPUs backtraces in a hung task event?
71 * Defaults to 0, can be changed via sysctl.
73 static unsigned int __read_mostly sysctl_hung_task_all_cpu_backtrace;
75 #define sysctl_hung_task_all_cpu_backtrace 0
76 #endif /* CONFIG_SMP */
79 * Should we panic (and reboot, if panic_timeout= is set) when a
80 * hung task is detected:
82 static unsigned int __read_mostly sysctl_hung_task_panic =
83 IS_ENABLED(CONFIG_BOOTPARAM_HUNG_TASK_PANIC);
86 hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
93 static struct notifier_block panic_block = {
94 .notifier_call = hung_task_panic,
98 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
99 static void debug_show_blocker(struct task_struct *task)
101 struct task_struct *g, *t;
102 unsigned long owner, blocker, blocker_type;
104 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "No rcu lock held");
106 blocker = READ_ONCE(task->blocker);
110 blocker_type = hung_task_get_blocker_type(blocker);
112 switch (blocker_type) {
113 case BLOCKER_TYPE_MUTEX:
114 owner = mutex_get_owner(
115 (struct mutex *)hung_task_blocker_to_lock(blocker));
117 case BLOCKER_TYPE_SEM:
118 owner = sem_last_holder(
119 (struct semaphore *)hung_task_blocker_to_lock(blocker));
127 if (unlikely(!owner)) {
128 switch (blocker_type) {
129 case BLOCKER_TYPE_MUTEX:
130 pr_err("INFO: task %s:%d is blocked on a mutex, but the owner is not found.\n",
131 task->comm, task->pid);
133 case BLOCKER_TYPE_SEM:
134 pr_err("INFO: task %s:%d is blocked on a semaphore, but the last holder is not found.\n",
135 task->comm, task->pid);
141 /* Ensure the owner information is correct. */
142 for_each_process_thread(g, t) {
143 if ((unsigned long)t != owner)
146 switch (blocker_type) {
147 case BLOCKER_TYPE_MUTEX:
148 pr_err("INFO: task %s:%d is blocked on a mutex likely owned by task %s:%d.\n",
149 task->comm, task->pid, t->comm, t->pid);
151 case BLOCKER_TYPE_SEM:
152 pr_err("INFO: task %s:%d blocked on a semaphore likely last held by task %s:%d\n",
153 task->comm, task->pid, t->comm, t->pid);
161 static inline void debug_show_blocker(struct task_struct *task)
166 static void check_hung_task(struct task_struct *t, unsigned long timeout)
168 unsigned long switch_count = t->nvcsw + t->nivcsw;
171 * Ensure the task is not frozen.
172 * Also, skip vfork and any other user process that freezer should skip.
174 if (unlikely(READ_ONCE(t->__state) & TASK_FROZEN))
178 * When a freshly created task is scheduled once, changes its state to
179 * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
182 if (unlikely(!switch_count))
185 if (switch_count != t->last_switch_count) {
186 t->last_switch_count = switch_count;
187 t->last_switch_time = jiffies;
190 if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
194 * This counter tracks the total number of tasks detected as hung
197 sysctl_hung_task_detect_count++;
199 trace_sched_process_hang(t);
201 if (sysctl_hung_task_panic) {
203 hung_task_show_lock = true;
204 hung_task_call_panic = true;
208 * Ok, the task did not get scheduled for more than 2 minutes,
211 if (sysctl_hung_task_warnings || hung_task_call_panic) {
212 if (sysctl_hung_task_warnings > 0)
213 sysctl_hung_task_warnings--;
214 pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
215 t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
216 pr_err(" %s %s %.*s\n",
217 print_tainted(), init_utsname()->release,
218 (int)strcspn(init_utsname()->version, " "),
219 init_utsname()->version);
220 if (t->flags & PF_POSTCOREDUMP)
221 pr_err(" Blocked by coredump.\n");
222 pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
223 " disables this message.\n");
225 debug_show_blocker(t);
226 hung_task_show_lock = true;
228 if (sysctl_hung_task_all_cpu_backtrace)
229 hung_task_show_all_bt = true;
230 if (!sysctl_hung_task_warnings)
231 pr_info("Future hung task reports are suppressed, see sysctl kernel.hung_task_warnings\n");
234 touch_nmi_watchdog();
238 * To avoid extending the RCU grace period for an unbounded amount of time,
239 * periodically exit the critical section and enter a new one.
241 * For preemptible RCU it is sufficient to call rcu_read_unlock in order
242 * to exit the grace period. For classic RCU, a reschedule is required.
244 static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
253 can_cont = pid_alive(g) && pid_alive(t);
261 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
262 * a really long time (120 seconds). If that happens, print out
265 static void check_hung_uninterruptible_tasks(unsigned long timeout)
267 int max_count = sysctl_hung_task_check_count;
268 unsigned long last_break = jiffies;
269 struct task_struct *g, *t;
272 * If the system crashed already then all bets are off,
273 * do not report extra hung tasks:
275 if (test_taint(TAINT_DIE) || did_panic)
278 hung_task_show_lock = false;
280 for_each_process_thread(g, t) {
285 if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
286 if (!rcu_lock_break(g, t))
288 last_break = jiffies;
291 * skip the TASK_KILLABLE tasks -- these can be killed
292 * skip the TASK_IDLE tasks -- those are genuinely idle
294 state = READ_ONCE(t->__state);
295 if ((state & TASK_UNINTERRUPTIBLE) &&
296 !(state & TASK_WAKEKILL) &&
297 !(state & TASK_NOLOAD))
298 check_hung_task(t, timeout);
302 if (hung_task_show_lock)
303 debug_show_all_locks();
305 if (hung_task_show_all_bt) {
306 hung_task_show_all_bt = false;
307 trigger_all_cpu_backtrace();
310 if (hung_task_call_panic)
311 panic("hung_task: blocked tasks");
314 static long hung_timeout_jiffies(unsigned long last_checked,
315 unsigned long timeout)
317 /* timeout of 0 will disable the watchdog */
318 return timeout ? last_checked - jiffies + timeout * HZ :
319 MAX_SCHEDULE_TIMEOUT;
324 * Process updating of timeout sysctl
326 static int proc_dohung_task_timeout_secs(const struct ctl_table *table, int write,
328 size_t *lenp, loff_t *ppos)
332 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
337 wake_up_process(watchdog_task);
344 * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
345 * and hung_task_check_interval_secs
347 static const unsigned long hung_task_timeout_max = (LONG_MAX / HZ);
348 static const struct ctl_table hung_task_sysctls[] = {
351 .procname = "hung_task_all_cpu_backtrace",
352 .data = &sysctl_hung_task_all_cpu_backtrace,
353 .maxlen = sizeof(int),
355 .proc_handler = proc_dointvec_minmax,
356 .extra1 = SYSCTL_ZERO,
357 .extra2 = SYSCTL_ONE,
359 #endif /* CONFIG_SMP */
361 .procname = "hung_task_panic",
362 .data = &sysctl_hung_task_panic,
363 .maxlen = sizeof(int),
365 .proc_handler = proc_dointvec_minmax,
366 .extra1 = SYSCTL_ZERO,
367 .extra2 = SYSCTL_ONE,
370 .procname = "hung_task_check_count",
371 .data = &sysctl_hung_task_check_count,
372 .maxlen = sizeof(int),
374 .proc_handler = proc_dointvec_minmax,
375 .extra1 = SYSCTL_ZERO,
378 .procname = "hung_task_timeout_secs",
379 .data = &sysctl_hung_task_timeout_secs,
380 .maxlen = sizeof(unsigned long),
382 .proc_handler = proc_dohung_task_timeout_secs,
383 .extra2 = (void *)&hung_task_timeout_max,
386 .procname = "hung_task_check_interval_secs",
387 .data = &sysctl_hung_task_check_interval_secs,
388 .maxlen = sizeof(unsigned long),
390 .proc_handler = proc_dohung_task_timeout_secs,
391 .extra2 = (void *)&hung_task_timeout_max,
394 .procname = "hung_task_warnings",
395 .data = &sysctl_hung_task_warnings,
396 .maxlen = sizeof(int),
398 .proc_handler = proc_dointvec_minmax,
399 .extra1 = SYSCTL_NEG_ONE,
402 .procname = "hung_task_detect_count",
403 .data = &sysctl_hung_task_detect_count,
404 .maxlen = sizeof(unsigned long),
406 .proc_handler = proc_doulongvec_minmax,
410 static void __init hung_task_sysctl_init(void)
412 register_sysctl_init("kernel", hung_task_sysctls);
415 #define hung_task_sysctl_init() do { } while (0)
416 #endif /* CONFIG_SYSCTL */
419 static atomic_t reset_hung_task = ATOMIC_INIT(0);
421 void reset_hung_task_detector(void)
423 atomic_set(&reset_hung_task, 1);
425 EXPORT_SYMBOL_GPL(reset_hung_task_detector);
427 static bool hung_detector_suspended;
429 static int hungtask_pm_notify(struct notifier_block *self,
430 unsigned long action, void *hcpu)
433 case PM_SUSPEND_PREPARE:
434 case PM_HIBERNATION_PREPARE:
435 case PM_RESTORE_PREPARE:
436 hung_detector_suspended = true;
438 case PM_POST_SUSPEND:
439 case PM_POST_HIBERNATION:
440 case PM_POST_RESTORE:
441 hung_detector_suspended = false;
450 * kthread which checks for tasks stuck in D state
452 static int watchdog(void *dummy)
454 unsigned long hung_last_checked = jiffies;
456 set_user_nice(current, 0);
459 unsigned long timeout = sysctl_hung_task_timeout_secs;
460 unsigned long interval = sysctl_hung_task_check_interval_secs;
465 interval = min_t(unsigned long, interval, timeout);
466 t = hung_timeout_jiffies(hung_last_checked, interval);
468 if (!atomic_xchg(&reset_hung_task, 0) &&
469 !hung_detector_suspended)
470 check_hung_uninterruptible_tasks(timeout);
471 hung_last_checked = jiffies;
474 schedule_timeout_interruptible(t);
480 static int __init hung_task_init(void)
482 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
484 /* Disable hung task detector on suspend */
485 pm_notifier(hungtask_pm_notify, 0);
487 watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
488 hung_task_sysctl_init();
492 subsys_initcall(hung_task_init);