softlockup.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/freezer.h>
  14. #include <linux/kthread.h>
  15. #include <linux/notifier.h>
  16. #include <linux/module.h>
  17. #include <asm/irq_regs.h>
  18. static DEFINE_SPINLOCK(print_lock);
  19. static DEFINE_PER_CPU(unsigned long, touch_timestamp);
  20. static DEFINE_PER_CPU(unsigned long, print_timestamp);
  21. static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
  22. static int did_panic;
  23. int softlockup_thresh = 10;
  24. static int
  25. softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
  26. {
  27. did_panic = 1;
  28. return NOTIFY_DONE;
  29. }
  30. static struct notifier_block panic_block = {
  31. .notifier_call = softlock_panic,
  32. };
  33. /*
  34. * Returns seconds, approximately. We don't need nanosecond
  35. * resolution, and we don't need to waste time with a big divide when
  36. * 2^30ns == 1.074s.
  37. */
  38. static unsigned long get_timestamp(int this_cpu)
  39. {
  40. return cpu_clock(this_cpu) >> 30; /* 2^30 ~= 10^9 */
  41. }
  42. void touch_softlockup_watchdog(void)
  43. {
  44. int this_cpu = raw_smp_processor_id();
  45. __raw_get_cpu_var(touch_timestamp) = get_timestamp(this_cpu);
  46. }
  47. EXPORT_SYMBOL(touch_softlockup_watchdog);
  48. void touch_all_softlockup_watchdogs(void)
  49. {
  50. int cpu;
  51. /* Cause each CPU to re-update its timestamp rather than complain */
  52. for_each_online_cpu(cpu)
  53. per_cpu(touch_timestamp, cpu) = 0;
  54. }
  55. EXPORT_SYMBOL(touch_all_softlockup_watchdogs);
  56. /*
  57. * This callback runs from the timer interrupt, and checks
  58. * whether the watchdog thread has hung or not:
  59. */
  60. void softlockup_tick(void)
  61. {
  62. int this_cpu = smp_processor_id();
  63. unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
  64. unsigned long print_timestamp;
  65. struct pt_regs *regs = get_irq_regs();
  66. unsigned long now;
  67. if (touch_timestamp == 0) {
  68. touch_softlockup_watchdog();
  69. return;
  70. }
  71. print_timestamp = per_cpu(print_timestamp, this_cpu);
  72. /* report at most once a second */
  73. if ((print_timestamp >= touch_timestamp &&
  74. print_timestamp < (touch_timestamp + 1)) ||
  75. did_panic || !per_cpu(watchdog_task, this_cpu)) {
  76. return;
  77. }
  78. /* do not print during early bootup: */
  79. if (unlikely(system_state != SYSTEM_RUNNING)) {
  80. touch_softlockup_watchdog();
  81. return;
  82. }
  83. now = get_timestamp(this_cpu);
  84. /* Wake up the high-prio watchdog task every second: */
  85. if (now > (touch_timestamp + 1))
  86. wake_up_process(per_cpu(watchdog_task, this_cpu));
  87. /* Warn about unreasonable 10+ seconds delays: */
  88. if (now <= (touch_timestamp + softlockup_thresh))
  89. return;
  90. per_cpu(print_timestamp, this_cpu) = touch_timestamp;
  91. spin_lock(&print_lock);
  92. printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d]\n",
  93. this_cpu, now - touch_timestamp,
  94. current->comm, task_pid_nr(current));
  95. if (regs)
  96. show_regs(regs);
  97. else
  98. dump_stack();
  99. spin_unlock(&print_lock);
  100. }
  101. /*
  102. * The watchdog thread - runs every second and touches the timestamp.
  103. */
  104. static int watchdog(void *__bind_cpu)
  105. {
  106. struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
  107. sched_setscheduler(current, SCHED_FIFO, &param);
  108. /* initialize timestamp */
  109. touch_softlockup_watchdog();
  110. /*
  111. * Run briefly once per second to reset the softlockup timestamp.
  112. * If this gets delayed for more than 10 seconds then the
  113. * debug-printout triggers in softlockup_tick().
  114. */
  115. while (!kthread_should_stop()) {
  116. set_current_state(TASK_INTERRUPTIBLE);
  117. touch_softlockup_watchdog();
  118. schedule();
  119. }
  120. return 0;
  121. }
  122. /*
  123. * Create/destroy watchdog threads as CPUs come and go:
  124. */
  125. static int __cpuinit
  126. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  127. {
  128. int hotcpu = (unsigned long)hcpu;
  129. struct task_struct *p;
  130. switch (action) {
  131. case CPU_UP_PREPARE:
  132. case CPU_UP_PREPARE_FROZEN:
  133. BUG_ON(per_cpu(watchdog_task, hotcpu));
  134. p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
  135. if (IS_ERR(p)) {
  136. printk(KERN_ERR "watchdog for %i failed\n", hotcpu);
  137. return NOTIFY_BAD;
  138. }
  139. per_cpu(touch_timestamp, hotcpu) = 0;
  140. per_cpu(watchdog_task, hotcpu) = p;
  141. kthread_bind(p, hotcpu);
  142. break;
  143. case CPU_ONLINE:
  144. case CPU_ONLINE_FROZEN:
  145. wake_up_process(per_cpu(watchdog_task, hotcpu));
  146. break;
  147. #ifdef CONFIG_HOTPLUG_CPU
  148. case CPU_UP_CANCELED:
  149. case CPU_UP_CANCELED_FROZEN:
  150. if (!per_cpu(watchdog_task, hotcpu))
  151. break;
  152. /* Unbind so it can run. Fall thru. */
  153. kthread_bind(per_cpu(watchdog_task, hotcpu),
  154. any_online_cpu(cpu_online_map));
  155. case CPU_DEAD:
  156. case CPU_DEAD_FROZEN:
  157. p = per_cpu(watchdog_task, hotcpu);
  158. per_cpu(watchdog_task, hotcpu) = NULL;
  159. kthread_stop(p);
  160. break;
  161. #endif /* CONFIG_HOTPLUG_CPU */
  162. }
  163. return NOTIFY_OK;
  164. }
  165. static struct notifier_block __cpuinitdata cpu_nfb = {
  166. .notifier_call = cpu_callback
  167. };
  168. __init void spawn_softlockup_task(void)
  169. {
  170. void *cpu = (void *)(long)smp_processor_id();
  171. int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  172. BUG_ON(err == NOTIFY_BAD);
  173. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  174. register_cpu_notifier(&cpu_nfb);
  175. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  176. }