hung_task.c 6.2 KB

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