cpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* CPU control.
  2. * (C) 2001, 2002, 2003, 2004 Rusty Russell
  3. *
  4. * This code is licenced under the GPL.
  5. */
  6. #include <linux/proc_fs.h>
  7. #include <linux/smp.h>
  8. #include <linux/init.h>
  9. #include <linux/notifier.h>
  10. #include <linux/sched.h>
  11. #include <linux/unistd.h>
  12. #include <linux/cpu.h>
  13. #include <linux/module.h>
  14. #include <linux/kthread.h>
  15. #include <linux/stop_machine.h>
  16. #include <linux/mutex.h>
  17. /* This protects CPUs going up and down... */
  18. static DEFINE_MUTEX(cpu_add_remove_lock);
  19. static DEFINE_MUTEX(cpu_bitmask_lock);
  20. static __cpuinitdata BLOCKING_NOTIFIER_HEAD(cpu_chain);
  21. #ifdef CONFIG_HOTPLUG_CPU
  22. /* Crappy recursive lock-takers in cpufreq! Complain loudly about idiots */
  23. static struct task_struct *recursive;
  24. static int recursive_depth;
  25. void lock_cpu_hotplug(void)
  26. {
  27. struct task_struct *tsk = current;
  28. if (tsk == recursive) {
  29. static int warnings = 10;
  30. if (warnings) {
  31. printk(KERN_ERR "Lukewarm IQ detected in hotplug locking\n");
  32. WARN_ON(1);
  33. warnings--;
  34. }
  35. recursive_depth++;
  36. return;
  37. }
  38. mutex_lock(&cpu_bitmask_lock);
  39. recursive = tsk;
  40. }
  41. EXPORT_SYMBOL_GPL(lock_cpu_hotplug);
  42. void unlock_cpu_hotplug(void)
  43. {
  44. WARN_ON(recursive != current);
  45. if (recursive_depth) {
  46. recursive_depth--;
  47. return;
  48. }
  49. mutex_unlock(&cpu_bitmask_lock);
  50. recursive = NULL;
  51. }
  52. EXPORT_SYMBOL_GPL(unlock_cpu_hotplug);
  53. #endif /* CONFIG_HOTPLUG_CPU */
  54. /* Need to know about CPUs going up/down? */
  55. int __cpuinit register_cpu_notifier(struct notifier_block *nb)
  56. {
  57. return blocking_notifier_chain_register(&cpu_chain, nb);
  58. }
  59. #ifdef CONFIG_HOTPLUG_CPU
  60. EXPORT_SYMBOL(register_cpu_notifier);
  61. void unregister_cpu_notifier(struct notifier_block *nb)
  62. {
  63. blocking_notifier_chain_unregister(&cpu_chain, nb);
  64. }
  65. EXPORT_SYMBOL(unregister_cpu_notifier);
  66. static inline void check_for_tasks(int cpu)
  67. {
  68. struct task_struct *p;
  69. write_lock_irq(&tasklist_lock);
  70. for_each_process(p) {
  71. if (task_cpu(p) == cpu &&
  72. (!cputime_eq(p->utime, cputime_zero) ||
  73. !cputime_eq(p->stime, cputime_zero)))
  74. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  75. (state = %ld, flags = %lx) \n",
  76. p->comm, p->pid, cpu, p->state, p->flags);
  77. }
  78. write_unlock_irq(&tasklist_lock);
  79. }
  80. /* Take this CPU down. */
  81. static int take_cpu_down(void *unused)
  82. {
  83. int err;
  84. /* Ensure this CPU doesn't handle any more interrupts. */
  85. err = __cpu_disable();
  86. if (err < 0)
  87. return err;
  88. /* Force idle task to run as soon as we yield: it should
  89. immediately notice cpu is offline and die quickly. */
  90. sched_idle_next();
  91. return 0;
  92. }
  93. int cpu_down(unsigned int cpu)
  94. {
  95. int err;
  96. struct task_struct *p;
  97. cpumask_t old_allowed, tmp;
  98. mutex_lock(&cpu_add_remove_lock);
  99. if (num_online_cpus() == 1) {
  100. err = -EBUSY;
  101. goto out;
  102. }
  103. if (!cpu_online(cpu)) {
  104. err = -EINVAL;
  105. goto out;
  106. }
  107. err = blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
  108. (void *)(long)cpu);
  109. if (err == NOTIFY_BAD) {
  110. printk("%s: attempt to take down CPU %u failed\n",
  111. __FUNCTION__, cpu);
  112. err = -EINVAL;
  113. goto out;
  114. }
  115. /* Ensure that we are not runnable on dying cpu */
  116. old_allowed = current->cpus_allowed;
  117. tmp = CPU_MASK_ALL;
  118. cpu_clear(cpu, tmp);
  119. set_cpus_allowed(current, tmp);
  120. mutex_lock(&cpu_bitmask_lock);
  121. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  122. mutex_unlock(&cpu_bitmask_lock);
  123. if (IS_ERR(p)) {
  124. /* CPU didn't die: tell everyone. Can't complain. */
  125. if (blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  126. (void *)(long)cpu) == NOTIFY_BAD)
  127. BUG();
  128. err = PTR_ERR(p);
  129. goto out_allowed;
  130. }
  131. if (cpu_online(cpu))
  132. goto out_thread;
  133. /* Wait for it to sleep (leaving idle task). */
  134. while (!idle_cpu(cpu))
  135. yield();
  136. /* This actually kills the CPU. */
  137. __cpu_die(cpu);
  138. /* Move it here so it can run. */
  139. kthread_bind(p, get_cpu());
  140. put_cpu();
  141. /* CPU is completely dead: tell everyone. Too late to complain. */
  142. if (blocking_notifier_call_chain(&cpu_chain, CPU_DEAD,
  143. (void *)(long)cpu) == NOTIFY_BAD)
  144. BUG();
  145. check_for_tasks(cpu);
  146. out_thread:
  147. err = kthread_stop(p);
  148. out_allowed:
  149. set_cpus_allowed(current, old_allowed);
  150. out:
  151. mutex_unlock(&cpu_add_remove_lock);
  152. return err;
  153. }
  154. #endif /*CONFIG_HOTPLUG_CPU*/
  155. int __devinit cpu_up(unsigned int cpu)
  156. {
  157. int ret;
  158. void *hcpu = (void *)(long)cpu;
  159. mutex_lock(&cpu_add_remove_lock);
  160. if (cpu_online(cpu) || !cpu_present(cpu)) {
  161. ret = -EINVAL;
  162. goto out;
  163. }
  164. ret = blocking_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
  165. if (ret == NOTIFY_BAD) {
  166. printk("%s: attempt to bring up CPU %u failed\n",
  167. __FUNCTION__, cpu);
  168. ret = -EINVAL;
  169. goto out_notify;
  170. }
  171. /* Arch-specific enabling code. */
  172. mutex_lock(&cpu_bitmask_lock);
  173. ret = __cpu_up(cpu);
  174. mutex_unlock(&cpu_bitmask_lock);
  175. if (ret != 0)
  176. goto out_notify;
  177. BUG_ON(!cpu_online(cpu));
  178. /* Now call notifier in preparation. */
  179. blocking_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  180. out_notify:
  181. if (ret != 0)
  182. blocking_notifier_call_chain(&cpu_chain,
  183. CPU_UP_CANCELED, hcpu);
  184. out:
  185. mutex_unlock(&cpu_add_remove_lock);
  186. return ret;
  187. }