cpu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  22. * Should always be manipulated under cpu_add_remove_lock
  23. */
  24. static int cpu_hotplug_disabled;
  25. #ifdef CONFIG_HOTPLUG_CPU
  26. /* Crappy recursive lock-takers in cpufreq! Complain loudly about idiots */
  27. static struct task_struct *recursive;
  28. static int recursive_depth;
  29. void lock_cpu_hotplug(void)
  30. {
  31. struct task_struct *tsk = current;
  32. if (tsk == recursive) {
  33. static int warnings = 10;
  34. if (warnings) {
  35. printk(KERN_ERR "Lukewarm IQ detected in hotplug locking\n");
  36. WARN_ON(1);
  37. warnings--;
  38. }
  39. recursive_depth++;
  40. return;
  41. }
  42. mutex_lock(&cpu_bitmask_lock);
  43. recursive = tsk;
  44. }
  45. EXPORT_SYMBOL_GPL(lock_cpu_hotplug);
  46. void unlock_cpu_hotplug(void)
  47. {
  48. WARN_ON(recursive != current);
  49. if (recursive_depth) {
  50. recursive_depth--;
  51. return;
  52. }
  53. mutex_unlock(&cpu_bitmask_lock);
  54. recursive = NULL;
  55. }
  56. EXPORT_SYMBOL_GPL(unlock_cpu_hotplug);
  57. #endif /* CONFIG_HOTPLUG_CPU */
  58. /* Need to know about CPUs going up/down? */
  59. int __cpuinit register_cpu_notifier(struct notifier_block *nb)
  60. {
  61. return blocking_notifier_chain_register(&cpu_chain, nb);
  62. }
  63. #ifdef CONFIG_HOTPLUG_CPU
  64. EXPORT_SYMBOL(register_cpu_notifier);
  65. void unregister_cpu_notifier(struct notifier_block *nb)
  66. {
  67. blocking_notifier_chain_unregister(&cpu_chain, nb);
  68. }
  69. EXPORT_SYMBOL(unregister_cpu_notifier);
  70. static inline void check_for_tasks(int cpu)
  71. {
  72. struct task_struct *p;
  73. write_lock_irq(&tasklist_lock);
  74. for_each_process(p) {
  75. if (task_cpu(p) == cpu &&
  76. (!cputime_eq(p->utime, cputime_zero) ||
  77. !cputime_eq(p->stime, cputime_zero)))
  78. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  79. (state = %ld, flags = %lx) \n",
  80. p->comm, p->pid, cpu, p->state, p->flags);
  81. }
  82. write_unlock_irq(&tasklist_lock);
  83. }
  84. /* Take this CPU down. */
  85. static int take_cpu_down(void *unused)
  86. {
  87. int err;
  88. /* Ensure this CPU doesn't handle any more interrupts. */
  89. err = __cpu_disable();
  90. if (err < 0)
  91. return err;
  92. /* Force idle task to run as soon as we yield: it should
  93. immediately notice cpu is offline and die quickly. */
  94. sched_idle_next();
  95. return 0;
  96. }
  97. /* Requires cpu_add_remove_lock to be held */
  98. static int _cpu_down(unsigned int cpu)
  99. {
  100. int err;
  101. struct task_struct *p;
  102. cpumask_t old_allowed, tmp;
  103. if (num_online_cpus() == 1)
  104. return -EBUSY;
  105. if (!cpu_online(cpu))
  106. return -EINVAL;
  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. return -EINVAL;
  113. }
  114. /* Ensure that we are not runnable on dying cpu */
  115. old_allowed = current->cpus_allowed;
  116. tmp = CPU_MASK_ALL;
  117. cpu_clear(cpu, tmp);
  118. set_cpus_allowed(current, tmp);
  119. mutex_lock(&cpu_bitmask_lock);
  120. p = __stop_machine_run(take_cpu_down, NULL, cpu);
  121. mutex_unlock(&cpu_bitmask_lock);
  122. if (IS_ERR(p)) {
  123. /* CPU didn't die: tell everyone. Can't complain. */
  124. if (blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
  125. (void *)(long)cpu) == NOTIFY_BAD)
  126. BUG();
  127. err = PTR_ERR(p);
  128. goto out_allowed;
  129. }
  130. if (cpu_online(cpu))
  131. goto out_thread;
  132. /* Wait for it to sleep (leaving idle task). */
  133. while (!idle_cpu(cpu))
  134. yield();
  135. /* This actually kills the CPU. */
  136. __cpu_die(cpu);
  137. /* Move it here so it can run. */
  138. kthread_bind(p, get_cpu());
  139. put_cpu();
  140. /* CPU is completely dead: tell everyone. Too late to complain. */
  141. if (blocking_notifier_call_chain(&cpu_chain, CPU_DEAD,
  142. (void *)(long)cpu) == NOTIFY_BAD)
  143. BUG();
  144. check_for_tasks(cpu);
  145. out_thread:
  146. err = kthread_stop(p);
  147. out_allowed:
  148. set_cpus_allowed(current, old_allowed);
  149. return err;
  150. }
  151. int cpu_down(unsigned int cpu)
  152. {
  153. int err = 0;
  154. mutex_lock(&cpu_add_remove_lock);
  155. if (cpu_hotplug_disabled)
  156. err = -EBUSY;
  157. else
  158. err = _cpu_down(cpu);
  159. mutex_unlock(&cpu_add_remove_lock);
  160. return err;
  161. }
  162. #endif /*CONFIG_HOTPLUG_CPU*/
  163. /* Requires cpu_add_remove_lock to be held */
  164. static int __devinit _cpu_up(unsigned int cpu)
  165. {
  166. int ret;
  167. void *hcpu = (void *)(long)cpu;
  168. if (cpu_online(cpu) || !cpu_present(cpu))
  169. return -EINVAL;
  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. mutex_lock(&cpu_bitmask_lock);
  179. ret = __cpu_up(cpu);
  180. mutex_unlock(&cpu_bitmask_lock);
  181. if (ret != 0)
  182. goto out_notify;
  183. BUG_ON(!cpu_online(cpu));
  184. /* Now call notifier in preparation. */
  185. blocking_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
  186. out_notify:
  187. if (ret != 0)
  188. blocking_notifier_call_chain(&cpu_chain,
  189. CPU_UP_CANCELED, hcpu);
  190. return ret;
  191. }
  192. int __devinit cpu_up(unsigned int cpu)
  193. {
  194. int err = 0;
  195. mutex_lock(&cpu_add_remove_lock);
  196. if (cpu_hotplug_disabled)
  197. err = -EBUSY;
  198. else
  199. err = _cpu_up(cpu);
  200. mutex_unlock(&cpu_add_remove_lock);
  201. return err;
  202. }
  203. #ifdef CONFIG_SUSPEND_SMP
  204. static cpumask_t frozen_cpus;
  205. int disable_nonboot_cpus(void)
  206. {
  207. int cpu, first_cpu, error;
  208. mutex_lock(&cpu_add_remove_lock);
  209. first_cpu = first_cpu(cpu_present_map);
  210. if (!cpu_online(first_cpu)) {
  211. error = _cpu_up(first_cpu);
  212. if (error) {
  213. printk(KERN_ERR "Could not bring CPU%d up.\n",
  214. first_cpu);
  215. goto out;
  216. }
  217. }
  218. error = set_cpus_allowed(current, cpumask_of_cpu(first_cpu));
  219. if (error) {
  220. printk(KERN_ERR "Could not run on CPU%d\n", first_cpu);
  221. goto out;
  222. }
  223. /* We take down all of the non-boot CPUs in one shot to avoid races
  224. * with the userspace trying to use the CPU hotplug at the same time
  225. */
  226. cpus_clear(frozen_cpus);
  227. printk("Disabling non-boot CPUs ...\n");
  228. for_each_online_cpu(cpu) {
  229. if (cpu == first_cpu)
  230. continue;
  231. error = _cpu_down(cpu);
  232. if (!error) {
  233. cpu_set(cpu, frozen_cpus);
  234. printk("CPU%d is down\n", cpu);
  235. } else {
  236. printk(KERN_ERR "Error taking CPU%d down: %d\n",
  237. cpu, error);
  238. break;
  239. }
  240. }
  241. if (!error) {
  242. BUG_ON(num_online_cpus() > 1);
  243. /* Make sure the CPUs won't be enabled by someone else */
  244. cpu_hotplug_disabled = 1;
  245. } else {
  246. printk(KERN_ERR "Non-boot CPUs are not disabled");
  247. }
  248. out:
  249. mutex_unlock(&cpu_add_remove_lock);
  250. return error;
  251. }
  252. void enable_nonboot_cpus(void)
  253. {
  254. int cpu, error;
  255. /* Allow everyone to use the CPU hotplug again */
  256. mutex_lock(&cpu_add_remove_lock);
  257. cpu_hotplug_disabled = 0;
  258. mutex_unlock(&cpu_add_remove_lock);
  259. printk("Enabling non-boot CPUs ...\n");
  260. for_each_cpu_mask(cpu, frozen_cpus) {
  261. error = cpu_up(cpu);
  262. if (!error) {
  263. printk("CPU%d is up\n", cpu);
  264. continue;
  265. }
  266. printk(KERN_WARNING "Error taking CPU%d up: %d\n",
  267. cpu, error);
  268. }
  269. cpus_clear(frozen_cpus);
  270. }
  271. #endif