hung_task.c 7.2 KB

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