cpu.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 RAW_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. recursive = NULL;
  54. mutex_unlock(&cpu_bitmask_lock);
  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. int ret;
  62. mutex_lock(&cpu_add_remove_lock);
  63. ret = raw_notifier_chain_register(&cpu_chain, nb);
  64. mutex_unlock(&cpu_add_remove_lock);
  65. return ret;
  66. }
  67. #ifdef CONFIG_HOTPLUG_CPU
  68. EXPORT_SYMBOL(register_cpu_notifier);
  69. void unregister_cpu_notifier(struct notifier_block *nb)
  70. {
  71. mutex_lock(&cpu_add_remove_lock);
  72. raw_notifier_chain_unregister(&cpu_chain, nb);
  73. mutex_unlock(&cpu_add_remove_lock);
  74. }
  75. EXPORT_SYMBOL(unregister_cpu_notifier);
  76. static inline void check_for_tasks(int cpu)
  77. {
  78. struct task_struct *p;
  79. write_lock_irq(&tasklist_lock);
  80. for_each_process(p) {
  81. if (task_cpu(p) == cpu &&
  82. (!cputime_eq(p->utime, cputime_zero) ||
  83. !cputime_eq(p->stime, cputime_zero)))
  84. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  85. (state = %ld, flags = %x) \n",
  86. p->comm, task_pid_nr(p), cpu,
  87. p->state, p->flags);
  88. }
  89. write_unlock_irq(&tasklist_lock);
  90. }
  91. struct take_cpu_down_param {
  92. unsigned long mod;
  93. void *hcpu;
  94. };
  95. /* Take this CPU down. */
  96. static int take_cpu_down(void *_param)
  97. {
  98. struct take_cpu_down_param *param = _param;
  99. int err;
  100. raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
  101. param->hcpu);
  102. /* Ensure this CPU doesn't handle any more interrupts. */
  103. err = __cpu_disable();
  104. if (err < 0)
  105. return err;
  106. /* Force idle task to run as soon as we yield: it should
  107. immediately notice cpu is offline and die quickly. */
  108. sched_idle_next();
  109. return 0;
  110. }
  111. /* Requires cpu_add_remove_lock to be held */
  112. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  113. {
  114. int err, nr_calls = 0;
  115. struct task_struct *p;
  116. cpumask_t old_allowed, tmp;
  117. void *hcpu = (void *)(long)cpu;
  118. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  119. struct take_cpu_down_param tcd_param = {
  120. .mod = mod,
  121. .hcpu = hcpu,
  122. };
  123. if (num_online_cpus() == 1)
  124. return -EBUSY;
  125. if (!cpu_online(cpu))
  126. return -EINVAL;
  127. raw_notifier_call_chain(&cpu_chain, CPU_LOCK_ACQUIRE, hcpu);
  128. err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
  129. hcpu, -1, &nr_calls);
  130. if (err == NOTIFY_BAD) {
  131. nr_calls--;
  132. __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  133. hcpu, nr_calls, NULL);
  134. printk("%s: attempt to take down CPU %u failed\n",
  135. __FUNCTION__, cpu);
  136. err = -EINVAL;
  137. goto out_release;
  138. }
  139. /* Ensure that we are not runnable on dying cpu */
  140. old_allowed = current->cpus_allowed;
  141. tmp = CPU_MASK_ALL;
  142. cpu_clear(cpu, tmp);
  143. set_cpus_allowed(current, tmp);
  144. mutex_lock(&cpu_bitmask_lock);
  145. p = __stop_machine_run(take_cpu_down, &tcd_param, cpu);
  146. mutex_unlock(&cpu_bitmask_lock);
  147. if (IS_ERR(p) || cpu_online(cpu)) {
  148. /* CPU didn't die: tell everyone. Can't complain. */
  149. if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  150. hcpu) == NOTIFY_BAD)
  151. BUG();
  152. if (IS_ERR(p)) {
  153. err = PTR_ERR(p);
  154. goto out_allowed;
  155. }
  156. goto out_thread;
  157. }
  158. /* Wait for it to sleep (leaving idle task). */
  159. while (!idle_cpu(cpu))
  160. yield();
  161. /* This actually kills the CPU. */
  162. __cpu_die(cpu);
  163. /* CPU is completely dead: tell everyone. Too late to complain. */
  164. if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
  165. hcpu) == NOTIFY_BAD)
  166. BUG();
  167. check_for_tasks(cpu);
  168. out_thread:
  169. err = kthread_stop(p);
  170. out_allowed:
  171. set_cpus_allowed(current, old_allowed);
  172. out_release:
  173. raw_notifier_call_chain(&cpu_chain, CPU_LOCK_RELEASE, hcpu);
  174. return err;
  175. }
  176. int cpu_down(unsigned int cpu)
  177. {
  178. int err = 0;
  179. mutex_lock(&cpu_add_remove_lock);
  180. if (cpu_hotplug_disabled)
  181. err = -EBUSY;
  182. else
  183. err = _cpu_down(cpu, 0);
  184. mutex_unlock(&cpu_add_remove_lock);
  185. return err;
  186. }
  187. #endif /*CONFIG_HOTPLUG_CPU*/
  188. /* Requires cpu_add_remove_lock to be held */
  189. static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
  190. {
  191. int ret, nr_calls = 0;
  192. void *hcpu = (void *)(long)cpu;
  193. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  194. if (cpu_online(cpu) || !cpu_present(cpu))
  195. return -EINVAL;
  196. raw_notifier_call_chain(&cpu_chain, CPU_LOCK_ACQUIRE, hcpu);
  197. ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
  198. -1, &nr_calls);
  199. if (ret == NOTIFY_BAD) {
  200. nr_calls--;
  201. printk("%s: attempt to bring up CPU %u failed\n",
  202. __FUNCTION__, cpu);
  203. ret = -EINVAL;
  204. goto out_notify;
  205. }
  206. /* Arch-specific enabling code. */
  207. mutex_lock(&cpu_bitmask_lock);
  208. ret = __cpu_up(cpu);
  209. mutex_unlock(&cpu_bitmask_lock);
  210. if (ret != 0)
  211. goto out_notify;
  212. BUG_ON(!cpu_online(cpu));
  213. /* Now call notifier in preparation. */
  214. raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
  215. out_notify:
  216. if (ret != 0)
  217. __raw_notifier_call_chain(&cpu_chain,
  218. CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
  219. raw_notifier_call_chain(&cpu_chain, CPU_LOCK_RELEASE, hcpu);
  220. return ret;
  221. }
  222. int __cpuinit cpu_up(unsigned int cpu)
  223. {
  224. int err = 0;
  225. if (!cpu_isset(cpu, cpu_possible_map)) {
  226. printk(KERN_ERR "can't online cpu %d because it is not "
  227. "configured as may-hotadd at boot time\n", cpu);
  228. #if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390)
  229. printk(KERN_ERR "please check additional_cpus= boot "
  230. "parameter\n");
  231. #endif
  232. return -EINVAL;
  233. }
  234. mutex_lock(&cpu_add_remove_lock);
  235. if (cpu_hotplug_disabled)
  236. err = -EBUSY;
  237. else
  238. err = _cpu_up(cpu, 0);
  239. mutex_unlock(&cpu_add_remove_lock);
  240. return err;
  241. }
  242. #ifdef CONFIG_PM_SLEEP_SMP
  243. static cpumask_t frozen_cpus;
  244. int disable_nonboot_cpus(void)
  245. {
  246. int cpu, first_cpu, error = 0;
  247. mutex_lock(&cpu_add_remove_lock);
  248. first_cpu = first_cpu(cpu_online_map);
  249. /* We take down all of the non-boot CPUs in one shot to avoid races
  250. * with the userspace trying to use the CPU hotplug at the same time
  251. */
  252. cpus_clear(frozen_cpus);
  253. printk("Disabling non-boot CPUs ...\n");
  254. for_each_online_cpu(cpu) {
  255. if (cpu == first_cpu)
  256. continue;
  257. error = _cpu_down(cpu, 1);
  258. if (!error) {
  259. cpu_set(cpu, frozen_cpus);
  260. printk("CPU%d is down\n", cpu);
  261. } else {
  262. printk(KERN_ERR "Error taking CPU%d down: %d\n",
  263. cpu, error);
  264. break;
  265. }
  266. }
  267. if (!error) {
  268. BUG_ON(num_online_cpus() > 1);
  269. /* Make sure the CPUs won't be enabled by someone else */
  270. cpu_hotplug_disabled = 1;
  271. } else {
  272. printk(KERN_ERR "Non-boot CPUs are not disabled\n");
  273. }
  274. mutex_unlock(&cpu_add_remove_lock);
  275. return error;
  276. }
  277. void enable_nonboot_cpus(void)
  278. {
  279. int cpu, error;
  280. /* Allow everyone to use the CPU hotplug again */
  281. mutex_lock(&cpu_add_remove_lock);
  282. cpu_hotplug_disabled = 0;
  283. if (cpus_empty(frozen_cpus))
  284. goto out;
  285. printk("Enabling non-boot CPUs ...\n");
  286. for_each_cpu_mask(cpu, frozen_cpus) {
  287. error = _cpu_up(cpu, 1);
  288. if (!error) {
  289. printk("CPU%d is up\n", cpu);
  290. continue;
  291. }
  292. printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
  293. }
  294. cpus_clear(frozen_cpus);
  295. out:
  296. mutex_unlock(&cpu_add_remove_lock);
  297. }
  298. #endif /* CONFIG_PM_SLEEP_SMP */