hung_task.c 7.2 KB

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