cpu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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/oom.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/export.h>
  16. #include <linux/bug.h>
  17. #include <linux/kthread.h>
  18. #include <linux/stop_machine.h>
  19. #include <linux/mutex.h>
  20. #include <linux/gfp.h>
  21. #include <linux/suspend.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/tick.h>
  24. #include <linux/irq.h>
  25. #include <trace/events/power.h>
  26. #include "smpboot.h"
  27. #ifdef CONFIG_SMP
  28. /* Serializes the updates to cpu_online_mask, cpu_present_mask */
  29. static DEFINE_MUTEX(cpu_add_remove_lock);
  30. bool cpuhp_tasks_frozen;
  31. EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
  32. /*
  33. * The following two APIs (cpu_maps_update_begin/done) must be used when
  34. * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
  35. * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
  36. * hotplug callback (un)registration performed using __register_cpu_notifier()
  37. * or __unregister_cpu_notifier().
  38. */
  39. void cpu_maps_update_begin(void)
  40. {
  41. mutex_lock(&cpu_add_remove_lock);
  42. }
  43. EXPORT_SYMBOL(cpu_notifier_register_begin);
  44. void cpu_maps_update_done(void)
  45. {
  46. mutex_unlock(&cpu_add_remove_lock);
  47. }
  48. EXPORT_SYMBOL(cpu_notifier_register_done);
  49. static RAW_NOTIFIER_HEAD(cpu_chain);
  50. /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
  51. * Should always be manipulated under cpu_add_remove_lock
  52. */
  53. static int cpu_hotplug_disabled;
  54. #ifdef CONFIG_HOTPLUG_CPU
  55. static struct {
  56. struct task_struct *active_writer;
  57. /* wait queue to wake up the active_writer */
  58. wait_queue_head_t wq;
  59. /* verifies that no writer will get active while readers are active */
  60. struct mutex lock;
  61. /*
  62. * Also blocks the new readers during
  63. * an ongoing cpu hotplug operation.
  64. */
  65. atomic_t refcount;
  66. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  67. struct lockdep_map dep_map;
  68. #endif
  69. } cpu_hotplug = {
  70. .active_writer = NULL,
  71. .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  72. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  73. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  74. .dep_map = {.name = "cpu_hotplug.lock" },
  75. #endif
  76. };
  77. /* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
  78. #define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
  79. #define cpuhp_lock_acquire_tryread() \
  80. lock_map_acquire_tryread(&cpu_hotplug.dep_map)
  81. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  82. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  83. void get_online_cpus(void)
  84. {
  85. might_sleep();
  86. if (cpu_hotplug.active_writer == current)
  87. return;
  88. cpuhp_lock_acquire_read();
  89. mutex_lock(&cpu_hotplug.lock);
  90. atomic_inc(&cpu_hotplug.refcount);
  91. mutex_unlock(&cpu_hotplug.lock);
  92. }
  93. EXPORT_SYMBOL_GPL(get_online_cpus);
  94. void put_online_cpus(void)
  95. {
  96. int refcount;
  97. if (cpu_hotplug.active_writer == current)
  98. return;
  99. refcount = atomic_dec_return(&cpu_hotplug.refcount);
  100. if (WARN_ON(refcount < 0)) /* try to fix things up */
  101. atomic_inc(&cpu_hotplug.refcount);
  102. if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
  103. wake_up(&cpu_hotplug.wq);
  104. cpuhp_lock_release();
  105. }
  106. EXPORT_SYMBOL_GPL(put_online_cpus);
  107. /*
  108. * This ensures that the hotplug operation can begin only when the
  109. * refcount goes to zero.
  110. *
  111. * Note that during a cpu-hotplug operation, the new readers, if any,
  112. * will be blocked by the cpu_hotplug.lock
  113. *
  114. * Since cpu_hotplug_begin() is always called after invoking
  115. * cpu_maps_update_begin(), we can be sure that only one writer is active.
  116. *
  117. * Note that theoretically, there is a possibility of a livelock:
  118. * - Refcount goes to zero, last reader wakes up the sleeping
  119. * writer.
  120. * - Last reader unlocks the cpu_hotplug.lock.
  121. * - A new reader arrives at this moment, bumps up the refcount.
  122. * - The writer acquires the cpu_hotplug.lock finds the refcount
  123. * non zero and goes to sleep again.
  124. *
  125. * However, this is very difficult to achieve in practice since
  126. * get_online_cpus() not an api which is called all that often.
  127. *
  128. */
  129. void cpu_hotplug_begin(void)
  130. {
  131. DEFINE_WAIT(wait);
  132. cpu_hotplug.active_writer = current;
  133. cpuhp_lock_acquire();
  134. for (;;) {
  135. mutex_lock(&cpu_hotplug.lock);
  136. prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
  137. if (likely(!atomic_read(&cpu_hotplug.refcount)))
  138. break;
  139. mutex_unlock(&cpu_hotplug.lock);
  140. schedule();
  141. }
  142. finish_wait(&cpu_hotplug.wq, &wait);
  143. }
  144. void cpu_hotplug_done(void)
  145. {
  146. cpu_hotplug.active_writer = NULL;
  147. mutex_unlock(&cpu_hotplug.lock);
  148. cpuhp_lock_release();
  149. }
  150. /*
  151. * Wait for currently running CPU hotplug operations to complete (if any) and
  152. * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
  153. * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
  154. * hotplug path before performing hotplug operations. So acquiring that lock
  155. * guarantees mutual exclusion from any currently running hotplug operations.
  156. */
  157. void cpu_hotplug_disable(void)
  158. {
  159. cpu_maps_update_begin();
  160. cpu_hotplug_disabled++;
  161. cpu_maps_update_done();
  162. }
  163. EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
  164. void cpu_hotplug_enable(void)
  165. {
  166. cpu_maps_update_begin();
  167. WARN_ON(--cpu_hotplug_disabled < 0);
  168. cpu_maps_update_done();
  169. }
  170. EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
  171. #endif /* CONFIG_HOTPLUG_CPU */
  172. /* Need to know about CPUs going up/down? */
  173. int register_cpu_notifier(struct notifier_block *nb)
  174. {
  175. int ret;
  176. cpu_maps_update_begin();
  177. ret = raw_notifier_chain_register(&cpu_chain, nb);
  178. cpu_maps_update_done();
  179. return ret;
  180. }
  181. int __register_cpu_notifier(struct notifier_block *nb)
  182. {
  183. return raw_notifier_chain_register(&cpu_chain, nb);
  184. }
  185. static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
  186. int *nr_calls)
  187. {
  188. unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
  189. void *hcpu = (void *)(long)cpu;
  190. int ret;
  191. ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
  192. nr_calls);
  193. return notifier_to_errno(ret);
  194. }
  195. static int cpu_notify(unsigned long val, unsigned int cpu)
  196. {
  197. return __cpu_notify(val, cpu, -1, NULL);
  198. }
  199. /* Notifier wrappers for transitioning to state machine */
  200. static int notify_prepare(unsigned int cpu)
  201. {
  202. int nr_calls = 0;
  203. int ret;
  204. ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
  205. if (ret) {
  206. nr_calls--;
  207. printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
  208. __func__, cpu);
  209. __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
  210. }
  211. return ret;
  212. }
  213. static int notify_online(unsigned int cpu)
  214. {
  215. cpu_notify(CPU_ONLINE, cpu);
  216. return 0;
  217. }
  218. static int bringup_cpu(unsigned int cpu)
  219. {
  220. struct task_struct *idle = idle_thread_get(cpu);
  221. int ret;
  222. /* Arch-specific enabling code. */
  223. ret = __cpu_up(cpu, idle);
  224. if (ret) {
  225. cpu_notify(CPU_UP_CANCELED, cpu);
  226. return ret;
  227. }
  228. BUG_ON(!cpu_online(cpu));
  229. return 0;
  230. }
  231. #ifdef CONFIG_HOTPLUG_CPU
  232. static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
  233. {
  234. BUG_ON(cpu_notify(val, cpu));
  235. }
  236. EXPORT_SYMBOL(register_cpu_notifier);
  237. EXPORT_SYMBOL(__register_cpu_notifier);
  238. void unregister_cpu_notifier(struct notifier_block *nb)
  239. {
  240. cpu_maps_update_begin();
  241. raw_notifier_chain_unregister(&cpu_chain, nb);
  242. cpu_maps_update_done();
  243. }
  244. EXPORT_SYMBOL(unregister_cpu_notifier);
  245. void __unregister_cpu_notifier(struct notifier_block *nb)
  246. {
  247. raw_notifier_chain_unregister(&cpu_chain, nb);
  248. }
  249. EXPORT_SYMBOL(__unregister_cpu_notifier);
  250. /**
  251. * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
  252. * @cpu: a CPU id
  253. *
  254. * This function walks all processes, finds a valid mm struct for each one and
  255. * then clears a corresponding bit in mm's cpumask. While this all sounds
  256. * trivial, there are various non-obvious corner cases, which this function
  257. * tries to solve in a safe manner.
  258. *
  259. * Also note that the function uses a somewhat relaxed locking scheme, so it may
  260. * be called only for an already offlined CPU.
  261. */
  262. void clear_tasks_mm_cpumask(int cpu)
  263. {
  264. struct task_struct *p;
  265. /*
  266. * This function is called after the cpu is taken down and marked
  267. * offline, so its not like new tasks will ever get this cpu set in
  268. * their mm mask. -- Peter Zijlstra
  269. * Thus, we may use rcu_read_lock() here, instead of grabbing
  270. * full-fledged tasklist_lock.
  271. */
  272. WARN_ON(cpu_online(cpu));
  273. rcu_read_lock();
  274. for_each_process(p) {
  275. struct task_struct *t;
  276. /*
  277. * Main thread might exit, but other threads may still have
  278. * a valid mm. Find one.
  279. */
  280. t = find_lock_task_mm(p);
  281. if (!t)
  282. continue;
  283. cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
  284. task_unlock(t);
  285. }
  286. rcu_read_unlock();
  287. }
  288. static inline void check_for_tasks(int dead_cpu)
  289. {
  290. struct task_struct *g, *p;
  291. read_lock(&tasklist_lock);
  292. for_each_process_thread(g, p) {
  293. if (!p->on_rq)
  294. continue;
  295. /*
  296. * We do the check with unlocked task_rq(p)->lock.
  297. * Order the reading to do not warn about a task,
  298. * which was running on this cpu in the past, and
  299. * it's just been woken on another cpu.
  300. */
  301. rmb();
  302. if (task_cpu(p) != dead_cpu)
  303. continue;
  304. pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
  305. p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
  306. }
  307. read_unlock(&tasklist_lock);
  308. }
  309. /* Take this CPU down. */
  310. static int take_cpu_down(void *_param)
  311. {
  312. int err, cpu = smp_processor_id();
  313. /* Ensure this CPU doesn't handle any more interrupts. */
  314. err = __cpu_disable();
  315. if (err < 0)
  316. return err;
  317. cpu_notify(CPU_DYING, cpu);
  318. /* Give up timekeeping duties */
  319. tick_handover_do_timer();
  320. /* Park the stopper thread */
  321. stop_machine_park(cpu);
  322. return 0;
  323. }
  324. /* Requires cpu_add_remove_lock to be held */
  325. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  326. {
  327. int err, nr_calls = 0;
  328. if (num_online_cpus() == 1)
  329. return -EBUSY;
  330. if (!cpu_online(cpu))
  331. return -EINVAL;
  332. cpu_hotplug_begin();
  333. cpuhp_tasks_frozen = tasks_frozen;
  334. err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
  335. if (err) {
  336. nr_calls--;
  337. __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
  338. pr_warn("%s: attempt to take down CPU %u failed\n",
  339. __func__, cpu);
  340. goto out_release;
  341. }
  342. /*
  343. * By now we've cleared cpu_active_mask, wait for all preempt-disabled
  344. * and RCU users of this state to go away such that all new such users
  345. * will observe it.
  346. *
  347. * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
  348. * not imply sync_sched(), so wait for both.
  349. *
  350. * Do sync before park smpboot threads to take care the rcu boost case.
  351. */
  352. if (IS_ENABLED(CONFIG_PREEMPT))
  353. synchronize_rcu_mult(call_rcu, call_rcu_sched);
  354. else
  355. synchronize_rcu();
  356. smpboot_park_threads(cpu);
  357. /*
  358. * Prevent irq alloc/free while the dying cpu reorganizes the
  359. * interrupt affinities.
  360. */
  361. irq_lock_sparse();
  362. /*
  363. * So now all preempt/rcu users must observe !cpu_active().
  364. */
  365. err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
  366. if (err) {
  367. /* CPU didn't die: tell everyone. Can't complain. */
  368. cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
  369. irq_unlock_sparse();
  370. goto out_release;
  371. }
  372. BUG_ON(cpu_online(cpu));
  373. /*
  374. * The migration_call() CPU_DYING callback will have removed all
  375. * runnable tasks from the cpu, there's only the idle task left now
  376. * that the migration thread is done doing the stop_machine thing.
  377. *
  378. * Wait for the stop thread to go away.
  379. */
  380. while (!per_cpu(cpu_dead_idle, cpu))
  381. cpu_relax();
  382. smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */
  383. per_cpu(cpu_dead_idle, cpu) = false;
  384. /* Interrupts are moved away from the dying cpu, reenable alloc/free */
  385. irq_unlock_sparse();
  386. hotplug_cpu__broadcast_tick_pull(cpu);
  387. /* This actually kills the CPU. */
  388. __cpu_die(cpu);
  389. /* CPU is completely dead: tell everyone. Too late to complain. */
  390. tick_cleanup_dead_cpu(cpu);
  391. cpu_notify_nofail(CPU_DEAD, cpu);
  392. check_for_tasks(cpu);
  393. out_release:
  394. cpu_hotplug_done();
  395. if (!err)
  396. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  397. return err;
  398. }
  399. int cpu_down(unsigned int cpu)
  400. {
  401. int err;
  402. cpu_maps_update_begin();
  403. if (cpu_hotplug_disabled) {
  404. err = -EBUSY;
  405. goto out;
  406. }
  407. err = _cpu_down(cpu, 0);
  408. out:
  409. cpu_maps_update_done();
  410. return err;
  411. }
  412. EXPORT_SYMBOL(cpu_down);
  413. #endif /*CONFIG_HOTPLUG_CPU*/
  414. /*
  415. * Unpark per-CPU smpboot kthreads at CPU-online time.
  416. */
  417. static int smpboot_thread_call(struct notifier_block *nfb,
  418. unsigned long action, void *hcpu)
  419. {
  420. int cpu = (long)hcpu;
  421. switch (action & ~CPU_TASKS_FROZEN) {
  422. case CPU_DOWN_FAILED:
  423. case CPU_ONLINE:
  424. smpboot_unpark_threads(cpu);
  425. break;
  426. default:
  427. break;
  428. }
  429. return NOTIFY_OK;
  430. }
  431. static struct notifier_block smpboot_thread_notifier = {
  432. .notifier_call = smpboot_thread_call,
  433. .priority = CPU_PRI_SMPBOOT,
  434. };
  435. void smpboot_thread_init(void)
  436. {
  437. register_cpu_notifier(&smpboot_thread_notifier);
  438. }
  439. /* Requires cpu_add_remove_lock to be held */
  440. static int _cpu_up(unsigned int cpu, int tasks_frozen)
  441. {
  442. struct task_struct *idle;
  443. int ret;
  444. cpu_hotplug_begin();
  445. if (cpu_online(cpu) || !cpu_present(cpu)) {
  446. ret = -EINVAL;
  447. goto out;
  448. }
  449. idle = idle_thread_get(cpu);
  450. if (IS_ERR(idle)) {
  451. ret = PTR_ERR(idle);
  452. goto out;
  453. }
  454. cpuhp_tasks_frozen = tasks_frozen;
  455. ret = smpboot_create_threads(cpu);
  456. if (ret)
  457. goto out;
  458. ret = notify_prepare(cpu);
  459. if (ret)
  460. goto out;
  461. ret = bringup_cpu(cpu);
  462. if (ret)
  463. goto out;
  464. notify_online(cpu);
  465. out:
  466. cpu_hotplug_done();
  467. return ret;
  468. }
  469. int cpu_up(unsigned int cpu)
  470. {
  471. int err = 0;
  472. if (!cpu_possible(cpu)) {
  473. pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
  474. cpu);
  475. #if defined(CONFIG_IA64)
  476. pr_err("please check additional_cpus= boot parameter\n");
  477. #endif
  478. return -EINVAL;
  479. }
  480. err = try_online_node(cpu_to_node(cpu));
  481. if (err)
  482. return err;
  483. cpu_maps_update_begin();
  484. if (cpu_hotplug_disabled) {
  485. err = -EBUSY;
  486. goto out;
  487. }
  488. err = _cpu_up(cpu, 0);
  489. out:
  490. cpu_maps_update_done();
  491. return err;
  492. }
  493. EXPORT_SYMBOL_GPL(cpu_up);
  494. #ifdef CONFIG_PM_SLEEP_SMP
  495. static cpumask_var_t frozen_cpus;
  496. int disable_nonboot_cpus(void)
  497. {
  498. int cpu, first_cpu, error = 0;
  499. cpu_maps_update_begin();
  500. first_cpu = cpumask_first(cpu_online_mask);
  501. /*
  502. * We take down all of the non-boot CPUs in one shot to avoid races
  503. * with the userspace trying to use the CPU hotplug at the same time
  504. */
  505. cpumask_clear(frozen_cpus);
  506. pr_info("Disabling non-boot CPUs ...\n");
  507. for_each_online_cpu(cpu) {
  508. if (cpu == first_cpu)
  509. continue;
  510. trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
  511. error = _cpu_down(cpu, 1);
  512. trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
  513. if (!error)
  514. cpumask_set_cpu(cpu, frozen_cpus);
  515. else {
  516. pr_err("Error taking CPU%d down: %d\n", cpu, error);
  517. break;
  518. }
  519. }
  520. if (!error)
  521. BUG_ON(num_online_cpus() > 1);
  522. else
  523. pr_err("Non-boot CPUs are not disabled\n");
  524. /*
  525. * Make sure the CPUs won't be enabled by someone else. We need to do
  526. * this even in case of failure as all disable_nonboot_cpus() users are
  527. * supposed to do enable_nonboot_cpus() on the failure path.
  528. */
  529. cpu_hotplug_disabled++;
  530. cpu_maps_update_done();
  531. return error;
  532. }
  533. void __weak arch_enable_nonboot_cpus_begin(void)
  534. {
  535. }
  536. void __weak arch_enable_nonboot_cpus_end(void)
  537. {
  538. }
  539. void enable_nonboot_cpus(void)
  540. {
  541. int cpu, error;
  542. /* Allow everyone to use the CPU hotplug again */
  543. cpu_maps_update_begin();
  544. WARN_ON(--cpu_hotplug_disabled < 0);
  545. if (cpumask_empty(frozen_cpus))
  546. goto out;
  547. pr_info("Enabling non-boot CPUs ...\n");
  548. arch_enable_nonboot_cpus_begin();
  549. for_each_cpu(cpu, frozen_cpus) {
  550. trace_suspend_resume(TPS("CPU_ON"), cpu, true);
  551. error = _cpu_up(cpu, 1);
  552. trace_suspend_resume(TPS("CPU_ON"), cpu, false);
  553. if (!error) {
  554. pr_info("CPU%d is up\n", cpu);
  555. continue;
  556. }
  557. pr_warn("Error taking CPU%d up: %d\n", cpu, error);
  558. }
  559. arch_enable_nonboot_cpus_end();
  560. cpumask_clear(frozen_cpus);
  561. out:
  562. cpu_maps_update_done();
  563. }
  564. static int __init alloc_frozen_cpus(void)
  565. {
  566. if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
  567. return -ENOMEM;
  568. return 0;
  569. }
  570. core_initcall(alloc_frozen_cpus);
  571. /*
  572. * When callbacks for CPU hotplug notifications are being executed, we must
  573. * ensure that the state of the system with respect to the tasks being frozen
  574. * or not, as reported by the notification, remains unchanged *throughout the
  575. * duration* of the execution of the callbacks.
  576. * Hence we need to prevent the freezer from racing with regular CPU hotplug.
  577. *
  578. * This synchronization is implemented by mutually excluding regular CPU
  579. * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
  580. * Hibernate notifications.
  581. */
  582. static int
  583. cpu_hotplug_pm_callback(struct notifier_block *nb,
  584. unsigned long action, void *ptr)
  585. {
  586. switch (action) {
  587. case PM_SUSPEND_PREPARE:
  588. case PM_HIBERNATION_PREPARE:
  589. cpu_hotplug_disable();
  590. break;
  591. case PM_POST_SUSPEND:
  592. case PM_POST_HIBERNATION:
  593. cpu_hotplug_enable();
  594. break;
  595. default:
  596. return NOTIFY_DONE;
  597. }
  598. return NOTIFY_OK;
  599. }
  600. static int __init cpu_hotplug_pm_sync_init(void)
  601. {
  602. /*
  603. * cpu_hotplug_pm_callback has higher priority than x86
  604. * bsp_pm_callback which depends on cpu_hotplug_pm_callback
  605. * to disable cpu hotplug to avoid cpu hotplug race.
  606. */
  607. pm_notifier(cpu_hotplug_pm_callback, 0);
  608. return 0;
  609. }
  610. core_initcall(cpu_hotplug_pm_sync_init);
  611. #endif /* CONFIG_PM_SLEEP_SMP */
  612. /**
  613. * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
  614. * @cpu: cpu that just started
  615. *
  616. * This function calls the cpu_chain notifiers with CPU_STARTING.
  617. * It must be called by the arch code on the new cpu, before the new cpu
  618. * enables interrupts and before the "boot" cpu returns from __cpu_up().
  619. */
  620. void notify_cpu_starting(unsigned int cpu)
  621. {
  622. cpu_notify(CPU_STARTING, cpu);
  623. }
  624. #endif /* CONFIG_SMP */
  625. /*
  626. * cpu_bit_bitmap[] is a special, "compressed" data structure that
  627. * represents all NR_CPUS bits binary values of 1<<nr.
  628. *
  629. * It is used by cpumask_of() to get a constant address to a CPU
  630. * mask value that has a single bit set only.
  631. */
  632. /* cpu_bit_bitmap[0] is empty - so we can back into it */
  633. #define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
  634. #define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
  635. #define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
  636. #define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
  637. const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
  638. MASK_DECLARE_8(0), MASK_DECLARE_8(8),
  639. MASK_DECLARE_8(16), MASK_DECLARE_8(24),
  640. #if BITS_PER_LONG > 32
  641. MASK_DECLARE_8(32), MASK_DECLARE_8(40),
  642. MASK_DECLARE_8(48), MASK_DECLARE_8(56),
  643. #endif
  644. };
  645. EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
  646. const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
  647. EXPORT_SYMBOL(cpu_all_bits);
  648. #ifdef CONFIG_INIT_ALL_POSSIBLE
  649. struct cpumask __cpu_possible_mask __read_mostly
  650. = {CPU_BITS_ALL};
  651. #else
  652. struct cpumask __cpu_possible_mask __read_mostly;
  653. #endif
  654. EXPORT_SYMBOL(__cpu_possible_mask);
  655. struct cpumask __cpu_online_mask __read_mostly;
  656. EXPORT_SYMBOL(__cpu_online_mask);
  657. struct cpumask __cpu_present_mask __read_mostly;
  658. EXPORT_SYMBOL(__cpu_present_mask);
  659. struct cpumask __cpu_active_mask __read_mostly;
  660. EXPORT_SYMBOL(__cpu_active_mask);
  661. void init_cpu_present(const struct cpumask *src)
  662. {
  663. cpumask_copy(&__cpu_present_mask, src);
  664. }
  665. void init_cpu_possible(const struct cpumask *src)
  666. {
  667. cpumask_copy(&__cpu_possible_mask, src);
  668. }
  669. void init_cpu_online(const struct cpumask *src)
  670. {
  671. cpumask_copy(&__cpu_online_mask, src);
  672. }