cpu.c 12 KB

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