hung_task.c 4.9 KB

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