cpu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 BLOCKING_NOTIFIER_HEAD(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. return blocking_notifier_chain_register(&cpu_chain, nb);
  64. }
  65. EXPORT_SYMBOL(register_cpu_notifier);
  66. void unregister_cpu_notifier(struct notifier_block *nb)
  67. {
  68. blocking_notifier_chain_unregister(&cpu_chain, nb);
  69. }
  70. EXPORT_SYMBOL(unregister_cpu_notifier);
  71. #ifdef CONFIG_HOTPLUG_CPU
  72. static inline void check_for_tasks(int cpu)
  73. {
  74. struct task_struct *p;
  75. write_lock_irq(&tasklist_lock);
  76. for_each_process(p) {
  77. if (task_cpu(p) == cpu &&
  78. (!cputime_eq(p->utime, cputime_zero) ||
  79. !cputime_eq(p->stime, cputime_zero)))
  80. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  81. (state = %ld, flags = %lx) \n",
  82. p->comm, p->pid, cpu, p->state, p->flags);
  83. }
  84. write_unlock_irq(&tasklist_lock);
  85. }
  86. /* Take this CPU down. */
  87. static int take_cpu_down(void *unused)
  88. {
  89. int err;
  90. /* Ensure this CPU doesn't handle any more interrupts. */
  91. err = __cpu_disable();
  92. if (err < 0)
  93. return err;
  94. /* Force idle task to run as soon as we yield: it should
  95. immediately notice cpu is offline and die quickly. */
  96. sched_idle_next();
  97. return 0;
  98. }
  99. int cpu_down(unsigned int cpu)
  100. {
  101. int err;
  102. struct task_struct *p;
  103. cpumask_t old_allowed, tmp;
  104. if ((err = lock_cpu_hotplug_interruptible()) != 0)
  105. return err;
  106. if (num_online_cpus() == 1) {
  107. err = -EBUSY;
  108. goto out;
  109. }
  110. if (!cpu_online(cpu)) {
  111. err = -EINVAL;
  112. goto out;
  113. }
  114. err = blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
  115. (void *)(long)cpu);
  116. if (err == NOTIFY_BAD) {
  117. printk("%s: attempt to take down CPU %u failed\n",
  118. __FUNCTION__, cpu);
  119. err = -EINVAL;
  120. goto out;
  121. }
  122. /* Ensure that we are not runnable on dying cpu */
  123. old_allowed = current->cpus_allowed;
  124. tmp = CPU_MASK_ALL;
  125. cpu_clear(cpu, tmp);
  126. set_cpus_allowed(current, tmp);
  127. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  128. if (IS_ERR(p)) {
  129. /* CPU didn't die: tell everyone. Can't complain. */
  130. if (blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  131. (void *)(long)cpu) == NOTIFY_BAD)
  132. BUG();
  133. err = PTR_ERR(p);
  134. goto out_allowed;
  135. }
  136. if (cpu_online(cpu))
  137. goto out_thread;
  138. /* Wait for it to sleep (leaving idle task). */
  139. while (!idle_cpu(cpu))
  140. yield();
  141. /* This actually kills the CPU. */
  142. __cpu_die(cpu);
  143. /* Move it here so it can run. */
  144. kthread_bind(p, get_cpu());
  145. put_cpu();
  146. /* CPU is completely dead: tell everyone. Too late to complain. */
  147. if (blocking_notifier_call_chain(&cpu_chain, CPU_DEAD,
  148. (void *)(long)cpu) == NOTIFY_BAD)
  149. BUG();
  150. check_for_tasks(cpu);
  151. out_thread:
  152. err = kthread_stop(p);
  153. out_allowed:
  154. set_cpus_allowed(current, old_allowed);
  155. out:
  156. unlock_cpu_hotplug();
  157. return err;
  158. }
  159. #endif /*CONFIG_HOTPLUG_CPU*/
  160. int __devinit cpu_up(unsigned int cpu)
  161. {
  162. int ret;
  163. void *hcpu = (void *)(long)cpu;
  164. if ((ret = lock_cpu_hotplug_interruptible()) != 0)
  165. return ret;
  166. if (cpu_online(cpu) || !cpu_present(cpu)) {
  167. ret = -EINVAL;
  168. goto out;
  169. }
  170. ret = blocking_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
  171. if (ret == NOTIFY_BAD) {
  172. printk("%s: attempt to bring up CPU %u failed\n",
  173. __FUNCTION__, cpu);
  174. ret = -EINVAL;
  175. goto out_notify;
  176. }
  177. /* Arch-specific enabling code. */
  178. ret = __cpu_up(cpu);
  179. if (ret != 0)
  180. goto out_notify;
  181. BUG_ON(!cpu_online(cpu));
  182. /* Now call notifier in preparation. */
  183. blocking_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  184. out_notify:
  185. if (ret != 0)
  186. blocking_notifier_call_chain(&cpu_chain,
  187. CPU_UP_CANCELED, hcpu);
  188. out:
  189. unlock_cpu_hotplug();
  190. return ret;
  191. }