softlockup.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Detect Soft Lockups
  3. *
  4. * started by Ingo Molnar, Copyright (C) 2005, 2006 Red Hat, Inc.
  5. *
  6. * this code detects soft lockups: incidents in where on a CPU
  7. * the kernel does not reschedule for 10 seconds or more.
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/cpu.h>
  11. #include <linux/nmi.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/freezer.h>
  15. #include <linux/kthread.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/notifier.h>
  18. #include <linux/module.h>
  19. #include <asm/irq_regs.h>
  20. static DEFINE_SPINLOCK(print_lock);
  21. static DEFINE_PER_CPU(unsigned long, touch_timestamp);
  22. static DEFINE_PER_CPU(unsigned long, print_timestamp);
  23. static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
  24. static int __read_mostly did_panic;
  25. int __read_mostly softlockup_thresh = 60;
  26. /*
  27. * Should we panic (and reboot, if panic_timeout= is set) when a
  28. * soft-lockup occurs:
  29. */
  30. unsigned int __read_mostly softlockup_panic =
  31. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  32. static int __init softlockup_panic_setup(char *str)
  33. {
  34. softlockup_panic = simple_strtoul(str, NULL, 0);
  35. return 1;
  36. }
  37. __setup("softlockup_panic=", softlockup_panic_setup);
  38. static int
  39. softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
  40. {
  41. did_panic = 1;
  42. return NOTIFY_DONE;
  43. }
  44. static struct notifier_block panic_block = {
  45. .notifier_call = softlock_panic,
  46. };
  47. /*
  48. * Returns seconds, approximately. We don't need nanosecond
  49. * resolution, and we don't need to waste time with a big divide when
  50. * 2^30ns == 1.074s.
  51. */
  52. static unsigned long get_timestamp(int this_cpu)
  53. {
  54. return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
  55. }
  56. static void __touch_softlockup_watchdog(void)
  57. {
  58. int this_cpu = raw_smp_processor_id();
  59. __raw_get_cpu_var(touch_timestamp) = get_timestamp(this_cpu);
  60. }
  61. void touch_softlockup_watchdog(void)
  62. {
  63. __raw_get_cpu_var(touch_timestamp) = 0;
  64. }
  65. EXPORT_SYMBOL(touch_softlockup_watchdog);
  66. void touch_all_softlockup_watchdogs(void)
  67. {
  68. int cpu;
  69. /* Cause each CPU to re-update its timestamp rather than complain */
  70. for_each_online_cpu(cpu)
  71. per_cpu(touch_timestamp, cpu) = 0;
  72. }
  73. EXPORT_SYMBOL(touch_all_softlockup_watchdogs);
  74. /*
  75. * This callback runs from the timer interrupt, and checks
  76. * whether the watchdog thread has hung or not:
  77. */
  78. void softlockup_tick(void)
  79. {
  80. int this_cpu = smp_processor_id();
  81. unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
  82. unsigned long print_timestamp;
  83. struct pt_regs *regs = get_irq_regs();
  84. unsigned long now;
  85. /* Is detection switched off? */
  86. if (!per_cpu(watchdog_task, this_cpu) || softlockup_thresh <= 0) {
  87. /* Be sure we don't false trigger if switched back on */
  88. if (touch_timestamp)
  89. per_cpu(touch_timestamp, this_cpu) = 0;
  90. return;
  91. }
  92. if (touch_timestamp == 0) {
  93. __touch_softlockup_watchdog();
  94. return;
  95. }
  96. print_timestamp = per_cpu(print_timestamp, this_cpu);
  97. /* report at most once a second */
  98. if (print_timestamp == touch_timestamp || did_panic)
  99. return;
  100. /* do not print during early bootup: */
  101. if (unlikely(system_state != SYSTEM_RUNNING)) {
  102. __touch_softlockup_watchdog();
  103. return;
  104. }
  105. now = get_timestamp(this_cpu);
  106. /*
  107. * Wake up the high-prio watchdog task twice per
  108. * threshold timespan.
  109. */
  110. if (now > touch_timestamp + softlockup_thresh/2)
  111. wake_up_process(per_cpu(watchdog_task, this_cpu));
  112. /* Warn about unreasonable delays: */
  113. if (now <= (touch_timestamp + softlockup_thresh))
  114. return;
  115. per_cpu(print_timestamp, this_cpu) = touch_timestamp;
  116. spin_lock(&print_lock);
  117. printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d]\n",
  118. this_cpu, now - touch_timestamp,
  119. current->comm, task_pid_nr(current));
  120. print_modules();
  121. print_irqtrace_events(current);
  122. if (regs)
  123. show_regs(regs);
  124. else
  125. dump_stack();
  126. spin_unlock(&print_lock);
  127. if (softlockup_panic)
  128. panic("softlockup: hung tasks");
  129. }
  130. /*
  131. * Have a reasonable limit on the number of tasks checked:
  132. */
  133. unsigned long __read_mostly sysctl_hung_task_check_count = 1024;
  134. /*
  135. * Zero means infinite timeout - no checking done:
  136. */
  137. unsigned long __read_mostly sysctl_hung_task_timeout_secs = 120;
  138. unsigned long __read_mostly sysctl_hung_task_warnings = 10;
  139. /*
  140. * Only do the hung-tasks check on one CPU:
  141. */
  142. static int check_cpu __read_mostly = -1;
  143. static void check_hung_task(struct task_struct *t, unsigned long now)
  144. {
  145. unsigned long switch_count = t->nvcsw + t->nivcsw;
  146. if (t->flags & PF_FROZEN)
  147. return;
  148. if (switch_count != t->last_switch_count || !t->last_switch_timestamp) {
  149. t->last_switch_count = switch_count;
  150. t->last_switch_timestamp = now;
  151. return;
  152. }
  153. if ((long)(now - t->last_switch_timestamp) <
  154. sysctl_hung_task_timeout_secs)
  155. return;
  156. if (sysctl_hung_task_warnings < 0)
  157. return;
  158. sysctl_hung_task_warnings--;
  159. /*
  160. * Ok, the task did not get scheduled for more than 2 minutes,
  161. * complain:
  162. */
  163. printk(KERN_ERR "INFO: task %s:%d blocked for more than "
  164. "%ld seconds.\n", t->comm, t->pid,
  165. sysctl_hung_task_timeout_secs);
  166. printk(KERN_ERR "\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
  167. " disables this message.\n");
  168. sched_show_task(t);
  169. __debug_show_held_locks(t);
  170. t->last_switch_timestamp = now;
  171. touch_nmi_watchdog();
  172. if (softlockup_panic)
  173. panic("softlockup: blocked tasks");
  174. }
  175. /*
  176. * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
  177. * a really long time (120 seconds). If that happens, print out
  178. * a warning.
  179. */
  180. static void check_hung_uninterruptible_tasks(int this_cpu)
  181. {
  182. int max_count = sysctl_hung_task_check_count;
  183. unsigned long now = get_timestamp(this_cpu);
  184. struct task_struct *g, *t;
  185. /*
  186. * If the system crashed already then all bets are off,
  187. * do not report extra hung tasks:
  188. */
  189. if ((tainted & TAINT_DIE) || did_panic)
  190. return;
  191. read_lock(&tasklist_lock);
  192. do_each_thread(g, t) {
  193. if (!--max_count)
  194. goto unlock;
  195. /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
  196. if (t->state == TASK_UNINTERRUPTIBLE)
  197. check_hung_task(t, now);
  198. } while_each_thread(g, t);
  199. unlock:
  200. read_unlock(&tasklist_lock);
  201. }
  202. /*
  203. * The watchdog thread - runs every second and touches the timestamp.
  204. */
  205. static int watchdog(void *__bind_cpu)
  206. {
  207. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  208. int this_cpu = (long)__bind_cpu;
  209. sched_setscheduler(current, SCHED_FIFO, &param);
  210. /* initialize timestamp */
  211. __touch_softlockup_watchdog();
  212. set_current_state(TASK_INTERRUPTIBLE);
  213. /*
  214. * Run briefly once per second to reset the softlockup timestamp.
  215. * If this gets delayed for more than 60 seconds then the
  216. * debug-printout triggers in softlockup_tick().
  217. */
  218. while (!kthread_should_stop()) {
  219. __touch_softlockup_watchdog();
  220. schedule();
  221. if (kthread_should_stop())
  222. break;
  223. if (this_cpu == check_cpu) {
  224. if (sysctl_hung_task_timeout_secs)
  225. check_hung_uninterruptible_tasks(this_cpu);
  226. }
  227. set_current_state(TASK_INTERRUPTIBLE);
  228. }
  229. __set_current_state(TASK_RUNNING);
  230. return 0;
  231. }
  232. /*
  233. * Create/destroy watchdog threads as CPUs come and go:
  234. */
  235. static int __cpuinit
  236. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  237. {
  238. int hotcpu = (unsigned long)hcpu;
  239. struct task_struct *p;
  240. switch (action) {
  241. case CPU_UP_PREPARE:
  242. case CPU_UP_PREPARE_FROZEN:
  243. BUG_ON(per_cpu(watchdog_task, hotcpu));
  244. p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
  245. if (IS_ERR(p)) {
  246. printk(KERN_ERR "watchdog for %i failed\n", hotcpu);
  247. return NOTIFY_BAD;
  248. }
  249. per_cpu(touch_timestamp, hotcpu) = 0;
  250. per_cpu(watchdog_task, hotcpu) = p;
  251. kthread_bind(p, hotcpu);
  252. break;
  253. case CPU_ONLINE:
  254. case CPU_ONLINE_FROZEN:
  255. check_cpu = any_online_cpu(cpu_online_map);
  256. wake_up_process(per_cpu(watchdog_task, hotcpu));
  257. break;
  258. #ifdef CONFIG_HOTPLUG_CPU
  259. case CPU_DOWN_PREPARE:
  260. case CPU_DOWN_PREPARE_FROZEN:
  261. if (hotcpu == check_cpu) {
  262. cpumask_t temp_cpu_online_map = cpu_online_map;
  263. cpu_clear(hotcpu, temp_cpu_online_map);
  264. check_cpu = any_online_cpu(temp_cpu_online_map);
  265. }
  266. break;
  267. case CPU_UP_CANCELED:
  268. case CPU_UP_CANCELED_FROZEN:
  269. if (!per_cpu(watchdog_task, hotcpu))
  270. break;
  271. /* Unbind so it can run. Fall thru. */
  272. kthread_bind(per_cpu(watchdog_task, hotcpu),
  273. any_online_cpu(cpu_online_map));
  274. case CPU_DEAD:
  275. case CPU_DEAD_FROZEN:
  276. p = per_cpu(watchdog_task, hotcpu);
  277. per_cpu(watchdog_task, hotcpu) = NULL;
  278. kthread_stop(p);
  279. break;
  280. #endif /* CONFIG_HOTPLUG_CPU */
  281. }
  282. return NOTIFY_OK;
  283. }
  284. static struct notifier_block __cpuinitdata cpu_nfb = {
  285. .notifier_call = cpu_callback
  286. };
  287. static int __initdata nosoftlockup;
  288. static int __init nosoftlockup_setup(char *str)
  289. {
  290. nosoftlockup = 1;
  291. return 1;
  292. }
  293. __setup("nosoftlockup", nosoftlockup_setup);
  294. static int __init spawn_softlockup_task(void)
  295. {
  296. void *cpu = (void *)(long)smp_processor_id();
  297. int err;
  298. if (nosoftlockup)
  299. return 0;
  300. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  301. if (err == NOTIFY_BAD) {
  302. BUG();
  303. return 1;
  304. }
  305. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  306. register_cpu_notifier(&cpu_nfb);
  307. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  308. return 0;
  309. }
  310. early_initcall(spawn_softlockup_task);