cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. /*
  26. * Represents all cpu's that are currently online.
  27. */
  28. cpumask_t cpu_online_map __read_mostly;
  29. EXPORT_SYMBOL(cpu_online_map);
  30. #ifdef CONFIG_INIT_ALL_POSSIBLE
  31. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
  32. #else
  33. cpumask_t cpu_possible_map __read_mostly;
  34. #endif
  35. EXPORT_SYMBOL(cpu_possible_map);
  36. #ifdef CONFIG_SMP
  37. /* Serializes the updates to cpu_online_map, cpu_present_map */
  38. static DEFINE_MUTEX(cpu_add_remove_lock);
  39. static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
  40. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  41. * Should always be manipulated under cpu_add_remove_lock
  42. */
  43. static int cpu_hotplug_disabled;
  44. static struct {
  45. struct task_struct *active_writer;
  46. struct mutex lock; /* Synchronizes accesses to refcount, */
  47. /*
  48. * Also blocks the new readers during
  49. * an ongoing cpu hotplug operation.
  50. */
  51. int refcount;
  52. } cpu_hotplug;
  53. void __init cpu_hotplug_init(void)
  54. {
  55. cpu_hotplug.active_writer = NULL;
  56. mutex_init(&cpu_hotplug.lock);
  57. cpu_hotplug.refcount = 0;
  58. }
  59. cpumask_t cpu_active_map;
  60. #ifdef CONFIG_HOTPLUG_CPU
  61. void get_online_cpus(void)
  62. {
  63. might_sleep();
  64. if (cpu_hotplug.active_writer == current)
  65. return;
  66. mutex_lock(&cpu_hotplug.lock);
  67. cpu_hotplug.refcount++;
  68. mutex_unlock(&cpu_hotplug.lock);
  69. }
  70. EXPORT_SYMBOL_GPL(get_online_cpus);
  71. void put_online_cpus(void)
  72. {
  73. if (cpu_hotplug.active_writer == current)
  74. return;
  75. mutex_lock(&cpu_hotplug.lock);
  76. if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
  77. wake_up_process(cpu_hotplug.active_writer);
  78. mutex_unlock(&cpu_hotplug.lock);
  79. }
  80. EXPORT_SYMBOL_GPL(put_online_cpus);
  81. #endif /* CONFIG_HOTPLUG_CPU */
  82. /*
  83. * The following two API's must be used when attempting
  84. * to serialize the updates to cpu_online_map, cpu_present_map.
  85. */
  86. void cpu_maps_update_begin(void)
  87. {
  88. mutex_lock(&cpu_add_remove_lock);
  89. }
  90. void cpu_maps_update_done(void)
  91. {
  92. mutex_unlock(&cpu_add_remove_lock);
  93. }
  94. /*
  95. * This ensures that the hotplug operation can begin only when the
  96. * refcount goes to zero.
  97. *
  98. * Note that during a cpu-hotplug operation, the new readers, if any,
  99. * will be blocked by the cpu_hotplug.lock
  100. *
  101. * Since cpu_hotplug_begin() is always called after invoking
  102. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  103. *
  104. * Note that theoretically, there is a possibility of a livelock:
  105. * - Refcount goes to zero, last reader wakes up the sleeping
  106. * writer.
  107. * - Last reader unlocks the cpu_hotplug.lock.
  108. * - A new reader arrives at this moment, bumps up the refcount.
  109. * - The writer acquires the cpu_hotplug.lock finds the refcount
  110. * non zero and goes to sleep again.
  111. *
  112. * However, this is very difficult to achieve in practice since
  113. * get_online_cpus() not an api which is called all that often.
  114. *
  115. */
  116. static void cpu_hotplug_begin(void)
  117. {
  118. cpu_hotplug.active_writer = current;
  119. for (;;) {
  120. mutex_lock(&cpu_hotplug.lock);
  121. if (likely(!cpu_hotplug.refcount))
  122. break;
  123. __set_current_state(TASK_UNINTERRUPTIBLE);
  124. mutex_unlock(&cpu_hotplug.lock);
  125. schedule();
  126. }
  127. }
  128. static void cpu_hotplug_done(void)
  129. {
  130. cpu_hotplug.active_writer = NULL;
  131. mutex_unlock(&cpu_hotplug.lock);
  132. }
  133. /* Need to know about CPUs going up/down? */
  134. int __ref register_cpu_notifier(struct notifier_block *nb)
  135. {
  136. int ret;
  137. cpu_maps_update_begin();
  138. ret = raw_notifier_chain_register(&cpu_chain, nb);
  139. cpu_maps_update_done();
  140. return ret;
  141. }
  142. #ifdef CONFIG_HOTPLUG_CPU
  143. EXPORT_SYMBOL(register_cpu_notifier);
  144. void __ref unregister_cpu_notifier(struct notifier_block *nb)
  145. {
  146. cpu_maps_update_begin();
  147. raw_notifier_chain_unregister(&cpu_chain, nb);
  148. cpu_maps_update_done();
  149. }
  150. EXPORT_SYMBOL(unregister_cpu_notifier);
  151. static inline void check_for_tasks(int cpu)
  152. {
  153. struct task_struct *p;
  154. write_lock_irq(&tasklist_lock);
  155. for_each_process(p) {
  156. if (task_cpu(p) == cpu &&
  157. (!cputime_eq(p->utime, cputime_zero) ||
  158. !cputime_eq(p->stime, cputime_zero)))
  159. printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
  160. (state = %ld, flags = %x) \n",
  161. p->comm, task_pid_nr(p), cpu,
  162. p->state, p->flags);
  163. }
  164. write_unlock_irq(&tasklist_lock);
  165. }
  166. struct take_cpu_down_param {
  167. unsigned long mod;
  168. void *hcpu;
  169. };
  170. /* Take this CPU down. */
  171. static int __ref take_cpu_down(void *_param)
  172. {
  173. struct take_cpu_down_param *param = _param;
  174. int err;
  175. /* Ensure this CPU doesn't handle any more interrupts. */
  176. err = __cpu_disable();
  177. if (err < 0)
  178. return err;
  179. raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
  180. param->hcpu);
  181. /* Force idle task to run as soon as we yield: it should
  182. immediately notice cpu is offline and die quickly. */
  183. sched_idle_next();
  184. return 0;
  185. }
  186. /* Requires cpu_add_remove_lock to be held */
  187. static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
  188. {
  189. int err, nr_calls = 0;
  190. cpumask_t old_allowed, tmp;
  191. void *hcpu = (void *)(long)cpu;
  192. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  193. struct take_cpu_down_param tcd_param = {
  194. .mod = mod,
  195. .hcpu = hcpu,
  196. };
  197. if (num_online_cpus() == 1)
  198. return -EBUSY;
  199. if (!cpu_online(cpu))
  200. return -EINVAL;
  201. cpu_hotplug_begin();
  202. err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
  203. hcpu, -1, &nr_calls);
  204. if (err == NOTIFY_BAD) {
  205. nr_calls--;
  206. __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  207. hcpu, nr_calls, NULL);
  208. printk("%s: attempt to take down CPU %u failed\n",
  209. __func__, cpu);
  210. err = -EINVAL;
  211. goto out_release;
  212. }
  213. /* Ensure that we are not runnable on dying cpu */
  214. old_allowed = current->cpus_allowed;
  215. cpus_setall(tmp);
  216. cpu_clear(cpu, tmp);
  217. set_cpus_allowed_ptr(current, &tmp);
  218. tmp = cpumask_of_cpu(cpu);
  219. err = __stop_machine(take_cpu_down, &tcd_param, &tmp);
  220. if (err) {
  221. /* CPU didn't die: tell everyone. Can't complain. */
  222. if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
  223. hcpu) == NOTIFY_BAD)
  224. BUG();
  225. goto out_allowed;
  226. }
  227. BUG_ON(cpu_online(cpu));
  228. /* Wait for it to sleep (leaving idle task). */
  229. while (!idle_cpu(cpu))
  230. yield();
  231. /* This actually kills the CPU. */
  232. __cpu_die(cpu);
  233. /* CPU is completely dead: tell everyone. Too late to complain. */
  234. if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
  235. hcpu) == NOTIFY_BAD)
  236. BUG();
  237. check_for_tasks(cpu);
  238. out_allowed:
  239. set_cpus_allowed_ptr(current, &old_allowed);
  240. out_release:
  241. cpu_hotplug_done();
  242. if (!err) {
  243. if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
  244. hcpu) == NOTIFY_BAD)
  245. BUG();
  246. }
  247. return err;
  248. }
  249. int __ref cpu_down(unsigned int cpu)
  250. {
  251. int err = 0;
  252. cpu_maps_update_begin();
  253. if (cpu_hotplug_disabled) {
  254. err = -EBUSY;
  255. goto out;
  256. }
  257. cpu_clear(cpu, cpu_active_map);
  258. /*
  259. * Make sure the all cpus did the reschedule and are not
  260. * using stale version of the cpu_active_map.
  261. * This is not strictly necessary becuase stop_machine()
  262. * that we run down the line already provides the required
  263. * synchronization. But it's really a side effect and we do not
  264. * want to depend on the innards of the stop_machine here.
  265. */
  266. synchronize_sched();
  267. err = _cpu_down(cpu, 0);
  268. if (cpu_online(cpu))
  269. cpu_set(cpu, cpu_active_map);
  270. out:
  271. cpu_maps_update_done();
  272. return err;
  273. }
  274. EXPORT_SYMBOL(cpu_down);
  275. #endif /*CONFIG_HOTPLUG_CPU*/
  276. /* Requires cpu_add_remove_lock to be held */
  277. static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
  278. {
  279. int ret, nr_calls = 0;
  280. void *hcpu = (void *)(long)cpu;
  281. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  282. if (cpu_online(cpu) || !cpu_present(cpu))
  283. return -EINVAL;
  284. cpu_hotplug_begin();
  285. ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
  286. -1, &nr_calls);
  287. if (ret == NOTIFY_BAD) {
  288. nr_calls--;
  289. printk("%s: attempt to bring up CPU %u failed\n",
  290. __func__, cpu);
  291. ret = -EINVAL;
  292. goto out_notify;
  293. }
  294. /* Arch-specific enabling code. */
  295. ret = __cpu_up(cpu);
  296. if (ret != 0)
  297. goto out_notify;
  298. BUG_ON(!cpu_online(cpu));
  299. cpu_set(cpu, cpu_active_map);
  300. /* Now call notifier in preparation. */
  301. raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
  302. out_notify:
  303. if (ret != 0)
  304. __raw_notifier_call_chain(&cpu_chain,
  305. CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
  306. cpu_hotplug_done();
  307. return ret;
  308. }
  309. int __cpuinit cpu_up(unsigned int cpu)
  310. {
  311. int err = 0;
  312. if (!cpu_isset(cpu, cpu_possible_map)) {
  313. printk(KERN_ERR "can't online cpu %d because it is not "
  314. "configured as may-hotadd at boot time\n", cpu);
  315. #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
  316. printk(KERN_ERR "please check additional_cpus= boot "
  317. "parameter\n");
  318. #endif
  319. return -EINVAL;
  320. }
  321. cpu_maps_update_begin();
  322. if (cpu_hotplug_disabled) {
  323. err = -EBUSY;
  324. goto out;
  325. }
  326. err = _cpu_up(cpu, 0);
  327. out:
  328. cpu_maps_update_done();
  329. return err;
  330. }
  331. #ifdef CONFIG_PM_SLEEP_SMP
  332. static cpumask_t frozen_cpus;
  333. int disable_nonboot_cpus(void)
  334. {
  335. int cpu, first_cpu, error = 0;
  336. cpu_maps_update_begin();
  337. first_cpu = first_cpu(cpu_online_map);
  338. /* We take down all of the non-boot CPUs in one shot to avoid races
  339. * with the userspace trying to use the CPU hotplug at the same time
  340. */
  341. cpus_clear(frozen_cpus);
  342. printk("Disabling non-boot CPUs ...\n");
  343. for_each_online_cpu(cpu) {
  344. if (cpu == first_cpu)
  345. continue;
  346. error = _cpu_down(cpu, 1);
  347. if (!error) {
  348. cpu_set(cpu, frozen_cpus);
  349. printk("CPU%d is down\n", cpu);
  350. } else {
  351. printk(KERN_ERR "Error taking CPU%d down: %d\n",
  352. cpu, error);
  353. break;
  354. }
  355. }
  356. if (!error) {
  357. BUG_ON(num_online_cpus() > 1);
  358. /* Make sure the CPUs won't be enabled by someone else */
  359. cpu_hotplug_disabled = 1;
  360. } else {
  361. printk(KERN_ERR "Non-boot CPUs are not disabled\n");
  362. }
  363. cpu_maps_update_done();
  364. return error;
  365. }
  366. void __ref enable_nonboot_cpus(void)
  367. {
  368. int cpu, error;
  369. /* Allow everyone to use the CPU hotplug again */
  370. cpu_maps_update_begin();
  371. cpu_hotplug_disabled = 0;
  372. if (cpus_empty(frozen_cpus))
  373. goto out;
  374. printk("Enabling non-boot CPUs ...\n");
  375. for_each_cpu_mask_nr(cpu, frozen_cpus) {
  376. error = _cpu_up(cpu, 1);
  377. if (!error) {
  378. printk("CPU%d is up\n", cpu);
  379. continue;
  380. }
  381. printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
  382. }
  383. cpus_clear(frozen_cpus);
  384. out:
  385. cpu_maps_update_done();
  386. }
  387. #endif /* CONFIG_PM_SLEEP_SMP */
  388. /**
  389. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  390. * @cpu: cpu that just started
  391. *
  392. * This function calls the cpu_chain notifiers with CPU_STARTING.
  393. * It must be called by the arch code on the new cpu, before the new cpu
  394. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  395. */
  396. void __cpuinit notify_cpu_starting(unsigned int cpu)
  397. {
  398. unsigned long val = CPU_STARTING;
  399. #ifdef CONFIG_PM_SLEEP_SMP
  400. if (cpu_isset(cpu, frozen_cpus))
  401. val = CPU_STARTING_FROZEN;
  402. #endif /* CONFIG_PM_SLEEP_SMP */
  403. raw_notifier_call_chain(&cpu_chain, val, (void *)(long)cpu);
  404. }
  405. #endif /* CONFIG_SMP */
  406. /*
  407. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  408. * represents all NR_CPUS bits binary values of 1<<nr.
  409. *
  410. * It is used by cpumask_of_cpu() to get a constant address to a CPU
  411. * mask value that has a single bit set only.
  412. */
  413. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  414. #define MASK_DECLARE_1(x) [x+1][0] = 1UL << (x)
  415. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  416. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  417. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  418. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  419. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  420. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  421. #if BITS_PER_LONG > 32
  422. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  423. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  424. #endif
  425. };
  426. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  427. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  428. EXPORT_SYMBOL(cpu_all_bits);