cpu.c 19 KB

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