hung_task.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_BATCHING 1024
  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_warnings && !sysctl_hung_task_panic)
  95. return;
  96. /*
  97. * Ok, the task did not get scheduled for more than 2 minutes,
  98. * complain:
  99. */
  100. if (sysctl_hung_task_warnings) {
  101. if (sysctl_hung_task_warnings > 0)
  102. sysctl_hung_task_warnings--;
  103. pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
  104. t->comm, t->pid, timeout);
  105. pr_err(" %s %s %.*s\n",
  106. print_tainted(), init_utsname()->release,
  107. (int)strcspn(init_utsname()->version, " "),
  108. init_utsname()->version);
  109. pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
  110. " disables this message.\n");
  111. sched_show_task(t);
  112. hung_task_show_lock = true;
  113. }
  114. touch_nmi_watchdog();
  115. if (sysctl_hung_task_panic) {
  116. hung_task_show_lock = true;
  117. hung_task_call_panic = true;
  118. }
  119. }
  120. /*
  121. * To avoid extending the RCU grace period for an unbounded amount of time,
  122. * periodically exit the critical section and enter a new one.
  123. *
  124. * For preemptible RCU it is sufficient to call rcu_read_unlock in order
  125. * to exit the grace period. For classic RCU, a reschedule is required.
  126. */
  127. static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
  128. {
  129. bool can_cont;
  130. get_task_struct(g);
  131. get_task_struct(t);
  132. rcu_read_unlock();
  133. cond_resched();
  134. rcu_read_lock();
  135. can_cont = pid_alive(g) && pid_alive(t);
  136. put_task_struct(t);
  137. put_task_struct(g);
  138. return can_cont;
  139. }
  140. /*
  141. * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
  142. * a really long time (120 seconds). If that happens, print out
  143. * a warning.
  144. */
  145. static void check_hung_uninterruptible_tasks(unsigned long timeout)
  146. {
  147. int max_count = sysctl_hung_task_check_count;
  148. int batch_count = HUNG_TASK_BATCHING;
  149. struct task_struct *g, *t;
  150. /*
  151. * If the system crashed already then all bets are off,
  152. * do not report extra hung tasks:
  153. */
  154. if (test_taint(TAINT_DIE) || did_panic)
  155. return;
  156. hung_task_show_lock = false;
  157. rcu_read_lock();
  158. for_each_process_thread(g, t) {
  159. if (!max_count--)
  160. goto unlock;
  161. if (!--batch_count) {
  162. batch_count = HUNG_TASK_BATCHING;
  163. if (!rcu_lock_break(g, t))
  164. goto unlock;
  165. }
  166. /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
  167. if (t->state == TASK_UNINTERRUPTIBLE)
  168. check_hung_task(t, timeout);
  169. }
  170. unlock:
  171. rcu_read_unlock();
  172. if (hung_task_show_lock)
  173. debug_show_all_locks();
  174. if (hung_task_call_panic) {
  175. trigger_all_cpu_backtrace();
  176. panic("hung_task: blocked tasks");
  177. }
  178. }
  179. static long hung_timeout_jiffies(unsigned long last_checked,
  180. unsigned long timeout)
  181. {
  182. /* timeout of 0 will disable the watchdog */
  183. return timeout ? last_checked - jiffies + timeout * HZ :
  184. MAX_SCHEDULE_TIMEOUT;
  185. }
  186. /*
  187. * Process updating of timeout sysctl
  188. */
  189. int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
  190. void __user *buffer,
  191. size_t *lenp, loff_t *ppos)
  192. {
  193. int ret;
  194. ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  195. if (ret || !write)
  196. goto out;
  197. wake_up_process(watchdog_task);
  198. out:
  199. return ret;
  200. }
  201. static atomic_t reset_hung_task = ATOMIC_INIT(0);
  202. void reset_hung_task_detector(void)
  203. {
  204. atomic_set(&reset_hung_task, 1);
  205. }
  206. EXPORT_SYMBOL_GPL(reset_hung_task_detector);
  207. static bool hung_detector_suspended;
  208. static int hungtask_pm_notify(struct notifier_block *self,
  209. unsigned long action, void *hcpu)
  210. {
  211. switch (action) {
  212. case PM_SUSPEND_PREPARE:
  213. case PM_HIBERNATION_PREPARE:
  214. case PM_RESTORE_PREPARE:
  215. hung_detector_suspended = true;
  216. break;
  217. case PM_POST_SUSPEND:
  218. case PM_POST_HIBERNATION:
  219. case PM_POST_RESTORE:
  220. hung_detector_suspended = false;
  221. break;
  222. default:
  223. break;
  224. }
  225. return NOTIFY_OK;
  226. }
  227. /*
  228. * kthread which checks for tasks stuck in D state
  229. */
  230. static int watchdog(void *dummy)
  231. {
  232. unsigned long hung_last_checked = jiffies;
  233. set_user_nice(current, 0);
  234. for ( ; ; ) {
  235. unsigned long timeout = sysctl_hung_task_timeout_secs;
  236. unsigned long interval = sysctl_hung_task_check_interval_secs;
  237. long t;
  238. if (interval == 0)
  239. interval = timeout;
  240. interval = min_t(unsigned long, interval, timeout);
  241. t = hung_timeout_jiffies(hung_last_checked, interval);
  242. if (t <= 0) {
  243. if (!atomic_xchg(&reset_hung_task, 0) &&
  244. !hung_detector_suspended)
  245. check_hung_uninterruptible_tasks(timeout);
  246. hung_last_checked = jiffies;
  247. continue;
  248. }
  249. schedule_timeout_interruptible(t);
  250. }
  251. return 0;
  252. }
  253. static int __init hung_task_init(void)
  254. {
  255. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  256. /* Disable hung task detector on suspend */
  257. pm_notifier(hungtask_pm_notify, 0);
  258. watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
  259. return 0;
  260. }
  261. subsys_initcall(hung_task_init);