hung_task.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Detect Hung Task
  3. *
  4. * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
  5. *
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/cpu.h>
  9. #include <linux/nmi.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/freezer.h>
  13. #include <linux/kthread.h>
  14. #include <linux/lockdep.h>
  15. #include <linux/export.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/suspend.h>
  18. #include <linux/utsname.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/sched/debug.h>
  21. #include <trace/events/sched.h>
  22. /*
  23. * The number of tasks checked:
  24. */
  25. int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
  26. /*
  27. * Limit number of tasks checked in a batch.
  28. *
  29. * This value controls the preemptibility of khungtaskd since preemption
  30. * is disabled during the critical section. It also controls the size of
  31. * the RCU grace period. So it needs to be upper-bound.
  32. */
  33. #define HUNG_TASK_LOCK_BREAK (HZ / 10)
  34. /*
  35. * Zero means infinite timeout - no checking done:
  36. */
  37. unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
  38. int __read_mostly sysctl_hung_task_warnings = 10;
  39. static int __read_mostly did_panic;
  40. static bool hung_task_show_lock;
  41. static bool hung_task_call_panic;
  42. static struct task_struct *watchdog_task;
  43. /*
  44. * Should we panic (and reboot, if panic_timeout= is set) when a
  45. * hung task is detected:
  46. */
  47. unsigned int __read_mostly sysctl_hung_task_panic =
  48. CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
  49. static int __init hung_task_panic_setup(char *str)
  50. {
  51. int rc = kstrtouint(str, 0, &sysctl_hung_task_panic);
  52. if (rc)
  53. return rc;
  54. return 1;
  55. }
  56. __setup("hung_task_panic=", hung_task_panic_setup);
  57. static int
  58. hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
  59. {
  60. did_panic = 1;
  61. return NOTIFY_DONE;
  62. }
  63. static struct notifier_block panic_block = {
  64. .notifier_call = hung_task_panic,
  65. };
  66. static void check_hung_task(struct task_struct *t, unsigned long timeout)
  67. {
  68. unsigned long switch_count = t->nvcsw + t->nivcsw;
  69. /*
  70. * Ensure the task is not frozen.
  71. * Also, skip vfork and any other user process that freezer should skip.
  72. */
  73. if (unlikely(t->flags & (PF_FROZEN | PF_FREEZER_SKIP)))
  74. return;
  75. /*
  76. * When a freshly created task is scheduled once, changes its state to
  77. * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
  78. * musn't be checked.
  79. */
  80. if (unlikely(!switch_count))
  81. return;
  82. if (switch_count != t->last_switch_count) {
  83. t->last_switch_count = switch_count;
  84. return;
  85. }
  86. trace_sched_process_hang(t);
  87. if (sysctl_hung_task_panic) {
  88. console_verbose();
  89. hung_task_show_lock = true;
  90. hung_task_call_panic = true;
  91. }
  92. /*
  93. * Ok, the task did not get scheduled for more than 2 minutes,
  94. * complain:
  95. */
  96. if (sysctl_hung_task_warnings) {
  97. if (sysctl_hung_task_warnings > 0)
  98. sysctl_hung_task_warnings--;
  99. pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
  100. t->comm, t->pid, timeout);
  101. pr_err(" %s %s %.*s\n",
  102. print_tainted(), init_utsname()->release,
  103. (int)strcspn(init_utsname()->version, " "),
  104. init_utsname()->version);
  105. pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
  106. " disables this message.\n");
  107. sched_show_task(t);
  108. hung_task_show_lock = true;
  109. }
  110. touch_nmi_watchdog();
  111. }
  112. /*
  113. * To avoid extending the RCU grace period for an unbounded amount of time,
  114. * periodically exit the critical section and enter a new one.
  115. *
  116. * For preemptible RCU it is sufficient to call rcu_read_unlock in order
  117. * to exit the grace period. For classic RCU, a reschedule is required.
  118. */
  119. static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
  120. {
  121. bool can_cont;
  122. get_task_struct(g);
  123. get_task_struct(t);
  124. rcu_read_unlock();
  125. cond_resched();
  126. rcu_read_lock();
  127. can_cont = pid_alive(g) && pid_alive(t);
  128. put_task_struct(t);
  129. put_task_struct(g);
  130. return can_cont;
  131. }
  132. /*
  133. * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
  134. * a really long time (120 seconds). If that happens, print out
  135. * a warning.
  136. */
  137. static void check_hung_uninterruptible_tasks(unsigned long timeout)
  138. {
  139. int max_count = sysctl_hung_task_check_count;
  140. unsigned long last_break = jiffies;
  141. struct task_struct *g, *t;
  142. /*
  143. * If the system crashed already then all bets are off,
  144. * do not report extra hung tasks:
  145. */
  146. if (test_taint(TAINT_DIE) || did_panic)
  147. return;
  148. hung_task_show_lock = false;
  149. rcu_read_lock();
  150. for_each_process_thread(g, t) {
  151. if (!max_count--)
  152. goto unlock;
  153. if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
  154. if (!rcu_lock_break(g, t))
  155. goto unlock;
  156. last_break = jiffies;
  157. }
  158. /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
  159. if (t->state == TASK_UNINTERRUPTIBLE)
  160. check_hung_task(t, timeout);
  161. }
  162. unlock:
  163. rcu_read_unlock();
  164. if (hung_task_show_lock)
  165. debug_show_all_locks();
  166. if (hung_task_call_panic) {
  167. trigger_all_cpu_backtrace();
  168. panic("hung_task: blocked tasks");
  169. }
  170. }
  171. static long hung_timeout_jiffies(unsigned long last_checked,
  172. unsigned long timeout)
  173. {
  174. /* timeout of 0 will disable the watchdog */
  175. return timeout ? last_checked - jiffies + timeout * HZ :
  176. MAX_SCHEDULE_TIMEOUT;
  177. }
  178. /*
  179. * Process updating of timeout sysctl
  180. */
  181. int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
  182. void __user *buffer,
  183. size_t *lenp, loff_t *ppos)
  184. {
  185. int ret;
  186. ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  187. if (ret || !write)
  188. goto out;
  189. wake_up_process(watchdog_task);
  190. out:
  191. return ret;
  192. }
  193. static atomic_t reset_hung_task = ATOMIC_INIT(0);
  194. void reset_hung_task_detector(void)
  195. {
  196. atomic_set(&reset_hung_task, 1);
  197. }
  198. EXPORT_SYMBOL_GPL(reset_hung_task_detector);
  199. static bool hung_detector_suspended;
  200. static int hungtask_pm_notify(struct notifier_block *self,
  201. unsigned long action, void *hcpu)
  202. {
  203. switch (action) {
  204. case PM_SUSPEND_PREPARE:
  205. case PM_HIBERNATION_PREPARE:
  206. case PM_RESTORE_PREPARE:
  207. hung_detector_suspended = true;
  208. break;
  209. case PM_POST_SUSPEND:
  210. case PM_POST_HIBERNATION:
  211. case PM_POST_RESTORE:
  212. hung_detector_suspended = false;
  213. break;
  214. default:
  215. break;
  216. }
  217. return NOTIFY_OK;
  218. }
  219. /*
  220. * kthread which checks for tasks stuck in D state
  221. */
  222. static int watchdog(void *dummy)
  223. {
  224. unsigned long hung_last_checked = jiffies;
  225. set_user_nice(current, 0);
  226. for ( ; ; ) {
  227. unsigned long timeout = sysctl_hung_task_timeout_secs;
  228. long t = hung_timeout_jiffies(hung_last_checked, timeout);
  229. if (t <= 0) {
  230. if (!atomic_xchg(&reset_hung_task, 0) &&
  231. !hung_detector_suspended)
  232. check_hung_uninterruptible_tasks(timeout);
  233. hung_last_checked = jiffies;
  234. continue;
  235. }
  236. schedule_timeout_interruptible(t);
  237. }
  238. return 0;
  239. }
  240. static int __init hung_task_init(void)
  241. {
  242. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  243. /* Disable hung task detector on suspend */
  244. pm_notifier(hungtask_pm_notify, 0);
  245. watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
  246. return 0;
  247. }
  248. subsys_initcall(hung_task_init);