softlockup.c 7.1 KB

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