cpu.c 20 KB

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