cpu.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /* Serializes the updates to cpu_online_map, cpu_present_map */
  18. static DEFINE_MUTEX(cpu_add_remove_lock);
  19. static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
  20. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  21. * Should always be manipulated under cpu_add_remove_lock
  22. */
  23. static int cpu_hotplug_disabled;
  24. static struct {
  25. struct task_struct *active_writer;
  26. struct mutex lock; /* Synchronizes accesses to refcount, */
  27. /*
  28. * Also blocks the new readers during
  29. * an ongoing cpu hotplug operation.
  30. */
  31. int refcount;
  32. } cpu_hotplug;
  33. void __init cpu_hotplug_init(void)
  34. {
  35. cpu_hotplug.active_writer = NULL;
  36. mutex_init(&cpu_hotplug.lock);
  37. cpu_hotplug.refcount = 0;
  38. }
  39. #ifdef CONFIG_HOTPLUG_CPU
  40. void get_online_cpus(void)
  41. {
  42. might_sleep();
  43. if (cpu_hotplug.active_writer == current)
  44. return;
  45. mutex_lock(&cpu_hotplug.lock);
  46. cpu_hotplug.refcount++;
  47. mutex_unlock(&cpu_hotplug.lock);
  48. }
  49. EXPORT_SYMBOL_GPL(get_online_cpus);
  50. void put_online_cpus(void)
  51. {
  52. if (cpu_hotplug.active_writer == current)
  53. return;
  54. mutex_lock(&cpu_hotplug.lock);
  55. if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
  56. wake_up_process(cpu_hotplug.active_writer);
  57. mutex_unlock(&cpu_hotplug.lock);
  58. }
  59. EXPORT_SYMBOL_GPL(put_online_cpus);
  60. #endif /* CONFIG_HOTPLUG_CPU */
  61. /*
  62. * The following two API's must be used when attempting
  63. * to serialize the updates to cpu_online_map, cpu_present_map.
  64. */
  65. void cpu_maps_update_begin(void)
  66. {
  67. mutex_lock(&cpu_add_remove_lock);
  68. }
  69. void cpu_maps_update_done(void)
  70. {
  71. mutex_unlock(&cpu_add_remove_lock);
  72. }
  73. /*
  74. * This ensures that the hotplug operation can begin only when the
  75. * refcount goes to zero.
  76. *
  77. * Note that during a cpu-hotplug operation, the new readers, if any,
  78. * will be blocked by the cpu_hotplug.lock
  79. *
  80. * Since cpu_hotplug_begin() is always called after invoking
  81. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  82. *
  83. * Note that theoretically, there is a possibility of a livelock:
  84. * - Refcount goes to zero, last reader wakes up the sleeping
  85. * writer.
  86. * - Last reader unlocks the cpu_hotplug.lock.
  87. * - A new reader arrives at this moment, bumps up the refcount.
  88. * - The writer acquires the cpu_hotplug.lock finds the refcount
  89. * non zero and goes to sleep again.
  90. *
  91. * However, this is very difficult to achieve in practice since
  92. * get_online_cpus() not an api which is called all that often.
  93. *
  94. */
  95. static void cpu_hotplug_begin(void)
  96. {
  97. cpu_hotplug.active_writer = current;
  98. for (;;) {
  99. mutex_lock(&cpu_hotplug.lock);
  100. if (likely(!cpu_hotplug.refcount))
  101. break;
  102. __set_current_state(TASK_UNINTERRUPTIBLE);
  103. mutex_unlock(&cpu_hotplug.lock);
  104. schedule();
  105. }
  106. }
  107. static void cpu_hotplug_done(void)
  108. {
  109. cpu_hotplug.active_writer = NULL;
  110. mutex_unlock(&cpu_hotplug.lock);
  111. }
  112. /* Need to know about CPUs going up/down? */
  113. int __ref register_cpu_notifier(struct notifier_block *nb)
  114. {
  115. int ret;
  116. cpu_maps_update_begin();
  117. ret = raw_notifier_chain_register(&cpu_chain, nb);
  118. cpu_maps_update_done();
  119. return ret;
  120. }
  121. #ifdef CONFIG_HOTPLUG_CPU
  122. EXPORT_SYMBOL(register_cpu_notifier);
  123. void __ref unregister_cpu_notifier(struct notifier_block *nb)
  124. {
  125. cpu_maps_update_begin();
  126. raw_notifier_chain_unregister(&cpu_chain, nb);
  127. cpu_maps_update_done();
  128. }
  129. EXPORT_SYMBOL(unregister_cpu_notifier);
  130. static inline void check_for_tasks(int cpu)
  131. {
  132. struct task_struct *p;
  133. write_lock_irq(&tasklist_lock);
  134. for_each_process(p) {
  135. if (task_cpu(p) == cpu &&
  136. (!cputime_eq(p->utime, cputime_zero) ||
  137. !cputime_eq(p->stime, cputime_zero)))
  138. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  139. (state = %ld, flags = %x) \n",
  140. p->comm, task_pid_nr(p), cpu,
  141. p->state, p->flags);
  142. }
  143. write_unlock_irq(&tasklist_lock);
  144. }
  145. struct take_cpu_down_param {
  146. unsigned long mod;
  147. void *hcpu;
  148. };
  149. /* Take this CPU down. */
  150. static int __ref take_cpu_down(void *_param)
  151. {
  152. struct take_cpu_down_param *param = _param;
  153. int err;
  154. raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
  155. param->hcpu);
  156. /* Ensure this CPU doesn't handle any more interrupts. */
  157. err = __cpu_disable();
  158. if (err < 0)
  159. return err;
  160. /* Force idle task to run as soon as we yield: it should
  161. immediately notice cpu is offline and die quickly. */
  162. sched_idle_next();
  163. return 0;
  164. }
  165. /* Requires cpu_add_remove_lock to be held */
  166. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  167. {
  168. int err, nr_calls = 0;
  169. struct task_struct *p;
  170. cpumask_t old_allowed, tmp;
  171. void *hcpu = (void *)(long)cpu;
  172. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  173. struct take_cpu_down_param tcd_param = {
  174. .mod = mod,
  175. .hcpu = hcpu,
  176. };
  177. if (num_online_cpus() == 1)
  178. return -EBUSY;
  179. if (!cpu_online(cpu))
  180. return -EINVAL;
  181. cpu_hotplug_begin();
  182. err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
  183. hcpu, -1, &nr_calls);
  184. if (err == NOTIFY_BAD) {
  185. nr_calls--;
  186. __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  187. hcpu, nr_calls, NULL);
  188. printk("%s: attempt to take down CPU %u failed\n",
  189. __func__, cpu);
  190. err = -EINVAL;
  191. goto out_release;
  192. }
  193. /* Ensure that we are not runnable on dying cpu */
  194. old_allowed = current->cpus_allowed;
  195. cpus_setall(tmp);
  196. cpu_clear(cpu, tmp);
  197. set_cpus_allowed_ptr(current, &tmp);
  198. p = __stop_machine_run(take_cpu_down, &tcd_param, cpu);
  199. if (IS_ERR(p) || cpu_online(cpu)) {
  200. /* CPU didn't die: tell everyone. Can't complain. */
  201. if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  202. hcpu) == NOTIFY_BAD)
  203. BUG();
  204. if (IS_ERR(p)) {
  205. err = PTR_ERR(p);
  206. goto out_allowed;
  207. }
  208. goto out_thread;
  209. }
  210. /* Wait for it to sleep (leaving idle task). */
  211. while (!idle_cpu(cpu))
  212. yield();
  213. /* This actually kills the CPU. */
  214. __cpu_die(cpu);
  215. /* CPU is completely dead: tell everyone. Too late to complain. */
  216. if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
  217. hcpu) == NOTIFY_BAD)
  218. BUG();
  219. check_for_tasks(cpu);
  220. out_thread:
  221. err = kthread_stop(p);
  222. out_allowed:
  223. set_cpus_allowed_ptr(current, &old_allowed);
  224. out_release:
  225. cpu_hotplug_done();
  226. return err;
  227. }
  228. int __ref cpu_down(unsigned int cpu)
  229. {
  230. int err = 0;
  231. cpu_maps_update_begin();
  232. if (cpu_hotplug_disabled)
  233. err = -EBUSY;
  234. else
  235. err = _cpu_down(cpu, 0);
  236. cpu_maps_update_done();
  237. return err;
  238. }
  239. #endif /*CONFIG_HOTPLUG_CPU*/
  240. /* Requires cpu_add_remove_lock to be held */
  241. static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
  242. {
  243. int ret, nr_calls = 0;
  244. void *hcpu = (void *)(long)cpu;
  245. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  246. if (cpu_online(cpu) || !cpu_present(cpu))
  247. return -EINVAL;
  248. cpu_hotplug_begin();
  249. ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
  250. -1, &nr_calls);
  251. if (ret == NOTIFY_BAD) {
  252. nr_calls--;
  253. printk("%s: attempt to bring up CPU %u failed\n",
  254. __func__, cpu);
  255. ret = -EINVAL;
  256. goto out_notify;
  257. }
  258. /* Arch-specific enabling code. */
  259. ret = __cpu_up(cpu);
  260. if (ret != 0)
  261. goto out_notify;
  262. BUG_ON(!cpu_online(cpu));
  263. /* Now call notifier in preparation. */
  264. raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
  265. out_notify:
  266. if (ret != 0)
  267. __raw_notifier_call_chain(&cpu_chain,
  268. CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
  269. cpu_hotplug_done();
  270. return ret;
  271. }
  272. int __cpuinit cpu_up(unsigned int cpu)
  273. {
  274. int err = 0;
  275. if (!cpu_isset(cpu, cpu_possible_map)) {
  276. printk(KERN_ERR "can't online cpu %d because it is not "
  277. "configured as may-hotadd at boot time\n", cpu);
  278. #if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390)
  279. printk(KERN_ERR "please check additional_cpus= boot "
  280. "parameter\n");
  281. #endif
  282. return -EINVAL;
  283. }
  284. cpu_maps_update_begin();
  285. if (cpu_hotplug_disabled)
  286. err = -EBUSY;
  287. else
  288. err = _cpu_up(cpu, 0);
  289. cpu_maps_update_done();
  290. return err;
  291. }
  292. #ifdef CONFIG_PM_SLEEP_SMP
  293. static cpumask_t frozen_cpus;
  294. int disable_nonboot_cpus(void)
  295. {
  296. int cpu, first_cpu, error = 0;
  297. cpu_maps_update_begin();
  298. first_cpu = first_cpu(cpu_online_map);
  299. /* We take down all of the non-boot CPUs in one shot to avoid races
  300. * with the userspace trying to use the CPU hotplug at the same time
  301. */
  302. cpus_clear(frozen_cpus);
  303. printk("Disabling non-boot CPUs ...\n");
  304. for_each_online_cpu(cpu) {
  305. if (cpu == first_cpu)
  306. continue;
  307. error = _cpu_down(cpu, 1);
  308. if (!error) {
  309. cpu_set(cpu, frozen_cpus);
  310. printk("CPU%d is down\n", cpu);
  311. } else {
  312. printk(KERN_ERR "Error taking CPU%d down: %d\n",
  313. cpu, error);
  314. break;
  315. }
  316. }
  317. if (!error) {
  318. BUG_ON(num_online_cpus() > 1);
  319. /* Make sure the CPUs won't be enabled by someone else */
  320. cpu_hotplug_disabled = 1;
  321. } else {
  322. printk(KERN_ERR "Non-boot CPUs are not disabled\n");
  323. }
  324. cpu_maps_update_done();
  325. return error;
  326. }
  327. void __ref enable_nonboot_cpus(void)
  328. {
  329. int cpu, error;
  330. /* Allow everyone to use the CPU hotplug again */
  331. cpu_maps_update_begin();
  332. cpu_hotplug_disabled = 0;
  333. if (cpus_empty(frozen_cpus))
  334. goto out;
  335. printk("Enabling non-boot CPUs ...\n");
  336. for_each_cpu_mask(cpu, frozen_cpus) {
  337. error = _cpu_up(cpu, 1);
  338. if (!error) {
  339. printk("CPU%d is up\n", cpu);
  340. continue;
  341. }
  342. printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
  343. }
  344. cpus_clear(frozen_cpus);
  345. out:
  346. cpu_maps_update_done();
  347. }
  348. #endif /* CONFIG_PM_SLEEP_SMP */