softlockup.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/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, touch_timestamp);
  18. static DEFINE_PER_CPU(unsigned long, print_timestamp);
  19. static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
  20. static int did_panic = 0;
  21. static int
  22. softlock_panic(struct notifier_block *this, unsigned long event, 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. __raw_get_cpu_var(touch_timestamp) = jiffies;
  33. }
  34. EXPORT_SYMBOL(touch_softlockup_watchdog);
  35. void touch_all_softlockup_watchdogs(void)
  36. {
  37. int cpu;
  38. /* Cause each CPU to re-update its timestamp rather than complain */
  39. for_each_online_cpu(cpu)
  40. per_cpu(touch_timestamp, cpu) = 0;
  41. }
  42. EXPORT_SYMBOL(touch_all_softlockup_watchdogs);
  43. /*
  44. * This callback runs from the timer interrupt, and checks
  45. * whether the watchdog thread has hung or not:
  46. */
  47. void softlockup_tick(void)
  48. {
  49. int this_cpu = smp_processor_id();
  50. unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
  51. if (touch_timestamp == 0) {
  52. touch_softlockup_watchdog();
  53. return;
  54. }
  55. /* prevent double reports: */
  56. if (per_cpu(print_timestamp, this_cpu) == touch_timestamp ||
  57. did_panic ||
  58. !per_cpu(watchdog_task, this_cpu))
  59. return;
  60. /* do not print during early bootup: */
  61. if (unlikely(system_state != SYSTEM_RUNNING)) {
  62. touch_softlockup_watchdog();
  63. return;
  64. }
  65. /* Wake up the high-prio watchdog task every second: */
  66. if (time_after(jiffies, touch_timestamp + HZ))
  67. wake_up_process(per_cpu(watchdog_task, this_cpu));
  68. /* Warn about unreasonable 10+ seconds delays: */
  69. if (time_after(jiffies, touch_timestamp + 10*HZ)) {
  70. per_cpu(print_timestamp, this_cpu) = touch_timestamp;
  71. spin_lock(&print_lock);
  72. printk(KERN_ERR "BUG: soft lockup detected on CPU#%d!\n",
  73. this_cpu);
  74. dump_stack();
  75. spin_unlock(&print_lock);
  76. }
  77. }
  78. /*
  79. * The watchdog thread - runs every second and touches the timestamp.
  80. */
  81. static int watchdog(void * __bind_cpu)
  82. {
  83. struct sched_param param = { .sched_priority = 99 };
  84. sched_setscheduler(current, SCHED_FIFO, &param);
  85. current->flags |= PF_NOFREEZE;
  86. /*
  87. * Run briefly once per second to reset the softlockup timestamp.
  88. * If this gets delayed for more than 10 seconds then the
  89. * debug-printout triggers in softlockup_tick().
  90. */
  91. while (!kthread_should_stop()) {
  92. set_current_state(TASK_INTERRUPTIBLE);
  93. touch_softlockup_watchdog();
  94. schedule();
  95. }
  96. return 0;
  97. }
  98. /*
  99. * Create/destroy watchdog threads as CPUs come and go:
  100. */
  101. static int __cpuinit
  102. cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  103. {
  104. int hotcpu = (unsigned long)hcpu;
  105. struct task_struct *p;
  106. switch (action) {
  107. case CPU_UP_PREPARE:
  108. BUG_ON(per_cpu(watchdog_task, hotcpu));
  109. p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
  110. if (IS_ERR(p)) {
  111. printk("watchdog for %i failed\n", hotcpu);
  112. return NOTIFY_BAD;
  113. }
  114. per_cpu(touch_timestamp, hotcpu) = jiffies;
  115. per_cpu(watchdog_task, hotcpu) = p;
  116. kthread_bind(p, hotcpu);
  117. break;
  118. case CPU_ONLINE:
  119. wake_up_process(per_cpu(watchdog_task, hotcpu));
  120. break;
  121. #ifdef CONFIG_HOTPLUG_CPU
  122. case CPU_UP_CANCELED:
  123. if (!per_cpu(watchdog_task, hotcpu))
  124. break;
  125. /* Unbind so it can run. Fall thru. */
  126. kthread_bind(per_cpu(watchdog_task, hotcpu),
  127. any_online_cpu(cpu_online_map));
  128. case CPU_DEAD:
  129. p = per_cpu(watchdog_task, hotcpu);
  130. per_cpu(watchdog_task, hotcpu) = NULL;
  131. kthread_stop(p);
  132. break;
  133. #endif /* CONFIG_HOTPLUG_CPU */
  134. }
  135. return NOTIFY_OK;
  136. }
  137. static struct notifier_block __cpuinitdata cpu_nfb = {
  138. .notifier_call = cpu_callback
  139. };
  140. __init void spawn_softlockup_task(void)
  141. {
  142. void *cpu = (void *)(long)smp_processor_id();
  143. int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  144. BUG_ON(err == NOTIFY_BAD);
  145. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  146. register_cpu_notifier(&cpu_nfb);
  147. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  148. }