softlockup.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <linux/sysctl.h>
  20. #include <asm/irq_regs.h>
  21. static DEFINE_SPINLOCK(print_lock);
  22. static DEFINE_PER_CPU(unsigned long, touch_timestamp);
  23. static DEFINE_PER_CPU(unsigned long, print_timestamp);
  24. static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
  25. static int __read_mostly did_panic;
  26. int __read_mostly softlockup_thresh = 60;
  27. /*
  28. * Should we panic (and reboot, if panic_timeout= is set) when a
  29. * soft-lockup occurs:
  30. */
  31. unsigned int __read_mostly softlockup_panic =
  32. CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
  33. static int __init softlockup_panic_setup(char *str)
  34. {
  35. softlockup_panic = simple_strtoul(str, NULL, 0);
  36. return 1;
  37. }
  38. __setup("softlockup_panic=", softlockup_panic_setup);
  39. static int
  40. softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
  41. {
  42. did_panic = 1;
  43. return NOTIFY_DONE;
  44. }
  45. static struct notifier_block panic_block = {
  46. .notifier_call = softlock_panic,
  47. };
  48. /*
  49. * Returns seconds, approximately. We don't need nanosecond
  50. * resolution, and we don't need to waste time with a big divide when
  51. * 2^30ns == 1.074s.
  52. */
  53. static unsigned long get_timestamp(int this_cpu)
  54. {
  55. return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
  56. }
  57. static void __touch_softlockup_watchdog(void)
  58. {
  59. int this_cpu = raw_smp_processor_id();
  60. __raw_get_cpu_var(touch_timestamp) = get_timestamp(this_cpu);
  61. }
  62. void touch_softlockup_watchdog(void)
  63. {
  64. __raw_get_cpu_var(touch_timestamp) = 0;
  65. }
  66. EXPORT_SYMBOL(touch_softlockup_watchdog);
  67. void touch_all_softlockup_watchdogs(void)
  68. {
  69. int cpu;
  70. /* Cause each CPU to re-update its timestamp rather than complain */
  71. for_each_online_cpu(cpu)
  72. per_cpu(touch_timestamp, cpu) = 0;
  73. }
  74. EXPORT_SYMBOL(touch_all_softlockup_watchdogs);
  75. int proc_dosoftlockup_thresh(struct ctl_table *table, int write,
  76. struct file *filp, void __user *buffer,
  77. size_t *lenp, loff_t *ppos)
  78. {
  79. touch_all_softlockup_watchdogs();
  80. return proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  81. }
  82. /*
  83. * This callback runs from the timer interrupt, and checks
  84. * whether the watchdog thread has hung or not:
  85. */
  86. void softlockup_tick(void)
  87. {
  88. int this_cpu = smp_processor_id();
  89. unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
  90. unsigned long print_timestamp;
  91. struct pt_regs *regs = get_irq_regs();
  92. unsigned long now;
  93. /* Is detection switched off? */
  94. if (!per_cpu(watchdog_task, this_cpu) || softlockup_thresh <= 0) {
  95. /* Be sure we don't false trigger if switched back on */
  96. if (touch_timestamp)
  97. per_cpu(touch_timestamp, this_cpu) = 0;
  98. return;
  99. }
  100. if (touch_timestamp == 0) {
  101. __touch_softlockup_watchdog();
  102. return;
  103. }
  104. print_timestamp = per_cpu(print_timestamp, this_cpu);
  105. /* report at most once a second */
  106. if (print_timestamp == touch_timestamp || did_panic)
  107. return;
  108. /* do not print during early bootup: */
  109. if (unlikely(system_state != SYSTEM_RUNNING)) {
  110. __touch_softlockup_watchdog();
  111. return;
  112. }
  113. now = get_timestamp(this_cpu);
  114. /*
  115. * Wake up the high-prio watchdog task twice per
  116. * threshold timespan.
  117. */
  118. if (now > touch_timestamp + softlockup_thresh/2)
  119. wake_up_process(per_cpu(watchdog_task, this_cpu));
  120. /* Warn about unreasonable delays: */
  121. if (now <= (touch_timestamp + softlockup_thresh))
  122. return;
  123. per_cpu(print_timestamp, this_cpu) = touch_timestamp;
  124. spin_lock(&print_lock);
  125. printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d]\n",
  126. this_cpu, now - touch_timestamp,
  127. current->comm, task_pid_nr(current));
  128. print_modules();
  129. print_irqtrace_events(current);
  130. if (regs)
  131. show_regs(regs);
  132. else
  133. dump_stack();
  134. spin_unlock(&print_lock);
  135. if (softlockup_panic)
  136. panic("softlockup: hung tasks");
  137. }
  138. /*
  139. * The watchdog thread - runs every second and touches the timestamp.
  140. */
  141. static int watchdog(void *__bind_cpu)
  142. {
  143. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  144. sched_setscheduler(current, SCHED_FIFO, &param);
  145. /* initialize timestamp */
  146. __touch_softlockup_watchdog();
  147. set_current_state(TASK_INTERRUPTIBLE);
  148. /*
  149. * Run briefly once per second to reset the softlockup timestamp.
  150. * If this gets delayed for more than 60 seconds then the
  151. * debug-printout triggers in softlockup_tick().
  152. */
  153. while (!kthread_should_stop()) {
  154. __touch_softlockup_watchdog();
  155. schedule();
  156. if (kthread_should_stop())
  157. break;
  158. set_current_state(TASK_INTERRUPTIBLE);
  159. }
  160. __set_current_state(TASK_RUNNING);
  161. return 0;
  162. }
  163. /*
  164. * Create/destroy watchdog threads as CPUs come and go:
  165. */
  166. static int __cpuinit
  167. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  168. {
  169. int hotcpu = (unsigned long)hcpu;
  170. struct task_struct *p;
  171. switch (action) {
  172. case CPU_UP_PREPARE:
  173. case CPU_UP_PREPARE_FROZEN:
  174. BUG_ON(per_cpu(watchdog_task, hotcpu));
  175. p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
  176. if (IS_ERR(p)) {
  177. printk(KERN_ERR "watchdog for %i failed\n", hotcpu);
  178. return NOTIFY_BAD;
  179. }
  180. per_cpu(touch_timestamp, hotcpu) = 0;
  181. per_cpu(watchdog_task, hotcpu) = p;
  182. kthread_bind(p, hotcpu);
  183. break;
  184. case CPU_ONLINE:
  185. case CPU_ONLINE_FROZEN:
  186. wake_up_process(per_cpu(watchdog_task, hotcpu));
  187. break;
  188. #ifdef CONFIG_HOTPLUG_CPU
  189. case CPU_UP_CANCELED:
  190. case CPU_UP_CANCELED_FROZEN:
  191. if (!per_cpu(watchdog_task, hotcpu))
  192. break;
  193. /* Unbind so it can run. Fall thru. */
  194. kthread_bind(per_cpu(watchdog_task, hotcpu),
  195. cpumask_any(cpu_online_mask));
  196. case CPU_DEAD:
  197. case CPU_DEAD_FROZEN:
  198. p = per_cpu(watchdog_task, hotcpu);
  199. per_cpu(watchdog_task, hotcpu) = NULL;
  200. kthread_stop(p);
  201. break;
  202. #endif /* CONFIG_HOTPLUG_CPU */
  203. }
  204. return NOTIFY_OK;
  205. }
  206. static struct notifier_block __cpuinitdata cpu_nfb = {
  207. .notifier_call = cpu_callback
  208. };
  209. static int __initdata nosoftlockup;
  210. static int __init nosoftlockup_setup(char *str)
  211. {
  212. nosoftlockup = 1;
  213. return 1;
  214. }
  215. __setup("nosoftlockup", nosoftlockup_setup);
  216. static int __init spawn_softlockup_task(void)
  217. {
  218. void *cpu = (void *)(long)smp_processor_id();
  219. int err;
  220. if (nosoftlockup)
  221. return 0;
  222. err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  223. if (err == NOTIFY_BAD) {
  224. BUG();
  225. return 1;
  226. }
  227. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  228. register_cpu_notifier(&cpu_nfb);
  229. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  230. return 0;
  231. }
  232. early_initcall(spawn_softlockup_task);