hung_task.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/panic_notifier.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/suspend.h>
  20. #include <linux/utsname.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/sched/debug.h>
  23. #include <linux/sched/sysctl.h>
  24. #include <trace/events/sched.h>
  25. #undef CREATE_TRACE_POINTS
  26. #include <trace/hooks/hung_task.h>
  27. /*
  28. * The number of tasks checked:
  29. */
  30. int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
  31. /*
  32. * Limit number of tasks checked in a batch.
  33. *
  34. * This value controls the preemptibility of khungtaskd since preemption
  35. * is disabled during the critical section. It also controls the size of
  36. * the RCU grace period. So it needs to be upper-bound.
  37. */
  38. #define HUNG_TASK_LOCK_BREAK (HZ / 10)
  39. /*
  40. * Zero means infinite timeout - no checking done:
  41. */
  42. unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
  43. /*
  44. * Zero (default value) means use sysctl_hung_task_timeout_secs:
  45. */
  46. unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
  47. int __read_mostly sysctl_hung_task_warnings = 10;
  48. static int __read_mostly did_panic;
  49. static bool hung_task_show_lock;
  50. static bool hung_task_call_panic;
  51. static bool hung_task_show_all_bt;
  52. static struct task_struct *watchdog_task;
  53. #ifdef CONFIG_SMP
  54. /*
  55. * Should we dump all CPUs backtraces in a hung task event?
  56. * Defaults to 0, can be changed via sysctl.
  57. */
  58. static unsigned int __read_mostly sysctl_hung_task_all_cpu_backtrace;
  59. #else
  60. #define sysctl_hung_task_all_cpu_backtrace 0
  61. #endif /* CONFIG_SMP */
  62. /*
  63. * Should we panic (and reboot, if panic_timeout= is set) when a
  64. * hung task is detected:
  65. */
  66. unsigned int __read_mostly sysctl_hung_task_panic =
  67. IS_ENABLED(CONFIG_BOOTPARAM_HUNG_TASK_PANIC);
  68. static int
  69. hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
  70. {
  71. did_panic = 1;
  72. return NOTIFY_DONE;
  73. }
  74. static struct notifier_block panic_block = {
  75. .notifier_call = hung_task_panic,
  76. };
  77. static void check_hung_task(struct task_struct *t, unsigned long timeout)
  78. {
  79. unsigned long switch_count = t->nvcsw + t->nivcsw;
  80. /*
  81. * Ensure the task is not frozen.
  82. * Also, skip vfork and any other user process that freezer should skip.
  83. */
  84. if (unlikely(READ_ONCE(t->__state) & TASK_FROZEN))
  85. return;
  86. /*
  87. * When a freshly created task is scheduled once, changes its state to
  88. * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
  89. * musn't be checked.
  90. */
  91. if (unlikely(!switch_count))
  92. return;
  93. if (switch_count != t->last_switch_count) {
  94. t->last_switch_count = switch_count;
  95. t->last_switch_time = jiffies;
  96. return;
  97. }
  98. if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
  99. return;
  100. trace_sched_process_hang(t);
  101. if (sysctl_hung_task_panic) {
  102. console_verbose();
  103. hung_task_show_lock = true;
  104. hung_task_call_panic = true;
  105. }
  106. /*
  107. * Ok, the task did not get scheduled for more than 2 minutes,
  108. * complain:
  109. */
  110. if (sysctl_hung_task_warnings) {
  111. if (sysctl_hung_task_warnings > 0)
  112. sysctl_hung_task_warnings--;
  113. pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
  114. t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
  115. pr_err(" %s %s %.*s\n",
  116. print_tainted(), init_utsname()->release,
  117. (int)strcspn(init_utsname()->version, " "),
  118. init_utsname()->version);
  119. pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
  120. " disables this message.\n");
  121. sched_show_task(t);
  122. hung_task_show_lock = true;
  123. if (sysctl_hung_task_all_cpu_backtrace)
  124. hung_task_show_all_bt = true;
  125. }
  126. touch_nmi_watchdog();
  127. }
  128. /*
  129. * To avoid extending the RCU grace period for an unbounded amount of time,
  130. * periodically exit the critical section and enter a new one.
  131. *
  132. * For preemptible RCU it is sufficient to call rcu_read_unlock in order
  133. * to exit the grace period. For classic RCU, a reschedule is required.
  134. */
  135. static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
  136. {
  137. bool can_cont;
  138. get_task_struct(g);
  139. get_task_struct(t);
  140. rcu_read_unlock();
  141. cond_resched();
  142. rcu_read_lock();
  143. can_cont = pid_alive(g) && pid_alive(t);
  144. put_task_struct(t);
  145. put_task_struct(g);
  146. return can_cont;
  147. }
  148. /*
  149. * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
  150. * a really long time (120 seconds). If that happens, print out
  151. * a warning.
  152. */
  153. static void check_hung_uninterruptible_tasks(unsigned long timeout)
  154. {
  155. int max_count = sysctl_hung_task_check_count;
  156. unsigned long last_break = jiffies;
  157. struct task_struct *g, *t;
  158. bool need_check = true;
  159. /*
  160. * If the system crashed already then all bets are off,
  161. * do not report extra hung tasks:
  162. */
  163. if (test_taint(TAINT_DIE) || did_panic)
  164. return;
  165. hung_task_show_lock = false;
  166. rcu_read_lock();
  167. for_each_process_thread(g, t) {
  168. unsigned int state;
  169. if (!max_count--)
  170. goto unlock;
  171. if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
  172. if (!rcu_lock_break(g, t))
  173. goto unlock;
  174. last_break = jiffies;
  175. }
  176. /*
  177. * skip the TASK_KILLABLE tasks -- these can be killed
  178. * skip the TASK_IDLE tasks -- those are genuinely idle
  179. */
  180. trace_android_vh_check_uninterrupt_tasks(t, timeout, &need_check);
  181. if (need_check) {
  182. state = READ_ONCE(t->__state);
  183. if ((state & TASK_UNINTERRUPTIBLE) &&
  184. !(state & TASK_WAKEKILL) &&
  185. !(state & TASK_NOLOAD))
  186. check_hung_task(t, timeout);
  187. }
  188. }
  189. trace_android_vh_check_uninterrupt_tasks_done(NULL);
  190. unlock:
  191. rcu_read_unlock();
  192. if (hung_task_show_lock)
  193. debug_show_all_locks();
  194. if (hung_task_show_all_bt) {
  195. hung_task_show_all_bt = false;
  196. trigger_all_cpu_backtrace();
  197. }
  198. if (hung_task_call_panic)
  199. panic("hung_task: blocked tasks");
  200. }
  201. static long hung_timeout_jiffies(unsigned long last_checked,
  202. unsigned long timeout)
  203. {
  204. /* timeout of 0 will disable the watchdog */
  205. return timeout ? last_checked - jiffies + timeout * HZ :
  206. MAX_SCHEDULE_TIMEOUT;
  207. }
  208. #ifdef CONFIG_SYSCTL
  209. /*
  210. * Process updating of timeout sysctl
  211. */
  212. static int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
  213. void *buffer,
  214. size_t *lenp, loff_t *ppos)
  215. {
  216. int ret;
  217. ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  218. if (ret || !write)
  219. goto out;
  220. wake_up_process(watchdog_task);
  221. out:
  222. return ret;
  223. }
  224. /*
  225. * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
  226. * and hung_task_check_interval_secs
  227. */
  228. static const unsigned long hung_task_timeout_max = (LONG_MAX / HZ);
  229. static struct ctl_table hung_task_sysctls[] = {
  230. #ifdef CONFIG_SMP
  231. {
  232. .procname = "hung_task_all_cpu_backtrace",
  233. .data = &sysctl_hung_task_all_cpu_backtrace,
  234. .maxlen = sizeof(int),
  235. .mode = 0644,
  236. .proc_handler = proc_dointvec_minmax,
  237. .extra1 = SYSCTL_ZERO,
  238. .extra2 = SYSCTL_ONE,
  239. },
  240. #endif /* CONFIG_SMP */
  241. {
  242. .procname = "hung_task_panic",
  243. .data = &sysctl_hung_task_panic,
  244. .maxlen = sizeof(int),
  245. .mode = 0644,
  246. .proc_handler = proc_dointvec_minmax,
  247. .extra1 = SYSCTL_ZERO,
  248. .extra2 = SYSCTL_ONE,
  249. },
  250. {
  251. .procname = "hung_task_check_count",
  252. .data = &sysctl_hung_task_check_count,
  253. .maxlen = sizeof(int),
  254. .mode = 0644,
  255. .proc_handler = proc_dointvec_minmax,
  256. .extra1 = SYSCTL_ZERO,
  257. },
  258. {
  259. .procname = "hung_task_timeout_secs",
  260. .data = &sysctl_hung_task_timeout_secs,
  261. .maxlen = sizeof(unsigned long),
  262. .mode = 0644,
  263. .proc_handler = proc_dohung_task_timeout_secs,
  264. .extra2 = (void *)&hung_task_timeout_max,
  265. },
  266. {
  267. .procname = "hung_task_check_interval_secs",
  268. .data = &sysctl_hung_task_check_interval_secs,
  269. .maxlen = sizeof(unsigned long),
  270. .mode = 0644,
  271. .proc_handler = proc_dohung_task_timeout_secs,
  272. .extra2 = (void *)&hung_task_timeout_max,
  273. },
  274. {
  275. .procname = "hung_task_warnings",
  276. .data = &sysctl_hung_task_warnings,
  277. .maxlen = sizeof(int),
  278. .mode = 0644,
  279. .proc_handler = proc_dointvec_minmax,
  280. .extra1 = SYSCTL_NEG_ONE,
  281. },
  282. {}
  283. };
  284. static void __init hung_task_sysctl_init(void)
  285. {
  286. register_sysctl_init("kernel", hung_task_sysctls);
  287. }
  288. #else
  289. #define hung_task_sysctl_init() do { } while (0)
  290. #endif /* CONFIG_SYSCTL */
  291. static atomic_t reset_hung_task = ATOMIC_INIT(0);
  292. void reset_hung_task_detector(void)
  293. {
  294. atomic_set(&reset_hung_task, 1);
  295. }
  296. EXPORT_SYMBOL_GPL(reset_hung_task_detector);
  297. static bool hung_detector_suspended;
  298. static int hungtask_pm_notify(struct notifier_block *self,
  299. unsigned long action, void *hcpu)
  300. {
  301. switch (action) {
  302. case PM_SUSPEND_PREPARE:
  303. case PM_HIBERNATION_PREPARE:
  304. case PM_RESTORE_PREPARE:
  305. hung_detector_suspended = true;
  306. break;
  307. case PM_POST_SUSPEND:
  308. case PM_POST_HIBERNATION:
  309. case PM_POST_RESTORE:
  310. hung_detector_suspended = false;
  311. break;
  312. default:
  313. break;
  314. }
  315. return NOTIFY_OK;
  316. }
  317. /*
  318. * kthread which checks for tasks stuck in D state
  319. */
  320. static int watchdog(void *dummy)
  321. {
  322. unsigned long hung_last_checked = jiffies;
  323. set_user_nice(current, 0);
  324. for ( ; ; ) {
  325. unsigned long timeout = sysctl_hung_task_timeout_secs;
  326. unsigned long interval = sysctl_hung_task_check_interval_secs;
  327. long t;
  328. if (interval == 0)
  329. interval = timeout;
  330. interval = min_t(unsigned long, interval, timeout);
  331. t = hung_timeout_jiffies(hung_last_checked, interval);
  332. if (t <= 0) {
  333. if (!atomic_xchg(&reset_hung_task, 0) &&
  334. !hung_detector_suspended)
  335. check_hung_uninterruptible_tasks(timeout);
  336. hung_last_checked = jiffies;
  337. continue;
  338. }
  339. schedule_timeout_interruptible(t);
  340. }
  341. return 0;
  342. }
  343. static int __init hung_task_init(void)
  344. {
  345. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  346. /* Disable hung task detector on suspend */
  347. pm_notifier(hungtask_pm_notify, 0);
  348. watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
  349. hung_task_sysctl_init();
  350. return 0;
  351. }
  352. subsys_initcall(hung_task_init);