softlockup.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Detect Soft Lockups
  3. *
  4. * started by Ingo Molnar, (C) 2005, Red Hat
  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/kthread.h>
  14. #include <linux/notifier.h>
  15. #include <linux/module.h>
  16. static DEFINE_SPINLOCK(print_lock);
  17. static DEFINE_PER_CPU(unsigned long, timestamp) = 0;
  18. static DEFINE_PER_CPU(unsigned long, print_timestamp) = 0;
  19. static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
  20. static int did_panic = 0;
  21. static int softlock_panic(struct notifier_block *this, unsigned long event,
  22. void *ptr)
  23. {
  24. did_panic = 1;
  25. return NOTIFY_DONE;
  26. }
  27. static struct notifier_block panic_block = {
  28. .notifier_call = softlock_panic,
  29. };
  30. void touch_softlockup_watchdog(void)
  31. {
  32. per_cpu(timestamp, raw_smp_processor_id()) = jiffies;
  33. }
  34. EXPORT_SYMBOL(touch_softlockup_watchdog);
  35. /*
  36. * This callback runs from the timer interrupt, and checks
  37. * whether the watchdog thread has hung or not:
  38. */
  39. void softlockup_tick(struct pt_regs *regs)
  40. {
  41. int this_cpu = smp_processor_id();
  42. unsigned long timestamp = per_cpu(timestamp, this_cpu);
  43. if (per_cpu(print_timestamp, this_cpu) == timestamp)
  44. return;
  45. /* Do not cause a second panic when there already was one */
  46. if (did_panic)
  47. return;
  48. if (time_after(jiffies, timestamp + 10*HZ)) {
  49. per_cpu(print_timestamp, this_cpu) = timestamp;
  50. spin_lock(&print_lock);
  51. printk(KERN_ERR "BUG: soft lockup detected on CPU#%d!\n",
  52. this_cpu);
  53. show_regs(regs);
  54. spin_unlock(&print_lock);
  55. }
  56. }
  57. /*
  58. * The watchdog thread - runs every second and touches the timestamp.
  59. */
  60. static int watchdog(void * __bind_cpu)
  61. {
  62. struct sched_param param = { .sched_priority = 99 };
  63. sched_setscheduler(current, SCHED_FIFO, &param);
  64. current->flags |= PF_NOFREEZE;
  65. set_current_state(TASK_INTERRUPTIBLE);
  66. /*
  67. * Run briefly once per second - if this gets delayed for
  68. * more than 10 seconds then the debug-printout triggers
  69. * in softlockup_tick():
  70. */
  71. while (!kthread_should_stop()) {
  72. msleep_interruptible(1000);
  73. touch_softlockup_watchdog();
  74. }
  75. __set_current_state(TASK_RUNNING);
  76. return 0;
  77. }
  78. /*
  79. * Create/destroy watchdog threads as CPUs come and go:
  80. */
  81. static int __devinit
  82. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  83. {
  84. int hotcpu = (unsigned long)hcpu;
  85. struct task_struct *p;
  86. switch (action) {
  87. case CPU_UP_PREPARE:
  88. BUG_ON(per_cpu(watchdog_task, hotcpu));
  89. p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
  90. if (IS_ERR(p)) {
  91. printk("watchdog for %i failed\n", hotcpu);
  92. return NOTIFY_BAD;
  93. }
  94. per_cpu(watchdog_task, hotcpu) = p;
  95. kthread_bind(p, hotcpu);
  96. break;
  97. case CPU_ONLINE:
  98. wake_up_process(per_cpu(watchdog_task, hotcpu));
  99. break;
  100. #ifdef CONFIG_HOTPLUG_CPU
  101. case CPU_UP_CANCELED:
  102. /* Unbind so it can run. Fall thru. */
  103. kthread_bind(per_cpu(watchdog_task, hotcpu),
  104. any_online_cpu(cpu_online_map));
  105. case CPU_DEAD:
  106. p = per_cpu(watchdog_task, hotcpu);
  107. per_cpu(watchdog_task, hotcpu) = NULL;
  108. kthread_stop(p);
  109. break;
  110. #endif /* CONFIG_HOTPLUG_CPU */
  111. }
  112. return NOTIFY_OK;
  113. }
  114. static struct notifier_block __devinitdata cpu_nfb = {
  115. .notifier_call = cpu_callback
  116. };
  117. __init void spawn_softlockup_task(void)
  118. {
  119. void *cpu = (void *)(long)smp_processor_id();
  120. cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  121. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  122. register_cpu_notifier(&cpu_nfb);
  123. notifier_chain_register(&panic_notifier_list, &panic_block);
  124. }