hung_task.c 6.5 KB

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