hung_task.c 5.7 KB

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