cpu.c 19 KB

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