cpu.c 13 KB

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