cpu.c 14 KB

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