cpu.c 19 KB

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