hung_task.c 7.7 KB

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