cpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <asm/semaphore.h>
  17. /* This protects CPUs going up and down... */
  18. static DECLARE_MUTEX(cpucontrol);
  19. static struct notifier_block *cpu_chain;
  20. #ifdef CONFIG_HOTPLUG_CPU
  21. static struct task_struct *lock_cpu_hotplug_owner;
  22. static int lock_cpu_hotplug_depth;
  23. static int __lock_cpu_hotplug(int interruptible)
  24. {
  25. int ret = 0;
  26. if (lock_cpu_hotplug_owner != current) {
  27. if (interruptible)
  28. ret = down_interruptible(&cpucontrol);
  29. else
  30. down(&cpucontrol);
  31. }
  32. /*
  33. * Set only if we succeed in locking
  34. */
  35. if (!ret) {
  36. lock_cpu_hotplug_depth++;
  37. lock_cpu_hotplug_owner = current;
  38. }
  39. return ret;
  40. }
  41. void lock_cpu_hotplug(void)
  42. {
  43. __lock_cpu_hotplug(0);
  44. }
  45. EXPORT_SYMBOL_GPL(lock_cpu_hotplug);
  46. void unlock_cpu_hotplug(void)
  47. {
  48. if (--lock_cpu_hotplug_depth == 0) {
  49. lock_cpu_hotplug_owner = NULL;
  50. up(&cpucontrol);
  51. }
  52. }
  53. EXPORT_SYMBOL_GPL(unlock_cpu_hotplug);
  54. int lock_cpu_hotplug_interruptible(void)
  55. {
  56. return __lock_cpu_hotplug(1);
  57. }
  58. EXPORT_SYMBOL_GPL(lock_cpu_hotplug_interruptible);
  59. #endif /* CONFIG_HOTPLUG_CPU */
  60. /* Need to know about CPUs going up/down? */
  61. int register_cpu_notifier(struct notifier_block *nb)
  62. {
  63. int ret;
  64. if ((ret = lock_cpu_hotplug_interruptible()) != 0)
  65. return ret;
  66. ret = notifier_chain_register(&cpu_chain, nb);
  67. unlock_cpu_hotplug();
  68. return ret;
  69. }
  70. EXPORT_SYMBOL(register_cpu_notifier);
  71. void unregister_cpu_notifier(struct notifier_block *nb)
  72. {
  73. lock_cpu_hotplug();
  74. notifier_chain_unregister(&cpu_chain, nb);
  75. unlock_cpu_hotplug();
  76. }
  77. EXPORT_SYMBOL(unregister_cpu_notifier);
  78. #ifdef CONFIG_HOTPLUG_CPU
  79. static inline void check_for_tasks(int cpu)
  80. {
  81. struct task_struct *p;
  82. write_lock_irq(&tasklist_lock);
  83. for_each_process(p) {
  84. if (task_cpu(p) == cpu &&
  85. (!cputime_eq(p->utime, cputime_zero) ||
  86. !cputime_eq(p->stime, cputime_zero)))
  87. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  88. (state = %ld, flags = %lx) \n",
  89. p->comm, p->pid, cpu, p->state, p->flags);
  90. }
  91. write_unlock_irq(&tasklist_lock);
  92. }
  93. /* Take this CPU down. */
  94. static int take_cpu_down(void *unused)
  95. {
  96. int err;
  97. /* Ensure this CPU doesn't handle any more interrupts. */
  98. err = __cpu_disable();
  99. if (err < 0)
  100. return err;
  101. /* Force idle task to run as soon as we yield: it should
  102. immediately notice cpu is offline and die quickly. */
  103. sched_idle_next();
  104. return 0;
  105. }
  106. int cpu_down(unsigned int cpu)
  107. {
  108. int err;
  109. struct task_struct *p;
  110. cpumask_t old_allowed, tmp;
  111. if ((err = lock_cpu_hotplug_interruptible()) != 0)
  112. return err;
  113. if (num_online_cpus() == 1) {
  114. err = -EBUSY;
  115. goto out;
  116. }
  117. if (!cpu_online(cpu)) {
  118. err = -EINVAL;
  119. goto out;
  120. }
  121. err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
  122. (void *)(long)cpu);
  123. if (err == NOTIFY_BAD) {
  124. printk("%s: attempt to take down CPU %u failed\n",
  125. __FUNCTION__, cpu);
  126. err = -EINVAL;
  127. goto out;
  128. }
  129. /* Ensure that we are not runnable on dying cpu */
  130. old_allowed = current->cpus_allowed;
  131. tmp = CPU_MASK_ALL;
  132. cpu_clear(cpu, tmp);
  133. set_cpus_allowed(current, tmp);
  134. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  135. if (IS_ERR(p)) {
  136. /* CPU didn't die: tell everyone. Can't complain. */
  137. if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  138. (void *)(long)cpu) == NOTIFY_BAD)
  139. BUG();
  140. err = PTR_ERR(p);
  141. goto out_allowed;
  142. }
  143. if (cpu_online(cpu))
  144. goto out_thread;
  145. /* Wait for it to sleep (leaving idle task). */
  146. while (!idle_cpu(cpu))
  147. yield();
  148. /* This actually kills the CPU. */
  149. __cpu_die(cpu);
  150. /* Move it here so it can run. */
  151. kthread_bind(p, get_cpu());
  152. put_cpu();
  153. /* CPU is completely dead: tell everyone. Too late to complain. */
  154. if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
  155. == NOTIFY_BAD)
  156. BUG();
  157. check_for_tasks(cpu);
  158. out_thread:
  159. err = kthread_stop(p);
  160. out_allowed:
  161. set_cpus_allowed(current, old_allowed);
  162. out:
  163. unlock_cpu_hotplug();
  164. return err;
  165. }
  166. #endif /*CONFIG_HOTPLUG_CPU*/
  167. int __devinit cpu_up(unsigned int cpu)
  168. {
  169. int ret;
  170. void *hcpu = (void *)(long)cpu;
  171. if ((ret = lock_cpu_hotplug_interruptible()) != 0)
  172. return ret;
  173. if (cpu_online(cpu) || !cpu_present(cpu)) {
  174. ret = -EINVAL;
  175. goto out;
  176. }
  177. ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
  178. if (ret == NOTIFY_BAD) {
  179. printk("%s: attempt to bring up CPU %u failed\n",
  180. __FUNCTION__, cpu);
  181. ret = -EINVAL;
  182. goto out_notify;
  183. }
  184. /* Arch-specific enabling code. */
  185. ret = __cpu_up(cpu);
  186. if (ret != 0)
  187. goto out_notify;
  188. if (!cpu_online(cpu))
  189. BUG();
  190. /* Now call notifier in preparation. */
  191. notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  192. out_notify:
  193. if (ret != 0)
  194. notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
  195. out:
  196. unlock_cpu_hotplug();
  197. return ret;
  198. }