cpu.c 9.7 KB

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