cpu.c 10.0 KB

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