cpu.c 19 KB

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