cpu.c 14 KB

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