cpu.c 20 KB

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